Location: PHPKode > scripts > dBrowser > dbrowser/dBrowser.php
<?
/**
	Classe dBrowser
		Autor:       Alexandre Tedeschi (d)
		E-Mail(MSN): alexandrebr at gmail.com
		ICQ#:        34715587
		Londrina/PR - Brasil
	Descrição:
		Robô navegador, que carrega consigo cookies,
		e permite enviar valores GET e POST através das páginas.
	Versão:
		1.0
	To Do:
		* Possibilitar enviar um arquivo através de POST
		* Traçar todos os links externos recebidos ao acessar um site
		* Aceitar conteúdo GZipped
		* Ativar suporte a diferentes protocolos, inclusive HTTPS
		* Tratamento de excessões e erros
	Comentários:
		Classe poderosa, que pode servir de base para todo o tipo
		de aplicação, automatização e/ou atualização automática de conteúdo
**/
class dBrowser{
	private $versao = "1.0";
	private $domain,$port,$path;
	private $host,$agent,$referer,$cookie,$post,$get;
	public  $ret; // ["status"] ["header"] ["body"]
	public  $debug = false;
	
	Function __construct(){
		$this->agent   = $this->getVersao();
		$this->referer = "";
	}
	
	Function getVersao(){
		return "dPHP-Robot $this->versao (alexandrebr#gmail.com) Alexandre Tedeschi / Brazil";
	}
	
	Function getResponse($kind = "ALL"){
		// O uso desta função é opcional, tendo em vista que $RET é public
		if($kind == "ALL")
			return $this->ret;
		elseif($kind == "header" || $kind == "status" || $kind == "body")
			return $this->ret[$kind];
		else
			return false;
	}
	
	Function setReferer($referer){
		$this->referer = $referer;
	}
	
	Function setAgent($agent){
		$this->agent = $agent;
	}
	
	Function setHost($host){
		$this->host = $host;
	}

	Function addCookieVar($name, $value){
		$this->cookie[$name] = $value;
	}
	
	Function addPostVar($name, $value){
		$this->post[$name] = $value;
	}
	
	Function addGetVar($name, $value){
		$this->get[$name] = $value;
	}
	
	Function browse($address){
		if($address[0] == "/" && $this->domain){
			$this->path = $address;
		}else{
			$url = parse_url($address);
			$this->domain = $this->host = $url["host"];
			$this->port   = isset($url["port"])?$url["port"]:80;
			$this->path   = isset($url["path"])?$url["path"]:"/";
		}
	}
	
	Function load(){
		$this->c = $c = fsockopen($this->domain, $this->port, $errno, $errstr);
		if(!$c){
			$this->error($errno, $errstr);
			return false;
		}
		
		$getorpost = sizeof($this->post)?"POST":"GET";
		
		$cookievar = ""; if(sizeof($this->cookie)) foreach($this->cookie as $id=>$value) $cookievar .= urlencode($id)."=".urlencode($value).";";
		$postvar   = ""; if(sizeof($this->post  )) foreach($this->post   as $id=>$value) $postvar   .= urlencode($id)."=".urlencode($value)."&";
		$getvar    = ""; if(sizeof($this->get   )) foreach($this->get    as $id=>$value) $getvar    .= urlencode($id)."=".urlencode($value)."&";
		
		$cookievar = substr($cookievar, 0, -1);
		$postvar   = substr($postvar, 0, -1);
		$getvar    = $getvar?"?".substr($getvar, -1):'';
		
		$r  = "$getorpost $this->path$getvar HTTP/1.1\r\n";
		$r .= "User-Agent: $this->agent\r\n";
		$r .= "Connection: close\r\n";
		if($this->host)
			$r .= "Host: $this->host\r\n";
		$r .= "Referer: $this->referer\r\n";
		$r .= $cookievar?"Cookie: $cookievar\r\n":"";
		$r .= "Content-type: application/x-www-form-urlencoded\r\n";
		$r .= "Content-Length: " . strlen($postvar) . "\r\n";
		if($postvar){
			$r .= "\r\n";
			$r .= "$postvar\r\n";
		}
		$r .= "\r\n";
		if($this->debug){
			echo "\n===== REQUEST =====\n";
			echo $r;
			echo "\n===== END OF =====\n";
		}
		fputs($c, $r);
		$ret = "";
		while(!feof($c))
			$ret .= fread($c, 1024);
		fclose($c);

		$this->adjustMe();
		$this->parseReceived($ret);
	}
	
	Function adjustMe(){
		$this->get  = Array();
		$this->post = Array();
		$this->setReferer("http://" . $this->domain . $this->path);
	}
	
	Function parseReceived($data){
		$logto = "header";
		$toget = 0;
		$strlog = "";
		$header = Array();
		
		$data = explode("\n", $data);
		for($x = 0; $x < sizeof($data); $x++){
			if($x == 0){
				$parts  = explode(" ", trim($data[$x]));
				$status = Array("version"=>$parts[0], "status"=>$parts[1], "string"=>join(" ", array_slice($parts, 2)));
				continue;
			}
			if($logto == "header"){
				$data[$x] = trim($data[$x]);
				if($data[$x] == ""){
					$logto = "body";
					$body  = join("\n", array_slice($data, $x+1));
					continue;
				}
				$parts = explode(": ", $data[$x]);
//				echo "Linha[$x]: $data[$x]\n";
				$header[$parts[0]][] = $parts[1];
			}
		}
		if(isset($header["Transfer-Encoding"]) && $header["Transfer-Encoding"][0] == "chunked")
			$body = $this->parseChunked($body);

		if(isset($header["Set-Cookie"]))
			$this->importCookies($header["Set-Cookie"]);

		$this->ret["status"] = $status;
		$this->ret["header"] = $header;
		$this->ret["body"]   = $body;
	}
	
	Function parseChunked($data){
		/* Essa parte aqui deu trabalho hein, puta que o pariu! */
		$toget = 0;
		$log   = $body = "";
		$ln = str_replace("\r\n", "\n", $data);
		$ln = explode("\n", $ln);
		$x = 0;
		foreach($ln as $linha){
			$x++;
			if(!$toget){
				$body .= $log;
				$log   = "";
				$toget = hexdec(trim($linha));
//				echo "Atualizando TOGET para: " . hexdec($linha) . "\n";
				continue;
			}
			if(strlen($linha)<$toget)
				$linha .= "\n";
//			echo "[$x] LINHA: (".strlen($linha).") '$linha'\n";
//			echo "[$x] LOG  : (".strlen($log)  .") '$log'\n";
//			echo "[$x] TOGET: '$toget'\n";
			$log .= $linha;
			$toget -= strlen($linha);
//			echo "[$x]-TOGET: '$toget'\n=======\n";
		}
//		echo "Body: $body";
		return $body;
	}
	
	Function importCookies($cookielist){
		foreach($cookielist as $cookie){
			$tmp = explode("=", $cookie);
			if($pos = strpos($tmp[1], ";"))
				$tmp[1] = substr($tmp[1], 0, ($pos?$pos:-1));
			$this->addCookieVar(urldecode($tmp[0]), urldecode($tmp[1]));
//			echo "Importando cookie: $tmp[0] = $tmp[1] ($cookie)\n";
		}
	}

	Function error($no, $str){
		if($this->debug)
			echo "erro($no): $str\n";
	}
}
Return current item: dBrowser