Advertisement
Guest User

Untitled

a guest
May 31st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.68 KB | None | 0 0
  1. <?php
  2. //Simple querystring class that can create querystrings and parse them
  3. class QueryString {
  4.     private $Values = array();
  5.    
  6.     public function __construct( $querystring = false ) {
  7.         if( $querystring !== false )
  8.             $this->ParseQueryString( $querystring );
  9.     }
  10.    
  11.     public function __get( $key ) {
  12.         if( !array_key_exists( $key, $this->Values ) ) return NULL;
  13.         return $this->Values[ $key ];
  14.     }
  15.    
  16.     public function __set( $key, $value ) {
  17.         $this->Values[ $key ] = $value;
  18.     }
  19.    
  20.     public function ParseQueryString( $string ) {
  21.         $tmp = array();
  22.         parse_str( $string, $tmp );
  23.         if( !count( $tmp ) ) return false;
  24.         foreach( $tmp as $key => $value )
  25.             $this->$key = $value;
  26.     }
  27.    
  28.     public function Set( $key, $value ) {
  29.         $this->__set( $key, $value );
  30.     }
  31.    
  32.     public function Get( $key ) {
  33.         return $this->__get( $key );
  34.     }
  35.    
  36.     public function Exists( $key ) {
  37.         return array_key_exists( $key, $this->Values );
  38.     }
  39.    
  40.     public function __toString() {
  41.         if( !count( $this->Values ) ) return '';
  42.         $tmp = array();
  43.         foreach( $this->Values as $key => $value ) $tmp[] = $key.'='.$value;
  44.         return implode( $tmp, '&' );
  45.     }
  46. }
  47.  
  48. //Simple enumeration
  49. class WebRequestMethod {
  50.     public static $Post = 'POST';
  51.     public static $Get = 'GET';
  52.     public static $Head = 'HEAD';
  53.     public static $Put = 'PUT';
  54.     public static $Delete = 'DELETE';
  55.     public static $Trace = 'TRACE';
  56.     public static $Options = 'OPTIONS';
  57.     public static $Connect = 'CONNECT';
  58. }
  59.  
  60.  
  61. //URI class that allows complete URI parsing and saves the parts in properties
  62. class URI {
  63.     public $URI = '', $Host = '', $Port = 0, $Path = '', $QueryString = '', $Query = false, $Scheme = '', $User = '', $Password = '', $Fragment = '';
  64.    
  65.     public function __construct( $uri = false ) {
  66.         if( $uri !== false )
  67.             $this->ParseURI( $uri );
  68.     }
  69.    
  70.     public function ParseURI( $uri ) {
  71.         $parts = parse_url( $uri );
  72.         $this->URI = $uri;
  73.         $this->Host = isset( $parts[ 'host' ] ) ? $parts[ 'host' ] : $this->Host;
  74.         $this->Port = isset( $parts[ 'port' ] ) ? intval( $parts[ 'port' ] ) : $this->Port;
  75.         $this->User = isset( $parts[ 'user' ] ) ? $parts[ 'user' ] : $this->User;
  76.         $this->Password = isset( $parts[ 'pass' ] ) ? $parts[ 'pass' ] : $this->Password;
  77.         $this->Path = isset( $parts[ 'path' ] ) ? $parts[ 'path' ] : $this->Path;
  78.         if( isset( $parts[ 'query' ] ) ) {
  79.             $this->QueryString = $parts[ 'query' ];
  80.             $this->Query = new QueryString( $parts[ 'query' ] );
  81.         }
  82.         $this->Scheme = isset( $parts[ 'scheme' ] ) ? $parts[ 'scheme' ] : $this->Scheme;
  83.         $this->Fragment = isset( $parts[ 'fragment' ] ) ? $parts[ 'fragment' ] : $this->Fragment;      
  84.     }
  85.    
  86.     public function __toString() {
  87.         return $this->URI;
  88.     }
  89. }
  90.  
  91. class MimeTypeCollection implements Iterator {
  92.     public $Items = array();
  93.    
  94.     public function __construct( $mimetypes = false ) {
  95.         if( is_array( $mimetypes ) && count( $mimetypes ) )
  96.             foreach( $mimetypes as $type ) $this->Add( $type );
  97.     }
  98.    
  99.     public function Add( $type ) {
  100.         if( !in_array( $type, $this->Items, true ) ) $this->Items[] = $type;
  101.     }
  102.    
  103.     public function Remove( $type ) {
  104.         if( count( $this->Items ) )
  105.             foreach( $this->Items as $key => $val )
  106.                 if( $val == $type ) unset( $this->Items[ $key ] );
  107.     }
  108.    
  109.     public function Rewind() {
  110.         reset( $this->Items );
  111.     }
  112.    
  113.     public function Current() {
  114.         return current( $this->Items );
  115.     }
  116.    
  117.     public function Next() {
  118.         next( $this->Items );
  119.     }
  120.    
  121.     public function Key() {
  122.         return key( $this->Items );
  123.     }
  124.    
  125.     public function Valid() {
  126.         return current( $this->Items );
  127.     }
  128.    
  129.     public function __toString() {
  130.         return implode( ',', $this->Items );
  131.     }
  132.        
  133. }
  134.  
  135. //Basic class for doing requests, allows Requests in all forms with all messages and schemes
  136. class WebRequest {
  137.    
  138.     private $Handle = false;
  139.    
  140.     public $URI = false;
  141.     public $Headers = array();
  142.     public $PostVars, $GetVars;
  143.     public $Method, $Type;
  144.     public $BufferSize = 1024;
  145.    
  146.     public function __construct( $uri = false ) {
  147.         if( is_object( $uri ) && get_class( $uri ) == 'URI' )
  148.             $this->URI = $uri;
  149.         if( is_string( $uri ) && class_exists( 'URI' ) )
  150.             $this->URI = new URI( $uri );
  151.            
  152.         $this->PostVars = new QueryString();
  153.         $this->GetVars = new QueryString();
  154.         if( $uri !== false ) $this->Open();
  155.         $this->Method = WebRequestMethod::$Get;
  156.         $this->Type = 'HTTP/1.1';
  157.     }
  158.    
  159.     public function AddHeader( $header, $value) {
  160.             $this->Headers[ $header ] = $value;
  161.     }
  162.    
  163.     public function SetMethod( WebRequestMethod $method ) {
  164.         $this->Method = $method;
  165.     }
  166.    
  167.     public function Open() {
  168.         if( !is_object( $this->URI ) ) return false;
  169.         $this->Handle = @fsockopen( $this->URI->Host, $this->URI->Port );
  170.         if( !is_resource( $this->Handle ) ) return false;
  171.         $this->Headers[ 'Host' ] = $this->URI->Host;
  172.         return true;
  173.     }
  174.    
  175.     public function Close() {
  176.         if( !is_resource( $this->Handle ) ) return false;
  177.         fclose( $this->Handle );
  178.         return true;
  179.     }
  180.    
  181.     public function Post( $querystring ) {
  182.         $this->PostVars = new QueryString( $querystring );
  183.         if( !isset( $this->Headers[ 'Content-Type' ] ) )
  184.             $this->Headers[ 'Content-Type' ] = 'application/x-www-form-urlencoded';
  185.         $this->Method = WebRequestMethod::$Post;
  186.     }
  187.    
  188.     public function Get( $querystring ) {
  189.         $this->GetVars = new QueryString( $querystring );
  190.     }
  191.    
  192.     public function Send( $message = "", $showmessage = false ) {
  193.         if( empty( $message ))
  194.             $message = (string)$this->PostVars;
  195.         $headers = "";
  196.         if( strlen( $message ) )
  197.             $this->Headers[ "Content-Length" ] = strlen( $message );
  198.             $headers = $this->Method." ".
  199.                 ( strlen( $this->URI->Path ) ? $this->URI->Path : "/" ).
  200.                     ( strval( $this->GetVars ) != "" ? "?".$this->GetVars :
  201.                         ( strval( $this->URI->Query ) != "" ? "?".$this->URI->Query : "" ) ).
  202.                     ( $this->URI->Fragment != "" ? "#".$this->URI->Fragment : " ".$this->Type )."\r\n";
  203.         if( count( $this->Headers ) )
  204.             foreach( $this->Headers as $header => $value ) $headers .= $header.": ".$value."\r\n";
  205.         $headers .= "\r\n";
  206.         if( $showmessage == true ) {
  207.             echo "<div style=\"border:1px solid gray\"><pre>";
  208.             echo $headers;
  209.             echo $message;
  210.             echo "</pre></div>";
  211.         }
  212.         if( !is_resource( $this->Handle ) ) return false;
  213.         fwrite( $this->Handle, $headers.$message );
  214.         return true;       
  215.     }
  216.    
  217.     public function GetResponse() {
  218.         if( !is_resource( $this->Handle ) ) return false;
  219.         $responsetext = "";
  220.         $contentlenght = 0;
  221.         while( $row = fgets( $this->Handle, $this->BufferSize ) ) {
  222.             if( preg_match( "/Content-Length: ([0-9]+)/i", $row, $match ) )
  223.                 $contentlenght = $match[1];
  224.             $responsetext .= $row;
  225.         }
  226.         return array( 0 => $responsetext, 1 => intval( $contentlenght ) );
  227.     }
  228.    
  229.     public function GetResponseText( $strip_html = true, $parse_html = false ) {
  230.         $response = $this->GetResponse();
  231.         if( $response[1] < 1 ) return "";
  232.         $responsetext = substr( $response[0], strlen( $response[0] ) - $response[1], $response[1] );
  233.         if( $strip_html === true ) $responsetext = strip_tags( $responsetext );
  234.         if( $parse_html === false ) $responsetext = htmlentities( $responsetext );
  235.         return $responsetext;
  236.     }
  237.    
  238.     public function GetResponseHeaders( $as_array = true ) {
  239.         if( !is_resource( $this->Handle ) ) return array();
  240.         echo "Hai";
  241.         $response = $this->GetResponse();
  242.         $responsetext = "";
  243.         if( $response[1] > 0 ) $responsetext = substr( $response[0], 0, strlen( $response[0] )-$response[1] );
  244.         return $as_array === true ? preg_split( "/(\r\n|\n)/i", $responsetext ) : $responsetext;
  245.     }
  246.            
  247. }
  248.  
  249. //HTTP Request Code Enum
  250. class HttpWebRequestState {
  251.     //2xx - Success Status Codes
  252.     public static $OK = 200;
  253.     public static $Created = 201;
  254.     public static $Accepted = 202;
  255.     public static $PartialInformation = 203;
  256.     public static $NoResponse = 204;
  257.     //3xx - Redirection Status Codes
  258.     public static $Moved = 301;
  259.     public static $Found = 302;
  260.     public static $Method = 303;
  261.     public static $NotModified = 304;
  262.     //4xx - Client Error Codes
  263.     public static $BadRequest = 400;
  264.     public static $Unauthorized = 401;
  265.     public static $PaymentRequired = 402;
  266.     public static $Forbidden = 403;
  267.     public static $NotFound = 404;
  268.     //5xx - Server Error Codes
  269.     public static $InternalError = 500;
  270.     public static $NotImplemented = 501;
  271.     public static $Overloaded = 502;
  272.     public static $Timeout = 503;
  273. }
  274.    
  275.  
  276. //A HTTP Webrequest class, default on port 80, handles the reponse headers partial, sends extended information
  277. class HttpWebRequest extends WebRequest {
  278.  
  279.     public $Port = 80;
  280.     public $UserAgent = "Unknown";
  281.     public $Accept;
  282.     public $AcceptLanguage = "en-us;en";
  283.     public $AcceptEncoding = "gzip,deflate";
  284.     public $AcceptCharset = "utf-8";
  285.     public $State = 0;
  286.    
  287.     public function __construct( $url ) {
  288.         parent::__construct( $url );
  289.         $this->Accept = new MimeTypeCollection( "text/html" );
  290.     }
  291.    
  292.     public function SendRequest( $message, $showmessage = false ) {
  293.         $this->URI->Port = $this->Port;
  294.         $this->URI->Scheme = "http";
  295.         $this->AddHeader( "User-Agent", $this->UserAgent );
  296.         $this->AddHeader( "Accept", strval( $this->Accept ) );
  297.         $this->AddHeader( "Accept-Language", $this->AcceptLanguage );
  298.         $this->AddHeader( "Accept-Encoding", $this->AcceptEncoding );
  299.         $this->AddHeader( "Accept-Charset", $this->AcceptCharset );
  300.         $this->AddHeader( "Connection", "close" );
  301.         $this->Send( $message, $showmessage );
  302.         $responseheaders = $this->GetResponseHeaders( true );
  303.         print_r( $responseheaders );
  304.         if( !count( $responseheaders ) ) return false;
  305.         $method = ""; $status = 0; $statusmessage = "";
  306.         if( preg_match( "/^([^ ]+) ([0-9]{3}) ([^\r\n]+)$/i", $responseheaders[0], $matches ) )
  307.         print_r( $matches );
  308.     }
  309. }
  310.    
  311.  
  312. $request = new HttpWebRequest( "rcs.dz-net.net/response.php?test=muha" );
  313. $request->Post( "komm=doch&du=sack" );
  314. $request->Get( "Eh=du&da=._." );
  315. $request->SendRequest( "", true );
  316.  
  317. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement