PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/XmlRpc/Request.php

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