PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/XmlRpc/Request.php

https://bitbucket.org/Ebozavrik/test-application
PHP | 466 lines | 233 code | 53 blank | 180 comment | 30 complexity | 039ecf511bf4d5a5243934087547beab MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * Zend_XmlRpc_Value
  22. */
  23. require_once 'Zend/XmlRpc/Value.php';
  24. /**
  25. * Zend_XmlRpc_Fault
  26. */
  27. require_once 'Zend/XmlRpc/Fault.php';
  28. /**
  29. * XmlRpc Request object
  30. *
  31. * Encapsulates an XmlRpc request, holding the method call and all parameters.
  32. * Provides accessors for these, as well as the ability to load from XML and to
  33. * create the XML request string.
  34. *
  35. * Additionally, if errors occur setting the method or parsing XML, a fault is
  36. * generated and stored in {@link $_fault}; developers may check for it using
  37. * {@link isFault()} and {@link getFault()}.
  38. *
  39. * @category Zend
  40. * @package Zend_XmlRpc
  41. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. * @version $Id: Request.php 25033 2012-08-17 19:50:08Z matthew $
  44. */
  45. class Zend_XmlRpc_Request
  46. {
  47. /**
  48. * Request character encoding
  49. * @var string
  50. */
  51. protected $_encoding = 'UTF-8';
  52. /**
  53. * Method to call
  54. * @var string
  55. */
  56. protected $_method;
  57. /**
  58. * XML request
  59. * @var string
  60. */
  61. protected $_xml;
  62. /**
  63. * Method parameters
  64. * @var array
  65. */
  66. protected $_params = array();
  67. /**
  68. * Fault object, if any
  69. * @var Zend_XmlRpc_Fault
  70. */
  71. protected $_fault = null;
  72. /**
  73. * XML-RPC type for each param
  74. * @var array
  75. */
  76. protected $_types = array();
  77. /**
  78. * XML-RPC request params
  79. * @var array
  80. */
  81. protected $_xmlRpcParams = array();
  82. /**
  83. * Create a new XML-RPC request
  84. *
  85. * @param string $method (optional)
  86. * @param array $params (optional)
  87. */
  88. public function __construct ($method = null, $params = null)
  89. {
  90. if ($method !== null) {
  91. $this->setMethod($method);
  92. }
  93. if ($params !== null) {
  94. $this->setParams($params);
  95. }
  96. }
  97. /**
  98. * Set encoding to use in request
  99. *
  100. * @param string $encoding
  101. *
  102. * @return Zend_XmlRpc_Request
  103. */
  104. public function setEncoding ($encoding)
  105. {
  106. $this->_encoding = $encoding;
  107. Zend_XmlRpc_Value::setEncoding($encoding);
  108. return $this;
  109. }
  110. /**
  111. * Retrieve current request encoding
  112. *
  113. * @return string
  114. */
  115. public function getEncoding ()
  116. {
  117. return $this->_encoding;
  118. }
  119. /**
  120. * Set method to call
  121. *
  122. * @param string $method
  123. *
  124. * @return boolean Returns true on success, false if method name is invalid
  125. */
  126. public function setMethod ($method)
  127. {
  128. if (!is_string($method) || !preg_match('/^[a-z0-9_.:\/]+$/i', $method)) {
  129. $this->_fault = new Zend_XmlRpc_Fault( 634, 'Invalid method name ("' . $method . '")' );
  130. $this->_fault->setEncoding($this->getEncoding());
  131. return false;
  132. }
  133. $this->_method = $method;
  134. return true;
  135. }
  136. /**
  137. * Retrieve call method
  138. *
  139. * @return string
  140. */
  141. public function getMethod ()
  142. {
  143. return $this->_method;
  144. }
  145. /**
  146. * Add a parameter to the parameter stack
  147. *
  148. * Adds a parameter to the parameter stack, associating it with the type
  149. * $type if provided
  150. *
  151. * @param mixed $value
  152. * @param string $type Optional; type hinting
  153. *
  154. * @return void
  155. */
  156. public function addParam ($value, $type = null)
  157. {
  158. $this->_params[] = $value;
  159. if (null === $type) {
  160. // Detect type if not provided explicitly
  161. if ($value instanceof Zend_XmlRpc_Value) {
  162. $type = $value->getType();
  163. } else {
  164. $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($value);
  165. $type = $xmlRpcValue->getType();
  166. }
  167. }
  168. $this->_types[] = $type;
  169. $this->_xmlRpcParams[] = array( 'value' => $value, 'type' => $type );
  170. }
  171. /**
  172. * Set the parameters array
  173. *
  174. * If called with a single, array value, that array is used to set the
  175. * parameters stack. If called with multiple values or a single non-array
  176. * value, the arguments are used to set the parameters stack.
  177. *
  178. * Best is to call with array of the format, in order to allow type hinting
  179. * when creating the XMLRPC values for each parameter:
  180. * <code>
  181. * $array = array(
  182. * array(
  183. * 'value' => $value,
  184. * 'type' => $type
  185. * )[, ... ]
  186. * );
  187. * </code>
  188. *
  189. * @access public
  190. * @return void
  191. */
  192. public function setParams ()
  193. {
  194. $argc = func_num_args();
  195. $argv = func_get_args();
  196. if (0 == $argc) {
  197. return;
  198. }
  199. if (( 1 == $argc ) && is_array($argv[0])) {
  200. $params = array();
  201. $types = array();
  202. $wellFormed = true;
  203. foreach ($argv[0] as $arg) {
  204. if (!is_array($arg) || !isset( $arg['value'] )) {
  205. $wellFormed = false;
  206. break;
  207. }
  208. $params[] = $arg['value'];
  209. if (!isset( $arg['type'] )) {
  210. $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($arg['value']);
  211. $arg['type'] = $xmlRpcValue->getType();
  212. }
  213. $types[] = $arg['type'];
  214. }
  215. if ($wellFormed) {
  216. $this->_xmlRpcParams = $argv[0];
  217. $this->_params = $params;
  218. $this->_types = $types;
  219. } else {
  220. $this->_params = $argv[0];
  221. $this->_types = array();
  222. $xmlRpcParams = array();
  223. foreach ($argv[0] as $arg) {
  224. if ($arg instanceof Zend_XmlRpc_Value) {
  225. $type = $arg->getType();
  226. } else {
  227. $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($arg);
  228. $type = $xmlRpcValue->getType();
  229. }
  230. $xmlRpcParams[] = array( 'value' => $arg, 'type' => $type );
  231. $this->_types[] = $type;
  232. }
  233. $this->_xmlRpcParams = $xmlRpcParams;
  234. }
  235. return;
  236. }
  237. $this->_params = $argv;
  238. $this->_types = array();
  239. $xmlRpcParams = array();
  240. foreach ($argv as $arg) {
  241. if ($arg instanceof Zend_XmlRpc_Value) {
  242. $type = $arg->getType();
  243. } else {
  244. $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($arg);
  245. $type = $xmlRpcValue->getType();
  246. }
  247. $xmlRpcParams[] = array( 'value' => $arg, 'type' => $type );
  248. $this->_types[] = $type;
  249. }
  250. $this->_xmlRpcParams = $xmlRpcParams;
  251. }
  252. /**
  253. * Retrieve the array of parameters
  254. *
  255. * @return array
  256. */
  257. public function getParams ()
  258. {
  259. return $this->_params;
  260. }
  261. /**
  262. * Return parameter types
  263. *
  264. * @return array
  265. */
  266. public function getTypes ()
  267. {
  268. return $this->_types;
  269. }
  270. /**
  271. * Load XML and parse into request components
  272. *
  273. * @param string $request
  274. *
  275. * @return boolean True on success, false if an error occurred.
  276. */
  277. public function loadXml ($request)
  278. {
  279. if (!is_string($request)) {
  280. $this->_fault = new Zend_XmlRpc_Fault( 635 );
  281. $this->_fault->setEncoding($this->getEncoding());
  282. return false;
  283. }
  284. // @see ZF-12293 - disable external entities for security purposes
  285. $loadEntities = libxml_disable_entity_loader(true);
  286. try {
  287. $dom = new DOMDocument;
  288. $dom->loadXML($request);
  289. foreach ($dom->childNodes as $child) {
  290. if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
  291. require_once 'Zend/XmlRpc/Exception.php';
  292. throw new Zend_XmlRpc_Exception(
  293. 'Invalid XML: Detected use of illegal DOCTYPE'
  294. );
  295. }
  296. }
  297. $xml = simplexml_import_dom($dom);
  298. libxml_disable_entity_loader($loadEntities);
  299. } catch (Exception $e) {
  300. // Not valid XML
  301. $this->_fault = new Zend_XmlRpc_Fault( 631 );
  302. $this->_fault->setEncoding($this->getEncoding());
  303. libxml_disable_entity_loader($loadEntities);
  304. return false;
  305. }
  306. // Check for method name
  307. if (empty( $xml->methodName )) {
  308. // Missing method name
  309. $this->_fault = new Zend_XmlRpc_Fault( 632 );
  310. $this->_fault->setEncoding($this->getEncoding());
  311. return false;
  312. }
  313. $this->_method = (string)$xml->methodName;
  314. // Check for parameters
  315. if (!empty( $xml->params )) {
  316. $types = array();
  317. $argv = array();
  318. foreach ($xml->params->children() as $param) {
  319. if (!isset( $param->value )) {
  320. $this->_fault = new Zend_XmlRpc_Fault( 633 );
  321. $this->_fault->setEncoding($this->getEncoding());
  322. return false;
  323. }
  324. try {
  325. $param = Zend_XmlRpc_Value::getXmlRpcValue($param->value, Zend_XmlRpc_Value::XML_STRING);
  326. $types[] = $param->getType();
  327. $argv[] = $param->getValue();
  328. } catch (Exception $e) {
  329. $this->_fault = new Zend_XmlRpc_Fault( 636 );
  330. $this->_fault->setEncoding($this->getEncoding());
  331. return false;
  332. }
  333. }
  334. $this->_types = $types;
  335. $this->_params = $argv;
  336. }
  337. $this->_xml = $request;
  338. return true;
  339. }
  340. /**
  341. * Does the current request contain errors and should it return a fault
  342. * response?
  343. *
  344. * @return boolean
  345. */
  346. public function isFault ()
  347. {
  348. return $this->_fault instanceof Zend_XmlRpc_Fault;
  349. }
  350. /**
  351. * Retrieve the fault response, if any
  352. *
  353. * @return null|Zend_XmlRpc_Fault
  354. */
  355. public function getFault ()
  356. {
  357. return $this->_fault;
  358. }
  359. /**
  360. * Retrieve method parameters as XMLRPC values
  361. *
  362. * @return array
  363. */
  364. protected function _getXmlRpcParams ()
  365. {
  366. $params = array();
  367. if (is_array($this->_xmlRpcParams)) {
  368. foreach ($this->_xmlRpcParams as $param) {
  369. $value = $param['value'];
  370. $type = isset( $param['type'] ) ? $param['type'] : Zend_XmlRpc_Value::AUTO_DETECT_TYPE;
  371. if (!$value instanceof Zend_XmlRpc_Value) {
  372. $value = Zend_XmlRpc_Value::getXmlRpcValue($value, $type);
  373. }
  374. $params[] = $value;
  375. }
  376. }
  377. return $params;
  378. }
  379. /**
  380. * Create XML request
  381. *
  382. * @return string
  383. */
  384. public function saveXml ()
  385. {
  386. $args = $this->_getXmlRpcParams();
  387. $method = $this->getMethod();
  388. $generator = Zend_XmlRpc_Value::getGenerator();
  389. $generator->openElement('methodCall')
  390. ->openElement('methodName', $method)
  391. ->closeElement('methodName');
  392. if (is_array($args) && count($args)) {
  393. $generator->openElement('params');
  394. foreach ($args as $arg) {
  395. $generator->openElement('param');
  396. $arg->generateXml();
  397. $generator->closeElement('param');
  398. }
  399. $generator->closeElement('params');
  400. }
  401. $generator->closeElement('methodCall');
  402. return $generator->flush();
  403. }
  404. /**
  405. * Return XML request
  406. *
  407. * @return string
  408. */
  409. public function __toString ()
  410. {
  411. return $this->saveXML();
  412. }
  413. }