PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Amf/Parse/Amf3/Serializer.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 534 lines | 331 code | 59 blank | 144 comment | 62 complexity | 07e4e0f389f4bc2c39941f26b6103278 MD5 | raw file
  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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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_key_exists($string, $this->_referenceStrings)
  222. ? $this->_referenceStrings[$string]
  223. : false;
  224. if ($ref === false){
  225. $this->_referenceStrings[$string] = count($this->_referenceStrings);
  226. $this->writeBinaryString($string);
  227. } else {
  228. $ref <<= 1;
  229. $this->writeInteger($ref);
  230. }
  231. return $this;
  232. }
  233. /**
  234. * Send ByteArray to output stream
  235. *
  236. * @param string|Zend_Amf_Value_ByteArray $data
  237. * @return Zend_Amf_Parse_Amf3_Serializer
  238. */
  239. public function writeByteArray(&$data)
  240. {
  241. if ($this->writeObjectReference($data)) {
  242. return $this;
  243. }
  244. if (is_string($data)) {
  245. //nothing to do
  246. } else if ($data instanceof Zend_Amf_Value_ByteArray) {
  247. $data = $data->getData();
  248. } else {
  249. require_once 'Zend/Amf/Exception.php';
  250. throw new Zend_Amf_Exception('Invalid ByteArray specified; must be a string or Zend_Amf_Value_ByteArray');
  251. }
  252. $this->writeBinaryString($data);
  253. return $this;
  254. }
  255. /**
  256. * Send xml to output stream
  257. *
  258. * @param DOMDocument|SimpleXMLElement $xml
  259. * @return Zend_Amf_Parse_Amf3_Serializer
  260. */
  261. public function writeXml($xml)
  262. {
  263. if ($this->writeObjectReference($xml)) {
  264. return $this;
  265. }
  266. if(is_string($xml)) {
  267. //nothing to do
  268. } else if ($xml instanceof DOMDocument) {
  269. $xml = $xml->saveXml();
  270. } else if ($xml instanceof SimpleXMLElement) {
  271. $xml = $xml->asXML();
  272. } else {
  273. require_once 'Zend/Amf/Exception.php';
  274. throw new Zend_Amf_Exception('Invalid xml specified; must be a DOMDocument or SimpleXMLElement');
  275. }
  276. $this->writeBinaryString($xml);
  277. return $this;
  278. }
  279. /**
  280. * Convert DateTime/Zend_Date to AMF date
  281. *
  282. * @param DateTime|Zend_Date $date
  283. * @return Zend_Amf_Parse_Amf3_Serializer
  284. */
  285. public function writeDate($date)
  286. {
  287. if ($this->writeObjectReference($date)) {
  288. return $this;
  289. }
  290. if ($date instanceof DateTime) {
  291. $dateString = $date->format('U') * 1000;
  292. } elseif ($date instanceof Zend_Date) {
  293. $dateString = $date->toString('U') * 1000;
  294. } else {
  295. require_once 'Zend/Amf/Exception.php';
  296. throw new Zend_Amf_Exception('Invalid date specified; must be a string DateTime or Zend_Date object');
  297. }
  298. $this->writeInteger(0x01);
  299. // write time to stream minus milliseconds
  300. $this->_stream->writeDouble($dateString);
  301. return $this;
  302. }
  303. /**
  304. * Write a PHP array back to the amf output stream
  305. *
  306. * @param array $array
  307. * @return Zend_Amf_Parse_Amf3_Serializer
  308. */
  309. public function writeArray(&$array)
  310. {
  311. // arrays aren't reference here but still counted
  312. $this->_referenceObjects[] = $array;
  313. // have to seperate mixed from numberic keys.
  314. $numeric = array();
  315. $string = array();
  316. foreach ($array as $key => &$value) {
  317. if (is_int($key)) {
  318. $numeric[] = $value;
  319. } else {
  320. $string[$key] = $value;
  321. }
  322. }
  323. // write the preamble id of the array
  324. $length = count($numeric);
  325. $id = ($length << 1) | 0x01;
  326. $this->writeInteger($id);
  327. //Write the mixed type array to the output stream
  328. foreach($string as $key => &$value) {
  329. $this->writeString($key)
  330. ->writeTypeMarker($value);
  331. }
  332. $this->writeString($this->_strEmpty);
  333. // Write the numeric array to ouput stream
  334. foreach($numeric as &$value) {
  335. $this->writeTypeMarker($value);
  336. }
  337. return $this;
  338. }
  339. /**
  340. * Check if the given object is in the reference table, write the reference if it exists,
  341. * otherwise add the object to the reference table
  342. *
  343. * @param mixed $object object reference to check for reference
  344. * @param mixed $objectByVal object to check for reference
  345. * @return Boolean true, if the reference was written, false otherwise
  346. */
  347. protected function writeObjectReference(&$object, $objectByVal = false)
  348. {
  349. // Workaround for PHP5 with E_STRICT enabled complaining about "Only
  350. // variables should be passed by reference"
  351. if ((null === $object) && ($objectByVal !== false)) {
  352. $object = &$objectByVal;
  353. }
  354. $hash = spl_object_hash($object);
  355. $ref = array_key_exists($hash, $this->_referenceObjects)
  356. ? $this->_referenceObjects[$hash]
  357. : false;
  358. // quickly handle object references
  359. if ($ref !== false){
  360. $ref <<= 1;
  361. $this->writeInteger($ref);
  362. return true;
  363. }
  364. $this->_referenceObjects[$hash] = count($this->_referenceObjects);
  365. return false;
  366. }
  367. /**
  368. * Write object to ouput stream
  369. *
  370. * @param mixed $data
  371. * @return Zend_Amf_Parse_Amf3_Serializer
  372. */
  373. public function writeObject($object)
  374. {
  375. if($this->writeObjectReference($object)){
  376. return $this;
  377. }
  378. $className = '';
  379. //Check to see if the object is a typed object and we need to change
  380. switch (true) {
  381. // the return class mapped name back to actionscript class name.
  382. case ($className = Zend_Amf_Parse_TypeLoader::getMappedClassName(get_class($object))):
  383. break;
  384. // Check to see if the user has defined an explicit Action Script type.
  385. case isset($object->_explicitType):
  386. $className = $object->_explicitType;
  387. break;
  388. // Check if user has defined a method for accessing the Action Script type
  389. case method_exists($object, 'getASClassName'):
  390. $className = $object->getASClassName();
  391. break;
  392. // No return class name is set make it a generic object
  393. case ($object instanceof stdClass):
  394. $className = '';
  395. break;
  396. // By default, use object's class name
  397. default:
  398. $className = get_class($object);
  399. break;
  400. }
  401. $writeTraits = true;
  402. //check to see, if we have a corresponding definition
  403. if(array_key_exists($className, $this->_referenceDefinitions)){
  404. $traitsInfo = $this->_referenceDefinitions[$className]['id'];
  405. $encoding = $this->_referenceDefinitions[$className]['encoding'];
  406. $propertyNames = $this->_referenceDefinitions[$className]['propertyNames'];
  407. $traitsInfo = ($traitsInfo << 2) | 0x01;
  408. $writeTraits = false;
  409. } else {
  410. $propertyNames = array();
  411. if($className == ''){
  412. //if there is no className, we interpret the class as dynamic without any sealed members
  413. $encoding = Zend_Amf_Constants::ET_DYNAMIC;
  414. } else {
  415. $encoding = Zend_Amf_Constants::ET_PROPLIST;
  416. foreach($object as $key => $value) {
  417. if( $key[0] != "_") {
  418. $propertyNames[] = $key;
  419. }
  420. }
  421. }
  422. $this->_referenceDefinitions[$className] = array(
  423. 'id' => count($this->_referenceDefinitions),
  424. 'encoding' => $encoding,
  425. 'propertyNames' => $propertyNames,
  426. );
  427. $traitsInfo = Zend_Amf_Constants::AMF3_OBJECT_ENCODING;
  428. $traitsInfo |= $encoding << 2;
  429. $traitsInfo |= (count($propertyNames) << 4);
  430. }
  431. $this->writeInteger($traitsInfo);
  432. if($writeTraits){
  433. $this->writeString($className);
  434. foreach ($propertyNames as $value) {
  435. $this->writeString($value);
  436. }
  437. }
  438. try {
  439. switch($encoding) {
  440. case Zend_Amf_Constants::ET_PROPLIST:
  441. //Write the sealed values to the output stream.
  442. foreach ($propertyNames as $key) {
  443. $this->writeTypeMarker($object->$key);
  444. }
  445. break;
  446. case Zend_Amf_Constants::ET_DYNAMIC:
  447. //Write the sealed values to the output stream.
  448. foreach ($propertyNames as $key) {
  449. $this->writeTypeMarker($object->$key);
  450. }
  451. //Write remaining properties
  452. foreach($object as $key => $value){
  453. if(!in_array($key,$propertyNames) && $key[0] != "_"){
  454. $this->writeString($key);
  455. $this->writeTypeMarker($value);
  456. }
  457. }
  458. //Write an empty string to end the dynamic part
  459. $this->writeString($this->_strEmpty);
  460. break;
  461. case Zend_Amf_Constants::ET_EXTERNAL:
  462. require_once 'Zend/Amf/Exception.php';
  463. throw new Zend_Amf_Exception('External Object Encoding not implemented');
  464. break;
  465. default:
  466. require_once 'Zend/Amf/Exception.php';
  467. throw new Zend_Amf_Exception('Unknown Object Encoding type: ' . $encoding);
  468. }
  469. } catch (Exception $e) {
  470. require_once 'Zend/Amf/Exception.php';
  471. throw new Zend_Amf_Exception('Unable to writeObject output: ' . $e->getMessage(), 0, $e);
  472. }
  473. return $this;
  474. }
  475. }