home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / httppost.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  6.4 KB  |  279 lines

  1. http_post 
  2.  
  3. A class that enables posting a 'form' from within a php script.
  4.  
  5.  
  6.  
  7. <?php
  8. #
  9. # http_post - PHP3 class for posting a 'form' from within a php3 script
  10. # Version 0.5b
  11. #
  12. # Copyright 2000 
  13. # Alan van den Bosch (alan@sanguis.com.au)
  14. # Sanguis Pty Ltd (acn 061 444 031) 
  15. #
  16. # Licence:
  17. # You are granted the right to use and/or redistribute this
  18. # code only if this licence and the copyright notice are included
  19. # and you accept that no warranty of any kind is made or implied 
  20. # by the author or Sanguis Pty Ltd.
  21. #
  22. # Methods:
  23. #
  24. # http_post() 
  25. #    Constructor used when creating a new instance of the http_post class.
  26. #    Returns true on success.
  27. #    ie.
  28. #        $a=new http_post;
  29. #
  30. #
  31. # set_server(string SERVER) 
  32. #    Set the server of the URI you wish to post to. see also set_action()
  33. #    Returns true on success.
  34. #    ie.
  35. #        $a->set_server("127.0.0.1");
  36. #    or
  37. #        $a->set_server("www.somehost.org");
  38. #
  39. #
  40. # set_port(string PORT) 
  41. #    Set the tcp port of the URI you wish to    post to. see also set_action()
  42. #    Returns true on success.
  43. #    ie.
  44. #        $a->set_port("8080");
  45. #
  46. #
  47. # set_file(string FILENAME) 
  48. #    Set the filename of the URI you wish to    post to. see also set_action()
  49. #    Returns true on success.
  50. #    ie.
  51. #        $a->set_file("/incoming.php3");
  52. #
  53. #
  54. # set_action(string ACTION) 
  55. #    Set the URI you wish to post to.
  56. #    Returns true on success.
  57. #    ie.
  58. #        $a->set_action("http://www.somehost.org:8080/incoming.php3");
  59. #
  60. # set_enctype(string ENCTYPE)
  61. #    Set the encoding type used for the post. Can have the values
  62. #    "application/x-www-form-urlencoded" or "multipart/form-data"
  63. #    Returns true on success.
  64. #    ie.
  65. #        $a->set_enctype("multipart/form-data");
  66. #
  67. #
  68. # set_element(string NAME, string VALUE)
  69. #    Set or update a single name/value pair to be posted
  70. #    Returns true on success.
  71. #    ie.
  72. #        $a->set_element("username","John Doe");
  73. #
  74. #
  75. # set_element(array ELEMENTS)
  76. #    Set or update a number of name/value pairs to be posted
  77. #    Returns true on success.
  78. #    ie.
  79. #        $a->set_element(array("username" => "John Doe",
  80. #                      "password" => "dead-ringer",
  81. #                      "age" => "99"));
  82. #
  83. #
  84. # set_timeout(integer TIMEOUT)
  85. #    Set the number of seconds to wait for the server to connect
  86. #    when posting. minimum value of 1 second.
  87. #    Returns true on success.
  88. #    ie.
  89. #        $a->set_timeout(10); 
  90. #
  91. # show_post()
  92. #    Show the current internal state of an instance, for debugging.
  93. #    Returns true on success.
  94. #    ie.
  95. #        $a->show_post();
  96. #
  97. #
  98. # send(boolean DISPLAY)
  99. #    Send the name/value pairs using the post method. The response
  100. #    can be echoed by setting DISPLAY to a true value. 
  101. #    Returns a string containing the raw response on success, false
  102. #    on failure.
  103. #    ie.
  104. #        $a->send(1);
  105. #
  106.  
  107.  
  108. class http_post
  109. {
  110.     function http_post(){
  111.         $this->_method="post";
  112.         $this->_server=$GLOBALS["HTTP_HOST"];
  113.         $this->_file="\\";
  114.         $this->_port="80";
  115.         $this->_enctype="application/x-www-form-urlencoded";
  116.         $this->_element=array();
  117.         $this->_timeout=20;
  118.     }
  119.  
  120.     function set_server($newServer=""){
  121.         if(strlen($newServer)<1)$newServer=$HTTP_HOST;
  122.         $this->_server=$newServer;
  123.         return 1;
  124.     }    
  125.  
  126.     function set_port($newPort="80"){
  127.         $newPort=intval($newPort);
  128.         if($newPort < 0 || $newPort > 65535)$newPort=80;
  129.         $this->_port=$newPort;
  130.         return 1;
  131.     }    
  132.  
  133.     function set_file($newFile="\\"){
  134.         $this->_file=$newFile;
  135.         return 1;
  136.     }    
  137.  
  138.     function set_action($newAction=""){
  139.         $pat="^((http://){1}([^:/]{0,}){1}(:([0-9]{1,})){0,1}){0,1}(.*)";
  140.  
  141.         if(eregi($pat,$newAction,$sub)){
  142.             if(strlen($sub[3])>0)$this->_server=$sub[3];
  143.             if(strlen($sub[5])>0)$this->_port=$sub[5];
  144.             $this->_file=$sub[6];
  145.             return 1;
  146.         }
  147.         return 0;
  148.     }
  149.  
  150.     function set_enctype($newEnctype="application/x-www-form-urlencoded"){
  151.         if($newEnctype != "application/x-www-form-urlencoded" &&
  152.             $newEnctype != "multipart/form-data"){
  153.             $newEnctype="application/x-www-form-urlencoded";
  154.         }
  155.         $this->_enctype=$newEnctype;
  156.         return 1;
  157.     }    
  158.  
  159.     function set_element($key="",$val=""){
  160.         if(is_array($key)){
  161.             $len=sizeof($key);
  162.             reset($key);
  163.             for($i=0;$i<$len;$i++){
  164.                 $cur=each($key);
  165.                 $k=$cur["key"];
  166.                 $v=$cur["value"];
  167.                 $this->_element[$k]=$v;
  168.             }
  169.         }
  170.         else{
  171.             if(strlen($key)>0)$this->_element[$key]=$val;
  172.         }
  173.         return 1;
  174.     }
  175.  
  176.     function set_timeout($newTimeout=20){
  177.         $newTimeout=intval($newTimeout);
  178.         if($newTimeout<1)$newTimeout=1;
  179.         $this->_timeout=$newTimeout;
  180.         return 1;
  181.     }    
  182.     
  183.     function show_post(){
  184.         $str="";
  185.         $str.="Action:".$this->_action."<br>";
  186.         $str.="Server:".$this->_server."<br>";
  187.         $str.="Port:".$this->_port."<br>";
  188.         $str.="File:".$this->_file."<br>";
  189.         $str.="Enctype:".$this->_enctype."<br>";
  190.     
  191.         echo $str;
  192.  
  193.         $len=sizeof($this->_element);
  194.         reset($this->_element);
  195.         for($i=0;$i<$len;$i++){
  196.             $cur=each($this->_element);
  197.             $key=$cur["key"];
  198.             $val=$cur["value"];
  199.             echo"Field:$key = $val<br>\n";
  200.         }
  201.         return 1;
  202.     }
  203.  
  204.     function send($display=0){
  205.         // open socket to server
  206.         $errno=$errstr=$retstr="";
  207.         $sk = fsockopen($this->_server,
  208.                 $this->_port,
  209.                 &$errno,
  210.                 &$errstr,
  211.                 $this->_timeout
  212.                 );
  213.         if(!$sk){
  214.             return 0;
  215.         }
  216.         else{
  217.             $boundary="----".md5(uniqid(rand()))."----";
  218.             $message=$this->_get_message($boundary);
  219.             $str="";
  220.             $str.=strtoupper($this->_method)." ";
  221.             $str.=$this->_file." HTTP/1.0 \r\n";
  222.             $str.="Referer: \r\n";
  223.             $str.="User-Agent: php-HTTP_POST/1.0 \r\n";
  224.             $str.="Host: ".$this->_server."\r\n";
  225.  
  226.             $str.="Content-type: ".$this->_enctype;
  227.             if($this->_enctype=="multipart/form-data"){
  228.                 $str.="; boundary=".$boundary;
  229.             }
  230.             $str.=" \r\n";
  231.     
  232.             $str.="Content-length: ".strlen($message)."\r\n\r\n";
  233.             $str.=$message;
  234.  
  235.             fputs($sk,$str);
  236.  
  237.             while(!feof($sk)){
  238.                 $resp=fgets($sk,80);
  239.                 $retstr.=$resp;
  240.                 if($display)echo $resp;
  241.             }
  242.  
  243.             fclose($sk);
  244.             return $retstr;
  245.         }
  246.     }        
  247.  
  248.     function _get_message($boundary=""){
  249.         $retstr="";
  250.  
  251.         $len=sizeof($this->_element);
  252.         reset($this->_element);
  253.  
  254.         $switch=($this->_enctype=="multipart/form-data")?0:1;
  255.  
  256.         for($i=0;$i<$len;$i++){
  257.             $cur=each($this->_element);
  258.             $key=$cur["key"];
  259.             $val=$cur["value"];
  260.             
  261.             if($switch){
  262.                 if(strlen($retstr)!=0)$retstr.="&";
  263.                 $retstr.=rawurlencode($key)."=";
  264.                 $retstr.=rawurlencode($val);    
  265.             }
  266.             else{
  267.                 $retstr.=$boundary."\r\n";
  268.                 $retstr.="Content-Disposition: form-data; ";
  269.                 $retstr.="name=\"$key\"\r\n\r\n$val\r\n\r\n";
  270.             }
  271.         }
  272.         if(!$switch)$retstr.=$boundary."\r\n";
  273.         return $retstr;
  274.     }
  275. }
  276.  
  277. ?>
  278.