PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Amf/Parse/Amf0/Deserializer.php

https://bitbucket.org/openfisma-ondemand/openfisma
PHP | 306 lines | 123 code | 33 blank | 150 comment | 10 complexity | 8cf9e97a5f2d845b08c5ef7215518328 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, GPL-3.0, Apache-2.0, EPL-1.0
  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_Amf
  17. * @subpackage Parse_Amf0
  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$
  21. */
  22. /** Zend_Amf_Constants */
  23. // require_once 'Zend/Amf/Constants.php';
  24. /** @see Zend_Amf_Parse_Deserializer */
  25. // require_once 'Zend/Amf/Parse/Deserializer.php';
  26. /**
  27. * Read an AMF0 input stream and convert it into PHP data types
  28. *
  29. * @todo Implement Typed Object Class Mapping
  30. * @todo Class could be implemented as Factory Class with each data type it's own class
  31. * @package Zend_Amf
  32. * @subpackage Parse_Amf0
  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. class Zend_Amf_Parse_Amf0_Deserializer extends Zend_Amf_Parse_Deserializer
  37. {
  38. /**
  39. * An array of objects used for recursively deserializing an object.
  40. * @var array
  41. */
  42. protected $_reference = array();
  43. /**
  44. * If AMF3 serialization occurs, update to AMF0 0x03
  45. *
  46. * @var int
  47. */
  48. protected $_objectEncoding = Zend_Amf_Constants::AMF0_OBJECT_ENCODING;
  49. /**
  50. * Read AMF markers and dispatch for deserialization
  51. *
  52. * Checks for AMF marker types and calls the appropriate methods
  53. * for deserializing those marker types. Markers are the data type of
  54. * the following value.
  55. *
  56. * @param integer $typeMarker
  57. * @return mixed whatever the data type is of the marker in php
  58. * @throws Zend_Amf_Exception for invalid type
  59. */
  60. public function readTypeMarker($typeMarker = null)
  61. {
  62. if ($typeMarker === null) {
  63. $typeMarker = $this->_stream->readByte();
  64. }
  65. switch($typeMarker) {
  66. // number
  67. case Zend_Amf_Constants::AMF0_NUMBER:
  68. return $this->_stream->readDouble();
  69. // boolean
  70. case Zend_Amf_Constants::AMF0_BOOLEAN:
  71. return (boolean) $this->_stream->readByte();
  72. // string
  73. case Zend_Amf_Constants::AMF0_STRING:
  74. return $this->_stream->readUTF();
  75. // object
  76. case Zend_Amf_Constants::AMF0_OBJECT:
  77. return $this->readObject();
  78. // null
  79. case Zend_Amf_Constants::AMF0_NULL:
  80. return null;
  81. // undefined
  82. case Zend_Amf_Constants::AMF0_UNDEFINED:
  83. return null;
  84. // Circular references are returned here
  85. case Zend_Amf_Constants::AMF0_REFERENCE:
  86. return $this->readReference();
  87. // mixed array with numeric and string keys
  88. case Zend_Amf_Constants::AMF0_MIXEDARRAY:
  89. return $this->readMixedArray();
  90. // array
  91. case Zend_Amf_Constants::AMF0_ARRAY:
  92. return $this->readArray();
  93. // date
  94. case Zend_Amf_Constants::AMF0_DATE:
  95. return $this->readDate();
  96. // longString strlen(string) > 2^16
  97. case Zend_Amf_Constants::AMF0_LONGSTRING:
  98. return $this->_stream->readLongUTF();
  99. //internal AS object, not supported
  100. case Zend_Amf_Constants::AMF0_UNSUPPORTED:
  101. return null;
  102. // XML
  103. case Zend_Amf_Constants::AMF0_XML:
  104. return $this->readXmlString();
  105. // typed object ie Custom Class
  106. case Zend_Amf_Constants::AMF0_TYPEDOBJECT:
  107. return $this->readTypedObject();
  108. //AMF3-specific
  109. case Zend_Amf_Constants::AMF0_AMF3:
  110. return $this->readAmf3TypeMarker();
  111. default:
  112. // require_once 'Zend/Amf/Exception.php';
  113. throw new Zend_Amf_Exception('Unsupported marker type: ' . $typeMarker);
  114. }
  115. }
  116. /**
  117. * Read AMF objects and convert to PHP objects
  118. *
  119. * Read the name value pair objects form the php message and convert them to
  120. * a php object class.
  121. *
  122. * Called when the marker type is 3.
  123. *
  124. * @param array|null $object
  125. * @return object
  126. */
  127. public function readObject($object = null)
  128. {
  129. if ($object === null) {
  130. $object = array();
  131. }
  132. while (true) {
  133. $key = $this->_stream->readUTF();
  134. $typeMarker = $this->_stream->readByte();
  135. if ($typeMarker != Zend_Amf_Constants::AMF0_OBJECTTERM ){
  136. //Recursivly call readTypeMarker to get the types of properties in the object
  137. $object[$key] = $this->readTypeMarker($typeMarker);
  138. } else {
  139. //encountered AMF object terminator
  140. break;
  141. }
  142. }
  143. $this->_reference[] = $object;
  144. return (object) $object;
  145. }
  146. /**
  147. * Read reference objects
  148. *
  149. * Used to gain access to the private array of reference objects.
  150. * Called when marker type is 7.
  151. *
  152. * @return object
  153. * @throws Zend_Amf_Exception for invalid reference keys
  154. */
  155. public function readReference()
  156. {
  157. $key = $this->_stream->readInt();
  158. if (!array_key_exists($key, $this->_reference)) {
  159. // require_once 'Zend/Amf/Exception.php';
  160. throw new Zend_Amf_Exception('Invalid reference key: '. $key);
  161. }
  162. return $this->_reference[$key];
  163. }
  164. /**
  165. * Reads an array with numeric and string indexes.
  166. *
  167. * Called when marker type is 8
  168. *
  169. * @todo As of Flash Player 9 there is not support for mixed typed arrays
  170. * so we handle this as an object. With the introduction of vectors
  171. * in Flash Player 10 this may need to be reconsidered.
  172. * @return array
  173. */
  174. public function readMixedArray()
  175. {
  176. $length = $this->_stream->readLong();
  177. return $this->readObject();
  178. }
  179. /**
  180. * Converts numerically indexed actiosncript arrays into php arrays.
  181. *
  182. * Called when marker type is 10
  183. *
  184. * @return array
  185. */
  186. public function readArray()
  187. {
  188. $length = $this->_stream->readLong();
  189. $array = array();
  190. while ($length--) {
  191. $array[] = $this->readTypeMarker();
  192. }
  193. return $array;
  194. }
  195. /**
  196. * Convert AS Date to Zend_Date
  197. *
  198. * @return Zend_Date
  199. */
  200. public function readDate()
  201. {
  202. // get the unix time stamp. Not sure why ActionScript does not use
  203. // milliseconds
  204. $timestamp = floor($this->_stream->readDouble() / 1000);
  205. // The timezone offset is never returned to the server; it is always 0,
  206. // so read and ignore.
  207. $offset = $this->_stream->readInt();
  208. // require_once 'Zend/Date.php';
  209. $date = new Zend_Date($timestamp);
  210. return $date;
  211. }
  212. /**
  213. * Convert XML to SimpleXml
  214. * If user wants DomDocument they can use dom_import_simplexml
  215. *
  216. * @return SimpleXml Object
  217. */
  218. public function readXmlString()
  219. {
  220. $string = $this->_stream->readLongUTF();
  221. return simplexml_load_string($string);
  222. }
  223. /**
  224. * Read Class that is to be mapped to a server class.
  225. *
  226. * Commonly used for Value Objects on the server
  227. *
  228. * @todo implement Typed Class mapping
  229. * @return object|array
  230. * @throws Zend_Amf_Exception if unable to load type
  231. */
  232. public function readTypedObject()
  233. {
  234. // require_once 'Zend/Amf/Parse/TypeLoader.php';
  235. // get the remote class name
  236. $className = $this->_stream->readUTF();
  237. $loader = Zend_Amf_Parse_TypeLoader::loadType($className);
  238. $returnObject = new $loader();
  239. $properties = get_object_vars($this->readObject());
  240. foreach($properties as $key=>$value) {
  241. if($key) {
  242. $returnObject->$key = $value;
  243. }
  244. }
  245. if($returnObject instanceof Zend_Amf_Value_Messaging_ArrayCollection) {
  246. $returnObject = get_object_vars($returnObject);
  247. }
  248. return $returnObject;
  249. }
  250. /**
  251. * AMF3 data type encountered load AMF3 Deserializer to handle
  252. * type markers.
  253. *
  254. * @return string
  255. */
  256. public function readAmf3TypeMarker()
  257. {
  258. // require_once 'Zend/Amf/Parse/Amf3/Deserializer.php';
  259. $deserializer = new Zend_Amf_Parse_Amf3_Deserializer($this->_stream);
  260. $this->_objectEncoding = Zend_Amf_Constants::AMF3_OBJECT_ENCODING;
  261. return $deserializer->readTypeMarker();
  262. }
  263. /**
  264. * Return the object encoding to check if an AMF3 object
  265. * is going to be return.
  266. *
  267. * @return int
  268. */
  269. public function getObjectEncoding()
  270. {
  271. return $this->_objectEncoding;
  272. }
  273. }