PageRenderTime 94ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 1ms

/trunk/lib/xmlrpc/xmlrpc.inc

https://bitbucket.org/pooshonk/esw
PHP | 3778 lines | 2859 code | 165 blank | 754 comment | 360 complexity | b17b5f4e05bca3f2bee79bca69b8ef38 MD5 | raw file
Possible License(s): LGPL-2.1

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

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