git-svn-id: https://svn.code.sf.net/p/phpxmlrpc/code/trunk/xmlrpc@59 013ecfd8-0664...
[plcapi.git] / lib / xmlrpc.inc
index 4ac287a..5269633 100644 (file)
                var $proxy_pass='';\r
                var $proxy_authtype=1;\r
                var $cookies=array();\r
-           var $extracurlopts=array();\r
+               var $extracurlopts=array();\r
 \r
                /**\r
                * List of http compression methods accepted by the client for responses.\r
                * valid strings are 'xmlrpcvals', 'phpvals' or 'xml'\r
                */\r
                var $return_type = 'xmlrpcvals';\r
+               /**\r
+               * Sent to servers in http headers\r
+               */\r
+               var $user_agent;\r
 \r
                /**\r
                * @param string $path either the complete server URL or the PATH part of the xmlrc server URL, e.g. /xmlrpc/server.php\r
 \r
                        // by default the xml parser can support these 3 charset encodings\r
                        $this->accepted_charset_encodings = array('UTF-8', 'ISO-8859-1', 'US-ASCII');\r
+\r
+                       // initialize user_agent string\r
+                       $this->user_agent = $GLOBALS['xmlrpcName'] . ' ' . $GLOBALS['xmlrpcVersion'];\r
                }\r
 \r
                /**\r
                        }\r
                }\r
 \r
-           /**\r
-           * Directly set cURL options, for extra flexibility\r
-           * It allows eg. to bind client to a specific IP interface / address\r
-           * @param $options array\r
-           */\r
-           function SetCurlOptions( $options )\r
-           {\r
-               $this->extracurlopts = $options;\r
-           }\r
+               /**\r
+               * Directly set cURL options, for extra flexibility\r
+               * It allows eg. to bind client to a specific IP interface / address\r
+               * @param $options array\r
+               */\r
+               function SetCurlOptions( $options )\r
+               {\r
+                       $this->extracurlopts = $options;\r
+               }\r
+\r
+               /**\r
+               * Set user-agent string that will be used by this client instance\r
+               * in http headers sent to the server\r
+               */\r
+               function SetUserAgent( $agentstring )\r
+               {\r
+                       $this->user_agent = $agentstring;\r
+               }\r
 \r
                /**\r
                * Send an xmlrpc request\r
                                $credentials='Authorization: Basic ' . base64_encode($username . ':' . $password) . "\r\n";\r
                                if ($authtype != 1)\r
                                {\r
-                                       error_log('XML-RPC: xmlrpc_client::send: warning. Only Basic auth is supported with HTTP 1.0');\r
+                                       error_log('XML-RPC: '.__METHOD__.': warning. Only Basic auth is supported with HTTP 1.0');\r
                                }\r
                        }\r
 \r
                                {\r
                                        if ($proxyauthtype != 1)\r
                                        {\r
-                                               error_log('XML-RPC: xmlrpc_client::send: warning. Only Basic auth to proxy is supported with HTTP 1.0');\r
+                                               error_log('XML-RPC: '.__METHOD__.': warning. Only Basic auth to proxy is supported with HTTP 1.0');\r
                                        }\r
                                        $proxy_credentials = 'Proxy-Authorization: Basic ' . base64_encode($proxyusername.':'.$proxypassword) . "\r\n";\r
                                }\r
                        }\r
 \r
                        $op= 'POST ' . $uri. " HTTP/1.0\r\n" .\r
-                               'User-Agent: ' . $GLOBALS['xmlrpcName'] . ' ' . $GLOBALS['xmlrpcVersion'] . "\r\n" .\r
+                               'User-Agent: ' . $this->user_agent . "\r\n" .\r
                                'Host: '. $server . ':' . $port . "\r\n" .\r
                                $credentials .\r
                                $proxy_credentials .\r
                        {\r
                                curl_setopt($curl, CURLOPT_VERBOSE, 1);\r
                        }\r
-                       curl_setopt($curl, CURLOPT_USERAGENT, $GLOBALS['xmlrpcName'].' '.$GLOBALS['xmlrpcVersion']);\r
+                       curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent);\r
                        // required for XMLRPC: post the data\r
                        curl_setopt($curl, CURLOPT_POST, 1);\r
                        // the data\r
                                }\r
                                else if ($authtype != 1)\r
                                {\r
-                                       error_log('XML-RPC: xmlrpc_client::send: warning. Only Basic auth is supported by the current PHP/curl install');\r
+                                       error_log('XML-RPC: '.__METHOD__.': warning. Only Basic auth is supported by the current PHP/curl install');\r
                                }\r
                        }\r
 \r
                                        }\r
                                        else if ($proxyauthtype != 1)\r
                                        {\r
-                                               error_log('XML-RPC: xmlrpc_client::send: warning. Only Basic auth to proxy is supported by the current PHP/curl install');\r
+                                               error_log('XML-RPC: '.__METHOD__.': warning. Only Basic auth to proxy is supported by the current PHP/curl install');\r
                                        }\r
                                }\r
                        }\r
                                curl_setopt($curl, CURLOPT_COOKIE, substr($cookieheader, 0, -2));\r
                        }\r
 \r
-                   foreach ($this->extracurlopts as $opt => $val)\r
-                   {\r
-                       curl_setopt($curl, $opt, $val);\r
-                   }\r
+                       foreach ($this->extracurlopts as $opt => $val)\r
+                       {\r
+                               curl_setopt($curl, $opt, $val);\r
+                       }\r
 \r
                        $result = curl_exec($curl);\r
 \r
@@ -2262,7 +2278,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                        }\r
                                        else\r
                                        {\r
-                                               error_log('XML-RPC: xmlrpcmsg::parseResponse: HTTPS via proxy error, tunnel connection possibly failed');\r
+                                               error_log('XML-RPC: '.__METHOD__.': HTTPS via proxy error, tunnel connection possibly failed');\r
                                                $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['http_error'], $GLOBALS['xmlrpcstr']['http_error']. ' (HTTPS via proxy error, tunnel connection possibly failed)');\r
                                                return $r;\r
                                        }\r
@@ -2283,7 +2299,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                if(!preg_match('/^HTTP\/[0-9.]+ 200 /', $data))\r
                                {\r
                                        $errstr= substr($data, 0, strpos($data, "\n")-1);\r
-                                       error_log('XML-RPC: xmlrpcmsg::parseResponse: HTTP error, got response: ' .$errstr);\r
+                                       error_log('XML-RPC: '.__METHOD__.': HTTP error, got response: ' .$errstr);\r
                                        $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['http_error'], $GLOBALS['xmlrpcstr']['http_error']. ' (' . $errstr . ')');\r
                                        return $r;\r
                                }\r
@@ -2408,7 +2424,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                        {\r
                                                if(!$data = decode_chunked($data))\r
                                                {\r
-                                                       error_log('XML-RPC: xmlrpcmsg::parseResponse: errors occurred when trying to rebuild the chunked data received from server');\r
+                                                       error_log('XML-RPC: '.__METHOD__.': errors occurred when trying to rebuild the chunked data received from server');\r
                                                        $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['dechunk_fail'], $GLOBALS['xmlrpcstr']['dechunk_fail']);\r
                                                        return $r;\r
                                                }\r
@@ -2438,14 +2454,14 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                                                }\r
                                                                else\r
                                                                {\r
-                                                                       error_log('XML-RPC: xmlrpcmsg::parseResponse: errors occurred when trying to decode the deflated data received from server');\r
+                                                                       error_log('XML-RPC: '.__METHOD__.': errors occurred when trying to decode the deflated data received from server');\r
                                                                        $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['decompress_fail'], $GLOBALS['xmlrpcstr']['decompress_fail']);\r
                                                                        return $r;\r
                                                                }\r
                                                        }\r
                                                        else\r
                                                        {\r
-                                                               error_log('XML-RPC: xmlrpcmsg::parseResponse: the server sent deflated data. Your php install must have the Zlib extension compiled in to support this.');\r
+                                                               error_log('XML-RPC: '.__METHOD__.': the server sent deflated data. Your php install must have the Zlib extension compiled in to support this.');\r
                                                                $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['cannot_decompress'], $GLOBALS['xmlrpcstr']['cannot_decompress']);\r
                                                                return $r;\r
                                                        }\r
@@ -2453,7 +2469,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                        }\r
                                } // end of 'if needed, de-chunk, re-inflate response'\r
 \r
-                               // real stupid hack to avoid PHP complaining about returning NULL by ref\r
+                               // real stupid hack to avoid PHP complaining about returning NULL by ref\r
                                $r = null;\r
                                $r =& $r;\r
                                return $r;\r
@@ -2477,7 +2493,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
 \r
                        if($data == '')\r
                        {\r
-                               error_log('XML-RPC: xmlrpcmsg::parseResponse: no response received from server.');\r
+                               error_log('XML-RPC: '.__METHOD__.': no response received from server.');\r
                                $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['no_data'], $GLOBALS['xmlrpcstr']['no_data']);\r
                                return $r;\r
                        }\r
@@ -2522,17 +2538,10 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
 \r
                        // be tolerant of junk after methodResponse (e.g. javascript ads automatically inserted by free hosts)\r
                        // idea from Luca Mariano <luca.mariano@email.it> originally in PEARified version of the lib\r
-                       $bd = false;\r
-                       // Poor man's version of strrpos for php 4...\r
-                       $pos = strpos($data, '</methodResponse>');\r
-                       while($pos || is_int($pos))\r
-                       {\r
-                               $bd = $pos+17;\r
-                               $pos = strpos($data, '</methodResponse>', $bd);\r
-                       }\r
-                       if($bd)\r
+                       $pos = strrpos($data, '</methodResponse>');\r
+                       if($pos !== false)\r
                        {\r
-                               $data = substr($data, 0, $bd);\r
+                               $data = substr($data, 0, $pos+17);\r
                        }\r
 \r
                        // if user wants back raw xml, give it to him\r
@@ -2563,7 +2572,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                        // makes the lib about 200% slower...\r
                        //if (!is_valid_charset($resp_encoding, array('UTF-8', 'ISO-8859-1', 'US-ASCII')))\r
                        {\r
-                               error_log('XML-RPC: xmlrpcmsg::parseResponse: invalid charset encoding of received response: '.$resp_encoding);\r
+                               error_log('XML-RPC: '.__METHOD__.': invalid charset encoding of received response: '.$resp_encoding);\r
                                $resp_encoding = $GLOBALS['xmlrpc_defencoding'];\r
                        }\r
                        $parser = xml_parser_create($resp_encoding);\r
@@ -2738,7 +2747,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                                $this->me['struct']=$val;\r
                                                break;\r
                                        default:\r
-                                               error_log("XML-RPC: xmlrpcval::xmlrpcval: not a known type ($type)");\r
+                                               error_log("XML-RPC: ".__METHOD__.": not a known type ($type)");\r
                                }\r
                                /*if($type=='')\r
                                {\r
@@ -2770,7 +2779,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                        $typeof=@$GLOBALS['xmlrpcTypes'][$type];\r
                        if($typeof!=1)\r
                        {\r
-                               error_log("XML-RPC: xmlrpcval::addScalar: not a scalar type ($type)");\r
+                               error_log("XML-RPC: ".__METHOD__.": not a scalar type ($type)");\r
                                return 0;\r
                        }\r
 \r
@@ -2792,10 +2801,10 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                        switch($this->mytype)\r
                        {\r
                                case 1:\r
-                                       error_log('XML-RPC: xmlrpcval::addScalar: scalar xmlrpcval can have only one value');\r
+                                       error_log('XML-RPC: '.__METHOD__.': scalar xmlrpcval can have only one value');\r
                                        return 0;\r
                                case 3:\r
-                                       error_log('XML-RPC: xmlrpcval::addScalar: cannot add anonymous scalar to struct xmlrpcval');\r
+                                       error_log('XML-RPC: '.__METHOD__.': cannot add anonymous scalar to struct xmlrpcval');\r
                                        return 0;\r
                                case 2:\r
                                        // we're adding a scalar value to an array here\r
@@ -2837,7 +2846,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                        }\r
                        else\r
                        {\r
-                               error_log('XML-RPC: xmlrpcval::addArray: already initialized as a [' . $this->kindOf() . ']');\r
+                               error_log('XML-RPC: '.__METHOD__.': already initialized as a [' . $this->kindOf() . ']');\r
                                return 0;\r
                        }\r
                }\r
@@ -2866,7 +2875,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                        }\r
                        else\r
                        {\r
-                               error_log('XML-RPC: xmlrpcval::addStruct: already initialized as a [' . $this->kindOf() . ']');\r
+                               error_log('XML-RPC: '.__METHOD__.': already initialized as a [' . $this->kindOf() . ']');\r
                                return 0;\r
                        }\r
                }\r
@@ -2939,8 +2948,8 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                                        break;\r
                                                case $GLOBALS['xmlrpcDouble']:\r
                                                        // avoid using standard conversion of float to string because it is locale-dependent,\r
-                                                       // and also because the xmlrpc spec forbids exponential notation\r
-                                                       // sprintf('%F') would be most likely ok but it is only available since PHP 4.3.10 and PHP 5.0.3.\r
+                                                       // and also because the xmlrpc spec forbids exponential notation.\r
+                                                       // sprintf('%F') could be most likely ok but it fails eg. on 2e-14.\r
                                                        // The code below tries its best at keeping max precision while avoiding exp notation,\r
                                                        // but there is of course no limit in the number of decimal places to be used...\r
                                                        $rs.="<${typ}>".preg_replace('/\\.?0+$/','',number_format((double)$val, 128, '.', ''))."</${typ}>";\r
@@ -3307,7 +3316,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                        $out = $xmlrpc_val->scalarval();\r
                                        if (is_string($out))\r
                                        {\r
-                                               $out= strtotime($out);\r
+                                               $out = strtotime($out);\r
                                        }\r
                                        if (is_int($out))\r
                                        {\r
@@ -3456,7 +3465,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                else\r
                                {\r
                                        $arr = array();\r
-                                   reset($php_val);\r
+                                       reset($php_val);\r
                                        while(list($k,$v) = each($php_val))\r
                                        {\r
                                                $arr[$k] = php_xmlrpc_encode($v, $options);\r