<?php

		// simple version of 'libary.php' for fast use of the chatting functions

	
	function start_code( $first=false, $db="", $is_online_refresh="false" )
	{

			/* set up paths */

		$domain = val( $_SERVER, "HTTP_HOST" );

		session_start();


		$_SESSION['domain'] = $domain;

		$_SESSION['pathroot'] = "/home/academi1/m-m-ventures.com/chatcity";


			// this variable is used throughout the code for this site

		$_SESSION['http_protocol'] = "http";



			/* set up variables */

		$database = "academi1_chatcity";

        if ($db != "")
		    $database = $db;
		    
        $_SESSION['database'] = $database;
		    

		$user = val( $_GET, "user" );


			/* remember user across pages */

			/* set config options */

		ini_set( "error_log", $_SESSION['pathroot']."/error_log.txt" );

		ini_set( "display_errors", "on" );
		ini_set( "display_startup_errors", "on" );

        error_reporting( E_ALL & ~E_DEPRECATED );
        
		ini_set( "gc_maxlifetime", 3*60*60 );		/* set session timeout to 3 hours */


		ini_set( "date.timezone", "Australia/Melbourne" );		

		$cxn = db_login( $database );

		return ($cxn);
	}	


	
		/* return an element in an array or an empty string if it doesn't exist */

	function val( $xarray, $variable )
	{
		if (array_key_exists( $variable, $xarray ))
			$value = $xarray[ $variable ];
		else
			$value = "";

		return ($value);
	}


    function run_query( $queryx, &$cxn )
	{
	    
        $result = $cxn->query( $queryx );

		if ($result == false)
			crash( "<span class='gblack'>Internal error: ".($cxn->errno)." ".($cxn->error)."<br><br>Query: ".$queryx."<br><br>Script terminated</span>", $cxn );

		return $result;
	}



	function db_login( $database )
	{
			// azure login credentials
		
//		$host="mlmlocalhost.mysql.database.azure.com";
//		$user="mlm1@mlmlocalhost";
//		$password="Tree$%12";
		
		$host="localhost";
		$user="academi1_mlm1";
		$password="plastic1";


        $cxn = new mysqli( $host, $user, $password, $database );

		if ($cxn->connect_errno)
		{
			echo "The system is currently unavailable.<br><br>Please try again in half an hour.";
			
			echo "Error number: ".$cxn->connect_errno."<br>";
			echo "Error text: ".$cxn->connect_error."<br>";

			exit();
		}


		return $cxn;
	}



	function query_num_rows($result)
	{
        return ($result->num_rows);		
	}



	function get_row( $result )
	{
        $data = $result->fetch_assoc();

		return $data;
	}

	
	
		/* return TRUE if a query returned at least one record */

	function query_has_rows( $query, $cxn )
	{
		$result = run_query( $query, $cxn );

		if (query_num_rows($result) != 0)
			return (true);
		else
			return (false);
	}
	

	function crash( $msg, $cxn )
	{
		$page = val( $_SERVER, "PHP_SELF" );
		
		$text = "Page: ".$page."  error: ".$msg;
				
//		$ip_address = get_ip_address();

		$ip_address = "";
		
		$queryx = "INSERT INTO sys_log (event, event_datetime, text) VALUES ('crash small library', '".date("Y-m-d H:i:s")."', '".prep_for_sql( $ip_address." ".$text )."')";

        $result = $cxn->query( $queryx );


//		echo red( "<br>The function that you have requested is not available. Please go to the home page and try again.<br><br>If possible, try using a different browser. The site works well with the latest version of Chrome." )."<br><br><br>";
//		
//		echo show_home_link();
//

		exit();
	}
	
	
		/* fix up a text string for use in an sql query */

	function prep_for_sql( $text )
	{
		$text = str_replace( "'", "''", $text );

		$text = str_replace( "\\", "", $text );

		return ($text);
	}	
?>