Value.php 16 KB

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