Value.php 15 KB

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