PHP Code:
class zWrapper
{
private $cookies, $LastPage, $useProxy;
public $proxyHost, $proxyPort;
public function Request($method, $url, $referer = '', $showHeaders = false)
{
$showHeaders = (bool)$showHeaders;
$file = '';
$host = '';
$data = '';
if (strcasecmp($method, 'GETPOST') == 0) {
if (($x = strpos($url, '~~')) !== false) {
$data = substr($url, $x + 2);
$url = substr($url, 0, $x);
}
} else
if (strcasecmp($method, 'POST') == 0) {
if (($x = strpos($url, '?')) !== false) {
$data = substr($url, $x + 1);
$url = substr($url, 0, $x);
}
}
if (strcasecmp(substr($url, 0, 7), 'http://') == 0) {
$host = substr($url, 7);
}
if (($x = strpos($host, '/')) !== false) {
$file = substr($host, $x);
$host = substr($host, 0, $x);
} else {
$file = '/';
}
if (isset($referer)) {
$referer = 'Referer: ' . $referer;
} else
if (isset($this->LastPage)) {
$referer = 'Referer: ' . $this->LastPage;
}
$this->LastPage = $url;
$fp = null;
if ($this->useProxy)
$fp = fsockopen($this->proxyHost, $this->proxyPort) or die($errstr . ' (' . $errno .
')');
else
$fp = fsockopen($host, 80) or die($errstr . ' (' . $errno . ')');
if ($fp) {
fwrite($fp, $this->headers($method, $file, $host, $referer, $data));
$r = '';
$s = microtime(true);
while (!feof($fp) && (microtime(true) - $s < 10000))
$r .= fgets($fp, 256);
fclose($fp);
$temp = explode("\r\n\r\n", $r, 2);
$this->process_headers($temp[0]);
if (strpos($temp[0], 'Content-Encoding: gzip') !== false) {
$temp[1] = $this->el_zip_gzDecode($temp[1]);
return $showHeaders ? implode("\r\n\r\n", $temp) : empty($temp[1]) ? implode("\r\n\r\n",
$temp) : $temp[1];
} else {
return $showHeaders ? $r : empty($temp[1]) ? implode("\r\n\r\n", $temp) : $temp[1];
}
}
}
public function clearCookies()
{
$this->cookies = array();
}
public function getCookies()
{
return $this->cookies;
}
public function setProxy($host, $port)
{
$this->proxyHost = $host;
$this->proxyPort = $port;
$this->useProxy = true;
}
public function offProxy()
{
$this->useProxy = false;
}
public function setCookies($cookies)
{
$this->cookies = $cookies;
}
public function __construct()
{
date_default_timezone_set('America/Los_Angeles');
$this->cookies = array();
if (!function_exists('stripos')) {
function stripos($haystack, $needle)
{
return strpos($haystack, stristr($haystack, $needle));
}
}
}
private function el_zip_gzDecode($data)
{
$gzMagic = sprintf("%X%X", ord(substr($data, 1, 1)), ord(substr($data, 0, 1)));
if ($gzMagic != "8B1F") {
$unpacked = @gzinflate($data);
return $unpacked;
}
$flags = ord(substr($data, 3, 1));
$headerlen = 10;
if ($flags & 4) {
$extralen = unpack('v', substr($data, 10, 2));
$extralen = $extralen[1];
$headerlen += 2 + $extralen;
}
if ($flags & 2) // CRC at end of header
$headerlen += 2;
return gzinflate(substr($data, $headerlen, strlen($data) - $headerlen - 8));
}
private function cookieString()
{
if (count($this->cookies) > 0) {
$string = 'Cookie:';
foreach ($this->cookies as $k => $v)
$string .= " $k=$v;";
return $string . "\r\n";
}
}
private function process_headers($headers)
{
$size = count($lines = explode("\n", $headers));
$x = -1;
while (++$x < $size) {
if (preg_match('/^Set-Cookie: ([^;]+);/', $lines[$x], $s)) {
list($key, $val) = explode("=", $s[1], 2);
$this->cookies[$key] = $val;
}
}
}
private function headers($method, $file, $host, $referer = "", $data = "")
{
$isPost = stripos($method, 'POST') !== false;
$h = ($isPost ? 'POST ' : 'GET ') . (@substr($file, 0, 1) == '/' ? $file : '/' .
$file) . " HTTP/1.1\r\n";
$h .= 'Host: ' . $host . "\r\n";
$h .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
$h .= "Accept-Language: en-us,en;q=0.5\r\n";
$h .= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
$h .= "Accept-Encoding: gzip,deflate\r\n";
if (!empty($referer))
$h .= $referer . "\r\n";
$h .= $this->cookieString();
$h .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13\r\n";
if ($isPost) {
$h .= "Content-Type: application/x-www-form-urlencoded\r\n";
$h .= 'Content-Length: ' . strlen($data) . "\r\n";
}
$h .= "Keep-Alive: 115\r\n";
$h .= "Connection: keep-alive\r\n\r\n";
#$h .= "Connection: close\r\n\r\n";
if ($isPost)
$h .= $data;
return $h;
}
}