@@ -5,45 +5,45 @@ |
||
5 | 5 | <h2>Send a U.S. state number to the server and get back the state name</h2> |
6 | 6 | <h3>The code demonstrates usage of the php_xmlrpc_encode function</h3> |
7 | 7 | <?php |
8 | - include("xmlrpc.inc"); |
|
8 | + include("xmlrpc.inc"); |
|
9 | 9 | |
10 | - // Play nice to PHP 5 installations with REGISTER_LONG_ARRAYS off |
|
11 | - if(!isset($HTTP_POST_VARS) && isset($_POST)) |
|
12 | - { |
|
13 | - $HTTP_POST_VARS = $_POST; |
|
14 | - } |
|
10 | + // Play nice to PHP 5 installations with REGISTER_LONG_ARRAYS off |
|
11 | + if(!isset($HTTP_POST_VARS) && isset($_POST)) |
|
12 | + { |
|
13 | + $HTTP_POST_VARS = $_POST; |
|
14 | + } |
|
15 | 15 | |
16 | - if(isset($HTTP_POST_VARS["stateno"]) && $HTTP_POST_VARS["stateno"]!="") |
|
17 | - { |
|
18 | - $stateno=(integer)$HTTP_POST_VARS["stateno"]; |
|
19 | - $f=new xmlrpcmsg('examples.getStateName', |
|
20 | - array(php_xmlrpc_encode($stateno)) |
|
21 | - ); |
|
22 | - print "<pre>Sending the following request:\n\n" . htmlentities($f->serialize()) . "\n\nDebug info of server data follows...\n\n"; |
|
23 | - $c=new xmlrpc_client("/server.php", "phpxmlrpc.sourceforge.net", 80); |
|
24 | - $c->setDebug(1); |
|
25 | - $r=&$c->send($f); |
|
26 | - if(!$r->faultCode()) |
|
27 | - { |
|
28 | - $v=$r->value(); |
|
29 | - print "</pre><br/>State number " . $stateno . " is " |
|
30 | - . htmlspecialchars($v->scalarval()) . "<br/>"; |
|
31 | - // print "<HR>I got this value back<BR><PRE>" . |
|
32 | - // htmlentities($r->serialize()). "</PRE><HR>\n"; |
|
33 | - } |
|
34 | - else |
|
35 | - { |
|
36 | - print "An error occurred: "; |
|
37 | - print "Code: " . htmlspecialchars($r->faultCode()) |
|
38 | - . " Reason: '" . htmlspecialchars($r->faultString()) . "'</pre><br/>"; |
|
39 | - } |
|
40 | - } |
|
41 | - else |
|
42 | - { |
|
43 | - $stateno = ""; |
|
44 | - } |
|
16 | + if(isset($HTTP_POST_VARS["stateno"]) && $HTTP_POST_VARS["stateno"]!="") |
|
17 | + { |
|
18 | + $stateno=(integer)$HTTP_POST_VARS["stateno"]; |
|
19 | + $f=new xmlrpcmsg('examples.getStateName', |
|
20 | + array(php_xmlrpc_encode($stateno)) |
|
21 | + ); |
|
22 | + print "<pre>Sending the following request:\n\n" . htmlentities($f->serialize()) . "\n\nDebug info of server data follows...\n\n"; |
|
23 | + $c=new xmlrpc_client("/server.php", "phpxmlrpc.sourceforge.net", 80); |
|
24 | + $c->setDebug(1); |
|
25 | + $r=&$c->send($f); |
|
26 | + if(!$r->faultCode()) |
|
27 | + { |
|
28 | + $v=$r->value(); |
|
29 | + print "</pre><br/>State number " . $stateno . " is " |
|
30 | + . htmlspecialchars($v->scalarval()) . "<br/>"; |
|
31 | + // print "<HR>I got this value back<BR><PRE>" . |
|
32 | + // htmlentities($r->serialize()). "</PRE><HR>\n"; |
|
33 | + } |
|
34 | + else |
|
35 | + { |
|
36 | + print "An error occurred: "; |
|
37 | + print "Code: " . htmlspecialchars($r->faultCode()) |
|
38 | + . " Reason: '" . htmlspecialchars($r->faultString()) . "'</pre><br/>"; |
|
39 | + } |
|
40 | + } |
|
41 | + else |
|
42 | + { |
|
43 | + $stateno = ""; |
|
44 | + } |
|
45 | 45 | |
46 | - print "<form action=\"client.php\" method=\"POST\"> |
|
46 | + print "<form action=\"client.php\" method=\"POST\"> |
|
47 | 47 | <input name=\"stateno\" value=\"" . $stateno . "\"><input type=\"submit\" value=\"go\" name=\"submit\"></form> |
48 | 48 | <p>Enter a state number to query its name</p>"; |
49 | 49 |
@@ -6,53 +6,53 @@ |
||
6 | 6 | * @license code licensed under the BSD License: http://phpxmlrpc.sourceforge.net/license.txt |
7 | 7 | */ |
8 | 8 | |
9 | - /** |
|
10 | - * Takes a client object, a remote method name, and a variable numbers of |
|
11 | - * php values, and calls the method with the supplied parameters. The |
|
12 | - * parameters are native php values and the result is an xmlrpcresp object. |
|
13 | - * |
|
14 | - * Notes: |
|
15 | - * The function encodes the received parameters using php_xmlrpc_encode: |
|
16 | - * the limitations of automatic encoding apply to this function too); |
|
17 | - * |
|
18 | - * the type of the value returned by the function can be changed setting |
|
19 | - * beforehand the 'return_type' member of the client object to 'phpvals' - |
|
20 | - * see the manual for more details about this capability). |
|
21 | - * |
|
22 | - * |
|
23 | - * @author Toth Istvan |
|
24 | - * |
|
25 | - * @param xmlrpc_client client object, properly set up to connect to server |
|
26 | - * @param string remote function name |
|
27 | - * @param mixed $parameter1 |
|
28 | - * @param mixed $parameter2 |
|
29 | - * @param mixed $parameter3 ... |
|
30 | - * @return xmlrpcresp or false on error |
|
31 | - */ |
|
32 | - function xmlrpccall_simple() |
|
33 | - { |
|
34 | - if(func_num_args() < 2) |
|
35 | - { |
|
36 | - // Incorrect |
|
37 | - return false; |
|
38 | - } |
|
39 | - else |
|
40 | - { |
|
41 | - $varargs = func_get_args(); |
|
42 | - $client = array_shift($varargs); |
|
43 | - $remote_function_name = array_shift($varargs); |
|
44 | - if (!is_a($client, 'xmlrpc_client') || !is_string($remote_function_name)) |
|
45 | - { |
|
46 | - return false; |
|
47 | - } |
|
9 | + /** |
|
10 | + * Takes a client object, a remote method name, and a variable numbers of |
|
11 | + * php values, and calls the method with the supplied parameters. The |
|
12 | + * parameters are native php values and the result is an xmlrpcresp object. |
|
13 | + * |
|
14 | + * Notes: |
|
15 | + * The function encodes the received parameters using php_xmlrpc_encode: |
|
16 | + * the limitations of automatic encoding apply to this function too); |
|
17 | + * |
|
18 | + * the type of the value returned by the function can be changed setting |
|
19 | + * beforehand the 'return_type' member of the client object to 'phpvals' - |
|
20 | + * see the manual for more details about this capability). |
|
21 | + * |
|
22 | + * |
|
23 | + * @author Toth Istvan |
|
24 | + * |
|
25 | + * @param xmlrpc_client client object, properly set up to connect to server |
|
26 | + * @param string remote function name |
|
27 | + * @param mixed $parameter1 |
|
28 | + * @param mixed $parameter2 |
|
29 | + * @param mixed $parameter3 ... |
|
30 | + * @return xmlrpcresp or false on error |
|
31 | + */ |
|
32 | + function xmlrpccall_simple() |
|
33 | + { |
|
34 | + if(func_num_args() < 2) |
|
35 | + { |
|
36 | + // Incorrect |
|
37 | + return false; |
|
38 | + } |
|
39 | + else |
|
40 | + { |
|
41 | + $varargs = func_get_args(); |
|
42 | + $client = array_shift($varargs); |
|
43 | + $remote_function_name = array_shift($varargs); |
|
44 | + if (!is_a($client, 'xmlrpc_client') || !is_string($remote_function_name)) |
|
45 | + { |
|
46 | + return false; |
|
47 | + } |
|
48 | 48 | |
49 | - $xmlrpcval_array = array(); |
|
50 | - foreach($varargs as $parameter) |
|
51 | - { |
|
52 | - $xmlrpcval_array[] = php_xmlrpc_encode($parameter); |
|
53 | - } |
|
49 | + $xmlrpcval_array = array(); |
|
50 | + foreach($varargs as $parameter) |
|
51 | + { |
|
52 | + $xmlrpcval_array[] = php_xmlrpc_encode($parameter); |
|
53 | + } |
|
54 | 54 | |
55 | - return $client->send(new xmlrpcmsg($remote_function_name, $xmlrpcval_array)); |
|
56 | - } |
|
57 | - } |
|
55 | + return $client->send(new xmlrpcmsg($remote_function_name, $xmlrpcval_array)); |
|
56 | + } |
|
57 | + } |
|
58 | 58 | ?> |
@@ -1,8 +1,8 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | // Allow users to see the source of this file even if PHP is not configured for it |
3 | 3 | if ((isset($HTTP_GET_VARS['showSource']) && $HTTP_GET_VARS['showSource']) || |
4 | - (isset($_GET['showSource']) && $_GET['showSource'])) |
|
5 | - { highlight_file(__FILE__); die(); } |
|
4 | + (isset($_GET['showSource']) && $_GET['showSource'])) |
|
5 | + { highlight_file(__FILE__); die(); } |
|
6 | 6 | ?> |
7 | 7 | <html> |
8 | 8 | <head><title>xmlrpc</title></head> |
@@ -18,36 +18,36 @@ discard block |
||
18 | 18 | |
19 | 19 | // Play nice to PHP 5 installations with REGISTER_LONG_ARRAYS off |
20 | 20 | if (!isset($HTTP_POST_VARS) && isset($_POST)) |
21 | - $HTTP_POST_VARS = $_POST; |
|
21 | + $HTTP_POST_VARS = $_POST; |
|
22 | 22 | |
23 | 23 | if (isset($HTTP_POST_VARS["server"]) && $HTTP_POST_VARS["server"]) { |
24 | - if ($HTTP_POST_VARS["server"]=="Userland") { |
|
25 | - $XP="/RPC2"; $XS="206.204.24.2"; |
|
26 | - } else { |
|
27 | - $XP="/xmlrpc/server.php"; $XS="pingu.heddley.com"; |
|
28 | - } |
|
29 | - $f=new xmlrpcmsg('mail.send'); |
|
30 | - $f->addParam(new xmlrpcval($HTTP_POST_VARS["mailto"])); |
|
31 | - $f->addParam(new xmlrpcval($HTTP_POST_VARS["mailsub"])); |
|
32 | - $f->addParam(new xmlrpcval($HTTP_POST_VARS["mailmsg"])); |
|
33 | - $f->addParam(new xmlrpcval($HTTP_POST_VARS["mailfrom"])); |
|
34 | - $f->addParam(new xmlrpcval($HTTP_POST_VARS["mailcc"])); |
|
35 | - $f->addParam(new xmlrpcval($HTTP_POST_VARS["mailbcc"])); |
|
36 | - $f->addParam(new xmlrpcval("text/plain")); |
|
24 | + if ($HTTP_POST_VARS["server"]=="Userland") { |
|
25 | + $XP="/RPC2"; $XS="206.204.24.2"; |
|
26 | + } else { |
|
27 | + $XP="/xmlrpc/server.php"; $XS="pingu.heddley.com"; |
|
28 | + } |
|
29 | + $f=new xmlrpcmsg('mail.send'); |
|
30 | + $f->addParam(new xmlrpcval($HTTP_POST_VARS["mailto"])); |
|
31 | + $f->addParam(new xmlrpcval($HTTP_POST_VARS["mailsub"])); |
|
32 | + $f->addParam(new xmlrpcval($HTTP_POST_VARS["mailmsg"])); |
|
33 | + $f->addParam(new xmlrpcval($HTTP_POST_VARS["mailfrom"])); |
|
34 | + $f->addParam(new xmlrpcval($HTTP_POST_VARS["mailcc"])); |
|
35 | + $f->addParam(new xmlrpcval($HTTP_POST_VARS["mailbcc"])); |
|
36 | + $f->addParam(new xmlrpcval("text/plain")); |
|
37 | 37 | |
38 | - $c=new xmlrpc_client($XP, $XS, 80); |
|
39 | - $c->setDebug(2); |
|
40 | - $r=&$c->send($f); |
|
41 | - if (!$r->faultCode()) { |
|
42 | - print "Mail sent OK<br/>\n"; |
|
43 | - } else { |
|
44 | - print "<fonr color=\"red\">"; |
|
45 | - print "Mail send failed<br/>\n"; |
|
46 | - print "Fault: "; |
|
47 | - print "Code: " . htmlspecialchars($r->faultCode()) . |
|
48 | - " Reason: '" . htmlspecialchars($r->faultString()) . "'<br/>"; |
|
49 | - print "</font><br/>"; |
|
50 | - } |
|
38 | + $c=new xmlrpc_client($XP, $XS, 80); |
|
39 | + $c->setDebug(2); |
|
40 | + $r=&$c->send($f); |
|
41 | + if (!$r->faultCode()) { |
|
42 | + print "Mail sent OK<br/>\n"; |
|
43 | + } else { |
|
44 | + print "<fonr color=\"red\">"; |
|
45 | + print "Mail send failed<br/>\n"; |
|
46 | + print "Fault: "; |
|
47 | + print "Code: " . htmlspecialchars($r->faultCode()) . |
|
48 | + " Reason: '" . htmlspecialchars($r->faultString()) . "'<br/>"; |
|
49 | + print "</font><br/>"; |
|
50 | + } |
|
51 | 51 | } |
52 | 52 | ?> |
53 | 53 | <form method="POST"> |
@@ -4,26 +4,26 @@ |
||
4 | 4 | <h1>Zope test demo</h1> |
5 | 5 | <h3>The code demonstrates usage of basic authentication to connect to the server</h3> |
6 | 6 | <?php |
7 | - include("xmlrpc.inc"); |
|
7 | + include("xmlrpc.inc"); |
|
8 | 8 | |
9 | - $f = new xmlrpcmsg('document_src', array()); |
|
10 | - $c = new xmlrpc_client("/index_html", "pingu.heddley.com", 9080); |
|
11 | - $c->setCredentials("username", "password"); |
|
12 | - $c->setDebug(2); |
|
13 | - $r = $c->send($f); |
|
14 | - if(!$r->faultCode()) |
|
15 | - { |
|
16 | - $v = $r->value(); |
|
17 | - print "I received:" . htmlspecialchars($v->scalarval()) . "<br/>"; |
|
18 | - print "<hr/>I got this value back<br/>pre>" . |
|
19 | - htmlentities($r->serialize()). "</pre>\n"; |
|
20 | - } |
|
21 | - else |
|
22 | - { |
|
23 | - print "An error occurred: "; |
|
24 | - print "Code: " . htmlspecialchars($r->faultCode()) |
|
25 | - . " Reason: '" . ($r->faultString()) . "'<br/>"; |
|
26 | - } |
|
9 | + $f = new xmlrpcmsg('document_src', array()); |
|
10 | + $c = new xmlrpc_client("/index_html", "pingu.heddley.com", 9080); |
|
11 | + $c->setCredentials("username", "password"); |
|
12 | + $c->setDebug(2); |
|
13 | + $r = $c->send($f); |
|
14 | + if(!$r->faultCode()) |
|
15 | + { |
|
16 | + $v = $r->value(); |
|
17 | + print "I received:" . htmlspecialchars($v->scalarval()) . "<br/>"; |
|
18 | + print "<hr/>I got this value back<br/>pre>" . |
|
19 | + htmlentities($r->serialize()). "</pre>\n"; |
|
20 | + } |
|
21 | + else |
|
22 | + { |
|
23 | + print "An error occurred: "; |
|
24 | + print "Code: " . htmlspecialchars($r->faultCode()) |
|
25 | + . " Reason: '" . ($r->faultString()) . "'<br/>"; |
|
26 | + } |
|
27 | 27 | ?> |
28 | 28 | </body> |
29 | 29 | </html> |
@@ -8,29 +8,29 @@ discard block |
||
8 | 8 | |
9 | 9 | function highlight($file) |
10 | 10 | { |
11 | - $starttag = '<pre class="programlisting">'; |
|
12 | - $endtag = '</pre>'; |
|
11 | + $starttag = '<pre class="programlisting">'; |
|
12 | + $endtag = '</pre>'; |
|
13 | 13 | |
14 | - $content = file_get_contents($file); |
|
15 | - $last = 0; |
|
16 | - $out = ''; |
|
17 | - while(($start = strpos($content, $starttag, $last)) !== false) |
|
18 | - { |
|
14 | + $content = file_get_contents($file); |
|
15 | + $last = 0; |
|
16 | + $out = ''; |
|
17 | + while(($start = strpos($content, $starttag, $last)) !== false) |
|
18 | + { |
|
19 | 19 | $end = strpos($content, $endtag, $start); |
20 | - $code = substr($content, $start+strlen($starttag), $end-$start-strlen($starttag)); |
|
21 | - if ($code[strlen($code)-1] == "\n") { |
|
22 | - $code = substr($code, 0, -1); |
|
23 | - } |
|
20 | + $code = substr($content, $start+strlen($starttag), $end-$start-strlen($starttag)); |
|
21 | + if ($code[strlen($code)-1] == "\n") { |
|
22 | + $code = substr($code, 0, -1); |
|
23 | + } |
|
24 | 24 | //var_dump($code); |
25 | - $code = str_replace(array('>', '<'), array('>', '<'), $code); |
|
25 | + $code = str_replace(array('>', '<'), array('>', '<'), $code); |
|
26 | 26 | $code = highlight_string('<?php '.$code, true); |
27 | 27 | $code = str_replace('<span style="color: #0000BB"><?php <br />', '<span style="color: #0000BB">', $code); |
28 | 28 | //echo($code); |
29 | 29 | $out = $out . substr($content, $last, $start+strlen($starttag)-$last) . $code . $endtag; |
30 | 30 | $last = $end+strlen($endtag); |
31 | - } |
|
32 | - $out .= substr($content, $last, strlen($content)); |
|
33 | - return $out; |
|
31 | + } |
|
32 | + $out .= substr($content, $last, strlen($content)); |
|
33 | + return $out; |
|
34 | 34 | } |
35 | 35 | |
36 | 36 | $dir = $argv[1]; |
@@ -38,11 +38,11 @@ discard block |
||
38 | 38 | $files = scandir($dir); |
39 | 39 | foreach($files as $file) |
40 | 40 | { |
41 | - if (substr($file, -5, 5) == '.html') |
|
42 | - { |
|
43 | - $out = highlight($dir.'/'.$file); |
|
44 | - file_put_contents($dir.'/'.$file, $out); |
|
45 | - } |
|
41 | + if (substr($file, -5, 5) == '.html') |
|
42 | + { |
|
43 | + $out = highlight($dir.'/'.$file); |
|
44 | + file_put_contents($dir.'/'.$file, $out); |
|
45 | + } |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | ?> |
49 | 49 | \ No newline at end of file |
@@ -14,921 +14,921 @@ |
||
14 | 14 | * @todo implement self-parsing of php code for PHP <= 4 |
15 | 15 | */ |
16 | 16 | |
17 | - // requires: xmlrpc.inc |
|
18 | - |
|
19 | - /** |
|
20 | - * Given a string defining a php type or phpxmlrpc type (loosely defined: strings |
|
21 | - * accepted come from javadoc blocks), return corresponding phpxmlrpc type. |
|
22 | - * NB: for php 'resource' types returns empty string, since resources cannot be serialized; |
|
23 | - * for php class names returns 'struct', since php objects can be serialized as xmlrpc structs |
|
24 | - * for php arrays always return array, even though arrays sometiles serialize as json structs |
|
25 | - * @param string $phptype |
|
26 | - * @return string |
|
27 | - */ |
|
28 | - function php_2_xmlrpc_type($phptype) |
|
29 | - { |
|
30 | - switch(strtolower($phptype)) |
|
31 | - { |
|
32 | - case 'string': |
|
33 | - return $GLOBALS['xmlrpcString']; |
|
34 | - case 'integer': |
|
35 | - case $GLOBALS['xmlrpcInt']: // 'int' |
|
36 | - case $GLOBALS['xmlrpcI4']: |
|
37 | - return $GLOBALS['xmlrpcInt']; |
|
38 | - case 'double': |
|
39 | - return $GLOBALS['xmlrpcDouble']; |
|
40 | - case 'boolean': |
|
41 | - return $GLOBALS['xmlrpcBoolean']; |
|
42 | - case 'array': |
|
43 | - return $GLOBALS['xmlrpcArray']; |
|
44 | - case 'object': |
|
45 | - return $GLOBALS['xmlrpcStruct']; |
|
46 | - case $GLOBALS['xmlrpcBase64']: |
|
47 | - case $GLOBALS['xmlrpcStruct']: |
|
48 | - return strtolower($phptype); |
|
49 | - case 'resource': |
|
50 | - return ''; |
|
51 | - default: |
|
52 | - if(class_exists($phptype)) |
|
53 | - { |
|
54 | - return $GLOBALS['xmlrpcStruct']; |
|
55 | - } |
|
56 | - else |
|
57 | - { |
|
58 | - // unknown: might be any 'extended' xmlrpc type |
|
59 | - return $GLOBALS['xmlrpcValue']; |
|
60 | - } |
|
61 | - } |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Given a string defining a phpxmlrpc type return corresponding php type. |
|
66 | - * @param string $xmlrpctype |
|
67 | - * @return string |
|
68 | - */ |
|
69 | - function xmlrpc_2_php_type($xmlrpctype) |
|
70 | - { |
|
71 | - switch(strtolower($xmlrpctype)) |
|
72 | - { |
|
73 | - case 'base64': |
|
74 | - case 'datetime.iso8601': |
|
75 | - case 'string': |
|
76 | - return $GLOBALS['xmlrpcString']; |
|
77 | - case 'int': |
|
78 | - case 'i4': |
|
79 | - return 'integer'; |
|
80 | - case 'struct': |
|
81 | - case 'array': |
|
82 | - return 'array'; |
|
83 | - case 'double': |
|
84 | - return 'float'; |
|
85 | - case 'undefined': |
|
86 | - return 'mixed'; |
|
87 | - case 'boolean': |
|
88 | - case 'null': |
|
89 | - default: |
|
90 | - // unknown: might be any xmlrpc type |
|
91 | - return strtolower($xmlrpctype); |
|
92 | - } |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Given a user-defined PHP function, create a PHP 'wrapper' function that can |
|
97 | - * be exposed as xmlrpc method from an xmlrpc_server object and called from remote |
|
98 | - * clients (as well as its corresponding signature info). |
|
99 | - * |
|
100 | - * Since php is a typeless language, to infer types of input and output parameters, |
|
101 | - * it relies on parsing the javadoc-style comment block associated with the given |
|
102 | - * function. Usage of xmlrpc native types (such as datetime.dateTime.iso8601 and base64) |
|
103 | - * in the @param tag is also allowed, if you need the php function to receive/send |
|
104 | - * data in that particular format (note that base64 encoding/decoding is transparently |
|
105 | - * carried out by the lib, while datetime vals are passed around as strings) |
|
106 | - * |
|
107 | - * Known limitations: |
|
108 | - * - only works for user-defined functions, not for PHP internal functions |
|
109 | - * (reflection does not support retrieving number/type of params for those) |
|
110 | - * - functions returning php objects will generate special xmlrpc responses: |
|
111 | - * when the xmlrpc decoding of those responses is carried out by this same lib, using |
|
112 | - * the appropriate param in php_xmlrpc_decode, the php objects will be rebuilt. |
|
113 | - * In short: php objects can be serialized, too (except for their resource members), |
|
114 | - * using this function. |
|
115 | - * Other libs might choke on the very same xml that will be generated in this case |
|
116 | - * (i.e. it has a nonstandard attribute on struct element tags) |
|
117 | - * - usage of javadoc @param tags using param names in a different order from the |
|
118 | - * function prototype is not considered valid (to be fixed?) |
|
119 | - * |
|
120 | - * Note that since rel. 2.0RC3 the preferred method to have the server call 'standard' |
|
121 | - * php functions (ie. functions not expecting a single xmlrpcmsg obj as parameter) |
|
122 | - * is by making use of the functions_parameters_type class member. |
|
123 | - * |
|
124 | - * @param string $funcname the name of the PHP user function to be exposed as xmlrpc method; array($obj, 'methodname') and array('class', 'methodname') are ok too |
|
125 | - * @param string $newfuncname (optional) name for function to be created |
|
126 | - * @param array $extra_options (optional) array of options for conversion. valid values include: |
|
127 | - * bool return_source when true, php code w. function definition will be returned, not evaluated |
|
128 | - * bool encode_php_objs let php objects be sent to server using the 'improved' xmlrpc notation, so server can deserialize them as php objects |
|
129 | - * bool decode_php_objs --- WARNING !!! possible security hazard. only use it with trusted servers --- |
|
130 | - * bool suppress_warnings remove from produced xml any runtime warnings due to the php function being invoked |
|
131 | - * @return false on error, or an array containing the name of the new php function, |
|
132 | - * its signature and docs, to be used in the server dispatch map |
|
133 | - * |
|
134 | - * @todo decide how to deal with params passed by ref: bomb out or allow? |
|
135 | - * @todo finish using javadoc info to build method sig if all params are named but out of order |
|
136 | - * @todo add a check for params of 'resource' type |
|
137 | - * @todo add some trigger_errors / error_log when returning false? |
|
138 | - * @todo what to do when the PHP function returns NULL? we are currently returning an empty string value... |
|
139 | - * @todo add an option to suppress php warnings in invocation of user function, similar to server debug level 3? |
|
140 | - * @todo if $newfuncname is empty, we could use create_user_func instead of eval, as it is possibly faster |
|
141 | - * @todo add a verbatim_object_copy parameter to allow avoiding the same obj instance? |
|
142 | - */ |
|
143 | - function wrap_php_function($funcname, $newfuncname='', $extra_options=array()) |
|
144 | - { |
|
145 | - $buildit = isset($extra_options['return_source']) ? !($extra_options['return_source']) : true; |
|
146 | - $prefix = isset($extra_options['prefix']) ? $extra_options['prefix'] : 'xmlrpc'; |
|
147 | - $encode_php_objects = isset($extra_options['encode_php_objs']) ? (bool)$extra_options['encode_php_objs'] : false; |
|
148 | - $decode_php_objects = isset($extra_options['decode_php_objs']) ? (bool)$extra_options['decode_php_objs'] : false; |
|
149 | - $catch_warnings = isset($extra_options['suppress_warnings']) && $extra_options['suppress_warnings'] ? '@' : ''; |
|
150 | - |
|
151 | - $exists = false; |
|
152 | - if (is_string($funcname) && strpos($funcname, '::') !== false) |
|
153 | - { |
|
154 | - $funcname = explode('::', $funcname); |
|
155 | - } |
|
156 | - if(is_array($funcname)) |
|
157 | - { |
|
158 | - if(count($funcname) < 2 || (!is_string($funcname[0]) && !is_object($funcname[0]))) |
|
159 | - { |
|
160 | - error_log('XML-RPC: syntax for function to be wrapped is wrong'); |
|
161 | - return false; |
|
162 | - } |
|
163 | - if(is_string($funcname[0])) |
|
164 | - { |
|
165 | - $plainfuncname = implode('::', $funcname); |
|
166 | - } |
|
167 | - elseif(is_object($funcname[0])) |
|
168 | - { |
|
169 | - $plainfuncname = get_class($funcname[0]) . '->' . $funcname[1]; |
|
170 | - } |
|
171 | - $exists = method_exists($funcname[0], $funcname[1]); |
|
172 | - } |
|
173 | - else |
|
174 | - { |
|
175 | - $plainfuncname = $funcname; |
|
176 | - $exists = function_exists($funcname); |
|
177 | - } |
|
178 | - |
|
179 | - if(!$exists) |
|
180 | - { |
|
181 | - error_log('XML-RPC: function to be wrapped is not defined: '.$plainfuncname); |
|
182 | - return false; |
|
183 | - } |
|
184 | - else |
|
185 | - { |
|
186 | - // determine name of new php function |
|
187 | - if($newfuncname == '') |
|
188 | - { |
|
189 | - if(is_array($funcname)) |
|
190 | - { |
|
191 | - if(is_string($funcname[0])) |
|
192 | - $xmlrpcfuncname = "{$prefix}_".implode('_', $funcname); |
|
193 | - else |
|
194 | - $xmlrpcfuncname = "{$prefix}_".get_class($funcname[0]) . '_' . $funcname[1]; |
|
195 | - } |
|
196 | - else |
|
197 | - { |
|
198 | - $xmlrpcfuncname = "{$prefix}_$funcname"; |
|
199 | - } |
|
200 | - } |
|
201 | - else |
|
202 | - { |
|
203 | - $xmlrpcfuncname = $newfuncname; |
|
204 | - } |
|
205 | - while($buildit && function_exists($xmlrpcfuncname)) |
|
206 | - { |
|
207 | - $xmlrpcfuncname .= 'x'; |
|
208 | - } |
|
209 | - |
|
210 | - // start to introspect PHP code |
|
211 | - if(is_array($funcname)) |
|
212 | - { |
|
213 | - $func = new ReflectionMethod($funcname[0], $funcname[1]); |
|
214 | - if($func->isPrivate()) |
|
215 | - { |
|
216 | - error_log('XML-RPC: method to be wrapped is private: '.$plainfuncname); |
|
217 | - return false; |
|
218 | - } |
|
219 | - if($func->isProtected()) |
|
220 | - { |
|
221 | - error_log('XML-RPC: method to be wrapped is protected: '.$plainfuncname); |
|
222 | - return false; |
|
223 | - } |
|
224 | - if($func->isConstructor()) |
|
225 | - { |
|
226 | - error_log('XML-RPC: method to be wrapped is the constructor: '.$plainfuncname); |
|
227 | - return false; |
|
228 | - } |
|
229 | - if($func->isDestructor()) |
|
230 | - { |
|
231 | - error_log('XML-RPC: method to be wrapped is the destructor: '.$plainfuncname); |
|
232 | - return false; |
|
233 | - } |
|
234 | - if($func->isAbstract()) |
|
235 | - { |
|
236 | - error_log('XML-RPC: method to be wrapped is abstract: '.$plainfuncname); |
|
237 | - return false; |
|
238 | - } |
|
239 | - /// @todo add more checks for static vs. nonstatic? |
|
240 | - } |
|
241 | - else |
|
242 | - { |
|
243 | - $func = new ReflectionFunction($funcname); |
|
244 | - } |
|
245 | - if($func->isInternal()) |
|
246 | - { |
|
247 | - // Note: from PHP 5.1.0 onward, we will possibly be able to use invokeargs |
|
248 | - // instead of getparameters to fully reflect internal php functions ? |
|
249 | - error_log('XML-RPC: function to be wrapped is internal: '.$plainfuncname); |
|
250 | - return false; |
|
251 | - } |
|
252 | - |
|
253 | - // retrieve parameter names, types and description from javadoc comments |
|
254 | - |
|
255 | - // function description |
|
256 | - $desc = ''; |
|
257 | - // type of return val: by default 'any' |
|
258 | - $returns = $GLOBALS['xmlrpcValue']; |
|
259 | - // desc of return val |
|
260 | - $returnsDocs = ''; |
|
261 | - // type + name of function parameters |
|
262 | - $paramDocs = array(); |
|
263 | - |
|
264 | - $docs = $func->getDocComment(); |
|
265 | - if($docs != '') |
|
266 | - { |
|
267 | - $docs = explode("\n", $docs); |
|
268 | - $i = 0; |
|
269 | - foreach($docs as $doc) |
|
270 | - { |
|
271 | - $doc = trim($doc, " \r\t/*"); |
|
272 | - if(strlen($doc) && strpos($doc, '@') !== 0 && !$i) |
|
273 | - { |
|
274 | - if($desc) |
|
275 | - { |
|
276 | - $desc .= "\n"; |
|
277 | - } |
|
278 | - $desc .= $doc; |
|
279 | - } |
|
280 | - elseif(strpos($doc, '@param') === 0) |
|
281 | - { |
|
282 | - // syntax: @param type [$name] desc |
|
283 | - if(preg_match('/@param\s+(\S+)(\s+\$\S+)?\s+(.+)/', $doc, $matches)) |
|
284 | - { |
|
285 | - if(strpos($matches[1], '|')) |
|
286 | - { |
|
287 | - //$paramDocs[$i]['type'] = explode('|', $matches[1]); |
|
288 | - $paramDocs[$i]['type'] = 'mixed'; |
|
289 | - } |
|
290 | - else |
|
291 | - { |
|
292 | - $paramDocs[$i]['type'] = $matches[1]; |
|
293 | - } |
|
294 | - $paramDocs[$i]['name'] = trim($matches[2]); |
|
295 | - $paramDocs[$i]['doc'] = $matches[3]; |
|
296 | - } |
|
297 | - $i++; |
|
298 | - } |
|
299 | - elseif(strpos($doc, '@return') === 0) |
|
300 | - { |
|
301 | - // syntax: @return type desc |
|
302 | - //$returns = preg_split('/\s+/', $doc); |
|
303 | - if(preg_match('/@return\s+(\S+)\s+(.+)/', $doc, $matches)) |
|
304 | - { |
|
305 | - $returns = php_2_xmlrpc_type($matches[1]); |
|
306 | - if(isset($matches[2])) |
|
307 | - { |
|
308 | - $returnsDocs = $matches[2]; |
|
309 | - } |
|
310 | - } |
|
311 | - } |
|
312 | - } |
|
313 | - } |
|
314 | - |
|
315 | - // execute introspection of actual function prototype |
|
316 | - $params = array(); |
|
317 | - $i = 0; |
|
318 | - foreach($func->getParameters() as $paramobj) |
|
319 | - { |
|
320 | - $params[$i] = array(); |
|
321 | - $params[$i]['name'] = '$'.$paramobj->getName(); |
|
322 | - $params[$i]['isoptional'] = $paramobj->isOptional(); |
|
323 | - $i++; |
|
324 | - } |
|
325 | - |
|
326 | - |
|
327 | - // start building of PHP code to be eval'd |
|
328 | - $innercode = ''; |
|
329 | - $i = 0; |
|
330 | - $parsvariations = array(); |
|
331 | - $pars = array(); |
|
332 | - $pnum = count($params); |
|
333 | - foreach($params as $param) |
|
334 | - { |
|
335 | - if (isset($paramDocs[$i]['name']) && $paramDocs[$i]['name'] && strtolower($paramDocs[$i]['name']) != strtolower($param['name'])) |
|
336 | - { |
|
337 | - // param name from phpdoc info does not match param definition! |
|
338 | - $paramDocs[$i]['type'] = 'mixed'; |
|
339 | - } |
|
340 | - |
|
341 | - if($param['isoptional']) |
|
342 | - { |
|
343 | - // this particular parameter is optional. save as valid previous list of parameters |
|
344 | - $innercode .= "if (\$paramcount > $i) {\n"; |
|
345 | - $parsvariations[] = $pars; |
|
346 | - } |
|
347 | - $innercode .= "\$p$i = \$msg->getParam($i);\n"; |
|
348 | - if ($decode_php_objects) |
|
349 | - { |
|
350 | - $innercode .= "if (\$p{$i}->kindOf() == 'scalar') \$p$i = \$p{$i}->scalarval(); else \$p$i = php_{$prefix}_decode(\$p$i, array('decode_php_objs'));\n"; |
|
351 | - } |
|
352 | - else |
|
353 | - { |
|
354 | - $innercode .= "if (\$p{$i}->kindOf() == 'scalar') \$p$i = \$p{$i}->scalarval(); else \$p$i = php_{$prefix}_decode(\$p$i);\n"; |
|
355 | - } |
|
356 | - |
|
357 | - $pars[] = "\$p$i"; |
|
358 | - $i++; |
|
359 | - if($param['isoptional']) |
|
360 | - { |
|
361 | - $innercode .= "}\n"; |
|
362 | - } |
|
363 | - if($i == $pnum) |
|
364 | - { |
|
365 | - // last allowed parameters combination |
|
366 | - $parsvariations[] = $pars; |
|
367 | - } |
|
368 | - } |
|
369 | - |
|
370 | - $sigs = array(); |
|
371 | - $psigs = array(); |
|
372 | - if(count($parsvariations) == 0) |
|
373 | - { |
|
374 | - // only known good synopsis = no parameters |
|
375 | - $parsvariations[] = array(); |
|
376 | - $minpars = 0; |
|
377 | - } |
|
378 | - else |
|
379 | - { |
|
380 | - $minpars = count($parsvariations[0]); |
|
381 | - } |
|
382 | - |
|
383 | - if($minpars) |
|
384 | - { |
|
385 | - // add to code the check for min params number |
|
386 | - // NB: this check needs to be done BEFORE decoding param values |
|
387 | - $innercode = "\$paramcount = \$msg->getNumParams();\n" . |
|
388 | - "if (\$paramcount < $minpars) return new {$prefix}resp(0, {$GLOBALS['xmlrpcerr']['incorrect_params']}, '{$GLOBALS['xmlrpcstr']['incorrect_params']}');\n" . $innercode; |
|
389 | - } |
|
390 | - else |
|
391 | - { |
|
392 | - $innercode = "\$paramcount = \$msg->getNumParams();\n" . $innercode; |
|
393 | - } |
|
394 | - |
|
395 | - $innercode .= "\$np = false;\n"; |
|
396 | - // since there are no closures in php, if we are given an object instance, |
|
397 | - // we store a pointer to it in a global var... |
|
398 | - if ( is_array($funcname) && is_object($funcname[0]) ) |
|
399 | - { |
|
400 | - $GLOBALS['xmlrpcWPFObjHolder'][$xmlrpcfuncname] =& $funcname[0]; |
|
401 | - $innercode .= "\$obj =& \$GLOBALS['xmlrpcWPFObjHolder']['$xmlrpcfuncname'];\n"; |
|
402 | - $realfuncname = '$obj->'.$funcname[1]; |
|
403 | - } |
|
404 | - else |
|
405 | - { |
|
406 | - $realfuncname = $plainfuncname; |
|
407 | - } |
|
408 | - foreach($parsvariations as $pars) |
|
409 | - { |
|
410 | - $innercode .= "if (\$paramcount == " . count($pars) . ") \$retval = {$catch_warnings}$realfuncname(" . implode(',', $pars) . "); else\n"; |
|
411 | - // build a 'generic' signature (only use an appropriate return type) |
|
412 | - $sig = array($returns); |
|
413 | - $psig = array($returnsDocs); |
|
414 | - for($i=0; $i < count($pars); $i++) |
|
415 | - { |
|
416 | - if (isset($paramDocs[$i]['type'])) |
|
417 | - { |
|
418 | - $sig[] = php_2_xmlrpc_type($paramDocs[$i]['type']); |
|
419 | - } |
|
420 | - else |
|
421 | - { |
|
422 | - $sig[] = $GLOBALS['xmlrpcValue']; |
|
423 | - } |
|
424 | - $psig[] = isset($paramDocs[$i]['doc']) ? $paramDocs[$i]['doc'] : ''; |
|
425 | - } |
|
426 | - $sigs[] = $sig; |
|
427 | - $psigs[] = $psig; |
|
428 | - } |
|
429 | - $innercode .= "\$np = true;\n"; |
|
430 | - $innercode .= "if (\$np) return new {$prefix}resp(0, {$GLOBALS['xmlrpcerr']['incorrect_params']}, '{$GLOBALS['xmlrpcstr']['incorrect_params']}'); else {\n"; |
|
431 | - //$innercode .= "if (\$_xmlrpcs_error_occurred) return new xmlrpcresp(0, $GLOBALS['xmlrpcerr']user, \$_xmlrpcs_error_occurred); else\n"; |
|
432 | - $innercode .= "if (is_a(\$retval, '{$prefix}resp')) return \$retval; else\n"; |
|
433 | - if($returns == $GLOBALS['xmlrpcDateTime'] || $returns == $GLOBALS['xmlrpcBase64']) |
|
434 | - { |
|
435 | - $innercode .= "return new {$prefix}resp(new {$prefix}val(\$retval, '$returns'));"; |
|
436 | - } |
|
437 | - else |
|
438 | - { |
|
439 | - if ($encode_php_objects) |
|
440 | - $innercode .= "return new {$prefix}resp(php_{$prefix}_encode(\$retval, array('encode_php_objs')));\n"; |
|
441 | - else |
|
442 | - $innercode .= "return new {$prefix}resp(php_{$prefix}_encode(\$retval));\n"; |
|
443 | - } |
|
444 | - // shall we exclude functions returning by ref? |
|
445 | - // if($func->returnsReference()) |
|
446 | - // return false; |
|
447 | - $code = "function $xmlrpcfuncname(\$msg) {\n" . $innercode . "}\n}"; |
|
448 | - //print_r($code); |
|
449 | - if ($buildit) |
|
450 | - { |
|
451 | - $allOK = 0; |
|
452 | - eval($code.'$allOK=1;'); |
|
453 | - // alternative |
|
454 | - //$xmlrpcfuncname = create_function('$m', $innercode); |
|
455 | - |
|
456 | - if(!$allOK) |
|
457 | - { |
|
458 | - error_log('XML-RPC: could not create function '.$xmlrpcfuncname.' to wrap php function '.$plainfuncname); |
|
459 | - return false; |
|
460 | - } |
|
461 | - } |
|
462 | - |
|
463 | - /// @todo examine if $paramDocs matches $parsvariations and build array for |
|
464 | - /// usage as method signature, plus put together a nice string for docs |
|
465 | - |
|
466 | - $ret = array('function' => $xmlrpcfuncname, 'signature' => $sigs, 'docstring' => $desc, 'signature_docs' => $psigs, 'source' => $code); |
|
467 | - return $ret; |
|
468 | - } |
|
469 | - } |
|
470 | - |
|
471 | - /** |
|
472 | - * Given a user-defined PHP class or php object, map its methods onto a list of |
|
473 | - * PHP 'wrapper' functions that can be exposed as xmlrpc methods from an xmlrpc_server |
|
474 | - * object and called from remote clients (as well as their corresponding signature info). |
|
475 | - * |
|
476 | - * @param mixed $classname the name of the class whose methods are to be exposed as xmlrpc methods, or an object instance of that class |
|
477 | - * @param array $extra_options see the docs for wrap_php_method for more options |
|
478 | - * string method_type 'static', 'nonstatic', 'all' and 'auto' (default); the latter will switch between static and non-static depending on wheter $classname is a class name or object instance |
|
479 | - * @return array or false on failure |
|
480 | - * |
|
481 | - * @todo get_class_methods will return both static and non-static methods. |
|
482 | - * we have to differentiate the action, depending on whether we received a class name or object |
|
483 | - */ |
|
484 | - function wrap_php_class($classname, $extra_options=array()) |
|
485 | - { |
|
486 | - $methodfilter = isset($extra_options['method_filter']) ? $extra_options['method_filter'] : ''; |
|
487 | - $methodtype = isset($extra_options['method_type']) ? $extra_options['method_type'] : 'auto'; |
|
488 | - |
|
489 | - $result = array(); |
|
490 | - $mlist = get_class_methods($classname); |
|
491 | - foreach($mlist as $mname) |
|
492 | - { |
|
493 | - if ($methodfilter == '' || preg_match($methodfilter, $mname)) |
|
494 | - { |
|
495 | - // echo $mlist."\n"; |
|
496 | - $func = new ReflectionMethod($classname, $mname); |
|
497 | - if(!$func->isPrivate() && !$func->isProtected() && !$func->isConstructor() && !$func->isDestructor() && !$func->isAbstract()) |
|
498 | - { |
|
499 | - if(($func->isStatic && ($methodtype == 'all' || $methodtype == 'static' || ($methodtype == 'auto' && is_string($classname)))) || |
|
500 | - (!$func->isStatic && ($methodtype == 'all' || $methodtype == 'nonstatic' || ($methodtype == 'auto' && is_object($classname))))) |
|
501 | - { |
|
502 | - $methodwrap = wrap_php_function(array($classname, $mname), '', $extra_options); |
|
503 | - if ( $methodwrap ) |
|
504 | - { |
|
505 | - $result[$methodwrap['function']] = $methodwrap['function']; |
|
506 | - } |
|
507 | - } |
|
508 | - } |
|
509 | - } |
|
510 | - } |
|
511 | - return $result; |
|
512 | - } |
|
513 | - |
|
514 | - /** |
|
515 | - * Given an xmlrpc client and a method name, register a php wrapper function |
|
516 | - * that will call it and return results using native php types for both |
|
517 | - * params and results. The generated php function will return an xmlrpcresp |
|
518 | - * object for failed xmlrpc calls |
|
519 | - * |
|
520 | - * Known limitations: |
|
521 | - * - server must support system.methodsignature for the wanted xmlrpc method |
|
522 | - * - for methods that expose many signatures, only one can be picked (we |
|
523 | - * could in principle check if signatures differ only by number of params |
|
524 | - * and not by type, but it would be more complication than we can spare time) |
|
525 | - * - nested xmlrpc params: the caller of the generated php function has to |
|
526 | - * encode on its own the params passed to the php function if these are structs |
|
527 | - * or arrays whose (sub)members include values of type datetime or base64 |
|
528 | - * |
|
529 | - * Notes: the connection properties of the given client will be copied |
|
530 | - * and reused for the connection used during the call to the generated |
|
531 | - * php function. |
|
532 | - * Calling the generated php function 'might' be slow: a new xmlrpc client |
|
533 | - * is created on every invocation and an xmlrpc-connection opened+closed. |
|
534 | - * An extra 'debug' param is appended to param list of xmlrpc method, useful |
|
535 | - * for debugging purposes. |
|
536 | - * |
|
537 | - * @param xmlrpc_client $client an xmlrpc client set up correctly to communicate with target server |
|
538 | - * @param string $methodname the xmlrpc method to be mapped to a php function |
|
539 | - * @param array $extra_options array of options that specify conversion details. valid options include |
|
540 | - * integer signum the index of the method signature to use in mapping (if method exposes many sigs) |
|
541 | - * integer timeout timeout (in secs) to be used when executing function/calling remote method |
|
542 | - * string protocol 'http' (default), 'http11' or 'https' |
|
543 | - * string new_function_name the name of php function to create. If unspecified, lib will pick an appropriate name |
|
544 | - * string return_source if true return php code w. function definition instead fo function name |
|
545 | - * bool encode_php_objs let php objects be sent to server using the 'improved' xmlrpc notation, so server can deserialize them as php objects |
|
546 | - * bool decode_php_objs --- WARNING !!! possible security hazard. only use it with trusted servers --- |
|
547 | - * mixed return_on_fault a php value to be returned when the xmlrpc call fails/returns a fault response (by default the xmlrpcresp object is returned in this case). If a string is used, '%faultCode%' and '%faultString%' tokens will be substituted with actual error values |
|
548 | - * bool debug set it to 1 or 2 to see debug results of querying server for method synopsis |
|
549 | - * @return string the name of the generated php function (or false) - OR AN ARRAY... |
|
550 | - */ |
|
551 | - function wrap_xmlrpc_method($client, $methodname, $extra_options=0, $timeout=0, $protocol='', $newfuncname='') |
|
552 | - { |
|
553 | - // mind numbing: let caller use sane calling convention (as per javadoc, 3 params), |
|
554 | - // OR the 2.0 calling convention (no options) - we really love backward compat, don't we? |
|
555 | - if (!is_array($extra_options)) |
|
556 | - { |
|
557 | - $signum = $extra_options; |
|
558 | - $extra_options = array(); |
|
559 | - } |
|
560 | - else |
|
561 | - { |
|
562 | - $signum = isset($extra_options['signum']) ? (int)$extra_options['signum'] : 0; |
|
563 | - $timeout = isset($extra_options['timeout']) ? (int)$extra_options['timeout'] : 0; |
|
564 | - $protocol = isset($extra_options['protocol']) ? $extra_options['protocol'] : ''; |
|
565 | - $newfuncname = isset($extra_options['new_function_name']) ? $extra_options['new_function_name'] : ''; |
|
566 | - } |
|
567 | - //$encode_php_objects = in_array('encode_php_objects', $extra_options); |
|
568 | - //$verbatim_client_copy = in_array('simple_client_copy', $extra_options) ? 1 : |
|
569 | - // in_array('build_class_code', $extra_options) ? 2 : 0; |
|
570 | - |
|
571 | - $encode_php_objects = isset($extra_options['encode_php_objs']) ? (bool)$extra_options['encode_php_objs'] : false; |
|
572 | - $decode_php_objects = isset($extra_options['decode_php_objs']) ? (bool)$extra_options['decode_php_objs'] : false; |
|
573 | - // it seems like the meaning of 'simple_client_copy' here is swapped wrt client_copy_mode later on... |
|
574 | - $simple_client_copy = isset($extra_options['simple_client_copy']) ? (int)($extra_options['simple_client_copy']) : 0; |
|
575 | - $buildit = isset($extra_options['return_source']) ? !($extra_options['return_source']) : true; |
|
576 | - $prefix = isset($extra_options['prefix']) ? $extra_options['prefix'] : 'xmlrpc'; |
|
577 | - if (isset($extra_options['return_on_fault'])) |
|
578 | - { |
|
579 | - $decode_fault = true; |
|
580 | - $fault_response = $extra_options['return_on_fault']; |
|
581 | - } |
|
582 | - else |
|
583 | - { |
|
584 | - $decode_fault = false; |
|
585 | - $fault_response = ''; |
|
586 | - } |
|
587 | - $debug = isset($extra_options['debug']) ? ($extra_options['debug']) : 0; |
|
588 | - |
|
589 | - $msgclass = $prefix.'msg'; |
|
590 | - $valclass = $prefix.'val'; |
|
591 | - $decodefunc = 'php_'.$prefix.'_decode'; |
|
592 | - |
|
593 | - $msg = new $msgclass('system.methodSignature'); |
|
594 | - $msg->addparam(new $valclass($methodname)); |
|
595 | - $client->setDebug($debug); |
|
596 | - $response =& $client->send($msg, $timeout, $protocol); |
|
597 | - if($response->faultCode()) |
|
598 | - { |
|
599 | - error_log('XML-RPC: could not retrieve method signature from remote server for method '.$methodname); |
|
600 | - return false; |
|
601 | - } |
|
602 | - else |
|
603 | - { |
|
604 | - $msig = $response->value(); |
|
605 | - if ($client->return_type != 'phpvals') |
|
606 | - { |
|
607 | - $msig = $decodefunc($msig); |
|
608 | - } |
|
609 | - if(!is_array($msig) || count($msig) <= $signum) |
|
610 | - { |
|
611 | - error_log('XML-RPC: could not retrieve method signature nr.'.$signum.' from remote server for method '.$methodname); |
|
612 | - return false; |
|
613 | - } |
|
614 | - else |
|
615 | - { |
|
616 | - // pick a suitable name for the new function, avoiding collisions |
|
617 | - if($newfuncname != '') |
|
618 | - { |
|
619 | - $xmlrpcfuncname = $newfuncname; |
|
620 | - } |
|
621 | - else |
|
622 | - { |
|
623 | - // take care to insure that methodname is translated to valid |
|
624 | - // php function name |
|
625 | - $xmlrpcfuncname = $prefix.'_'.preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), |
|
626 | - array('_', ''), $methodname); |
|
627 | - } |
|
628 | - while($buildit && function_exists($xmlrpcfuncname)) |
|
629 | - { |
|
630 | - $xmlrpcfuncname .= 'x'; |
|
631 | - } |
|
632 | - |
|
633 | - $msig = $msig[$signum]; |
|
634 | - $mdesc = ''; |
|
635 | - // if in 'offline' mode, get method description too. |
|
636 | - // in online mode, favour speed of operation |
|
637 | - if(!$buildit) |
|
638 | - { |
|
639 | - $msg = new $msgclass('system.methodHelp'); |
|
640 | - $msg->addparam(new $valclass($methodname)); |
|
641 | - $response =& $client->send($msg, $timeout, $protocol); |
|
642 | - if (!$response->faultCode()) |
|
643 | - { |
|
644 | - $mdesc = $response->value(); |
|
645 | - if ($client->return_type != 'phpvals') |
|
646 | - { |
|
647 | - $mdesc = $mdesc->scalarval(); |
|
648 | - } |
|
649 | - } |
|
650 | - } |
|
651 | - |
|
652 | - $results = build_remote_method_wrapper_code($client, $methodname, |
|
653 | - $xmlrpcfuncname, $msig, $mdesc, $timeout, $protocol, $simple_client_copy, |
|
654 | - $prefix, $decode_php_objects, $encode_php_objects, $decode_fault, |
|
655 | - $fault_response); |
|
656 | - |
|
657 | - //print_r($code); |
|
658 | - if ($buildit) |
|
659 | - { |
|
660 | - $allOK = 0; |
|
661 | - eval($results['source'].'$allOK=1;'); |
|
662 | - // alternative |
|
663 | - //$xmlrpcfuncname = create_function('$m', $innercode); |
|
664 | - if($allOK) |
|
665 | - { |
|
666 | - return $xmlrpcfuncname; |
|
667 | - } |
|
668 | - else |
|
669 | - { |
|
670 | - error_log('XML-RPC: could not create function '.$xmlrpcfuncname.' to wrap remote method '.$methodname); |
|
671 | - return false; |
|
672 | - } |
|
673 | - } |
|
674 | - else |
|
675 | - { |
|
676 | - $results['function'] = $xmlrpcfuncname; |
|
677 | - return $results; |
|
678 | - } |
|
679 | - } |
|
680 | - } |
|
681 | - } |
|
682 | - |
|
683 | - /** |
|
684 | - * Similar to wrap_xmlrpc_method, but will generate a php class that wraps |
|
685 | - * all xmlrpc methods exposed by the remote server as own methods. |
|
686 | - * For more details see wrap_xmlrpc_method. |
|
687 | - * @param xmlrpc_client $client the client obj all set to query the desired server |
|
688 | - * @param array $extra_options list of options for wrapped code |
|
689 | - * @return mixed false on error, the name of the created class if all ok or an array with code, class name and comments (if the appropriatevoption is set in extra_options) |
|
690 | - */ |
|
691 | - function wrap_xmlrpc_server($client, $extra_options=array()) |
|
692 | - { |
|
693 | - $methodfilter = isset($extra_options['method_filter']) ? $extra_options['method_filter'] : ''; |
|
694 | - //$signum = isset($extra_options['signum']) ? (int)$extra_options['signum'] : 0; |
|
695 | - $timeout = isset($extra_options['timeout']) ? (int)$extra_options['timeout'] : 0; |
|
696 | - $protocol = isset($extra_options['protocol']) ? $extra_options['protocol'] : ''; |
|
697 | - $newclassname = isset($extra_options['new_class_name']) ? $extra_options['new_class_name'] : ''; |
|
698 | - $encode_php_objects = isset($extra_options['encode_php_objs']) ? (bool)$extra_options['encode_php_objs'] : false; |
|
699 | - $decode_php_objects = isset($extra_options['decode_php_objs']) ? (bool)$extra_options['decode_php_objs'] : false; |
|
700 | - $verbatim_client_copy = isset($extra_options['simple_client_copy']) ? !($extra_options['simple_client_copy']) : true; |
|
701 | - $buildit = isset($extra_options['return_source']) ? !($extra_options['return_source']) : true; |
|
702 | - $prefix = isset($extra_options['prefix']) ? $extra_options['prefix'] : 'xmlrpc'; |
|
703 | - |
|
704 | - $msgclass = $prefix.'msg'; |
|
705 | - //$valclass = $prefix.'val'; |
|
706 | - $decodefunc = 'php_'.$prefix.'_decode'; |
|
707 | - |
|
708 | - $msg = new $msgclass('system.listMethods'); |
|
709 | - $response =& $client->send($msg, $timeout, $protocol); |
|
710 | - if($response->faultCode()) |
|
711 | - { |
|
712 | - error_log('XML-RPC: could not retrieve method list from remote server'); |
|
713 | - return false; |
|
714 | - } |
|
715 | - else |
|
716 | - { |
|
717 | - $mlist = $response->value(); |
|
718 | - if ($client->return_type != 'phpvals') |
|
719 | - { |
|
720 | - $mlist = $decodefunc($mlist); |
|
721 | - } |
|
722 | - if(!is_array($mlist) || !count($mlist)) |
|
723 | - { |
|
724 | - error_log('XML-RPC: could not retrieve meaningful method list from remote server'); |
|
725 | - return false; |
|
726 | - } |
|
727 | - else |
|
728 | - { |
|
729 | - // pick a suitable name for the new function, avoiding collisions |
|
730 | - if($newclassname != '') |
|
731 | - { |
|
732 | - $xmlrpcclassname = $newclassname; |
|
733 | - } |
|
734 | - else |
|
735 | - { |
|
736 | - $xmlrpcclassname = $prefix.'_'.preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), |
|
737 | - array('_', ''), $client->server).'_client'; |
|
738 | - } |
|
739 | - while($buildit && class_exists($xmlrpcclassname)) |
|
740 | - { |
|
741 | - $xmlrpcclassname .= 'x'; |
|
742 | - } |
|
743 | - |
|
744 | - /// @todo add function setdebug() to new class, to enable/disable debugging |
|
745 | - $source = "class $xmlrpcclassname\n{\nvar \$client;\n\n"; |
|
746 | - $source .= "function $xmlrpcclassname()\n{\n"; |
|
747 | - $source .= build_client_wrapper_code($client, $verbatim_client_copy, $prefix); |
|
748 | - $source .= "\$this->client =& \$client;\n}\n\n"; |
|
749 | - $opts = array('simple_client_copy' => 2, 'return_source' => true, |
|
750 | - 'timeout' => $timeout, 'protocol' => $protocol, |
|
751 | - 'encode_php_objs' => $encode_php_objects, 'prefix' => $prefix, |
|
752 | - 'decode_php_objs' => $decode_php_objects |
|
753 | - ); |
|
754 | - /// @todo build javadoc for class definition, too |
|
755 | - foreach($mlist as $mname) |
|
756 | - { |
|
757 | - if ($methodfilter == '' || preg_match($methodfilter, $mname)) |
|
758 | - { |
|
759 | - $opts['new_function_name'] = preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), |
|
760 | - array('_', ''), $mname); |
|
761 | - $methodwrap = wrap_xmlrpc_method($client, $mname, $opts); |
|
762 | - if ($methodwrap) |
|
763 | - { |
|
764 | - if (!$buildit) |
|
765 | - { |
|
766 | - $source .= $methodwrap['docstring']; |
|
767 | - } |
|
768 | - $source .= $methodwrap['source']."\n"; |
|
769 | - } |
|
770 | - else |
|
771 | - { |
|
772 | - error_log('XML-RPC: will not create class method to wrap remote method '.$mname); |
|
773 | - } |
|
774 | - } |
|
775 | - } |
|
776 | - $source .= "}\n"; |
|
777 | - if ($buildit) |
|
778 | - { |
|
779 | - $allOK = 0; |
|
780 | - eval($source.'$allOK=1;'); |
|
781 | - // alternative |
|
782 | - //$xmlrpcfuncname = create_function('$m', $innercode); |
|
783 | - if($allOK) |
|
784 | - { |
|
785 | - return $xmlrpcclassname; |
|
786 | - } |
|
787 | - else |
|
788 | - { |
|
789 | - error_log('XML-RPC: could not create class '.$xmlrpcclassname.' to wrap remote server '.$client->server); |
|
790 | - return false; |
|
791 | - } |
|
792 | - } |
|
793 | - else |
|
794 | - { |
|
795 | - return array('class' => $xmlrpcclassname, 'code' => $source, 'docstring' => ''); |
|
796 | - } |
|
797 | - } |
|
798 | - } |
|
799 | - } |
|
800 | - |
|
801 | - /** |
|
802 | - * Given the necessary info, build php code that creates a new function to |
|
803 | - * invoke a remote xmlrpc method. |
|
804 | - * Take care that no full checking of input parameters is done to ensure that |
|
805 | - * valid php code is emitted. |
|
806 | - * Note: real spaghetti code follows... |
|
807 | - * @access private |
|
808 | - */ |
|
809 | - function build_remote_method_wrapper_code($client, $methodname, $xmlrpcfuncname, |
|
810 | - $msig, $mdesc='', $timeout=0, $protocol='', $client_copy_mode=0, $prefix='xmlrpc', |
|
811 | - $decode_php_objects=false, $encode_php_objects=false, $decode_fault=false, |
|
812 | - $fault_response='') |
|
813 | - { |
|
814 | - $code = "function $xmlrpcfuncname ("; |
|
815 | - if ($client_copy_mode < 2) |
|
816 | - { |
|
817 | - // client copy mode 0 or 1 == partial / full client copy in emitted code |
|
818 | - $innercode = build_client_wrapper_code($client, $client_copy_mode, $prefix); |
|
819 | - $innercode .= "\$client->setDebug(\$debug);\n"; |
|
820 | - $this_ = ''; |
|
821 | - } |
|
822 | - else |
|
823 | - { |
|
824 | - // client copy mode 2 == no client copy in emitted code |
|
825 | - $innercode = ''; |
|
826 | - $this_ = 'this->'; |
|
827 | - } |
|
828 | - $innercode .= "\$msg = new {$prefix}msg('$methodname');\n"; |
|
829 | - |
|
830 | - if ($mdesc != '') |
|
831 | - { |
|
832 | - // take care that PHP comment is not terminated unwillingly by method description |
|
833 | - $mdesc = "/**\n* ".str_replace('*/', '* /', $mdesc)."\n"; |
|
834 | - } |
|
835 | - else |
|
836 | - { |
|
837 | - $mdesc = "/**\nFunction $xmlrpcfuncname\n"; |
|
838 | - } |
|
839 | - |
|
840 | - // param parsing |
|
841 | - $plist = array(); |
|
842 | - $pcount = count($msig); |
|
843 | - for($i = 1; $i < $pcount; $i++) |
|
844 | - { |
|
845 | - $plist[] = "\$p$i"; |
|
846 | - $ptype = $msig[$i]; |
|
847 | - if($ptype == 'i4' || $ptype == 'int' || $ptype == 'boolean' || $ptype == 'double' || |
|
848 | - $ptype == 'string' || $ptype == 'dateTime.iso8601' || $ptype == 'base64' || $ptype == 'null') |
|
849 | - { |
|
850 | - // only build directly xmlrpcvals when type is known and scalar |
|
851 | - $innercode .= "\$p$i = new {$prefix}val(\$p$i, '$ptype');\n"; |
|
852 | - } |
|
853 | - else |
|
854 | - { |
|
855 | - if ($encode_php_objects) |
|
856 | - { |
|
857 | - $innercode .= "\$p$i =& php_{$prefix}_encode(\$p$i, array('encode_php_objs'));\n"; |
|
858 | - } |
|
859 | - else |
|
860 | - { |
|
861 | - $innercode .= "\$p$i =& php_{$prefix}_encode(\$p$i);\n"; |
|
862 | - } |
|
863 | - } |
|
864 | - $innercode .= "\$msg->addparam(\$p$i);\n"; |
|
865 | - $mdesc .= '* @param '.xmlrpc_2_php_type($ptype)." \$p$i\n"; |
|
866 | - } |
|
867 | - if ($client_copy_mode < 2) |
|
868 | - { |
|
869 | - $plist[] = '$debug=0'; |
|
870 | - $mdesc .= "* @param int \$debug when 1 (or 2) will enable debugging of the underlying {$prefix} call (defaults to 0)\n"; |
|
871 | - } |
|
872 | - $plist = implode(', ', $plist); |
|
873 | - $mdesc .= '* @return '.xmlrpc_2_php_type($msig[0])." (or an {$prefix}resp obj instance if call fails)\n*/\n"; |
|
874 | - |
|
875 | - $innercode .= "\$res =& \${$this_}client->send(\$msg, $timeout, '$protocol');\n"; |
|
876 | - if ($decode_fault) |
|
877 | - { |
|
878 | - if (is_string($fault_response) && ((strpos($fault_response, '%faultCode%') !== false) || (strpos($fault_response, '%faultString%') !== false))) |
|
879 | - { |
|
880 | - $respcode = "str_replace(array('%faultCode%', '%faultString%'), array(\$res->faultCode(), \$res->faultString()), '".str_replace("'", "''", $fault_response)."')"; |
|
881 | - } |
|
882 | - else |
|
883 | - { |
|
884 | - $respcode = var_export($fault_response, true); |
|
885 | - } |
|
886 | - } |
|
887 | - else |
|
888 | - { |
|
889 | - $respcode = '$res'; |
|
890 | - } |
|
891 | - if ($decode_php_objects) |
|
892 | - { |
|
893 | - $innercode .= "if (\$res->faultcode()) return $respcode; else return php_{$prefix}_decode(\$res->value(), array('decode_php_objs'));"; |
|
894 | - } |
|
895 | - else |
|
896 | - { |
|
897 | - $innercode .= "if (\$res->faultcode()) return $respcode; else return php_{$prefix}_decode(\$res->value());"; |
|
898 | - } |
|
899 | - |
|
900 | - $code = $code . $plist. ") {\n" . $innercode . "\n}\n"; |
|
901 | - |
|
902 | - return array('source' => $code, 'docstring' => $mdesc); |
|
903 | - } |
|
904 | - |
|
905 | - /** |
|
906 | - * Given necessary info, generate php code that will rebuild a client object |
|
907 | - * Take care that no full checking of input parameters is done to ensure that |
|
908 | - * valid php code is emitted. |
|
909 | - * @access private |
|
910 | - */ |
|
911 | - function build_client_wrapper_code($client, $verbatim_client_copy, $prefix='xmlrpc') |
|
912 | - { |
|
913 | - $code = "\$client = new {$prefix}_client('".str_replace("'", "\'", $client->path). |
|
914 | - "', '" . str_replace("'", "\'", $client->server) . "', $client->port);\n"; |
|
915 | - |
|
916 | - // copy all client fields to the client that will be generated runtime |
|
917 | - // (this provides for future expansion or subclassing of client obj) |
|
918 | - if ($verbatim_client_copy) |
|
919 | - { |
|
920 | - foreach($client as $fld => $val) |
|
921 | - { |
|
922 | - if($fld != 'debug' && $fld != 'return_type') |
|
923 | - { |
|
924 | - $val = var_export($val, true); |
|
925 | - $code .= "\$client->$fld = $val;\n"; |
|
926 | - } |
|
927 | - } |
|
928 | - } |
|
929 | - // only make sure that client always returns the correct data type |
|
930 | - $code .= "\$client->return_type = '{$prefix}vals';\n"; |
|
931 | - //$code .= "\$client->setDebug(\$debug);\n"; |
|
932 | - return $code; |
|
933 | - } |
|
17 | + // requires: xmlrpc.inc |
|
18 | + |
|
19 | + /** |
|
20 | + * Given a string defining a php type or phpxmlrpc type (loosely defined: strings |
|
21 | + * accepted come from javadoc blocks), return corresponding phpxmlrpc type. |
|
22 | + * NB: for php 'resource' types returns empty string, since resources cannot be serialized; |
|
23 | + * for php class names returns 'struct', since php objects can be serialized as xmlrpc structs |
|
24 | + * for php arrays always return array, even though arrays sometiles serialize as json structs |
|
25 | + * @param string $phptype |
|
26 | + * @return string |
|
27 | + */ |
|
28 | + function php_2_xmlrpc_type($phptype) |
|
29 | + { |
|
30 | + switch(strtolower($phptype)) |
|
31 | + { |
|
32 | + case 'string': |
|
33 | + return $GLOBALS['xmlrpcString']; |
|
34 | + case 'integer': |
|
35 | + case $GLOBALS['xmlrpcInt']: // 'int' |
|
36 | + case $GLOBALS['xmlrpcI4']: |
|
37 | + return $GLOBALS['xmlrpcInt']; |
|
38 | + case 'double': |
|
39 | + return $GLOBALS['xmlrpcDouble']; |
|
40 | + case 'boolean': |
|
41 | + return $GLOBALS['xmlrpcBoolean']; |
|
42 | + case 'array': |
|
43 | + return $GLOBALS['xmlrpcArray']; |
|
44 | + case 'object': |
|
45 | + return $GLOBALS['xmlrpcStruct']; |
|
46 | + case $GLOBALS['xmlrpcBase64']: |
|
47 | + case $GLOBALS['xmlrpcStruct']: |
|
48 | + return strtolower($phptype); |
|
49 | + case 'resource': |
|
50 | + return ''; |
|
51 | + default: |
|
52 | + if(class_exists($phptype)) |
|
53 | + { |
|
54 | + return $GLOBALS['xmlrpcStruct']; |
|
55 | + } |
|
56 | + else |
|
57 | + { |
|
58 | + // unknown: might be any 'extended' xmlrpc type |
|
59 | + return $GLOBALS['xmlrpcValue']; |
|
60 | + } |
|
61 | + } |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Given a string defining a phpxmlrpc type return corresponding php type. |
|
66 | + * @param string $xmlrpctype |
|
67 | + * @return string |
|
68 | + */ |
|
69 | + function xmlrpc_2_php_type($xmlrpctype) |
|
70 | + { |
|
71 | + switch(strtolower($xmlrpctype)) |
|
72 | + { |
|
73 | + case 'base64': |
|
74 | + case 'datetime.iso8601': |
|
75 | + case 'string': |
|
76 | + return $GLOBALS['xmlrpcString']; |
|
77 | + case 'int': |
|
78 | + case 'i4': |
|
79 | + return 'integer'; |
|
80 | + case 'struct': |
|
81 | + case 'array': |
|
82 | + return 'array'; |
|
83 | + case 'double': |
|
84 | + return 'float'; |
|
85 | + case 'undefined': |
|
86 | + return 'mixed'; |
|
87 | + case 'boolean': |
|
88 | + case 'null': |
|
89 | + default: |
|
90 | + // unknown: might be any xmlrpc type |
|
91 | + return strtolower($xmlrpctype); |
|
92 | + } |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Given a user-defined PHP function, create a PHP 'wrapper' function that can |
|
97 | + * be exposed as xmlrpc method from an xmlrpc_server object and called from remote |
|
98 | + * clients (as well as its corresponding signature info). |
|
99 | + * |
|
100 | + * Since php is a typeless language, to infer types of input and output parameters, |
|
101 | + * it relies on parsing the javadoc-style comment block associated with the given |
|
102 | + * function. Usage of xmlrpc native types (such as datetime.dateTime.iso8601 and base64) |
|
103 | + * in the @param tag is also allowed, if you need the php function to receive/send |
|
104 | + * data in that particular format (note that base64 encoding/decoding is transparently |
|
105 | + * carried out by the lib, while datetime vals are passed around as strings) |
|
106 | + * |
|
107 | + * Known limitations: |
|
108 | + * - only works for user-defined functions, not for PHP internal functions |
|
109 | + * (reflection does not support retrieving number/type of params for those) |
|
110 | + * - functions returning php objects will generate special xmlrpc responses: |
|
111 | + * when the xmlrpc decoding of those responses is carried out by this same lib, using |
|
112 | + * the appropriate param in php_xmlrpc_decode, the php objects will be rebuilt. |
|
113 | + * In short: php objects can be serialized, too (except for their resource members), |
|
114 | + * using this function. |
|
115 | + * Other libs might choke on the very same xml that will be generated in this case |
|
116 | + * (i.e. it has a nonstandard attribute on struct element tags) |
|
117 | + * - usage of javadoc @param tags using param names in a different order from the |
|
118 | + * function prototype is not considered valid (to be fixed?) |
|
119 | + * |
|
120 | + * Note that since rel. 2.0RC3 the preferred method to have the server call 'standard' |
|
121 | + * php functions (ie. functions not expecting a single xmlrpcmsg obj as parameter) |
|
122 | + * is by making use of the functions_parameters_type class member. |
|
123 | + * |
|
124 | + * @param string $funcname the name of the PHP user function to be exposed as xmlrpc method; array($obj, 'methodname') and array('class', 'methodname') are ok too |
|
125 | + * @param string $newfuncname (optional) name for function to be created |
|
126 | + * @param array $extra_options (optional) array of options for conversion. valid values include: |
|
127 | + * bool return_source when true, php code w. function definition will be returned, not evaluated |
|
128 | + * bool encode_php_objs let php objects be sent to server using the 'improved' xmlrpc notation, so server can deserialize them as php objects |
|
129 | + * bool decode_php_objs --- WARNING !!! possible security hazard. only use it with trusted servers --- |
|
130 | + * bool suppress_warnings remove from produced xml any runtime warnings due to the php function being invoked |
|
131 | + * @return false on error, or an array containing the name of the new php function, |
|
132 | + * its signature and docs, to be used in the server dispatch map |
|
133 | + * |
|
134 | + * @todo decide how to deal with params passed by ref: bomb out or allow? |
|
135 | + * @todo finish using javadoc info to build method sig if all params are named but out of order |
|
136 | + * @todo add a check for params of 'resource' type |
|
137 | + * @todo add some trigger_errors / error_log when returning false? |
|
138 | + * @todo what to do when the PHP function returns NULL? we are currently returning an empty string value... |
|
139 | + * @todo add an option to suppress php warnings in invocation of user function, similar to server debug level 3? |
|
140 | + * @todo if $newfuncname is empty, we could use create_user_func instead of eval, as it is possibly faster |
|
141 | + * @todo add a verbatim_object_copy parameter to allow avoiding the same obj instance? |
|
142 | + */ |
|
143 | + function wrap_php_function($funcname, $newfuncname='', $extra_options=array()) |
|
144 | + { |
|
145 | + $buildit = isset($extra_options['return_source']) ? !($extra_options['return_source']) : true; |
|
146 | + $prefix = isset($extra_options['prefix']) ? $extra_options['prefix'] : 'xmlrpc'; |
|
147 | + $encode_php_objects = isset($extra_options['encode_php_objs']) ? (bool)$extra_options['encode_php_objs'] : false; |
|
148 | + $decode_php_objects = isset($extra_options['decode_php_objs']) ? (bool)$extra_options['decode_php_objs'] : false; |
|
149 | + $catch_warnings = isset($extra_options['suppress_warnings']) && $extra_options['suppress_warnings'] ? '@' : ''; |
|
150 | + |
|
151 | + $exists = false; |
|
152 | + if (is_string($funcname) && strpos($funcname, '::') !== false) |
|
153 | + { |
|
154 | + $funcname = explode('::', $funcname); |
|
155 | + } |
|
156 | + if(is_array($funcname)) |
|
157 | + { |
|
158 | + if(count($funcname) < 2 || (!is_string($funcname[0]) && !is_object($funcname[0]))) |
|
159 | + { |
|
160 | + error_log('XML-RPC: syntax for function to be wrapped is wrong'); |
|
161 | + return false; |
|
162 | + } |
|
163 | + if(is_string($funcname[0])) |
|
164 | + { |
|
165 | + $plainfuncname = implode('::', $funcname); |
|
166 | + } |
|
167 | + elseif(is_object($funcname[0])) |
|
168 | + { |
|
169 | + $plainfuncname = get_class($funcname[0]) . '->' . $funcname[1]; |
|
170 | + } |
|
171 | + $exists = method_exists($funcname[0], $funcname[1]); |
|
172 | + } |
|
173 | + else |
|
174 | + { |
|
175 | + $plainfuncname = $funcname; |
|
176 | + $exists = function_exists($funcname); |
|
177 | + } |
|
178 | + |
|
179 | + if(!$exists) |
|
180 | + { |
|
181 | + error_log('XML-RPC: function to be wrapped is not defined: '.$plainfuncname); |
|
182 | + return false; |
|
183 | + } |
|
184 | + else |
|
185 | + { |
|
186 | + // determine name of new php function |
|
187 | + if($newfuncname == '') |
|
188 | + { |
|
189 | + if(is_array($funcname)) |
|
190 | + { |
|
191 | + if(is_string($funcname[0])) |
|
192 | + $xmlrpcfuncname = "{$prefix}_".implode('_', $funcname); |
|
193 | + else |
|
194 | + $xmlrpcfuncname = "{$prefix}_".get_class($funcname[0]) . '_' . $funcname[1]; |
|
195 | + } |
|
196 | + else |
|
197 | + { |
|
198 | + $xmlrpcfuncname = "{$prefix}_$funcname"; |
|
199 | + } |
|
200 | + } |
|
201 | + else |
|
202 | + { |
|
203 | + $xmlrpcfuncname = $newfuncname; |
|
204 | + } |
|
205 | + while($buildit && function_exists($xmlrpcfuncname)) |
|
206 | + { |
|
207 | + $xmlrpcfuncname .= 'x'; |
|
208 | + } |
|
209 | + |
|
210 | + // start to introspect PHP code |
|
211 | + if(is_array($funcname)) |
|
212 | + { |
|
213 | + $func = new ReflectionMethod($funcname[0], $funcname[1]); |
|
214 | + if($func->isPrivate()) |
|
215 | + { |
|
216 | + error_log('XML-RPC: method to be wrapped is private: '.$plainfuncname); |
|
217 | + return false; |
|
218 | + } |
|
219 | + if($func->isProtected()) |
|
220 | + { |
|
221 | + error_log('XML-RPC: method to be wrapped is protected: '.$plainfuncname); |
|
222 | + return false; |
|
223 | + } |
|
224 | + if($func->isConstructor()) |
|
225 | + { |
|
226 | + error_log('XML-RPC: method to be wrapped is the constructor: '.$plainfuncname); |
|
227 | + return false; |
|
228 | + } |
|
229 | + if($func->isDestructor()) |
|
230 | + { |
|
231 | + error_log('XML-RPC: method to be wrapped is the destructor: '.$plainfuncname); |
|
232 | + return false; |
|
233 | + } |
|
234 | + if($func->isAbstract()) |
|
235 | + { |
|
236 | + error_log('XML-RPC: method to be wrapped is abstract: '.$plainfuncname); |
|
237 | + return false; |
|
238 | + } |
|
239 | + /// @todo add more checks for static vs. nonstatic? |
|
240 | + } |
|
241 | + else |
|
242 | + { |
|
243 | + $func = new ReflectionFunction($funcname); |
|
244 | + } |
|
245 | + if($func->isInternal()) |
|
246 | + { |
|
247 | + // Note: from PHP 5.1.0 onward, we will possibly be able to use invokeargs |
|
248 | + // instead of getparameters to fully reflect internal php functions ? |
|
249 | + error_log('XML-RPC: function to be wrapped is internal: '.$plainfuncname); |
|
250 | + return false; |
|
251 | + } |
|
252 | + |
|
253 | + // retrieve parameter names, types and description from javadoc comments |
|
254 | + |
|
255 | + // function description |
|
256 | + $desc = ''; |
|
257 | + // type of return val: by default 'any' |
|
258 | + $returns = $GLOBALS['xmlrpcValue']; |
|
259 | + // desc of return val |
|
260 | + $returnsDocs = ''; |
|
261 | + // type + name of function parameters |
|
262 | + $paramDocs = array(); |
|
263 | + |
|
264 | + $docs = $func->getDocComment(); |
|
265 | + if($docs != '') |
|
266 | + { |
|
267 | + $docs = explode("\n", $docs); |
|
268 | + $i = 0; |
|
269 | + foreach($docs as $doc) |
|
270 | + { |
|
271 | + $doc = trim($doc, " \r\t/*"); |
|
272 | + if(strlen($doc) && strpos($doc, '@') !== 0 && !$i) |
|
273 | + { |
|
274 | + if($desc) |
|
275 | + { |
|
276 | + $desc .= "\n"; |
|
277 | + } |
|
278 | + $desc .= $doc; |
|
279 | + } |
|
280 | + elseif(strpos($doc, '@param') === 0) |
|
281 | + { |
|
282 | + // syntax: @param type [$name] desc |
|
283 | + if(preg_match('/@param\s+(\S+)(\s+\$\S+)?\s+(.+)/', $doc, $matches)) |
|
284 | + { |
|
285 | + if(strpos($matches[1], '|')) |
|
286 | + { |
|
287 | + //$paramDocs[$i]['type'] = explode('|', $matches[1]); |
|
288 | + $paramDocs[$i]['type'] = 'mixed'; |
|
289 | + } |
|
290 | + else |
|
291 | + { |
|
292 | + $paramDocs[$i]['type'] = $matches[1]; |
|
293 | + } |
|
294 | + $paramDocs[$i]['name'] = trim($matches[2]); |
|
295 | + $paramDocs[$i]['doc'] = $matches[3]; |
|
296 | + } |
|
297 | + $i++; |
|
298 | + } |
|
299 | + elseif(strpos($doc, '@return') === 0) |
|
300 | + { |
|
301 | + // syntax: @return type desc |
|
302 | + //$returns = preg_split('/\s+/', $doc); |
|
303 | + if(preg_match('/@return\s+(\S+)\s+(.+)/', $doc, $matches)) |
|
304 | + { |
|
305 | + $returns = php_2_xmlrpc_type($matches[1]); |
|
306 | + if(isset($matches[2])) |
|
307 | + { |
|
308 | + $returnsDocs = $matches[2]; |
|
309 | + } |
|
310 | + } |
|
311 | + } |
|
312 | + } |
|
313 | + } |
|
314 | + |
|
315 | + // execute introspection of actual function prototype |
|
316 | + $params = array(); |
|
317 | + $i = 0; |
|
318 | + foreach($func->getParameters() as $paramobj) |
|
319 | + { |
|
320 | + $params[$i] = array(); |
|
321 | + $params[$i]['name'] = '$'.$paramobj->getName(); |
|
322 | + $params[$i]['isoptional'] = $paramobj->isOptional(); |
|
323 | + $i++; |
|
324 | + } |
|
325 | + |
|
326 | + |
|
327 | + // start building of PHP code to be eval'd |
|
328 | + $innercode = ''; |
|
329 | + $i = 0; |
|
330 | + $parsvariations = array(); |
|
331 | + $pars = array(); |
|
332 | + $pnum = count($params); |
|
333 | + foreach($params as $param) |
|
334 | + { |
|
335 | + if (isset($paramDocs[$i]['name']) && $paramDocs[$i]['name'] && strtolower($paramDocs[$i]['name']) != strtolower($param['name'])) |
|
336 | + { |
|
337 | + // param name from phpdoc info does not match param definition! |
|
338 | + $paramDocs[$i]['type'] = 'mixed'; |
|
339 | + } |
|
340 | + |
|
341 | + if($param['isoptional']) |
|
342 | + { |
|
343 | + // this particular parameter is optional. save as valid previous list of parameters |
|
344 | + $innercode .= "if (\$paramcount > $i) {\n"; |
|
345 | + $parsvariations[] = $pars; |
|
346 | + } |
|
347 | + $innercode .= "\$p$i = \$msg->getParam($i);\n"; |
|
348 | + if ($decode_php_objects) |
|
349 | + { |
|
350 | + $innercode .= "if (\$p{$i}->kindOf() == 'scalar') \$p$i = \$p{$i}->scalarval(); else \$p$i = php_{$prefix}_decode(\$p$i, array('decode_php_objs'));\n"; |
|
351 | + } |
|
352 | + else |
|
353 | + { |
|
354 | + $innercode .= "if (\$p{$i}->kindOf() == 'scalar') \$p$i = \$p{$i}->scalarval(); else \$p$i = php_{$prefix}_decode(\$p$i);\n"; |
|
355 | + } |
|
356 | + |
|
357 | + $pars[] = "\$p$i"; |
|
358 | + $i++; |
|
359 | + if($param['isoptional']) |
|
360 | + { |
|
361 | + $innercode .= "}\n"; |
|
362 | + } |
|
363 | + if($i == $pnum) |
|
364 | + { |
|
365 | + // last allowed parameters combination |
|
366 | + $parsvariations[] = $pars; |
|
367 | + } |
|
368 | + } |
|
369 | + |
|
370 | + $sigs = array(); |
|
371 | + $psigs = array(); |
|
372 | + if(count($parsvariations) == 0) |
|
373 | + { |
|
374 | + // only known good synopsis = no parameters |
|
375 | + $parsvariations[] = array(); |
|
376 | + $minpars = 0; |
|
377 | + } |
|
378 | + else |
|
379 | + { |
|
380 | + $minpars = count($parsvariations[0]); |
|
381 | + } |
|
382 | + |
|
383 | + if($minpars) |
|
384 | + { |
|
385 | + // add to code the check for min params number |
|
386 | + // NB: this check needs to be done BEFORE decoding param values |
|
387 | + $innercode = "\$paramcount = \$msg->getNumParams();\n" . |
|
388 | + "if (\$paramcount < $minpars) return new {$prefix}resp(0, {$GLOBALS['xmlrpcerr']['incorrect_params']}, '{$GLOBALS['xmlrpcstr']['incorrect_params']}');\n" . $innercode; |
|
389 | + } |
|
390 | + else |
|
391 | + { |
|
392 | + $innercode = "\$paramcount = \$msg->getNumParams();\n" . $innercode; |
|
393 | + } |
|
394 | + |
|
395 | + $innercode .= "\$np = false;\n"; |
|
396 | + // since there are no closures in php, if we are given an object instance, |
|
397 | + // we store a pointer to it in a global var... |
|
398 | + if ( is_array($funcname) && is_object($funcname[0]) ) |
|
399 | + { |
|
400 | + $GLOBALS['xmlrpcWPFObjHolder'][$xmlrpcfuncname] =& $funcname[0]; |
|
401 | + $innercode .= "\$obj =& \$GLOBALS['xmlrpcWPFObjHolder']['$xmlrpcfuncname'];\n"; |
|
402 | + $realfuncname = '$obj->'.$funcname[1]; |
|
403 | + } |
|
404 | + else |
|
405 | + { |
|
406 | + $realfuncname = $plainfuncname; |
|
407 | + } |
|
408 | + foreach($parsvariations as $pars) |
|
409 | + { |
|
410 | + $innercode .= "if (\$paramcount == " . count($pars) . ") \$retval = {$catch_warnings}$realfuncname(" . implode(',', $pars) . "); else\n"; |
|
411 | + // build a 'generic' signature (only use an appropriate return type) |
|
412 | + $sig = array($returns); |
|
413 | + $psig = array($returnsDocs); |
|
414 | + for($i=0; $i < count($pars); $i++) |
|
415 | + { |
|
416 | + if (isset($paramDocs[$i]['type'])) |
|
417 | + { |
|
418 | + $sig[] = php_2_xmlrpc_type($paramDocs[$i]['type']); |
|
419 | + } |
|
420 | + else |
|
421 | + { |
|
422 | + $sig[] = $GLOBALS['xmlrpcValue']; |
|
423 | + } |
|
424 | + $psig[] = isset($paramDocs[$i]['doc']) ? $paramDocs[$i]['doc'] : ''; |
|
425 | + } |
|
426 | + $sigs[] = $sig; |
|
427 | + $psigs[] = $psig; |
|
428 | + } |
|
429 | + $innercode .= "\$np = true;\n"; |
|
430 | + $innercode .= "if (\$np) return new {$prefix}resp(0, {$GLOBALS['xmlrpcerr']['incorrect_params']}, '{$GLOBALS['xmlrpcstr']['incorrect_params']}'); else {\n"; |
|
431 | + //$innercode .= "if (\$_xmlrpcs_error_occurred) return new xmlrpcresp(0, $GLOBALS['xmlrpcerr']user, \$_xmlrpcs_error_occurred); else\n"; |
|
432 | + $innercode .= "if (is_a(\$retval, '{$prefix}resp')) return \$retval; else\n"; |
|
433 | + if($returns == $GLOBALS['xmlrpcDateTime'] || $returns == $GLOBALS['xmlrpcBase64']) |
|
434 | + { |
|
435 | + $innercode .= "return new {$prefix}resp(new {$prefix}val(\$retval, '$returns'));"; |
|
436 | + } |
|
437 | + else |
|
438 | + { |
|
439 | + if ($encode_php_objects) |
|
440 | + $innercode .= "return new {$prefix}resp(php_{$prefix}_encode(\$retval, array('encode_php_objs')));\n"; |
|
441 | + else |
|
442 | + $innercode .= "return new {$prefix}resp(php_{$prefix}_encode(\$retval));\n"; |
|
443 | + } |
|
444 | + // shall we exclude functions returning by ref? |
|
445 | + // if($func->returnsReference()) |
|
446 | + // return false; |
|
447 | + $code = "function $xmlrpcfuncname(\$msg) {\n" . $innercode . "}\n}"; |
|
448 | + //print_r($code); |
|
449 | + if ($buildit) |
|
450 | + { |
|
451 | + $allOK = 0; |
|
452 | + eval($code.'$allOK=1;'); |
|
453 | + // alternative |
|
454 | + //$xmlrpcfuncname = create_function('$m', $innercode); |
|
455 | + |
|
456 | + if(!$allOK) |
|
457 | + { |
|
458 | + error_log('XML-RPC: could not create function '.$xmlrpcfuncname.' to wrap php function '.$plainfuncname); |
|
459 | + return false; |
|
460 | + } |
|
461 | + } |
|
462 | + |
|
463 | + /// @todo examine if $paramDocs matches $parsvariations and build array for |
|
464 | + /// usage as method signature, plus put together a nice string for docs |
|
465 | + |
|
466 | + $ret = array('function' => $xmlrpcfuncname, 'signature' => $sigs, 'docstring' => $desc, 'signature_docs' => $psigs, 'source' => $code); |
|
467 | + return $ret; |
|
468 | + } |
|
469 | + } |
|
470 | + |
|
471 | + /** |
|
472 | + * Given a user-defined PHP class or php object, map its methods onto a list of |
|
473 | + * PHP 'wrapper' functions that can be exposed as xmlrpc methods from an xmlrpc_server |
|
474 | + * object and called from remote clients (as well as their corresponding signature info). |
|
475 | + * |
|
476 | + * @param mixed $classname the name of the class whose methods are to be exposed as xmlrpc methods, or an object instance of that class |
|
477 | + * @param array $extra_options see the docs for wrap_php_method for more options |
|
478 | + * string method_type 'static', 'nonstatic', 'all' and 'auto' (default); the latter will switch between static and non-static depending on wheter $classname is a class name or object instance |
|
479 | + * @return array or false on failure |
|
480 | + * |
|
481 | + * @todo get_class_methods will return both static and non-static methods. |
|
482 | + * we have to differentiate the action, depending on whether we received a class name or object |
|
483 | + */ |
|
484 | + function wrap_php_class($classname, $extra_options=array()) |
|
485 | + { |
|
486 | + $methodfilter = isset($extra_options['method_filter']) ? $extra_options['method_filter'] : ''; |
|
487 | + $methodtype = isset($extra_options['method_type']) ? $extra_options['method_type'] : 'auto'; |
|
488 | + |
|
489 | + $result = array(); |
|
490 | + $mlist = get_class_methods($classname); |
|
491 | + foreach($mlist as $mname) |
|
492 | + { |
|
493 | + if ($methodfilter == '' || preg_match($methodfilter, $mname)) |
|
494 | + { |
|
495 | + // echo $mlist."\n"; |
|
496 | + $func = new ReflectionMethod($classname, $mname); |
|
497 | + if(!$func->isPrivate() && !$func->isProtected() && !$func->isConstructor() && !$func->isDestructor() && !$func->isAbstract()) |
|
498 | + { |
|
499 | + if(($func->isStatic && ($methodtype == 'all' || $methodtype == 'static' || ($methodtype == 'auto' && is_string($classname)))) || |
|
500 | + (!$func->isStatic && ($methodtype == 'all' || $methodtype == 'nonstatic' || ($methodtype == 'auto' && is_object($classname))))) |
|
501 | + { |
|
502 | + $methodwrap = wrap_php_function(array($classname, $mname), '', $extra_options); |
|
503 | + if ( $methodwrap ) |
|
504 | + { |
|
505 | + $result[$methodwrap['function']] = $methodwrap['function']; |
|
506 | + } |
|
507 | + } |
|
508 | + } |
|
509 | + } |
|
510 | + } |
|
511 | + return $result; |
|
512 | + } |
|
513 | + |
|
514 | + /** |
|
515 | + * Given an xmlrpc client and a method name, register a php wrapper function |
|
516 | + * that will call it and return results using native php types for both |
|
517 | + * params and results. The generated php function will return an xmlrpcresp |
|
518 | + * object for failed xmlrpc calls |
|
519 | + * |
|
520 | + * Known limitations: |
|
521 | + * - server must support system.methodsignature for the wanted xmlrpc method |
|
522 | + * - for methods that expose many signatures, only one can be picked (we |
|
523 | + * could in principle check if signatures differ only by number of params |
|
524 | + * and not by type, but it would be more complication than we can spare time) |
|
525 | + * - nested xmlrpc params: the caller of the generated php function has to |
|
526 | + * encode on its own the params passed to the php function if these are structs |
|
527 | + * or arrays whose (sub)members include values of type datetime or base64 |
|
528 | + * |
|
529 | + * Notes: the connection properties of the given client will be copied |
|
530 | + * and reused for the connection used during the call to the generated |
|
531 | + * php function. |
|
532 | + * Calling the generated php function 'might' be slow: a new xmlrpc client |
|
533 | + * is created on every invocation and an xmlrpc-connection opened+closed. |
|
534 | + * An extra 'debug' param is appended to param list of xmlrpc method, useful |
|
535 | + * for debugging purposes. |
|
536 | + * |
|
537 | + * @param xmlrpc_client $client an xmlrpc client set up correctly to communicate with target server |
|
538 | + * @param string $methodname the xmlrpc method to be mapped to a php function |
|
539 | + * @param array $extra_options array of options that specify conversion details. valid options include |
|
540 | + * integer signum the index of the method signature to use in mapping (if method exposes many sigs) |
|
541 | + * integer timeout timeout (in secs) to be used when executing function/calling remote method |
|
542 | + * string protocol 'http' (default), 'http11' or 'https' |
|
543 | + * string new_function_name the name of php function to create. If unspecified, lib will pick an appropriate name |
|
544 | + * string return_source if true return php code w. function definition instead fo function name |
|
545 | + * bool encode_php_objs let php objects be sent to server using the 'improved' xmlrpc notation, so server can deserialize them as php objects |
|
546 | + * bool decode_php_objs --- WARNING !!! possible security hazard. only use it with trusted servers --- |
|
547 | + * mixed return_on_fault a php value to be returned when the xmlrpc call fails/returns a fault response (by default the xmlrpcresp object is returned in this case). If a string is used, '%faultCode%' and '%faultString%' tokens will be substituted with actual error values |
|
548 | + * bool debug set it to 1 or 2 to see debug results of querying server for method synopsis |
|
549 | + * @return string the name of the generated php function (or false) - OR AN ARRAY... |
|
550 | + */ |
|
551 | + function wrap_xmlrpc_method($client, $methodname, $extra_options=0, $timeout=0, $protocol='', $newfuncname='') |
|
552 | + { |
|
553 | + // mind numbing: let caller use sane calling convention (as per javadoc, 3 params), |
|
554 | + // OR the 2.0 calling convention (no options) - we really love backward compat, don't we? |
|
555 | + if (!is_array($extra_options)) |
|
556 | + { |
|
557 | + $signum = $extra_options; |
|
558 | + $extra_options = array(); |
|
559 | + } |
|
560 | + else |
|
561 | + { |
|
562 | + $signum = isset($extra_options['signum']) ? (int)$extra_options['signum'] : 0; |
|
563 | + $timeout = isset($extra_options['timeout']) ? (int)$extra_options['timeout'] : 0; |
|
564 | + $protocol = isset($extra_options['protocol']) ? $extra_options['protocol'] : ''; |
|
565 | + $newfuncname = isset($extra_options['new_function_name']) ? $extra_options['new_function_name'] : ''; |
|
566 | + } |
|
567 | + //$encode_php_objects = in_array('encode_php_objects', $extra_options); |
|
568 | + //$verbatim_client_copy = in_array('simple_client_copy', $extra_options) ? 1 : |
|
569 | + // in_array('build_class_code', $extra_options) ? 2 : 0; |
|
570 | + |
|
571 | + $encode_php_objects = isset($extra_options['encode_php_objs']) ? (bool)$extra_options['encode_php_objs'] : false; |
|
572 | + $decode_php_objects = isset($extra_options['decode_php_objs']) ? (bool)$extra_options['decode_php_objs'] : false; |
|
573 | + // it seems like the meaning of 'simple_client_copy' here is swapped wrt client_copy_mode later on... |
|
574 | + $simple_client_copy = isset($extra_options['simple_client_copy']) ? (int)($extra_options['simple_client_copy']) : 0; |
|
575 | + $buildit = isset($extra_options['return_source']) ? !($extra_options['return_source']) : true; |
|
576 | + $prefix = isset($extra_options['prefix']) ? $extra_options['prefix'] : 'xmlrpc'; |
|
577 | + if (isset($extra_options['return_on_fault'])) |
|
578 | + { |
|
579 | + $decode_fault = true; |
|
580 | + $fault_response = $extra_options['return_on_fault']; |
|
581 | + } |
|
582 | + else |
|
583 | + { |
|
584 | + $decode_fault = false; |
|
585 | + $fault_response = ''; |
|
586 | + } |
|
587 | + $debug = isset($extra_options['debug']) ? ($extra_options['debug']) : 0; |
|
588 | + |
|
589 | + $msgclass = $prefix.'msg'; |
|
590 | + $valclass = $prefix.'val'; |
|
591 | + $decodefunc = 'php_'.$prefix.'_decode'; |
|
592 | + |
|
593 | + $msg = new $msgclass('system.methodSignature'); |
|
594 | + $msg->addparam(new $valclass($methodname)); |
|
595 | + $client->setDebug($debug); |
|
596 | + $response =& $client->send($msg, $timeout, $protocol); |
|
597 | + if($response->faultCode()) |
|
598 | + { |
|
599 | + error_log('XML-RPC: could not retrieve method signature from remote server for method '.$methodname); |
|
600 | + return false; |
|
601 | + } |
|
602 | + else |
|
603 | + { |
|
604 | + $msig = $response->value(); |
|
605 | + if ($client->return_type != 'phpvals') |
|
606 | + { |
|
607 | + $msig = $decodefunc($msig); |
|
608 | + } |
|
609 | + if(!is_array($msig) || count($msig) <= $signum) |
|
610 | + { |
|
611 | + error_log('XML-RPC: could not retrieve method signature nr.'.$signum.' from remote server for method '.$methodname); |
|
612 | + return false; |
|
613 | + } |
|
614 | + else |
|
615 | + { |
|
616 | + // pick a suitable name for the new function, avoiding collisions |
|
617 | + if($newfuncname != '') |
|
618 | + { |
|
619 | + $xmlrpcfuncname = $newfuncname; |
|
620 | + } |
|
621 | + else |
|
622 | + { |
|
623 | + // take care to insure that methodname is translated to valid |
|
624 | + // php function name |
|
625 | + $xmlrpcfuncname = $prefix.'_'.preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), |
|
626 | + array('_', ''), $methodname); |
|
627 | + } |
|
628 | + while($buildit && function_exists($xmlrpcfuncname)) |
|
629 | + { |
|
630 | + $xmlrpcfuncname .= 'x'; |
|
631 | + } |
|
632 | + |
|
633 | + $msig = $msig[$signum]; |
|
634 | + $mdesc = ''; |
|
635 | + // if in 'offline' mode, get method description too. |
|
636 | + // in online mode, favour speed of operation |
|
637 | + if(!$buildit) |
|
638 | + { |
|
639 | + $msg = new $msgclass('system.methodHelp'); |
|
640 | + $msg->addparam(new $valclass($methodname)); |
|
641 | + $response =& $client->send($msg, $timeout, $protocol); |
|
642 | + if (!$response->faultCode()) |
|
643 | + { |
|
644 | + $mdesc = $response->value(); |
|
645 | + if ($client->return_type != 'phpvals') |
|
646 | + { |
|
647 | + $mdesc = $mdesc->scalarval(); |
|
648 | + } |
|
649 | + } |
|
650 | + } |
|
651 | + |
|
652 | + $results = build_remote_method_wrapper_code($client, $methodname, |
|
653 | + $xmlrpcfuncname, $msig, $mdesc, $timeout, $protocol, $simple_client_copy, |
|
654 | + $prefix, $decode_php_objects, $encode_php_objects, $decode_fault, |
|
655 | + $fault_response); |
|
656 | + |
|
657 | + //print_r($code); |
|
658 | + if ($buildit) |
|
659 | + { |
|
660 | + $allOK = 0; |
|
661 | + eval($results['source'].'$allOK=1;'); |
|
662 | + // alternative |
|
663 | + //$xmlrpcfuncname = create_function('$m', $innercode); |
|
664 | + if($allOK) |
|
665 | + { |
|
666 | + return $xmlrpcfuncname; |
|
667 | + } |
|
668 | + else |
|
669 | + { |
|
670 | + error_log('XML-RPC: could not create function '.$xmlrpcfuncname.' to wrap remote method '.$methodname); |
|
671 | + return false; |
|
672 | + } |
|
673 | + } |
|
674 | + else |
|
675 | + { |
|
676 | + $results['function'] = $xmlrpcfuncname; |
|
677 | + return $results; |
|
678 | + } |
|
679 | + } |
|
680 | + } |
|
681 | + } |
|
682 | + |
|
683 | + /** |
|
684 | + * Similar to wrap_xmlrpc_method, but will generate a php class that wraps |
|
685 | + * all xmlrpc methods exposed by the remote server as own methods. |
|
686 | + * For more details see wrap_xmlrpc_method. |
|
687 | + * @param xmlrpc_client $client the client obj all set to query the desired server |
|
688 | + * @param array $extra_options list of options for wrapped code |
|
689 | + * @return mixed false on error, the name of the created class if all ok or an array with code, class name and comments (if the appropriatevoption is set in extra_options) |
|
690 | + */ |
|
691 | + function wrap_xmlrpc_server($client, $extra_options=array()) |
|
692 | + { |
|
693 | + $methodfilter = isset($extra_options['method_filter']) ? $extra_options['method_filter'] : ''; |
|
694 | + //$signum = isset($extra_options['signum']) ? (int)$extra_options['signum'] : 0; |
|
695 | + $timeout = isset($extra_options['timeout']) ? (int)$extra_options['timeout'] : 0; |
|
696 | + $protocol = isset($extra_options['protocol']) ? $extra_options['protocol'] : ''; |
|
697 | + $newclassname = isset($extra_options['new_class_name']) ? $extra_options['new_class_name'] : ''; |
|
698 | + $encode_php_objects = isset($extra_options['encode_php_objs']) ? (bool)$extra_options['encode_php_objs'] : false; |
|
699 | + $decode_php_objects = isset($extra_options['decode_php_objs']) ? (bool)$extra_options['decode_php_objs'] : false; |
|
700 | + $verbatim_client_copy = isset($extra_options['simple_client_copy']) ? !($extra_options['simple_client_copy']) : true; |
|
701 | + $buildit = isset($extra_options['return_source']) ? !($extra_options['return_source']) : true; |
|
702 | + $prefix = isset($extra_options['prefix']) ? $extra_options['prefix'] : 'xmlrpc'; |
|
703 | + |
|
704 | + $msgclass = $prefix.'msg'; |
|
705 | + //$valclass = $prefix.'val'; |
|
706 | + $decodefunc = 'php_'.$prefix.'_decode'; |
|
707 | + |
|
708 | + $msg = new $msgclass('system.listMethods'); |
|
709 | + $response =& $client->send($msg, $timeout, $protocol); |
|
710 | + if($response->faultCode()) |
|
711 | + { |
|
712 | + error_log('XML-RPC: could not retrieve method list from remote server'); |
|
713 | + return false; |
|
714 | + } |
|
715 | + else |
|
716 | + { |
|
717 | + $mlist = $response->value(); |
|
718 | + if ($client->return_type != 'phpvals') |
|
719 | + { |
|
720 | + $mlist = $decodefunc($mlist); |
|
721 | + } |
|
722 | + if(!is_array($mlist) || !count($mlist)) |
|
723 | + { |
|
724 | + error_log('XML-RPC: could not retrieve meaningful method list from remote server'); |
|
725 | + return false; |
|
726 | + } |
|
727 | + else |
|
728 | + { |
|
729 | + // pick a suitable name for the new function, avoiding collisions |
|
730 | + if($newclassname != '') |
|
731 | + { |
|
732 | + $xmlrpcclassname = $newclassname; |
|
733 | + } |
|
734 | + else |
|
735 | + { |
|
736 | + $xmlrpcclassname = $prefix.'_'.preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), |
|
737 | + array('_', ''), $client->server).'_client'; |
|
738 | + } |
|
739 | + while($buildit && class_exists($xmlrpcclassname)) |
|
740 | + { |
|
741 | + $xmlrpcclassname .= 'x'; |
|
742 | + } |
|
743 | + |
|
744 | + /// @todo add function setdebug() to new class, to enable/disable debugging |
|
745 | + $source = "class $xmlrpcclassname\n{\nvar \$client;\n\n"; |
|
746 | + $source .= "function $xmlrpcclassname()\n{\n"; |
|
747 | + $source .= build_client_wrapper_code($client, $verbatim_client_copy, $prefix); |
|
748 | + $source .= "\$this->client =& \$client;\n}\n\n"; |
|
749 | + $opts = array('simple_client_copy' => 2, 'return_source' => true, |
|
750 | + 'timeout' => $timeout, 'protocol' => $protocol, |
|
751 | + 'encode_php_objs' => $encode_php_objects, 'prefix' => $prefix, |
|
752 | + 'decode_php_objs' => $decode_php_objects |
|
753 | + ); |
|
754 | + /// @todo build javadoc for class definition, too |
|
755 | + foreach($mlist as $mname) |
|
756 | + { |
|
757 | + if ($methodfilter == '' || preg_match($methodfilter, $mname)) |
|
758 | + { |
|
759 | + $opts['new_function_name'] = preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), |
|
760 | + array('_', ''), $mname); |
|
761 | + $methodwrap = wrap_xmlrpc_method($client, $mname, $opts); |
|
762 | + if ($methodwrap) |
|
763 | + { |
|
764 | + if (!$buildit) |
|
765 | + { |
|
766 | + $source .= $methodwrap['docstring']; |
|
767 | + } |
|
768 | + $source .= $methodwrap['source']."\n"; |
|
769 | + } |
|
770 | + else |
|
771 | + { |
|
772 | + error_log('XML-RPC: will not create class method to wrap remote method '.$mname); |
|
773 | + } |
|
774 | + } |
|
775 | + } |
|
776 | + $source .= "}\n"; |
|
777 | + if ($buildit) |
|
778 | + { |
|
779 | + $allOK = 0; |
|
780 | + eval($source.'$allOK=1;'); |
|
781 | + // alternative |
|
782 | + //$xmlrpcfuncname = create_function('$m', $innercode); |
|
783 | + if($allOK) |
|
784 | + { |
|
785 | + return $xmlrpcclassname; |
|
786 | + } |
|
787 | + else |
|
788 | + { |
|
789 | + error_log('XML-RPC: could not create class '.$xmlrpcclassname.' to wrap remote server '.$client->server); |
|
790 | + return false; |
|
791 | + } |
|
792 | + } |
|
793 | + else |
|
794 | + { |
|
795 | + return array('class' => $xmlrpcclassname, 'code' => $source, 'docstring' => ''); |
|
796 | + } |
|
797 | + } |
|
798 | + } |
|
799 | + } |
|
800 | + |
|
801 | + /** |
|
802 | + * Given the necessary info, build php code that creates a new function to |
|
803 | + * invoke a remote xmlrpc method. |
|
804 | + * Take care that no full checking of input parameters is done to ensure that |
|
805 | + * valid php code is emitted. |
|
806 | + * Note: real spaghetti code follows... |
|
807 | + * @access private |
|
808 | + */ |
|
809 | + function build_remote_method_wrapper_code($client, $methodname, $xmlrpcfuncname, |
|
810 | + $msig, $mdesc='', $timeout=0, $protocol='', $client_copy_mode=0, $prefix='xmlrpc', |
|
811 | + $decode_php_objects=false, $encode_php_objects=false, $decode_fault=false, |
|
812 | + $fault_response='') |
|
813 | + { |
|
814 | + $code = "function $xmlrpcfuncname ("; |
|
815 | + if ($client_copy_mode < 2) |
|
816 | + { |
|
817 | + // client copy mode 0 or 1 == partial / full client copy in emitted code |
|
818 | + $innercode = build_client_wrapper_code($client, $client_copy_mode, $prefix); |
|
819 | + $innercode .= "\$client->setDebug(\$debug);\n"; |
|
820 | + $this_ = ''; |
|
821 | + } |
|
822 | + else |
|
823 | + { |
|
824 | + // client copy mode 2 == no client copy in emitted code |
|
825 | + $innercode = ''; |
|
826 | + $this_ = 'this->'; |
|
827 | + } |
|
828 | + $innercode .= "\$msg = new {$prefix}msg('$methodname');\n"; |
|
829 | + |
|
830 | + if ($mdesc != '') |
|
831 | + { |
|
832 | + // take care that PHP comment is not terminated unwillingly by method description |
|
833 | + $mdesc = "/**\n* ".str_replace('*/', '* /', $mdesc)."\n"; |
|
834 | + } |
|
835 | + else |
|
836 | + { |
|
837 | + $mdesc = "/**\nFunction $xmlrpcfuncname\n"; |
|
838 | + } |
|
839 | + |
|
840 | + // param parsing |
|
841 | + $plist = array(); |
|
842 | + $pcount = count($msig); |
|
843 | + for($i = 1; $i < $pcount; $i++) |
|
844 | + { |
|
845 | + $plist[] = "\$p$i"; |
|
846 | + $ptype = $msig[$i]; |
|
847 | + if($ptype == 'i4' || $ptype == 'int' || $ptype == 'boolean' || $ptype == 'double' || |
|
848 | + $ptype == 'string' || $ptype == 'dateTime.iso8601' || $ptype == 'base64' || $ptype == 'null') |
|
849 | + { |
|
850 | + // only build directly xmlrpcvals when type is known and scalar |
|
851 | + $innercode .= "\$p$i = new {$prefix}val(\$p$i, '$ptype');\n"; |
|
852 | + } |
|
853 | + else |
|
854 | + { |
|
855 | + if ($encode_php_objects) |
|
856 | + { |
|
857 | + $innercode .= "\$p$i =& php_{$prefix}_encode(\$p$i, array('encode_php_objs'));\n"; |
|
858 | + } |
|
859 | + else |
|
860 | + { |
|
861 | + $innercode .= "\$p$i =& php_{$prefix}_encode(\$p$i);\n"; |
|
862 | + } |
|
863 | + } |
|
864 | + $innercode .= "\$msg->addparam(\$p$i);\n"; |
|
865 | + $mdesc .= '* @param '.xmlrpc_2_php_type($ptype)." \$p$i\n"; |
|
866 | + } |
|
867 | + if ($client_copy_mode < 2) |
|
868 | + { |
|
869 | + $plist[] = '$debug=0'; |
|
870 | + $mdesc .= "* @param int \$debug when 1 (or 2) will enable debugging of the underlying {$prefix} call (defaults to 0)\n"; |
|
871 | + } |
|
872 | + $plist = implode(', ', $plist); |
|
873 | + $mdesc .= '* @return '.xmlrpc_2_php_type($msig[0])." (or an {$prefix}resp obj instance if call fails)\n*/\n"; |
|
874 | + |
|
875 | + $innercode .= "\$res =& \${$this_}client->send(\$msg, $timeout, '$protocol');\n"; |
|
876 | + if ($decode_fault) |
|
877 | + { |
|
878 | + if (is_string($fault_response) && ((strpos($fault_response, '%faultCode%') !== false) || (strpos($fault_response, '%faultString%') !== false))) |
|
879 | + { |
|
880 | + $respcode = "str_replace(array('%faultCode%', '%faultString%'), array(\$res->faultCode(), \$res->faultString()), '".str_replace("'", "''", $fault_response)."')"; |
|
881 | + } |
|
882 | + else |
|
883 | + { |
|
884 | + $respcode = var_export($fault_response, true); |
|
885 | + } |
|
886 | + } |
|
887 | + else |
|
888 | + { |
|
889 | + $respcode = '$res'; |
|
890 | + } |
|
891 | + if ($decode_php_objects) |
|
892 | + { |
|
893 | + $innercode .= "if (\$res->faultcode()) return $respcode; else return php_{$prefix}_decode(\$res->value(), array('decode_php_objs'));"; |
|
894 | + } |
|
895 | + else |
|
896 | + { |
|
897 | + $innercode .= "if (\$res->faultcode()) return $respcode; else return php_{$prefix}_decode(\$res->value());"; |
|
898 | + } |
|
899 | + |
|
900 | + $code = $code . $plist. ") {\n" . $innercode . "\n}\n"; |
|
901 | + |
|
902 | + return array('source' => $code, 'docstring' => $mdesc); |
|
903 | + } |
|
904 | + |
|
905 | + /** |
|
906 | + * Given necessary info, generate php code that will rebuild a client object |
|
907 | + * Take care that no full checking of input parameters is done to ensure that |
|
908 | + * valid php code is emitted. |
|
909 | + * @access private |
|
910 | + */ |
|
911 | + function build_client_wrapper_code($client, $verbatim_client_copy, $prefix='xmlrpc') |
|
912 | + { |
|
913 | + $code = "\$client = new {$prefix}_client('".str_replace("'", "\'", $client->path). |
|
914 | + "', '" . str_replace("'", "\'", $client->server) . "', $client->port);\n"; |
|
915 | + |
|
916 | + // copy all client fields to the client that will be generated runtime |
|
917 | + // (this provides for future expansion or subclassing of client obj) |
|
918 | + if ($verbatim_client_copy) |
|
919 | + { |
|
920 | + foreach($client as $fld => $val) |
|
921 | + { |
|
922 | + if($fld != 'debug' && $fld != 'return_type') |
|
923 | + { |
|
924 | + $val = var_export($val, true); |
|
925 | + $code .= "\$client->$fld = $val;\n"; |
|
926 | + } |
|
927 | + } |
|
928 | + } |
|
929 | + // only make sure that client always returns the correct data type |
|
930 | + $code .= "\$client->return_type = '{$prefix}vals';\n"; |
|
931 | + //$code .= "\$client->setDebug(\$debug);\n"; |
|
932 | + return $code; |
|
933 | + } |
|
934 | 934 | ?> |
935 | 935 | \ No newline at end of file |
@@ -34,98 +34,98 @@ discard block |
||
34 | 34 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
35 | 35 | // OF THE POSSIBILITY OF SUCH DAMAGE. |
36 | 36 | |
37 | - if(!function_exists('xml_parser_create')) |
|
38 | - { |
|
39 | - // For PHP 4 onward, XML functionality is always compiled-in on windows: |
|
40 | - // no more need to dl-open it. It might have been compiled out on *nix... |
|
41 | - if(strtoupper(substr(PHP_OS, 0, 3) != 'WIN')) |
|
42 | - { |
|
43 | - dl('xml.so'); |
|
44 | - } |
|
45 | - } |
|
46 | - |
|
47 | - // G. Giunta 2005/01/29: declare global these variables, |
|
48 | - // so that xmlrpc.inc will work even if included from within a function |
|
49 | - // Milosch: 2005/08/07 - explicitly request these via $GLOBALS where used. |
|
50 | - $GLOBALS['xmlrpcI4']='i4'; |
|
51 | - $GLOBALS['xmlrpcInt']='int'; |
|
52 | - $GLOBALS['xmlrpcBoolean']='boolean'; |
|
53 | - $GLOBALS['xmlrpcDouble']='double'; |
|
54 | - $GLOBALS['xmlrpcString']='string'; |
|
55 | - $GLOBALS['xmlrpcDateTime']='dateTime.iso8601'; |
|
56 | - $GLOBALS['xmlrpcBase64']='base64'; |
|
57 | - $GLOBALS['xmlrpcArray']='array'; |
|
58 | - $GLOBALS['xmlrpcStruct']='struct'; |
|
59 | - $GLOBALS['xmlrpcValue']='undefined'; |
|
60 | - |
|
61 | - $GLOBALS['xmlrpcTypes']=array( |
|
62 | - $GLOBALS['xmlrpcI4'] => 1, |
|
63 | - $GLOBALS['xmlrpcInt'] => 1, |
|
64 | - $GLOBALS['xmlrpcBoolean'] => 1, |
|
65 | - $GLOBALS['xmlrpcString'] => 1, |
|
66 | - $GLOBALS['xmlrpcDouble'] => 1, |
|
67 | - $GLOBALS['xmlrpcDateTime'] => 1, |
|
68 | - $GLOBALS['xmlrpcBase64'] => 1, |
|
69 | - $GLOBALS['xmlrpcArray'] => 2, |
|
70 | - $GLOBALS['xmlrpcStruct'] => 3 |
|
71 | - ); |
|
72 | - |
|
73 | - $GLOBALS['xmlrpc_valid_parents'] = array( |
|
74 | - 'VALUE' => array('MEMBER', 'DATA', 'PARAM', 'FAULT'), |
|
75 | - 'BOOLEAN' => array('VALUE'), |
|
76 | - 'I4' => array('VALUE'), |
|
77 | - 'INT' => array('VALUE'), |
|
78 | - 'STRING' => array('VALUE'), |
|
79 | - 'DOUBLE' => array('VALUE'), |
|
80 | - 'DATETIME.ISO8601' => array('VALUE'), |
|
81 | - 'BASE64' => array('VALUE'), |
|
82 | - 'MEMBER' => array('STRUCT'), |
|
83 | - 'NAME' => array('MEMBER'), |
|
84 | - 'DATA' => array('ARRAY'), |
|
85 | - 'ARRAY' => array('VALUE'), |
|
86 | - 'STRUCT' => array('VALUE'), |
|
87 | - 'PARAM' => array('PARAMS'), |
|
88 | - 'METHODNAME' => array('METHODCALL'), |
|
89 | - 'PARAMS' => array('METHODCALL', 'METHODRESPONSE'), |
|
90 | - 'FAULT' => array('METHODRESPONSE'), |
|
91 | - 'NIL' => array('VALUE'), // only used when extension activated |
|
92 | - 'EX:NIL' => array('VALUE') // only used when extension activated |
|
93 | - ); |
|
94 | - |
|
95 | - // define extra types for supporting NULL (useful for json or <NIL/>) |
|
96 | - $GLOBALS['xmlrpcNull']='null'; |
|
97 | - $GLOBALS['xmlrpcTypes']['null']=1; |
|
98 | - |
|
99 | - // Not in use anymore since 2.0. Shall we remove it? |
|
100 | - /// @deprecated |
|
101 | - $GLOBALS['xmlEntities']=array( |
|
102 | - 'amp' => '&', |
|
103 | - 'quot' => '"', |
|
104 | - 'lt' => '<', |
|
105 | - 'gt' => '>', |
|
106 | - 'apos' => "'" |
|
107 | - ); |
|
108 | - |
|
109 | - // tables used for transcoding different charsets into us-ascii xml |
|
110 | - |
|
111 | - $GLOBALS['xml_iso88591_Entities']=array(); |
|
112 | - $GLOBALS['xml_iso88591_Entities']['in'] = array(); |
|
113 | - $GLOBALS['xml_iso88591_Entities']['out'] = array(); |
|
114 | - for ($i = 0; $i < 32; $i++) |
|
115 | - { |
|
116 | - $GLOBALS['xml_iso88591_Entities']['in'][] = chr($i); |
|
117 | - $GLOBALS['xml_iso88591_Entities']['out'][] = '&#'.$i.';'; |
|
118 | - } |
|
119 | - for ($i = 160; $i < 256; $i++) |
|
120 | - { |
|
121 | - $GLOBALS['xml_iso88591_Entities']['in'][] = chr($i); |
|
122 | - $GLOBALS['xml_iso88591_Entities']['out'][] = '&#'.$i.';'; |
|
123 | - } |
|
124 | - |
|
125 | - /// @todo add to iso table the characters from cp_1252 range, i.e. 128 to 159? |
|
126 | - /// These will NOT be present in true ISO-8859-1, but will save the unwary |
|
127 | - /// windows user from sending junk (though no luck when reciving them...) |
|
128 | - /* |
|
37 | + if(!function_exists('xml_parser_create')) |
|
38 | + { |
|
39 | + // For PHP 4 onward, XML functionality is always compiled-in on windows: |
|
40 | + // no more need to dl-open it. It might have been compiled out on *nix... |
|
41 | + if(strtoupper(substr(PHP_OS, 0, 3) != 'WIN')) |
|
42 | + { |
|
43 | + dl('xml.so'); |
|
44 | + } |
|
45 | + } |
|
46 | + |
|
47 | + // G. Giunta 2005/01/29: declare global these variables, |
|
48 | + // so that xmlrpc.inc will work even if included from within a function |
|
49 | + // Milosch: 2005/08/07 - explicitly request these via $GLOBALS where used. |
|
50 | + $GLOBALS['xmlrpcI4']='i4'; |
|
51 | + $GLOBALS['xmlrpcInt']='int'; |
|
52 | + $GLOBALS['xmlrpcBoolean']='boolean'; |
|
53 | + $GLOBALS['xmlrpcDouble']='double'; |
|
54 | + $GLOBALS['xmlrpcString']='string'; |
|
55 | + $GLOBALS['xmlrpcDateTime']='dateTime.iso8601'; |
|
56 | + $GLOBALS['xmlrpcBase64']='base64'; |
|
57 | + $GLOBALS['xmlrpcArray']='array'; |
|
58 | + $GLOBALS['xmlrpcStruct']='struct'; |
|
59 | + $GLOBALS['xmlrpcValue']='undefined'; |
|
60 | + |
|
61 | + $GLOBALS['xmlrpcTypes']=array( |
|
62 | + $GLOBALS['xmlrpcI4'] => 1, |
|
63 | + $GLOBALS['xmlrpcInt'] => 1, |
|
64 | + $GLOBALS['xmlrpcBoolean'] => 1, |
|
65 | + $GLOBALS['xmlrpcString'] => 1, |
|
66 | + $GLOBALS['xmlrpcDouble'] => 1, |
|
67 | + $GLOBALS['xmlrpcDateTime'] => 1, |
|
68 | + $GLOBALS['xmlrpcBase64'] => 1, |
|
69 | + $GLOBALS['xmlrpcArray'] => 2, |
|
70 | + $GLOBALS['xmlrpcStruct'] => 3 |
|
71 | + ); |
|
72 | + |
|
73 | + $GLOBALS['xmlrpc_valid_parents'] = array( |
|
74 | + 'VALUE' => array('MEMBER', 'DATA', 'PARAM', 'FAULT'), |
|
75 | + 'BOOLEAN' => array('VALUE'), |
|
76 | + 'I4' => array('VALUE'), |
|
77 | + 'INT' => array('VALUE'), |
|
78 | + 'STRING' => array('VALUE'), |
|
79 | + 'DOUBLE' => array('VALUE'), |
|
80 | + 'DATETIME.ISO8601' => array('VALUE'), |
|
81 | + 'BASE64' => array('VALUE'), |
|
82 | + 'MEMBER' => array('STRUCT'), |
|
83 | + 'NAME' => array('MEMBER'), |
|
84 | + 'DATA' => array('ARRAY'), |
|
85 | + 'ARRAY' => array('VALUE'), |
|
86 | + 'STRUCT' => array('VALUE'), |
|
87 | + 'PARAM' => array('PARAMS'), |
|
88 | + 'METHODNAME' => array('METHODCALL'), |
|
89 | + 'PARAMS' => array('METHODCALL', 'METHODRESPONSE'), |
|
90 | + 'FAULT' => array('METHODRESPONSE'), |
|
91 | + 'NIL' => array('VALUE'), // only used when extension activated |
|
92 | + 'EX:NIL' => array('VALUE') // only used when extension activated |
|
93 | + ); |
|
94 | + |
|
95 | + // define extra types for supporting NULL (useful for json or <NIL/>) |
|
96 | + $GLOBALS['xmlrpcNull']='null'; |
|
97 | + $GLOBALS['xmlrpcTypes']['null']=1; |
|
98 | + |
|
99 | + // Not in use anymore since 2.0. Shall we remove it? |
|
100 | + /// @deprecated |
|
101 | + $GLOBALS['xmlEntities']=array( |
|
102 | + 'amp' => '&', |
|
103 | + 'quot' => '"', |
|
104 | + 'lt' => '<', |
|
105 | + 'gt' => '>', |
|
106 | + 'apos' => "'" |
|
107 | + ); |
|
108 | + |
|
109 | + // tables used for transcoding different charsets into us-ascii xml |
|
110 | + |
|
111 | + $GLOBALS['xml_iso88591_Entities']=array(); |
|
112 | + $GLOBALS['xml_iso88591_Entities']['in'] = array(); |
|
113 | + $GLOBALS['xml_iso88591_Entities']['out'] = array(); |
|
114 | + for ($i = 0; $i < 32; $i++) |
|
115 | + { |
|
116 | + $GLOBALS['xml_iso88591_Entities']['in'][] = chr($i); |
|
117 | + $GLOBALS['xml_iso88591_Entities']['out'][] = '&#'.$i.';'; |
|
118 | + } |
|
119 | + for ($i = 160; $i < 256; $i++) |
|
120 | + { |
|
121 | + $GLOBALS['xml_iso88591_Entities']['in'][] = chr($i); |
|
122 | + $GLOBALS['xml_iso88591_Entities']['out'][] = '&#'.$i.';'; |
|
123 | + } |
|
124 | + |
|
125 | + /// @todo add to iso table the characters from cp_1252 range, i.e. 128 to 159? |
|
126 | + /// These will NOT be present in true ISO-8859-1, but will save the unwary |
|
127 | + /// windows user from sending junk (though no luck when reciving them...) |
|
128 | + /* |
|
129 | 129 | $GLOBALS['xml_cp1252_Entities']=array(); |
130 | 130 | for ($i = 128; $i < 160; $i++) |
131 | 131 | { |
@@ -143,222 +143,222 @@ discard block |
||
143 | 143 | ); |
144 | 144 | */ |
145 | 145 | |
146 | - $GLOBALS['xmlrpcerr'] = array( |
|
147 | - 'unknown_method'=>1, |
|
148 | - 'invalid_return'=>2, |
|
149 | - 'incorrect_params'=>3, |
|
150 | - 'introspect_unknown'=>4, |
|
151 | - 'http_error'=>5, |
|
152 | - 'no_data'=>6, |
|
153 | - 'no_ssl'=>7, |
|
154 | - 'curl_fail'=>8, |
|
155 | - 'invalid_request'=>15, |
|
156 | - 'no_curl'=>16, |
|
157 | - 'server_error'=>17, |
|
158 | - 'multicall_error'=>18, |
|
159 | - 'multicall_notstruct'=>9, |
|
160 | - 'multicall_nomethod'=>10, |
|
161 | - 'multicall_notstring'=>11, |
|
162 | - 'multicall_recursion'=>12, |
|
163 | - 'multicall_noparams'=>13, |
|
164 | - 'multicall_notarray'=>14, |
|
165 | - |
|
166 | - 'cannot_decompress'=>103, |
|
167 | - 'decompress_fail'=>104, |
|
168 | - 'dechunk_fail'=>105, |
|
169 | - 'server_cannot_decompress'=>106, |
|
170 | - 'server_decompress_fail'=>107 |
|
171 | - ); |
|
172 | - |
|
173 | - $GLOBALS['xmlrpcstr'] = array( |
|
174 | - 'unknown_method'=>'Unknown method', |
|
175 | - 'invalid_return'=>'Invalid return payload: enable debugging to examine incoming payload', |
|
176 | - 'incorrect_params'=>'Incorrect parameters passed to method', |
|
177 | - 'introspect_unknown'=>"Can't introspect: method unknown", |
|
178 | - 'http_error'=>"Didn't receive 200 OK from remote server.", |
|
179 | - 'no_data'=>'No data received from server.', |
|
180 | - 'no_ssl'=>'No SSL support compiled in.', |
|
181 | - 'curl_fail'=>'CURL error', |
|
182 | - 'invalid_request'=>'Invalid request payload', |
|
183 | - 'no_curl'=>'No CURL support compiled in.', |
|
184 | - 'server_error'=>'Internal server error', |
|
185 | - 'multicall_error'=>'Received from server invalid multicall response', |
|
186 | - 'multicall_notstruct'=>'system.multicall expected struct', |
|
187 | - 'multicall_nomethod'=>'missing methodName', |
|
188 | - 'multicall_notstring'=>'methodName is not a string', |
|
189 | - 'multicall_recursion'=>'recursive system.multicall forbidden', |
|
190 | - 'multicall_noparams'=>'missing params', |
|
191 | - 'multicall_notarray'=>'params is not an array', |
|
192 | - |
|
193 | - 'cannot_decompress'=>'Received from server compressed HTTP and cannot decompress', |
|
194 | - 'decompress_fail'=>'Received from server invalid compressed HTTP', |
|
195 | - 'dechunk_fail'=>'Received from server invalid chunked HTTP', |
|
196 | - 'server_cannot_decompress'=>'Received from client compressed HTTP request and cannot decompress', |
|
197 | - 'server_decompress_fail'=>'Received from client invalid compressed HTTP request' |
|
198 | - ); |
|
199 | - |
|
200 | - // The charset encoding used by the server for received messages and |
|
201 | - // by the client for received responses when received charset cannot be determined |
|
202 | - // or is not supported |
|
203 | - $GLOBALS['xmlrpc_defencoding']='UTF-8'; |
|
204 | - |
|
205 | - // The encoding used internally by PHP. |
|
206 | - // String values received as xml will be converted to this, and php strings will be converted to xml |
|
207 | - // as if having been coded with this |
|
208 | - $GLOBALS['xmlrpc_internalencoding']='ISO-8859-1'; |
|
209 | - |
|
210 | - $GLOBALS['xmlrpcName']='XML-RPC for PHP'; |
|
211 | - $GLOBALS['xmlrpcVersion']='3.0.1'; |
|
212 | - |
|
213 | - // let user errors start at 800 |
|
214 | - $GLOBALS['xmlrpcerruser']=800; |
|
215 | - // let XML parse errors start at 100 |
|
216 | - $GLOBALS['xmlrpcerrxml']=100; |
|
217 | - |
|
218 | - // formulate backslashes for escaping regexp |
|
219 | - // Not in use anymore since 2.0. Shall we remove it? |
|
220 | - /// @deprecated |
|
221 | - $GLOBALS['xmlrpc_backslash']=chr(92).chr(92); |
|
222 | - |
|
223 | - // set to TRUE to enable correct decoding of <NIL/> and <EX:NIL/> values |
|
224 | - $GLOBALS['xmlrpc_null_extension']=false; |
|
225 | - |
|
226 | - // set to TRUE to enable encoding of php NULL values to <EX:NIL/> instead of <NIL/> |
|
227 | - $GLOBALS['xmlrpc_null_apache_encoding']=false; |
|
228 | - $GLOBALS['xmlrpc_null_apache_encoding_ns']='http://ws.apache.org/xmlrpc/namespaces/extensions'; |
|
229 | - |
|
230 | - // used to store state during parsing |
|
231 | - // quick explanation of components: |
|
232 | - // ac - used to accumulate values |
|
233 | - // isf - used to indicate a parsing fault (2) or xmlrpcresp fault (1) |
|
234 | - // isf_reason - used for storing xmlrpcresp fault string |
|
235 | - // lv - used to indicate "looking for a value": implements |
|
236 | - // the logic to allow values with no types to be strings |
|
237 | - // params - used to store parameters in method calls |
|
238 | - // method - used to store method name |
|
239 | - // stack - array with genealogy of xml elements names: |
|
240 | - // used to validate nesting of xmlrpc elements |
|
241 | - $GLOBALS['_xh']=null; |
|
242 | - |
|
243 | - /** |
|
244 | - * Convert a string to the correct XML representation in a target charset |
|
245 | - * To help correct communication of non-ascii chars inside strings, regardless |
|
246 | - * of the charset used when sending requests, parsing them, sending responses |
|
247 | - * and parsing responses, an option is to convert all non-ascii chars present in the message |
|
248 | - * into their equivalent 'charset entity'. Charset entities enumerated this way |
|
249 | - * are independent of the charset encoding used to transmit them, and all XML |
|
250 | - * parsers are bound to understand them. |
|
251 | - * Note that in the std case we are not sending a charset encoding mime type |
|
252 | - * along with http headers, so we are bound by RFC 3023 to emit strict us-ascii. |
|
253 | - * |
|
254 | - * @todo do a bit of basic benchmarking (strtr vs. str_replace) |
|
255 | - * @todo make usage of iconv() or recode_string() or mb_string() where available |
|
256 | - */ |
|
257 | - function xmlrpc_encode_entitites($data, $src_encoding='', $dest_encoding='') |
|
258 | - { |
|
259 | - if ($src_encoding == '') |
|
260 | - { |
|
261 | - // lame, but we know no better... |
|
262 | - $src_encoding = $GLOBALS['xmlrpc_internalencoding']; |
|
263 | - } |
|
264 | - |
|
265 | - switch(strtoupper($src_encoding.'_'.$dest_encoding)) |
|
266 | - { |
|
267 | - case 'ISO-8859-1_': |
|
268 | - case 'ISO-8859-1_US-ASCII': |
|
269 | - $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); |
|
270 | - $escaped_data = str_replace($GLOBALS['xml_iso88591_Entities']['in'], $GLOBALS['xml_iso88591_Entities']['out'], $escaped_data); |
|
271 | - break; |
|
272 | - case 'ISO-8859-1_UTF-8': |
|
273 | - $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); |
|
274 | - $escaped_data = utf8_encode($escaped_data); |
|
275 | - break; |
|
276 | - case 'ISO-8859-1_ISO-8859-1': |
|
277 | - case 'US-ASCII_US-ASCII': |
|
278 | - case 'US-ASCII_UTF-8': |
|
279 | - case 'US-ASCII_': |
|
280 | - case 'US-ASCII_ISO-8859-1': |
|
281 | - case 'UTF-8_UTF-8': |
|
282 | - //case 'CP1252_CP1252': |
|
283 | - $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); |
|
284 | - break; |
|
285 | - case 'UTF-8_': |
|
286 | - case 'UTF-8_US-ASCII': |
|
287 | - case 'UTF-8_ISO-8859-1': |
|
288 | - // NB: this will choke on invalid UTF-8, going most likely beyond EOF |
|
289 | - $escaped_data = ''; |
|
290 | - // be kind to users creating string xmlrpcvals out of different php types |
|
291 | - $data = (string) $data; |
|
292 | - $ns = strlen ($data); |
|
293 | - for ($nn = 0; $nn < $ns; $nn++) |
|
294 | - { |
|
295 | - $ch = $data[$nn]; |
|
296 | - $ii = ord($ch); |
|
297 | - //1 7 0bbbbbbb (127) |
|
298 | - if ($ii < 128) |
|
299 | - { |
|
300 | - /// @todo shall we replace this with a (supposedly) faster str_replace? |
|
301 | - switch($ii){ |
|
302 | - case 34: |
|
303 | - $escaped_data .= '"'; |
|
304 | - break; |
|
305 | - case 38: |
|
306 | - $escaped_data .= '&'; |
|
307 | - break; |
|
308 | - case 39: |
|
309 | - $escaped_data .= '''; |
|
310 | - break; |
|
311 | - case 60: |
|
312 | - $escaped_data .= '<'; |
|
313 | - break; |
|
314 | - case 62: |
|
315 | - $escaped_data .= '>'; |
|
316 | - break; |
|
317 | - default: |
|
318 | - $escaped_data .= $ch; |
|
319 | - } // switch |
|
320 | - } |
|
321 | - //2 11 110bbbbb 10bbbbbb (2047) |
|
322 | - else if ($ii>>5 == 6) |
|
323 | - { |
|
324 | - $b1 = ($ii & 31); |
|
325 | - $ii = ord($data[$nn+1]); |
|
326 | - $b2 = ($ii & 63); |
|
327 | - $ii = ($b1 * 64) + $b2; |
|
328 | - $ent = sprintf ('&#%d;', $ii); |
|
329 | - $escaped_data .= $ent; |
|
330 | - $nn += 1; |
|
331 | - } |
|
332 | - //3 16 1110bbbb 10bbbbbb 10bbbbbb |
|
333 | - else if ($ii>>4 == 14) |
|
334 | - { |
|
335 | - $b1 = ($ii & 15); |
|
336 | - $ii = ord($data[$nn+1]); |
|
337 | - $b2 = ($ii & 63); |
|
338 | - $ii = ord($data[$nn+2]); |
|
339 | - $b3 = ($ii & 63); |
|
340 | - $ii = ((($b1 * 64) + $b2) * 64) + $b3; |
|
341 | - $ent = sprintf ('&#%d;', $ii); |
|
342 | - $escaped_data .= $ent; |
|
343 | - $nn += 2; |
|
344 | - } |
|
345 | - //4 21 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb |
|
346 | - else if ($ii>>3 == 30) |
|
347 | - { |
|
348 | - $b1 = ($ii & 7); |
|
349 | - $ii = ord($data[$nn+1]); |
|
350 | - $b2 = ($ii & 63); |
|
351 | - $ii = ord($data[$nn+2]); |
|
352 | - $b3 = ($ii & 63); |
|
353 | - $ii = ord($data[$nn+3]); |
|
354 | - $b4 = ($ii & 63); |
|
355 | - $ii = ((((($b1 * 64) + $b2) * 64) + $b3) * 64) + $b4; |
|
356 | - $ent = sprintf ('&#%d;', $ii); |
|
357 | - $escaped_data .= $ent; |
|
358 | - $nn += 3; |
|
359 | - } |
|
360 | - } |
|
361 | - break; |
|
146 | + $GLOBALS['xmlrpcerr'] = array( |
|
147 | + 'unknown_method'=>1, |
|
148 | + 'invalid_return'=>2, |
|
149 | + 'incorrect_params'=>3, |
|
150 | + 'introspect_unknown'=>4, |
|
151 | + 'http_error'=>5, |
|
152 | + 'no_data'=>6, |
|
153 | + 'no_ssl'=>7, |
|
154 | + 'curl_fail'=>8, |
|
155 | + 'invalid_request'=>15, |
|
156 | + 'no_curl'=>16, |
|
157 | + 'server_error'=>17, |
|
158 | + 'multicall_error'=>18, |
|
159 | + 'multicall_notstruct'=>9, |
|
160 | + 'multicall_nomethod'=>10, |
|
161 | + 'multicall_notstring'=>11, |
|
162 | + 'multicall_recursion'=>12, |
|
163 | + 'multicall_noparams'=>13, |
|
164 | + 'multicall_notarray'=>14, |
|
165 | + |
|
166 | + 'cannot_decompress'=>103, |
|
167 | + 'decompress_fail'=>104, |
|
168 | + 'dechunk_fail'=>105, |
|
169 | + 'server_cannot_decompress'=>106, |
|
170 | + 'server_decompress_fail'=>107 |
|
171 | + ); |
|
172 | + |
|
173 | + $GLOBALS['xmlrpcstr'] = array( |
|
174 | + 'unknown_method'=>'Unknown method', |
|
175 | + 'invalid_return'=>'Invalid return payload: enable debugging to examine incoming payload', |
|
176 | + 'incorrect_params'=>'Incorrect parameters passed to method', |
|
177 | + 'introspect_unknown'=>"Can't introspect: method unknown", |
|
178 | + 'http_error'=>"Didn't receive 200 OK from remote server.", |
|
179 | + 'no_data'=>'No data received from server.', |
|
180 | + 'no_ssl'=>'No SSL support compiled in.', |
|
181 | + 'curl_fail'=>'CURL error', |
|
182 | + 'invalid_request'=>'Invalid request payload', |
|
183 | + 'no_curl'=>'No CURL support compiled in.', |
|
184 | + 'server_error'=>'Internal server error', |
|
185 | + 'multicall_error'=>'Received from server invalid multicall response', |
|
186 | + 'multicall_notstruct'=>'system.multicall expected struct', |
|
187 | + 'multicall_nomethod'=>'missing methodName', |
|
188 | + 'multicall_notstring'=>'methodName is not a string', |
|
189 | + 'multicall_recursion'=>'recursive system.multicall forbidden', |
|
190 | + 'multicall_noparams'=>'missing params', |
|
191 | + 'multicall_notarray'=>'params is not an array', |
|
192 | + |
|
193 | + 'cannot_decompress'=>'Received from server compressed HTTP and cannot decompress', |
|
194 | + 'decompress_fail'=>'Received from server invalid compressed HTTP', |
|
195 | + 'dechunk_fail'=>'Received from server invalid chunked HTTP', |
|
196 | + 'server_cannot_decompress'=>'Received from client compressed HTTP request and cannot decompress', |
|
197 | + 'server_decompress_fail'=>'Received from client invalid compressed HTTP request' |
|
198 | + ); |
|
199 | + |
|
200 | + // The charset encoding used by the server for received messages and |
|
201 | + // by the client for received responses when received charset cannot be determined |
|
202 | + // or is not supported |
|
203 | + $GLOBALS['xmlrpc_defencoding']='UTF-8'; |
|
204 | + |
|
205 | + // The encoding used internally by PHP. |
|
206 | + // String values received as xml will be converted to this, and php strings will be converted to xml |
|
207 | + // as if having been coded with this |
|
208 | + $GLOBALS['xmlrpc_internalencoding']='ISO-8859-1'; |
|
209 | + |
|
210 | + $GLOBALS['xmlrpcName']='XML-RPC for PHP'; |
|
211 | + $GLOBALS['xmlrpcVersion']='3.0.1'; |
|
212 | + |
|
213 | + // let user errors start at 800 |
|
214 | + $GLOBALS['xmlrpcerruser']=800; |
|
215 | + // let XML parse errors start at 100 |
|
216 | + $GLOBALS['xmlrpcerrxml']=100; |
|
217 | + |
|
218 | + // formulate backslashes for escaping regexp |
|
219 | + // Not in use anymore since 2.0. Shall we remove it? |
|
220 | + /// @deprecated |
|
221 | + $GLOBALS['xmlrpc_backslash']=chr(92).chr(92); |
|
222 | + |
|
223 | + // set to TRUE to enable correct decoding of <NIL/> and <EX:NIL/> values |
|
224 | + $GLOBALS['xmlrpc_null_extension']=false; |
|
225 | + |
|
226 | + // set to TRUE to enable encoding of php NULL values to <EX:NIL/> instead of <NIL/> |
|
227 | + $GLOBALS['xmlrpc_null_apache_encoding']=false; |
|
228 | + $GLOBALS['xmlrpc_null_apache_encoding_ns']='http://ws.apache.org/xmlrpc/namespaces/extensions'; |
|
229 | + |
|
230 | + // used to store state during parsing |
|
231 | + // quick explanation of components: |
|
232 | + // ac - used to accumulate values |
|
233 | + // isf - used to indicate a parsing fault (2) or xmlrpcresp fault (1) |
|
234 | + // isf_reason - used for storing xmlrpcresp fault string |
|
235 | + // lv - used to indicate "looking for a value": implements |
|
236 | + // the logic to allow values with no types to be strings |
|
237 | + // params - used to store parameters in method calls |
|
238 | + // method - used to store method name |
|
239 | + // stack - array with genealogy of xml elements names: |
|
240 | + // used to validate nesting of xmlrpc elements |
|
241 | + $GLOBALS['_xh']=null; |
|
242 | + |
|
243 | + /** |
|
244 | + * Convert a string to the correct XML representation in a target charset |
|
245 | + * To help correct communication of non-ascii chars inside strings, regardless |
|
246 | + * of the charset used when sending requests, parsing them, sending responses |
|
247 | + * and parsing responses, an option is to convert all non-ascii chars present in the message |
|
248 | + * into their equivalent 'charset entity'. Charset entities enumerated this way |
|
249 | + * are independent of the charset encoding used to transmit them, and all XML |
|
250 | + * parsers are bound to understand them. |
|
251 | + * Note that in the std case we are not sending a charset encoding mime type |
|
252 | + * along with http headers, so we are bound by RFC 3023 to emit strict us-ascii. |
|
253 | + * |
|
254 | + * @todo do a bit of basic benchmarking (strtr vs. str_replace) |
|
255 | + * @todo make usage of iconv() or recode_string() or mb_string() where available |
|
256 | + */ |
|
257 | + function xmlrpc_encode_entitites($data, $src_encoding='', $dest_encoding='') |
|
258 | + { |
|
259 | + if ($src_encoding == '') |
|
260 | + { |
|
261 | + // lame, but we know no better... |
|
262 | + $src_encoding = $GLOBALS['xmlrpc_internalencoding']; |
|
263 | + } |
|
264 | + |
|
265 | + switch(strtoupper($src_encoding.'_'.$dest_encoding)) |
|
266 | + { |
|
267 | + case 'ISO-8859-1_': |
|
268 | + case 'ISO-8859-1_US-ASCII': |
|
269 | + $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); |
|
270 | + $escaped_data = str_replace($GLOBALS['xml_iso88591_Entities']['in'], $GLOBALS['xml_iso88591_Entities']['out'], $escaped_data); |
|
271 | + break; |
|
272 | + case 'ISO-8859-1_UTF-8': |
|
273 | + $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); |
|
274 | + $escaped_data = utf8_encode($escaped_data); |
|
275 | + break; |
|
276 | + case 'ISO-8859-1_ISO-8859-1': |
|
277 | + case 'US-ASCII_US-ASCII': |
|
278 | + case 'US-ASCII_UTF-8': |
|
279 | + case 'US-ASCII_': |
|
280 | + case 'US-ASCII_ISO-8859-1': |
|
281 | + case 'UTF-8_UTF-8': |
|
282 | + //case 'CP1252_CP1252': |
|
283 | + $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); |
|
284 | + break; |
|
285 | + case 'UTF-8_': |
|
286 | + case 'UTF-8_US-ASCII': |
|
287 | + case 'UTF-8_ISO-8859-1': |
|
288 | + // NB: this will choke on invalid UTF-8, going most likely beyond EOF |
|
289 | + $escaped_data = ''; |
|
290 | + // be kind to users creating string xmlrpcvals out of different php types |
|
291 | + $data = (string) $data; |
|
292 | + $ns = strlen ($data); |
|
293 | + for ($nn = 0; $nn < $ns; $nn++) |
|
294 | + { |
|
295 | + $ch = $data[$nn]; |
|
296 | + $ii = ord($ch); |
|
297 | + //1 7 0bbbbbbb (127) |
|
298 | + if ($ii < 128) |
|
299 | + { |
|
300 | + /// @todo shall we replace this with a (supposedly) faster str_replace? |
|
301 | + switch($ii){ |
|
302 | + case 34: |
|
303 | + $escaped_data .= '"'; |
|
304 | + break; |
|
305 | + case 38: |
|
306 | + $escaped_data .= '&'; |
|
307 | + break; |
|
308 | + case 39: |
|
309 | + $escaped_data .= '''; |
|
310 | + break; |
|
311 | + case 60: |
|
312 | + $escaped_data .= '<'; |
|
313 | + break; |
|
314 | + case 62: |
|
315 | + $escaped_data .= '>'; |
|
316 | + break; |
|
317 | + default: |
|
318 | + $escaped_data .= $ch; |
|
319 | + } // switch |
|
320 | + } |
|
321 | + //2 11 110bbbbb 10bbbbbb (2047) |
|
322 | + else if ($ii>>5 == 6) |
|
323 | + { |
|
324 | + $b1 = ($ii & 31); |
|
325 | + $ii = ord($data[$nn+1]); |
|
326 | + $b2 = ($ii & 63); |
|
327 | + $ii = ($b1 * 64) + $b2; |
|
328 | + $ent = sprintf ('&#%d;', $ii); |
|
329 | + $escaped_data .= $ent; |
|
330 | + $nn += 1; |
|
331 | + } |
|
332 | + //3 16 1110bbbb 10bbbbbb 10bbbbbb |
|
333 | + else if ($ii>>4 == 14) |
|
334 | + { |
|
335 | + $b1 = ($ii & 15); |
|
336 | + $ii = ord($data[$nn+1]); |
|
337 | + $b2 = ($ii & 63); |
|
338 | + $ii = ord($data[$nn+2]); |
|
339 | + $b3 = ($ii & 63); |
|
340 | + $ii = ((($b1 * 64) + $b2) * 64) + $b3; |
|
341 | + $ent = sprintf ('&#%d;', $ii); |
|
342 | + $escaped_data .= $ent; |
|
343 | + $nn += 2; |
|
344 | + } |
|
345 | + //4 21 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb |
|
346 | + else if ($ii>>3 == 30) |
|
347 | + { |
|
348 | + $b1 = ($ii & 7); |
|
349 | + $ii = ord($data[$nn+1]); |
|
350 | + $b2 = ($ii & 63); |
|
351 | + $ii = ord($data[$nn+2]); |
|
352 | + $b3 = ($ii & 63); |
|
353 | + $ii = ord($data[$nn+3]); |
|
354 | + $b4 = ($ii & 63); |
|
355 | + $ii = ((((($b1 * 64) + $b2) * 64) + $b3) * 64) + $b4; |
|
356 | + $ent = sprintf ('&#%d;', $ii); |
|
357 | + $escaped_data .= $ent; |
|
358 | + $nn += 3; |
|
359 | + } |
|
360 | + } |
|
361 | + break; |
|
362 | 362 | /* |
363 | 363 | case 'CP1252_': |
364 | 364 | case 'CP1252_US-ASCII': |
@@ -378,2443 +378,2443 @@ discard block |
||
378 | 378 | $escaped_data = str_replace($GLOBALS['xml_cp1252_Entities']['in'], $GLOBALS['xml_cp1252_Entities']['out'], $escaped_data); |
379 | 379 | break; |
380 | 380 | */ |
381 | - default: |
|
382 | - $escaped_data = ''; |
|
383 | - error_log("Converting from $src_encoding to $dest_encoding: not supported..."); |
|
384 | - } |
|
385 | - return $escaped_data; |
|
386 | - } |
|
387 | - |
|
388 | - /// xml parser handler function for opening element tags |
|
389 | - function xmlrpc_se($parser, $name, $attrs, $accept_single_vals=false) |
|
390 | - { |
|
391 | - // if invalid xmlrpc already detected, skip all processing |
|
392 | - if ($GLOBALS['_xh']['isf'] < 2) |
|
393 | - { |
|
394 | - // check for correct element nesting |
|
395 | - // top level element can only be of 2 types |
|
396 | - /// @todo optimization creep: save this check into a bool variable, instead of using count() every time: |
|
397 | - /// there is only a single top level element in xml anyway |
|
398 | - if (count($GLOBALS['_xh']['stack']) == 0) |
|
399 | - { |
|
400 | - if ($name != 'METHODRESPONSE' && $name != 'METHODCALL' && ( |
|
401 | - $name != 'VALUE' && !$accept_single_vals)) |
|
402 | - { |
|
403 | - $GLOBALS['_xh']['isf'] = 2; |
|
404 | - $GLOBALS['_xh']['isf_reason'] = 'missing top level xmlrpc element'; |
|
405 | - return; |
|
406 | - } |
|
407 | - else |
|
408 | - { |
|
409 | - $GLOBALS['_xh']['rt'] = strtolower($name); |
|
410 | - $GLOBALS['_xh']['rt'] = strtolower($name); |
|
411 | - } |
|
412 | - } |
|
413 | - else |
|
414 | - { |
|
415 | - // not top level element: see if parent is OK |
|
416 | - $parent = end($GLOBALS['_xh']['stack']); |
|
417 | - if (!array_key_exists($name, $GLOBALS['xmlrpc_valid_parents']) || !in_array($parent, $GLOBALS['xmlrpc_valid_parents'][$name])) |
|
418 | - { |
|
419 | - $GLOBALS['_xh']['isf'] = 2; |
|
420 | - $GLOBALS['_xh']['isf_reason'] = "xmlrpc element $name cannot be child of $parent"; |
|
421 | - return; |
|
422 | - } |
|
423 | - } |
|
424 | - |
|
425 | - switch($name) |
|
426 | - { |
|
427 | - // optimize for speed switch cases: most common cases first |
|
428 | - case 'VALUE': |
|
429 | - /// @todo we could check for 2 VALUE elements inside a MEMBER or PARAM element |
|
430 | - $GLOBALS['_xh']['vt']='value'; // indicator: no value found yet |
|
431 | - $GLOBALS['_xh']['ac']=''; |
|
432 | - $GLOBALS['_xh']['lv']=1; |
|
433 | - $GLOBALS['_xh']['php_class']=null; |
|
434 | - break; |
|
435 | - case 'I4': |
|
436 | - case 'INT': |
|
437 | - case 'STRING': |
|
438 | - case 'BOOLEAN': |
|
439 | - case 'DOUBLE': |
|
440 | - case 'DATETIME.ISO8601': |
|
441 | - case 'BASE64': |
|
442 | - if ($GLOBALS['_xh']['vt']!='value') |
|
443 | - { |
|
444 | - //two data elements inside a value: an error occurred! |
|
445 | - $GLOBALS['_xh']['isf'] = 2; |
|
446 | - $GLOBALS['_xh']['isf_reason'] = "$name element following a {$GLOBALS['_xh']['vt']} element inside a single value"; |
|
447 | - return; |
|
448 | - } |
|
449 | - $GLOBALS['_xh']['ac']=''; // reset the accumulator |
|
450 | - break; |
|
451 | - case 'STRUCT': |
|
452 | - case 'ARRAY': |
|
453 | - if ($GLOBALS['_xh']['vt']!='value') |
|
454 | - { |
|
455 | - //two data elements inside a value: an error occurred! |
|
456 | - $GLOBALS['_xh']['isf'] = 2; |
|
457 | - $GLOBALS['_xh']['isf_reason'] = "$name element following a {$GLOBALS['_xh']['vt']} element inside a single value"; |
|
458 | - return; |
|
459 | - } |
|
460 | - // create an empty array to hold child values, and push it onto appropriate stack |
|
461 | - $cur_val = array(); |
|
462 | - $cur_val['values'] = array(); |
|
463 | - $cur_val['type'] = $name; |
|
464 | - // check for out-of-band information to rebuild php objs |
|
465 | - // and in case it is found, save it |
|
466 | - if (@isset($attrs['PHP_CLASS'])) |
|
467 | - { |
|
468 | - $cur_val['php_class'] = $attrs['PHP_CLASS']; |
|
469 | - } |
|
470 | - $GLOBALS['_xh']['valuestack'][] = $cur_val; |
|
471 | - $GLOBALS['_xh']['vt']='data'; // be prepared for a data element next |
|
472 | - break; |
|
473 | - case 'DATA': |
|
474 | - if ($GLOBALS['_xh']['vt']!='data') |
|
475 | - { |
|
476 | - //two data elements inside a value: an error occurred! |
|
477 | - $GLOBALS['_xh']['isf'] = 2; |
|
478 | - $GLOBALS['_xh']['isf_reason'] = "found two data elements inside an array element"; |
|
479 | - return; |
|
480 | - } |
|
481 | - case 'METHODCALL': |
|
482 | - case 'METHODRESPONSE': |
|
483 | - case 'PARAMS': |
|
484 | - // valid elements that add little to processing |
|
485 | - break; |
|
486 | - case 'METHODNAME': |
|
487 | - case 'NAME': |
|
488 | - /// @todo we could check for 2 NAME elements inside a MEMBER element |
|
489 | - $GLOBALS['_xh']['ac']=''; |
|
490 | - break; |
|
491 | - case 'FAULT': |
|
492 | - $GLOBALS['_xh']['isf']=1; |
|
493 | - break; |
|
494 | - case 'MEMBER': |
|
495 | - $GLOBALS['_xh']['valuestack'][count($GLOBALS['_xh']['valuestack'])-1]['name']=''; // set member name to null, in case we do not find in the xml later on |
|
496 | - //$GLOBALS['_xh']['ac']=''; |
|
497 | - // Drop trough intentionally |
|
498 | - case 'PARAM': |
|
499 | - // clear value type, so we can check later if no value has been passed for this param/member |
|
500 | - $GLOBALS['_xh']['vt']=null; |
|
501 | - break; |
|
502 | - case 'NIL': |
|
503 | - case 'EX:NIL': |
|
504 | - if ($GLOBALS['xmlrpc_null_extension']) |
|
505 | - { |
|
506 | - if ($GLOBALS['_xh']['vt']!='value') |
|
507 | - { |
|
508 | - //two data elements inside a value: an error occurred! |
|
509 | - $GLOBALS['_xh']['isf'] = 2; |
|
510 | - $GLOBALS['_xh']['isf_reason'] = "$name element following a {$GLOBALS['_xh']['vt']} element inside a single value"; |
|
511 | - return; |
|
512 | - } |
|
513 | - $GLOBALS['_xh']['ac']=''; // reset the accumulator |
|
514 | - break; |
|
515 | - } |
|
516 | - // we do not support the <NIL/> extension, so |
|
517 | - // drop through intentionally |
|
518 | - default: |
|
519 | - /// INVALID ELEMENT: RAISE ISF so that it is later recognized!!! |
|
520 | - $GLOBALS['_xh']['isf'] = 2; |
|
521 | - $GLOBALS['_xh']['isf_reason'] = "found not-xmlrpc xml element $name"; |
|
522 | - break; |
|
523 | - } |
|
524 | - |
|
525 | - // Save current element name to stack, to validate nesting |
|
526 | - $GLOBALS['_xh']['stack'][] = $name; |
|
527 | - |
|
528 | - /// @todo optimization creep: move this inside the big switch() above |
|
529 | - if($name!='VALUE') |
|
530 | - { |
|
531 | - $GLOBALS['_xh']['lv']=0; |
|
532 | - } |
|
533 | - } |
|
534 | - } |
|
535 | - |
|
536 | - /// Used in decoding xml chunks that might represent single xmlrpc values |
|
537 | - function xmlrpc_se_any($parser, $name, $attrs) |
|
538 | - { |
|
539 | - xmlrpc_se($parser, $name, $attrs, true); |
|
540 | - } |
|
541 | - |
|
542 | - /// xml parser handler function for close element tags |
|
543 | - function xmlrpc_ee($parser, $name, $rebuild_xmlrpcvals = true) |
|
544 | - { |
|
545 | - if ($GLOBALS['_xh']['isf'] < 2) |
|
546 | - { |
|
547 | - // push this element name from stack |
|
548 | - // NB: if XML validates, correct opening/closing is guaranteed and |
|
549 | - // we do not have to check for $name == $curr_elem. |
|
550 | - // we also checked for proper nesting at start of elements... |
|
551 | - $curr_elem = array_pop($GLOBALS['_xh']['stack']); |
|
552 | - |
|
553 | - switch($name) |
|
554 | - { |
|
555 | - case 'VALUE': |
|
556 | - // This if() detects if no scalar was inside <VALUE></VALUE> |
|
557 | - if ($GLOBALS['_xh']['vt']=='value') |
|
558 | - { |
|
559 | - $GLOBALS['_xh']['value']=$GLOBALS['_xh']['ac']; |
|
560 | - $GLOBALS['_xh']['vt']=$GLOBALS['xmlrpcString']; |
|
561 | - } |
|
562 | - |
|
563 | - if ($rebuild_xmlrpcvals) |
|
564 | - { |
|
565 | - // build the xmlrpc val out of the data received, and substitute it |
|
566 | - $temp = new xmlrpcval($GLOBALS['_xh']['value'], $GLOBALS['_xh']['vt']); |
|
567 | - // in case we got info about underlying php class, save it |
|
568 | - // in the object we're rebuilding |
|
569 | - if (isset($GLOBALS['_xh']['php_class'])) |
|
570 | - $temp->_php_class = $GLOBALS['_xh']['php_class']; |
|
571 | - // check if we are inside an array or struct: |
|
572 | - // if value just built is inside an array, let's move it into array on the stack |
|
573 | - $vscount = count($GLOBALS['_xh']['valuestack']); |
|
574 | - if ($vscount && $GLOBALS['_xh']['valuestack'][$vscount-1]['type']=='ARRAY') |
|
575 | - { |
|
576 | - $GLOBALS['_xh']['valuestack'][$vscount-1]['values'][] = $temp; |
|
577 | - } |
|
578 | - else |
|
579 | - { |
|
580 | - $GLOBALS['_xh']['value'] = $temp; |
|
581 | - } |
|
582 | - } |
|
583 | - else |
|
584 | - { |
|
585 | - /// @todo this needs to treat correctly php-serialized objects, |
|
586 | - /// since std deserializing is done by php_xmlrpc_decode, |
|
587 | - /// which we will not be calling... |
|
588 | - if (isset($GLOBALS['_xh']['php_class'])) |
|
589 | - { |
|
590 | - } |
|
591 | - |
|
592 | - // check if we are inside an array or struct: |
|
593 | - // if value just built is inside an array, let's move it into array on the stack |
|
594 | - $vscount = count($GLOBALS['_xh']['valuestack']); |
|
595 | - if ($vscount && $GLOBALS['_xh']['valuestack'][$vscount-1]['type']=='ARRAY') |
|
596 | - { |
|
597 | - $GLOBALS['_xh']['valuestack'][$vscount-1]['values'][] = $GLOBALS['_xh']['value']; |
|
598 | - } |
|
599 | - } |
|
600 | - break; |
|
601 | - case 'BOOLEAN': |
|
602 | - case 'I4': |
|
603 | - case 'INT': |
|
604 | - case 'STRING': |
|
605 | - case 'DOUBLE': |
|
606 | - case 'DATETIME.ISO8601': |
|
607 | - case 'BASE64': |
|
608 | - $GLOBALS['_xh']['vt']=strtolower($name); |
|
609 | - /// @todo: optimization creep - remove the if/elseif cycle below |
|
610 | - /// since the case() in which we are already did that |
|
611 | - if ($name=='STRING') |
|
612 | - { |
|
613 | - $GLOBALS['_xh']['value']=$GLOBALS['_xh']['ac']; |
|
614 | - } |
|
615 | - elseif ($name=='DATETIME.ISO8601') |
|
616 | - { |
|
617 | - if (!preg_match('/^[0-9]{8}T[0-9]{2}:[0-9]{2}:[0-9]{2}$/', $GLOBALS['_xh']['ac'])) |
|
618 | - { |
|
619 | - error_log('XML-RPC: invalid value received in DATETIME: '.$GLOBALS['_xh']['ac']); |
|
620 | - } |
|
621 | - $GLOBALS['_xh']['vt']=$GLOBALS['xmlrpcDateTime']; |
|
622 | - $GLOBALS['_xh']['value']=$GLOBALS['_xh']['ac']; |
|
623 | - } |
|
624 | - elseif ($name=='BASE64') |
|
625 | - { |
|
626 | - /// @todo check for failure of base64 decoding / catch warnings |
|
627 | - $GLOBALS['_xh']['value']=base64_decode($GLOBALS['_xh']['ac']); |
|
628 | - } |
|
629 | - elseif ($name=='BOOLEAN') |
|
630 | - { |
|
631 | - // special case here: we translate boolean 1 or 0 into PHP |
|
632 | - // constants true or false. |
|
633 | - // Strings 'true' and 'false' are accepted, even though the |
|
634 | - // spec never mentions them (see eg. Blogger api docs) |
|
635 | - // NB: this simple checks helps a lot sanitizing input, ie no |
|
636 | - // security problems around here |
|
637 | - if ($GLOBALS['_xh']['ac']=='1' || strcasecmp($GLOBALS['_xh']['ac'], 'true') == 0) |
|
638 | - { |
|
639 | - $GLOBALS['_xh']['value']=true; |
|
640 | - } |
|
641 | - else |
|
642 | - { |
|
643 | - // log if receiveing something strange, even though we set the value to false anyway |
|
644 | - if ($GLOBALS['_xh']['ac']!='0' && strcasecmp($GLOBALS['_xh']['ac'], 'false') != 0) |
|
645 | - error_log('XML-RPC: invalid value received in BOOLEAN: '.$GLOBALS['_xh']['ac']); |
|
646 | - $GLOBALS['_xh']['value']=false; |
|
647 | - } |
|
648 | - } |
|
649 | - elseif ($name=='DOUBLE') |
|
650 | - { |
|
651 | - // we have a DOUBLE |
|
652 | - // we must check that only 0123456789-.<space> are characters here |
|
653 | - // NOTE: regexp could be much stricter than this... |
|
654 | - if (!preg_match('/^[+-eE0123456789 \t.]+$/', $GLOBALS['_xh']['ac'])) |
|
655 | - { |
|
656 | - /// @todo: find a better way of throwing an error than this! |
|
657 | - error_log('XML-RPC: non numeric value received in DOUBLE: '.$GLOBALS['_xh']['ac']); |
|
658 | - $GLOBALS['_xh']['value']='ERROR_NON_NUMERIC_FOUND'; |
|
659 | - } |
|
660 | - else |
|
661 | - { |
|
662 | - // it's ok, add it on |
|
663 | - $GLOBALS['_xh']['value']=(double)$GLOBALS['_xh']['ac']; |
|
664 | - } |
|
665 | - } |
|
666 | - else |
|
667 | - { |
|
668 | - // we have an I4/INT |
|
669 | - // we must check that only 0123456789-<space> are characters here |
|
670 | - if (!preg_match('/^[+-]?[0123456789 \t]+$/', $GLOBALS['_xh']['ac'])) |
|
671 | - { |
|
672 | - /// @todo find a better way of throwing an error than this! |
|
673 | - error_log('XML-RPC: non numeric value received in INT: '.$GLOBALS['_xh']['ac']); |
|
674 | - $GLOBALS['_xh']['value']='ERROR_NON_NUMERIC_FOUND'; |
|
675 | - } |
|
676 | - else |
|
677 | - { |
|
678 | - // it's ok, add it on |
|
679 | - $GLOBALS['_xh']['value']=(int)$GLOBALS['_xh']['ac']; |
|
680 | - } |
|
681 | - } |
|
682 | - //$GLOBALS['_xh']['ac']=''; // is this necessary? |
|
683 | - $GLOBALS['_xh']['lv']=3; // indicate we've found a value |
|
684 | - break; |
|
685 | - case 'NAME': |
|
686 | - $GLOBALS['_xh']['valuestack'][count($GLOBALS['_xh']['valuestack'])-1]['name'] = $GLOBALS['_xh']['ac']; |
|
687 | - break; |
|
688 | - case 'MEMBER': |
|
689 | - //$GLOBALS['_xh']['ac']=''; // is this necessary? |
|
690 | - // add to array in the stack the last element built, |
|
691 | - // unless no VALUE was found |
|
692 | - if ($GLOBALS['_xh']['vt']) |
|
693 | - { |
|
694 | - $vscount = count($GLOBALS['_xh']['valuestack']); |
|
695 | - $GLOBALS['_xh']['valuestack'][$vscount-1]['values'][$GLOBALS['_xh']['valuestack'][$vscount-1]['name']] = $GLOBALS['_xh']['value']; |
|
696 | - } else |
|
697 | - error_log('XML-RPC: missing VALUE inside STRUCT in received xml'); |
|
698 | - break; |
|
699 | - case 'DATA': |
|
700 | - //$GLOBALS['_xh']['ac']=''; // is this necessary? |
|
701 | - $GLOBALS['_xh']['vt']=null; // reset this to check for 2 data elements in a row - even if they're empty |
|
702 | - break; |
|
703 | - case 'STRUCT': |
|
704 | - case 'ARRAY': |
|
705 | - // fetch out of stack array of values, and promote it to current value |
|
706 | - $curr_val = array_pop($GLOBALS['_xh']['valuestack']); |
|
707 | - $GLOBALS['_xh']['value'] = $curr_val['values']; |
|
708 | - $GLOBALS['_xh']['vt']=strtolower($name); |
|
709 | - if (isset($curr_val['php_class'])) |
|
710 | - { |
|
711 | - $GLOBALS['_xh']['php_class'] = $curr_val['php_class']; |
|
712 | - } |
|
713 | - break; |
|
714 | - case 'PARAM': |
|
715 | - // add to array of params the current value, |
|
716 | - // unless no VALUE was found |
|
717 | - if ($GLOBALS['_xh']['vt']) |
|
718 | - { |
|
719 | - $GLOBALS['_xh']['params'][]=$GLOBALS['_xh']['value']; |
|
720 | - $GLOBALS['_xh']['pt'][]=$GLOBALS['_xh']['vt']; |
|
721 | - } |
|
722 | - else |
|
723 | - error_log('XML-RPC: missing VALUE inside PARAM in received xml'); |
|
724 | - break; |
|
725 | - case 'METHODNAME': |
|
726 | - $GLOBALS['_xh']['method']=preg_replace('/^[\n\r\t ]+/', '', $GLOBALS['_xh']['ac']); |
|
727 | - break; |
|
728 | - case 'NIL': |
|
729 | - case 'EX:NIL': |
|
730 | - if ($GLOBALS['xmlrpc_null_extension']) |
|
731 | - { |
|
732 | - $GLOBALS['_xh']['vt']='null'; |
|
733 | - $GLOBALS['_xh']['value']=null; |
|
734 | - $GLOBALS['_xh']['lv']=3; |
|
735 | - break; |
|
736 | - } |
|
737 | - // drop through intentionally if nil extension not enabled |
|
738 | - case 'PARAMS': |
|
739 | - case 'FAULT': |
|
740 | - case 'METHODCALL': |
|
741 | - case 'METHORESPONSE': |
|
742 | - break; |
|
743 | - default: |
|
744 | - // End of INVALID ELEMENT! |
|
745 | - // shall we add an assert here for unreachable code??? |
|
746 | - break; |
|
747 | - } |
|
748 | - } |
|
749 | - } |
|
750 | - |
|
751 | - /// Used in decoding xmlrpc requests/responses without rebuilding xmlrpc values |
|
752 | - function xmlrpc_ee_fast($parser, $name) |
|
753 | - { |
|
754 | - xmlrpc_ee($parser, $name, false); |
|
755 | - } |
|
756 | - |
|
757 | - /// xml parser handler function for character data |
|
758 | - function xmlrpc_cd($parser, $data) |
|
759 | - { |
|
760 | - // skip processing if xml fault already detected |
|
761 | - if ($GLOBALS['_xh']['isf'] < 2) |
|
762 | - { |
|
763 | - // "lookforvalue==3" means that we've found an entire value |
|
764 | - // and should discard any further character data |
|
765 | - if($GLOBALS['_xh']['lv']!=3) |
|
766 | - { |
|
767 | - // G. Giunta 2006-08-23: useless change of 'lv' from 1 to 2 |
|
768 | - //if($GLOBALS['_xh']['lv']==1) |
|
769 | - //{ |
|
770 | - // if we've found text and we're just in a <value> then |
|
771 | - // say we've found a value |
|
772 | - //$GLOBALS['_xh']['lv']=2; |
|
773 | - //} |
|
774 | - // we always initialize the accumulator before starting parsing, anyway... |
|
775 | - //if(!@isset($GLOBALS['_xh']['ac'])) |
|
776 | - //{ |
|
777 | - // $GLOBALS['_xh']['ac'] = ''; |
|
778 | - //} |
|
779 | - $GLOBALS['_xh']['ac'].=$data; |
|
780 | - } |
|
781 | - } |
|
782 | - } |
|
783 | - |
|
784 | - /// xml parser handler function for 'other stuff', ie. not char data or |
|
785 | - /// element start/end tag. In fact it only gets called on unknown entities... |
|
786 | - function xmlrpc_dh($parser, $data) |
|
787 | - { |
|
788 | - // skip processing if xml fault already detected |
|
789 | - if ($GLOBALS['_xh']['isf'] < 2) |
|
790 | - { |
|
791 | - if(substr($data, 0, 1) == '&' && substr($data, -1, 1) == ';') |
|
792 | - { |
|
793 | - // G. Giunta 2006-08-25: useless change of 'lv' from 1 to 2 |
|
794 | - //if($GLOBALS['_xh']['lv']==1) |
|
795 | - //{ |
|
796 | - // $GLOBALS['_xh']['lv']=2; |
|
797 | - //} |
|
798 | - $GLOBALS['_xh']['ac'].=$data; |
|
799 | - } |
|
800 | - } |
|
801 | - return true; |
|
802 | - } |
|
803 | - |
|
804 | - class xmlrpc_client |
|
805 | - { |
|
806 | - var $path; |
|
807 | - var $server; |
|
808 | - var $port=0; |
|
809 | - var $method='http'; |
|
810 | - var $errno; |
|
811 | - var $errstr; |
|
812 | - var $debug=0; |
|
813 | - var $username=''; |
|
814 | - var $password=''; |
|
815 | - var $authtype=1; |
|
816 | - var $cert=''; |
|
817 | - var $certpass=''; |
|
818 | - var $cacert=''; |
|
819 | - var $cacertdir=''; |
|
820 | - var $key=''; |
|
821 | - var $keypass=''; |
|
822 | - var $verifypeer=true; |
|
823 | - var $verifyhost=1; |
|
824 | - var $no_multicall=false; |
|
825 | - var $proxy=''; |
|
826 | - var $proxyport=0; |
|
827 | - var $proxy_user=''; |
|
828 | - var $proxy_pass=''; |
|
829 | - var $proxy_authtype=1; |
|
830 | - var $cookies=array(); |
|
831 | - var $extracurlopts=array(); |
|
832 | - |
|
833 | - /** |
|
834 | - * List of http compression methods accepted by the client for responses. |
|
835 | - * NB: PHP supports deflate, gzip compressions out of the box if compiled w. zlib |
|
836 | - * |
|
837 | - * NNB: you can set it to any non-empty array for HTTP11 and HTTPS, since |
|
838 | - * in those cases it will be up to CURL to decide the compression methods |
|
839 | - * it supports. You might check for the presence of 'zlib' in the output of |
|
840 | - * curl_version() to determine wheter compression is supported or not |
|
841 | - */ |
|
842 | - var $accepted_compression = array(); |
|
843 | - /** |
|
844 | - * Name of compression scheme to be used for sending requests. |
|
845 | - * Either null, gzip or deflate |
|
846 | - */ |
|
847 | - var $request_compression = ''; |
|
848 | - /** |
|
849 | - * CURL handle: used for keep-alive connections (PHP 4.3.8 up, see: |
|
850 | - * http://curl.haxx.se/docs/faq.html#7.3) |
|
851 | - */ |
|
852 | - var $xmlrpc_curl_handle = null; |
|
853 | - /// Whether to use persistent connections for http 1.1 and https |
|
854 | - var $keepalive = false; |
|
855 | - /// Charset encodings that can be decoded without problems by the client |
|
856 | - var $accepted_charset_encodings = array(); |
|
857 | - /// Charset encoding to be used in serializing request. NULL = use ASCII |
|
858 | - var $request_charset_encoding = ''; |
|
859 | - /** |
|
860 | - * Decides the content of xmlrpcresp objects returned by calls to send() |
|
861 | - * valid strings are 'xmlrpcvals', 'phpvals' or 'xml' |
|
862 | - */ |
|
863 | - var $return_type = 'xmlrpcvals'; |
|
864 | - /** |
|
865 | - * Sent to servers in http headers |
|
866 | - */ |
|
867 | - var $user_agent; |
|
868 | - |
|
869 | - /** |
|
870 | - * @param string $path either the complete server URL or the PATH part of the xmlrc server URL, e.g. /xmlrpc/server.php |
|
871 | - * @param string $server the server name / ip address |
|
872 | - * @param integer $port the port the server is listening on, defaults to 80 or 443 depending on protocol used |
|
873 | - * @param string $method the http protocol variant: defaults to 'http', 'https' and 'http11' can be used if CURL is installed |
|
874 | - */ |
|
875 | - function __construct($path, $server='', $port='', $method='') |
|
876 | - { |
|
877 | - // allow user to specify all params in $path |
|
878 | - if($server == '' and $port == '' and $method == '') |
|
879 | - { |
|
880 | - $parts = parse_url($path); |
|
881 | - $server = $parts['host']; |
|
882 | - $path = isset($parts['path']) ? $parts['path'] : ''; |
|
883 | - if(isset($parts['query'])) |
|
884 | - { |
|
885 | - $path .= '?'.$parts['query']; |
|
886 | - } |
|
887 | - if(isset($parts['fragment'])) |
|
888 | - { |
|
889 | - $path .= '#'.$parts['fragment']; |
|
890 | - } |
|
891 | - if(isset($parts['port'])) |
|
892 | - { |
|
893 | - $port = $parts['port']; |
|
894 | - } |
|
895 | - if(isset($parts['scheme'])) |
|
896 | - { |
|
897 | - $method = $parts['scheme']; |
|
898 | - } |
|
899 | - if(isset($parts['user'])) |
|
900 | - { |
|
901 | - $this->username = $parts['user']; |
|
902 | - } |
|
903 | - if(isset($parts['pass'])) |
|
904 | - { |
|
905 | - $this->password = $parts['pass']; |
|
906 | - } |
|
907 | - } |
|
908 | - if($path == '' || $path[0] != '/') |
|
909 | - { |
|
910 | - $this->path='/'.$path; |
|
911 | - } |
|
912 | - else |
|
913 | - { |
|
914 | - $this->path=$path; |
|
915 | - } |
|
916 | - $this->server=$server; |
|
917 | - if($port != '') |
|
918 | - { |
|
919 | - $this->port=$port; |
|
920 | - } |
|
921 | - if($method != '') |
|
922 | - { |
|
923 | - $this->method=$method; |
|
924 | - } |
|
925 | - |
|
926 | - // if ZLIB is enabled, let the client by default accept compressed responses |
|
927 | - if(function_exists('gzinflate') || ( |
|
928 | - function_exists('curl_init') && (($info = curl_version()) && |
|
929 | - ((is_string($info) && strpos($info, 'zlib') !== null) || isset($info['libz_version']))) |
|
930 | - )) |
|
931 | - { |
|
932 | - $this->accepted_compression = array('gzip', 'deflate'); |
|
933 | - } |
|
934 | - |
|
935 | - // keepalives: enabled by default |
|
936 | - $this->keepalive = true; |
|
937 | - |
|
938 | - // by default the xml parser can support these 3 charset encodings |
|
939 | - $this->accepted_charset_encodings = array('UTF-8', 'ISO-8859-1', 'US-ASCII'); |
|
940 | - |
|
941 | - // initialize user_agent string |
|
942 | - $this->user_agent = $GLOBALS['xmlrpcName'] . ' ' . $GLOBALS['xmlrpcVersion']; |
|
943 | - } |
|
944 | - |
|
945 | - /** |
|
946 | - * @deprecated |
|
947 | - */ |
|
948 | - function xmlrpc_client($path, $server='', $port='', $method='') |
|
949 | - { |
|
950 | - self::__construct($path, $server, $port, $method); |
|
951 | - } |
|
952 | - |
|
953 | - /** |
|
954 | - * Enables/disables the echoing to screen of the xmlrpc responses received |
|
955 | - * @param integer $in values 0, 1 and 2 are supported (2 = echo sent msg too, before received response) |
|
956 | - * @access public |
|
957 | - */ |
|
958 | - function setDebug($in) |
|
959 | - { |
|
960 | - $this->debug=$in; |
|
961 | - } |
|
962 | - |
|
963 | - /** |
|
964 | - * Add some http BASIC AUTH credentials, used by the client to authenticate |
|
965 | - * @param string $u username |
|
966 | - * @param string $p password |
|
967 | - * @param integer $t auth type. See curl_setopt man page for supported auth types. Defaults to CURLAUTH_BASIC (basic auth) |
|
968 | - * @access public |
|
969 | - */ |
|
970 | - function setCredentials($u, $p, $t=1) |
|
971 | - { |
|
972 | - $this->username=$u; |
|
973 | - $this->password=$p; |
|
974 | - $this->authtype=$t; |
|
975 | - } |
|
976 | - |
|
977 | - /** |
|
978 | - * Add a client-side https certificate |
|
979 | - * @param string $cert |
|
980 | - * @param string $certpass |
|
981 | - * @access public |
|
982 | - */ |
|
983 | - function setCertificate($cert, $certpass) |
|
984 | - { |
|
985 | - $this->cert = $cert; |
|
986 | - $this->certpass = $certpass; |
|
987 | - } |
|
988 | - |
|
989 | - /** |
|
990 | - * Add a CA certificate to verify server with (see man page about |
|
991 | - * CURLOPT_CAINFO for more details) |
|
992 | - * @param string $cacert certificate file name (or dir holding certificates) |
|
993 | - * @param bool $is_dir set to true to indicate cacert is a dir. defaults to false |
|
994 | - * @access public |
|
995 | - */ |
|
996 | - function setCaCertificate($cacert, $is_dir=false) |
|
997 | - { |
|
998 | - if ($is_dir) |
|
999 | - { |
|
1000 | - $this->cacertdir = $cacert; |
|
1001 | - } |
|
1002 | - else |
|
1003 | - { |
|
1004 | - $this->cacert = $cacert; |
|
1005 | - } |
|
1006 | - } |
|
1007 | - |
|
1008 | - /** |
|
1009 | - * Set attributes for SSL communication: private SSL key |
|
1010 | - * NB: does not work in older php/curl installs |
|
1011 | - * Thanks to Daniel Convissor |
|
1012 | - * @param string $key The name of a file containing a private SSL key |
|
1013 | - * @param string $keypass The secret password needed to use the private SSL key |
|
1014 | - * @access public |
|
1015 | - */ |
|
1016 | - function setKey($key, $keypass) |
|
1017 | - { |
|
1018 | - $this->key = $key; |
|
1019 | - $this->keypass = $keypass; |
|
1020 | - } |
|
1021 | - |
|
1022 | - /** |
|
1023 | - * Set attributes for SSL communication: verify server certificate |
|
1024 | - * @param bool $i enable/disable verification of peer certificate |
|
1025 | - * @access public |
|
1026 | - */ |
|
1027 | - function setSSLVerifyPeer($i) |
|
1028 | - { |
|
1029 | - $this->verifypeer = $i; |
|
1030 | - } |
|
1031 | - |
|
1032 | - /** |
|
1033 | - * Set attributes for SSL communication: verify match of server cert w. hostname |
|
1034 | - * @param int $i |
|
1035 | - * @access public |
|
1036 | - */ |
|
1037 | - function setSSLVerifyHost($i) |
|
1038 | - { |
|
1039 | - $this->verifyhost = $i; |
|
1040 | - } |
|
1041 | - |
|
1042 | - /** |
|
1043 | - * Set proxy info |
|
1044 | - * @param string $proxyhost |
|
1045 | - * @param string $proxyport Defaults to 8080 for HTTP and 443 for HTTPS |
|
1046 | - * @param string $proxyusername Leave blank if proxy has public access |
|
1047 | - * @param string $proxypassword Leave blank if proxy has public access |
|
1048 | - * @param int $proxyauthtype set to constant CURLAUTH_NTLM to use NTLM auth with proxy |
|
1049 | - * @access public |
|
1050 | - */ |
|
1051 | - function setProxy($proxyhost, $proxyport, $proxyusername = '', $proxypassword = '', $proxyauthtype = 1) |
|
1052 | - { |
|
1053 | - $this->proxy = $proxyhost; |
|
1054 | - $this->proxyport = $proxyport; |
|
1055 | - $this->proxy_user = $proxyusername; |
|
1056 | - $this->proxy_pass = $proxypassword; |
|
1057 | - $this->proxy_authtype = $proxyauthtype; |
|
1058 | - } |
|
1059 | - |
|
1060 | - /** |
|
1061 | - * Enables/disables reception of compressed xmlrpc responses. |
|
1062 | - * Note that enabling reception of compressed responses merely adds some standard |
|
1063 | - * http headers to xmlrpc requests. It is up to the xmlrpc server to return |
|
1064 | - * compressed responses when receiving such requests. |
|
1065 | - * @param string $compmethod either 'gzip', 'deflate', 'any' or '' |
|
1066 | - * @access public |
|
1067 | - */ |
|
1068 | - function setAcceptedCompression($compmethod) |
|
1069 | - { |
|
1070 | - if ($compmethod == 'any') |
|
1071 | - $this->accepted_compression = array('gzip', 'deflate'); |
|
1072 | - else |
|
1073 | - if ($compmethod == false ) |
|
1074 | - $this->accepted_compression = array(); |
|
1075 | - else |
|
1076 | - $this->accepted_compression = array($compmethod); |
|
1077 | - } |
|
1078 | - |
|
1079 | - /** |
|
1080 | - * Enables/disables http compression of xmlrpc request. |
|
1081 | - * Take care when sending compressed requests: servers might not support them |
|
1082 | - * (and automatic fallback to uncompressed requests is not yet implemented) |
|
1083 | - * @param string $compmethod either 'gzip', 'deflate' or '' |
|
1084 | - * @access public |
|
1085 | - */ |
|
1086 | - function setRequestCompression($compmethod) |
|
1087 | - { |
|
1088 | - $this->request_compression = $compmethod; |
|
1089 | - } |
|
1090 | - |
|
1091 | - /** |
|
1092 | - * Adds a cookie to list of cookies that will be sent to server. |
|
1093 | - * NB: setting any param but name and value will turn the cookie into a 'version 1' cookie: |
|
1094 | - * do not do it unless you know what you are doing |
|
1095 | - * @param string $name |
|
1096 | - * @param string $value |
|
1097 | - * @param string $path |
|
1098 | - * @param string $domain |
|
1099 | - * @param int $port |
|
1100 | - * @access public |
|
1101 | - * |
|
1102 | - * @todo check correctness of urlencoding cookie value (copied from php way of doing it...) |
|
1103 | - */ |
|
1104 | - function setCookie($name, $value='', $path='', $domain='', $port=null) |
|
1105 | - { |
|
1106 | - $this->cookies[$name]['value'] = urlencode($value); |
|
1107 | - if ($path || $domain || $port) |
|
1108 | - { |
|
1109 | - $this->cookies[$name]['path'] = $path; |
|
1110 | - $this->cookies[$name]['domain'] = $domain; |
|
1111 | - $this->cookies[$name]['port'] = $port; |
|
1112 | - $this->cookies[$name]['version'] = 1; |
|
1113 | - } |
|
1114 | - else |
|
1115 | - { |
|
1116 | - $this->cookies[$name]['version'] = 0; |
|
1117 | - } |
|
1118 | - } |
|
1119 | - |
|
1120 | - /** |
|
1121 | - * Directly set cURL options, for extra flexibility |
|
1122 | - * It allows eg. to bind client to a specific IP interface / address |
|
1123 | - * @param array $options |
|
1124 | - */ |
|
1125 | - function SetCurlOptions( $options ) |
|
1126 | - { |
|
1127 | - $this->extracurlopts = $options; |
|
1128 | - } |
|
1129 | - |
|
1130 | - /** |
|
1131 | - * Set user-agent string that will be used by this client instance |
|
1132 | - * in http headers sent to the server |
|
1133 | - */ |
|
1134 | - function SetUserAgent( $agentstring ) |
|
1135 | - { |
|
1136 | - $this->user_agent = $agentstring; |
|
1137 | - } |
|
1138 | - |
|
1139 | - /** |
|
1140 | - * Send an xmlrpc request |
|
1141 | - * @param mixed $msg The message object, or an array of messages for using multicall, or the complete xml representation of a request |
|
1142 | - * @param integer $timeout Connection timeout, in seconds, If unspecified, a platform specific timeout will apply |
|
1143 | - * @param string $method if left unspecified, the http protocol chosen during creation of the object will be used |
|
1144 | - * @return xmlrpcresp |
|
1145 | - * @access public |
|
1146 | - */ |
|
1147 | - function& send($msg, $timeout=0, $method='') |
|
1148 | - { |
|
1149 | - // if user deos not specify http protocol, use native method of this client |
|
1150 | - // (i.e. method set during call to constructor) |
|
1151 | - if($method == '') |
|
1152 | - { |
|
1153 | - $method = $this->method; |
|
1154 | - } |
|
1155 | - |
|
1156 | - if(is_array($msg)) |
|
1157 | - { |
|
1158 | - // $msg is an array of xmlrpcmsg's |
|
1159 | - $r = $this->multicall($msg, $timeout, $method); |
|
1160 | - return $r; |
|
1161 | - } |
|
1162 | - elseif(is_string($msg)) |
|
1163 | - { |
|
1164 | - $n = new xmlrpcmsg(''); |
|
1165 | - $n->payload = $msg; |
|
1166 | - $msg = $n; |
|
1167 | - } |
|
1168 | - |
|
1169 | - // where msg is an xmlrpcmsg |
|
1170 | - $msg->debug=$this->debug; |
|
1171 | - |
|
1172 | - if($method == 'https') |
|
1173 | - { |
|
1174 | - $r =& $this->sendPayloadHTTPS( |
|
1175 | - $msg, |
|
1176 | - $this->server, |
|
1177 | - $this->port, |
|
1178 | - $timeout, |
|
1179 | - $this->username, |
|
1180 | - $this->password, |
|
1181 | - $this->authtype, |
|
1182 | - $this->cert, |
|
1183 | - $this->certpass, |
|
1184 | - $this->cacert, |
|
1185 | - $this->cacertdir, |
|
1186 | - $this->proxy, |
|
1187 | - $this->proxyport, |
|
1188 | - $this->proxy_user, |
|
1189 | - $this->proxy_pass, |
|
1190 | - $this->proxy_authtype, |
|
1191 | - $this->keepalive, |
|
1192 | - $this->key, |
|
1193 | - $this->keypass |
|
1194 | - ); |
|
1195 | - } |
|
1196 | - elseif($method == 'http11') |
|
1197 | - { |
|
1198 | - $r =& $this->sendPayloadCURL( |
|
1199 | - $msg, |
|
1200 | - $this->server, |
|
1201 | - $this->port, |
|
1202 | - $timeout, |
|
1203 | - $this->username, |
|
1204 | - $this->password, |
|
1205 | - $this->authtype, |
|
1206 | - null, |
|
1207 | - null, |
|
1208 | - null, |
|
1209 | - null, |
|
1210 | - $this->proxy, |
|
1211 | - $this->proxyport, |
|
1212 | - $this->proxy_user, |
|
1213 | - $this->proxy_pass, |
|
1214 | - $this->proxy_authtype, |
|
1215 | - 'http', |
|
1216 | - $this->keepalive |
|
1217 | - ); |
|
1218 | - } |
|
1219 | - else |
|
1220 | - { |
|
1221 | - $r =& $this->sendPayloadHTTP10( |
|
1222 | - $msg, |
|
1223 | - $this->server, |
|
1224 | - $this->port, |
|
1225 | - $timeout, |
|
1226 | - $this->username, |
|
1227 | - $this->password, |
|
1228 | - $this->authtype, |
|
1229 | - $this->proxy, |
|
1230 | - $this->proxyport, |
|
1231 | - $this->proxy_user, |
|
1232 | - $this->proxy_pass, |
|
1233 | - $this->proxy_authtype |
|
1234 | - ); |
|
1235 | - } |
|
1236 | - |
|
1237 | - return $r; |
|
1238 | - } |
|
1239 | - |
|
1240 | - /** |
|
1241 | - * @access private |
|
1242 | - */ |
|
1243 | - function &sendPayloadHTTP10($msg, $server, $port, $timeout=0, |
|
1244 | - $username='', $password='', $authtype=1, $proxyhost='', |
|
1245 | - $proxyport=0, $proxyusername='', $proxypassword='', $proxyauthtype=1) |
|
1246 | - { |
|
1247 | - if($port==0) |
|
1248 | - { |
|
1249 | - $port=80; |
|
1250 | - } |
|
1251 | - |
|
1252 | - // Only create the payload if it was not created previously |
|
1253 | - if(empty($msg->payload)) |
|
1254 | - { |
|
1255 | - $msg->createPayload($this->request_charset_encoding); |
|
1256 | - } |
|
1257 | - |
|
1258 | - $payload = $msg->payload; |
|
1259 | - // Deflate request body and set appropriate request headers |
|
1260 | - if(function_exists('gzdeflate') && ($this->request_compression == 'gzip' || $this->request_compression == 'deflate')) |
|
1261 | - { |
|
1262 | - if($this->request_compression == 'gzip') |
|
1263 | - { |
|
1264 | - $a = @gzencode($payload); |
|
1265 | - if($a) |
|
1266 | - { |
|
1267 | - $payload = $a; |
|
1268 | - $encoding_hdr = "Content-Encoding: gzip\r\n"; |
|
1269 | - } |
|
1270 | - } |
|
1271 | - else |
|
1272 | - { |
|
1273 | - $a = @gzcompress($payload); |
|
1274 | - if($a) |
|
1275 | - { |
|
1276 | - $payload = $a; |
|
1277 | - $encoding_hdr = "Content-Encoding: deflate\r\n"; |
|
1278 | - } |
|
1279 | - } |
|
1280 | - } |
|
1281 | - else |
|
1282 | - { |
|
1283 | - $encoding_hdr = ''; |
|
1284 | - } |
|
1285 | - |
|
1286 | - // thanks to Grant Rauscher <[email protected]> for this |
|
1287 | - $credentials=''; |
|
1288 | - if($username!='') |
|
1289 | - { |
|
1290 | - $credentials='Authorization: Basic ' . base64_encode($username . ':' . $password) . "\r\n"; |
|
1291 | - if ($authtype != 1) |
|
1292 | - { |
|
1293 | - error_log('XML-RPC: '.__METHOD__.': warning. Only Basic auth is supported with HTTP 1.0'); |
|
1294 | - } |
|
1295 | - } |
|
1296 | - |
|
1297 | - $accepted_encoding = ''; |
|
1298 | - if(is_array($this->accepted_compression) && count($this->accepted_compression)) |
|
1299 | - { |
|
1300 | - $accepted_encoding = 'Accept-Encoding: ' . implode(', ', $this->accepted_compression) . "\r\n"; |
|
1301 | - } |
|
1302 | - |
|
1303 | - $proxy_credentials = ''; |
|
1304 | - if($proxyhost) |
|
1305 | - { |
|
1306 | - if($proxyport == 0) |
|
1307 | - { |
|
1308 | - $proxyport = 8080; |
|
1309 | - } |
|
1310 | - $connectserver = $proxyhost; |
|
1311 | - $connectport = $proxyport; |
|
1312 | - $uri = 'http://'.$server.':'.$port.$this->path; |
|
1313 | - if($proxyusername != '') |
|
1314 | - { |
|
1315 | - if ($proxyauthtype != 1) |
|
1316 | - { |
|
1317 | - error_log('XML-RPC: '.__METHOD__.': warning. Only Basic auth to proxy is supported with HTTP 1.0'); |
|
1318 | - } |
|
1319 | - $proxy_credentials = 'Proxy-Authorization: Basic ' . base64_encode($proxyusername.':'.$proxypassword) . "\r\n"; |
|
1320 | - } |
|
1321 | - } |
|
1322 | - else |
|
1323 | - { |
|
1324 | - $connectserver = $server; |
|
1325 | - $connectport = $port; |
|
1326 | - $uri = $this->path; |
|
1327 | - } |
|
1328 | - |
|
1329 | - // Cookie generation, as per rfc2965 (version 1 cookies) or |
|
1330 | - // netscape's rules (version 0 cookies) |
|
1331 | - $cookieheader=''; |
|
1332 | - if (count($this->cookies)) |
|
1333 | - { |
|
1334 | - $version = ''; |
|
1335 | - foreach ($this->cookies as $name => $cookie) |
|
1336 | - { |
|
1337 | - if ($cookie['version']) |
|
1338 | - { |
|
1339 | - $version = ' $Version="' . $cookie['version'] . '";'; |
|
1340 | - $cookieheader .= ' ' . $name . '="' . $cookie['value'] . '";'; |
|
1341 | - if ($cookie['path']) |
|
1342 | - $cookieheader .= ' $Path="' . $cookie['path'] . '";'; |
|
1343 | - if ($cookie['domain']) |
|
1344 | - $cookieheader .= ' $Domain="' . $cookie['domain'] . '";'; |
|
1345 | - if ($cookie['port']) |
|
1346 | - $cookieheader .= ' $Port="' . $cookie['port'] . '";'; |
|
1347 | - } |
|
1348 | - else |
|
1349 | - { |
|
1350 | - $cookieheader .= ' ' . $name . '=' . $cookie['value'] . ";"; |
|
1351 | - } |
|
1352 | - } |
|
1353 | - $cookieheader = 'Cookie:' . $version . substr($cookieheader, 0, -1) . "\r\n"; |
|
1354 | - } |
|
1355 | - |
|
1356 | - // omit port if 80 |
|
1357 | - $port = ($port == 80) ? '' : (':' . $port); |
|
1358 | - |
|
1359 | - $op= 'POST ' . $uri. " HTTP/1.0\r\n" . |
|
1360 | - 'User-Agent: ' . $this->user_agent . "\r\n" . |
|
1361 | - 'Host: '. $server . $port . "\r\n" . |
|
1362 | - $credentials . |
|
1363 | - $proxy_credentials . |
|
1364 | - $accepted_encoding . |
|
1365 | - $encoding_hdr . |
|
1366 | - 'Accept-Charset: ' . implode(',', $this->accepted_charset_encodings) . "\r\n" . |
|
1367 | - $cookieheader . |
|
1368 | - 'Content-Type: ' . $msg->content_type . "\r\nContent-Length: " . |
|
1369 | - strlen($payload) . "\r\n\r\n" . |
|
1370 | - $payload; |
|
1371 | - |
|
1372 | - if($this->debug > 1) |
|
1373 | - { |
|
1374 | - print "<PRE>\n---SENDING---\n" . htmlentities($op) . "\n---END---\n</PRE>"; |
|
1375 | - // let the client see this now in case http times out... |
|
1376 | - flush(); |
|
1377 | - } |
|
1378 | - |
|
1379 | - if($timeout>0) |
|
1380 | - { |
|
1381 | - $fp=@fsockopen($connectserver, $connectport, $this->errno, $this->errstr, $timeout); |
|
1382 | - } |
|
1383 | - else |
|
1384 | - { |
|
1385 | - $fp=@fsockopen($connectserver, $connectport, $this->errno, $this->errstr); |
|
1386 | - } |
|
1387 | - if($fp) |
|
1388 | - { |
|
1389 | - if($timeout>0 && function_exists('stream_set_timeout')) |
|
1390 | - { |
|
1391 | - stream_set_timeout($fp, $timeout); |
|
1392 | - } |
|
1393 | - } |
|
1394 | - else |
|
1395 | - { |
|
1396 | - $this->errstr='Connect error: '.$this->errstr; |
|
1397 | - $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['http_error'], $this->errstr . ' (' . $this->errno . ')'); |
|
1398 | - return $r; |
|
1399 | - } |
|
1400 | - |
|
1401 | - if(!fputs($fp, $op, strlen($op))) |
|
1402 | - { |
|
1403 | - fclose($fp); |
|
1404 | - $this->errstr='Write error'; |
|
1405 | - $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['http_error'], $this->errstr); |
|
1406 | - return $r; |
|
1407 | - } |
|
1408 | - else |
|
1409 | - { |
|
1410 | - // reset errno and errstr on successful socket connection |
|
1411 | - $this->errstr = ''; |
|
1412 | - } |
|
1413 | - // G. Giunta 2005/10/24: close socket before parsing. |
|
1414 | - // should yield slightly better execution times, and make easier recursive calls (e.g. to follow http redirects) |
|
1415 | - $ipd=''; |
|
1416 | - do |
|
1417 | - { |
|
1418 | - // shall we check for $data === FALSE? |
|
1419 | - // as per the manual, it signals an error |
|
1420 | - $ipd.=fread($fp, 32768); |
|
1421 | - } while(!feof($fp)); |
|
1422 | - fclose($fp); |
|
1423 | - $r =& $msg->parseResponse($ipd, false, $this->return_type); |
|
1424 | - return $r; |
|
1425 | - |
|
1426 | - } |
|
1427 | - |
|
1428 | - /** |
|
1429 | - * @access private |
|
1430 | - */ |
|
1431 | - function &sendPayloadHTTPS($msg, $server, $port, $timeout=0, $username='', |
|
1432 | - $password='', $authtype=1, $cert='',$certpass='', $cacert='', $cacertdir='', |
|
1433 | - $proxyhost='', $proxyport=0, $proxyusername='', $proxypassword='', $proxyauthtype=1, |
|
1434 | - $keepalive=false, $key='', $keypass='') |
|
1435 | - { |
|
1436 | - $r =& $this->sendPayloadCURL($msg, $server, $port, $timeout, $username, |
|
1437 | - $password, $authtype, $cert, $certpass, $cacert, $cacertdir, $proxyhost, $proxyport, |
|
1438 | - $proxyusername, $proxypassword, $proxyauthtype, 'https', $keepalive, $key, $keypass); |
|
1439 | - return $r; |
|
1440 | - } |
|
1441 | - |
|
1442 | - /** |
|
1443 | - * Contributed by Justin Miller <[email protected]> |
|
1444 | - * Requires curl to be built into PHP |
|
1445 | - * NB: CURL versions before 7.11.10 cannot use proxy to talk to https servers! |
|
1446 | - * @access private |
|
1447 | - */ |
|
1448 | - function &sendPayloadCURL($msg, $server, $port, $timeout=0, $username='', |
|
1449 | - $password='', $authtype=1, $cert='', $certpass='', $cacert='', $cacertdir='', |
|
1450 | - $proxyhost='', $proxyport=0, $proxyusername='', $proxypassword='', $proxyauthtype=1, $method='https', |
|
1451 | - $keepalive=false, $key='', $keypass='') |
|
1452 | - { |
|
1453 | - if(!function_exists('curl_init')) |
|
1454 | - { |
|
1455 | - $this->errstr='CURL unavailable on this install'; |
|
1456 | - $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['no_curl'], $GLOBALS['xmlrpcstr']['no_curl']); |
|
1457 | - return $r; |
|
1458 | - } |
|
1459 | - if($method == 'https') |
|
1460 | - { |
|
1461 | - if(($info = curl_version()) && |
|
1462 | - ((is_string($info) && strpos($info, 'OpenSSL') === null) || (is_array($info) && !isset($info['ssl_version'])))) |
|
1463 | - { |
|
1464 | - $this->errstr='SSL unavailable on this install'; |
|
1465 | - $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['no_ssl'], $GLOBALS['xmlrpcstr']['no_ssl']); |
|
1466 | - return $r; |
|
1467 | - } |
|
1468 | - } |
|
1469 | - |
|
1470 | - if($port == 0) |
|
1471 | - { |
|
1472 | - if($method == 'http') |
|
1473 | - { |
|
1474 | - $port = 80; |
|
1475 | - } |
|
1476 | - else |
|
1477 | - { |
|
1478 | - $port = 443; |
|
1479 | - } |
|
1480 | - } |
|
1481 | - |
|
1482 | - // Only create the payload if it was not created previously |
|
1483 | - if(empty($msg->payload)) |
|
1484 | - { |
|
1485 | - $msg->createPayload($this->request_charset_encoding); |
|
1486 | - } |
|
1487 | - |
|
1488 | - // Deflate request body and set appropriate request headers |
|
1489 | - $payload = $msg->payload; |
|
1490 | - if(function_exists('gzdeflate') && ($this->request_compression == 'gzip' || $this->request_compression == 'deflate')) |
|
1491 | - { |
|
1492 | - if($this->request_compression == 'gzip') |
|
1493 | - { |
|
1494 | - $a = @gzencode($payload); |
|
1495 | - if($a) |
|
1496 | - { |
|
1497 | - $payload = $a; |
|
1498 | - $encoding_hdr = 'Content-Encoding: gzip'; |
|
1499 | - } |
|
1500 | - } |
|
1501 | - else |
|
1502 | - { |
|
1503 | - $a = @gzcompress($payload); |
|
1504 | - if($a) |
|
1505 | - { |
|
1506 | - $payload = $a; |
|
1507 | - $encoding_hdr = 'Content-Encoding: deflate'; |
|
1508 | - } |
|
1509 | - } |
|
1510 | - } |
|
1511 | - else |
|
1512 | - { |
|
1513 | - $encoding_hdr = ''; |
|
1514 | - } |
|
1515 | - |
|
1516 | - if($this->debug > 1) |
|
1517 | - { |
|
1518 | - print "<PRE>\n---SENDING---\n" . htmlentities($payload) . "\n---END---\n</PRE>"; |
|
1519 | - // let the client see this now in case http times out... |
|
1520 | - flush(); |
|
1521 | - } |
|
1522 | - |
|
1523 | - if(!$keepalive || !$this->xmlrpc_curl_handle) |
|
1524 | - { |
|
1525 | - $curl = curl_init($method . '://' . $server . ':' . $port . $this->path); |
|
1526 | - if($keepalive) |
|
1527 | - { |
|
1528 | - $this->xmlrpc_curl_handle = $curl; |
|
1529 | - } |
|
1530 | - } |
|
1531 | - else |
|
1532 | - { |
|
1533 | - $curl = $this->xmlrpc_curl_handle; |
|
1534 | - } |
|
1535 | - |
|
1536 | - // results into variable |
|
1537 | - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
|
1538 | - |
|
1539 | - if($this->debug) |
|
1540 | - { |
|
1541 | - curl_setopt($curl, CURLOPT_VERBOSE, 1); |
|
1542 | - } |
|
1543 | - curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent); |
|
1544 | - // required for XMLRPC: post the data |
|
1545 | - curl_setopt($curl, CURLOPT_POST, 1); |
|
1546 | - // the data |
|
1547 | - curl_setopt($curl, CURLOPT_POSTFIELDS, $payload); |
|
1548 | - |
|
1549 | - // return the header too |
|
1550 | - curl_setopt($curl, CURLOPT_HEADER, 1); |
|
1551 | - |
|
1552 | - // NB: if we set an empty string, CURL will add http header indicating |
|
1553 | - // ALL methods it is supporting. This is possibly a better option than |
|
1554 | - // letting the user tell what curl can / cannot do... |
|
1555 | - if(is_array($this->accepted_compression) && count($this->accepted_compression)) |
|
1556 | - { |
|
1557 | - //curl_setopt($curl, CURLOPT_ENCODING, implode(',', $this->accepted_compression)); |
|
1558 | - // empty string means 'any supported by CURL' (shall we catch errors in case CURLOPT_SSLKEY undefined ?) |
|
1559 | - if (count($this->accepted_compression) == 1) |
|
1560 | - { |
|
1561 | - curl_setopt($curl, CURLOPT_ENCODING, $this->accepted_compression[0]); |
|
1562 | - } |
|
1563 | - else |
|
1564 | - curl_setopt($curl, CURLOPT_ENCODING, ''); |
|
1565 | - } |
|
1566 | - // extra headers |
|
1567 | - $headers = array('Content-Type: ' . $msg->content_type , 'Accept-Charset: ' . implode(',', $this->accepted_charset_encodings)); |
|
1568 | - // if no keepalive is wanted, let the server know it in advance |
|
1569 | - if(!$keepalive) |
|
1570 | - { |
|
1571 | - $headers[] = 'Connection: close'; |
|
1572 | - } |
|
1573 | - // request compression header |
|
1574 | - if($encoding_hdr) |
|
1575 | - { |
|
1576 | - $headers[] = $encoding_hdr; |
|
1577 | - } |
|
1578 | - |
|
1579 | - curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); |
|
1580 | - // timeout is borked |
|
1581 | - if($timeout) |
|
1582 | - { |
|
1583 | - curl_setopt($curl, CURLOPT_TIMEOUT, $timeout == 1 ? 1 : $timeout - 1); |
|
1584 | - } |
|
1585 | - |
|
1586 | - if($username && $password) |
|
1587 | - { |
|
1588 | - curl_setopt($curl, CURLOPT_USERPWD, $username.':'.$password); |
|
1589 | - if (defined('CURLOPT_HTTPAUTH')) |
|
1590 | - { |
|
1591 | - curl_setopt($curl, CURLOPT_HTTPAUTH, $authtype); |
|
1592 | - } |
|
1593 | - else if ($authtype != 1) |
|
1594 | - { |
|
1595 | - error_log('XML-RPC: '.__METHOD__.': warning. Only Basic auth is supported by the current PHP/curl install'); |
|
1596 | - } |
|
1597 | - } |
|
1598 | - |
|
1599 | - if($method == 'https') |
|
1600 | - { |
|
1601 | - // set cert file |
|
1602 | - if($cert) |
|
1603 | - { |
|
1604 | - curl_setopt($curl, CURLOPT_SSLCERT, $cert); |
|
1605 | - } |
|
1606 | - // set cert password |
|
1607 | - if($certpass) |
|
1608 | - { |
|
1609 | - curl_setopt($curl, CURLOPT_SSLCERTPASSWD, $certpass); |
|
1610 | - } |
|
1611 | - // whether to verify remote host's cert |
|
1612 | - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->verifypeer); |
|
1613 | - // set ca certificates file/dir |
|
1614 | - if($cacert) |
|
1615 | - { |
|
1616 | - curl_setopt($curl, CURLOPT_CAINFO, $cacert); |
|
1617 | - } |
|
1618 | - if($cacertdir) |
|
1619 | - { |
|
1620 | - curl_setopt($curl, CURLOPT_CAPATH, $cacertdir); |
|
1621 | - } |
|
1622 | - // set key file (shall we catch errors in case CURLOPT_SSLKEY undefined ?) |
|
1623 | - if($key) |
|
1624 | - { |
|
1625 | - curl_setopt($curl, CURLOPT_SSLKEY, $key); |
|
1626 | - } |
|
1627 | - // set key password (shall we catch errors in case CURLOPT_SSLKEY undefined ?) |
|
1628 | - if($keypass) |
|
1629 | - { |
|
1630 | - curl_setopt($curl, CURLOPT_SSLKEYPASSWD, $keypass); |
|
1631 | - } |
|
1632 | - |
|
1633 | - // Upgrade transparently to more stringent check for versions of php which do not support otherwise. |
|
1634 | - // Doing it in constructor would be cleaner; doing it here saves us a couple of function calls |
|
1635 | - if($this->verifyhost == 1 && $info = curl_version() && version_compare($info['version'], '7.28.1') >= 0) |
|
1636 | - { |
|
1637 | - $this->verifyhost = 2; |
|
1638 | - } |
|
1639 | - // whether to verify cert's common name (CN); 0 for no, 1 to verify that it exists, and 2 to verify that it matches the hostname used |
|
1640 | - curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, $this->verifyhost); |
|
1641 | - } |
|
1642 | - |
|
1643 | - // proxy info |
|
1644 | - if($proxyhost) |
|
1645 | - { |
|
1646 | - if($proxyport == 0) |
|
1647 | - { |
|
1648 | - $proxyport = 8080; // NB: even for HTTPS, local connection is on port 8080 |
|
1649 | - } |
|
1650 | - curl_setopt($curl, CURLOPT_PROXY, $proxyhost.':'.$proxyport); |
|
1651 | - //curl_setopt($curl, CURLOPT_PROXYPORT,$proxyport); |
|
1652 | - if($proxyusername) |
|
1653 | - { |
|
1654 | - curl_setopt($curl, CURLOPT_PROXYUSERPWD, $proxyusername.':'.$proxypassword); |
|
1655 | - if (defined('CURLOPT_PROXYAUTH')) |
|
1656 | - { |
|
1657 | - curl_setopt($curl, CURLOPT_PROXYAUTH, $proxyauthtype); |
|
1658 | - } |
|
1659 | - else if ($proxyauthtype != 1) |
|
1660 | - { |
|
1661 | - error_log('XML-RPC: '.__METHOD__.': warning. Only Basic auth to proxy is supported by the current PHP/curl install'); |
|
1662 | - } |
|
1663 | - } |
|
1664 | - } |
|
1665 | - |
|
1666 | - // NB: should we build cookie http headers by hand rather than let CURL do it? |
|
1667 | - // the following code does not honour 'expires', 'path' and 'domain' cookie attributes |
|
1668 | - // set to client obj the the user... |
|
1669 | - if (count($this->cookies)) |
|
1670 | - { |
|
1671 | - $cookieheader = ''; |
|
1672 | - foreach ($this->cookies as $name => $cookie) |
|
1673 | - { |
|
1674 | - $cookieheader .= $name . '=' . $cookie['value'] . '; '; |
|
1675 | - } |
|
1676 | - curl_setopt($curl, CURLOPT_COOKIE, substr($cookieheader, 0, -2)); |
|
1677 | - } |
|
1678 | - |
|
1679 | - foreach ($this->extracurlopts as $opt => $val) |
|
1680 | - { |
|
1681 | - curl_setopt($curl, $opt, $val); |
|
1682 | - } |
|
1683 | - |
|
1684 | - $result = curl_exec($curl); |
|
1685 | - |
|
1686 | - if ($this->debug > 1) |
|
1687 | - { |
|
1688 | - print "<PRE>\n---CURL INFO---\n"; |
|
1689 | - foreach(curl_getinfo($curl) as $name => $val) |
|
1690 | - { |
|
1691 | - if (is_array($val)) |
|
1692 | - { |
|
1693 | - $val = implode("\n", $val); |
|
1694 | - } |
|
1695 | - print $name . ': ' . htmlentities($val) . "\n"; |
|
1696 | - } |
|
1697 | - |
|
1698 | - print "---END---\n</PRE>"; |
|
1699 | - } |
|
1700 | - |
|
1701 | - if(!$result) /// @todo we should use a better check here - what if we get back '' or '0'? |
|
1702 | - { |
|
1703 | - $this->errstr='no response'; |
|
1704 | - $resp=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['curl_fail'], $GLOBALS['xmlrpcstr']['curl_fail']. ': '. curl_error($curl)); |
|
1705 | - curl_close($curl); |
|
1706 | - if($keepalive) |
|
1707 | - { |
|
1708 | - $this->xmlrpc_curl_handle = null; |
|
1709 | - } |
|
1710 | - } |
|
1711 | - else |
|
1712 | - { |
|
1713 | - if(!$keepalive) |
|
1714 | - { |
|
1715 | - curl_close($curl); |
|
1716 | - } |
|
1717 | - $resp =& $msg->parseResponse($result, true, $this->return_type); |
|
1718 | - // if we got back a 302, we can not reuse the curl handle for later calls |
|
1719 | - if($resp->faultCode() == $GLOBALS['xmlrpcerr']['http_error'] && $keepalive) |
|
1720 | - { |
|
1721 | - curl_close($curl); |
|
1722 | - $this->xmlrpc_curl_handle = null; |
|
1723 | - } |
|
1724 | - } |
|
1725 | - return $resp; |
|
1726 | - } |
|
1727 | - |
|
1728 | - /** |
|
1729 | - * Send an array of request messages and return an array of responses. |
|
1730 | - * Unless $this->no_multicall has been set to true, it will try first |
|
1731 | - * to use one single xmlrpc call to server method system.multicall, and |
|
1732 | - * revert to sending many successive calls in case of failure. |
|
1733 | - * This failure is also stored in $this->no_multicall for subsequent calls. |
|
1734 | - * Unfortunately, there is no server error code universally used to denote |
|
1735 | - * the fact that multicall is unsupported, so there is no way to reliably |
|
1736 | - * distinguish between that and a temporary failure. |
|
1737 | - * If you are sure that server supports multicall and do not want to |
|
1738 | - * fallback to using many single calls, set the fourth parameter to FALSE. |
|
1739 | - * |
|
1740 | - * NB: trying to shoehorn extra functionality into existing syntax has resulted |
|
1741 | - * in pretty much convoluted code... |
|
1742 | - * |
|
1743 | - * @param array $msgs an array of xmlrpcmsg objects |
|
1744 | - * @param integer $timeout connection timeout (in seconds) |
|
1745 | - * @param string $method the http protocol variant to be used |
|
1746 | - * @param boolean fallback When true, upon receiving an error during multicall, multiple single calls will be attempted |
|
1747 | - * @return array |
|
1748 | - * @access public |
|
1749 | - */ |
|
1750 | - function multicall($msgs, $timeout=0, $method='', $fallback=true) |
|
1751 | - { |
|
1752 | - if ($method == '') |
|
1753 | - { |
|
1754 | - $method = $this->method; |
|
1755 | - } |
|
1756 | - if(!$this->no_multicall) |
|
1757 | - { |
|
1758 | - $results = $this->_try_multicall($msgs, $timeout, $method); |
|
1759 | - if(is_array($results)) |
|
1760 | - { |
|
1761 | - // System.multicall succeeded |
|
1762 | - return $results; |
|
1763 | - } |
|
1764 | - else |
|
1765 | - { |
|
1766 | - // either system.multicall is unsupported by server, |
|
1767 | - // or call failed for some other reason. |
|
1768 | - if ($fallback) |
|
1769 | - { |
|
1770 | - // Don't try it next time... |
|
1771 | - $this->no_multicall = true; |
|
1772 | - } |
|
1773 | - else |
|
1774 | - { |
|
1775 | - if (is_a($results, 'xmlrpcresp')) |
|
1776 | - { |
|
1777 | - $result = $results; |
|
1778 | - } |
|
1779 | - else |
|
1780 | - { |
|
1781 | - $result = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['multicall_error'], $GLOBALS['xmlrpcstr']['multicall_error']); |
|
1782 | - } |
|
1783 | - } |
|
1784 | - } |
|
1785 | - } |
|
1786 | - else |
|
1787 | - { |
|
1788 | - // override fallback, in case careless user tries to do two |
|
1789 | - // opposite things at the same time |
|
1790 | - $fallback = true; |
|
1791 | - } |
|
1792 | - |
|
1793 | - $results = array(); |
|
1794 | - if ($fallback) |
|
1795 | - { |
|
1796 | - // system.multicall is (probably) unsupported by server: |
|
1797 | - // emulate multicall via multiple requests |
|
1798 | - foreach($msgs as $msg) |
|
1799 | - { |
|
1800 | - $results[] =& $this->send($msg, $timeout, $method); |
|
1801 | - } |
|
1802 | - } |
|
1803 | - else |
|
1804 | - { |
|
1805 | - // user does NOT want to fallback on many single calls: |
|
1806 | - // since we should always return an array of responses, |
|
1807 | - // return an array with the same error repeated n times |
|
1808 | - foreach($msgs as $msg) |
|
1809 | - { |
|
1810 | - $results[] = $result; |
|
1811 | - } |
|
1812 | - } |
|
1813 | - return $results; |
|
1814 | - } |
|
1815 | - |
|
1816 | - /** |
|
1817 | - * Attempt to boxcar $msgs via system.multicall. |
|
1818 | - * Returns either an array of xmlrpcreponses, an xmlrpc error response |
|
1819 | - * or false (when received response does not respect valid multicall syntax) |
|
1820 | - * @access private |
|
1821 | - */ |
|
1822 | - function _try_multicall($msgs, $timeout, $method) |
|
1823 | - { |
|
1824 | - // Construct multicall message |
|
1825 | - $calls = array(); |
|
1826 | - foreach($msgs as $msg) |
|
1827 | - { |
|
1828 | - $call['methodName'] = new xmlrpcval($msg->method(),'string'); |
|
1829 | - $numParams = $msg->getNumParams(); |
|
1830 | - $params = array(); |
|
1831 | - for($i = 0; $i < $numParams; $i++) |
|
1832 | - { |
|
1833 | - $params[$i] = $msg->getParam($i); |
|
1834 | - } |
|
1835 | - $call['params'] = new xmlrpcval($params, 'array'); |
|
1836 | - $calls[] = new xmlrpcval($call, 'struct'); |
|
1837 | - } |
|
1838 | - $multicall = new xmlrpcmsg('system.multicall'); |
|
1839 | - $multicall->addParam(new xmlrpcval($calls, 'array')); |
|
1840 | - |
|
1841 | - // Attempt RPC call |
|
1842 | - $result =& $this->send($multicall, $timeout, $method); |
|
1843 | - |
|
1844 | - if($result->faultCode() != 0) |
|
1845 | - { |
|
1846 | - // call to system.multicall failed |
|
1847 | - return $result; |
|
1848 | - } |
|
1849 | - |
|
1850 | - // Unpack responses. |
|
1851 | - $rets = $result->value(); |
|
1852 | - |
|
1853 | - if ($this->return_type == 'xml') |
|
1854 | - { |
|
1855 | - return $rets; |
|
1856 | - } |
|
1857 | - else if ($this->return_type == 'phpvals') |
|
1858 | - { |
|
1859 | - ///@todo test this code branch... |
|
1860 | - $rets = $result->value(); |
|
1861 | - if(!is_array($rets)) |
|
1862 | - { |
|
1863 | - return false; // bad return type from system.multicall |
|
1864 | - } |
|
1865 | - $numRets = count($rets); |
|
1866 | - if($numRets != count($msgs)) |
|
1867 | - { |
|
1868 | - return false; // wrong number of return values. |
|
1869 | - } |
|
1870 | - |
|
1871 | - $response = array(); |
|
1872 | - for($i = 0; $i < $numRets; $i++) |
|
1873 | - { |
|
1874 | - $val = $rets[$i]; |
|
1875 | - if (!is_array($val)) { |
|
1876 | - return false; |
|
1877 | - } |
|
1878 | - switch(count($val)) |
|
1879 | - { |
|
1880 | - case 1: |
|
1881 | - if(!isset($val[0])) |
|
1882 | - { |
|
1883 | - return false; // Bad value |
|
1884 | - } |
|
1885 | - // Normal return value |
|
1886 | - $response[$i] = new xmlrpcresp($val[0], 0, '', 'phpvals'); |
|
1887 | - break; |
|
1888 | - case 2: |
|
1889 | - /// @todo remove usage of @: it is apparently quite slow |
|
1890 | - $code = @$val['faultCode']; |
|
1891 | - if(!is_int($code)) |
|
1892 | - { |
|
1893 | - return false; |
|
1894 | - } |
|
1895 | - $str = @$val['faultString']; |
|
1896 | - if(!is_string($str)) |
|
1897 | - { |
|
1898 | - return false; |
|
1899 | - } |
|
1900 | - $response[$i] = new xmlrpcresp(0, $code, $str); |
|
1901 | - break; |
|
1902 | - default: |
|
1903 | - return false; |
|
1904 | - } |
|
1905 | - } |
|
1906 | - return $response; |
|
1907 | - } |
|
1908 | - else // return type == 'xmlrpcvals' |
|
1909 | - { |
|
1910 | - $rets = $result->value(); |
|
1911 | - if($rets->kindOf() != 'array') |
|
1912 | - { |
|
1913 | - return false; // bad return type from system.multicall |
|
1914 | - } |
|
1915 | - $numRets = $rets->arraysize(); |
|
1916 | - if($numRets != count($msgs)) |
|
1917 | - { |
|
1918 | - return false; // wrong number of return values. |
|
1919 | - } |
|
1920 | - |
|
1921 | - $response = array(); |
|
1922 | - for($i = 0; $i < $numRets; $i++) |
|
1923 | - { |
|
1924 | - $val = $rets->arraymem($i); |
|
1925 | - switch($val->kindOf()) |
|
1926 | - { |
|
1927 | - case 'array': |
|
1928 | - if($val->arraysize() != 1) |
|
1929 | - { |
|
1930 | - return false; // Bad value |
|
1931 | - } |
|
1932 | - // Normal return value |
|
1933 | - $response[$i] = new xmlrpcresp($val->arraymem(0)); |
|
1934 | - break; |
|
1935 | - case 'struct': |
|
1936 | - $code = $val->structmem('faultCode'); |
|
1937 | - if($code->kindOf() != 'scalar' || $code->scalartyp() != 'int') |
|
1938 | - { |
|
1939 | - return false; |
|
1940 | - } |
|
1941 | - $str = $val->structmem('faultString'); |
|
1942 | - if($str->kindOf() != 'scalar' || $str->scalartyp() != 'string') |
|
1943 | - { |
|
1944 | - return false; |
|
1945 | - } |
|
1946 | - $response[$i] = new xmlrpcresp(0, $code->scalarval(), $str->scalarval()); |
|
1947 | - break; |
|
1948 | - default: |
|
1949 | - return false; |
|
1950 | - } |
|
1951 | - } |
|
1952 | - return $response; |
|
1953 | - } |
|
1954 | - } |
|
1955 | - } // end class xmlrpc_client |
|
1956 | - |
|
1957 | - class xmlrpcresp |
|
1958 | - { |
|
1959 | - var $val = 0; |
|
1960 | - var $valtyp; |
|
1961 | - var $errno = 0; |
|
1962 | - var $errstr = ''; |
|
1963 | - var $payload; |
|
1964 | - var $hdrs = array(); |
|
1965 | - var $_cookies = array(); |
|
1966 | - var $content_type = 'text/xml'; |
|
1967 | - var $raw_data = ''; |
|
1968 | - |
|
1969 | - /** |
|
1970 | - * @param mixed $val either an xmlrpcval obj, a php value or the xml serialization of an xmlrpcval (a string) |
|
1971 | - * @param integer $fcode set it to anything but 0 to create an error response |
|
1972 | - * @param string $fstr the error string, in case of an error response |
|
1973 | - * @param string $valtyp either 'xmlrpcvals', 'phpvals' or 'xml' |
|
1974 | - * |
|
1975 | - * @todo add check that $val / $fcode / $fstr is of correct type??? |
|
1976 | - * NB: as of now we do not do it, since it might be either an xmlrpcval or a plain |
|
1977 | - * php val, or a complete xml chunk, depending on usage of xmlrpc_client::send() inside which creator is called... |
|
1978 | - */ |
|
1979 | - function __construct($val, $fcode = 0, $fstr = '', $valtyp='') |
|
1980 | - { |
|
1981 | - if($fcode != 0) |
|
1982 | - { |
|
1983 | - // error response |
|
1984 | - $this->errno = $fcode; |
|
1985 | - $this->errstr = $fstr; |
|
1986 | - //$this->errstr = htmlspecialchars($fstr); // XXX: encoding probably shouldn't be done here; fix later. |
|
1987 | - } |
|
1988 | - else |
|
1989 | - { |
|
1990 | - // successful response |
|
1991 | - $this->val = $val; |
|
1992 | - if ($valtyp == '') |
|
1993 | - { |
|
1994 | - // user did not declare type of response value: try to guess it |
|
1995 | - if (is_object($this->val) && is_a($this->val, 'xmlrpcval')) |
|
1996 | - { |
|
1997 | - $this->valtyp = 'xmlrpcvals'; |
|
1998 | - } |
|
1999 | - else if (is_string($this->val)) |
|
2000 | - { |
|
2001 | - $this->valtyp = 'xml'; |
|
2002 | - |
|
2003 | - } |
|
2004 | - else |
|
2005 | - { |
|
2006 | - $this->valtyp = 'phpvals'; |
|
2007 | - } |
|
2008 | - } |
|
2009 | - else |
|
2010 | - { |
|
2011 | - // user declares type of resp value: believe him |
|
2012 | - $this->valtyp = $valtyp; |
|
2013 | - } |
|
2014 | - } |
|
2015 | - } |
|
2016 | - |
|
2017 | - /** |
|
2018 | - * @deprecated |
|
2019 | - */ |
|
2020 | - function xmlrpcresp($val, $fcode = 0, $fstr = '', $valtyp='') |
|
2021 | - { |
|
2022 | - self::__construct($val, $fcode, $fstr, $valtyp); |
|
2023 | - } |
|
2024 | - |
|
2025 | - /** |
|
2026 | - * Returns the error code of the response. |
|
2027 | - * @return integer the error code of this response (0 for not-error responses) |
|
2028 | - * @access public |
|
2029 | - */ |
|
2030 | - function faultCode() |
|
2031 | - { |
|
2032 | - return $this->errno; |
|
2033 | - } |
|
2034 | - |
|
2035 | - /** |
|
2036 | - * Returns the error code of the response. |
|
2037 | - * @return string the error string of this response ('' for not-error responses) |
|
2038 | - * @access public |
|
2039 | - */ |
|
2040 | - function faultString() |
|
2041 | - { |
|
2042 | - return $this->errstr; |
|
2043 | - } |
|
2044 | - |
|
2045 | - /** |
|
2046 | - * Returns the value received by the server. |
|
2047 | - * @return mixed the xmlrpcval object returned by the server. Might be an xml string or php value if the response has been created by specially configured xmlrpc_client objects |
|
2048 | - * @access public |
|
2049 | - */ |
|
2050 | - function value() |
|
2051 | - { |
|
2052 | - return $this->val; |
|
2053 | - } |
|
2054 | - |
|
2055 | - /** |
|
2056 | - * Returns an array with the cookies received from the server. |
|
2057 | - * Array has the form: $cookiename => array ('value' => $val, $attr1 => $val1, $attr2 = $val2, ...) |
|
2058 | - * with attributes being e.g. 'expires', 'path', domain'. |
|
2059 | - * NB: cookies sent as 'expired' by the server (i.e. with an expiry date in the past) |
|
2060 | - * are still present in the array. It is up to the user-defined code to decide |
|
2061 | - * how to use the received cookies, and whether they have to be sent back with the next |
|
2062 | - * request to the server (using xmlrpc_client::setCookie) or not |
|
2063 | - * @return array array of cookies received from the server |
|
2064 | - * @access public |
|
2065 | - */ |
|
2066 | - function cookies() |
|
2067 | - { |
|
2068 | - return $this->_cookies; |
|
2069 | - } |
|
2070 | - |
|
2071 | - /** |
|
2072 | - * Returns xml representation of the response. XML prologue not included |
|
2073 | - * @param string $charset_encoding the charset to be used for serialization. if null, US-ASCII is assumed |
|
2074 | - * @return string the xml representation of the response |
|
2075 | - * @access public |
|
2076 | - */ |
|
2077 | - function serialize($charset_encoding='') |
|
2078 | - { |
|
2079 | - if ($charset_encoding != '') |
|
2080 | - $this->content_type = 'text/xml; charset=' . $charset_encoding; |
|
2081 | - else |
|
2082 | - $this->content_type = 'text/xml'; |
|
2083 | - if ($GLOBALS['xmlrpc_null_apache_encoding']) |
|
2084 | - { |
|
2085 | - $result = "<methodResponse xmlns:ex=\"".$GLOBALS['xmlrpc_null_apache_encoding_ns']."\">\n"; |
|
2086 | - } |
|
2087 | - else |
|
2088 | - { |
|
2089 | - $result = "<methodResponse>\n"; |
|
2090 | - } |
|
2091 | - if($this->errno) |
|
2092 | - { |
|
2093 | - // G. Giunta 2005/2/13: let non-ASCII response messages be tolerated by clients |
|
2094 | - // by xml-encoding non ascii chars |
|
2095 | - $result .= "<fault>\n" . |
|
381 | + default: |
|
382 | + $escaped_data = ''; |
|
383 | + error_log("Converting from $src_encoding to $dest_encoding: not supported..."); |
|
384 | + } |
|
385 | + return $escaped_data; |
|
386 | + } |
|
387 | + |
|
388 | + /// xml parser handler function for opening element tags |
|
389 | + function xmlrpc_se($parser, $name, $attrs, $accept_single_vals=false) |
|
390 | + { |
|
391 | + // if invalid xmlrpc already detected, skip all processing |
|
392 | + if ($GLOBALS['_xh']['isf'] < 2) |
|
393 | + { |
|
394 | + // check for correct element nesting |
|
395 | + // top level element can only be of 2 types |
|
396 | + /// @todo optimization creep: save this check into a bool variable, instead of using count() every time: |
|
397 | + /// there is only a single top level element in xml anyway |
|
398 | + if (count($GLOBALS['_xh']['stack']) == 0) |
|
399 | + { |
|
400 | + if ($name != 'METHODRESPONSE' && $name != 'METHODCALL' && ( |
|
401 | + $name != 'VALUE' && !$accept_single_vals)) |
|
402 | + { |
|
403 | + $GLOBALS['_xh']['isf'] = 2; |
|
404 | + $GLOBALS['_xh']['isf_reason'] = 'missing top level xmlrpc element'; |
|
405 | + return; |
|
406 | + } |
|
407 | + else |
|
408 | + { |
|
409 | + $GLOBALS['_xh']['rt'] = strtolower($name); |
|
410 | + $GLOBALS['_xh']['rt'] = strtolower($name); |
|
411 | + } |
|
412 | + } |
|
413 | + else |
|
414 | + { |
|
415 | + // not top level element: see if parent is OK |
|
416 | + $parent = end($GLOBALS['_xh']['stack']); |
|
417 | + if (!array_key_exists($name, $GLOBALS['xmlrpc_valid_parents']) || !in_array($parent, $GLOBALS['xmlrpc_valid_parents'][$name])) |
|
418 | + { |
|
419 | + $GLOBALS['_xh']['isf'] = 2; |
|
420 | + $GLOBALS['_xh']['isf_reason'] = "xmlrpc element $name cannot be child of $parent"; |
|
421 | + return; |
|
422 | + } |
|
423 | + } |
|
424 | + |
|
425 | + switch($name) |
|
426 | + { |
|
427 | + // optimize for speed switch cases: most common cases first |
|
428 | + case 'VALUE': |
|
429 | + /// @todo we could check for 2 VALUE elements inside a MEMBER or PARAM element |
|
430 | + $GLOBALS['_xh']['vt']='value'; // indicator: no value found yet |
|
431 | + $GLOBALS['_xh']['ac']=''; |
|
432 | + $GLOBALS['_xh']['lv']=1; |
|
433 | + $GLOBALS['_xh']['php_class']=null; |
|
434 | + break; |
|
435 | + case 'I4': |
|
436 | + case 'INT': |
|
437 | + case 'STRING': |
|
438 | + case 'BOOLEAN': |
|
439 | + case 'DOUBLE': |
|
440 | + case 'DATETIME.ISO8601': |
|
441 | + case 'BASE64': |
|
442 | + if ($GLOBALS['_xh']['vt']!='value') |
|
443 | + { |
|
444 | + //two data elements inside a value: an error occurred! |
|
445 | + $GLOBALS['_xh']['isf'] = 2; |
|
446 | + $GLOBALS['_xh']['isf_reason'] = "$name element following a {$GLOBALS['_xh']['vt']} element inside a single value"; |
|
447 | + return; |
|
448 | + } |
|
449 | + $GLOBALS['_xh']['ac']=''; // reset the accumulator |
|
450 | + break; |
|
451 | + case 'STRUCT': |
|
452 | + case 'ARRAY': |
|
453 | + if ($GLOBALS['_xh']['vt']!='value') |
|
454 | + { |
|
455 | + //two data elements inside a value: an error occurred! |
|
456 | + $GLOBALS['_xh']['isf'] = 2; |
|
457 | + $GLOBALS['_xh']['isf_reason'] = "$name element following a {$GLOBALS['_xh']['vt']} element inside a single value"; |
|
458 | + return; |
|
459 | + } |
|
460 | + // create an empty array to hold child values, and push it onto appropriate stack |
|
461 | + $cur_val = array(); |
|
462 | + $cur_val['values'] = array(); |
|
463 | + $cur_val['type'] = $name; |
|
464 | + // check for out-of-band information to rebuild php objs |
|
465 | + // and in case it is found, save it |
|
466 | + if (@isset($attrs['PHP_CLASS'])) |
|
467 | + { |
|
468 | + $cur_val['php_class'] = $attrs['PHP_CLASS']; |
|
469 | + } |
|
470 | + $GLOBALS['_xh']['valuestack'][] = $cur_val; |
|
471 | + $GLOBALS['_xh']['vt']='data'; // be prepared for a data element next |
|
472 | + break; |
|
473 | + case 'DATA': |
|
474 | + if ($GLOBALS['_xh']['vt']!='data') |
|
475 | + { |
|
476 | + //two data elements inside a value: an error occurred! |
|
477 | + $GLOBALS['_xh']['isf'] = 2; |
|
478 | + $GLOBALS['_xh']['isf_reason'] = "found two data elements inside an array element"; |
|
479 | + return; |
|
480 | + } |
|
481 | + case 'METHODCALL': |
|
482 | + case 'METHODRESPONSE': |
|
483 | + case 'PARAMS': |
|
484 | + // valid elements that add little to processing |
|
485 | + break; |
|
486 | + case 'METHODNAME': |
|
487 | + case 'NAME': |
|
488 | + /// @todo we could check for 2 NAME elements inside a MEMBER element |
|
489 | + $GLOBALS['_xh']['ac']=''; |
|
490 | + break; |
|
491 | + case 'FAULT': |
|
492 | + $GLOBALS['_xh']['isf']=1; |
|
493 | + break; |
|
494 | + case 'MEMBER': |
|
495 | + $GLOBALS['_xh']['valuestack'][count($GLOBALS['_xh']['valuestack'])-1]['name']=''; // set member name to null, in case we do not find in the xml later on |
|
496 | + //$GLOBALS['_xh']['ac']=''; |
|
497 | + // Drop trough intentionally |
|
498 | + case 'PARAM': |
|
499 | + // clear value type, so we can check later if no value has been passed for this param/member |
|
500 | + $GLOBALS['_xh']['vt']=null; |
|
501 | + break; |
|
502 | + case 'NIL': |
|
503 | + case 'EX:NIL': |
|
504 | + if ($GLOBALS['xmlrpc_null_extension']) |
|
505 | + { |
|
506 | + if ($GLOBALS['_xh']['vt']!='value') |
|
507 | + { |
|
508 | + //two data elements inside a value: an error occurred! |
|
509 | + $GLOBALS['_xh']['isf'] = 2; |
|
510 | + $GLOBALS['_xh']['isf_reason'] = "$name element following a {$GLOBALS['_xh']['vt']} element inside a single value"; |
|
511 | + return; |
|
512 | + } |
|
513 | + $GLOBALS['_xh']['ac']=''; // reset the accumulator |
|
514 | + break; |
|
515 | + } |
|
516 | + // we do not support the <NIL/> extension, so |
|
517 | + // drop through intentionally |
|
518 | + default: |
|
519 | + /// INVALID ELEMENT: RAISE ISF so that it is later recognized!!! |
|
520 | + $GLOBALS['_xh']['isf'] = 2; |
|
521 | + $GLOBALS['_xh']['isf_reason'] = "found not-xmlrpc xml element $name"; |
|
522 | + break; |
|
523 | + } |
|
524 | + |
|
525 | + // Save current element name to stack, to validate nesting |
|
526 | + $GLOBALS['_xh']['stack'][] = $name; |
|
527 | + |
|
528 | + /// @todo optimization creep: move this inside the big switch() above |
|
529 | + if($name!='VALUE') |
|
530 | + { |
|
531 | + $GLOBALS['_xh']['lv']=0; |
|
532 | + } |
|
533 | + } |
|
534 | + } |
|
535 | + |
|
536 | + /// Used in decoding xml chunks that might represent single xmlrpc values |
|
537 | + function xmlrpc_se_any($parser, $name, $attrs) |
|
538 | + { |
|
539 | + xmlrpc_se($parser, $name, $attrs, true); |
|
540 | + } |
|
541 | + |
|
542 | + /// xml parser handler function for close element tags |
|
543 | + function xmlrpc_ee($parser, $name, $rebuild_xmlrpcvals = true) |
|
544 | + { |
|
545 | + if ($GLOBALS['_xh']['isf'] < 2) |
|
546 | + { |
|
547 | + // push this element name from stack |
|
548 | + // NB: if XML validates, correct opening/closing is guaranteed and |
|
549 | + // we do not have to check for $name == $curr_elem. |
|
550 | + // we also checked for proper nesting at start of elements... |
|
551 | + $curr_elem = array_pop($GLOBALS['_xh']['stack']); |
|
552 | + |
|
553 | + switch($name) |
|
554 | + { |
|
555 | + case 'VALUE': |
|
556 | + // This if() detects if no scalar was inside <VALUE></VALUE> |
|
557 | + if ($GLOBALS['_xh']['vt']=='value') |
|
558 | + { |
|
559 | + $GLOBALS['_xh']['value']=$GLOBALS['_xh']['ac']; |
|
560 | + $GLOBALS['_xh']['vt']=$GLOBALS['xmlrpcString']; |
|
561 | + } |
|
562 | + |
|
563 | + if ($rebuild_xmlrpcvals) |
|
564 | + { |
|
565 | + // build the xmlrpc val out of the data received, and substitute it |
|
566 | + $temp = new xmlrpcval($GLOBALS['_xh']['value'], $GLOBALS['_xh']['vt']); |
|
567 | + // in case we got info about underlying php class, save it |
|
568 | + // in the object we're rebuilding |
|
569 | + if (isset($GLOBALS['_xh']['php_class'])) |
|
570 | + $temp->_php_class = $GLOBALS['_xh']['php_class']; |
|
571 | + // check if we are inside an array or struct: |
|
572 | + // if value just built is inside an array, let's move it into array on the stack |
|
573 | + $vscount = count($GLOBALS['_xh']['valuestack']); |
|
574 | + if ($vscount && $GLOBALS['_xh']['valuestack'][$vscount-1]['type']=='ARRAY') |
|
575 | + { |
|
576 | + $GLOBALS['_xh']['valuestack'][$vscount-1]['values'][] = $temp; |
|
577 | + } |
|
578 | + else |
|
579 | + { |
|
580 | + $GLOBALS['_xh']['value'] = $temp; |
|
581 | + } |
|
582 | + } |
|
583 | + else |
|
584 | + { |
|
585 | + /// @todo this needs to treat correctly php-serialized objects, |
|
586 | + /// since std deserializing is done by php_xmlrpc_decode, |
|
587 | + /// which we will not be calling... |
|
588 | + if (isset($GLOBALS['_xh']['php_class'])) |
|
589 | + { |
|
590 | + } |
|
591 | + |
|
592 | + // check if we are inside an array or struct: |
|
593 | + // if value just built is inside an array, let's move it into array on the stack |
|
594 | + $vscount = count($GLOBALS['_xh']['valuestack']); |
|
595 | + if ($vscount && $GLOBALS['_xh']['valuestack'][$vscount-1]['type']=='ARRAY') |
|
596 | + { |
|
597 | + $GLOBALS['_xh']['valuestack'][$vscount-1]['values'][] = $GLOBALS['_xh']['value']; |
|
598 | + } |
|
599 | + } |
|
600 | + break; |
|
601 | + case 'BOOLEAN': |
|
602 | + case 'I4': |
|
603 | + case 'INT': |
|
604 | + case 'STRING': |
|
605 | + case 'DOUBLE': |
|
606 | + case 'DATETIME.ISO8601': |
|
607 | + case 'BASE64': |
|
608 | + $GLOBALS['_xh']['vt']=strtolower($name); |
|
609 | + /// @todo: optimization creep - remove the if/elseif cycle below |
|
610 | + /// since the case() in which we are already did that |
|
611 | + if ($name=='STRING') |
|
612 | + { |
|
613 | + $GLOBALS['_xh']['value']=$GLOBALS['_xh']['ac']; |
|
614 | + } |
|
615 | + elseif ($name=='DATETIME.ISO8601') |
|
616 | + { |
|
617 | + if (!preg_match('/^[0-9]{8}T[0-9]{2}:[0-9]{2}:[0-9]{2}$/', $GLOBALS['_xh']['ac'])) |
|
618 | + { |
|
619 | + error_log('XML-RPC: invalid value received in DATETIME: '.$GLOBALS['_xh']['ac']); |
|
620 | + } |
|
621 | + $GLOBALS['_xh']['vt']=$GLOBALS['xmlrpcDateTime']; |
|
622 | + $GLOBALS['_xh']['value']=$GLOBALS['_xh']['ac']; |
|
623 | + } |
|
624 | + elseif ($name=='BASE64') |
|
625 | + { |
|
626 | + /// @todo check for failure of base64 decoding / catch warnings |
|
627 | + $GLOBALS['_xh']['value']=base64_decode($GLOBALS['_xh']['ac']); |
|
628 | + } |
|
629 | + elseif ($name=='BOOLEAN') |
|
630 | + { |
|
631 | + // special case here: we translate boolean 1 or 0 into PHP |
|
632 | + // constants true or false. |
|
633 | + // Strings 'true' and 'false' are accepted, even though the |
|
634 | + // spec never mentions them (see eg. Blogger api docs) |
|
635 | + // NB: this simple checks helps a lot sanitizing input, ie no |
|
636 | + // security problems around here |
|
637 | + if ($GLOBALS['_xh']['ac']=='1' || strcasecmp($GLOBALS['_xh']['ac'], 'true') == 0) |
|
638 | + { |
|
639 | + $GLOBALS['_xh']['value']=true; |
|
640 | + } |
|
641 | + else |
|
642 | + { |
|
643 | + // log if receiveing something strange, even though we set the value to false anyway |
|
644 | + if ($GLOBALS['_xh']['ac']!='0' && strcasecmp($GLOBALS['_xh']['ac'], 'false') != 0) |
|
645 | + error_log('XML-RPC: invalid value received in BOOLEAN: '.$GLOBALS['_xh']['ac']); |
|
646 | + $GLOBALS['_xh']['value']=false; |
|
647 | + } |
|
648 | + } |
|
649 | + elseif ($name=='DOUBLE') |
|
650 | + { |
|
651 | + // we have a DOUBLE |
|
652 | + // we must check that only 0123456789-.<space> are characters here |
|
653 | + // NOTE: regexp could be much stricter than this... |
|
654 | + if (!preg_match('/^[+-eE0123456789 \t.]+$/', $GLOBALS['_xh']['ac'])) |
|
655 | + { |
|
656 | + /// @todo: find a better way of throwing an error than this! |
|
657 | + error_log('XML-RPC: non numeric value received in DOUBLE: '.$GLOBALS['_xh']['ac']); |
|
658 | + $GLOBALS['_xh']['value']='ERROR_NON_NUMERIC_FOUND'; |
|
659 | + } |
|
660 | + else |
|
661 | + { |
|
662 | + // it's ok, add it on |
|
663 | + $GLOBALS['_xh']['value']=(double)$GLOBALS['_xh']['ac']; |
|
664 | + } |
|
665 | + } |
|
666 | + else |
|
667 | + { |
|
668 | + // we have an I4/INT |
|
669 | + // we must check that only 0123456789-<space> are characters here |
|
670 | + if (!preg_match('/^[+-]?[0123456789 \t]+$/', $GLOBALS['_xh']['ac'])) |
|
671 | + { |
|
672 | + /// @todo find a better way of throwing an error than this! |
|
673 | + error_log('XML-RPC: non numeric value received in INT: '.$GLOBALS['_xh']['ac']); |
|
674 | + $GLOBALS['_xh']['value']='ERROR_NON_NUMERIC_FOUND'; |
|
675 | + } |
|
676 | + else |
|
677 | + { |
|
678 | + // it's ok, add it on |
|
679 | + $GLOBALS['_xh']['value']=(int)$GLOBALS['_xh']['ac']; |
|
680 | + } |
|
681 | + } |
|
682 | + //$GLOBALS['_xh']['ac']=''; // is this necessary? |
|
683 | + $GLOBALS['_xh']['lv']=3; // indicate we've found a value |
|
684 | + break; |
|
685 | + case 'NAME': |
|
686 | + $GLOBALS['_xh']['valuestack'][count($GLOBALS['_xh']['valuestack'])-1]['name'] = $GLOBALS['_xh']['ac']; |
|
687 | + break; |
|
688 | + case 'MEMBER': |
|
689 | + //$GLOBALS['_xh']['ac']=''; // is this necessary? |
|
690 | + // add to array in the stack the last element built, |
|
691 | + // unless no VALUE was found |
|
692 | + if ($GLOBALS['_xh']['vt']) |
|
693 | + { |
|
694 | + $vscount = count($GLOBALS['_xh']['valuestack']); |
|
695 | + $GLOBALS['_xh']['valuestack'][$vscount-1]['values'][$GLOBALS['_xh']['valuestack'][$vscount-1]['name']] = $GLOBALS['_xh']['value']; |
|
696 | + } else |
|
697 | + error_log('XML-RPC: missing VALUE inside STRUCT in received xml'); |
|
698 | + break; |
|
699 | + case 'DATA': |
|
700 | + //$GLOBALS['_xh']['ac']=''; // is this necessary? |
|
701 | + $GLOBALS['_xh']['vt']=null; // reset this to check for 2 data elements in a row - even if they're empty |
|
702 | + break; |
|
703 | + case 'STRUCT': |
|
704 | + case 'ARRAY': |
|
705 | + // fetch out of stack array of values, and promote it to current value |
|
706 | + $curr_val = array_pop($GLOBALS['_xh']['valuestack']); |
|
707 | + $GLOBALS['_xh']['value'] = $curr_val['values']; |
|
708 | + $GLOBALS['_xh']['vt']=strtolower($name); |
|
709 | + if (isset($curr_val['php_class'])) |
|
710 | + { |
|
711 | + $GLOBALS['_xh']['php_class'] = $curr_val['php_class']; |
|
712 | + } |
|
713 | + break; |
|
714 | + case 'PARAM': |
|
715 | + // add to array of params the current value, |
|
716 | + // unless no VALUE was found |
|
717 | + if ($GLOBALS['_xh']['vt']) |
|
718 | + { |
|
719 | + $GLOBALS['_xh']['params'][]=$GLOBALS['_xh']['value']; |
|
720 | + $GLOBALS['_xh']['pt'][]=$GLOBALS['_xh']['vt']; |
|
721 | + } |
|
722 | + else |
|
723 | + error_log('XML-RPC: missing VALUE inside PARAM in received xml'); |
|
724 | + break; |
|
725 | + case 'METHODNAME': |
|
726 | + $GLOBALS['_xh']['method']=preg_replace('/^[\n\r\t ]+/', '', $GLOBALS['_xh']['ac']); |
|
727 | + break; |
|
728 | + case 'NIL': |
|
729 | + case 'EX:NIL': |
|
730 | + if ($GLOBALS['xmlrpc_null_extension']) |
|
731 | + { |
|
732 | + $GLOBALS['_xh']['vt']='null'; |
|
733 | + $GLOBALS['_xh']['value']=null; |
|
734 | + $GLOBALS['_xh']['lv']=3; |
|
735 | + break; |
|
736 | + } |
|
737 | + // drop through intentionally if nil extension not enabled |
|
738 | + case 'PARAMS': |
|
739 | + case 'FAULT': |
|
740 | + case 'METHODCALL': |
|
741 | + case 'METHORESPONSE': |
|
742 | + break; |
|
743 | + default: |
|
744 | + // End of INVALID ELEMENT! |
|
745 | + // shall we add an assert here for unreachable code??? |
|
746 | + break; |
|
747 | + } |
|
748 | + } |
|
749 | + } |
|
750 | + |
|
751 | + /// Used in decoding xmlrpc requests/responses without rebuilding xmlrpc values |
|
752 | + function xmlrpc_ee_fast($parser, $name) |
|
753 | + { |
|
754 | + xmlrpc_ee($parser, $name, false); |
|
755 | + } |
|
756 | + |
|
757 | + /// xml parser handler function for character data |
|
758 | + function xmlrpc_cd($parser, $data) |
|
759 | + { |
|
760 | + // skip processing if xml fault already detected |
|
761 | + if ($GLOBALS['_xh']['isf'] < 2) |
|
762 | + { |
|
763 | + // "lookforvalue==3" means that we've found an entire value |
|
764 | + // and should discard any further character data |
|
765 | + if($GLOBALS['_xh']['lv']!=3) |
|
766 | + { |
|
767 | + // G. Giunta 2006-08-23: useless change of 'lv' from 1 to 2 |
|
768 | + //if($GLOBALS['_xh']['lv']==1) |
|
769 | + //{ |
|
770 | + // if we've found text and we're just in a <value> then |
|
771 | + // say we've found a value |
|
772 | + //$GLOBALS['_xh']['lv']=2; |
|
773 | + //} |
|
774 | + // we always initialize the accumulator before starting parsing, anyway... |
|
775 | + //if(!@isset($GLOBALS['_xh']['ac'])) |
|
776 | + //{ |
|
777 | + // $GLOBALS['_xh']['ac'] = ''; |
|
778 | + //} |
|
779 | + $GLOBALS['_xh']['ac'].=$data; |
|
780 | + } |
|
781 | + } |
|
782 | + } |
|
783 | + |
|
784 | + /// xml parser handler function for 'other stuff', ie. not char data or |
|
785 | + /// element start/end tag. In fact it only gets called on unknown entities... |
|
786 | + function xmlrpc_dh($parser, $data) |
|
787 | + { |
|
788 | + // skip processing if xml fault already detected |
|
789 | + if ($GLOBALS['_xh']['isf'] < 2) |
|
790 | + { |
|
791 | + if(substr($data, 0, 1) == '&' && substr($data, -1, 1) == ';') |
|
792 | + { |
|
793 | + // G. Giunta 2006-08-25: useless change of 'lv' from 1 to 2 |
|
794 | + //if($GLOBALS['_xh']['lv']==1) |
|
795 | + //{ |
|
796 | + // $GLOBALS['_xh']['lv']=2; |
|
797 | + //} |
|
798 | + $GLOBALS['_xh']['ac'].=$data; |
|
799 | + } |
|
800 | + } |
|
801 | + return true; |
|
802 | + } |
|
803 | + |
|
804 | + class xmlrpc_client |
|
805 | + { |
|
806 | + var $path; |
|
807 | + var $server; |
|
808 | + var $port=0; |
|
809 | + var $method='http'; |
|
810 | + var $errno; |
|
811 | + var $errstr; |
|
812 | + var $debug=0; |
|
813 | + var $username=''; |
|
814 | + var $password=''; |
|
815 | + var $authtype=1; |
|
816 | + var $cert=''; |
|
817 | + var $certpass=''; |
|
818 | + var $cacert=''; |
|
819 | + var $cacertdir=''; |
|
820 | + var $key=''; |
|
821 | + var $keypass=''; |
|
822 | + var $verifypeer=true; |
|
823 | + var $verifyhost=1; |
|
824 | + var $no_multicall=false; |
|
825 | + var $proxy=''; |
|
826 | + var $proxyport=0; |
|
827 | + var $proxy_user=''; |
|
828 | + var $proxy_pass=''; |
|
829 | + var $proxy_authtype=1; |
|
830 | + var $cookies=array(); |
|
831 | + var $extracurlopts=array(); |
|
832 | + |
|
833 | + /** |
|
834 | + * List of http compression methods accepted by the client for responses. |
|
835 | + * NB: PHP supports deflate, gzip compressions out of the box if compiled w. zlib |
|
836 | + * |
|
837 | + * NNB: you can set it to any non-empty array for HTTP11 and HTTPS, since |
|
838 | + * in those cases it will be up to CURL to decide the compression methods |
|
839 | + * it supports. You might check for the presence of 'zlib' in the output of |
|
840 | + * curl_version() to determine wheter compression is supported or not |
|
841 | + */ |
|
842 | + var $accepted_compression = array(); |
|
843 | + /** |
|
844 | + * Name of compression scheme to be used for sending requests. |
|
845 | + * Either null, gzip or deflate |
|
846 | + */ |
|
847 | + var $request_compression = ''; |
|
848 | + /** |
|
849 | + * CURL handle: used for keep-alive connections (PHP 4.3.8 up, see: |
|
850 | + * http://curl.haxx.se/docs/faq.html#7.3) |
|
851 | + */ |
|
852 | + var $xmlrpc_curl_handle = null; |
|
853 | + /// Whether to use persistent connections for http 1.1 and https |
|
854 | + var $keepalive = false; |
|
855 | + /// Charset encodings that can be decoded without problems by the client |
|
856 | + var $accepted_charset_encodings = array(); |
|
857 | + /// Charset encoding to be used in serializing request. NULL = use ASCII |
|
858 | + var $request_charset_encoding = ''; |
|
859 | + /** |
|
860 | + * Decides the content of xmlrpcresp objects returned by calls to send() |
|
861 | + * valid strings are 'xmlrpcvals', 'phpvals' or 'xml' |
|
862 | + */ |
|
863 | + var $return_type = 'xmlrpcvals'; |
|
864 | + /** |
|
865 | + * Sent to servers in http headers |
|
866 | + */ |
|
867 | + var $user_agent; |
|
868 | + |
|
869 | + /** |
|
870 | + * @param string $path either the complete server URL or the PATH part of the xmlrc server URL, e.g. /xmlrpc/server.php |
|
871 | + * @param string $server the server name / ip address |
|
872 | + * @param integer $port the port the server is listening on, defaults to 80 or 443 depending on protocol used |
|
873 | + * @param string $method the http protocol variant: defaults to 'http', 'https' and 'http11' can be used if CURL is installed |
|
874 | + */ |
|
875 | + function __construct($path, $server='', $port='', $method='') |
|
876 | + { |
|
877 | + // allow user to specify all params in $path |
|
878 | + if($server == '' and $port == '' and $method == '') |
|
879 | + { |
|
880 | + $parts = parse_url($path); |
|
881 | + $server = $parts['host']; |
|
882 | + $path = isset($parts['path']) ? $parts['path'] : ''; |
|
883 | + if(isset($parts['query'])) |
|
884 | + { |
|
885 | + $path .= '?'.$parts['query']; |
|
886 | + } |
|
887 | + if(isset($parts['fragment'])) |
|
888 | + { |
|
889 | + $path .= '#'.$parts['fragment']; |
|
890 | + } |
|
891 | + if(isset($parts['port'])) |
|
892 | + { |
|
893 | + $port = $parts['port']; |
|
894 | + } |
|
895 | + if(isset($parts['scheme'])) |
|
896 | + { |
|
897 | + $method = $parts['scheme']; |
|
898 | + } |
|
899 | + if(isset($parts['user'])) |
|
900 | + { |
|
901 | + $this->username = $parts['user']; |
|
902 | + } |
|
903 | + if(isset($parts['pass'])) |
|
904 | + { |
|
905 | + $this->password = $parts['pass']; |
|
906 | + } |
|
907 | + } |
|
908 | + if($path == '' || $path[0] != '/') |
|
909 | + { |
|
910 | + $this->path='/'.$path; |
|
911 | + } |
|
912 | + else |
|
913 | + { |
|
914 | + $this->path=$path; |
|
915 | + } |
|
916 | + $this->server=$server; |
|
917 | + if($port != '') |
|
918 | + { |
|
919 | + $this->port=$port; |
|
920 | + } |
|
921 | + if($method != '') |
|
922 | + { |
|
923 | + $this->method=$method; |
|
924 | + } |
|
925 | + |
|
926 | + // if ZLIB is enabled, let the client by default accept compressed responses |
|
927 | + if(function_exists('gzinflate') || ( |
|
928 | + function_exists('curl_init') && (($info = curl_version()) && |
|
929 | + ((is_string($info) && strpos($info, 'zlib') !== null) || isset($info['libz_version']))) |
|
930 | + )) |
|
931 | + { |
|
932 | + $this->accepted_compression = array('gzip', 'deflate'); |
|
933 | + } |
|
934 | + |
|
935 | + // keepalives: enabled by default |
|
936 | + $this->keepalive = true; |
|
937 | + |
|
938 | + // by default the xml parser can support these 3 charset encodings |
|
939 | + $this->accepted_charset_encodings = array('UTF-8', 'ISO-8859-1', 'US-ASCII'); |
|
940 | + |
|
941 | + // initialize user_agent string |
|
942 | + $this->user_agent = $GLOBALS['xmlrpcName'] . ' ' . $GLOBALS['xmlrpcVersion']; |
|
943 | + } |
|
944 | + |
|
945 | + /** |
|
946 | + * @deprecated |
|
947 | + */ |
|
948 | + function xmlrpc_client($path, $server='', $port='', $method='') |
|
949 | + { |
|
950 | + self::__construct($path, $server, $port, $method); |
|
951 | + } |
|
952 | + |
|
953 | + /** |
|
954 | + * Enables/disables the echoing to screen of the xmlrpc responses received |
|
955 | + * @param integer $in values 0, 1 and 2 are supported (2 = echo sent msg too, before received response) |
|
956 | + * @access public |
|
957 | + */ |
|
958 | + function setDebug($in) |
|
959 | + { |
|
960 | + $this->debug=$in; |
|
961 | + } |
|
962 | + |
|
963 | + /** |
|
964 | + * Add some http BASIC AUTH credentials, used by the client to authenticate |
|
965 | + * @param string $u username |
|
966 | + * @param string $p password |
|
967 | + * @param integer $t auth type. See curl_setopt man page for supported auth types. Defaults to CURLAUTH_BASIC (basic auth) |
|
968 | + * @access public |
|
969 | + */ |
|
970 | + function setCredentials($u, $p, $t=1) |
|
971 | + { |
|
972 | + $this->username=$u; |
|
973 | + $this->password=$p; |
|
974 | + $this->authtype=$t; |
|
975 | + } |
|
976 | + |
|
977 | + /** |
|
978 | + * Add a client-side https certificate |
|
979 | + * @param string $cert |
|
980 | + * @param string $certpass |
|
981 | + * @access public |
|
982 | + */ |
|
983 | + function setCertificate($cert, $certpass) |
|
984 | + { |
|
985 | + $this->cert = $cert; |
|
986 | + $this->certpass = $certpass; |
|
987 | + } |
|
988 | + |
|
989 | + /** |
|
990 | + * Add a CA certificate to verify server with (see man page about |
|
991 | + * CURLOPT_CAINFO for more details) |
|
992 | + * @param string $cacert certificate file name (or dir holding certificates) |
|
993 | + * @param bool $is_dir set to true to indicate cacert is a dir. defaults to false |
|
994 | + * @access public |
|
995 | + */ |
|
996 | + function setCaCertificate($cacert, $is_dir=false) |
|
997 | + { |
|
998 | + if ($is_dir) |
|
999 | + { |
|
1000 | + $this->cacertdir = $cacert; |
|
1001 | + } |
|
1002 | + else |
|
1003 | + { |
|
1004 | + $this->cacert = $cacert; |
|
1005 | + } |
|
1006 | + } |
|
1007 | + |
|
1008 | + /** |
|
1009 | + * Set attributes for SSL communication: private SSL key |
|
1010 | + * NB: does not work in older php/curl installs |
|
1011 | + * Thanks to Daniel Convissor |
|
1012 | + * @param string $key The name of a file containing a private SSL key |
|
1013 | + * @param string $keypass The secret password needed to use the private SSL key |
|
1014 | + * @access public |
|
1015 | + */ |
|
1016 | + function setKey($key, $keypass) |
|
1017 | + { |
|
1018 | + $this->key = $key; |
|
1019 | + $this->keypass = $keypass; |
|
1020 | + } |
|
1021 | + |
|
1022 | + /** |
|
1023 | + * Set attributes for SSL communication: verify server certificate |
|
1024 | + * @param bool $i enable/disable verification of peer certificate |
|
1025 | + * @access public |
|
1026 | + */ |
|
1027 | + function setSSLVerifyPeer($i) |
|
1028 | + { |
|
1029 | + $this->verifypeer = $i; |
|
1030 | + } |
|
1031 | + |
|
1032 | + /** |
|
1033 | + * Set attributes for SSL communication: verify match of server cert w. hostname |
|
1034 | + * @param int $i |
|
1035 | + * @access public |
|
1036 | + */ |
|
1037 | + function setSSLVerifyHost($i) |
|
1038 | + { |
|
1039 | + $this->verifyhost = $i; |
|
1040 | + } |
|
1041 | + |
|
1042 | + /** |
|
1043 | + * Set proxy info |
|
1044 | + * @param string $proxyhost |
|
1045 | + * @param string $proxyport Defaults to 8080 for HTTP and 443 for HTTPS |
|
1046 | + * @param string $proxyusername Leave blank if proxy has public access |
|
1047 | + * @param string $proxypassword Leave blank if proxy has public access |
|
1048 | + * @param int $proxyauthtype set to constant CURLAUTH_NTLM to use NTLM auth with proxy |
|
1049 | + * @access public |
|
1050 | + */ |
|
1051 | + function setProxy($proxyhost, $proxyport, $proxyusername = '', $proxypassword = '', $proxyauthtype = 1) |
|
1052 | + { |
|
1053 | + $this->proxy = $proxyhost; |
|
1054 | + $this->proxyport = $proxyport; |
|
1055 | + $this->proxy_user = $proxyusername; |
|
1056 | + $this->proxy_pass = $proxypassword; |
|
1057 | + $this->proxy_authtype = $proxyauthtype; |
|
1058 | + } |
|
1059 | + |
|
1060 | + /** |
|
1061 | + * Enables/disables reception of compressed xmlrpc responses. |
|
1062 | + * Note that enabling reception of compressed responses merely adds some standard |
|
1063 | + * http headers to xmlrpc requests. It is up to the xmlrpc server to return |
|
1064 | + * compressed responses when receiving such requests. |
|
1065 | + * @param string $compmethod either 'gzip', 'deflate', 'any' or '' |
|
1066 | + * @access public |
|
1067 | + */ |
|
1068 | + function setAcceptedCompression($compmethod) |
|
1069 | + { |
|
1070 | + if ($compmethod == 'any') |
|
1071 | + $this->accepted_compression = array('gzip', 'deflate'); |
|
1072 | + else |
|
1073 | + if ($compmethod == false ) |
|
1074 | + $this->accepted_compression = array(); |
|
1075 | + else |
|
1076 | + $this->accepted_compression = array($compmethod); |
|
1077 | + } |
|
1078 | + |
|
1079 | + /** |
|
1080 | + * Enables/disables http compression of xmlrpc request. |
|
1081 | + * Take care when sending compressed requests: servers might not support them |
|
1082 | + * (and automatic fallback to uncompressed requests is not yet implemented) |
|
1083 | + * @param string $compmethod either 'gzip', 'deflate' or '' |
|
1084 | + * @access public |
|
1085 | + */ |
|
1086 | + function setRequestCompression($compmethod) |
|
1087 | + { |
|
1088 | + $this->request_compression = $compmethod; |
|
1089 | + } |
|
1090 | + |
|
1091 | + /** |
|
1092 | + * Adds a cookie to list of cookies that will be sent to server. |
|
1093 | + * NB: setting any param but name and value will turn the cookie into a 'version 1' cookie: |
|
1094 | + * do not do it unless you know what you are doing |
|
1095 | + * @param string $name |
|
1096 | + * @param string $value |
|
1097 | + * @param string $path |
|
1098 | + * @param string $domain |
|
1099 | + * @param int $port |
|
1100 | + * @access public |
|
1101 | + * |
|
1102 | + * @todo check correctness of urlencoding cookie value (copied from php way of doing it...) |
|
1103 | + */ |
|
1104 | + function setCookie($name, $value='', $path='', $domain='', $port=null) |
|
1105 | + { |
|
1106 | + $this->cookies[$name]['value'] = urlencode($value); |
|
1107 | + if ($path || $domain || $port) |
|
1108 | + { |
|
1109 | + $this->cookies[$name]['path'] = $path; |
|
1110 | + $this->cookies[$name]['domain'] = $domain; |
|
1111 | + $this->cookies[$name]['port'] = $port; |
|
1112 | + $this->cookies[$name]['version'] = 1; |
|
1113 | + } |
|
1114 | + else |
|
1115 | + { |
|
1116 | + $this->cookies[$name]['version'] = 0; |
|
1117 | + } |
|
1118 | + } |
|
1119 | + |
|
1120 | + /** |
|
1121 | + * Directly set cURL options, for extra flexibility |
|
1122 | + * It allows eg. to bind client to a specific IP interface / address |
|
1123 | + * @param array $options |
|
1124 | + */ |
|
1125 | + function SetCurlOptions( $options ) |
|
1126 | + { |
|
1127 | + $this->extracurlopts = $options; |
|
1128 | + } |
|
1129 | + |
|
1130 | + /** |
|
1131 | + * Set user-agent string that will be used by this client instance |
|
1132 | + * in http headers sent to the server |
|
1133 | + */ |
|
1134 | + function SetUserAgent( $agentstring ) |
|
1135 | + { |
|
1136 | + $this->user_agent = $agentstring; |
|
1137 | + } |
|
1138 | + |
|
1139 | + /** |
|
1140 | + * Send an xmlrpc request |
|
1141 | + * @param mixed $msg The message object, or an array of messages for using multicall, or the complete xml representation of a request |
|
1142 | + * @param integer $timeout Connection timeout, in seconds, If unspecified, a platform specific timeout will apply |
|
1143 | + * @param string $method if left unspecified, the http protocol chosen during creation of the object will be used |
|
1144 | + * @return xmlrpcresp |
|
1145 | + * @access public |
|
1146 | + */ |
|
1147 | + function& send($msg, $timeout=0, $method='') |
|
1148 | + { |
|
1149 | + // if user deos not specify http protocol, use native method of this client |
|
1150 | + // (i.e. method set during call to constructor) |
|
1151 | + if($method == '') |
|
1152 | + { |
|
1153 | + $method = $this->method; |
|
1154 | + } |
|
1155 | + |
|
1156 | + if(is_array($msg)) |
|
1157 | + { |
|
1158 | + // $msg is an array of xmlrpcmsg's |
|
1159 | + $r = $this->multicall($msg, $timeout, $method); |
|
1160 | + return $r; |
|
1161 | + } |
|
1162 | + elseif(is_string($msg)) |
|
1163 | + { |
|
1164 | + $n = new xmlrpcmsg(''); |
|
1165 | + $n->payload = $msg; |
|
1166 | + $msg = $n; |
|
1167 | + } |
|
1168 | + |
|
1169 | + // where msg is an xmlrpcmsg |
|
1170 | + $msg->debug=$this->debug; |
|
1171 | + |
|
1172 | + if($method == 'https') |
|
1173 | + { |
|
1174 | + $r =& $this->sendPayloadHTTPS( |
|
1175 | + $msg, |
|
1176 | + $this->server, |
|
1177 | + $this->port, |
|
1178 | + $timeout, |
|
1179 | + $this->username, |
|
1180 | + $this->password, |
|
1181 | + $this->authtype, |
|
1182 | + $this->cert, |
|
1183 | + $this->certpass, |
|
1184 | + $this->cacert, |
|
1185 | + $this->cacertdir, |
|
1186 | + $this->proxy, |
|
1187 | + $this->proxyport, |
|
1188 | + $this->proxy_user, |
|
1189 | + $this->proxy_pass, |
|
1190 | + $this->proxy_authtype, |
|
1191 | + $this->keepalive, |
|
1192 | + $this->key, |
|
1193 | + $this->keypass |
|
1194 | + ); |
|
1195 | + } |
|
1196 | + elseif($method == 'http11') |
|
1197 | + { |
|
1198 | + $r =& $this->sendPayloadCURL( |
|
1199 | + $msg, |
|
1200 | + $this->server, |
|
1201 | + $this->port, |
|
1202 | + $timeout, |
|
1203 | + $this->username, |
|
1204 | + $this->password, |
|
1205 | + $this->authtype, |
|
1206 | + null, |
|
1207 | + null, |
|
1208 | + null, |
|
1209 | + null, |
|
1210 | + $this->proxy, |
|
1211 | + $this->proxyport, |
|
1212 | + $this->proxy_user, |
|
1213 | + $this->proxy_pass, |
|
1214 | + $this->proxy_authtype, |
|
1215 | + 'http', |
|
1216 | + $this->keepalive |
|
1217 | + ); |
|
1218 | + } |
|
1219 | + else |
|
1220 | + { |
|
1221 | + $r =& $this->sendPayloadHTTP10( |
|
1222 | + $msg, |
|
1223 | + $this->server, |
|
1224 | + $this->port, |
|
1225 | + $timeout, |
|
1226 | + $this->username, |
|
1227 | + $this->password, |
|
1228 | + $this->authtype, |
|
1229 | + $this->proxy, |
|
1230 | + $this->proxyport, |
|
1231 | + $this->proxy_user, |
|
1232 | + $this->proxy_pass, |
|
1233 | + $this->proxy_authtype |
|
1234 | + ); |
|
1235 | + } |
|
1236 | + |
|
1237 | + return $r; |
|
1238 | + } |
|
1239 | + |
|
1240 | + /** |
|
1241 | + * @access private |
|
1242 | + */ |
|
1243 | + function &sendPayloadHTTP10($msg, $server, $port, $timeout=0, |
|
1244 | + $username='', $password='', $authtype=1, $proxyhost='', |
|
1245 | + $proxyport=0, $proxyusername='', $proxypassword='', $proxyauthtype=1) |
|
1246 | + { |
|
1247 | + if($port==0) |
|
1248 | + { |
|
1249 | + $port=80; |
|
1250 | + } |
|
1251 | + |
|
1252 | + // Only create the payload if it was not created previously |
|
1253 | + if(empty($msg->payload)) |
|
1254 | + { |
|
1255 | + $msg->createPayload($this->request_charset_encoding); |
|
1256 | + } |
|
1257 | + |
|
1258 | + $payload = $msg->payload; |
|
1259 | + // Deflate request body and set appropriate request headers |
|
1260 | + if(function_exists('gzdeflate') && ($this->request_compression == 'gzip' || $this->request_compression == 'deflate')) |
|
1261 | + { |
|
1262 | + if($this->request_compression == 'gzip') |
|
1263 | + { |
|
1264 | + $a = @gzencode($payload); |
|
1265 | + if($a) |
|
1266 | + { |
|
1267 | + $payload = $a; |
|
1268 | + $encoding_hdr = "Content-Encoding: gzip\r\n"; |
|
1269 | + } |
|
1270 | + } |
|
1271 | + else |
|
1272 | + { |
|
1273 | + $a = @gzcompress($payload); |
|
1274 | + if($a) |
|
1275 | + { |
|
1276 | + $payload = $a; |
|
1277 | + $encoding_hdr = "Content-Encoding: deflate\r\n"; |
|
1278 | + } |
|
1279 | + } |
|
1280 | + } |
|
1281 | + else |
|
1282 | + { |
|
1283 | + $encoding_hdr = ''; |
|
1284 | + } |
|
1285 | + |
|
1286 | + // thanks to Grant Rauscher <[email protected]> for this |
|
1287 | + $credentials=''; |
|
1288 | + if($username!='') |
|
1289 | + { |
|
1290 | + $credentials='Authorization: Basic ' . base64_encode($username . ':' . $password) . "\r\n"; |
|
1291 | + if ($authtype != 1) |
|
1292 | + { |
|
1293 | + error_log('XML-RPC: '.__METHOD__.': warning. Only Basic auth is supported with HTTP 1.0'); |
|
1294 | + } |
|
1295 | + } |
|
1296 | + |
|
1297 | + $accepted_encoding = ''; |
|
1298 | + if(is_array($this->accepted_compression) && count($this->accepted_compression)) |
|
1299 | + { |
|
1300 | + $accepted_encoding = 'Accept-Encoding: ' . implode(', ', $this->accepted_compression) . "\r\n"; |
|
1301 | + } |
|
1302 | + |
|
1303 | + $proxy_credentials = ''; |
|
1304 | + if($proxyhost) |
|
1305 | + { |
|
1306 | + if($proxyport == 0) |
|
1307 | + { |
|
1308 | + $proxyport = 8080; |
|
1309 | + } |
|
1310 | + $connectserver = $proxyhost; |
|
1311 | + $connectport = $proxyport; |
|
1312 | + $uri = 'http://'.$server.':'.$port.$this->path; |
|
1313 | + if($proxyusername != '') |
|
1314 | + { |
|
1315 | + if ($proxyauthtype != 1) |
|
1316 | + { |
|
1317 | + error_log('XML-RPC: '.__METHOD__.': warning. Only Basic auth to proxy is supported with HTTP 1.0'); |
|
1318 | + } |
|
1319 | + $proxy_credentials = 'Proxy-Authorization: Basic ' . base64_encode($proxyusername.':'.$proxypassword) . "\r\n"; |
|
1320 | + } |
|
1321 | + } |
|
1322 | + else |
|
1323 | + { |
|
1324 | + $connectserver = $server; |
|
1325 | + $connectport = $port; |
|
1326 | + $uri = $this->path; |
|
1327 | + } |
|
1328 | + |
|
1329 | + // Cookie generation, as per rfc2965 (version 1 cookies) or |
|
1330 | + // netscape's rules (version 0 cookies) |
|
1331 | + $cookieheader=''; |
|
1332 | + if (count($this->cookies)) |
|
1333 | + { |
|
1334 | + $version = ''; |
|
1335 | + foreach ($this->cookies as $name => $cookie) |
|
1336 | + { |
|
1337 | + if ($cookie['version']) |
|
1338 | + { |
|
1339 | + $version = ' $Version="' . $cookie['version'] . '";'; |
|
1340 | + $cookieheader .= ' ' . $name . '="' . $cookie['value'] . '";'; |
|
1341 | + if ($cookie['path']) |
|
1342 | + $cookieheader .= ' $Path="' . $cookie['path'] . '";'; |
|
1343 | + if ($cookie['domain']) |
|
1344 | + $cookieheader .= ' $Domain="' . $cookie['domain'] . '";'; |
|
1345 | + if ($cookie['port']) |
|
1346 | + $cookieheader .= ' $Port="' . $cookie['port'] . '";'; |
|
1347 | + } |
|
1348 | + else |
|
1349 | + { |
|
1350 | + $cookieheader .= ' ' . $name . '=' . $cookie['value'] . ";"; |
|
1351 | + } |
|
1352 | + } |
|
1353 | + $cookieheader = 'Cookie:' . $version . substr($cookieheader, 0, -1) . "\r\n"; |
|
1354 | + } |
|
1355 | + |
|
1356 | + // omit port if 80 |
|
1357 | + $port = ($port == 80) ? '' : (':' . $port); |
|
1358 | + |
|
1359 | + $op= 'POST ' . $uri. " HTTP/1.0\r\n" . |
|
1360 | + 'User-Agent: ' . $this->user_agent . "\r\n" . |
|
1361 | + 'Host: '. $server . $port . "\r\n" . |
|
1362 | + $credentials . |
|
1363 | + $proxy_credentials . |
|
1364 | + $accepted_encoding . |
|
1365 | + $encoding_hdr . |
|
1366 | + 'Accept-Charset: ' . implode(',', $this->accepted_charset_encodings) . "\r\n" . |
|
1367 | + $cookieheader . |
|
1368 | + 'Content-Type: ' . $msg->content_type . "\r\nContent-Length: " . |
|
1369 | + strlen($payload) . "\r\n\r\n" . |
|
1370 | + $payload; |
|
1371 | + |
|
1372 | + if($this->debug > 1) |
|
1373 | + { |
|
1374 | + print "<PRE>\n---SENDING---\n" . htmlentities($op) . "\n---END---\n</PRE>"; |
|
1375 | + // let the client see this now in case http times out... |
|
1376 | + flush(); |
|
1377 | + } |
|
1378 | + |
|
1379 | + if($timeout>0) |
|
1380 | + { |
|
1381 | + $fp=@fsockopen($connectserver, $connectport, $this->errno, $this->errstr, $timeout); |
|
1382 | + } |
|
1383 | + else |
|
1384 | + { |
|
1385 | + $fp=@fsockopen($connectserver, $connectport, $this->errno, $this->errstr); |
|
1386 | + } |
|
1387 | + if($fp) |
|
1388 | + { |
|
1389 | + if($timeout>0 && function_exists('stream_set_timeout')) |
|
1390 | + { |
|
1391 | + stream_set_timeout($fp, $timeout); |
|
1392 | + } |
|
1393 | + } |
|
1394 | + else |
|
1395 | + { |
|
1396 | + $this->errstr='Connect error: '.$this->errstr; |
|
1397 | + $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['http_error'], $this->errstr . ' (' . $this->errno . ')'); |
|
1398 | + return $r; |
|
1399 | + } |
|
1400 | + |
|
1401 | + if(!fputs($fp, $op, strlen($op))) |
|
1402 | + { |
|
1403 | + fclose($fp); |
|
1404 | + $this->errstr='Write error'; |
|
1405 | + $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['http_error'], $this->errstr); |
|
1406 | + return $r; |
|
1407 | + } |
|
1408 | + else |
|
1409 | + { |
|
1410 | + // reset errno and errstr on successful socket connection |
|
1411 | + $this->errstr = ''; |
|
1412 | + } |
|
1413 | + // G. Giunta 2005/10/24: close socket before parsing. |
|
1414 | + // should yield slightly better execution times, and make easier recursive calls (e.g. to follow http redirects) |
|
1415 | + $ipd=''; |
|
1416 | + do |
|
1417 | + { |
|
1418 | + // shall we check for $data === FALSE? |
|
1419 | + // as per the manual, it signals an error |
|
1420 | + $ipd.=fread($fp, 32768); |
|
1421 | + } while(!feof($fp)); |
|
1422 | + fclose($fp); |
|
1423 | + $r =& $msg->parseResponse($ipd, false, $this->return_type); |
|
1424 | + return $r; |
|
1425 | + |
|
1426 | + } |
|
1427 | + |
|
1428 | + /** |
|
1429 | + * @access private |
|
1430 | + */ |
|
1431 | + function &sendPayloadHTTPS($msg, $server, $port, $timeout=0, $username='', |
|
1432 | + $password='', $authtype=1, $cert='',$certpass='', $cacert='', $cacertdir='', |
|
1433 | + $proxyhost='', $proxyport=0, $proxyusername='', $proxypassword='', $proxyauthtype=1, |
|
1434 | + $keepalive=false, $key='', $keypass='') |
|
1435 | + { |
|
1436 | + $r =& $this->sendPayloadCURL($msg, $server, $port, $timeout, $username, |
|
1437 | + $password, $authtype, $cert, $certpass, $cacert, $cacertdir, $proxyhost, $proxyport, |
|
1438 | + $proxyusername, $proxypassword, $proxyauthtype, 'https', $keepalive, $key, $keypass); |
|
1439 | + return $r; |
|
1440 | + } |
|
1441 | + |
|
1442 | + /** |
|
1443 | + * Contributed by Justin Miller <[email protected]> |
|
1444 | + * Requires curl to be built into PHP |
|
1445 | + * NB: CURL versions before 7.11.10 cannot use proxy to talk to https servers! |
|
1446 | + * @access private |
|
1447 | + */ |
|
1448 | + function &sendPayloadCURL($msg, $server, $port, $timeout=0, $username='', |
|
1449 | + $password='', $authtype=1, $cert='', $certpass='', $cacert='', $cacertdir='', |
|
1450 | + $proxyhost='', $proxyport=0, $proxyusername='', $proxypassword='', $proxyauthtype=1, $method='https', |
|
1451 | + $keepalive=false, $key='', $keypass='') |
|
1452 | + { |
|
1453 | + if(!function_exists('curl_init')) |
|
1454 | + { |
|
1455 | + $this->errstr='CURL unavailable on this install'; |
|
1456 | + $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['no_curl'], $GLOBALS['xmlrpcstr']['no_curl']); |
|
1457 | + return $r; |
|
1458 | + } |
|
1459 | + if($method == 'https') |
|
1460 | + { |
|
1461 | + if(($info = curl_version()) && |
|
1462 | + ((is_string($info) && strpos($info, 'OpenSSL') === null) || (is_array($info) && !isset($info['ssl_version'])))) |
|
1463 | + { |
|
1464 | + $this->errstr='SSL unavailable on this install'; |
|
1465 | + $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['no_ssl'], $GLOBALS['xmlrpcstr']['no_ssl']); |
|
1466 | + return $r; |
|
1467 | + } |
|
1468 | + } |
|
1469 | + |
|
1470 | + if($port == 0) |
|
1471 | + { |
|
1472 | + if($method == 'http') |
|
1473 | + { |
|
1474 | + $port = 80; |
|
1475 | + } |
|
1476 | + else |
|
1477 | + { |
|
1478 | + $port = 443; |
|
1479 | + } |
|
1480 | + } |
|
1481 | + |
|
1482 | + // Only create the payload if it was not created previously |
|
1483 | + if(empty($msg->payload)) |
|
1484 | + { |
|
1485 | + $msg->createPayload($this->request_charset_encoding); |
|
1486 | + } |
|
1487 | + |
|
1488 | + // Deflate request body and set appropriate request headers |
|
1489 | + $payload = $msg->payload; |
|
1490 | + if(function_exists('gzdeflate') && ($this->request_compression == 'gzip' || $this->request_compression == 'deflate')) |
|
1491 | + { |
|
1492 | + if($this->request_compression == 'gzip') |
|
1493 | + { |
|
1494 | + $a = @gzencode($payload); |
|
1495 | + if($a) |
|
1496 | + { |
|
1497 | + $payload = $a; |
|
1498 | + $encoding_hdr = 'Content-Encoding: gzip'; |
|
1499 | + } |
|
1500 | + } |
|
1501 | + else |
|
1502 | + { |
|
1503 | + $a = @gzcompress($payload); |
|
1504 | + if($a) |
|
1505 | + { |
|
1506 | + $payload = $a; |
|
1507 | + $encoding_hdr = 'Content-Encoding: deflate'; |
|
1508 | + } |
|
1509 | + } |
|
1510 | + } |
|
1511 | + else |
|
1512 | + { |
|
1513 | + $encoding_hdr = ''; |
|
1514 | + } |
|
1515 | + |
|
1516 | + if($this->debug > 1) |
|
1517 | + { |
|
1518 | + print "<PRE>\n---SENDING---\n" . htmlentities($payload) . "\n---END---\n</PRE>"; |
|
1519 | + // let the client see this now in case http times out... |
|
1520 | + flush(); |
|
1521 | + } |
|
1522 | + |
|
1523 | + if(!$keepalive || !$this->xmlrpc_curl_handle) |
|
1524 | + { |
|
1525 | + $curl = curl_init($method . '://' . $server . ':' . $port . $this->path); |
|
1526 | + if($keepalive) |
|
1527 | + { |
|
1528 | + $this->xmlrpc_curl_handle = $curl; |
|
1529 | + } |
|
1530 | + } |
|
1531 | + else |
|
1532 | + { |
|
1533 | + $curl = $this->xmlrpc_curl_handle; |
|
1534 | + } |
|
1535 | + |
|
1536 | + // results into variable |
|
1537 | + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
|
1538 | + |
|
1539 | + if($this->debug) |
|
1540 | + { |
|
1541 | + curl_setopt($curl, CURLOPT_VERBOSE, 1); |
|
1542 | + } |
|
1543 | + curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent); |
|
1544 | + // required for XMLRPC: post the data |
|
1545 | + curl_setopt($curl, CURLOPT_POST, 1); |
|
1546 | + // the data |
|
1547 | + curl_setopt($curl, CURLOPT_POSTFIELDS, $payload); |
|
1548 | + |
|
1549 | + // return the header too |
|
1550 | + curl_setopt($curl, CURLOPT_HEADER, 1); |
|
1551 | + |
|
1552 | + // NB: if we set an empty string, CURL will add http header indicating |
|
1553 | + // ALL methods it is supporting. This is possibly a better option than |
|
1554 | + // letting the user tell what curl can / cannot do... |
|
1555 | + if(is_array($this->accepted_compression) && count($this->accepted_compression)) |
|
1556 | + { |
|
1557 | + //curl_setopt($curl, CURLOPT_ENCODING, implode(',', $this->accepted_compression)); |
|
1558 | + // empty string means 'any supported by CURL' (shall we catch errors in case CURLOPT_SSLKEY undefined ?) |
|
1559 | + if (count($this->accepted_compression) == 1) |
|
1560 | + { |
|
1561 | + curl_setopt($curl, CURLOPT_ENCODING, $this->accepted_compression[0]); |
|
1562 | + } |
|
1563 | + else |
|
1564 | + curl_setopt($curl, CURLOPT_ENCODING, ''); |
|
1565 | + } |
|
1566 | + // extra headers |
|
1567 | + $headers = array('Content-Type: ' . $msg->content_type , 'Accept-Charset: ' . implode(',', $this->accepted_charset_encodings)); |
|
1568 | + // if no keepalive is wanted, let the server know it in advance |
|
1569 | + if(!$keepalive) |
|
1570 | + { |
|
1571 | + $headers[] = 'Connection: close'; |
|
1572 | + } |
|
1573 | + // request compression header |
|
1574 | + if($encoding_hdr) |
|
1575 | + { |
|
1576 | + $headers[] = $encoding_hdr; |
|
1577 | + } |
|
1578 | + |
|
1579 | + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); |
|
1580 | + // timeout is borked |
|
1581 | + if($timeout) |
|
1582 | + { |
|
1583 | + curl_setopt($curl, CURLOPT_TIMEOUT, $timeout == 1 ? 1 : $timeout - 1); |
|
1584 | + } |
|
1585 | + |
|
1586 | + if($username && $password) |
|
1587 | + { |
|
1588 | + curl_setopt($curl, CURLOPT_USERPWD, $username.':'.$password); |
|
1589 | + if (defined('CURLOPT_HTTPAUTH')) |
|
1590 | + { |
|
1591 | + curl_setopt($curl, CURLOPT_HTTPAUTH, $authtype); |
|
1592 | + } |
|
1593 | + else if ($authtype != 1) |
|
1594 | + { |
|
1595 | + error_log('XML-RPC: '.__METHOD__.': warning. Only Basic auth is supported by the current PHP/curl install'); |
|
1596 | + } |
|
1597 | + } |
|
1598 | + |
|
1599 | + if($method == 'https') |
|
1600 | + { |
|
1601 | + // set cert file |
|
1602 | + if($cert) |
|
1603 | + { |
|
1604 | + curl_setopt($curl, CURLOPT_SSLCERT, $cert); |
|
1605 | + } |
|
1606 | + // set cert password |
|
1607 | + if($certpass) |
|
1608 | + { |
|
1609 | + curl_setopt($curl, CURLOPT_SSLCERTPASSWD, $certpass); |
|
1610 | + } |
|
1611 | + // whether to verify remote host's cert |
|
1612 | + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->verifypeer); |
|
1613 | + // set ca certificates file/dir |
|
1614 | + if($cacert) |
|
1615 | + { |
|
1616 | + curl_setopt($curl, CURLOPT_CAINFO, $cacert); |
|
1617 | + } |
|
1618 | + if($cacertdir) |
|
1619 | + { |
|
1620 | + curl_setopt($curl, CURLOPT_CAPATH, $cacertdir); |
|
1621 | + } |
|
1622 | + // set key file (shall we catch errors in case CURLOPT_SSLKEY undefined ?) |
|
1623 | + if($key) |
|
1624 | + { |
|
1625 | + curl_setopt($curl, CURLOPT_SSLKEY, $key); |
|
1626 | + } |
|
1627 | + // set key password (shall we catch errors in case CURLOPT_SSLKEY undefined ?) |
|
1628 | + if($keypass) |
|
1629 | + { |
|
1630 | + curl_setopt($curl, CURLOPT_SSLKEYPASSWD, $keypass); |
|
1631 | + } |
|
1632 | + |
|
1633 | + // Upgrade transparently to more stringent check for versions of php which do not support otherwise. |
|
1634 | + // Doing it in constructor would be cleaner; doing it here saves us a couple of function calls |
|
1635 | + if($this->verifyhost == 1 && $info = curl_version() && version_compare($info['version'], '7.28.1') >= 0) |
|
1636 | + { |
|
1637 | + $this->verifyhost = 2; |
|
1638 | + } |
|
1639 | + // whether to verify cert's common name (CN); 0 for no, 1 to verify that it exists, and 2 to verify that it matches the hostname used |
|
1640 | + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, $this->verifyhost); |
|
1641 | + } |
|
1642 | + |
|
1643 | + // proxy info |
|
1644 | + if($proxyhost) |
|
1645 | + { |
|
1646 | + if($proxyport == 0) |
|
1647 | + { |
|
1648 | + $proxyport = 8080; // NB: even for HTTPS, local connection is on port 8080 |
|
1649 | + } |
|
1650 | + curl_setopt($curl, CURLOPT_PROXY, $proxyhost.':'.$proxyport); |
|
1651 | + //curl_setopt($curl, CURLOPT_PROXYPORT,$proxyport); |
|
1652 | + if($proxyusername) |
|
1653 | + { |
|
1654 | + curl_setopt($curl, CURLOPT_PROXYUSERPWD, $proxyusername.':'.$proxypassword); |
|
1655 | + if (defined('CURLOPT_PROXYAUTH')) |
|
1656 | + { |
|
1657 | + curl_setopt($curl, CURLOPT_PROXYAUTH, $proxyauthtype); |
|
1658 | + } |
|
1659 | + else if ($proxyauthtype != 1) |
|
1660 | + { |
|
1661 | + error_log('XML-RPC: '.__METHOD__.': warning. Only Basic auth to proxy is supported by the current PHP/curl install'); |
|
1662 | + } |
|
1663 | + } |
|
1664 | + } |
|
1665 | + |
|
1666 | + // NB: should we build cookie http headers by hand rather than let CURL do it? |
|
1667 | + // the following code does not honour 'expires', 'path' and 'domain' cookie attributes |
|
1668 | + // set to client obj the the user... |
|
1669 | + if (count($this->cookies)) |
|
1670 | + { |
|
1671 | + $cookieheader = ''; |
|
1672 | + foreach ($this->cookies as $name => $cookie) |
|
1673 | + { |
|
1674 | + $cookieheader .= $name . '=' . $cookie['value'] . '; '; |
|
1675 | + } |
|
1676 | + curl_setopt($curl, CURLOPT_COOKIE, substr($cookieheader, 0, -2)); |
|
1677 | + } |
|
1678 | + |
|
1679 | + foreach ($this->extracurlopts as $opt => $val) |
|
1680 | + { |
|
1681 | + curl_setopt($curl, $opt, $val); |
|
1682 | + } |
|
1683 | + |
|
1684 | + $result = curl_exec($curl); |
|
1685 | + |
|
1686 | + if ($this->debug > 1) |
|
1687 | + { |
|
1688 | + print "<PRE>\n---CURL INFO---\n"; |
|
1689 | + foreach(curl_getinfo($curl) as $name => $val) |
|
1690 | + { |
|
1691 | + if (is_array($val)) |
|
1692 | + { |
|
1693 | + $val = implode("\n", $val); |
|
1694 | + } |
|
1695 | + print $name . ': ' . htmlentities($val) . "\n"; |
|
1696 | + } |
|
1697 | + |
|
1698 | + print "---END---\n</PRE>"; |
|
1699 | + } |
|
1700 | + |
|
1701 | + if(!$result) /// @todo we should use a better check here - what if we get back '' or '0'? |
|
1702 | + { |
|
1703 | + $this->errstr='no response'; |
|
1704 | + $resp=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['curl_fail'], $GLOBALS['xmlrpcstr']['curl_fail']. ': '. curl_error($curl)); |
|
1705 | + curl_close($curl); |
|
1706 | + if($keepalive) |
|
1707 | + { |
|
1708 | + $this->xmlrpc_curl_handle = null; |
|
1709 | + } |
|
1710 | + } |
|
1711 | + else |
|
1712 | + { |
|
1713 | + if(!$keepalive) |
|
1714 | + { |
|
1715 | + curl_close($curl); |
|
1716 | + } |
|
1717 | + $resp =& $msg->parseResponse($result, true, $this->return_type); |
|
1718 | + // if we got back a 302, we can not reuse the curl handle for later calls |
|
1719 | + if($resp->faultCode() == $GLOBALS['xmlrpcerr']['http_error'] && $keepalive) |
|
1720 | + { |
|
1721 | + curl_close($curl); |
|
1722 | + $this->xmlrpc_curl_handle = null; |
|
1723 | + } |
|
1724 | + } |
|
1725 | + return $resp; |
|
1726 | + } |
|
1727 | + |
|
1728 | + /** |
|
1729 | + * Send an array of request messages and return an array of responses. |
|
1730 | + * Unless $this->no_multicall has been set to true, it will try first |
|
1731 | + * to use one single xmlrpc call to server method system.multicall, and |
|
1732 | + * revert to sending many successive calls in case of failure. |
|
1733 | + * This failure is also stored in $this->no_multicall for subsequent calls. |
|
1734 | + * Unfortunately, there is no server error code universally used to denote |
|
1735 | + * the fact that multicall is unsupported, so there is no way to reliably |
|
1736 | + * distinguish between that and a temporary failure. |
|
1737 | + * If you are sure that server supports multicall and do not want to |
|
1738 | + * fallback to using many single calls, set the fourth parameter to FALSE. |
|
1739 | + * |
|
1740 | + * NB: trying to shoehorn extra functionality into existing syntax has resulted |
|
1741 | + * in pretty much convoluted code... |
|
1742 | + * |
|
1743 | + * @param array $msgs an array of xmlrpcmsg objects |
|
1744 | + * @param integer $timeout connection timeout (in seconds) |
|
1745 | + * @param string $method the http protocol variant to be used |
|
1746 | + * @param boolean fallback When true, upon receiving an error during multicall, multiple single calls will be attempted |
|
1747 | + * @return array |
|
1748 | + * @access public |
|
1749 | + */ |
|
1750 | + function multicall($msgs, $timeout=0, $method='', $fallback=true) |
|
1751 | + { |
|
1752 | + if ($method == '') |
|
1753 | + { |
|
1754 | + $method = $this->method; |
|
1755 | + } |
|
1756 | + if(!$this->no_multicall) |
|
1757 | + { |
|
1758 | + $results = $this->_try_multicall($msgs, $timeout, $method); |
|
1759 | + if(is_array($results)) |
|
1760 | + { |
|
1761 | + // System.multicall succeeded |
|
1762 | + return $results; |
|
1763 | + } |
|
1764 | + else |
|
1765 | + { |
|
1766 | + // either system.multicall is unsupported by server, |
|
1767 | + // or call failed for some other reason. |
|
1768 | + if ($fallback) |
|
1769 | + { |
|
1770 | + // Don't try it next time... |
|
1771 | + $this->no_multicall = true; |
|
1772 | + } |
|
1773 | + else |
|
1774 | + { |
|
1775 | + if (is_a($results, 'xmlrpcresp')) |
|
1776 | + { |
|
1777 | + $result = $results; |
|
1778 | + } |
|
1779 | + else |
|
1780 | + { |
|
1781 | + $result = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['multicall_error'], $GLOBALS['xmlrpcstr']['multicall_error']); |
|
1782 | + } |
|
1783 | + } |
|
1784 | + } |
|
1785 | + } |
|
1786 | + else |
|
1787 | + { |
|
1788 | + // override fallback, in case careless user tries to do two |
|
1789 | + // opposite things at the same time |
|
1790 | + $fallback = true; |
|
1791 | + } |
|
1792 | + |
|
1793 | + $results = array(); |
|
1794 | + if ($fallback) |
|
1795 | + { |
|
1796 | + // system.multicall is (probably) unsupported by server: |
|
1797 | + // emulate multicall via multiple requests |
|
1798 | + foreach($msgs as $msg) |
|
1799 | + { |
|
1800 | + $results[] =& $this->send($msg, $timeout, $method); |
|
1801 | + } |
|
1802 | + } |
|
1803 | + else |
|
1804 | + { |
|
1805 | + // user does NOT want to fallback on many single calls: |
|
1806 | + // since we should always return an array of responses, |
|
1807 | + // return an array with the same error repeated n times |
|
1808 | + foreach($msgs as $msg) |
|
1809 | + { |
|
1810 | + $results[] = $result; |
|
1811 | + } |
|
1812 | + } |
|
1813 | + return $results; |
|
1814 | + } |
|
1815 | + |
|
1816 | + /** |
|
1817 | + * Attempt to boxcar $msgs via system.multicall. |
|
1818 | + * Returns either an array of xmlrpcreponses, an xmlrpc error response |
|
1819 | + * or false (when received response does not respect valid multicall syntax) |
|
1820 | + * @access private |
|
1821 | + */ |
|
1822 | + function _try_multicall($msgs, $timeout, $method) |
|
1823 | + { |
|
1824 | + // Construct multicall message |
|
1825 | + $calls = array(); |
|
1826 | + foreach($msgs as $msg) |
|
1827 | + { |
|
1828 | + $call['methodName'] = new xmlrpcval($msg->method(),'string'); |
|
1829 | + $numParams = $msg->getNumParams(); |
|
1830 | + $params = array(); |
|
1831 | + for($i = 0; $i < $numParams; $i++) |
|
1832 | + { |
|
1833 | + $params[$i] = $msg->getParam($i); |
|
1834 | + } |
|
1835 | + $call['params'] = new xmlrpcval($params, 'array'); |
|
1836 | + $calls[] = new xmlrpcval($call, 'struct'); |
|
1837 | + } |
|
1838 | + $multicall = new xmlrpcmsg('system.multicall'); |
|
1839 | + $multicall->addParam(new xmlrpcval($calls, 'array')); |
|
1840 | + |
|
1841 | + // Attempt RPC call |
|
1842 | + $result =& $this->send($multicall, $timeout, $method); |
|
1843 | + |
|
1844 | + if($result->faultCode() != 0) |
|
1845 | + { |
|
1846 | + // call to system.multicall failed |
|
1847 | + return $result; |
|
1848 | + } |
|
1849 | + |
|
1850 | + // Unpack responses. |
|
1851 | + $rets = $result->value(); |
|
1852 | + |
|
1853 | + if ($this->return_type == 'xml') |
|
1854 | + { |
|
1855 | + return $rets; |
|
1856 | + } |
|
1857 | + else if ($this->return_type == 'phpvals') |
|
1858 | + { |
|
1859 | + ///@todo test this code branch... |
|
1860 | + $rets = $result->value(); |
|
1861 | + if(!is_array($rets)) |
|
1862 | + { |
|
1863 | + return false; // bad return type from system.multicall |
|
1864 | + } |
|
1865 | + $numRets = count($rets); |
|
1866 | + if($numRets != count($msgs)) |
|
1867 | + { |
|
1868 | + return false; // wrong number of return values. |
|
1869 | + } |
|
1870 | + |
|
1871 | + $response = array(); |
|
1872 | + for($i = 0; $i < $numRets; $i++) |
|
1873 | + { |
|
1874 | + $val = $rets[$i]; |
|
1875 | + if (!is_array($val)) { |
|
1876 | + return false; |
|
1877 | + } |
|
1878 | + switch(count($val)) |
|
1879 | + { |
|
1880 | + case 1: |
|
1881 | + if(!isset($val[0])) |
|
1882 | + { |
|
1883 | + return false; // Bad value |
|
1884 | + } |
|
1885 | + // Normal return value |
|
1886 | + $response[$i] = new xmlrpcresp($val[0], 0, '', 'phpvals'); |
|
1887 | + break; |
|
1888 | + case 2: |
|
1889 | + /// @todo remove usage of @: it is apparently quite slow |
|
1890 | + $code = @$val['faultCode']; |
|
1891 | + if(!is_int($code)) |
|
1892 | + { |
|
1893 | + return false; |
|
1894 | + } |
|
1895 | + $str = @$val['faultString']; |
|
1896 | + if(!is_string($str)) |
|
1897 | + { |
|
1898 | + return false; |
|
1899 | + } |
|
1900 | + $response[$i] = new xmlrpcresp(0, $code, $str); |
|
1901 | + break; |
|
1902 | + default: |
|
1903 | + return false; |
|
1904 | + } |
|
1905 | + } |
|
1906 | + return $response; |
|
1907 | + } |
|
1908 | + else // return type == 'xmlrpcvals' |
|
1909 | + { |
|
1910 | + $rets = $result->value(); |
|
1911 | + if($rets->kindOf() != 'array') |
|
1912 | + { |
|
1913 | + return false; // bad return type from system.multicall |
|
1914 | + } |
|
1915 | + $numRets = $rets->arraysize(); |
|
1916 | + if($numRets != count($msgs)) |
|
1917 | + { |
|
1918 | + return false; // wrong number of return values. |
|
1919 | + } |
|
1920 | + |
|
1921 | + $response = array(); |
|
1922 | + for($i = 0; $i < $numRets; $i++) |
|
1923 | + { |
|
1924 | + $val = $rets->arraymem($i); |
|
1925 | + switch($val->kindOf()) |
|
1926 | + { |
|
1927 | + case 'array': |
|
1928 | + if($val->arraysize() != 1) |
|
1929 | + { |
|
1930 | + return false; // Bad value |
|
1931 | + } |
|
1932 | + // Normal return value |
|
1933 | + $response[$i] = new xmlrpcresp($val->arraymem(0)); |
|
1934 | + break; |
|
1935 | + case 'struct': |
|
1936 | + $code = $val->structmem('faultCode'); |
|
1937 | + if($code->kindOf() != 'scalar' || $code->scalartyp() != 'int') |
|
1938 | + { |
|
1939 | + return false; |
|
1940 | + } |
|
1941 | + $str = $val->structmem('faultString'); |
|
1942 | + if($str->kindOf() != 'scalar' || $str->scalartyp() != 'string') |
|
1943 | + { |
|
1944 | + return false; |
|
1945 | + } |
|
1946 | + $response[$i] = new xmlrpcresp(0, $code->scalarval(), $str->scalarval()); |
|
1947 | + break; |
|
1948 | + default: |
|
1949 | + return false; |
|
1950 | + } |
|
1951 | + } |
|
1952 | + return $response; |
|
1953 | + } |
|
1954 | + } |
|
1955 | + } // end class xmlrpc_client |
|
1956 | + |
|
1957 | + class xmlrpcresp |
|
1958 | + { |
|
1959 | + var $val = 0; |
|
1960 | + var $valtyp; |
|
1961 | + var $errno = 0; |
|
1962 | + var $errstr = ''; |
|
1963 | + var $payload; |
|
1964 | + var $hdrs = array(); |
|
1965 | + var $_cookies = array(); |
|
1966 | + var $content_type = 'text/xml'; |
|
1967 | + var $raw_data = ''; |
|
1968 | + |
|
1969 | + /** |
|
1970 | + * @param mixed $val either an xmlrpcval obj, a php value or the xml serialization of an xmlrpcval (a string) |
|
1971 | + * @param integer $fcode set it to anything but 0 to create an error response |
|
1972 | + * @param string $fstr the error string, in case of an error response |
|
1973 | + * @param string $valtyp either 'xmlrpcvals', 'phpvals' or 'xml' |
|
1974 | + * |
|
1975 | + * @todo add check that $val / $fcode / $fstr is of correct type??? |
|
1976 | + * NB: as of now we do not do it, since it might be either an xmlrpcval or a plain |
|
1977 | + * php val, or a complete xml chunk, depending on usage of xmlrpc_client::send() inside which creator is called... |
|
1978 | + */ |
|
1979 | + function __construct($val, $fcode = 0, $fstr = '', $valtyp='') |
|
1980 | + { |
|
1981 | + if($fcode != 0) |
|
1982 | + { |
|
1983 | + // error response |
|
1984 | + $this->errno = $fcode; |
|
1985 | + $this->errstr = $fstr; |
|
1986 | + //$this->errstr = htmlspecialchars($fstr); // XXX: encoding probably shouldn't be done here; fix later. |
|
1987 | + } |
|
1988 | + else |
|
1989 | + { |
|
1990 | + // successful response |
|
1991 | + $this->val = $val; |
|
1992 | + if ($valtyp == '') |
|
1993 | + { |
|
1994 | + // user did not declare type of response value: try to guess it |
|
1995 | + if (is_object($this->val) && is_a($this->val, 'xmlrpcval')) |
|
1996 | + { |
|
1997 | + $this->valtyp = 'xmlrpcvals'; |
|
1998 | + } |
|
1999 | + else if (is_string($this->val)) |
|
2000 | + { |
|
2001 | + $this->valtyp = 'xml'; |
|
2002 | + |
|
2003 | + } |
|
2004 | + else |
|
2005 | + { |
|
2006 | + $this->valtyp = 'phpvals'; |
|
2007 | + } |
|
2008 | + } |
|
2009 | + else |
|
2010 | + { |
|
2011 | + // user declares type of resp value: believe him |
|
2012 | + $this->valtyp = $valtyp; |
|
2013 | + } |
|
2014 | + } |
|
2015 | + } |
|
2016 | + |
|
2017 | + /** |
|
2018 | + * @deprecated |
|
2019 | + */ |
|
2020 | + function xmlrpcresp($val, $fcode = 0, $fstr = '', $valtyp='') |
|
2021 | + { |
|
2022 | + self::__construct($val, $fcode, $fstr, $valtyp); |
|
2023 | + } |
|
2024 | + |
|
2025 | + /** |
|
2026 | + * Returns the error code of the response. |
|
2027 | + * @return integer the error code of this response (0 for not-error responses) |
|
2028 | + * @access public |
|
2029 | + */ |
|
2030 | + function faultCode() |
|
2031 | + { |
|
2032 | + return $this->errno; |
|
2033 | + } |
|
2034 | + |
|
2035 | + /** |
|
2036 | + * Returns the error code of the response. |
|
2037 | + * @return string the error string of this response ('' for not-error responses) |
|
2038 | + * @access public |
|
2039 | + */ |
|
2040 | + function faultString() |
|
2041 | + { |
|
2042 | + return $this->errstr; |
|
2043 | + } |
|
2044 | + |
|
2045 | + /** |
|
2046 | + * Returns the value received by the server. |
|
2047 | + * @return mixed the xmlrpcval object returned by the server. Might be an xml string or php value if the response has been created by specially configured xmlrpc_client objects |
|
2048 | + * @access public |
|
2049 | + */ |
|
2050 | + function value() |
|
2051 | + { |
|
2052 | + return $this->val; |
|
2053 | + } |
|
2054 | + |
|
2055 | + /** |
|
2056 | + * Returns an array with the cookies received from the server. |
|
2057 | + * Array has the form: $cookiename => array ('value' => $val, $attr1 => $val1, $attr2 = $val2, ...) |
|
2058 | + * with attributes being e.g. 'expires', 'path', domain'. |
|
2059 | + * NB: cookies sent as 'expired' by the server (i.e. with an expiry date in the past) |
|
2060 | + * are still present in the array. It is up to the user-defined code to decide |
|
2061 | + * how to use the received cookies, and whether they have to be sent back with the next |
|
2062 | + * request to the server (using xmlrpc_client::setCookie) or not |
|
2063 | + * @return array array of cookies received from the server |
|
2064 | + * @access public |
|
2065 | + */ |
|
2066 | + function cookies() |
|
2067 | + { |
|
2068 | + return $this->_cookies; |
|
2069 | + } |
|
2070 | + |
|
2071 | + /** |
|
2072 | + * Returns xml representation of the response. XML prologue not included |
|
2073 | + * @param string $charset_encoding the charset to be used for serialization. if null, US-ASCII is assumed |
|
2074 | + * @return string the xml representation of the response |
|
2075 | + * @access public |
|
2076 | + */ |
|
2077 | + function serialize($charset_encoding='') |
|
2078 | + { |
|
2079 | + if ($charset_encoding != '') |
|
2080 | + $this->content_type = 'text/xml; charset=' . $charset_encoding; |
|
2081 | + else |
|
2082 | + $this->content_type = 'text/xml'; |
|
2083 | + if ($GLOBALS['xmlrpc_null_apache_encoding']) |
|
2084 | + { |
|
2085 | + $result = "<methodResponse xmlns:ex=\"".$GLOBALS['xmlrpc_null_apache_encoding_ns']."\">\n"; |
|
2086 | + } |
|
2087 | + else |
|
2088 | + { |
|
2089 | + $result = "<methodResponse>\n"; |
|
2090 | + } |
|
2091 | + if($this->errno) |
|
2092 | + { |
|
2093 | + // G. Giunta 2005/2/13: let non-ASCII response messages be tolerated by clients |
|
2094 | + // by xml-encoding non ascii chars |
|
2095 | + $result .= "<fault>\n" . |
|
2096 | 2096 | "<value>\n<struct><member><name>faultCode</name>\n<value><int>" . $this->errno . |
2097 | 2097 | "</int></value>\n</member>\n<member>\n<name>faultString</name>\n<value><string>" . |
2098 | 2098 | xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $charset_encoding) . "</string></value>\n</member>\n" . |
2099 | 2099 | "</struct>\n</value>\n</fault>"; |
2100 | - } |
|
2101 | - else |
|
2102 | - { |
|
2103 | - if(!is_object($this->val) || !is_a($this->val, 'xmlrpcval')) |
|
2104 | - { |
|
2105 | - if (is_string($this->val) && $this->valtyp == 'xml') |
|
2106 | - { |
|
2107 | - $result .= "<params>\n<param>\n" . |
|
2108 | - $this->val . |
|
2109 | - "</param>\n</params>"; |
|
2110 | - } |
|
2111 | - else |
|
2112 | - { |
|
2113 | - /// @todo try to build something serializable? |
|
2114 | - die('cannot serialize xmlrpcresp objects whose content is native php values'); |
|
2115 | - } |
|
2116 | - } |
|
2117 | - else |
|
2118 | - { |
|
2119 | - $result .= "<params>\n<param>\n" . |
|
2120 | - $this->val->serialize($charset_encoding) . |
|
2121 | - "</param>\n</params>"; |
|
2122 | - } |
|
2123 | - } |
|
2124 | - $result .= "\n</methodResponse>"; |
|
2125 | - $this->payload = $result; |
|
2126 | - return $result; |
|
2127 | - } |
|
2128 | - } |
|
2129 | - |
|
2130 | - class xmlrpcmsg |
|
2131 | - { |
|
2132 | - var $payload; |
|
2133 | - var $methodname; |
|
2134 | - var $params=array(); |
|
2135 | - var $debug=0; |
|
2136 | - var $content_type = 'text/xml'; |
|
2137 | - |
|
2138 | - /** |
|
2139 | - * @param string $meth the name of the method to invoke |
|
2140 | - * @param array $pars array of parameters to be passed to the method (xmlrpcval objects) |
|
2141 | - */ |
|
2142 | - function __construct($meth, $pars=0) |
|
2143 | - { |
|
2144 | - $this->methodname=$meth; |
|
2145 | - if(is_array($pars) && count($pars)>0) |
|
2146 | - { |
|
2147 | - for($i=0; $i<count($pars); $i++) |
|
2148 | - { |
|
2149 | - $this->addParam($pars[$i]); |
|
2150 | - } |
|
2151 | - } |
|
2152 | - } |
|
2153 | - |
|
2154 | - /** |
|
2155 | - * @deprecated |
|
2156 | - */ |
|
2157 | - function xmlrpcmsg($meth, $pars=0) |
|
2158 | - { |
|
2159 | - self::__construct($meth, $pars); |
|
2160 | - } |
|
2161 | - |
|
2162 | - /** |
|
2163 | - * @access private |
|
2164 | - */ |
|
2165 | - function xml_header($charset_encoding='') |
|
2166 | - { |
|
2167 | - if ($charset_encoding != '') |
|
2168 | - { |
|
2169 | - return "<?xml version=\"1.0\" encoding=\"$charset_encoding\" ?" . ">\n<methodCall>\n"; |
|
2170 | - } |
|
2171 | - else |
|
2172 | - { |
|
2173 | - return "<?xml version=\"1.0\"?" . ">\n<methodCall>\n"; |
|
2174 | - } |
|
2175 | - } |
|
2176 | - |
|
2177 | - /** |
|
2178 | - * @access private |
|
2179 | - */ |
|
2180 | - function xml_footer() |
|
2181 | - { |
|
2182 | - return '</methodCall>'; |
|
2183 | - } |
|
2184 | - |
|
2185 | - /** |
|
2186 | - * @access private |
|
2187 | - */ |
|
2188 | - function kindOf() |
|
2189 | - { |
|
2190 | - return 'msg'; |
|
2191 | - } |
|
2192 | - |
|
2193 | - /** |
|
2194 | - * @access private |
|
2195 | - */ |
|
2196 | - function createPayload($charset_encoding='') |
|
2197 | - { |
|
2198 | - if ($charset_encoding != '') |
|
2199 | - $this->content_type = 'text/xml; charset=' . $charset_encoding; |
|
2200 | - else |
|
2201 | - $this->content_type = 'text/xml'; |
|
2202 | - $this->payload=$this->xml_header($charset_encoding); |
|
2203 | - $this->payload.='<methodName>' . xmlrpc_encode_entitites($this->methodname, $GLOBALS['xmlrpc_internalencoding'], $charset_encoding) . "</methodName>\n"; |
|
2204 | - $this->payload.="<params>\n"; |
|
2205 | - for($i=0; $i<count($this->params); $i++) |
|
2206 | - { |
|
2207 | - $p=$this->params[$i]; |
|
2208 | - $this->payload.="<param>\n" . $p->serialize($charset_encoding) . |
|
2209 | - "</param>\n"; |
|
2210 | - } |
|
2211 | - $this->payload.="</params>\n"; |
|
2212 | - $this->payload.=$this->xml_footer(); |
|
2213 | - } |
|
2214 | - |
|
2215 | - /** |
|
2216 | - * Gets/sets the xmlrpc method to be invoked |
|
2217 | - * @param string $meth the method to be set (leave empty not to set it) |
|
2218 | - * @return string the method that will be invoked |
|
2219 | - * @access public |
|
2220 | - */ |
|
2221 | - function method($meth='') |
|
2222 | - { |
|
2223 | - if($meth!='') |
|
2224 | - { |
|
2225 | - $this->methodname=$meth; |
|
2226 | - } |
|
2227 | - return $this->methodname; |
|
2228 | - } |
|
2229 | - |
|
2230 | - /** |
|
2231 | - * Returns xml representation of the message. XML prologue included |
|
2232 | - * @param string $charset_encoding |
|
2233 | - * @return string the xml representation of the message, xml prologue included |
|
2234 | - * @access public |
|
2235 | - */ |
|
2236 | - function serialize($charset_encoding='') |
|
2237 | - { |
|
2238 | - $this->createPayload($charset_encoding); |
|
2239 | - return $this->payload; |
|
2240 | - } |
|
2241 | - |
|
2242 | - /** |
|
2243 | - * Add a parameter to the list of parameters to be used upon method invocation |
|
2244 | - * @param xmlrpcval $par |
|
2245 | - * @return boolean false on failure |
|
2246 | - * @access public |
|
2247 | - */ |
|
2248 | - function addParam($par) |
|
2249 | - { |
|
2250 | - // add check: do not add to self params which are not xmlrpcvals |
|
2251 | - if(is_object($par) && is_a($par, 'xmlrpcval')) |
|
2252 | - { |
|
2253 | - $this->params[]=$par; |
|
2254 | - return true; |
|
2255 | - } |
|
2256 | - else |
|
2257 | - { |
|
2258 | - return false; |
|
2259 | - } |
|
2260 | - } |
|
2261 | - |
|
2262 | - /** |
|
2263 | - * Returns the nth parameter in the message. The index zero-based. |
|
2264 | - * @param integer $i the index of the parameter to fetch (zero based) |
|
2265 | - * @return xmlrpcval the i-th parameter |
|
2266 | - * @access public |
|
2267 | - */ |
|
2268 | - function getParam($i) { return $this->params[$i]; } |
|
2269 | - |
|
2270 | - /** |
|
2271 | - * Returns the number of parameters in the messge. |
|
2272 | - * @return integer the number of parameters currently set |
|
2273 | - * @access public |
|
2274 | - */ |
|
2275 | - function getNumParams() { return count($this->params); } |
|
2276 | - |
|
2277 | - /** |
|
2278 | - * Given an open file handle, read all data available and parse it as axmlrpc response. |
|
2279 | - * NB: the file handle is not closed by this function. |
|
2280 | - * NNB: might have trouble in rare cases to work on network streams, as we |
|
2281 | - * check for a read of 0 bytes instead of feof($fp). |
|
2282 | - * But since checking for feof(null) returns false, we would risk an |
|
2283 | - * infinite loop in that case, because we cannot trust the caller |
|
2284 | - * to give us a valid pointer to an open file... |
|
2285 | - * @access public |
|
2286 | - * @param resource $fp stream pointer |
|
2287 | - * @return xmlrpcresp |
|
2288 | - * @todo add 2nd & 3rd param to be passed to ParseResponse() ??? |
|
2289 | - */ |
|
2290 | - function &parseResponseFile($fp) |
|
2291 | - { |
|
2292 | - $ipd=''; |
|
2293 | - while($data=fread($fp, 32768)) |
|
2294 | - { |
|
2295 | - $ipd.=$data; |
|
2296 | - } |
|
2297 | - //fclose($fp); |
|
2298 | - $r =& $this->parseResponse($ipd); |
|
2299 | - return $r; |
|
2300 | - } |
|
2301 | - |
|
2302 | - /** |
|
2303 | - * Parses HTTP headers and separates them from data. |
|
2304 | - * @access private |
|
2305 | - */ |
|
2306 | - function &parseResponseHeaders(&$data, $headers_processed=false) |
|
2307 | - { |
|
2308 | - // Support "web-proxy-tunelling" connections for https through proxies |
|
2309 | - if(preg_match('/^HTTP\/1\.[0-1] 200 Connection established/', $data)) |
|
2310 | - { |
|
2311 | - // Look for CR/LF or simple LF as line separator, |
|
2312 | - // (even though it is not valid http) |
|
2313 | - $pos = strpos($data,"\r\n\r\n"); |
|
2314 | - if($pos || is_int($pos)) |
|
2315 | - { |
|
2316 | - $bd = $pos+4; |
|
2317 | - } |
|
2318 | - else |
|
2319 | - { |
|
2320 | - $pos = strpos($data,"\n\n"); |
|
2321 | - if($pos || is_int($pos)) |
|
2322 | - { |
|
2323 | - $bd = $pos+2; |
|
2324 | - } |
|
2325 | - else |
|
2326 | - { |
|
2327 | - // No separation between response headers and body: fault? |
|
2328 | - $bd = 0; |
|
2329 | - } |
|
2330 | - } |
|
2331 | - if ($bd) |
|
2332 | - { |
|
2333 | - // this filters out all http headers from proxy. |
|
2334 | - // maybe we could take them into account, too? |
|
2335 | - $data = substr($data, $bd); |
|
2336 | - } |
|
2337 | - else |
|
2338 | - { |
|
2339 | - error_log('XML-RPC: '.__METHOD__.': HTTPS via proxy error, tunnel connection possibly failed'); |
|
2340 | - $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['http_error'], $GLOBALS['xmlrpcstr']['http_error']. ' (HTTPS via proxy error, tunnel connection possibly failed)'); |
|
2341 | - return $r; |
|
2342 | - } |
|
2343 | - } |
|
2344 | - |
|
2345 | - // Strip HTTP 1.1 100 Continue header if present |
|
2346 | - while(preg_match('/^HTTP\/1\.1 1[0-9]{2} /', $data)) |
|
2347 | - { |
|
2348 | - $pos = strpos($data, 'HTTP', 12); |
|
2349 | - // server sent a Continue header without any (valid) content following... |
|
2350 | - // give the client a chance to know it |
|
2351 | - if(!$pos && !is_int($pos)) // works fine in php 3, 4 and 5 |
|
2352 | - { |
|
2353 | - break; |
|
2354 | - } |
|
2355 | - $data = substr($data, $pos); |
|
2356 | - } |
|
2357 | - if(!preg_match('/^HTTP\/[0-9.]+ 200 /', $data)) |
|
2358 | - { |
|
2359 | - $errstr= substr($data, 0, strpos($data, "\n")-1); |
|
2360 | - error_log('XML-RPC: '.__METHOD__.': HTTP error, got response: ' .$errstr); |
|
2361 | - $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['http_error'], $GLOBALS['xmlrpcstr']['http_error']. ' (' . $errstr . ')'); |
|
2362 | - return $r; |
|
2363 | - } |
|
2364 | - |
|
2365 | - $GLOBALS['_xh']['headers'] = array(); |
|
2366 | - $GLOBALS['_xh']['cookies'] = array(); |
|
2367 | - |
|
2368 | - // be tolerant to usage of \n instead of \r\n to separate headers and data |
|
2369 | - // (even though it is not valid http) |
|
2370 | - $pos = strpos($data,"\r\n\r\n"); |
|
2371 | - if($pos || is_int($pos)) |
|
2372 | - { |
|
2373 | - $bd = $pos+4; |
|
2374 | - } |
|
2375 | - else |
|
2376 | - { |
|
2377 | - $pos = strpos($data,"\n\n"); |
|
2378 | - if($pos || is_int($pos)) |
|
2379 | - { |
|
2380 | - $bd = $pos+2; |
|
2381 | - } |
|
2382 | - else |
|
2383 | - { |
|
2384 | - // No separation between response headers and body: fault? |
|
2385 | - // we could take some action here instead of going on... |
|
2386 | - $bd = 0; |
|
2387 | - } |
|
2388 | - } |
|
2389 | - // be tolerant to line endings, and extra empty lines |
|
2390 | - $ar = preg_split("/\r?\n/", trim(substr($data, 0, $pos))); |
|
2391 | - while(list(,$line) = @each($ar)) |
|
2392 | - { |
|
2393 | - // take care of multi-line headers and cookies |
|
2394 | - $arr = explode(':',$line,2); |
|
2395 | - if(count($arr) > 1) |
|
2396 | - { |
|
2397 | - $header_name = strtolower(trim($arr[0])); |
|
2398 | - /// @todo some other headers (the ones that allow a CSV list of values) |
|
2399 | - /// do allow many values to be passed using multiple header lines. |
|
2400 | - /// We should add content to $GLOBALS['_xh']['headers'][$header_name] |
|
2401 | - /// instead of replacing it for those... |
|
2402 | - if ($header_name == 'set-cookie' || $header_name == 'set-cookie2') |
|
2403 | - { |
|
2404 | - if ($header_name == 'set-cookie2') |
|
2405 | - { |
|
2406 | - // version 2 cookies: |
|
2407 | - // there could be many cookies on one line, comma separated |
|
2408 | - $cookies = explode(',', $arr[1]); |
|
2409 | - } |
|
2410 | - else |
|
2411 | - { |
|
2412 | - $cookies = array($arr[1]); |
|
2413 | - } |
|
2414 | - foreach ($cookies as $cookie) |
|
2415 | - { |
|
2416 | - // glue together all received cookies, using a comma to separate them |
|
2417 | - // (same as php does with getallheaders()) |
|
2418 | - if (isset($GLOBALS['_xh']['headers'][$header_name])) |
|
2419 | - $GLOBALS['_xh']['headers'][$header_name] .= ', ' . trim($cookie); |
|
2420 | - else |
|
2421 | - $GLOBALS['_xh']['headers'][$header_name] = trim($cookie); |
|
2422 | - // parse cookie attributes, in case user wants to correctly honour them |
|
2423 | - // feature creep: only allow rfc-compliant cookie attributes? |
|
2424 | - // @todo support for server sending multiple time cookie with same name, but using different PATHs |
|
2425 | - $cookie = explode(';', $cookie); |
|
2426 | - foreach ($cookie as $pos => $val) |
|
2427 | - { |
|
2428 | - $val = explode('=', $val, 2); |
|
2429 | - $tag = trim($val[0]); |
|
2430 | - $val = trim(@$val[1]); |
|
2431 | - /// @todo with version 1 cookies, we should strip leading and trailing " chars |
|
2432 | - if ($pos == 0) |
|
2433 | - { |
|
2434 | - $cookiename = $tag; |
|
2435 | - $GLOBALS['_xh']['cookies'][$tag] = array(); |
|
2436 | - $GLOBALS['_xh']['cookies'][$cookiename]['value'] = urldecode($val); |
|
2437 | - } |
|
2438 | - else |
|
2439 | - { |
|
2440 | - if ($tag != 'value') |
|
2441 | - { |
|
2442 | - $GLOBALS['_xh']['cookies'][$cookiename][$tag] = $val; |
|
2443 | - } |
|
2444 | - } |
|
2445 | - } |
|
2446 | - } |
|
2447 | - } |
|
2448 | - else |
|
2449 | - { |
|
2450 | - $GLOBALS['_xh']['headers'][$header_name] = trim($arr[1]); |
|
2451 | - } |
|
2452 | - } |
|
2453 | - elseif(isset($header_name)) |
|
2454 | - { |
|
2455 | - /// @todo version1 cookies might span multiple lines, thus breaking the parsing above |
|
2456 | - $GLOBALS['_xh']['headers'][$header_name] .= ' ' . trim($line); |
|
2457 | - } |
|
2458 | - } |
|
2459 | - |
|
2460 | - $data = substr($data, $bd); |
|
2461 | - |
|
2462 | - if($this->debug && count($GLOBALS['_xh']['headers'])) |
|
2463 | - { |
|
2464 | - print '<PRE>'; |
|
2465 | - foreach($GLOBALS['_xh']['headers'] as $header => $value) |
|
2466 | - { |
|
2467 | - print htmlentities("HEADER: $header: $value\n"); |
|
2468 | - } |
|
2469 | - foreach($GLOBALS['_xh']['cookies'] as $header => $value) |
|
2470 | - { |
|
2471 | - print htmlentities("COOKIE: $header={$value['value']}\n"); |
|
2472 | - } |
|
2473 | - print "</PRE>\n"; |
|
2474 | - } |
|
2475 | - |
|
2476 | - // if CURL was used for the call, http headers have been processed, |
|
2477 | - // and dechunking + reinflating have been carried out |
|
2478 | - if(!$headers_processed) |
|
2479 | - { |
|
2480 | - // Decode chunked encoding sent by http 1.1 servers |
|
2481 | - if(isset($GLOBALS['_xh']['headers']['transfer-encoding']) && $GLOBALS['_xh']['headers']['transfer-encoding'] == 'chunked') |
|
2482 | - { |
|
2483 | - if(!$data = decode_chunked($data)) |
|
2484 | - { |
|
2485 | - error_log('XML-RPC: '.__METHOD__.': errors occurred when trying to rebuild the chunked data received from server'); |
|
2486 | - $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['dechunk_fail'], $GLOBALS['xmlrpcstr']['dechunk_fail']); |
|
2487 | - return $r; |
|
2488 | - } |
|
2489 | - } |
|
2490 | - |
|
2491 | - // Decode gzip-compressed stuff |
|
2492 | - // code shamelessly inspired from nusoap library by Dietrich Ayala |
|
2493 | - if(isset($GLOBALS['_xh']['headers']['content-encoding'])) |
|
2494 | - { |
|
2495 | - $GLOBALS['_xh']['headers']['content-encoding'] = str_replace('x-', '', $GLOBALS['_xh']['headers']['content-encoding']); |
|
2496 | - if($GLOBALS['_xh']['headers']['content-encoding'] == 'deflate' || $GLOBALS['_xh']['headers']['content-encoding'] == 'gzip') |
|
2497 | - { |
|
2498 | - // if decoding works, use it. else assume data wasn't gzencoded |
|
2499 | - if(function_exists('gzinflate')) |
|
2500 | - { |
|
2501 | - if($GLOBALS['_xh']['headers']['content-encoding'] == 'deflate' && $degzdata = @gzuncompress($data)) |
|
2502 | - { |
|
2503 | - $data = $degzdata; |
|
2504 | - if($this->debug) |
|
2505 | - print "<PRE>---INFLATED RESPONSE---[".strlen($data)." chars]---\n" . htmlentities($data) . "\n---END---</PRE>"; |
|
2506 | - } |
|
2507 | - elseif($GLOBALS['_xh']['headers']['content-encoding'] == 'gzip' && $degzdata = @gzinflate(substr($data, 10))) |
|
2508 | - { |
|
2509 | - $data = $degzdata; |
|
2510 | - if($this->debug) |
|
2511 | - print "<PRE>---INFLATED RESPONSE---[".strlen($data)." chars]---\n" . htmlentities($data) . "\n---END---</PRE>"; |
|
2512 | - } |
|
2513 | - else |
|
2514 | - { |
|
2515 | - error_log('XML-RPC: '.__METHOD__.': errors occurred when trying to decode the deflated data received from server'); |
|
2516 | - $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['decompress_fail'], $GLOBALS['xmlrpcstr']['decompress_fail']); |
|
2517 | - return $r; |
|
2518 | - } |
|
2519 | - } |
|
2520 | - else |
|
2521 | - { |
|
2522 | - error_log('XML-RPC: '.__METHOD__.': the server sent deflated data. Your php install must have the Zlib extension compiled in to support this.'); |
|
2523 | - $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['cannot_decompress'], $GLOBALS['xmlrpcstr']['cannot_decompress']); |
|
2524 | - return $r; |
|
2525 | - } |
|
2526 | - } |
|
2527 | - } |
|
2528 | - } // end of 'if needed, de-chunk, re-inflate response' |
|
2529 | - |
|
2530 | - // real stupid hack to avoid PHP complaining about returning NULL by ref |
|
2531 | - $r = null; |
|
2532 | - $r =& $r; |
|
2533 | - return $r; |
|
2534 | - } |
|
2535 | - |
|
2536 | - /** |
|
2537 | - * Parse the xmlrpc response contained in the string $data and return an xmlrpcresp object. |
|
2538 | - * @param string $data the xmlrpc response, eventually including http headers |
|
2539 | - * @param bool $headers_processed when true prevents parsing HTTP headers for interpretation of content-encoding and consequent decoding |
|
2540 | - * @param string $return_type decides return type, i.e. content of response->value(). Either 'xmlrpcvals', 'xml' or 'phpvals' |
|
2541 | - * @return xmlrpcresp |
|
2542 | - * @access public |
|
2543 | - */ |
|
2544 | - function &parseResponse($data='', $headers_processed=false, $return_type='xmlrpcvals') |
|
2545 | - { |
|
2546 | - if($this->debug) |
|
2547 | - { |
|
2548 | - //by maHo, replaced htmlspecialchars with htmlentities |
|
2549 | - print "<PRE>---GOT---\n" . htmlentities($data) . "\n---END---\n</PRE>"; |
|
2550 | - } |
|
2551 | - |
|
2552 | - if($data == '') |
|
2553 | - { |
|
2554 | - error_log('XML-RPC: '.__METHOD__.': no response received from server.'); |
|
2555 | - $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['no_data'], $GLOBALS['xmlrpcstr']['no_data']); |
|
2556 | - return $r; |
|
2557 | - } |
|
2558 | - |
|
2559 | - $GLOBALS['_xh']=array(); |
|
2560 | - |
|
2561 | - $raw_data = $data; |
|
2562 | - // parse the HTTP headers of the response, if present, and separate them from data |
|
2563 | - if(substr($data, 0, 4) == 'HTTP') |
|
2564 | - { |
|
2565 | - $r =& $this->parseResponseHeaders($data, $headers_processed); |
|
2566 | - if ($r) |
|
2567 | - { |
|
2568 | - // failed processing of HTTP response headers |
|
2569 | - // save into response obj the full payload received, for debugging |
|
2570 | - $r->raw_data = $data; |
|
2571 | - return $r; |
|
2572 | - } |
|
2573 | - } |
|
2574 | - else |
|
2575 | - { |
|
2576 | - $GLOBALS['_xh']['headers'] = array(); |
|
2577 | - $GLOBALS['_xh']['cookies'] = array(); |
|
2578 | - } |
|
2579 | - |
|
2580 | - if($this->debug) |
|
2581 | - { |
|
2582 | - $start = strpos($data, '<!-- SERVER DEBUG INFO (BASE64 ENCODED):'); |
|
2583 | - if ($start) |
|
2584 | - { |
|
2585 | - $start += strlen('<!-- SERVER DEBUG INFO (BASE64 ENCODED):'); |
|
2586 | - $end = strpos($data, '-->', $start); |
|
2587 | - $comments = substr($data, $start, $end-$start); |
|
2588 | - print "<PRE>---SERVER DEBUG INFO (DECODED) ---\n\t".htmlentities(str_replace("\n", "\n\t", base64_decode($comments)))."\n---END---\n</PRE>"; |
|
2589 | - } |
|
2590 | - } |
|
2591 | - |
|
2592 | - // be tolerant of extra whitespace in response body |
|
2593 | - $data = trim($data); |
|
2594 | - |
|
2595 | - /// @todo return an error msg if $data=='' ? |
|
2596 | - |
|
2597 | - // be tolerant of junk after methodResponse (e.g. javascript ads automatically inserted by free hosts) |
|
2598 | - // idea from Luca Mariano <[email protected]> originally in PEARified version of the lib |
|
2599 | - $pos = strrpos($data, '</methodResponse>'); |
|
2600 | - if($pos !== false) |
|
2601 | - { |
|
2602 | - $data = substr($data, 0, $pos+17); |
|
2603 | - } |
|
2604 | - |
|
2605 | - // if user wants back raw xml, give it to him |
|
2606 | - if ($return_type == 'xml') |
|
2607 | - { |
|
2608 | - $r = new xmlrpcresp($data, 0, '', 'xml'); |
|
2609 | - $r->hdrs = $GLOBALS['_xh']['headers']; |
|
2610 | - $r->_cookies = $GLOBALS['_xh']['cookies']; |
|
2611 | - $r->raw_data = $raw_data; |
|
2612 | - return $r; |
|
2613 | - } |
|
2614 | - |
|
2615 | - // try to 'guestimate' the character encoding of the received response |
|
2616 | - $resp_encoding = guess_encoding(@$GLOBALS['_xh']['headers']['content-type'], $data); |
|
2617 | - |
|
2618 | - $GLOBALS['_xh']['ac']=''; |
|
2619 | - //$GLOBALS['_xh']['qt']=''; //unused... |
|
2620 | - $GLOBALS['_xh']['stack'] = array(); |
|
2621 | - $GLOBALS['_xh']['valuestack'] = array(); |
|
2622 | - $GLOBALS['_xh']['isf']=0; // 0 = OK, 1 for xmlrpc fault responses, 2 = invalid xmlrpc |
|
2623 | - $GLOBALS['_xh']['isf_reason']=''; |
|
2624 | - $GLOBALS['_xh']['rt']=''; // 'methodcall or 'methodresponse' |
|
2625 | - |
|
2626 | - // Since parsing will fail if charset is not specified in the xml prologue, |
|
2627 | - // the encoding is not UTF8 and there are non-ascii chars in the text, we try to work round that... |
|
2628 | - // The following code might be better for mb_string enabled installs, but |
|
2629 | - // makes the lib about 200% slower... |
|
2630 | - //if (!is_valid_charset($resp_encoding, array('UTF-8'))) |
|
2631 | - if (!in_array($resp_encoding, array('UTF-8', 'US-ASCII')) && !has_encoding($data)) { |
|
2632 | - if ($resp_encoding == 'ISO-8859-1') { |
|
2633 | - $data = utf8_encode($data); |
|
2634 | - } else { |
|
2635 | - if (extension_loaded('mbstring')) { |
|
2636 | - $data = mb_convert_encoding($data, 'UTF-8', $resp_encoding); |
|
2637 | - } else { |
|
2638 | - error_log('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of received request: ' . $resp_encoding); |
|
2639 | - } |
|
2640 | - } |
|
2641 | - } |
|
2642 | - |
|
2643 | - $parser = xml_parser_create(); |
|
2644 | - xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true); |
|
2645 | - // G. Giunta 2005/02/13: PHP internally uses ISO-8859-1, so we have to tell |
|
2646 | - // the xml parser to give us back data in the expected charset. |
|
2647 | - // What if internal encoding is not in one of the 3 allowed? |
|
2648 | - // we use the broadest one, ie. utf8 |
|
2649 | - // This allows to send data which is native in various charset, |
|
2650 | - // by extending xmlrpc_encode_entitites() and setting xmlrpc_internalencoding |
|
2651 | - if (!in_array($GLOBALS['xmlrpc_internalencoding'], array('UTF-8', 'ISO-8859-1', 'US-ASCII'))) |
|
2652 | - { |
|
2653 | - xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8'); |
|
2654 | - } |
|
2655 | - else |
|
2656 | - { |
|
2657 | - xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $GLOBALS['xmlrpc_internalencoding']); |
|
2658 | - } |
|
2659 | - |
|
2660 | - if ($return_type == 'phpvals') |
|
2661 | - { |
|
2662 | - xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee_fast'); |
|
2663 | - } |
|
2664 | - else |
|
2665 | - { |
|
2666 | - xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee'); |
|
2667 | - } |
|
2668 | - |
|
2669 | - xml_set_character_data_handler($parser, 'xmlrpc_cd'); |
|
2670 | - xml_set_default_handler($parser, 'xmlrpc_dh'); |
|
2671 | - |
|
2672 | - // first error check: xml not well formed |
|
2673 | - if(!xml_parse($parser, $data, count($data))) |
|
2674 | - { |
|
2675 | - // thanks to Peter Kocks <[email protected]> |
|
2676 | - if((xml_get_current_line_number($parser)) == 1) |
|
2677 | - { |
|
2678 | - $errstr = 'XML error at line 1, check URL'; |
|
2679 | - } |
|
2680 | - else |
|
2681 | - { |
|
2682 | - $errstr = sprintf('XML error: %s at line %d, column %d', |
|
2683 | - xml_error_string(xml_get_error_code($parser)), |
|
2684 | - xml_get_current_line_number($parser), xml_get_current_column_number($parser)); |
|
2685 | - } |
|
2686 | - error_log($errstr); |
|
2687 | - $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['invalid_return'], $GLOBALS['xmlrpcstr']['invalid_return'].' ('.$errstr.')'); |
|
2688 | - xml_parser_free($parser); |
|
2689 | - if($this->debug) |
|
2690 | - { |
|
2691 | - print $errstr; |
|
2692 | - } |
|
2693 | - $r->hdrs = $GLOBALS['_xh']['headers']; |
|
2694 | - $r->_cookies = $GLOBALS['_xh']['cookies']; |
|
2695 | - $r->raw_data = $raw_data; |
|
2696 | - return $r; |
|
2697 | - } |
|
2698 | - xml_parser_free($parser); |
|
2699 | - // second error check: xml well formed but not xml-rpc compliant |
|
2700 | - if ($GLOBALS['_xh']['isf'] > 1) |
|
2701 | - { |
|
2702 | - if ($this->debug) |
|
2703 | - { |
|
2704 | - /// @todo echo something for user? |
|
2705 | - } |
|
2706 | - |
|
2707 | - $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['invalid_return'], |
|
2708 | - $GLOBALS['xmlrpcstr']['invalid_return'] . ' ' . $GLOBALS['_xh']['isf_reason']); |
|
2709 | - } |
|
2710 | - // third error check: parsing of the response has somehow gone boink. |
|
2711 | - // NB: shall we omit this check, since we trust the parsing code? |
|
2712 | - elseif ($return_type == 'xmlrpcvals' && !is_object($GLOBALS['_xh']['value'])) |
|
2713 | - { |
|
2714 | - // something odd has happened |
|
2715 | - // and it's time to generate a client side error |
|
2716 | - // indicating something odd went on |
|
2717 | - $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['invalid_return'], |
|
2718 | - $GLOBALS['xmlrpcstr']['invalid_return']); |
|
2719 | - } |
|
2720 | - else |
|
2721 | - { |
|
2722 | - if ($this->debug) |
|
2723 | - { |
|
2724 | - print "<PRE>---PARSED---\n"; |
|
2725 | - // somehow htmlentities chokes on var_export, and some full html string... |
|
2726 | - //print htmlentitites(var_export($GLOBALS['_xh']['value'], true)); |
|
2727 | - print htmlspecialchars(var_export($GLOBALS['_xh']['value'], true)); |
|
2728 | - print "\n---END---</PRE>"; |
|
2729 | - } |
|
2730 | - |
|
2731 | - // note that using =& will raise an error if $GLOBALS['_xh']['st'] does not generate an object. |
|
2732 | - $v =& $GLOBALS['_xh']['value']; |
|
2733 | - |
|
2734 | - if($GLOBALS['_xh']['isf']) |
|
2735 | - { |
|
2736 | - /// @todo we should test here if server sent an int and a string, |
|
2737 | - /// and/or coerce them into such... |
|
2738 | - if ($return_type == 'xmlrpcvals') |
|
2739 | - { |
|
2740 | - $errno_v = $v->structmem('faultCode'); |
|
2741 | - $errstr_v = $v->structmem('faultString'); |
|
2742 | - $errno = $errno_v->scalarval(); |
|
2743 | - $errstr = $errstr_v->scalarval(); |
|
2744 | - } |
|
2745 | - else |
|
2746 | - { |
|
2747 | - $errno = $v['faultCode']; |
|
2748 | - $errstr = $v['faultString']; |
|
2749 | - } |
|
2750 | - |
|
2751 | - if($errno == 0) |
|
2752 | - { |
|
2753 | - // FAULT returned, errno needs to reflect that |
|
2754 | - $errno = -1; |
|
2755 | - } |
|
2756 | - |
|
2757 | - $r = new xmlrpcresp(0, $errno, $errstr); |
|
2758 | - } |
|
2759 | - else |
|
2760 | - { |
|
2761 | - $r=new xmlrpcresp($v, 0, '', $return_type); |
|
2762 | - } |
|
2763 | - } |
|
2764 | - |
|
2765 | - $r->hdrs = $GLOBALS['_xh']['headers']; |
|
2766 | - $r->_cookies = $GLOBALS['_xh']['cookies']; |
|
2767 | - $r->raw_data = $raw_data; |
|
2768 | - return $r; |
|
2769 | - } |
|
2770 | - } |
|
2771 | - |
|
2772 | - class xmlrpcval |
|
2773 | - { |
|
2774 | - var $me=array(); |
|
2775 | - var $mytype=0; |
|
2776 | - var $_php_class=null; |
|
2777 | - |
|
2778 | - /** |
|
2779 | - * @param mixed $val |
|
2780 | - * @param string $type any valid xmlrpc type name (lowercase). If null, 'string' is assumed |
|
2781 | - */ |
|
2782 | - function __construct($val=-1, $type='') |
|
2783 | - { |
|
2784 | - /// @todo: optimization creep - do not call addXX, do it all inline. |
|
2785 | - /// downside: booleans will not be coerced anymore |
|
2786 | - if($val!==-1 || $type!='') |
|
2787 | - { |
|
2788 | - // optimization creep: inlined all work done by constructor |
|
2789 | - switch($type) |
|
2790 | - { |
|
2791 | - case '': |
|
2792 | - $this->mytype=1; |
|
2793 | - $this->me['string']=$val; |
|
2794 | - break; |
|
2795 | - case 'i4': |
|
2796 | - case 'int': |
|
2797 | - case 'double': |
|
2798 | - case 'string': |
|
2799 | - case 'boolean': |
|
2800 | - case 'dateTime.iso8601': |
|
2801 | - case 'base64': |
|
2802 | - case 'null': |
|
2803 | - $this->mytype=1; |
|
2804 | - $this->me[$type]=$val; |
|
2805 | - break; |
|
2806 | - case 'array': |
|
2807 | - $this->mytype=2; |
|
2808 | - $this->me['array']=$val; |
|
2809 | - break; |
|
2810 | - case 'struct': |
|
2811 | - $this->mytype=3; |
|
2812 | - $this->me['struct']=$val; |
|
2813 | - break; |
|
2814 | - default: |
|
2815 | - error_log("XML-RPC: ".__METHOD__.": not a known type ($type)"); |
|
2816 | - } |
|
2817 | - /*if($type=='') |
|
2100 | + } |
|
2101 | + else |
|
2102 | + { |
|
2103 | + if(!is_object($this->val) || !is_a($this->val, 'xmlrpcval')) |
|
2104 | + { |
|
2105 | + if (is_string($this->val) && $this->valtyp == 'xml') |
|
2106 | + { |
|
2107 | + $result .= "<params>\n<param>\n" . |
|
2108 | + $this->val . |
|
2109 | + "</param>\n</params>"; |
|
2110 | + } |
|
2111 | + else |
|
2112 | + { |
|
2113 | + /// @todo try to build something serializable? |
|
2114 | + die('cannot serialize xmlrpcresp objects whose content is native php values'); |
|
2115 | + } |
|
2116 | + } |
|
2117 | + else |
|
2118 | + { |
|
2119 | + $result .= "<params>\n<param>\n" . |
|
2120 | + $this->val->serialize($charset_encoding) . |
|
2121 | + "</param>\n</params>"; |
|
2122 | + } |
|
2123 | + } |
|
2124 | + $result .= "\n</methodResponse>"; |
|
2125 | + $this->payload = $result; |
|
2126 | + return $result; |
|
2127 | + } |
|
2128 | + } |
|
2129 | + |
|
2130 | + class xmlrpcmsg |
|
2131 | + { |
|
2132 | + var $payload; |
|
2133 | + var $methodname; |
|
2134 | + var $params=array(); |
|
2135 | + var $debug=0; |
|
2136 | + var $content_type = 'text/xml'; |
|
2137 | + |
|
2138 | + /** |
|
2139 | + * @param string $meth the name of the method to invoke |
|
2140 | + * @param array $pars array of parameters to be passed to the method (xmlrpcval objects) |
|
2141 | + */ |
|
2142 | + function __construct($meth, $pars=0) |
|
2143 | + { |
|
2144 | + $this->methodname=$meth; |
|
2145 | + if(is_array($pars) && count($pars)>0) |
|
2146 | + { |
|
2147 | + for($i=0; $i<count($pars); $i++) |
|
2148 | + { |
|
2149 | + $this->addParam($pars[$i]); |
|
2150 | + } |
|
2151 | + } |
|
2152 | + } |
|
2153 | + |
|
2154 | + /** |
|
2155 | + * @deprecated |
|
2156 | + */ |
|
2157 | + function xmlrpcmsg($meth, $pars=0) |
|
2158 | + { |
|
2159 | + self::__construct($meth, $pars); |
|
2160 | + } |
|
2161 | + |
|
2162 | + /** |
|
2163 | + * @access private |
|
2164 | + */ |
|
2165 | + function xml_header($charset_encoding='') |
|
2166 | + { |
|
2167 | + if ($charset_encoding != '') |
|
2168 | + { |
|
2169 | + return "<?xml version=\"1.0\" encoding=\"$charset_encoding\" ?" . ">\n<methodCall>\n"; |
|
2170 | + } |
|
2171 | + else |
|
2172 | + { |
|
2173 | + return "<?xml version=\"1.0\"?" . ">\n<methodCall>\n"; |
|
2174 | + } |
|
2175 | + } |
|
2176 | + |
|
2177 | + /** |
|
2178 | + * @access private |
|
2179 | + */ |
|
2180 | + function xml_footer() |
|
2181 | + { |
|
2182 | + return '</methodCall>'; |
|
2183 | + } |
|
2184 | + |
|
2185 | + /** |
|
2186 | + * @access private |
|
2187 | + */ |
|
2188 | + function kindOf() |
|
2189 | + { |
|
2190 | + return 'msg'; |
|
2191 | + } |
|
2192 | + |
|
2193 | + /** |
|
2194 | + * @access private |
|
2195 | + */ |
|
2196 | + function createPayload($charset_encoding='') |
|
2197 | + { |
|
2198 | + if ($charset_encoding != '') |
|
2199 | + $this->content_type = 'text/xml; charset=' . $charset_encoding; |
|
2200 | + else |
|
2201 | + $this->content_type = 'text/xml'; |
|
2202 | + $this->payload=$this->xml_header($charset_encoding); |
|
2203 | + $this->payload.='<methodName>' . xmlrpc_encode_entitites($this->methodname, $GLOBALS['xmlrpc_internalencoding'], $charset_encoding) . "</methodName>\n"; |
|
2204 | + $this->payload.="<params>\n"; |
|
2205 | + for($i=0; $i<count($this->params); $i++) |
|
2206 | + { |
|
2207 | + $p=$this->params[$i]; |
|
2208 | + $this->payload.="<param>\n" . $p->serialize($charset_encoding) . |
|
2209 | + "</param>\n"; |
|
2210 | + } |
|
2211 | + $this->payload.="</params>\n"; |
|
2212 | + $this->payload.=$this->xml_footer(); |
|
2213 | + } |
|
2214 | + |
|
2215 | + /** |
|
2216 | + * Gets/sets the xmlrpc method to be invoked |
|
2217 | + * @param string $meth the method to be set (leave empty not to set it) |
|
2218 | + * @return string the method that will be invoked |
|
2219 | + * @access public |
|
2220 | + */ |
|
2221 | + function method($meth='') |
|
2222 | + { |
|
2223 | + if($meth!='') |
|
2224 | + { |
|
2225 | + $this->methodname=$meth; |
|
2226 | + } |
|
2227 | + return $this->methodname; |
|
2228 | + } |
|
2229 | + |
|
2230 | + /** |
|
2231 | + * Returns xml representation of the message. XML prologue included |
|
2232 | + * @param string $charset_encoding |
|
2233 | + * @return string the xml representation of the message, xml prologue included |
|
2234 | + * @access public |
|
2235 | + */ |
|
2236 | + function serialize($charset_encoding='') |
|
2237 | + { |
|
2238 | + $this->createPayload($charset_encoding); |
|
2239 | + return $this->payload; |
|
2240 | + } |
|
2241 | + |
|
2242 | + /** |
|
2243 | + * Add a parameter to the list of parameters to be used upon method invocation |
|
2244 | + * @param xmlrpcval $par |
|
2245 | + * @return boolean false on failure |
|
2246 | + * @access public |
|
2247 | + */ |
|
2248 | + function addParam($par) |
|
2249 | + { |
|
2250 | + // add check: do not add to self params which are not xmlrpcvals |
|
2251 | + if(is_object($par) && is_a($par, 'xmlrpcval')) |
|
2252 | + { |
|
2253 | + $this->params[]=$par; |
|
2254 | + return true; |
|
2255 | + } |
|
2256 | + else |
|
2257 | + { |
|
2258 | + return false; |
|
2259 | + } |
|
2260 | + } |
|
2261 | + |
|
2262 | + /** |
|
2263 | + * Returns the nth parameter in the message. The index zero-based. |
|
2264 | + * @param integer $i the index of the parameter to fetch (zero based) |
|
2265 | + * @return xmlrpcval the i-th parameter |
|
2266 | + * @access public |
|
2267 | + */ |
|
2268 | + function getParam($i) { return $this->params[$i]; } |
|
2269 | + |
|
2270 | + /** |
|
2271 | + * Returns the number of parameters in the messge. |
|
2272 | + * @return integer the number of parameters currently set |
|
2273 | + * @access public |
|
2274 | + */ |
|
2275 | + function getNumParams() { return count($this->params); } |
|
2276 | + |
|
2277 | + /** |
|
2278 | + * Given an open file handle, read all data available and parse it as axmlrpc response. |
|
2279 | + * NB: the file handle is not closed by this function. |
|
2280 | + * NNB: might have trouble in rare cases to work on network streams, as we |
|
2281 | + * check for a read of 0 bytes instead of feof($fp). |
|
2282 | + * But since checking for feof(null) returns false, we would risk an |
|
2283 | + * infinite loop in that case, because we cannot trust the caller |
|
2284 | + * to give us a valid pointer to an open file... |
|
2285 | + * @access public |
|
2286 | + * @param resource $fp stream pointer |
|
2287 | + * @return xmlrpcresp |
|
2288 | + * @todo add 2nd & 3rd param to be passed to ParseResponse() ??? |
|
2289 | + */ |
|
2290 | + function &parseResponseFile($fp) |
|
2291 | + { |
|
2292 | + $ipd=''; |
|
2293 | + while($data=fread($fp, 32768)) |
|
2294 | + { |
|
2295 | + $ipd.=$data; |
|
2296 | + } |
|
2297 | + //fclose($fp); |
|
2298 | + $r =& $this->parseResponse($ipd); |
|
2299 | + return $r; |
|
2300 | + } |
|
2301 | + |
|
2302 | + /** |
|
2303 | + * Parses HTTP headers and separates them from data. |
|
2304 | + * @access private |
|
2305 | + */ |
|
2306 | + function &parseResponseHeaders(&$data, $headers_processed=false) |
|
2307 | + { |
|
2308 | + // Support "web-proxy-tunelling" connections for https through proxies |
|
2309 | + if(preg_match('/^HTTP\/1\.[0-1] 200 Connection established/', $data)) |
|
2310 | + { |
|
2311 | + // Look for CR/LF or simple LF as line separator, |
|
2312 | + // (even though it is not valid http) |
|
2313 | + $pos = strpos($data,"\r\n\r\n"); |
|
2314 | + if($pos || is_int($pos)) |
|
2315 | + { |
|
2316 | + $bd = $pos+4; |
|
2317 | + } |
|
2318 | + else |
|
2319 | + { |
|
2320 | + $pos = strpos($data,"\n\n"); |
|
2321 | + if($pos || is_int($pos)) |
|
2322 | + { |
|
2323 | + $bd = $pos+2; |
|
2324 | + } |
|
2325 | + else |
|
2326 | + { |
|
2327 | + // No separation between response headers and body: fault? |
|
2328 | + $bd = 0; |
|
2329 | + } |
|
2330 | + } |
|
2331 | + if ($bd) |
|
2332 | + { |
|
2333 | + // this filters out all http headers from proxy. |
|
2334 | + // maybe we could take them into account, too? |
|
2335 | + $data = substr($data, $bd); |
|
2336 | + } |
|
2337 | + else |
|
2338 | + { |
|
2339 | + error_log('XML-RPC: '.__METHOD__.': HTTPS via proxy error, tunnel connection possibly failed'); |
|
2340 | + $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['http_error'], $GLOBALS['xmlrpcstr']['http_error']. ' (HTTPS via proxy error, tunnel connection possibly failed)'); |
|
2341 | + return $r; |
|
2342 | + } |
|
2343 | + } |
|
2344 | + |
|
2345 | + // Strip HTTP 1.1 100 Continue header if present |
|
2346 | + while(preg_match('/^HTTP\/1\.1 1[0-9]{2} /', $data)) |
|
2347 | + { |
|
2348 | + $pos = strpos($data, 'HTTP', 12); |
|
2349 | + // server sent a Continue header without any (valid) content following... |
|
2350 | + // give the client a chance to know it |
|
2351 | + if(!$pos && !is_int($pos)) // works fine in php 3, 4 and 5 |
|
2352 | + { |
|
2353 | + break; |
|
2354 | + } |
|
2355 | + $data = substr($data, $pos); |
|
2356 | + } |
|
2357 | + if(!preg_match('/^HTTP\/[0-9.]+ 200 /', $data)) |
|
2358 | + { |
|
2359 | + $errstr= substr($data, 0, strpos($data, "\n")-1); |
|
2360 | + error_log('XML-RPC: '.__METHOD__.': HTTP error, got response: ' .$errstr); |
|
2361 | + $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['http_error'], $GLOBALS['xmlrpcstr']['http_error']. ' (' . $errstr . ')'); |
|
2362 | + return $r; |
|
2363 | + } |
|
2364 | + |
|
2365 | + $GLOBALS['_xh']['headers'] = array(); |
|
2366 | + $GLOBALS['_xh']['cookies'] = array(); |
|
2367 | + |
|
2368 | + // be tolerant to usage of \n instead of \r\n to separate headers and data |
|
2369 | + // (even though it is not valid http) |
|
2370 | + $pos = strpos($data,"\r\n\r\n"); |
|
2371 | + if($pos || is_int($pos)) |
|
2372 | + { |
|
2373 | + $bd = $pos+4; |
|
2374 | + } |
|
2375 | + else |
|
2376 | + { |
|
2377 | + $pos = strpos($data,"\n\n"); |
|
2378 | + if($pos || is_int($pos)) |
|
2379 | + { |
|
2380 | + $bd = $pos+2; |
|
2381 | + } |
|
2382 | + else |
|
2383 | + { |
|
2384 | + // No separation between response headers and body: fault? |
|
2385 | + // we could take some action here instead of going on... |
|
2386 | + $bd = 0; |
|
2387 | + } |
|
2388 | + } |
|
2389 | + // be tolerant to line endings, and extra empty lines |
|
2390 | + $ar = preg_split("/\r?\n/", trim(substr($data, 0, $pos))); |
|
2391 | + while(list(,$line) = @each($ar)) |
|
2392 | + { |
|
2393 | + // take care of multi-line headers and cookies |
|
2394 | + $arr = explode(':',$line,2); |
|
2395 | + if(count($arr) > 1) |
|
2396 | + { |
|
2397 | + $header_name = strtolower(trim($arr[0])); |
|
2398 | + /// @todo some other headers (the ones that allow a CSV list of values) |
|
2399 | + /// do allow many values to be passed using multiple header lines. |
|
2400 | + /// We should add content to $GLOBALS['_xh']['headers'][$header_name] |
|
2401 | + /// instead of replacing it for those... |
|
2402 | + if ($header_name == 'set-cookie' || $header_name == 'set-cookie2') |
|
2403 | + { |
|
2404 | + if ($header_name == 'set-cookie2') |
|
2405 | + { |
|
2406 | + // version 2 cookies: |
|
2407 | + // there could be many cookies on one line, comma separated |
|
2408 | + $cookies = explode(',', $arr[1]); |
|
2409 | + } |
|
2410 | + else |
|
2411 | + { |
|
2412 | + $cookies = array($arr[1]); |
|
2413 | + } |
|
2414 | + foreach ($cookies as $cookie) |
|
2415 | + { |
|
2416 | + // glue together all received cookies, using a comma to separate them |
|
2417 | + // (same as php does with getallheaders()) |
|
2418 | + if (isset($GLOBALS['_xh']['headers'][$header_name])) |
|
2419 | + $GLOBALS['_xh']['headers'][$header_name] .= ', ' . trim($cookie); |
|
2420 | + else |
|
2421 | + $GLOBALS['_xh']['headers'][$header_name] = trim($cookie); |
|
2422 | + // parse cookie attributes, in case user wants to correctly honour them |
|
2423 | + // feature creep: only allow rfc-compliant cookie attributes? |
|
2424 | + // @todo support for server sending multiple time cookie with same name, but using different PATHs |
|
2425 | + $cookie = explode(';', $cookie); |
|
2426 | + foreach ($cookie as $pos => $val) |
|
2427 | + { |
|
2428 | + $val = explode('=', $val, 2); |
|
2429 | + $tag = trim($val[0]); |
|
2430 | + $val = trim(@$val[1]); |
|
2431 | + /// @todo with version 1 cookies, we should strip leading and trailing " chars |
|
2432 | + if ($pos == 0) |
|
2433 | + { |
|
2434 | + $cookiename = $tag; |
|
2435 | + $GLOBALS['_xh']['cookies'][$tag] = array(); |
|
2436 | + $GLOBALS['_xh']['cookies'][$cookiename]['value'] = urldecode($val); |
|
2437 | + } |
|
2438 | + else |
|
2439 | + { |
|
2440 | + if ($tag != 'value') |
|
2441 | + { |
|
2442 | + $GLOBALS['_xh']['cookies'][$cookiename][$tag] = $val; |
|
2443 | + } |
|
2444 | + } |
|
2445 | + } |
|
2446 | + } |
|
2447 | + } |
|
2448 | + else |
|
2449 | + { |
|
2450 | + $GLOBALS['_xh']['headers'][$header_name] = trim($arr[1]); |
|
2451 | + } |
|
2452 | + } |
|
2453 | + elseif(isset($header_name)) |
|
2454 | + { |
|
2455 | + /// @todo version1 cookies might span multiple lines, thus breaking the parsing above |
|
2456 | + $GLOBALS['_xh']['headers'][$header_name] .= ' ' . trim($line); |
|
2457 | + } |
|
2458 | + } |
|
2459 | + |
|
2460 | + $data = substr($data, $bd); |
|
2461 | + |
|
2462 | + if($this->debug && count($GLOBALS['_xh']['headers'])) |
|
2463 | + { |
|
2464 | + print '<PRE>'; |
|
2465 | + foreach($GLOBALS['_xh']['headers'] as $header => $value) |
|
2466 | + { |
|
2467 | + print htmlentities("HEADER: $header: $value\n"); |
|
2468 | + } |
|
2469 | + foreach($GLOBALS['_xh']['cookies'] as $header => $value) |
|
2470 | + { |
|
2471 | + print htmlentities("COOKIE: $header={$value['value']}\n"); |
|
2472 | + } |
|
2473 | + print "</PRE>\n"; |
|
2474 | + } |
|
2475 | + |
|
2476 | + // if CURL was used for the call, http headers have been processed, |
|
2477 | + // and dechunking + reinflating have been carried out |
|
2478 | + if(!$headers_processed) |
|
2479 | + { |
|
2480 | + // Decode chunked encoding sent by http 1.1 servers |
|
2481 | + if(isset($GLOBALS['_xh']['headers']['transfer-encoding']) && $GLOBALS['_xh']['headers']['transfer-encoding'] == 'chunked') |
|
2482 | + { |
|
2483 | + if(!$data = decode_chunked($data)) |
|
2484 | + { |
|
2485 | + error_log('XML-RPC: '.__METHOD__.': errors occurred when trying to rebuild the chunked data received from server'); |
|
2486 | + $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['dechunk_fail'], $GLOBALS['xmlrpcstr']['dechunk_fail']); |
|
2487 | + return $r; |
|
2488 | + } |
|
2489 | + } |
|
2490 | + |
|
2491 | + // Decode gzip-compressed stuff |
|
2492 | + // code shamelessly inspired from nusoap library by Dietrich Ayala |
|
2493 | + if(isset($GLOBALS['_xh']['headers']['content-encoding'])) |
|
2494 | + { |
|
2495 | + $GLOBALS['_xh']['headers']['content-encoding'] = str_replace('x-', '', $GLOBALS['_xh']['headers']['content-encoding']); |
|
2496 | + if($GLOBALS['_xh']['headers']['content-encoding'] == 'deflate' || $GLOBALS['_xh']['headers']['content-encoding'] == 'gzip') |
|
2497 | + { |
|
2498 | + // if decoding works, use it. else assume data wasn't gzencoded |
|
2499 | + if(function_exists('gzinflate')) |
|
2500 | + { |
|
2501 | + if($GLOBALS['_xh']['headers']['content-encoding'] == 'deflate' && $degzdata = @gzuncompress($data)) |
|
2502 | + { |
|
2503 | + $data = $degzdata; |
|
2504 | + if($this->debug) |
|
2505 | + print "<PRE>---INFLATED RESPONSE---[".strlen($data)." chars]---\n" . htmlentities($data) . "\n---END---</PRE>"; |
|
2506 | + } |
|
2507 | + elseif($GLOBALS['_xh']['headers']['content-encoding'] == 'gzip' && $degzdata = @gzinflate(substr($data, 10))) |
|
2508 | + { |
|
2509 | + $data = $degzdata; |
|
2510 | + if($this->debug) |
|
2511 | + print "<PRE>---INFLATED RESPONSE---[".strlen($data)." chars]---\n" . htmlentities($data) . "\n---END---</PRE>"; |
|
2512 | + } |
|
2513 | + else |
|
2514 | + { |
|
2515 | + error_log('XML-RPC: '.__METHOD__.': errors occurred when trying to decode the deflated data received from server'); |
|
2516 | + $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['decompress_fail'], $GLOBALS['xmlrpcstr']['decompress_fail']); |
|
2517 | + return $r; |
|
2518 | + } |
|
2519 | + } |
|
2520 | + else |
|
2521 | + { |
|
2522 | + error_log('XML-RPC: '.__METHOD__.': the server sent deflated data. Your php install must have the Zlib extension compiled in to support this.'); |
|
2523 | + $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['cannot_decompress'], $GLOBALS['xmlrpcstr']['cannot_decompress']); |
|
2524 | + return $r; |
|
2525 | + } |
|
2526 | + } |
|
2527 | + } |
|
2528 | + } // end of 'if needed, de-chunk, re-inflate response' |
|
2529 | + |
|
2530 | + // real stupid hack to avoid PHP complaining about returning NULL by ref |
|
2531 | + $r = null; |
|
2532 | + $r =& $r; |
|
2533 | + return $r; |
|
2534 | + } |
|
2535 | + |
|
2536 | + /** |
|
2537 | + * Parse the xmlrpc response contained in the string $data and return an xmlrpcresp object. |
|
2538 | + * @param string $data the xmlrpc response, eventually including http headers |
|
2539 | + * @param bool $headers_processed when true prevents parsing HTTP headers for interpretation of content-encoding and consequent decoding |
|
2540 | + * @param string $return_type decides return type, i.e. content of response->value(). Either 'xmlrpcvals', 'xml' or 'phpvals' |
|
2541 | + * @return xmlrpcresp |
|
2542 | + * @access public |
|
2543 | + */ |
|
2544 | + function &parseResponse($data='', $headers_processed=false, $return_type='xmlrpcvals') |
|
2545 | + { |
|
2546 | + if($this->debug) |
|
2547 | + { |
|
2548 | + //by maHo, replaced htmlspecialchars with htmlentities |
|
2549 | + print "<PRE>---GOT---\n" . htmlentities($data) . "\n---END---\n</PRE>"; |
|
2550 | + } |
|
2551 | + |
|
2552 | + if($data == '') |
|
2553 | + { |
|
2554 | + error_log('XML-RPC: '.__METHOD__.': no response received from server.'); |
|
2555 | + $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['no_data'], $GLOBALS['xmlrpcstr']['no_data']); |
|
2556 | + return $r; |
|
2557 | + } |
|
2558 | + |
|
2559 | + $GLOBALS['_xh']=array(); |
|
2560 | + |
|
2561 | + $raw_data = $data; |
|
2562 | + // parse the HTTP headers of the response, if present, and separate them from data |
|
2563 | + if(substr($data, 0, 4) == 'HTTP') |
|
2564 | + { |
|
2565 | + $r =& $this->parseResponseHeaders($data, $headers_processed); |
|
2566 | + if ($r) |
|
2567 | + { |
|
2568 | + // failed processing of HTTP response headers |
|
2569 | + // save into response obj the full payload received, for debugging |
|
2570 | + $r->raw_data = $data; |
|
2571 | + return $r; |
|
2572 | + } |
|
2573 | + } |
|
2574 | + else |
|
2575 | + { |
|
2576 | + $GLOBALS['_xh']['headers'] = array(); |
|
2577 | + $GLOBALS['_xh']['cookies'] = array(); |
|
2578 | + } |
|
2579 | + |
|
2580 | + if($this->debug) |
|
2581 | + { |
|
2582 | + $start = strpos($data, '<!-- SERVER DEBUG INFO (BASE64 ENCODED):'); |
|
2583 | + if ($start) |
|
2584 | + { |
|
2585 | + $start += strlen('<!-- SERVER DEBUG INFO (BASE64 ENCODED):'); |
|
2586 | + $end = strpos($data, '-->', $start); |
|
2587 | + $comments = substr($data, $start, $end-$start); |
|
2588 | + print "<PRE>---SERVER DEBUG INFO (DECODED) ---\n\t".htmlentities(str_replace("\n", "\n\t", base64_decode($comments)))."\n---END---\n</PRE>"; |
|
2589 | + } |
|
2590 | + } |
|
2591 | + |
|
2592 | + // be tolerant of extra whitespace in response body |
|
2593 | + $data = trim($data); |
|
2594 | + |
|
2595 | + /// @todo return an error msg if $data=='' ? |
|
2596 | + |
|
2597 | + // be tolerant of junk after methodResponse (e.g. javascript ads automatically inserted by free hosts) |
|
2598 | + // idea from Luca Mariano <[email protected]> originally in PEARified version of the lib |
|
2599 | + $pos = strrpos($data, '</methodResponse>'); |
|
2600 | + if($pos !== false) |
|
2601 | + { |
|
2602 | + $data = substr($data, 0, $pos+17); |
|
2603 | + } |
|
2604 | + |
|
2605 | + // if user wants back raw xml, give it to him |
|
2606 | + if ($return_type == 'xml') |
|
2607 | + { |
|
2608 | + $r = new xmlrpcresp($data, 0, '', 'xml'); |
|
2609 | + $r->hdrs = $GLOBALS['_xh']['headers']; |
|
2610 | + $r->_cookies = $GLOBALS['_xh']['cookies']; |
|
2611 | + $r->raw_data = $raw_data; |
|
2612 | + return $r; |
|
2613 | + } |
|
2614 | + |
|
2615 | + // try to 'guestimate' the character encoding of the received response |
|
2616 | + $resp_encoding = guess_encoding(@$GLOBALS['_xh']['headers']['content-type'], $data); |
|
2617 | + |
|
2618 | + $GLOBALS['_xh']['ac']=''; |
|
2619 | + //$GLOBALS['_xh']['qt']=''; //unused... |
|
2620 | + $GLOBALS['_xh']['stack'] = array(); |
|
2621 | + $GLOBALS['_xh']['valuestack'] = array(); |
|
2622 | + $GLOBALS['_xh']['isf']=0; // 0 = OK, 1 for xmlrpc fault responses, 2 = invalid xmlrpc |
|
2623 | + $GLOBALS['_xh']['isf_reason']=''; |
|
2624 | + $GLOBALS['_xh']['rt']=''; // 'methodcall or 'methodresponse' |
|
2625 | + |
|
2626 | + // Since parsing will fail if charset is not specified in the xml prologue, |
|
2627 | + // the encoding is not UTF8 and there are non-ascii chars in the text, we try to work round that... |
|
2628 | + // The following code might be better for mb_string enabled installs, but |
|
2629 | + // makes the lib about 200% slower... |
|
2630 | + //if (!is_valid_charset($resp_encoding, array('UTF-8'))) |
|
2631 | + if (!in_array($resp_encoding, array('UTF-8', 'US-ASCII')) && !has_encoding($data)) { |
|
2632 | + if ($resp_encoding == 'ISO-8859-1') { |
|
2633 | + $data = utf8_encode($data); |
|
2634 | + } else { |
|
2635 | + if (extension_loaded('mbstring')) { |
|
2636 | + $data = mb_convert_encoding($data, 'UTF-8', $resp_encoding); |
|
2637 | + } else { |
|
2638 | + error_log('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of received request: ' . $resp_encoding); |
|
2639 | + } |
|
2640 | + } |
|
2641 | + } |
|
2642 | + |
|
2643 | + $parser = xml_parser_create(); |
|
2644 | + xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true); |
|
2645 | + // G. Giunta 2005/02/13: PHP internally uses ISO-8859-1, so we have to tell |
|
2646 | + // the xml parser to give us back data in the expected charset. |
|
2647 | + // What if internal encoding is not in one of the 3 allowed? |
|
2648 | + // we use the broadest one, ie. utf8 |
|
2649 | + // This allows to send data which is native in various charset, |
|
2650 | + // by extending xmlrpc_encode_entitites() and setting xmlrpc_internalencoding |
|
2651 | + if (!in_array($GLOBALS['xmlrpc_internalencoding'], array('UTF-8', 'ISO-8859-1', 'US-ASCII'))) |
|
2652 | + { |
|
2653 | + xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8'); |
|
2654 | + } |
|
2655 | + else |
|
2656 | + { |
|
2657 | + xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $GLOBALS['xmlrpc_internalencoding']); |
|
2658 | + } |
|
2659 | + |
|
2660 | + if ($return_type == 'phpvals') |
|
2661 | + { |
|
2662 | + xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee_fast'); |
|
2663 | + } |
|
2664 | + else |
|
2665 | + { |
|
2666 | + xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee'); |
|
2667 | + } |
|
2668 | + |
|
2669 | + xml_set_character_data_handler($parser, 'xmlrpc_cd'); |
|
2670 | + xml_set_default_handler($parser, 'xmlrpc_dh'); |
|
2671 | + |
|
2672 | + // first error check: xml not well formed |
|
2673 | + if(!xml_parse($parser, $data, count($data))) |
|
2674 | + { |
|
2675 | + // thanks to Peter Kocks <[email protected]> |
|
2676 | + if((xml_get_current_line_number($parser)) == 1) |
|
2677 | + { |
|
2678 | + $errstr = 'XML error at line 1, check URL'; |
|
2679 | + } |
|
2680 | + else |
|
2681 | + { |
|
2682 | + $errstr = sprintf('XML error: %s at line %d, column %d', |
|
2683 | + xml_error_string(xml_get_error_code($parser)), |
|
2684 | + xml_get_current_line_number($parser), xml_get_current_column_number($parser)); |
|
2685 | + } |
|
2686 | + error_log($errstr); |
|
2687 | + $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['invalid_return'], $GLOBALS['xmlrpcstr']['invalid_return'].' ('.$errstr.')'); |
|
2688 | + xml_parser_free($parser); |
|
2689 | + if($this->debug) |
|
2690 | + { |
|
2691 | + print $errstr; |
|
2692 | + } |
|
2693 | + $r->hdrs = $GLOBALS['_xh']['headers']; |
|
2694 | + $r->_cookies = $GLOBALS['_xh']['cookies']; |
|
2695 | + $r->raw_data = $raw_data; |
|
2696 | + return $r; |
|
2697 | + } |
|
2698 | + xml_parser_free($parser); |
|
2699 | + // second error check: xml well formed but not xml-rpc compliant |
|
2700 | + if ($GLOBALS['_xh']['isf'] > 1) |
|
2701 | + { |
|
2702 | + if ($this->debug) |
|
2703 | + { |
|
2704 | + /// @todo echo something for user? |
|
2705 | + } |
|
2706 | + |
|
2707 | + $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['invalid_return'], |
|
2708 | + $GLOBALS['xmlrpcstr']['invalid_return'] . ' ' . $GLOBALS['_xh']['isf_reason']); |
|
2709 | + } |
|
2710 | + // third error check: parsing of the response has somehow gone boink. |
|
2711 | + // NB: shall we omit this check, since we trust the parsing code? |
|
2712 | + elseif ($return_type == 'xmlrpcvals' && !is_object($GLOBALS['_xh']['value'])) |
|
2713 | + { |
|
2714 | + // something odd has happened |
|
2715 | + // and it's time to generate a client side error |
|
2716 | + // indicating something odd went on |
|
2717 | + $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['invalid_return'], |
|
2718 | + $GLOBALS['xmlrpcstr']['invalid_return']); |
|
2719 | + } |
|
2720 | + else |
|
2721 | + { |
|
2722 | + if ($this->debug) |
|
2723 | + { |
|
2724 | + print "<PRE>---PARSED---\n"; |
|
2725 | + // somehow htmlentities chokes on var_export, and some full html string... |
|
2726 | + //print htmlentitites(var_export($GLOBALS['_xh']['value'], true)); |
|
2727 | + print htmlspecialchars(var_export($GLOBALS['_xh']['value'], true)); |
|
2728 | + print "\n---END---</PRE>"; |
|
2729 | + } |
|
2730 | + |
|
2731 | + // note that using =& will raise an error if $GLOBALS['_xh']['st'] does not generate an object. |
|
2732 | + $v =& $GLOBALS['_xh']['value']; |
|
2733 | + |
|
2734 | + if($GLOBALS['_xh']['isf']) |
|
2735 | + { |
|
2736 | + /// @todo we should test here if server sent an int and a string, |
|
2737 | + /// and/or coerce them into such... |
|
2738 | + if ($return_type == 'xmlrpcvals') |
|
2739 | + { |
|
2740 | + $errno_v = $v->structmem('faultCode'); |
|
2741 | + $errstr_v = $v->structmem('faultString'); |
|
2742 | + $errno = $errno_v->scalarval(); |
|
2743 | + $errstr = $errstr_v->scalarval(); |
|
2744 | + } |
|
2745 | + else |
|
2746 | + { |
|
2747 | + $errno = $v['faultCode']; |
|
2748 | + $errstr = $v['faultString']; |
|
2749 | + } |
|
2750 | + |
|
2751 | + if($errno == 0) |
|
2752 | + { |
|
2753 | + // FAULT returned, errno needs to reflect that |
|
2754 | + $errno = -1; |
|
2755 | + } |
|
2756 | + |
|
2757 | + $r = new xmlrpcresp(0, $errno, $errstr); |
|
2758 | + } |
|
2759 | + else |
|
2760 | + { |
|
2761 | + $r=new xmlrpcresp($v, 0, '', $return_type); |
|
2762 | + } |
|
2763 | + } |
|
2764 | + |
|
2765 | + $r->hdrs = $GLOBALS['_xh']['headers']; |
|
2766 | + $r->_cookies = $GLOBALS['_xh']['cookies']; |
|
2767 | + $r->raw_data = $raw_data; |
|
2768 | + return $r; |
|
2769 | + } |
|
2770 | + } |
|
2771 | + |
|
2772 | + class xmlrpcval |
|
2773 | + { |
|
2774 | + var $me=array(); |
|
2775 | + var $mytype=0; |
|
2776 | + var $_php_class=null; |
|
2777 | + |
|
2778 | + /** |
|
2779 | + * @param mixed $val |
|
2780 | + * @param string $type any valid xmlrpc type name (lowercase). If null, 'string' is assumed |
|
2781 | + */ |
|
2782 | + function __construct($val=-1, $type='') |
|
2783 | + { |
|
2784 | + /// @todo: optimization creep - do not call addXX, do it all inline. |
|
2785 | + /// downside: booleans will not be coerced anymore |
|
2786 | + if($val!==-1 || $type!='') |
|
2787 | + { |
|
2788 | + // optimization creep: inlined all work done by constructor |
|
2789 | + switch($type) |
|
2790 | + { |
|
2791 | + case '': |
|
2792 | + $this->mytype=1; |
|
2793 | + $this->me['string']=$val; |
|
2794 | + break; |
|
2795 | + case 'i4': |
|
2796 | + case 'int': |
|
2797 | + case 'double': |
|
2798 | + case 'string': |
|
2799 | + case 'boolean': |
|
2800 | + case 'dateTime.iso8601': |
|
2801 | + case 'base64': |
|
2802 | + case 'null': |
|
2803 | + $this->mytype=1; |
|
2804 | + $this->me[$type]=$val; |
|
2805 | + break; |
|
2806 | + case 'array': |
|
2807 | + $this->mytype=2; |
|
2808 | + $this->me['array']=$val; |
|
2809 | + break; |
|
2810 | + case 'struct': |
|
2811 | + $this->mytype=3; |
|
2812 | + $this->me['struct']=$val; |
|
2813 | + break; |
|
2814 | + default: |
|
2815 | + error_log("XML-RPC: ".__METHOD__.": not a known type ($type)"); |
|
2816 | + } |
|
2817 | + /*if($type=='') |
|
2818 | 2818 | { |
2819 | 2819 | $type='string'; |
2820 | 2820 | } |
@@ -2830,1079 +2830,1079 @@ discard block |
||
2830 | 2830 | { |
2831 | 2831 | $this->addStruct($val); |
2832 | 2832 | }*/ |
2833 | - } |
|
2834 | - } |
|
2835 | - |
|
2836 | - /** |
|
2837 | - * @deprecated |
|
2838 | - */ |
|
2839 | - function xmlrpcval($val=-1, $type='') |
|
2840 | - { |
|
2841 | - self::__construct($val, $type); |
|
2842 | - } |
|
2843 | - |
|
2844 | - /** |
|
2845 | - * Add a single php value to an (unitialized) xmlrpcval |
|
2846 | - * @param mixed $val |
|
2847 | - * @param string $type |
|
2848 | - * @return int 1 or 0 on failure |
|
2849 | - */ |
|
2850 | - function addScalar($val, $type='string') |
|
2851 | - { |
|
2852 | - $typeof=@$GLOBALS['xmlrpcTypes'][$type]; |
|
2853 | - if($typeof!=1) |
|
2854 | - { |
|
2855 | - error_log("XML-RPC: ".__METHOD__.": not a scalar type ($type)"); |
|
2856 | - return 0; |
|
2857 | - } |
|
2858 | - |
|
2859 | - // coerce booleans into correct values |
|
2860 | - // NB: we should either do it for datetimes, integers and doubles, too, |
|
2861 | - // or just plain remove this check, implemented on booleans only... |
|
2862 | - if($type==$GLOBALS['xmlrpcBoolean']) |
|
2863 | - { |
|
2864 | - if(strcasecmp($val,'true')==0 || $val==1 || ($val==true && strcasecmp($val,'false'))) |
|
2865 | - { |
|
2866 | - $val=true; |
|
2867 | - } |
|
2868 | - else |
|
2869 | - { |
|
2870 | - $val=false; |
|
2871 | - } |
|
2872 | - } |
|
2873 | - |
|
2874 | - switch($this->mytype) |
|
2875 | - { |
|
2876 | - case 1: |
|
2877 | - error_log('XML-RPC: '.__METHOD__.': scalar xmlrpcval can have only one value'); |
|
2878 | - return 0; |
|
2879 | - case 3: |
|
2880 | - error_log('XML-RPC: '.__METHOD__.': cannot add anonymous scalar to struct xmlrpcval'); |
|
2881 | - return 0; |
|
2882 | - case 2: |
|
2883 | - // we're adding a scalar value to an array here |
|
2884 | - //$ar=$this->me['array']; |
|
2885 | - //$ar[]=new xmlrpcval($val, $type); |
|
2886 | - //$this->me['array']=$ar; |
|
2887 | - // Faster (?) avoid all the costly array-copy-by-val done here... |
|
2888 | - $this->me['array'][]=new xmlrpcval($val, $type); |
|
2889 | - return 1; |
|
2890 | - default: |
|
2891 | - // a scalar, so set the value and remember we're scalar |
|
2892 | - $this->me[$type]=$val; |
|
2893 | - $this->mytype=$typeof; |
|
2894 | - return 1; |
|
2895 | - } |
|
2896 | - } |
|
2897 | - |
|
2898 | - /** |
|
2899 | - * Add an array of xmlrpcval objects to an xmlrpcval |
|
2900 | - * @param array $vals |
|
2901 | - * @return int 1 or 0 on failure |
|
2902 | - * @access public |
|
2903 | - * |
|
2904 | - * @todo add some checking for $vals to be an array of xmlrpcvals? |
|
2905 | - */ |
|
2906 | - function addArray($vals) |
|
2907 | - { |
|
2908 | - if($this->mytype==0) |
|
2909 | - { |
|
2910 | - $this->mytype=$GLOBALS['xmlrpcTypes']['array']; |
|
2911 | - $this->me['array']=$vals; |
|
2912 | - return 1; |
|
2913 | - } |
|
2914 | - elseif($this->mytype==2) |
|
2915 | - { |
|
2916 | - // we're adding to an array here |
|
2917 | - $this->me['array'] = array_merge($this->me['array'], $vals); |
|
2918 | - return 1; |
|
2919 | - } |
|
2920 | - else |
|
2921 | - { |
|
2922 | - error_log('XML-RPC: '.__METHOD__.': already initialized as a [' . $this->kindOf() . ']'); |
|
2923 | - return 0; |
|
2924 | - } |
|
2925 | - } |
|
2926 | - |
|
2927 | - /** |
|
2928 | - * Add an array of named xmlrpcval objects to an xmlrpcval |
|
2929 | - * @param array $vals |
|
2930 | - * @return int 1 or 0 on failure |
|
2931 | - * @access public |
|
2932 | - * |
|
2933 | - * @todo add some checking for $vals to be an array? |
|
2934 | - */ |
|
2935 | - function addStruct($vals) |
|
2936 | - { |
|
2937 | - if($this->mytype==0) |
|
2938 | - { |
|
2939 | - $this->mytype=$GLOBALS['xmlrpcTypes']['struct']; |
|
2940 | - $this->me['struct']=$vals; |
|
2941 | - return 1; |
|
2942 | - } |
|
2943 | - elseif($this->mytype==3) |
|
2944 | - { |
|
2945 | - // we're adding to a struct here |
|
2946 | - $this->me['struct'] = array_merge($this->me['struct'], $vals); |
|
2947 | - return 1; |
|
2948 | - } |
|
2949 | - else |
|
2950 | - { |
|
2951 | - error_log('XML-RPC: '.__METHOD__.': already initialized as a [' . $this->kindOf() . ']'); |
|
2952 | - return 0; |
|
2953 | - } |
|
2954 | - } |
|
2955 | - |
|
2956 | - // poor man's version of print_r ??? |
|
2957 | - // DEPRECATED! |
|
2958 | - function dump($ar) |
|
2959 | - { |
|
2960 | - foreach($ar as $key => $val) |
|
2961 | - { |
|
2962 | - echo "$key => $val<br />"; |
|
2963 | - if($key == 'array') |
|
2964 | - { |
|
2965 | - while(list($key2, $val2) = each($val)) |
|
2966 | - { |
|
2967 | - echo "-- $key2 => $val2<br />"; |
|
2968 | - } |
|
2969 | - } |
|
2970 | - } |
|
2971 | - } |
|
2972 | - |
|
2973 | - /** |
|
2974 | - * Returns a string containing "struct", "array" or "scalar" describing the base type of the value |
|
2975 | - * @return string |
|
2976 | - * @access public |
|
2977 | - */ |
|
2978 | - function kindOf() |
|
2979 | - { |
|
2980 | - switch($this->mytype) |
|
2981 | - { |
|
2982 | - case 3: |
|
2983 | - return 'struct'; |
|
2984 | - break; |
|
2985 | - case 2: |
|
2986 | - return 'array'; |
|
2987 | - break; |
|
2988 | - case 1: |
|
2989 | - return 'scalar'; |
|
2990 | - break; |
|
2991 | - default: |
|
2992 | - return 'undef'; |
|
2993 | - } |
|
2994 | - } |
|
2995 | - |
|
2996 | - /** |
|
2997 | - * @access private |
|
2998 | - */ |
|
2999 | - function serializedata($typ, $val, $charset_encoding='') |
|
3000 | - { |
|
3001 | - $rs=''; |
|
3002 | - switch(@$GLOBALS['xmlrpcTypes'][$typ]) |
|
3003 | - { |
|
3004 | - case 1: |
|
3005 | - switch($typ) |
|
3006 | - { |
|
3007 | - case $GLOBALS['xmlrpcBase64']: |
|
3008 | - $rs.="<${typ}>" . base64_encode($val) . "</${typ}>"; |
|
3009 | - break; |
|
3010 | - case $GLOBALS['xmlrpcBoolean']: |
|
3011 | - $rs.="<${typ}>" . ($val ? '1' : '0') . "</${typ}>"; |
|
3012 | - break; |
|
3013 | - case $GLOBALS['xmlrpcString']: |
|
3014 | - // G. Giunta 2005/2/13: do NOT use htmlentities, since |
|
3015 | - // it will produce named html entities, which are invalid xml |
|
3016 | - $rs.="<${typ}>" . xmlrpc_encode_entitites($val, $GLOBALS['xmlrpc_internalencoding'], $charset_encoding). "</${typ}>"; |
|
3017 | - break; |
|
3018 | - case $GLOBALS['xmlrpcInt']: |
|
3019 | - case $GLOBALS['xmlrpcI4']: |
|
3020 | - $rs.="<${typ}>".(int)$val."</${typ}>"; |
|
3021 | - break; |
|
3022 | - case $GLOBALS['xmlrpcDouble']: |
|
3023 | - // avoid using standard conversion of float to string because it is locale-dependent, |
|
3024 | - // and also because the xmlrpc spec forbids exponential notation. |
|
3025 | - // sprintf('%F') could be most likely ok but it fails eg. on 2e-14. |
|
3026 | - // The code below tries its best at keeping max precision while avoiding exp notation, |
|
3027 | - // but there is of course no limit in the number of decimal places to be used... |
|
3028 | - $rs.="<${typ}>".preg_replace('/\\.?0+$/','',number_format((double)$val, 128, '.', ''))."</${typ}>"; |
|
3029 | - break; |
|
3030 | - case $GLOBALS['xmlrpcDateTime']: |
|
3031 | - if (is_string($val)) |
|
3032 | - { |
|
3033 | - $rs.="<${typ}>${val}</${typ}>"; |
|
3034 | - } |
|
3035 | - else if(is_a($val, 'DateTime')) |
|
3036 | - { |
|
3037 | - $rs.="<${typ}>".$val->format('Ymd\TH:i:s')."</${typ}>"; |
|
3038 | - } |
|
3039 | - else if(is_int($val)) |
|
3040 | - { |
|
3041 | - $rs.="<${typ}>".strftime("%Y%m%dT%H:%M:%S", $val)."</${typ}>"; |
|
3042 | - } |
|
3043 | - else |
|
3044 | - { |
|
3045 | - // not really a good idea here: but what shall we output anyway? left for backward compat... |
|
3046 | - $rs.="<${typ}>${val}</${typ}>"; |
|
3047 | - } |
|
3048 | - break; |
|
3049 | - case $GLOBALS['xmlrpcNull']: |
|
3050 | - if ($GLOBALS['xmlrpc_null_apache_encoding']) |
|
3051 | - { |
|
3052 | - $rs.="<ex:nil/>"; |
|
3053 | - } |
|
3054 | - else |
|
3055 | - { |
|
3056 | - $rs.="<nil/>"; |
|
3057 | - } |
|
3058 | - break; |
|
3059 | - default: |
|
3060 | - // no standard type value should arrive here, but provide a possibility |
|
3061 | - // for xmlrpcvals of unknown type... |
|
3062 | - $rs.="<${typ}>${val}</${typ}>"; |
|
3063 | - } |
|
3064 | - break; |
|
3065 | - case 3: |
|
3066 | - // struct |
|
3067 | - if ($this->_php_class) |
|
3068 | - { |
|
3069 | - $rs.='<struct php_class="' . $this->_php_class . "\">\n"; |
|
3070 | - } |
|
3071 | - else |
|
3072 | - { |
|
3073 | - $rs.="<struct>\n"; |
|
3074 | - } |
|
3075 | - foreach($val as $key2 => $val2) |
|
3076 | - { |
|
3077 | - $rs.='<member><name>'.xmlrpc_encode_entitites($key2, $GLOBALS['xmlrpc_internalencoding'], $charset_encoding)."</name>\n"; |
|
3078 | - //$rs.=$this->serializeval($val2); |
|
3079 | - $rs.=$val2->serialize($charset_encoding); |
|
3080 | - $rs.="</member>\n"; |
|
3081 | - } |
|
3082 | - $rs.='</struct>'; |
|
3083 | - break; |
|
3084 | - case 2: |
|
3085 | - // array |
|
3086 | - $rs.="<array>\n<data>\n"; |
|
3087 | - for($i=0; $i<count($val); $i++) |
|
3088 | - { |
|
3089 | - //$rs.=$this->serializeval($val[$i]); |
|
3090 | - $rs.=$val[$i]->serialize($charset_encoding); |
|
3091 | - } |
|
3092 | - $rs.="</data>\n</array>"; |
|
3093 | - break; |
|
3094 | - default: |
|
3095 | - break; |
|
3096 | - } |
|
3097 | - return $rs; |
|
3098 | - } |
|
3099 | - |
|
3100 | - /** |
|
3101 | - * Returns xml representation of the value. XML prologue not included |
|
3102 | - * @param string $charset_encoding the charset to be used for serialization. if null, US-ASCII is assumed |
|
3103 | - * @return string |
|
3104 | - * @access public |
|
3105 | - */ |
|
3106 | - function serialize($charset_encoding='') |
|
3107 | - { |
|
3108 | - // add check? slower, but helps to avoid recursion in serializing broken xmlrpcvals... |
|
3109 | - //if (is_object($o) && (get_class($o) == 'xmlrpcval' || is_subclass_of($o, 'xmlrpcval'))) |
|
3110 | - //{ |
|
3111 | - reset($this->me); |
|
3112 | - list($typ, $val) = each($this->me); |
|
3113 | - return '<value>' . $this->serializedata($typ, $val, $charset_encoding) . "</value>\n"; |
|
3114 | - //} |
|
3115 | - } |
|
3116 | - |
|
3117 | - // DEPRECATED |
|
3118 | - function serializeval($o) |
|
3119 | - { |
|
3120 | - // add check? slower, but helps to avoid recursion in serializing broken xmlrpcvals... |
|
3121 | - //if (is_object($o) && (get_class($o) == 'xmlrpcval' || is_subclass_of($o, 'xmlrpcval'))) |
|
3122 | - //{ |
|
3123 | - $ar=$o->me; |
|
3124 | - reset($ar); |
|
3125 | - list($typ, $val) = each($ar); |
|
3126 | - return '<value>' . $this->serializedata($typ, $val) . "</value>\n"; |
|
3127 | - //} |
|
3128 | - } |
|
3129 | - |
|
3130 | - /** |
|
3131 | - * Checks whether a struct member with a given name is present. |
|
3132 | - * Works only on xmlrpcvals of type struct. |
|
3133 | - * @param string $m the name of the struct member to be looked up |
|
3134 | - * @return boolean |
|
3135 | - * @access public |
|
3136 | - */ |
|
3137 | - function structmemexists($m) |
|
3138 | - { |
|
3139 | - return array_key_exists($m, $this->me['struct']); |
|
3140 | - } |
|
3141 | - |
|
3142 | - /** |
|
3143 | - * Returns the value of a given struct member (an xmlrpcval object in itself). |
|
3144 | - * Will raise a php warning if struct member of given name does not exist |
|
3145 | - * @param string $m the name of the struct member to be looked up |
|
3146 | - * @return xmlrpcval |
|
3147 | - * @access public |
|
3148 | - */ |
|
3149 | - function structmem($m) |
|
3150 | - { |
|
3151 | - return $this->me['struct'][$m]; |
|
3152 | - } |
|
3153 | - |
|
3154 | - /** |
|
3155 | - * Reset internal pointer for xmlrpcvals of type struct. |
|
3156 | - * @access public |
|
3157 | - */ |
|
3158 | - function structreset() |
|
3159 | - { |
|
3160 | - reset($this->me['struct']); |
|
3161 | - } |
|
3162 | - |
|
3163 | - /** |
|
3164 | - * Return next member element for xmlrpcvals of type struct. |
|
3165 | - * @return xmlrpcval |
|
3166 | - * @access public |
|
3167 | - */ |
|
3168 | - function structeach() |
|
3169 | - { |
|
3170 | - return each($this->me['struct']); |
|
3171 | - } |
|
3172 | - |
|
3173 | - // DEPRECATED! this code looks like it is very fragile and has not been fixed |
|
3174 | - // for a long long time. Shall we remove it for 2.0? |
|
3175 | - function getval() |
|
3176 | - { |
|
3177 | - // UNSTABLE |
|
3178 | - reset($this->me); |
|
3179 | - list($a,$b)=each($this->me); |
|
3180 | - // contributed by I Sofer, 2001-03-24 |
|
3181 | - // add support for nested arrays to scalarval |
|
3182 | - // i've created a new method here, so as to |
|
3183 | - // preserve back compatibility |
|
3184 | - |
|
3185 | - if(is_array($b)) |
|
3186 | - { |
|
3187 | - @reset($b); |
|
3188 | - while(list($id,$cont) = @each($b)) |
|
3189 | - { |
|
3190 | - $b[$id] = $cont->scalarval(); |
|
3191 | - } |
|
3192 | - } |
|
3193 | - |
|
3194 | - // add support for structures directly encoding php objects |
|
3195 | - if(is_object($b)) |
|
3196 | - { |
|
3197 | - $t = get_object_vars($b); |
|
3198 | - @reset($t); |
|
3199 | - while(list($id,$cont) = @each($t)) |
|
3200 | - { |
|
3201 | - $t[$id] = $cont->scalarval(); |
|
3202 | - } |
|
3203 | - @reset($t); |
|
3204 | - while(list($id,$cont) = @each($t)) |
|
3205 | - { |
|
3206 | - @$b->$id = $cont; |
|
3207 | - } |
|
3208 | - } |
|
3209 | - // end contrib |
|
3210 | - return $b; |
|
3211 | - } |
|
3212 | - |
|
3213 | - /** |
|
3214 | - * Returns the value of a scalar xmlrpcval |
|
3215 | - * @return mixed |
|
3216 | - * @access public |
|
3217 | - */ |
|
3218 | - function scalarval() |
|
3219 | - { |
|
3220 | - reset($this->me); |
|
3221 | - list(,$b)=each($this->me); |
|
3222 | - return $b; |
|
3223 | - } |
|
3224 | - |
|
3225 | - /** |
|
3226 | - * Returns the type of the xmlrpcval. |
|
3227 | - * For integers, 'int' is always returned in place of 'i4' |
|
3228 | - * @return string |
|
3229 | - * @access public |
|
3230 | - */ |
|
3231 | - function scalartyp() |
|
3232 | - { |
|
3233 | - reset($this->me); |
|
3234 | - list($a,)=each($this->me); |
|
3235 | - if($a==$GLOBALS['xmlrpcI4']) |
|
3236 | - { |
|
3237 | - $a=$GLOBALS['xmlrpcInt']; |
|
3238 | - } |
|
3239 | - return $a; |
|
3240 | - } |
|
3241 | - |
|
3242 | - /** |
|
3243 | - * Returns the m-th member of an xmlrpcval of struct type |
|
3244 | - * @param integer $m the index of the value to be retrieved (zero based) |
|
3245 | - * @return xmlrpcval |
|
3246 | - * @access public |
|
3247 | - */ |
|
3248 | - function arraymem($m) |
|
3249 | - { |
|
3250 | - return $this->me['array'][$m]; |
|
3251 | - } |
|
3252 | - |
|
3253 | - /** |
|
3254 | - * Returns the number of members in an xmlrpcval of array type |
|
3255 | - * @return integer |
|
3256 | - * @access public |
|
3257 | - */ |
|
3258 | - function arraysize() |
|
3259 | - { |
|
3260 | - return count($this->me['array']); |
|
3261 | - } |
|
3262 | - |
|
3263 | - /** |
|
3264 | - * Returns the number of members in an xmlrpcval of struct type |
|
3265 | - * @return integer |
|
3266 | - * @access public |
|
3267 | - */ |
|
3268 | - function structsize() |
|
3269 | - { |
|
3270 | - return count($this->me['struct']); |
|
3271 | - } |
|
3272 | - } |
|
3273 | - |
|
3274 | - |
|
3275 | - // date helpers |
|
3276 | - |
|
3277 | - /** |
|
3278 | - * Given a timestamp, return the corresponding ISO8601 encoded string. |
|
3279 | - * |
|
3280 | - * Really, timezones ought to be supported |
|
3281 | - * but the XML-RPC spec says: |
|
3282 | - * |
|
3283 | - * "Don't assume a timezone. It should be specified by the server in its |
|
3284 | - * documentation what assumptions it makes about timezones." |
|
3285 | - * |
|
3286 | - * These routines always assume localtime unless |
|
3287 | - * $utc is set to 1, in which case UTC is assumed |
|
3288 | - * and an adjustment for locale is made when encoding |
|
3289 | - * |
|
3290 | - * @param int $timet (timestamp) |
|
3291 | - * @param int $utc (0 or 1) |
|
3292 | - * @return string |
|
3293 | - */ |
|
3294 | - function iso8601_encode($timet, $utc=0) |
|
3295 | - { |
|
3296 | - if(!$utc) |
|
3297 | - { |
|
3298 | - $t=strftime("%Y%m%dT%H:%M:%S", $timet); |
|
3299 | - } |
|
3300 | - else |
|
3301 | - { |
|
3302 | - if(function_exists('gmstrftime')) |
|
3303 | - { |
|
3304 | - // gmstrftime doesn't exist in some versions |
|
3305 | - // of PHP |
|
3306 | - $t=gmstrftime("%Y%m%dT%H:%M:%S", $timet); |
|
3307 | - } |
|
3308 | - else |
|
3309 | - { |
|
3310 | - $t=strftime("%Y%m%dT%H:%M:%S", $timet-date('Z')); |
|
3311 | - } |
|
3312 | - } |
|
3313 | - return $t; |
|
3314 | - } |
|
3315 | - |
|
3316 | - /** |
|
3317 | - * Given an ISO8601 date string, return a timet in the localtime, or UTC |
|
3318 | - * @param string $idate |
|
3319 | - * @param int $utc either 0 or 1 |
|
3320 | - * @return int (datetime) |
|
3321 | - */ |
|
3322 | - function iso8601_decode($idate, $utc=0) |
|
3323 | - { |
|
3324 | - $t=0; |
|
3325 | - if(preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})/', $idate, $regs)) |
|
3326 | - { |
|
3327 | - if($utc) |
|
3328 | - { |
|
3329 | - $t=gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]); |
|
3330 | - } |
|
3331 | - else |
|
3332 | - { |
|
3333 | - $t=mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]); |
|
3334 | - } |
|
3335 | - } |
|
3336 | - return $t; |
|
3337 | - } |
|
3338 | - |
|
3339 | - /** |
|
3340 | - * Takes an xmlrpc value in PHP xmlrpcval object format and translates it into native PHP types. |
|
3341 | - * |
|
3342 | - * Works with xmlrpc message objects as input, too. |
|
3343 | - * |
|
3344 | - * Given proper options parameter, can rebuild generic php object instances |
|
3345 | - * (provided those have been encoded to xmlrpc format using a corresponding |
|
3346 | - * option in php_xmlrpc_encode()) |
|
3347 | - * PLEASE NOTE that rebuilding php objects involves calling their constructor function. |
|
3348 | - * This means that the remote communication end can decide which php code will |
|
3349 | - * get executed on your server, leaving the door possibly open to 'php-injection' |
|
3350 | - * style of attacks (provided you have some classes defined on your server that |
|
3351 | - * might wreak havoc if instances are built outside an appropriate context). |
|
3352 | - * Make sure you trust the remote server/client before eanbling this! |
|
3353 | - * |
|
3354 | - * @author Dan Libby ([email protected]) |
|
3355 | - * |
|
3356 | - * @param xmlrpcval $xmlrpc_val |
|
3357 | - * @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 |
|
3358 | - * @return mixed |
|
3359 | - */ |
|
3360 | - function php_xmlrpc_decode($xmlrpc_val, $options=array()) |
|
3361 | - { |
|
3362 | - switch($xmlrpc_val->kindOf()) |
|
3363 | - { |
|
3364 | - case 'scalar': |
|
3365 | - if (in_array('extension_api', $options)) |
|
3366 | - { |
|
3367 | - reset($xmlrpc_val->me); |
|
3368 | - list($typ,$val) = each($xmlrpc_val->me); |
|
3369 | - switch ($typ) |
|
3370 | - { |
|
3371 | - case 'dateTime.iso8601': |
|
3372 | - $xmlrpc_val->scalar = $val; |
|
3373 | - $xmlrpc_val->xmlrpc_type = 'datetime'; |
|
3374 | - $xmlrpc_val->timestamp = iso8601_decode($val); |
|
3375 | - return $xmlrpc_val; |
|
3376 | - case 'base64': |
|
3377 | - $xmlrpc_val->scalar = $val; |
|
3378 | - $xmlrpc_val->type = $typ; |
|
3379 | - return $xmlrpc_val; |
|
3380 | - default: |
|
3381 | - return $xmlrpc_val->scalarval(); |
|
3382 | - } |
|
3383 | - } |
|
3384 | - if (in_array('dates_as_objects', $options) && $xmlrpc_val->scalartyp() == 'dateTime.iso8601') |
|
3385 | - { |
|
3386 | - // we return a Datetime object instead of a string |
|
3387 | - // since now the constructor of xmlrpcval accepts safely strings, ints and datetimes, |
|
3388 | - // we cater to all 3 cases here |
|
3389 | - $out = $xmlrpc_val->scalarval(); |
|
3390 | - if (is_string($out)) |
|
3391 | - { |
|
3392 | - $out = strtotime($out); |
|
3393 | - } |
|
3394 | - if (is_int($out)) |
|
3395 | - { |
|
3396 | - $result = new Datetime(); |
|
3397 | - $result->setTimestamp($out); |
|
3398 | - return $result; |
|
3399 | - } |
|
3400 | - elseif (is_a($out, 'Datetime')) |
|
3401 | - { |
|
3402 | - return $out; |
|
3403 | - } |
|
3404 | - } |
|
3405 | - return $xmlrpc_val->scalarval(); |
|
3406 | - case 'array': |
|
3407 | - $size = $xmlrpc_val->arraysize(); |
|
3408 | - $arr = array(); |
|
3409 | - for($i = 0; $i < $size; $i++) |
|
3410 | - { |
|
3411 | - $arr[] = php_xmlrpc_decode($xmlrpc_val->arraymem($i), $options); |
|
3412 | - } |
|
3413 | - return $arr; |
|
3414 | - case 'struct': |
|
3415 | - $xmlrpc_val->structreset(); |
|
3416 | - // If user said so, try to rebuild php objects for specific struct vals. |
|
3417 | - /// @todo should we raise a warning for class not found? |
|
3418 | - // shall we check for proper subclass of xmlrpcval instead of |
|
3419 | - // presence of _php_class to detect what we can do? |
|
3420 | - if (in_array('decode_php_objs', $options) && $xmlrpc_val->_php_class != '' |
|
3421 | - && class_exists($xmlrpc_val->_php_class)) |
|
3422 | - { |
|
3423 | - $obj = @new $xmlrpc_val->_php_class; |
|
3424 | - while(list($key,$value)=$xmlrpc_val->structeach()) |
|
3425 | - { |
|
3426 | - $obj->$key = php_xmlrpc_decode($value, $options); |
|
3427 | - } |
|
3428 | - return $obj; |
|
3429 | - } |
|
3430 | - else |
|
3431 | - { |
|
3432 | - $arr = array(); |
|
3433 | - while(list($key,$value)=$xmlrpc_val->structeach()) |
|
3434 | - { |
|
3435 | - $arr[$key] = php_xmlrpc_decode($value, $options); |
|
3436 | - } |
|
3437 | - return $arr; |
|
3438 | - } |
|
3439 | - case 'msg': |
|
3440 | - $paramcount = $xmlrpc_val->getNumParams(); |
|
3441 | - $arr = array(); |
|
3442 | - for($i = 0; $i < $paramcount; $i++) |
|
3443 | - { |
|
3444 | - $arr[] = php_xmlrpc_decode($xmlrpc_val->getParam($i)); |
|
3445 | - } |
|
3446 | - return $arr; |
|
3447 | - } |
|
3448 | - } |
|
3449 | - |
|
3450 | - // This constant left here only for historical reasons... |
|
3451 | - // it was used to decide if we have to define xmlrpc_encode on our own, but |
|
3452 | - // we do not do it anymore |
|
3453 | - if(function_exists('xmlrpc_decode')) |
|
3454 | - { |
|
3455 | - define('XMLRPC_EPI_ENABLED','1'); |
|
3456 | - } |
|
3457 | - else |
|
3458 | - { |
|
3459 | - define('XMLRPC_EPI_ENABLED','0'); |
|
3460 | - } |
|
3461 | - |
|
3462 | - /** |
|
3463 | - * Takes native php types and encodes them into xmlrpc PHP object format. |
|
3464 | - * It will not re-encode xmlrpcval objects. |
|
3465 | - * |
|
3466 | - * Feature creep -- could support more types via optional type argument |
|
3467 | - * (string => datetime support has been added, ??? => base64 not yet) |
|
3468 | - * |
|
3469 | - * If given a proper options parameter, php object instances will be encoded |
|
3470 | - * into 'special' xmlrpc values, that can later be decoded into php objects |
|
3471 | - * by calling php_xmlrpc_decode() with a corresponding option |
|
3472 | - * |
|
3473 | - * @author Dan Libby ([email protected]) |
|
3474 | - * |
|
3475 | - * @param mixed $php_val the value to be converted into an xmlrpcval object |
|
3476 | - * @param array $options can include 'encode_php_objs', 'auto_dates', 'null_extension' or 'extension_api' |
|
3477 | - * @return xmlrpcval |
|
3478 | - */ |
|
3479 | - function php_xmlrpc_encode($php_val, $options=array()) |
|
3480 | - { |
|
3481 | - $type = gettype($php_val); |
|
3482 | - switch($type) |
|
3483 | - { |
|
3484 | - case 'string': |
|
3485 | - if (in_array('auto_dates', $options) && preg_match('/^[0-9]{8}T[0-9]{2}:[0-9]{2}:[0-9]{2}$/', $php_val)) |
|
3486 | - $xmlrpc_val = new xmlrpcval($php_val, $GLOBALS['xmlrpcDateTime']); |
|
3487 | - else |
|
3488 | - $xmlrpc_val = new xmlrpcval($php_val, $GLOBALS['xmlrpcString']); |
|
3489 | - break; |
|
3490 | - case 'integer': |
|
3491 | - $xmlrpc_val = new xmlrpcval($php_val, $GLOBALS['xmlrpcInt']); |
|
3492 | - break; |
|
3493 | - case 'double': |
|
3494 | - $xmlrpc_val = new xmlrpcval($php_val, $GLOBALS['xmlrpcDouble']); |
|
3495 | - break; |
|
3496 | - // <G_Giunta_2001-02-29> |
|
3497 | - // Add support for encoding/decoding of booleans, since they are supported in PHP |
|
3498 | - case 'boolean': |
|
3499 | - $xmlrpc_val = new xmlrpcval($php_val, $GLOBALS['xmlrpcBoolean']); |
|
3500 | - break; |
|
3501 | - // </G_Giunta_2001-02-29> |
|
3502 | - case 'array': |
|
3503 | - // PHP arrays can be encoded to either xmlrpc structs or arrays, |
|
3504 | - // depending on wheter they are hashes or plain 0..n integer indexed |
|
3505 | - // A shorter one-liner would be |
|
3506 | - // $tmp = array_diff(array_keys($php_val), range(0, count($php_val)-1)); |
|
3507 | - // but execution time skyrockets! |
|
3508 | - $j = 0; |
|
3509 | - $arr = array(); |
|
3510 | - $ko = false; |
|
3511 | - foreach($php_val as $key => $val) |
|
3512 | - { |
|
3513 | - $arr[$key] = php_xmlrpc_encode($val, $options); |
|
3514 | - if(!$ko && $key !== $j) |
|
3515 | - { |
|
3516 | - $ko = true; |
|
3517 | - } |
|
3518 | - $j++; |
|
3519 | - } |
|
3520 | - if($ko) |
|
3521 | - { |
|
3522 | - $xmlrpc_val = new xmlrpcval($arr, $GLOBALS['xmlrpcStruct']); |
|
3523 | - } |
|
3524 | - else |
|
3525 | - { |
|
3526 | - $xmlrpc_val = new xmlrpcval($arr, $GLOBALS['xmlrpcArray']); |
|
3527 | - } |
|
3528 | - break; |
|
3529 | - case 'object': |
|
3530 | - if(is_a($php_val, 'xmlrpcval')) |
|
3531 | - { |
|
3532 | - $xmlrpc_val = $php_val; |
|
3533 | - } |
|
3534 | - else if(is_a($php_val, 'DateTime')) |
|
3535 | - { |
|
3536 | - $xmlrpc_val = new xmlrpcval($php_val->format('Ymd\TH:i:s'), $GLOBALS['xmlrpcStruct']); |
|
3537 | - } |
|
3538 | - else |
|
3539 | - { |
|
3540 | - $arr = array(); |
|
3541 | - reset($php_val); |
|
3542 | - while(list($k,$v) = each($php_val)) |
|
3543 | - { |
|
3544 | - $arr[$k] = php_xmlrpc_encode($v, $options); |
|
3545 | - } |
|
3546 | - $xmlrpc_val = new xmlrpcval($arr, $GLOBALS['xmlrpcStruct']); |
|
3547 | - if (in_array('encode_php_objs', $options)) |
|
3548 | - { |
|
3549 | - // let's save original class name into xmlrpcval: |
|
3550 | - // might be useful later on... |
|
3551 | - $xmlrpc_val->_php_class = get_class($php_val); |
|
3552 | - } |
|
3553 | - } |
|
3554 | - break; |
|
3555 | - case 'NULL': |
|
3556 | - if (in_array('extension_api', $options)) |
|
3557 | - { |
|
3558 | - $xmlrpc_val = new xmlrpcval('', $GLOBALS['xmlrpcString']); |
|
3559 | - } |
|
3560 | - else if (in_array('null_extension', $options)) |
|
3561 | - { |
|
3562 | - $xmlrpc_val = new xmlrpcval('', $GLOBALS['xmlrpcNull']); |
|
3563 | - } |
|
3564 | - else |
|
3565 | - { |
|
3566 | - $xmlrpc_val = new xmlrpcval(); |
|
3567 | - } |
|
3568 | - break; |
|
3569 | - case 'resource': |
|
3570 | - if (in_array('extension_api', $options)) |
|
3571 | - { |
|
3572 | - $xmlrpc_val = new xmlrpcval((int)$php_val, $GLOBALS['xmlrpcInt']); |
|
3573 | - } |
|
3574 | - else |
|
3575 | - { |
|
3576 | - $xmlrpc_val = new xmlrpcval(); |
|
3577 | - } |
|
3578 | - // catch "user function", "unknown type" |
|
3579 | - default: |
|
3580 | - // giancarlo pinerolo <[email protected]> |
|
3581 | - // it has to return |
|
3582 | - // an empty object in case, not a boolean. |
|
3583 | - $xmlrpc_val = new xmlrpcval(); |
|
3584 | - break; |
|
3585 | - } |
|
3586 | - return $xmlrpc_val; |
|
3587 | - } |
|
3588 | - |
|
3589 | - /** |
|
3590 | - * Convert the xml representation of a method response, method request or single |
|
3591 | - * xmlrpc value into the appropriate object (a.k.a. deserialize) |
|
3592 | - * @param string $xml_val |
|
3593 | - * @param array $options |
|
3594 | - * @return mixed false on error, or an instance of either xmlrpcval, xmlrpcmsg or xmlrpcresp |
|
3595 | - */ |
|
3596 | - function php_xmlrpc_decode_xml($xml_val, $options=array()) |
|
3597 | - { |
|
3598 | - $GLOBALS['_xh'] = array(); |
|
3599 | - $GLOBALS['_xh']['ac'] = ''; |
|
3600 | - $GLOBALS['_xh']['stack'] = array(); |
|
3601 | - $GLOBALS['_xh']['valuestack'] = array(); |
|
3602 | - $GLOBALS['_xh']['params'] = array(); |
|
3603 | - $GLOBALS['_xh']['pt'] = array(); |
|
3604 | - $GLOBALS['_xh']['isf'] = 0; |
|
3605 | - $GLOBALS['_xh']['isf_reason'] = ''; |
|
3606 | - $GLOBALS['_xh']['method'] = false; |
|
3607 | - $GLOBALS['_xh']['rt'] = ''; |
|
3608 | - |
|
3609 | - // 'guestimate' encoding |
|
3610 | - $val_encoding = guess_encoding('', $xml_val); |
|
3611 | - |
|
3612 | - // Since parsing will fail if charset is not specified in the xml prologue, |
|
3613 | - // the encoding is not UTF8 and there are non-ascii chars in the text, we try to work round that... |
|
3614 | - // The following code might be better for mb_string enabled installs, but |
|
3615 | - // makes the lib about 200% slower... |
|
3616 | - //if (!is_valid_charset($val_encoding, array('UTF-8'))) |
|
3617 | - if (!in_array($val_encoding, array('UTF-8', 'US-ASCII')) && !has_encoding($xml_val)) { |
|
3618 | - if ($val_encoding == 'ISO-8859-1') { |
|
3619 | - $xml_val = utf8_encode($xml_val); |
|
3620 | - } else { |
|
3621 | - if (extension_loaded('mbstring')) { |
|
3622 | - $xml_val = mb_convert_encoding($xml_val, 'UTF-8', $val_encoding); |
|
3623 | - } else { |
|
3624 | - error_log('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of received request: ' . $val_encoding); |
|
3625 | - } |
|
3626 | - } |
|
3627 | - } |
|
3628 | - |
|
3629 | - $parser = xml_parser_create(); |
|
3630 | - xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true); |
|
3631 | - // What if internal encoding is not in one of the 3 allowed? |
|
3632 | - // we use the broadest one, ie. utf8! |
|
3633 | - if (!in_array($GLOBALS['xmlrpc_internalencoding'], array('UTF-8', 'ISO-8859-1', 'US-ASCII'))) |
|
3634 | - { |
|
3635 | - xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8'); |
|
3636 | - } |
|
3637 | - else |
|
3638 | - { |
|
3639 | - xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $GLOBALS['xmlrpc_internalencoding']); |
|
3640 | - } |
|
3641 | - xml_set_element_handler($parser, 'xmlrpc_se_any', 'xmlrpc_ee'); |
|
3642 | - xml_set_character_data_handler($parser, 'xmlrpc_cd'); |
|
3643 | - xml_set_default_handler($parser, 'xmlrpc_dh'); |
|
3644 | - if(!xml_parse($parser, $xml_val, 1)) |
|
3645 | - { |
|
3646 | - $errstr = sprintf('XML error: %s at line %d, column %d', |
|
3647 | - xml_error_string(xml_get_error_code($parser)), |
|
3648 | - xml_get_current_line_number($parser), xml_get_current_column_number($parser)); |
|
3649 | - error_log($errstr); |
|
3650 | - xml_parser_free($parser); |
|
3651 | - return false; |
|
3652 | - } |
|
3653 | - xml_parser_free($parser); |
|
3654 | - if ($GLOBALS['_xh']['isf'] > 1) // test that $GLOBALS['_xh']['value'] is an obj, too??? |
|
3655 | - { |
|
3656 | - error_log($GLOBALS['_xh']['isf_reason']); |
|
3657 | - return false; |
|
3658 | - } |
|
3659 | - switch ($GLOBALS['_xh']['rt']) |
|
3660 | - { |
|
3661 | - case 'methodresponse': |
|
3662 | - $v =& $GLOBALS['_xh']['value']; |
|
3663 | - if ($GLOBALS['_xh']['isf'] == 1) |
|
3664 | - { |
|
3665 | - $vc = $v->structmem('faultCode'); |
|
3666 | - $vs = $v->structmem('faultString'); |
|
3667 | - $r = new xmlrpcresp(0, $vc->scalarval(), $vs->scalarval()); |
|
3668 | - } |
|
3669 | - else |
|
3670 | - { |
|
3671 | - $r = new xmlrpcresp($v); |
|
3672 | - } |
|
3673 | - return $r; |
|
3674 | - case 'methodcall': |
|
3675 | - $m = new xmlrpcmsg($GLOBALS['_xh']['method']); |
|
3676 | - for($i=0; $i < count($GLOBALS['_xh']['params']); $i++) |
|
3677 | - { |
|
3678 | - $m->addParam($GLOBALS['_xh']['params'][$i]); |
|
3679 | - } |
|
3680 | - return $m; |
|
3681 | - case 'value': |
|
3682 | - return $GLOBALS['_xh']['value']; |
|
3683 | - default: |
|
3684 | - return false; |
|
3685 | - } |
|
3686 | - } |
|
3687 | - |
|
3688 | - /** |
|
3689 | - * decode a string that is encoded w/ "chunked" transfer encoding |
|
3690 | - * as defined in rfc2068 par. 19.4.6 |
|
3691 | - * code shamelessly stolen from nusoap library by Dietrich Ayala |
|
3692 | - * |
|
3693 | - * @param string $buffer the string to be decoded |
|
3694 | - * @return string |
|
3695 | - */ |
|
3696 | - function decode_chunked($buffer) |
|
3697 | - { |
|
3698 | - // length := 0 |
|
3699 | - $length = 0; |
|
3700 | - $new = ''; |
|
3701 | - |
|
3702 | - // read chunk-size, chunk-extension (if any) and crlf |
|
3703 | - // get the position of the linebreak |
|
3704 | - $chunkend = strpos($buffer,"\r\n") + 2; |
|
3705 | - $temp = substr($buffer,0,$chunkend); |
|
3706 | - $chunk_size = hexdec( trim($temp) ); |
|
3707 | - $chunkstart = $chunkend; |
|
3708 | - while($chunk_size > 0) |
|
3709 | - { |
|
3710 | - $chunkend = strpos($buffer, "\r\n", $chunkstart + $chunk_size); |
|
3711 | - |
|
3712 | - // just in case we got a broken connection |
|
3713 | - if($chunkend == false) |
|
3714 | - { |
|
3715 | - $chunk = substr($buffer,$chunkstart); |
|
3716 | - // append chunk-data to entity-body |
|
3717 | - $new .= $chunk; |
|
3718 | - $length += strlen($chunk); |
|
3719 | - break; |
|
3720 | - } |
|
3721 | - |
|
3722 | - // read chunk-data and crlf |
|
3723 | - $chunk = substr($buffer,$chunkstart,$chunkend-$chunkstart); |
|
3724 | - // append chunk-data to entity-body |
|
3725 | - $new .= $chunk; |
|
3726 | - // length := length + chunk-size |
|
3727 | - $length += strlen($chunk); |
|
3728 | - // read chunk-size and crlf |
|
3729 | - $chunkstart = $chunkend + 2; |
|
3730 | - |
|
3731 | - $chunkend = strpos($buffer,"\r\n",$chunkstart)+2; |
|
3732 | - if($chunkend == false) |
|
3733 | - { |
|
3734 | - break; //just in case we got a broken connection |
|
3735 | - } |
|
3736 | - $temp = substr($buffer,$chunkstart,$chunkend-$chunkstart); |
|
3737 | - $chunk_size = hexdec( trim($temp) ); |
|
3738 | - $chunkstart = $chunkend; |
|
3739 | - } |
|
3740 | - return $new; |
|
3741 | - } |
|
3742 | - |
|
3743 | - /** |
|
3744 | - * xml charset encoding guessing helper function. |
|
3745 | - * Tries to determine the charset encoding of an XML chunk received over HTTP. |
|
3746 | - * NB: according to the spec (RFC 3023), if text/xml content-type is received over HTTP without a content-type, |
|
3747 | - * we SHOULD assume it is strictly US-ASCII. But we try to be more tolerant of unconforming (legacy?) clients/servers, |
|
3748 | - * which will be most probably using UTF-8 anyway... |
|
3749 | - * |
|
3750 | - * @param string $httpheader the http Content-type header |
|
3751 | - * @param string $xmlchunk xml content buffer |
|
3752 | - * @param string $encoding_prefs comma separated list of character encodings to be used as default (when mb extension is enabled) |
|
3753 | - * @return string |
|
3754 | - * |
|
3755 | - * @todo explore usage of mb_http_input(): does it detect http headers + post data? if so, use it instead of hand-detection!!! |
|
3756 | - */ |
|
3757 | - function guess_encoding($httpheader='', $xmlchunk='', $encoding_prefs=null) |
|
3758 | - { |
|
3759 | - // discussion: see http://www.yale.edu/pclt/encoding/ |
|
3760 | - // 1 - test if encoding is specified in HTTP HEADERS |
|
3761 | - |
|
3762 | - //Details: |
|
3763 | - // LWS: (\13\10)?( |\t)+ |
|
3764 | - // token: (any char but excluded stuff)+ |
|
3765 | - // quoted string: " (any char but double quotes and cointrol chars)* " |
|
3766 | - // header: Content-type = ...; charset=value(; ...)* |
|
3767 | - // where value is of type token, no LWS allowed between 'charset' and value |
|
3768 | - // Note: we do not check for invalid chars in VALUE: |
|
3769 | - // this had better be done using pure ereg as below |
|
3770 | - // Note 2: we might be removing whitespace/tabs that ought to be left in if |
|
3771 | - // the received charset is a quoted string. But nobody uses such charset names... |
|
3772 | - |
|
3773 | - /// @todo this test will pass if ANY header has charset specification, not only Content-Type. Fix it? |
|
3774 | - $matches = array(); |
|
3775 | - if(preg_match('/;\s*charset\s*=([^;]+)/i', $httpheader, $matches)) |
|
3776 | - { |
|
3777 | - return strtoupper(trim($matches[1], " \t\"")); |
|
3778 | - } |
|
3779 | - |
|
3780 | - // 2 - scan the first bytes of the data for a UTF-16 (or other) BOM pattern |
|
3781 | - // (source: http://www.w3.org/TR/2000/REC-xml-20001006) |
|
3782 | - // NOTE: actually, according to the spec, even if we find the BOM and determine |
|
3783 | - // an encoding, we should check if there is an encoding specified |
|
3784 | - // in the xml declaration, and verify if they match. |
|
3785 | - /// @todo implement check as described above? |
|
3786 | - /// @todo implement check for first bytes of string even without a BOM? (It sure looks harder than for cases WITH a BOM) |
|
3787 | - if(preg_match('/^(\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\x00\x00\xFF\xFE|\xFE\xFF\x00\x00)/', $xmlchunk)) |
|
3788 | - { |
|
3789 | - return 'UCS-4'; |
|
3790 | - } |
|
3791 | - elseif(preg_match('/^(\xFE\xFF|\xFF\xFE)/', $xmlchunk)) |
|
3792 | - { |
|
3793 | - return 'UTF-16'; |
|
3794 | - } |
|
3795 | - elseif(preg_match('/^(\xEF\xBB\xBF)/', $xmlchunk)) |
|
3796 | - { |
|
3797 | - return 'UTF-8'; |
|
3798 | - } |
|
3799 | - |
|
3800 | - // 3 - test if encoding is specified in the xml declaration |
|
3801 | - // Details: |
|
3802 | - // SPACE: (#x20 | #x9 | #xD | #xA)+ === [ \x9\xD\xA]+ |
|
3803 | - // EQ: SPACE?=SPACE? === [ \x9\xD\xA]*=[ \x9\xD\xA]* |
|
3804 | - if (preg_match('/^<\?xml\s+version\s*=\s*'. "((?:\"[a-zA-Z0-9_.:-]+\")|(?:'[a-zA-Z0-9_.:-]+'))". |
|
3805 | - '\s+encoding\s*=\s*' . "((?:\"[A-Za-z][A-Za-z0-9._-]*\")|(?:'[A-Za-z][A-Za-z0-9._-]*'))/", |
|
3806 | - $xmlchunk, $matches)) |
|
3807 | - { |
|
3808 | - return strtoupper(substr($matches[2], 1, -1)); |
|
3809 | - } |
|
3810 | - |
|
3811 | - // 4 - if mbstring is available, let it do the guesswork |
|
3812 | - // NB: we favour finding an encoding that is compatible with what we can process |
|
3813 | - if(extension_loaded('mbstring')) |
|
3814 | - { |
|
3815 | - if($encoding_prefs) |
|
3816 | - { |
|
3817 | - $enc = mb_detect_encoding($xmlchunk, $encoding_prefs); |
|
3818 | - } |
|
3819 | - else |
|
3820 | - { |
|
3821 | - $enc = mb_detect_encoding($xmlchunk); |
|
3822 | - } |
|
3823 | - // NB: mb_detect likes to call it ascii, xml parser likes to call it US_ASCII... |
|
3824 | - // IANA also likes better US-ASCII, so go with it |
|
3825 | - if($enc == 'ASCII') |
|
3826 | - { |
|
3827 | - $enc = 'US-'.$enc; |
|
3828 | - } |
|
3829 | - return $enc; |
|
3830 | - } |
|
3831 | - else |
|
3832 | - { |
|
3833 | - // no encoding specified: as per HTTP1.1 assume it is iso-8859-1? |
|
3834 | - // Both RFC 2616 (HTTP 1.1) and 1945 (HTTP 1.0) clearly state that for text/xxx content types |
|
3835 | - // this should be the standard. And we should be getting text/xml as request and response. |
|
3836 | - // BUT we have to be backward compatible with the lib, which always used UTF-8 as default... |
|
3837 | - return $GLOBALS['xmlrpc_defencoding']; |
|
3838 | - } |
|
3839 | - } |
|
3840 | - |
|
3841 | - /** |
|
3842 | - * Helper function: checks if an xml chunk as a charset declaration (BOM or in the xml declaration) |
|
3843 | - * |
|
3844 | - * @param string $xmlChunk |
|
3845 | - * @return bool |
|
3846 | - */ |
|
3847 | - function has_encoding($xmlChunk) |
|
3848 | - { |
|
3849 | - // scan the first bytes of the data for a UTF-16 (or other) BOM pattern |
|
3850 | - // (source: http://www.w3.org/TR/2000/REC-xml-20001006) |
|
3851 | - if (preg_match('/^(\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\x00\x00\xFF\xFE|\xFE\xFF\x00\x00)/', $xmlChunk)) |
|
3852 | - { |
|
3853 | - return true; |
|
3854 | - } |
|
3855 | - elseif (preg_match('/^(\xFE\xFF|\xFF\xFE)/', $xmlChunk)) |
|
3856 | - { |
|
3857 | - return true; |
|
3858 | - } |
|
3859 | - elseif (preg_match('/^(\xEF\xBB\xBF)/', $xmlChunk)) |
|
3860 | - { |
|
3861 | - return true; |
|
3862 | - } |
|
3863 | - |
|
3864 | - // test if encoding is specified in the xml declaration |
|
3865 | - // Details: |
|
3866 | - // SPACE: (#x20 | #x9 | #xD | #xA)+ === [ \x9\xD\xA]+ |
|
3867 | - // EQ: SPACE?=SPACE? === [ \x9\xD\xA]*=[ \x9\xD\xA]* |
|
3868 | - if (preg_match('/^<\?xml\s+version\s*=\s*' . "((?:\"[a-zA-Z0-9_.:-]+\")|(?:'[a-zA-Z0-9_.:-]+'))" . |
|
3869 | - '\s+encoding\s*=\s*' . "((?:\"[A-Za-z][A-Za-z0-9._-]*\")|(?:'[A-Za-z][A-Za-z0-9._-]*'))/", |
|
3870 | - $xmlChunk, $matches)) |
|
3871 | - { |
|
3872 | - return true; |
|
3873 | - } |
|
3874 | - |
|
3875 | - return false; |
|
3876 | - } |
|
3877 | - |
|
3878 | - /** |
|
3879 | - * Checks if a given charset encoding is present in a list of encodings or |
|
3880 | - * if it is a valid subset of any encoding in the list |
|
3881 | - * @param string $encoding charset to be tested |
|
3882 | - * @param mixed $validlist comma separated list of valid charsets (or array of charsets) |
|
3883 | - * @return bool |
|
3884 | - */ |
|
3885 | - function is_valid_charset($encoding, $validlist) |
|
3886 | - { |
|
3887 | - $charset_supersets = array( |
|
3888 | - 'US-ASCII' => array ('ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', |
|
3889 | - 'ISO-8859-5', 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', |
|
3890 | - 'ISO-8859-9', 'ISO-8859-10', 'ISO-8859-11', 'ISO-8859-12', |
|
3891 | - 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'UTF-8', |
|
3892 | - 'EUC-JP', 'EUC-', 'EUC-KR', 'EUC-CN') |
|
3893 | - ); |
|
3894 | - if (is_string($validlist)) |
|
3895 | - $validlist = explode(',', $validlist); |
|
3896 | - if (@in_array(strtoupper($encoding), $validlist)) |
|
3897 | - return true; |
|
3898 | - else |
|
3899 | - { |
|
3900 | - if (array_key_exists($encoding, $charset_supersets)) |
|
3901 | - foreach ($validlist as $allowed) |
|
3902 | - if (in_array($allowed, $charset_supersets[$encoding])) |
|
3903 | - return true; |
|
3904 | - return false; |
|
3905 | - } |
|
3906 | - } |
|
2833 | + } |
|
2834 | + } |
|
2835 | + |
|
2836 | + /** |
|
2837 | + * @deprecated |
|
2838 | + */ |
|
2839 | + function xmlrpcval($val=-1, $type='') |
|
2840 | + { |
|
2841 | + self::__construct($val, $type); |
|
2842 | + } |
|
2843 | + |
|
2844 | + /** |
|
2845 | + * Add a single php value to an (unitialized) xmlrpcval |
|
2846 | + * @param mixed $val |
|
2847 | + * @param string $type |
|
2848 | + * @return int 1 or 0 on failure |
|
2849 | + */ |
|
2850 | + function addScalar($val, $type='string') |
|
2851 | + { |
|
2852 | + $typeof=@$GLOBALS['xmlrpcTypes'][$type]; |
|
2853 | + if($typeof!=1) |
|
2854 | + { |
|
2855 | + error_log("XML-RPC: ".__METHOD__.": not a scalar type ($type)"); |
|
2856 | + return 0; |
|
2857 | + } |
|
2858 | + |
|
2859 | + // coerce booleans into correct values |
|
2860 | + // NB: we should either do it for datetimes, integers and doubles, too, |
|
2861 | + // or just plain remove this check, implemented on booleans only... |
|
2862 | + if($type==$GLOBALS['xmlrpcBoolean']) |
|
2863 | + { |
|
2864 | + if(strcasecmp($val,'true')==0 || $val==1 || ($val==true && strcasecmp($val,'false'))) |
|
2865 | + { |
|
2866 | + $val=true; |
|
2867 | + } |
|
2868 | + else |
|
2869 | + { |
|
2870 | + $val=false; |
|
2871 | + } |
|
2872 | + } |
|
2873 | + |
|
2874 | + switch($this->mytype) |
|
2875 | + { |
|
2876 | + case 1: |
|
2877 | + error_log('XML-RPC: '.__METHOD__.': scalar xmlrpcval can have only one value'); |
|
2878 | + return 0; |
|
2879 | + case 3: |
|
2880 | + error_log('XML-RPC: '.__METHOD__.': cannot add anonymous scalar to struct xmlrpcval'); |
|
2881 | + return 0; |
|
2882 | + case 2: |
|
2883 | + // we're adding a scalar value to an array here |
|
2884 | + //$ar=$this->me['array']; |
|
2885 | + //$ar[]=new xmlrpcval($val, $type); |
|
2886 | + //$this->me['array']=$ar; |
|
2887 | + // Faster (?) avoid all the costly array-copy-by-val done here... |
|
2888 | + $this->me['array'][]=new xmlrpcval($val, $type); |
|
2889 | + return 1; |
|
2890 | + default: |
|
2891 | + // a scalar, so set the value and remember we're scalar |
|
2892 | + $this->me[$type]=$val; |
|
2893 | + $this->mytype=$typeof; |
|
2894 | + return 1; |
|
2895 | + } |
|
2896 | + } |
|
2897 | + |
|
2898 | + /** |
|
2899 | + * Add an array of xmlrpcval objects to an xmlrpcval |
|
2900 | + * @param array $vals |
|
2901 | + * @return int 1 or 0 on failure |
|
2902 | + * @access public |
|
2903 | + * |
|
2904 | + * @todo add some checking for $vals to be an array of xmlrpcvals? |
|
2905 | + */ |
|
2906 | + function addArray($vals) |
|
2907 | + { |
|
2908 | + if($this->mytype==0) |
|
2909 | + { |
|
2910 | + $this->mytype=$GLOBALS['xmlrpcTypes']['array']; |
|
2911 | + $this->me['array']=$vals; |
|
2912 | + return 1; |
|
2913 | + } |
|
2914 | + elseif($this->mytype==2) |
|
2915 | + { |
|
2916 | + // we're adding to an array here |
|
2917 | + $this->me['array'] = array_merge($this->me['array'], $vals); |
|
2918 | + return 1; |
|
2919 | + } |
|
2920 | + else |
|
2921 | + { |
|
2922 | + error_log('XML-RPC: '.__METHOD__.': already initialized as a [' . $this->kindOf() . ']'); |
|
2923 | + return 0; |
|
2924 | + } |
|
2925 | + } |
|
2926 | + |
|
2927 | + /** |
|
2928 | + * Add an array of named xmlrpcval objects to an xmlrpcval |
|
2929 | + * @param array $vals |
|
2930 | + * @return int 1 or 0 on failure |
|
2931 | + * @access public |
|
2932 | + * |
|
2933 | + * @todo add some checking for $vals to be an array? |
|
2934 | + */ |
|
2935 | + function addStruct($vals) |
|
2936 | + { |
|
2937 | + if($this->mytype==0) |
|
2938 | + { |
|
2939 | + $this->mytype=$GLOBALS['xmlrpcTypes']['struct']; |
|
2940 | + $this->me['struct']=$vals; |
|
2941 | + return 1; |
|
2942 | + } |
|
2943 | + elseif($this->mytype==3) |
|
2944 | + { |
|
2945 | + // we're adding to a struct here |
|
2946 | + $this->me['struct'] = array_merge($this->me['struct'], $vals); |
|
2947 | + return 1; |
|
2948 | + } |
|
2949 | + else |
|
2950 | + { |
|
2951 | + error_log('XML-RPC: '.__METHOD__.': already initialized as a [' . $this->kindOf() . ']'); |
|
2952 | + return 0; |
|
2953 | + } |
|
2954 | + } |
|
2955 | + |
|
2956 | + // poor man's version of print_r ??? |
|
2957 | + // DEPRECATED! |
|
2958 | + function dump($ar) |
|
2959 | + { |
|
2960 | + foreach($ar as $key => $val) |
|
2961 | + { |
|
2962 | + echo "$key => $val<br />"; |
|
2963 | + if($key == 'array') |
|
2964 | + { |
|
2965 | + while(list($key2, $val2) = each($val)) |
|
2966 | + { |
|
2967 | + echo "-- $key2 => $val2<br />"; |
|
2968 | + } |
|
2969 | + } |
|
2970 | + } |
|
2971 | + } |
|
2972 | + |
|
2973 | + /** |
|
2974 | + * Returns a string containing "struct", "array" or "scalar" describing the base type of the value |
|
2975 | + * @return string |
|
2976 | + * @access public |
|
2977 | + */ |
|
2978 | + function kindOf() |
|
2979 | + { |
|
2980 | + switch($this->mytype) |
|
2981 | + { |
|
2982 | + case 3: |
|
2983 | + return 'struct'; |
|
2984 | + break; |
|
2985 | + case 2: |
|
2986 | + return 'array'; |
|
2987 | + break; |
|
2988 | + case 1: |
|
2989 | + return 'scalar'; |
|
2990 | + break; |
|
2991 | + default: |
|
2992 | + return 'undef'; |
|
2993 | + } |
|
2994 | + } |
|
2995 | + |
|
2996 | + /** |
|
2997 | + * @access private |
|
2998 | + */ |
|
2999 | + function serializedata($typ, $val, $charset_encoding='') |
|
3000 | + { |
|
3001 | + $rs=''; |
|
3002 | + switch(@$GLOBALS['xmlrpcTypes'][$typ]) |
|
3003 | + { |
|
3004 | + case 1: |
|
3005 | + switch($typ) |
|
3006 | + { |
|
3007 | + case $GLOBALS['xmlrpcBase64']: |
|
3008 | + $rs.="<${typ}>" . base64_encode($val) . "</${typ}>"; |
|
3009 | + break; |
|
3010 | + case $GLOBALS['xmlrpcBoolean']: |
|
3011 | + $rs.="<${typ}>" . ($val ? '1' : '0') . "</${typ}>"; |
|
3012 | + break; |
|
3013 | + case $GLOBALS['xmlrpcString']: |
|
3014 | + // G. Giunta 2005/2/13: do NOT use htmlentities, since |
|
3015 | + // it will produce named html entities, which are invalid xml |
|
3016 | + $rs.="<${typ}>" . xmlrpc_encode_entitites($val, $GLOBALS['xmlrpc_internalencoding'], $charset_encoding). "</${typ}>"; |
|
3017 | + break; |
|
3018 | + case $GLOBALS['xmlrpcInt']: |
|
3019 | + case $GLOBALS['xmlrpcI4']: |
|
3020 | + $rs.="<${typ}>".(int)$val."</${typ}>"; |
|
3021 | + break; |
|
3022 | + case $GLOBALS['xmlrpcDouble']: |
|
3023 | + // avoid using standard conversion of float to string because it is locale-dependent, |
|
3024 | + // and also because the xmlrpc spec forbids exponential notation. |
|
3025 | + // sprintf('%F') could be most likely ok but it fails eg. on 2e-14. |
|
3026 | + // The code below tries its best at keeping max precision while avoiding exp notation, |
|
3027 | + // but there is of course no limit in the number of decimal places to be used... |
|
3028 | + $rs.="<${typ}>".preg_replace('/\\.?0+$/','',number_format((double)$val, 128, '.', ''))."</${typ}>"; |
|
3029 | + break; |
|
3030 | + case $GLOBALS['xmlrpcDateTime']: |
|
3031 | + if (is_string($val)) |
|
3032 | + { |
|
3033 | + $rs.="<${typ}>${val}</${typ}>"; |
|
3034 | + } |
|
3035 | + else if(is_a($val, 'DateTime')) |
|
3036 | + { |
|
3037 | + $rs.="<${typ}>".$val->format('Ymd\TH:i:s')."</${typ}>"; |
|
3038 | + } |
|
3039 | + else if(is_int($val)) |
|
3040 | + { |
|
3041 | + $rs.="<${typ}>".strftime("%Y%m%dT%H:%M:%S", $val)."</${typ}>"; |
|
3042 | + } |
|
3043 | + else |
|
3044 | + { |
|
3045 | + // not really a good idea here: but what shall we output anyway? left for backward compat... |
|
3046 | + $rs.="<${typ}>${val}</${typ}>"; |
|
3047 | + } |
|
3048 | + break; |
|
3049 | + case $GLOBALS['xmlrpcNull']: |
|
3050 | + if ($GLOBALS['xmlrpc_null_apache_encoding']) |
|
3051 | + { |
|
3052 | + $rs.="<ex:nil/>"; |
|
3053 | + } |
|
3054 | + else |
|
3055 | + { |
|
3056 | + $rs.="<nil/>"; |
|
3057 | + } |
|
3058 | + break; |
|
3059 | + default: |
|
3060 | + // no standard type value should arrive here, but provide a possibility |
|
3061 | + // for xmlrpcvals of unknown type... |
|
3062 | + $rs.="<${typ}>${val}</${typ}>"; |
|
3063 | + } |
|
3064 | + break; |
|
3065 | + case 3: |
|
3066 | + // struct |
|
3067 | + if ($this->_php_class) |
|
3068 | + { |
|
3069 | + $rs.='<struct php_class="' . $this->_php_class . "\">\n"; |
|
3070 | + } |
|
3071 | + else |
|
3072 | + { |
|
3073 | + $rs.="<struct>\n"; |
|
3074 | + } |
|
3075 | + foreach($val as $key2 => $val2) |
|
3076 | + { |
|
3077 | + $rs.='<member><name>'.xmlrpc_encode_entitites($key2, $GLOBALS['xmlrpc_internalencoding'], $charset_encoding)."</name>\n"; |
|
3078 | + //$rs.=$this->serializeval($val2); |
|
3079 | + $rs.=$val2->serialize($charset_encoding); |
|
3080 | + $rs.="</member>\n"; |
|
3081 | + } |
|
3082 | + $rs.='</struct>'; |
|
3083 | + break; |
|
3084 | + case 2: |
|
3085 | + // array |
|
3086 | + $rs.="<array>\n<data>\n"; |
|
3087 | + for($i=0; $i<count($val); $i++) |
|
3088 | + { |
|
3089 | + //$rs.=$this->serializeval($val[$i]); |
|
3090 | + $rs.=$val[$i]->serialize($charset_encoding); |
|
3091 | + } |
|
3092 | + $rs.="</data>\n</array>"; |
|
3093 | + break; |
|
3094 | + default: |
|
3095 | + break; |
|
3096 | + } |
|
3097 | + return $rs; |
|
3098 | + } |
|
3099 | + |
|
3100 | + /** |
|
3101 | + * Returns xml representation of the value. XML prologue not included |
|
3102 | + * @param string $charset_encoding the charset to be used for serialization. if null, US-ASCII is assumed |
|
3103 | + * @return string |
|
3104 | + * @access public |
|
3105 | + */ |
|
3106 | + function serialize($charset_encoding='') |
|
3107 | + { |
|
3108 | + // add check? slower, but helps to avoid recursion in serializing broken xmlrpcvals... |
|
3109 | + //if (is_object($o) && (get_class($o) == 'xmlrpcval' || is_subclass_of($o, 'xmlrpcval'))) |
|
3110 | + //{ |
|
3111 | + reset($this->me); |
|
3112 | + list($typ, $val) = each($this->me); |
|
3113 | + return '<value>' . $this->serializedata($typ, $val, $charset_encoding) . "</value>\n"; |
|
3114 | + //} |
|
3115 | + } |
|
3116 | + |
|
3117 | + // DEPRECATED |
|
3118 | + function serializeval($o) |
|
3119 | + { |
|
3120 | + // add check? slower, but helps to avoid recursion in serializing broken xmlrpcvals... |
|
3121 | + //if (is_object($o) && (get_class($o) == 'xmlrpcval' || is_subclass_of($o, 'xmlrpcval'))) |
|
3122 | + //{ |
|
3123 | + $ar=$o->me; |
|
3124 | + reset($ar); |
|
3125 | + list($typ, $val) = each($ar); |
|
3126 | + return '<value>' . $this->serializedata($typ, $val) . "</value>\n"; |
|
3127 | + //} |
|
3128 | + } |
|
3129 | + |
|
3130 | + /** |
|
3131 | + * Checks whether a struct member with a given name is present. |
|
3132 | + * Works only on xmlrpcvals of type struct. |
|
3133 | + * @param string $m the name of the struct member to be looked up |
|
3134 | + * @return boolean |
|
3135 | + * @access public |
|
3136 | + */ |
|
3137 | + function structmemexists($m) |
|
3138 | + { |
|
3139 | + return array_key_exists($m, $this->me['struct']); |
|
3140 | + } |
|
3141 | + |
|
3142 | + /** |
|
3143 | + * Returns the value of a given struct member (an xmlrpcval object in itself). |
|
3144 | + * Will raise a php warning if struct member of given name does not exist |
|
3145 | + * @param string $m the name of the struct member to be looked up |
|
3146 | + * @return xmlrpcval |
|
3147 | + * @access public |
|
3148 | + */ |
|
3149 | + function structmem($m) |
|
3150 | + { |
|
3151 | + return $this->me['struct'][$m]; |
|
3152 | + } |
|
3153 | + |
|
3154 | + /** |
|
3155 | + * Reset internal pointer for xmlrpcvals of type struct. |
|
3156 | + * @access public |
|
3157 | + */ |
|
3158 | + function structreset() |
|
3159 | + { |
|
3160 | + reset($this->me['struct']); |
|
3161 | + } |
|
3162 | + |
|
3163 | + /** |
|
3164 | + * Return next member element for xmlrpcvals of type struct. |
|
3165 | + * @return xmlrpcval |
|
3166 | + * @access public |
|
3167 | + */ |
|
3168 | + function structeach() |
|
3169 | + { |
|
3170 | + return each($this->me['struct']); |
|
3171 | + } |
|
3172 | + |
|
3173 | + // DEPRECATED! this code looks like it is very fragile and has not been fixed |
|
3174 | + // for a long long time. Shall we remove it for 2.0? |
|
3175 | + function getval() |
|
3176 | + { |
|
3177 | + // UNSTABLE |
|
3178 | + reset($this->me); |
|
3179 | + list($a,$b)=each($this->me); |
|
3180 | + // contributed by I Sofer, 2001-03-24 |
|
3181 | + // add support for nested arrays to scalarval |
|
3182 | + // i've created a new method here, so as to |
|
3183 | + // preserve back compatibility |
|
3184 | + |
|
3185 | + if(is_array($b)) |
|
3186 | + { |
|
3187 | + @reset($b); |
|
3188 | + while(list($id,$cont) = @each($b)) |
|
3189 | + { |
|
3190 | + $b[$id] = $cont->scalarval(); |
|
3191 | + } |
|
3192 | + } |
|
3193 | + |
|
3194 | + // add support for structures directly encoding php objects |
|
3195 | + if(is_object($b)) |
|
3196 | + { |
|
3197 | + $t = get_object_vars($b); |
|
3198 | + @reset($t); |
|
3199 | + while(list($id,$cont) = @each($t)) |
|
3200 | + { |
|
3201 | + $t[$id] = $cont->scalarval(); |
|
3202 | + } |
|
3203 | + @reset($t); |
|
3204 | + while(list($id,$cont) = @each($t)) |
|
3205 | + { |
|
3206 | + @$b->$id = $cont; |
|
3207 | + } |
|
3208 | + } |
|
3209 | + // end contrib |
|
3210 | + return $b; |
|
3211 | + } |
|
3212 | + |
|
3213 | + /** |
|
3214 | + * Returns the value of a scalar xmlrpcval |
|
3215 | + * @return mixed |
|
3216 | + * @access public |
|
3217 | + */ |
|
3218 | + function scalarval() |
|
3219 | + { |
|
3220 | + reset($this->me); |
|
3221 | + list(,$b)=each($this->me); |
|
3222 | + return $b; |
|
3223 | + } |
|
3224 | + |
|
3225 | + /** |
|
3226 | + * Returns the type of the xmlrpcval. |
|
3227 | + * For integers, 'int' is always returned in place of 'i4' |
|
3228 | + * @return string |
|
3229 | + * @access public |
|
3230 | + */ |
|
3231 | + function scalartyp() |
|
3232 | + { |
|
3233 | + reset($this->me); |
|
3234 | + list($a,)=each($this->me); |
|
3235 | + if($a==$GLOBALS['xmlrpcI4']) |
|
3236 | + { |
|
3237 | + $a=$GLOBALS['xmlrpcInt']; |
|
3238 | + } |
|
3239 | + return $a; |
|
3240 | + } |
|
3241 | + |
|
3242 | + /** |
|
3243 | + * Returns the m-th member of an xmlrpcval of struct type |
|
3244 | + * @param integer $m the index of the value to be retrieved (zero based) |
|
3245 | + * @return xmlrpcval |
|
3246 | + * @access public |
|
3247 | + */ |
|
3248 | + function arraymem($m) |
|
3249 | + { |
|
3250 | + return $this->me['array'][$m]; |
|
3251 | + } |
|
3252 | + |
|
3253 | + /** |
|
3254 | + * Returns the number of members in an xmlrpcval of array type |
|
3255 | + * @return integer |
|
3256 | + * @access public |
|
3257 | + */ |
|
3258 | + function arraysize() |
|
3259 | + { |
|
3260 | + return count($this->me['array']); |
|
3261 | + } |
|
3262 | + |
|
3263 | + /** |
|
3264 | + * Returns the number of members in an xmlrpcval of struct type |
|
3265 | + * @return integer |
|
3266 | + * @access public |
|
3267 | + */ |
|
3268 | + function structsize() |
|
3269 | + { |
|
3270 | + return count($this->me['struct']); |
|
3271 | + } |
|
3272 | + } |
|
3273 | + |
|
3274 | + |
|
3275 | + // date helpers |
|
3276 | + |
|
3277 | + /** |
|
3278 | + * Given a timestamp, return the corresponding ISO8601 encoded string. |
|
3279 | + * |
|
3280 | + * Really, timezones ought to be supported |
|
3281 | + * but the XML-RPC spec says: |
|
3282 | + * |
|
3283 | + * "Don't assume a timezone. It should be specified by the server in its |
|
3284 | + * documentation what assumptions it makes about timezones." |
|
3285 | + * |
|
3286 | + * These routines always assume localtime unless |
|
3287 | + * $utc is set to 1, in which case UTC is assumed |
|
3288 | + * and an adjustment for locale is made when encoding |
|
3289 | + * |
|
3290 | + * @param int $timet (timestamp) |
|
3291 | + * @param int $utc (0 or 1) |
|
3292 | + * @return string |
|
3293 | + */ |
|
3294 | + function iso8601_encode($timet, $utc=0) |
|
3295 | + { |
|
3296 | + if(!$utc) |
|
3297 | + { |
|
3298 | + $t=strftime("%Y%m%dT%H:%M:%S", $timet); |
|
3299 | + } |
|
3300 | + else |
|
3301 | + { |
|
3302 | + if(function_exists('gmstrftime')) |
|
3303 | + { |
|
3304 | + // gmstrftime doesn't exist in some versions |
|
3305 | + // of PHP |
|
3306 | + $t=gmstrftime("%Y%m%dT%H:%M:%S", $timet); |
|
3307 | + } |
|
3308 | + else |
|
3309 | + { |
|
3310 | + $t=strftime("%Y%m%dT%H:%M:%S", $timet-date('Z')); |
|
3311 | + } |
|
3312 | + } |
|
3313 | + return $t; |
|
3314 | + } |
|
3315 | + |
|
3316 | + /** |
|
3317 | + * Given an ISO8601 date string, return a timet in the localtime, or UTC |
|
3318 | + * @param string $idate |
|
3319 | + * @param int $utc either 0 or 1 |
|
3320 | + * @return int (datetime) |
|
3321 | + */ |
|
3322 | + function iso8601_decode($idate, $utc=0) |
|
3323 | + { |
|
3324 | + $t=0; |
|
3325 | + if(preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})/', $idate, $regs)) |
|
3326 | + { |
|
3327 | + if($utc) |
|
3328 | + { |
|
3329 | + $t=gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]); |
|
3330 | + } |
|
3331 | + else |
|
3332 | + { |
|
3333 | + $t=mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]); |
|
3334 | + } |
|
3335 | + } |
|
3336 | + return $t; |
|
3337 | + } |
|
3338 | + |
|
3339 | + /** |
|
3340 | + * Takes an xmlrpc value in PHP xmlrpcval object format and translates it into native PHP types. |
|
3341 | + * |
|
3342 | + * Works with xmlrpc message objects as input, too. |
|
3343 | + * |
|
3344 | + * Given proper options parameter, can rebuild generic php object instances |
|
3345 | + * (provided those have been encoded to xmlrpc format using a corresponding |
|
3346 | + * option in php_xmlrpc_encode()) |
|
3347 | + * PLEASE NOTE that rebuilding php objects involves calling their constructor function. |
|
3348 | + * This means that the remote communication end can decide which php code will |
|
3349 | + * get executed on your server, leaving the door possibly open to 'php-injection' |
|
3350 | + * style of attacks (provided you have some classes defined on your server that |
|
3351 | + * might wreak havoc if instances are built outside an appropriate context). |
|
3352 | + * Make sure you trust the remote server/client before eanbling this! |
|
3353 | + * |
|
3354 | + * @author Dan Libby ([email protected]) |
|
3355 | + * |
|
3356 | + * @param xmlrpcval $xmlrpc_val |
|
3357 | + * @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 |
|
3358 | + * @return mixed |
|
3359 | + */ |
|
3360 | + function php_xmlrpc_decode($xmlrpc_val, $options=array()) |
|
3361 | + { |
|
3362 | + switch($xmlrpc_val->kindOf()) |
|
3363 | + { |
|
3364 | + case 'scalar': |
|
3365 | + if (in_array('extension_api', $options)) |
|
3366 | + { |
|
3367 | + reset($xmlrpc_val->me); |
|
3368 | + list($typ,$val) = each($xmlrpc_val->me); |
|
3369 | + switch ($typ) |
|
3370 | + { |
|
3371 | + case 'dateTime.iso8601': |
|
3372 | + $xmlrpc_val->scalar = $val; |
|
3373 | + $xmlrpc_val->xmlrpc_type = 'datetime'; |
|
3374 | + $xmlrpc_val->timestamp = iso8601_decode($val); |
|
3375 | + return $xmlrpc_val; |
|
3376 | + case 'base64': |
|
3377 | + $xmlrpc_val->scalar = $val; |
|
3378 | + $xmlrpc_val->type = $typ; |
|
3379 | + return $xmlrpc_val; |
|
3380 | + default: |
|
3381 | + return $xmlrpc_val->scalarval(); |
|
3382 | + } |
|
3383 | + } |
|
3384 | + if (in_array('dates_as_objects', $options) && $xmlrpc_val->scalartyp() == 'dateTime.iso8601') |
|
3385 | + { |
|
3386 | + // we return a Datetime object instead of a string |
|
3387 | + // since now the constructor of xmlrpcval accepts safely strings, ints and datetimes, |
|
3388 | + // we cater to all 3 cases here |
|
3389 | + $out = $xmlrpc_val->scalarval(); |
|
3390 | + if (is_string($out)) |
|
3391 | + { |
|
3392 | + $out = strtotime($out); |
|
3393 | + } |
|
3394 | + if (is_int($out)) |
|
3395 | + { |
|
3396 | + $result = new Datetime(); |
|
3397 | + $result->setTimestamp($out); |
|
3398 | + return $result; |
|
3399 | + } |
|
3400 | + elseif (is_a($out, 'Datetime')) |
|
3401 | + { |
|
3402 | + return $out; |
|
3403 | + } |
|
3404 | + } |
|
3405 | + return $xmlrpc_val->scalarval(); |
|
3406 | + case 'array': |
|
3407 | + $size = $xmlrpc_val->arraysize(); |
|
3408 | + $arr = array(); |
|
3409 | + for($i = 0; $i < $size; $i++) |
|
3410 | + { |
|
3411 | + $arr[] = php_xmlrpc_decode($xmlrpc_val->arraymem($i), $options); |
|
3412 | + } |
|
3413 | + return $arr; |
|
3414 | + case 'struct': |
|
3415 | + $xmlrpc_val->structreset(); |
|
3416 | + // If user said so, try to rebuild php objects for specific struct vals. |
|
3417 | + /// @todo should we raise a warning for class not found? |
|
3418 | + // shall we check for proper subclass of xmlrpcval instead of |
|
3419 | + // presence of _php_class to detect what we can do? |
|
3420 | + if (in_array('decode_php_objs', $options) && $xmlrpc_val->_php_class != '' |
|
3421 | + && class_exists($xmlrpc_val->_php_class)) |
|
3422 | + { |
|
3423 | + $obj = @new $xmlrpc_val->_php_class; |
|
3424 | + while(list($key,$value)=$xmlrpc_val->structeach()) |
|
3425 | + { |
|
3426 | + $obj->$key = php_xmlrpc_decode($value, $options); |
|
3427 | + } |
|
3428 | + return $obj; |
|
3429 | + } |
|
3430 | + else |
|
3431 | + { |
|
3432 | + $arr = array(); |
|
3433 | + while(list($key,$value)=$xmlrpc_val->structeach()) |
|
3434 | + { |
|
3435 | + $arr[$key] = php_xmlrpc_decode($value, $options); |
|
3436 | + } |
|
3437 | + return $arr; |
|
3438 | + } |
|
3439 | + case 'msg': |
|
3440 | + $paramcount = $xmlrpc_val->getNumParams(); |
|
3441 | + $arr = array(); |
|
3442 | + for($i = 0; $i < $paramcount; $i++) |
|
3443 | + { |
|
3444 | + $arr[] = php_xmlrpc_decode($xmlrpc_val->getParam($i)); |
|
3445 | + } |
|
3446 | + return $arr; |
|
3447 | + } |
|
3448 | + } |
|
3449 | + |
|
3450 | + // This constant left here only for historical reasons... |
|
3451 | + // it was used to decide if we have to define xmlrpc_encode on our own, but |
|
3452 | + // we do not do it anymore |
|
3453 | + if(function_exists('xmlrpc_decode')) |
|
3454 | + { |
|
3455 | + define('XMLRPC_EPI_ENABLED','1'); |
|
3456 | + } |
|
3457 | + else |
|
3458 | + { |
|
3459 | + define('XMLRPC_EPI_ENABLED','0'); |
|
3460 | + } |
|
3461 | + |
|
3462 | + /** |
|
3463 | + * Takes native php types and encodes them into xmlrpc PHP object format. |
|
3464 | + * It will not re-encode xmlrpcval objects. |
|
3465 | + * |
|
3466 | + * Feature creep -- could support more types via optional type argument |
|
3467 | + * (string => datetime support has been added, ??? => base64 not yet) |
|
3468 | + * |
|
3469 | + * If given a proper options parameter, php object instances will be encoded |
|
3470 | + * into 'special' xmlrpc values, that can later be decoded into php objects |
|
3471 | + * by calling php_xmlrpc_decode() with a corresponding option |
|
3472 | + * |
|
3473 | + * @author Dan Libby ([email protected]) |
|
3474 | + * |
|
3475 | + * @param mixed $php_val the value to be converted into an xmlrpcval object |
|
3476 | + * @param array $options can include 'encode_php_objs', 'auto_dates', 'null_extension' or 'extension_api' |
|
3477 | + * @return xmlrpcval |
|
3478 | + */ |
|
3479 | + function php_xmlrpc_encode($php_val, $options=array()) |
|
3480 | + { |
|
3481 | + $type = gettype($php_val); |
|
3482 | + switch($type) |
|
3483 | + { |
|
3484 | + case 'string': |
|
3485 | + if (in_array('auto_dates', $options) && preg_match('/^[0-9]{8}T[0-9]{2}:[0-9]{2}:[0-9]{2}$/', $php_val)) |
|
3486 | + $xmlrpc_val = new xmlrpcval($php_val, $GLOBALS['xmlrpcDateTime']); |
|
3487 | + else |
|
3488 | + $xmlrpc_val = new xmlrpcval($php_val, $GLOBALS['xmlrpcString']); |
|
3489 | + break; |
|
3490 | + case 'integer': |
|
3491 | + $xmlrpc_val = new xmlrpcval($php_val, $GLOBALS['xmlrpcInt']); |
|
3492 | + break; |
|
3493 | + case 'double': |
|
3494 | + $xmlrpc_val = new xmlrpcval($php_val, $GLOBALS['xmlrpcDouble']); |
|
3495 | + break; |
|
3496 | + // <G_Giunta_2001-02-29> |
|
3497 | + // Add support for encoding/decoding of booleans, since they are supported in PHP |
|
3498 | + case 'boolean': |
|
3499 | + $xmlrpc_val = new xmlrpcval($php_val, $GLOBALS['xmlrpcBoolean']); |
|
3500 | + break; |
|
3501 | + // </G_Giunta_2001-02-29> |
|
3502 | + case 'array': |
|
3503 | + // PHP arrays can be encoded to either xmlrpc structs or arrays, |
|
3504 | + // depending on wheter they are hashes or plain 0..n integer indexed |
|
3505 | + // A shorter one-liner would be |
|
3506 | + // $tmp = array_diff(array_keys($php_val), range(0, count($php_val)-1)); |
|
3507 | + // but execution time skyrockets! |
|
3508 | + $j = 0; |
|
3509 | + $arr = array(); |
|
3510 | + $ko = false; |
|
3511 | + foreach($php_val as $key => $val) |
|
3512 | + { |
|
3513 | + $arr[$key] = php_xmlrpc_encode($val, $options); |
|
3514 | + if(!$ko && $key !== $j) |
|
3515 | + { |
|
3516 | + $ko = true; |
|
3517 | + } |
|
3518 | + $j++; |
|
3519 | + } |
|
3520 | + if($ko) |
|
3521 | + { |
|
3522 | + $xmlrpc_val = new xmlrpcval($arr, $GLOBALS['xmlrpcStruct']); |
|
3523 | + } |
|
3524 | + else |
|
3525 | + { |
|
3526 | + $xmlrpc_val = new xmlrpcval($arr, $GLOBALS['xmlrpcArray']); |
|
3527 | + } |
|
3528 | + break; |
|
3529 | + case 'object': |
|
3530 | + if(is_a($php_val, 'xmlrpcval')) |
|
3531 | + { |
|
3532 | + $xmlrpc_val = $php_val; |
|
3533 | + } |
|
3534 | + else if(is_a($php_val, 'DateTime')) |
|
3535 | + { |
|
3536 | + $xmlrpc_val = new xmlrpcval($php_val->format('Ymd\TH:i:s'), $GLOBALS['xmlrpcStruct']); |
|
3537 | + } |
|
3538 | + else |
|
3539 | + { |
|
3540 | + $arr = array(); |
|
3541 | + reset($php_val); |
|
3542 | + while(list($k,$v) = each($php_val)) |
|
3543 | + { |
|
3544 | + $arr[$k] = php_xmlrpc_encode($v, $options); |
|
3545 | + } |
|
3546 | + $xmlrpc_val = new xmlrpcval($arr, $GLOBALS['xmlrpcStruct']); |
|
3547 | + if (in_array('encode_php_objs', $options)) |
|
3548 | + { |
|
3549 | + // let's save original class name into xmlrpcval: |
|
3550 | + // might be useful later on... |
|
3551 | + $xmlrpc_val->_php_class = get_class($php_val); |
|
3552 | + } |
|
3553 | + } |
|
3554 | + break; |
|
3555 | + case 'NULL': |
|
3556 | + if (in_array('extension_api', $options)) |
|
3557 | + { |
|
3558 | + $xmlrpc_val = new xmlrpcval('', $GLOBALS['xmlrpcString']); |
|
3559 | + } |
|
3560 | + else if (in_array('null_extension', $options)) |
|
3561 | + { |
|
3562 | + $xmlrpc_val = new xmlrpcval('', $GLOBALS['xmlrpcNull']); |
|
3563 | + } |
|
3564 | + else |
|
3565 | + { |
|
3566 | + $xmlrpc_val = new xmlrpcval(); |
|
3567 | + } |
|
3568 | + break; |
|
3569 | + case 'resource': |
|
3570 | + if (in_array('extension_api', $options)) |
|
3571 | + { |
|
3572 | + $xmlrpc_val = new xmlrpcval((int)$php_val, $GLOBALS['xmlrpcInt']); |
|
3573 | + } |
|
3574 | + else |
|
3575 | + { |
|
3576 | + $xmlrpc_val = new xmlrpcval(); |
|
3577 | + } |
|
3578 | + // catch "user function", "unknown type" |
|
3579 | + default: |
|
3580 | + // giancarlo pinerolo <[email protected]> |
|
3581 | + // it has to return |
|
3582 | + // an empty object in case, not a boolean. |
|
3583 | + $xmlrpc_val = new xmlrpcval(); |
|
3584 | + break; |
|
3585 | + } |
|
3586 | + return $xmlrpc_val; |
|
3587 | + } |
|
3588 | + |
|
3589 | + /** |
|
3590 | + * Convert the xml representation of a method response, method request or single |
|
3591 | + * xmlrpc value into the appropriate object (a.k.a. deserialize) |
|
3592 | + * @param string $xml_val |
|
3593 | + * @param array $options |
|
3594 | + * @return mixed false on error, or an instance of either xmlrpcval, xmlrpcmsg or xmlrpcresp |
|
3595 | + */ |
|
3596 | + function php_xmlrpc_decode_xml($xml_val, $options=array()) |
|
3597 | + { |
|
3598 | + $GLOBALS['_xh'] = array(); |
|
3599 | + $GLOBALS['_xh']['ac'] = ''; |
|
3600 | + $GLOBALS['_xh']['stack'] = array(); |
|
3601 | + $GLOBALS['_xh']['valuestack'] = array(); |
|
3602 | + $GLOBALS['_xh']['params'] = array(); |
|
3603 | + $GLOBALS['_xh']['pt'] = array(); |
|
3604 | + $GLOBALS['_xh']['isf'] = 0; |
|
3605 | + $GLOBALS['_xh']['isf_reason'] = ''; |
|
3606 | + $GLOBALS['_xh']['method'] = false; |
|
3607 | + $GLOBALS['_xh']['rt'] = ''; |
|
3608 | + |
|
3609 | + // 'guestimate' encoding |
|
3610 | + $val_encoding = guess_encoding('', $xml_val); |
|
3611 | + |
|
3612 | + // Since parsing will fail if charset is not specified in the xml prologue, |
|
3613 | + // the encoding is not UTF8 and there are non-ascii chars in the text, we try to work round that... |
|
3614 | + // The following code might be better for mb_string enabled installs, but |
|
3615 | + // makes the lib about 200% slower... |
|
3616 | + //if (!is_valid_charset($val_encoding, array('UTF-8'))) |
|
3617 | + if (!in_array($val_encoding, array('UTF-8', 'US-ASCII')) && !has_encoding($xml_val)) { |
|
3618 | + if ($val_encoding == 'ISO-8859-1') { |
|
3619 | + $xml_val = utf8_encode($xml_val); |
|
3620 | + } else { |
|
3621 | + if (extension_loaded('mbstring')) { |
|
3622 | + $xml_val = mb_convert_encoding($xml_val, 'UTF-8', $val_encoding); |
|
3623 | + } else { |
|
3624 | + error_log('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of received request: ' . $val_encoding); |
|
3625 | + } |
|
3626 | + } |
|
3627 | + } |
|
3628 | + |
|
3629 | + $parser = xml_parser_create(); |
|
3630 | + xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true); |
|
3631 | + // What if internal encoding is not in one of the 3 allowed? |
|
3632 | + // we use the broadest one, ie. utf8! |
|
3633 | + if (!in_array($GLOBALS['xmlrpc_internalencoding'], array('UTF-8', 'ISO-8859-1', 'US-ASCII'))) |
|
3634 | + { |
|
3635 | + xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8'); |
|
3636 | + } |
|
3637 | + else |
|
3638 | + { |
|
3639 | + xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $GLOBALS['xmlrpc_internalencoding']); |
|
3640 | + } |
|
3641 | + xml_set_element_handler($parser, 'xmlrpc_se_any', 'xmlrpc_ee'); |
|
3642 | + xml_set_character_data_handler($parser, 'xmlrpc_cd'); |
|
3643 | + xml_set_default_handler($parser, 'xmlrpc_dh'); |
|
3644 | + if(!xml_parse($parser, $xml_val, 1)) |
|
3645 | + { |
|
3646 | + $errstr = sprintf('XML error: %s at line %d, column %d', |
|
3647 | + xml_error_string(xml_get_error_code($parser)), |
|
3648 | + xml_get_current_line_number($parser), xml_get_current_column_number($parser)); |
|
3649 | + error_log($errstr); |
|
3650 | + xml_parser_free($parser); |
|
3651 | + return false; |
|
3652 | + } |
|
3653 | + xml_parser_free($parser); |
|
3654 | + if ($GLOBALS['_xh']['isf'] > 1) // test that $GLOBALS['_xh']['value'] is an obj, too??? |
|
3655 | + { |
|
3656 | + error_log($GLOBALS['_xh']['isf_reason']); |
|
3657 | + return false; |
|
3658 | + } |
|
3659 | + switch ($GLOBALS['_xh']['rt']) |
|
3660 | + { |
|
3661 | + case 'methodresponse': |
|
3662 | + $v =& $GLOBALS['_xh']['value']; |
|
3663 | + if ($GLOBALS['_xh']['isf'] == 1) |
|
3664 | + { |
|
3665 | + $vc = $v->structmem('faultCode'); |
|
3666 | + $vs = $v->structmem('faultString'); |
|
3667 | + $r = new xmlrpcresp(0, $vc->scalarval(), $vs->scalarval()); |
|
3668 | + } |
|
3669 | + else |
|
3670 | + { |
|
3671 | + $r = new xmlrpcresp($v); |
|
3672 | + } |
|
3673 | + return $r; |
|
3674 | + case 'methodcall': |
|
3675 | + $m = new xmlrpcmsg($GLOBALS['_xh']['method']); |
|
3676 | + for($i=0; $i < count($GLOBALS['_xh']['params']); $i++) |
|
3677 | + { |
|
3678 | + $m->addParam($GLOBALS['_xh']['params'][$i]); |
|
3679 | + } |
|
3680 | + return $m; |
|
3681 | + case 'value': |
|
3682 | + return $GLOBALS['_xh']['value']; |
|
3683 | + default: |
|
3684 | + return false; |
|
3685 | + } |
|
3686 | + } |
|
3687 | + |
|
3688 | + /** |
|
3689 | + * decode a string that is encoded w/ "chunked" transfer encoding |
|
3690 | + * as defined in rfc2068 par. 19.4.6 |
|
3691 | + * code shamelessly stolen from nusoap library by Dietrich Ayala |
|
3692 | + * |
|
3693 | + * @param string $buffer the string to be decoded |
|
3694 | + * @return string |
|
3695 | + */ |
|
3696 | + function decode_chunked($buffer) |
|
3697 | + { |
|
3698 | + // length := 0 |
|
3699 | + $length = 0; |
|
3700 | + $new = ''; |
|
3701 | + |
|
3702 | + // read chunk-size, chunk-extension (if any) and crlf |
|
3703 | + // get the position of the linebreak |
|
3704 | + $chunkend = strpos($buffer,"\r\n") + 2; |
|
3705 | + $temp = substr($buffer,0,$chunkend); |
|
3706 | + $chunk_size = hexdec( trim($temp) ); |
|
3707 | + $chunkstart = $chunkend; |
|
3708 | + while($chunk_size > 0) |
|
3709 | + { |
|
3710 | + $chunkend = strpos($buffer, "\r\n", $chunkstart + $chunk_size); |
|
3711 | + |
|
3712 | + // just in case we got a broken connection |
|
3713 | + if($chunkend == false) |
|
3714 | + { |
|
3715 | + $chunk = substr($buffer,$chunkstart); |
|
3716 | + // append chunk-data to entity-body |
|
3717 | + $new .= $chunk; |
|
3718 | + $length += strlen($chunk); |
|
3719 | + break; |
|
3720 | + } |
|
3721 | + |
|
3722 | + // read chunk-data and crlf |
|
3723 | + $chunk = substr($buffer,$chunkstart,$chunkend-$chunkstart); |
|
3724 | + // append chunk-data to entity-body |
|
3725 | + $new .= $chunk; |
|
3726 | + // length := length + chunk-size |
|
3727 | + $length += strlen($chunk); |
|
3728 | + // read chunk-size and crlf |
|
3729 | + $chunkstart = $chunkend + 2; |
|
3730 | + |
|
3731 | + $chunkend = strpos($buffer,"\r\n",$chunkstart)+2; |
|
3732 | + if($chunkend == false) |
|
3733 | + { |
|
3734 | + break; //just in case we got a broken connection |
|
3735 | + } |
|
3736 | + $temp = substr($buffer,$chunkstart,$chunkend-$chunkstart); |
|
3737 | + $chunk_size = hexdec( trim($temp) ); |
|
3738 | + $chunkstart = $chunkend; |
|
3739 | + } |
|
3740 | + return $new; |
|
3741 | + } |
|
3742 | + |
|
3743 | + /** |
|
3744 | + * xml charset encoding guessing helper function. |
|
3745 | + * Tries to determine the charset encoding of an XML chunk received over HTTP. |
|
3746 | + * NB: according to the spec (RFC 3023), if text/xml content-type is received over HTTP without a content-type, |
|
3747 | + * we SHOULD assume it is strictly US-ASCII. But we try to be more tolerant of unconforming (legacy?) clients/servers, |
|
3748 | + * which will be most probably using UTF-8 anyway... |
|
3749 | + * |
|
3750 | + * @param string $httpheader the http Content-type header |
|
3751 | + * @param string $xmlchunk xml content buffer |
|
3752 | + * @param string $encoding_prefs comma separated list of character encodings to be used as default (when mb extension is enabled) |
|
3753 | + * @return string |
|
3754 | + * |
|
3755 | + * @todo explore usage of mb_http_input(): does it detect http headers + post data? if so, use it instead of hand-detection!!! |
|
3756 | + */ |
|
3757 | + function guess_encoding($httpheader='', $xmlchunk='', $encoding_prefs=null) |
|
3758 | + { |
|
3759 | + // discussion: see http://www.yale.edu/pclt/encoding/ |
|
3760 | + // 1 - test if encoding is specified in HTTP HEADERS |
|
3761 | + |
|
3762 | + //Details: |
|
3763 | + // LWS: (\13\10)?( |\t)+ |
|
3764 | + // token: (any char but excluded stuff)+ |
|
3765 | + // quoted string: " (any char but double quotes and cointrol chars)* " |
|
3766 | + // header: Content-type = ...; charset=value(; ...)* |
|
3767 | + // where value is of type token, no LWS allowed between 'charset' and value |
|
3768 | + // Note: we do not check for invalid chars in VALUE: |
|
3769 | + // this had better be done using pure ereg as below |
|
3770 | + // Note 2: we might be removing whitespace/tabs that ought to be left in if |
|
3771 | + // the received charset is a quoted string. But nobody uses such charset names... |
|
3772 | + |
|
3773 | + /// @todo this test will pass if ANY header has charset specification, not only Content-Type. Fix it? |
|
3774 | + $matches = array(); |
|
3775 | + if(preg_match('/;\s*charset\s*=([^;]+)/i', $httpheader, $matches)) |
|
3776 | + { |
|
3777 | + return strtoupper(trim($matches[1], " \t\"")); |
|
3778 | + } |
|
3779 | + |
|
3780 | + // 2 - scan the first bytes of the data for a UTF-16 (or other) BOM pattern |
|
3781 | + // (source: http://www.w3.org/TR/2000/REC-xml-20001006) |
|
3782 | + // NOTE: actually, according to the spec, even if we find the BOM and determine |
|
3783 | + // an encoding, we should check if there is an encoding specified |
|
3784 | + // in the xml declaration, and verify if they match. |
|
3785 | + /// @todo implement check as described above? |
|
3786 | + /// @todo implement check for first bytes of string even without a BOM? (It sure looks harder than for cases WITH a BOM) |
|
3787 | + if(preg_match('/^(\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\x00\x00\xFF\xFE|\xFE\xFF\x00\x00)/', $xmlchunk)) |
|
3788 | + { |
|
3789 | + return 'UCS-4'; |
|
3790 | + } |
|
3791 | + elseif(preg_match('/^(\xFE\xFF|\xFF\xFE)/', $xmlchunk)) |
|
3792 | + { |
|
3793 | + return 'UTF-16'; |
|
3794 | + } |
|
3795 | + elseif(preg_match('/^(\xEF\xBB\xBF)/', $xmlchunk)) |
|
3796 | + { |
|
3797 | + return 'UTF-8'; |
|
3798 | + } |
|
3799 | + |
|
3800 | + // 3 - test if encoding is specified in the xml declaration |
|
3801 | + // Details: |
|
3802 | + // SPACE: (#x20 | #x9 | #xD | #xA)+ === [ \x9\xD\xA]+ |
|
3803 | + // EQ: SPACE?=SPACE? === [ \x9\xD\xA]*=[ \x9\xD\xA]* |
|
3804 | + if (preg_match('/^<\?xml\s+version\s*=\s*'. "((?:\"[a-zA-Z0-9_.:-]+\")|(?:'[a-zA-Z0-9_.:-]+'))". |
|
3805 | + '\s+encoding\s*=\s*' . "((?:\"[A-Za-z][A-Za-z0-9._-]*\")|(?:'[A-Za-z][A-Za-z0-9._-]*'))/", |
|
3806 | + $xmlchunk, $matches)) |
|
3807 | + { |
|
3808 | + return strtoupper(substr($matches[2], 1, -1)); |
|
3809 | + } |
|
3810 | + |
|
3811 | + // 4 - if mbstring is available, let it do the guesswork |
|
3812 | + // NB: we favour finding an encoding that is compatible with what we can process |
|
3813 | + if(extension_loaded('mbstring')) |
|
3814 | + { |
|
3815 | + if($encoding_prefs) |
|
3816 | + { |
|
3817 | + $enc = mb_detect_encoding($xmlchunk, $encoding_prefs); |
|
3818 | + } |
|
3819 | + else |
|
3820 | + { |
|
3821 | + $enc = mb_detect_encoding($xmlchunk); |
|
3822 | + } |
|
3823 | + // NB: mb_detect likes to call it ascii, xml parser likes to call it US_ASCII... |
|
3824 | + // IANA also likes better US-ASCII, so go with it |
|
3825 | + if($enc == 'ASCII') |
|
3826 | + { |
|
3827 | + $enc = 'US-'.$enc; |
|
3828 | + } |
|
3829 | + return $enc; |
|
3830 | + } |
|
3831 | + else |
|
3832 | + { |
|
3833 | + // no encoding specified: as per HTTP1.1 assume it is iso-8859-1? |
|
3834 | + // Both RFC 2616 (HTTP 1.1) and 1945 (HTTP 1.0) clearly state that for text/xxx content types |
|
3835 | + // this should be the standard. And we should be getting text/xml as request and response. |
|
3836 | + // BUT we have to be backward compatible with the lib, which always used UTF-8 as default... |
|
3837 | + return $GLOBALS['xmlrpc_defencoding']; |
|
3838 | + } |
|
3839 | + } |
|
3840 | + |
|
3841 | + /** |
|
3842 | + * Helper function: checks if an xml chunk as a charset declaration (BOM or in the xml declaration) |
|
3843 | + * |
|
3844 | + * @param string $xmlChunk |
|
3845 | + * @return bool |
|
3846 | + */ |
|
3847 | + function has_encoding($xmlChunk) |
|
3848 | + { |
|
3849 | + // scan the first bytes of the data for a UTF-16 (or other) BOM pattern |
|
3850 | + // (source: http://www.w3.org/TR/2000/REC-xml-20001006) |
|
3851 | + if (preg_match('/^(\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\x00\x00\xFF\xFE|\xFE\xFF\x00\x00)/', $xmlChunk)) |
|
3852 | + { |
|
3853 | + return true; |
|
3854 | + } |
|
3855 | + elseif (preg_match('/^(\xFE\xFF|\xFF\xFE)/', $xmlChunk)) |
|
3856 | + { |
|
3857 | + return true; |
|
3858 | + } |
|
3859 | + elseif (preg_match('/^(\xEF\xBB\xBF)/', $xmlChunk)) |
|
3860 | + { |
|
3861 | + return true; |
|
3862 | + } |
|
3863 | + |
|
3864 | + // test if encoding is specified in the xml declaration |
|
3865 | + // Details: |
|
3866 | + // SPACE: (#x20 | #x9 | #xD | #xA)+ === [ \x9\xD\xA]+ |
|
3867 | + // EQ: SPACE?=SPACE? === [ \x9\xD\xA]*=[ \x9\xD\xA]* |
|
3868 | + if (preg_match('/^<\?xml\s+version\s*=\s*' . "((?:\"[a-zA-Z0-9_.:-]+\")|(?:'[a-zA-Z0-9_.:-]+'))" . |
|
3869 | + '\s+encoding\s*=\s*' . "((?:\"[A-Za-z][A-Za-z0-9._-]*\")|(?:'[A-Za-z][A-Za-z0-9._-]*'))/", |
|
3870 | + $xmlChunk, $matches)) |
|
3871 | + { |
|
3872 | + return true; |
|
3873 | + } |
|
3874 | + |
|
3875 | + return false; |
|
3876 | + } |
|
3877 | + |
|
3878 | + /** |
|
3879 | + * Checks if a given charset encoding is present in a list of encodings or |
|
3880 | + * if it is a valid subset of any encoding in the list |
|
3881 | + * @param string $encoding charset to be tested |
|
3882 | + * @param mixed $validlist comma separated list of valid charsets (or array of charsets) |
|
3883 | + * @return bool |
|
3884 | + */ |
|
3885 | + function is_valid_charset($encoding, $validlist) |
|
3886 | + { |
|
3887 | + $charset_supersets = array( |
|
3888 | + 'US-ASCII' => array ('ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', |
|
3889 | + 'ISO-8859-5', 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', |
|
3890 | + 'ISO-8859-9', 'ISO-8859-10', 'ISO-8859-11', 'ISO-8859-12', |
|
3891 | + 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'UTF-8', |
|
3892 | + 'EUC-JP', 'EUC-', 'EUC-KR', 'EUC-CN') |
|
3893 | + ); |
|
3894 | + if (is_string($validlist)) |
|
3895 | + $validlist = explode(',', $validlist); |
|
3896 | + if (@in_array(strtoupper($encoding), $validlist)) |
|
3897 | + return true; |
|
3898 | + else |
|
3899 | + { |
|
3900 | + if (array_key_exists($encoding, $charset_supersets)) |
|
3901 | + foreach ($validlist as $allowed) |
|
3902 | + if (in_array($allowed, $charset_supersets[$encoding])) |
|
3903 | + return true; |
|
3904 | + return false; |
|
3905 | + } |
|
3906 | + } |
|
3907 | 3907 | |
3908 | 3908 | ?> |
3909 | 3909 | \ No newline at end of file |
@@ -34,1215 +34,1215 @@ |
||
34 | 34 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
35 | 35 | // OF THE POSSIBILITY OF SUCH DAMAGE. |
36 | 36 | |
37 | - // XML RPC Server class |
|
38 | - // requires: xmlrpc.inc |
|
39 | - |
|
40 | - $GLOBALS['xmlrpcs_capabilities'] = array( |
|
41 | - // xmlrpc spec: always supported |
|
42 | - 'xmlrpc' => new xmlrpcval(array( |
|
43 | - 'specUrl' => new xmlrpcval('http://www.xmlrpc.com/spec', 'string'), |
|
44 | - 'specVersion' => new xmlrpcval(1, 'int') |
|
45 | - ), 'struct'), |
|
46 | - // if we support system.xxx functions, we always support multicall, too... |
|
47 | - // Note that, as of 2006/09/17, the following URL does not respond anymore |
|
48 | - 'system.multicall' => new xmlrpcval(array( |
|
49 | - 'specUrl' => new xmlrpcval('http://www.xmlrpc.com/discuss/msgReader$1208', 'string'), |
|
50 | - 'specVersion' => new xmlrpcval(1, 'int') |
|
51 | - ), 'struct'), |
|
52 | - // introspection: version 2! we support 'mixed', too |
|
53 | - 'introspection' => new xmlrpcval(array( |
|
54 | - 'specUrl' => new xmlrpcval('http://phpxmlrpc.sourceforge.net/doc-2/ch10.html', 'string'), |
|
55 | - 'specVersion' => new xmlrpcval(2, 'int') |
|
56 | - ), 'struct') |
|
57 | - ); |
|
58 | - |
|
59 | - /* Functions that implement system.XXX methods of xmlrpc servers */ |
|
60 | - $_xmlrpcs_getCapabilities_sig=array(array($GLOBALS['xmlrpcStruct'])); |
|
61 | - $_xmlrpcs_getCapabilities_doc='This method lists all the capabilites that the XML-RPC server has: the (more or less standard) extensions to the xmlrpc spec that it adheres to'; |
|
62 | - $_xmlrpcs_getCapabilities_sdoc=array(array('list of capabilities, described as structs with a version number and url for the spec')); |
|
63 | - function _xmlrpcs_getCapabilities($server, $m=null) |
|
64 | - { |
|
65 | - $outAr = $GLOBALS['xmlrpcs_capabilities']; |
|
66 | - // NIL extension |
|
67 | - if ($GLOBALS['xmlrpc_null_extension']) { |
|
68 | - $outAr['nil'] = new xmlrpcval(array( |
|
69 | - 'specUrl' => new xmlrpcval('http://www.ontosys.com/xml-rpc/extensions.php', 'string'), |
|
70 | - 'specVersion' => new xmlrpcval(1, 'int') |
|
71 | - ), 'struct'); |
|
72 | - } |
|
73 | - return new xmlrpcresp(new xmlrpcval($outAr, 'struct')); |
|
74 | - } |
|
75 | - |
|
76 | - // listMethods: signature was either a string, or nothing. |
|
77 | - // The useless string variant has been removed |
|
78 | - $_xmlrpcs_listMethods_sig=array(array($GLOBALS['xmlrpcArray'])); |
|
79 | - $_xmlrpcs_listMethods_doc='This method lists all the methods that the XML-RPC server knows how to dispatch'; |
|
80 | - $_xmlrpcs_listMethods_sdoc=array(array('list of method names')); |
|
81 | - function _xmlrpcs_listMethods($server, $m=null) // if called in plain php values mode, second param is missing |
|
82 | - { |
|
83 | - |
|
84 | - $outAr=array(); |
|
85 | - foreach($server->dmap as $key => $val) |
|
86 | - { |
|
87 | - $outAr[]=new xmlrpcval($key, 'string'); |
|
88 | - } |
|
89 | - if($server->allow_system_funcs) |
|
90 | - { |
|
91 | - foreach($GLOBALS['_xmlrpcs_dmap'] as $key => $val) |
|
92 | - { |
|
93 | - $outAr[]=new xmlrpcval($key, 'string'); |
|
94 | - } |
|
95 | - } |
|
96 | - return new xmlrpcresp(new xmlrpcval($outAr, 'array')); |
|
97 | - } |
|
98 | - |
|
99 | - $_xmlrpcs_methodSignature_sig=array(array($GLOBALS['xmlrpcArray'], $GLOBALS['xmlrpcString'])); |
|
100 | - $_xmlrpcs_methodSignature_doc='Returns an array of known signatures (an array of arrays) for the method name passed. If no signatures are known, returns a none-array (test for type != array to detect missing signature)'; |
|
101 | - $_xmlrpcs_methodSignature_sdoc=array(array('list of known signatures, each sig being an array of xmlrpc type names', 'name of method to be described')); |
|
102 | - function _xmlrpcs_methodSignature($server, $m) |
|
103 | - { |
|
104 | - // let accept as parameter both an xmlrpcval or string |
|
105 | - if (is_object($m)) |
|
106 | - { |
|
107 | - $methName=$m->getParam(0); |
|
108 | - $methName=$methName->scalarval(); |
|
109 | - } |
|
110 | - else |
|
111 | - { |
|
112 | - $methName=$m; |
|
113 | - } |
|
114 | - if(strpos($methName, "system.") === 0) |
|
115 | - { |
|
116 | - $dmap=$GLOBALS['_xmlrpcs_dmap']; $sysCall=1; |
|
117 | - } |
|
118 | - else |
|
119 | - { |
|
120 | - $dmap=$server->dmap; $sysCall=0; |
|
121 | - } |
|
122 | - if(isset($dmap[$methName])) |
|
123 | - { |
|
124 | - if(isset($dmap[$methName]['signature'])) |
|
125 | - { |
|
126 | - $sigs=array(); |
|
127 | - foreach($dmap[$methName]['signature'] as $inSig) |
|
128 | - { |
|
129 | - $cursig=array(); |
|
130 | - foreach($inSig as $sig) |
|
131 | - { |
|
132 | - $cursig[]=new xmlrpcval($sig, 'string'); |
|
133 | - } |
|
134 | - $sigs[]=new xmlrpcval($cursig, 'array'); |
|
135 | - } |
|
136 | - $r=new xmlrpcresp(new xmlrpcval($sigs, 'array')); |
|
137 | - } |
|
138 | - else |
|
139 | - { |
|
140 | - // NB: according to the official docs, we should be returning a |
|
141 | - // "none-array" here, which means not-an-array |
|
142 | - $r=new xmlrpcresp(new xmlrpcval('undef', 'string')); |
|
143 | - } |
|
144 | - } |
|
145 | - else |
|
146 | - { |
|
147 | - $r=new xmlrpcresp(0,$GLOBALS['xmlrpcerr']['introspect_unknown'], $GLOBALS['xmlrpcstr']['introspect_unknown']); |
|
148 | - } |
|
149 | - return $r; |
|
150 | - } |
|
151 | - |
|
152 | - $_xmlrpcs_methodHelp_sig=array(array($GLOBALS['xmlrpcString'], $GLOBALS['xmlrpcString'])); |
|
153 | - $_xmlrpcs_methodHelp_doc='Returns help text if defined for the method passed, otherwise returns an empty string'; |
|
154 | - $_xmlrpcs_methodHelp_sdoc=array(array('method description', 'name of the method to be described')); |
|
155 | - function _xmlrpcs_methodHelp($server, $m) |
|
156 | - { |
|
157 | - // let accept as parameter both an xmlrpcval or string |
|
158 | - if (is_object($m)) |
|
159 | - { |
|
160 | - $methName=$m->getParam(0); |
|
161 | - $methName=$methName->scalarval(); |
|
162 | - } |
|
163 | - else |
|
164 | - { |
|
165 | - $methName=$m; |
|
166 | - } |
|
167 | - if(strpos($methName, "system.") === 0) |
|
168 | - { |
|
169 | - $dmap=$GLOBALS['_xmlrpcs_dmap']; $sysCall=1; |
|
170 | - } |
|
171 | - else |
|
172 | - { |
|
173 | - $dmap=$server->dmap; $sysCall=0; |
|
174 | - } |
|
175 | - if(isset($dmap[$methName])) |
|
176 | - { |
|
177 | - if(isset($dmap[$methName]['docstring'])) |
|
178 | - { |
|
179 | - $r=new xmlrpcresp(new xmlrpcval($dmap[$methName]['docstring']), 'string'); |
|
180 | - } |
|
181 | - else |
|
182 | - { |
|
183 | - $r=new xmlrpcresp(new xmlrpcval('', 'string')); |
|
184 | - } |
|
185 | - } |
|
186 | - else |
|
187 | - { |
|
188 | - $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['introspect_unknown'], $GLOBALS['xmlrpcstr']['introspect_unknown']); |
|
189 | - } |
|
190 | - return $r; |
|
191 | - } |
|
192 | - |
|
193 | - $_xmlrpcs_multicall_sig = array(array($GLOBALS['xmlrpcArray'], $GLOBALS['xmlrpcArray'])); |
|
194 | - $_xmlrpcs_multicall_doc = 'Boxcar multiple RPC calls in one request. See http://www.xmlrpc.com/discuss/msgReader$1208 for details'; |
|
195 | - $_xmlrpcs_multicall_sdoc = array(array('list of response structs, where each struct has the usual members', 'list of calls, with each call being represented as a struct, with members "methodname" and "params"')); |
|
196 | - function _xmlrpcs_multicall_error($err) |
|
197 | - { |
|
198 | - if(is_string($err)) |
|
199 | - { |
|
200 | - $str = $GLOBALS['xmlrpcstr']["multicall_${err}"]; |
|
201 | - $code = $GLOBALS['xmlrpcerr']["multicall_${err}"]; |
|
202 | - } |
|
203 | - else |
|
204 | - { |
|
205 | - $code = $err->faultCode(); |
|
206 | - $str = $err->faultString(); |
|
207 | - } |
|
208 | - $struct = array(); |
|
209 | - $struct['faultCode'] = new xmlrpcval($code, 'int'); |
|
210 | - $struct['faultString'] = new xmlrpcval($str, 'string'); |
|
211 | - return new xmlrpcval($struct, 'struct'); |
|
212 | - } |
|
213 | - |
|
214 | - function _xmlrpcs_multicall_do_call($server, $call) |
|
215 | - { |
|
216 | - if($call->kindOf() != 'struct') |
|
217 | - { |
|
218 | - return _xmlrpcs_multicall_error('notstruct'); |
|
219 | - } |
|
220 | - $methName = @$call->structmem('methodName'); |
|
221 | - if(!$methName) |
|
222 | - { |
|
223 | - return _xmlrpcs_multicall_error('nomethod'); |
|
224 | - } |
|
225 | - if($methName->kindOf() != 'scalar' || $methName->scalartyp() != 'string') |
|
226 | - { |
|
227 | - return _xmlrpcs_multicall_error('notstring'); |
|
228 | - } |
|
229 | - if($methName->scalarval() == 'system.multicall') |
|
230 | - { |
|
231 | - return _xmlrpcs_multicall_error('recursion'); |
|
232 | - } |
|
233 | - |
|
234 | - $params = @$call->structmem('params'); |
|
235 | - if(!$params) |
|
236 | - { |
|
237 | - return _xmlrpcs_multicall_error('noparams'); |
|
238 | - } |
|
239 | - if($params->kindOf() != 'array') |
|
240 | - { |
|
241 | - return _xmlrpcs_multicall_error('notarray'); |
|
242 | - } |
|
243 | - $numParams = $params->arraysize(); |
|
244 | - |
|
245 | - $msg = new xmlrpcmsg($methName->scalarval()); |
|
246 | - for($i = 0; $i < $numParams; $i++) |
|
247 | - { |
|
248 | - if(!$msg->addParam($params->arraymem($i))) |
|
249 | - { |
|
250 | - $i++; |
|
251 | - return _xmlrpcs_multicall_error(new xmlrpcresp(0, |
|
252 | - $GLOBALS['xmlrpcerr']['incorrect_params'], |
|
253 | - $GLOBALS['xmlrpcstr']['incorrect_params'] . ": probable xml error in param " . $i)); |
|
254 | - } |
|
255 | - } |
|
256 | - |
|
257 | - $result = $server->execute($msg); |
|
258 | - |
|
259 | - if($result->faultCode() != 0) |
|
260 | - { |
|
261 | - return _xmlrpcs_multicall_error($result); // Method returned fault. |
|
262 | - } |
|
263 | - |
|
264 | - return new xmlrpcval(array($result->value()), 'array'); |
|
265 | - } |
|
266 | - |
|
267 | - function _xmlrpcs_multicall_do_call_phpvals($server, $call) |
|
268 | - { |
|
269 | - if(!is_array($call)) |
|
270 | - { |
|
271 | - return _xmlrpcs_multicall_error('notstruct'); |
|
272 | - } |
|
273 | - if(!array_key_exists('methodName', $call)) |
|
274 | - { |
|
275 | - return _xmlrpcs_multicall_error('nomethod'); |
|
276 | - } |
|
277 | - if (!is_string($call['methodName'])) |
|
278 | - { |
|
279 | - return _xmlrpcs_multicall_error('notstring'); |
|
280 | - } |
|
281 | - if($call['methodName'] == 'system.multicall') |
|
282 | - { |
|
283 | - return _xmlrpcs_multicall_error('recursion'); |
|
284 | - } |
|
285 | - if(!array_key_exists('params', $call)) |
|
286 | - { |
|
287 | - return _xmlrpcs_multicall_error('noparams'); |
|
288 | - } |
|
289 | - if(!is_array($call['params'])) |
|
290 | - { |
|
291 | - return _xmlrpcs_multicall_error('notarray'); |
|
292 | - } |
|
293 | - |
|
294 | - // this is a real dirty and simplistic hack, since we might have received a |
|
295 | - // base64 or datetime values, but they will be listed as strings here... |
|
296 | - $numParams = count($call['params']); |
|
297 | - $pt = array(); |
|
298 | - foreach($call['params'] as $val) |
|
299 | - $pt[] = php_2_xmlrpc_type(gettype($val)); |
|
300 | - |
|
301 | - $result = $server->execute($call['methodName'], $call['params'], $pt); |
|
302 | - |
|
303 | - if($result->faultCode() != 0) |
|
304 | - { |
|
305 | - return _xmlrpcs_multicall_error($result); // Method returned fault. |
|
306 | - } |
|
307 | - |
|
308 | - return new xmlrpcval(array($result->value()), 'array'); |
|
309 | - } |
|
310 | - |
|
311 | - function _xmlrpcs_multicall($server, $m) |
|
312 | - { |
|
313 | - $result = array(); |
|
314 | - // let accept a plain list of php parameters, beside a single xmlrpc msg object |
|
315 | - if (is_object($m)) |
|
316 | - { |
|
317 | - $calls = $m->getParam(0); |
|
318 | - $numCalls = $calls->arraysize(); |
|
319 | - for($i = 0; $i < $numCalls; $i++) |
|
320 | - { |
|
321 | - $call = $calls->arraymem($i); |
|
322 | - $result[$i] = _xmlrpcs_multicall_do_call($server, $call); |
|
323 | - } |
|
324 | - } |
|
325 | - else |
|
326 | - { |
|
327 | - $numCalls=count($m); |
|
328 | - for($i = 0; $i < $numCalls; $i++) |
|
329 | - { |
|
330 | - $result[$i] = _xmlrpcs_multicall_do_call_phpvals($server, $m[$i]); |
|
331 | - } |
|
332 | - } |
|
333 | - |
|
334 | - return new xmlrpcresp(new xmlrpcval($result, 'array')); |
|
335 | - } |
|
336 | - |
|
337 | - $GLOBALS['_xmlrpcs_dmap']=array( |
|
338 | - 'system.listMethods' => array( |
|
339 | - 'function' => '_xmlrpcs_listMethods', |
|
340 | - 'signature' => $_xmlrpcs_listMethods_sig, |
|
341 | - 'docstring' => $_xmlrpcs_listMethods_doc, |
|
342 | - 'signature_docs' => $_xmlrpcs_listMethods_sdoc), |
|
343 | - 'system.methodHelp' => array( |
|
344 | - 'function' => '_xmlrpcs_methodHelp', |
|
345 | - 'signature' => $_xmlrpcs_methodHelp_sig, |
|
346 | - 'docstring' => $_xmlrpcs_methodHelp_doc, |
|
347 | - 'signature_docs' => $_xmlrpcs_methodHelp_sdoc), |
|
348 | - 'system.methodSignature' => array( |
|
349 | - 'function' => '_xmlrpcs_methodSignature', |
|
350 | - 'signature' => $_xmlrpcs_methodSignature_sig, |
|
351 | - 'docstring' => $_xmlrpcs_methodSignature_doc, |
|
352 | - 'signature_docs' => $_xmlrpcs_methodSignature_sdoc), |
|
353 | - 'system.multicall' => array( |
|
354 | - 'function' => '_xmlrpcs_multicall', |
|
355 | - 'signature' => $_xmlrpcs_multicall_sig, |
|
356 | - 'docstring' => $_xmlrpcs_multicall_doc, |
|
357 | - 'signature_docs' => $_xmlrpcs_multicall_sdoc), |
|
358 | - 'system.getCapabilities' => array( |
|
359 | - 'function' => '_xmlrpcs_getCapabilities', |
|
360 | - 'signature' => $_xmlrpcs_getCapabilities_sig, |
|
361 | - 'docstring' => $_xmlrpcs_getCapabilities_doc, |
|
362 | - 'signature_docs' => $_xmlrpcs_getCapabilities_sdoc) |
|
363 | - ); |
|
364 | - |
|
365 | - $GLOBALS['_xmlrpcs_occurred_errors'] = ''; |
|
366 | - $GLOBALS['_xmlrpcs_prev_ehandler'] = ''; |
|
367 | - |
|
368 | - /** |
|
369 | - * Error handler used to track errors that occur during server-side execution of PHP code. |
|
370 | - * This allows to report back to the client whether an internal error has occurred or not |
|
371 | - * using an xmlrpc response object, instead of letting the client deal with the html junk |
|
372 | - * that a PHP execution error on the server generally entails. |
|
373 | - * |
|
374 | - * NB: in fact a user defined error handler can only handle WARNING, NOTICE and USER_* errors. |
|
375 | - * |
|
376 | - */ |
|
377 | - function _xmlrpcs_errorHandler($errcode, $errstring, $filename=null, $lineno=null, $context=null) |
|
378 | - { |
|
379 | - // obey the @ protocol |
|
380 | - if (error_reporting() == 0) |
|
381 | - return; |
|
382 | - |
|
383 | - //if($errcode != E_NOTICE && $errcode != E_WARNING && $errcode != E_USER_NOTICE && $errcode != E_USER_WARNING) |
|
384 | - if($errcode != E_STRICT) |
|
385 | - { |
|
386 | - $GLOBALS['_xmlrpcs_occurred_errors'] = $GLOBALS['_xmlrpcs_occurred_errors'] . $errstring . "\n"; |
|
387 | - } |
|
388 | - // Try to avoid as much as possible disruption to the previous error handling |
|
389 | - // mechanism in place |
|
390 | - if($GLOBALS['_xmlrpcs_prev_ehandler'] == '') |
|
391 | - { |
|
392 | - // The previous error handler was the default: all we should do is log error |
|
393 | - // to the default error log (if level high enough) |
|
394 | - if(ini_get('log_errors') && (intval(ini_get('error_reporting')) & $errcode)) |
|
395 | - { |
|
396 | - error_log($errstring); |
|
397 | - } |
|
398 | - } |
|
399 | - else |
|
400 | - { |
|
401 | - // Pass control on to previous error handler, trying to avoid loops... |
|
402 | - if($GLOBALS['_xmlrpcs_prev_ehandler'] != '_xmlrpcs_errorHandler') |
|
403 | - { |
|
404 | - // NB: this code will NOT work on php < 4.0.2: only 2 params were used for error handlers |
|
405 | - if(is_array($GLOBALS['_xmlrpcs_prev_ehandler'])) |
|
406 | - { |
|
407 | - // the following works both with static class methods and plain object methods as error handler |
|
408 | - call_user_func_array($GLOBALS['_xmlrpcs_prev_ehandler'], array($errcode, $errstring, $filename, $lineno, $context)); |
|
409 | - } |
|
410 | - else |
|
411 | - { |
|
412 | - $GLOBALS['_xmlrpcs_prev_ehandler']($errcode, $errstring, $filename, $lineno, $context); |
|
413 | - } |
|
414 | - } |
|
415 | - } |
|
416 | - } |
|
417 | - |
|
418 | - $GLOBALS['_xmlrpc_debuginfo']=''; |
|
419 | - |
|
420 | - /** |
|
421 | - * Add a string to the debug info that can be later seralized by the server |
|
422 | - * as part of the response message. |
|
423 | - * Note that for best compatibility, the debug string should be encoded using |
|
424 | - * the $GLOBALS['xmlrpc_internalencoding'] character set. |
|
425 | - * @param string $m |
|
426 | - * @access public |
|
427 | - */ |
|
428 | - function xmlrpc_debugmsg($m) |
|
429 | - { |
|
430 | - $GLOBALS['_xmlrpc_debuginfo'] .= $m . "\n"; |
|
431 | - } |
|
432 | - |
|
433 | - class xmlrpc_server |
|
434 | - { |
|
435 | - /** |
|
436 | - * Array defining php functions exposed as xmlrpc methods by this server |
|
437 | - * @access private |
|
438 | - */ |
|
439 | - var $dmap=array(); |
|
440 | - /** |
|
441 | - * Defines how functions in dmap will be invoked: either using an xmlrpc msg object |
|
442 | - * or plain php values. |
|
443 | - * valid strings are 'xmlrpcvals', 'phpvals' or 'epivals' |
|
444 | - */ |
|
445 | - var $functions_parameters_type='xmlrpcvals'; |
|
446 | - /** |
|
447 | - * Option used for fine-tuning the encoding the php values returned from |
|
448 | - * functions registered in the dispatch map when the functions_parameters_types |
|
449 | - * member is set to 'phpvals' |
|
450 | - * @see php_xmlrpc_encode for a list of values |
|
451 | - */ |
|
452 | - var $phpvals_encoding_options = array( 'auto_dates' ); |
|
453 | - /// controls whether the server is going to echo debugging messages back to the client as comments in response body. valid values: 0,1,2,3 |
|
454 | - var $debug = 1; |
|
455 | - /** |
|
456 | - * Controls behaviour of server when invoked user function throws an exception: |
|
457 | - * 0 = catch it and return an 'internal error' xmlrpc response (default) |
|
458 | - * 1 = catch it and return an xmlrpc response with the error corresponding to the exception |
|
459 | - * 2 = allow the exception to float to the upper layers |
|
460 | - */ |
|
461 | - var $exception_handling = 0; |
|
462 | - /** |
|
463 | - * When set to true, it will enable HTTP compression of the response, in case |
|
464 | - * the client has declared its support for compression in the request. |
|
465 | - */ |
|
466 | - var $compress_response = false; |
|
467 | - /** |
|
468 | - * List of http compression methods accepted by the server for requests. |
|
469 | - * NB: PHP supports deflate, gzip compressions out of the box if compiled w. zlib |
|
470 | - */ |
|
471 | - var $accepted_compression = array(); |
|
472 | - /// shall we serve calls to system.* methods? |
|
473 | - var $allow_system_funcs = true; |
|
474 | - /// list of charset encodings natively accepted for requests |
|
475 | - var $accepted_charset_encodings = array(); |
|
476 | - /** |
|
477 | - * charset encoding to be used for response. |
|
478 | - * NB: if we can, we will convert the generated response from internal_encoding to the intended one. |
|
479 | - * can be: a supported xml encoding (only UTF-8 and ISO-8859-1 at present, unless mbstring is enabled), |
|
480 | - * null (leave unspecified in response, convert output stream to US_ASCII), |
|
481 | - * 'default' (use xmlrpc library default as specified in xmlrpc.inc, convert output stream if needed), |
|
482 | - * or 'auto' (use client-specified charset encoding or same as request if request headers do not specify it (unless request is US-ASCII: then use library default anyway). |
|
483 | - * NB: pretty dangerous if you accept every charset and do not have mbstring enabled) |
|
484 | - */ |
|
485 | - var $response_charset_encoding = ''; |
|
486 | - /** |
|
487 | - * Storage for internal debug info |
|
488 | - * @access private |
|
489 | - */ |
|
490 | - var $debug_info = ''; |
|
491 | - /** |
|
492 | - * Extra data passed at runtime to method handling functions. Used only by EPI layer |
|
493 | - */ |
|
494 | - var $user_data = null; |
|
495 | - |
|
496 | - /** |
|
497 | - * @param array $dispmap the dispatch map with definition of exposed services |
|
498 | - * @param boolean $servicenow set to false to prevent the server from running upon construction |
|
499 | - */ |
|
500 | - function __construct($dispMap=null, $serviceNow=true) |
|
501 | - { |
|
502 | - // if ZLIB is enabled, let the server by default accept compressed requests, |
|
503 | - // and compress responses sent to clients that support them |
|
504 | - if(function_exists('gzinflate')) |
|
505 | - { |
|
506 | - $this->accepted_compression = array('gzip', 'deflate'); |
|
507 | - $this->compress_response = true; |
|
508 | - } |
|
509 | - |
|
510 | - // by default the xml parser can support these 3 charset encodings |
|
511 | - $this->accepted_charset_encodings = array('UTF-8', 'ISO-8859-1', 'US-ASCII'); |
|
512 | - |
|
513 | - // dispMap is a dispatch array of methods |
|
514 | - // mapped to function names and signatures |
|
515 | - // if a method |
|
516 | - // doesn't appear in the map then an unknown |
|
517 | - // method error is generated |
|
518 | - /* milosch - changed to make passing dispMap optional. |
|
37 | + // XML RPC Server class |
|
38 | + // requires: xmlrpc.inc |
|
39 | + |
|
40 | + $GLOBALS['xmlrpcs_capabilities'] = array( |
|
41 | + // xmlrpc spec: always supported |
|
42 | + 'xmlrpc' => new xmlrpcval(array( |
|
43 | + 'specUrl' => new xmlrpcval('http://www.xmlrpc.com/spec', 'string'), |
|
44 | + 'specVersion' => new xmlrpcval(1, 'int') |
|
45 | + ), 'struct'), |
|
46 | + // if we support system.xxx functions, we always support multicall, too... |
|
47 | + // Note that, as of 2006/09/17, the following URL does not respond anymore |
|
48 | + 'system.multicall' => new xmlrpcval(array( |
|
49 | + 'specUrl' => new xmlrpcval('http://www.xmlrpc.com/discuss/msgReader$1208', 'string'), |
|
50 | + 'specVersion' => new xmlrpcval(1, 'int') |
|
51 | + ), 'struct'), |
|
52 | + // introspection: version 2! we support 'mixed', too |
|
53 | + 'introspection' => new xmlrpcval(array( |
|
54 | + 'specUrl' => new xmlrpcval('http://phpxmlrpc.sourceforge.net/doc-2/ch10.html', 'string'), |
|
55 | + 'specVersion' => new xmlrpcval(2, 'int') |
|
56 | + ), 'struct') |
|
57 | + ); |
|
58 | + |
|
59 | + /* Functions that implement system.XXX methods of xmlrpc servers */ |
|
60 | + $_xmlrpcs_getCapabilities_sig=array(array($GLOBALS['xmlrpcStruct'])); |
|
61 | + $_xmlrpcs_getCapabilities_doc='This method lists all the capabilites that the XML-RPC server has: the (more or less standard) extensions to the xmlrpc spec that it adheres to'; |
|
62 | + $_xmlrpcs_getCapabilities_sdoc=array(array('list of capabilities, described as structs with a version number and url for the spec')); |
|
63 | + function _xmlrpcs_getCapabilities($server, $m=null) |
|
64 | + { |
|
65 | + $outAr = $GLOBALS['xmlrpcs_capabilities']; |
|
66 | + // NIL extension |
|
67 | + if ($GLOBALS['xmlrpc_null_extension']) { |
|
68 | + $outAr['nil'] = new xmlrpcval(array( |
|
69 | + 'specUrl' => new xmlrpcval('http://www.ontosys.com/xml-rpc/extensions.php', 'string'), |
|
70 | + 'specVersion' => new xmlrpcval(1, 'int') |
|
71 | + ), 'struct'); |
|
72 | + } |
|
73 | + return new xmlrpcresp(new xmlrpcval($outAr, 'struct')); |
|
74 | + } |
|
75 | + |
|
76 | + // listMethods: signature was either a string, or nothing. |
|
77 | + // The useless string variant has been removed |
|
78 | + $_xmlrpcs_listMethods_sig=array(array($GLOBALS['xmlrpcArray'])); |
|
79 | + $_xmlrpcs_listMethods_doc='This method lists all the methods that the XML-RPC server knows how to dispatch'; |
|
80 | + $_xmlrpcs_listMethods_sdoc=array(array('list of method names')); |
|
81 | + function _xmlrpcs_listMethods($server, $m=null) // if called in plain php values mode, second param is missing |
|
82 | + { |
|
83 | + |
|
84 | + $outAr=array(); |
|
85 | + foreach($server->dmap as $key => $val) |
|
86 | + { |
|
87 | + $outAr[]=new xmlrpcval($key, 'string'); |
|
88 | + } |
|
89 | + if($server->allow_system_funcs) |
|
90 | + { |
|
91 | + foreach($GLOBALS['_xmlrpcs_dmap'] as $key => $val) |
|
92 | + { |
|
93 | + $outAr[]=new xmlrpcval($key, 'string'); |
|
94 | + } |
|
95 | + } |
|
96 | + return new xmlrpcresp(new xmlrpcval($outAr, 'array')); |
|
97 | + } |
|
98 | + |
|
99 | + $_xmlrpcs_methodSignature_sig=array(array($GLOBALS['xmlrpcArray'], $GLOBALS['xmlrpcString'])); |
|
100 | + $_xmlrpcs_methodSignature_doc='Returns an array of known signatures (an array of arrays) for the method name passed. If no signatures are known, returns a none-array (test for type != array to detect missing signature)'; |
|
101 | + $_xmlrpcs_methodSignature_sdoc=array(array('list of known signatures, each sig being an array of xmlrpc type names', 'name of method to be described')); |
|
102 | + function _xmlrpcs_methodSignature($server, $m) |
|
103 | + { |
|
104 | + // let accept as parameter both an xmlrpcval or string |
|
105 | + if (is_object($m)) |
|
106 | + { |
|
107 | + $methName=$m->getParam(0); |
|
108 | + $methName=$methName->scalarval(); |
|
109 | + } |
|
110 | + else |
|
111 | + { |
|
112 | + $methName=$m; |
|
113 | + } |
|
114 | + if(strpos($methName, "system.") === 0) |
|
115 | + { |
|
116 | + $dmap=$GLOBALS['_xmlrpcs_dmap']; $sysCall=1; |
|
117 | + } |
|
118 | + else |
|
119 | + { |
|
120 | + $dmap=$server->dmap; $sysCall=0; |
|
121 | + } |
|
122 | + if(isset($dmap[$methName])) |
|
123 | + { |
|
124 | + if(isset($dmap[$methName]['signature'])) |
|
125 | + { |
|
126 | + $sigs=array(); |
|
127 | + foreach($dmap[$methName]['signature'] as $inSig) |
|
128 | + { |
|
129 | + $cursig=array(); |
|
130 | + foreach($inSig as $sig) |
|
131 | + { |
|
132 | + $cursig[]=new xmlrpcval($sig, 'string'); |
|
133 | + } |
|
134 | + $sigs[]=new xmlrpcval($cursig, 'array'); |
|
135 | + } |
|
136 | + $r=new xmlrpcresp(new xmlrpcval($sigs, 'array')); |
|
137 | + } |
|
138 | + else |
|
139 | + { |
|
140 | + // NB: according to the official docs, we should be returning a |
|
141 | + // "none-array" here, which means not-an-array |
|
142 | + $r=new xmlrpcresp(new xmlrpcval('undef', 'string')); |
|
143 | + } |
|
144 | + } |
|
145 | + else |
|
146 | + { |
|
147 | + $r=new xmlrpcresp(0,$GLOBALS['xmlrpcerr']['introspect_unknown'], $GLOBALS['xmlrpcstr']['introspect_unknown']); |
|
148 | + } |
|
149 | + return $r; |
|
150 | + } |
|
151 | + |
|
152 | + $_xmlrpcs_methodHelp_sig=array(array($GLOBALS['xmlrpcString'], $GLOBALS['xmlrpcString'])); |
|
153 | + $_xmlrpcs_methodHelp_doc='Returns help text if defined for the method passed, otherwise returns an empty string'; |
|
154 | + $_xmlrpcs_methodHelp_sdoc=array(array('method description', 'name of the method to be described')); |
|
155 | + function _xmlrpcs_methodHelp($server, $m) |
|
156 | + { |
|
157 | + // let accept as parameter both an xmlrpcval or string |
|
158 | + if (is_object($m)) |
|
159 | + { |
|
160 | + $methName=$m->getParam(0); |
|
161 | + $methName=$methName->scalarval(); |
|
162 | + } |
|
163 | + else |
|
164 | + { |
|
165 | + $methName=$m; |
|
166 | + } |
|
167 | + if(strpos($methName, "system.") === 0) |
|
168 | + { |
|
169 | + $dmap=$GLOBALS['_xmlrpcs_dmap']; $sysCall=1; |
|
170 | + } |
|
171 | + else |
|
172 | + { |
|
173 | + $dmap=$server->dmap; $sysCall=0; |
|
174 | + } |
|
175 | + if(isset($dmap[$methName])) |
|
176 | + { |
|
177 | + if(isset($dmap[$methName]['docstring'])) |
|
178 | + { |
|
179 | + $r=new xmlrpcresp(new xmlrpcval($dmap[$methName]['docstring']), 'string'); |
|
180 | + } |
|
181 | + else |
|
182 | + { |
|
183 | + $r=new xmlrpcresp(new xmlrpcval('', 'string')); |
|
184 | + } |
|
185 | + } |
|
186 | + else |
|
187 | + { |
|
188 | + $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['introspect_unknown'], $GLOBALS['xmlrpcstr']['introspect_unknown']); |
|
189 | + } |
|
190 | + return $r; |
|
191 | + } |
|
192 | + |
|
193 | + $_xmlrpcs_multicall_sig = array(array($GLOBALS['xmlrpcArray'], $GLOBALS['xmlrpcArray'])); |
|
194 | + $_xmlrpcs_multicall_doc = 'Boxcar multiple RPC calls in one request. See http://www.xmlrpc.com/discuss/msgReader$1208 for details'; |
|
195 | + $_xmlrpcs_multicall_sdoc = array(array('list of response structs, where each struct has the usual members', 'list of calls, with each call being represented as a struct, with members "methodname" and "params"')); |
|
196 | + function _xmlrpcs_multicall_error($err) |
|
197 | + { |
|
198 | + if(is_string($err)) |
|
199 | + { |
|
200 | + $str = $GLOBALS['xmlrpcstr']["multicall_${err}"]; |
|
201 | + $code = $GLOBALS['xmlrpcerr']["multicall_${err}"]; |
|
202 | + } |
|
203 | + else |
|
204 | + { |
|
205 | + $code = $err->faultCode(); |
|
206 | + $str = $err->faultString(); |
|
207 | + } |
|
208 | + $struct = array(); |
|
209 | + $struct['faultCode'] = new xmlrpcval($code, 'int'); |
|
210 | + $struct['faultString'] = new xmlrpcval($str, 'string'); |
|
211 | + return new xmlrpcval($struct, 'struct'); |
|
212 | + } |
|
213 | + |
|
214 | + function _xmlrpcs_multicall_do_call($server, $call) |
|
215 | + { |
|
216 | + if($call->kindOf() != 'struct') |
|
217 | + { |
|
218 | + return _xmlrpcs_multicall_error('notstruct'); |
|
219 | + } |
|
220 | + $methName = @$call->structmem('methodName'); |
|
221 | + if(!$methName) |
|
222 | + { |
|
223 | + return _xmlrpcs_multicall_error('nomethod'); |
|
224 | + } |
|
225 | + if($methName->kindOf() != 'scalar' || $methName->scalartyp() != 'string') |
|
226 | + { |
|
227 | + return _xmlrpcs_multicall_error('notstring'); |
|
228 | + } |
|
229 | + if($methName->scalarval() == 'system.multicall') |
|
230 | + { |
|
231 | + return _xmlrpcs_multicall_error('recursion'); |
|
232 | + } |
|
233 | + |
|
234 | + $params = @$call->structmem('params'); |
|
235 | + if(!$params) |
|
236 | + { |
|
237 | + return _xmlrpcs_multicall_error('noparams'); |
|
238 | + } |
|
239 | + if($params->kindOf() != 'array') |
|
240 | + { |
|
241 | + return _xmlrpcs_multicall_error('notarray'); |
|
242 | + } |
|
243 | + $numParams = $params->arraysize(); |
|
244 | + |
|
245 | + $msg = new xmlrpcmsg($methName->scalarval()); |
|
246 | + for($i = 0; $i < $numParams; $i++) |
|
247 | + { |
|
248 | + if(!$msg->addParam($params->arraymem($i))) |
|
249 | + { |
|
250 | + $i++; |
|
251 | + return _xmlrpcs_multicall_error(new xmlrpcresp(0, |
|
252 | + $GLOBALS['xmlrpcerr']['incorrect_params'], |
|
253 | + $GLOBALS['xmlrpcstr']['incorrect_params'] . ": probable xml error in param " . $i)); |
|
254 | + } |
|
255 | + } |
|
256 | + |
|
257 | + $result = $server->execute($msg); |
|
258 | + |
|
259 | + if($result->faultCode() != 0) |
|
260 | + { |
|
261 | + return _xmlrpcs_multicall_error($result); // Method returned fault. |
|
262 | + } |
|
263 | + |
|
264 | + return new xmlrpcval(array($result->value()), 'array'); |
|
265 | + } |
|
266 | + |
|
267 | + function _xmlrpcs_multicall_do_call_phpvals($server, $call) |
|
268 | + { |
|
269 | + if(!is_array($call)) |
|
270 | + { |
|
271 | + return _xmlrpcs_multicall_error('notstruct'); |
|
272 | + } |
|
273 | + if(!array_key_exists('methodName', $call)) |
|
274 | + { |
|
275 | + return _xmlrpcs_multicall_error('nomethod'); |
|
276 | + } |
|
277 | + if (!is_string($call['methodName'])) |
|
278 | + { |
|
279 | + return _xmlrpcs_multicall_error('notstring'); |
|
280 | + } |
|
281 | + if($call['methodName'] == 'system.multicall') |
|
282 | + { |
|
283 | + return _xmlrpcs_multicall_error('recursion'); |
|
284 | + } |
|
285 | + if(!array_key_exists('params', $call)) |
|
286 | + { |
|
287 | + return _xmlrpcs_multicall_error('noparams'); |
|
288 | + } |
|
289 | + if(!is_array($call['params'])) |
|
290 | + { |
|
291 | + return _xmlrpcs_multicall_error('notarray'); |
|
292 | + } |
|
293 | + |
|
294 | + // this is a real dirty and simplistic hack, since we might have received a |
|
295 | + // base64 or datetime values, but they will be listed as strings here... |
|
296 | + $numParams = count($call['params']); |
|
297 | + $pt = array(); |
|
298 | + foreach($call['params'] as $val) |
|
299 | + $pt[] = php_2_xmlrpc_type(gettype($val)); |
|
300 | + |
|
301 | + $result = $server->execute($call['methodName'], $call['params'], $pt); |
|
302 | + |
|
303 | + if($result->faultCode() != 0) |
|
304 | + { |
|
305 | + return _xmlrpcs_multicall_error($result); // Method returned fault. |
|
306 | + } |
|
307 | + |
|
308 | + return new xmlrpcval(array($result->value()), 'array'); |
|
309 | + } |
|
310 | + |
|
311 | + function _xmlrpcs_multicall($server, $m) |
|
312 | + { |
|
313 | + $result = array(); |
|
314 | + // let accept a plain list of php parameters, beside a single xmlrpc msg object |
|
315 | + if (is_object($m)) |
|
316 | + { |
|
317 | + $calls = $m->getParam(0); |
|
318 | + $numCalls = $calls->arraysize(); |
|
319 | + for($i = 0; $i < $numCalls; $i++) |
|
320 | + { |
|
321 | + $call = $calls->arraymem($i); |
|
322 | + $result[$i] = _xmlrpcs_multicall_do_call($server, $call); |
|
323 | + } |
|
324 | + } |
|
325 | + else |
|
326 | + { |
|
327 | + $numCalls=count($m); |
|
328 | + for($i = 0; $i < $numCalls; $i++) |
|
329 | + { |
|
330 | + $result[$i] = _xmlrpcs_multicall_do_call_phpvals($server, $m[$i]); |
|
331 | + } |
|
332 | + } |
|
333 | + |
|
334 | + return new xmlrpcresp(new xmlrpcval($result, 'array')); |
|
335 | + } |
|
336 | + |
|
337 | + $GLOBALS['_xmlrpcs_dmap']=array( |
|
338 | + 'system.listMethods' => array( |
|
339 | + 'function' => '_xmlrpcs_listMethods', |
|
340 | + 'signature' => $_xmlrpcs_listMethods_sig, |
|
341 | + 'docstring' => $_xmlrpcs_listMethods_doc, |
|
342 | + 'signature_docs' => $_xmlrpcs_listMethods_sdoc), |
|
343 | + 'system.methodHelp' => array( |
|
344 | + 'function' => '_xmlrpcs_methodHelp', |
|
345 | + 'signature' => $_xmlrpcs_methodHelp_sig, |
|
346 | + 'docstring' => $_xmlrpcs_methodHelp_doc, |
|
347 | + 'signature_docs' => $_xmlrpcs_methodHelp_sdoc), |
|
348 | + 'system.methodSignature' => array( |
|
349 | + 'function' => '_xmlrpcs_methodSignature', |
|
350 | + 'signature' => $_xmlrpcs_methodSignature_sig, |
|
351 | + 'docstring' => $_xmlrpcs_methodSignature_doc, |
|
352 | + 'signature_docs' => $_xmlrpcs_methodSignature_sdoc), |
|
353 | + 'system.multicall' => array( |
|
354 | + 'function' => '_xmlrpcs_multicall', |
|
355 | + 'signature' => $_xmlrpcs_multicall_sig, |
|
356 | + 'docstring' => $_xmlrpcs_multicall_doc, |
|
357 | + 'signature_docs' => $_xmlrpcs_multicall_sdoc), |
|
358 | + 'system.getCapabilities' => array( |
|
359 | + 'function' => '_xmlrpcs_getCapabilities', |
|
360 | + 'signature' => $_xmlrpcs_getCapabilities_sig, |
|
361 | + 'docstring' => $_xmlrpcs_getCapabilities_doc, |
|
362 | + 'signature_docs' => $_xmlrpcs_getCapabilities_sdoc) |
|
363 | + ); |
|
364 | + |
|
365 | + $GLOBALS['_xmlrpcs_occurred_errors'] = ''; |
|
366 | + $GLOBALS['_xmlrpcs_prev_ehandler'] = ''; |
|
367 | + |
|
368 | + /** |
|
369 | + * Error handler used to track errors that occur during server-side execution of PHP code. |
|
370 | + * This allows to report back to the client whether an internal error has occurred or not |
|
371 | + * using an xmlrpc response object, instead of letting the client deal with the html junk |
|
372 | + * that a PHP execution error on the server generally entails. |
|
373 | + * |
|
374 | + * NB: in fact a user defined error handler can only handle WARNING, NOTICE and USER_* errors. |
|
375 | + * |
|
376 | + */ |
|
377 | + function _xmlrpcs_errorHandler($errcode, $errstring, $filename=null, $lineno=null, $context=null) |
|
378 | + { |
|
379 | + // obey the @ protocol |
|
380 | + if (error_reporting() == 0) |
|
381 | + return; |
|
382 | + |
|
383 | + //if($errcode != E_NOTICE && $errcode != E_WARNING && $errcode != E_USER_NOTICE && $errcode != E_USER_WARNING) |
|
384 | + if($errcode != E_STRICT) |
|
385 | + { |
|
386 | + $GLOBALS['_xmlrpcs_occurred_errors'] = $GLOBALS['_xmlrpcs_occurred_errors'] . $errstring . "\n"; |
|
387 | + } |
|
388 | + // Try to avoid as much as possible disruption to the previous error handling |
|
389 | + // mechanism in place |
|
390 | + if($GLOBALS['_xmlrpcs_prev_ehandler'] == '') |
|
391 | + { |
|
392 | + // The previous error handler was the default: all we should do is log error |
|
393 | + // to the default error log (if level high enough) |
|
394 | + if(ini_get('log_errors') && (intval(ini_get('error_reporting')) & $errcode)) |
|
395 | + { |
|
396 | + error_log($errstring); |
|
397 | + } |
|
398 | + } |
|
399 | + else |
|
400 | + { |
|
401 | + // Pass control on to previous error handler, trying to avoid loops... |
|
402 | + if($GLOBALS['_xmlrpcs_prev_ehandler'] != '_xmlrpcs_errorHandler') |
|
403 | + { |
|
404 | + // NB: this code will NOT work on php < 4.0.2: only 2 params were used for error handlers |
|
405 | + if(is_array($GLOBALS['_xmlrpcs_prev_ehandler'])) |
|
406 | + { |
|
407 | + // the following works both with static class methods and plain object methods as error handler |
|
408 | + call_user_func_array($GLOBALS['_xmlrpcs_prev_ehandler'], array($errcode, $errstring, $filename, $lineno, $context)); |
|
409 | + } |
|
410 | + else |
|
411 | + { |
|
412 | + $GLOBALS['_xmlrpcs_prev_ehandler']($errcode, $errstring, $filename, $lineno, $context); |
|
413 | + } |
|
414 | + } |
|
415 | + } |
|
416 | + } |
|
417 | + |
|
418 | + $GLOBALS['_xmlrpc_debuginfo']=''; |
|
419 | + |
|
420 | + /** |
|
421 | + * Add a string to the debug info that can be later seralized by the server |
|
422 | + * as part of the response message. |
|
423 | + * Note that for best compatibility, the debug string should be encoded using |
|
424 | + * the $GLOBALS['xmlrpc_internalencoding'] character set. |
|
425 | + * @param string $m |
|
426 | + * @access public |
|
427 | + */ |
|
428 | + function xmlrpc_debugmsg($m) |
|
429 | + { |
|
430 | + $GLOBALS['_xmlrpc_debuginfo'] .= $m . "\n"; |
|
431 | + } |
|
432 | + |
|
433 | + class xmlrpc_server |
|
434 | + { |
|
435 | + /** |
|
436 | + * Array defining php functions exposed as xmlrpc methods by this server |
|
437 | + * @access private |
|
438 | + */ |
|
439 | + var $dmap=array(); |
|
440 | + /** |
|
441 | + * Defines how functions in dmap will be invoked: either using an xmlrpc msg object |
|
442 | + * or plain php values. |
|
443 | + * valid strings are 'xmlrpcvals', 'phpvals' or 'epivals' |
|
444 | + */ |
|
445 | + var $functions_parameters_type='xmlrpcvals'; |
|
446 | + /** |
|
447 | + * Option used for fine-tuning the encoding the php values returned from |
|
448 | + * functions registered in the dispatch map when the functions_parameters_types |
|
449 | + * member is set to 'phpvals' |
|
450 | + * @see php_xmlrpc_encode for a list of values |
|
451 | + */ |
|
452 | + var $phpvals_encoding_options = array( 'auto_dates' ); |
|
453 | + /// controls whether the server is going to echo debugging messages back to the client as comments in response body. valid values: 0,1,2,3 |
|
454 | + var $debug = 1; |
|
455 | + /** |
|
456 | + * Controls behaviour of server when invoked user function throws an exception: |
|
457 | + * 0 = catch it and return an 'internal error' xmlrpc response (default) |
|
458 | + * 1 = catch it and return an xmlrpc response with the error corresponding to the exception |
|
459 | + * 2 = allow the exception to float to the upper layers |
|
460 | + */ |
|
461 | + var $exception_handling = 0; |
|
462 | + /** |
|
463 | + * When set to true, it will enable HTTP compression of the response, in case |
|
464 | + * the client has declared its support for compression in the request. |
|
465 | + */ |
|
466 | + var $compress_response = false; |
|
467 | + /** |
|
468 | + * List of http compression methods accepted by the server for requests. |
|
469 | + * NB: PHP supports deflate, gzip compressions out of the box if compiled w. zlib |
|
470 | + */ |
|
471 | + var $accepted_compression = array(); |
|
472 | + /// shall we serve calls to system.* methods? |
|
473 | + var $allow_system_funcs = true; |
|
474 | + /// list of charset encodings natively accepted for requests |
|
475 | + var $accepted_charset_encodings = array(); |
|
476 | + /** |
|
477 | + * charset encoding to be used for response. |
|
478 | + * NB: if we can, we will convert the generated response from internal_encoding to the intended one. |
|
479 | + * can be: a supported xml encoding (only UTF-8 and ISO-8859-1 at present, unless mbstring is enabled), |
|
480 | + * null (leave unspecified in response, convert output stream to US_ASCII), |
|
481 | + * 'default' (use xmlrpc library default as specified in xmlrpc.inc, convert output stream if needed), |
|
482 | + * or 'auto' (use client-specified charset encoding or same as request if request headers do not specify it (unless request is US-ASCII: then use library default anyway). |
|
483 | + * NB: pretty dangerous if you accept every charset and do not have mbstring enabled) |
|
484 | + */ |
|
485 | + var $response_charset_encoding = ''; |
|
486 | + /** |
|
487 | + * Storage for internal debug info |
|
488 | + * @access private |
|
489 | + */ |
|
490 | + var $debug_info = ''; |
|
491 | + /** |
|
492 | + * Extra data passed at runtime to method handling functions. Used only by EPI layer |
|
493 | + */ |
|
494 | + var $user_data = null; |
|
495 | + |
|
496 | + /** |
|
497 | + * @param array $dispmap the dispatch map with definition of exposed services |
|
498 | + * @param boolean $servicenow set to false to prevent the server from running upon construction |
|
499 | + */ |
|
500 | + function __construct($dispMap=null, $serviceNow=true) |
|
501 | + { |
|
502 | + // if ZLIB is enabled, let the server by default accept compressed requests, |
|
503 | + // and compress responses sent to clients that support them |
|
504 | + if(function_exists('gzinflate')) |
|
505 | + { |
|
506 | + $this->accepted_compression = array('gzip', 'deflate'); |
|
507 | + $this->compress_response = true; |
|
508 | + } |
|
509 | + |
|
510 | + // by default the xml parser can support these 3 charset encodings |
|
511 | + $this->accepted_charset_encodings = array('UTF-8', 'ISO-8859-1', 'US-ASCII'); |
|
512 | + |
|
513 | + // dispMap is a dispatch array of methods |
|
514 | + // mapped to function names and signatures |
|
515 | + // if a method |
|
516 | + // doesn't appear in the map then an unknown |
|
517 | + // method error is generated |
|
518 | + /* milosch - changed to make passing dispMap optional. |
|
519 | 519 | * instead, you can use the class add_to_map() function |
520 | 520 | * to add functions manually (borrowed from SOAPX4) |
521 | 521 | */ |
522 | - if($dispMap) |
|
523 | - { |
|
524 | - $this->dmap = $dispMap; |
|
525 | - if($serviceNow) |
|
526 | - { |
|
527 | - $this->service(); |
|
528 | - } |
|
529 | - } |
|
530 | - } |
|
531 | - |
|
532 | - /** |
|
533 | - * @deprecated |
|
534 | - */ |
|
535 | - function xmlrpc_client($dispMap=null, $serviceNow=true) |
|
536 | - { |
|
537 | - self::__construct($dispMap, $serviceNow); |
|
538 | - } |
|
539 | - |
|
540 | - /** |
|
541 | - * Set debug level of server. |
|
542 | - * @param integer $in debug lvl: determines info added to xmlrpc responses (as xml comments) |
|
543 | - * 0 = no debug info, |
|
544 | - * 1 = msgs set from user with debugmsg(), |
|
545 | - * 2 = add complete xmlrpc request (headers and body), |
|
546 | - * 3 = add also all processing warnings happened during method processing |
|
547 | - * (NB: this involves setting a custom error handler, and might interfere |
|
548 | - * with the standard processing of the php function exposed as method. In |
|
549 | - * particular, triggering an USER_ERROR level error will not halt script |
|
550 | - * execution anymore, but just end up logged in the xmlrpc response) |
|
551 | - * Note that info added at level 2 and 3 will be base64 encoded |
|
552 | - * @access public |
|
553 | - */ |
|
554 | - function setDebug($in) |
|
555 | - { |
|
556 | - $this->debug=$in; |
|
557 | - } |
|
558 | - |
|
559 | - /** |
|
560 | - * Return a string with the serialized representation of all debug info |
|
561 | - * @param string $charset_encoding the target charset encoding for the serialization |
|
562 | - * @return string an XML comment (or two) |
|
563 | - */ |
|
564 | - function serializeDebug($charset_encoding='') |
|
565 | - { |
|
566 | - // Tough encoding problem: which internal charset should we assume for debug info? |
|
567 | - // It might contain a copy of raw data received from client, ie with unknown encoding, |
|
568 | - // intermixed with php generated data and user generated data... |
|
569 | - // so we split it: system debug is base 64 encoded, |
|
570 | - // user debug info should be encoded by the end user using the INTERNAL_ENCODING |
|
571 | - $out = ''; |
|
572 | - if ($this->debug_info != '') |
|
573 | - { |
|
574 | - $out .= "<!-- SERVER DEBUG INFO (BASE64 ENCODED):\n".base64_encode($this->debug_info)."\n-->\n"; |
|
575 | - } |
|
576 | - if($GLOBALS['_xmlrpc_debuginfo']!='') |
|
577 | - { |
|
578 | - |
|
579 | - $out .= "<!-- DEBUG INFO:\n" . xmlrpc_encode_entitites(str_replace('--', '_-', $GLOBALS['_xmlrpc_debuginfo']), $GLOBALS['xmlrpc_internalencoding'], $charset_encoding) . "\n-->\n"; |
|
580 | - // NB: a better solution MIGHT be to use CDATA, but we need to insert it |
|
581 | - // into return payload AFTER the beginning tag |
|
582 | - //$out .= "<![CDATA[ DEBUG INFO:\n\n" . str_replace(']]>', ']_]_>', $GLOBALS['_xmlrpc_debuginfo']) . "\n]]>\n"; |
|
583 | - } |
|
584 | - return $out; |
|
585 | - } |
|
586 | - |
|
587 | - /** |
|
588 | - * Execute the xmlrpc request, printing the response |
|
589 | - * @param string $data the request body. If null, the http POST request will be examined |
|
590 | - * @return xmlrpcresp the response object (usually not used by caller...) |
|
591 | - * @access public |
|
592 | - */ |
|
593 | - function service($data=null, $return_payload=false) |
|
594 | - { |
|
595 | - if ($data === null) |
|
596 | - { |
|
597 | - // workaround for a known bug in php ver. 5.2.2 that broke $HTTP_RAW_POST_DATA |
|
598 | - $data = file_get_contents('php://input'); |
|
599 | - } |
|
600 | - $raw_data = $data; |
|
601 | - |
|
602 | - // reset internal debug info |
|
603 | - $this->debug_info = ''; |
|
604 | - |
|
605 | - // Echo back what we received, before parsing it |
|
606 | - if($this->debug > 1) |
|
607 | - { |
|
608 | - $this->debugmsg("+++GOT+++\n" . $data . "\n+++END+++"); |
|
609 | - } |
|
610 | - |
|
611 | - $r = $this->parseRequestHeaders($data, $req_charset, $resp_charset, $resp_encoding); |
|
612 | - if (!$r) |
|
613 | - { |
|
614 | - $r=$this->parseRequest($data, $req_charset); |
|
615 | - } |
|
616 | - |
|
617 | - // save full body of request into response, for more debugging usages |
|
618 | - $r->raw_data = $raw_data; |
|
619 | - |
|
620 | - if($this->debug > 2 && $GLOBALS['_xmlrpcs_occurred_errors']) |
|
621 | - { |
|
622 | - $this->debugmsg("+++PROCESSING ERRORS AND WARNINGS+++\n" . |
|
623 | - $GLOBALS['_xmlrpcs_occurred_errors'] . "+++END+++"); |
|
624 | - } |
|
625 | - |
|
626 | - $payload=$this->xml_header($resp_charset); |
|
627 | - if($this->debug > 0) |
|
628 | - { |
|
629 | - $payload = $payload . $this->serializeDebug($resp_charset); |
|
630 | - } |
|
631 | - |
|
632 | - // G. Giunta 2006-01-27: do not create response serialization if it has |
|
633 | - // already happened. Helps building json magic |
|
634 | - if (empty($r->payload)) |
|
635 | - { |
|
636 | - $r->serialize($resp_charset); |
|
637 | - } |
|
638 | - $payload = $payload . $r->payload; |
|
639 | - |
|
640 | - if ($return_payload) |
|
641 | - { |
|
642 | - return $payload; |
|
643 | - } |
|
644 | - |
|
645 | - // if we get a warning/error that has output some text before here, then we cannot |
|
646 | - // add a new header. We cannot say we are sending xml, either... |
|
647 | - if(!headers_sent()) |
|
648 | - { |
|
649 | - header('Content-Type: '.$r->content_type); |
|
650 | - // we do not know if client actually told us an accepted charset, but if he did |
|
651 | - // we have to tell him what we did |
|
652 | - header("Vary: Accept-Charset"); |
|
653 | - |
|
654 | - // http compression of output: only |
|
655 | - // if we can do it, and we want to do it, and client asked us to, |
|
656 | - // and php ini settings do not force it already |
|
657 | - $php_no_self_compress = !ini_get('zlib.output_compression') && (ini_get('output_handler') != 'ob_gzhandler'); |
|
658 | - if($this->compress_response && function_exists('gzencode') && $resp_encoding != '' |
|
659 | - && $php_no_self_compress) |
|
660 | - { |
|
661 | - if(strpos($resp_encoding, 'gzip') !== false) |
|
662 | - { |
|
663 | - $payload = gzencode($payload); |
|
664 | - header("Content-Encoding: gzip"); |
|
665 | - header("Vary: Accept-Encoding"); |
|
666 | - } |
|
667 | - elseif (strpos($resp_encoding, 'deflate') !== false) |
|
668 | - { |
|
669 | - $payload = gzcompress($payload); |
|
670 | - header("Content-Encoding: deflate"); |
|
671 | - header("Vary: Accept-Encoding"); |
|
672 | - } |
|
673 | - } |
|
674 | - |
|
675 | - // do not ouput content-length header if php is compressing output for us: |
|
676 | - // it will mess up measurements |
|
677 | - if($php_no_self_compress) |
|
678 | - { |
|
679 | - header('Content-Length: ' . (int)strlen($payload)); |
|
680 | - } |
|
681 | - } |
|
682 | - else |
|
683 | - { |
|
684 | - error_log('XML-RPC: '.__METHOD__.': http headers already sent before response is fully generated. Check for php warning or error messages'); |
|
685 | - } |
|
686 | - |
|
687 | - print $payload; |
|
688 | - |
|
689 | - // return request, in case subclasses want it |
|
690 | - return $r; |
|
691 | - } |
|
692 | - |
|
693 | - /** |
|
694 | - * Add a method to the dispatch map |
|
695 | - * @param string $methodname the name with which the method will be made available |
|
696 | - * @param string $function the php function that will get invoked |
|
697 | - * @param array $sig the array of valid method signatures |
|
698 | - * @param string $doc method documentation |
|
699 | - * @param array $sigdoc the array of valid method signatures docs (one string per param, one for return type) |
|
700 | - * @access public |
|
701 | - */ |
|
702 | - function add_to_map($methodname,$function,$sig=null,$doc=false,$sigdoc=false) |
|
703 | - { |
|
704 | - $this->dmap[$methodname] = array( |
|
705 | - 'function' => $function, |
|
706 | - 'docstring' => $doc |
|
707 | - ); |
|
708 | - if ($sig) |
|
709 | - { |
|
710 | - $this->dmap[$methodname]['signature'] = $sig; |
|
711 | - } |
|
712 | - if ($sigdoc) |
|
713 | - { |
|
714 | - $this->dmap[$methodname]['signature_docs'] = $sigdoc; |
|
715 | - } |
|
716 | - } |
|
717 | - |
|
718 | - /** |
|
719 | - * Verify type and number of parameters received against a list of known signatures |
|
720 | - * @param array $in array of either xmlrpcval objects or xmlrpc type definitions |
|
721 | - * @param array $sig array of known signatures to match against |
|
722 | - * @return array |
|
723 | - * @access private |
|
724 | - */ |
|
725 | - function verifySignature($in, $sig) |
|
726 | - { |
|
727 | - // check each possible signature in turn |
|
728 | - if (is_object($in)) |
|
729 | - { |
|
730 | - $numParams = $in->getNumParams(); |
|
731 | - } |
|
732 | - else |
|
733 | - { |
|
734 | - $numParams = count($in); |
|
735 | - } |
|
736 | - foreach($sig as $cursig) |
|
737 | - { |
|
738 | - if(count($cursig)==$numParams+1) |
|
739 | - { |
|
740 | - $itsOK=1; |
|
741 | - for($n=0; $n<$numParams; $n++) |
|
742 | - { |
|
743 | - if (is_object($in)) |
|
744 | - { |
|
745 | - $p=$in->getParam($n); |
|
746 | - if($p->kindOf() == 'scalar') |
|
747 | - { |
|
748 | - $pt=$p->scalartyp(); |
|
749 | - } |
|
750 | - else |
|
751 | - { |
|
752 | - $pt=$p->kindOf(); |
|
753 | - } |
|
754 | - } |
|
755 | - else |
|
756 | - { |
|
757 | - $pt= $in[$n] == 'i4' ? 'int' : strtolower($in[$n]); // dispatch maps never use i4... |
|
758 | - } |
|
759 | - |
|
760 | - // param index is $n+1, as first member of sig is return type |
|
761 | - if($pt != $cursig[$n+1] && $cursig[$n+1] != $GLOBALS['xmlrpcValue']) |
|
762 | - { |
|
763 | - $itsOK=0; |
|
764 | - $pno=$n+1; |
|
765 | - $wanted=$cursig[$n+1]; |
|
766 | - $got=$pt; |
|
767 | - break; |
|
768 | - } |
|
769 | - } |
|
770 | - if($itsOK) |
|
771 | - { |
|
772 | - return array(1,''); |
|
773 | - } |
|
774 | - } |
|
775 | - } |
|
776 | - if(isset($wanted)) |
|
777 | - { |
|
778 | - return array(0, "Wanted ${wanted}, got ${got} at param ${pno}"); |
|
779 | - } |
|
780 | - else |
|
781 | - { |
|
782 | - return array(0, "No method signature matches number of parameters"); |
|
783 | - } |
|
784 | - } |
|
785 | - |
|
786 | - /** |
|
787 | - * Parse http headers received along with xmlrpc request. If needed, inflate request |
|
788 | - * @return mixed null on success or an xmlrpcresp |
|
789 | - * @access private |
|
790 | - */ |
|
791 | - function parseRequestHeaders(&$data, &$req_encoding, &$resp_encoding, &$resp_compression) |
|
792 | - { |
|
793 | - // check if $_SERVER is populated: it might have been disabled via ini file |
|
794 | - // (this is true even when in CLI mode) |
|
795 | - if (count($_SERVER) == 0) |
|
796 | - { |
|
797 | - error_log('XML-RPC: '.__METHOD__.': cannot parse request headers as $_SERVER is not populated'); |
|
798 | - } |
|
799 | - |
|
800 | - if($this->debug > 1) |
|
801 | - { |
|
802 | - if(function_exists('getallheaders')) |
|
803 | - { |
|
804 | - $this->debugmsg(''); // empty line |
|
805 | - foreach(getallheaders() as $name => $val) |
|
806 | - { |
|
807 | - $this->debugmsg("HEADER: $name: $val"); |
|
808 | - } |
|
809 | - } |
|
810 | - |
|
811 | - } |
|
812 | - |
|
813 | - if(isset($_SERVER['HTTP_CONTENT_ENCODING'])) |
|
814 | - { |
|
815 | - $content_encoding = str_replace('x-', '', $_SERVER['HTTP_CONTENT_ENCODING']); |
|
816 | - } |
|
817 | - else |
|
818 | - { |
|
819 | - $content_encoding = ''; |
|
820 | - } |
|
821 | - |
|
822 | - // check if request body has been compressed and decompress it |
|
823 | - if($content_encoding != '' && strlen($data)) |
|
824 | - { |
|
825 | - if($content_encoding == 'deflate' || $content_encoding == 'gzip') |
|
826 | - { |
|
827 | - // if decoding works, use it. else assume data wasn't gzencoded |
|
828 | - if(function_exists('gzinflate') && in_array($content_encoding, $this->accepted_compression)) |
|
829 | - { |
|
830 | - if($content_encoding == 'deflate' && $degzdata = @gzuncompress($data)) |
|
831 | - { |
|
832 | - $data = $degzdata; |
|
833 | - if($this->debug > 1) |
|
834 | - { |
|
835 | - $this->debugmsg("\n+++INFLATED REQUEST+++[".strlen($data)." chars]+++\n" . $data . "\n+++END+++"); |
|
836 | - } |
|
837 | - } |
|
838 | - elseif($content_encoding == 'gzip' && $degzdata = @gzinflate(substr($data, 10))) |
|
839 | - { |
|
840 | - $data = $degzdata; |
|
841 | - if($this->debug > 1) |
|
842 | - $this->debugmsg("+++INFLATED REQUEST+++[".strlen($data)." chars]+++\n" . $data . "\n+++END+++"); |
|
843 | - } |
|
844 | - else |
|
845 | - { |
|
846 | - $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['server_decompress_fail'], $GLOBALS['xmlrpcstr']['server_decompress_fail']); |
|
847 | - return $r; |
|
848 | - } |
|
849 | - } |
|
850 | - else |
|
851 | - { |
|
852 | - //error_log('The server sent deflated data. Your php install must have the Zlib extension compiled in to support this.'); |
|
853 | - $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['server_cannot_decompress'], $GLOBALS['xmlrpcstr']['server_cannot_decompress']); |
|
854 | - return $r; |
|
855 | - } |
|
856 | - } |
|
857 | - } |
|
858 | - |
|
859 | - // check if client specified accepted charsets, and if we know how to fulfill |
|
860 | - // the request |
|
861 | - if ($this->response_charset_encoding == 'auto') |
|
862 | - { |
|
863 | - $resp_encoding = ''; |
|
864 | - if (isset($_SERVER['HTTP_ACCEPT_CHARSET'])) |
|
865 | - { |
|
866 | - // here we should check if we can match the client-requested encoding |
|
867 | - // with the encodings we know we can generate. |
|
868 | - /// @todo we should parse q=0.x preferences instead of getting first charset specified... |
|
869 | - $client_accepted_charsets = explode(',', strtoupper($_SERVER['HTTP_ACCEPT_CHARSET'])); |
|
870 | - // Give preference to internal encoding |
|
871 | - $known_charsets = array($GLOBALS['xmlrpc_internalencoding'], 'UTF-8', 'ISO-8859-1', 'US-ASCII'); |
|
872 | - foreach ($known_charsets as $charset) |
|
873 | - { |
|
874 | - foreach ($client_accepted_charsets as $accepted) |
|
875 | - if (strpos($accepted, $charset) === 0) |
|
876 | - { |
|
877 | - $resp_encoding = $charset; |
|
878 | - break; |
|
879 | - } |
|
880 | - if ($resp_encoding) |
|
881 | - break; |
|
882 | - } |
|
883 | - } |
|
884 | - } |
|
885 | - else |
|
886 | - { |
|
887 | - $resp_encoding = $this->response_charset_encoding; |
|
888 | - } |
|
889 | - |
|
890 | - if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) |
|
891 | - { |
|
892 | - $resp_compression = $_SERVER['HTTP_ACCEPT_ENCODING']; |
|
893 | - } |
|
894 | - else |
|
895 | - { |
|
896 | - $resp_compression = ''; |
|
897 | - } |
|
898 | - |
|
899 | - // 'guestimate' request encoding |
|
900 | - /// @todo check if mbstring is enabled and automagic input conversion is on: it might mingle with this check??? |
|
901 | - $req_encoding = guess_encoding(isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '', |
|
902 | - $data); |
|
903 | - |
|
904 | - return null; |
|
905 | - } |
|
906 | - |
|
907 | - /** |
|
908 | - * Parse an xml chunk containing an xmlrpc request and execute the corresponding |
|
909 | - * php function registered with the server |
|
910 | - * @param string $data the xml request |
|
911 | - * @param string $req_encoding (optional) the charset encoding of the xml request |
|
912 | - * @return xmlrpcresp |
|
913 | - * @access private |
|
914 | - */ |
|
915 | - function parseRequest($data, $req_encoding='') |
|
916 | - { |
|
917 | - // 2005/05/07 commented and moved into caller function code |
|
918 | - //if($data=='') |
|
919 | - //{ |
|
920 | - // $data=$GLOBALS['HTTP_RAW_POST_DATA']; |
|
921 | - //} |
|
922 | - |
|
923 | - // G. Giunta 2005/02/13: we do NOT expect to receive html entities |
|
924 | - // so we do not try to convert them into xml character entities |
|
925 | - //$data = xmlrpc_html_entity_xlate($data); |
|
926 | - |
|
927 | - $GLOBALS['_xh']=array(); |
|
928 | - $GLOBALS['_xh']['ac']=''; |
|
929 | - $GLOBALS['_xh']['stack']=array(); |
|
930 | - $GLOBALS['_xh']['valuestack'] = array(); |
|
931 | - $GLOBALS['_xh']['params']=array(); |
|
932 | - $GLOBALS['_xh']['pt']=array(); |
|
933 | - $GLOBALS['_xh']['isf']=0; |
|
934 | - $GLOBALS['_xh']['isf_reason']=''; |
|
935 | - $GLOBALS['_xh']['method']=false; // so we can check later if we got a methodname or not |
|
936 | - $GLOBALS['_xh']['rt']=''; |
|
937 | - |
|
938 | - // decompose incoming XML into request structure |
|
939 | - |
|
940 | - if ($req_encoding != '') |
|
941 | - { |
|
942 | - // Since parsing will fail if charset is not specified in the xml prologue, |
|
943 | - // the encoding is not UTF8 and there are non-ascii chars in the text, we try to work round that... |
|
944 | - // The following code might be better for mb_string enabled installs, but |
|
945 | - // makes the lib about 200% slower... |
|
946 | - //if (!is_valid_charset($req_encoding, array('UTF-8'))) |
|
947 | - if (!in_array($req_encoding, array('UTF-8', 'US-ASCII')) && !has_encoding($data)) { |
|
948 | - if ($req_encoding == 'ISO-8859-1') { |
|
949 | - $data = utf8_encode($data); |
|
950 | - } else { |
|
951 | - if (extension_loaded('mbstring')) { |
|
952 | - $data = mb_convert_encoding($data, 'UTF-8', $req_encoding); |
|
953 | - } else { |
|
954 | - error_log('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of received request: ' . $req_encoding); |
|
955 | - } |
|
956 | - } |
|
957 | - } |
|
958 | - } |
|
959 | - |
|
960 | - $parser = xml_parser_create(); |
|
961 | - xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true); |
|
962 | - // G. Giunta 2005/02/13: PHP internally uses ISO-8859-1, so we have to tell |
|
963 | - // the xml parser to give us back data in the expected charset |
|
964 | - // What if internal encoding is not in one of the 3 allowed? |
|
965 | - // we use the broadest one, ie. utf8 |
|
966 | - // This allows to send data which is native in various charset, |
|
967 | - // by extending xmlrpc_encode_entitites() and setting xmlrpc_internalencoding |
|
968 | - if (!in_array($GLOBALS['xmlrpc_internalencoding'], array('UTF-8', 'ISO-8859-1', 'US-ASCII'))) |
|
969 | - { |
|
970 | - xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8'); |
|
971 | - } |
|
972 | - else |
|
973 | - { |
|
974 | - xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $GLOBALS['xmlrpc_internalencoding']); |
|
975 | - } |
|
976 | - |
|
977 | - if ($this->functions_parameters_type != 'xmlrpcvals') |
|
978 | - xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee_fast'); |
|
979 | - else |
|
980 | - xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee'); |
|
981 | - xml_set_character_data_handler($parser, 'xmlrpc_cd'); |
|
982 | - xml_set_default_handler($parser, 'xmlrpc_dh'); |
|
983 | - if(!xml_parse($parser, $data, 1)) |
|
984 | - { |
|
985 | - // return XML error as a faultCode |
|
986 | - $r=new xmlrpcresp(0, |
|
987 | - $GLOBALS['xmlrpcerrxml']+xml_get_error_code($parser), |
|
988 | - sprintf('XML error: %s at line %d, column %d', |
|
989 | - xml_error_string(xml_get_error_code($parser)), |
|
990 | - xml_get_current_line_number($parser), xml_get_current_column_number($parser))); |
|
991 | - xml_parser_free($parser); |
|
992 | - } |
|
993 | - elseif ($GLOBALS['_xh']['isf']) |
|
994 | - { |
|
995 | - xml_parser_free($parser); |
|
996 | - $r=new xmlrpcresp(0, |
|
997 | - $GLOBALS['xmlrpcerr']['invalid_request'], |
|
998 | - $GLOBALS['xmlrpcstr']['invalid_request'] . ' ' . $GLOBALS['_xh']['isf_reason']); |
|
999 | - } |
|
1000 | - else |
|
1001 | - { |
|
1002 | - xml_parser_free($parser); |
|
1003 | - // small layering violation in favor of speed and memory usage: |
|
1004 | - // we should allow the 'execute' method handle this, but in the |
|
1005 | - // most common scenario (xmlrpcvals type server with some methods |
|
1006 | - // registered as phpvals) that would mean a useless encode+decode pass |
|
1007 | - if ($this->functions_parameters_type != 'xmlrpcvals' || (isset($this->dmap[$GLOBALS['_xh']['method']]['parameters_type']) && ($this->dmap[$GLOBALS['_xh']['method']]['parameters_type'] == 'phpvals'))) |
|
1008 | - { |
|
1009 | - if($this->debug > 1) |
|
1010 | - { |
|
1011 | - $this->debugmsg("\n+++PARSED+++\n".var_export($GLOBALS['_xh']['params'], true)."\n+++END+++"); |
|
1012 | - } |
|
1013 | - $r = $this->execute($GLOBALS['_xh']['method'], $GLOBALS['_xh']['params'], $GLOBALS['_xh']['pt']); |
|
1014 | - } |
|
1015 | - else |
|
1016 | - { |
|
1017 | - // build an xmlrpcmsg object with data parsed from xml |
|
1018 | - $m=new xmlrpcmsg($GLOBALS['_xh']['method']); |
|
1019 | - // now add parameters in |
|
1020 | - for($i=0; $i<count($GLOBALS['_xh']['params']); $i++) |
|
1021 | - { |
|
1022 | - $m->addParam($GLOBALS['_xh']['params'][$i]); |
|
1023 | - } |
|
1024 | - |
|
1025 | - if($this->debug > 1) |
|
1026 | - { |
|
1027 | - $this->debugmsg("\n+++PARSED+++\n".var_export($m, true)."\n+++END+++"); |
|
1028 | - } |
|
1029 | - $r = $this->execute($m); |
|
1030 | - } |
|
1031 | - } |
|
1032 | - return $r; |
|
1033 | - } |
|
1034 | - |
|
1035 | - /** |
|
1036 | - * Execute a method invoked by the client, checking parameters used |
|
1037 | - * @param mixed $m either an xmlrpcmsg obj or a method name |
|
1038 | - * @param array $params array with method parameters as php types (if m is method name only) |
|
1039 | - * @param array $paramtypes array with xmlrpc types of method parameters (if m is method name only) |
|
1040 | - * @return xmlrpcresp |
|
1041 | - * @access private |
|
1042 | - */ |
|
1043 | - function execute($m, $params=null, $paramtypes=null) |
|
1044 | - { |
|
1045 | - if (is_object($m)) |
|
1046 | - { |
|
1047 | - $methName = $m->method(); |
|
1048 | - } |
|
1049 | - else |
|
1050 | - { |
|
1051 | - $methName = $m; |
|
1052 | - } |
|
1053 | - $sysCall = $this->allow_system_funcs && (strpos($methName, "system.") === 0); |
|
1054 | - $dmap = $sysCall ? $GLOBALS['_xmlrpcs_dmap'] : $this->dmap; |
|
1055 | - |
|
1056 | - if(!isset($dmap[$methName]['function'])) |
|
1057 | - { |
|
1058 | - // No such method |
|
1059 | - return new xmlrpcresp(0, |
|
1060 | - $GLOBALS['xmlrpcerr']['unknown_method'], |
|
1061 | - $GLOBALS['xmlrpcstr']['unknown_method']); |
|
1062 | - } |
|
1063 | - |
|
1064 | - // Check signature |
|
1065 | - if(isset($dmap[$methName]['signature'])) |
|
1066 | - { |
|
1067 | - $sig = $dmap[$methName]['signature']; |
|
1068 | - if (is_object($m)) |
|
1069 | - { |
|
1070 | - list($ok, $errstr) = $this->verifySignature($m, $sig); |
|
1071 | - } |
|
1072 | - else |
|
1073 | - { |
|
1074 | - list($ok, $errstr) = $this->verifySignature($paramtypes, $sig); |
|
1075 | - } |
|
1076 | - if(!$ok) |
|
1077 | - { |
|
1078 | - // Didn't match. |
|
1079 | - return new xmlrpcresp( |
|
1080 | - 0, |
|
1081 | - $GLOBALS['xmlrpcerr']['incorrect_params'], |
|
1082 | - $GLOBALS['xmlrpcstr']['incorrect_params'] . ": ${errstr}" |
|
1083 | - ); |
|
1084 | - } |
|
1085 | - } |
|
1086 | - |
|
1087 | - $func = $dmap[$methName]['function']; |
|
1088 | - // let the 'class::function' syntax be accepted in dispatch maps |
|
1089 | - if(is_string($func) && strpos($func, '::')) |
|
1090 | - { |
|
1091 | - $func = explode('::', $func); |
|
1092 | - } |
|
1093 | - // verify that function to be invoked is in fact callable |
|
1094 | - if(!is_callable($func)) |
|
1095 | - { |
|
1096 | - error_log("XML-RPC: ".__METHOD__.": function $func registered as method handler is not callable"); |
|
1097 | - return new xmlrpcresp( |
|
1098 | - 0, |
|
1099 | - $GLOBALS['xmlrpcerr']['server_error'], |
|
1100 | - $GLOBALS['xmlrpcstr']['server_error'] . ": no function matches method" |
|
1101 | - ); |
|
1102 | - } |
|
1103 | - |
|
1104 | - // If debug level is 3, we should catch all errors generated during |
|
1105 | - // processing of user function, and log them as part of response |
|
1106 | - if($this->debug > 2) |
|
1107 | - { |
|
1108 | - $GLOBALS['_xmlrpcs_prev_ehandler'] = set_error_handler('_xmlrpcs_errorHandler'); |
|
1109 | - } |
|
1110 | - try |
|
1111 | - { |
|
1112 | - // Allow mixed-convention servers |
|
1113 | - if (is_object($m)) |
|
1114 | - { |
|
1115 | - if($sysCall) |
|
1116 | - { |
|
1117 | - $r = call_user_func($func, $this, $m); |
|
1118 | - } |
|
1119 | - else |
|
1120 | - { |
|
1121 | - $r = call_user_func($func, $m); |
|
1122 | - } |
|
1123 | - if (!is_a($r, 'xmlrpcresp')) |
|
1124 | - { |
|
1125 | - error_log("XML-RPC: ".__METHOD__.": function $func registered as method handler does not return an xmlrpcresp object"); |
|
1126 | - if (is_a($r, 'xmlrpcval')) |
|
1127 | - { |
|
1128 | - $r = new xmlrpcresp($r); |
|
1129 | - } |
|
1130 | - else |
|
1131 | - { |
|
1132 | - $r = new xmlrpcresp( |
|
1133 | - 0, |
|
1134 | - $GLOBALS['xmlrpcerr']['server_error'], |
|
1135 | - $GLOBALS['xmlrpcstr']['server_error'] . ": function does not return xmlrpcresp object" |
|
1136 | - ); |
|
1137 | - } |
|
1138 | - } |
|
1139 | - } |
|
1140 | - else |
|
1141 | - { |
|
1142 | - // call a 'plain php' function |
|
1143 | - if($sysCall) |
|
1144 | - { |
|
1145 | - array_unshift($params, $this); |
|
1146 | - $r = call_user_func_array($func, $params); |
|
1147 | - } |
|
1148 | - else |
|
1149 | - { |
|
1150 | - // 3rd API convention for method-handling functions: EPI-style |
|
1151 | - if ($this->functions_parameters_type == 'epivals') |
|
1152 | - { |
|
1153 | - $r = call_user_func_array($func, array($methName, $params, $this->user_data)); |
|
1154 | - // mimic EPI behaviour: if we get an array that looks like an error, make it |
|
1155 | - // an eror response |
|
1156 | - if (is_array($r) && array_key_exists('faultCode', $r) && array_key_exists('faultString', $r)) |
|
1157 | - { |
|
1158 | - $r = new xmlrpcresp(0, (integer)$r['faultCode'], (string)$r['faultString']); |
|
1159 | - } |
|
1160 | - else |
|
1161 | - { |
|
1162 | - // functions using EPI api should NOT return resp objects, |
|
1163 | - // so make sure we encode the return type correctly |
|
1164 | - $r = new xmlrpcresp(php_xmlrpc_encode($r, array('extension_api'))); |
|
1165 | - } |
|
1166 | - } |
|
1167 | - else |
|
1168 | - { |
|
1169 | - $r = call_user_func_array($func, $params); |
|
1170 | - } |
|
1171 | - } |
|
1172 | - // the return type can be either an xmlrpcresp object or a plain php value... |
|
1173 | - if (!is_a($r, 'xmlrpcresp')) |
|
1174 | - { |
|
1175 | - // what should we assume here about automatic encoding of datetimes |
|
1176 | - // and php classes instances??? |
|
1177 | - $r = new xmlrpcresp(php_xmlrpc_encode($r, $this->phpvals_encoding_options)); |
|
1178 | - } |
|
1179 | - } |
|
1180 | - } |
|
1181 | - catch(Exception $e) |
|
1182 | - { |
|
1183 | - // (barring errors in the lib) an uncatched exception happened |
|
1184 | - // in the called function, we wrap it in a proper error-response |
|
1185 | - switch($this->exception_handling) |
|
1186 | - { |
|
1187 | - case 2: |
|
1188 | - throw $e; |
|
1189 | - break; |
|
1190 | - case 1: |
|
1191 | - $r = new xmlrpcresp(0, $e->getCode(), $e->getMessage()); |
|
1192 | - break; |
|
1193 | - default: |
|
1194 | - $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['server_error'], $GLOBALS['xmlrpcstr']['server_error']); |
|
1195 | - } |
|
1196 | - } |
|
1197 | - if($this->debug > 2) |
|
1198 | - { |
|
1199 | - // note: restore the error handler we found before calling the |
|
1200 | - // user func, even if it has been changed inside the func itself |
|
1201 | - if($GLOBALS['_xmlrpcs_prev_ehandler']) |
|
1202 | - { |
|
1203 | - set_error_handler($GLOBALS['_xmlrpcs_prev_ehandler']); |
|
1204 | - } |
|
1205 | - else |
|
1206 | - { |
|
1207 | - restore_error_handler(); |
|
1208 | - } |
|
1209 | - } |
|
1210 | - return $r; |
|
1211 | - } |
|
1212 | - |
|
1213 | - /** |
|
1214 | - * add a string to the 'internal debug message' (separate from 'user debug message') |
|
1215 | - * @param string $string |
|
1216 | - * @access private |
|
1217 | - */ |
|
1218 | - function debugmsg($string) |
|
1219 | - { |
|
1220 | - $this->debug_info .= $string."\n"; |
|
1221 | - } |
|
1222 | - |
|
1223 | - /** |
|
1224 | - * @access private |
|
1225 | - */ |
|
1226 | - function xml_header($charset_encoding='') |
|
1227 | - { |
|
1228 | - if ($charset_encoding != '') |
|
1229 | - { |
|
1230 | - return "<?xml version=\"1.0\" encoding=\"$charset_encoding\"?" . ">\n"; |
|
1231 | - } |
|
1232 | - else |
|
1233 | - { |
|
1234 | - return "<?xml version=\"1.0\"?" . ">\n"; |
|
1235 | - } |
|
1236 | - } |
|
1237 | - |
|
1238 | - /** |
|
1239 | - * A debugging routine: just echoes back the input packet as a string value |
|
1240 | - * DEPRECATED! |
|
1241 | - */ |
|
1242 | - function echoInput() |
|
1243 | - { |
|
1244 | - $r=new xmlrpcresp(new xmlrpcval( "'Aha said I: '" . $GLOBALS['HTTP_RAW_POST_DATA'], 'string')); |
|
1245 | - print $r->serialize(); |
|
1246 | - } |
|
1247 | - } |
|
522 | + if($dispMap) |
|
523 | + { |
|
524 | + $this->dmap = $dispMap; |
|
525 | + if($serviceNow) |
|
526 | + { |
|
527 | + $this->service(); |
|
528 | + } |
|
529 | + } |
|
530 | + } |
|
531 | + |
|
532 | + /** |
|
533 | + * @deprecated |
|
534 | + */ |
|
535 | + function xmlrpc_client($dispMap=null, $serviceNow=true) |
|
536 | + { |
|
537 | + self::__construct($dispMap, $serviceNow); |
|
538 | + } |
|
539 | + |
|
540 | + /** |
|
541 | + * Set debug level of server. |
|
542 | + * @param integer $in debug lvl: determines info added to xmlrpc responses (as xml comments) |
|
543 | + * 0 = no debug info, |
|
544 | + * 1 = msgs set from user with debugmsg(), |
|
545 | + * 2 = add complete xmlrpc request (headers and body), |
|
546 | + * 3 = add also all processing warnings happened during method processing |
|
547 | + * (NB: this involves setting a custom error handler, and might interfere |
|
548 | + * with the standard processing of the php function exposed as method. In |
|
549 | + * particular, triggering an USER_ERROR level error will not halt script |
|
550 | + * execution anymore, but just end up logged in the xmlrpc response) |
|
551 | + * Note that info added at level 2 and 3 will be base64 encoded |
|
552 | + * @access public |
|
553 | + */ |
|
554 | + function setDebug($in) |
|
555 | + { |
|
556 | + $this->debug=$in; |
|
557 | + } |
|
558 | + |
|
559 | + /** |
|
560 | + * Return a string with the serialized representation of all debug info |
|
561 | + * @param string $charset_encoding the target charset encoding for the serialization |
|
562 | + * @return string an XML comment (or two) |
|
563 | + */ |
|
564 | + function serializeDebug($charset_encoding='') |
|
565 | + { |
|
566 | + // Tough encoding problem: which internal charset should we assume for debug info? |
|
567 | + // It might contain a copy of raw data received from client, ie with unknown encoding, |
|
568 | + // intermixed with php generated data and user generated data... |
|
569 | + // so we split it: system debug is base 64 encoded, |
|
570 | + // user debug info should be encoded by the end user using the INTERNAL_ENCODING |
|
571 | + $out = ''; |
|
572 | + if ($this->debug_info != '') |
|
573 | + { |
|
574 | + $out .= "<!-- SERVER DEBUG INFO (BASE64 ENCODED):\n".base64_encode($this->debug_info)."\n-->\n"; |
|
575 | + } |
|
576 | + if($GLOBALS['_xmlrpc_debuginfo']!='') |
|
577 | + { |
|
578 | + |
|
579 | + $out .= "<!-- DEBUG INFO:\n" . xmlrpc_encode_entitites(str_replace('--', '_-', $GLOBALS['_xmlrpc_debuginfo']), $GLOBALS['xmlrpc_internalencoding'], $charset_encoding) . "\n-->\n"; |
|
580 | + // NB: a better solution MIGHT be to use CDATA, but we need to insert it |
|
581 | + // into return payload AFTER the beginning tag |
|
582 | + //$out .= "<![CDATA[ DEBUG INFO:\n\n" . str_replace(']]>', ']_]_>', $GLOBALS['_xmlrpc_debuginfo']) . "\n]]>\n"; |
|
583 | + } |
|
584 | + return $out; |
|
585 | + } |
|
586 | + |
|
587 | + /** |
|
588 | + * Execute the xmlrpc request, printing the response |
|
589 | + * @param string $data the request body. If null, the http POST request will be examined |
|
590 | + * @return xmlrpcresp the response object (usually not used by caller...) |
|
591 | + * @access public |
|
592 | + */ |
|
593 | + function service($data=null, $return_payload=false) |
|
594 | + { |
|
595 | + if ($data === null) |
|
596 | + { |
|
597 | + // workaround for a known bug in php ver. 5.2.2 that broke $HTTP_RAW_POST_DATA |
|
598 | + $data = file_get_contents('php://input'); |
|
599 | + } |
|
600 | + $raw_data = $data; |
|
601 | + |
|
602 | + // reset internal debug info |
|
603 | + $this->debug_info = ''; |
|
604 | + |
|
605 | + // Echo back what we received, before parsing it |
|
606 | + if($this->debug > 1) |
|
607 | + { |
|
608 | + $this->debugmsg("+++GOT+++\n" . $data . "\n+++END+++"); |
|
609 | + } |
|
610 | + |
|
611 | + $r = $this->parseRequestHeaders($data, $req_charset, $resp_charset, $resp_encoding); |
|
612 | + if (!$r) |
|
613 | + { |
|
614 | + $r=$this->parseRequest($data, $req_charset); |
|
615 | + } |
|
616 | + |
|
617 | + // save full body of request into response, for more debugging usages |
|
618 | + $r->raw_data = $raw_data; |
|
619 | + |
|
620 | + if($this->debug > 2 && $GLOBALS['_xmlrpcs_occurred_errors']) |
|
621 | + { |
|
622 | + $this->debugmsg("+++PROCESSING ERRORS AND WARNINGS+++\n" . |
|
623 | + $GLOBALS['_xmlrpcs_occurred_errors'] . "+++END+++"); |
|
624 | + } |
|
625 | + |
|
626 | + $payload=$this->xml_header($resp_charset); |
|
627 | + if($this->debug > 0) |
|
628 | + { |
|
629 | + $payload = $payload . $this->serializeDebug($resp_charset); |
|
630 | + } |
|
631 | + |
|
632 | + // G. Giunta 2006-01-27: do not create response serialization if it has |
|
633 | + // already happened. Helps building json magic |
|
634 | + if (empty($r->payload)) |
|
635 | + { |
|
636 | + $r->serialize($resp_charset); |
|
637 | + } |
|
638 | + $payload = $payload . $r->payload; |
|
639 | + |
|
640 | + if ($return_payload) |
|
641 | + { |
|
642 | + return $payload; |
|
643 | + } |
|
644 | + |
|
645 | + // if we get a warning/error that has output some text before here, then we cannot |
|
646 | + // add a new header. We cannot say we are sending xml, either... |
|
647 | + if(!headers_sent()) |
|
648 | + { |
|
649 | + header('Content-Type: '.$r->content_type); |
|
650 | + // we do not know if client actually told us an accepted charset, but if he did |
|
651 | + // we have to tell him what we did |
|
652 | + header("Vary: Accept-Charset"); |
|
653 | + |
|
654 | + // http compression of output: only |
|
655 | + // if we can do it, and we want to do it, and client asked us to, |
|
656 | + // and php ini settings do not force it already |
|
657 | + $php_no_self_compress = !ini_get('zlib.output_compression') && (ini_get('output_handler') != 'ob_gzhandler'); |
|
658 | + if($this->compress_response && function_exists('gzencode') && $resp_encoding != '' |
|
659 | + && $php_no_self_compress) |
|
660 | + { |
|
661 | + if(strpos($resp_encoding, 'gzip') !== false) |
|
662 | + { |
|
663 | + $payload = gzencode($payload); |
|
664 | + header("Content-Encoding: gzip"); |
|
665 | + header("Vary: Accept-Encoding"); |
|
666 | + } |
|
667 | + elseif (strpos($resp_encoding, 'deflate') !== false) |
|
668 | + { |
|
669 | + $payload = gzcompress($payload); |
|
670 | + header("Content-Encoding: deflate"); |
|
671 | + header("Vary: Accept-Encoding"); |
|
672 | + } |
|
673 | + } |
|
674 | + |
|
675 | + // do not ouput content-length header if php is compressing output for us: |
|
676 | + // it will mess up measurements |
|
677 | + if($php_no_self_compress) |
|
678 | + { |
|
679 | + header('Content-Length: ' . (int)strlen($payload)); |
|
680 | + } |
|
681 | + } |
|
682 | + else |
|
683 | + { |
|
684 | + error_log('XML-RPC: '.__METHOD__.': http headers already sent before response is fully generated. Check for php warning or error messages'); |
|
685 | + } |
|
686 | + |
|
687 | + print $payload; |
|
688 | + |
|
689 | + // return request, in case subclasses want it |
|
690 | + return $r; |
|
691 | + } |
|
692 | + |
|
693 | + /** |
|
694 | + * Add a method to the dispatch map |
|
695 | + * @param string $methodname the name with which the method will be made available |
|
696 | + * @param string $function the php function that will get invoked |
|
697 | + * @param array $sig the array of valid method signatures |
|
698 | + * @param string $doc method documentation |
|
699 | + * @param array $sigdoc the array of valid method signatures docs (one string per param, one for return type) |
|
700 | + * @access public |
|
701 | + */ |
|
702 | + function add_to_map($methodname,$function,$sig=null,$doc=false,$sigdoc=false) |
|
703 | + { |
|
704 | + $this->dmap[$methodname] = array( |
|
705 | + 'function' => $function, |
|
706 | + 'docstring' => $doc |
|
707 | + ); |
|
708 | + if ($sig) |
|
709 | + { |
|
710 | + $this->dmap[$methodname]['signature'] = $sig; |
|
711 | + } |
|
712 | + if ($sigdoc) |
|
713 | + { |
|
714 | + $this->dmap[$methodname]['signature_docs'] = $sigdoc; |
|
715 | + } |
|
716 | + } |
|
717 | + |
|
718 | + /** |
|
719 | + * Verify type and number of parameters received against a list of known signatures |
|
720 | + * @param array $in array of either xmlrpcval objects or xmlrpc type definitions |
|
721 | + * @param array $sig array of known signatures to match against |
|
722 | + * @return array |
|
723 | + * @access private |
|
724 | + */ |
|
725 | + function verifySignature($in, $sig) |
|
726 | + { |
|
727 | + // check each possible signature in turn |
|
728 | + if (is_object($in)) |
|
729 | + { |
|
730 | + $numParams = $in->getNumParams(); |
|
731 | + } |
|
732 | + else |
|
733 | + { |
|
734 | + $numParams = count($in); |
|
735 | + } |
|
736 | + foreach($sig as $cursig) |
|
737 | + { |
|
738 | + if(count($cursig)==$numParams+1) |
|
739 | + { |
|
740 | + $itsOK=1; |
|
741 | + for($n=0; $n<$numParams; $n++) |
|
742 | + { |
|
743 | + if (is_object($in)) |
|
744 | + { |
|
745 | + $p=$in->getParam($n); |
|
746 | + if($p->kindOf() == 'scalar') |
|
747 | + { |
|
748 | + $pt=$p->scalartyp(); |
|
749 | + } |
|
750 | + else |
|
751 | + { |
|
752 | + $pt=$p->kindOf(); |
|
753 | + } |
|
754 | + } |
|
755 | + else |
|
756 | + { |
|
757 | + $pt= $in[$n] == 'i4' ? 'int' : strtolower($in[$n]); // dispatch maps never use i4... |
|
758 | + } |
|
759 | + |
|
760 | + // param index is $n+1, as first member of sig is return type |
|
761 | + if($pt != $cursig[$n+1] && $cursig[$n+1] != $GLOBALS['xmlrpcValue']) |
|
762 | + { |
|
763 | + $itsOK=0; |
|
764 | + $pno=$n+1; |
|
765 | + $wanted=$cursig[$n+1]; |
|
766 | + $got=$pt; |
|
767 | + break; |
|
768 | + } |
|
769 | + } |
|
770 | + if($itsOK) |
|
771 | + { |
|
772 | + return array(1,''); |
|
773 | + } |
|
774 | + } |
|
775 | + } |
|
776 | + if(isset($wanted)) |
|
777 | + { |
|
778 | + return array(0, "Wanted ${wanted}, got ${got} at param ${pno}"); |
|
779 | + } |
|
780 | + else |
|
781 | + { |
|
782 | + return array(0, "No method signature matches number of parameters"); |
|
783 | + } |
|
784 | + } |
|
785 | + |
|
786 | + /** |
|
787 | + * Parse http headers received along with xmlrpc request. If needed, inflate request |
|
788 | + * @return mixed null on success or an xmlrpcresp |
|
789 | + * @access private |
|
790 | + */ |
|
791 | + function parseRequestHeaders(&$data, &$req_encoding, &$resp_encoding, &$resp_compression) |
|
792 | + { |
|
793 | + // check if $_SERVER is populated: it might have been disabled via ini file |
|
794 | + // (this is true even when in CLI mode) |
|
795 | + if (count($_SERVER) == 0) |
|
796 | + { |
|
797 | + error_log('XML-RPC: '.__METHOD__.': cannot parse request headers as $_SERVER is not populated'); |
|
798 | + } |
|
799 | + |
|
800 | + if($this->debug > 1) |
|
801 | + { |
|
802 | + if(function_exists('getallheaders')) |
|
803 | + { |
|
804 | + $this->debugmsg(''); // empty line |
|
805 | + foreach(getallheaders() as $name => $val) |
|
806 | + { |
|
807 | + $this->debugmsg("HEADER: $name: $val"); |
|
808 | + } |
|
809 | + } |
|
810 | + |
|
811 | + } |
|
812 | + |
|
813 | + if(isset($_SERVER['HTTP_CONTENT_ENCODING'])) |
|
814 | + { |
|
815 | + $content_encoding = str_replace('x-', '', $_SERVER['HTTP_CONTENT_ENCODING']); |
|
816 | + } |
|
817 | + else |
|
818 | + { |
|
819 | + $content_encoding = ''; |
|
820 | + } |
|
821 | + |
|
822 | + // check if request body has been compressed and decompress it |
|
823 | + if($content_encoding != '' && strlen($data)) |
|
824 | + { |
|
825 | + if($content_encoding == 'deflate' || $content_encoding == 'gzip') |
|
826 | + { |
|
827 | + // if decoding works, use it. else assume data wasn't gzencoded |
|
828 | + if(function_exists('gzinflate') && in_array($content_encoding, $this->accepted_compression)) |
|
829 | + { |
|
830 | + if($content_encoding == 'deflate' && $degzdata = @gzuncompress($data)) |
|
831 | + { |
|
832 | + $data = $degzdata; |
|
833 | + if($this->debug > 1) |
|
834 | + { |
|
835 | + $this->debugmsg("\n+++INFLATED REQUEST+++[".strlen($data)." chars]+++\n" . $data . "\n+++END+++"); |
|
836 | + } |
|
837 | + } |
|
838 | + elseif($content_encoding == 'gzip' && $degzdata = @gzinflate(substr($data, 10))) |
|
839 | + { |
|
840 | + $data = $degzdata; |
|
841 | + if($this->debug > 1) |
|
842 | + $this->debugmsg("+++INFLATED REQUEST+++[".strlen($data)." chars]+++\n" . $data . "\n+++END+++"); |
|
843 | + } |
|
844 | + else |
|
845 | + { |
|
846 | + $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['server_decompress_fail'], $GLOBALS['xmlrpcstr']['server_decompress_fail']); |
|
847 | + return $r; |
|
848 | + } |
|
849 | + } |
|
850 | + else |
|
851 | + { |
|
852 | + //error_log('The server sent deflated data. Your php install must have the Zlib extension compiled in to support this.'); |
|
853 | + $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['server_cannot_decompress'], $GLOBALS['xmlrpcstr']['server_cannot_decompress']); |
|
854 | + return $r; |
|
855 | + } |
|
856 | + } |
|
857 | + } |
|
858 | + |
|
859 | + // check if client specified accepted charsets, and if we know how to fulfill |
|
860 | + // the request |
|
861 | + if ($this->response_charset_encoding == 'auto') |
|
862 | + { |
|
863 | + $resp_encoding = ''; |
|
864 | + if (isset($_SERVER['HTTP_ACCEPT_CHARSET'])) |
|
865 | + { |
|
866 | + // here we should check if we can match the client-requested encoding |
|
867 | + // with the encodings we know we can generate. |
|
868 | + /// @todo we should parse q=0.x preferences instead of getting first charset specified... |
|
869 | + $client_accepted_charsets = explode(',', strtoupper($_SERVER['HTTP_ACCEPT_CHARSET'])); |
|
870 | + // Give preference to internal encoding |
|
871 | + $known_charsets = array($GLOBALS['xmlrpc_internalencoding'], 'UTF-8', 'ISO-8859-1', 'US-ASCII'); |
|
872 | + foreach ($known_charsets as $charset) |
|
873 | + { |
|
874 | + foreach ($client_accepted_charsets as $accepted) |
|
875 | + if (strpos($accepted, $charset) === 0) |
|
876 | + { |
|
877 | + $resp_encoding = $charset; |
|
878 | + break; |
|
879 | + } |
|
880 | + if ($resp_encoding) |
|
881 | + break; |
|
882 | + } |
|
883 | + } |
|
884 | + } |
|
885 | + else |
|
886 | + { |
|
887 | + $resp_encoding = $this->response_charset_encoding; |
|
888 | + } |
|
889 | + |
|
890 | + if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) |
|
891 | + { |
|
892 | + $resp_compression = $_SERVER['HTTP_ACCEPT_ENCODING']; |
|
893 | + } |
|
894 | + else |
|
895 | + { |
|
896 | + $resp_compression = ''; |
|
897 | + } |
|
898 | + |
|
899 | + // 'guestimate' request encoding |
|
900 | + /// @todo check if mbstring is enabled and automagic input conversion is on: it might mingle with this check??? |
|
901 | + $req_encoding = guess_encoding(isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '', |
|
902 | + $data); |
|
903 | + |
|
904 | + return null; |
|
905 | + } |
|
906 | + |
|
907 | + /** |
|
908 | + * Parse an xml chunk containing an xmlrpc request and execute the corresponding |
|
909 | + * php function registered with the server |
|
910 | + * @param string $data the xml request |
|
911 | + * @param string $req_encoding (optional) the charset encoding of the xml request |
|
912 | + * @return xmlrpcresp |
|
913 | + * @access private |
|
914 | + */ |
|
915 | + function parseRequest($data, $req_encoding='') |
|
916 | + { |
|
917 | + // 2005/05/07 commented and moved into caller function code |
|
918 | + //if($data=='') |
|
919 | + //{ |
|
920 | + // $data=$GLOBALS['HTTP_RAW_POST_DATA']; |
|
921 | + //} |
|
922 | + |
|
923 | + // G. Giunta 2005/02/13: we do NOT expect to receive html entities |
|
924 | + // so we do not try to convert them into xml character entities |
|
925 | + //$data = xmlrpc_html_entity_xlate($data); |
|
926 | + |
|
927 | + $GLOBALS['_xh']=array(); |
|
928 | + $GLOBALS['_xh']['ac']=''; |
|
929 | + $GLOBALS['_xh']['stack']=array(); |
|
930 | + $GLOBALS['_xh']['valuestack'] = array(); |
|
931 | + $GLOBALS['_xh']['params']=array(); |
|
932 | + $GLOBALS['_xh']['pt']=array(); |
|
933 | + $GLOBALS['_xh']['isf']=0; |
|
934 | + $GLOBALS['_xh']['isf_reason']=''; |
|
935 | + $GLOBALS['_xh']['method']=false; // so we can check later if we got a methodname or not |
|
936 | + $GLOBALS['_xh']['rt']=''; |
|
937 | + |
|
938 | + // decompose incoming XML into request structure |
|
939 | + |
|
940 | + if ($req_encoding != '') |
|
941 | + { |
|
942 | + // Since parsing will fail if charset is not specified in the xml prologue, |
|
943 | + // the encoding is not UTF8 and there are non-ascii chars in the text, we try to work round that... |
|
944 | + // The following code might be better for mb_string enabled installs, but |
|
945 | + // makes the lib about 200% slower... |
|
946 | + //if (!is_valid_charset($req_encoding, array('UTF-8'))) |
|
947 | + if (!in_array($req_encoding, array('UTF-8', 'US-ASCII')) && !has_encoding($data)) { |
|
948 | + if ($req_encoding == 'ISO-8859-1') { |
|
949 | + $data = utf8_encode($data); |
|
950 | + } else { |
|
951 | + if (extension_loaded('mbstring')) { |
|
952 | + $data = mb_convert_encoding($data, 'UTF-8', $req_encoding); |
|
953 | + } else { |
|
954 | + error_log('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of received request: ' . $req_encoding); |
|
955 | + } |
|
956 | + } |
|
957 | + } |
|
958 | + } |
|
959 | + |
|
960 | + $parser = xml_parser_create(); |
|
961 | + xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true); |
|
962 | + // G. Giunta 2005/02/13: PHP internally uses ISO-8859-1, so we have to tell |
|
963 | + // the xml parser to give us back data in the expected charset |
|
964 | + // What if internal encoding is not in one of the 3 allowed? |
|
965 | + // we use the broadest one, ie. utf8 |
|
966 | + // This allows to send data which is native in various charset, |
|
967 | + // by extending xmlrpc_encode_entitites() and setting xmlrpc_internalencoding |
|
968 | + if (!in_array($GLOBALS['xmlrpc_internalencoding'], array('UTF-8', 'ISO-8859-1', 'US-ASCII'))) |
|
969 | + { |
|
970 | + xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8'); |
|
971 | + } |
|
972 | + else |
|
973 | + { |
|
974 | + xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $GLOBALS['xmlrpc_internalencoding']); |
|
975 | + } |
|
976 | + |
|
977 | + if ($this->functions_parameters_type != 'xmlrpcvals') |
|
978 | + xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee_fast'); |
|
979 | + else |
|
980 | + xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee'); |
|
981 | + xml_set_character_data_handler($parser, 'xmlrpc_cd'); |
|
982 | + xml_set_default_handler($parser, 'xmlrpc_dh'); |
|
983 | + if(!xml_parse($parser, $data, 1)) |
|
984 | + { |
|
985 | + // return XML error as a faultCode |
|
986 | + $r=new xmlrpcresp(0, |
|
987 | + $GLOBALS['xmlrpcerrxml']+xml_get_error_code($parser), |
|
988 | + sprintf('XML error: %s at line %d, column %d', |
|
989 | + xml_error_string(xml_get_error_code($parser)), |
|
990 | + xml_get_current_line_number($parser), xml_get_current_column_number($parser))); |
|
991 | + xml_parser_free($parser); |
|
992 | + } |
|
993 | + elseif ($GLOBALS['_xh']['isf']) |
|
994 | + { |
|
995 | + xml_parser_free($parser); |
|
996 | + $r=new xmlrpcresp(0, |
|
997 | + $GLOBALS['xmlrpcerr']['invalid_request'], |
|
998 | + $GLOBALS['xmlrpcstr']['invalid_request'] . ' ' . $GLOBALS['_xh']['isf_reason']); |
|
999 | + } |
|
1000 | + else |
|
1001 | + { |
|
1002 | + xml_parser_free($parser); |
|
1003 | + // small layering violation in favor of speed and memory usage: |
|
1004 | + // we should allow the 'execute' method handle this, but in the |
|
1005 | + // most common scenario (xmlrpcvals type server with some methods |
|
1006 | + // registered as phpvals) that would mean a useless encode+decode pass |
|
1007 | + if ($this->functions_parameters_type != 'xmlrpcvals' || (isset($this->dmap[$GLOBALS['_xh']['method']]['parameters_type']) && ($this->dmap[$GLOBALS['_xh']['method']]['parameters_type'] == 'phpvals'))) |
|
1008 | + { |
|
1009 | + if($this->debug > 1) |
|
1010 | + { |
|
1011 | + $this->debugmsg("\n+++PARSED+++\n".var_export($GLOBALS['_xh']['params'], true)."\n+++END+++"); |
|
1012 | + } |
|
1013 | + $r = $this->execute($GLOBALS['_xh']['method'], $GLOBALS['_xh']['params'], $GLOBALS['_xh']['pt']); |
|
1014 | + } |
|
1015 | + else |
|
1016 | + { |
|
1017 | + // build an xmlrpcmsg object with data parsed from xml |
|
1018 | + $m=new xmlrpcmsg($GLOBALS['_xh']['method']); |
|
1019 | + // now add parameters in |
|
1020 | + for($i=0; $i<count($GLOBALS['_xh']['params']); $i++) |
|
1021 | + { |
|
1022 | + $m->addParam($GLOBALS['_xh']['params'][$i]); |
|
1023 | + } |
|
1024 | + |
|
1025 | + if($this->debug > 1) |
|
1026 | + { |
|
1027 | + $this->debugmsg("\n+++PARSED+++\n".var_export($m, true)."\n+++END+++"); |
|
1028 | + } |
|
1029 | + $r = $this->execute($m); |
|
1030 | + } |
|
1031 | + } |
|
1032 | + return $r; |
|
1033 | + } |
|
1034 | + |
|
1035 | + /** |
|
1036 | + * Execute a method invoked by the client, checking parameters used |
|
1037 | + * @param mixed $m either an xmlrpcmsg obj or a method name |
|
1038 | + * @param array $params array with method parameters as php types (if m is method name only) |
|
1039 | + * @param array $paramtypes array with xmlrpc types of method parameters (if m is method name only) |
|
1040 | + * @return xmlrpcresp |
|
1041 | + * @access private |
|
1042 | + */ |
|
1043 | + function execute($m, $params=null, $paramtypes=null) |
|
1044 | + { |
|
1045 | + if (is_object($m)) |
|
1046 | + { |
|
1047 | + $methName = $m->method(); |
|
1048 | + } |
|
1049 | + else |
|
1050 | + { |
|
1051 | + $methName = $m; |
|
1052 | + } |
|
1053 | + $sysCall = $this->allow_system_funcs && (strpos($methName, "system.") === 0); |
|
1054 | + $dmap = $sysCall ? $GLOBALS['_xmlrpcs_dmap'] : $this->dmap; |
|
1055 | + |
|
1056 | + if(!isset($dmap[$methName]['function'])) |
|
1057 | + { |
|
1058 | + // No such method |
|
1059 | + return new xmlrpcresp(0, |
|
1060 | + $GLOBALS['xmlrpcerr']['unknown_method'], |
|
1061 | + $GLOBALS['xmlrpcstr']['unknown_method']); |
|
1062 | + } |
|
1063 | + |
|
1064 | + // Check signature |
|
1065 | + if(isset($dmap[$methName]['signature'])) |
|
1066 | + { |
|
1067 | + $sig = $dmap[$methName]['signature']; |
|
1068 | + if (is_object($m)) |
|
1069 | + { |
|
1070 | + list($ok, $errstr) = $this->verifySignature($m, $sig); |
|
1071 | + } |
|
1072 | + else |
|
1073 | + { |
|
1074 | + list($ok, $errstr) = $this->verifySignature($paramtypes, $sig); |
|
1075 | + } |
|
1076 | + if(!$ok) |
|
1077 | + { |
|
1078 | + // Didn't match. |
|
1079 | + return new xmlrpcresp( |
|
1080 | + 0, |
|
1081 | + $GLOBALS['xmlrpcerr']['incorrect_params'], |
|
1082 | + $GLOBALS['xmlrpcstr']['incorrect_params'] . ": ${errstr}" |
|
1083 | + ); |
|
1084 | + } |
|
1085 | + } |
|
1086 | + |
|
1087 | + $func = $dmap[$methName]['function']; |
|
1088 | + // let the 'class::function' syntax be accepted in dispatch maps |
|
1089 | + if(is_string($func) && strpos($func, '::')) |
|
1090 | + { |
|
1091 | + $func = explode('::', $func); |
|
1092 | + } |
|
1093 | + // verify that function to be invoked is in fact callable |
|
1094 | + if(!is_callable($func)) |
|
1095 | + { |
|
1096 | + error_log("XML-RPC: ".__METHOD__.": function $func registered as method handler is not callable"); |
|
1097 | + return new xmlrpcresp( |
|
1098 | + 0, |
|
1099 | + $GLOBALS['xmlrpcerr']['server_error'], |
|
1100 | + $GLOBALS['xmlrpcstr']['server_error'] . ": no function matches method" |
|
1101 | + ); |
|
1102 | + } |
|
1103 | + |
|
1104 | + // If debug level is 3, we should catch all errors generated during |
|
1105 | + // processing of user function, and log them as part of response |
|
1106 | + if($this->debug > 2) |
|
1107 | + { |
|
1108 | + $GLOBALS['_xmlrpcs_prev_ehandler'] = set_error_handler('_xmlrpcs_errorHandler'); |
|
1109 | + } |
|
1110 | + try |
|
1111 | + { |
|
1112 | + // Allow mixed-convention servers |
|
1113 | + if (is_object($m)) |
|
1114 | + { |
|
1115 | + if($sysCall) |
|
1116 | + { |
|
1117 | + $r = call_user_func($func, $this, $m); |
|
1118 | + } |
|
1119 | + else |
|
1120 | + { |
|
1121 | + $r = call_user_func($func, $m); |
|
1122 | + } |
|
1123 | + if (!is_a($r, 'xmlrpcresp')) |
|
1124 | + { |
|
1125 | + error_log("XML-RPC: ".__METHOD__.": function $func registered as method handler does not return an xmlrpcresp object"); |
|
1126 | + if (is_a($r, 'xmlrpcval')) |
|
1127 | + { |
|
1128 | + $r = new xmlrpcresp($r); |
|
1129 | + } |
|
1130 | + else |
|
1131 | + { |
|
1132 | + $r = new xmlrpcresp( |
|
1133 | + 0, |
|
1134 | + $GLOBALS['xmlrpcerr']['server_error'], |
|
1135 | + $GLOBALS['xmlrpcstr']['server_error'] . ": function does not return xmlrpcresp object" |
|
1136 | + ); |
|
1137 | + } |
|
1138 | + } |
|
1139 | + } |
|
1140 | + else |
|
1141 | + { |
|
1142 | + // call a 'plain php' function |
|
1143 | + if($sysCall) |
|
1144 | + { |
|
1145 | + array_unshift($params, $this); |
|
1146 | + $r = call_user_func_array($func, $params); |
|
1147 | + } |
|
1148 | + else |
|
1149 | + { |
|
1150 | + // 3rd API convention for method-handling functions: EPI-style |
|
1151 | + if ($this->functions_parameters_type == 'epivals') |
|
1152 | + { |
|
1153 | + $r = call_user_func_array($func, array($methName, $params, $this->user_data)); |
|
1154 | + // mimic EPI behaviour: if we get an array that looks like an error, make it |
|
1155 | + // an eror response |
|
1156 | + if (is_array($r) && array_key_exists('faultCode', $r) && array_key_exists('faultString', $r)) |
|
1157 | + { |
|
1158 | + $r = new xmlrpcresp(0, (integer)$r['faultCode'], (string)$r['faultString']); |
|
1159 | + } |
|
1160 | + else |
|
1161 | + { |
|
1162 | + // functions using EPI api should NOT return resp objects, |
|
1163 | + // so make sure we encode the return type correctly |
|
1164 | + $r = new xmlrpcresp(php_xmlrpc_encode($r, array('extension_api'))); |
|
1165 | + } |
|
1166 | + } |
|
1167 | + else |
|
1168 | + { |
|
1169 | + $r = call_user_func_array($func, $params); |
|
1170 | + } |
|
1171 | + } |
|
1172 | + // the return type can be either an xmlrpcresp object or a plain php value... |
|
1173 | + if (!is_a($r, 'xmlrpcresp')) |
|
1174 | + { |
|
1175 | + // what should we assume here about automatic encoding of datetimes |
|
1176 | + // and php classes instances??? |
|
1177 | + $r = new xmlrpcresp(php_xmlrpc_encode($r, $this->phpvals_encoding_options)); |
|
1178 | + } |
|
1179 | + } |
|
1180 | + } |
|
1181 | + catch(Exception $e) |
|
1182 | + { |
|
1183 | + // (barring errors in the lib) an uncatched exception happened |
|
1184 | + // in the called function, we wrap it in a proper error-response |
|
1185 | + switch($this->exception_handling) |
|
1186 | + { |
|
1187 | + case 2: |
|
1188 | + throw $e; |
|
1189 | + break; |
|
1190 | + case 1: |
|
1191 | + $r = new xmlrpcresp(0, $e->getCode(), $e->getMessage()); |
|
1192 | + break; |
|
1193 | + default: |
|
1194 | + $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['server_error'], $GLOBALS['xmlrpcstr']['server_error']); |
|
1195 | + } |
|
1196 | + } |
|
1197 | + if($this->debug > 2) |
|
1198 | + { |
|
1199 | + // note: restore the error handler we found before calling the |
|
1200 | + // user func, even if it has been changed inside the func itself |
|
1201 | + if($GLOBALS['_xmlrpcs_prev_ehandler']) |
|
1202 | + { |
|
1203 | + set_error_handler($GLOBALS['_xmlrpcs_prev_ehandler']); |
|
1204 | + } |
|
1205 | + else |
|
1206 | + { |
|
1207 | + restore_error_handler(); |
|
1208 | + } |
|
1209 | + } |
|
1210 | + return $r; |
|
1211 | + } |
|
1212 | + |
|
1213 | + /** |
|
1214 | + * add a string to the 'internal debug message' (separate from 'user debug message') |
|
1215 | + * @param string $string |
|
1216 | + * @access private |
|
1217 | + */ |
|
1218 | + function debugmsg($string) |
|
1219 | + { |
|
1220 | + $this->debug_info .= $string."\n"; |
|
1221 | + } |
|
1222 | + |
|
1223 | + /** |
|
1224 | + * @access private |
|
1225 | + */ |
|
1226 | + function xml_header($charset_encoding='') |
|
1227 | + { |
|
1228 | + if ($charset_encoding != '') |
|
1229 | + { |
|
1230 | + return "<?xml version=\"1.0\" encoding=\"$charset_encoding\"?" . ">\n"; |
|
1231 | + } |
|
1232 | + else |
|
1233 | + { |
|
1234 | + return "<?xml version=\"1.0\"?" . ">\n"; |
|
1235 | + } |
|
1236 | + } |
|
1237 | + |
|
1238 | + /** |
|
1239 | + * A debugging routine: just echoes back the input packet as a string value |
|
1240 | + * DEPRECATED! |
|
1241 | + */ |
|
1242 | + function echoInput() |
|
1243 | + { |
|
1244 | + $r=new xmlrpcresp(new xmlrpcval( "'Aha said I: '" . $GLOBALS['HTTP_RAW_POST_DATA'], 'string')); |
|
1245 | + print $r->serialize(); |
|
1246 | + } |
|
1247 | + } |
|
1248 | 1248 | ?> |
1249 | 1249 | \ No newline at end of file |