Value.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  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-2010 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. }
  147. return $this->_xml;
  148. }
  149. /**
  150. * Generate XML code that represent a native XML/RPC value
  151. *
  152. * @return void
  153. */
  154. public function generateXml()
  155. {
  156. if (!$this->_xml) {
  157. $this->_generateXml();
  158. }
  159. }
  160. /**
  161. * Creates a Zend_XmlRpc_Value* object, representing a native XML-RPC value
  162. * A XmlRpcValue object can be created in 3 ways:
  163. * 1. Autodetecting the native type out of a PHP variable
  164. * (if $type is not set or equal to Zend_XmlRpc_Value::AUTO_DETECT_TYPE)
  165. * 2. By specifing the native type ($type is one of the Zend_XmlRpc_Value::XMLRPC_TYPE_* constants)
  166. * 3. From a XML string ($type is set to Zend_XmlRpc_Value::XML_STRING)
  167. *
  168. * By default the value type is autodetected according to it's PHP type
  169. *
  170. * @param mixed $value
  171. * @param Zend_XmlRpc_Value::constant $type
  172. *
  173. * @return Zend_XmlRpc_Value
  174. * @static
  175. */
  176. public static function getXmlRpcValue($value, $type = self::AUTO_DETECT_TYPE)
  177. {
  178. switch ($type) {
  179. case self::AUTO_DETECT_TYPE:
  180. // Auto detect the XML-RPC native type from the PHP type of $value
  181. return self::_phpVarToNativeXmlRpc($value);
  182. case self::XML_STRING:
  183. // Parse the XML string given in $value and get the XML-RPC value in it
  184. return self::_xmlStringToNativeXmlRpc($value);
  185. case self::XMLRPC_TYPE_I4:
  186. // fall through to the next case
  187. case self::XMLRPC_TYPE_INTEGER:
  188. require_once 'Zend/XmlRpc/Value/Integer.php';
  189. return new Zend_XmlRpc_Value_Integer($value);
  190. case self::XMLRPC_TYPE_I8:
  191. // fall through to the next case
  192. case self::XMLRPC_TYPE_APACHEI8:
  193. require_once 'Zend/XmlRpc/Value/BigInteger.php';
  194. return new Zend_XmlRpc_Value_BigInteger($value);
  195. case self::XMLRPC_TYPE_DOUBLE:
  196. require_once 'Zend/XmlRpc/Value/Double.php';
  197. return new Zend_XmlRpc_Value_Double($value);
  198. case self::XMLRPC_TYPE_BOOLEAN:
  199. require_once 'Zend/XmlRpc/Value/Boolean.php';
  200. return new Zend_XmlRpc_Value_Boolean($value);
  201. case self::XMLRPC_TYPE_STRING:
  202. require_once 'Zend/XmlRpc/Value/String.php';
  203. return new Zend_XmlRpc_Value_String($value);
  204. case self::XMLRPC_TYPE_BASE64:
  205. require_once 'Zend/XmlRpc/Value/Base64.php';
  206. return new Zend_XmlRpc_Value_Base64($value);
  207. case self::XMLRPC_TYPE_NIL:
  208. // fall through to the next case
  209. case self::XMLRPC_TYPE_APACHENIL:
  210. require_once 'Zend/XmlRpc/Value/Nil.php';
  211. return new Zend_XmlRpc_Value_Nil();
  212. case self::XMLRPC_TYPE_DATETIME:
  213. require_once 'Zend/XmlRpc/Value/DateTime.php';
  214. return new Zend_XmlRpc_Value_DateTime($value);
  215. case self::XMLRPC_TYPE_ARRAY:
  216. require_once 'Zend/XmlRpc/Value/Array.php';
  217. return new Zend_XmlRpc_Value_Array($value);
  218. case self::XMLRPC_TYPE_STRUCT:
  219. require_once 'Zend/XmlRpc/Value/Struct.php';
  220. return new Zend_XmlRpc_Value_Struct($value);
  221. default:
  222. require_once 'Zend/XmlRpc/Value/Exception.php';
  223. throw new Zend_XmlRpc_Value_Exception('Given type is not a '. __CLASS__ .' constant');
  224. }
  225. }
  226. /**
  227. * Transform a PHP native variable into a XML-RPC native value
  228. *
  229. * @param mixed $value The PHP variable for convertion
  230. *
  231. * @return Zend_XmlRpc_Value
  232. * @static
  233. */
  234. protected static function _phpVarToNativeXmlRpc($value)
  235. {
  236. switch (gettype($value)) {
  237. case 'object':
  238. // Check to see if it's an XmlRpc value
  239. if ($value instanceof Zend_XmlRpc_Value) {
  240. return $value;
  241. }
  242. if ($value instanceof Zend_Crypt_Math_BigInteger) {
  243. require_once 'Zend/XmlRpc/Value/BigInteger.php';
  244. return new Zend_XmlRpc_Value_BigInteger($value);
  245. }
  246. if ($value instanceof Zend_Date or $value instanceof DateTime) {
  247. require_once 'Zend/XmlRpc/Value/DateTime.php';
  248. return new Zend_XmlRpc_Value_DateTime($value);
  249. }
  250. // Otherwise, we convert the object into a struct
  251. $value = get_object_vars($value);
  252. // Break intentionally omitted
  253. case 'array':
  254. // Default native type for a PHP array (a simple numeric array) is 'array'
  255. require_once 'Zend/XmlRpc/Value/Array.php';
  256. $obj = 'Zend_XmlRpc_Value_Array';
  257. // Determine if this is an associative array
  258. if (!empty($value) && is_array($value) && (array_keys($value) !== range(0, count($value) - 1))) {
  259. require_once 'Zend/XmlRpc/Value/Struct.php';
  260. $obj = 'Zend_XmlRpc_Value_Struct';
  261. }
  262. return new $obj($value);
  263. case 'integer':
  264. require_once 'Zend/XmlRpc/Value/Integer.php';
  265. return new Zend_XmlRpc_Value_Integer($value);
  266. case 'double':
  267. require_once 'Zend/XmlRpc/Value/Double.php';
  268. return new Zend_XmlRpc_Value_Double($value);
  269. case 'boolean':
  270. require_once 'Zend/XmlRpc/Value/Boolean.php';
  271. return new Zend_XmlRpc_Value_Boolean($value);
  272. case 'NULL':
  273. case 'null':
  274. require_once 'Zend/XmlRpc/Value/Nil.php';
  275. return new Zend_XmlRpc_Value_Nil();
  276. case 'string':
  277. // Fall through to the next case
  278. default:
  279. // If type isn't identified (or identified as string), it treated as string
  280. require_once 'Zend/XmlRpc/Value/String.php';
  281. return new Zend_XmlRpc_Value_String($value);
  282. }
  283. }
  284. /**
  285. * Transform an XML string into a XML-RPC native value
  286. *
  287. * @param string|SimpleXMLElement $xml A SimpleXMLElement object represent the XML string
  288. * It can be also a valid XML string for convertion
  289. *
  290. * @return Zend_XmlRpc_Value
  291. * @static
  292. */
  293. protected static function _xmlStringToNativeXmlRpc($xml)
  294. {
  295. self::_createSimpleXMLElement($xml);
  296. self::_extractTypeAndValue($xml, $type, $value);
  297. switch ($type) {
  298. // All valid and known XML-RPC native values
  299. case self::XMLRPC_TYPE_I4:
  300. // Fall through to the next case
  301. case self::XMLRPC_TYPE_INTEGER:
  302. require_once 'Zend/XmlRpc/Value/Integer.php';
  303. $xmlrpcValue = new Zend_XmlRpc_Value_Integer($value);
  304. break;
  305. case self::XMLRPC_TYPE_APACHEI8:
  306. // Fall through to the next case
  307. case self::XMLRPC_TYPE_I8:
  308. require_once 'Zend/XmlRpc/Value/BigInteger.php';
  309. $xmlrpcValue = new Zend_XmlRpc_Value_BigInteger($value);
  310. break;
  311. case self::XMLRPC_TYPE_DOUBLE:
  312. require_once 'Zend/XmlRpc/Value/Double.php';
  313. $xmlrpcValue = new Zend_XmlRpc_Value_Double($value);
  314. break;
  315. case self::XMLRPC_TYPE_BOOLEAN:
  316. require_once 'Zend/XmlRpc/Value/Boolean.php';
  317. $xmlrpcValue = new Zend_XmlRpc_Value_Boolean($value);
  318. break;
  319. case self::XMLRPC_TYPE_STRING:
  320. require_once 'Zend/XmlRpc/Value/String.php';
  321. $xmlrpcValue = new Zend_XmlRpc_Value_String($value);
  322. break;
  323. case self::XMLRPC_TYPE_DATETIME: // The value should already be in a iso8601 format
  324. require_once 'Zend/XmlRpc/Value/DateTime.php';
  325. $xmlrpcValue = new Zend_XmlRpc_Value_DateTime($value);
  326. break;
  327. case self::XMLRPC_TYPE_BASE64: // The value should already be base64 encoded
  328. require_once 'Zend/XmlRpc/Value/Base64.php';
  329. $xmlrpcValue = new Zend_XmlRpc_Value_Base64($value, true);
  330. break;
  331. case self::XMLRPC_TYPE_NIL:
  332. // Fall through to the next case
  333. case self::XMLRPC_TYPE_APACHENIL:
  334. // The value should always be NULL
  335. require_once 'Zend/XmlRpc/Value/Nil.php';
  336. $xmlrpcValue = new Zend_XmlRpc_Value_Nil();
  337. break;
  338. case self::XMLRPC_TYPE_ARRAY:
  339. // PHP 5.2.4 introduced a regression in how empty($xml->value)
  340. // returns; need to look for the item specifically
  341. $data = null;
  342. foreach ($value->children() as $key => $value) {
  343. if ('data' == $key) {
  344. $data = $value;
  345. break;
  346. }
  347. }
  348. if (null === $data) {
  349. require_once 'Zend/XmlRpc/Value/Exception.php';
  350. throw new Zend_XmlRpc_Value_Exception('Invalid XML for XML-RPC native '. self::XMLRPC_TYPE_ARRAY .' type: ARRAY tag must contain DATA tag');
  351. }
  352. $values = array();
  353. // Parse all the elements of the array from the XML string
  354. // (simple xml element) to Zend_XmlRpc_Value objects
  355. foreach ($data->value as $element) {
  356. $values[] = self::_xmlStringToNativeXmlRpc($element);
  357. }
  358. require_once 'Zend/XmlRpc/Value/Array.php';
  359. $xmlrpcValue = new Zend_XmlRpc_Value_Array($values);
  360. break;
  361. case self::XMLRPC_TYPE_STRUCT:
  362. $values = array();
  363. // Parse all the memebers of the struct from the XML string
  364. // (simple xml element) to Zend_XmlRpc_Value objects
  365. foreach ($value->member as $member) {
  366. // @todo? If a member doesn't have a <value> tag, we don't add it to the struct
  367. // Maybe we want to throw an exception here ?
  368. if (!isset($member->value) or !isset($member->name)) {
  369. continue;
  370. //throw new Zend_XmlRpc_Value_Exception('Member of the '. self::XMLRPC_TYPE_STRUCT .' XML-RPC native type must contain a VALUE tag');
  371. }
  372. $values[(string)$member->name] = self::_xmlStringToNativeXmlRpc($member->value);
  373. }
  374. require_once 'Zend/XmlRpc/Value/Struct.php';
  375. $xmlrpcValue = new Zend_XmlRpc_Value_Struct($values);
  376. break;
  377. default:
  378. require_once 'Zend/XmlRpc/Value/Exception.php';
  379. throw new Zend_XmlRpc_Value_Exception('Value type \''. $type .'\' parsed from the XML string is not a known XML-RPC native type');
  380. break;
  381. }
  382. $xmlrpcValue->_setXML($xml->asXML());
  383. return $xmlrpcValue;
  384. }
  385. protected static function _createSimpleXMLElement(&$xml)
  386. {
  387. if ($xml instanceof SimpleXMLElement) {
  388. return;
  389. }
  390. try {
  391. $xml = new SimpleXMLElement($xml);
  392. } catch (Exception $e) {
  393. // The given string is not a valid XML
  394. require_once 'Zend/XmlRpc/Value/Exception.php';
  395. throw new Zend_XmlRpc_Value_Exception('Failed to create XML-RPC value from XML string: ' . $e->getMessage(), $e->getCode(), $e);
  396. }
  397. }
  398. /**
  399. * Extract XML/RPC type and value from SimpleXMLElement object
  400. *
  401. * @param SimpleXMLElement $xml
  402. * @param string &$type Type bind variable
  403. * @param string &$value Value bind variable
  404. * @return void
  405. */
  406. protected static function _extractTypeAndValue(SimpleXMLElement $xml, &$type, &$value)
  407. {
  408. list($type, $value) = each($xml);
  409. if (!$type and $value === null) {
  410. $namespaces = array('ex' => 'http://ws.apache.org/xmlrpc/namespaces/extensions');
  411. foreach ($namespaces as $namespaceName => $namespaceUri) {
  412. $namespaceXml = $xml->children($namespaceUri);
  413. list($type, $value) = each($namespaceXml);
  414. if ($type !== null) {
  415. $type = $namespaceName . ':' . $type;
  416. break;
  417. }
  418. }
  419. }
  420. // If no type was specified, the default is string
  421. if (!$type) {
  422. $type = self::XMLRPC_TYPE_STRING;
  423. }
  424. }
  425. /**
  426. * @param $xml
  427. * @return void
  428. */
  429. protected function _setXML($xml)
  430. {
  431. $this->_xml = $this->getGenerator()->stripDeclaration($xml);
  432. }
  433. }