PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/zendframework1/library/Zend/XmlRpc/Request.php

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