<?php

						/*************************************\

						    Handshakes Professional (3.2)
						            by DZOIC LTD
						       Copyright(c) 2005-2006
						        http://www.dzoic.com

						\*************************************/

	$SECRET_KEY = "";    //please, input your secret key here

	$gateway = 1;
	$output  = "";

	set_time_limit(0);
	ignore_user_abort(1);

	//load global configuration
	include("../includes/config/config.inc.php");

	//load libraries
	include(DOC_ROOT."/libraries.php");

	$keypost = $Get->val("key");
	if(!$SECRET_KEY || $keypost != md5($SECRET_KEY))
		exit;

	$action = $Get->val("action");
	switch($action){
		case 'register':
			gateway_register();
			break;
		case 'login':
			gateway_login();
			break;
		case 'logout':
			gateway_logout();
			break;
		case 'account':
			gateway_account();
			break;
		case 'delete':
			gateway_delete();
			break;
	}//switch

function gateway_register(){

	global $output;

	$vals = array("username","email","password","password_md5","fname","lname");
	$values = $GLOBALS["Get"]->arr($vals,1);

	$md5 = 0;
	if($values["password_md5"]){
		$values["password"] = $values["password_md5"];
		$md5 = 1;
	}//if

	$GLOBALS["Member"]->values = $values;
	$mem_id = $GLOBALS["Member"]->create($md5);

	if($mem_id && $GLOBALS["settings"]["default_friend"]){
		$GLOBALS["Network"]->add_friend($mem_id,$GLOBALS["settings"]["default_friend"]);
	}//if

	$sql_query = "UPDATE members SET verified='1' WHERE mem_id=".$mem_id;
	$GLOBALS["DB"]->execute($sql_query);

	$output = "<hs></hs>";

}//gateway_register

function gateway_login(){

	global $output;

	$vals = array("username","email","password","password_md5");
	$values = $GLOBALS["Get"]->arr($vals,1);
	extract($values);

	$md5 = 0;
	if($password_md5){
		$password = $password_md5;
		$md5 = 1;
	}//if

	$GLOBALS["Member"]->login($email,$username,$password,0,$md5);
	$output = "<hs>".$output."</hs>";

}//gateway_login

function gateway_logout(){

	global $output;

	$vals = array("username","email","password");
	$values = $GLOBALS["Get"]->arr($vals,1);
	extract($values);

	$GLOBALS["Member"]->logout();
	$output = "<hs>".$output."</hs>";

}//gateway_logout

function gateway_account(){

	global $output;

	$vals = array("username","email","password");
	$values = $GLOBALS["Get"]->arr($vals,1);

	$GLOBALS["DB"]->clean();

	if($values["email"]){
		$sql_query = "UPDATE members SET email=':email:' WHERE username=':username:'";
		$GLOBALS["DB"]->values = $values;
		$GLOBALS["DB"]->execute($sql_query);
	}//if

	$GLOBALS["DB"]->clean();

	if($values["password"]){
		$sql_query = "UPDATE members SET password=MD5(':password:') WHERE username=':username:'";
		$GLOBALS["DB"]->values = $values;
		$GLOBALS["DB"]->execute($sql_query);
	}//if

	extract($values);
	$GLOBALS["Member"]->login($email,$username,$password);

	$output = "<hs>".$output."</hs>";

}//gateway_account

function gateway_delete(){

	global $output;

	$username = $GLOBALS["Get"]->val("username");
	$sql_query = "SELECT mem_id FROM members WHERE username=':username:'";
	$GLOBALS["DB"]->values = array("username"=>$username);
	$mem_id = $GLOBALS["DB"]->single($sql_query);

	include_once(DOC_ROOT."/functions/delete_functions.php");
	delete_member($mem_id);

	$output = "<hs></hs>";

}//gateway_delete

	$encoding = extract_encoding("html");

	Header("Content-Type:text/xml; charset=".$encoding);
	print "<?xml version=\"1.0\" encoding=\"".$encoding."\"?>\n";

	echo $output;

?>