PageRenderTime 72ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/vendor/zf/Zend/Amf/Response.php

https://github.com/StefanRHRO/SCRM
PHP | 205 lines | 85 code | 20 blank | 100 comment | 5 complexity | 8a43e1a158d3608ae224e651b3f1ffc9 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Amf
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Response.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /** @see Zend_Amf_Constants */
  22. require_once 'Zend/Amf/Constants.php';
  23. /** @see Zend_Amf_Parse_OutputStream */
  24. require_once 'Zend/Amf/Parse/OutputStream.php';
  25. /** @see Zend_Amf_Parse_Amf0_Serializer */
  26. require_once 'Zend/Amf/Parse/Amf0/Serializer.php';
  27. /**
  28. * Handles converting the PHP object ready for response back into AMF
  29. *
  30. * @package Zend_Amf
  31. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Amf_Response
  35. {
  36. /**
  37. * @var int Object encoding for response
  38. */
  39. protected $_objectEncoding = 0;
  40. /**
  41. * Array of Zend_Amf_Value_MessageBody objects
  42. * @var array
  43. */
  44. protected $_bodies = array();
  45. /**
  46. * Array of Zend_Amf_Value_MessageHeader objects
  47. * @var array
  48. */
  49. protected $_headers = array();
  50. /**
  51. * @var Zend_Amf_Parse_OutputStream
  52. */
  53. protected $_outputStream;
  54. /**
  55. * Instantiate new output stream and start serialization
  56. *
  57. * @return Zend_Amf_Response
  58. */
  59. public function finalize()
  60. {
  61. $this->_outputStream = new Zend_Amf_Parse_OutputStream();
  62. $this->writeMessage($this->_outputStream);
  63. return $this;
  64. }
  65. /**
  66. * Serialize the PHP data types back into Actionscript and
  67. * create and AMF stream.
  68. *
  69. * @param Zend_Amf_Parse_OutputStream $stream
  70. * @return Zend_Amf_Response
  71. */
  72. public function writeMessage(Zend_Amf_Parse_OutputStream $stream)
  73. {
  74. $objectEncoding = $this->_objectEncoding;
  75. //Write encoding to start of stream. Preamble byte is written of two byte Unsigned Short
  76. $stream->writeByte(0x00);
  77. $stream->writeByte($objectEncoding);
  78. // Loop through the AMF Headers that need to be returned.
  79. $headerCount = count($this->_headers);
  80. $stream->writeInt($headerCount);
  81. foreach ($this->getAmfHeaders() as $header) {
  82. $serializer = new Zend_Amf_Parse_Amf0_Serializer($stream);
  83. $stream->writeUTF($header->name);
  84. $stream->writeByte($header->mustRead);
  85. $stream->writeLong(Zend_Amf_Constants::UNKNOWN_CONTENT_LENGTH);
  86. if (is_object($header->data)) {
  87. // Workaround for PHP5 with E_STRICT enabled complaining about
  88. // "Only variables should be passed by reference"
  89. $placeholder = null;
  90. $serializer->writeTypeMarker($placeholder, null, $header->data);
  91. } else {
  92. $serializer->writeTypeMarker($header->data);
  93. }
  94. }
  95. // loop through the AMF bodies that need to be returned.
  96. $bodyCount = count($this->_bodies);
  97. $stream->writeInt($bodyCount);
  98. foreach ($this->_bodies as $body) {
  99. $serializer = new Zend_Amf_Parse_Amf0_Serializer($stream);
  100. $stream->writeUTF($body->getTargetURI());
  101. $stream->writeUTF($body->getResponseURI());
  102. $stream->writeLong(Zend_Amf_Constants::UNKNOWN_CONTENT_LENGTH);
  103. $bodyData = $body->getData();
  104. $markerType = ($this->_objectEncoding == Zend_Amf_Constants::AMF0_OBJECT_ENCODING) ? null : Zend_Amf_Constants::AMF0_AMF3;
  105. if (is_object($bodyData)) {
  106. // Workaround for PHP5 with E_STRICT enabled complaining about
  107. // "Only variables should be passed by reference"
  108. $placeholder = null;
  109. $serializer->writeTypeMarker($placeholder, $markerType, $bodyData);
  110. } else {
  111. $serializer->writeTypeMarker($bodyData, $markerType);
  112. }
  113. }
  114. return $this;
  115. }
  116. /**
  117. * Return the output stream content
  118. *
  119. * @return string The contents of the output stream
  120. */
  121. public function getResponse()
  122. {
  123. return $this->_outputStream->getStream();
  124. }
  125. /**
  126. * Return the output stream content
  127. *
  128. * @return string
  129. */
  130. public function __toString()
  131. {
  132. return $this->getResponse();
  133. }
  134. /**
  135. * Add an AMF body to be sent to the Flash Player
  136. *
  137. * @param Zend_Amf_Value_MessageBody $body
  138. * @return Zend_Amf_Response
  139. */
  140. public function addAmfBody(Zend_Amf_Value_MessageBody $body)
  141. {
  142. $this->_bodies[] = $body;
  143. return $this;
  144. }
  145. /**
  146. * Return an array of AMF bodies to be serialized
  147. *
  148. * @return array
  149. */
  150. public function getAmfBodies()
  151. {
  152. return $this->_bodies;
  153. }
  154. /**
  155. * Add an AMF Header to be sent back to the flash player
  156. *
  157. * @param Zend_Amf_Value_MessageHeader $header
  158. * @return Zend_Amf_Response
  159. */
  160. public function addAmfHeader(Zend_Amf_Value_MessageHeader $header)
  161. {
  162. $this->_headers[] = $header;
  163. return $this;
  164. }
  165. /**
  166. * Retrieve attached AMF message headers
  167. *
  168. * @return array Array of Zend_Amf_Value_MessageHeader objects
  169. */
  170. public function getAmfHeaders()
  171. {
  172. return $this->_headers;
  173. }
  174. /**
  175. * Set the AMF encoding that will be used for serialization
  176. *
  177. * @param int $encoding
  178. * @return Zend_Amf_Response
  179. */
  180. public function setObjectEncoding($encoding)
  181. {
  182. $this->_objectEncoding = $encoding;
  183. return $this;
  184. }
  185. }