cvs: php4 /pear/XML RPC.php
| From: | Martin Jansen | Date: | Thu, 12 Jul 2001 16:38:51 +0000 |
| Subject: | cvs: php4 /pear/XML RPC.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-268@lists.php.net to get a copy of this message | ||
mj Thu Jul 12 12:38:51 2001 EDT
Modified files:
/php4/pear/XML RPC.php
Log:
* Merged in latest changes to XML RPC from www.usefulinc.com
Index: php4/pear/XML/RPC.php diff -u php4/pear/XML/RPC.php:1.7 php4/pear/XML/RPC.php:1.8 --- php4/pear/XML/RPC.php:1.7 Wed Jul 11 01:29:12 2001 +++ php4/pear/XML/RPC.php Thu Jul 12 12:38:51 2001 @@ -1,7 +1,7 @@ <?php -// by Edd Dumbill (C) 1999-2000 +// by Edd Dumbill (C) 1999-2001 // <edd@usefulinc.com> -// $Id: RPC.php,v 1.7 2001/07/11 05:29:12 mj Exp $ +// $Id: RPC.php,v 1.8 2001/07/12 16:38:51 mj Exp $ // License is granted to use or modify this software ("XML-RPC for PHP") // for commercial or non-commercial use provided the copyright of the author @@ -18,7 +18,8 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// Adapted to PEAR standards by Stig Sæther Bakken <stig@php.net> +// Adapted to PEAR standards by Stig Sæther Bakken <stig@php.net> and +// Martin Jansen <mj@php.net> require_once "PEAR.php"; @@ -156,6 +157,7 @@ $XML_RPC_xh[$parser]['lv']=1; $XML_RPC_xh[$parser]['vt']=$XML_RPC_String; $XML_RPC_xh[$parser]['ac']=""; + $XML_RPC_xh[$parser]['qt']=0; // look for a value: if this is still 1 by the // time we reach the first data segment then the type is string // by implication and we need to add in a quote @@ -177,6 +179,9 @@ } else if ($name=="BASE64") { $XML_RPC_xh[$parser]['qt']=2; } else { + // No quoting is required here -- but + // at the end of the element we must check + // for data format errors. $XML_RPC_xh[$parser]['qt']=0; } break; @@ -228,10 +233,23 @@ $XML_RPC_xh[$parser]['st'].="\"". $XML_RPC_xh[$parser]['ac'] . "\""; } else if ($XML_RPC_xh[$parser]['qt']==2) { $XML_RPC_xh[$parser]['st'].="base64_decode('". $XML_RPC_xh[$parser]['ac'] . "')"; + } else if ($name=="BOOLEAN") { + $XML_RPC_xh[$parser]['st'].=$_xh[$parser]['ac']; + } else { + // we have an I4, INT or a DOUBLE + // we must check that only 0123456789-.<space> are characters here + if (!ereg("^\-?[0123456789 \t\.]+$", $XML_RPC_xh[$parser]['ac'])) { + // TODO: find a better way of throwing an error + // than this! + error_log("XML-RPC: non numeric value received in INT or DOUBLE"); + $XML_RPC_xh[$parser]['st'].="ERROR_NON_NUMERIC_FOUND"; } else { + // it's ok, add it on $XML_RPC_xh[$parser]['st'].=$XML_RPC_xh[$parser]['ac']; } + $XML_RPC_xh[$parser]['ac']=""; $XML_RPC_xh[$parser]['qt']=0; + $_xh[$parser]['lv']=3; // indicate we've found a value break; case "VALUE": // deal with a string value @@ -280,21 +298,33 @@ function XML_RPC_cd($parser, $data) { - global $XML_RPC_xh, $XML_RPC_backslash, $XML_RPC_twoslash; + global $XML_RPC_xh, $XML_RPC_backslash; //if (ereg("^[\n\r \t]+$", $data)) return; // print "adding [${data}]\n"; - if ($XML_RPC_xh[$parser]['lv']==1) { - $XML_RPC_xh[$parser]['qt']=1; - $XML_RPC_xh[$parser]['lv']=2; - } - if ($XML_RPC_xh[$parser]['qt']) { // quoted string - $XML_RPC_xh[$parser]['ac'].=str_replace('\$', '\\$', + + if ($XML_RPC_xh[$parser]['lv']!=3) { + // "lookforvalue==3" means that we've found an entire value + // and should discard any further character data + + if ($XML_RPC_xh[$parser]['lv']==1) { + // if we've found text and we're just in a <value> then + // turn quoting on, as this will be a string + $XML_RPC_xh[$parser]['qt']=1; + // and say we've found a value + $XML_RPC_xh[$parser]['lv']=2; + } + + if (isset($XML_RPC_xh[$parser]['qt']) && $XML_RPC_xh[$parser]['qt']) { + // quoted string: replace characters that eval would + // do special things with + $XML_RPC_xh[$parser]['ac'].=str_replace('\$', '\\$', str_replace('"', '\"', str_replace(chr(92),$XML_RPC_backslash, $data))); + } + else + $XML_RPC_xh[$parser]['ac'].=$data; } - else - $XML_RPC_xh[$parser]['ac'].=$data; } @@ -355,90 +385,6 @@ } -/**************************************************************** -* XML_RPC_decode takes a message in PHP xmlrpc object format * -* and tranlates it into native PHP types. * -* * -* author: Dan Libby (dan@libby.com) * -****************************************************************/ -function XML_RPC_decode($xmlrpcval) -{ - $kind = $xmlrpcval->kindOf(); - - if($kind == "scalar") { - return $xmlrpcval->scalarval(); - } - else if($kind == "array") { - $size = $xmlrpcval->arraysize(); - $arr = array(); - - for($i = 0; $i < $size; $i++) { - array_append($arr, XML_RPC_decode($xmlrpcval->arraymem($i)) ); - } - return $arr; - } - else if($kind == "struct") { - $xmlrpcval->structreset(); - $arr = array(); - - while(list($key,$value)=$xmlrpcval->structeach()) { - $arr[$key] = XML_RPC_decode($value); - } - return $arr; - } -} - - -/**************************************************************** -* XML_RPC_encode takes native php types and encodes them into * -* xmlrpc PHP object format. * -* BUG: All sequential arrays are turned into structs. I don't * -* know of a good way to determine if an array is sequential * -* only. * -* * -* feature creep -- could support more types via optional type * -* argument. * -* * -* author: Dan Libby (dan@libby.com) * -****************************************************************/ -function XML_RPC_encode($php_val) -{ - global $XML_RPC_Int; - global $XML_RPC_Double; - global $XML_RPC_String; - global $XML_RPC_Array; - global $XML_RPC_Struct; - - $type = gettype($php_val); - $xmlrpcval = new XML_RPC_Value; - - switch ($type) { - case "array": - case "object": - $arr = array(); - while (list($k,$v) = each($php_val)) { - $arr[$k] = XML_RPC_encode($v); - } - $xmlrpcval->addStruct($arr); - break; - case "integer": - $xmlrpcval->addScalar($php_val, $XML_RPC_Int); - break; - case "double": - $xmlrpcval->addScalar($php_val, $XML_RPC_Double); - break; - case "string": - $xmlrpcval->addScalar($php_val, $XML_RPC_String); - break; - case "unknown type": - default: - $xmlrpcval = false; - break; - } - return $xmlrpcval; -} - - class XML_RPC_Client { var $path; @@ -455,8 +401,8 @@ $this->port=$port; $this->server=$server; $this->path=$path; - $this->proxy = $proxy; - $this->proxy_port = $proxy_port ; + $this->proxy = $proxy; + $this->proxy_port = $proxy_port ; } function setDebug($in) @@ -570,7 +516,14 @@ } } - function faultCode() { return $this->fn; } + function faultCode() + { + if (isset($this->fn)) + return $this->fn; + else + return 0; + } + function faultString() { return $this->fs; } function value() { return $this->xv; } @@ -685,12 +638,13 @@ $XML_RPC_xh[$parser]['cm']=0; $XML_RPC_xh[$parser]['isf']=0; $XML_RPC_xh[$parser]['ac']=""; - + $XML_RPC_xh[$parser]['qt']=""; + xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true); xml_set_element_handler($parser, "XML_RPC_se", "XML_RPC_ee"); xml_set_character_data_handler($parser, "XML_RPC_cd"); xml_set_default_handler($parser, "XML_RPC_dh"); - //$xmlrpcvalue=new XML_RPC_Value; + $xmlrpc_value = new XML_RPC_Value; $hdrfnd=0; if ($this->debug) @@ -730,28 +684,28 @@ } xml_parser_free($parser); if ($this->debug) { - print "<PRE>---EVALING---[" . - strlen($XML_RPC_xh[$parser]['st']) . " chars]---\n" . - htmlspecialchars($XML_RPC_xh[$parser]['st']) . ";\n---END---</PRE>"; + print "<PRE>---EVALING---[" . + strlen($XML_RPC_xh[$parser]['st']) . " chars]---\n" . + htmlspecialchars($XML_RPC_xh[$parser]['st']) . ";\n---END---</PRE>"; } if (strlen($XML_RPC_xh[$parser]['st'])==0) { - // then something odd has happened - // and it's time to generate a client side error - // indicating something odd went on - $r=new XML_RPC_Response(0, $XML_RPC_err["invalid_return"], - $XML_RPC_str["invalid_return"]); - } else { - eval('$v=' . $XML_RPC_xh[$parser]['st'] . '; $allOK=1;'); - if ($XML_RPC_xh[$parser]['isf']) { - $f=$v->structmem("faultCode"); - $fs=$v->structmem("faultString"); - $r=new XML_RPC_Response($v, $f->scalarval(), + // then something odd has happened + // and it's time to generate a client side error + // indicating something odd went on + $r=new XML_RPC_Response(0, $XML_RPC_err["invalid_return"], + $XML_RPC_str["invalid_return"]); + } else { + eval('$v=' . $XML_RPC_xh[$parser]['st'] . '; $allOK=1;'); + if ($XML_RPC_xh[$parser]['isf']) { + $f=$v->structmem("faultCode"); + $fs=$v->structmem("faultString"); + $r=new XML_RPC_Response($v, $f->scalarval(), $fs->scalarval()); - } else { - $r=new XML_RPC_Response($v); - } + } else { + $r=new XML_RPC_Response($v); + } } - $r->hdrs=split('\r?\n', $XML_RPC_xh[$parser]['ha'][1]); + $r->hdrs=split('\r?\n', $XML_RPC_xh[$parser]['ha'][1]); return $r; } @@ -795,7 +749,11 @@ } if ($type==$XML_RPC_Boolean) { - if (strcasecmp($val,"true")==0 || $val==1 || $val==true) { + if (strcasecmp($val,"true")==0 || + $val==1 || + ($val==true && + strcasecmp($val,"false"))) { + $val=1; } else { $val=0; @@ -873,7 +831,7 @@ function serializedata($typ, $val) { $rs=""; - global $XML_RPC_Types, $XML_RPC_Base64, $XML_RPC_String; + global $XML_RPC_Types, $XML_RPC_Base64, $XML_RPC_String, $XML_RPC_Boolean; switch($XML_RPC_Types[$typ]) { case 3: // struct @@ -948,7 +906,40 @@ { return each($this->me["struct"]); } + + function getval() { + // UNSTABLE + global $XML_RPC_BOOLEAN, $XML_RPC_Base64; + + reset($this->me); + list($a,$b)=each($this->me); + + // contributed by I Sofer, 2001-03-24 + // add support for nested arrays to scalarval + // i've created a new method here, so as to + // preserve back compatibility + + if (is_array($b)) { + foreach ($b as $id => $cont) { + $b[$id] = $cont->scalarval(); + } + } + // add support for structures directly encoding php objects + if (is_object($b)) { + $t = get_object_vars($b); + foreach ($t as $id => $cont) { + $t[$id] = $cont->scalarval(); + } + foreach ($t as $id => $cont) { + eval('$b->'.$id.' = $cont;'); + } + } + + // end contrib + return $b; + } + function scalarval() { global $XML_RPC_Boolean, $XML_RPC_Base64; @@ -979,6 +970,137 @@ list($a,$b)=each($this->me); return sizeof($b); } +} + + +/** + * date helpers + */ +function iso8601_encode($timet, $utc=0) { + // return an ISO8601 encoded string + // really, timezones ought to be supported + // but the XML-RPC spec says: + // + // "Don't assume a timezone. It should be specified by the server in its + // documentation what assumptions it makes about timezones." + // + // these routines always assume localtime unless + // $utc is set to 1, in which case UTC is assumed + // and an adjustment for locale is made when encoding + if (!$utc) { + $t=strftime("%Y%m%dT%H:%M:%S", $timet); + } else { + if (function_exists("gmstrftime")) + // gmstrftime doesn't exist in some versions + // of PHP + $t=gmstrftime("%Y%m%dT%H:%M:%S", $timet); + else { + $t=strftime("%Y%m%dT%H:%M:%S", $timet-date("Z")); + } + } + + return $t; +} + +function iso8601_decode($idate, $utc=0) { + // return a timet in the localtime, or UTC + $t=0; + if (ereg("([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})",$idate, $regs)) { + + if ($utc) { + $t=gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]); + } else { + $t=mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]); + } + } + + return $t; +} + +/****************************************************************** +* XML_RPC_decode takes a message in PHP XML_RPC object format and * +* tranlates it into native PHP types. * +* * +* author: Dan Libby (dan@libby.com) * +******************************************************************/ +function XML_RPC_decode($XML_RPC_val) { + $kind = $XML_RPC_val->kindOf(); + + if($kind == "scalar") { + return $XML_RPC_val->scalarval(); + } + else if($kind == "array") { + $size = $XML_RPC_val->arraysize(); + $arr = array(); + + for($i = 0; $i < $size; $i++) { + $arr[]=XML_RPC_decode($XML_RPC_val->arraymem($i)); + } + return $arr; + } + else if($kind == "struct") { + $XML_RPC_val->structreset(); + $arr = array(); + + while(list($key,$value)=$XML_RPC_val->structeach()) { + $arr[$key] = XML_RPC_decode($value); + } + return $arr; + } +} + +/***************************************************************** +* XML_RPC_encode takes native php types and encodes them into * +* XML_RPC PHP object format. * +* BUG: All sequential arrays are turned into structs. I don't * +* know of a good way to determine if an array is sequential * +* only. * +* * +* feature creep -- could support more types via optional type * +* argument. * +* * +* author: Dan Libby (dan@libby.com) * +*****************************************************************/ +function XML_RPC_encode($php_val) { + global $XML_RPC_Int; + global $XML_RPC_Double; + global $XML_RPC_String; + global $XML_RPC_Array; + global $XML_RPC_Struct; + + $type = gettype($php_val); + $XML_RPC_val = new XML_RPC_val; + + switch($type) { + case "array": + case "object": + $arr = array(); + while (list($k,$v) = each($php_val)) { + $arr[$k] = XML_RPC_encode($v); + } + $XML_RPC_val->addStruct($arr); + break; + case "integer": + $XML_RPC_val->addScalar($php_val, $XML_RPC_Int); + break; + case "double": + $XML_RPC_val->addScalar($php_val, $XML_RPC_Double); + break; + case "string": + $XML_RPC_val->addScalar($php_val, $XML_RPC_String); + break; +// <G_Giunta_2001-02-29> +// Add support for encoding/decoding of booleans, since they are supported in PHP + case "boolean": + $XML_RPC_val->addScalar($php_val, $XML_RPC_Boolean); + break; +// </G_Giunta_2001-02-29> + case "unknown type": + default: + $XML_RPC_val = false; + break; + } + return $XML_RPC_val; } ?>
Index: php4/pear/XML/RPC.php diff -u php4/pear/XML/RPC.php:1.7 php4/pear/XML/RPC.php:1.8 --- php4/pear/XML/RPC.php:1.7 Wed Jul 11 01:29:12 2001 +++ php4/pear/XML/RPC.php Thu Jul 12 12:38:51 2001 @@ -1,7 +1,7 @@ <?php -// by Edd Dumbill (C) 1999-2000 +// by Edd Dumbill (C) 1999-2001 // <edd@usefulinc.com> -// $Id: RPC.php,v 1.7 2001/07/11 05:29:12 mj Exp $ +// $Id: RPC.php,v 1.8 2001/07/12 16:38:51 mj Exp $ // License is granted to use or modify this software ("XML-RPC for PHP") // for commercial or non-commercial use provided the copyright of the author @@ -18,7 +18,8 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// Adapted to PEAR standards by Stig Sæther Bakken <stig@php.net> +// Adapted to PEAR standards by Stig Sæther Bakken <stig@php.net> and +// Martin Jansen <mj@php.net> require_once "PEAR.php"; @@ -156,6 +157,7 @@ $XML_RPC_xh[$parser]['lv']=1; $XML_RPC_xh[$parser]['vt']=$XML_RPC_String; $XML_RPC_xh[$parser]['ac']=""; + $XML_RPC_xh[$parser]['qt']=0; // look for a value: if this is still 1 by the // time we reach the first data segment then the type is string // by implication and we need to add in a quote @@ -177,6 +179,9 @@ } else if ($name=="BASE64") { $XML_RPC_xh[$parser]['qt']=2; } else { + // No quoting is required here -- but + // at the end of the element we must check + // for data format errors. $XML_RPC_xh[$parser]['qt']=0; } break; @@ -228,10 +233,23 @@ $XML_RPC_xh[$parser]['st'].="\"". $XML_RPC_xh[$parser]['ac'] . "\""; } else if ($XML_RPC_xh[$parser]['qt']==2) { $XML_RPC_xh[$parser]['st'].="base64_decode('". $XML_RPC_xh[$parser]['ac'] . "')"; + } else if ($name=="BOOLEAN") { + $XML_RPC_xh[$parser]['st'].=$_xh[$parser]['ac']; + } else { + // we have an I4, INT or a DOUBLE + // we must check that only 0123456789-.<space> are characters here + if (!ereg("^\-?[0123456789 \t\.]+$", $XML_RPC_xh[$parser]['ac'])) { + // TODO: find a better way of throwing an error + // than this! + error_log("XML-RPC: non numeric value received in INT or DOUBLE"); + $XML_RPC_xh[$parser]['st'].="ERROR_NON_NUMERIC_FOUND"; } else { + // it's ok, add it on $XML_RPC_xh[$parser]['st'].=$XML_RPC_xh[$parser]['ac']; } + $XML_RPC_xh[$parser]['ac']=""; $XML_RPC_xh[$parser]['qt']=0; + $_xh[$parser]['lv']=3; // indicate we've found a value break; case "VALUE": // deal with a string value @@ -280,21 +298,33 @@ function XML_RPC_cd($parser, $data) { - global $XML_RPC_xh, $XML_RPC_backslash, $XML_RPC_twoslash; + global $XML_RPC_xh, $XML_RPC_backslash; //if (ereg("^[\n\r \t]+$", $data)) return; // print "adding [${data}]\n"; - if ($XML_RPC_xh[$parser]['lv']==1) { - $XML_RPC_xh[$parser]['qt']=1; - $XML_RPC_xh[$parser]['lv']=2; - } - if ($XML_RPC_xh[$parser]['qt']) { // quoted string - $XML_RPC_xh[$parser]['ac'].=str_replace('\$', '\\$', + + if ($XML_RPC_xh[$parser]['lv']!=3) { + // "lookforvalue==3" means that we've found an entire value + // and should discard any further character data + + if ($XML_RPC_xh[$parser]['lv']==1) { + // if we've found text and we're just in a <value> then + // turn quoting on, as this will be a string + $XML_RPC_xh[$parser]['qt']=1; + // and say we've found a value + $XML_RPC_xh[$parser]['lv']=2; + } + + if (isset($XML_RPC_xh[$parser]['qt']) && $XML_RPC_xh[$parser]['qt']) { + // quoted string: replace characters that eval would + // do special things with + $XML_RPC_xh[$parser]['ac'].=str_replace('\$', '\\$', str_replace('"', '\"', str_replace(chr(92),$XML_RPC_backslash, $data))); + } + else + $XML_RPC_xh[$parser]['ac'].=$data; } - else - $XML_RPC_xh[$parser]['ac'].=$data; } @@ -355,90 +385,6 @@ } -/**************************************************************** -* XML_RPC_decode takes a message in PHP xmlrpc object format * -* and tranlates it into native PHP types. * -* * -* author: Dan Libby (dan@libby.com) * -****************************************************************/ -function XML_RPC_decode($xmlrpcval) -{ - $kind = $xmlrpcval->kindOf(); - - if($kind == "scalar") { - return $xmlrpcval->scalarval(); - } - else if($kind == "array") { - $size = $xmlrpcval->arraysize(); - $arr = array(); - - for($i = 0; $i < $size; $i++) { - array_append($arr, XML_RPC_decode($xmlrpcval->arraymem($i)) ); - } - return $arr; - } - else if($kind == "struct") { - $xmlrpcval->structreset(); - $arr = array(); - - while(list($key,$value)=$xmlrpcval->structeach()) { - $arr[$key] = XML_RPC_decode($value); - } - return $arr; - } -} - - -/**************************************************************** -* XML_RPC_encode takes native php types and encodes them into * -* xmlrpc PHP object format. * -* BUG: All sequential arrays are turned into structs. I don't * -* know of a good way to determine if an array is sequential * -* only. * -* * -* feature creep -- could support more types via optional type * -* argument. * -* * -* author: Dan Libby (dan@libby.com) * -****************************************************************/ -function XML_RPC_encode($php_val) -{ - global $XML_RPC_Int; - global $XML_RPC_Double; - global $XML_RPC_String; - global $XML_RPC_Array; - global $XML_RPC_Struct; - - $type = gettype($php_val); - $xmlrpcval = new XML_RPC_Value; - - switch ($type) { - case "array": - case "object": - $arr = array(); - while (list($k,$v) = each($php_val)) { - $arr[$k] = XML_RPC_encode($v); - } - $xmlrpcval->addStruct($arr); - break; - case "integer": - $xmlrpcval->addScalar($php_val, $XML_RPC_Int); - break; - case "double": - $xmlrpcval->addScalar($php_val, $XML_RPC_Double); - break; - case "string": - $xmlrpcval->addScalar($php_val, $XML_RPC_String); - break; - case "unknown type": - default: - $xmlrpcval = false; - break; - } - return $xmlrpcval; -} - - class XML_RPC_Client { var $path; @@ -455,8 +401,8 @@ $this->port=$port; $this->server=$server; $this->path=$path; - $this->proxy = $proxy; - $this->proxy_port = $proxy_port ; + $this->proxy = $proxy; + $this->proxy_port = $proxy_port ; } function setDebug($in) @@ -570,7 +516,14 @@ } } - function faultCode() { return $this->fn; } + function faultCode() + { + if (isset($this->fn)) + return $this->fn; + else + return 0; + } + function faultString() { return $this->fs; } function value() { return $this->xv; } @@ -685,12 +638,13 @@ $XML_RPC_xh[$parser]['cm']=0; $XML_RPC_xh[$parser]['isf']=0; $XML_RPC_xh[$parser]['ac']=""; - + $XML_RPC_xh[$parser]['qt']=""; + xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true); xml_set_element_handler($parser, "XML_RPC_se", "XML_RPC_ee"); xml_set_character_data_handler($parser, "XML_RPC_cd"); xml_set_default_handler($parser, "XML_RPC_dh"); - //$xmlrpcvalue=new XML_RPC_Value; + $xmlrpc_value = new XML_RPC_Value; $hdrfnd=0; if ($this->debug) @@ -730,28 +684,28 @@ } xml_parser_free($parser); if ($this->debug) { - print "<PRE>---EVALING---[" . - strlen($XML_RPC_xh[$parser]['st']) . " chars]---\n" . - htmlspecialchars($XML_RPC_xh[$parser]['st']) . ";\n---END---</PRE>"; + print "<PRE>---EVALING---[" . + strlen($XML_RPC_xh[$parser]['st']) . " chars]---\n" . + htmlspecialchars($XML_RPC_xh[$parser]['st']) . ";\n---END---</PRE>"; } if (strlen($XML_RPC_xh[$parser]['st'])==0) { - // then something odd has happened - // and it's time to generate a client side error - // indicating something odd went on - $r=new XML_RPC_Response(0, $XML_RPC_err["invalid_return"], - $XML_RPC_str["invalid_return"]); - } else { - eval('$v=' . $XML_RPC_xh[$parser]['st'] . '; $allOK=1;'); - if ($XML_RPC_xh[$parser]['isf']) { - $f=$v->structmem("faultCode"); - $fs=$v->structmem("faultString"); - $r=new XML_RPC_Response($v, $f->scalarval(), + // then something odd has happened + // and it's time to generate a client side error + // indicating something odd went on + $r=new XML_RPC_Response(0, $XML_RPC_err["invalid_return"], + $XML_RPC_str["invalid_return"]); + } else { + eval('$v=' . $XML_RPC_xh[$parser]['st'] . '; $allOK=1;'); + if ($XML_RPC_xh[$parser]['isf']) { + $f=$v->structmem("faultCode"); + $fs=$v->structmem("faultString"); + $r=new XML_RPC_Response($v, $f->scalarval(), $fs->scalarval()); - } else { - $r=new XML_RPC_Response($v); - } + } else { + $r=new XML_RPC_Response($v); + } } - $r->hdrs=split('\r?\n', $XML_RPC_xh[$parser]['ha'][1]); + $r->hdrs=split('\r?\n', $XML_RPC_xh[$parser]['ha'][1]); return $r; } @@ -795,7 +749,11 @@ } if ($type==$XML_RPC_Boolean) { - if (strcasecmp($val,"true")==0 || $val==1 || $val==true) { + if (strcasecmp($val,"true")==0 || + $val==1 || + ($val==true && + strcasecmp($val,"false"))) { + $val=1; } else { $val=0; @@ -873,7 +831,7 @@ function serializedata($typ, $val) { $rs=""; - global $XML_RPC_Types, $XML_RPC_Base64, $XML_RPC_String; + global $XML_RPC_Types, $XML_RPC_Base64, $XML_RPC_String, $XML_RPC_Boolean; switch($XML_RPC_Types[$typ]) { case 3: // struct @@ -948,7 +906,40 @@ { return each($this->me["struct"]); } + + function getval() { + // UNSTABLE + global $XML_RPC_BOOLEAN, $XML_RPC_Base64; + + reset($this->me); + list($a,$b)=each($this->me); + + // contributed by I Sofer, 2001-03-24 + // add support for nested arrays to scalarval + // i've created a new method here, so as to + // preserve back compatibility + + if (is_array($b)) { + foreach ($b as $id => $cont) { + $b[$id] = $cont->scalarval(); + } + } + // add support for structures directly encoding php objects + if (is_object($b)) { + $t = get_object_vars($b); + foreach ($t as $id => $cont) { + $t[$id] = $cont->scalarval(); + } + foreach ($t as $id => $cont) { + eval('$b->'.$id.' = $cont;'); + } + } + + // end contrib + return $b; + } + function scalarval() { global $XML_RPC_Boolean, $XML_RPC_Base64; @@ -979,6 +970,137 @@ list($a,$b)=each($this->me); return sizeof($b); } +} + + +/** + * date helpers + */ +function iso8601_encode($timet, $utc=0) { + // return an ISO8601 encoded string + // really, timezones ought to be supported + // but the XML-RPC spec says: + // + // "Don't assume a timezone. It should be specified by the server in its + // documentation what assumptions it makes about timezones." + // + // these routines always assume localtime unless + // $utc is set to 1, in which case UTC is assumed + // and an adjustment for locale is made when encoding + if (!$utc) { + $t=strftime("%Y%m%dT%H:%M:%S", $timet); + } else { + if (function_exists("gmstrftime")) + // gmstrftime doesn't exist in some versions + // of PHP + $t=gmstrftime("%Y%m%dT%H:%M:%S", $timet); + else { + $t=strftime("%Y%m%dT%H:%M:%S", $timet-date("Z")); + } + } + + return $t; +} + +function iso8601_decode($idate, $utc=0) { + // return a timet in the localtime, or UTC + $t=0; + if (ereg("([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})",$idate, $regs)) { + + if ($utc) { + $t=gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]); + } else { + $t=mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]); + } + } + + return $t; +} + +/****************************************************************** +* XML_RPC_decode takes a message in PHP XML_RPC object format and * +* tranlates it into native PHP types. * +* * +* author: Dan Libby (dan@libby.com) * +******************************************************************/ +function XML_RPC_decode($XML_RPC_val) { + $kind = $XML_RPC_val->kindOf(); + + if($kind == "scalar") { + return $XML_RPC_val->scalarval(); + } + else if($kind == "array") { + $size = $XML_RPC_val->arraysize(); + $arr = array(); + + for($i = 0; $i < $size; $i++) { + $arr[]=XML_RPC_decode($XML_RPC_val->arraymem($i)); + } + return $arr; + } + else if($kind == "struct") { + $XML_RPC_val->structreset(); + $arr = array(); + + while(list($key,$value)=$XML_RPC_val->structeach()) { + $arr[$key] = XML_RPC_decode($value); + } + return $arr; + } +} + +/***************************************************************** +* XML_RPC_encode takes native php types and encodes them into * +* XML_RPC PHP object format. * +* BUG: All sequential arrays are turned into structs. I don't * +* know of a good way to determine if an array is sequential * +* only. * +* * +* feature creep -- could support more types via optional type * +* argument. * +* * +* author: Dan Libby (dan@libby.com) * +*****************************************************************/ +function XML_RPC_encode($php_val) { + global $XML_RPC_Int; + global $XML_RPC_Double; + global $XML_RPC_String; + global $XML_RPC_Array; + global $XML_RPC_Struct; + + $type = gettype($php_val); + $XML_RPC_val = new XML_RPC_val; + + switch($type) { + case "array": + case "object": + $arr = array(); + while (list($k,$v) = each($php_val)) { + $arr[$k] = XML_RPC_encode($v); + } + $XML_RPC_val->addStruct($arr); + break; + case "integer": + $XML_RPC_val->addScalar($php_val, $XML_RPC_Int); + break; + case "double": + $XML_RPC_val->addScalar($php_val, $XML_RPC_Double); + break; + case "string": + $XML_RPC_val->addScalar($php_val, $XML_RPC_String); + break; +// <G_Giunta_2001-02-29> +// Add support for encoding/decoding of booleans, since they are supported in PHP + case "boolean": + $XML_RPC_val->addScalar($php_val, $XML_RPC_Boolean); + break; +// </G_Giunta_2001-02-29> + case "unknown type": + default: + $XML_RPC_val = false; + break; + } + return $XML_RPC_val; } ?>