<?php

function phpbb_register($values){
	
	$host_details = parse_url($values["url"]);
	$host = $host_details["host"];
	$phpbb_path = $host_details["path"];
	if(!ereg("/$",$phpbb_path))
		$phpbb_path.= "/";
		
	$cookie_file = phpbb_cookie_file();	
	@unlink($cookie_file);
	
	$result = phpbb_curl("http://".$host.$phpbb_path."profile.php",$cookie_file,"mode=register&agreed=true");
	preg_match("/name=\"sid\" value=\"([^\"]+)\"/",$result,$match);
	$sid = $match[1];	
		
	$phpbb_values = array(
		"mode" => "register",
		"agreed" => "true",
		"coppa" => "0",			
		"username" => $values["username"],
		"email" => $values["email"],
		"new_password" => $values["password"],
		"password_confirm" => $values["password"],
		"viewemail" => "0",
		"hideonline" => "0",
		"notifyreply" => "1",
		"notifypm" => "1",
		"popup_pm" => "1",
		"attachsig" => "1",
		"allowbbcode" => "1",
		"allowhtml" => "1",
		"allowsmilies" => "1",
		"language" => "english",
		"style" => "1",
		"timezone" => "0",
		"dateformat" => "D M d, Y g:i a",
		"icq" => "",
		"aim" => "",
		"msn" => "",
		"yim" => "",
		"website" => "",
		"location" => "",
		"occupation" => "",
		"interests" => "",
		"signature" => "",
		"submit" => "1",
		"sid" => $sid
	);
		
	while(list($key,$val) = each($phpbb_values)){
		$post_string.= $key."=".urlencode($val)."&";
	}//while
	$post_string = rtrim($post_string,"&");
	
	$result = phpbb_curl("http://".$host.$phpbb_path."profile.php",$cookie_file,$post_string);
    
    return true;
	
}//phpbb_register

function phpbb_login($values){
	
	$host_details = parse_url($values["url"]);
	$host = $host_details["host"];
	$cookie_url = eregi_replace("^www","",$host);
	$phpbb_path = $host_details["path"];
	if(!ereg("/$",$phpbb_path))
		$phpbb_path.= "/";

	$phpbb_values = array(
		"username" => $values["username"],
		"password" => $values["password"],
		"redirect" => "",
		"submit" => "1",
		"login" => "1"
	);
	
	if($values["remember"])
		$phpbb_values["autologin"] = "1";
			
	while(list($key,$val) = each($phpbb_values)){
		$post_string.= $key."=".urlencode($val)."&";
	}//while
	$post_string = rtrim($post_string,"&");
	
	$cookie_file = phpbb_cookie_file();	
	@unlink($cookie_file);
	$line = phpbb_curl("http://".$host.$phpbb_path."login.php",$cookie_file,$post_string,"",1);
	
	$line = ereg_replace("\r","",$line);
	$lines = explode("\n",$line);
	$skip[0] = 1;
	$skip[1] = 1;
	foreach($lines as $header){
		if(eregi("^Set-Cookie",$header)){
			preg_match_all("/^Set-Cookie: ([^=]+)=([^;]+);/",$header,$matches);
			$name   = $matches[1][0];
			$value  = urldecode($matches[2][0]);
			preg_match_all("/expires=([^;]+);/",$header,$matches);
			$expire_string = $matches[1][0];
			if($expire_string)
				$expire = strtotime($expire_string);
			else
				$expire = 0;
			if($name == "phpbb2mysql_data" && $skip[0]){
				$skip[0] = 0;
			}//if
			elseif($name == "phpbb2mysql_sid" && $skip[1]){
				$skip[1] = 0;
			}//elseif
			else{			
				load_cookie($name,$value,$expire,$cookie_url);
			}//else
		}//if
		elseif(eregi("^Location",$header)){
			$link = ereg_replace("Location: ","",$header);
			$url = parse_url($link);    				
			parse_str($url["query"],$phpbb_answer);
			$session_id = $phpbb_answer["sid"];
			load_cookie("phpbb_sid",$phpbb_answer["sid"],0);
		}//elseif
	}//foreach
	
	$client_ip = ( !empty($HTTP_SERVER_VARS['REMOTE_ADDR']) ) ? $HTTP_SERVER_VARS['REMOTE_ADDR'] : ( ( !empty($HTTP_ENV_VARS['REMOTE_ADDR']) ) ? $HTTP_ENV_VARS['REMOTE_ADDR'] : getenv('REMOTE_ADDR') );
	$user_ip = phpbb_encode_ip($client_ip);
	
	$conn = @mysql_connect($values["dbhost"],$values["dbuser"],$values["dbpass"],1);
	@mysql_select_db($values["dbname"],$conn);
	
	$sql_query = "UPDATE ".$values["dbprefix"]."sessions SET session_ip = '".$user_ip."'
				  WHERE session_id='".$session_id."'";
	@mysql_query($sql_query,$conn);
	@mysql_close($conn);
	
	return true;
		
}//phpbb_login

function phpbb_logout($values){
	
	$host_details = parse_url($values["url"]);
	$host = $host_details["host"];
	$phpbb_path = $host_details["path"];
	if(!ereg("/$",$phpbb_path))
		$phpbb_path.= "/";

	$phpbb_values = array(		
		"submit" => "1",
		"logout" => "true",
		"sid" => $GLOBALS["Get"]->cookie("phpbb_sid")
	);
			
	while(list($key,$val) = each($phpbb_values)){
		$post_string.= $key."=".urlencode($val)."&";
	}//while
	$post_string = rtrim($post_string,"&");
			
	$fp = fsockopen ($host, 80, $errno, $errstr);
	if($fp){
    fputs($fp, "POST ".$phpbb_path."login.php HTTP/1.1\r\n"); 
    fputs($fp, "Host: ".$host."\r\n"); 
	fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); 
	fputs($fp, "Content-length: ".strlen($post_string)."\r\n");
	while(list($key,$val) = each($_COOKIE)){
		$cookies.= $key."=".urlencode($val)."; ";
	}//while
	$cookies = rtrim($cookies,'; ');
	fputs($fp, "Cookie: ".$cookies."\n");
	fputs($fp, "Connection: close\r\n\r\n"); 
	fputs($fp, $post_string."\r\n\r\n");
	$line = "";
	while (!feof($fp)) {
		$line.= fgets ($fp,1024);
	}//while
	fclose ($fp);
	}//if
    		
	$line = ereg_replace("\r","",$line);
	$lines = explode("\n",$line);
	foreach($lines as $header){
		if(eregi("^Set-Cookie",$header)){
			preg_match_all("/^Set-Cookie: ([^=]+)=([^;]+);/",$header,$matches);
			$name   = $matches[1][0];
			$value  = urldecode($matches[2][0]);
			preg_match_all("/expires=([^;]+);/",$header,$matches);
			$expire_string = $matches[1][0];
			if($expire_string)
				$expire = strtotime($expire_string);
			else
				$expire = 0;			
			load_cookie($name,$value,$expire);
		}//if
	}//foreach
	
	load_cookie("phpbb_sid","",time()-10);
	
	return true;
	
}//phpbb_logout

function phpbb_link($values){
	
	return rtrim($values["url"],"/")."/index.php?sid=".$GLOBALS["Get"]->cookie("phpbb_sid");
	
}//phpbb_link

function phpbb_encode_ip($dotquad_ip){
	
	$ip_sep = explode('.', $dotquad_ip);
	return sprintf('%02x%02x%02x%02x', $ip_sep[0], $ip_sep[1], $ip_sep[2], $ip_sep[3]);
	
}//phpbb_encode_ip

function phpbb_curl($url,$cookie_path="",$postfileds="",$referrer="",$header="",$follow=1){
	
	$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    if($follow)
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

	if($referrer!="")	
		curl_setopt($ch, CURLOPT_REFERER, $referrer);
		
	if($cookie_path!=""){
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
		curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
	}//if
	
	if($postfileds!=""){
		curl_setopt($ch, CURLOPT_POST, 1); 
		curl_setopt($ch, CURLOPT_POSTFIELDS,$postfileds); 
	}//if
	
	if($header!="")
		curl_setopt($ch, CURLOPT_HEADER, 1);

	$result = curl_exec ($ch);
	curl_close ($ch);
	
	return $result;
	
}//phpbb_curl

function phpbb_cookie_file(){
	
	mt_srand((double)microtime()*100000);
	$fpath = DOC_ROOT."/temp";
	$fname = hs_substr(md5(time().mt_rand(0,100)),0,15);
	
	$cookie_file = $fpath."/".$fname.".txt";
	
	return $cookie_file;
	
}//phpbb_cookie_file

?>