PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Zend/XmlRpc/Request.php

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