PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/XmlRpc/Value.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 517 lines | 294 code | 51 blank | 172 comment | 24 complexity | 2f96c3bb08a7e7cbe8f311245216fa93 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_XmlRpc
  17. * @subpackage Value
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Value.php 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /**
  23. * Represent a native XML-RPC value entity, used as parameters for the methods
  24. * called by the Zend_XmlRpc_Client object and as the return value for those calls.
  25. *
  26. * This object as a very important static function Zend_XmlRpc_Value::getXmlRpcValue, this
  27. * function acts likes a factory for the Zend_XmlRpc_Value objects
  28. *
  29. * Using this function, users/Zend_XmlRpc_Client object can create the Zend_XmlRpc_Value objects
  30. * from PHP variables, XML string or by specifing the exact XML-RPC natvie type
  31. *
  32. * @package Zend_XmlRpc
  33. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. abstract class Zend_XmlRpc_Value
  37. {
  38. /**
  39. * The native XML-RPC representation of this object's value
  40. *
  41. * If the native type of this object is array or struct, this will be an array
  42. * of Zend_XmlRpc_Value objects
  43. */
  44. protected $_value;
  45. /**
  46. * The native XML-RPC type of this object
  47. * One of the XMLRPC_TYPE_* constants
  48. */
  49. protected $_type;
  50. /**
  51. * XML code representation of this object (will be calculated only once)
  52. */
  53. protected $_xml;
  54. /**
  55. * @var Zend_XmlRpc_Generator_GeneratorAbstract
  56. */
  57. protected static $_generator;
  58. /**
  59. * Specify that the XML-RPC native type will be auto detected from a PHP variable type
  60. */
  61. const AUTO_DETECT_TYPE = 'auto_detect';
  62. /**
  63. * Specify that the XML-RPC value will be parsed out from a given XML code
  64. */
  65. const XML_STRING = 'xml';
  66. /**
  67. * All the XML-RPC native types
  68. */
  69. const XMLRPC_TYPE_I4 = 'i4';
  70. const XMLRPC_TYPE_INTEGER = 'int';
  71. const XMLRPC_TYPE_I8 = 'i8';
  72. const XMLRPC_TYPE_APACHEI8 = 'ex:i8';
  73. const XMLRPC_TYPE_DOUBLE = 'double';
  74. const XMLRPC_TYPE_BOOLEAN = 'boolean';
  75. const XMLRPC_TYPE_STRING = 'string';
  76. const XMLRPC_TYPE_DATETIME = 'dateTime.iso8601';
  77. const XMLRPC_TYPE_BASE64 = 'base64';
  78. const XMLRPC_TYPE_ARRAY = 'array';
  79. const XMLRPC_TYPE_STRUCT = 'struct';
  80. const XMLRPC_TYPE_NIL = 'nil';
  81. const XMLRPC_TYPE_APACHENIL = 'ex:nil';
  82. /**
  83. * Get the native XML-RPC type (the type is one of the Zend_XmlRpc_Value::XMLRPC_TYPE_* constants)
  84. *
  85. * @return string
  86. */
  87. public function getType()
  88. {
  89. return $this->_type;
  90. }
  91. /**
  92. * Get XML generator instance
  93. *
  94. * @return Zend_XmlRpc_Generator_GeneratorAbstract
  95. */
  96. public static function getGenerator()
  97. {
  98. if (!self::$_generator) {
  99. if (extension_loaded('xmlwriter')) {
  100. require_once 'Zend/XmlRpc/Generator/XmlWriter.php';
  101. self::$_generator = new Zend_XmlRpc_Generator_XmlWriter();
  102. } else {
  103. require_once 'Zend/XmlRpc/Generator/DomDocument.php';
  104. self::$_generator = new Zend_XmlRpc_Generator_DomDocument();
  105. }
  106. }
  107. return self::$_generator;
  108. }
  109. /**
  110. * Sets XML generator instance
  111. *
  112. * @param Zend_XmlRpc_Generator_GeneratorAbstract $generator
  113. * @return void
  114. */
  115. public static function setGenerator(Zend_XmlRpc_Generator_GeneratorAbstract $generator)
  116. {
  117. self::$_generator = $generator;
  118. }
  119. /**
  120. * Changes the encoding of the generator
  121. *
  122. * @param string $encoding
  123. * @return void
  124. */
  125. public static function setEncoding($encoding)
  126. {
  127. $generator = self::getGenerator();
  128. $newGenerator = new $generator($encoding);
  129. self::setGenerator($newGenerator);
  130. }
  131. /**
  132. * Return the value of this object, convert the XML-RPC native value into a PHP variable
  133. *
  134. * @return mixed
  135. */
  136. abstract public function getValue();
  137. /**
  138. * Return the XML code that represent a native MXL-RPC value
  139. *
  140. * @return string
  141. */
  142. public function saveXml()
  143. {
  144. if (!$this->_xml) {
  145. $this->generateXml();
  146. $this->_xml = (string) $this->getGenerator();
  147. }
  148. return $this->_xml;
  149. }
  150. /**
  151. * Generate XML code that represent a native XML/RPC value
  152. *
  153. * @return void
  154. */
  155. public function generateXml()
  156. {
  157. $this->_generateXml();
  158. }
  159. /**
  160. * Creates a Zend_XmlRpc_Value* object, representing a native XML-RPC value
  161. * A XmlRpcValue object can be created in 3 ways:
  162. * 1. Autodetecting the native type out of a PHP variable
  163. * (if $type is not set or equal to Zend_XmlRpc_Value::AUTO_DETECT_TYPE)
  164. * 2. By specifing the native type ($type is one of the Zend_XmlRpc_Value::XMLRPC_TYPE_* constants)
  165. * 3. From a XML string ($type is set to Zend_XmlRpc_Value::XML_STRING)
  166. *
  167. * By default the value type is autodetected according to it's PHP type
  168. *
  169. * @param mixed $value
  170. * @param Zend_XmlRpc_Value::constant $type
  171. *
  172. * @return Zend_XmlRpc_Value
  173. * @static
  174. */
  175. public static function getXmlRpcValue($value, $type = self::AUTO_DETECT_TYPE)
  176. {
  177. switch ($type) {
  178. case self::AUTO_DETECT_TYPE:
  179. // Auto detect the XML-RPC native type from the PHP type of $value
  180. return self::_phpVarToNativeXmlRpc($value);
  181. case self::XML_STRING:
  182. // Parse the XML string given in $value and get the XML-RPC value in it
  183. return self::_xmlStringToNativeXmlRpc($value);
  184. case self::XMLRPC_TYPE_I4:
  185. // fall through to the next case
  186. case self::XMLRPC_TYPE_INTEGER:
  187. require_once 'Zend/XmlRpc/Value/Integer.php';
  188. return new Zend_XmlRpc_Value_Integer($value);
  189. case self::XMLRPC_TYPE_I8:
  190. // fall through to the next case
  191. case self::XMLRPC_TYPE_APACHEI8:
  192. require_once 'Zend/XmlRpc/Value/BigInteger.php';
  193. return new Zend_XmlRpc_Value_BigInteger($value);
  194. case self::XMLRPC_TYPE_DOUBLE:
  195. require_once 'Zend/XmlRpc/Value/Double.php';
  196. return new Zend_XmlRpc_Value_Double($value);
  197. case self::XMLRPC_TYPE_BOOLEAN:
  198. require_once 'Zend/XmlRpc/Value/Boolean.php';
  199. return new Zend_XmlRpc_Value_Boolean($value);
  200. case self::XMLRPC_TYPE_STRING:
  201. require_once 'Zend/XmlRpc/Value/String.php';
  202. return new Zend_XmlRpc_Value_String($value);
  203. case self::XMLRPC_TYPE_BASE64:
  204. require_once 'Zend/XmlRpc/Value/Base64.php';
  205. return new Zend_XmlRpc_Value_Base64($value);
  206. case self::XMLRPC_TYPE_NIL:
  207. // fall through to the next case
  208. case self::XMLRPC_TYPE_APACHENIL:
  209. require_once 'Zend/XmlRpc/Value/Nil.php';
  210. return new Zend_XmlRpc_Value_Nil();
  211. case self::XMLRPC_TYPE_DATETIME:
  212. require_once 'Zend/XmlRpc/Value/DateTime.php';
  213. return new Zend_XmlRpc_Value_DateTime($value);
  214. case self::XMLRPC_TYPE_ARRAY:
  215. require_once 'Zend/XmlRpc/Value/Array.php';
  216. return new Zend_XmlRpc_Value_Array($value);
  217. case self::XMLRPC_TYPE_STRUCT:
  218. require_once 'Zend/XmlRpc/Value/Struct.php';
  219. return new Zend_XmlRpc_Value_Struct($value);
  220. default:
  221. require_once 'Zend/XmlRpc/Value/Exception.php';
  222. throw new Zend_XmlRpc_Value_Exception('Given type is not a '. __CLASS__ .' constant');
  223. }
  224. }
  225. /**
  226. * Get XML-RPC type for a PHP native variable
  227. *
  228. * @static
  229. * @param mixed $value
  230. * @return string
  231. */
  232. public static function getXmlRpcTypeByValue($value)
  233. {
  234. if (is_object($value)) {
  235. if ($value instanceof Zend_XmlRpc_Value) {
  236. return $value->getType();
  237. } elseif (($value instanceof Zend_Date) || ($value instanceof DateTime)) {
  238. return self::XMLRPC_TYPE_DATETIME;
  239. }
  240. return self::getXmlRpcTypeByValue(get_object_vars($value));
  241. } elseif (is_array($value)) {
  242. if (!empty($value) && is_array($value) && (array_keys($value) !== range(0, count($value) - 1))) {
  243. return self::XMLRPC_TYPE_STRUCT;
  244. }
  245. return self::XMLRPC_TYPE_ARRAY;
  246. } elseif (is_int($value)) {
  247. return ($value > PHP_INT_MAX) ? self::XMLRPC_TYPE_I8 : self::XMLRPC_TYPE_INTEGER;
  248. } elseif (is_double($value)) {
  249. return self::XMLRPC_TYPE_DOUBLE;
  250. } elseif (is_bool($value)) {
  251. return self::XMLRPC_TYPE_BOOLEAN;
  252. } elseif (is_null($value)) {
  253. return self::XMLRPC_TYPE_NIL;
  254. } elseif (is_string($value)) {
  255. return self::XMLRPC_TYPE_STRING;
  256. }
  257. throw new Zend_XmlRpc_Value_Exception(sprintf(
  258. 'No matching XMLRPC type found for php type %s.',
  259. gettype($value)
  260. ));
  261. }
  262. /**
  263. * Transform a PHP native variable into a XML-RPC native value
  264. *
  265. * @param mixed $value The PHP variable for convertion
  266. *
  267. * @return Zend_XmlRpc_Value
  268. * @static
  269. */
  270. protected static function _phpVarToNativeXmlRpc($value)
  271. {
  272. // @see http://framework.zend.com/issues/browse/ZF-8623
  273. if (is_object($value)) {
  274. if ($value instanceof Zend_XmlRpc_Value) {
  275. return $value;
  276. }
  277. if ($value instanceof Zend_Crypt_Math_BigInteger) {
  278. require_once 'Zend/XmlRpc/Value/Exception.php';
  279. throw new Zend_XmlRpc_Value_Exception(
  280. 'Using Zend_Crypt_Math_BigInteger to get an ' .
  281. 'instance of Zend_XmlRpc_Value_BigInteger is not ' .
  282. 'available anymore.'
  283. );
  284. }
  285. }
  286. switch (self::getXmlRpcTypeByValue($value))
  287. {
  288. case self::XMLRPC_TYPE_DATETIME:
  289. require_once 'Zend/XmlRpc/Value/DateTime.php';
  290. return new Zend_XmlRpc_Value_DateTime($value);
  291. case self::XMLRPC_TYPE_ARRAY:
  292. require_once 'Zend/XmlRpc/Value/Array.php';
  293. return new Zend_XmlRpc_Value_Array($value);
  294. case self::XMLRPC_TYPE_STRUCT:
  295. require_once 'Zend/XmlRpc/Value/Struct.php';
  296. return new Zend_XmlRpc_Value_Struct($value);
  297. case self::XMLRPC_TYPE_INTEGER:
  298. require_once 'Zend/XmlRpc/Value/Integer.php';
  299. return new Zend_XmlRpc_Value_Integer($value);
  300. case self::XMLRPC_TYPE_DOUBLE:
  301. require_once 'Zend/XmlRpc/Value/Double.php';
  302. return new Zend_XmlRpc_Value_Double($value);
  303. case self::XMLRPC_TYPE_BOOLEAN:
  304. require_once 'Zend/XmlRpc/Value/Boolean.php';
  305. return new Zend_XmlRpc_Value_Boolean($value);
  306. case self::XMLRPC_TYPE_NIL:
  307. require_once 'Zend/XmlRpc/Value/Nil.php';
  308. return new Zend_XmlRpc_Value_Nil;
  309. case self::XMLRPC_TYPE_STRING:
  310. // Fall through to the next case
  311. default:
  312. // If type isn't identified (or identified as string), it treated as string
  313. require_once 'Zend/XmlRpc/Value/String.php';
  314. return new Zend_XmlRpc_Value_String($value);
  315. }
  316. }
  317. /**
  318. * Transform an XML string into a XML-RPC native value
  319. *
  320. * @param string|SimpleXMLElement $xml A SimpleXMLElement object represent the XML string
  321. * It can be also a valid XML string for convertion
  322. *
  323. * @return Zend_XmlRpc_Value
  324. * @static
  325. */
  326. protected static function _xmlStringToNativeXmlRpc($xml)
  327. {
  328. self::_createSimpleXMLElement($xml);
  329. self::_extractTypeAndValue($xml, $type, $value);
  330. switch ($type) {
  331. // All valid and known XML-RPC native values
  332. case self::XMLRPC_TYPE_I4:
  333. // Fall through to the next case
  334. case self::XMLRPC_TYPE_INTEGER:
  335. require_once 'Zend/XmlRpc/Value/Integer.php';
  336. $xmlrpcValue = new Zend_XmlRpc_Value_Integer($value);
  337. break;
  338. case self::XMLRPC_TYPE_APACHEI8:
  339. // Fall through to the next case
  340. case self::XMLRPC_TYPE_I8:
  341. require_once 'Zend/XmlRpc/Value/BigInteger.php';
  342. $xmlrpcValue = new Zend_XmlRpc_Value_BigInteger($value);
  343. break;
  344. case self::XMLRPC_TYPE_DOUBLE:
  345. require_once 'Zend/XmlRpc/Value/Double.php';
  346. $xmlrpcValue = new Zend_XmlRpc_Value_Double($value);
  347. break;
  348. case self::XMLRPC_TYPE_BOOLEAN:
  349. require_once 'Zend/XmlRpc/Value/Boolean.php';
  350. $xmlrpcValue = new Zend_XmlRpc_Value_Boolean($value);
  351. break;
  352. case self::XMLRPC_TYPE_STRING:
  353. require_once 'Zend/XmlRpc/Value/String.php';
  354. $xmlrpcValue = new Zend_XmlRpc_Value_String($value);
  355. break;
  356. case self::XMLRPC_TYPE_DATETIME: // The value should already be in a iso8601 format
  357. require_once 'Zend/XmlRpc/Value/DateTime.php';
  358. $xmlrpcValue = new Zend_XmlRpc_Value_DateTime($value);
  359. break;
  360. case self::XMLRPC_TYPE_BASE64: // The value should already be base64 encoded
  361. require_once 'Zend/XmlRpc/Value/Base64.php';
  362. $xmlrpcValue = new Zend_XmlRpc_Value_Base64($value, true);
  363. break;
  364. case self::XMLRPC_TYPE_NIL:
  365. // Fall through to the next case
  366. case self::XMLRPC_TYPE_APACHENIL:
  367. // The value should always be NULL
  368. require_once 'Zend/XmlRpc/Value/Nil.php';
  369. $xmlrpcValue = new Zend_XmlRpc_Value_Nil();
  370. break;
  371. case self::XMLRPC_TYPE_ARRAY:
  372. // PHP 5.2.4 introduced a regression in how empty($xml->value)
  373. // returns; need to look for the item specifically
  374. $data = null;
  375. foreach ($value->children() as $key => $value) {
  376. if ('data' == $key) {
  377. $data = $value;
  378. break;
  379. }
  380. }
  381. if (null === $data) {
  382. require_once 'Zend/XmlRpc/Value/Exception.php';
  383. throw new Zend_XmlRpc_Value_Exception('Invalid XML for XML-RPC native '. self::XMLRPC_TYPE_ARRAY .' type: ARRAY tag must contain DATA tag');
  384. }
  385. $values = array();
  386. // Parse all the elements of the array from the XML string
  387. // (simple xml element) to Zend_XmlRpc_Value objects
  388. foreach ($data->value as $element) {
  389. $values[] = self::_xmlStringToNativeXmlRpc($element);
  390. }
  391. require_once 'Zend/XmlRpc/Value/Array.php';
  392. $xmlrpcValue = new Zend_XmlRpc_Value_Array($values);
  393. break;
  394. case self::XMLRPC_TYPE_STRUCT:
  395. $values = array();
  396. // Parse all the memebers of the struct from the XML string
  397. // (simple xml element) to Zend_XmlRpc_Value objects
  398. foreach ($value->member as $member) {
  399. // @todo? If a member doesn't have a <value> tag, we don't add it to the struct
  400. // Maybe we want to throw an exception here ?
  401. if (!isset($member->value) or !isset($member->name)) {
  402. continue;
  403. //throw new Zend_XmlRpc_Value_Exception('Member of the '. self::XMLRPC_TYPE_STRUCT .' XML-RPC native type must contain a VALUE tag');
  404. }
  405. $values[(string)$member->name] = self::_xmlStringToNativeXmlRpc($member->value);
  406. }
  407. require_once 'Zend/XmlRpc/Value/Struct.php';
  408. $xmlrpcValue = new Zend_XmlRpc_Value_Struct($values);
  409. break;
  410. default:
  411. require_once 'Zend/XmlRpc/Value/Exception.php';
  412. throw new Zend_XmlRpc_Value_Exception('Value type \''. $type .'\' parsed from the XML string is not a known XML-RPC native type');
  413. break;
  414. }
  415. $xmlrpcValue->_setXML($xml->asXML());
  416. return $xmlrpcValue;
  417. }
  418. protected static function _createSimpleXMLElement(&$xml)
  419. {
  420. if ($xml instanceof SimpleXMLElement) {
  421. return;
  422. }
  423. try {
  424. $xml = new SimpleXMLElement($xml);
  425. } catch (Exception $e) {
  426. // The given string is not a valid XML
  427. require_once 'Zend/XmlRpc/Value/Exception.php';
  428. throw new Zend_XmlRpc_Value_Exception('Failed to create XML-RPC value from XML string: ' . $e->getMessage(), $e->getCode(), $e);
  429. }
  430. }
  431. /**
  432. * Extract XML/RPC type and value from SimpleXMLElement object
  433. *
  434. * @param SimpleXMLElement $xml
  435. * @param string &$type Type bind variable
  436. * @param string &$value Value bind variable
  437. * @return void
  438. */
  439. protected static function _extractTypeAndValue(SimpleXMLElement $xml, &$type, &$value)
  440. {
  441. list($type, $value) = each($xml);
  442. if (!$type and $value === null) {
  443. $namespaces = array('ex' => 'http://ws.apache.org/xmlrpc/namespaces/extensions');
  444. foreach ($namespaces as $namespaceName => $namespaceUri) {
  445. $namespaceXml = $xml->children($namespaceUri);
  446. list($type, $value) = each($namespaceXml);
  447. if ($type !== null) {
  448. $type = $namespaceName . ':' . $type;
  449. break;
  450. }
  451. }
  452. }
  453. // If no type was specified, the default is string
  454. if (!$type) {
  455. $type = self::XMLRPC_TYPE_STRING;
  456. }
  457. }
  458. /**
  459. * @param string $xml
  460. * @return void
  461. */
  462. protected function _setXML($xml)
  463. {
  464. $this->_xml = $this->getGenerator()->stripDeclaration($xml);
  465. }
  466. }