- xmlrpcs.inc: add a new member var in server class to allow fine-tuning of the encod...
[plcapi.git] / lib / xmlrpc.inc
index b02eae2..4ac287a 100644 (file)
@@ -89,7 +89,8 @@
                'METHODNAME' => array('METHODCALL'),\r
                'PARAMS' => array('METHODCALL', 'METHODRESPONSE'),\r
                'FAULT' => array('METHODRESPONSE'),\r
-               'NIL' => array('VALUE') // only used when extension activated\r
+               'NIL' => array('VALUE'), // only used when extension activated\r
+               'EX:NIL' => array('VALUE') // only used when extension activated\r
        );\r
 \r
        // define extra types for supporting NULL (useful for json or <NIL/>)\r
        /// @deprecated\r
        $GLOBALS['xmlrpc_backslash']=chr(92).chr(92);\r
 \r
-       // set to TRUE to enable correct decoding of <NIL/> values\r
+       // set to TRUE to enable correct decoding of <NIL/> and <EX:NIL/> values\r
        $GLOBALS['xmlrpc_null_extension']=false;\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
+\r
        // used to store state during parsing\r
        // quick explanation of components:\r
        //   ac - used to accumulate values\r
                                else\r
                                {\r
                                        $GLOBALS['_xh']['rt'] = strtolower($name);\r
+                                       $GLOBALS['_xh']['rt'] = strtolower($name);\r
                                }\r
                        }\r
                        else\r
                                        $GLOBALS['_xh']['vt']=null;\r
                                        break;\r
                                case 'NIL':\r
+                               case 'EX:NIL':\r
                                        if ($GLOBALS['xmlrpc_null_extension'])\r
                                        {\r
                                                if ($GLOBALS['_xh']['vt']!='value')\r
                                        $GLOBALS['_xh']['method']=preg_replace('/^[\n\r\t ]+/', '', $GLOBALS['_xh']['ac']);\r
                                        break;\r
                                case 'NIL':\r
+                               case 'EX:NIL':\r
                                        if ($GLOBALS['xmlrpc_null_extension'])\r
                                        {\r
                                                $GLOBALS['_xh']['vt']='null';\r
                var $proxy_pass='';\r
                var $proxy_authtype=1;\r
                var $cookies=array();\r
+           var $extracurlopts=array();\r
+\r
                /**\r
                * List of http compression methods accepted by the client for responses.\r
                * NB: PHP supports deflate, gzip compressions out of the box if compiled w. zlib\r
                                $this->accepted_compression = array('gzip', 'deflate');\r
                        }\r
 \r
-                       // keepalives: enabled by default ONLY for PHP >= 4.3.8\r
+                       // keepalives: enabled by default\r
                        $this->keepalive = true;\r
 \r
                        // by default the xml parser can support these 3 charset encodings\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
                /**\r
                * Send an xmlrpc request\r
                * @param mixed $msg The message object, or an array of messages for using multicall, or the complete xml representation of a request\r
 \r
                        if(!fputs($fp, $op, strlen($op)))\r
                        {\r
-                       fclose($fp);\r
+                               fclose($fp);\r
                                $this->errstr='Write error';\r
                                $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['http_error'], $this->errstr);\r
                                return $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
+\r
                        $result = curl_exec($curl);\r
 \r
                        if ($this->debug > 1)\r
@@ -2181,7 +2205,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                * Given an open file handle, read all data available and parse it as axmlrpc response.\r
                * NB: the file handle is not closed by this function.\r
                * NNB: might have trouble in rare cases to work on network streams, as we\r
-        *      check for a read of 0 bytes instead of feof($fp).\r
+               *      check for a read of 0 bytes instead of feof($fp).\r
                *      But since checking for feof(null) returns false, we would risk an\r
                *      infinite loop in that case, because we cannot trust the caller\r
                *      to give us a valid pointer to an open file...\r
@@ -2289,7 +2313,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                        }\r
                                }\r
                                // be tolerant to line endings, and extra empty lines\r
-                               $ar = split("\r?\n", trim(substr($data, 0, $pos)));\r
+                               $ar = preg_split("/\r?\n/", trim(substr($data, 0, $pos)));\r
                                while(list(,$line) = @each($ar))\r
                                {\r
                                        // take care of multi-line headers and cookies\r
@@ -2751,8 +2775,8 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                        }\r
 \r
                        // coerce booleans into correct values\r
-                       // NB: we should iether do it for datetimes, integers and doubles, too,\r
-                       // or just plain remove this check, implemnted on booleans only...\r
+                       // NB: we should either do it for datetimes, integers and doubles, too,\r
+                       // or just plain remove this check, implemented on booleans only...\r
                        if($type==$GLOBALS['xmlrpcBoolean'])\r
                        {\r
                                if(strcasecmp($val,'true')==0 || $val==1 || ($val==true && strcasecmp($val,'false')))\r
@@ -2914,15 +2938,41 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                                        $rs.="<${typ}>".(int)$val."</${typ}>";\r
                                                        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
-                                               // 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
+                                                       // 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
+                                                       // 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
+                                                       break;\r
+                                               case $GLOBALS['xmlrpcDateTime']:\r
+                                                       if (is_string($val))\r
+                                                       {\r
+                                                               $rs.="<${typ}>${val}</${typ}>";\r
+                                                       }\r
+                                                       else if(is_a($val, 'DateTime'))\r
+                                                       {\r
+                                                               $rs.="<${typ}>".$val->format('Ymd\TH:i:s')."</${typ}>";\r
+                                                       }\r
+                                                       else if(is_int($val))\r
+                                                       {\r
+                                                               $rs.="<${typ}>".strftime("%Y%m%dT%H:%M:%S", $val)."</${typ}>";\r
+                                                       }\r
+                                                       else\r
+                                                       {\r
+                                                               // not really a good idea here: but what shall we output anyway? left for backward compat...\r
+                                                               $rs.="<${typ}>${val}</${typ}>";\r
+                                                       }\r
                                                        break;\r
                                                case $GLOBALS['xmlrpcNull']:\r
-                                                       $rs.="<nil/>";\r
+                                                       if ($GLOBALS['xmlrpc_null_apache_encoding'])\r
+                                                       {\r
+                                                               $rs.="<ex:nil/>";\r
+                                                       }\r
+                                                       else\r
+                                                       {\r
+                                                               $rs.="<nil/>";\r
+                                                       }\r
                                                        break;\r
                                                default:\r
                                                        // no standard type value should arrive here, but provide a possibility\r
@@ -3222,7 +3272,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
        * @author Dan Libby (dan@libby.com)\r
        *\r
        * @param xmlrpcval $xmlrpc_val\r
-       * @param array $options if 'decode_php_objs' is set in the options array, xmlrpc structs can be decoded into php objects\r
+       * @param array $options if 'decode_php_objs' is set in the options array, xmlrpc structs can be decoded into php objects; if 'dates_as_objects' is set xmlrpc datetimes are decoded as php DateTime objects (standard is\r
        * @return mixed\r
        */\r
        function php_xmlrpc_decode($xmlrpc_val, $options=array())\r
@@ -3249,6 +3299,27 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                                        return $xmlrpc_val->scalarval();\r
                                        }\r
                                }\r
+                               if (in_array('dates_as_objects', $options) && $xmlrpc_val->scalartyp() == 'dateTime.iso8601')\r
+                               {\r
+                                       // we return a Datetime object instead of a string\r
+                                       // since now the constructor of xmlrpcval accepts safely strings, ints and datetimes,\r
+                                       // we cater to all 3 cases here\r
+                                       $out = $xmlrpc_val->scalarval();\r
+                                       if (is_string($out))\r
+                                       {\r
+                                               $out= strtotime($out);\r
+                                       }\r
+                                       if (is_int($out))\r
+                                       {\r
+                                               $result = new Datetime();\r
+                                               $result->setTimestamp($out);\r
+                                               return $result;\r
+                                       }\r
+                                       elseif (is_a($out, 'Datetime'))\r
+                                       {\r
+                                               return $out;\r
+                                       }\r
+                               }\r
                                return $xmlrpc_val->scalarval();\r
                        case 'array':\r
                                $size = $xmlrpc_val->arraysize();\r
@@ -3323,7 +3394,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
        * @param array $options can include 'encode_php_objs', 'auto_dates', 'null_extension' or 'extension_api'\r
        * @return xmlrpcval\r
        */\r
-       function &php_xmlrpc_encode($php_val, $options=array())\r
+       function php_xmlrpc_encode($php_val, $options=array())\r
        {\r
                $type = gettype($php_val);\r
                switch($type)\r
@@ -3357,7 +3428,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                $ko = false;\r
                                foreach($php_val as $key => $val)\r
                                {\r
-                                       $arr[$key] =& php_xmlrpc_encode($val, $options);\r
+                                       $arr[$key] = php_xmlrpc_encode($val, $options);\r
                                        if(!$ko && $key !== $j)\r
                                        {\r
                                                $ko = true;\r
@@ -3378,9 +3449,14 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                {\r
                                        $xmlrpc_val = $php_val;\r
                                }\r
+                               else if(is_a($php_val, 'DateTime'))\r
+                               {\r
+                                       $xmlrpc_val = new xmlrpcval($php_val->format('Ymd\TH:i:s'), $GLOBALS['xmlrpcStruct']);\r
+                               }\r
                                else\r
                                {\r
                                        $arr = array();\r
+                                   reset($php_val);\r
                                        while(list($k,$v) = each($php_val))\r
                                        {\r
                                                $arr[$k] = php_xmlrpc_encode($v, $options);\r
@@ -3399,7 +3475,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                {\r
                                        $xmlrpc_val = new xmlrpcval('', $GLOBALS['xmlrpcString']);\r
                                }\r
-                               if (in_array('null_extension', $options))\r
+                               else if (in_array('null_extension', $options))\r
                                {\r
                                        $xmlrpc_val = new xmlrpcval('', $GLOBALS['xmlrpcNull']);\r
                                }\r
@@ -3589,7 +3665,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                // Note: we do not check for invalid chars in VALUE:\r
                //   this had better be done using pure ereg as below\r
                // Note 2: we might be removing whitespace/tabs that ought to be left in if\r
-        //   the received charset is a quoted string. But nobody uses such charset names...\r
+               //   the received charset is a quoted string. But nobody uses such charset names...\r
 \r
                /// @todo this test will pass if ANY header has charset specification, not only Content-Type. Fix it?\r
                $matches = array();\r