<?
	require_once('control/include/user.php');
	require_once('control/include/mysql.php');
	require_once('control/include/functions.php');
	require_once('control/include/fb.php');
	
	ob_start();
	session_start();
	
	// SETUP MYSQL HANDLERS
	$mysqldb1 = new mysql($host, $user, $pswd, $db); $mysqldb1->connect(); $mysqldb1->select();
	$mysqldb2 = new mysql($host, $user, $pswd, $db); $mysqldb2->connect(); $mysqldb2->select();
	
	if ($_GET['logout']) {
	
		setcookie('client_id', '', 0, "/", "", 0);
		$goto = '<script language = "JavaScript">top.location = "http://'.$domain.'/";</script>';
		print $goto;
		
	} elseif (isset($_POST['username']) and isset($_POST['password'])) {
		
		$md5 = md5($_POST['username'].$_POST['password']);
		
		//fb($md5);
		
		if (strlen($_POST['md5']) == 32 and $_POST['md5'] == $md5) { // PREVENT HACKING BY SQL INJECTION (andreaginnproperties.co.za was hacked on 31 AUGUST 2009)
		$query = "SELECT * FROM client WHERE md5='".$md5."' AND status='enabled' LIMIT 1";
		//fb($query);
		}
		$mysqldb1->query($query);
		$client = $mysqldb1->fetchArray();
		
		if ($client['id'] != '') { 
			setcookie('client_id', $client['id'], 0, "/", "", 0);
			print 1;
		} else {
			print 0;
		}
		
	} elseif ($_POST['show']) {
?>
	<? if (!isset($_COOKIE['client_id'])) { ?>
    <div class="column_module_heading">Client Login</div>
    <div class="column_module_body">
	<form action="" method="post" name="login_form" id="login_form">
	<p>
    USERNAME<br/>
	<input type="text" name="username" id="username" />
    </p>
	<p>
    PASSWORD<br/>
	<input type="password" name="password" id="password" />
    </p>
    <input type="button" name="authenticate" id="authenticate" value="LOGIN" />
    </form>
    </div>
    <? } else { ?>
    <div class="column_module_heading">Client Login</div>
    <div class="column_module_body">
    <p>You are currently logged in.<br/>To logout <a href="/login.php?logout=1" target="_self">click here</a>.</p>
    </div>
    <? } ?>
<?
	}
?>
<?
	$mysqldb1->close();
	$mysqldb2->close();
?>