?? rpc.php
字號:
include_once 'PEAR.php'; PEAR::raiseError(get_class($this) . ": " . $msg, $code); }}class XML_RPC_Client extends XML_RPC_Base { var $path; var $server; var $port; var $errno; var $errstring; var $debug = 0; var $username = ""; var $password = ""; function XML_RPC_Client($path, $server, $port = 80, $proxy = '', $proxy_port = 8080, $proxy_user = '', $proxy_pass = '') { $this->port = $port; $this->server = $server; $this->path = $path; $this->proxy = $proxy; $this->proxy_port = $proxy_port; $this->proxy_user = $proxy_user; $this->proxy_pass = $proxy_pass; } function setDebug($in) { if ($in) { $this->debug = 1; } else { $this->debug = 0; } } function setCredentials($u, $p) { $this->username = $u; $this->password = $p; } function send($msg, $timeout = 0) { // where msg is an xmlrpcmsg $msg->debug = $this->debug; return $this->sendPayloadHTTP10($msg, $this->server, $this->port, $timeout, $this->username, $this->password); } function sendPayloadHTTP10($msg, $server, $port, $timeout=0, $username = "", $password = "") { // If we're using a proxy open a socket to the proxy server instead to the xml-rpc server if ($this->proxy){ if ($timeout > 0) { $fp = fsockopen($this->proxy, $this->proxy_port, $this->errno, $this->errstr, $timeout); } else { $fp = fsockopen($this->proxy, $this->proxy_port, $this->errno, $this->errstr); } } else { if ($timeout > 0) { $fp = fsockopen($server, $port, $this->errno, $this->errstr, $timeout); } else { $fp = fsockopen($server, $port, $this->errno, $this->errstr); } } if (!$fp && $this->proxy) { $this->raiseError( "Connection to proxy server " . $this->proxy . ":" . $this->proxy_port . " failed", XML_RPC_ERROR_CONNECTION_FAILED); } elseif (!$fp) { "Connection to RPC server " . $this->server . " failed", XML_RPC_ERROR_CONNECTION_FAILED); } // Only create the payload if it was not created previously if (empty($msg->payload)) { $msg->createPayload(); } // thanks to Grant Rauscher <grant7@firstworld.net> for this $credentials = ""; if ($username != "") { $credentials = "Authorization: Basic " . base64_encode($username . ":" . $password) . "\r\n"; } if ($this->proxy) { $op = "POST http://" . $this->server; if ($this->proxy_port) { $op .= ":" . $this->port; } } else { $op = "POST "; } $op .= $this->path. " HTTP/1.0\r\n" . "User-Agent: PEAR XML_RPC\r\n" . "Host: " . $this->server . "\r\n"; if ($this->proxy && $this->proxy_user != '') { $op .= 'Proxy-Authorization: Basic ' . base64_encode($this->proxy_user . ':' . $this->proxy_pass) . "\r\n"; } $op .= $credentials . "Content-Type: text/xml\r\n" . "Content-Length: " . strlen($msg->payload) . "\r\n\r\n" . $msg->payload; if (!fputs($fp, $op, strlen($op))) { $this->errstr = "Write error"; return 0; } $resp = $msg->parseResponseFile($fp); fclose($fp); return $resp; }}class XML_RPC_Response extends XML_RPC_Base{ var $xv; var $fn; var $fs; var $hdrs; function XML_RPC_Response($val, $fcode = 0, $fstr = "") { if ($fcode != 0) { $this->fn = $fcode; $this->fs = htmlspecialchars($fstr); } else { $this->xv = $val; } } function faultCode() { if (isset($this->fn)) { return $this->fn; } else { return 0; } } function faultString() { return $this->fs; } function value() { return $this->xv; } function serialize() { $rs = "<methodResponse>\n"; if ($this->fn) { $rs .= "<fault> <value> <struct> <member> <name>faultCode</name> <value><int>" . $this->fn . "</int></value> </member> <member> <name>faultString</name> <value><string>" . $this->fs . "</string></value> </member> </struct> </value></fault>"; } else { $rs .= "<params>\n<param>\n" . $this->xv->serialize() . "</param>\n</params>"; } $rs .= "\n</methodResponse>"; return $rs; }}class XML_RPC_Message extends XML_RPC_Base{ var $payload; var $methodname; var $params = array(); var $debug = 0; function XML_RPC_Message($meth, $pars = 0) { $this->methodname = $meth; if (is_array($pars) && sizeof($pars)>0) { for($i = 0; $i < sizeof($pars); $i++) { $this->addParam($pars[$i]); } } } function xml_header() { return "<?xml version=\"1.0\"?>\n<methodCall>\n"; } function xml_footer() { return "</methodCall>\n"; } function createPayload() { $this->payload = $this->xml_header(); $this->payload .= "<methodName>" . $this->methodname . "</methodName>\n"; $this->payload .= "<params>\n"; for($i = 0; $i < sizeof($this->params); $i++) { $p = $this->params[$i]; $this->payload .= "<param>\n" . $p->serialize() . "</param>\n"; } $this->payload .= "</params>\n"; $this->payload .= $this->xml_footer(); $this->payload = str_replace("\n", "\r\n", $this->payload); } function method($meth = "") { if ($meth != "") { $this->methodname = $meth; } return $this->methodname; } function serialize() { $this->createPayload(); return $this->payload; } function addParam($par) { $this->params[] = $par; } function getParam($i) { return $this->params[$i]; } function getNumParams() { return sizeof($this->params); } function parseResponseFile($fp) { $ipd = ""; while($data = fread($fp, 32768)) { $ipd .= $data; } return $this->parseResponse($ipd); } function parseResponse($data = "") { global $XML_RPC_xh,$XML_RPC_err,$XML_RPC_str; global $XML_RPC_defencoding; $parser = xml_parser_create($XML_RPC_defencoding); $XML_RPC_xh[$parser] = array(); $XML_RPC_xh[$parser]['st'] = ""; $XML_RPC_xh[$parser]['cm'] = 0; $XML_RPC_xh[$parser]['isf'] = 0; $XML_RPC_xh[$parser]['ac'] = ""; $XML_RPC_xh[$parser]['qt'] = ""; xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true); xml_set_element_handler($parser, "XML_RPC_se", "XML_RPC_ee"); xml_set_character_data_handler($parser, "XML_RPC_cd"); xml_set_default_handler($parser, "XML_RPC_dh"); $xmlrpc_value = new XML_RPC_Value; $hdrfnd = 0; if ($this->debug) { print "<PRE>---GOT---\n"; print isset($_SERVER['SERVER_PROTOCOL']) ? htmlspecialchars($data) : $data; print "\n---END---\n</PRE>"; } // see if we got an HTTP 200 OK, else bomb // but only do this if we're using the HTTP protocol. if (ereg("^HTTP",$data) && !ereg("^HTTP/[0-9\.]+ 200 ", $data)) { $errstr = substr($data, 0, strpos($data, "\n")-1); error_log("HTTP error, got response: " . $errstr); $r = new XML_RPC_Response(0, $XML_RPC_err["http_error"], $XML_RPC_str["http_error"] . " (" . $errstr . ")"); xml_parser_free($parser); return $r; } // gotta get rid of headers here if ((!$hdrfnd) && ($brpos = strpos($data,"\r\n\r\n"))) { $XML_RPC_xh[$parser]['ha'] = substr($data, 0, $brpos); $data = substr($data, $brpos + 4); $hdrfnd = 1; } // be tolerant of junk after methodResponse (e.g. javascript automatically inserted by free hosts) // thanks to Luca Mariano <luca.mariano@email.it> $data = substr($data, 0, strpos($data, "</methodResponse>") + 17); if (!xml_parse($parser, $data, sizeof($data))) { // thanks to Peter Kocks <peter.kocks@baygate.com> if ((xml_get_current_line_number($parser)) == 1) { $errstr = "XML error at line 1, check URL"; } else { $errstr = sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser)); } error_log($errstr); $r = new XML_RPC_Response(0, $XML_RPC_err["invalid_return"], $XML_RPC_str["invalid_return"]); xml_parser_free($parser); return $r; } xml_parser_free($parser); if ($this->debug) { print "<PRE>---EVALING---[" . strlen($XML_RPC_xh[$parser]['st']) . " chars]---\n" . htmlspecialchars($XML_RPC_xh[$parser]['st']) . ";\n---END---</PRE>"; } if (strlen($XML_RPC_xh[$parser]['st']) == 0) { // then something odd has happened // and it's time to generate a client side error // indicating something odd went on $r = new XML_RPC_Response(0, $XML_RPC_err["invalid_return"], $XML_RPC_str["invalid_return"]); } else { eval('$v=' . $XML_RPC_xh[$parser]['st'] . '; $allOK=1;'); if ($XML_RPC_xh[$parser]['isf']) { $f = $v->structmem("faultCode"); $fs = $v->structmem("faultString"); $r = new XML_RPC_Response($v, $f->scalarval(), $fs->scalarval()); } else { $r = new XML_RPC_Response($v); } } $r->hdrs = split("\r?\n", $XML_RPC_xh[$parser]['ha'][1]); return $r; }}class XML_RPC_Value extends XML_RPC_Base{ var $me = array(); var $mytype = 0; function XML_RPC_Value($val = -1, $type = "") { global $XML_RPC_Types; $this->me = array(); $this->mytype = 0; if ($val != -1 || $type != "") { if ($type == "") { $type="string"; } if ($XML_RPC_Types[$type] == 1) { $this->addScalar($val,$type); } elseif ($XML_RPC_Types[$type] == 2) { $this->addArray($val); } elseif ($XML_RPC_Types[$type] == 3) { $this->addStruct($val); } } } function addScalar($val, $type = "string") { global $XML_RPC_Types, $XML_RPC_Boolean;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -