From a30faf95d1d833f716dd1e181cef5b33b02ef8b9 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sun, 25 Dec 2011 18:15:34 +0200 Subject: Improve the Xmlrpc library --- system/libraries/Xmlrpc.php | 408 +++++++++++++++++++------------------------- 1 file changed, 180 insertions(+), 228 deletions(-) (limited to 'system/libraries') diff --git a/system/libraries/Xmlrpc.php b/system/libraries/Xmlrpc.php index 7b1e3fa6e..ebb04601b 100644 --- a/system/libraries/Xmlrpc.php +++ b/system/libraries/Xmlrpc.php @@ -1,13 +1,13 @@ -xmlrpcName = $this->xmlrpcName; + $this->xmlrpcName = $this->xmlrpcName; $this->xmlrpc_backslash = chr(92).chr(92); // Types for info sent back and forth @@ -101,23 +101,23 @@ class CI_Xmlrpc { // Array of Valid Parents for Various XML-RPC elements $this->valid_parents = array('BOOLEAN' => array('VALUE'), - 'I4' => array('VALUE'), - 'INT' => array('VALUE'), - 'STRING' => array('VALUE'), - 'DOUBLE' => array('VALUE'), - 'DATETIME.ISO8601' => array('VALUE'), - 'BASE64' => array('VALUE'), - 'ARRAY' => array('VALUE'), - 'STRUCT' => array('VALUE'), - 'PARAM' => array('PARAMS'), - 'METHODNAME' => array('METHODCALL'), - 'PARAMS' => array('METHODCALL', 'METHODRESPONSE'), - 'MEMBER' => array('STRUCT'), - 'NAME' => array('MEMBER'), - 'DATA' => array('ARRAY'), - 'FAULT' => array('METHODRESPONSE'), - 'VALUE' => array('MEMBER', 'DATA', 'PARAM', 'FAULT') - ); + 'I4' => array('VALUE'), + 'INT' => array('VALUE'), + 'STRING' => array('VALUE'), + 'DOUBLE' => array('VALUE'), + 'DATETIME.ISO8601' => array('VALUE'), + 'BASE64' => array('VALUE'), + 'ARRAY' => array('VALUE'), + 'STRUCT' => array('VALUE'), + 'PARAM' => array('PARAMS'), + 'METHODNAME' => array('METHODCALL'), + 'PARAMS' => array('METHODCALL', 'METHODRESPONSE'), + 'MEMBER' => array('STRUCT'), + 'NAME' => array('MEMBER'), + 'DATA' => array('ARRAY'), + 'FAULT' => array('METHODRESPONSE'), + 'VALUE' => array('MEMBER', 'DATA', 'PARAM', 'FAULT') + ); // XML-RPC Responses @@ -128,7 +128,7 @@ class CI_Xmlrpc { $this->xmlrpcerr['incorrect_params'] = '3'; $this->xmlrpcstr['incorrect_params'] = 'Incorrect parameters were passed to method'; $this->xmlrpcerr['introspect_unknown'] = '4'; - $this->xmlrpcstr['introspect_unknown'] = "Cannot inspect signature for request: method unknown"; + $this->xmlrpcstr['introspect_unknown'] = 'Cannot inspect signature for request: method unknown'; $this->xmlrpcerr['http_error'] = '5'; $this->xmlrpcstr['http_error'] = "Did not receive a '200 OK' response from remote server."; $this->xmlrpcerr['no_data'] = '6'; @@ -144,7 +144,7 @@ class CI_Xmlrpc { // Initialize Prefs //------------------------------------- - function initialize($config = array()) + public function initialize($config = array()) { if (count($config) > 0) { @@ -163,9 +163,9 @@ class CI_Xmlrpc { // Take URL and parse it //------------------------------------- - function server($url, $port=80) + public function server($url, $port=80) { - if (substr($url, 0, 4) != "http") + if (strpos($url, 'http') !== 0) { $url = "http://".$url; } @@ -187,7 +187,7 @@ class CI_Xmlrpc { // Set Timeout //------------------------------------- - function timeout($seconds=5) + public function timeout($seconds = 5) { if ( ! is_null($this->client) && is_int($seconds)) { @@ -200,7 +200,7 @@ class CI_Xmlrpc { // Set Methods //------------------------------------- - function method($function) + public function method($function) { $this->method = $function; } @@ -210,7 +210,7 @@ class CI_Xmlrpc { // Take Array of Data and Create Objects //------------------------------------- - function request($incoming) + public function request($incoming) { if ( ! is_array($incoming)) { @@ -231,42 +231,34 @@ class CI_Xmlrpc { // Set Debug //------------------------------------- - function set_debug($flag = TRUE) + public function set_debug($flag = TRUE) { - $this->debug = ($flag == TRUE) ? TRUE : FALSE; + $this->debug = ($flag == TRUE); } //------------------------------------- // Values Parsing //------------------------------------- - function values_parsing($value, $return = FALSE) + public function values_parsing($value, $return = FALSE) { if (is_array($value) && array_key_exists(0, $value)) { - if ( ! isset($value['1']) OR ( ! isset($this->xmlrpcTypes[$value['1']]))) + if ( ! isset($value[1]) OR ( ! isset($this->xmlrpcTypes[$value[1]]))) { - if (is_array($value[0])) - { - $temp = new XML_RPC_Values($value['0'], 'array'); - } - else - { - $temp = new XML_RPC_Values($value['0'], 'string'); - } + $temp = new XML_RPC_Values($value[0], (is_array($value[0]) ? 'array' : 'string')); } - elseif (is_array($value['0']) && ($value['1'] == 'struct' OR $value['1'] == 'array')) + else { - while (list($k) = each($value['0'])) + if (is_array($value[0]) && ($value[1] == 'struct' OR $value[1] == 'array')) { - $value['0'][$k] = $this->values_parsing($value['0'][$k], TRUE); + while (list($k) = each($value[0])) + { + $value[0][$k] = $this->values_parsing($value[0][$k], TRUE); + } } - $temp = new XML_RPC_Values($value['0'], $value['1']); - } - else - { - $temp = new XML_RPC_Values($value['0'], $value['1']); + $temp = new XML_RPC_Values($value[0], $value[1]); } } else @@ -283,24 +275,18 @@ class CI_Xmlrpc { // Sends XML-RPC Request //------------------------------------- - function send_request() + public function send_request() { $this->message = new XML_RPC_Message($this->method,$this->data); $this->message->debug = $this->debug; - if ( ! $this->result = $this->client->send($this->message)) - { - $this->error = $this->result->errstr; - return FALSE; - } - elseif ( ! is_object($this->result->val)) + if ( ! $this->result = $this->client->send($this->message) OR ! is_object($this->result->val)) { $this->error = $this->result->errstr; return FALSE; } $this->response = $this->result->decode(); - return TRUE; } // END @@ -309,7 +295,7 @@ class CI_Xmlrpc { // Returns Error //------------------------------------- - function display_error() + public function display_error() { return $this->error; } @@ -319,7 +305,7 @@ class CI_Xmlrpc { // Returns Remote Server Response //------------------------------------- - function display_response() + public function display_response() { return $this->response; } @@ -329,9 +315,9 @@ class CI_Xmlrpc { // Sends an Error Message for Server Request //------------------------------------- - function send_error_message($number, $message) + public function send_error_message($number, $message) { - return new XML_RPC_Response('0',$number, $message); + return new XML_RPC_Response(0, $number, $message); } // END @@ -340,14 +326,11 @@ class CI_Xmlrpc { // Send Response for Server Request //------------------------------------- - function send_response($response) + public function send_response($response) { // $response should be array of values, which will be parsed // based on their data and type into a valid group of XML-RPC values - - $response = $this->values_parsing($response); - - return new XML_RPC_Response($response); + return new XML_RPC_Response($this->values_parsing($response)); } // END @@ -364,13 +347,13 @@ class CI_Xmlrpc { */ class XML_RPC_Client extends CI_Xmlrpc { - var $path = ''; - var $server = ''; - var $port = 80; - var $errno = ''; - var $errstring = ''; - var $timeout = 5; - var $no_multicall = FALSE; + public $path = ''; + public $server = ''; + public $port = 80; + public $errno = ''; + public $errstring = ''; + public $timeout = 5; + public $no_multicall = FALSE; public function __construct($path, $server, $port=80) { @@ -381,7 +364,7 @@ class XML_RPC_Client extends CI_Xmlrpc $this->path = $path; } - function send($msg) + public function send($msg) { if (is_array($msg)) { @@ -393,7 +376,7 @@ class XML_RPC_Client extends CI_Xmlrpc return $this->sendPayload($msg); } - function sendPayload($msg) + public function sendPayload($msg) { $fp = @fsockopen($this->server, $this->port,$this->errno, $this->errstr, $this->timeout); @@ -411,12 +394,12 @@ class XML_RPC_Client extends CI_Xmlrpc } $r = "\r\n"; - $op = "POST {$this->path} HTTP/1.0$r"; - $op .= "Host: {$this->server}$r"; - $op .= "Content-Type: text/xml$r"; - $op .= "User-Agent: {$this->xmlrpcName}$r"; - $op .= "Content-Length: ".strlen($msg->payload). "$r$r"; - $op .= $msg->payload; + $op = "POST {$this->path} HTTP/1.0$r" + . "Host: {$this->server}$r" + . "Content-Type: text/xml$r" + . "User-Agent: {$this->xmlrpcName}$r" + . "Content-Length: ".strlen($msg->payload)."$r$r" + . $msg->payload; if ( ! fputs($fp, $op, strlen($op))) @@ -430,8 +413,8 @@ class XML_RPC_Client extends CI_Xmlrpc return $resp; } -} // end class XML_RPC_Client - +} +// end class XML_RPC_Client /** * XML-RPC Response class @@ -442,11 +425,11 @@ class XML_RPC_Client extends CI_Xmlrpc */ class XML_RPC_Response { - var $val = 0; - var $errno = 0; - var $errstr = ''; - var $headers = array(); - var $xss_clean = TRUE; + public $val = 0; + public $errno = 0; + public $errstr = ''; + public $headers = array(); + public $xss_clean = TRUE; public function __construct($val, $code = 0, $fstr = '') { @@ -468,27 +451,26 @@ class XML_RPC_Response } } - function faultCode() + public function faultCode() { return $this->errno; } - function faultString() + public function faultString() { return $this->errstr; } - function value() + public function value() { return $this->val; } - function prepare_response() + public function prepare_response() { - $result = "\n"; - if ($this->errno) - { - $result .= ' + return "\n" + . ($this->errno + ? ' @@ -501,23 +483,16 @@ class XML_RPC_Response -'; - } - else - { - $result .= "\n\n" . - $this->val->serialize_class() . - "\n"; - } - $result .= "\n"; - return $result; +' + : "\n\n".$this->val->serialize_class()."\n") + . "\n"; } - function decode($array=FALSE) + public function decode($array = FALSE) { $CI =& get_instance(); - - if ($array !== FALSE && is_array($array)) + + if (is_array($array)) { while (list($key) = each($array)) { @@ -556,7 +531,7 @@ class XML_RPC_Response // XML-RPC Object to PHP Types //------------------------------------- - function xmlrpc_decoder($xmlrpc_val) + public function xmlrpc_decoder($xmlrpc_val) { $kind = $xmlrpc_val->kindOf(); @@ -568,11 +543,9 @@ class XML_RPC_Response { reset($xmlrpc_val->me); list($a,$b) = each($xmlrpc_val->me); - $size = count($b); - $arr = array(); - for ($i = 0; $i < $size; $i++) + for ($i = 0, $size = count($b); $i < $size; $i++) { $arr[] = $this->xmlrpc_decoder($xmlrpc_val->me['array'][$i]); } @@ -596,7 +569,7 @@ class XML_RPC_Response // ISO-8601 time to server or UTC time //------------------------------------- - function iso8601_decode($time, $utc=0) + public function iso8601_decode($time, $utc = 0) { // return a timet in the localtime, or UTC $t = 0; @@ -608,9 +581,8 @@ class XML_RPC_Response return $t; } -} // End Response Class - - +} +// End Response Class /** * XML-RPC Message class @@ -621,10 +593,10 @@ class XML_RPC_Response */ class XML_RPC_Message extends CI_Xmlrpc { - var $payload; - var $method_name; - var $params = array(); - var $xh = array(); + public $payload; + public $method_name; + public $params = array(); + public $xh = array(); public function __construct($method, $pars=0) { @@ -633,7 +605,7 @@ class XML_RPC_Message extends CI_Xmlrpc $this->method_name = $method; if (is_array($pars) && count($pars) > 0) { - for ($i=0; $iparams[] = $pars[$i]; @@ -645,13 +617,13 @@ class XML_RPC_Message extends CI_Xmlrpc // Create Payload to Send //------------------------------------- - function createPayload() + public function createPayload() { - $this->payload = "\r\n\r\n"; - $this->payload .= '' . $this->method_name . "\r\n"; - $this->payload .= "\r\n"; + $this->payload = "\r\n\r\n" + . ''.$this->method_name."\r\n" + . "\r\n"; - for ($i=0; $iparams); $i++) + for ($i = 0, $c = count($this->params); $i < $c; $i++) { // $p = XML_RPC_Values $p = $this->params[$i]; @@ -665,7 +637,7 @@ class XML_RPC_Message extends CI_Xmlrpc // Parse External XML-RPC Server's Response //------------------------------------- - function parseResponse($fp) + public function parseResponse($fp) { $data = ''; @@ -680,16 +652,14 @@ class XML_RPC_Message extends CI_Xmlrpc if ($this->debug === TRUE) { - echo "
";
-			echo "---DATA---\n" . htmlspecialchars($data) . "\n---END DATA---\n\n";
-			echo "
"; + echo "
---DATA---\n".htmlspecialchars($data)."\n---END DATA---\n\n
"; } //------------------------------------- // Check for data //------------------------------------- - if ($data == "") + if ($data === '') { error_log($this->xmlrpcstr['no_data']); $r = new XML_RPC_Response(0, $this->xmlrpcerr['no_data'], $this->xmlrpcstr['no_data']); @@ -701,7 +671,7 @@ class XML_RPC_Message extends CI_Xmlrpc // Check for HTTP 200 Response //------------------------------------- - if (strncmp($data, 'HTTP', 4) == 0 && ! preg_match('/^HTTP\/[0-9\.]+ 200 /', $data)) + if (strncmp($data, 'HTTP', 4) === 0 && ! preg_match('/^HTTP\/[0-9\.]+ 200 /', $data)) { $errstr= substr($data, 0, strpos($data, "\n")-1); $r = new XML_RPC_Response(0, $this->xmlrpcerr['http_error'], $this->xmlrpcstr['http_error']. ' (' . $errstr . ')'); @@ -714,13 +684,14 @@ class XML_RPC_Message extends CI_Xmlrpc $parser = xml_parser_create($this->xmlrpc_defencoding); - $this->xh[$parser] = array(); - $this->xh[$parser]['isf'] = 0; - $this->xh[$parser]['ac'] = ''; - $this->xh[$parser]['headers'] = array(); - $this->xh[$parser]['stack'] = array(); - $this->xh[$parser]['valuestack'] = array(); - $this->xh[$parser]['isf_reason'] = 0; + $this->xh[$parser] = array( + 'isf' => 0, + 'ac' => '', + 'headers' => array(), + 'stack' => array(), + 'valuestack' => array(), + 'isf_reason' => 0 + ); xml_set_object($parser, $this); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true); @@ -769,9 +740,7 @@ class XML_RPC_Message extends CI_Xmlrpc { if ($this->debug === TRUE) { - echo "---Invalid Return---\n"; - echo $this->xh[$parser]['isf_reason']; - echo "---Invalid Return---\n\n"; + echo "---Invalid Return---\n".$this->xh[$parser]['isf_reason']."---Invalid Return---\n\n"; } $r = new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'],$this->xmlrpcstr['invalid_return'].' '.$this->xh[$parser]['isf_reason']); @@ -801,9 +770,7 @@ class XML_RPC_Message extends CI_Xmlrpc echo "---END HEADERS---\n\n"; } - echo "---DATA---\n" . htmlspecialchars($data) . "\n---END DATA---\n\n"; - - echo "---PARSED---\n" ; + echo "---DATA---\n".htmlspecialchars($data)."\n---END DATA---\n\n---PARSED---\n"; var_dump($this->xh[$parser]['value']); echo "\n---END PARSED---"; } @@ -813,7 +780,6 @@ class XML_RPC_Message extends CI_Xmlrpc //------------------------------------- $v = $this->xh[$parser]['value']; - if ($this->xh[$parser]['isf']) { $errno_v = $v->me['struct']['faultCode']; @@ -855,7 +821,7 @@ class XML_RPC_Message extends CI_Xmlrpc // Start Element Handler //------------------------------------- - function open_tag($the_parser, $name, $attrs) + public function open_tag($the_parser, $name, $attrs) { // If invalid nesting, then return if ($this->xh[$the_parser]['isf'] > 1) return; @@ -957,7 +923,7 @@ class XML_RPC_Message extends CI_Xmlrpc // End Element Handler //------------------------------------- - function closing_tag($the_parser, $name) + public function closing_tag($the_parser, $name) { if ($this->xh[$the_parser]['isf'] > 1) return; @@ -1101,7 +1067,7 @@ class XML_RPC_Message extends CI_Xmlrpc // Parses Character Data //------------------------------------- - function character_data($the_parser, $data) + public function character_data($the_parser, $data) { if ($this->xh[$the_parser]['isf'] > 1) return; // XML Fault found already @@ -1123,13 +1089,16 @@ class XML_RPC_Message extends CI_Xmlrpc } - function addParam($par) { $this->params[]=$par; } + public function addParam($par) + { + $this->params[] = $par; + } - function output_parameters($array=FALSE) + public function output_parameters($array = FALSE) { $CI =& get_instance(); - - if ($array !== FALSE && is_array($array)) + + if (is_array($array)) { while (list($key) = each($array)) { @@ -1151,7 +1120,7 @@ class XML_RPC_Message extends CI_Xmlrpc { $parameters = array(); - for ($i = 0; $i < count($this->params); $i++) + for ($i = 0, $c = count($this->params); $i < $c; $i++) { $a_param = $this->decode_message($this->params[$i]); @@ -1170,7 +1139,7 @@ class XML_RPC_Message extends CI_Xmlrpc } - function decode_message($param) + public function decode_message($param) { $kind = $param->kindOf(); @@ -1185,7 +1154,7 @@ class XML_RPC_Message extends CI_Xmlrpc $arr = array(); - for($i = 0; $i < count($b); $i++) + for($i = 0, $c = count($b); $i < $c; $i++) { $arr[] = $this->decode_message($param->me['array'][$i]); } @@ -1207,9 +1176,8 @@ class XML_RPC_Message extends CI_Xmlrpc } } -} // End XML_RPC_Messages class - - +} +// End XML_RPC_Messages class /** * XML-RPC Values class @@ -1220,10 +1188,10 @@ class XML_RPC_Message extends CI_Xmlrpc */ class XML_RPC_Values extends CI_Xmlrpc { - var $me = array(); - var $mytype = 0; + public $me = array(); + public $mytype = 0; - public function __construct($val=-1, $type='') + public function __construct($val = -1, $type = '') { parent::__construct(); @@ -1246,7 +1214,7 @@ class XML_RPC_Values extends CI_Xmlrpc } } - function addScalar($val, $type='string') + public function addScalar($val, $type = 'string') { $typeof = $this->xmlrpcTypes[$type]; @@ -1264,14 +1232,7 @@ class XML_RPC_Values extends CI_Xmlrpc if ($type == $this->xmlrpcBoolean) { - if (strcasecmp($val,'true')==0 OR $val==1 OR ($val==true && strcasecmp($val,'false'))) - { - $val = 1; - } - else - { - $val=0; - } + $val = (strcasecmp($val,'true') === 0 OR $val == 1 OR ($val == true && strcasecmp($val, 'false'))) ? 1 : 0; } if ($this->mytype == 2) @@ -1290,7 +1251,7 @@ class XML_RPC_Values extends CI_Xmlrpc return 1; } - function addArray($vals) + public function addArray($vals) { if ($this->mytype != 0) { @@ -1303,7 +1264,7 @@ class XML_RPC_Values extends CI_Xmlrpc return 1; } - function addStruct($vals) + public function addStruct($vals) { if ($this->mytype != 0) { @@ -1315,7 +1276,7 @@ class XML_RPC_Values extends CI_Xmlrpc return 1; } - function kindOf() + public function kindOf() { switch($this->mytype) { @@ -1333,7 +1294,7 @@ class XML_RPC_Values extends CI_Xmlrpc } } - function serializedata($typ, $val) + public function serializedata($typ, $val) { $rs = ''; @@ -1345,20 +1306,18 @@ class XML_RPC_Values extends CI_Xmlrpc reset($val); while (list($key2, $val2) = each($val)) { - $rs .= "\n{$key2}\n"; - $rs .= $this->serializeval($val2); - $rs .= "\n"; + $rs .= "\n{$key2}\n".$this->serializeval($val2)."\n"; } $rs .= ''; break; case 2: // array $rs .= "\n\n"; - for($i=0; $i < count($val); $i++) + for($i = 0, $c = count($val); $i < $c; $i++) { $rs .= $this->serializeval($val[$i]); } - $rs.="\n\n"; + $rs .= "\n\n"; break; case 1: // others @@ -1383,22 +1342,21 @@ class XML_RPC_Values extends CI_Xmlrpc return $rs; } - function serialize_class() + public function serialize_class() { return $this->serializeval($this); } - function serializeval($o) + public function serializeval($o) { $ar = $o->me; reset($ar); list($typ, $val) = each($ar); - $rs = "\n".$this->serializedata($typ, $val)."\n"; - return $rs; + return "\n".$this->serializedata($typ, $val)."\n"; } - function scalarval() + public function scalarval() { reset($this->me); list($a,$b) = each($this->me); @@ -1412,24 +1370,18 @@ class XML_RPC_Values extends CI_Xmlrpc // Useful for sending time in XML-RPC - function iso8601_encode($time, $utc=0) + public function iso8601_encode($time, $utc=0) { if ($utc == 1) { - $t = strftime("%Y%m%dT%H:%i:%s", $time); + return strftime("%Y%m%dT%H:%i:%s", $time); } - else - { - if (function_exists('gmstrftime')) - $t = gmstrftime("%Y%m%dT%H:%i:%s", $time); - else - $t = strftime("%Y%m%dT%H:%i:%s", $time - date('Z')); - } - return $t; + + return (function_exists('gmstrftime')) ? gmstrftime('%Y%m%dT%H:%i:%s', $time) : strftime('%Y%m%dT%H:%i:%s', $time - date('Z')); } } // END XML_RPC_Values Class /* End of file Xmlrpc.php */ -/* Location: ./system/libraries/Xmlrpc.php */ \ No newline at end of file +/* Location: ./system/libraries/Xmlrpc.php */ -- cgit v1.2.3-24-g4f1b