PageRenderTime 26ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/atlas/engine/ezc/MvcTools/src/response_writers/http.php

https://github.com/jacomyma/GEXF-Atlas
PHP | 199 lines | 120 code | 17 blank | 62 comment | 14 complexity | af6ad8bdcabf6a7e107bdacdfb7cc744 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved.
  4. * @license http://ez.no/licenses/new_bsd New BSD License
  5. * @version 1.0
  6. * @filesource
  7. * @package MvcTools
  8. */
  9. /**
  10. * Request parser that uses HTTP headers to populate an ezcMvcRequest object.
  11. *
  12. * @package MvcTools
  13. * @version 1.0
  14. * @mainclass
  15. */
  16. class ezcMvcHttpResponseWriter extends ezcMvcResponseWriter
  17. {
  18. /**
  19. * Contains the response struct
  20. *
  21. * @var ezcMvcResponse
  22. */
  23. protected $response;
  24. /**
  25. * Contains an array of header name to value mappings
  26. *
  27. * @var array(string=>string)
  28. */
  29. public $headers;
  30. /**
  31. * Creates a new ezcMvcHttpResponseWriter class to write $response
  32. *
  33. * @param ezcMvcResponse $response
  34. */
  35. public function __construct( ezcMvcResponse $response )
  36. {
  37. $this->response = $response;
  38. $this->headers = array();
  39. }
  40. /**
  41. * Takes the raw protocol depending response body, and the protocol
  42. * abstract response headers and forges a response to the client. Then it sends
  43. * the assembled response to the client.
  44. */
  45. public function handleResponse()
  46. {
  47. // process all headers
  48. $this->processStandardHeaders();
  49. if ( $this->response->cache instanceof ezcMvcResultCache )
  50. {
  51. $this->processCacheHeaders();
  52. }
  53. if ( $this->response->content instanceof ezcMvcResultContent )
  54. {
  55. $this->processContentHeaders();
  56. }
  57. // process the status headers through objects
  58. if ( $this->response->status instanceof ezcMvcResultStatusObject )
  59. {
  60. $this->response->status->process( $this );
  61. }
  62. // automatically add content-length header
  63. $this->headers['Content-Length'] = strlen( $this->response->body );
  64. // write output
  65. foreach ( $this->headers as $header => $value )
  66. {
  67. header( "$header: $value" );
  68. }
  69. // do cookies
  70. foreach ( $this->response->cookies as $cookie )
  71. {
  72. $this->processCookie( $cookie );
  73. }
  74. echo $this->response->body;
  75. }
  76. /**
  77. * Takes a $cookie and uses PHP's setcookie() function to add cookies to the output stream.
  78. *
  79. * @param ezcMvcResultCookie $cookie
  80. */
  81. private function processCookie( ezcMvcResultCookie $cookie )
  82. {
  83. $args = array();
  84. $args[] = $cookie->name;
  85. $args[] = $cookie->value;
  86. if ( $cookie->expire instanceof DateTime )
  87. {
  88. $args[] = $cookie->expire->format( 'U' );
  89. }
  90. else
  91. {
  92. $args[] = null;
  93. }
  94. $args[] = $cookie->domain;
  95. $args[] = $cookie->path;
  96. $args[] = $cookie->secure;
  97. $args[] = $cookie->httpOnly;
  98. call_user_func_array( 'setcookie', $args );
  99. }
  100. /**
  101. * Checks whether there is a DateTime object in $obj->$prop and sets a header accordingly.
  102. *
  103. * @param Object $obj
  104. * @param string $prop
  105. * @param string $headerName
  106. * @param bool $default
  107. */
  108. private function doDate( $obj, $prop, $headerName, $default = false )
  109. {
  110. if ( $obj->$prop instanceof DateTime )
  111. {
  112. $headerDate = clone $obj->$prop;
  113. $headerDate->setTimezone( new DateTimeZone( "UTC" ) );
  114. $this->headers[$headerName] = $headerDate->format( 'D, d M Y H:i:s \G\M\T' );
  115. return;
  116. }
  117. if ( $default )
  118. {
  119. $headerDate = new DateTime( "UTC" );
  120. $this->headers[$headerName] = $headerDate->format( 'D, d M Y H:i:s \G\M\T' );
  121. }
  122. }
  123. /**
  124. * Processes the standard headers that are not subdivided into other structs.
  125. */
  126. protected function processStandardHeaders()
  127. {
  128. $res = $this->response;
  129. // generator
  130. $this->headers['X-Powered-By'] = $res->generator !== ''
  131. ? $res->generator
  132. : "eZ Components MvcTools";
  133. $this->doDate( $res, 'date', 'Date', true );
  134. }
  135. /**
  136. * Processes the caching related headers.
  137. */
  138. protected function processCacheHeaders()
  139. {
  140. $cache = $this->response->cache;
  141. if ( $cache->vary )
  142. {
  143. $this->headers['Vary'] = $cache->vary;
  144. }
  145. $this->doDate( $cache, 'expire', 'Expires' );
  146. if ( count( $cache->controls ) )
  147. {
  148. $this->headers['Cache-Control'] = join( ', ', $cache->controls );
  149. }
  150. if ( $cache->pragma )
  151. {
  152. $this->headers['Pragma'] = $cache->pragma;
  153. }
  154. $this->doDate( $cache, 'lastModified', 'Last-Modified' );
  155. }
  156. /**
  157. * Processes the content type related headers.
  158. */
  159. protected function processContentHeaders()
  160. {
  161. $content = $this->response->content;
  162. $defaultContentType = 'text/html';
  163. if ( $content->language )
  164. {
  165. $this->headers['Content-Language'] = $content->language;
  166. }
  167. if ( $content->type || $content->charset )
  168. {
  169. $contentType = $content->type ? $content->type : $defaultContentType;
  170. if ( $content->charset )
  171. {
  172. $contentType .= '; charset=' . $content->charset;
  173. }
  174. $this->headers['Content-Type'] = $contentType;
  175. }
  176. if ( $content->encoding )
  177. {
  178. $this->headers['Content-Encoding'] = $content->encoding;
  179. }
  180. }
  181. }
  182. ?>