Serializer.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. // arrays aren't reference here but still counted
  298. $this->_referenceObjects[] = $array;
  299. // have to seperate mixed from numberic keys.
  300. $numeric = array();
  301. $string = array();
  302. foreach ($array as $key => $value) {
  303. if (is_int($key)) {
  304. $numeric[] = $value;
  305. } else {
  306. $string[$key] = $value;
  307. }
  308. }
  309. // write the preamble id of the array
  310. $length = count($numeric);
  311. $id = ($length << 1) | 0x01;
  312. $this->writeInteger($id);
  313. //Write the mixed type array to the output stream
  314. foreach($string as $key => $value) {
  315. $this->writeString($key)
  316. ->writeTypeMarker($value);
  317. }
  318. $this->writeString('');
  319. // Write the numeric array to ouput stream
  320. foreach($numeric as $value) {
  321. $this->writeTypeMarker($value);
  322. }
  323. return $this;
  324. }
  325. /**
  326. * Check if the given object is in the reference table, write the reference if it exists,
  327. * otherwise add the object to the reference table
  328. *
  329. * @param mixed $object object to check for reference
  330. * @return Boolean true, if the reference was written, false otherwise
  331. */
  332. protected function writeObjectReference($object)
  333. {
  334. $ref = array_search($object, $this->_referenceObjects,true);
  335. //quickly handle object references
  336. if($ref !== false){
  337. $ref <<= 1;
  338. $this->writeInteger($ref);
  339. return true;
  340. }
  341. $this->_referenceObjects[] = $object;
  342. return false;
  343. }
  344. /**
  345. * Write object to ouput stream
  346. *
  347. * @param mixed $data
  348. * @return Zend_Amf_Parse_Amf3_Serializer
  349. */
  350. public function writeObject($object)
  351. {
  352. if($this->writeObjectReference($object)){
  353. return $this;
  354. }
  355. $className = '';
  356. //Check to see if the object is a typed object and we need to change
  357. switch (true) {
  358. // the return class mapped name back to actionscript class name.
  359. case ($className = Zend_Amf_Parse_TypeLoader::getMappedClassName(get_class($object))):
  360. break;
  361. // Check to see if the user has defined an explicit Action Script type.
  362. case isset($object->_explicitType):
  363. $className = $object->_explicitType;
  364. break;
  365. // Check if user has defined a method for accessing the Action Script type
  366. case method_exists($object, 'getASClassName'):
  367. $className = $object->getASClassName();
  368. break;
  369. // No return class name is set make it a generic object
  370. case ($object instanceof stdClass):
  371. $className = '';
  372. break;
  373. // By default, use object's class name
  374. default:
  375. $className = get_class($object);
  376. break;
  377. }
  378. $writeTraits = true;
  379. //check to see, if we have a corresponding definition
  380. if(array_key_exists($className, $this->_referenceDefinitions)){
  381. $traitsInfo = $this->_referenceDefinitions[$className]['id'];
  382. $encoding = $this->_referenceDefinitions[$className]['encoding'];
  383. $propertyNames = $this->_referenceDefinitions[$className]['propertyNames'];
  384. $traitsInfo = ($traitsInfo << 2) | 0x01;
  385. $writeTraits = false;
  386. } else {
  387. $propertyNames = array();
  388. if($className == ''){
  389. //if there is no className, we interpret the class as dynamic without any sealed members
  390. $encoding = Zend_Amf_Constants::ET_DYNAMIC;
  391. } else {
  392. $encoding = Zend_Amf_Constants::ET_PROPLIST;
  393. foreach($object as $key => $value) {
  394. if( $key[0] != "_") {
  395. $propertyNames[] = $key;
  396. }
  397. }
  398. }
  399. $this->_referenceDefinitions[$className] = array(
  400. 'id' => count($this->_referenceDefinitions),
  401. 'encoding' => $encoding,
  402. 'propertyNames' => $propertyNames,
  403. );
  404. $traitsInfo = Zend_Amf_Constants::AMF3_OBJECT_ENCODING;
  405. $traitsInfo |= $encoding << 2;
  406. $traitsInfo |= (count($propertyNames) << 4);
  407. }
  408. $this->writeInteger($traitsInfo);
  409. if($writeTraits){
  410. $this->writeString($className);
  411. foreach ($propertyNames as $value) {
  412. $this->writeString($value);
  413. }
  414. }
  415. try {
  416. switch($encoding) {
  417. case Zend_Amf_Constants::ET_PROPLIST:
  418. //Write the sealed values to the output stream.
  419. foreach ($propertyNames as $key) {
  420. $this->writeTypeMarker($object->$key);
  421. }
  422. break;
  423. case Zend_Amf_Constants::ET_DYNAMIC:
  424. //Write the sealed values to the output stream.
  425. foreach ($propertyNames as $key) {
  426. $this->writeTypeMarker($object->$key);
  427. }
  428. //Write remaining properties
  429. foreach($object as $key => $value){
  430. if(!in_array($key,$propertyNames) && $key[0] != "_"){
  431. $this->writeString($key);
  432. $this->writeTypeMarker($value);
  433. }
  434. }
  435. //Write an empty string to end the dynamic part
  436. $this->writeString('');
  437. break;
  438. case Zend_Amf_Constants::ET_EXTERNAL:
  439. require_once 'Zend/Amf/Exception.php';
  440. throw new Zend_Amf_Exception('External Object Encoding not implemented');
  441. break;
  442. default:
  443. require_once 'Zend/Amf/Exception.php';
  444. throw new Zend_Amf_Exception('Unknown Object Encoding type: ' . $encoding);
  445. }
  446. } catch (Exception $e) {
  447. require_once 'Zend/Amf/Exception.php';
  448. throw new Zend_Amf_Exception('Unable to writeObject output: ' . $e->getMessage(), 0, $e);
  449. }
  450. return $this;
  451. }
  452. }