<?php
class Request {
	public function hit($url) {
		$dynamic_source = fopen($url, 'r');  
		if (!$dynamic_source) {
			return "<strong>Unable to load $sourcepage
			- Static page! Update Failed!</strong>";
		}
		
		$htmldata = '';
		while (!feof($dynamic_source)) {
		  set_time_limit(120);
		  $htmldata .= fread($dynamic_source, 8192);
		}
		fclose($dynamic_source);
		return $htmldata;
	}
	public function get($url, $referer) {
		$url = parse_url($url);
	
		if ($url['scheme'] != 'http') { 
			die('Only HTTP request are supported !');
		}
	
		// extract host and path:
		$host = $url['host'];
		$path = $url['path'];
	
		// open a socket connection on port 80
		$fp = fsockopen($host, 80);
		if(!$fp) return array('','');
	
		// send the request headers:
		fputs($fp, "GET $path HTTP/1.1\r\n");
		fputs($fp, "Host: $host\r\n");
		fputs($fp, "Referer: $referer\r\n");
		fputs($fp, "Connection: close\r\n\r\n");
		fputs($fp, $data);
		$result = '';
	
		while(!feof($fp)) {
			set_time_limit(120);
			// receive the results of the request
			$result .= fgets($fp, 128);
		}
	
		// close the socket connection:
		fclose($fp);
	
		// split the result header from the content
		$result = explode("\r\n\r\n", $result, 2);
		$header = isset($result[0]) ? $result[0] : '';
		$content = isset($result[1]) ? $result[1] : '';
	
		// return as array:
		return array($header, $content);
	}
	public function post($url, $referer, $_data) {
		// convert variables array to string:
		$data = array();    
	
		while(list($n,$v) = each($_data)){
			$data[] = urlencode($n)."=".urlencode($v);
		}    
		
		$data = implode('&', $data);
	
		// format --> test1=a&test2=b etc.
		// parse the given URL
		$url = parse_url($url);
	
		if ($url['scheme'] != 'http') { 
			die('Only HTTP request are supported !');
		}
	
		// extract host and path:
		$host = $url['host'];
		$path = $url['path'];
	
		// open a socket connection on port 80
		$fp = fsockopen($host, 80);
		if(!$fp) return array('','');
		
		// send the request headers:
		fputs($fp, "POST $path HTTP/1.1\r\n");
		fputs($fp, "Host: $host\r\n");
		fputs($fp, "Referer: $referer\r\n");
		fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
		fputs($fp, "Content-length: ". strlen($data) ."\r\n");
		fputs($fp, "Connection: close\r\n\r\n");
		fputs($fp, $data);
		$result = '';
	
		while(!feof($fp)) {
			set_time_limit(120);
			// receive the results of the request
			$result .= fgets($fp, 128);
		}
	
		// close the socket connection:
		fclose($fp);
	
		// split the result header from the content
		$result = explode("\r\n\r\n", $result, 2);
		$header = isset($result[0]) ? $result[0] : '';
		$content = isset($result[1]) ? $result[1] : '';
	
		// return as array:
		return array($header, $content);
	}
	public function web_reg_save_param($lbound,$rbound,$content) {
		$p1=strpos($content,$lbound);
		if($p1===false) return "";
		$p1+=strlen($lbound);
		
		$p2=strpos($content,$rbound,intval($p1));
		if($p2===false) return "";
		return substr($content,$p1,$p2-$p1);
	}
	public function web_reg_save_param1($lbound,$rbound,$content) {
		$p2=0; $p1=0;
		$list=array();
		while($p1!==false) {
			$p1=strpos($content,$lbound,intval($p2));
			if($p1===false) return $list;
			$p1+=strlen($lbound);
			
			$p2=strpos($content,$rbound,intval($p1));
			if($p2===false) return $list;
			$x=substr($content,$p1,$p2-$p1);
			if(strpos($x,"'")===false && strpos($x,'"')===false && strlen($x)<512) {
				echo "<br/> - ".$x;
				$list[]=$x;
			}
		}
		return $list;
	}
}
?>