Location: PHPKode > projects > WebSecurityTester > php-gtk/security/lib/HttpRequest.cls.php
<?
/**
* Class HttpRequest
* 
* This class allow you to simulate HTTP requests
* using GET/POST methods to a specific host:port
* you can send multiple vars, cookies and files simulating file UPLOADS
* 
* @author: Simone Cosci <hide@address.com>
* @version: 1.0
* @copyright: Copyright 2006, InterWorld.it
* 
* */


class HttpRequest{
	
	var $bytes;	 		// number of bytes to read for each request
	var $server; 		// server ip
	var $host;	 		// domain 
	var $use_proxy;		// boolean
	var $proxy;			// proxy informations
	var $vars;	 		// Array var_name=>var_value
	var $files;			// Array Files upload var_name=>{[fname]=>/path/to/file,[ctype]=>'text/plain'}
	var $cookies;		// Array var_name=>var_value
	var $headers;		// request headers array
	var $err;	 		// err
	var $method; 		// GET/POST
	var $port;	 		// Server port
	var $timeout;		// connection timeout
	var $target; 		// Remote script path
	var $protocol;		// protocol schema (http/udp/ssl)
	
	var $result; 		// response result
	var $response;		// Object HttpResponse
	
	var $_query;	 	// full query-string name=>value&name=>value&name=>value
	var $_request;		// complete HTTP request

	/**
	 * HttpRequest Constuctor
	 * @access public
	 * @return HttpRequest
	 */
	function HttpRequest(){
		$this->protocol = 'http';
		$this->protocol_version = '1.0';
		$this->bytes = 0;
		$this->server = '127.0.0.1';
		$this->host = 'localhost';
		$this->use_proxy = false;
		$this->proxy = array(
								'host' => "www.anonymouse.org",
								'user' => '',
								'pass' => '',
								'port' => 8080
							);
		$this->vars = array();
		$this->files = array();
		$this->cookies = array();
		$this->headers = array();

		$this->err = '';
		$this->method = 'GET';
		$this->port = 80;
		$this->timeout = 5;
		$this->target = '/index.php';
		
		$this->response = &new HttpResponse();

		$this->_query = "";
		$this->_request = '';
		$this->_result = '';
	}

	/**
	 * Initialize an HttpRequest
	 * @access private
	 * @return boolean
	 */
	function Init(){
		//error_reporting(0);
		$this->err = '';
		$this->result = '';
		$this->_request = '';
		$this->_query = '';
		foreach ($this->vars as $var_name=>$var_value){
			$this->_query .= urlencode($var_name)."=".urlencode($var_value)."&";
		}
		$this->_query = substr($this->_query,0,-1);
		$this->_request = $this->method." ";
		$this->_request .= $this->target;
		if($this->method=='GET'){
			/* SIMPLE GET METHOD */
			if(!empty($this->_query))$this->_request .= "?".$this->_query;
			if(isset($this->headers['Content-Type'])) unset($this->headers['Content-Type']);
			if(isset($this->headers['Content-Length'])) unset($this->headers['Content-Length']);
		}else{
			/* POST METHOD */
			if(count($this->files)>0){
				/* WITH FILES */
				srand((double)microtime()*1000000);
				$boundary = "---------------------".substr(md5(rand(0,32000)),0,10);
   				$this->headers['Content-Type'] = "multipart/form-data, boundary=$boundary";
				$data = '';
				foreach($this->vars as $var_name=>$var_value){
					$data .="--$boundary\r\n";
					$data .= "Content-Disposition: form-data; name=\"".$var_name."\"\r\n";
					$data .= "\r\n".$var_value."\r\n";
					$data .="--$boundary\r\n";
				}
				foreach($this->files as $var_name=>$file_info){
					if(file_exists($file_info['fname'])){
						$fs = filesize($file_info['fname']);
						if($fs>0){
							$ctype = (isset($file_info['ctype']))?$file_info['ctype']:mime_content_type($file_info['fname']);
							$data .="--$boundary\r\n";
							$data .= "Content-Disposition: form-data; name=\"$var_name\"; filename=\"".$file_info['fname']."\"\r\n";
							$data .= "Content-Type: ".$ctype."\r\n\r\n";
							$fh = fopen($file_info['fname'],'rb');
							while(!feof($fh)) $data .= fread($fh,1024);
							fclose($fh);
							$data .="\r\n--$boundary\r\n";
						}
					}else{
						$this->err .= $file_info['fname']." not found\r\n";
						return false;
					}
				}
				$data .="\r\n--$boundary--\r\n";
				$this->headers['Content-Length'] = strlen($data);
			}else{
				/* WITHOUT FILES (ONLY VARS) */
				$lenght = strlen($this->_query);
				$this->headers['Content-Type'] = 'application/x-www-form-urlencoded';
				$this->headers['Content-Length'] = $lenght;
			}
		}
		if(count($this->cookies)>0){
			$cookies = array();
			foreach($this->cookies as $cookie_name=>$cookie_value) $cookies[] = "$cookie_name=$cookie_value";
			$this->headers['Cookie'] = implode('; ',$cookies);
		}
		if($this->protocol=='http'){
			$this->_request .= " HTTP/".$this->protocol_version."\r\n";
			$this->_request .= "Host: ".($this->use_proxy?$this->proxy['host']:$this->host)."\r\n";
		}
		if(count($this->headers>0)){
			foreach($this->headers as $header_name=>$header_value){
				$this->_request .= $header_name.": ".$header_value."\r\n";
			}
		}
		if($this->method=='POST'){
			if(count($this->files)==0)
				$this->_request .= "\r\n".$this->_query."\r\n";
			else{
				$this->_request .= "\r\n".$data;
			}
		}else{
			$this->_request .= "\r\n";
		}
		return true;
	}
	
	/**
	 * Send an http request and store the http response in a HttpResponse object
	 * @access public
	 * @return boolean
	 */
	function Send(){
		$this->_result = '';
		if($this->server!=""){
			$errno = ''; $errstr = '';
			if($this->use_proxy){
				$this->headers['Proxy-Authorization'] = "Basic ".base64_encode($this->proxy['user'].":".$this->proxy['pass']);
				$this->target = $this->protocol.'://'.$this->host.$this->target;
				if(!$this->Init()) return false;
				$fp = fsockopen ($this->proxy['host'], $this->proxy['port'], $errno, $errstr, $this->timeout);
			}else{
				if(!$this->Init()) return false;
				$fp = fsockopen ($this->server, $this->port, $errno, $errstr, $this->timeout);
			}
			if (!$fp) {
				$this->err = "$errstr ($errno)";
			} else {
				fputs($fp, $this->_request);
				if($this->bytes<=0){
					while(!feof($fp)) {
						$this->_result .= fgets($fp,1024);
					}
				}else 
					$this->_result = fgets($fp,$this->bytes);
				fclose($fp);
				$this->response = &new HttpResponse($this->_result);
				$this->response->Init();
				return true;
			}
		}else 
			$this->err = "Empty Server value.";
		return false;
	}
	

}


?>
Return current item: WebSecurityTester