|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00024 require_once 'Zend/Amf/Constants.php'; 00025 00027 require_once 'Zend/Amf/Parse/Deserializer.php'; 00028 00039 class Zend_Amf_Parse_Amf0_Deserializer extends Zend_Amf_Parse_Deserializer 00040 { 00045 protected $_reference = array(); 00046 00052 protected $_objectEncoding = Zend_Amf_Constants::AMF0_OBJECT_ENCODING; 00053 00065 public function readTypeMarker($typeMarker = null) 00066 { 00067 if ($typeMarker === null) { 00068 $typeMarker = $this->_stream->readByte(); 00069 } 00070 00071 switch($typeMarker) { 00072 // number 00073 case Zend_Amf_Constants::AMF0_NUMBER: 00074 return $this->_stream->readDouble(); 00075 00076 // boolean 00077 case Zend_Amf_Constants::AMF0_BOOLEAN: 00078 return (boolean) $this->_stream->readByte(); 00079 00080 // string 00081 case Zend_Amf_Constants::AMF0_STRING: 00082 return $this->_stream->readUTF(); 00083 00084 // object 00085 case Zend_Amf_Constants::AMF0_OBJECT: 00086 return $this->readObject(); 00087 00088 // null 00089 case Zend_Amf_Constants::AMF0_NULL: 00090 return null; 00091 00092 // undefined 00093 case Zend_Amf_Constants::AMF0_UNDEFINED: 00094 return null; 00095 00096 // Circular references are returned here 00097 case Zend_Amf_Constants::AMF0_REFERENCE: 00098 return $this->readReference(); 00099 00100 // mixed array with numeric and string keys 00101 case Zend_Amf_Constants::AMF0_MIXEDARRAY: 00102 return $this->readMixedArray(); 00103 00104 // array 00105 case Zend_Amf_Constants::AMF0_ARRAY: 00106 return $this->readArray(); 00107 00108 // date 00109 case Zend_Amf_Constants::AMF0_DATE: 00110 return $this->readDate(); 00111 00112 // longString strlen(string) > 2^16 00113 case Zend_Amf_Constants::AMF0_LONGSTRING: 00114 return $this->_stream->readLongUTF(); 00115 00116 //internal AS object, not supported 00117 case Zend_Amf_Constants::AMF0_UNSUPPORTED: 00118 return null; 00119 00120 // XML 00121 case Zend_Amf_Constants::AMF0_XML: 00122 return $this->readXmlString(); 00123 00124 // typed object ie Custom Class 00125 case Zend_Amf_Constants::AMF0_TYPEDOBJECT: 00126 return $this->readTypedObject(); 00127 00128 //AMF3-specific 00129 case Zend_Amf_Constants::AMF0_AMF3: 00130 return $this->readAmf3TypeMarker(); 00131 00132 default: 00133 require_once 'Zend/Amf/Exception.php'; 00134 throw new Zend_Amf_Exception('Unsupported marker type: ' . $typeMarker); 00135 } 00136 } 00137 00149 public function readObject($object = null) 00150 { 00151 if ($object === null) { 00152 $object = array(); 00153 } 00154 00155 while (true) { 00156 $key = $this->_stream->readUTF(); 00157 $typeMarker = $this->_stream->readByte(); 00158 if ($typeMarker != Zend_Amf_Constants::AMF0_OBJECTTERM ){ 00159 //Recursivly call readTypeMarker to get the types of properties in the object 00160 $object[$key] = $this->readTypeMarker($typeMarker); 00161 } else { 00162 //encountered AMF object terminator 00163 break; 00164 } 00165 } 00166 $this->_reference[] = $object; 00167 return (object) $object; 00168 } 00169 00179 public function readReference() 00180 { 00181 $key = $this->_stream->readInt(); 00182 if (!array_key_exists($key, $this->_reference)) { 00183 require_once 'Zend/Amf/Exception.php'; 00184 throw new Zend_Amf_Exception('Invalid reference key: '. $key); 00185 } 00186 return $this->_reference[$key]; 00187 } 00188 00199 public function readMixedArray() 00200 { 00201 $length = $this->_stream->readLong(); 00202 return $this->readObject(); 00203 } 00204 00212 public function readArray() 00213 { 00214 $length = $this->_stream->readLong(); 00215 $array = array(); 00216 while ($length--) { 00217 $array[] = $this->readTypeMarker(); 00218 } 00219 return $array; 00220 } 00221 00227 public function readDate() 00228 { 00229 // get the unix time stamp. Not sure why ActionScript does not use 00230 // milliseconds 00231 $timestamp = floor($this->_stream->readDouble() / 1000); 00232 00233 // The timezone offset is never returned to the server; it is always 0, 00234 // so read and ignore. 00235 $offset = $this->_stream->readInt(); 00236 00237 require_once 'Zend/Date.php'; 00238 $date = new Zend_Date($timestamp); 00239 return $date; 00240 } 00241 00248 public function readXmlString() 00249 { 00250 $string = $this->_stream->readLongUTF(); 00251 return simplexml_load_string($string); 00252 } 00253 00263 public function readTypedObject() 00264 { 00265 require_once 'Zend/Amf/Parse/TypeLoader.php'; 00266 // get the remote class name 00267 $className = $this->_stream->readUTF(); 00268 $loader = Zend_Amf_Parse_TypeLoader::loadType($className); 00269 $returnObject = new $loader(); 00270 $properties = get_object_vars($this->readObject()); 00271 foreach($properties as $key=>$value) { 00272 if($key) { 00273 $returnObject->$key = $value; 00274 } 00275 } 00276 if($returnObject instanceof Zend_Amf_Value_Messaging_ArrayCollection) { 00277 $returnObject = get_object_vars($returnObject); 00278 } 00279 return $returnObject; 00280 } 00281 00288 public function readAmf3TypeMarker() 00289 { 00290 require_once 'Zend/Amf/Parse/Amf3/Deserializer.php'; 00291 $deserializer = new Zend_Amf_Parse_Amf3_Deserializer($this->_stream); 00292 $this->_objectEncoding = Zend_Amf_Constants::AMF3_OBJECT_ENCODING; 00293 return $deserializer->readTypeMarker(); 00294 } 00295 00302 public function getObjectEncoding() 00303 { 00304 return $this->_objectEncoding; 00305 } 00306 }