<?php 
 /**
 *      [XCECP] (C) COLNET Inc.
 *      This is NOT a freeware, use is subject to license terms
 */

class  http {
     protected $_useragent = ""; //Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
     protected $_url; 
	 protected $_ssl = false; 
     protected $_followlocation; 
     protected $_timeout; 
     protected $_maxRedirects; 
     protected $_cookieFileLocation = './cookie.txt'; 
     protected $_post; 
     public $_postFields; 
     protected $_getFields; 
     protected $_referer =""; 
     public $params; 
     protected $_session; 
     protected $_webpage; 
     protected $_includeHeader; 
     protected $_noBody; 
     protected $_status; 
     protected $_binaryTransfer; 
     public    $authentication = 0; 
     public    $auth_name      = ''; 
     public    $auth_pass      = ''; 
	 public    $saveCookie = false;
	 public    $sendCookie = false;
	 public    $includeHeader = false;
	private    $cookies = '';
	public $maxredirs = 0;
	public $debug = false;
	public $error = '';
	 public $headers  = array();
     public function useAuth($use){ 
       $this->authentication = 0; 
       if($use == true) $this->authentication = 1; 
     } 
	public function setSsl($val=false){
		if($val===false || $val===true) $this->_ssl = $val;
	}
     public function setName($name){ 
       $this->auth_name = $name; 
     } 
     public function setPass($pass){ 
       $this->auth_pass = $pass; 
     } 
		public function setCookies($cookies){
			$this->cookies = $cookies;
			$this->sendCookie = true;
		}
		public function setTimeout($time){
			$this->_timeout = $time;
		}

     public function __construct($url,$followlocation = true,$timeOut = 10,$maxRedirecs = 4,$binaryTransfer = false,$includeHeader = false,$noBody = false) 
     { 
         $this->_url = $url; 
         $this->_followlocation = $followlocation; 
         $this->_timeout = $timeOut; 
         $this->_maxRedirects = $maxRedirecs; 
         $this->_noBody = $noBody; 
         $this->_includeHeader = $includeHeader; 
         $this->_binaryTransfer = $binaryTransfer; 

         $this->_cookieFileLocation = dirname(__FILE__).'/cookie.txt'; 

     } 

     public function setReferer($referer){ 
       $this->_referer = $referer; 
     } 

     public function setCookiFileLocation($path) 
     { 
         $this->_cookieFileLocation = $path; 
     }

	 public function parseParam($postFields){
		 if(is_array($postFields)){
			 if(count($postFields)==0){return false;}
			$param = $postFields;
			$postFields = "";
			foreach($param as $key=>$val){
				$type = gettype($val);//if($key=='image'){echo $type;exit;}
				if($type!='object' && $type!='resource' && $type!='array'){
					$postFields .= "&".$key."=".$val;
				}
			}
			$postFields = substr($postFields,1);
		 }
		return $postFields;
	 }

     public function setPost ($postFields) 
     {
		$postFieldsStr = $this->parseParam($postFields);
        $this->_post = true;
		if(is_array($postFields)){
			if(isset($postFields['image'])){
				$this->_postFields = $postFields;
			}else{
				$image=false;
				foreach($postFields as $key=>$val){
					$type = gettype($val);
					if($type=='object'){$image=true;break;}	
					if($type=='string' && substr($val,0,1)=='@'){$postFields[$key]=new CURLFile(substr($val,1));$image=true;break;}	
				}
				if($image){
					$this->_postFields = $postFields;
				}else{
					$this->_postFields = $postFieldsStr;
				}
			}
		}else{
			$this->_postFields = $postFieldsStr;
		}
		$this->params = $postFieldsStr;
		if($this->debug){echo'POST:';print_r($this->_postFields);}
		return $this->_postFields;
     } 

     public function setUserAgent($userAgent) 
     { 
         $this->_useragent = $userAgent; 
     } 

     public function createCurl($url = 'nul') 
     { 
        if($url != 'nul'){ 
          $this->_url = $url; 
        } 
		//saveLog($this->_url,2);
		if($this->debug===true){
			echo $this->_url;
		}

         $s = curl_init(); 
		 if($this->headers && is_array($this->headers)){
			curl_setopt($s,CURLOPT_HTTPHEADER,$this->headers); 
		 }
		 //curl_setopt($s,CURLOPT_PROXY , "http://www.ueidc.com:80/"); 
         curl_setopt($s,CURLOPT_URL,$this->_url);
         curl_setopt($s,CURLOPT_TIMEOUT,$this->_timeout); 
         curl_setopt($s,CURLOPT_MAXREDIRS,$this->_maxRedirects); 
         curl_setopt($s,CURLOPT_RETURNTRANSFER,true); 
         curl_setopt($s,CURLOPT_FOLLOWLOCATION,$this->_followlocation); 
		 if($this->cookies){
			 if($this->debug){
				 echo 'Send Text Cookies';
			 }
			 curl_setopt($s,CURLOPT_COOKIE,$this->cookies);
		 }
		 if($this->saveCookie){
			 if($this->debug){
			 echo 'Save Cookies';
			 }
			curl_setopt($s,CURLOPT_COOKIEJAR,$this->_cookieFileLocation); 
		 }
 		 if($this->sendCookie){
			 if($this->debug){
			 echo 'Send File Cookies';
			 }
			curl_setopt($s,CURLOPT_COOKIEFILE,$this->_cookieFileLocation); 
		 }
		 
		 if($this->_ssl===true){
			curl_setopt($s, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($s, CURLOPT_SSL_VERIFYHOST, FALSE);
			//curl_setopt($s, CURLOPT_SSLVERSION, 3);
			curl_setopt($s, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
		 }

         if($this->authentication == 1){ 
           curl_setopt($s, CURLOPT_USERPWD, $this->auth_name.':'.$this->auth_pass); 
         } 
         if($this->_post) 
         { 
             curl_setopt($s,CURLOPT_POST,true); 
             curl_setopt($s,CURLOPT_POSTFIELDS,$this->_postFields); 
			//echo $this->_postFields;exit;
         } 

         if($this->_includeHeader || $this->includeHeader) 
         { 
               curl_setopt($s,CURLOPT_HEADER,true); 
         } 

         if($this->_noBody) 
         { 
             curl_setopt($s,CURLOPT_NOBODY,true); 
         } 
         /* 
         if($this->_binary) 
         { 
             curl_setopt($s,CURLOPT_BINARYTRANSFER,true); 
         } 
         */ 
         if($this->_useragent)curl_setopt($s,CURLOPT_USERAGENT,$this->_useragent); 
         if($this->_referer)curl_setopt($s,CURLOPT_REFERER,$this->_referer);
		if($this->maxredirs){
			curl_setopt($s,CURLOPT_MAXREDIRS,$this->maxredirs);
		}
         $this->_webpage = curl_exec($s);
         $this->_status = curl_getinfo($s,CURLINFO_HTTP_CODE); 
         $this->_contentLength = curl_getinfo($s,CURLINFO_CONTENT_LENGTH_DOWNLOAD); 
		 $this->_effectiveUrl = curl_getinfo($s,CURLINFO_EFFECTIVE_URL); 
		 $this->_contentType = curl_getinfo($s,CURLINFO_CONTENT_TYPE); 
				   if(curl_errno($s)){
					   $this->error=curl_error($s);
					   if($this->debug) echo $this->error;
				   }
         curl_close($s); 
		 //echo  $this->_webpage;exit;
		 //saveLog($this->responseText(),2);
     }

   public function getHttpStatus() 
   { 
       return $this->_status; 
   }
   public function getCookie(){
	   $cookies = '';
	   if($this->saveCookie){
			$fp = fopen($this->_cookieFileLocation,'r');
			while(!feof($fp)){
				$line = fgets($fp);$line = str_replace(array("\r","\n"),'',$line);//echo $line."\n";
				if(strlen($line)>1){$arr = explode("\t",$line);
					if($arr && is_array($arr) && count($arr)==7)$cookies.=$arr[5].'='.$arr[6].'; ';
				}
			}
			fclose($fp);
	   }
	   $cookies = preg_replace("/; $/",'',$cookies);
		return $cookies;
   }
   public function getContentLength() 
   { 
       return $this->_contentLength; 
   }
   public function getContentType() 
   { 
       return $this->_contentType; 
   }
   public function getEffectiveUrl() 
   { 
       return $this->_effectiveUrl; 
   } 

   public function responseText(){ 
      return $this->_webpage; 
   } 
} 
?>