All pastes #1403974 Raw Edit

Stuff

public text v1 · immutable
#1403974 ·published 2009-04-26 21:47 UTC
rendered paste body
<?php
	class httpwrapper {
		private $cookies;
		private $lastpage;
		private $html;
		private $headers;

		function __construct() {
			$this->useproxy = false;
			$this->cookies = array();
			$this->lastpage = NULL;
		}

		public function getLastPage() {
			return $this->lastpage;
		}

		public function clearCookies() {
			$this->cookies = array();
		}

		public function req($type, $url, $postdata = NULL, $referer = NULL, $follow_location = true, $return_headers = false) {
			if($referer == NULL) {
				if($this->lastpage != NULL)
					$r = "Referer: " . $this->lastpage . "\r\n";
			} else
				$r = "Referer: $referer\r\n";

			$url = str_replace("http://", "", $url);
			$host = $this->getHost($url);
			$path = $this->getPath($url);

			$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8";

			$type = strtoupper($type);
			if(!in_array($type,array("GET","POST"))) $type="GET";

			$f = fsockopen($host . ":80");
			if($f) {
				$h = "$type $path HTTP/1.1\r\n";
				$h .= "HOST: $host\r\n";
				$h .= "User-Agent: $agent\r\n";
				$h .= $this->cookieString();
				$h .= $r;
				if($type == "POST") {
					$length = strlen($postdata);
					$h .= "Content-Type: application/x-www-form-urlencoded\r\n";
					$h .= "Content-Length: $length\r\n";
				}
				$h .= "Connection: close\r\n\r\n";

				if($type == "POST")
					$h .= $postdata;

				fwrite($f,$h);
				while(!feof($f))
					$data.=fgets($f,256);
				list($headers,$html) = explode("\r\n\r\n", $data, 2);
				//list($rand,$html) = explode("\r\n", $html, 2);
				$this->html = $html;
				$this->lastpage = "http://".$url;
				$this->process_headers($headers,$url,$follow_location,$return_headers);
				return ($return_headers) ? $this->headers : $this->html;
			}
		}

		private function getHost($url) {
			if(strpos($url,"/") === FALSE)
				return $url;
			else
				return $this->gsb("<".$url,"<","/");
		}

		private function getPath($url) {
			if(strpos($url,"/") === FALSE)
				return "/";
			else
				return "/".$this->gsb($url.">","/",">");
		}

		private function process_headers($headers,$url,$follow,$return) {
			$this->headers = $headers;
			$lines = explode("\n", $headers);
			foreach($lines AS $line) {
				if(substr($line,0,10) == "Set-Cookie") {
					$cookie = $this->gsb($line, "Set-Cookie: ", ";");
					list($key,$val) = explode("=", $cookie, 2);
					$this->cookies[$key] = $val;
				} else if(substr($line,0,8) == "Location" && $follow == true) {
					$location = $this->gsb($line.">>", "Location: ", ">>");
					$this->req("GET", "http://".$this->getHost($url).$location, null, null, true, $return);
				}
			}
		}

		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 gsb($search, $start, $end) {
			list($a,$b) = explode($start, $search, 2);
			list($a,$b) = explode($end, $b, 2);
			return $a;
		}
	}
?>