Request.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Amf_Parse_InputStream */
  21. require_once 'Zend/Amf/Parse/InputStream.php';
  22. /** Zend_Amf_Parse_Amf0_Deserializer */
  23. require_once 'Zend/Amf/Parse/Amf0/Deserializer.php';
  24. /** Zend_Amf_Constants */
  25. require_once 'Zend/Amf/Constants.php';
  26. /** Zend_Amf_Value_MessageHeader */
  27. require_once 'Zend/Amf/Value/MessageHeader.php';
  28. /** Zend_Amf_Value_MessageBody */
  29. require_once 'Zend/Amf/Value/MessageBody.php';
  30. /**
  31. * Handle the incoming AMF request by deserializing the data to php object
  32. * types and storing the data for Zend_Amf_Server to handle for processing.
  33. *
  34. * @todo Currently not checking if the object needs to be Type Mapped to a server object.
  35. * @package Zend_Amf
  36. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Amf_Request
  40. {
  41. /**
  42. * @var int AMF client type (AMF0, AMF3)
  43. */
  44. protected $_clientType = 0; // default AMF0
  45. /**
  46. * @var array Message bodies
  47. */
  48. protected $_bodies = array();
  49. /**
  50. * @var array Message headers
  51. */
  52. protected $_headers = array();
  53. /**
  54. * @var int Message encoding to use for objects in response
  55. */
  56. protected $_objectEncoding = 0;
  57. /**
  58. * @var Zend_Amf_Parse_InputStream
  59. */
  60. protected $_inputStream;
  61. /**
  62. * @var Zend_Amf_Parse_AMF0_Deserializer
  63. */
  64. protected $_deserializer;
  65. /**
  66. * Time of the request
  67. * @var mixed
  68. */
  69. protected $_time;
  70. /**
  71. * Prepare the AMF InputStream for parsing.
  72. *
  73. * @param string $request
  74. * @return Zend_Amf_Request
  75. */
  76. public function initialize($request)
  77. {
  78. $this->_inputStream = new Zend_Amf_Parse_InputStream($request);
  79. $this->_deserializer = new Zend_Amf_Parse_Amf0_Deserializer($this->_inputStream);
  80. $this->readMessage($this->_inputStream);
  81. return $this;
  82. }
  83. /**
  84. * Takes the raw AMF input stream and converts it into valid PHP objects
  85. *
  86. * @param Zend_Amf_Parse_InputStream
  87. * @return Zend_Amf_Request
  88. */
  89. public function readMessage(Zend_Amf_Parse_InputStream $stream)
  90. {
  91. $clientVersion = $stream->readUnsignedShort();
  92. if (($clientVersion != Zend_Amf_Constants::AMF0_OBJECT_ENCODING)
  93. && ($clientVersion != Zend_Amf_Constants::AMF3_OBJECT_ENCODING)
  94. ) {
  95. require_once 'Zend/Amf/Exception.php';
  96. throw new Zend_Amf_Exception('Unknown Player Version ' . $clientVersion);
  97. }
  98. $this->_bodies = array();
  99. $this->_headers = array();
  100. $headerCount = $stream->readInt();
  101. // Iterate through the AMF envelope header
  102. while ($headerCount--) {
  103. $this->_headers[] = $this->readHeader();
  104. }
  105. // Iterate through the AMF envelope body
  106. $bodyCount = $stream->readInt();
  107. while ($bodyCount--) {
  108. $this->_bodies[] = $this->readBody();
  109. }
  110. return $this;
  111. }
  112. /**
  113. * Deserialize a message header from the input stream.
  114. *
  115. * A message header is structured as:
  116. * - NAME String
  117. * - MUST UNDERSTAND Boolean
  118. * - LENGTH Int
  119. * - DATA Object
  120. *
  121. * @return Zend_Amf_Value_MessageHeader
  122. */
  123. public function readHeader()
  124. {
  125. $name = $this->_inputStream->readUTF();
  126. $mustRead = (bool)$this->_inputStream->readByte();
  127. $length = $this->_inputStream->readLong();
  128. try {
  129. $data = $this->_deserializer->readTypeMarker();
  130. } catch (Exception $e) {
  131. require_once 'Zend/Amf/Exception.php';
  132. throw new Zend_Amf_Exception('Unable to parse ' . $name . ' header data: ' . $e->getMessage() . ' '. $e->getLine());
  133. }
  134. $header = new Zend_Amf_Value_MessageHeader($name, $mustRead, $data, $length);
  135. return $header;
  136. }
  137. /**
  138. * Deserialize a message body from the input stream
  139. *
  140. * @return Zend_Amf_Value_MessageBody
  141. */
  142. public function readBody()
  143. {
  144. $targetURI = $this->_inputStream->readUTF();
  145. $responseURI = $this->_inputStream->readUTF();
  146. $length = $this->_inputStream->readLong();
  147. try {
  148. $data = $this->_deserializer->readTypeMarker();
  149. } catch (Exception $e) {
  150. require_once 'Zend/Amf/Exception.php';
  151. throw new Zend_Amf_Exception('Unable to parse ' . $targetURI . ' body data ' . $e->getMessage());
  152. }
  153. // Check for AMF3 objectEncoding
  154. if ($this->_deserializer->getObjectEncoding() == Zend_Amf_Constants::AMF3_OBJECT_ENCODING) {
  155. /*
  156. * When and AMF3 message is sent to the server it is nested inside
  157. * an AMF0 array called Content. The following code gets the object
  158. * out of the content array and sets it as the message data.
  159. */
  160. if(is_array($data) && $data[0] instanceof Zend_Amf_Value_Messaging_AbstractMessage){
  161. $data = $data[0];
  162. }
  163. // set the encoding so we return our message in AMF3
  164. $this->_objectEncoding = Zend_Amf_Constants::AMF3_OBJECT_ENCODING;
  165. }
  166. $body = new Zend_Amf_Value_MessageBody($targetURI, $responseURI, $data);
  167. return $body;
  168. }
  169. /**
  170. * Return an array of the body objects that were found in the amf request.
  171. *
  172. * @return array {target, response, length, content}
  173. */
  174. public function getAmfBodies()
  175. {
  176. return $this->_bodies;
  177. }
  178. /**
  179. * Accessor to private array of message bodies.
  180. *
  181. * @param Zend_Amf_Value_MessageBody $message
  182. * @return Zend_Amf_Request
  183. */
  184. public function addAmfBody(Zend_Amf_Value_MessageBody $message)
  185. {
  186. $this->_bodies[] = $message;
  187. return $this;
  188. }
  189. /**
  190. * Return an array of headers that were found in the amf request.
  191. *
  192. * @return array {operation, mustUnderstand, length, param}
  193. */
  194. public function getAmfHeaders()
  195. {
  196. return $this->_headers;
  197. }
  198. /**
  199. * Return the either 0 or 3 for respect AMF version
  200. *
  201. * @return int
  202. */
  203. public function getObjectEncoding()
  204. {
  205. return $this->_objectEncoding;
  206. }
  207. /**
  208. * Set the object response encoding
  209. *
  210. * @param mixed $int
  211. * @return Zend_Amf_Request
  212. */
  213. public function setObjectEncoding($int)
  214. {
  215. $this->_objectEncoding = $int;
  216. return $this;
  217. }
  218. }