Example #1
0
 /**
  * Builds a composite AMF response based on the response bodies inside the 
  * original AMF request.
  *
  * @return Zend_Amf_Response_Http
  */
 public function processResponseBodies()
 {
     $responseBodies = $this->request->getAmfBodies();
     foreach ($responseBodies as $body) {
         //Extract params from request body
         $return = $this->extractUriAndParams($body);
         //Create fake request object
         $liRequest = new Request(array('data' => $return['params']));
         //Assign URL to request based on details
         if (isset($return['source'])) {
             $liRequest->url = '/' . $return['source'] . '/' . $return['method'];
         } elseif (isset($return['targetURI'])) {
             $liRequest->url = '/' . $return['targetURI'];
         }
         //Assign request params
         $liRequest->params += $return['params'];
         //Dispatch the request normally, and get the controller data
         $controllerResponse = Dispatcher::run($liRequest);
         //Add on the response data (or error) to the current response
         if (isset($controllerResponse->body['error'])) {
             $netStatusEvent = new StdClass();
             $netStatusEvent->_explicitType = 'flex.messaging.messages.ErrorMessage';
             $netStatusEvent->faultString = $controllerResponse->body['error'];
             $newBody = new \Zend_Amf_Value_MessageBody($body->getResponseURI() . \Zend_AMF_Constants::STATUS_METHOD, null, $netStatusEvent);
             $this->response->addAmfBody($newBody);
         } else {
             $newBody = new \Zend_Amf_Value_MessageBody($body->getResponseURI() . \Zend_AMF_Constants::STATUS_METHOD, null, $controllerResponse->body);
             $this->response->addAmfBody($newBody);
         }
     }
     return $this->response;
 }
Example #2
0
 protected function _callService($method, $class = 'Zend_Amf_Resource_testclass')
 {
     $request = new Zend_Amf_Request();
     $request->setObjectEncoding(0x3);
     $this->_server->setClass($class);
     $newBody = new Zend_Amf_Value_MessageBody("{$class}.{$method}", "/1", array("test"));
     $request->addAmfBody($newBody);
     $this->_server->handle($request);
     $response = $this->_server->getResponse();
     return $response;
 }
Example #3
0
 public function testToStringShouldProxyToGetResponse()
 {
     $this->testResponseShouldAggregateMessageHeaders();
     $this->_response->finalize();
     $response = $this->_response->getResponse();
     $test = $this->_response->__toString();
     $this->assertSame($response, $test);
 }
Example #4
0
 /**
  * Test Amf0 credentials sent to the server
  *
  */
 public function testAmf0CredentialsInHeader()
 {
     $myRequest = file_get_contents(dirname(__FILE__) . '/Request/mock/credentialsheaderAmf0.bin');
     // send the mock object request to be deserialized
     $this->_request->initialize($myRequest);
     // Make sure that no headers where recieved
     $this->assertEquals(1, sizeof($this->_request->getAmfHeaders()));
     $requestHeaders = $this->_request->getAmfHeaders();
     $this->assertTrue($requestHeaders[0] instanceof Zend_Amf_Value_MessageHeader);
     $this->assertEquals('Credentials', $requestHeaders[0]->name);
     $this->assertFalse($requestHeaders[0]->mustRead);
     $data = $requestHeaders[0]->data;
     // Check the resulting header
     $this->assertEquals('admin', $data->userid);
     $this->assertEquals('pw123', $data->password);
 }
Example #5
0
 /**
  * Takes the deserialized AMF request and performs any operations.
  *
  * @todo   should implement and SPL observer pattern for custom AMF headers
  * @todo   DescribeService support
  * @param  Zend_Amf_Request $request
  * @return Zend_Amf_Response
  * @throws Zend_Amf_server_Exception|Exception
  */
 protected function _handle(Zend_Amf_Request $request)
 {
     // Get the object encoding of the request.
     $objectEncoding = $request->getObjectEncoding();
     // create a response object to place the output from the services.
     $response = $this->getResponse();
     // set response encoding
     $response->setObjectEncoding($objectEncoding);
     $responseBody = $request->getAmfBodies();
     $handleAuth = false;
     if ($this->_auth) {
         $headers = $request->getAmfHeaders();
         if (isset($headers[Zend_Amf_Constants::CREDENTIALS_HEADER]) && isset($headers[Zend_Amf_Constants::CREDENTIALS_HEADER]->userid)) {
             $handleAuth = true;
         }
     }
     // Iterate through each of the service calls in the AMF request
     foreach ($responseBody as $body) {
         try {
             if ($handleAuth) {
                 if ($this->_handleAuth($headers[Zend_Amf_Constants::CREDENTIALS_HEADER]->userid, $headers[Zend_Amf_Constants::CREDENTIALS_HEADER]->password)) {
                     // use RequestPersistentHeader to clear credentials
                     $response->addAmfHeader(new Zend_Amf_Value_MessageHeader(Zend_Amf_Constants::PERSISTENT_HEADER, false, new Zend_Amf_Value_MessageHeader(Zend_Amf_Constants::CREDENTIALS_HEADER, false, null)));
                     $handleAuth = false;
                 }
             }
             if ($objectEncoding == Zend_Amf_Constants::AMF0_OBJECT_ENCODING) {
                 // AMF0 Object Encoding
                 $targetURI = $body->getTargetURI();
                 $message = '';
                 // Split the target string into its values.
                 $source = substr($targetURI, 0, strrpos($targetURI, '.'));
                 if ($source) {
                     // Break off method name from namespace into source
                     $method = substr(strrchr($targetURI, '.'), 1);
                     $return = $this->_dispatch($method, $body->getData(), $source);
                 } else {
                     // Just have a method name.
                     $return = $this->_dispatch($targetURI, $body->getData());
                 }
             } else {
                 // AMF3 read message type
                 $message = $body->getData();
                 if ($message instanceof Zend_Amf_Value_Messaging_CommandMessage) {
                     // async call with command message
                     $return = $this->_loadCommandMessage($message);
                 } elseif ($message instanceof Zend_Amf_Value_Messaging_RemotingMessage) {
                     require_once 'Zend/Amf/Value/Messaging/AcknowledgeMessage.php';
                     $return = new Zend_Amf_Value_Messaging_AcknowledgeMessage($message);
                     $return->body = $this->_dispatch($message->operation, $message->body, $message->source);
                 } else {
                     // Amf3 message sent with netConnection
                     $targetURI = $body->getTargetURI();
                     // Split the target string into its values.
                     $source = substr($targetURI, 0, strrpos($targetURI, '.'));
                     if ($source) {
                         // Break off method name from namespace into source
                         $method = substr(strrchr($targetURI, '.'), 1);
                         $return = $this->_dispatch($method, $body->getData(), $source);
                     } else {
                         // Just have a method name.
                         $return = $this->_dispatch($targetURI, $body->getData());
                     }
                 }
             }
             $responseType = Zend_AMF_Constants::RESULT_METHOD;
         } catch (Exception $e) {
             $return = $this->_errorMessage($objectEncoding, $message, $e->getMessage(), $e->getTraceAsString(), $e->getCode(), $e->getLine());
             $responseType = Zend_AMF_Constants::STATUS_METHOD;
         }
         $responseURI = $body->getResponseURI() . $responseType;
         $newBody = new Zend_Amf_Value_MessageBody($responseURI, null, $return);
         $response->addAmfBody($newBody);
     }
     // Add a session header to the body if session is requested.
     if ($this->isSession()) {
         $currentID = session_id();
         $joint = "?";
         if (isset($_SERVER['QUERY_STRING'])) {
             if (!strpos($_SERVER['QUERY_STRING'], $currentID) !== FALSE) {
                 if (strrpos($_SERVER['QUERY_STRING'], "?") !== FALSE) {
                     $joint = "&";
                 }
             }
         }
         // create a new AMF message header with the session id as a variable.
         $sessionValue = $joint . $this->_sessionName . "=" . $currentID;
         $sessionHeader = new Zend_Amf_Value_MessageHeader(Zend_Amf_Constants::URL_APPEND_HEADER, false, $sessionValue);
         $response->addAmfHeader($sessionHeader);
     }
     // serialize the response and return serialized body.
     $response->finalize();
 }
Example #6
0
 /**
  * Takes the deserialized AMF request and performs any operations.
  *
  * @todo   should implement and SPL observer pattern for custom AMF headers
  * @todo   implement AMF header authentication
  * @param  Zend_Amf_Request $request
  * @return Zend_Amf_Response
  * @throws Zend_Amf_server_Exception|Exception
  */
 protected function _handle(Zend_Amf_Request $request)
 {
     // Get the object encoding of the request.
     $objectEncoding = $request->getObjectEncoding();
     // create a response object to place the output from the services.
     $response = $this->getResponse();
     // set reponse encoding
     $response->setObjectEncoding($objectEncoding);
     $responseBody = $request->getAmfBodies();
     // Iterate through each of the service calls in the AMF request
     foreach ($responseBody as $body) {
         try {
             if ($objectEncoding == Zend_Amf_Constants::AMF0_OBJECT_ENCODING) {
                 // AMF0 Object Encoding
                 $targetURI = $body->getTargetURI();
                 // Split the target string into its values.
                 $source = substr($targetURI, 0, strrpos($targetURI, '.'));
                 if ($source) {
                     // Break off method name from namespace into source
                     $method = substr(strrchr($targetURI, '.'), 1);
                     $return = $this->_dispatch($method, $body->getData(), $source);
                 } else {
                     // Just have a method name.
                     $return = $this->_dispatch($targetURI, $body->getData());
                 }
             } else {
                 // AMF3 read message type
                 $message = $body->getData();
                 if ($message instanceof Zend_Amf_Value_Messaging_CommandMessage) {
                     // async call with command message
                     $return = $this->_loadCommandMessage($message);
                 } elseif ($message instanceof Zend_Amf_Value_Messaging_RemotingMessage) {
                     require_once 'Zend/Amf/Value/Messaging/AcknowledgeMessage.php';
                     $return = new Zend_Amf_Value_Messaging_AcknowledgeMessage($message);
                     $return->body = $this->_dispatch($message->operation, $message->body, $message->source);
                 } else {
                     // Amf3 message sent with netConnection
                     $targetURI = $body->getTargetURI();
                     // Split the target string into its values.
                     $source = substr($targetURI, 0, strrpos($targetURI, '.'));
                     if ($source) {
                         // Break off method name from namespace into source
                         $method = substr(strrchr($targetURI, '.'), 1);
                         $return = $this->_dispatch($method, array($body->getData()), $source);
                     } else {
                         // Just have a method name.
                         $return = $this->_dispatch($targetURI, $body->getData());
                     }
                 }
             }
             $responseType = Zend_AMF_Constants::RESULT_METHOD;
         } catch (Exception $e) {
             switch ($objectEncoding) {
                 case Zend_Amf_Constants::AMF0_OBJECT_ENCODING:
                     $return = array('description' => $this->isProduction() ? '' : $e->getMessage(), 'detail' => $this->isProduction() ? '' : $e->getTraceAsString(), 'line' => $this->isProduction() ? 0 : $e->getLine(), 'code' => $e->getCode());
                     break;
                 case Zend_Amf_Constants::AMF3_OBJECT_ENCODING:
                     require_once 'Zend/Amf/Value/Messaging/ErrorMessage.php';
                     $return = new Zend_Amf_Value_Messaging_ErrorMessage($message);
                     $return->faultString = $this->isProduction() ? '' : $e->getMessage();
                     $return->faultCode = $e->getCode();
                     $return->faultDetail = $this->isProduction() ? '' : $e->getTraceAsString();
                     break;
             }
             $responseType = Zend_AMF_Constants::STATUS_METHOD;
         }
         $responseURI = $body->getResponseURI() . $responseType;
         $newBody = new Zend_Amf_Value_MessageBody($responseURI, null, $return);
         $response->addAmfBody($newBody);
     }
     // serialize the response and return serialized body.
     $response->finalize();
 }
Example #7
0
 public function testCtorExcection()
 {
     $this->_server->setClass('Zend_Amf_testException');
     $this->_server->setProduction(false);
     $message = new Zend_Amf_Value_Messaging_RemotingMessage();
     $message->operation = 'hello';
     $message->source = 'Zend_Amf_testException';
     $message->body = array("123");
     // create a mock message body to place th remoting message inside
     $newBody = new Zend_Amf_Value_MessageBody(null, "/1", $message);
     $request = new Zend_Amf_Request();
     // at the requested service to a request
     $request->addAmfBody($newBody);
     $request->setObjectEncoding(0x3);
     // let the server handle mock request
     $this->_server->handle($request);
     $response = $this->_server->getResponse()->getAMFBodies();
     $this->assertTrue($response[0]->getData() instanceof Zend_Amf_Value_Messaging_ErrorMessage);
     $this->assertContains("Oops, exception!", $response[0]->getData()->faultString);
 }
Example #8
0
 /**
  * @group ZF-6130
  */
 public function testServerShouldCastObjectArgumentsToAppropriateType()
 {
     $server = new Zend_Amf_Server();
     $server->addDirectory(dirname(__FILE__) . '/_files/zf-6130/services');
     // Create a mock message
     $message = new Zend_Amf_Value_Messaging_RemotingMessage();
     $message->operation = 'createEmployee';
     $message->source = 'EmployeeService';
     // original raw request used "destination"
     $message->body = array(array('office' => 322, 'departmentid' => 3, 'street' => 32, 'zipcode' => 32, 'state' => 32, 'lastname' => 4, 'firstname' => 2, 'photofile' => 322, 'city' => 32, 'id' => 1, 'title' => 4, 'officephone' => 233, 'email' => 32, 'cellphone' => 22));
     $body = new Zend_Amf_Value_MessageBody(null, "", $message);
     $request = new Zend_Amf_Request();
     $request->addAmfBody($body);
     $request->setObjectEncoding(0x3);
     $response = $server->handle($request);
     $employee = EmployeeService::$employee;
     $this->assertNotNull($employee);
     $this->assertNotEquals(1, $employee->id);
     $this->assertRegexp('/[a-z0-9]{3,}/', $employee->id);
 }
Example #9
0
 public function testLogout()
 {
     Zend_Session::$_unitTestEnabled = true;
     $this->_server->setAuth(new RightPassword("testuser", "testrole"));
     $this->_acl->addRole(new Zend_Acl_Role("testrole"));
     $this->_acl->allow("testrole", null, null);
     $this->_server->setAcl($this->_acl);
     $resp = $this->_callServiceAuth("testuser", "");
     $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
     $this->assertContains("hello", $resp[1]->getData());
     // After logout same request should not be allowed
     $this->setUp();
     $this->_server->setAuth(new RightPassword("testuser", "testrole"));
     $this->_server->setAcl($this->_acl);
     $request = new Zend_Amf_Request();
     $request->setObjectEncoding(0x3);
     $this->_addLogout($request);
     $this->_addServiceCall($request);
     $this->_server->handle($request);
     $resp = $this->_server->getResponse()->getAmfBodies();
     $this->assertTrue($resp[0]->getData() instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
     $data = $resp[1]->getData();
     $this->assertTrue($data instanceof Zend_Amf_Value_Messaging_ErrorMessage);
     $this->assertContains("not allowed", $data->faultString);
 }
 /**
  * Handles passing a request through the amf client
  * @param {string} $servicePath Service path i.e ServerController.connect
  * @param {object|array} $data Data to be sent with the request should be an array or an object
  * @return {array} Server response
  */
 protected function getAMFResponse($servicePath, $data = null)
 {
     require_once 'Zend/Amf/Request.php';
     require_once 'Zend/Amf/Constants.php';
     require_once 'Zend/Amf/Value/MessageBody.php';
     require_once 'Zend/Amf/Value/Messaging/RemotingMessage.php';
     require_once 'Zend/Amf/Value/Messaging/ErrorMessage.php';
     if ($data) {
         if (is_array($data)) {
             $data = $this->arrayToObject($data);
         } else {
             if (!is_object($data)) {
                 user_error('$data is not an array or object', E_USER_ERROR);
             }
         }
     }
     //Find the method and service
     $service = explode('.', $servicePath);
     $method = array_pop($service);
     $service = implode('.', $service);
     //Build the message
     $message = new Zend_Amf_Value_Messaging_RemotingMessage();
     $message->parameters = $data;
     $message->operation = $method;
     $message->source = $service;
     //Build the message body
     $body = new Zend_Amf_Value_MessageBody($servicePath, '/1', array($data));
     //Build the AMF Request
     $request = new Zend_Amf_Request();
     $request->addAmfBody($body);
     $request->setObjectEncoding(Zend_Amf_Constants::AMF3_OBJECT_ENCODING);
     //Init the client api
     $amfClient = new CodeBank_ClientAPI();
     $amfClient->setTestRequest($request);
     //Capture the response as an amf input stream
     ob_start();
     $response = $amfClient->index();
     ob_end_clean();
     //Get the amf bodies
     $bodies = $response->getAmfBodies();
     if (count($bodies) > 0) {
         $body = $bodies[0]->getData();
         if ($body instanceof Zend_Amf_Value_Messaging_ErrorMessage) {
             $this->fail('AMF Server returned an error: ' . $body->faultString . "\n\n" . $body->faultDetail);
             return false;
         }
         return $body;
     }
     return false;
 }
Example #11
0
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        //curl_setopt($ch, CURLOPT_VERBOSE, true);
        curl_setopt($ch, CURLOPT_URL, $this->amf_gateway);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->getHttpPost());
        $this->resp_data = curl_exec($ch);
        curl_close($ch);
    }
}
return;
//for test
/*
 $data['tnd']   = 2;
 $data['session_key'] = 'skvalue';
 $data['pmid']  = 1111;
 //*/
//*
$data = new stdClass();
$data->tnd = 2;
$data->session_key = 'skvalue';
$data->pmid = 1111;
//*/
$myreq = new AmfRequestClient('TestAmf.sendback', $data);
$myreq->doRequest();
echo "Request:\n{$myreq->post_data}\n\nResponse:\n{$myreq->resp_data}\n";
return;
$req = new Zend_Amf_Request();
$req->initialize($sreq);
var_dump($myreq);
var_dump($req);
//$myreq->doRequest();