import v1.0.0-RC4 | 2009-05-20
This commit is contained in:
321
libs/Zend/Amf/Parse/Amf0/Deserializer.php
Normal file
321
libs/Zend/Amf/Parse/Amf0/Deserializer.php
Normal file
@ -0,0 +1,321 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Amf
|
||||
* @subpackage Parse_Amf0
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/** Zend_Amf_Parse_Deserializer */
|
||||
require_once 'Zend/Amf/Parse/Deserializer.php';
|
||||
|
||||
/**
|
||||
* Read an AMF0 input stream and convert it into PHP data types
|
||||
*
|
||||
* @todo Implement Typed Object Class Mapping
|
||||
* @todo Class could be implmented as Factory Class with each data type it's own class
|
||||
* @package Zend_Amf
|
||||
* @subpackage Parse_Amf0
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Amf_Parse_Amf0_Deserializer extends Zend_Amf_Parse_Deserializer
|
||||
{
|
||||
/**
|
||||
* An array of objects used for recursivly deserializing an object.
|
||||
* @var array
|
||||
*/
|
||||
protected $_reference = array();
|
||||
|
||||
/**
|
||||
* If AMF3 serialization occurs, update to AMF0 0x03
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $_objectEncoding = Zend_Amf_Constants::AMF0_OBJECT_ENCODING;
|
||||
|
||||
/**
|
||||
* refrence to AMF3 deserializer
|
||||
*
|
||||
* @var Zend_Amf_Parse_Amf3_Deserializer
|
||||
*/
|
||||
protected $_deserializer = null;
|
||||
|
||||
/**
|
||||
* Read AMF markers and dispatch for deserialization
|
||||
*
|
||||
* Checks for AMF marker types and calls the appropriate methods
|
||||
* for deserializing those marker types. Markers are the data type of
|
||||
* the following value.
|
||||
*
|
||||
* @param integer $typeMarker
|
||||
* @return mixed whatever the data type is of the marker in php
|
||||
* @return mixed
|
||||
* @throws Zend_Amf_Exception for invalid type
|
||||
*/
|
||||
public function readTypeMarker($typeMarker = null)
|
||||
{
|
||||
if (is_null($typeMarker)) {
|
||||
$typeMarker = $this->_stream->readByte();
|
||||
}
|
||||
|
||||
switch($typeMarker) {
|
||||
// number
|
||||
case Zend_Amf_Constants::AMF0_NUMBER:
|
||||
return $this->_stream->readDouble();
|
||||
|
||||
// boolean
|
||||
case Zend_Amf_Constants::AMF0_BOOLEAN:
|
||||
return (boolean) $this->_stream->readByte();
|
||||
|
||||
// string
|
||||
case Zend_Amf_Constants::AMF0_STRING:
|
||||
return $this->_stream->readUTF();
|
||||
|
||||
// object
|
||||
case Zend_Amf_Constants::AMF0_OBJECT:
|
||||
return $this->readObject();
|
||||
|
||||
// null
|
||||
case Zend_Amf_Constants::AMF0_NULL:
|
||||
return null;
|
||||
|
||||
// undefined
|
||||
case Zend_Amf_Constants::AMF0_UNDEFINED:
|
||||
return null;
|
||||
|
||||
// Circular references are returned here
|
||||
case Zend_Amf_Constants::AMF0_REFERENCE:
|
||||
return $this->readReference();
|
||||
|
||||
// mixed array with numeric and string keys
|
||||
case Zend_Amf_Constants::AMF0_MIXEDARRAY:
|
||||
return $this->readMixedArray();
|
||||
|
||||
// array
|
||||
case Zend_Amf_Constants::AMF0_ARRAY:
|
||||
return $this->readArray();
|
||||
|
||||
// date
|
||||
case Zend_Amf_Constants::AMF0_DATE:
|
||||
return $this->readDate();
|
||||
|
||||
// longString strlen(string) > 2^16
|
||||
case Zend_Amf_Constants::AMF0_LONGSTRING:
|
||||
return $this->_stream->readLongUTF();
|
||||
|
||||
//internal AS object, not supported
|
||||
case Zend_Amf_Constants::AMF0_UNSUPPORTED:
|
||||
return null;
|
||||
|
||||
// XML
|
||||
case Zend_Amf_Constants::AMF0_XML:
|
||||
return $this->readXmlString();
|
||||
|
||||
// typed object ie Custom Class
|
||||
case Zend_Amf_Constants::AMF0_TYPEDOBJECT:
|
||||
return $this->readTypedObject();
|
||||
|
||||
//AMF3-specific
|
||||
case Zend_Amf_Constants::AMF0_AMF3:
|
||||
return $this->readAmf3TypeMarker();
|
||||
|
||||
default:
|
||||
require_once 'Zend/Amf/Exception.php';
|
||||
throw new Zend_Amf_Exception('Unsupported marker type: ' . $typeMarker);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read AMF objects and convert to PHP objects
|
||||
*
|
||||
* Read the name value pair objects form the php message and convert them to
|
||||
* a php object class.
|
||||
*
|
||||
* Called when the marker type is 3.
|
||||
*
|
||||
* @param array|null $object
|
||||
* @return object
|
||||
*/
|
||||
public function readObject($object = null)
|
||||
{
|
||||
if (is_null($object)) {
|
||||
$object = array();
|
||||
}
|
||||
|
||||
while (true) {
|
||||
$key = $this->_stream->readUTF();
|
||||
$typeMarker = $this->_stream->readByte();
|
||||
if ($typeMarker != Zend_Amf_Constants::AMF0_OBJECTTERM ){
|
||||
//Recursivly call readTypeMarker to get the types of properties in the object
|
||||
$object[$key] = $this->readTypeMarker($typeMarker);
|
||||
} else {
|
||||
//encountered AMF object terminator
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->_reference[] = $object;
|
||||
return (object) $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read reference objects
|
||||
*
|
||||
* Used to gain access to the private array of refrence objects.
|
||||
* Called when marker type is 7.
|
||||
*
|
||||
* @return object
|
||||
* @throws Zend_Amf_Exception for invalid reference keys
|
||||
*/
|
||||
public function readReference()
|
||||
{
|
||||
$key = $this->_stream->readInt();
|
||||
if (!array_key_exists($key, $this->_reference)) {
|
||||
require_once 'Zend/Amf/Exception.php';
|
||||
throw new Zend_Amf_Exception('Invalid reference key: '. $key);
|
||||
}
|
||||
return $this->_reference[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads an array with numeric and string indexes.
|
||||
*
|
||||
* Called when marker type is 8
|
||||
*
|
||||
* @todo As of Flash Player 9 there is not support for mixed typed arrays
|
||||
* so we handle this as an object. With the introduction of vectors
|
||||
* in Flash Player 10 this may need to be reconsidered.
|
||||
* @return array
|
||||
*/
|
||||
public function readMixedArray()
|
||||
{
|
||||
$length = $this->_stream->readLong();
|
||||
return $this->readObject();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts numberically indexed actiosncript arrays into php arrays.
|
||||
*
|
||||
* Called when marker type is 10
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function readArray()
|
||||
{
|
||||
$length = $this->_stream->readLong();
|
||||
$array = array();
|
||||
while ($length--) {
|
||||
$array[] = $this->readTypeMarker();
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert AS Date to Zend_Date
|
||||
*
|
||||
* @return Zend_Date
|
||||
*/
|
||||
public function readDate()
|
||||
{
|
||||
// get the unix time stamp. Not sure why ActionScript does not use
|
||||
// milliseconds
|
||||
$timestamp = floor($this->_stream->readDouble() / 1000);
|
||||
|
||||
// The timezone offset is never returned to the server; it is always 0,
|
||||
// so read and ignore.
|
||||
$offset = $this->_stream->readInt();
|
||||
|
||||
require_once 'Zend/Date.php';
|
||||
$date = new Zend_Date($timestamp);
|
||||
return $date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert XML to SimpleXml
|
||||
* If user wants DomDocument they can use dom_import_simplexml
|
||||
*
|
||||
* @return SimpleXml Object
|
||||
*/
|
||||
public function readXmlString()
|
||||
{
|
||||
$string = $this->_stream->readLongUTF();
|
||||
return simplexml_load_string($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read Class that is to be mapped to a server class.
|
||||
*
|
||||
* Commonly used for Value Objects on the server
|
||||
*
|
||||
* @todo implement Typed Class mapping
|
||||
* @return object
|
||||
* @throws Zend_Amf_Exception if unable to load type
|
||||
*/
|
||||
public function readTypedObject()
|
||||
{
|
||||
require_once 'Zend/Amf/Parse/TypeLoader.php';
|
||||
// get the remote class name
|
||||
$className = $this->_stream->readUTF();
|
||||
$loader = Zend_Amf_Parse_TypeLoader::loadType($className);
|
||||
$returnObject = new $loader();
|
||||
$properties = get_object_vars($this->readObject());
|
||||
foreach($properties as $key=>$value) {
|
||||
if($key) {
|
||||
$returnObject->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $returnObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* AMF3 data type encountered load AMF3 Deserializer to handle
|
||||
* type markers.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function readAmf3TypeMarker()
|
||||
{
|
||||
$deserializer = $this->getDeserializer();
|
||||
$this->_objectEncoding = Zend_Amf_Constants::AMF3_OBJECT_ENCODING;
|
||||
return $deserializer->readTypeMarker();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the object encoding to check if an AMF3 object
|
||||
* is going to be return.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getObjectEncoding()
|
||||
{
|
||||
return $this->_objectEncoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get deserializer
|
||||
*
|
||||
* @return Zend_Amf_Parse_Amf3_Deserializer
|
||||
*/
|
||||
public function getDeserializer()
|
||||
{
|
||||
if (null === $this->_deserializer) {
|
||||
require_once 'Zend/Amf/Parse/Amf3/Deserializer.php';
|
||||
$this->_deserializer = new Zend_Amf_Parse_Amf3_Deserializer($this->_stream);
|
||||
}
|
||||
return $this->_deserializer;
|
||||
}
|
||||
}
|
289
libs/Zend/Amf/Parse/Amf0/Serializer.php
Normal file
289
libs/Zend/Amf/Parse/Amf0/Serializer.php
Normal file
@ -0,0 +1,289 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Amf
|
||||
* @subpackage Parse_Amf0
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/** Zend_Amf_Parse_Serializer */
|
||||
require_once 'Zend/Amf/Parse/Serializer.php';
|
||||
|
||||
/**
|
||||
* Serializer php misc types back to there corresponding AMF0 Type Marker.
|
||||
*
|
||||
* @uses Zend_Amf_Parse_Serializer
|
||||
* @package Zend_Amf
|
||||
* @subpackage Parse_Amf0
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Amf_Parse_Amf0_Serializer extends Zend_Amf_Parse_Serializer
|
||||
{
|
||||
/**
|
||||
* @var string Name of the class to be returned
|
||||
*/
|
||||
protected $_className = '';
|
||||
|
||||
/**
|
||||
* Determine type and serialize accordingly
|
||||
*
|
||||
* Checks to see if the type was declared and then either
|
||||
* auto negotiates the type or relies on the user defined markerType to
|
||||
* serialize the data into amf
|
||||
*
|
||||
* @param misc $data
|
||||
* @param misc $markerType
|
||||
* @return Zend_Amf_Parse_Amf0_Serializer
|
||||
* @throws Zend_Amf_Exception for unrecognized types or data
|
||||
*/
|
||||
public function writeTypeMarker($data, $markerType = null)
|
||||
{
|
||||
if (null !== $markerType) {
|
||||
// Write the Type Marker to denote the following action script data type
|
||||
$this->_stream->writeByte($markerType);
|
||||
switch($markerType) {
|
||||
case Zend_Amf_Constants::AMF0_NUMBER:
|
||||
$this->_stream->writeDouble($data);
|
||||
break;
|
||||
case Zend_Amf_Constants::AMF0_BOOLEAN:
|
||||
$this->_stream->writeByte($data);
|
||||
break;
|
||||
case Zend_Amf_Constants::AMF0_STRING:
|
||||
$this->_stream->writeUTF($data);
|
||||
break;
|
||||
case Zend_Amf_Constants::AMF0_OBJECT:
|
||||
$this->writeObject($data);
|
||||
break;
|
||||
case Zend_Amf_Constants::AMF0_NULL:
|
||||
break;
|
||||
case Zend_Amf_Constants::AMF0_MIXEDARRAY:
|
||||
// Write length of numeric keys as zero.
|
||||
$this->_stream->writeLong(0);
|
||||
$this->writeObject($data);
|
||||
break;
|
||||
case Zend_Amf_Constants::AMF0_ARRAY:
|
||||
$this->writeArray($data);
|
||||
break;
|
||||
case Zend_Amf_Constants::AMF0_DATE:
|
||||
$this->writeDate($data);
|
||||
break;
|
||||
case Zend_Amf_Constants::AMF0_LONGSTRING:
|
||||
$this->_stream->writeLongUTF($data);
|
||||
break;
|
||||
case Zend_Amf_Constants::AMF0_TYPEDOBJECT:
|
||||
$this->writeTypedObject($data);
|
||||
break;
|
||||
case Zend_Amf_Constants::AMF0_AMF3:
|
||||
$this->writeAmf3TypeMarker($data);
|
||||
break;
|
||||
default:
|
||||
require_once 'Zend/Amf/Exception.php';
|
||||
throw new Zend_Amf_Exception("Unknown Type Marker: " . $markerType);
|
||||
}
|
||||
} else {
|
||||
switch (true) {
|
||||
case (is_int($data) || is_float($data)):
|
||||
$markerType = Zend_Amf_Constants::AMF0_NUMBER;
|
||||
break;
|
||||
case (is_bool($data)):
|
||||
$markerType = Zend_Amf_Constants::AMF0_BOOLEAN;
|
||||
break;
|
||||
case (is_string($data) && (strlen($data) > 65536)):
|
||||
$markerType = Zend_Amf_Constants::AMF0_LONGSTRING;
|
||||
break;
|
||||
case (is_string($data)):
|
||||
$markerType = Zend_Amf_Constants::AMF0_STRING;
|
||||
break;
|
||||
case (is_object($data)):
|
||||
if (($data instanceof DateTime) || ($data instanceof Zend_Date)) {
|
||||
$markerType = Zend_Amf_Constants::AMF0_DATE;
|
||||
} else {
|
||||
|
||||
if($className = $this->getClassName($data)){
|
||||
//Object is a Typed object set classname
|
||||
$markerType = Zend_Amf_Constants::AMF0_TYPEDOBJECT;
|
||||
$this->_className = $className;
|
||||
} else {
|
||||
// Object is a generic classname
|
||||
$markerType = Zend_Amf_Constants::AMF0_OBJECT;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case (null === $data):
|
||||
$markerType = Zend_Amf_Constants::AMF0_NULL;
|
||||
break;
|
||||
case (is_array($data)):
|
||||
// check if it is a mixed typed array
|
||||
foreach (array_keys($data) as $key) {
|
||||
if (!is_numeric($key)) {
|
||||
$markerType = Zend_Amf_Constants::AMF0_MIXEDARRAY;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Dealing with a standard numeric array
|
||||
if(!$markerType){
|
||||
$markerType = Zend_Amf_Constants::AMF0_ARRAY;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
require_once 'Zend/Amf/Exception.php';
|
||||
throw new Zend_Amf_Exception('Unsupported data type: ' . gettype($data));
|
||||
}
|
||||
|
||||
$this->writeTypeMarker($data, $markerType);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a php array with string or mixed keys.
|
||||
*
|
||||
* @param object $data
|
||||
* @return Zend_Amf_Parse_Amf0_Serializer
|
||||
*/
|
||||
public function writeObject($object)
|
||||
{
|
||||
// Loop each element and write the name of the property.
|
||||
foreach ($object as $key => $value) {
|
||||
$this->_stream->writeUTF($key);
|
||||
$this->writeTypeMarker($value);
|
||||
}
|
||||
|
||||
// Write the end object flag
|
||||
$this->_stream->writeInt(0);
|
||||
$this->_stream->writeByte(Zend_Amf_Constants::AMF0_OBJECTTERM);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a standard numeric array to the output stream. If a mixed array
|
||||
* is encountered call writeTypeMarker with mixed array.
|
||||
*
|
||||
* @param array $array
|
||||
* @return Zend_Amf_Parse_Amf0_Serializer
|
||||
*/
|
||||
public function writeArray($array)
|
||||
{
|
||||
$length = count($array);
|
||||
if (!$length < 0) {
|
||||
// write the length of the array
|
||||
$this->_stream->writeLong(0);
|
||||
} else {
|
||||
// Write the length of the numberic array
|
||||
$this->_stream->writeLong($length);
|
||||
for ($i=0; $i<$length; $i++) {
|
||||
$value = isset($array[$i]) ? $array[$i] : null;
|
||||
$this->writeTypeMarker($value);
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the DateTime into an AMF Date
|
||||
*
|
||||
* @param DateTime|Zend_Date $data
|
||||
* @return Zend_Amf_Parse_Amf0_Serializer
|
||||
*/
|
||||
public function writeDate($data)
|
||||
{
|
||||
if ($data instanceof DateTime) {
|
||||
$dateString = $data->format('U');
|
||||
} elseif ($data instanceof Zend_Date) {
|
||||
$dateString = $data->toString('U');
|
||||
} else {
|
||||
require_once 'Zend/Amf/Exception.php';
|
||||
throw new Zend_Amf_Exception('Invalid date specified; must be a DateTime or Zend_Date object');
|
||||
}
|
||||
$dateString *= 1000;
|
||||
|
||||
// Make the conversion and remove milliseconds.
|
||||
$this->_stream->writeDouble($dateString);
|
||||
|
||||
// Flash does not respect timezone but requires it.
|
||||
$this->_stream->writeInt(0);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a class mapped object to the output stream.
|
||||
*
|
||||
* @param object $data
|
||||
* @return Zend_Amf_Parse_Amf0_Serializer
|
||||
*/
|
||||
public function writeTypedObject($data)
|
||||
{
|
||||
$this->_stream->writeUTF($this->_className);
|
||||
$this->writeObject($data);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encountered and AMF3 Type Marker use AMF3 serializer. Once AMF3 is
|
||||
* enountered it will not return to AMf0.
|
||||
*
|
||||
* @param string $data
|
||||
* @return Zend_Amf_Parse_Amf0_Serializer
|
||||
*/
|
||||
public function writeAmf3TypeMarker($data)
|
||||
{
|
||||
require_once 'Zend/Amf/Parse/Amf3/Serializer.php';
|
||||
$serializer = new Zend_Amf_Parse_Amf3_Serializer($this->_stream);
|
||||
$serializer->writeTypeMarker($data);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find if the class name is a class mapped name and return the
|
||||
* respective classname if it is.
|
||||
*
|
||||
* @param object $object
|
||||
* @return false|string $className
|
||||
*/
|
||||
protected function getClassName($object)
|
||||
{
|
||||
require_once 'Zend/Amf/Parse/TypeLoader.php';
|
||||
//Check to see if the object is a typed object and we need to change
|
||||
$className = '';
|
||||
switch (true) {
|
||||
// the return class mapped name back to actionscript class name.
|
||||
case Zend_Amf_Parse_TypeLoader::getMappedClassName(get_class($object)):
|
||||
$className = Zend_Amf_Parse_TypeLoader::getMappedClassName(get_class($object));
|
||||
break;
|
||||
// Check to see if the user has defined an explicit Action Script type.
|
||||
case isset($object->_explicitType):
|
||||
$className = $object->_explicitType;
|
||||
unset($object->_explicitType);
|
||||
break;
|
||||
// Check if user has defined a method for accessing the Action Script type
|
||||
case method_exists($object, 'getASClassName'):
|
||||
$className = $object->getASClassName();
|
||||
break;
|
||||
// No return class name is set make it a generic object
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if(!$className == '') {
|
||||
return $className;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
410
libs/Zend/Amf/Parse/Amf3/Deserializer.php
Normal file
410
libs/Zend/Amf/Parse/Amf3/Deserializer.php
Normal file
@ -0,0 +1,410 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Amf
|
||||
* @subpackage Parse_Amf3
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/** Zend_Amf_Parse_Deserializer */
|
||||
require_once 'Zend/Amf/Parse/Deserializer.php';
|
||||
|
||||
/** Zend_Amf_Parse_TypeLoader */
|
||||
require_once 'Zend/Amf/Parse/TypeLoader.php';
|
||||
|
||||
/**
|
||||
* Read an AMF3 input stream and convert it into PHP data types.
|
||||
*
|
||||
* @todo readObject to handle Typed Objects
|
||||
* @todo readXMLStrimg to be implemented.
|
||||
* @todo Class could be implmented as Factory Class with each data type it's own class.
|
||||
* @package Zend_Amf
|
||||
* @subpackage Parse_Amf3
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Amf_Parse_Amf3_Deserializer extends Zend_Amf_Parse_Deserializer
|
||||
{
|
||||
/**
|
||||
* Total number of objects in the referenceObject array
|
||||
* @var int
|
||||
*/
|
||||
protected $_objectCount;
|
||||
|
||||
/**
|
||||
* An array of reference objects per amf body
|
||||
* @var array
|
||||
*/
|
||||
protected $_referenceObjects = array();
|
||||
|
||||
/**
|
||||
* An array of reference strings per amf body
|
||||
* @var array
|
||||
*/
|
||||
protected $_referenceStrings = array();
|
||||
|
||||
/**
|
||||
* An array of reference class definitions per body
|
||||
* @var array
|
||||
*/
|
||||
protected $_referenceDefinitions = array();
|
||||
|
||||
/**
|
||||
* Read AMF markers and dispatch for deserialization
|
||||
*
|
||||
* Checks for AMF marker types and calls the appropriate methods
|
||||
* for deserializing those marker types. markers are the data type of
|
||||
* the following value.
|
||||
*
|
||||
* @param integer $typeMarker
|
||||
* @return mixed Whatever the corresponding PHP data type is
|
||||
* @throws Zend_Amf_Exception for unidentified marker type
|
||||
*/
|
||||
public function readTypeMarker($typeMarker = null)
|
||||
{
|
||||
if(null === $typeMarker) {
|
||||
$typeMarker = $this->_stream->readByte();
|
||||
}
|
||||
|
||||
switch($typeMarker) {
|
||||
case Zend_Amf_Constants::AMF3_UNDEFINED:
|
||||
return null;
|
||||
case Zend_Amf_Constants::AMF3_NULL:
|
||||
return null;
|
||||
case Zend_Amf_Constants::AMF3_BOOLEAN_FALSE:
|
||||
return false;
|
||||
case Zend_Amf_Constants::AMF3_BOOLEAN_TRUE:
|
||||
return true;
|
||||
case Zend_Amf_Constants::AMF3_INTEGER:
|
||||
return $this->readInteger();
|
||||
case Zend_Amf_Constants::AMF3_NUMBER:
|
||||
return $this->_stream->readDouble();
|
||||
case Zend_Amf_Constants::AMF3_STRING:
|
||||
return $this->readString();
|
||||
case Zend_Amf_Constants::AMF3_DATE:
|
||||
return $this->readDate();
|
||||
case Zend_Amf_Constants::AMF3_ARRAY:
|
||||
return $this->readArray();
|
||||
case Zend_Amf_Constants::AMF3_OBJECT:
|
||||
return $this->readObject();
|
||||
case Zend_Amf_Constants::AMF3_XML:
|
||||
case Zend_Amf_Constants::AMF3_XMLSTRING:
|
||||
return $this->readXmlString();
|
||||
case Zend_Amf_Constants::AMF3_BYTEARRAY:
|
||||
return $this->readString();
|
||||
default:
|
||||
require_once 'Zend/Amf/Exception.php';
|
||||
throw new Zend_Amf_Exception('Unsupported type marker: ' . $typeMarker);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read and deserialize an integer
|
||||
*
|
||||
* AMF 3 represents smaller integers with fewer bytes using the most
|
||||
* significant bit of each byte. The worst case uses 32-bits
|
||||
* to represent a 29-bit number, which is what we would have
|
||||
* done with no compression.
|
||||
* - 0x00000000 - 0x0000007F : 0xxxxxxx
|
||||
* - 0x00000080 - 0x00003FFF : 1xxxxxxx 0xxxxxxx
|
||||
* - 0x00004000 - 0x001FFFFF : 1xxxxxxx 1xxxxxxx 0xxxxxxx
|
||||
* - 0x00200000 - 0x3FFFFFFF : 1xxxxxxx 1xxxxxxx 1xxxxxxx xxxxxxxx
|
||||
* - 0x40000000 - 0xFFFFFFFF : throw range exception
|
||||
*
|
||||
*
|
||||
* 0x04 -> integer type code, followed by up to 4 bytes of data.
|
||||
*
|
||||
* @see: Parsing integers on OSFlash {http://osflash.org/amf3/parsing_integers>} for the AMF3 integer data format.
|
||||
* @return int|float
|
||||
*/
|
||||
public function readInteger()
|
||||
{
|
||||
$count = 1;
|
||||
$intReference = $this->_stream->readByte();
|
||||
$result = 0;
|
||||
while ((($intReference & 0x80) != 0) && $count < 4) {
|
||||
$result <<= 7;
|
||||
$result |= ($intReference & 0x7f);
|
||||
$intReference = $this->_stream->readByte();
|
||||
$count++;
|
||||
}
|
||||
if ($count < 4) {
|
||||
$result <<= 7;
|
||||
$result |= $intReference;
|
||||
} else {
|
||||
// Use all 8 bits from the 4th byte
|
||||
$result <<= 8;
|
||||
$result |= $intReference;
|
||||
|
||||
// Check if the integer should be negative
|
||||
if (($result & 0x10000000) != 0) {
|
||||
//and extend the sign bit
|
||||
$result |= 0xe0000000;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read and deserialize a string
|
||||
*
|
||||
* Strings can be sent as a reference to a previously
|
||||
* occurring String by using an index to the implicit string reference table.
|
||||
* Strings are encoding using UTF-8 - however the header may either
|
||||
* describe a string literal or a string reference.
|
||||
*
|
||||
* - string = 0×06 string-data
|
||||
* - string-data = integer-data [ modified-utf-8 ]
|
||||
* - modified-utf-8 = *OCTET
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public function readString()
|
||||
{
|
||||
$stringReference = $this->readInteger();
|
||||
|
||||
//Check if this is a reference string
|
||||
if (($stringReference & 0x01) == 0) {
|
||||
// reference string
|
||||
$stringReference = $stringReference >> 1;
|
||||
if ($stringReference >= count($this->_referenceStrings)) {
|
||||
require_once 'Zend/Amf/Exception.php';
|
||||
throw new Zend_Amf_Exception('Undefined string reference: ' . $stringReference);
|
||||
}
|
||||
// reference string found
|
||||
return $this->_referenceStrings[$stringReference];
|
||||
}
|
||||
|
||||
$length = $stringReference >> 1;
|
||||
if ($length) {
|
||||
$string = $this->_stream->readBytes($length);
|
||||
$this->_referenceStrings[] = $string;
|
||||
} else {
|
||||
$string = "";
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read and deserialize a date
|
||||
*
|
||||
* Data is the number of milliseconds elapsed since the epoch
|
||||
* of midnight, 1st Jan 1970 in the UTC time zone.
|
||||
* Local time zone information is not sent to flash.
|
||||
*
|
||||
* - date = 0×08 integer-data [ number-data ]
|
||||
*
|
||||
* @return Zend_Date
|
||||
*/
|
||||
public function readDate()
|
||||
{
|
||||
$dateReference = $this->readInteger();
|
||||
if (($dateReference & 0x01) == 0) {
|
||||
$dateReference = $dateReference >> 1;
|
||||
if ($dateReference>=count($this->_referenceObjects)) {
|
||||
require_once 'Zend/Amf/Exception.php';
|
||||
throw new Zend_Amf_Exception('Undefined date reference: ' . $dateReference);
|
||||
}
|
||||
return $this->_referenceObjects[$dateReference];
|
||||
}
|
||||
|
||||
$timestamp = floor($this->_stream->readDouble() / 1000);
|
||||
|
||||
require_once 'Zend/Date.php';
|
||||
$dateTime = new Zend_Date((int) $timestamp);
|
||||
$this->_referenceObjects[] = $dateTime;
|
||||
return $dateTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read amf array to PHP array
|
||||
*
|
||||
* - array = 0×09 integer-data ( [ 1OCTET *amf3-data ] | [OCTET *amf3-data 1] | [ OCTET *amf-data ] )
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function readArray()
|
||||
{
|
||||
$arrayReference = $this->readInteger();
|
||||
if (($arrayReference & 0x01)==0){
|
||||
$arrayReference = $arrayReference >> 1;
|
||||
if ($arrayReference>=count($this->_referenceObjects)) {
|
||||
require_once 'Zend/Amf/Exception.php';
|
||||
throw new Zend_Amf_Exception('Unknow array reference: ' . $arrayReference);
|
||||
}
|
||||
return $this->_referenceObjects[$arrayReference];
|
||||
}
|
||||
|
||||
// Create a holder for the array in the reference list
|
||||
$data = array();
|
||||
$this->_referenceObjects[] &= $data;
|
||||
$key = $this->readString();
|
||||
|
||||
// Iterating for string based keys.
|
||||
while ($key != '') {
|
||||
$data[$key] = $this->readTypeMarker();
|
||||
$key = $this->readString();
|
||||
}
|
||||
|
||||
$arrayReference = $arrayReference >>1;
|
||||
|
||||
//We have a dense array
|
||||
for ($i=0; $i < $arrayReference; $i++) {
|
||||
$data[] = $this->readTypeMarker();
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read an object from the AMF stream and convert it into a PHP object
|
||||
*
|
||||
* @todo Rather than using an array of traitsInfo create Zend_Amf_Value_TraitsInfo
|
||||
* @return object
|
||||
*/
|
||||
public function readObject()
|
||||
{
|
||||
$traitsInfo = $this->readInteger();
|
||||
$storedObject = ($traitsInfo & 0x01)==0;
|
||||
$traitsInfo = $traitsInfo >> 1;
|
||||
|
||||
// Check if the Object is in the stored Objects reference table
|
||||
if ($storedObject) {
|
||||
$ref = $traitsInfo;
|
||||
if (!isset($this->_referenceObjects[$ref])) {
|
||||
require_once 'Zend/Amf/Exception.php';
|
||||
throw new Zend_Amf_Exception('Unknown Object reference: ' . $ref);
|
||||
}
|
||||
$returnObject = $this->_referenceObjects[$ref];
|
||||
} else {
|
||||
// Check if the Object is in the stored Definistions reference table
|
||||
$storedClass = ($traitsInfo & 0x01) == 0;
|
||||
$traitsInfo = $traitsInfo >> 1;
|
||||
if ($storedClass) {
|
||||
$ref = $traitsInfo;
|
||||
if (!isset($this->_referenceDefinitions[$ref])) {
|
||||
require_once 'Zend/Amf/Exception.php';
|
||||
throw new Zend_Amf_Exception('Unknows Definition reference: '. $ref);
|
||||
}
|
||||
// Populate the reference attributes
|
||||
$className = $this->_referenceDefinitions[$ref]['className'];
|
||||
$encoding = $this->_referenceDefinitions[$ref]['encoding'];
|
||||
$propertyNames = $this->_referenceDefinitions[$ref]['propertyNames'];
|
||||
} else {
|
||||
// The class was not in the reference tables. Start reading rawdata to build traits.
|
||||
// Create a traits table. Zend_Amf_Value_TraitsInfo would be ideal
|
||||
$className = $this->readString();
|
||||
$encoding = $traitsInfo & 0x03;
|
||||
$propertyNames = array();
|
||||
$traitsInfo = $traitsInfo >> 2;
|
||||
}
|
||||
|
||||
// We now have the object traits defined in variables. Time to go to work:
|
||||
if (!$className) {
|
||||
// No class name generic object
|
||||
$returnObject = new stdClass();
|
||||
} else {
|
||||
// Defined object
|
||||
// Typed object lookup agsinst registered classname maps
|
||||
if ($loader = Zend_Amf_Parse_TypeLoader::loadType($className)) {
|
||||
$returnObject = new $loader();
|
||||
} else {
|
||||
//user defined typed object
|
||||
require_once 'Zend/Amf/Exception.php';
|
||||
throw new Zend_Amf_Exception('Typed object not found: '. $className . ' ');
|
||||
}
|
||||
}
|
||||
|
||||
// Add the Object ot the reference table
|
||||
$this->_referenceObjects[] = $returnObject;
|
||||
|
||||
// Check encoding types for additional processing.
|
||||
switch ($encoding) {
|
||||
case (Zend_Amf_Constants::ET_EXTERNAL):
|
||||
// Externalizable object such as {ArrayCollection} and {ObjectProxy}
|
||||
if (!$storedClass) {
|
||||
$this->_referenceDefinitions[] = array(
|
||||
'className' => $className,
|
||||
'encoding' => $encoding,
|
||||
'propertyNames' => $propertyNames,
|
||||
);
|
||||
}
|
||||
$returnObject->externalizedData = $this->readTypeMarker();
|
||||
break;
|
||||
case (Zend_Amf_Constants::ET_DYNAMIC):
|
||||
// used for Name-value encoding
|
||||
if (!$storedClass) {
|
||||
$this->_referenceDefinitions[] = array(
|
||||
'className' => $className,
|
||||
'encoding' => $encoding,
|
||||
'propertyNames' => $propertyNames,
|
||||
);
|
||||
}
|
||||
// not a refrence object read name value properties from byte stream
|
||||
$properties = array(); // clear value
|
||||
do {
|
||||
$property = $this->readString();
|
||||
if ($property != "") {
|
||||
$propertyNames[] = $property;
|
||||
$properties[$property] = $this->readTypeMarker();
|
||||
}
|
||||
} while ($property !="");
|
||||
break;
|
||||
default:
|
||||
// basic property list object.
|
||||
if (!$storedClass) {
|
||||
$count = $traitsInfo; // Number of properties in the list
|
||||
for($i=0; $i< $count; $i++) {
|
||||
$propertyNames[] = $this->readString();
|
||||
}
|
||||
// Add a refrence to the class.
|
||||
$this->_referenceDefinitions[] = array(
|
||||
'className' => $className,
|
||||
'encoding' => $encoding,
|
||||
'propertyNames' => $propertyNames,
|
||||
);
|
||||
}
|
||||
$properties = array(); // clear value
|
||||
foreach ($propertyNames as $property) {
|
||||
$properties[$property] = $this->readTypeMarker();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// Add properties back to the return object.
|
||||
foreach($properties as $key=>$value) {
|
||||
if($key) {
|
||||
$returnObject->$key = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $returnObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert XML to SimpleXml
|
||||
* If user wants DomDocument they can use dom_import_simplexml
|
||||
*
|
||||
* @return SimpleXml Object
|
||||
*/
|
||||
public function readXmlString()
|
||||
{
|
||||
$xmlReference = $this->readInteger();
|
||||
$length = $xmlReference >> 1;
|
||||
$string = $this->_stream->readBytes($length);
|
||||
return simplexml_load_string($string);
|
||||
}
|
||||
}
|
317
libs/Zend/Amf/Parse/Amf3/Serializer.php
Normal file
317
libs/Zend/Amf/Parse/Amf3/Serializer.php
Normal file
@ -0,0 +1,317 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Amf
|
||||
* @subpackage Parse_Amf3
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/** Zend_Amf_Parse_Serializer */
|
||||
require_once 'Zend/Amf/Parse/Serializer.php';
|
||||
|
||||
/** Zend_Amf_Parse_TypeLoader */
|
||||
require_once 'Zend/Amf/Parse/TypeLoader.php';
|
||||
|
||||
/**
|
||||
* Detect PHP object type and convert it to a corresponding AMF3 object type
|
||||
*
|
||||
* @package Zend_Amf
|
||||
* @subpackage Parse_Amf3
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Amf_Parse_Amf3_Serializer extends Zend_Amf_Parse_Serializer
|
||||
{
|
||||
/**
|
||||
* Serialize PHP types to AMF3 and write to stream
|
||||
*
|
||||
* Checks to see if the type was declared and then either
|
||||
* auto negotiates the type or use the user defined markerType to
|
||||
* serialize the data from php back to AMF3
|
||||
*
|
||||
* @param mixed $content
|
||||
* @param int $markerType
|
||||
* @return void
|
||||
*/
|
||||
public function writeTypeMarker($data, $markerType=null)
|
||||
{
|
||||
if (null !== $markerType) {
|
||||
// Write the Type Marker to denote the following action script data type
|
||||
$this->_stream->writeByte($markerType);
|
||||
|
||||
switch ($markerType) {
|
||||
case Zend_Amf_Constants::AMF3_NULL:
|
||||
break;
|
||||
case Zend_Amf_Constants::AMF3_BOOLEAN_FALSE:
|
||||
break;
|
||||
case Zend_Amf_Constants::AMF3_BOOLEAN_TRUE:
|
||||
break;
|
||||
case Zend_Amf_Constants::AMF3_INTEGER:
|
||||
$this->writeInteger($data);
|
||||
break;
|
||||
case Zend_Amf_Constants::AMF3_NUMBER:
|
||||
$this->_stream->writeDouble($data);
|
||||
break;
|
||||
case Zend_Amf_Constants::AMF3_STRING:
|
||||
$this->writeString($data);
|
||||
break;
|
||||
case Zend_Amf_Constants::AMF3_DATE:
|
||||
$this->writeDate($data);
|
||||
break;
|
||||
case Zend_Amf_Constants::AMF3_ARRAY:
|
||||
$this->writeArray($data);
|
||||
break;
|
||||
case Zend_Amf_Constants::AMF3_OBJECT:
|
||||
$this->writeObject($data);
|
||||
break;
|
||||
case Zend_Amf_Constants::AMF3_BYTEARRAY:
|
||||
$this->writeString($data);
|
||||
break;
|
||||
default:
|
||||
require_once 'Zend/Amf/Exception.php';
|
||||
throw new Zend_Amf_Exception('Unknown Type Marker: ' . $markerType);
|
||||
}
|
||||
} else {
|
||||
// Detect Type Marker
|
||||
switch (true) {
|
||||
case (null === $data):
|
||||
$markerType = Zend_Amf_Constants::AMF3_NULL;
|
||||
break;
|
||||
case (is_bool($data)):
|
||||
if ($data){
|
||||
$markerType = Zend_Amf_Constants::AMF3_BOOLEAN_TRUE;
|
||||
} else {
|
||||
$markerType = Zend_Amf_Constants::AMF3_BOOLEAN_FALSE;
|
||||
}
|
||||
break;
|
||||
case (is_int($data)):
|
||||
if (($data > 0xFFFFFFF) || ($data < -268435456)) {
|
||||
$markerType = Zend_Amf_Constants::AMF3_NUMBER;
|
||||
} else {
|
||||
$markerType = Zend_Amf_Constants::AMF3_INTEGER;
|
||||
}
|
||||
break;
|
||||
case (is_float($data)):
|
||||
$markerType = Zend_Amf_Constants::AMF3_NUMBER;
|
||||
break;
|
||||
case (is_string($data)):
|
||||
$markerType = Zend_Amf_Constants::AMF3_STRING;
|
||||
break;
|
||||
case (is_array($data)):
|
||||
$markerType = Zend_Amf_Constants::AMF3_ARRAY;
|
||||
break;
|
||||
case (is_object($data)):
|
||||
// Handle object types.
|
||||
if (($data instanceof DateTime) || ($data instanceof Zend_Date)) {
|
||||
$markerType = Zend_Amf_Constants::AMF3_DATE;
|
||||
} else if ($data instanceof Zend_Amf_Value_ByteArray) {
|
||||
$markerType = Zend_Amf_Constants::AMF3_BYTEARRAY;
|
||||
} else {
|
||||
$markerType = Zend_Amf_Constants::AMF3_OBJECT;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
require_once 'Zend/Amf/Exception.php';
|
||||
throw new Zend_Amf_Exception('Unsupported data type: ' . gettype($data));
|
||||
}
|
||||
$this->writeTypeMarker($data, $markerType);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an AMF3 integer
|
||||
*
|
||||
* @param int|float $data
|
||||
* @return Zend_Amf_Parse_Amf3_Serializer
|
||||
*/
|
||||
public function writeInteger($int)
|
||||
{
|
||||
if (($int & 0xffffff80) == 0) {
|
||||
$this->_stream->writeByte($int & 0x7f);
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (($int & 0xffffc000) == 0 ) {
|
||||
$this->_stream->writeByte(($int >> 7 ) | 0x80);
|
||||
$this->_stream->writeByte($int & 0x7f);
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (($int & 0xffe00000) == 0) {
|
||||
$this->_stream->writeByte(($int >> 14 ) | 0x80);
|
||||
$this->_stream->writeByte(($int >> 7 ) | 0x80);
|
||||
$this->_stream->writeByte($int & 0x7f);
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->_stream->writeByte(($int >> 22 ) | 0x80);
|
||||
$this->_stream->writeByte(($int >> 15 ) | 0x80);
|
||||
$this->_stream->writeByte(($int >> 8 ) | 0x80);
|
||||
$this->_stream->writeByte($int & 0xff);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send string to output stream
|
||||
*
|
||||
* @param string $string
|
||||
* @return Zend_Amf_Parse_Amf3_Serializer
|
||||
*/
|
||||
public function writeString($string)
|
||||
{
|
||||
$ref = strlen($string) << 1 | 0x01;
|
||||
$this->writeInteger($ref);
|
||||
$this->_stream->writeBytes($string);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert DateTime/Zend_Date to AMF date
|
||||
*
|
||||
* @param DateTime|Zend_Date $date
|
||||
* @return Zend_Amf_Parse_Amf3_Serializer
|
||||
*/
|
||||
public function writeDate($date)
|
||||
{
|
||||
if ($date instanceof DateTime) {
|
||||
$dateString = $date->format('U') * 1000;
|
||||
} elseif ($date instanceof Zend_Date) {
|
||||
$dateString = $date->toString('U') * 1000;
|
||||
} else {
|
||||
require_once 'Zend/Amf/Exception.php';
|
||||
throw new Zend_Amf_Exception('Invalid date specified; must be a string DateTime or Zend_Date object');
|
||||
}
|
||||
|
||||
$this->writeInteger(0x01);
|
||||
// write time to stream minus milliseconds
|
||||
$this->_stream->writeDouble($dateString);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a PHP array back to the amf output stream
|
||||
*
|
||||
* @param array $array
|
||||
* @return Zend_Amf_Parse_Amf3_Serializer
|
||||
*/
|
||||
public function writeArray(array $array)
|
||||
{
|
||||
// have to seperate mixed from numberic keys.
|
||||
$numeric = array();
|
||||
$string = array();
|
||||
foreach ($array as $key => $value) {
|
||||
if (is_int($key)) {
|
||||
$numeric[] = $value;
|
||||
} else {
|
||||
$string[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
// write the preamble id of the array
|
||||
$length = count($numeric);
|
||||
$id = ($length << 1) | 0x01;
|
||||
$this->writeInteger($id);
|
||||
|
||||
//Write the mixed type array to the output stream
|
||||
foreach($string as $key => $value) {
|
||||
$this->writeString($key)
|
||||
->writeTypeMarker($value);
|
||||
}
|
||||
$this->writeString('');
|
||||
|
||||
// Write the numeric array to ouput stream
|
||||
foreach($numeric as $value) {
|
||||
$this->writeTypeMarker($value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write object to ouput stream
|
||||
*
|
||||
* @param mixed $data
|
||||
* @return Zend_Amf_Parse_Amf3_Serializer
|
||||
*/
|
||||
public function writeObject($object)
|
||||
{
|
||||
$encoding = Zend_Amf_Constants::ET_PROPLIST;
|
||||
$className = '';
|
||||
|
||||
//Check to see if the object is a typed object and we need to change
|
||||
switch (true) {
|
||||
// the return class mapped name back to actionscript class name.
|
||||
case ($className = Zend_Amf_Parse_TypeLoader::getMappedClassName(get_class($object))):
|
||||
break;
|
||||
|
||||
// Check to see if the user has defined an explicit Action Script type.
|
||||
case isset($object->_explicitType):
|
||||
$className = $object->_explicitType;
|
||||
unset($object->_explicitType);
|
||||
break;
|
||||
|
||||
// Check if user has defined a method for accessing the Action Script type
|
||||
case method_exists($object, 'getASClassName'):
|
||||
$className = $object->getASClassName();
|
||||
break;
|
||||
|
||||
// No return class name is set make it a generic object
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
$traitsInfo = Zend_Amf_Constants::AMF3_OBJECT_ENCODING;
|
||||
$traitsInfo |= $encoding << 2;
|
||||
try {
|
||||
switch($encoding) {
|
||||
case Zend_Amf_Constants::ET_PROPLIST:
|
||||
$count = 0;
|
||||
foreach($object as $key => $value) {
|
||||
$count++;
|
||||
}
|
||||
$traitsInfo |= ($count << 4);
|
||||
|
||||
// Write the object ID
|
||||
$this->writeInteger($traitsInfo);
|
||||
|
||||
// Write the classname
|
||||
$this->writeString($className);
|
||||
|
||||
// Write the object Key's to the output stream
|
||||
foreach ($object as $key => $value) {
|
||||
$this->writeString($key);
|
||||
}
|
||||
|
||||
//Write the object values to the output stream.
|
||||
foreach ($object as $key => $value) {
|
||||
$this->writeTypeMarker($value);
|
||||
}
|
||||
break;
|
||||
case Zend_Amf_Constants::ET_EXTERNAL:
|
||||
require_once 'Zend/Amf/Exception.php';
|
||||
throw new Zend_Amf_Exception('External Object Encoding not implemented');
|
||||
break;
|
||||
default:
|
||||
require_once 'Zend/Amf/Exception.php';
|
||||
throw new Zend_Amf_Exception('Unknown Object Encoding type: ' . $encoding);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
require_once 'Zend/Amf/Exception.php';
|
||||
throw new Zend_Amf_Exception('Unable to writeObject output: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
64
libs/Zend/Amf/Parse/Deserializer.php
Normal file
64
libs/Zend/Amf/Parse/Deserializer.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Amf
|
||||
* @subpackage Parse
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/**
|
||||
* Abstract cass that all deserializer must implement.
|
||||
*
|
||||
* Logic for deserialization of the AMF envelop is based on resources supplied
|
||||
* by Adobe Blaze DS. For and example of deserialization please review the BlazeDS
|
||||
* source tree.
|
||||
*
|
||||
* @see http://opensource.adobe.com/svn/opensource/blazeds/trunk/modules/core/src/java/flex/messaging/io/amf/
|
||||
* @package Zend_Amf
|
||||
* @subpackage Parse
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
abstract class Zend_Amf_Parse_Deserializer
|
||||
{
|
||||
/**
|
||||
* The raw string that represents the AMF request.
|
||||
*
|
||||
* @var Zend_Amf_Parse_InputStream
|
||||
*/
|
||||
protected $_stream;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Zend_Amf_Parse_InputStream $stream
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Zend_Amf_Parse_InputStream $stream)
|
||||
{
|
||||
$this->_stream = $stream;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for AMF marker types and calls the appropriate methods
|
||||
* for deserializing those marker types. Markers are the data type of
|
||||
* the following value.
|
||||
*
|
||||
* @param int $typeMarker
|
||||
* @return mixed Whatever the data type is of the marker in php
|
||||
*/
|
||||
public abstract function readTypeMarker($markerType = null);
|
||||
}
|
38
libs/Zend/Amf/Parse/InputStream.php
Normal file
38
libs/Zend/Amf/Parse/InputStream.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Amf
|
||||
* @subpackage Parse
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/** Zend_Amf_Util_BinaryStream */
|
||||
require_once 'Zend/Amf/Util/BinaryStream.php';
|
||||
|
||||
/**
|
||||
* InputStream is used to iterate at a binary level through the AMF request.
|
||||
*
|
||||
* InputStream extends BinaryStream as eventually BinaryStream could be placed
|
||||
* outside of Zend_Amf in order to allow other packages to use the class.
|
||||
*
|
||||
* @package Zend_Amf
|
||||
* @subpackage Parse
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Amf_Parse_InputStream extends Zend_Amf_Util_BinaryStream
|
||||
{
|
||||
}
|
48
libs/Zend/Amf/Parse/OutputStream.php
Normal file
48
libs/Zend/Amf/Parse/OutputStream.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Amf
|
||||
* @subpackage Parse
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/** Zend_Amf_Util_BinaryStream */
|
||||
require_once 'Zend/Amf/Util/BinaryStream.php';
|
||||
|
||||
/**
|
||||
* Iterate at a binary level through the AMF response
|
||||
*
|
||||
* OutputStream extends BinaryStream as eventually BinaryStream could be placed
|
||||
* outside of Zend_Amf in order to allow other packages to use the class.
|
||||
*
|
||||
* @uses Zend_Amf_Util_BinaryStream
|
||||
* @package Zend_Amf
|
||||
* @subpackage Parse
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Amf_Parse_OutputStream extends Zend_Amf_Util_BinaryStream
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('');
|
||||
}
|
||||
}
|
58
libs/Zend/Amf/Parse/Serializer.php
Normal file
58
libs/Zend/Amf/Parse/Serializer.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Amf
|
||||
* @subpackage Parse
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/**
|
||||
* Base abstract class for all AMF serializers.
|
||||
*
|
||||
* @package Zend_Amf
|
||||
* @subpackage Parse
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
abstract class Zend_Amf_Parse_Serializer
|
||||
{
|
||||
/**
|
||||
* Refrence to the current output stream being constructed
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_stream;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Zend_Amf_Parse_OutputStream $stream
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Zend_Amf_Parse_OutputStream $stream)
|
||||
{
|
||||
$this->_stream = $stream;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the PHP object type and convert it into an AMF object type
|
||||
*
|
||||
* @param mixed $content
|
||||
* @param int $markerType
|
||||
* @return void
|
||||
*/
|
||||
public abstract function writeTypeMarker($content, $markerType=null);
|
||||
}
|
133
libs/Zend/Amf/Parse/TypeLoader.php
Normal file
133
libs/Zend/Amf/Parse/TypeLoader.php
Normal file
@ -0,0 +1,133 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Amf
|
||||
* @subpackage Parse
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
require_once 'Zend/Amf/Value/Messaging/AcknowledgeMessage.php';
|
||||
require_once 'Zend/Amf/Value/Messaging/AsyncMessage.php';
|
||||
require_once 'Zend/Amf/Value/Messaging/CommandMessage.php';
|
||||
require_once 'Zend/Amf/Value/Messaging/ErrorMessage.php';
|
||||
require_once 'Zend/Amf/Value/Messaging/RemotingMessage.php';
|
||||
|
||||
/**
|
||||
* Loads a local class and executes the instantiation of that class.
|
||||
*
|
||||
* @todo PHP 5.3 can drastically change this class w/ namespace and the new call_user_func w/ namespace
|
||||
* @package Zend_Amf
|
||||
* @subpackage Parse
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
final class Zend_Amf_Parse_TypeLoader
|
||||
{
|
||||
/**
|
||||
* @var string callback class
|
||||
*/
|
||||
public static $callbackClass;
|
||||
|
||||
/**
|
||||
* @var array AMF class map
|
||||
*/
|
||||
public static $classMap = array (
|
||||
'flex.messaging.messages.AcknowledgeMessage' => 'Zend_Amf_Value_Messaging_AcknowledgeMessage',
|
||||
'flex.messaging.messages.ErrorMessage' => 'Zend_Amf_Value_Messaging_AsyncMessage',
|
||||
'flex.messaging.messages.CommandMessage' => 'Zend_Amf_Value_Messaging_CommandMessage',
|
||||
'flex.messaging.messages.ErrorMessage' => 'Zend_Amf_Value_Messaging_ErrorMessage',
|
||||
'flex.messaging.messages.RemotingMessage' => 'Zend_Amf_Value_Messaging_RemotingMessage',
|
||||
);
|
||||
|
||||
/**
|
||||
* @var array Default class map
|
||||
*/
|
||||
protected static $_defaultClassMap = array(
|
||||
'flex.messaging.messages.AcknowledgeMessage' => 'Zend_Amf_Value_Messaging_AcknowledgeMessage',
|
||||
'flex.messaging.messages.ErrorMessage' => 'Zend_Amf_Value_Messaging_AsyncMessage',
|
||||
'flex.messaging.messages.CommandMessage' => 'Zend_Amf_Value_Messaging_CommandMessage',
|
||||
'flex.messaging.messages.ErrorMessage' => 'Zend_Amf_Value_Messaging_ErrorMessage',
|
||||
'flex.messaging.messages.RemotingMessage' => 'Zend_Amf_Value_Messaging_RemotingMessage',
|
||||
);
|
||||
|
||||
/**
|
||||
* Load the mapped class type into a callback.
|
||||
*
|
||||
* @param string $className
|
||||
* @return object|false
|
||||
*/
|
||||
public static function loadType($className)
|
||||
{
|
||||
$class = false;
|
||||
$callBack = false;
|
||||
$class = self::getMappedClassName($className);
|
||||
if (!class_exists($class)) {
|
||||
require_once 'Zend/Amf/Exception.php';
|
||||
throw new Zend_Amf_Exception($className .' mapped class '. $class . ' is not defined');
|
||||
}
|
||||
|
||||
return $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks up the supplied call name to its mapped class name
|
||||
*
|
||||
* @param string $className
|
||||
* @return string
|
||||
*/
|
||||
public static function getMappedClassName($className)
|
||||
{
|
||||
$mappedName = array_search($className, self::$classMap);
|
||||
|
||||
if ($mappedName) {
|
||||
return $mappedName;
|
||||
}
|
||||
|
||||
$mappedName = array_search($className, array_flip(self::$classMap));
|
||||
|
||||
if ($mappedName) {
|
||||
return $mappedName;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Map PHP class names to ActionScript class names
|
||||
*
|
||||
* Allows users to map the class names of there action script classes
|
||||
* to the equivelent php class name. Used in deserialization to load a class
|
||||
* and serialiation to set the class name of the returned object.
|
||||
*
|
||||
* @param string $asClassName
|
||||
* @param string $phpClassName
|
||||
* @return void
|
||||
*/
|
||||
public static function setMapping($asClassName, $phpClassName)
|
||||
{
|
||||
self::$classMap[$asClassName] = $phpClassName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset type map
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function resetMap()
|
||||
{
|
||||
self::$classMap = self::$_defaultClassMap;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user