Request.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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$
  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. * @return Zend_XmlRpc_Request
  102. */
  103. public function setEncoding($encoding)
  104. {
  105. $this->_encoding = $encoding;
  106. Zend_XmlRpc_Value::setEncoding($encoding);
  107. return $this;
  108. }
  109. /**
  110. * Retrieve current request encoding
  111. *
  112. * @return string
  113. */
  114. public function getEncoding()
  115. {
  116. return $this->_encoding;
  117. }
  118. /**
  119. * Set method to call
  120. *
  121. * @param string $method
  122. * @return boolean Returns true on success, false if method name is invalid
  123. */
  124. public function setMethod($method)
  125. {
  126. if (!is_string($method) || !preg_match('/^[a-z0-9_.:\/]+$/i', $method)) {
  127. $this->_fault = new Zend_XmlRpc_Fault(634, 'Invalid method name ("' . $method . '")');
  128. $this->_fault->setEncoding($this->getEncoding());
  129. return false;
  130. }
  131. $this->_method = $method;
  132. return true;
  133. }
  134. /**
  135. * Retrieve call method
  136. *
  137. * @return string
  138. */
  139. public function getMethod()
  140. {
  141. return $this->_method;
  142. }
  143. /**
  144. * Add a parameter to the parameter stack
  145. *
  146. * Adds a parameter to the parameter stack, associating it with the type
  147. * $type if provided
  148. *
  149. * @param mixed $value
  150. * @param string $type Optional; type hinting
  151. * @return void
  152. */
  153. public function addParam($value, $type = null)
  154. {
  155. $this->_params[] = $value;
  156. if (null === $type) {
  157. // Detect type if not provided explicitly
  158. if ($value instanceof Zend_XmlRpc_Value) {
  159. $type = $value->getType();
  160. } else {
  161. $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($value);
  162. $type = $xmlRpcValue->getType();
  163. }
  164. }
  165. $this->_types[] = $type;
  166. $this->_xmlRpcParams[] = array('value' => $value, 'type' => $type);
  167. }
  168. /**
  169. * Set the parameters array
  170. *
  171. * If called with a single, array value, that array is used to set the
  172. * parameters stack. If called with multiple values or a single non-array
  173. * value, the arguments are used to set the parameters stack.
  174. *
  175. * Best is to call with array of the format, in order to allow type hinting
  176. * when creating the XMLRPC values for each parameter:
  177. * <code>
  178. * $array = array(
  179. * array(
  180. * 'value' => $value,
  181. * 'type' => $type
  182. * )[, ... ]
  183. * );
  184. * </code>
  185. *
  186. * @access public
  187. * @return void
  188. */
  189. public function setParams()
  190. {
  191. $argc = func_num_args();
  192. $argv = func_get_args();
  193. if (0 == $argc) {
  194. return;
  195. }
  196. if ((1 == $argc) && is_array($argv[0])) {
  197. $params = array();
  198. $types = array();
  199. $wellFormed = true;
  200. foreach ($argv[0] as $arg) {
  201. if (!is_array($arg) || !isset($arg['value'])) {
  202. $wellFormed = false;
  203. break;
  204. }
  205. $params[] = $arg['value'];
  206. if (!isset($arg['type'])) {
  207. $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($arg['value']);
  208. $arg['type'] = $xmlRpcValue->getType();
  209. }
  210. $types[] = $arg['type'];
  211. }
  212. if ($wellFormed) {
  213. $this->_xmlRpcParams = $argv[0];
  214. $this->_params = $params;
  215. $this->_types = $types;
  216. } else {
  217. $this->_params = $argv[0];
  218. $this->_types = array();
  219. $xmlRpcParams = array();
  220. foreach ($argv[0] as $arg) {
  221. if ($arg instanceof Zend_XmlRpc_Value) {
  222. $type = $arg->getType();
  223. } else {
  224. $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($arg);
  225. $type = $xmlRpcValue->getType();
  226. }
  227. $xmlRpcParams[] = array('value' => $arg, 'type' => $type);
  228. $this->_types[] = $type;
  229. }
  230. $this->_xmlRpcParams = $xmlRpcParams;
  231. }
  232. return;
  233. }
  234. $this->_params = $argv;
  235. $this->_types = array();
  236. $xmlRpcParams = array();
  237. foreach ($argv as $arg) {
  238. if ($arg instanceof Zend_XmlRpc_Value) {
  239. $type = $arg->getType();
  240. } else {
  241. $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($arg);
  242. $type = $xmlRpcValue->getType();
  243. }
  244. $xmlRpcParams[] = array('value' => $arg, 'type' => $type);
  245. $this->_types[] = $type;
  246. }
  247. $this->_xmlRpcParams = $xmlRpcParams;
  248. }
  249. /**
  250. * Retrieve the array of parameters
  251. *
  252. * @return array
  253. */
  254. public function getParams()
  255. {
  256. return $this->_params;
  257. }
  258. /**
  259. * Return parameter types
  260. *
  261. * @return array
  262. */
  263. public function getTypes()
  264. {
  265. return $this->_types;
  266. }
  267. /**
  268. * Load XML and parse into request components
  269. *
  270. * @param string $request
  271. * @return boolean True on success, false if an error occurred.
  272. */
  273. public function loadXml($request)
  274. {
  275. if (!is_string($request)) {
  276. $this->_fault = new Zend_XmlRpc_Fault(635);
  277. $this->_fault->setEncoding($this->getEncoding());
  278. return false;
  279. }
  280. // @see ZF-12293 - disable external entities for security purposes
  281. $loadEntities = libxml_disable_entity_loader(true);
  282. try {
  283. $dom = new DOMDocument;
  284. $dom->loadXML($request);
  285. foreach ($dom->childNodes as $child) {
  286. if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
  287. require_once 'Zend/XmlRpc/Exception.php';
  288. throw new Zend_XmlRpc_Exception(
  289. 'Invalid XML: Detected use of illegal DOCTYPE'
  290. );
  291. }
  292. }
  293. $xml = simplexml_import_dom($dom);
  294. libxml_disable_entity_loader($loadEntities);
  295. } catch (Exception $e) {
  296. // Not valid XML
  297. $this->_fault = new Zend_XmlRpc_Fault(631);
  298. $this->_fault->setEncoding($this->getEncoding());
  299. libxml_disable_entity_loader($loadEntities);
  300. return false;
  301. }
  302. // Check for method name
  303. if (empty($xml->methodName)) {
  304. // Missing method name
  305. $this->_fault = new Zend_XmlRpc_Fault(632);
  306. $this->_fault->setEncoding($this->getEncoding());
  307. return false;
  308. }
  309. $this->_method = (string) $xml->methodName;
  310. // Check for parameters
  311. if (!empty($xml->params)) {
  312. $types = array();
  313. $argv = array();
  314. foreach ($xml->params->children() as $param) {
  315. if (!isset($param->value)) {
  316. $this->_fault = new Zend_XmlRpc_Fault(633);
  317. $this->_fault->setEncoding($this->getEncoding());
  318. return false;
  319. }
  320. try {
  321. $param = Zend_XmlRpc_Value::getXmlRpcValue($param->value, Zend_XmlRpc_Value::XML_STRING);
  322. $types[] = $param->getType();
  323. $argv[] = $param->getValue();
  324. } catch (Exception $e) {
  325. $this->_fault = new Zend_XmlRpc_Fault(636);
  326. $this->_fault->setEncoding($this->getEncoding());
  327. return false;
  328. }
  329. }
  330. $this->_types = $types;
  331. $this->_params = $argv;
  332. }
  333. $this->_xml = $request;
  334. return true;
  335. }
  336. /**
  337. * Does the current request contain errors and should it return a fault
  338. * response?
  339. *
  340. * @return boolean
  341. */
  342. public function isFault()
  343. {
  344. return $this->_fault instanceof Zend_XmlRpc_Fault;
  345. }
  346. /**
  347. * Retrieve the fault response, if any
  348. *
  349. * @return null|Zend_XmlRpc_Fault
  350. */
  351. public function getFault()
  352. {
  353. return $this->_fault;
  354. }
  355. /**
  356. * Retrieve method parameters as XMLRPC values
  357. *
  358. * @return array
  359. */
  360. protected function _getXmlRpcParams()
  361. {
  362. $params = array();
  363. if (is_array($this->_xmlRpcParams)) {
  364. foreach ($this->_xmlRpcParams as $param) {
  365. $value = $param['value'];
  366. $type = isset($param['type']) ? $param['type'] : Zend_XmlRpc_Value::AUTO_DETECT_TYPE;
  367. if (!$value instanceof Zend_XmlRpc_Value) {
  368. $value = Zend_XmlRpc_Value::getXmlRpcValue($value, $type);
  369. }
  370. $params[] = $value;
  371. }
  372. }
  373. return $params;
  374. }
  375. /**
  376. * Create XML request
  377. *
  378. * @return string
  379. */
  380. public function saveXml()
  381. {
  382. $args = $this->_getXmlRpcParams();
  383. $method = $this->getMethod();
  384. $generator = Zend_XmlRpc_Value::getGenerator();
  385. $generator->openElement('methodCall')
  386. ->openElement('methodName', $method)
  387. ->closeElement('methodName');
  388. if (is_array($args) && count($args)) {
  389. $generator->openElement('params');
  390. foreach ($args as $arg) {
  391. $generator->openElement('param');
  392. $arg->generateXml();
  393. $generator->closeElement('param');
  394. }
  395. $generator->closeElement('params');
  396. }
  397. $generator->closeElement('methodCall');
  398. return $generator->flush();
  399. }
  400. /**
  401. * Return XML request
  402. *
  403. * @return string
  404. */
  405. public function __toString()
  406. {
  407. return $this->saveXML();
  408. }
  409. }