AMFBaseDeserializer.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. /**
  3. * AMFDeserializer takes the raw amf input stream and converts it PHP objects
  4. * representing the data.
  5. *
  6. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  7. * @copyright (c) 2003 amfphp.org
  8. * @package flashservices
  9. * @subpackage io
  10. * @version $Id$
  11. */
  12. /**
  13. * Required classes
  14. */
  15. require_once(AMFPHP_BASE . "shared/util/MessageBody.php");
  16. require_once(AMFPHP_BASE . "shared/util/MessageHeader.php");
  17. require_once(AMFPHP_BASE . "amf/util/DateWrapper.php");
  18. class AMFBaseDeserializer {
  19. /**
  20. * The raw data input
  21. *
  22. * @access private
  23. * @var string
  24. */
  25. var $raw_data;
  26. /**
  27. * The current seek cursor of the stream
  28. *
  29. * @access private
  30. * @var int
  31. */
  32. var $current_byte;
  33. /**
  34. * The length of the stream. Since this class is not actually using a stream
  35. * the entire content of the stream is passed in as the initial argument so the
  36. * length can be determined.
  37. *
  38. * @access private
  39. * @var int
  40. */
  41. var $content_length;
  42. /**
  43. * The number of headers in the packet.
  44. *
  45. * @access private
  46. * @var int
  47. */
  48. var $header_count;
  49. /**
  50. * The content of the packet headers
  51. *
  52. * @access private
  53. * @var string
  54. */
  55. var $headers;
  56. /**
  57. * The number of bodys in the packet.
  58. *
  59. * @access private
  60. * @var int
  61. */
  62. var $body_count;
  63. /**
  64. * The content of the body elements
  65. *
  66. * @access private
  67. * @var string
  68. */
  69. var $body;
  70. /**
  71. * The object to store the amf data.
  72. *
  73. * @access private
  74. * @var object
  75. */
  76. var $amfdata;
  77. /**
  78. * The instance of the amfinput stream object
  79. *
  80. * @access private
  81. * @var object
  82. */
  83. var $inputStream;
  84. /**
  85. * metaInfo
  86. */
  87. var $meta;
  88. var $storedStrings;
  89. var $storedObjects;
  90. var $storedDefinitions;
  91. var $amf0storedObjects;
  92. var $native;
  93. /**
  94. * Constructor method for the deserializer. Constructing the deserializer converts the input stream
  95. * content to a AMFObject.
  96. *
  97. * @param object $is The referenced input stream
  98. */
  99. function AMFBaseDeserializer($rd) {
  100. $this->isBigEndian = AMFPHP_BIG_ENDIAN;
  101. $this->current_byte = 0;
  102. $this->raw_data = $rd; // store the stream in this object
  103. $this->content_length = strlen($this->raw_data); // grab the total length of this stream
  104. $this->charsetHandler = new CharsetHandler('flashtophp');
  105. $this->storedStrings = array();
  106. $this->storedObjects = array();
  107. $this->storedDefinitions = array();
  108. $this->native = $GLOBALS['amfphp']['native'] && function_exists('amf_decode');
  109. $this->decodeFlags = (AMFPHP_BIG_ENDIAN*2) | 4;
  110. }
  111. /**
  112. * deserialize invokes this class to transform the raw data into valid object
  113. *
  114. * @param object $amfdata The object to put the deserialized data in
  115. */
  116. function deserialize (&$amfdata) {
  117. $time = microtime_float();
  118. $this->amfdata = &$amfdata;
  119. $this->readHeader(); // read the binary header
  120. $this->readBody(); // read the binary body
  121. if($this->decodeFlags & 1 == 1)
  122. {
  123. //AMF3 mode
  124. $GLOBALS['amfphp']['encoding'] = "amf3";
  125. }
  126. global $amfphp;
  127. $amfphp['decodeTime'] = microtime_float() - $time;
  128. }
  129. /**
  130. * returns the built AMFObject from the deserialization operation
  131. *
  132. * @return object The deserialized AMFObject
  133. */
  134. function getAMFObject() {
  135. return $this->amfdata;
  136. }
  137. /**
  138. * Decode callback is triggered when an object is encountered on decode
  139. */
  140. function decodeCallback($event, $arg)
  141. {
  142. if($event == 1) //Object
  143. {
  144. $type =$arg;
  145. return $this->mapClass($type);
  146. }
  147. else if($event == 2) //Object post decode
  148. {
  149. $obj = $arg;
  150. if(method_exists($obj, 'init'))
  151. {
  152. $obj->init();
  153. }
  154. return $obj;
  155. }
  156. else if($event == 3) //XML post-decode
  157. {
  158. return $arg;
  159. }
  160. else if($event == 4) //Serializable post-decode
  161. {
  162. if($type == 'flex.messaging.io.ArrayCollection' || $type == 'flex.messaging.io.ObjectProxy')
  163. {
  164. return;
  165. }
  166. else
  167. {
  168. trigger_error("Unable to read externalizable data type " . $type, E_USER_ERROR);
  169. return "error";
  170. }
  171. }
  172. else if($event == 5) //ByteArray post decode
  173. {
  174. return new ByteArray($arg);
  175. }
  176. }
  177. /**
  178. * readHeader converts that header section of the amf message into php obects.
  179. * Header information typically contains meta data about the message.
  180. */
  181. function readHeader() {
  182. $topByte = $this->readByte(); // ignore the first two bytes -- version or something
  183. $secondByte = $this->readByte(); //0 for Flash,
  184. //1 for FlashComm
  185. //Disable debug events for FlashComm
  186. $GLOBALS['amfphp']['isFlashComm'] = $secondByte == 1;
  187. //If firstByte != 0, then the AMF data is corrupted, for example the transmission
  188. //
  189. if(!($topByte == 0 || $topByte == 3))
  190. {
  191. trigger_error("Malformed AMF message, connection may have dropped");
  192. exit();
  193. }
  194. $this->header_count = $this->readInt(); // find the total number of header elements
  195. while ($this->header_count--) { // loop over all of the header elements
  196. $name = $this->readUTF();
  197. $required = $this->readByte() == 1; // find the must understand flag
  198. //$length = $this->readLong(); // grab the length of the header element
  199. $this->current_byte += 4; // grab the length of the header element
  200. if($this->native)
  201. {
  202. $content = amf_decode($this->raw_data, $this->decodeFlags, $this->current_byte, array(& $this, "decodeCallback"));
  203. }
  204. else
  205. {
  206. $type = $this->readByte(); // grab the type of the element
  207. $content = $this->readData($type); // turn the element into real data
  208. }
  209. $this->amfdata->addHeader(new MessageHeader($name, $required, $content)); // save the name/value into the headers array
  210. }
  211. }
  212. /**
  213. * readBody converts the payload of the message into php objects.
  214. */
  215. function readBody() {
  216. $this->body_count = $this->readInt(); // find the total number of body elements
  217. while ($this->body_count--) { // loop over all of the body elements
  218. $this->amf0storedObjects = array();
  219. $this->storedStrings = array();
  220. $this->storedObjects = array();
  221. $this->storedDefinitions = array();
  222. $target = $this->readUTF();
  223. $response = $this->readUTF(); // the response that the client understands
  224. //$length = $this->readLong(); // grab the length of the body element
  225. $this->current_byte += 4;
  226. if($this->native)
  227. $data = amf_decode($this->raw_data, $this->decodeFlags, $this->current_byte, array(& $this, "decodeCallback"));
  228. else
  229. {
  230. $type = $this->readByte(); // grab the type of the element
  231. $data = $this->readData($type); // turn the element into real data
  232. }
  233. $this->amfdata->addBody(new MessageBody($target, $response, $data)); // add the body element to the body object
  234. }
  235. }
  236. /********************************************************************************
  237. * This used to be in AmfInputStream
  238. ********************************************************************************
  239. /**
  240. * readByte grabs the next byte from the data stream and returns it.
  241. *
  242. * @return int The next byte converted into an integer
  243. */
  244. function readByte() {
  245. return ord($this->raw_data[$this->current_byte++]); // return the next byte
  246. }
  247. /**
  248. * readInt grabs the next 2 bytes and returns the next two bytes, shifted and combined
  249. * to produce the resulting integer
  250. *
  251. * @return int The resulting integer from the next 2 bytes
  252. */
  253. function readInt() {
  254. return ((ord($this->raw_data[$this->current_byte++]) << 8) |
  255. ord($this->raw_data[$this->current_byte++])); // read the next 2 bytes, shift and add
  256. }
  257. /**
  258. * readUTF first grabs the next 2 bytes which represent the string length.
  259. * Then it grabs the next (len) bytes of the resulting string.
  260. *
  261. * @return string The utf8 decoded string
  262. */
  263. function readUTF() {
  264. $length = $this->readInt(); // get the length of the string (1st 2 bytes)
  265. //BUg fix:: if string is empty skip ahead
  266. if($length == 0)
  267. {
  268. return "";
  269. }
  270. else
  271. {
  272. $val = substr($this->raw_data, $this->current_byte, $length); // grab the string
  273. $this->current_byte += $length; // move the seek head to the end of the string
  274. return $this->charsetHandler->transliterate($val); // return the string
  275. }
  276. }
  277. /**
  278. * readLong grabs the next 4 bytes shifts and combines them to produce an integer
  279. *
  280. * @return int The resulting integer from the next 4 bytes
  281. */
  282. function readLong() {
  283. return ((ord($this->raw_data[$this->current_byte++]) << 24) |
  284. (ord($this->raw_data[$this->current_byte++]) << 16) |
  285. (ord($this->raw_data[$this->current_byte++]) << 8) |
  286. ord($this->raw_data[$this->current_byte++])); // read the next 4 bytes, shift and add
  287. }
  288. /**
  289. * readDouble reads the floating point value from the bytes stream and properly orders
  290. * the bytes depending on the system architecture.
  291. *
  292. * @return float The floating point value of the next 8 bytes
  293. */
  294. function readDouble() {
  295. $bytes = substr($this->raw_data, $this->current_byte, 8);
  296. $this->current_byte += 8;
  297. if ($this->isBigEndian) {
  298. $bytes = strrev($bytes);
  299. }
  300. $zz = unpack("dflt", $bytes); // unpack the bytes
  301. return $zz['flt']; // return the number from the associative array
  302. }
  303. /**
  304. * readLongUTF first grabs the next 4 bytes which represent the string length.
  305. * Then it grabs the next (len) bytes of the resulting in the string
  306. *
  307. * @return string The utf8 decoded string
  308. */
  309. function readLongUTF() {
  310. $length = $this->readLong(); // get the length of the string (1st 4 bytes)
  311. $val = substr($this->raw_data, $this->current_byte, $length); // grab the string
  312. $this->current_byte += $length; // move the seek head to the end of the string
  313. return $this->charsetHandler->transliterate($val); // return the string
  314. }
  315. function mapClass($typeIdentifier)
  316. {
  317. //Check out if class exists
  318. if($typeIdentifier == "")
  319. {
  320. return NULL;
  321. }
  322. $clazz = NULL;
  323. $mappedClass = str_replace('.', '/', $typeIdentifier);
  324. if($typeIdentifier == "flex.messaging.messages.CommandMessage")
  325. {
  326. return new CommandMessage();
  327. }
  328. if($typeIdentifier == "flex.messaging.messages.RemotingMessage")
  329. {
  330. return new RemotingMessage();
  331. }
  332. if(isset($GLOBALS['amfphp']['incomingClassMappings'][$typeIdentifier]))
  333. {
  334. $mappedClass = str_replace('.', '/', $GLOBALS['amfphp']['incomingClassMappings'][$typeIdentifier]);
  335. }
  336. $include = FALSE;
  337. if(file_exists($GLOBALS['amfphp']['customMappingsPath'] . $mappedClass . '.php'))
  338. {
  339. $include = $GLOBALS['amfphp']['customMappingsPath'] . $mappedClass . '.php';
  340. }
  341. elseif(file_exists($GLOBALS['amfphp']['customMappingsPath'] . $mappedClass . '.class.php'))
  342. {
  343. $include = $GLOBALS['amfphp']['customMappingsPath'] . $mappedClass . '.class.php';
  344. }
  345. if($include !== FALSE)
  346. {
  347. include_once($include);
  348. $lastPlace = strrpos('/' . $mappedClass, '/');
  349. $classname = substr($mappedClass, $lastPlace);
  350. if(class_exists($classname))
  351. {
  352. $clazz = new $classname;
  353. }
  354. }
  355. return $clazz; // return the object
  356. }
  357. }
  358. ?>