Codice PHP:
class HTTP_Request
{
public function __construct()
{
}
public function Request( $host, $data )
{
if( !filter_var($host, FILTER_VALIDATE_URL) === false ){
$this->PostData = 'url='.$host.'&'.$data;
if( $this->Initialize() ) return $this->Send();
}else return $host.' is not a valid URL';
}
private function Initialize()
{
$this->Method = "POST";
if (empty($this->PostData))
{$this->Method = "GET";}
$this->Port = 80;
if (strtolower(substr($this->Host, 0, 5)) == "https")
{$this->Port = 443;}
$this->Host = explode("//", $this->Host, 2);
if (count($this->Host) < 2)
{$this->Host[1] = $this->Host[0];}
$this->Host = explode("/", $this->Host[1], 2);
if ($this->Port == 443)
{$this->SSLAdd = "ssl://";}
$this->Host[0] = explode(":", $this->Host[0]);
if (count($this->Host[0]) > 1)
{
$this->Port = $this->Host[0][1];
$this->Host[0] = $this->Host[0][0];
}
else
{$this->Host[0] = $this->Host[0][0];}
return true;
}
private function Send()
{
$this->attempt++;
$Socket = fsockopen($this->SSLAdd.$this->Host[0], $this->Port, $errno, $errstr, 4.9);
if ($Socket)
{
fputs($Socket, $this->Method." /".$this->Host[1]." HTTP/1.1\r\n".
"Host: ".$this->Host[0]."\r\n".
"Content-type: application/x-www-form-urlencoded\r\n".
"User-Agent: Opera/9.01 (Windows NT 5.1; U; en)\r\n".
"Accept-Language: de-DE,de;q=0.9,en;q=0.8\r\n".
"Accept-Charset: iso-8859-1, utf-8, utf-16, *;q=0.1\r\n".
"Content-length: ".strlen($this->PostData)."\r\n".
"Connection: close\r\n".
"\r\n".
$this->PostData);
$Tme = time();
while(!feof($Socket) && $Tme + 30 > time())
{$Res = $Res.fgets($Socket, 256);}
fclose($Socket);
}
if ( $errno == 0 )
{
$Res = explode("\r\n\r\n", $Res, 2);
return $Res[1];
}
else
{
if ( $this->attempt < 3 )
{
sleep(3);
$this->Send();
}
else return '{"errno":"'.$errno.'","errstr":"'.$errstr.'"}';
}
}
}
Codice PHP:
class User extends HTTP_Request
{
private $user;
public function __construct($value)
{
...
}
public function online()
{
$myfile = fopen("file.txt", "r");
$Host = base64_decode( fgets($myfile) );
fclose($myfile);
$json = base64_encode('{"request":["agent","'.$this->user['uuid'].'","online"]}');
$response = json_decode(parent::Request($Host, 'data='.$json), true);
return ($response['response']['agent']['online'] == 1) ? true : false ;
}
public function onlineMonitoring($value)
{
$myfile = fopen("file.txt", "r");
$Host = base64_decode( fgets($myfile) );
fclose($myfile);
if( !empty($value) ){
$value = ($value==1)?'add':'remove';
$json = base64_encode('{"request":["agent","'.$this->user['uuid'].'","list","'.$value.'"],"details":["'.$this->user['name'].'"]}');
}else{
$value = 'check';
$json = base64_encode('{"request":["agent","'.$this->user['uuid'].'","list","check"]}');
}
$response = json_decode(parent::Request($Host, 'data='.$json), true);
return ($response['response']['agent']['list'][$value] == 1) ? true : false ;
}
}
Codice PHP:
$user = new User($x);
$response['online'] = $user->online(); // OK
$response['monitoring'] = $user->onlineMonitoring(); // Can't do transparent proxying without a Host: header.