PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/DevApp/library/ServerLibraries/ZendFramework/1.7/library/Zend/Amf/Parse/Amf0/Deserializer.php

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