PageRenderTime 29ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/zend/Zend/XmlRpc/Request.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 446 lines | 226 code | 44 blank | 176 comment | 29 complexity | feb755d6ea2c85fd7e17d76b3d656d4b 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-2010 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-2010 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. $xml = new SimpleXMLElement($request);
  284. } catch (Exception $e) {
  285. // Not valid XML
  286. $this->_fault = new Zend_XmlRpc_Fault(631);
  287. $this->_fault->setEncoding($this->getEncoding());
  288. libxml_disable_entity_loader($loadEntities);
  289. return false;
  290. }
  291. // Check for method name
  292. if (empty($xml->methodName)) {
  293. // Missing method name
  294. $this->_fault = new Zend_XmlRpc_Fault(632);
  295. $this->_fault->setEncoding($this->getEncoding());
  296. libxml_disable_entity_loader($loadEntities);
  297. return false;
  298. }
  299. $this->_method = (string) $xml->methodName;
  300. // Check for parameters
  301. if (!empty($xml->params)) {
  302. $types = array();
  303. $argv = array();
  304. foreach ($xml->params->children() as $param) {
  305. if (!isset($param->value)) {
  306. $this->_fault = new Zend_XmlRpc_Fault(633);
  307. $this->_fault->setEncoding($this->getEncoding());
  308. libxml_disable_entity_loader($loadEntities);
  309. return false;
  310. }
  311. try {
  312. $param = Zend_XmlRpc_Value::getXmlRpcValue($param->value, Zend_XmlRpc_Value::XML_STRING);
  313. $types[] = $param->getType();
  314. $argv[] = $param->getValue();
  315. } catch (Exception $e) {
  316. $this->_fault = new Zend_XmlRpc_Fault(636);
  317. $this->_fault->setEncoding($this->getEncoding());
  318. libxml_disable_entity_loader($loadEntities);
  319. return false;
  320. }
  321. }
  322. $this->_types = $types;
  323. $this->_params = $argv;
  324. }
  325. libxml_disable_entity_loader($loadEntities);
  326. $this->_xml = $request;
  327. return true;
  328. }
  329. /**
  330. * Does the current request contain errors and should it return a fault
  331. * response?
  332. *
  333. * @return boolean
  334. */
  335. public function isFault()
  336. {
  337. return $this->_fault instanceof Zend_XmlRpc_Fault;
  338. }
  339. /**
  340. * Retrieve the fault response, if any
  341. *
  342. * @return null|Zend_XmlRpc_Fault
  343. */
  344. public function getFault()
  345. {
  346. return $this->_fault;
  347. }
  348. /**
  349. * Retrieve method parameters as XMLRPC values
  350. *
  351. * @return array
  352. */
  353. protected function _getXmlRpcParams()
  354. {
  355. $params = array();
  356. if (is_array($this->_xmlRpcParams)) {
  357. foreach ($this->_xmlRpcParams as $param) {
  358. $value = $param['value'];
  359. $type = isset($param['type']) ? $param['type'] : Zend_XmlRpc_Value::AUTO_DETECT_TYPE;
  360. if (!$value instanceof Zend_XmlRpc_Value) {
  361. $value = Zend_XmlRpc_Value::getXmlRpcValue($value, $type);
  362. }
  363. $params[] = $value;
  364. }
  365. }
  366. return $params;
  367. }
  368. /**
  369. * Create XML request
  370. *
  371. * @return string
  372. */
  373. public function saveXml()
  374. {
  375. $args = $this->_getXmlRpcParams();
  376. $method = $this->getMethod();
  377. $generator = Zend_XmlRpc_Value::getGenerator();
  378. $generator->openElement('methodCall')
  379. ->openElement('methodName', $method)
  380. ->closeElement('methodName');
  381. if (is_array($args) && count($args)) {
  382. $generator->openElement('params');
  383. foreach ($args as $arg) {
  384. $generator->openElement('param');
  385. $arg->generateXml();
  386. $generator->closeElement('param');
  387. }
  388. $generator->closeElement('params');
  389. }
  390. $generator->closeElement('methodCall');
  391. return $generator->flush();
  392. }
  393. /**
  394. * Return XML request
  395. *
  396. * @return string
  397. */
  398. public function __toString()
  399. {
  400. return $this->saveXML();
  401. }
  402. }