http.class.php
Upload User: feiyaoda
Upload Date: 2016-11-21
Package Size: 9556k
Code Size: 20k
Category:

WEB Mail

Development Platform:

PHP

  1. <?php
  2. /*
  3.  * http.php
  4.  *
  5.  * @(#) $Header: /home/mlemos/cvsroot/PHPlibrary/http.php,v 1.7 2002/01/12 02:39:57 mlemos Exp $
  6.  *
  7.  */
  8. class http_class
  9. {
  10.     var $host_name="";
  11.     var $host_port=80;
  12.     var $proxy_host_name="";
  13.     var $proxy_host_port=80;
  14.     var $request_method="GET";
  15.     var $user_agent="Manuel Lemos HTTP class test script";
  16.     var $request_uri="";
  17.     var $protocol_version="1.0";
  18.     var $debug=0;
  19.     var $support_cookies=1;
  20.     var $cookies=array();
  21.     /* private variables - DO NOT ACCESS */
  22.     var $state="Disconnected";
  23.     var $connection=0;
  24.     var $content_length=0;
  25.     var $read_length=0;
  26.     var $request_host="";
  27.     var $next_token="";
  28.     var $months=array(
  29.         "Jan"=>"01",
  30.         "Feb"=>"02",
  31.         "Mar"=>"03",
  32.         "Apr"=>"04",
  33.         "May"=>"05",
  34.         "Jun"=>"06",
  35.         "Jul"=>"07",
  36.         "Aug"=>"08",
  37.         "Sep"=>"09",
  38.         "Oct"=>"10",
  39.         "Nov"=>"11",
  40.         "Dec"=>"12");
  41.     /* Private methods - DO NOT CALL */
  42.     Function Tokenize($string,$separator="")
  43.     {
  44.         if(!strcmp($separator,""))
  45.         {
  46.             $separator=$string;
  47.             $string=$this->next_token;
  48.         }
  49.         for($character=0;$character<strlen($separator);$character++)
  50.         {
  51.             if(GetType($position=strpos($string,$separator[$character]))=="integer")
  52.                 $found=(IsSet($found) ? min($found,$position) : $position);
  53.         }
  54.         if(IsSet($found))
  55.         {
  56.             $this->next_token=substr($string,$found+1);
  57.             return(substr($string,0,$found));
  58.         }
  59.         else
  60.         {
  61.             $this->next_token="";
  62.             return($string);
  63.         }
  64.     }
  65.     Function OutputDebug($message)
  66.     {
  67.         echo $message,"n";
  68.     }
  69.     Function GetLine()
  70.     {
  71.         for($line="";;)
  72.         {
  73.             if(feof($this->connection)
  74.             || !($part=fgets($this->connection,100)))
  75.                 return(0);
  76.             $line.=$part;
  77.             $length=strlen($line);
  78.             if($length>=2
  79.             && substr($line,$length-2,2)=="rn")
  80.             {
  81.                 $line=substr($line,0,$length-2);
  82.                 if($this->debug)
  83.                     $this->OutputDebug("< $line");
  84.                 return($line);
  85.             }
  86.         }
  87.     }
  88.     Function PutLine($line)
  89.     {
  90.         if($this->debug)
  91.             $this->OutputDebug("> $line");
  92.         return(fputs($this->connection,"$linern"));
  93.     }
  94.     Function PutData($data)
  95.     {
  96.         if($this->debug)
  97.             $this->OutputDebug("> $data");
  98.         return(fputs($this->connection,$data));
  99.     }
  100.     Function Readbytes($length)
  101.     {
  102.         if($this->debug)
  103.         {
  104.             if(($bytes=fread($this->connection,$length))!="")
  105.                 $this->OutputDebug("< $bytes");
  106.             return($bytes);
  107.         }
  108.         else
  109.             return(fread($this->connection,$length));
  110.     }
  111.     Function EndOfInput()
  112.     {
  113.         return(feof($this->connection));
  114.     }
  115.     Function Connect($host_name,$host_port)
  116.     {
  117.         if($this->debug)
  118.             $this->OutputDebug("Connecting to $host_name...");
  119.         if(($this->connection=fsockopen($host_name,$host_port,&$error))==0)
  120.         {
  121.             switch($error)
  122.             {
  123.                 case -3:
  124.                     return("-3 socket could not be created");
  125.                 case -4:
  126.                     return("-4 dns lookup on hostname "".$host_name."" failed");
  127.                 case -5:
  128.                     return("-5 connection refused or timed out");
  129.                 case -6:
  130.                     return("-6 fdopen() call failed");
  131.                 case -7:
  132.                     return("-7 setvbuf() call failed");
  133.                 default:
  134.                     return($error." could not connect to the host "".$host_name.""");
  135.             }
  136.         }
  137.         else
  138.         {
  139.             if($this->debug)
  140.                 $this->OutputDebug("Connected to $host_name");
  141.             $this->state="Connected";
  142.             return("");
  143.         }
  144.     }
  145.     Function Disconnect()
  146.     {
  147.         if($this->debug)
  148.             $this->OutputDebug("Disconnected from ".$this->host_name);
  149.         fclose($this->connection);
  150.         return("");
  151.     }
  152.     /* Public methods */
  153.     Function Open($arguments)
  154.     {
  155.         if($this->state!="Disconnected")
  156.             return("1 already connected");
  157.         if(IsSet($arguments["HostName"]))
  158.             $this->host_name=$arguments["HostName"];
  159.         if(IsSet($arguments["HostPort"]))
  160.             $this->host_port=$arguments["HostPort"];
  161.         if(IsSet($arguments["ProxyHostName"]))
  162.             $this->proxy_host_name=$arguments["ProxyHostName"];
  163.         if(IsSet($arguments["ProxyHostPort"]))
  164.             $this->proxy_host_port=$arguments["ProxyHostPort"];
  165.         if(strlen($this->proxy_host_name)==0)
  166.         {
  167.             if(strlen($this->host_name)==0)
  168.                 return("2 it was not specified a valid hostname");
  169.             $host_name=$this->host_name;
  170.             $host_port=$this->host_port;
  171.         }
  172.         else
  173.         {
  174.             $host_name=$this->proxy_host_name;
  175.             $host_port=$this->proxy_host_port;
  176.         }
  177.         $error=$this->Connect($host_name,$host_port);
  178.         if(strlen($error)==0)
  179.             $this->state="Connected";
  180.         return($error);
  181.     }
  182.     Function Close()
  183.     {
  184.         if($this->state=="Disconnected")
  185.             return("1 already disconnected");
  186.         $error=$this->Disconnect();
  187.         if(strlen($error)==0)
  188.             $this->state="Disconnected";
  189.         return($error);
  190.     }
  191.     Function SendRequest($arguments)
  192.     {
  193.         switch($this->state)
  194.         {
  195.             case "Disconnected":
  196.                 return("1 connection was not yet established");
  197.             case "Connected":
  198.                 break;
  199.             default:
  200.                 return("2 can not send request in the current connection state");
  201.         }
  202.         if(IsSet($arguments["RequestMethod"]))
  203.             $this->request_method=$arguments["RequestMethod"];
  204.         if(IsSet($arguments["User-Agent"]))
  205.             $this->user_agent=$arguments["User-Agent"];
  206.         if(strlen($this->request_method)==0)
  207.             return("3 it was not specified a valid request method");
  208.         if(IsSet($arguments["RequestURI"]))
  209.             $this->request_uri=$arguments["RequestURI"];
  210.         if(strlen($this->request_uri)==0
  211.         || substr($this->request_uri,0,1)!="/")
  212.             return("4 it was not specified a valid request URI");
  213.         $request_body="";
  214.         $headers=(IsSet($arguments["Headers"]) ? $arguments["Headers"] : array());
  215.         if($this->request_method=="POST")
  216.         {
  217.             if(IsSet($arguments["PostValues"]))
  218.             {
  219.                 $values=$arguments["PostValues"];
  220.                 if(GetType($values)!="array")
  221.                     return("5 it was not specified a valid POST method values array");
  222.                 for($request_body="",Reset($values),$value=0;$value<count($values);Next($values),$value++)
  223.                 {
  224.                     if($value>0)
  225.                         $request_body.="&";
  226.                     $request_body.=Key($values)."=".UrlEncode($values[Key($values)]);
  227.                 }
  228.                 $headers["Content-type"]="application/x-www-form-urlencoded";
  229.             }
  230.             else
  231.             {
  232.                 if(IsSet($arguments["Body"]))
  233.                     $request_body=$arguments["Body"];
  234.             }
  235.         }
  236.         if(strlen($this->proxy_host_name)==0)
  237.             $request_uri=$this->request_uri;
  238.         else
  239.             $request_uri="http://".$this->host_name.($this->host_port==80 ? "" : ":".$this->host_port).$this->request_uri;
  240.         
  241.         // added
  242.         $this->log_sending_data[] = $this->request_method." ".$request_uri." HTTP/".$this->protocol_version;
  243.         if(($success=$this->PutLine($this->request_method." ".$request_uri." HTTP/".$this->protocol_version)))
  244.         {
  245.             if(($body_length=strlen($request_body)))
  246.                 $headers["Content-length"]=$body_length;
  247.             for($host_set=0,Reset($headers),$header=0;$header<count($headers);Next($headers),$header++)
  248.             {
  249.                 $header_name=Key($headers);
  250.                 $header_value=$headers[$header_name];
  251.                 if(GetType($header_value)=="array")
  252.                 {
  253.                     for(Reset($header_value),$value=0;$value<count($header_value);Next($header_value),$value++)
  254.                     {
  255.                         // added
  256.                         $this->log_sending_data[] = "$header_name: ".$header_value[Key($header_value)];
  257.                         if(!$success=$this->PutLine("$header_name: ".$header_value[Key($header_value)]))
  258.                             break 2;
  259.                     }
  260.                 }
  261.                 else
  262.                 {
  263.                     // added
  264.                     $this->log_sending_data[] = "$header_name: $header_value";
  265.                     
  266.                     if(!$success=$this->PutLine("$header_name: $header_value"))
  267.                         break;
  268.                 }
  269.                 if(strtolower(Key($headers))=="host")
  270.                 {
  271.                     $this->request_host=strtolower($header_value);
  272.                     $host_set=1;
  273.                 }
  274.             }
  275.             if($success)
  276.             {
  277.                 if(!$host_set)
  278.                 {
  279.                     // added
  280.                     $this->log_sending_data[] = "Host: ".$this->host_name;                    
  281.                     $success=$this->PutLine("Host: ".$this->host_name);
  282.                     $this->request_host=strtolower($this->host_name);
  283.                 }
  284.                 if(count($this->cookies)
  285.                 && IsSet($this->cookies[0]))
  286.                 {
  287.                     $now=gmdate("Y-m-d H-i-s");
  288.                     for($cookies=array(),$domain=0,Reset($this->cookies[0]);$domain<count($this->cookies[0]);Next($this->cookies[0]),$domain++)
  289.                     {
  290.                         $domain_pattern=Key($this->cookies[0]);
  291.                         $match=strlen($this->request_host)-strlen($domain_pattern);
  292.                         if($match>=0
  293.                         && !strcmp($domain_pattern,substr($this->request_host,$match))
  294.                         && ($match==0
  295.                         || $domain_pattern[0]=="."
  296.                         || $this->request_host[$match-1]=="."))
  297.                         {
  298.                             for(Reset($this->cookies[0][$domain_pattern]),$path_part=0;$path_part<count($this->cookies[0][$domain_pattern]);Next($this->cookies[0][$domain_pattern]),$path_part++)
  299.                             {
  300.                                 $path=Key($this->cookies[0][$domain_pattern]);
  301.                                 if(strlen($this->request_uri)>=strlen($path)
  302.                                 && substr($this->request_uri,0,strlen($path))==$path)
  303.                                 {
  304.                                     for(Reset($this->cookies[0][$domain_pattern][$path]),$cookie=0;$cookie<count($this->cookies[0][$domain_pattern][$path]);Next($this->cookies[0][$domain_pattern][$path]),$cookie++)
  305.                                     {
  306.                                         $cookie_name=Key($this->cookies[0][$domain_pattern][$path]);
  307.                                         $expires=$this->cookies[0][$domain_pattern][$path][$cookie_name]["expires"];
  308.                                         if($expires==""
  309.                                         || strcmp($now,$expires)<0)
  310.                                             $cookies[$cookie_name]=$this->cookies[0][$domain_pattern][$path][$cookie_name];
  311.                                     }
  312.                                 }
  313.                             }
  314.                         }
  315.                     }
  316.                     for(Reset($cookies),$cookie=0;$cookie<count($cookies);Next($cookies),$cookie++)
  317.                     {
  318.                         $cookie_name=Key($cookies);
  319.                         // added
  320.                         $this->log_sending_data[] = "Cookie: ".UrlEncode($cookie_name)."=".$cookies[$cookie_name]["value"].";";                           
  321.                         if(!($success=$this->PutLine("Cookie: ".UrlEncode($cookie_name)."=".$cookies[$cookie_name]["value"].";")))
  322.                             break;
  323.                     }
  324.                 }
  325.                 if($success)
  326.                 {
  327.                     if($success)
  328.                     {
  329.                         // added
  330.                         $this->log_sending_data[] = "";                                                   
  331.                         $success=$this->PutLine("");
  332.                         $this->log_sending_data[] = $request_body;
  333.                         if($body_length
  334.                         && $success)
  335.                             $success=$this->PutData($request_body);
  336.                     }
  337.                 }
  338.             }
  339.         }
  340.         if(!$success)
  341.             return("5 could not send the HTTP request");
  342.         $this->state="RequestSent";
  343.         return("");
  344.     }
  345.     Function ReadReplyHeaders(&$headers)
  346.     {
  347.         switch($this->state)
  348.         {
  349.             case "Disconnected":
  350.                 return("1 connection was not yet established");
  351.             case "Connected":
  352.                 return("2 request was not sent");
  353.             case "RequestSent":
  354.                 break;
  355.             default:
  356.                 return("3 can not get request headers in the current connection state");
  357.         }
  358.         $headers=array();
  359.         $this->content_length=$this->read_length=0;
  360.         $this->content_length_set=0;
  361.         for(;;)
  362.         {
  363.             $line=$this->GetLine();
  364.             if(GetType($line)!="string")
  365.                 return("4 could not read request reply");
  366.             if($line=="")
  367.             {
  368.                 $this->state="GotReplyHeaders";
  369.                 return("");
  370.             }
  371.             $header_name=strtolower($this->Tokenize($line,":"));
  372.             $header_value=Trim(Chop($this->Tokenize("rn")));
  373.             if(IsSet($headers[$header_name]))
  374.             {
  375.                 if(GetType($headers[$header_name])=="string")
  376.                     $headers[$header_name]=array($headers[$header_name]);
  377.                 $headers[$header_name][]=$header_value;
  378.             }
  379.             else
  380.                 $headers[$header_name]=$header_value;
  381.             switch($header_name)
  382.             {
  383.                 case "content-length":
  384.                     $this->content_length=intval($headers[$header_name]);
  385.                     $this->content_length_set=1;
  386.                     break;
  387.                 case "set-cookie":
  388.                     if($this->support_cookies)
  389.                     {
  390.                         $cookie_name=trim($this->Tokenize($headers[$header_name],"="));
  391.                         $cookie_value=$this->Tokenize(";");
  392.                         $domain=$this->request_host;
  393.                         $path="/";
  394.                         $expires="";
  395.                         $secure=0;
  396.                         while(($name=strtolower(trim($this->Tokenize("="))))!="")
  397.                         {
  398.                             $value=UrlDecode($this->Tokenize(";"));
  399.                             switch($name)
  400.                             {
  401.                                 case "domain":
  402.                                     if($value==""
  403.                                     || !strpos($value,".",$value[0]=="."))
  404.                                         break;
  405.                                     $domain=strtolower($value);
  406.                                     break;
  407.                                 case "path":
  408.                                     if($value!=""
  409.                                     && $value[0]=="/")
  410.                                         $path=$value;
  411.                                     break;
  412.                                 case "expires":
  413.                                     if(ereg("^((Mon|Monday|Tue|Tuesday|Wed|Wednesday|Thu|Thursday|Fri|Friday|Sat|Saturday|Sun|Sunday), )?([0-9]{2})\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\-([0-9]{2,4}) ([0-9]{2})\:([0-9]{2})\:([0-9]{2}) GMT$",$value,$matches))
  414.                                     {
  415.                                         $year=intval($matches[5]);
  416.                                         if($year<1900)
  417.                                             $year+=($year<70 ? 2000 : 1900);
  418.                                         $expires="$year-".$this->months[$matches[4]]."-".$matches[3]." ".$matches[6].":".$matches[7].":".$matches[8];
  419.                                     }
  420.                                     break;
  421.                                 case "secure":
  422.                                     $secure=1;
  423.                                     break;
  424.                             }
  425.                         }
  426.                         $this->cookies[$secure][$domain][$path][$cookie_name]=array(
  427.                             "name"=>$cookie_name,
  428.                             "value"=>$cookie_value,
  429.                             "domain"=>$domain,
  430.                             "path"=>$path,
  431.                             "expires"=>$expires,
  432.                             "secure"=>$secure
  433.                         );
  434.                     }
  435.             }
  436.         }
  437.     }
  438.     Function ReadReplyBody(&$body,$length)
  439.     {
  440.         switch($this->state)
  441.         {
  442.             case "Disconnected":
  443.                 return("1 connection was not yet established");
  444.             case "Connected":
  445.                 return("2 request was not sent");
  446.             case "RequestSent":
  447.                 if(($error=$this->ReadReplyHeaders(&$headers))!="")
  448.                     return($error);
  449.                 break;
  450.             case "GotReplyHeaders":
  451.                 break;
  452.             default:
  453.                 return("3 can not get request headers in the current connection state");
  454.         }
  455.         $body="";
  456.         if($this->content_length_set)
  457.             $length=min($this->content_length-$this->read_length,$length);
  458.         if($length>0
  459.         && !$this->EndOfInput()
  460.         && ($body=$this->ReadBytes($length))=="")
  461.             return("4 could not get the request reply body");
  462.         $this->read_length+=$length;
  463.         return("");
  464.     }
  465.     Function GetPersistentCookies(&$cookies)
  466.     {
  467.         $now=gmdate("Y-m-d H-i-s");
  468.         $cookies=array();
  469.         for($secure_cookies=0,Reset($this->cookies);$secure_cookies<count($this->cookies);Next($this->cookies),$secure_cookies++)
  470.         {
  471.             $secure=Key($this->cookies);
  472.             for($domain=0,Reset($this->cookies[$secure]);$domain<count($this->cookies[$secure]);Next($this->cookies[$secure]),$domain++)
  473.             {
  474.                 $domain_pattern=Key($this->cookies[$secure]);
  475.                 for(Reset($this->cookies[$secure][$domain_pattern]),$path_part=0;$path_part<count($this->cookies[$secure][$domain_pattern]);Next($this->cookies[$secure][$domain_pattern]),$path_part++)
  476.                 {
  477.                     $path=Key($this->cookies[$secure][$domain_pattern]);
  478.                     for(Reset($this->cookies[$secure][$domain_pattern][$path]),$cookie=0;$cookie<count($this->cookies[$secure][$domain_pattern][$path]);Next($this->cookies[$secure][$domain_pattern][$path]),$cookie++)
  479.                     {
  480.                         $cookie_name=Key($this->cookies[$secure][$domain_pattern][$path]);
  481.                         $expires=$this->cookies[$secure][$domain_pattern][$path][$cookie_name]["expires"];
  482.                         if($expires!=""
  483.                         && strcmp($now,$expires)<0)
  484.                             $cookies[$secure][$domain_pattern][$path][$cookie_name]=$this->cookies[$secure][$domain_pattern][$path][$cookie_name];
  485.                     }
  486.                 }
  487.             }
  488.         }
  489.     }
  490. };
  491. ?>