Serializer.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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_Amf3
  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_Serializer */
  22. require_once 'Zend/Amf/Parse/Serializer.php';
  23. /** Zend_Amf_Parse_TypeLoader */
  24. require_once 'Zend/Amf/Parse/TypeLoader.php';
  25. /**
  26. * Detect PHP object type and convert it to a corresponding AMF3 object type
  27. *
  28. * @package Zend_Amf
  29. * @subpackage Parse_Amf3
  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_Amf3_Serializer extends Zend_Amf_Parse_Serializer
  34. {
  35. /**
  36. * reference key to objects that have already been encountered.
  37. * @var array
  38. */
  39. private $_references = array();
  40. /**
  41. * Serialize PHP types to AMF3 and write to stream
  42. *
  43. * Checks to see if the type was declared and then either
  44. * auto negotiates the type or use the user defined markerType to
  45. * serialize the data from php back to AMF3
  46. *
  47. * @param mixed $content
  48. * @param int $markerType
  49. * @return void
  50. */
  51. public function writeTypeMarker($data, $markerType=null)
  52. {
  53. if (null !== $markerType) {
  54. // Write the Type Marker to denote the following action script data type
  55. $this->_stream->writeByte($markerType);
  56. switch ($markerType) {
  57. case Zend_Amf_Constants::AMF3_NULL:
  58. break;
  59. case Zend_Amf_Constants::AMF3_BOOLEAN_FALSE:
  60. break;
  61. case Zend_Amf_Constants::AMF3_BOOLEAN_TRUE:
  62. break;
  63. case Zend_Amf_Constants::AMF3_INTEGER:
  64. $this->writeInteger($data);
  65. break;
  66. case Zend_Amf_Constants::AMF3_NUMBER:
  67. $this->_stream->writeDouble($data);
  68. break;
  69. case Zend_Amf_Constants::AMF3_STRING:
  70. $this->writeString($data);
  71. break;
  72. case Zend_Amf_Constants::AMF3_DATE:
  73. $this->writeDate($data);
  74. break;
  75. case Zend_Amf_Constants::AMF3_ARRAY:
  76. $this->writeArray($data);
  77. break;
  78. case Zend_Amf_Constants::AMF3_OBJECT:
  79. $this->writeObject($data);
  80. break;
  81. case Zend_Amf_Constants::AMF3_BYTEARRAY:
  82. $this->writeString($data instanceof Zend_Amf_Value_ByteArray ? $data->getData() : $data);
  83. break;
  84. case Zend_Amf_Constants::AMF3_XMLSTRING;
  85. $this->writeString($data);
  86. break;
  87. default:
  88. require_once 'Zend/Amf/Exception.php';
  89. throw new Zend_Amf_Exception('Unknown Type Marker: ' . $markerType);
  90. }
  91. } else {
  92. // Detect Type Marker
  93. if(is_resource($data)) {
  94. $data = Zend_Amf_Parse_TypeLoader::handleResource($data);
  95. }
  96. switch (true) {
  97. case (null === $data):
  98. $markerType = Zend_Amf_Constants::AMF3_NULL;
  99. break;
  100. case (is_bool($data)):
  101. if ($data){
  102. $markerType = Zend_Amf_Constants::AMF3_BOOLEAN_TRUE;
  103. } else {
  104. $markerType = Zend_Amf_Constants::AMF3_BOOLEAN_FALSE;
  105. }
  106. break;
  107. case (is_int($data)):
  108. if (($data > 0xFFFFFFF) || ($data < -268435456)) {
  109. $markerType = Zend_Amf_Constants::AMF3_NUMBER;
  110. } else {
  111. $markerType = Zend_Amf_Constants::AMF3_INTEGER;
  112. }
  113. break;
  114. case (is_float($data)):
  115. $markerType = Zend_Amf_Constants::AMF3_NUMBER;
  116. break;
  117. case (is_string($data)):
  118. $markerType = Zend_Amf_Constants::AMF3_STRING;
  119. break;
  120. case (is_array($data)):
  121. $markerType = Zend_Amf_Constants::AMF3_ARRAY;
  122. break;
  123. case (is_object($data)):
  124. // Handle object types.
  125. if (($data instanceof DateTime) || ($data instanceof Zend_Date)) {
  126. $markerType = Zend_Amf_Constants::AMF3_DATE;
  127. } else if ($data instanceof Zend_Amf_Value_ByteArray) {
  128. $markerType = Zend_Amf_Constants::AMF3_BYTEARRAY;
  129. } else if ($data instanceof DOMDocument) {
  130. // convert object to string
  131. $data = $data->saveXml();
  132. $markerType = Zend_Amf_Constants::AMF3_XMLSTRING;
  133. } else if ($data instanceof SimpleXMLElement) {
  134. // convert object to string;
  135. $data = $data->asXML();
  136. $markerType = Zend_Amf_Constants::AMF3_XMLSTRING;
  137. } else {
  138. $markerType = Zend_Amf_Constants::AMF3_OBJECT;
  139. }
  140. break;
  141. default:
  142. require_once 'Zend/Amf/Exception.php';
  143. throw new Zend_Amf_Exception('Unsupported data type: ' . gettype($data));
  144. }
  145. $this->writeTypeMarker($data, $markerType);
  146. }
  147. }
  148. /**
  149. * Write an AMF3 integer
  150. *
  151. * @param int|float $data
  152. * @return Zend_Amf_Parse_Amf3_Serializer
  153. */
  154. public function writeInteger($int)
  155. {
  156. if (($int & 0xffffff80) == 0) {
  157. $this->_stream->writeByte($int & 0x7f);
  158. return $this;
  159. }
  160. if (($int & 0xffffc000) == 0 ) {
  161. $this->_stream->writeByte(($int >> 7 ) | 0x80);
  162. $this->_stream->writeByte($int & 0x7f);
  163. return $this;
  164. }
  165. if (($int & 0xffe00000) == 0) {
  166. $this->_stream->writeByte(($int >> 14 ) | 0x80);
  167. $this->_stream->writeByte(($int >> 7 ) | 0x80);
  168. $this->_stream->writeByte($int & 0x7f);
  169. return $this;
  170. }
  171. $this->_stream->writeByte(($int >> 22 ) | 0x80);
  172. $this->_stream->writeByte(($int >> 15 ) | 0x80);
  173. $this->_stream->writeByte(($int >> 8 ) | 0x80);
  174. $this->_stream->writeByte($int & 0xff);
  175. return $this;
  176. }
  177. /**
  178. * Send string to output stream
  179. *
  180. * @param string $string
  181. * @return Zend_Amf_Parse_Amf3_Serializer
  182. */
  183. public function writeString($string)
  184. {
  185. $ref = strlen($string) << 1 | 0x01;
  186. $this->writeInteger($ref);
  187. $this->_stream->writeBytes($string);
  188. return $this;
  189. }
  190. /**
  191. * Convert DateTime/Zend_Date to AMF date
  192. *
  193. * @param DateTime|Zend_Date $date
  194. * @return Zend_Amf_Parse_Amf3_Serializer
  195. */
  196. public function writeDate($date)
  197. {
  198. if ($date instanceof DateTime) {
  199. $dateString = $date->format('U') * 1000;
  200. } elseif ($date instanceof Zend_Date) {
  201. $dateString = $date->toString('U') * 1000;
  202. } else {
  203. require_once 'Zend/Amf/Exception.php';
  204. throw new Zend_Amf_Exception('Invalid date specified; must be a string DateTime or Zend_Date object');
  205. }
  206. $this->writeInteger(0x01);
  207. // write time to stream minus milliseconds
  208. $this->_stream->writeDouble($dateString);
  209. return $this;
  210. }
  211. /**
  212. * Write a PHP array back to the amf output stream
  213. *
  214. * @param array $array
  215. * @return Zend_Amf_Parse_Amf3_Serializer
  216. */
  217. public function writeArray(array $array)
  218. {
  219. $this->_references[] = $array;
  220. // have to seperate mixed from numberic keys.
  221. $numeric = array();
  222. $string = array();
  223. foreach ($array as $key => $value) {
  224. if (is_int($key)) {
  225. $numeric[] = $value;
  226. } else {
  227. $string[$key] = $value;
  228. }
  229. }
  230. // write the preamble id of the array
  231. $length = count($numeric);
  232. $id = ($length << 1) | 0x01;
  233. $this->writeInteger($id);
  234. //Write the mixed type array to the output stream
  235. foreach($string as $key => $value) {
  236. $this->writeString($key)
  237. ->writeTypeMarker($value);
  238. }
  239. $this->writeString('');
  240. // Write the numeric array to ouput stream
  241. foreach($numeric as $value) {
  242. $this->writeTypeMarker($value);
  243. }
  244. return $this;
  245. }
  246. /**
  247. * Create a reference to an existing object.
  248. * @param $key
  249. * @return Zend_Amf_Parse_Amf3_Serializer
  250. */
  251. public function writeReference($key)
  252. {
  253. $this->writeInteger($key << 1);
  254. return $this;
  255. }
  256. /**
  257. * Check to see if the reference already exists in the lookup table.
  258. * @param $object
  259. * @return object reference | false if it is not found.
  260. */
  261. private function referenceExist($object)
  262. {
  263. $key = array_search($object, $this->_references, true);
  264. if($key !== false) {
  265. return $key;
  266. } else {
  267. $this->_references[] = $object;
  268. return false;
  269. }
  270. }
  271. /**
  272. * Write object to ouput stream
  273. *
  274. * @param mixed $data
  275. * @return Zend_Amf_Parse_Amf3_Serializer
  276. */
  277. public function writeObject($object)
  278. {
  279. if(($key = $this->referenceExist($object)) !== false) {
  280. $this->writeReference($key);
  281. return $this;
  282. }
  283. $encoding = Zend_Amf_Constants::ET_PROPLIST;
  284. $className = '';
  285. //Check to see if the object is a typed object and we need to change
  286. switch (true) {
  287. // the return class mapped name back to actionscript class name.
  288. case ($className = Zend_Amf_Parse_TypeLoader::getMappedClassName(get_class($object))):
  289. break;
  290. // Check to see if the user has defined an explicit Action Script type.
  291. case isset($object->_explicitType):
  292. $className = $object->_explicitType;
  293. break;
  294. // Check if user has defined a method for accessing the Action Script type
  295. case method_exists($object, 'getASClassName'):
  296. $className = $object->getASClassName();
  297. break;
  298. // No return class name is set make it a generic object
  299. case ($object instanceof stdClass):
  300. $className = '';
  301. break;
  302. // By default, use object's class name
  303. default:
  304. $className = get_class($object);
  305. break;
  306. }
  307. $traitsInfo = Zend_Amf_Constants::AMF3_OBJECT_ENCODING;
  308. $traitsInfo |= $encoding << 2;
  309. try {
  310. switch($encoding) {
  311. case Zend_Amf_Constants::ET_PROPLIST:
  312. $count = 0;
  313. foreach($object as $key => $value) {
  314. if( $key[0] != "_") {
  315. $count++;
  316. }
  317. }
  318. $traitsInfo |= ($count << 4);
  319. // Write the object ID
  320. $this->writeInteger($traitsInfo);
  321. // Write the classname
  322. $this->writeString($className);
  323. // Write the object Key's to the output stream
  324. foreach ($object as $key => $value) {
  325. if( $key[0] != "_") {
  326. $this->writeString($key);
  327. }
  328. }
  329. //Write the object values to the output stream.
  330. foreach ($object as $key => $value) {
  331. if( $key[0] != "_") {
  332. $this->writeTypeMarker($value);
  333. }
  334. }
  335. break;
  336. case Zend_Amf_Constants::ET_EXTERNAL:
  337. require_once 'Zend/Amf/Exception.php';
  338. throw new Zend_Amf_Exception('External Object Encoding not implemented');
  339. break;
  340. default:
  341. require_once 'Zend/Amf/Exception.php';
  342. throw new Zend_Amf_Exception('Unknown Object Encoding type: ' . $encoding);
  343. }
  344. } catch (Exception $e) {
  345. require_once 'Zend/Amf/Exception.php';
  346. throw new Zend_Amf_Exception('Unable to writeObject output: ' . $e->getMessage());
  347. }
  348. return $this;
  349. }
  350. }