PageRenderTime 56ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/phpxmlrpc/xmlrpc.php

https://bitbucket.org/asosso/joomla15
PHP | 3640 lines | 2808 code | 153 blank | 679 comment | 348 complexity | 77f5a039def9879a7a374203c9fe0594 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. // by Edd Dumbill (C) 1999-2002
  3. // <edd@usefulinc.com>
  4. // $Id: xmlrpc.inc,v 1.158 2007/03/01 21:21:02 ggiunta Exp $
  5. // Copyright (c) 1999,2000,2002 Edd Dumbill.
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions
  10. // are met:
  11. //
  12. // * Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. //
  15. // * Redistributions in binary form must reproduce the above
  16. // copyright notice, this list of conditions and the following
  17. // disclaimer in the documentation and/or other materials provided
  18. // with the distribution.
  19. //
  20. // * Neither the name of the "XML-RPC for PHP" nor the names of its
  21. // contributors may be used to endorse or promote products derived
  22. // from this software without specific prior written permission.
  23. //
  24. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. // REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  30. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  33. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  35. // OF THE POSSIBILITY OF SUCH DAMAGE.
  36. if(!function_exists('xml_parser_create'))
  37. {
  38. // For PHP 4 onward, XML functionality is always compiled-in on windows:
  39. // no more need to dl-open it. It might have been compiled out on *nix...
  40. if(strtoupper(substr(PHP_OS, 0, 3) != 'WIN'))
  41. {
  42. dl('xml.so');
  43. }
  44. }
  45. // Try to be backward compat with php < 4.2 (are we not being nice ?)
  46. $phpversion = phpversion();
  47. if($phpversion[0] == '4' && $phpversion[2] < 2)
  48. {
  49. // give an opportunity to user to specify where to include other files from
  50. if(!defined('PHP_XMLRPC_COMPAT_DIR'))
  51. {
  52. define('PHP_XMLRPC_COMPAT_DIR',dirname(__FILE__).'/compat/');
  53. }
  54. if($phpversion[2] == '0')
  55. {
  56. if($phpversion[4] < 6)
  57. {
  58. include(PHP_XMLRPC_COMPAT_DIR.'is_callable.php');
  59. }
  60. include(PHP_XMLRPC_COMPAT_DIR.'is_scalar.php');
  61. include(PHP_XMLRPC_COMPAT_DIR.'array_key_exists.php');
  62. include(PHP_XMLRPC_COMPAT_DIR.'version_compare.php');
  63. }
  64. include(PHP_XMLRPC_COMPAT_DIR.'var_export.php');
  65. include(PHP_XMLRPC_COMPAT_DIR.'is_a.php');
  66. }
  67. // G. Giunta 2005/01/29: declare global these variables,
  68. // so that xmlrpc.inc will work even if included from within a function
  69. // Milosch: 2005/08/07 - explicitly request these via $GLOBALS where used.
  70. $GLOBALS['xmlrpcI4']='i4';
  71. $GLOBALS['xmlrpcInt']='int';
  72. $GLOBALS['xmlrpcBoolean']='boolean';
  73. $GLOBALS['xmlrpcDouble']='double';
  74. $GLOBALS['xmlrpcString']='string';
  75. $GLOBALS['xmlrpcDateTime']='dateTime.iso8601';
  76. $GLOBALS['xmlrpcBase64']='base64';
  77. $GLOBALS['xmlrpcArray']='array';
  78. $GLOBALS['xmlrpcStruct']='struct';
  79. $GLOBALS['xmlrpcValue']='undefined';
  80. $GLOBALS['xmlrpcTypes']=array(
  81. $GLOBALS['xmlrpcI4'] => 1,
  82. $GLOBALS['xmlrpcInt'] => 1,
  83. $GLOBALS['xmlrpcBoolean'] => 1,
  84. $GLOBALS['xmlrpcString'] => 1,
  85. $GLOBALS['xmlrpcDouble'] => 1,
  86. $GLOBALS['xmlrpcDateTime'] => 1,
  87. $GLOBALS['xmlrpcBase64'] => 1,
  88. $GLOBALS['xmlrpcArray'] => 2,
  89. $GLOBALS['xmlrpcStruct'] => 3
  90. );
  91. $GLOBALS['xmlrpc_valid_parents'] = array(
  92. 'VALUE' => array('MEMBER', 'DATA', 'PARAM', 'FAULT'),
  93. 'BOOLEAN' => array('VALUE'),
  94. 'I4' => array('VALUE'),
  95. 'INT' => array('VALUE'),
  96. 'STRING' => array('VALUE'),
  97. 'DOUBLE' => array('VALUE'),
  98. 'DATETIME.ISO8601' => array('VALUE'),
  99. 'BASE64' => array('VALUE'),
  100. 'MEMBER' => array('STRUCT'),
  101. 'NAME' => array('MEMBER'),
  102. 'DATA' => array('ARRAY'),
  103. 'ARRAY' => array('VALUE'),
  104. 'STRUCT' => array('VALUE'),
  105. 'PARAM' => array('PARAMS'),
  106. 'METHODNAME' => array('METHODCALL'),
  107. 'PARAMS' => array('METHODCALL', 'METHODRESPONSE'),
  108. 'FAULT' => array('METHODRESPONSE'),
  109. 'NIL' => array('VALUE') // only used when extension activated
  110. );
  111. // define extra types for supporting NULL (useful for json or <NIL/>)
  112. $GLOBALS['xmlrpcNull']='null';
  113. $GLOBALS['xmlrpcTypes']['null']=1;
  114. // Not in use anymore since 2.0. Shall we remove it?
  115. /// @deprecated
  116. $GLOBALS['xmlEntities']=array(
  117. 'amp' => '&',
  118. 'quot' => '"',
  119. 'lt' => '<',
  120. 'gt' => '>',
  121. 'apos' => "'"
  122. );
  123. // tables used for transcoding different charsets into us-ascii xml
  124. $GLOBALS['xml_iso88591_Entities']=array();
  125. $GLOBALS['xml_iso88591_Entities']['in'] = array();
  126. $GLOBALS['xml_iso88591_Entities']['out'] = array();
  127. for ($i = 0; $i < 32; $i++)
  128. {
  129. $GLOBALS['xml_iso88591_Entities']['in'][] = chr($i);
  130. $GLOBALS['xml_iso88591_Entities']['out'][] = '&#'.$i.';';
  131. }
  132. for ($i = 160; $i < 256; $i++)
  133. {
  134. $GLOBALS['xml_iso88591_Entities']['in'][] = chr($i);
  135. $GLOBALS['xml_iso88591_Entities']['out'][] = '&#'.$i.';';
  136. }
  137. /// @todo add to iso table the characters from cp_1252 range, i.e. 128 to 159.
  138. /// These will NOT be present in true ISO-8859-1, but will save the unwary
  139. /// windows user from sending junk.
  140. /*
  141. $cp1252_to_xmlent =
  142. array(
  143. '\x80'=>'&#x20AC;', '\x81'=>'?', '\x82'=>'&#x201A;', '\x83'=>'&#x0192;',
  144. '\x84'=>'&#x201E;', '\x85'=>'&#x2026;', '\x86'=>'&#x2020;', \x87'=>'&#x2021;',
  145. '\x88'=>'&#x02C6;', '\x89'=>'&#x2030;', '\x8A'=>'&#x0160;', '\x8B'=>'&#x2039;',
  146. '\x8C'=>'&#x0152;', '\x8D'=>'?', '\x8E'=>'&#x017D;', '\x8F'=>'?',
  147. '\x90'=>'?', '\x91'=>'&#x2018;', '\x92'=>'&#x2019;', '\x93'=>'&#x201C;',
  148. '\x94'=>'&#x201D;', '\x95'=>'&#x2022;', '\x96'=>'&#x2013;', '\x97'=>'&#x2014;',
  149. '\x98'=>'&#x02DC;', '\x99'=>'&#x2122;', '\x9A'=>'&#x0161;', '\x9B'=>'&#x203A;',
  150. '\x9C'=>'&#x0153;', '\x9D'=>'?', '\x9E'=>'&#x017E;', '\x9F'=>'&#x0178;'
  151. );
  152. */
  153. $GLOBALS['xmlrpcerr']['unknown_method']=1;
  154. $GLOBALS['xmlrpcstr']['unknown_method']='Unknown method';
  155. $GLOBALS['xmlrpcerr']['invalid_return']=2;
  156. $GLOBALS['xmlrpcstr']['invalid_return']='Invalid return payload: enable debugging to examine incoming payload';
  157. $GLOBALS['xmlrpcerr']['incorrect_params']=3;
  158. $GLOBALS['xmlrpcstr']['incorrect_params']='Incorrect parameters passed to method';
  159. $GLOBALS['xmlrpcerr']['introspect_unknown']=4;
  160. $GLOBALS['xmlrpcstr']['introspect_unknown']="Can't introspect: method unknown";
  161. $GLOBALS['xmlrpcerr']['http_error']=5;
  162. $GLOBALS['xmlrpcstr']['http_error']="Didn't receive 200 OK from remote server.";
  163. $GLOBALS['xmlrpcerr']['no_data']=6;
  164. $GLOBALS['xmlrpcstr']['no_data']='No data received from server.';
  165. $GLOBALS['xmlrpcerr']['no_ssl']=7;
  166. $GLOBALS['xmlrpcstr']['no_ssl']='No SSL support compiled in.';
  167. $GLOBALS['xmlrpcerr']['curl_fail']=8;
  168. $GLOBALS['xmlrpcstr']['curl_fail']='CURL error';
  169. $GLOBALS['xmlrpcerr']['invalid_request']=15;
  170. $GLOBALS['xmlrpcstr']['invalid_request']='Invalid request payload';
  171. $GLOBALS['xmlrpcerr']['no_curl']=16;
  172. $GLOBALS['xmlrpcstr']['no_curl']='No CURL support compiled in.';
  173. $GLOBALS['xmlrpcerr']['server_error']=17;
  174. $GLOBALS['xmlrpcstr']['server_error']='Internal server error';
  175. $GLOBALS['xmlrpcerr']['multicall_error']=18;
  176. $GLOBALS['xmlrpcstr']['multicall_error']='Received from server invalid multicall response';
  177. $GLOBALS['xmlrpcerr']['multicall_notstruct'] = 9;
  178. $GLOBALS['xmlrpcstr']['multicall_notstruct'] = 'system.multicall expected struct';
  179. $GLOBALS['xmlrpcerr']['multicall_nomethod'] = 10;
  180. $GLOBALS['xmlrpcstr']['multicall_nomethod'] = 'missing methodName';
  181. $GLOBALS['xmlrpcerr']['multicall_notstring'] = 11;
  182. $GLOBALS['xmlrpcstr']['multicall_notstring'] = 'methodName is not a string';
  183. $GLOBALS['xmlrpcerr']['multicall_recursion'] = 12;
  184. $GLOBALS['xmlrpcstr']['multicall_recursion'] = 'recursive system.multicall forbidden';
  185. $GLOBALS['xmlrpcerr']['multicall_noparams'] = 13;
  186. $GLOBALS['xmlrpcstr']['multicall_noparams'] = 'missing params';
  187. $GLOBALS['xmlrpcerr']['multicall_notarray'] = 14;
  188. $GLOBALS['xmlrpcstr']['multicall_notarray'] = 'params is not an array';
  189. $GLOBALS['xmlrpcerr']['cannot_decompress']=103;
  190. $GLOBALS['xmlrpcstr']['cannot_decompress']='Received from server compressed HTTP and cannot decompress';
  191. $GLOBALS['xmlrpcerr']['decompress_fail']=104;
  192. $GLOBALS['xmlrpcstr']['decompress_fail']='Received from server invalid compressed HTTP';
  193. $GLOBALS['xmlrpcerr']['dechunk_fail']=105;
  194. $GLOBALS['xmlrpcstr']['dechunk_fail']='Received from server invalid chunked HTTP';
  195. $GLOBALS['xmlrpcerr']['server_cannot_decompress']=106;
  196. $GLOBALS['xmlrpcstr']['server_cannot_decompress']='Received from client compressed HTTP request and cannot decompress';
  197. $GLOBALS['xmlrpcerr']['server_decompress_fail']=107;
  198. $GLOBALS['xmlrpcstr']['server_decompress_fail']='Received from client invalid compressed HTTP request';
  199. // The charset encoding used by the server for received messages and
  200. // by the client for received responses when received charset cannot be determined
  201. // or is not supported
  202. $GLOBALS['xmlrpc_defencoding']='UTF-8';
  203. // The encoding used internally by PHP.
  204. // String values received as xml will be converted to this, and php strings will be converted to xml
  205. // as if having been coded with this
  206. $GLOBALS['xmlrpc_internalencoding']='ISO-8859-1';
  207. $GLOBALS['xmlrpcName']='XML-RPC for PHP';
  208. $GLOBALS['xmlrpcVersion']='2.2';
  209. // let user errors start at 800
  210. $GLOBALS['xmlrpcerruser']=800;
  211. // let XML parse errors start at 100
  212. $GLOBALS['xmlrpcerrxml']=100;
  213. // formulate backslashes for escaping regexp
  214. // Not in use anymore since 2.0. Shall we remove it?
  215. /// @deprecated
  216. $GLOBALS['xmlrpc_backslash']=chr(92).chr(92);
  217. // set to TRUE to enable correct decoding of <NIL/> values
  218. $GLOBALS['xmlrpc_null_extension']=false;
  219. // used to store state during parsing
  220. // quick explanation of components:
  221. // ac - used to accumulate values
  222. // isf - used to indicate a parsing fault (2) or xmlrpcresp fault (1)
  223. // isf_reason - used for storing xmlrpcresp fault string
  224. // lv - used to indicate "looking for a value": implements
  225. // the logic to allow values with no types to be strings
  226. // params - used to store parameters in method calls
  227. // method - used to store method name
  228. // stack - array with genealogy of xml elements names:
  229. // used to validate nesting of xmlrpc elements
  230. $GLOBALS['_xh']=null;
  231. /**
  232. * Convert a string to the correct XML representation in a target charset
  233. * To help correct communication of non-ascii chars inside strings, regardless
  234. * of the charset used when sending requests, parsing them, sending responses
  235. * and parsing responses, an option is to convert all non-ascii chars present in the message
  236. * into their equivalent 'charset entity'. Charset entities enumerated this way
  237. * are independent of the charset encoding used to transmit them, and all XML
  238. * parsers are bound to understand them.
  239. * Note that in the std case we are not sending a charset encoding mime type
  240. * along with http headers, so we are bound by RFC 3023 to emit strict us-ascii.
  241. *
  242. * @todo do a bit of basic benchmarking (strtr vs. str_replace)
  243. * @todo make usage of iconv() or recode_string() or mb_string() where available
  244. */
  245. function xmlrpc_encode_entitites($data, $src_encoding='', $dest_encoding='')
  246. {
  247. if ($src_encoding == '')
  248. {
  249. // lame, but we know no better...
  250. $src_encoding = $GLOBALS['xmlrpc_internalencoding'];
  251. }
  252. switch(strtoupper($src_encoding.'_'.$dest_encoding))
  253. {
  254. case 'ISO-8859-1_':
  255. case 'ISO-8859-1_US-ASCII':
  256. $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $data);
  257. $escaped_data = str_replace($GLOBALS['xml_iso88591_Entities']['in'], $GLOBALS['xml_iso88591_Entities']['out'], $escaped_data);
  258. break;
  259. case 'ISO-8859-1_UTF-8':
  260. $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $data);
  261. $escaped_data = utf8_encode($escaped_data);
  262. break;
  263. case 'ISO-8859-1_ISO-8859-1':
  264. case 'US-ASCII_US-ASCII':
  265. case 'US-ASCII_UTF-8':
  266. case 'US-ASCII_':
  267. case 'US-ASCII_ISO-8859-1':
  268. case 'UTF-8_UTF-8':
  269. $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $data);
  270. break;
  271. case 'UTF-8_':
  272. case 'UTF-8_US-ASCII':
  273. case 'UTF-8_ISO-8859-1':
  274. // NB: this will choke on invalid UTF-8, going most likely beyond EOF
  275. $escaped_data = '';
  276. // be kind to users creating string xmlrpcvals out of different php types
  277. $data = (string) $data;
  278. $ns = strlen ($data);
  279. for ($nn = 0; $nn < $ns; $nn++)
  280. {
  281. $ch = $data[$nn];
  282. $ii = ord($ch);
  283. //1 7 0bbbbbbb (127)
  284. if ($ii < 128)
  285. {
  286. /// @todo shall we replace this with a (supposedly) faster str_replace?
  287. switch($ii){
  288. case 34:
  289. $escaped_data .= '&quot;';
  290. break;
  291. case 38:
  292. $escaped_data .= '&amp;';
  293. break;
  294. case 39:
  295. $escaped_data .= '&apos;';
  296. break;
  297. case 60:
  298. $escaped_data .= '&lt;';
  299. break;
  300. case 62:
  301. $escaped_data .= '&gt;';
  302. break;
  303. default:
  304. $escaped_data .= $ch;
  305. } // switch
  306. }
  307. //2 11 110bbbbb 10bbbbbb (2047)
  308. else if ($ii>>5 == 6)
  309. {
  310. $b1 = ($ii & 31);
  311. $ii = ord($data[$nn+1]);
  312. $b2 = ($ii & 63);
  313. $ii = ($b1 * 64) + $b2;
  314. $ent = sprintf ('&#%d;', $ii);
  315. $escaped_data .= $ent;
  316. $nn += 1;
  317. }
  318. //3 16 1110bbbb 10bbbbbb 10bbbbbb
  319. else if ($ii>>4 == 14)
  320. {
  321. $b1 = ($ii & 31);
  322. $ii = ord($data[$nn+1]);
  323. $b2 = ($ii & 63);
  324. $ii = ord($data[$nn+2]);
  325. $b3 = ($ii & 63);
  326. $ii = ((($b1 * 64) + $b2) * 64) + $b3;
  327. $ent = sprintf ('&#%d;', $ii);
  328. $escaped_data .= $ent;
  329. $nn += 2;
  330. }
  331. //4 21 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  332. else if ($ii>>3 == 30)
  333. {
  334. $b1 = ($ii & 31);
  335. $ii = ord($data[$nn+1]);
  336. $b2 = ($ii & 63);
  337. $ii = ord($data[$nn+2]);
  338. $b3 = ($ii & 63);
  339. $ii = ord($data[$nn+3]);
  340. $b4 = ($ii & 63);
  341. $ii = ((((($b1 * 64) + $b2) * 64) + $b3) * 64) + $b4;
  342. $ent = sprintf ('&#%d;', $ii);
  343. $escaped_data .= $ent;
  344. $nn += 3;
  345. }
  346. }
  347. break;
  348. default:
  349. $escaped_data = '';
  350. error_log("Converting from $src_encoding to $dest_encoding: not supported...");
  351. }
  352. return $escaped_data;
  353. }
  354. /// xml parser handler function for opening element tags
  355. function xmlrpc_se($parser, $name, $attrs, $accept_single_vals=false)
  356. {
  357. // if invalid xmlrpc already detected, skip all processing
  358. if ($GLOBALS['_xh']['isf'] < 2)
  359. {
  360. // check for correct element nesting
  361. // top level element can only be of 2 types
  362. /// @todo optimization creep: save this check into a bool variable, instead of using count() every time:
  363. /// there is only a single top level element in xml anyway
  364. if (count($GLOBALS['_xh']['stack']) == 0)
  365. {
  366. if ($name != 'METHODRESPONSE' && $name != 'METHODCALL' && (
  367. $name != 'VALUE' && !$accept_single_vals))
  368. {
  369. $GLOBALS['_xh']['isf'] = 2;
  370. $GLOBALS['_xh']['isf_reason'] = 'missing top level xmlrpc element';
  371. return;
  372. }
  373. else
  374. {
  375. $GLOBALS['_xh']['rt'] = strtolower($name);
  376. }
  377. }
  378. else
  379. {
  380. // not top level element: see if parent is OK
  381. $parent = end($GLOBALS['_xh']['stack']);
  382. if (!array_key_exists($name, $GLOBALS['xmlrpc_valid_parents']) || !in_array($parent, $GLOBALS['xmlrpc_valid_parents'][$name]))
  383. {
  384. $GLOBALS['_xh']['isf'] = 2;
  385. $GLOBALS['_xh']['isf_reason'] = "xmlrpc element $name cannot be child of $parent";
  386. return;
  387. }
  388. }
  389. switch($name)
  390. {
  391. // optimize for speed switch cases: most common cases first
  392. case 'VALUE':
  393. /// @todo we could check for 2 VALUE elements inside a MEMBER or PARAM element
  394. $GLOBALS['_xh']['vt']='value'; // indicator: no value found yet
  395. $GLOBALS['_xh']['ac']='';
  396. $GLOBALS['_xh']['lv']=1;
  397. $GLOBALS['_xh']['php_class']=null;
  398. break;
  399. case 'I4':
  400. case 'INT':
  401. case 'STRING':
  402. case 'BOOLEAN':
  403. case 'DOUBLE':
  404. case 'DATETIME.ISO8601':
  405. case 'BASE64':
  406. if ($GLOBALS['_xh']['vt']!='value')
  407. {
  408. //two data elements inside a value: an error occurred!
  409. $GLOBALS['_xh']['isf'] = 2;
  410. $GLOBALS['_xh']['isf_reason'] = "$name element following a {$GLOBALS['_xh']['vt']} element inside a single value";
  411. return;
  412. }
  413. $GLOBALS['_xh']['ac']=''; // reset the accumulator
  414. break;
  415. case 'STRUCT':
  416. case 'ARRAY':
  417. if ($GLOBALS['_xh']['vt']!='value')
  418. {
  419. //two data elements inside a value: an error occurred!
  420. $GLOBALS['_xh']['isf'] = 2;
  421. $GLOBALS['_xh']['isf_reason'] = "$name element following a {$GLOBALS['_xh']['vt']} element inside a single value";
  422. return;
  423. }
  424. // create an empty array to hold child values, and push it onto appropriate stack
  425. $cur_val = array();
  426. $cur_val['values'] = array();
  427. $cur_val['type'] = $name;
  428. // check for out-of-band information to rebuild php objs
  429. // and in case it is found, save it
  430. if (@isset($attrs['PHP_CLASS']))
  431. {
  432. $cur_val['php_class'] = $attrs['PHP_CLASS'];
  433. }
  434. $GLOBALS['_xh']['valuestack'][] = $cur_val;
  435. $GLOBALS['_xh']['vt']='data'; // be prepared for a data element next
  436. break;
  437. case 'DATA':
  438. if ($GLOBALS['_xh']['vt']!='data')
  439. {
  440. //two data elements inside a value: an error occurred!
  441. $GLOBALS['_xh']['isf'] = 2;
  442. $GLOBALS['_xh']['isf_reason'] = "found two data elements inside an array element";
  443. return;
  444. }
  445. case 'METHODCALL':
  446. case 'METHODRESPONSE':
  447. case 'PARAMS':
  448. // valid elements that add little to processing
  449. break;
  450. case 'METHODNAME':
  451. case 'NAME':
  452. /// @todo we could check for 2 NAME elements inside a MEMBER element
  453. $GLOBALS['_xh']['ac']='';
  454. break;
  455. case 'FAULT':
  456. $GLOBALS['_xh']['isf']=1;
  457. break;
  458. case 'MEMBER':
  459. $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
  460. //$GLOBALS['_xh']['ac']='';
  461. // Drop trough intentionally
  462. case 'PARAM':
  463. // clear value type, so we can check later if no value has been passed for this param/member
  464. $GLOBALS['_xh']['vt']=null;
  465. break;
  466. case 'NIL':
  467. if ($GLOBALS['xmlrpc_null_extension'])
  468. {
  469. if ($GLOBALS['_xh']['vt']!='value')
  470. {
  471. //two data elements inside a value: an error occurred!
  472. $GLOBALS['_xh']['isf'] = 2;
  473. $GLOBALS['_xh']['isf_reason'] = "$name element following a {$GLOBALS['_xh']['vt']} element inside a single value";
  474. return;
  475. }
  476. $GLOBALS['_xh']['ac']=''; // reset the accumulator
  477. break;
  478. }
  479. // we do not support the <NIL/> extension, so
  480. // drop through intentionally
  481. default:
  482. /// INVALID ELEMENT: RAISE ISF so that it is later recognized!!!
  483. $GLOBALS['_xh']['isf'] = 2;
  484. $GLOBALS['_xh']['isf_reason'] = "found not-xmlrpc xml element $name";
  485. break;
  486. }
  487. // Save current element name to stack, to validate nesting
  488. $GLOBALS['_xh']['stack'][] = $name;
  489. /// @todo optimization creep: move this inside the big switch() above
  490. if($name!='VALUE')
  491. {
  492. $GLOBALS['_xh']['lv']=0;
  493. }
  494. }
  495. }
  496. /// Used in decoding xml chunks that might represent single xmlrpc values
  497. function xmlrpc_se_any($parser, $name, $attrs)
  498. {
  499. xmlrpc_se($parser, $name, $attrs, true);
  500. }
  501. /// xml parser handler function for close element tags
  502. function xmlrpc_ee($parser, $name, $rebuild_xmlrpcvals = true)
  503. {
  504. if ($GLOBALS['_xh']['isf'] < 2)
  505. {
  506. // push this element name from stack
  507. // NB: if XML validates, correct opening/closing is guaranteed and
  508. // we do not have to check for $name == $curr_elem.
  509. // we also checked for proper nesting at start of elements...
  510. $curr_elem = array_pop($GLOBALS['_xh']['stack']);
  511. switch($name)
  512. {
  513. case 'VALUE':
  514. // This if() detects if no scalar was inside <VALUE></VALUE>
  515. if ($GLOBALS['_xh']['vt']=='value')
  516. {
  517. $GLOBALS['_xh']['value']=$GLOBALS['_xh']['ac'];
  518. $GLOBALS['_xh']['vt']=$GLOBALS['xmlrpcString'];
  519. }
  520. if ($rebuild_xmlrpcvals)
  521. {
  522. // build the xmlrpc val out of the data received, and substitute it
  523. $temp =& new xmlrpcval($GLOBALS['_xh']['value'], $GLOBALS['_xh']['vt']);
  524. // in case we got info about underlying php class, save it
  525. // in the object we're rebuilding
  526. if (isset($GLOBALS['_xh']['php_class']))
  527. $temp->_php_class = $GLOBALS['_xh']['php_class'];
  528. // check if we are inside an array or struct:
  529. // if value just built is inside an array, let's move it into array on the stack
  530. $vscount = count($GLOBALS['_xh']['valuestack']);
  531. if ($vscount && $GLOBALS['_xh']['valuestack'][$vscount-1]['type']=='ARRAY')
  532. {
  533. $GLOBALS['_xh']['valuestack'][$vscount-1]['values'][] = $temp;
  534. }
  535. else
  536. {
  537. $GLOBALS['_xh']['value'] = $temp;
  538. }
  539. }
  540. else
  541. {
  542. /// @todo this needs to treat correctly php-serialized objects,
  543. /// since std deserializing is done by php_xmlrpc_decode,
  544. /// which we will not be calling...
  545. if (isset($GLOBALS['_xh']['php_class']))
  546. {
  547. }
  548. // check if we are inside an array or struct:
  549. // if value just built is inside an array, let's move it into array on the stack
  550. $vscount = count($GLOBALS['_xh']['valuestack']);
  551. if ($vscount && $GLOBALS['_xh']['valuestack'][$vscount-1]['type']=='ARRAY')
  552. {
  553. $GLOBALS['_xh']['valuestack'][$vscount-1]['values'][] = $GLOBALS['_xh']['value'];
  554. }
  555. }
  556. break;
  557. case 'BOOLEAN':
  558. case 'I4':
  559. case 'INT':
  560. case 'STRING':
  561. case 'DOUBLE':
  562. case 'DATETIME.ISO8601':
  563. case 'BASE64':
  564. $GLOBALS['_xh']['vt']=strtolower($name);
  565. /// @todo: optimization creep - remove the if/elseif cycle below
  566. /// since the case() in which we are already did that
  567. if ($name=='STRING')
  568. {
  569. $GLOBALS['_xh']['value']=$GLOBALS['_xh']['ac'];
  570. }
  571. elseif ($name=='DATETIME.ISO8601')
  572. {
  573. if (!preg_match('/^[0-9]{8}T[0-9]{2}:[0-9]{2}:[0-9]{2}$/', $GLOBALS['_xh']['ac']))
  574. {
  575. error_log('XML-RPC: invalid value received in DATETIME: '.$GLOBALS['_xh']['ac']);
  576. }
  577. $GLOBALS['_xh']['vt']=$GLOBALS['xmlrpcDateTime'];
  578. $GLOBALS['_xh']['value']=$GLOBALS['_xh']['ac'];
  579. }
  580. elseif ($name=='BASE64')
  581. {
  582. /// @todo check for failure of base64 decoding / catch warnings
  583. $GLOBALS['_xh']['value']=base64_decode($GLOBALS['_xh']['ac']);
  584. }
  585. elseif ($name=='BOOLEAN')
  586. {
  587. // special case here: we translate boolean 1 or 0 into PHP
  588. // constants true or false.
  589. // Strings 'true' and 'false' are accepted, even though the
  590. // spec never mentions them (see eg. Blogger api docs)
  591. // NB: this simple checks helps a lot sanitizing input, ie no
  592. // security problems around here
  593. if ($GLOBALS['_xh']['ac']=='1' || strcasecmp($GLOBALS['_xh']['ac'], 'true') == 0)
  594. {
  595. $GLOBALS['_xh']['value']=true;
  596. }
  597. else
  598. {
  599. // log if receiveing something strange, even though we set the value to false anyway
  600. if ($GLOBALS['_xh']['ac']!='0' && strcasecmp($_xh[$parser]['ac'], 'false') != 0)
  601. error_log('XML-RPC: invalid value received in BOOLEAN: '.$GLOBALS['_xh']['ac']);
  602. $GLOBALS['_xh']['value']=false;
  603. }
  604. }
  605. elseif ($name=='DOUBLE')
  606. {
  607. // we have a DOUBLE
  608. // we must check that only 0123456789-.<space> are characters here
  609. if (!preg_match('/^[+-]?[eE0123456789 \t.]+$/', $GLOBALS['_xh']['ac']))
  610. {
  611. /// @todo: find a better way of throwing an error
  612. // than this!
  613. error_log('XML-RPC: non numeric value received in DOUBLE: '.$GLOBALS['_xh']['ac']);
  614. $GLOBALS['_xh']['value']='ERROR_NON_NUMERIC_FOUND';
  615. }
  616. else
  617. {
  618. // it's ok, add it on
  619. $GLOBALS['_xh']['value']=(double)$GLOBALS['_xh']['ac'];
  620. }
  621. }
  622. else
  623. {
  624. // we have an I4/INT
  625. // we must check that only 0123456789-<space> are characters here
  626. if (!preg_match('/^[+-]?[0123456789 \t]+$/', $GLOBALS['_xh']['ac']))
  627. {
  628. /// @todo find a better way of throwing an error
  629. // than this!
  630. error_log('XML-RPC: non numeric value received in INT: '.$GLOBALS['_xh']['ac']);
  631. $GLOBALS['_xh']['value']='ERROR_NON_NUMERIC_FOUND';
  632. }
  633. else
  634. {
  635. // it's ok, add it on
  636. $GLOBALS['_xh']['value']=(int)$GLOBALS['_xh']['ac'];
  637. }
  638. }
  639. //$GLOBALS['_xh']['ac']=''; // is this necessary?
  640. $GLOBALS['_xh']['lv']=3; // indicate we've found a value
  641. break;
  642. case 'NAME':
  643. $GLOBALS['_xh']['valuestack'][count($GLOBALS['_xh']['valuestack'])-1]['name'] = $GLOBALS['_xh']['ac'];
  644. break;
  645. case 'MEMBER':
  646. //$GLOBALS['_xh']['ac']=''; // is this necessary?
  647. // add to array in the stack the last element built,
  648. // unless no VALUE was found
  649. if ($GLOBALS['_xh']['vt'])
  650. {
  651. $vscount = count($GLOBALS['_xh']['valuestack']);
  652. $GLOBALS['_xh']['valuestack'][$vscount-1]['values'][$GLOBALS['_xh']['valuestack'][$vscount-1]['name']] = $GLOBALS['_xh']['value'];
  653. } else
  654. error_log('XML-RPC: missing VALUE inside STRUCT in received xml');
  655. break;
  656. case 'DATA':
  657. //$GLOBALS['_xh']['ac']=''; // is this necessary?
  658. $GLOBALS['_xh']['vt']=null; // reset this to check for 2 data elements in a row - even if they're empty
  659. break;
  660. case 'STRUCT':
  661. case 'ARRAY':
  662. // fetch out of stack array of values, and promote it to current value
  663. $curr_val = array_pop($GLOBALS['_xh']['valuestack']);
  664. $GLOBALS['_xh']['value'] = $curr_val['values'];
  665. $GLOBALS['_xh']['vt']=strtolower($name);
  666. if (isset($curr_val['php_class']))
  667. {
  668. $GLOBALS['_xh']['php_class'] = $curr_val['php_class'];
  669. }
  670. break;
  671. case 'PARAM':
  672. // add to array of params the current value,
  673. // unless no VALUE was found
  674. if ($GLOBALS['_xh']['vt'])
  675. {
  676. $GLOBALS['_xh']['params'][]=$GLOBALS['_xh']['value'];
  677. $GLOBALS['_xh']['pt'][]=$GLOBALS['_xh']['vt'];
  678. }
  679. else
  680. error_log('XML-RPC: missing VALUE inside PARAM in received xml');
  681. break;
  682. case 'METHODNAME':
  683. $GLOBALS['_xh']['method']=preg_replace('/^[\n\r\t ]+/', '', $GLOBALS['_xh']['ac']);
  684. break;
  685. case 'NIL':
  686. if ($GLOBALS['xmlrpc_null_extension'])
  687. {
  688. $GLOBALS['_xh']['vt']='null';
  689. $GLOBALS['_xh']['value']=null;
  690. $GLOBALS['_xh']['lv']=3;
  691. break;
  692. }
  693. // drop through intentionally if nil extension not enabled
  694. case 'PARAMS':
  695. case 'FAULT':
  696. case 'METHODCALL':
  697. case 'METHORESPONSE':
  698. break;
  699. default:
  700. // End of INVALID ELEMENT!
  701. // shall we add an assert here for unreachable code???
  702. break;
  703. }
  704. }
  705. }
  706. /// Used in decoding xmlrpc requests/responses without rebuilding xmlrpc values
  707. function xmlrpc_ee_fast($parser, $name)
  708. {
  709. xmlrpc_ee($parser, $name, false);
  710. }
  711. /// xml parser handler function for character data
  712. function xmlrpc_cd($parser, $data)
  713. {
  714. // skip processing if xml fault already detected
  715. if ($GLOBALS['_xh']['isf'] < 2)
  716. {
  717. // "lookforvalue==3" means that we've found an entire value
  718. // and should discard any further character data
  719. if($GLOBALS['_xh']['lv']!=3)
  720. {
  721. // G. Giunta 2006-08-23: useless change of 'lv' from 1 to 2
  722. //if($GLOBALS['_xh']['lv']==1)
  723. //{
  724. // if we've found text and we're just in a <value> then
  725. // say we've found a value
  726. //$GLOBALS['_xh']['lv']=2;
  727. //}
  728. // we always initialize the accumulator before starting parsing, anyway...
  729. //if(!@isset($GLOBALS['_xh']['ac']))
  730. //{
  731. // $GLOBALS['_xh']['ac'] = '';
  732. //}
  733. $GLOBALS['_xh']['ac'].=$data;
  734. }
  735. }
  736. }
  737. /// xml parser handler function for 'other stuff', ie. not char data or
  738. /// element start/end tag. In fact it only gets called on unknown entities...
  739. function xmlrpc_dh($parser, $data)
  740. {
  741. // skip processing if xml fault already detected
  742. if ($GLOBALS['_xh']['isf'] < 2)
  743. {
  744. if(substr($data, 0, 1) == '&' && substr($data, -1, 1) == ';')
  745. {
  746. // G. Giunta 2006-08-25: useless change of 'lv' from 1 to 2
  747. //if($GLOBALS['_xh']['lv']==1)
  748. //{
  749. // $GLOBALS['_xh']['lv']=2;
  750. //}
  751. $GLOBALS['_xh']['ac'].=$data;
  752. }
  753. }
  754. return true;
  755. }
  756. class xmlrpc_client
  757. {
  758. var $path;
  759. var $server;
  760. var $port=0;
  761. var $method='http';
  762. var $errno;
  763. var $errstr;
  764. var $debug=0;
  765. var $username='';
  766. var $password='';
  767. var $authtype=1;
  768. var $cert='';
  769. var $certpass='';
  770. var $cacert='';
  771. var $cacertdir='';
  772. var $key='';
  773. var $keypass='';
  774. var $verifypeer=true;
  775. var $verifyhost=1;
  776. var $no_multicall=false;
  777. var $proxy='';
  778. var $proxyport=0;
  779. var $proxy_user='';
  780. var $proxy_pass='';
  781. var $proxy_authtype=1;
  782. var $cookies=array();
  783. /**
  784. * List of http compression methods accepted by the client for responses.
  785. * NB: PHP supports deflate, gzip compressions out of the box if compiled w. zlib
  786. *
  787. * NNB: you can set it to any non-empty array for HTTP11 and HTTPS, since
  788. * in those cases it will be up to CURL to decide the compression methods
  789. * it supports. You might check for the presence of 'zlib' in the output of
  790. * curl_version() to determine wheter compression is supported or not
  791. */
  792. var $accepted_compression = array();
  793. /**
  794. * Name of compression scheme to be used for sending requests.
  795. * Either null, gzip or deflate
  796. */
  797. var $request_compression = '';
  798. /**
  799. * CURL handle: used for keep-alive connections (PHP 4.3.8 up, see:
  800. * http://curl.haxx.se/docs/faq.html#7.3)
  801. */
  802. var $xmlrpc_curl_handle = null;
  803. /// Wheter to use persistent connections for http 1.1 and https
  804. var $keepalive = false;
  805. /// Charset encodings that can be decoded without problems by the client
  806. var $accepted_charset_encodings = array();
  807. /// Charset encoding to be used in serializing request. NULL = use ASCII
  808. var $request_charset_encoding = '';
  809. /**
  810. * Decides the content of xmlrpcresp objects returned by calls to send()
  811. * valid strings are 'xmlrpcvals', 'phpvals' or 'xml'
  812. */
  813. var $return_type = 'xmlrpcvals';
  814. /**
  815. * @param string $path either the complete server URL or the PATH part of the xmlrc server URL, e.g. /xmlrpc/server.php
  816. * @param string $server the server name / ip address
  817. * @param integer $port the port the server is listening on, defaults to 80 or 443 depending on protocol used
  818. * @param string $method the http protocol variant: defaults to 'http', 'https' and 'http11' can be used if CURL is installed
  819. */
  820. function xmlrpc_client($path, $server='', $port='', $method='')
  821. {
  822. // allow user to specify all params in $path
  823. if($server == '' and $port == '' and $method == '')
  824. {
  825. $parts = parse_url($path);
  826. $server = $parts['host'];
  827. $path = $parts['path'];
  828. if(isset($parts['query']))
  829. {
  830. $path .= '?'.$parts['query'];
  831. }
  832. if(isset($parts['fragment']))
  833. {
  834. $path .= '#'.$parts['fragment'];
  835. }
  836. if(isset($parts['port']))
  837. {
  838. $port = $parts['port'];
  839. }
  840. if(isset($parts['scheme']))
  841. {
  842. $method = $parts['scheme'];
  843. }
  844. if(isset($parts['user']))
  845. {
  846. $this->username = $parts['user'];
  847. }
  848. if(isset($parts['pass']))
  849. {
  850. $this->password = $parts['pass'];
  851. }
  852. }
  853. if($path == '' || $path[0] != '/')
  854. {
  855. $this->path='/'.$path;
  856. }
  857. else
  858. {
  859. $this->path=$path;
  860. }
  861. $this->server=$server;
  862. if($port != '')
  863. {
  864. $this->port=$port;
  865. }
  866. if($method != '')
  867. {
  868. $this->method=$method;
  869. }
  870. // if ZLIB is enabled, let the client by default accept compressed responses
  871. if(function_exists('gzinflate') || (
  872. function_exists('curl_init') && (($info = curl_version()) &&
  873. ((is_string($info) && strpos($info, 'zlib') !== null) || isset($info['libz_version'])))
  874. ))
  875. {
  876. $this->accepted_compression = array('gzip', 'deflate');
  877. }
  878. // keepalives: enabled by default ONLY for PHP >= 4.3.8
  879. // (see http://curl.haxx.se/docs/faq.html#7.3)
  880. if(version_compare(phpversion(), '4.3.8') >= 0)
  881. {
  882. $this->keepalive = true;
  883. }
  884. // by default the xml parser can support these 3 charset encodings
  885. $this->accepted_charset_encodings = array('UTF-8', 'ISO-8859-1', 'US-ASCII');
  886. }
  887. /**
  888. * Enables/disables the echoing to screen of the xmlrpc responses received
  889. * @param integer $debug values 0, 1 and 2 are supported (2 = echo sent msg too, before received response)
  890. * @access public
  891. */
  892. function setDebug($in)
  893. {
  894. $this->debug=$in;
  895. }
  896. /**
  897. * Add some http BASIC AUTH credentials, used by the client to authenticate
  898. * @param string $u username
  899. * @param string $p password
  900. * @param integer $t auth type. See curl_setopt man page for supported auth types. Defaults to CURLAUTH_BASIC (basic auth)
  901. * @access public
  902. */
  903. function setCredentials($u, $p, $t=1)
  904. {
  905. $this->username=$u;
  906. $this->password=$p;
  907. $this->authtype=$t;
  908. }
  909. /**
  910. * Add a client-side https certificate
  911. * @param string $cert
  912. * @param string $certpass
  913. * @access public
  914. */
  915. function setCertificate($cert, $certpass)
  916. {
  917. $this->cert = $cert;
  918. $this->certpass = $certpass;
  919. }
  920. /**
  921. * Add a CA certificate to verify server with (see man page about
  922. * CURLOPT_CAINFO for more details
  923. * @param string $cacert certificate file name (or dir holding certificates)
  924. * @param bool $is_dir set to true to indicate cacert is a dir. defaults to false
  925. * @access public
  926. */
  927. function setCaCertificate($cacert, $is_dir=false)
  928. {
  929. if ($is_dir)
  930. {
  931. $this->cacert = $cacert;
  932. }
  933. else
  934. {
  935. $this->cacertdir = $cacert;
  936. }
  937. }
  938. /**
  939. * Set attributes for SSL communication: private SSL key
  940. * @param string $key The name of a file containing a private SSL key
  941. * @param string $keypass The secret password needed to use the private SSL key
  942. * @access public
  943. * NB: does not work in older php/curl installs
  944. * Thanks to Daniel Convissor
  945. */
  946. function setKey($key, $keypass)
  947. {
  948. $this->key = $key;
  949. $this->keypass = $keypass;
  950. }
  951. /**
  952. * Set attributes for SSL communication: verify server certificate
  953. * @param bool $i enable/disable verification of peer certificate
  954. * @access public
  955. */
  956. function setSSLVerifyPeer($i)
  957. {
  958. $this->verifypeer = $i;
  959. }
  960. /**
  961. * Set attributes for SSL communication: verify match of server cert w. hostname
  962. * @param int $i
  963. * @access public
  964. */
  965. function setSSLVerifyHost($i)
  966. {
  967. $this->verifyhost = $i;
  968. }
  969. /**
  970. * Set proxy info
  971. * @param string $proxyhost
  972. * @param string $proxyport Defaults to 8080 for HTTP and 443 for HTTPS
  973. * @param string $proxyusername Leave blank if proxy has public access
  974. * @param string $proxypassword Leave blank if proxy has public access
  975. * @param int $proxyauthtype set to constant CURLAUTH_NTLM to use NTLM auth with proxy
  976. * @access public
  977. */
  978. function setProxy($proxyhost, $proxyport, $proxyusername = '', $proxypassword = '', $proxyauthtype = 1)
  979. {
  980. $this->proxy = $proxyhost;
  981. $this->proxyport = $proxyport;
  982. $this->proxy_user = $proxyusername;
  983. $this->proxy_pass = $proxypassword;
  984. $this->proxy_authtype = $proxyauthtype;
  985. }
  986. /**
  987. * Enables/disables reception of compressed xmlrpc responses.
  988. * Note that enabling reception of compressed responses merely adds some standard
  989. * http headers to xmlrpc requests. It is up to the xmlrpc server to return
  990. * compressed responses when receiving such requests.
  991. * @param string $compmethod either 'gzip', 'deflate', 'any' or ''
  992. * @access public
  993. */
  994. function setAcceptedCompression($compmethod)
  995. {
  996. if ($compmethod == 'any')
  997. $this->accepted_compression = array('gzip', 'deflate');
  998. else
  999. $this->accepted_compression = array($compmethod);
  1000. }
  1001. /**
  1002. * Enables/disables http compression of xmlrpc request.
  1003. * Take care when sending compressed requests: servers might not support them
  1004. * (and automatic fallback to uncompressed requests is not yet implemented)
  1005. * @param string $compmethod either 'gzip', 'deflate' or ''
  1006. * @access public
  1007. */
  1008. function setRequestCompression($compmethod)
  1009. {
  1010. $this->request_compression = $compmethod;
  1011. }
  1012. /**
  1013. * Adds a cookie to list of cookies that will be sent to server.
  1014. * NB: setting any param but name and value will turn the cookie into a 'version 1' cookie:
  1015. * do not do it unless you know what you are doing
  1016. * @param string $name
  1017. * @param string $value
  1018. * @param string $path
  1019. * @param string $domain
  1020. * @param int $port
  1021. * @access public
  1022. *
  1023. * @todo check correctness of urlencoding cookie value (copied from php way of doing it...)
  1024. */
  1025. function setCookie($name, $value='', $path='', $domain='', $port=null)
  1026. {
  1027. $this->cookies[$name]['value'] = urlencode($value);
  1028. if ($path || $domain || $port)
  1029. {
  1030. $this->cookies[$name]['path'] = $path;
  1031. $this->cookies[$name]['domain'] = $domain;
  1032. $this->cookies[$name]['port'] = $port;
  1033. $this->cookies[$name]['version'] = 1;
  1034. }
  1035. else
  1036. {
  1037. $this->cookies[$name]['version'] = 0;
  1038. }
  1039. }
  1040. /**
  1041. * Send an xmlrpc request
  1042. * @param mixed $msg The message object, or an array of messages for using multicall, or the complete xml representation of a request
  1043. * @param integer $timeout Connection timeout, in seconds, If unspecified, a platform specific timeout will apply
  1044. * @param string $method if left unspecified, the http protocol chosen during creation of the object will be used
  1045. * @return xmlrpcresp
  1046. * @access public
  1047. */
  1048. function& send($msg, $timeout=0, $method='')
  1049. {
  1050. // if user deos not specify http protocol, use native method of this client
  1051. // (i.e. method set during call to constructor)
  1052. if($method == '')
  1053. {
  1054. $method = $this->method;
  1055. }
  1056. if(is_array($msg))
  1057. {
  1058. // $msg is an array of xmlrpcmsg's
  1059. $r = $this->multicall($msg, $timeout, $method);
  1060. return $r;
  1061. }
  1062. elseif(is_string($msg))
  1063. {
  1064. $n =& new xmlrpcmsg('');
  1065. $n->payload = $msg;
  1066. $msg = $n;
  1067. }
  1068. // where msg is an xmlrpcmsg
  1069. $msg->debug=$this->debug;
  1070. if($method == 'https')
  1071. {
  1072. $r =& $this->sendPayloadHTTPS(
  1073. $msg,
  1074. $this->server,
  1075. $this->port,
  1076. $timeout,
  1077. $this->username,
  1078. $this->password,
  1079. $this->authtype,
  1080. $this->cert,
  1081. $this->certpass,
  1082. $this->cacert,
  1083. $this->cacertdir,
  1084. $this->proxy,
  1085. $this->proxyport,
  1086. $this->proxy_user,
  1087. $this->proxy_pass,
  1088. $this->proxy_authtype,
  1089. $this->keepalive,
  1090. $this->key,
  1091. $this->keypass
  1092. );
  1093. }
  1094. elseif($method == 'http11')
  1095. {
  1096. $r =& $this->sendPayloadCURL(
  1097. $msg,
  1098. $this->server,
  1099. $this->port,
  1100. $timeout,
  1101. $this->username,
  1102. $this->password,
  1103. $this->authtype,
  1104. null,
  1105. null,
  1106. null,
  1107. null,
  1108. $this->proxy,
  1109. $this->proxyport,
  1110. $this->proxy_user,
  1111. $this->proxy_pass,
  1112. $this->proxy_authtype,
  1113. 'http',
  1114. $this->keepalive
  1115. );
  1116. }
  1117. else
  1118. {
  1119. $r =& $this->sendPayloadHTTP10(
  1120. $msg,
  1121. $this->server,
  1122. $this->port,
  1123. $timeout,
  1124. $this->username,
  1125. $this->password,
  1126. $this->authtype,
  1127. $this->proxy,
  1128. $this->proxyport,
  1129. $this->proxy_user,
  1130. $this->proxy_pass,
  1131. $this->proxy_authtype
  1132. );
  1133. }
  1134. return $r;
  1135. }
  1136. /**
  1137. * @access private
  1138. */
  1139. function &sendPayloadHTTP10($msg, $server, $port, $timeout=0,
  1140. $username='', $password='', $authtype=1, $proxyhost='',
  1141. $proxyport=0, $proxyusername='', $proxypassword='', $proxyauthtype=1)
  1142. {
  1143. if($port==0)
  1144. {
  1145. $port=80;
  1146. }
  1147. // Only create the payload if it was not created previously
  1148. if(empty($msg->payload))
  1149. {
  1150. $msg->createPayload($this->request_charset_encoding);
  1151. }
  1152. $payload = $msg->payload;
  1153. // Deflate request body and set appropriate request headers
  1154. if(function_exists('gzdeflate') && ($this->request_compression == 'gzip' || $this->request_compression == 'deflate'))
  1155. {
  1156. if($this->request_compression == 'gzip')
  1157. {
  1158. $a = @gzencode($payload);
  1159. if($a)
  1160. {
  1161. $payload = $a;
  1162. $encoding_hdr = "Content-Encoding: gzip\r\n";
  1163. }
  1164. }
  1165. else
  1166. {
  1167. $a = @gzcompress($payload);
  1168. if($a)
  1169. {
  1170. $payload = $a;
  1171. $encoding_hdr = "Content-Encoding: deflate\r\n";
  1172. }
  1173. }
  1174. }
  1175. else
  1176. {
  1177. $encoding_hdr = '';
  1178. }
  1179. // thanks to Grant Rauscher <grant7@firstworld.net> for this
  1180. $credentials='';
  1181. if($username!='')
  1182. {
  1183. $credentials='Authorization: Basic ' . base64_encode($username . ':' . $password) . "\r\n";
  1184. if ($authtype != 1)
  1185. {
  1186. error_log('XML-RPC: xmlrpc_client::send: warning. Only Basic auth is supported with HTTP 1.0');
  1187. }
  1188. }
  1189. $accepted_encoding = '';
  1190. if(is_array($this->accepted_compression) && count($this->accepted_compression))
  1191. {
  1192. $accepted_encoding = 'Accept-Encoding: ' . implode(', ', $this->accepted_compression) . "\r\n";
  1193. }
  1194. $proxy_credentials = '';
  1195. if($proxyhost)
  1196. {
  1197. if($proxyport == 0)
  1198. {
  1199. $proxyport = 8080;
  1200. }
  1201. $connectserver = $proxyhost;
  1202. $connectport = $proxyport;
  1203. $uri = 'http://'.$server.':'.$port.$this->path;
  1204. if($proxyusername != '')
  1205. {
  1206. if ($proxyauthtype != 1)
  1207. {
  1208. error_log('XML-RPC: xmlrpc_client::send: warning. Only Basic auth to proxy is supported with HTTP 1.0');
  1209. }
  1210. $proxy_credentials = 'Proxy-Authorization: Basic ' . base64_encode($proxyusername.':'.$proxypassword) . "\r\n";
  1211. }
  1212. }
  1213. else
  1214. {
  1215. $connectserver = $server;
  1216. $connectport = $port;
  1217. $uri = $this->path;
  1218. }
  1219. // Cookie generation, as per rfc2965 (version 1 cookies) or
  1220. // netscape's rules (version 0 cookies)
  1221. $cookieheader='';
  1222. foreach ($this->cookies as $name => $cookie)
  1223. {
  1224. if ($cookie['version'])
  1225. {
  1226. $cookieheader .= 'Cookie: $Version="' . $cookie['version'] . '"; ';
  1227. $cookieheader .= $name . '="' . $cookie['value'] . '";';
  1228. if ($cookie['path'])
  1229. $cookieheader .= ' $Path="' . $cookie['path'] . '";';
  1230. if ($cookie['domain'])
  1231. $cookieheader .= ' $Domain="' . $cookie['domain'] . '";';
  1232. if ($cookie['port'])
  1233. $cookieheader .= ' $Port="' . $cookie['domain'] . '";';
  1234. $cookieheader = substr($cookieheader, 0, -1) . "\r\n";
  1235. }
  1236. else
  1237. {
  1238. $cookieheader .= 'Cookie: ' . $name . '=' . $cookie['value'] . "\r\n";
  1239. }
  1240. }
  1241. $op= 'POST ' . $uri. " HTTP/1.0\r\n" .
  1242. 'User-Agent: ' . $GLOBALS['xmlrpcName'] . ' ' . $GLOBALS['xmlrpcVersion'] . "\r\n" .
  1243. 'Host: '. $server . ':' . $port . "\r\n" .
  1244. $credentials .
  1245. $proxy_credentials .
  1246. $accepted_encoding .
  1247. $encoding_hdr .
  1248. 'Accept-Charset: ' . implode(',', $this->accepted_charset_encodings) . "\r\n" .
  1249. $cookieheader .
  1250. 'Content-Type: ' . $msg->content_type . "\r\nContent-Length: " .
  1251. strlen($payload) . "\r\n\r\n" .
  1252. $payload;
  1253. if($this->debug > 1)
  1254. {
  1255. print "<PRE>\n---SENDING---\n" . htmlentities($op) . "\n---END---\n</PRE>";
  1256. // let the client see this now in case http times out...
  1257. flush();
  1258. }
  1259. if($timeout>0)
  1260. {
  1261. $fp=@fsockopen($connectserver, $connectport, $this->errno, $this->errstr, $timeout);
  1262. }
  1263. else
  1264. {
  1265. $fp=@fsockopen($connectserver, $connectport, $this->errno, $this->errstr);
  1266. }
  1267. if($fp)
  1268. {
  1269. if($timeout>0 && function_exists('stream_set_timeout'))
  1270. {
  1271. stream_set_timeout($fp, $timeout);
  1272. }
  1273. }
  1274. else
  1275. {
  1276. $this->errstr='Connect error: '.$this->errstr;
  1277. $r=&new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['http_error'], $this->errstr . ' (' . $this->errno . ')');
  1278. return $r;
  1279. }
  1280. if(!fputs($fp, $op, strlen($op)))
  1281. {
  1282. $this->errstr='Write error';
  1283. $r=&new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['http_error'], $this->errstr);
  1284. return $r;
  1285. }
  1286. else
  1287. {
  1288. // reset errno and errstr on succesful socket connection
  1289. $this->errstr = '';
  1290. }
  1291. // G. Giunta 2005/10/24: close socket before parsing.
  1292. // should yeld slightly better execution times, and make easier recursive calls (e.g. to follow http redirects)
  1293. $ipd='';
  1294. while($data=fread($fp, 32768))
  1295. {
  1296. // shall we check for $data === FALSE?
  1297. // as per the manual, it signals an error
  1298. $ipd.=$data;
  1299. }
  1300. fclose($fp);
  1301. $r =& $msg->parseResponse($ipd, false, $this->return_type);
  1302. return $r;
  1303. }
  1304. /**
  1305. * @access private
  1306. */
  1307. function &sendPayloadHTTPS($msg, $server, $port, $timeout=0, $username='',
  1308. $password='', $authtype=1, $cert='',$certpass='', $cacert='', $cacertdir='',
  1309. $proxyhost='', $proxyport=0, $proxyusername='', $proxypassword='', $proxyauthtype=1,
  1310. $keepalive=false, $key='', $keypass='')
  1311. {
  1312. $r =& $this->sendPayloadCURL($msg, $server, $port, $timeout, $username,
  1313. $password, $authtype, $cert, $certpass, $cacert, $cacertdir, $proxyhost, $proxyport,
  1314. $proxyusername, $proxypassword, $proxyauthtype, 'https', $keepalive, $key, $keypass);
  1315. return $r;
  1316. }
  1317. /**
  1318. * Contributed by Justin Miller <justin@voxel.net>
  1319. * Requires curl to be built into PHP
  1320. * NB: CURL versions before 7.11.10 cannot use proxy to talk to https servers!
  1321. * @access private
  1322. */
  1323. function &sendPayloadCURL($msg, $server, $port, $timeout=0, $username='',
  1324. $password='', $authtype=1, $cert='', $certpass='', $cacert='', $cacertdir='',
  1325. $proxyhost='', $proxyport=0, $proxyusername='', $proxypassword='', $proxyauthtype=1, $method='https',
  1326. $keepalive=false, $key='', $keypass='')
  1327. {
  1328. if(!function_exists('curl_init'))
  1329. {
  1330. $this->errstr='CURL unavailable on this install';
  1331. $r=&new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['no_curl'], $GLOBALS['xmlrpcstr']['no_curl']);
  1332. return $r;
  1333. }
  1334. if($method == 'https')
  1335. {
  1336. if(($info = curl_version()) &&
  1337. ((is_string($info) && strpos($info, 'OpenSSL') === null) || (is_array($info) && !isset($info['ssl_version']))))
  1338. {
  1339. $this->errstr='SSL unavailable on this install';
  1340. $r=&new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['no_ssl'], $GLOBALS['xmlrpcstr']['no_ssl']);
  1341. return $r;
  1342. }
  1343. }
  1344. if($port == 0)
  1345. {
  1346. if($method == 'http')
  1347. {
  1348. $port = 80;
  1349. }
  1350. else
  1351. {
  1352. $port = 443;
  1353. }
  1354. }
  1355. // Only create the payload if it was not created previously
  1356. if(empty($msg->payload))
  1357. {
  1358. $msg->createPayload($this->request_charset_encoding);
  1359. }
  1360. // Deflate request body and set appropriate request headers
  1361. $payload = $msg->payload;
  1362. if(function_exists('gzdeflate') && ($this->request_compression == 'gzip' || $this->request_compression == 'deflate'))
  1363. {
  1364. if($this->request_compression == 'gzip')
  1365. {
  1366. $a = @gzencode($payload);
  1367. if($a)
  1368. {
  1369. $payload = $a;
  1370. $encoding_hdr = 'Content-Encoding: gzip';
  1371. }
  1372. }
  1373. else
  1374. {
  1375. $a = @gzcompress($payload);
  1376. if($a)
  1377. {
  1378. $payload = $a;
  1379. $encoding_hdr = 'Content-Encoding: deflate';
  1380. }
  1381. }
  1382. }
  1383. else
  1384. {
  1385. $encoding_hdr = '';
  1386. }
  1387. if($this->debug > 1)
  1388. {
  1389. print "<PRE>\n---SENDING---\n" . htmlentities($payload) . "\n---END---\n</PRE>";
  1390. // let the client see this now in case http times out...
  1391. flush();
  1392. }
  1393. if(!$keepalive || !$this->xmlrpc_curl_handle)
  1394. {
  1395. $curl = curl_init($method . '://' . $server . ':' . $port . $this->path);
  1396. if($keepalive)
  1397. {
  1398. $this->xmlrpc_curl_handle = $curl;
  1399. }
  1400. }
  1401. else
  1402. {
  1403. $curl = $this->xmlrpc_curl_handle;
  1404. }
  1405. // results into variable
  1406. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  1407. if($this->debug)
  1408. {
  1409. curl_setopt($curl, CURLOPT_VERBOSE, 1);
  1410. }
  1411. curl_setopt($curl, CURLOPT_USERAGENT, $GLOBALS['xmlrpcName'].' '.$GLOBALS['xmlrpcVersion']);
  1412. // required for XMLRPC: post the data
  1413. curl_setopt($curl, CURLOPT_POST, 1);
  1414. // the data
  1415. curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
  1416. // return the header too
  1417. curl_setopt($curl, CURLOPT_HEADER, 1);
  1418. // will only work with PHP >= 5.0
  1419. // NB: if we set an empty string, CURL will add http header indicating
  1420. // ALL methods it is supporting. This is possibly a better option than
  1421. // letting the user tell what curl can / cannot do...
  1422. if(is_array($this->accepted_compression) && count($this->accepted_compression))
  1423. {
  1424. //curl_setopt($curl, CURLOPT_ENCODING, implode(',', $this->accepted_compression));
  1425. // empty string means 'any supported by CURL' (shall we catch errors in case CURLOPT_SSLKEY undefined ?)
  1426. if (count($this->accepted_compression) == 1)
  1427. {
  1428. curl_setopt($curl, CURLOPT_ENCODING, $this->accepted_compression[0]);
  1429. }
  1430. else
  1431. curl_setopt($curl, CURLOPT_ENCODING, '');
  1432. }
  1433. // extra headers
  1434. $headers = array('Content-Type: ' . $msg->content_type , 'Accept-Charset: ' . implode(',', $this->accepted_charset_encodings));
  1435. // if no keepalive is wanted, let the server know it in advance
  1436. if(!$keepalive)
  1437. {
  1438. $headers[] = 'Connection: close';
  1439. }
  1440. // request compression header
  1441. if($encoding_hdr)
  1442. {
  1443. $headers[] = $encoding_hdr;
  1444. }
  1445. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  1446. // timeout is borked
  1447. if($timeout)
  1448. {
  1449. curl_setopt($curl, CURLOPT_TIMEOUT, $timeout == 1 ? 1 : $timeout - 1);
  1450. }
  1451. if($username && $password)
  1452. {
  1453. curl_setopt($curl, CURLOPT_USERPWD, $username.':'.$password);
  1454. if (defined('CURLOPT_HTTPAUTH'))
  1455. {
  1456. curl_setopt($curl, CURLOPT_HTTPAUTH, $authtype);
  1457. }
  1458. else if ($authtype != 1)
  1459. {
  1460. error_log('XML-RPC: xmlrpc_client::send: warning. Only Basic auth is supported by the current PHP/curl install');
  1461. }
  1462. }
  1463. if($method == 'https')
  1464. {
  1465. // set cert file
  1466. if($cert)
  1467. {
  1468. curl_setopt($curl, CURLOPT_SSLCERT, $cert);
  1469. }
  1470. // set cert password
  1471. if($certpass)
  1472. {
  1473. curl_setopt($curl, CURLOPT_SSLCERTPASSWD, $certpass);
  1474. }
  1475. // whether to verify remote host's cert
  1476. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->verifypeer);
  1477. // set ca certificates file/dir
  1478. if($cacert)
  1479. {
  1480. curl_setopt($curl, CURLOPT_CAINFO, $cacert);

Large files files are truncated, but you can click here to view the full file