2 // by Edd Dumbill (C) 1999-2001
\r
3 // <edd@usefulinc.com>
\r
4 // $Id: xmlrpc.inc.php,v 1.4 2005-03-16 08:10:35 kimitake Exp $
\r
5 // $NucleusJP: xmlrpc.inc.php,v 1.4 2005/03/08 06:49:33 kimitake Exp $
\r
8 Modifications made for use with Nucleus:
\r
11 xmlrpc_encode -> _xmlrpc_encode
\r
12 xmlrpc_decode -> _xmlrpc_decode
\r
16 // Copyright (c) 1999,2000,2001 Edd Dumbill.
\r
17 // All rights reserved.
\r
19 // Redistribution and use in source and binary forms, with or without
\r
20 // modification, are permitted provided that the following conditions
\r
23 // * Redistributions of source code must retain the above copyright
\r
24 // notice, this list of conditions and the following disclaimer.
\r
26 // * Redistributions in binary form must reproduce the above
\r
27 // copyright notice, this list of conditions and the following
\r
28 // disclaimer in the documentation and/or other materials provided
\r
29 // with the distribution.
\r
31 // * Neither the name of the "XML-RPC for PHP" nor the names of its
\r
32 // contributors may be used to endorse or promote products derived
\r
33 // from this software without specific prior written permission.
\r
35 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
\r
36 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
\r
37 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
\r
38 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
\r
39 // REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
40 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
\r
41 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
\r
42 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
\r
43 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
\r
44 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
\r
45 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
\r
46 // OF THE POSSIBILITY OF SUCH DAMAGE.
\r
48 if (!function_exists('xml_parser_create')) {
\r
49 // Win 32 fix. From: "Leo West" <lwest@imaginet.fr>
\r
59 $xmlrpcBoolean="boolean";
\r
60 $xmlrpcDouble="double";
\r
61 $xmlrpcString="string";
\r
62 $xmlrpcDateTime="dateTime.iso8601";
\r
63 $xmlrpcBase64="base64";
\r
64 $xmlrpcArray="array";
\r
65 $xmlrpcStruct="struct";
\r
68 $xmlrpcTypes=array($xmlrpcI4 => 1,
\r
70 $xmlrpcBoolean => 1,
\r
73 $xmlrpcDateTime => 1,
\r
76 $xmlrpcStruct => 3);
\r
78 $xmlEntities=array( "amp" => "&",
\r
84 $xmlrpcerr["unknown_method"]=1;
\r
85 $xmlrpcstr["unknown_method"]="Unknown method";
\r
86 $xmlrpcerr["invalid_return"]=2;
\r
87 $xmlrpcstr["invalid_return"]="Invalid return payload: enabling debugging to examine incoming payload";
\r
88 $xmlrpcerr["incorrect_params"]=3;
\r
89 $xmlrpcstr["incorrect_params"]="Incorrect parameters passed to method";
\r
90 $xmlrpcerr["introspect_unknown"]=4;
\r
91 $xmlrpcstr["introspect_unknown"]="Can't introspect: method unknown";
\r
92 $xmlrpcerr["http_error"]=5;
\r
93 $xmlrpcstr["http_error"]="Didn't receive 200 OK from remote server.";
\r
94 $xmlrpcerr["no_data"]=6;
\r
95 $xmlrpcstr["no_data"]="No data received from server.";
\r
96 $xmlrpcerr["no_ssl"]=7;
\r
97 $xmlrpcstr["no_ssl"]="No SSL support compiled in.";
\r
98 $xmlrpcerr["curl_fail"]=8;
\r
99 $xmlrpcstr["curl_fail"]="CURL error";
\r
101 $xmlrpc_defencoding="UTF-8";
\r
103 $xmlrpcName="XML-RPC for PHP";
\r
104 $xmlrpcVersion="1.02";
\r
106 // let user errors start at 800
\r
107 $xmlrpcerruser=800;
\r
108 // let XML parse errors start at 100
\r
111 // formulate backslashes for escaping regexp
\r
112 $xmlrpc_backslash=chr(92).chr(92);
\r
114 // used to store state during parsing
\r
115 // quick explanation of components:
\r
116 // st - used to build up a string for evaluation
\r
117 // ac - used to accumulate values
\r
118 // qt - used to decide if quotes are needed for evaluation
\r
119 // cm - used to denote struct or array (comma needed)
\r
120 // isf - used to indicate a fault
\r
121 // lv - used to indicate "looking for a value": implements
\r
122 // the logic to allow values with no types to be strings
\r
123 // params - used to store parameters in method calls
\r
124 // method - used to store method name
\r
128 function xmlrpc_entity_decode($string) {
\r
129 $top=split("&", $string);
\r
132 while($i<sizeof($top)) {
\r
133 if (ereg("^([#a-zA-Z0-9]+);", $top[$i], $regs)) {
\r
134 $op.=ereg_replace("^[#a-zA-Z0-9]+;",
\r
135 xmlrpc_lookup_entity($regs[1]),
\r
141 $op.="&" . $top[$i];
\r
148 function xmlrpc_lookup_entity($ent) {
\r
149 global $xmlEntities;
\r
151 if (isset($xmlEntities[strtolower($ent)]))
\r
152 return $xmlEntities[strtolower($ent)];
\r
153 if (ereg("^#([0-9]+)$", $ent, $regs))
\r
154 return chr($regs[1]);
\r
158 function xmlrpc_se($parser, $name, $attrs) {
\r
159 global $_xh, $xmlrpcDateTime, $xmlrpcString;
\r
164 $_xh[$parser]['st'].="array(";
\r
165 $_xh[$parser]['cm']++;
\r
166 // this last line turns quoting off
\r
167 // this means if we get an empty array we'll
\r
168 // simply get a bit of whitespace in the eval
\r
169 $_xh[$parser]['qt']=0;
\r
172 $_xh[$parser]['st'].="'"; $_xh[$parser]['ac']="";
\r
175 $_xh[$parser]['isf']=1;
\r
178 $_xh[$parser]['st']="";
\r
181 $_xh[$parser]['st'].="new xmlrpcval(";
\r
182 $_xh[$parser]['vt']=$xmlrpcString;
\r
183 $_xh[$parser]['ac']="";
\r
184 $_xh[$parser]['qt']=0;
\r
185 $_xh[$parser]['lv']=1;
\r
186 // look for a value: if this is still 1 by the
\r
187 // time we reach the first data segment then the type is string
\r
188 // by implication and we need to add in a quote
\r
196 case "DATETIME.ISO8601":
\r
198 $_xh[$parser]['ac']=""; // reset the accumulator
\r
200 if ($name=="DATETIME.ISO8601" || $name=="STRING") {
\r
201 $_xh[$parser]['qt']=1;
\r
202 if ($name=="DATETIME.ISO8601")
\r
203 $_xh[$parser]['vt']=$xmlrpcDateTime;
\r
204 } else if ($name=="BASE64") {
\r
205 $_xh[$parser]['qt']=2;
\r
207 // No quoting is required here -- but
\r
208 // at the end of the element we must check
\r
209 // for data format errors.
\r
210 $_xh[$parser]['qt']=0;
\r
214 $_xh[$parser]['ac']="";
\r
220 if ($name!="VALUE") $_xh[$parser]['lv']=0;
\r
223 function xmlrpc_ee($parser, $name) {
\r
224 global $_xh,$xmlrpcTypes,$xmlrpcString;
\r
229 if ($_xh[$parser]['cm'] && substr($_xh[$parser]['st'], -1) ==',') {
\r
230 $_xh[$parser]['st']=substr($_xh[$parser]['st'],0,-1);
\r
232 $_xh[$parser]['st'].=")";
\r
233 $_xh[$parser]['vt']=strtolower($name);
\r
234 $_xh[$parser]['cm']--;
\r
237 $_xh[$parser]['st'].= $_xh[$parser]['ac'] . "' => ";
\r
240 // special case here: we translate boolean 1 or 0 into PHP
\r
241 // constants true or false
\r
242 if ($_xh[$parser]['ac']=='1')
\r
243 $_xh[$parser]['ac']="true";
\r
245 $_xh[$parser]['ac']="false";
\r
246 $_xh[$parser]['vt']=strtolower($name);
\r
247 // Drop through intentionally.
\r
252 case "DATETIME.ISO8601":
\r
254 if ($_xh[$parser]['qt']==1) {
\r
255 // we use double quotes rather than single so backslashification works OK
\r
256 $_xh[$parser]['st'].="\"". $_xh[$parser]['ac'] . "\"";
\r
257 } else if ($_xh[$parser]['qt']==2) {
\r
258 $_xh[$parser]['st'].="base64_decode('". $_xh[$parser]['ac'] . "')";
\r
259 } else if ($name=="BOOLEAN") {
\r
260 $_xh[$parser]['st'].=$_xh[$parser]['ac'];
\r
262 // we have an I4, INT or a DOUBLE
\r
263 // we must check that only 0123456789-.<space> are characters here
\r
264 if (!ereg("^\-?[0123456789 \t\.]+$", $_xh[$parser]['ac'])) {
\r
265 // TODO: find a better way of throwing an error
\r
267 error_log("XML-RPC: non numeric value received in INT or DOUBLE");
\r
268 $_xh[$parser]['st'].="ERROR_NON_NUMERIC_FOUND";
\r
270 // it's ok, add it on
\r
271 $_xh[$parser]['st'].=$_xh[$parser]['ac'];
\r
274 $_xh[$parser]['ac']=""; $_xh[$parser]['qt']=0;
\r
275 $_xh[$parser]['lv']=3; // indicate we've found a value
\r
278 // deal with a string value
\r
279 if (strlen($_xh[$parser]['ac'])>0 &&
\r
280 $_xh[$parser]['vt']==$xmlrpcString) {
\r
281 $_xh[$parser]['st'].="\"". $_xh[$parser]['ac'] . "\"";
\r
283 // This if() detects if no scalar was inside <VALUE></VALUE>
\r
284 // and pads an empty "".
\r
285 if($_xh[$parser]['st'][strlen($_xh[$parser]['st'])-1] == '(') {
\r
286 $_xh[$parser]['st'].= '""';
\r
288 $_xh[$parser]['st'].=", '" . $_xh[$parser]['vt'] . "')";
\r
289 if ($_xh[$parser]['cm']) $_xh[$parser]['st'].=",";
\r
292 $_xh[$parser]['ac']=""; $_xh[$parser]['qt']=0;
\r
295 $_xh[$parser]['ac']=""; $_xh[$parser]['qt']=0;
\r
298 $_xh[$parser]['params'][]=$_xh[$parser]['st'];
\r
301 $_xh[$parser]['method']=ereg_replace("^[\n\r\t ]+", "",
\r
302 $_xh[$parser]['ac']);
\r
305 // special case here: we translate boolean 1 or 0 into PHP
\r
306 // constants true or false
\r
307 if ($_xh[$parser]['ac']=='1')
\r
308 $_xh[$parser]['ac']="true";
\r
310 $_xh[$parser]['ac']="false";
\r
311 $_xh[$parser]['vt']=strtolower($name);
\r
316 // if it's a valid type name, set the type
\r
317 if (isset($xmlrpcTypes[strtolower($name)])) {
\r
318 $_xh[$parser]['vt']=strtolower($name);
\r
323 function xmlrpc_cd($parser, $data)
\r
325 global $_xh, $xmlrpc_backslash;
\r
327 //if (ereg("^[\n\r \t]+$", $data)) return;
\r
328 // print "adding [${data}]\n";
\r
330 if ($_xh[$parser]['lv']!=3) {
\r
331 // "lookforvalue==3" means that we've found an entire value
\r
332 // and should discard any further character data
\r
333 if ($_xh[$parser]['lv']==1) {
\r
334 // if we've found text and we're just in a <value> then
\r
335 // turn quoting on, as this will be a string
\r
336 $_xh[$parser]['qt']=1;
\r
337 // and say we've found a value
\r
338 $_xh[$parser]['lv']=2;
\r
340 // replace characters that eval would
\r
341 // do special things with
\r
342 $_xh[$parser]['ac'].=str_replace('$', '\$',
\r
343 str_replace('"', '\"', str_replace(chr(92),
\r
344 $xmlrpc_backslash, $data)));
\r
348 function xmlrpc_dh($parser, $data)
\r
351 if (substr($data, 0, 1) == "&" && substr($data, -1, 1) == ";") {
\r
352 if ($_xh[$parser]['lv']==1) {
\r
353 $_xh[$parser]['qt']=1;
\r
354 $_xh[$parser]['lv']=2;
\r
356 $_xh[$parser]['ac'].=str_replace('$', '\$',
\r
357 str_replace('"', '\"', str_replace(chr(92),
\r
358 $xmlrpc_backslash, $data)));
\r
362 class xmlrpc_client {
\r
374 function xmlrpc_client($path, $server, $port=0) {
\r
375 $this->port=$port; $this->server=$server; $this->path=$path;
\r
378 function setDebug($in) {
\r
386 function setCredentials($u, $p) {
\r
387 $this->username=$u;
\r
388 $this->password=$p;
\r
391 function setCertificate($cert, $certpass) {
\r
392 $this->cert = $cert;
\r
393 $this->certpass = $certpass;
\r
396 function send($msg, $timeout=0, $method='http') {
\r
397 // where msg is an xmlrpcmsg
\r
398 $msg->debug=$this->debug;
\r
400 if ($method == 'https') {
\r
401 return $this->sendPayloadHTTPS($msg,
\r
403 $this->port, $timeout,
\r
404 $this->username, $this->password,
\r
408 return $this->sendPayloadHTTP10($msg, $this->server, $this->port,
\r
409 $timeout, $this->username,
\r
414 function sendPayloadHTTP10($msg, $server, $port, $timeout=0,
\r
415 $username="", $password="") {
\r
416 if ($port==0) $port=80;
\r
418 $fp=@fsockopen($server, $port,
\r
419 $this->errno, $this->errstr, $timeout);
\r
421 $fp=@fsockopen($server, $port,
\r
422 $this->errno, $this->errstr);
\r
426 // Only create the payload if it was not created previously
\r
427 if(empty($msg->payload)) $msg->createPayload();
\r
429 // thanks to Grant Rauscher <grant7@firstworld.net>
\r
432 if ($username!="") {
\r
433 $credentials="Authorization: Basic " .
\r
434 base64_encode($username . ":" . $password) . "\r\n";
\r
437 $op= "POST " . $this->path. " HTTP/1.0\r\nUser-Agent: PHP XMLRPC 1.0\r\n" .
\r
438 "Host: ". $this->server . "\r\n" .
\r
440 "Content-Type: text/xml\r\nContent-Length: " .
\r
441 strlen($msg->payload) . "\r\n\r\n" .
\r
444 if (!fputs($fp, $op, strlen($op))) {
\r
445 $this->errstr="Write error";
\r
448 $resp=$msg->parseResponseFile($fp);
\r
453 // contributed by Justin Miller <justin@voxel.net>
\r
454 // requires curl to be built into PHP
\r
455 function sendPayloadHTTPS($msg, $server, $port, $timeout=0,
\r
456 $username="", $password="", $cert="",
\r
458 global $xmlrpcerr, $xmlrpcstr;
\r
459 if ($port == 0) $port = 443;
\r
461 // Only create the payload if it was not created previously
\r
462 if(empty($msg->payload)) $msg->createPayload();
\r
464 if (!function_exists("curl_init")) {
\r
465 $r=new xmlrpcresp(0, $xmlrpcerr["no_ssl"],
\r
466 $xmlrpcstr["no_ssl"]);
\r
470 $curl = curl_init("https://" . $server . ':' . $port .
\r
473 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
\r
474 // results into variable
\r
475 if ($this->debug) {
\r
476 curl_setopt($curl, CURLOPT_VERBOSE, 1);
\r
478 curl_setopt($curl, CURLOPT_USERAGENT, 'PHP XMLRPC 1.0');
\r
479 // required for XMLRPC
\r
480 curl_setopt($curl, CURLOPT_POST, 1);
\r
482 curl_setopt($curl, CURLOPT_POSTFIELDS, $msg->payload);
\r
484 curl_setopt($curl, CURLOPT_HEADER, 1);
\r
485 // return the header too
\r
486 curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
\r
487 // required for XMLRPC
\r
488 if ($timeout) curl_setopt($curl, CURLOPT_TIMEOUT, $timeout == 1 ? 1 :
\r
490 // timeout is borked
\r
491 if ($username && $password) curl_setopt($curl, CURLOPT_USERPWD,
\r
492 "$username:$password");
\r
494 if ($cert) curl_setopt($curl, CURLOPT_SSLCERT, $cert);
\r
496 if ($certpass) curl_setopt($curl, CURLOPT_SSLCERTPASSWD,
\r
498 // set cert password
\r
500 $result = curl_exec($curl);
\r
503 $resp=new xmlrpcresp(0,
\r
504 $xmlrpcerr["curl_fail"],
\r
505 $xmlrpcstr["curl_fail"]. ": ".
\r
506 curl_error($curl));
\r
508 $resp = $msg->parseResponse($result);
\r
514 } // end class xmlrpc_client
\r
522 function xmlrpcresp($val, $fcode=0, $fstr="") {
\r
526 $this->fs=htmlspecialchars($fstr);
\r
533 function faultCode() {
\r
534 if (isset($this->fn))
\r
540 function faultString() { return $this->fs; }
\r
541 function value() { return $this->xv; }
\r
543 function serialize() {
\r
544 $rs="<methodResponse>\n";
\r
550 <name>faultCode</name>
\r
551 <value><int>" . $this->fn . "</int></value>
\r
554 <name>faultString</name>
\r
555 <value><string>" . $this->fs . "</string></value>
\r
561 $rs.="<params>\n<param>\n" . $this->xv->serialize() .
\r
562 "</param>\n</params>";
\r
564 $rs.="\n</methodResponse>";
\r
572 var $params=array();
\r
575 function xmlrpcmsg($meth, $pars=0) {
\r
576 $this->methodname=$meth;
\r
577 if (is_array($pars) && sizeof($pars)>0) {
\r
578 for($i=0; $i<sizeof($pars); $i++)
\r
579 $this->addParam($pars[$i]);
\r
583 function xml_header() {
\r
584 return "<" . "?xml version=\"1.0\"?".">\n<methodCall>\n";
\r
587 function xml_footer() {
\r
588 return "</methodCall>\n";
\r
591 function createPayload() {
\r
592 $this->payload=$this->xml_header();
\r
593 $this->payload.="<methodName>" . $this->methodname . "</methodName>\n";
\r
594 // if (sizeof($this->params)) {
\r
595 $this->payload.="<params>\n";
\r
596 for($i=0; $i<sizeof($this->params); $i++) {
\r
597 $p=$this->params[$i];
\r
598 $this->payload.="<param>\n" . $p->serialize() .
\r
601 $this->payload.="</params>\n";
\r
603 $this->payload.=$this->xml_footer();
\r
604 $this->payload=str_replace("\n", "\r\n", $this->payload);
\r
607 function method($meth="") {
\r
609 $this->methodname=$meth;
\r
611 return $this->methodname;
\r
614 function serialize() {
\r
615 $this->createPayload();
\r
616 return $this->payload;
\r
619 function addParam($par) { $this->params[]=$par; }
\r
620 function getParam($i) { return $this->params[$i]; }
\r
621 function getNumParams() { return sizeof($this->params); }
\r
623 function parseResponseFile($fp) {
\r
626 while($data=fread($fp, 32768)) {
\r
629 return $this->parseResponse($ipd);
\r
632 function parseResponse($data="") {
\r
633 global $_xh,$xmlrpcerr,$xmlrpcstr;
\r
634 global $xmlrpc_defencoding;
\r
637 $parser = xml_parser_create($xmlrpc_defencoding);
\r
639 $_xh[$parser]=array();
\r
641 $_xh[$parser]['st']="";
\r
642 $_xh[$parser]['cm']=0;
\r
643 $_xh[$parser]['isf']=0;
\r
644 $_xh[$parser]['ac']="";
\r
645 $_xh[$parser]['qt']="";
\r
646 $_xh[$parser]['ha']="";
\r
647 $_xh[$parser]['ac']="";
\r
649 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
\r
650 xml_set_element_handler($parser, "xmlrpc_se", "xmlrpc_ee");
\r
651 xml_set_character_data_handler($parser, "xmlrpc_cd");
\r
652 xml_set_default_handler($parser, "xmlrpc_dh");
\r
653 $xmlrpc_value=new xmlrpcval;
\r
656 print "<PRE>---GOT---\n" . htmlspecialchars($data) .
\r
657 "\n---END---\n</PRE>";
\r
659 error_log("No response received from server.");
\r
660 $r=new xmlrpcresp(0, $xmlrpcerr["no_data"],
\r
661 $xmlrpcstr["no_data"]);
\r
662 xml_parser_free($parser);
\r
665 // see if we got an HTTP 200 OK, else bomb
\r
666 // but only do this if we're using the HTTP protocol.
\r
667 if (ereg("^HTTP",$data) &&
\r
668 !ereg("^HTTP/[0-9\.]+ 200 ", $data)) {
\r
669 $errstr= substr($data, 0, strpos($data, "\n")-1);
\r
670 error_log("HTTP error, got response: " .$errstr);
\r
671 $r=new xmlrpcresp(0, $xmlrpcerr["http_error"],
\r
672 $xmlrpcstr["http_error"]. " (" . $errstr . ")");
\r
673 xml_parser_free($parser);
\r
677 // if using HTTP, then gotta get rid of HTTP headers here
\r
678 // and we store them in the 'ha' bit of our data array
\r
679 if (ereg("^HTTP", $data)) {
\r
680 $ar=explode("\r\n", $data);
\r
683 for ($i=0; $i<sizeof($ar); $i++) {
\r
685 if (strlen($ar[$i])>0) {
\r
686 $_xh[$parser]['ha'].=$ar[$i]. "\r\n";
\r
691 $newdata.=$ar[$i] . "\r\n";
\r
697 if (!xml_parse($parser, $data, sizeof($data))) {
\r
698 // thanks to Peter Kocks <peter.kocks@baygate.com>
\r
699 if((xml_get_current_line_number($parser)) == 1)
\r
700 $errstr = "XML error at line 1, check URL";
\r
702 $errstr = sprintf("XML error: %s at line %d",
\r
703 xml_error_string(xml_get_error_code($parser)),
\r
704 xml_get_current_line_number($parser));
\r
705 error_log($errstr);
\r
706 $r=new xmlrpcresp(0, $xmlrpcerr["invalid_return"],
\r
707 $xmlrpcstr["invalid_return"]);
\r
708 xml_parser_free($parser);
\r
711 xml_parser_free($parser);
\r
712 if ($this->debug) {
\r
713 print "<PRE>---EVALING---[" .
\r
714 strlen($_xh[$parser]['st']) . " chars]---\n" .
\r
715 htmlspecialchars($_xh[$parser]['st']) . ";\n---END---</PRE>";
\r
717 if (strlen($_xh[$parser]['st'])==0) {
\r
718 // then something odd has happened
\r
719 // and it's time to generate a client side error
\r
720 // indicating something odd went on
\r
721 $r=new xmlrpcresp(0, $xmlrpcerr["invalid_return"],
\r
722 $xmlrpcstr["invalid_return"]);
\r
724 eval('$v=' . $_xh[$parser]['st'] . '; $allOK=1;');
\r
725 if ($_xh[$parser]['isf']) {
\r
726 $f=$v->structmem("faultCode");
\r
727 $fs=$v->structmem("faultString");
\r
728 $r=new xmlrpcresp($v, $f->scalarval(),
\r
731 $r=new xmlrpcresp($v);
\r
734 $r->hdrs=split("\r?\n", $_xh[$parser]['ha']);
\r
744 function xmlrpcval($val=-1, $type="") {
\r
745 global $xmlrpcTypes;
\r
748 if ($val!=-1 || $type!="") {
\r
749 if ($type=="") $type="string";
\r
750 if ($xmlrpcTypes[$type]==1) {
\r
751 $this->addScalar($val,$type);
\r
753 else if ($xmlrpcTypes[$type]==2)
\r
754 $this->addArray($val);
\r
755 else if ($xmlrpcTypes[$type]==3)
\r
756 $this->addStruct($val);
\r
760 function addScalar($val, $type="string") {
\r
761 global $xmlrpcTypes, $xmlrpcBoolean;
\r
763 if ($this->mytype==1) {
\r
764 echo "<B>xmlrpcval</B>: scalar can have only one value<BR>";
\r
767 $typeof=$xmlrpcTypes[$type];
\r
769 echo "<B>xmlrpcval</B>: not a scalar type (${typeof})<BR>";
\r
773 if ($type==$xmlrpcBoolean) {
\r
774 if (strcasecmp($val,"true")==0 ||
\r
775 $val==1 || ($val==true &&
\r
776 strcasecmp($val,"false"))) {
\r
783 if ($this->mytype==2) {
\r
784 // we're adding to an array here
\r
785 $ar=$this->me["array"];
\r
786 $ar[]=new xmlrpcval($val, $type);
\r
787 $this->me["array"]=$ar;
\r
789 // a scalar, so set the value and remember we're scalar
\r
790 $this->me[$type]=$val;
\r
791 $this->mytype=$typeof;
\r
796 function addArray($vals) {
\r
797 global $xmlrpcTypes;
\r
798 if ($this->mytype!=0) {
\r
799 echo "<B>xmlrpcval</B>: already initialized as a [" .
\r
800 $this->kindOf() . "]<BR>";
\r
804 $this->mytype=$xmlrpcTypes["array"];
\r
805 $this->me["array"]=$vals;
\r
809 function addStruct($vals) {
\r
810 global $xmlrpcTypes;
\r
811 if ($this->mytype!=0) {
\r
812 echo "<B>xmlrpcval</B>: already initialized as a [" .
\r
813 $this->kindOf() . "]<BR>";
\r
816 $this->mytype=$xmlrpcTypes["struct"];
\r
817 $this->me["struct"]=$vals;
\r
821 function dump($ar) {
\r
823 while ( list( $key, $val ) = each( $ar ) ) {
\r
824 echo "$key => $val<br>";
\r
825 if ($key == 'array')
\r
826 while ( list( $key2, $val2 ) = each( $val ) ) {
\r
827 echo "-- $key2 => $val2<br>";
\r
832 function kindOf() {
\r
833 switch($this->mytype) {
\r
848 function serializedata($typ, $val) {
\r
850 global $xmlrpcTypes, $xmlrpcBase64, $xmlrpcString,
\r
852 switch($xmlrpcTypes[$typ]) {
\r
857 while(list($key2, $val2)=each($val)) {
\r
858 $rs.="<member><name>${key2}</name>\n";
\r
859 $rs.=$this->serializeval($val2);
\r
860 $rs.="</member>\n";
\r
866 $rs.="<array>\n<data>\n";
\r
867 for($i=0; $i<sizeof($val); $i++) {
\r
868 $rs.=$this->serializeval($val[$i]);
\r
870 $rs.="</data>\n</array>";
\r
874 case $xmlrpcBase64:
\r
875 $rs.="<${typ}>" . base64_encode($val) . "</${typ}>";
\r
877 case $xmlrpcBoolean:
\r
878 $rs.="<${typ}>" . ($val ? "1" : "0") . "</${typ}>";
\r
880 case $xmlrpcString:
\r
881 $rs.="<${typ}>" . htmlspecialchars($val). "</${typ}>";
\r
884 $rs.="<${typ}>${val}</${typ}>";
\r
893 function serialize() {
\r
894 return $this->serializeval($this);
\r
897 function serializeval($o) {
\r
898 global $xmlrpcTypes;
\r
902 list($typ, $val) = each($ar);
\r
904 $rs.=$this->serializedata($typ, $val);
\r
909 function structmem($m) {
\r
910 $nv=$this->me["struct"][$m];
\r
914 function structreset() {
\r
915 reset($this->me["struct"]);
\r
918 function structeach() {
\r
919 return each($this->me["struct"]);
\r
922 function getval() {
\r
924 global $xmlrpcBoolean, $xmlrpcBase64;
\r
926 list($a,$b)=each($this->me);
\r
927 // contributed by I Sofer, 2001-03-24
\r
928 // add support for nested arrays to scalarval
\r
929 // i've created a new method here, so as to
\r
930 // preserve back compatibility
\r
932 if (is_array($b)) {
\r
933 foreach ($b as $id => $cont) {
\r
934 $b[$id] = $cont->scalarval();
\r
938 // add support for structures directly encoding php objects
\r
939 if (is_object($b)) {
\r
940 $t = get_object_vars($b);
\r
941 foreach ($t as $id => $cont) {
\r
942 $t[$id] = $cont->scalarval();
\r
944 foreach ($t as $id => $cont) {
\r
945 eval('$b->'.$id.' = $cont;');
\r
952 function scalarval() {
\r
953 global $xmlrpcBoolean, $xmlrpcBase64;
\r
955 list($a,$b)=each($this->me);
\r
959 function scalartyp() {
\r
960 global $xmlrpcI4, $xmlrpcInt;
\r
962 list($a,$b)=each($this->me);
\r
963 if ($a==$xmlrpcI4)
\r
968 function arraymem($m) {
\r
969 $nv=$this->me["array"][$m];
\r
973 function arraysize() {
\r
975 list($a,$b)=each($this->me);
\r
981 function iso8601_encode($timet, $utc=0) {
\r
982 // return an ISO8601 encoded string
\r
983 // really, timezones ought to be supported
\r
984 // but the XML-RPC spec says:
\r
986 // "Don't assume a timezone. It should be specified by the server in its
\r
987 // documentation what assumptions it makes about timezones."
\r
989 // these routines always assume localtime unless
\r
990 // $utc is set to 1, in which case UTC is assumed
\r
991 // and an adjustment for locale is made when encoding
\r
993 $t=strftime("%Y%m%dT%H:%M:%S", $timet);
\r
995 if (function_exists("gmstrftime"))
\r
996 // gmstrftime doesn't exist in some versions
\r
998 $t=gmstrftime("%Y%m%dT%H:%M:%S", $timet);
\r
1000 $t=strftime("%Y%m%dT%H:%M:%S", $timet-date("Z"));
\r
1006 function iso8601_decode($idate, $utc=0) {
\r
1007 // return a timet in the localtime, or UTC
\r
1009 if (ereg("([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})",
\r
1012 $t=gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
\r
1014 $t=mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
\r
1020 /****************************************************************
\r
1021 * xmlrpc_decode takes a message in PHP xmlrpc object format and *
\r
1022 * tranlates it into native PHP types. *
\r
1024 * author: Dan Libby (dan@libby.com) *
\r
1025 ****************************************************************/
\r
1026 function _xmlrpc_decode($xmlrpc_val) {
\r
1027 $kind = $xmlrpc_val->kindOf();
\r
1029 if($kind == "scalar") {
\r
1030 return $xmlrpc_val->scalarval();
\r
1032 else if($kind == "array") {
\r
1033 $size = $xmlrpc_val->arraysize();
\r
1036 for($i = 0; $i < $size; $i++) {
\r
1037 $arr[]=_xmlrpc_decode($xmlrpc_val->arraymem($i));
\r
1041 else if($kind == "struct") {
\r
1042 $xmlrpc_val->structreset();
\r
1045 while(list($key,$value)=$xmlrpc_val->structeach()) {
\r
1046 $arr[$key] = _xmlrpc_decode($value);
\r
1052 /****************************************************************
\r
1053 * xmlrpc_encode takes native php types and encodes them into *
\r
1054 * xmlrpc PHP object format. *
\r
1055 * BUG: All sequential arrays are turned into structs. I don't *
\r
1056 * know of a good way to determine if an array is sequential *
\r
1059 * feature creep -- could support more types via optional type *
\r
1062 * author: Dan Libby (dan@libby.com) *
\r
1063 ****************************************************************/
\r
1064 function _xmlrpc_encode($php_val) {
\r
1065 global $xmlrpcInt;
\r
1066 global $xmlrpcDouble;
\r
1067 global $xmlrpcString;
\r
1068 global $xmlrpcArray;
\r
1069 global $xmlrpcStruct;
\r
1070 global $xmlrpcBoolean;
\r
1072 $type = gettype($php_val);
\r
1073 $xmlrpc_val = new xmlrpcval;
\r
1079 while (list($k,$v) = each($php_val)) {
\r
1080 $arr[$k] = _xmlrpc_encode($v);
\r
1082 $xmlrpc_val->addStruct($arr);
\r
1085 $xmlrpc_val->addScalar($php_val, $xmlrpcInt);
\r
1088 $xmlrpc_val->addScalar($php_val, $xmlrpcDouble);
\r
1091 $xmlrpc_val->addScalar($php_val, $xmlrpcString);
\r
1093 // <G_Giunta_2001-02-29>
\r
1094 // Add support for encoding/decoding of booleans, since they are supported in PHP
\r
1096 $xmlrpc_val->addScalar($php_val, $xmlrpcBoolean);
\r
1098 // </G_Giunta_2001-02-29>
\r
1099 case "unknown type":
\r
1101 // giancarlo pinerolo <ping@alt.it>
\r
1102 // it has to return
\r
1103 // an empty object in case (which is already
\r
1104 // at this point), not a boolean.
\r
1107 return $xmlrpc_val;
\r