Serializer.php 18 KB

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