Serializer.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Serializer.php 2504 2011-12-28 07:35:29Z liu21st $
  21. */
  22. /** Zend_Amf_Parse_Serializer */
  23. require_once 'Zend/Amf/Parse/Serializer.php';
  24. /** Zend_Amf_Parse_TypeLoader */
  25. require_once 'Zend/Amf/Parse/TypeLoader.php';
  26. /**
  27. * Detect PHP object type and convert it to a corresponding AMF3 object type
  28. *
  29. * @package Zend_Amf
  30. * @subpackage Parse_Amf3
  31. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Amf_Parse_Amf3_Serializer extends Zend_Amf_Parse_Serializer
  35. {
  36. /**
  37. * An array of reference objects per amf body
  38. * @var array
  39. */
  40. protected $_referenceObjects = array();
  41. /**
  42. * An array of reference strings per amf body
  43. * @var array
  44. */
  45. protected $_referenceStrings = array();
  46. /**
  47. * An array of reference class definitions, indexed by classname
  48. * @var array
  49. */
  50. protected $_referenceDefinitions = array();
  51. /**
  52. * Serialize PHP types to AMF3 and write to stream
  53. *
  54. * Checks to see if the type was declared and then either
  55. * auto negotiates the type or use the user defined markerType to
  56. * serialize the data from php back to AMF3
  57. *
  58. * @param mixed $content
  59. * @param int $markerType
  60. * @return void
  61. */
  62. public function writeTypeMarker($data, $markerType=null)
  63. {
  64. if (null !== $markerType) {
  65. // Write the Type Marker to denote the following action script data type
  66. $this->_stream->writeByte($markerType);
  67. switch ($markerType) {
  68. case Zend_Amf_Constants::AMF3_NULL:
  69. break;
  70. case Zend_Amf_Constants::AMF3_BOOLEAN_FALSE:
  71. break;
  72. case Zend_Amf_Constants::AMF3_BOOLEAN_TRUE:
  73. break;
  74. case Zend_Amf_Constants::AMF3_INTEGER:
  75. $this->writeInteger($data);
  76. break;
  77. case Zend_Amf_Constants::AMF3_NUMBER:
  78. $this->_stream->writeDouble($data);
  79. break;
  80. case Zend_Amf_Constants::AMF3_STRING:
  81. $this->writeString($data);
  82. break;
  83. case Zend_Amf_Constants::AMF3_DATE:
  84. $this->writeDate($data);
  85. break;
  86. case Zend_Amf_Constants::AMF3_ARRAY:
  87. $this->writeArray($data);
  88. break;
  89. case Zend_Amf_Constants::AMF3_OBJECT:
  90. $this->writeObject($data);
  91. break;
  92. case Zend_Amf_Constants::AMF3_BYTEARRAY:
  93. $this->writeByteArray($data);
  94. break;
  95. case Zend_Amf_Constants::AMF3_XMLSTRING;
  96. $this->writeXml($data);
  97. break;
  98. default:
  99. require_once 'Zend/Amf/Exception.php';
  100. throw new Zend_Amf_Exception('Unknown Type Marker: ' . $markerType);
  101. }
  102. } else {
  103. // Detect Type Marker
  104. if(is_resource($data)) {
  105. $data = Zend_Amf_Parse_TypeLoader::handleResource($data);
  106. }
  107. switch (true) {
  108. case (null === $data):
  109. $markerType = Zend_Amf_Constants::AMF3_NULL;
  110. break;
  111. case (is_bool($data)):
  112. if ($data){
  113. $markerType = Zend_Amf_Constants::AMF3_BOOLEAN_TRUE;
  114. } else {
  115. $markerType = Zend_Amf_Constants::AMF3_BOOLEAN_FALSE;
  116. }
  117. break;
  118. case (is_int($data)):
  119. if (($data > 0xFFFFFFF) || ($data < -268435456)) {
  120. $markerType = Zend_Amf_Constants::AMF3_NUMBER;
  121. } else {
  122. $markerType = Zend_Amf_Constants::AMF3_INTEGER;
  123. }
  124. break;
  125. case (is_float($data)):
  126. $markerType = Zend_Amf_Constants::AMF3_NUMBER;
  127. break;
  128. case (is_string($data)):
  129. $markerType = Zend_Amf_Constants::AMF3_STRING;
  130. break;
  131. case (is_array($data)):
  132. $markerType = Zend_Amf_Constants::AMF3_ARRAY;
  133. break;
  134. case (is_object($data)):
  135. // Handle object types.
  136. if (($data instanceof DateTime) || ($data instanceof Zend_Date)) {
  137. $markerType = Zend_Amf_Constants::AMF3_DATE;
  138. } else if ($data instanceof Zend_Amf_Value_ByteArray) {
  139. $markerType = Zend_Amf_Constants::AMF3_BYTEARRAY;
  140. } else if (($data instanceof DOMDocument) || ($data instanceof SimpleXMLElement)) {
  141. $markerType = Zend_Amf_Constants::AMF3_XMLSTRING;
  142. } else {
  143. $markerType = Zend_Amf_Constants::AMF3_OBJECT;
  144. }
  145. break;
  146. default:
  147. require_once 'Zend/Amf/Exception.php';
  148. throw new Zend_Amf_Exception('Unsupported data type: ' . gettype($data));
  149. }
  150. $this->writeTypeMarker($data, $markerType);
  151. }
  152. }
  153. /**
  154. * Write an AMF3 integer
  155. *
  156. * @param int|float $data
  157. * @return Zend_Amf_Parse_Amf3_Serializer
  158. */
  159. public function writeInteger($int)
  160. {
  161. if (($int & 0xffffff80) == 0) {
  162. $this->_stream->writeByte($int & 0x7f);
  163. return $this;
  164. }
  165. if (($int & 0xffffc000) == 0 ) {
  166. $this->_stream->writeByte(($int >> 7 ) | 0x80);
  167. $this->_stream->writeByte($int & 0x7f);
  168. return $this;
  169. }
  170. if (($int & 0xffe00000) == 0) {
  171. $this->_stream->writeByte(($int >> 14 ) | 0x80);
  172. $this->_stream->writeByte(($int >> 7 ) | 0x80);
  173. $this->_stream->writeByte($int & 0x7f);
  174. return $this;
  175. }
  176. $this->_stream->writeByte(($int >> 22 ) | 0x80);
  177. $this->_stream->writeByte(($int >> 15 ) | 0x80);
  178. $this->_stream->writeByte(($int >> 8 ) | 0x80);
  179. $this->_stream->writeByte($int & 0xff);
  180. return $this;
  181. }
  182. /**
  183. * Send string to output stream, without trying to reference it.
  184. * The string is prepended with strlen($string) << 1 | 0x01
  185. *
  186. * @param string $string
  187. * @return Zend_Amf_Parse_Amf3_Serializer
  188. */
  189. protected function writeBinaryString($string){
  190. $ref = strlen($string) << 1 | 0x01;
  191. $this->writeInteger($ref);
  192. $this->_stream->writeBytes($string);
  193. return $this;
  194. }
  195. /**
  196. * Send string to output stream
  197. *
  198. * @param string $string
  199. * @return Zend_Amf_Parse_Amf3_Serializer
  200. */
  201. public function writeString($string)
  202. {
  203. $len = strlen($string);
  204. if(!$len){
  205. $this->writeInteger(0x01);
  206. return $this;
  207. }
  208. $ref = array_search($string, $this->_referenceStrings, true);
  209. if($ref === false){
  210. $this->_referenceStrings[] = $string;
  211. $this->writeBinaryString($string);
  212. } else {
  213. $ref <<= 1;
  214. $this->writeInteger($ref);
  215. }
  216. return $this;
  217. }
  218. /**
  219. * Send ByteArray to output stream
  220. *
  221. * @param string|Zend_Amf_Value_ByteArray $data
  222. * @return Zend_Amf_Parse_Amf3_Serializer
  223. */
  224. public function writeByteArray($data){
  225. if($this->writeObjectReference($data)){
  226. return $this;
  227. }
  228. if(is_string($data)) {
  229. //nothing to do
  230. } else if ($data instanceof Zend_Amf_Value_ByteArray) {
  231. $data = $data->getData();
  232. } else {
  233. require_once 'Zend/Amf/Exception.php';
  234. throw new Zend_Amf_Exception('Invalid ByteArray specified; must be a string or Zend_Amf_Value_ByteArray');
  235. }
  236. $this->writeBinaryString($data);
  237. return $this;
  238. }
  239. /**
  240. * Send xml to output stream
  241. *
  242. * @param DOMDocument|SimpleXMLElement $xml
  243. * @return Zend_Amf_Parse_Amf3_Serializer
  244. */
  245. public function writeXml($xml)
  246. {
  247. if($this->writeObjectReference($xml)){
  248. return $this;
  249. }
  250. if(is_string($xml)) {
  251. //nothing to do
  252. } else if ($xml instanceof DOMDocument) {
  253. $xml = $xml->saveXml();
  254. } else if ($xml instanceof SimpleXMLElement) {
  255. $xml = $xml->asXML();
  256. } else {
  257. require_once 'Zend/Amf/Exception.php';
  258. throw new Zend_Amf_Exception('Invalid xml specified; must be a DOMDocument or SimpleXMLElement');
  259. }
  260. $this->writeBinaryString($xml);
  261. return $this;
  262. }
  263. /**
  264. * Convert DateTime/Zend_Date to AMF date
  265. *
  266. * @param DateTime|Zend_Date $date
  267. * @return Zend_Amf_Parse_Amf3_Serializer
  268. */
  269. public function writeDate($date)
  270. {
  271. if($this->writeObjectReference($date)){
  272. return $this;
  273. }
  274. if ($date instanceof DateTime) {
  275. $dateString = $date->format('U') * 1000;
  276. } elseif ($date instanceof Zend_Date) {
  277. $dateString = $date->toString('U') * 1000;
  278. } else {
  279. require_once 'Zend/Amf/Exception.php';
  280. throw new Zend_Amf_Exception('Invalid date specified; must be a string DateTime or Zend_Date object');
  281. }
  282. $this->writeInteger(0x01);
  283. // write time to stream minus milliseconds
  284. $this->_stream->writeDouble($dateString);
  285. return $this;
  286. }
  287. /**
  288. * Write a PHP array back to the amf output stream
  289. *
  290. * @param array $array
  291. * @return Zend_Amf_Parse_Amf3_Serializer
  292. */
  293. public function writeArray(array $array)
  294. {
  295. if($this->writeObjectReference($array)){
  296. return $this;
  297. }
  298. // have to seperate mixed from numberic keys.
  299. $numeric = array();
  300. $string = array();
  301. foreach ($array as $key => $value) {
  302. if (is_int($key)) {
  303. $numeric[] = $value;
  304. } else {
  305. $string[$key] = $value;
  306. }
  307. }
  308. // write the preamble id of the array
  309. $length = count($numeric);
  310. $id = ($length << 1) | 0x01;
  311. $this->writeInteger($id);
  312. //Write the mixed type array to the output stream
  313. foreach($string as $key => $value) {
  314. $this->writeString($key)
  315. ->writeTypeMarker($value);
  316. }
  317. $this->writeString('');
  318. // Write the numeric array to ouput stream
  319. foreach($numeric as $value) {
  320. $this->writeTypeMarker($value);
  321. }
  322. return $this;
  323. }
  324. /**
  325. * Check if the given object is in the reference table, write the reference if it exists,
  326. * otherwise add the object to the reference table
  327. *
  328. * @param mixed $object object to check for reference
  329. * @return Boolean true, if the reference was written, false otherwise
  330. */
  331. protected function writeObjectReference($object){
  332. $ref = array_search($object, $this->_referenceObjects,true);
  333. //quickly handle object references
  334. if($ref !== false){
  335. $ref <<= 1;
  336. $this->writeInteger($ref);
  337. return true;
  338. }
  339. $this->_referenceObjects[] = $object;
  340. return false;
  341. }
  342. /**
  343. * Write object to ouput stream
  344. *
  345. * @param mixed $data
  346. * @return Zend_Amf_Parse_Amf3_Serializer
  347. */
  348. public function writeObject($object)
  349. {
  350. if($this->writeObjectReference($object)){
  351. return $this;
  352. }
  353. $className = '';
  354. //Check to see if the object is a typed object and we need to change
  355. switch (true) {
  356. // the return class mapped name back to actionscript class name.
  357. case ($className = Zend_Amf_Parse_TypeLoader::getMappedClassName(get_class($object))):
  358. break;
  359. // Check to see if the user has defined an explicit Action Script type.
  360. case isset($object->_explicitType):
  361. $className = $object->_explicitType;
  362. break;
  363. // Check if user has defined a method for accessing the Action Script type
  364. case method_exists($object, 'getASClassName'):
  365. $className = $object->getASClassName();
  366. break;
  367. // No return class name is set make it a generic object
  368. case ($object instanceof stdClass):
  369. $className = '';
  370. break;
  371. // By default, use object's class name
  372. default:
  373. $className = get_class($object);
  374. break;
  375. }
  376. $writeTraits = true;
  377. //check to see, if we have a corresponding definition
  378. if(array_key_exists($className, $this->_referenceDefinitions)){
  379. $traitsInfo = $this->_referenceDefinitions[$className]['id'];
  380. $encoding = $this->_referenceDefinitions[$className]['encoding'];
  381. $propertyNames = $this->_referenceDefinitions[$className]['propertyNames'];
  382. $traitsInfo = ($traitsInfo << 2) | 0x01;
  383. $writeTraits = false;
  384. } else {
  385. $propertyNames = array();
  386. if($className == ''){
  387. //if there is no className, we interpret the class as dynamic without any sealed members
  388. $encoding = Zend_Amf_Constants::ET_DYNAMIC;
  389. } else {
  390. $encoding = Zend_Amf_Constants::ET_PROPLIST;
  391. foreach($object as $key => $value) {
  392. if( $key[0] != "_") {
  393. $propertyNames[] = $key;
  394. }
  395. }
  396. }
  397. $this->_referenceDefinitions[$className] = array(
  398. 'id' => count($this->_referenceDefinitions),
  399. 'encoding' => $encoding,
  400. 'propertyNames' => $propertyNames,
  401. );
  402. $traitsInfo = Zend_Amf_Constants::AMF3_OBJECT_ENCODING;
  403. $traitsInfo |= $encoding << 2;
  404. $traitsInfo |= (count($propertyNames) << 4);
  405. }
  406. $this->writeInteger($traitsInfo);
  407. if($writeTraits){
  408. $this->writeString($className);
  409. foreach ($propertyNames as $value) {
  410. $this->writeString($value);
  411. }
  412. }
  413. try {
  414. switch($encoding) {
  415. case Zend_Amf_Constants::ET_PROPLIST:
  416. //Write the sealed values to the output stream.
  417. foreach ($propertyNames as $key) {
  418. $this->writeTypeMarker($object->$key);
  419. }
  420. break;
  421. case Zend_Amf_Constants::ET_DYNAMIC:
  422. //Write the sealed values to the output stream.
  423. foreach ($propertyNames as $key) {
  424. $this->writeTypeMarker($object->$key);
  425. }
  426. //Write remaining properties
  427. foreach($object as $key => $value){
  428. if(!in_array($key,$propertyNames) && $key[0] != "_"){
  429. $this->writeString($key);
  430. $this->writeTypeMarker($value);
  431. }
  432. }
  433. //Write an empty string to end the dynamic part
  434. $this->writeString('');
  435. break;
  436. case Zend_Amf_Constants::ET_EXTERNAL:
  437. require_once 'Zend/Amf/Exception.php';
  438. throw new Zend_Amf_Exception('External Object Encoding not implemented');
  439. break;
  440. default:
  441. require_once 'Zend/Amf/Exception.php';
  442. throw new Zend_Amf_Exception('Unknown Object Encoding type: ' . $encoding);
  443. }
  444. } catch (Exception $e) {
  445. require_once 'Zend/Amf/Exception.php';
  446. throw new Zend_Amf_Exception('Unable to writeObject output: ' . $e->getMessage());
  447. }
  448. return $this;
  449. }
  450. }