DirectAdmin APIhttpsocket.php文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DirectAdmin APIhttpsocket.php文件相关的知识,希望对你有一定的参考价值。
Socket communication class for DirectAdmin
<?php /** * Socket communication class. * * Originally designed for use with DirectAdmin's API, this class will fill any HTTP socket need. * * Very, very basic usage: * $Socket = new HTTPSocket; * echo $Socket->get('http://user:[email protected]/somedir/some.file?query=string&this=that'); * * @author Phi1 'l0rdphi1' Stier <[email protected]> * @package HTTPSocket * @version 2.6 */ class HTTPSocket { var $version = '2.6'; /* all vars are private except $error, $query_cache, and $doFollowLocationHeader */ var $method = 'GET'; var $remote_host; var $remote_port; var $remote_uname; var $remote_passwd; var $result; var $result_header; var $result_body; var $result_status_code; var $lastTransferSpeed; var $bind_host; var $doFollowLocationHeader = TRUE; var $redirectURL; /** * Create server "connection". * */ function connect($host, $port = '' ) { { $port = 80; } $this->remote_host = $host; $this->remote_port = $port; } function bind( $ip = '' ) { if ( $ip == '' ) { $ip = $_SERVER['SERVER_ADDR']; } $this->bind_host = $ip; } /** * Change the method being used to communicate. * * @param string|null request method. supports GET, POST, and HEAD. default is GET */ function set_method( $method = 'GET' ) { } /** * Specify a username and password. * * @param string|null username. defualt is null * @param string|null password. defualt is null */ function set_login( $uname = '', $passwd = '' ) { { $this->remote_uname = $uname; } { $this->remote_passwd = $passwd; } } /** * Query the server * * @param string containing properly formatted server API. See DA API docs and examples. Http:// URLs O.K. too. * @param string|array query to pass to url * @param int if connection KB/s drops below value here, will drop connection */ function query( $request, $content = '', $doSpeedCheck = 0 ) { $this->result_status_code = NULL; // is our request a http:// ... ? { $this->connect($location['host'],$location['port']); $this->set_login($location['user'],$location['pass']); $request = $location['path']; $content = $location['query']; { $request = '/'; } } 'User-Agent' => "HTTPSocket/$this->version", 'Host' => ( $this->remote_port == 80 ? $this->remote_host : "$this->remote_host:$this->remote_port" ), 'Accept' => '*/*', 'Connection' => 'Close' ); foreach ( $this->extra_headers as $key => $value ) { $array_headers[$key] = $value; } $this->result = $this->result_header = $this->result_body = ''; // was content sent as an array? if so, turn it into a string { foreach ( $content as $key => $value ) { } } $OK = TRUE; // instance connection if ($this->bind_host) { { $OK = FALSE; } } else { } if ( !$socket || !$OK ) { $this->error[] = "Can't create socket connection to $this->remote_host:$this->remote_port."; return 0; } // if we have a username and password, add the header { $array_headers['Authorization'] = 'Basic '.base64_encode("$this->remote_uname:$this->remote_passwd"); } // for DA skins: if $this->remote_passwd is NULL, try to use the login key system { $array_headers['Cookie'] = "session={$_SERVER['SESSION_ID']}; key={$_SERVER['SESSION_KEY']}"; } // if method is POST, add content length & type headers if ( $this->method == 'POST' ) { $array_headers['Content-type'] = 'application/x-www-form-urlencoded'; } // else method is GET or HEAD. we don't support anything else right now. else { if ($content) { $request .= "?$content"; } } // prepare query $query = "$this->method $request HTTP/1.0 "; foreach ( $array_headers as $key => $value ) { $query .= "$key: $value "; } $query .= " "; // if POST we need to append our content if ( $this->method == 'POST' && $content ) { $query .= "$content "; } // query connection if ($this->bind_host) { // now load results { $this->result .= $out; } } else { // now load results $this->lastTransferSpeed = 0; $length = 0; $prevSecond = 0; { $this->result .= $chunk; if ( $elapsedTime > 0 ) { $this->lastTransferSpeed = ($length/1024)/$elapsedTime; } if ( $doSpeedCheck > 0 && $elapsedTime > 5 && $this->lastTransferSpeed < $doSpeedCheck ) { $this->warn[] = "kB/s for last 5 seconds is below 50 kB/s (~".( ($length/1024)/$elapsedTime )."), dropping connection..."; $this->result_status_code = 503; break; } } if ( $this->lastTransferSpeed == 0 ) { $this->lastTransferSpeed = $length/1024; } } ",$this->result,2); if ($this->bind_host) { } else { } $this->query_cache[] = $query; $headers = $this->fetch_header(); // what return status did we get? if (!$this->result_status_code) { $this->result_status_code = $matches[1]; } // did we get the full file? { $this->result_status_code = 206; } // now, if we're being passed a location header, should we follow it? if ($this->doFollowLocationHeader) { if ($headers['location']) { $this->redirectURL = $headers['location']; $this->query($headers['location']); } } } function getTransferSpeed() { return $this->lastTransferSpeed; } /** * The quick way to get a URL's content :) * * @param string URL * @param boolean return as array? (like PHP's file() command) * @return string result body */ function get($location, $asArray = FALSE ) { $this->query($location); if ( $this->get_status_code() == 200 ) { if ($asArray) { } return $this->fetch_body(); } return FALSE; } /** * Returns the last status code. * 200 = OK; * 403 = FORBIDDEN; * etc. * * @return int status code */ function get_status_code() { return $this->result_status_code; } /** * Adds a header, sent with the next query. * * @param string header name * @param string header value */ function add_header($key,$value) { $this->extra_headers[$key] = $value; } /** * Clears any extra headers. * */ function clear_headers() { } /** * Return the result of a query. * * @return string result */ function fetch_result() { return $this->result; } /** * Return the header of result (stuff before body). * * @param string (optional) header to return * @return array result header */ function fetch_header( $header = '' ) { ",$this->result_header); foreach ( $array_headers as $pair ) { } if ( $header != '' ) { } return $array_return; } /** * Return the body of result (stuff after header). * * @return string result body */ function fetch_body() { return $this->result_body; } /** * Return parsed body in array format. * * @return array result parsed */ function fetch_parsed_body() { return $x; } } ?>
以上是关于DirectAdmin APIhttpsocket.php文件的主要内容,如果未能解决你的问题,请参考以下文章