$v){ if ($k == 0) continue; $str2 = explode($e, @$str1[$k]); $str3[$k] = @$str2[0]; } return @$str3; } class httpSession { var $host; var $port; var $path; var $query; var $cookies; var $cookies_arr; var $text; var $status; var $referer; var $user_agent; var $follow_redirect; function __construct() { $this->follow_redirect = true; } function setCookie ($name, $val){ $this->cookies_arr[$name] = $val; $this->cookies = ''; foreach ($this->cookies_arr as $key => $val){ $this->cookies .= $key. '='. $val. ';'; } $this->cookies = substr($this->cookies, 0, -1); } function getCookie ($name){ return $this->cookies_arr[$name]; } function deleteCookies(){ empty($this->cookies_arr); $this->cookies = ''; } function setCookiesHeader($str){ $this->deleteCookies(); $this->cookies = $str; preg_match_all('/\s?(.*?)\=(.*?)(?:\;|\z)/', $str, $arr); foreach ($arr[1] as $key => $val) { $cookies_arr[$val] = $arr[2][$key]; } } function processResponse ($res){ $arr = explode("\r\n\r\n", $res); $head = $arr[0]; unset($arr[0]); $this->text = implode("\r\n\r\n", $arr); $c_arr = extractMulti('Set-Cookie: ', ';', $head); if(isset($c_arr)){ foreach ($c_arr as $key => $val){ $arr = explode('=', $val); $nm = $arr[0]; unset($arr[0]); $cv = implode('=', $arr); $this->setCookie($nm, $cv); } } $this->status = (int)extractSingle("HTTP/1.0 ", " ", $head); if (!isset($this->status) || !$this->status) $this->status = (int)extractSingle("HTTP/1.1 ", " ", $head); if (($this->status == 301 || $this->status == 302 || $this->status == 200) && isset($this->follow_redirect) && $this->follow_redirect) { $redir = extractSingle("location: ", "\r\n", strtolower($head)); if (isset($redir) && $redir!=''){ //$redir = substr($redir, 0, -1); if(strstr($redir, 'http://')) return $this->httpGet($redir); else return $this->httpGet('http://'. $this->host. $this->path. $redir); } } $this->referer = 'http://'. $this->host. $this->path. '?'. $this->query; return $this->text ; } function urlToHostPath($url){ $ar = parse_url($url); $this->host = @$ar['host']; $this->query = @$ar['query']; $this->path = @$ar['path']; $this->port = @$ar['port']; if (!isset($this->port) || $this->port=='') $this->port = '80'; } function httpGet($url){ $this->urlToHostPath($url); if (isset($this->query) && $this->query!=''){ $nPath = $this->path. "?". $this->query; }else{ $nPath = $this->path; } if ($nPath=='') $nPath = '/'; $pack = "GET $nPath HTTP/1.0\r\n". "Host: {$this->host}\r\n". "Cookie: {$this->cookies}\r\n". "User-Agent: {$this->user_agent}\r\n". "Referer: {$this->referer}\r\n". "Connection: Close\r\n". "\r\n"; $sock = @fsockopen ($this->host, $this->port, $errno, $errstr, 150); if(!$sock) return false; fwrite ($sock, $pack); $resp = ""; while (!feof($sock)){ $resp .= @fread($sock, 8192); } fclose($sock); return $this->processResponse($resp); } function httpPost($url, $data){ $this->urlToHostPath($url); if (isset($this->query) && $this->query!=''){ $nPath = $this->path. "?". $this->query; }else{ $nPath = $this->path; } if ($nPath=='') $nPath = '/'; $len = strlen($data); $pack = "POST $nPath HTTP/1.0\r\n". "Host: {$this->host}\r\n". "User-Agent: {$this->user_agent}\r\n". "Referer: {$this->referer}\r\n". "Connection: close\r\n". "Cookie: {$this->cookies}\r\n". "Content-Type: application/x-www-form-urlencoded\r\n". "Content-Length: $len\r\n". "\r\n$data"; $sock = @fsockopen($this->host, $this->port, $errno, $errstr, 150); if(!$sock) return false; fwrite ($sock, $pack); $resp = ""; while (!feof($sock)){ $resp .= @fread($sock, 8192); } fclose($sock); return $this->processResponse($resp); } } ?>