File indexing completed on 2024-12-22 05:36:27

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Amf
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /** @see Zend_Amf_Constants */
0023 // require_once 'Zend/Amf/Constants.php';
0024 
0025 /** @see Zend_Amf_Parse_OutputStream */
0026 // require_once 'Zend/Amf/Parse/OutputStream.php';
0027 
0028 /** @see Zend_Amf_Parse_Amf0_Serializer */
0029 // require_once 'Zend/Amf/Parse/Amf0/Serializer.php';
0030 
0031 /**
0032  * Handles converting the PHP object ready for response back into AMF
0033  *
0034  * @package    Zend_Amf
0035  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0036  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0037  */
0038 class Zend_Amf_Response
0039 {
0040     /**
0041      * @var int Object encoding for response
0042      */
0043     protected $_objectEncoding = 0;
0044 
0045     /**
0046      * Array of Zend_Amf_Value_MessageBody objects
0047      * @var array
0048      */
0049     protected $_bodies = array();
0050 
0051     /**
0052      * Array of Zend_Amf_Value_MessageHeader objects
0053      * @var array
0054      */
0055     protected $_headers = array();
0056 
0057     /**
0058      * @var Zend_Amf_Parse_OutputStream
0059      */
0060     protected $_outputStream;
0061 
0062     /**
0063      * Instantiate new output stream and start serialization
0064      *
0065      * @return Zend_Amf_Response
0066      */
0067     public function finalize()
0068     {
0069         $this->_outputStream = new Zend_Amf_Parse_OutputStream();
0070         $this->writeMessage($this->_outputStream);
0071         return $this;
0072     }
0073 
0074     /**
0075      * Serialize the PHP data types back into Actionscript and
0076      * create and AMF stream.
0077      *
0078      * @param  Zend_Amf_Parse_OutputStream $stream
0079      * @return Zend_Amf_Response
0080      */
0081     public function writeMessage(Zend_Amf_Parse_OutputStream $stream)
0082     {
0083         $objectEncoding = $this->_objectEncoding;
0084 
0085         //Write encoding to start of stream. Preamble byte is written of two byte Unsigned Short
0086         $stream->writeByte(0x00);
0087         $stream->writeByte($objectEncoding);
0088 
0089         // Loop through the AMF Headers that need to be returned.
0090         $headerCount = count($this->_headers);
0091         $stream->writeInt($headerCount);
0092         foreach ($this->getAmfHeaders() as $header) {
0093             $serializer = new Zend_Amf_Parse_Amf0_Serializer($stream);
0094             $stream->writeUTF($header->name);
0095             $stream->writeByte($header->mustRead);
0096             $stream->writeLong(Zend_Amf_Constants::UNKNOWN_CONTENT_LENGTH);
0097             if (is_object($header->data)) {
0098                 // Workaround for PHP5 with E_STRICT enabled complaining about
0099                 // "Only variables should be passed by reference"
0100                 $placeholder = null;
0101                 $serializer->writeTypeMarker($placeholder, null, $header->data);
0102             } else {
0103                 $serializer->writeTypeMarker($header->data);
0104             }
0105         }
0106 
0107         // loop through the AMF bodies that need to be returned.
0108         $bodyCount = count($this->_bodies);
0109         $stream->writeInt($bodyCount);
0110         foreach ($this->_bodies as $body) {
0111             $serializer = new Zend_Amf_Parse_Amf0_Serializer($stream);
0112             $stream->writeUTF($body->getTargetURI());
0113             $stream->writeUTF($body->getResponseURI());
0114             $stream->writeLong(Zend_Amf_Constants::UNKNOWN_CONTENT_LENGTH);
0115             $bodyData = $body->getData();
0116             $markerType = ($this->_objectEncoding == Zend_Amf_Constants::AMF0_OBJECT_ENCODING) ? null : Zend_Amf_Constants::AMF0_AMF3;
0117             if (is_object($bodyData)) {
0118                 // Workaround for PHP5 with E_STRICT enabled complaining about
0119                 // "Only variables should be passed by reference"
0120                 $placeholder = null;
0121                 $serializer->writeTypeMarker($placeholder, $markerType, $bodyData);
0122             } else {
0123                 $serializer->writeTypeMarker($bodyData, $markerType);
0124             }
0125         }
0126 
0127         return $this;
0128     }
0129 
0130     /**
0131      * Return the output stream content
0132      *
0133      * @return string The contents of the output stream
0134      */
0135     public function getResponse()
0136     {
0137         return $this->_outputStream->getStream();
0138     }
0139 
0140     /**
0141      * Return the output stream content
0142      *
0143      * @return string
0144      */
0145     public function __toString()
0146     {
0147         return $this->getResponse();
0148     }
0149 
0150     /**
0151      * Add an AMF body to be sent to the Flash Player
0152      *
0153      * @param  Zend_Amf_Value_MessageBody $body
0154      * @return Zend_Amf_Response
0155      */
0156     public function addAmfBody(Zend_Amf_Value_MessageBody $body)
0157     {
0158         $this->_bodies[] = $body;
0159         return $this;
0160     }
0161 
0162     /**
0163      * Return an array of AMF bodies to be serialized
0164      *
0165      * @return array
0166      */
0167     public function getAmfBodies()
0168     {
0169         return $this->_bodies;
0170     }
0171 
0172     /**
0173      * Add an AMF Header to be sent back to the flash player
0174      *
0175      * @param  Zend_Amf_Value_MessageHeader $header
0176      * @return Zend_Amf_Response
0177      */
0178     public function addAmfHeader(Zend_Amf_Value_MessageHeader $header)
0179     {
0180         $this->_headers[] = $header;
0181         return $this;
0182     }
0183 
0184     /**
0185      * Retrieve attached AMF message headers
0186      *
0187      * @return array Array of Zend_Amf_Value_MessageHeader objects
0188      */
0189     public function getAmfHeaders()
0190     {
0191         return $this->_headers;
0192     }
0193 
0194     /**
0195      * Set the AMF encoding that will be used for serialization
0196      *
0197      * @param  int $encoding
0198      * @return Zend_Amf_Response
0199      */
0200     public function setObjectEncoding($encoding)
0201     {
0202         $this->_objectEncoding = $encoding;
0203         return $this;
0204     }
0205 }