remove one more warning with php 7.2
[plcapi.git] / lib / xmlrpc.inc
index 5deade3..13bcdfc 100644 (file)
@@ -1,7 +1,6 @@
 <?php\r
 // by Edd Dumbill (C) 1999-2002\r
 // <edd@usefulinc.com>\r
-// $Id: xmlrpc.inc,v 1.174 2009/03/16 19:36:38 ggiunta Exp $\r
 \r
 // Copyright (c) 1999,2000,2002 Edd Dumbill.\r
 // All rights reserved.\r
        $GLOBALS['xmlrpc_internalencoding']='ISO-8859-1';\r
 \r
        $GLOBALS['xmlrpcName']='XML-RPC for PHP';\r
-       $GLOBALS['xmlrpcVersion']='3.0.0.beta';\r
+       $GLOBALS['xmlrpcVersion']='3.1.2';\r
 \r
        // let user errors start at 800\r
        $GLOBALS['xmlrpcerruser']=800;\r
 \r
        // set to TRUE to enable encoding of php NULL values to <EX:NIL/> instead of <NIL/>\r
        $GLOBALS['xmlrpc_null_apache_encoding']=false;\r
-    $GLOBALS['xmlrpc_null_apache_encoding_ns']='http://ws.apache.org/xmlrpc/namespaces/extensions';\r
+       $GLOBALS['xmlrpc_null_apache_encoding_ns']='http://ws.apache.org/xmlrpc/namespaces/extensions';\r
 \r
        // used to store state during parsing\r
        // quick explanation of components:\r
                                        $GLOBALS['_xh']['value'] = $curr_val['values'];\r
                                        $GLOBALS['_xh']['vt']=strtolower($name);\r
                                        if (isset($curr_val['php_class']))\r
-\r
                                        {\r
                                                $GLOBALS['_xh']['php_class'] = $curr_val['php_class'];\r
                                        }\r
                var $keypass='';\r
                var $verifypeer=true;\r
                var $verifyhost=1;\r
+               var $sslversion=0; // corresponds to CURL_SSLVERSION_DEFAULT\r
                var $no_multicall=false;\r
                var $proxy='';\r
                var $proxyport=0;\r
                * http://curl.haxx.se/docs/faq.html#7.3)\r
                */\r
                var $xmlrpc_curl_handle = null;\r
-               /// Wheter to use persistent connections for http 1.1 and https\r
+               /// Whether to use persistent connections for http 1.1 and https\r
                var $keepalive = false;\r
                /// Charset encodings that can be decoded without problems by the client\r
                var $accepted_charset_encodings = array();\r
                * @param integer $port the port the server is listening on, defaults to 80 or 443 depending on protocol used\r
                * @param string $method the http protocol variant: defaults to 'http', 'https' and 'http11' can be used if CURL is installed\r
                */\r
-               function xmlrpc_client($path, $server='', $port='', $method='')\r
+               function __construct($path, $server='', $port='', $method='')\r
                {\r
                        // allow user to specify all params in $path\r
                        if($server == '' and $port == '' and $method == '')\r
                        $this->user_agent = $GLOBALS['xmlrpcName'] . ' ' . $GLOBALS['xmlrpcVersion'];\r
                }\r
 \r
+               /**\r
+               * @deprecated\r
+               */\r
+               function xmlrpc_client($path, $server='', $port='', $method='')\r
+               {\r
+                       self::__construct($path, $server, $port, $method);\r
+               }\r
+\r
                /**\r
                * Enables/disables the echoing to screen of the xmlrpc responses received\r
-               * @param integer $debug values 0, 1 and 2 are supported (2 = echo sent msg too, before received response)\r
+               * @param integer $in values 0, 1 and 2 are supported (2 = echo sent msg too, before received response)\r
                * @access public\r
                */\r
                function setDebug($in)\r
 \r
                /**\r
                * Add a CA certificate to verify server with (see man page about\r
-               * CURLOPT_CAINFO for more details\r
+               * CURLOPT_CAINFO for more details)\r
                * @param string $cacert certificate file name (or dir holding certificates)\r
                * @param bool $is_dir set to true to indicate cacert is a dir. defaults to false\r
                * @access public\r
                function setCaCertificate($cacert, $is_dir=false)\r
                {\r
                        if ($is_dir)\r
-\r
                        {\r
                                $this->cacertdir = $cacert;\r
                        }\r
                        $this->verifyhost = $i;\r
                }\r
 \r
+               /**\r
+               * Set attributes for SSL communication: SSL version to use. Best left at 0 (default value ): let cURL decide\r
+               *\r
+               * @param int $i\r
+               */\r
+               public function setSSLVersion($i)\r
+               {\r
+                       $this->sslversion = $i;\r
+               }\r
+\r
                /**\r
                * Set proxy info\r
                * @param string $proxyhost\r
                        if ($compmethod == 'any')\r
                                $this->accepted_compression = array('gzip', 'deflate');\r
                        else\r
-                           if ($compmethod == false )\r
-                               $this->accepted_compression = array();\r
-                   else\r
-                               $this->accepted_compression = array($compmethod);\r
+                               if ($compmethod == false )\r
+                                       $this->accepted_compression = array();\r
+                               else\r
+                                       $this->accepted_compression = array($compmethod);\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
+               * @param array $options\r
                */\r
                function SetCurlOptions( $options )\r
                {\r
                        $msg->debug=$this->debug;\r
 \r
                        if($method == 'https')\r
-\r
                        {\r
                                $r =& $this->sendPayloadHTTPS(\r
                                        $msg,\r
                                        $this->proxy_authtype,\r
                                        $this->keepalive,\r
                                        $this->key,\r
-                                       $this->keypass\r
+                                       $this->keypass,\r
+                                       $this->sslversion\r
                                );\r
                        }\r
                        elseif($method == 'http11')\r
                                $cookieheader = 'Cookie:' . $version . substr($cookieheader, 0, -1) . "\r\n";\r
                        }\r
 \r
-                   // omit port if 80\r
-                   $port = ($port == '80') ? '' : (':' . $port);\r
+                       // omit port if 80\r
+                       $port = ($port == 80) ? '' : (':' . $port);\r
 \r
                        $op= 'POST ' . $uri. " HTTP/1.0\r\n" .\r
                                'User-Agent: ' . $this->user_agent . "\r\n" .\r
                        }\r
                        else\r
                        {\r
-                               // reset errno and errstr on succesful socket connection\r
+                               // reset errno and errstr on successful socket connection\r
                                $this->errstr = '';\r
                        }\r
                        // G. Giunta 2005/10/24: close socket before parsing.\r
-                       // should yeld slightly better execution times, and make easier recursive calls (e.g. to follow http redirects)\r
+                       // should yield slightly better execution times, and make easier recursive calls (e.g. to follow http redirects)\r
                        $ipd='';\r
                        do\r
                        {\r
                function &sendPayloadHTTPS($msg, $server, $port, $timeout=0, $username='',\r
                        $password='', $authtype=1, $cert='',$certpass='', $cacert='', $cacertdir='',\r
                        $proxyhost='', $proxyport=0, $proxyusername='', $proxypassword='', $proxyauthtype=1,\r
-                       $keepalive=false, $key='', $keypass='')\r
+                       $keepalive=false, $key='', $keypass='', $sslVersion = 0)\r
                {\r
                        $r =& $this->sendPayloadCURL($msg, $server, $port, $timeout, $username,\r
                                $password, $authtype, $cert, $certpass, $cacert, $cacertdir, $proxyhost, $proxyport,\r
-                               $proxyusername, $proxypassword, $proxyauthtype, 'https', $keepalive, $key, $keypass);\r
+                               $proxyusername, $proxypassword, $proxyauthtype, 'https', $keepalive, $key, $keypass, $sslVersion);\r
                        return $r;\r
                }\r
 \r
                function &sendPayloadCURL($msg, $server, $port, $timeout=0, $username='',\r
                        $password='', $authtype=1, $cert='', $certpass='', $cacert='', $cacertdir='',\r
                        $proxyhost='', $proxyport=0, $proxyusername='', $proxypassword='', $proxyauthtype=1, $method='https',\r
-                       $keepalive=false, $key='', $keypass='')\r
+                       $keepalive=false, $key='', $keypass='', $sslVersion = 0)\r
                {\r
                        if(!function_exists('curl_init'))\r
                        {\r
                        // return the header too\r
                        curl_setopt($curl, CURLOPT_HEADER, 1);\r
 \r
-                       // will only work with PHP >= 5.0\r
                        // NB: if we set an empty string, CURL will add http header indicating\r
                        // ALL methods it is supporting. This is possibly a better option than\r
                        // letting the user tell what curl can / cannot do...\r
                                {\r
                                        curl_setopt($curl, CURLOPT_SSLKEYPASSWD, $keypass);\r
                                }\r
+\r
+                               // Upgrade transparently to more stringent check for versions of php which do not support otherwise.\r
+                               // Doing it in constructor would be cleaner; doing it here saves us a couple of function calls\r
+                               if($this->verifyhost == 1 && $info = curl_version() && version_compare($info['version'], '7.28.1') >= 0)\r
+                               {\r
+                                       $this->verifyhost = 2;\r
+                               }\r
                                // whether to verify cert's common name (CN); 0 for no, 1 to verify that it exists, and 2 to verify that it matches the hostname used\r
                                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, $this->verifyhost);\r
+                               // allow usage of different SSL versions\r
+                               curl_setopt($curl, CURLOPT_SSLVERSION, $sslVersion);\r
                        }\r
 \r
                        // proxy info\r
                                        curl_close($curl);\r
                                }\r
                                $resp =& $msg->parseResponse($result, true, $this->return_type);\r
+                               // if we got back a 302, we can not reuse the curl handle for later calls\r
+                               if($resp->faultCode() == $GLOBALS['xmlrpcerr']['http_error'] && $keepalive)\r
+                               {\r
+                                       curl_close($curl);\r
+                                       $this->xmlrpc_curl_handle = null;\r
+                               }\r
                        }\r
                        return $resp;\r
                }\r
                * @param array $msgs an array of xmlrpcmsg objects\r
                * @param integer $timeout connection timeout (in seconds)\r
                * @param string $method the http protocol variant to be used\r
-               * @param boolean fallback When true, upon receiveing an error during multicall, multiple single calls will be attempted\r
+               * @param boolean fallback When true, upon receiving an error during multicall, multiple single calls will be attempted\r
                * @return array\r
                * @access public\r
                */\r
                                                case 1:\r
                                                        if(!isset($val[0]))\r
                                                        {\r
-\r
                                                                return false;           // Bad value\r
                                                        }\r
                                                        // Normal return value\r
                * NB: as of now we do not do it, since it might be either an xmlrpcval or a plain\r
                * php val, or a complete xml chunk, depending on usage of xmlrpc_client::send() inside which creator is called...\r
                */\r
-               function xmlrpcresp($val, $fcode = 0, $fstr = '', $valtyp='')\r
+               function __construct($val, $fcode = 0, $fstr = '', $valtyp='')\r
                {\r
                        if($fcode != 0)\r
                        {\r
                        }\r
                }\r
 \r
+               /**\r
+               * @deprecated\r
+               */\r
+               function xmlrpcresp($val, $fcode = 0, $fstr = '', $valtyp='')\r
+               {\r
+                       self::__construct($val, $fcode, $fstr, $valtyp);\r
+               }\r
+\r
                /**\r
                * Returns the error code of the response.\r
                * @return integer the error code of this response (0 for not-error responses)\r
                * with attributes being e.g. 'expires', 'path', domain'.\r
                * NB: cookies sent as 'expired' by the server (i.e. with an expiry date in the past)\r
                * are still present in the array. It is up to the user-defined code to decide\r
-               * how to use the received cookies, and wheter they have to be sent back with the next\r
+               * how to use the received cookies, and whether they have to be sent back with the next\r
                * request to the server (using xmlrpc_client::setCookie) or not\r
                * @return array array of cookies received from the server\r
                * @access public\r
                                $this->content_type = 'text/xml; charset=' . $charset_encoding;\r
                        else\r
                                $this->content_type = 'text/xml';\r
-                   if ($GLOBALS['xmlrpc_null_apache_encoding'])\r
-                   {\r
-                       $result = "<methodResponse xmlns:ex=\"".$GLOBALS['xmlrpc_null_apache_encoding_ns']."\">\n";\r
-                   }\r
-                   else\r
-                   {\r
-                       $result = "<methodResponse>\n";\r
-                   }\r
+                       if ($GLOBALS['xmlrpc_null_apache_encoding'])\r
+                       {\r
+                               $result = "<methodResponse xmlns:ex=\"".$GLOBALS['xmlrpc_null_apache_encoding_ns']."\">\n";\r
+                       }\r
+                       else\r
+                       {\r
+                       $result = "<methodResponse>\n";\r
+                       }\r
                        if($this->errno)\r
                        {\r
                                // G. Giunta 2005/2/13: let non-ASCII response messages be tolerated by clients\r
@@ -2114,9 +2151,9 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
 \r
                /**\r
                * @param string $meth the name of the method to invoke\r
-               * @param array $pars array of parameters to be paased to the method (xmlrpcval objects)\r
+               * @param array $pars array of parameters to be passed to the method (xmlrpcval objects)\r
                */\r
-               function xmlrpcmsg($meth, $pars=0)\r
+               function __construct($meth, $pars=0)\r
                {\r
                        $this->methodname=$meth;\r
                        if(is_array($pars) && count($pars)>0)\r
@@ -2128,6 +2165,14 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                        }\r
                }\r
 \r
+               /**\r
+               * @deprecated\r
+               */\r
+               function xmlrpcmsg($meth, $pars=0)\r
+               {\r
+                       self::__construct($meth, $pars);\r
+               }\r
+\r
                /**\r
                * @access private\r
                */\r
@@ -2169,7 +2214,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                        else\r
                                $this->content_type = 'text/xml';\r
                        $this->payload=$this->xml_header($charset_encoding);\r
-                       $this->payload.='<methodName>' . $this->methodname . "</methodName>\n";\r
+                       $this->payload.='<methodName>' . xmlrpc_encode_entitites($this->methodname, $GLOBALS['xmlrpc_internalencoding'], $charset_encoding) . "</methodName>\n";\r
                        $this->payload.="<params>\n";\r
                        for($i=0; $i<count($this->params); $i++)\r
                        {\r
@@ -2198,6 +2243,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
 \r
                /**\r
                * Returns xml representation of the message. XML prologue included\r
+               * @param string $charset_encoding\r
                * @return string the xml representation of the message, xml prologue included\r
                * @access public\r
                */\r
@@ -2251,6 +2297,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                *      infinite loop in that case, because we cannot trust the caller\r
                *      to give us a valid pointer to an open file...\r
                * @access public\r
+               * @param resource $fp stream pointer\r
                * @return xmlrpcresp\r
                * @todo add 2nd & 3rd param to be passed to ParseResponse() ???\r
                */\r
@@ -2355,7 +2402,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                }\r
                                // be tolerant to line endings, and extra empty lines\r
                                $ar = preg_split("/\r?\n/", trim(substr($data, 0, $pos)));\r
-                               while(list(,$line) = @each($ar))\r
+               foreach($ar as $line)\r
                                {\r
                                        // take care of multi-line headers and cookies\r
                                        $arr = explode(':',$line,2);\r
@@ -2406,7 +2453,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                                                        {\r
                                                                                if ($tag != 'value')\r
                                                                                {\r
-                                                                                 $GLOBALS['_xh']['cookies'][$cookiename][$tag] = $val;\r
+                                                                                       $GLOBALS['_xh']['cookies'][$cookiename][$tag] = $val;\r
                                                                                }\r
                                                                        }\r
                                                                }\r
@@ -2590,17 +2637,24 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                        $GLOBALS['_xh']['isf_reason']='';\r
                        $GLOBALS['_xh']['rt']=''; // 'methodcall or 'methodresponse'\r
 \r
-                       // if response charset encoding is not known / supported, try to use\r
-                       // the default encoding and parse the xml anyway, but log a warning...\r
-                       if (!in_array($resp_encoding, array('UTF-8', 'ISO-8859-1', 'US-ASCII')))\r
-                       // the following code might be better for mb_string enabled installs, but\r
+                       // Since parsing will fail if charset is not specified in the xml prologue,\r
+                       // the encoding is not UTF8 and there are non-ascii chars in the text, we try to work round that...\r
+                       // The following code might be better for mb_string enabled installs, but\r
                        // 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: '.__METHOD__.': invalid charset encoding of received response: '.$resp_encoding);\r
-                               $resp_encoding = $GLOBALS['xmlrpc_defencoding'];\r
+                       //if (!is_valid_charset($resp_encoding, array('UTF-8')))\r
+                       if (!in_array($resp_encoding, array('UTF-8', 'US-ASCII')) && !has_encoding($data)) {\r
+                               if ($resp_encoding == 'ISO-8859-1') {\r
+                                       $data = utf8_encode($data);\r
+                               } else {\r
+                                       if (extension_loaded('mbstring')) {\r
+                                               $data = mb_convert_encoding($data, 'UTF-8', $resp_encoding);\r
+                                       } else {\r
+                                               error_log('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of received request: ' . $resp_encoding);\r
+                                       }\r
+                               }\r
                        }\r
-                       $parser = xml_parser_create($resp_encoding);\r
+\r
+                       $parser = xml_parser_create();\r
                        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);\r
                        // G. Giunta 2005/02/13: PHP internally uses ISO-8859-1, so we have to tell\r
                        // the xml parser to give us back data in the expected charset.\r
@@ -2630,7 +2684,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                        xml_set_default_handler($parser, 'xmlrpc_dh');\r
 \r
                        // first error check: xml not well formed\r
-                       if(!xml_parse($parser, $data, count($data)))\r
+                       if(!xml_parse($parser, $data, 1))\r
                        {\r
                                // thanks to Peter Kocks <peter.kocks@baygate.com>\r
                                if((xml_get_current_line_number($parser)) == 1)\r
@@ -2739,7 +2793,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                * @param mixed $val\r
                * @param string $type any valid xmlrpc type name (lowercase). If null, 'string' is assumed\r
                */\r
-               function xmlrpcval($val=-1, $type='')\r
+               function __construct($val=-1, $type='')\r
                {\r
                        /// @todo: optimization creep - do not call addXX, do it all inline.\r
                        /// downside: booleans will not be coerced anymore\r
@@ -2793,6 +2847,14 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                        }\r
                }\r
 \r
+               /**\r
+               * @deprecated\r
+               */\r
+               function xmlrpcval($val=-1, $type='')\r
+               {\r
+                       self::__construct($val, $type);\r
+               }\r
+\r
                /**\r
                * Add a single php value to an (unitialized) xmlrpcval\r
                * @param mixed $val\r
@@ -2906,7 +2968,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                }\r
 \r
                // poor man's version of print_r ???\r
-               // DEPRECATED!\r
+               /// @deprecated\r
                function dump($ar)\r
                {\r
                        foreach($ar as $key => $val)\r
@@ -2914,7 +2976,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                echo "$key => $val<br />";\r
                                if($key == 'array')\r
                                {\r
-                                       while(list($key2, $val2) = each($val))\r
+                    foreach($val as $key2 => $val2)\r
                                        {\r
                                                echo "-- $key2 => $val2<br />";\r
                                        }\r
@@ -3060,27 +3122,26 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                        // add check? slower, but helps to avoid recursion in serializing broken xmlrpcvals...\r
                        //if (is_object($o) && (get_class($o) == 'xmlrpcval' || is_subclass_of($o, 'xmlrpcval')))\r
                        //{\r
-                               reset($this->me);\r
-                               list($typ, $val) = each($this->me);\r
+                               $val = reset($this->me);\r
+                               $typ = key($this->me);\r
                                return '<value>' . $this->serializedata($typ, $val, $charset_encoding) . "</value>\n";\r
                        //}\r
                }\r
 \r
-               // DEPRECATED\r
+               /// @deprecated\r
                function serializeval($o)\r
                {\r
                        // add check? slower, but helps to avoid recursion in serializing broken xmlrpcvals...\r
                        //if (is_object($o) && (get_class($o) == 'xmlrpcval' || is_subclass_of($o, 'xmlrpcval')))\r
                        //{\r
-                               $ar=$o->me;\r
-                               reset($ar);\r
-                               list($typ, $val) = each($ar);\r
+                               $val = reset($o->me);\r
+                               $typ = key($o->me);\r
                                return '<value>' . $this->serializedata($typ, $val) . "</value>\n";\r
                        //}\r
                }\r
 \r
                /**\r
-               * Checks wheter a struct member with a given name is present.\r
+               * Checks whether a struct member with a given name is present.\r
                * Works only on xmlrpcvals of type struct.\r
                * @param string $m the name of the struct member to be looked up\r
                * @return boolean\r
@@ -3116,19 +3177,20 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                * Return next member element for xmlrpcvals of type struct.\r
                * @return xmlrpcval\r
                * @access public\r
+               * @deprecated this will be removed in future versions\r
                */\r
                function structeach()\r
                {\r
-                       return each($this->me['struct']);\r
+                       return @each($this->me['struct']);\r
                }\r
 \r
-               // DEPRECATED! this code looks like it is very fragile and has not been fixed\r
+               // this code looks like it is very fragile and has not been fixed\r
                // for a long long time. Shall we remove it for 2.0?\r
+               /// @deprecated\r
                function getval()\r
                {\r
                        // UNSTABLE\r
-                       reset($this->me);\r
-                       list($a,$b)=each($this->me);\r
+                       $b = reset($this->me);\r
                        // contributed by I Sofer, 2001-03-24\r
                        // add support for nested arrays to scalarval\r
                        // i've created a new method here, so as to\r
@@ -3136,8 +3198,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
 \r
                        if(is_array($b))\r
                        {\r
-                               @reset($b);\r
-                               while(list($id,$cont) = @each($b))\r
+                               foreach($b as $id => $cont)\r
                                {\r
                                        $b[$id] = $cont->scalarval();\r
                                }\r
@@ -3147,13 +3208,11 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                        if(is_object($b))\r
                        {\r
                                $t = get_object_vars($b);\r
-                               @reset($t);\r
-                               while(list($id,$cont) = @each($t))\r
+                               foreach($t as $id => $cont)\r
                                {\r
                                        $t[$id] = $cont->scalarval();\r
                                }\r
-                               @reset($t);\r
-                               while(list($id,$cont) = @each($t))\r
+                foreach($t as $id => $cont)\r
                                {\r
                                        @$b->$id = $cont;\r
                                }\r
@@ -3169,8 +3228,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                */\r
                function scalarval()\r
                {\r
-                       reset($this->me);\r
-                       list(,$b)=each($this->me);\r
+                       $b = reset($this->me);\r
                        return $b;\r
                }\r
 \r
@@ -3183,7 +3241,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                function scalartyp()\r
                {\r
                        reset($this->me);\r
-                       list($a,)=each($this->me);\r
+            $a = key($this->me);\r
                        if($a==$GLOBALS['xmlrpcI4'])\r
                        {\r
                                $a=$GLOBALS['xmlrpcInt'];\r
@@ -3316,8 +3374,8 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                        case 'scalar':\r
                                if (in_array('extension_api', $options))\r
                                {\r
-                                       reset($xmlrpc_val->me);\r
-                                       list($typ,$val) = each($xmlrpc_val->me);\r
+                    $val = reset($xmlrpc_val->me);\r
+                    $typ = key($xmlrpc_val->me);\r
                                        switch ($typ)\r
                                        {\r
                                                case 'dateTime.iso8601':\r
@@ -3490,8 +3548,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                else\r
                                {\r
                                        $arr = array();\r
-                                       reset($php_val);\r
-                                       while(list($k,$v) = each($php_val))\r
+                    foreach($php_val as $k => $v)\r
                                        {\r
                                                $arr[$k] = php_xmlrpc_encode($v, $options);\r
                                        }\r
@@ -3524,7 +3581,6 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                        $xmlrpc_val = new xmlrpcval((int)$php_val, $GLOBALS['xmlrpcInt']);\r
                                }\r
                                else\r
-\r
                                {\r
                                        $xmlrpc_val = new xmlrpcval();\r
                                }\r
@@ -3558,7 +3614,27 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                $GLOBALS['_xh']['isf_reason'] = '';\r
                $GLOBALS['_xh']['method'] = false;\r
                $GLOBALS['_xh']['rt'] = '';\r
-               /// @todo 'guestimate' encoding\r
+\r
+               // 'guestimate' encoding\r
+               $val_encoding = guess_encoding('', $xml_val);\r
+\r
+               // Since parsing will fail if charset is not specified in the xml prologue,\r
+               // the encoding is not UTF8 and there are non-ascii chars in the text, we try to work round that...\r
+               // The following code might be better for mb_string enabled installs, but\r
+               // makes the lib about 200% slower...\r
+               //if (!is_valid_charset($val_encoding, array('UTF-8')))\r
+               if (!in_array($val_encoding, array('UTF-8', 'US-ASCII')) && !has_encoding($xml_val)) {\r
+                       if ($val_encoding == 'ISO-8859-1') {\r
+                               $xml_val = utf8_encode($xml_val);\r
+                       } else {\r
+                               if (extension_loaded('mbstring')) {\r
+                                       $xml_val = mb_convert_encoding($xml_val, 'UTF-8', $val_encoding);\r
+                               } else {\r
+                                       error_log('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of received request: ' . $val_encoding);\r
+                               }\r
+                       }\r
+               }\r
+\r
                $parser = xml_parser_create();\r
                xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);\r
                // What if internal encoding is not in one of the 3 allowed?\r
@@ -3680,9 +3756,10 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
        * we SHOULD assume it is strictly US-ASCII. But we try to be more tolerant of unconforming (legacy?) clients/servers,\r
        * which will be most probably using UTF-8 anyway...\r
        *\r
-       * @param string $httpheaders the http Content-type header\r
+       * @param string $httpheader the http Content-type header\r
        * @param string $xmlchunk xml content buffer\r
        * @param string $encoding_prefs comma separated list of character encodings to be used as default (when mb extension is enabled)\r
+       * @return string\r
        *\r
        * @todo explore usage of mb_http_input(): does it detect http headers + post data? if so, use it instead of hand-detection!!!\r
        */\r
@@ -3726,7 +3803,6 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                }\r
                elseif(preg_match('/^(\xEF\xBB\xBF)/', $xmlchunk))\r
                {\r
-\r
                        return 'UTF-8';\r
                }\r
 \r
@@ -3771,11 +3847,49 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                }\r
        }\r
 \r
+       /**\r
+        * Helper function: checks if an xml chunk as a charset declaration (BOM or in the xml declaration)\r
+        *\r
+        * @param string $xmlChunk\r
+        * @return bool\r
+        */\r
+       function has_encoding($xmlChunk)\r
+       {\r
+               // scan the first bytes of the data for a UTF-16 (or other) BOM pattern\r
+               //       (source: http://www.w3.org/TR/2000/REC-xml-20001006)\r
+               if (preg_match('/^(\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\x00\x00\xFF\xFE|\xFE\xFF\x00\x00)/', $xmlChunk))\r
+               {\r
+                       return true;\r
+               }\r
+               elseif (preg_match('/^(\xFE\xFF|\xFF\xFE)/', $xmlChunk))\r
+               {\r
+                       return true;\r
+               }\r
+               elseif (preg_match('/^(\xEF\xBB\xBF)/', $xmlChunk))\r
+               {\r
+                       return true;\r
+               }\r
+\r
+               // test if encoding is specified in the xml declaration\r
+               // Details:\r
+               // SPACE:               (#x20 | #x9 | #xD | #xA)+ === [ \x9\xD\xA]+\r
+               // EQ:                  SPACE?=SPACE? === [ \x9\xD\xA]*=[ \x9\xD\xA]*\r
+               if (preg_match('/^<\?xml\s+version\s*=\s*' . "((?:\"[a-zA-Z0-9_.:-]+\")|(?:'[a-zA-Z0-9_.:-]+'))" .\r
+                       '\s+encoding\s*=\s*' . "((?:\"[A-Za-z][A-Za-z0-9._-]*\")|(?:'[A-Za-z][A-Za-z0-9._-]*'))/",\r
+                       $xmlChunk, $matches))\r
+               {\r
+                       return true;\r
+               }\r
+\r
+               return false;\r
+       }\r
+\r
        /**\r
        * Checks if a given charset encoding is present in a list of encodings or\r
        * if it is a valid subset of any encoding in the list\r
        * @param string $encoding charset to be tested\r
        * @param mixed $validlist comma separated list of valid charsets (or array of charsets)\r
+       * @return bool\r
        */\r
        function is_valid_charset($encoding, $validlist)\r
        {\r
@@ -3796,8 +3910,8 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                foreach ($validlist as $allowed)\r
                                        if (in_array($allowed, $charset_supersets[$encoding]))\r
                                                return true;\r
-                               return false;\r
+                       return false;\r
                }\r
        }\r
 \r
-?>
\ No newline at end of file
+?>\r