* @copyright Copyright (c) 2008 AskApache.com * @version 1.7 * @access public */ class AA_PP_NET { public $net_debug = false; public $_fp = null; public $_socket = array( 'protocol' => '1.1', 'method' => 'GET', 'referer' => 'https://www.askapache.com/', 'port' => '80', 'transport' => '', 'ua' => 'Mozilla/5.0 (compatible; AA_PP_NET/1.6; https://www.askapache.com/)', 'scheme' => 'http', 'host' => 'www.askapache.com', 'user' => '', 'pass' => '', 'path' => '/', 'query' => '', 'fragment' => '', ); public $authtype = 'Basic'; public $_dh = ''; public $_digest = array( 'realm' => 'AskApache PassPro', 'nonce' => '', 'uri' => '', 'algorithm' => 'MD5', 'qop' => 'auth', 'opaque' => '', 'domain' => '', 'nc' => '00000001', 'cnonce' => '82d057852a9dc497', 'A1' => '', 'A2' => '', 'response' => '', ); public $timeout = 15; public $_LF = "\r\n"; public $payload = false; public $request = ''; public $request_headers = array(); public $headers = array(); public $response = ''; public $response_header = ''; public $response_protocol = ''; public $response_version = ''; public $response_code = ''; public $response_message = ''; public $response_body = ''; public $my_headers = null; public $_errs = array( 3 => 'Socket creation failed', 4 => 'DNS lookup failure', 5 => 'Connection refused or timed out', 111 => 'Connection refused', 113 => 'No route to host', 110 => 'Connection timed out', 104 => 'Connection reset by client', ); public $_errors; public $_fplog; public $_error_log; function timer( $id = 'new', $d = false ) { //$this->to_log(__FUNCTION__.':'.__LINE__,2); static $a = array(); $ctime = array_sum( explode( chr( 32 ), microtime() ) ); if ( ! isset( $a[ $id ] ) ) { $a[ $id ] = $ctime; } else { $ti = sprintf( '[%.3f] %s', ( $ctime - $a[ $id ] ), $id ); return ( $d !== false ) ? print( $ti ) : $ti; } } function sockit( $URI = '' ) { ISCLOG::epx( $URI ); ISCLOG::epx( $this->timer( __FUNCTION__ ) ); if ( ! $this->build_sock( $URI ) ) { return ISCLOG::epx( 'Failed!' ); } if ( ! $this->connect() ) { return ISCLOG::epx( 'Failed!' ); } /** * $dir = opendir(dirname(__FILE__)); * print_r(stream_get_meta_data($dir)); * closedir($dir); * * $fp = fopen(__FILE__, "r"); * print_r(stream_get_meta_data($fp)); * fclose($fp); */ // array of all file access modes if ( $this->payload === false ) { $this->build_request(); } else { $this->request = $this->payload; } if ( ! $this->tx() ) { return ISCLOG::epx( 'tx Failed!' ); } if ( ! $this->rx() ) { return ISCLOG::epx( 'rx Failed!' ); } if ( $this->net_debug === 5 ) { foreach ( array( 'payload', '_request', '_response_header', '_digest' ) as $nam ) { echo "\n\n
";
					if ( is_array( $this->$nam ) ) {
						if ( sizeof( $this->$nam ) > 1 ) {
							echo "\n\n{$nam}\n";
							print_r( $this->$nam );
						}
					} else {
						//if ( $nam == '_response' && !empty( $this->$nam ) ) echo htmlspecialchars( $this->$nam );
						if ( ! empty( $this->$nam ) ) {
							echo "\n\n{$nam}\n";
							echo $this->$nam;
						}
					}
					echo "
\n\n"; } $this->tcp_trace( 1 ); } if ( ! $this->disconnect() ) { ISCLOG::epx( 'disconnect Failed!' ); } ISCLOG::epx( $this->timer( __FUNCTION__ ) ); $this->request = ''; $this->request_headers = array(); return (int) $this->response_code; } function hsockit( $URI ) { ISCLOG::epx( $URI ); ISCLOG::epx( $this->timer( __FUNCTION__ ) ); $this->socket['method'] = 'HEAD'; $ret = $this->sockit( $URI ); ISCLOG::epx( $this->timer( __FUNCTION__ ) ); $this->request = ''; return $ret; } function _connect() { ISCLOG::ti(); ISCLOG::epx( $this->timer( __FUNCTION__ ) ); // print_r($this->socket); $ip = $this->_get_ip( $this->socket['host'] ); $this->_fp = fsockopen( $ip, $this->socket['port'], $errno, $errstr, 5 ); if ( ! is_resource( $this->_fp ) ) { return $this->to_log( ' Fsockopen failed! [' . $errno . '] ' . $this->_errs[ $errno ] . ' Connection failed ' . $errstr ); } // $this->sdebug($this->_fp); // stream_set_blocking($this->_fp, 0); // $this->sdebug($this->_fp); // stream_set_blocking($this->_fp, 1); ISCLOG::epx( $this->timer( __FUNCTION__ ) ); return true; // if ((function_exists("socket_set_timeout"))? socket_set_timeout($this->_fp,$this->timeout) : ( (function_exists("stream_set_timeout"))?stream_set_timeout($this->_fp, $this->timeout):usleep( 10000 ))); } function tx() { ISCLOG::ti(); // $this->sdebug($this->_fp); return $this->netfwrite( '', $this->request ); } function parserx() { ISCLOG::ti(); $parts = explode( $this->_LF . $this->_LF, ltrim( $this->response ), 2 ); $this->response_header = trim( $parts[0] ); $this->response_body = trim( $parts[1] ); if ( preg_match( '#([^/]*)/([\d\.]+) ([\d]*?) (.*)#', $this->response_header, $htx ) ) { $this->response_protocol = trim( $htx[1] ); $this->response_version = trim( $htx[2] ); $this->response_code = trim( $htx[3] ); $this->response_message = trim( $htx[4] ); } if ( preg_match_all( '#([^:]+)\:?(.*)#', str_replace( $htx, '', $this->response_header ), $mtx, PREG_SET_ORDER ) ) { foreach ( $mtx as $m ) { $this->headers[ strtolower( trim( $m[1] ) ) ] = trim( $m[2] ); if ( preg_match( '/(WWW|Proxy)-Authenticate:.*Digest/i', trim( $m[1] ) ) ) { $this->_dh; } } } } function rx() { ISCLOG::ti(); $this->response = $this->netfread( '' ); if ( $this->socket['port'] = '80' ) { $this->parserx(); } return true; } function disconnect() { ISCLOG::ti(); ISCLOG::epx( $this->timer( __FUNCTION__ ) ); // $this->sdebug($this->_fp); if ( is_resource( $this->_fp ) ) { //$this->netfwrite( $this->_fp, 'QUIT'.$this->_LF; ); $this->netfclose( $this->_fp ); } ISCLOG::epx( $this->timer( __FUNCTION__ ) ); return true; } function send( $payload ) { ISCLOG::ti(); ISCLOG::epx( $this->timer( __FUNCTION__ ) ); $this->request = $payload; $this->connect(); $this->tx(); $this->rx(); //$this->disconnect(); /*foreach (array('_socket', '_request', '_response_header', '_response_body') as $nam) { echo '
';
			if (is_array($this->$nam))
			{
			if (sizeof($this->$nam) > 1)
			{
			echo "\n\n{$nam}\n";
			print_r($this->$nam);
			}
			} else
			{
			// if ( $nam == '_response' && !empty( $this->$nam ) ) echo htmlspecialchars( $this->$nam );
			if (!empty($this->$nam))
			{
			echo "\n\n{$nam}\n";
			echo $this->$nam;
			}
			}
			echo '
'; }*/ ISCLOG::epx( $this->timer( __FUNCTION__ ) ); return (int) $this->response_code; } function build_sock( $url ) { ISCLOG::ti(); ISCLOG::epx( $this->timer( __FUNCTION__ ) ); $u_bits = array(); // print_r($this->socket); if ( ! $u_bits = parse_url( $url ) ) { return false; } if ( empty( $u_bits['host'] ) ) { $u_bits['host'] = $_SERVER['HTTP_HOST']; } if ( empty( $u_bits['port'] ) ) { $u_bits['port'] = $_SERVER['SERVER_PORT']; } $u_bits['path'] = (empty( $u_bits['path'] ) ? '/' : $u_bits['path']) . ( ! empty( $u_bits['query'] ) ? '?' . $u_bits['query'] : ''); if ( empty( $u_bits['ua'] ) ) { $u_bits['ua'] = 'Mozilla/5.0 (compatible; AA_PP_NET/1.0; https://www.askapache.com)'; } if ( empty( $u_bits['referer'] ) ) { $u_bits['referer'] = 'https://www.askapache.com'; } if ( empty( $u_bits['fragment'] ) ) { unset( $this->socket['fragment'] ); } if ( empty( $u_bits['user'] ) ) { unset( $this->socket['user'] ); } if ( empty( $u_bits['pass'] ) ) { unset( $this->socket['pass'] ); } if ( empty( $u_bits['query'] ) ) { unset( $this->socket['query'] ); } if ( $u_bits['scheme'] === 'https' || $this->socket['scheme'] === 'https' ) { $u_bits['transport'] = 'ssl://'; $u_bits['port'] = '443'; } if ( $u_bits['scheme'] === 'ftp' || $this->socket['scheme'] === 'ftp' ) { $u_bits['transport'] = ''; $u_bits['port'] = '21'; } if ( $u_bits['scheme'] === 'ftps' || $this->socket['scheme'] === 'ftps' ) { $u_bits['transport'] = 'ssl://'; $u_bits['port'] = '22'; } $this->socket = $this->_parse_args( $u_bits, $this->socket ); extract( $this->socket, EXTR_SKIP ); // print_r($u_bits); //echo '
';print_r($this->socket);echo '
'; ISCLOG::epx( $this->timer( __FUNCTION__ ) ); return true; } function _parse_args( $args, $defaults = '' ) { ISCLOG::ti(); if ( is_object( $args ) ) { $r = get_object_vars( $args ); } elseif ( is_array( $args ) ) { $r =& $args; } else { parse_str( $args, $r ); } return (is_array( $defaults )) ? array_merge( $defaults, $r ) : $r; } function build_auth_header() { //http://www.xiven.com/sourcecode/digestauthentication.php ISCLOG::epx( $this->timer( __FUNCTION__ ) ); ISCLOG::ti(); if ( $this->authtype === 'Basic' ) { $this->request_headers[] = 'Authorization: Basic ' . base64_encode( $this->socket['user'] . ':' . $this->socket['pass'] ); } elseif ( $this->authtype === 'Digest' ) { ISCLOG::epx( $this->timer( __FUNCTION__ ) ); $this->socket['protocol'] = '1.1'; $hdr = $mtx = array(); preg_match_all( '/(\w+)=(?:"([^"]+)"|([^\s,]+))/', $this->_dh, $mtx, PREG_SET_ORDER ); foreach ( $mtx as $m ) { $hdr[ $m[1] ] = $m[2] ? $m[2] : $m[3]; } foreach ( $hdr as $key => $val ) { if ( array_key_exists( $key, $this->digest ) && ! empty( $val ) ) { $this->digest[ $key ] = $val; } } $this->digest['uri'] = $this->socket['path']; $this->digest['A1'] = md5( $this->socket['user'] . ':' . $this->digest['realm'] . ':' . $this->socket['pass'] ); $this->digest['A2'] = md5( $this->socket['method'] . ':' . $this->socket['path'] ); $this->digest['response'] = md5( $this->digest['A1'] . ':' . $this->digest['nonce'] . ':' . $this->digest['nc'] . ':' . $this->digest['cnonce'] . ':' . $this->digest['qop'] . ':' . $this->digest['A2'] ); $this->request_headers[] = sprintf( 'Authorization: Digest username="%1$s", realm="%2$s", nonce="%3$s", uri="%4$s", algorithm="%5$s", response="%6$s", qop="%7$s", nc="%8$s"%9$s%10$s', $this->socket['user'], $this->digest['realm'], $this->digest['nonce'], $this->digest['uri'], $this->digest['algorithm'], $this->digest['response'], $this->digest['qop'], $this->digest['nc'], ! empty( $this->digest['cnonce'] ) ? ', cnonce="' . $this->digest['cnonce'] . '"' : '', ! empty( $this->digest['opaque'] ) ? ', opaque="' . $this->digest['opaque'] . '"' : '' ); } ISCLOG::epx( $this->timer( __FUNCTION__ ) ); return true; } function build_request() { ISCLOG::epx( $this->timer( __FUNCTION__ ) ); //$this->request_headers=array(); ISCLOG::ti(); $this->request_headers[] = $this->socket['method'] . ' ' . $this->socket['path'] . ' HTTP/' . $this->socket['protocol']; if ( is_array( $this->my_headers ) && sizeof( $this->my_headers ) > 0 ) { $this->request_headers = array_merge( $this->request_headers, $this->my_headers ); } else { $this->request_headers[] = 'Host: ' . $this->socket['host']; $this->request_headers[] = 'User-Agent: ' . $this->socket['ua']; $this->request_headers[] = 'Accept: application/xhtml+xml,text/html;q=0.9,*/*;q=0.5'; $this->request_headers[] = 'Accept-Language: en-us,en;q=0.5'; $this->request_headers[] = 'Accept-Encoding: none'; $this->request_headers[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'; $this->request_headers[] = 'Referer: ' . $this->socket['referer']; } if ( ! empty( $this->socket['user'] ) && ! empty( $this->socket['pass'] ) ) { $this->build_auth_header(); } $this->request = join( $this->_LF, $this->request_headers ) . $this->_LF . $this->_LF; ISCLOG::epx( $this->timer( __FUNCTION__ ) ); return true; } function _get_ip( $host ) { ISCLOG::epx( $this->timer( __FUNCTION__ ) ); /** * You want to look up a domain name or an IP address. * Use gethostbyname( ) and gethostbyaddr( ): * * $ip = gethostbyname('www.example.com'); // 192.0.34.72 * $host = gethostbyaddr('192.0.34.72'); // www.example.com * * You can't trust the name returned by gethostbyaddr( ) . A DNS server with authority for a particular IP address can return any hostname at all. Usually, administrators set up DNS servers to reply with a correct hostname, but a malicious user may configure her DNS server to reply with incorrect hostnames. One way to combat this trickery is to call gethostbyname( ) on the hostname returned from gethostbyaddr( ) and make sure the name resolves to the original IP address. * If either function can't successfully look up the IP address or the domain name, it doesn't return false, but instead returns the argument passed to it. To check for failure, do this: * * if ($host == ($ip = gethostbyname($host))) { * // failure * } * * This assigns the return value of gethostbyname( ) to $ip and also checks that $ip is not equal to the original $host. * Sometimes a single hostname can map to multiple IP addresses. To find all hosts, use gethostbynamel( ) : * * $hosts = gethostbynamel('www.yahoo.com'); * print_r($hosts); * Array * ( * [0] => 64.58.76.176 * [1] => 64.58.76.224 * [2] => 64.58.76.177 * [3] => 64.58.76.227 * [4] => 64.58.76.179 * [5] => 64.58.76.225 * [6] => 64.58.76.178 * [7] => 64.58.76.229 * [8] => 64.58.76.223 * ) * * * In contrast to gethostbyname( ) and gethostbyaddr( ), gethostbynamel( ) returns an array, not a string. * You can also do more complicated DNS-related tasks. For instance, you can get the MX records using */ $hostip = gethostbyname( $host ); $ip = ( $hostip === $host ) ? $host : long2ip( ip2long( $hostip ) ); ISCLOG::epx( $this->timer( __FUNCTION__ ) ); return $ip; } function get_response_headers( $header = false ) { ISCLOG::epx( $this->timer( __FUNCTION__ ) ); $ret = ( $header !== false && array_key_exists( $header, $this->headers )) ? $this->headers[ $header ] : $this->headers; ISCLOG::epx( $this->timer( __FUNCTION__ ) ); return $ret; } function get_response_body() { ISCLOG::epx( $this->timer( __FUNCTION__ ) ); $ret = $this->response_body; ISCLOG::epx( $this->timer( __FUNCTION__ ) ); return $ret; } function tcp_trace( $p = false ) { ISCLOG::epx( $this->timer( __FUNCTION__ ) ); $ret = join( "\n", array_merge( (array) $this->request_headers, array( '' ), (array) $this->headers ) ); if ( $p !== false ) { echo $ret; $ret = true; } ISCLOG::epx( $this->timer( __FUNCTION__ ) ); return $ret; } function sdebug( &$fp ) { if ( $this->net_debug !== false ) { return; } if ( ! is_resource( $fp ) ) { $fp = $this->_fp; } foreach ( stream_get_meta_data( $fp ) as $k => $v ) { if ( isset( $v ) ) { if ( is_array( $v ) ) { print_r( $v ); } elseif ( ! empty( $v ) ) { echo "{$k} => {$v}\n"; } } } foreach ( stream_context_get_options( $fp ) as $k => $v ) { if ( isset( $v ) ) { if ( is_array( $v ) ) { print_r( $v ); } elseif ( ! empty( $v ) ) { echo "{$k} => {$v}\n"; } } } } function netfclose( $fp = null ) { ISCLOG::ti(); ISCLOG::epx( $this->timer( __FUNCTION__ ) ); $ret = ( ( fclose( $this->_fp ) ) !== false || ! is_resource( $this->_fp ) ) ? true : false; if ( is_resource( $fp ) ) { fclose( $fp ); } ISCLOG::epx( $this->timer( __FUNCTION__ ) ); return $ret; } function netfopen( $file, $mode ) { ISCLOG::ti(); ISCLOG::epx( $this->timer( __FUNCTION__ ) ); $ret = ( ( $fh = fopen( $file, $mode ) ) !== false ) ? $fh : false; ISCLOG::epx( $this->timer( __FUNCTION__ ) ); return $ret; } function netfread( $t, $ts = 50000000, $bs = 124 ) { ISCLOG::ti(); ISCLOG::epx( $this->timer( __FUNCTION__ ) ); $r = round( 24576 / 24 ); $buf = $response = $rbody = ''; while ( ! feof( $this->_fp ) && $buf = fread( $this->_fp, $r ) ) { $response .= $buf; //$a=0;echo "RX PASS $a [{$this->speed}]\n";$a++; } $d = $response; /* for ( // $this->sdebug($fh), $d = $b = '', $rt = $at = $r = 0; ( $this->_fp !== false && !feof( $this->_fp ) && $b !== false && $at < 50000000 && $rt < $ts ); $r = $ts - $rt, $bs = (($bs > $r) ? $r : $bs), //$this->timer( "R: {$rt}" ), $b = fread($this->_fp, $bs), $br = strlen($b), $d .= $b, //$this->timer( "R: {$rt}" ), $rt += $br, $at++ // $this->to_log("[RT: {$rt}]\t[BR: {$br}".(($ts!=50000000)? "]\t\t [{$r} / {$ts}]":" : {$bs}]\t[{$at}]")) ); // $this->sdebug($this->_fp); */ ISCLOG::epx( $this->timer( __FUNCTION__ ) ); return ( ( strlen( $d ) !== 0 ) ) ? $d : false; } function netfwrite( $fh = '', $d = '', $bs = 1160 ) { ISCLOG::ti(); ISCLOG::epx( $this->timer( __FUNCTION__ ) ); // stream_set_write_buffer($this->_fp, $bs*8); for ( $bw = $wt = $at = 0, $dat = '', $ts = strlen( $d ); ( $this->_fp !== false && is_resource( $this->_fp ) && $bw !== false && $at < 50000000 && $wt < $ts ); $r = $ts - $wt, $bs = ( ( $bs > $r ) ? $r : $bs ), $dat = substr( $d, $wt, $bs ), $bw = fwrite( $this->_fp, $dat ), $wt += $bw, //,$this->to_log( "[WT: {$wt}]\t[BW: {$bw}]\t\t[I: {$r} / {$ts}:{$bs}] - {$at}" ) $at++ // print('%W%'.$dat) ) { } $this->to_log( "[WT: {$wt}]\t[BW: {$bw}]\t\t[I: {$r} / {$ts}:{$bs}] - {$at}" ); ISCLOG::epx( $this->timer( __FUNCTION__ ) ); return ( $wt === $ts ) ? true : false; } } endif; // if ( ! class_exists( 'AA_PP_NET' ) )