<?php

$HS_URL = ""; //Handshakes script URL
$HS_KEY = "secret"; //Handshakes secret key

function hs_call($action,$values){
	
	if($_GET["hs_gateway"] || $_POST["hs_gateway"])
		return false;
	
	$host = parse_url($GLOBALS["HS_URL"]);
	if(!eregi("/$",$host["path"]))
		$host["path"].= "/";
	
	$fp = @fsockopen($host["host"], 80, $errno, $errstr, 10);
	if(!$fp){
    	return false;
	}//if
	else {
		$post_string = "action=".$action."&key=".urlencode($GLOBALS["HS_KEY"]);
		while(list($key,$val) = each($values)){
			$post_string.= "&".$key."=".urlencode($val);
		}//while
    	fputs($fp, "POST ".$host["path"]."3rdparty/gateway.php HTTP/1.1"."\r\n"); 
        fputs($fp, "Host: ".$host["host"]."\r\n"); 
        fputs($fp, "Content-type: application/x-www-form-urlencoded"."\r\n");
        fputs($fp, "Content-length: ".strlen($post_string)."\r\n");
        fputs($fp, "Connection: close"."\r\n"."\r\n");
        fputs($fp, $post_string . "\r\n"."\r\n");
        stream_set_timeout($fp, 10);
        
        while (!feof($fp)) {
        	$line = fgets ($fp,1024);
        	if(eregi("<hs>",$line)){
        		$reply = $line;
        		break;
        	}//if
        }//while
        fclose ($fp);

        $parser = xml_parser_create();
        xml_parse_into_struct($parser,$reply,$xml_elements,$xml_index);
        foreach($xml_elements as $element){
        	$tag        = strtolower($element["tag"]);
        	$type       = $element["type"];
        	$value      = $element["value"];
        	$level      = $element["level"];
        	$attributes = $element["attributes"];

        	if($tag == "cookie" && $type == "complete"){
        		SetCookie($attributes["NAME"],$attributes["VALUE"],$attributes["EXPIRE"],$attributes["PATH"],$attributes["DOMAIN"]);
        	}//if

        }//foreach
	}//else
	
	return true;
	
}//hs_call

?>