Value.php 17 KB

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