Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Simple querystring class that can create querystrings and parse them
- class QueryString {
- private $Values = array();
- public function __construct( $querystring = false ) {
- if( $querystring !== false )
- $this->ParseQueryString( $querystring );
- }
- public function __get( $key ) {
- if( !array_key_exists( $key, $this->Values ) ) return NULL;
- return $this->Values[ $key ];
- }
- public function __set( $key, $value ) {
- $this->Values[ $key ] = $value;
- }
- public function ParseQueryString( $string ) {
- $tmp = array();
- parse_str( $string, $tmp );
- if( !count( $tmp ) ) return false;
- foreach( $tmp as $key => $value )
- $this->$key = $value;
- }
- public function Set( $key, $value ) {
- $this->__set( $key, $value );
- }
- public function Get( $key ) {
- return $this->__get( $key );
- }
- public function Exists( $key ) {
- return array_key_exists( $key, $this->Values );
- }
- public function __toString() {
- if( !count( $this->Values ) ) return '';
- $tmp = array();
- foreach( $this->Values as $key => $value ) $tmp[] = $key.'='.$value;
- return implode( $tmp, '&' );
- }
- }
- //Simple enumeration
- class WebRequestMethod {
- public static $Post = 'POST';
- public static $Get = 'GET';
- public static $Head = 'HEAD';
- public static $Put = 'PUT';
- public static $Delete = 'DELETE';
- public static $Trace = 'TRACE';
- public static $Options = 'OPTIONS';
- public static $Connect = 'CONNECT';
- }
- //URI class that allows complete URI parsing and saves the parts in properties
- class URI {
- public $URI = '', $Host = '', $Port = 0, $Path = '', $QueryString = '', $Query = false, $Scheme = '', $User = '', $Password = '', $Fragment = '';
- public function __construct( $uri = false ) {
- if( $uri !== false )
- $this->ParseURI( $uri );
- }
- public function ParseURI( $uri ) {
- $parts = parse_url( $uri );
- $this->URI = $uri;
- $this->Host = isset( $parts[ 'host' ] ) ? $parts[ 'host' ] : $this->Host;
- $this->Port = isset( $parts[ 'port' ] ) ? intval( $parts[ 'port' ] ) : $this->Port;
- $this->User = isset( $parts[ 'user' ] ) ? $parts[ 'user' ] : $this->User;
- $this->Password = isset( $parts[ 'pass' ] ) ? $parts[ 'pass' ] : $this->Password;
- $this->Path = isset( $parts[ 'path' ] ) ? $parts[ 'path' ] : $this->Path;
- if( isset( $parts[ 'query' ] ) ) {
- $this->QueryString = $parts[ 'query' ];
- $this->Query = new QueryString( $parts[ 'query' ] );
- }
- $this->Scheme = isset( $parts[ 'scheme' ] ) ? $parts[ 'scheme' ] : $this->Scheme;
- $this->Fragment = isset( $parts[ 'fragment' ] ) ? $parts[ 'fragment' ] : $this->Fragment;
- }
- public function __toString() {
- return $this->URI;
- }
- }
- class MimeTypeCollection implements Iterator {
- public $Items = array();
- public function __construct( $mimetypes = false ) {
- if( is_array( $mimetypes ) && count( $mimetypes ) )
- foreach( $mimetypes as $type ) $this->Add( $type );
- }
- public function Add( $type ) {
- if( !in_array( $type, $this->Items, true ) ) $this->Items[] = $type;
- }
- public function Remove( $type ) {
- if( count( $this->Items ) )
- foreach( $this->Items as $key => $val )
- if( $val == $type ) unset( $this->Items[ $key ] );
- }
- public function Rewind() {
- reset( $this->Items );
- }
- public function Current() {
- return current( $this->Items );
- }
- public function Next() {
- next( $this->Items );
- }
- public function Key() {
- return key( $this->Items );
- }
- public function Valid() {
- return current( $this->Items );
- }
- public function __toString() {
- return implode( ',', $this->Items );
- }
- }
- //Basic class for doing requests, allows Requests in all forms with all messages and schemes
- class WebRequest {
- private $Handle = false;
- public $URI = false;
- public $Headers = array();
- public $PostVars, $GetVars;
- public $Method, $Type;
- public $BufferSize = 1024;
- public function __construct( $uri = false ) {
- if( is_object( $uri ) && get_class( $uri ) == 'URI' )
- $this->URI = $uri;
- if( is_string( $uri ) && class_exists( 'URI' ) )
- $this->URI = new URI( $uri );
- $this->PostVars = new QueryString();
- $this->GetVars = new QueryString();
- if( $uri !== false ) $this->Open();
- $this->Method = WebRequestMethod::$Get;
- $this->Type = 'HTTP/1.1';
- }
- public function AddHeader( $header, $value) {
- $this->Headers[ $header ] = $value;
- }
- public function SetMethod( WebRequestMethod $method ) {
- $this->Method = $method;
- }
- public function Open() {
- if( !is_object( $this->URI ) ) return false;
- $this->Handle = @fsockopen( $this->URI->Host, $this->URI->Port );
- if( !is_resource( $this->Handle ) ) return false;
- $this->Headers[ 'Host' ] = $this->URI->Host;
- return true;
- }
- public function Close() {
- if( !is_resource( $this->Handle ) ) return false;
- fclose( $this->Handle );
- return true;
- }
- public function Post( $querystring ) {
- $this->PostVars = new QueryString( $querystring );
- if( !isset( $this->Headers[ 'Content-Type' ] ) )
- $this->Headers[ 'Content-Type' ] = 'application/x-www-form-urlencoded';
- $this->Method = WebRequestMethod::$Post;
- }
- public function Get( $querystring ) {
- $this->GetVars = new QueryString( $querystring );
- }
- public function Send( $message = "", $showmessage = false ) {
- if( empty( $message ))
- $message = (string)$this->PostVars;
- $headers = "";
- if( strlen( $message ) )
- $this->Headers[ "Content-Length" ] = strlen( $message );
- $headers = $this->Method." ".
- ( strlen( $this->URI->Path ) ? $this->URI->Path : "/" ).
- ( strval( $this->GetVars ) != "" ? "?".$this->GetVars :
- ( strval( $this->URI->Query ) != "" ? "?".$this->URI->Query : "" ) ).
- ( $this->URI->Fragment != "" ? "#".$this->URI->Fragment : " ".$this->Type )."\r\n";
- if( count( $this->Headers ) )
- foreach( $this->Headers as $header => $value ) $headers .= $header.": ".$value."\r\n";
- $headers .= "\r\n";
- if( $showmessage == true ) {
- echo "<div style=\"border:1px solid gray\"><pre>";
- echo $headers;
- echo $message;
- echo "</pre></div>";
- }
- if( !is_resource( $this->Handle ) ) return false;
- fwrite( $this->Handle, $headers.$message );
- return true;
- }
- public function GetResponse() {
- if( !is_resource( $this->Handle ) ) return false;
- $responsetext = "";
- $contentlenght = 0;
- while( $row = fgets( $this->Handle, $this->BufferSize ) ) {
- if( preg_match( "/Content-Length: ([0-9]+)/i", $row, $match ) )
- $contentlenght = $match[1];
- $responsetext .= $row;
- }
- return array( 0 => $responsetext, 1 => intval( $contentlenght ) );
- }
- public function GetResponseText( $strip_html = true, $parse_html = false ) {
- $response = $this->GetResponse();
- if( $response[1] < 1 ) return "";
- $responsetext = substr( $response[0], strlen( $response[0] ) - $response[1], $response[1] );
- if( $strip_html === true ) $responsetext = strip_tags( $responsetext );
- if( $parse_html === false ) $responsetext = htmlentities( $responsetext );
- return $responsetext;
- }
- public function GetResponseHeaders( $as_array = true ) {
- if( !is_resource( $this->Handle ) ) return array();
- echo "Hai";
- $response = $this->GetResponse();
- $responsetext = "";
- if( $response[1] > 0 ) $responsetext = substr( $response[0], 0, strlen( $response[0] )-$response[1] );
- return $as_array === true ? preg_split( "/(\r\n|\n)/i", $responsetext ) : $responsetext;
- }
- }
- //HTTP Request Code Enum
- class HttpWebRequestState {
- //2xx - Success Status Codes
- public static $OK = 200;
- public static $Created = 201;
- public static $Accepted = 202;
- public static $PartialInformation = 203;
- public static $NoResponse = 204;
- //3xx - Redirection Status Codes
- public static $Moved = 301;
- public static $Found = 302;
- public static $Method = 303;
- public static $NotModified = 304;
- //4xx - Client Error Codes
- public static $BadRequest = 400;
- public static $Unauthorized = 401;
- public static $PaymentRequired = 402;
- public static $Forbidden = 403;
- public static $NotFound = 404;
- //5xx - Server Error Codes
- public static $InternalError = 500;
- public static $NotImplemented = 501;
- public static $Overloaded = 502;
- public static $Timeout = 503;
- }
- //A HTTP Webrequest class, default on port 80, handles the reponse headers partial, sends extended information
- class HttpWebRequest extends WebRequest {
- public $Port = 80;
- public $UserAgent = "Unknown";
- public $Accept;
- public $AcceptLanguage = "en-us;en";
- public $AcceptEncoding = "gzip,deflate";
- public $AcceptCharset = "utf-8";
- public $State = 0;
- public function __construct( $url ) {
- parent::__construct( $url );
- $this->Accept = new MimeTypeCollection( "text/html" );
- }
- public function SendRequest( $message, $showmessage = false ) {
- $this->URI->Port = $this->Port;
- $this->URI->Scheme = "http";
- $this->AddHeader( "User-Agent", $this->UserAgent );
- $this->AddHeader( "Accept", strval( $this->Accept ) );
- $this->AddHeader( "Accept-Language", $this->AcceptLanguage );
- $this->AddHeader( "Accept-Encoding", $this->AcceptEncoding );
- $this->AddHeader( "Accept-Charset", $this->AcceptCharset );
- $this->AddHeader( "Connection", "close" );
- $this->Send( $message, $showmessage );
- $responseheaders = $this->GetResponseHeaders( true );
- print_r( $responseheaders );
- if( !count( $responseheaders ) ) return false;
- $method = ""; $status = 0; $statusmessage = "";
- if( preg_match( "/^([^ ]+) ([0-9]{3}) ([^\r\n]+)$/i", $responseheaders[0], $matches ) )
- print_r( $matches );
- }
- }
- $request = new HttpWebRequest( "rcs.dz-net.net/response.php?test=muha" );
- $request->Post( "komm=doch&du=sack" );
- $request->Get( "Eh=du&da=._." );
- $request->SendRequest( "", true );
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement