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-2010 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. /** Zend_Amf_Parse_Serializer */
  25. require_once 'Zend/Amf/Parse/Serializer.php';
  26. /** Zend_Amf_Parse_TypeLoader */
  27. require_once 'Zend/Amf/Parse/TypeLoader.php';
  28. /**
  29. * Detect PHP object type and convert it to a corresponding AMF3 object type
  30. *
  31. * @package Zend_Amf
  32. * @subpackage Parse_Amf3
  33. * @copyright Copyright (c) 2005-2010 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_Amf3_Serializer extends Zend_Amf_Parse_Serializer
  37. {
  38. /**
  39. * An array of reference objects per amf body
  40. * @var array
  41. */
  42. protected $_referenceObjects = array();
  43. /**
  44. * An array of reference strings per amf body
  45. * @var array
  46. */
  47. protected $_referenceStrings = array();
  48. /**
  49. * An array of reference class definitions, indexed by classname
  50. * @var array
  51. */
  52. protected $_referenceDefinitions = array();
  53. /**
  54. * Serialize PHP types to AMF3 and write to stream
  55. *
  56. * Checks to see if the type was declared and then either
  57. * auto negotiates the type or use the user defined markerType to
  58. * serialize the data from php back to AMF3
  59. *
  60. * @param mixed $content
  61. * @param int $markerType
  62. * @return void
  63. */
  64. public function writeTypeMarker($data, $markerType=null)
  65. {
  66. if (null !== $markerType) {
  67. // Write the Type Marker to denote the following action script data type
  68. $this->_stream->writeByte($markerType);
  69. switch ($markerType) {
  70. case Zend_Amf_Constants::AMF3_NULL:
  71. break;
  72. case Zend_Amf_Constants::AMF3_BOOLEAN_FALSE:
  73. break;
  74. case Zend_Amf_Constants::AMF3_BOOLEAN_TRUE:
  75. break;
  76. case Zend_Amf_Constants::AMF3_INTEGER:
  77. $this->writeInteger($data);
  78. break;
  79. case Zend_Amf_Constants::AMF3_NUMBER:
  80. $this->_stream->writeDouble($data);
  81. break;
  82. case Zend_Amf_Constants::AMF3_STRING:
  83. $this->writeString($data);
  84. break;
  85. case Zend_Amf_Constants::AMF3_DATE:
  86. $this->writeDate($data);
  87. break;
  88. case Zend_Amf_Constants::AMF3_ARRAY:
  89. $this->writeArray($data);
  90. break;
  91. case Zend_Amf_Constants::AMF3_OBJECT:
  92. $this->writeObject($data);
  93. break;
  94. case Zend_Amf_Constants::AMF3_BYTEARRAY:
  95. $this->writeByteArray($data);
  96. break;
  97. case Zend_Amf_Constants::AMF3_XMLSTRING;
  98. $this->writeXml($data);
  99. break;
  100. default:
  101. require_once 'Zend/Amf/Exception.php';
  102. throw new Zend_Amf_Exception('Unknown Type Marker: ' . $markerType);
  103. }
  104. } else {
  105. // Detect Type Marker
  106. if(is_resource($data)) {
  107. $data = Zend_Amf_Parse_TypeLoader::handleResource($data);
  108. }
  109. switch (true) {
  110. case (null === $data):
  111. $markerType = Zend_Amf_Constants::AMF3_NULL;
  112. break;
  113. case (is_bool($data)):
  114. if ($data){
  115. $markerType = Zend_Amf_Constants::AMF3_BOOLEAN_TRUE;
  116. } else {
  117. $markerType = Zend_Amf_Constants::AMF3_BOOLEAN_FALSE;
  118. }
  119. break;
  120. case (is_int($data)):
  121. if (($data > 0xFFFFFFF) || ($data < -268435456)) {
  122. $markerType = Zend_Amf_Constants::AMF3_NUMBER;
  123. } else {
  124. $markerType = Zend_Amf_Constants::AMF3_INTEGER;
  125. }
  126. break;
  127. case (is_float($data)):
  128. $markerType = Zend_Amf_Constants::AMF3_NUMBER;
  129. break;
  130. case (is_string($data)):
  131. $markerType = Zend_Amf_Constants::AMF3_STRING;
  132. break;
  133. case (is_array($data)):
  134. $markerType = Zend_Amf_Constants::AMF3_ARRAY;
  135. break;
  136. case (is_object($data)):
  137. // Handle object types.
  138. if (($data instanceof DateTime) || ($data instanceof Zend_Date)) {
  139. $markerType = Zend_Amf_Constants::AMF3_DATE;
  140. } else if ($data instanceof Zend_Amf_Value_ByteArray) {
  141. $markerType = Zend_Amf_Constants::AMF3_BYTEARRAY;
  142. } else if (($data instanceof DOMDocument) || ($data instanceof SimpleXMLElement)) {
  143. $markerType = Zend_Amf_Constants::AMF3_XMLSTRING;
  144. } else {
  145. $markerType = Zend_Amf_Constants::AMF3_OBJECT;
  146. }
  147. break;
  148. default:
  149. require_once 'Zend/Amf/Exception.php';
  150. throw new Zend_Amf_Exception('Unsupported data type: ' . gettype($data));
  151. }
  152. $this->writeTypeMarker($data, $markerType);
  153. }
  154. }
  155. /**
  156. * Write an AMF3 integer
  157. *
  158. * @param int|float $data
  159. * @return Zend_Amf_Parse_Amf3_Serializer
  160. */
  161. public function writeInteger($int)
  162. {
  163. if (($int & 0xffffff80) == 0) {
  164. $this->_stream->writeByte($int & 0x7f);
  165. return $this;
  166. }
  167. if (($int & 0xffffc000) == 0 ) {
  168. $this->_stream->writeByte(($int >> 7 ) | 0x80);
  169. $this->_stream->writeByte($int & 0x7f);
  170. return $this;
  171. }
  172. if (($int & 0xffe00000) == 0) {
  173. $this->_stream->writeByte(($int >> 14 ) | 0x80);
  174. $this->_stream->writeByte(($int >> 7 ) | 0x80);
  175. $this->_stream->writeByte($int & 0x7f);
  176. return $this;
  177. }
  178. $this->_stream->writeByte(($int >> 22 ) | 0x80);
  179. $this->_stream->writeByte(($int >> 15 ) | 0x80);
  180. $this->_stream->writeByte(($int >> 8 ) | 0x80);
  181. $this->_stream->writeByte($int & 0xff);
  182. return $this;
  183. }
  184. /**
  185. * Send string to output stream, without trying to reference it.
  186. * The string is prepended with strlen($string) << 1 | 0x01
  187. *
  188. * @param string $string
  189. * @return Zend_Amf_Parse_Amf3_Serializer
  190. */
  191. protected function writeBinaryString($string){
  192. $ref = strlen($string) << 1 | 0x01;
  193. $this->writeInteger($ref);
  194. $this->_stream->writeBytes($string);
  195. return $this;
  196. }
  197. /**
  198. * Send string to output stream
  199. *
  200. * @param string $string
  201. * @return Zend_Amf_Parse_Amf3_Serializer
  202. */
  203. public function writeString($string)
  204. {
  205. $len = strlen($string);
  206. if(!$len){
  207. $this->writeInteger(0x01);
  208. return $this;
  209. }
  210. $ref = array_search($string, $this->_referenceStrings, true);
  211. if($ref === false){
  212. $this->_referenceStrings[] = $string;
  213. $this->writeBinaryString($string);
  214. } else {
  215. $ref <<= 1;
  216. $this->writeInteger($ref);
  217. }
  218. return $this;
  219. }
  220. /**
  221. * Send ByteArray to output stream
  222. *
  223. * @param string|Zend_Amf_Value_ByteArray $data
  224. * @return Zend_Amf_Parse_Amf3_Serializer
  225. */
  226. public function writeByteArray($data){
  227. if($this->writeObjectReference($data)){
  228. return $this;
  229. }
  230. if(is_string($data)) {
  231. //nothing to do
  232. } else if ($data instanceof Zend_Amf_Value_ByteArray) {
  233. $data = $data->getData();
  234. } else {
  235. require_once 'Zend/Amf/Exception.php';
  236. throw new Zend_Amf_Exception('Invalid ByteArray specified; must be a string or Zend_Amf_Value_ByteArray');
  237. }
  238. $this->writeBinaryString($data);
  239. return $this;
  240. }
  241. /**
  242. * Send xml to output stream
  243. *
  244. * @param DOMDocument|SimpleXMLElement $xml
  245. * @return Zend_Amf_Parse_Amf3_Serializer
  246. */
  247. public function writeXml($xml)
  248. {
  249. if($this->writeObjectReference($xml)){
  250. return $this;
  251. }
  252. if(is_string($xml)) {
  253. //nothing to do
  254. } else if ($xml instanceof DOMDocument) {
  255. $xml = $xml->saveXml();
  256. } else if ($xml instanceof SimpleXMLElement) {
  257. $xml = $xml->asXML();
  258. } else {
  259. require_once 'Zend/Amf/Exception.php';
  260. throw new Zend_Amf_Exception('Invalid xml specified; must be a DOMDocument or SimpleXMLElement');
  261. }
  262. $this->writeBinaryString($xml);
  263. return $this;
  264. }
  265. /**
  266. * Convert DateTime/Zend_Date to AMF date
  267. *
  268. * @param DateTime|Zend_Date $date
  269. * @return Zend_Amf_Parse_Amf3_Serializer
  270. */
  271. public function writeDate($date)
  272. {
  273. if($this->writeObjectReference($date)){
  274. return $this;
  275. }
  276. if ($date instanceof DateTime) {
  277. $dateString = $date->format('U') * 1000;
  278. } elseif ($date instanceof Zend_Date) {
  279. $dateString = $date->toString('U') * 1000;
  280. } else {
  281. require_once 'Zend/Amf/Exception.php';
  282. throw new Zend_Amf_Exception('Invalid date specified; must be a string DateTime or Zend_Date object');
  283. }
  284. $this->writeInteger(0x01);
  285. // write time to stream minus milliseconds
  286. $this->_stream->writeDouble($dateString);
  287. return $this;
  288. }
  289. /**
  290. * Write a PHP array back to the amf output stream
  291. *
  292. * @param array $array
  293. * @return Zend_Amf_Parse_Amf3_Serializer
  294. */
  295. public function writeArray(array $array)
  296. {
  297. // have to seperate mixed from numberic keys.
  298. $numeric = array();
  299. $string = array();
  300. foreach ($array as $key => $value) {
  301. if (is_int($key)) {
  302. $numeric[] = $value;
  303. } else {
  304. $string[$key] = $value;
  305. }
  306. }
  307. // write the preamble id of the array
  308. $length = count($numeric);
  309. $id = ($length << 1) | 0x01;
  310. $this->writeInteger($id);
  311. //Write the mixed type array to the output stream
  312. foreach($string as $key => $value) {
  313. $this->writeString($key)
  314. ->writeTypeMarker($value);
  315. }
  316. $this->writeString('');
  317. // Write the numeric array to ouput stream
  318. foreach($numeric as $value) {
  319. $this->writeTypeMarker($value);
  320. }
  321. return $this;
  322. }
  323. /**
  324. * Check if the given object is in the reference table, write the reference if it exists,
  325. * otherwise add the object to the reference table
  326. *
  327. * @param mixed $object object to check for reference
  328. * @return Boolean true, if the reference was written, false otherwise
  329. */
  330. protected function writeObjectReference($object)
  331. {
  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(), 0, $e);
  447. }
  448. return $this;
  449. }
  450. }