<?php
@ini_set('memory_limit', '-1');
@set_time_limit(0);
!defined("MONKEYMAO") && die("Accessed Defined!");
class httpget {
	var $homeurl;
	var $basepath;
	var $pages;
	function int($url) {
			if($url=="") return ;
			$urls = "";
			$urls = @parse_url($url);			
	    if(is_array($urls)){
		  	$this->homeurl = $urls["host"];
		  	$this->basepath = $this->homeurl.$urls["path"];
		  	$this->basepath = preg_replace("/\/([^\/]*)\.(.*)$/","/",$this->basepath);
		  	$this->basepath = preg_replace("/\/$/","",$this->basepath);
			$this->pages = $urls["path"];
		 }		
	}
	function openurl($url,$f=1,$c=2,$r=0,$a=0,$cf=0,$post=""){
		global $oldheader;
		$this -> int($url);
		$url = $this->homeurl;
		$ip = gethostbyname($this->homeurl);			
		$page = $this->pages ? $this->pages : '/';
		$open = fsockopen($ip, 80, $errno, $errstr, 60);
		$send = $post ? "POST ".$this->pages." HTTP/1.0\r\n" : "GET ".$this->pages." HTTP/1.0\r\n";
		$send .= "Host: ".$this->homeurl."\r\n";
		if ($r){
			$send .= "Referer: $r\r\n";
		}else{
			if ($_SERVER['HTTP_REFERER']){
				$send .= "Referer: {$_SERVER['HTTP_REFERER']}\r\n";
			}
		}
		if ($cf){
			if (@file_exists($cf)){
				$cookie = urldecode(@file_get_contents($cf));
				if ($cookie){
					$send .= "Cookie: $cookie\r\n";
					$add = @fopen($cf,'w');
					fwrite($add,"");
					fclose($add);
				}
			}
		}
		$send .= "Accept-Language: en-us, en;q=0.50\r\n";
		$send .=$a ? "User-Agent: $a\r\n" : "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n";

		if ($post){
			$send .= "Content-Type: application/x-www-form-urlencoded\r\n";  
			$send .= "Content-Length: " .strlen($post) ."\r\n\r\n";
			$send .= $post;
		}else{
			$send .= "Connection: Close\r\n\r\n";
		}
		fputs($open, $send);
		while (!feof($open)) {
			$return .= fgets($open, 4096);
		}
		fclose($open);
		$return = @explode("\r\n\r\n",$return,2);
		$header = $return[0];
		if ($cf){
			if (preg_match("/Set\-Cookie\: /i","$header")){
				$cookie = @explode("Set-Cookie: ",$header,2);
				$cookie = $cookie[1];
				$cookie = explode("\r",$cookie);
				$cookie = $cookie[0];
				$cookie = str_replace("path=/","",$cookie[0]);
				$add = @fopen($cf,'a');
				fwrite($add,$cookie,strlen($read));
				fclose($add);
			}
		}
		if ($oldheader){
			$header = "$oldheader<br /><br />\n$header";
		}
		$header = str_replace("\n","<br />",$header);
		$body = $return[1] ? $return[1] : '';
		switch($c) {
			case 1:
				$return = $header;
			break;			
			case 2:
				$return = $body ? $body : $header;
			break;
			case 3:
				$return = "$header$body";
			break;
		}


		if ($f){
			if (preg_match("/Location\:/","$header")){
				$url = @explode("Location: ",$header);
				$url = $url[1];
				$url = @explode("\r",$url);
				$url = $url[0];
				$oldheader = str_replace("\r\n\r\n","",$header);
				$l = "&#76&#111&#99&#97&#116&#105&#111&#110&#58";
				$oldheader = str_replace("Location:",$l,$oldheader);
				return $this->openurl($url,$f,$c,$r,$a,$cf,$pd);
			}else{
				return $return;
			}
		}else{
			return $return;
		}
	}
	function formaturl($surl)
	  {
		$i = 0;
		$dstr = "";
		$pstr = "";
		$okurl = "";
		$pathStep = 0;
		$surl = trim($surl);
		if($surl=="") return "";
		$pos = strpos($surl,"#");
		if($pos>0) $surl = substr($surl,0,$pos);
		if($surl[0]=="/"){
			$okurl = "http://".$this->homeurl.$surl;
		}
		else if($surl[0]==".")
		{
		  if(strlen($surl)<=1) return "";
		  else if($surl[1]=="/")
		  {
			$okurl = "http://".$this->basepath."/".substr($surl,2,strlen($surl)-2);
			}
		  else{
			$urls = explode("/",$surl);
			foreach($urls as $u){
			  if($u=="..") $pathStep++;
			  else if($i<count($urls)-1) $dstr .= $urls[$i]."/";
			  else $dstr .= $urls[$i];
			  $i++;
			}
			$urls = explode("/",$this->basepath);
			if(count($urls) <= $pathStep)
				return "";
			else{
			  $pstr = "http://";
			  for($i=0;$i<count($urls)-$pathStep;$i++)
			  { $pstr .= $urls[$i]."/"; }
			  $okurl = $pstr.$dstr;
			}
		  }
		}
		else
		{
		  if(strlen($surl)<7)
			$okurl = "http://".$this->basepath."/".$surl;
		  else if(strtolower(substr($surl,0,7))=="http://")
			$okurl = $surl;
		  else
			$okurl = "http://".$this->basepath."/".$surl;
		}
		$okurl = eregi_replace("^(http://)","",$okurl);
		$okurl = eregi_replace("/{1,}","/",$okurl);
		return "http://".$okurl;
	  }
}

//$get = new httpget;
//$url = "http://www.mmwor.cn/admin/xinggan/200829214712.htm";

//$html = $get -> openurl($url);

//$img_array = array();
//preg_match_all("/(<img[^>]+src=\"([^\"]+)\"[^>]*>)|(<img[^>]+src='([^']+)'[^>]*>)/i",$html,$img_array);
//foreach($img_array[2] as $key => $value){
//	echo $get -> fillurl($value)."<br>";
//} 
?> 