2 // by Edd Dumbill (C) 1999-2001
4 // $Id: xmlrpc.inc.php,v 1.1.1.1 2005-02-28 07:14:15 kimitake Exp $
7 Modifications made for use with Nucleus:
10 xmlrpc_encode -> _xmlrpc_encode
11 xmlrpc_decode -> _xmlrpc_decode
15 // Copyright (c) 1999,2000,2001 Edd Dumbill.
16 // All rights reserved.
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
25 // * Redistributions in binary form must reproduce the above
26 // copyright notice, this list of conditions and the following
27 // disclaimer in the documentation and/or other materials provided
28 // with the distribution.
30 // * Neither the name of the "XML-RPC for PHP" nor the names of its
31 // contributors may be used to endorse or promote products derived
32 // from this software without specific prior written permission.
34 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
37 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
38 // REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
39 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
45 // OF THE POSSIBILITY OF SUCH DAMAGE.
47 if (!function_exists('xml_parser_create')) {
48 // Win 32 fix. From: "Leo West" <lwest@imaginet.fr>
58 $xmlrpcBoolean="boolean";
59 $xmlrpcDouble="double";
60 $xmlrpcString="string";
61 $xmlrpcDateTime="dateTime.iso8601";
62 $xmlrpcBase64="base64";
64 $xmlrpcStruct="struct";
67 $xmlrpcTypes=array($xmlrpcI4 => 1,
77 $xmlEntities=array( "amp" => "&",
83 $xmlrpcerr["unknown_method"]=1;
84 $xmlrpcstr["unknown_method"]="Unknown method";
85 $xmlrpcerr["invalid_return"]=2;
86 $xmlrpcstr["invalid_return"]="Invalid return payload: enabling debugging to examine incoming payload";
87 $xmlrpcerr["incorrect_params"]=3;
88 $xmlrpcstr["incorrect_params"]="Incorrect parameters passed to method";
89 $xmlrpcerr["introspect_unknown"]=4;
90 $xmlrpcstr["introspect_unknown"]="Can't introspect: method unknown";
91 $xmlrpcerr["http_error"]=5;
92 $xmlrpcstr["http_error"]="Didn't receive 200 OK from remote server.";
93 $xmlrpcerr["no_data"]=6;
94 $xmlrpcstr["no_data"]="No data received from server.";
95 $xmlrpcerr["no_ssl"]=7;
96 $xmlrpcstr["no_ssl"]="No SSL support compiled in.";
97 $xmlrpcerr["curl_fail"]=8;
98 $xmlrpcstr["curl_fail"]="CURL error";
100 $xmlrpc_defencoding="UTF-8";
102 $xmlrpcName="XML-RPC for PHP";
103 $xmlrpcVersion="1.02";
105 // let user errors start at 800
107 // let XML parse errors start at 100
110 // formulate backslashes for escaping regexp
111 $xmlrpc_backslash=chr(92).chr(92);
113 // used to store state during parsing
114 // quick explanation of components:
115 // st - used to build up a string for evaluation
116 // ac - used to accumulate values
117 // qt - used to decide if quotes are needed for evaluation
118 // cm - used to denote struct or array (comma needed)
119 // isf - used to indicate a fault
120 // lv - used to indicate "looking for a value": implements
121 // the logic to allow values with no types to be strings
122 // params - used to store parameters in method calls
123 // method - used to store method name
127 function xmlrpc_entity_decode($string) {
128 $top=split("&", $string);
131 while($i<sizeof($top)) {
132 if (ereg("^([#a-zA-Z0-9]+);", $top[$i], $regs)) {
133 $op.=ereg_replace("^[#a-zA-Z0-9]+;",
134 xmlrpc_lookup_entity($regs[1]),
147 function xmlrpc_lookup_entity($ent) {
150 if (isset($xmlEntities[strtolower($ent)]))
151 return $xmlEntities[strtolower($ent)];
152 if (ereg("^#([0-9]+)$", $ent, $regs))
153 return chr($regs[1]);
157 function xmlrpc_se($parser, $name, $attrs) {
158 global $_xh, $xmlrpcDateTime, $xmlrpcString;
163 $_xh[$parser]['st'].="array(";
164 $_xh[$parser]['cm']++;
165 // this last line turns quoting off
166 // this means if we get an empty array we'll
167 // simply get a bit of whitespace in the eval
168 $_xh[$parser]['qt']=0;
171 $_xh[$parser]['st'].="'"; $_xh[$parser]['ac']="";
174 $_xh[$parser]['isf']=1;
177 $_xh[$parser]['st']="";
180 $_xh[$parser]['st'].="new xmlrpcval(";
181 $_xh[$parser]['vt']=$xmlrpcString;
182 $_xh[$parser]['ac']="";
183 $_xh[$parser]['qt']=0;
184 $_xh[$parser]['lv']=1;
185 // look for a value: if this is still 1 by the
186 // time we reach the first data segment then the type is string
187 // by implication and we need to add in a quote
195 case "DATETIME.ISO8601":
197 $_xh[$parser]['ac']=""; // reset the accumulator
199 if ($name=="DATETIME.ISO8601" || $name=="STRING") {
200 $_xh[$parser]['qt']=1;
201 if ($name=="DATETIME.ISO8601")
202 $_xh[$parser]['vt']=$xmlrpcDateTime;
203 } else if ($name=="BASE64") {
204 $_xh[$parser]['qt']=2;
206 // No quoting is required here -- but
207 // at the end of the element we must check
208 // for data format errors.
209 $_xh[$parser]['qt']=0;
213 $_xh[$parser]['ac']="";
219 if ($name!="VALUE") $_xh[$parser]['lv']=0;
222 function xmlrpc_ee($parser, $name) {
223 global $_xh,$xmlrpcTypes,$xmlrpcString;
228 if ($_xh[$parser]['cm'] && substr($_xh[$parser]['st'], -1) ==',') {
229 $_xh[$parser]['st']=substr($_xh[$parser]['st'],0,-1);
231 $_xh[$parser]['st'].=")";
232 $_xh[$parser]['vt']=strtolower($name);
233 $_xh[$parser]['cm']--;
236 $_xh[$parser]['st'].= $_xh[$parser]['ac'] . "' => ";
239 // special case here: we translate boolean 1 or 0 into PHP
240 // constants true or false
241 if ($_xh[$parser]['ac']=='1')
242 $_xh[$parser]['ac']="true";
244 $_xh[$parser]['ac']="false";
245 $_xh[$parser]['vt']=strtolower($name);
246 // Drop through intentionally.
251 case "DATETIME.ISO8601":
253 if ($_xh[$parser]['qt']==1) {
254 // we use double quotes rather than single so backslashification works OK
255 $_xh[$parser]['st'].="\"". $_xh[$parser]['ac'] . "\"";
256 } else if ($_xh[$parser]['qt']==2) {
257 $_xh[$parser]['st'].="base64_decode('". $_xh[$parser]['ac'] . "')";
258 } else if ($name=="BOOLEAN") {
259 $_xh[$parser]['st'].=$_xh[$parser]['ac'];
261 // we have an I4, INT or a DOUBLE
262 // we must check that only 0123456789-.<space> are characters here
263 if (!ereg("^\-?[0123456789 \t\.]+$", $_xh[$parser]['ac'])) {
264 // TODO: find a better way of throwing an error
266 error_log("XML-RPC: non numeric value received in INT or DOUBLE");
267 $_xh[$parser]['st'].="ERROR_NON_NUMERIC_FOUND";
269 // it's ok, add it on
270 $_xh[$parser]['st'].=$_xh[$parser]['ac'];
273 $_xh[$parser]['ac']=""; $_xh[$parser]['qt']=0;
274 $_xh[$parser]['lv']=3; // indicate we've found a value
277 // deal with a string value
278 if (strlen($_xh[$parser]['ac'])>0 &&
279 $_xh[$parser]['vt']==$xmlrpcString) {
280 $_xh[$parser]['st'].="\"". $_xh[$parser]['ac'] . "\"";
282 // This if() detects if no scalar was inside <VALUE></VALUE>
283 // and pads an empty "".
284 if($_xh[$parser]['st'][strlen($_xh[$parser]['st'])-1] == '(') {
285 $_xh[$parser]['st'].= '""';
287 $_xh[$parser]['st'].=", '" . $_xh[$parser]['vt'] . "')";
288 if ($_xh[$parser]['cm']) $_xh[$parser]['st'].=",";
291 $_xh[$parser]['ac']=""; $_xh[$parser]['qt']=0;
294 $_xh[$parser]['ac']=""; $_xh[$parser]['qt']=0;
297 $_xh[$parser]['params'][]=$_xh[$parser]['st'];
300 $_xh[$parser]['method']=ereg_replace("^[\n\r\t ]+", "",
301 $_xh[$parser]['ac']);
304 // special case here: we translate boolean 1 or 0 into PHP
305 // constants true or false
306 if ($_xh[$parser]['ac']=='1')
307 $_xh[$parser]['ac']="true";
309 $_xh[$parser]['ac']="false";
310 $_xh[$parser]['vt']=strtolower($name);
315 // if it's a valid type name, set the type
316 if (isset($xmlrpcTypes[strtolower($name)])) {
317 $_xh[$parser]['vt']=strtolower($name);
322 function xmlrpc_cd($parser, $data)
324 global $_xh, $xmlrpc_backslash;
326 //if (ereg("^[\n\r \t]+$", $data)) return;
327 // print "adding [${data}]\n";
329 if ($_xh[$parser]['lv']!=3) {
330 // "lookforvalue==3" means that we've found an entire value
331 // and should discard any further character data
332 if ($_xh[$parser]['lv']==1) {
333 // if we've found text and we're just in a <value> then
334 // turn quoting on, as this will be a string
335 $_xh[$parser]['qt']=1;
336 // and say we've found a value
337 $_xh[$parser]['lv']=2;
339 // replace characters that eval would
340 // do special things with
341 $_xh[$parser]['ac'].=str_replace('$', '\$',
342 str_replace('"', '\"', str_replace(chr(92),
343 $xmlrpc_backslash, $data)));
347 function xmlrpc_dh($parser, $data)
350 if (substr($data, 0, 1) == "&" && substr($data, -1, 1) == ";") {
351 if ($_xh[$parser]['lv']==1) {
352 $_xh[$parser]['qt']=1;
353 $_xh[$parser]['lv']=2;
355 $_xh[$parser]['ac'].=str_replace('$', '\$',
356 str_replace('"', '\"', str_replace(chr(92),
357 $xmlrpc_backslash, $data)));
361 class xmlrpc_client {
373 function xmlrpc_client($path, $server, $port=0) {
374 $this->port=$port; $this->server=$server; $this->path=$path;
377 function setDebug($in) {
385 function setCredentials($u, $p) {
390 function setCertificate($cert, $certpass) {
392 $this->certpass = $certpass;
395 function send($msg, $timeout=0, $method='http') {
396 // where msg is an xmlrpcmsg
397 $msg->debug=$this->debug;
399 if ($method == 'https') {
400 return $this->sendPayloadHTTPS($msg,
402 $this->port, $timeout,
403 $this->username, $this->password,
407 return $this->sendPayloadHTTP10($msg, $this->server, $this->port,
408 $timeout, $this->username,
413 function sendPayloadHTTP10($msg, $server, $port, $timeout=0,
414 $username="", $password="") {
415 if ($port==0) $port=80;
417 $fp=@fsockopen($server, $port,
418 $this->errno, $this->errstr, $timeout);
420 $fp=@fsockopen($server, $port,
421 $this->errno, $this->errstr);
425 // Only create the payload if it was not created previously
426 if(empty($msg->payload)) $msg->createPayload();
428 // thanks to Grant Rauscher <grant7@firstworld.net>
432 $credentials="Authorization: Basic " .
433 base64_encode($username . ":" . $password) . "\r\n";
436 $op= "POST " . $this->path. " HTTP/1.0\r\nUser-Agent: PHP XMLRPC 1.0\r\n" .
437 "Host: ". $this->server . "\r\n" .
439 "Content-Type: text/xml\r\nContent-Length: " .
440 strlen($msg->payload) . "\r\n\r\n" .
443 if (!fputs($fp, $op, strlen($op))) {
444 $this->errstr="Write error";
447 $resp=$msg->parseResponseFile($fp);
452 // contributed by Justin Miller <justin@voxel.net>
453 // requires curl to be built into PHP
454 function sendPayloadHTTPS($msg, $server, $port, $timeout=0,
455 $username="", $password="", $cert="",
457 global $xmlrpcerr, $xmlrpcstr;
458 if ($port == 0) $port = 443;
460 // Only create the payload if it was not created previously
461 if(empty($msg->payload)) $msg->createPayload();
463 if (!function_exists("curl_init")) {
464 $r=new xmlrpcresp(0, $xmlrpcerr["no_ssl"],
465 $xmlrpcstr["no_ssl"]);
469 $curl = curl_init("https://" . $server . ':' . $port .
472 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
473 // results into variable
475 curl_setopt($curl, CURLOPT_VERBOSE, 1);
477 curl_setopt($curl, CURLOPT_USERAGENT, 'PHP XMLRPC 1.0');
478 // required for XMLRPC
479 curl_setopt($curl, CURLOPT_POST, 1);
481 curl_setopt($curl, CURLOPT_POSTFIELDS, $msg->payload);
483 curl_setopt($curl, CURLOPT_HEADER, 1);
484 // return the header too
485 curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
486 // required for XMLRPC
487 if ($timeout) curl_setopt($curl, CURLOPT_TIMEOUT, $timeout == 1 ? 1 :
490 if ($username && $password) curl_setopt($curl, CURLOPT_USERPWD,
491 "$username:$password");
493 if ($cert) curl_setopt($curl, CURLOPT_SSLCERT, $cert);
495 if ($certpass) curl_setopt($curl, CURLOPT_SSLCERTPASSWD,
499 $result = curl_exec($curl);
502 $resp=new xmlrpcresp(0,
503 $xmlrpcerr["curl_fail"],
504 $xmlrpcstr["curl_fail"]. ": ".
507 $resp = $msg->parseResponse($result);
513 } // end class xmlrpc_client
521 function xmlrpcresp($val, $fcode=0, $fstr="") {
525 $this->fs=htmlspecialchars($fstr);
532 function faultCode() {
533 if (isset($this->fn))
539 function faultString() { return $this->fs; }
540 function value() { return $this->xv; }
542 function serialize() {
543 $rs="<methodResponse>\n";
549 <name>faultCode</name>
550 <value><int>" . $this->fn . "</int></value>
553 <name>faultString</name>
554 <value><string>" . $this->fs . "</string></value>
560 $rs.="<params>\n<param>\n" . $this->xv->serialize() .
561 "</param>\n</params>";
563 $rs.="\n</methodResponse>";
574 function xmlrpcmsg($meth, $pars=0) {
575 $this->methodname=$meth;
576 if (is_array($pars) && sizeof($pars)>0) {
577 for($i=0; $i<sizeof($pars); $i++)
578 $this->addParam($pars[$i]);
582 function xml_header() {
583 return "<" . "?xml version=\"1.0\"?".">\n<methodCall>\n";
586 function xml_footer() {
587 return "</methodCall>\n";
590 function createPayload() {
591 $this->payload=$this->xml_header();
592 $this->payload.="<methodName>" . $this->methodname . "</methodName>\n";
593 // if (sizeof($this->params)) {
594 $this->payload.="<params>\n";
595 for($i=0; $i<sizeof($this->params); $i++) {
596 $p=$this->params[$i];
597 $this->payload.="<param>\n" . $p->serialize() .
600 $this->payload.="</params>\n";
602 $this->payload.=$this->xml_footer();
603 $this->payload=str_replace("\n", "\r\n", $this->payload);
606 function method($meth="") {
608 $this->methodname=$meth;
610 return $this->methodname;
613 function serialize() {
614 $this->createPayload();
615 return $this->payload;
618 function addParam($par) { $this->params[]=$par; }
619 function getParam($i) { return $this->params[$i]; }
620 function getNumParams() { return sizeof($this->params); }
622 function parseResponseFile($fp) {
625 while($data=fread($fp, 32768)) {
628 return $this->parseResponse($ipd);
631 function parseResponse($data="") {
632 global $_xh,$xmlrpcerr,$xmlrpcstr;
633 global $xmlrpc_defencoding;
636 $parser = xml_parser_create($xmlrpc_defencoding);
638 $_xh[$parser]=array();
640 $_xh[$parser]['st']="";
641 $_xh[$parser]['cm']=0;
642 $_xh[$parser]['isf']=0;
643 $_xh[$parser]['ac']="";
644 $_xh[$parser]['qt']="";
645 $_xh[$parser]['ha']="";
646 $_xh[$parser]['ac']="";
648 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
649 xml_set_element_handler($parser, "xmlrpc_se", "xmlrpc_ee");
650 xml_set_character_data_handler($parser, "xmlrpc_cd");
651 xml_set_default_handler($parser, "xmlrpc_dh");
652 $xmlrpc_value=new xmlrpcval;
655 print "<PRE>---GOT---\n" . htmlspecialchars($data) .
656 "\n---END---\n</PRE>";
658 error_log("No response received from server.");
659 $r=new xmlrpcresp(0, $xmlrpcerr["no_data"],
660 $xmlrpcstr["no_data"]);
661 xml_parser_free($parser);
664 // see if we got an HTTP 200 OK, else bomb
665 // but only do this if we're using the HTTP protocol.
666 if (ereg("^HTTP",$data) &&
667 !ereg("^HTTP/[0-9\.]+ 200 ", $data)) {
668 $errstr= substr($data, 0, strpos($data, "\n")-1);
669 error_log("HTTP error, got response: " .$errstr);
670 $r=new xmlrpcresp(0, $xmlrpcerr["http_error"],
671 $xmlrpcstr["http_error"]. " (" . $errstr . ")");
672 xml_parser_free($parser);
676 // if using HTTP, then gotta get rid of HTTP headers here
677 // and we store them in the 'ha' bit of our data array
678 if (ereg("^HTTP", $data)) {
679 $ar=explode("\r\n", $data);
682 for ($i=0; $i<sizeof($ar); $i++) {
684 if (strlen($ar[$i])>0) {
685 $_xh[$parser]['ha'].=$ar[$i]. "\r\n";
690 $newdata.=$ar[$i] . "\r\n";
696 if (!xml_parse($parser, $data, sizeof($data))) {
697 // thanks to Peter Kocks <peter.kocks@baygate.com>
698 if((xml_get_current_line_number($parser)) == 1)
699 $errstr = "XML error at line 1, check URL";
701 $errstr = sprintf("XML error: %s at line %d",
702 xml_error_string(xml_get_error_code($parser)),
703 xml_get_current_line_number($parser));
705 $r=new xmlrpcresp(0, $xmlrpcerr["invalid_return"],
706 $xmlrpcstr["invalid_return"]);
707 xml_parser_free($parser);
710 xml_parser_free($parser);
712 print "<PRE>---EVALING---[" .
713 strlen($_xh[$parser]['st']) . " chars]---\n" .
714 htmlspecialchars($_xh[$parser]['st']) . ";\n---END---</PRE>";
716 if (strlen($_xh[$parser]['st'])==0) {
717 // then something odd has happened
718 // and it's time to generate a client side error
719 // indicating something odd went on
720 $r=new xmlrpcresp(0, $xmlrpcerr["invalid_return"],
721 $xmlrpcstr["invalid_return"]);
723 eval('$v=' . $_xh[$parser]['st'] . '; $allOK=1;');
724 if ($_xh[$parser]['isf']) {
725 $f=$v->structmem("faultCode");
726 $fs=$v->structmem("faultString");
727 $r=new xmlrpcresp($v, $f->scalarval(),
730 $r=new xmlrpcresp($v);
733 $r->hdrs=split("\r?\n", $_xh[$parser]['ha']);
743 function xmlrpcval($val=-1, $type="") {
747 if ($val!=-1 || $type!="") {
748 if ($type=="") $type="string";
749 if ($xmlrpcTypes[$type]==1) {
750 $this->addScalar($val,$type);
752 else if ($xmlrpcTypes[$type]==2)
753 $this->addArray($val);
754 else if ($xmlrpcTypes[$type]==3)
755 $this->addStruct($val);
759 function addScalar($val, $type="string") {
760 global $xmlrpcTypes, $xmlrpcBoolean;
762 if ($this->mytype==1) {
763 echo "<B>xmlrpcval</B>: scalar can have only one value<BR>";
766 $typeof=$xmlrpcTypes[$type];
768 echo "<B>xmlrpcval</B>: not a scalar type (${typeof})<BR>";
772 if ($type==$xmlrpcBoolean) {
773 if (strcasecmp($val,"true")==0 ||
774 $val==1 || ($val==true &&
775 strcasecmp($val,"false"))) {
782 if ($this->mytype==2) {
783 // we're adding to an array here
784 $ar=$this->me["array"];
785 $ar[]=new xmlrpcval($val, $type);
786 $this->me["array"]=$ar;
788 // a scalar, so set the value and remember we're scalar
789 $this->me[$type]=$val;
790 $this->mytype=$typeof;
795 function addArray($vals) {
797 if ($this->mytype!=0) {
798 echo "<B>xmlrpcval</B>: already initialized as a [" .
799 $this->kindOf() . "]<BR>";
803 $this->mytype=$xmlrpcTypes["array"];
804 $this->me["array"]=$vals;
808 function addStruct($vals) {
810 if ($this->mytype!=0) {
811 echo "<B>xmlrpcval</B>: already initialized as a [" .
812 $this->kindOf() . "]<BR>";
815 $this->mytype=$xmlrpcTypes["struct"];
816 $this->me["struct"]=$vals;
822 while ( list( $key, $val ) = each( $ar ) ) {
823 echo "$key => $val<br>";
825 while ( list( $key2, $val2 ) = each( $val ) ) {
826 echo "-- $key2 => $val2<br>";
832 switch($this->mytype) {
847 function serializedata($typ, $val) {
849 global $xmlrpcTypes, $xmlrpcBase64, $xmlrpcString,
851 switch($xmlrpcTypes[$typ]) {
856 while(list($key2, $val2)=each($val)) {
857 $rs.="<member><name>${key2}</name>\n";
858 $rs.=$this->serializeval($val2);
865 $rs.="<array>\n<data>\n";
866 for($i=0; $i<sizeof($val); $i++) {
867 $rs.=$this->serializeval($val[$i]);
869 $rs.="</data>\n</array>";
874 $rs.="<${typ}>" . base64_encode($val) . "</${typ}>";
877 $rs.="<${typ}>" . ($val ? "1" : "0") . "</${typ}>";
880 $rs.="<${typ}>" . htmlspecialchars($val). "</${typ}>";
883 $rs.="<${typ}>${val}</${typ}>";
892 function serialize() {
893 return $this->serializeval($this);
896 function serializeval($o) {
901 list($typ, $val) = each($ar);
903 $rs.=$this->serializedata($typ, $val);
908 function structmem($m) {
909 $nv=$this->me["struct"][$m];
913 function structreset() {
914 reset($this->me["struct"]);
917 function structeach() {
918 return each($this->me["struct"]);
923 global $xmlrpcBoolean, $xmlrpcBase64;
925 list($a,$b)=each($this->me);
926 // contributed by I Sofer, 2001-03-24
927 // add support for nested arrays to scalarval
928 // i've created a new method here, so as to
929 // preserve back compatibility
932 foreach ($b as $id => $cont) {
933 $b[$id] = $cont->scalarval();
937 // add support for structures directly encoding php objects
939 $t = get_object_vars($b);
940 foreach ($t as $id => $cont) {
941 $t[$id] = $cont->scalarval();
943 foreach ($t as $id => $cont) {
944 eval('$b->'.$id.' = $cont;');
951 function scalarval() {
952 global $xmlrpcBoolean, $xmlrpcBase64;
954 list($a,$b)=each($this->me);
958 function scalartyp() {
959 global $xmlrpcI4, $xmlrpcInt;
961 list($a,$b)=each($this->me);
967 function arraymem($m) {
968 $nv=$this->me["array"][$m];
972 function arraysize() {
974 list($a,$b)=each($this->me);
980 function iso8601_encode($timet, $utc=0) {
981 // return an ISO8601 encoded string
982 // really, timezones ought to be supported
983 // but the XML-RPC spec says:
985 // "Don't assume a timezone. It should be specified by the server in its
986 // documentation what assumptions it makes about timezones."
988 // these routines always assume localtime unless
989 // $utc is set to 1, in which case UTC is assumed
990 // and an adjustment for locale is made when encoding
992 $t=strftime("%Y%m%dT%H:%M:%S", $timet);
994 if (function_exists("gmstrftime"))
995 // gmstrftime doesn't exist in some versions
997 $t=gmstrftime("%Y%m%dT%H:%M:%S", $timet);
999 $t=strftime("%Y%m%dT%H:%M:%S", $timet-date("Z"));
1005 function iso8601_decode($idate, $utc=0) {
1006 // return a timet in the localtime, or UTC
1008 if (ereg("([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})",
1011 $t=gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
1013 $t=mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
1019 /****************************************************************
1020 * xmlrpc_decode takes a message in PHP xmlrpc object format and *
1021 * tranlates it into native PHP types. *
1023 * author: Dan Libby (dan@libby.com) *
1024 ****************************************************************/
1025 function _xmlrpc_decode($xmlrpc_val) {
1026 $kind = $xmlrpc_val->kindOf();
1028 if($kind == "scalar") {
1029 return $xmlrpc_val->scalarval();
1031 else if($kind == "array") {
1032 $size = $xmlrpc_val->arraysize();
1035 for($i = 0; $i < $size; $i++) {
1036 $arr[]=_xmlrpc_decode($xmlrpc_val->arraymem($i));
1040 else if($kind == "struct") {
1041 $xmlrpc_val->structreset();
1044 while(list($key,$value)=$xmlrpc_val->structeach()) {
1045 $arr[$key] = _xmlrpc_decode($value);
1051 /****************************************************************
1052 * xmlrpc_encode takes native php types and encodes them into *
1053 * xmlrpc PHP object format. *
1054 * BUG: All sequential arrays are turned into structs. I don't *
1055 * know of a good way to determine if an array is sequential *
1058 * feature creep -- could support more types via optional type *
1061 * author: Dan Libby (dan@libby.com) *
1062 ****************************************************************/
1063 function _xmlrpc_encode($php_val) {
1065 global $xmlrpcDouble;
1066 global $xmlrpcString;
1067 global $xmlrpcArray;
1068 global $xmlrpcStruct;
1069 global $xmlrpcBoolean;
1071 $type = gettype($php_val);
1072 $xmlrpc_val = new xmlrpcval;
1078 while (list($k,$v) = each($php_val)) {
1079 $arr[$k] = _xmlrpc_encode($v);
1081 $xmlrpc_val->addStruct($arr);
1084 $xmlrpc_val->addScalar($php_val, $xmlrpcInt);
1087 $xmlrpc_val->addScalar($php_val, $xmlrpcDouble);
1090 $xmlrpc_val->addScalar($php_val, $xmlrpcString);
1092 // <G_Giunta_2001-02-29>
1093 // Add support for encoding/decoding of booleans, since they are supported in PHP
1095 $xmlrpc_val->addScalar($php_val, $xmlrpcBoolean);
1097 // </G_Giunta_2001-02-29>
1098 case "unknown type":
1100 // giancarlo pinerolo <ping@alt.it>
1102 // an empty object in case (which is already
1103 // at this point), not a boolean.