public function setUp() { $this->_server = new Zend_Amf_Server(); $this->_server->setProduction(false); Zend_Amf_Parse_TypeLoader::resetMap(); $this->_acl = new Zend_Acl(); }
/** * Map ActionScript classes to PHP classes * * @param string $asClass * @param string $phpClass * @return Zend_Amf_Server */ public function setClassMap($asClass, $phpClass) { require_once 'Zend/Amf/Parse/TypeLoader.php'; Zend_Amf_Parse_TypeLoader::setMapping($asClass, $phpClass); return $this; }
/** * Map from PHP type name to AS type name * * @param string $typename PHP type name * @return string AS type name */ protected function _phpTypeToAS($typename) { if (class_exists($typename)) { $vars = get_class_vars($typename); if (isset($vars['_explicitType'])) { return $vars['_explicitType']; } } if (false !== ($asname = Zend_Amf_Parse_TypeLoader::getMappedClassName($typename))) { return $asname; } return $typename; }
/** * Add a class mapping and lookup the mapping to make sure * the mapping succeeds */ public function testClassMap() { $this->_server->setClassMap('controller.test', 'Zend_Amf_testclass'); $className = Zend_Amf_Parse_TypeLoader::getMappedClassName('Zend_Amf_testclass'); $this->assertEquals('controller.test', $className); }
public function writeObject($object) { if ($this->writeObjectReference($object)) { return $this; } $className = ''; switch (true) { case $className = Zend_Amf_Parse_TypeLoader::getMappedClassName(get_class($object)): break; case isset($object->_explicitType): $className = $object->_explicitType; break; case method_exists($object, 'getASClassName'): $className = $object->getASClassName(); break; case $object instanceof stdClass: $className = ''; break; default: $className = get_class($object); break; } $writeTraits = true; if (array_key_exists($className, $this->_referenceDefinitions)) { $traitsInfo = $this->_referenceDefinitions[$className]['id']; $encoding = $this->_referenceDefinitions[$className]['encoding']; $propertyNames = $this->_referenceDefinitions[$className]['propertyNames']; $traitsInfo = $traitsInfo << 2 | 0x1; $writeTraits = false; } else { $propertyNames = array(); if ($className == '') { $encoding = Zend_Amf_Constants::ET_DYNAMIC; } else { $encoding = Zend_Amf_Constants::ET_PROPLIST; foreach ($object as $key => $value) { if ($key[0] != "_") { $propertyNames[] = $key; } } } $this->_referenceDefinitions[$className] = array('id' => count($this->_referenceDefinitions), 'encoding' => $encoding, 'propertyNames' => $propertyNames); $traitsInfo = Zend_Amf_Constants::AMF3_OBJECT_ENCODING; $traitsInfo |= $encoding << 2; $traitsInfo |= count($propertyNames) << 4; } $this->writeInteger($traitsInfo); if ($writeTraits) { $this->writeString($className); foreach ($propertyNames as $value) { $this->writeString($value); } } try { switch ($encoding) { case Zend_Amf_Constants::ET_PROPLIST: foreach ($propertyNames as $key) { $this->writeTypeMarker($object->{$key}); } break; case Zend_Amf_Constants::ET_DYNAMIC: foreach ($propertyNames as $key) { $this->writeTypeMarker($object->{$key}); } foreach ($object as $key => $value) { if (!in_array($key, $propertyNames) && $key[0] != "_") { $this->writeString($key); $this->writeTypeMarker($value); } } $this->writeString(''); 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(), 0, $e); } return $this; }
/** * 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|array */ public function readObject() { $traitsInfo = $this->readInteger(); $storedObject = ($traitsInfo & 0x1) == 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 & 0x1) == 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 & 0x3; $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; $properties = array(); // clear value // 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 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); } 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; } } } if ($returnObject instanceof Zend_Amf_Value_Messaging_ArrayCollection) { if (isset($returnObject->externalizedData)) { $returnObject = $returnObject->externalizedData; } else { $returnObject = get_object_vars($returnObject); } } return $returnObject; }
/** * Map ActionScript classes to PHP classes * * @param string $asClass * @param string $phpClass * @return Zend_Amf_Server */ public function setClassMap($asClass, $phpClass) { Zend_Amf_Parse_TypeLoader::setMapping($asClass, $phpClass); return $this; }
/** * Set loader for resource type handlers * * @param Zend_Loader_PluginLoader_Interface $loader */ public static function setResourceLoader(Zend_Loader_PluginLoader_Interface $loader) { self::$_resourceLoader = $loader; }
public function testAmf0TypedObjecDeserializedToNativePHPObject() { Zend_Amf_Parse_TypeLoader::setMapping("ContactVO", "Contact"); $myRequest = file_get_contents(dirname(__FILE__) . '/Request/mock/typedObjectAmf0Request.bin'); // send the mock object request to be deserialized $this->_request->initialize($myRequest); // Make sure that no headers where recieved $this->assertEquals(0, sizeof($this->_request->getAmfHeaders())); // Make sure that the message body was set after deserialization $this->assertEquals(1, sizeof($this->_request->getAmfBodies())); $bodies = $this->_request->getAmfBodies(); $this->assertTrue($bodies[0] instanceof Zend_Amf_Value_MessageBody); $data = $bodies[0]->getData(); // Make sure that we are dealing with a PHP simpleXml element $this->assertTrue($data[0] instanceof Contact); // Make sure that the xml was deserialized properly and check its value $this->assertEquals('arnold', (string) $data[0]->lastname); }
public function testUnknownClassMap() { $class = Zend_Amf_Parse_TypeLoader::loadType('com.example.vo.Bogus'); $this->assertEquals('stdClass', $class); }
/** * 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. // Check to see if the user has defined an explicit Action Script type. case isset($object->_explicitType): $className = $object->_explicitType; break; // Check if user has defined a method for accessing the Action Script type // 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 // 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) { if ($key[0] != "_") { $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) { if ($key[0] != "_") { $this->writeString($key); } } //Write the object values to the output stream. foreach ($object as $key => $value) { if ($key[0] != "_") { $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; }
/** * Defining new unknown resource type, handler has no parse() * */ public function testCtxNoParse() { Zend_Amf_Parse_TypeLoader::setResourceLoader(new Zend_Amf_TestResourceLoader("3")); try { $resp = $this->_callService("returnCtx"); } catch (Zend_Amf_Server_Exception $e) { $this->assertContains("Could not call parse()", $e->getMessage()); return; } $this->fail("Failed to throw exception on unknown resource"); }
/** * Reset type map * * @return void */ public static function resetMap() { self::$classMap = self::$_defaultClassMap; }
public function readObject() { $traitsInfo = $this->readInteger(); $storedObject = ($traitsInfo & 0x1) == 0; $traitsInfo = $traitsInfo >> 1; 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 { $storedClass = ($traitsInfo & 0x1) == 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); } $className = $this->_referenceDefinitions[$ref]['className']; $encoding = $this->_referenceDefinitions[$ref]['encoding']; $propertyNames = $this->_referenceDefinitions[$ref]['propertyNames']; } else { $className = $this->readString(); $encoding = $traitsInfo & 0x3; $propertyNames = array(); $traitsInfo = $traitsInfo >> 2; } if (!$className) { $returnObject = new stdClass(); } else { if ($loader = Zend_Amf_Parse_TypeLoader::loadType($className)) { $returnObject = new $loader(); } else { require_once 'Zend/Amf/Exception.php'; throw new Zend_Amf_Exception('Typed object not found: ' . $className . ' '); } } $this->_referenceObjects[] = $returnObject; $properties = array(); switch ($encoding) { case Zend_Amf_Constants::ET_EXTERNAL: if (!$storedClass) { $this->_referenceDefinitions[] = array('className' => $className, 'encoding' => $encoding, 'propertyNames' => $propertyNames); } $returnObject->externalizedData = $this->readTypeMarker(); break; case Zend_Amf_Constants::ET_DYNAMIC: if (!$storedClass) { $this->_referenceDefinitions[] = array('className' => $className, 'encoding' => $encoding, 'propertyNames' => $propertyNames); } do { $property = $this->readString(); if ($property != "") { $propertyNames[] = $property; $properties[$property] = $this->readTypeMarker(); } } while ($property != ""); break; default: if (!$storedClass) { $count = $traitsInfo; for ($i = 0; $i < $count; $i++) { $propertyNames[] = $this->readString(); } $this->_referenceDefinitions[] = array('className' => $className, 'encoding' => $encoding, 'propertyNames' => $propertyNames); } foreach ($propertyNames as $property) { $properties[$property] = $this->readTypeMarker(); } break; } foreach ($properties as $key => $value) { if ($key) { $returnObject->{$key} = $value; } } } if ($returnObject instanceof Zend_Amf_Value_Messaging_ArrayCollection) { if (isset($returnObject->externalizedData)) { $returnObject = $returnObject->externalizedData; } else { $returnObject = get_object_vars($returnObject); } } return $returnObject; }
/** * Write object to ouput stream * * @param mixed $data * @return Zend_Amf_Parse_Amf3_Serializer */ public function writeObject($object) { if ($this->writeObjectReference($object)) { return $this; } $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. // Check to see if the user has defined an explicit Action Script type. case isset($object->_explicitType): $className = $object->_explicitType; break; // Check if user has defined a method for accessing the Action Script type // 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 // No return class name is set make it a generic object case $object instanceof stdClass: $className = ''; break; // By default, use object's class name // By default, use object's class name default: $className = get_class($object); break; } $writeTraits = true; //check to see, if we have a corresponding definition if (array_key_exists($className, $this->_referenceDefinitions)) { $traitsInfo = $this->_referenceDefinitions[$className]['id']; $encoding = $this->_referenceDefinitions[$className]['encoding']; $propertyNames = $this->_referenceDefinitions[$className]['propertyNames']; $traitsInfo = $traitsInfo << 2 | 0x1; $writeTraits = false; } else { $propertyNames = array(); if ($className == '') { //if there is no className, we interpret the class as dynamic without any sealed members $encoding = Zend_Amf_Constants::ET_DYNAMIC; } else { $encoding = Zend_Amf_Constants::ET_PROPLIST; foreach ($object as $key => $value) { if ($key[0] != "_") { $propertyNames[] = $key; } } } $this->_referenceDefinitions[$className] = array('id' => count($this->_referenceDefinitions), 'encoding' => $encoding, 'propertyNames' => $propertyNames); $traitsInfo = Zend_Amf_Constants::AMF3_OBJECT_ENCODING; $traitsInfo |= $encoding << 2; $traitsInfo |= count($propertyNames) << 4; } $this->writeInteger($traitsInfo); if ($writeTraits) { $this->writeString($className); foreach ($propertyNames as $value) { $this->writeString($value); } } try { switch ($encoding) { case Zend_Amf_Constants::ET_PROPLIST: //Write the sealed values to the output stream. foreach ($propertyNames as $key) { $this->writeTypeMarker($object->{$key}); } break; case Zend_Amf_Constants::ET_DYNAMIC: //Write the sealed values to the output stream. foreach ($propertyNames as $key) { $this->writeTypeMarker($object->{$key}); } //Write remaining properties foreach ($object as $key => $value) { if (!in_array($key, $propertyNames) && $key[0] != "_") { $this->writeString($key); $this->writeTypeMarker($value); } } //Write an empty string to end the dynamic part $this->writeString($this->_strEmpty); 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(), 0, $e); } return $this; }
public function testPhpObjectSerializedToAmf0TypedObjectClassMap() { Zend_Amf_Parse_TypeLoader::setMapping("ContactVO", "Contact"); $data = array(); $contact = new Contact(); $contact->id = '15'; $contact->firstname = 'Joe'; $contact->lastname = 'Smith'; $contact->email = '*****@*****.**'; $contact->mobile = '123-456-7890'; unset($contact->_explicitType); array_push($data, $contact); $contact = new Contact(); $contact->id = '23'; $contact->firstname = 'Adobe'; $contact->lastname = 'Flex'; $contact->email = '*****@*****.**'; $contact->mobile = '123-456-7890'; unset($contact->_explicitType); array_push($data, $contact); $newBody = new Zend_Amf_Value_MessageBody('/1/onResult', null, $data); $this->_response->setObjectEncoding(0x0); $this->_response->addAmfBody($newBody); $this->_response->finalize(); $testResponse = $this->_response->getResponse(); // Load the expected response. $mockResponse = file_get_contents(dirname(__FILE__) . '/Response/mock/typedObjectAmf0Response.bin'); // Check that the response matches the expected serialized value $this->assertEquals($mockResponse, $testResponse); }
/** * 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|array * @throws Zend_Amf_Exception if unable to load type */ public function readTypedObject() { require_once PHP_LIBRARY_PATH . '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; } } if ($returnObject instanceof Zend_Amf_Value_Messaging_ArrayCollection) { $returnObject = get_object_vars($returnObject); } return $returnObject; }
/** * 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; }
/** * 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. // Check to see if the user has defined an explicit Action Script type. case isset($object->_explicitType): $className = $object->_explicitType; break; // Check if user has defined a method for accessing the Action Script type // 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 // No return class name is set make it a generic object case $object instanceof stdClass: $className = ''; break; // By default, use object's class name // By default, use object's class name default: $className = get_class($object); break; } if (!$className == '') { return $className; } else { return false; } }
/** * Loads a remote class or method and executes the function and returns the result * @param {string} $method Is the method to execute * @param {mixed} $param values for the method * @return {mixed} $response the result of executing the method * @throws Zend_Amf_Server_Exception */ protected function _dispatch($method, $params = null, $source = null) { if ($source) { if (($mapped = Zend_Amf_Parse_TypeLoader::getMappedClassName($source)) !== false) { $source = $mapped; } } $qualifiedName = empty($source) ? $method : $source . '.' . $method; if (!isset($this->_table[$qualifiedName])) { // if source is null a method that was not defined was called. if ($source) { $className = 'CodeBank' . str_replace('.', '_', $source); if (class_exists($className, false) && !isset($this->_classAllowed[$className])) { require_once 'Zend/Amf/Server/Exception.php'; throw new Zend_Amf_Server_Exception('Can not call "' . $className . '" - use setClass()'); } try { $this->getLoader()->load($className); } catch (Exception $e) { require_once 'Zend/Amf/Server/Exception.php'; throw new Zend_Amf_Server_Exception('Class "' . $className . '" does not exist: ' . $e->getMessage(), 0, $e); } // Add the new loaded class to the server. $this->setClass($className, $source); } if (!isset($this->_table[$qualifiedName])) { // Source is null or doesn't contain specified method require_once 'Zend/Amf/Server/Exception.php'; throw new Zend_Amf_Server_Exception('Method "' . $method . '" does not exist'); } } $info = $this->_table[$qualifiedName]; $argv = $info->getInvokeArguments(); if (0 < count($argv)) { $params = array_merge($params, $argv); } if ($info instanceof Zend_Server_Reflection_Function) { $func = $info->getName(); $this->_checkAcl(null, $func); $return = call_user_func_array($func, $params); } else { if ($info instanceof Zend_Server_Reflection_Method) { // Get class $class = $info->getDeclaringClass()->getName(); //Check permissions if ($this->_canAccess($class) == false) { $response = CodeBank_ClientAPI::responseBase(); $response['status'] = 'EROR'; $response['message'] = _t('CodeBankAPI.PERMISSION_DENINED', '_Permission Denied'); return $response; } if ('static' == $info->isStatic()) { // for some reason, invokeArgs() does not work the same as // invoke(), and expects the first argument to be an object. // So, using a callback if the method is static. $this->_checkAcl($class, $info->getName()); $return = call_user_func_array(array($class, $info->getName()), $params); } else { // Object methods try { $object = $info->getDeclaringClass()->newInstance(); } catch (Exception $e) { throw new Zend_Amf_Server_Exception('Error instantiating class ' . $class . ' to invoke method ' . $info->getName() . ': ' . $e->getMessage(), 621, $e); } $this->_checkAcl($object, $info->getName()); $return = $info->invokeArgs($object, $params); } } else { throw new Zend_Amf_Server_Exception('Method missing implementation ' . get_class($info)); } } return $return; }