PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/core/HttpResponse.php

https://github.com/amanai/next24
PHP | 327 lines | 174 code | 30 blank | 123 comment | 19 complexity | 533132000accdab20b9a35cff1b95dbc MD5 | raw file
  1. <?php
  2. /**
  3. * HttpResponse class
  4. */
  5. class HttpResponse extends ApplicationManager implements IManager{
  6. /**
  7. * @var boolean whether to buffer output
  8. */
  9. private $_bufferOutput = true;
  10. /**
  11. * @var THttpCookieCollection list of cookies to return
  12. */
  13. private $_cookies = null;
  14. /**
  15. * @var integer response status code
  16. */
  17. private $_status = 200;
  18. /**
  19. * @var string content type
  20. */
  21. private $_contentType = null;
  22. /**
  23. * @var string character set, e.g. UTF-8
  24. */
  25. private $_charset = '';
  26. public function initialize(IConfigParameter $configuration){
  27. Project::setResponse($this);
  28. $this -> _common_config($configuration);
  29. if (($defaultCharset = $configuration -> get('DefaultCharset', null)) !== null){
  30. $this -> _charset = $defaultCharset;
  31. unset($defaultCharset);
  32. }
  33. if (($contentType = $configuration -> get('ContentType', null)) !== null){
  34. $this -> _contentType = $contentType;
  35. unset($contentType);
  36. }
  37. if (($cacheExpire = $configuration -> get('CacheExpire', null)) !== null){
  38. $this -> setCacheExpire($cacheExpire);
  39. unset($cacheExpire);
  40. }
  41. if (($cacheControl = $configuration -> get('CacheControl', null)) !== null){
  42. $this -> setCacheControl($cacheControl);
  43. unset($cacheControl);
  44. }
  45. ob_start();
  46. }
  47. /**
  48. * Destructor.
  49. * Flushes any existing content in buffer.
  50. */
  51. public function __destruct()
  52. {
  53. if($this->_bufferOutput)
  54. @ob_end_flush();
  55. }
  56. /**
  57. * @return integer time-to-live for cached session pages in minutes, this has no effect for nocache limiter. Defaults to 180.
  58. */
  59. public function getCacheExpire()
  60. {
  61. return session_cache_expire();
  62. }
  63. /**
  64. * @param integer time-to-live for cached session pages in minutes, this has no effect for nocache limiter.
  65. */
  66. public function setCacheExpire($value)
  67. {
  68. session_cache_expire((int)$value);
  69. }
  70. /**
  71. * @return string cache control method to use for session pages
  72. */
  73. public function getCacheControl()
  74. {
  75. return session_cache_limiter();
  76. }
  77. /**
  78. * @param string cache control method to use for session pages. Valid values
  79. * include none/nocache/private/private_no_expire/public
  80. */
  81. public function setCacheControl($value)
  82. {
  83. $value = strtolower(trim($value));
  84. $modes = array('none','nocache','private','private_no_expire','public');
  85. if (in_array($value, $modes)){
  86. session_cache_limiter($value);
  87. }
  88. }
  89. /**
  90. * @return string content type, default is text/html
  91. */
  92. public function setContentType($type)
  93. {
  94. //$this -> _debugger -> add(AppDebugger::RESPONSE, "Set content type:".$type);
  95. $this->_contentType = $type;
  96. }
  97. /**
  98. * @return string current content type
  99. */
  100. public function getContentType()
  101. {
  102. return $this->_contentType;
  103. }
  104. /**
  105. * @return string output charset.
  106. */
  107. public function getCharset()
  108. {
  109. return $this->_charset;
  110. }
  111. /**
  112. * @param string output charset.
  113. */
  114. public function setCharset($charset)
  115. {
  116. //$this -> _debugger -> add(AppDebugger::RESPONSE, "Set charset:".$charset);
  117. $this->_charset = $charset;
  118. }
  119. /**
  120. * @return boolean whether to enable output buffer
  121. */
  122. public function getBufferOutput()
  123. {
  124. return $this->_bufferOutput;
  125. }
  126. /**
  127. * @param boolean whether to enable output buffer
  128. * @throws TInvalidOperationException if session is started already
  129. */
  130. public function setBufferOutput($value)
  131. {
  132. if($this -> _initialized)
  133. throw new operationException('httpresponse_bufferoutput_unchangeable');
  134. else
  135. $this -> _bufferOutput=(bool)$value;
  136. }
  137. /**
  138. * @return integer HTTP status code, defaults to 200
  139. */
  140. public function getStatusCode()
  141. {
  142. return $this -> _status;
  143. }
  144. /**
  145. * @param integer HTTP status code
  146. */
  147. public function setStatusCode($status)
  148. {
  149. $this -> _status = (int)$status;
  150. }
  151. /**
  152. * Outputs a string.
  153. * It may not be sent back to user immediately if output buffer is enabled.
  154. * @param string string to be output
  155. */
  156. public function write($str)
  157. {
  158. echo $str;
  159. }
  160. /**
  161. * Sends a file back to user.
  162. * Make sure not to output anything else after calling this method.
  163. * @param string file name
  164. * @param string content to be set. If null, the content will be read from the server file pointed to by $fileName.
  165. * @param string mime type of the content.
  166. * @param array list of headers to be sent
  167. * @throws TInvalidDataValueException if the file cannot be found
  168. */
  169. public function writeFile($fileName,$content=null,$mimeType=null,$headers=null)
  170. {
  171. $this -> _debugger -> add(AppDebugger::RESPONSE, "Write file started");
  172. static $defaultMimeTypes=array(
  173. 'css'=>'text/css',
  174. 'gif'=>'image/gif',
  175. 'jpg'=>'image/jpeg',
  176. 'jpeg'=>'image/jpeg',
  177. 'htm'=>'text/html',
  178. 'html'=>'text/html',
  179. 'js'=>'javascript/js'
  180. );
  181. if($mimeType===null)
  182. {
  183. $mimeType='text/plain';
  184. if(function_exists('mime_content_type'))
  185. $mimeType=mime_content_type($fileName);
  186. else if(($ext=strrchr($fileName,'.'))!==false)
  187. {
  188. $ext=substr($ext,1);
  189. if(isset($defaultMimeTypes[$ext]))
  190. $mimeType=$defaultMimeTypes[$ext];
  191. }
  192. }
  193. $fn=basename($fileName);
  194. if(is_array($headers))
  195. {
  196. foreach($headers as $h)
  197. header($h);
  198. }
  199. else
  200. {
  201. header('Pragma: public');
  202. header('Expires: 0');
  203. header('Cache-Component: must-revalidate, post-check=0, pre-check=0');
  204. }
  205. header("Content-type: $mimeType");
  206. header('Content-Length: '.($content===null?filesize($fileName):strlen($content)));
  207. header("Content-Disposition: attachment; filename=\"$fn\"");
  208. header('Content-Transfer-Encoding: binary');
  209. if($content===null)
  210. readfile($fileName);
  211. else
  212. echo $content;
  213. }
  214. /**
  215. * Redirects the browser to the specified URL.
  216. * The current application will be terminated after this method is invoked.
  217. * @param string URL to be redirected to. If the URL is a relative one, the base URL of
  218. * the current request will be inserted at the beginning.
  219. */
  220. public function redirect($url){
  221. //if (Project::getApplication() -> getRequestCompleted()){
  222. header('Location: '.str_replace('&amp;','&',$url));
  223. exit();
  224. //}
  225. /*if(!$this->getApplication()->getRequestCompleted())
  226. $this->getApplication()->onEndRequest();
  227. if($url[0]==='/')
  228. $url=$this->getRequest()->getBaseUrl().$url;
  229. header('Location: '.str_replace('&amp;','&',$url));
  230. exit();*/
  231. }
  232. /**
  233. * Reloads the current page.
  234. * The effect of this method call is the same as user pressing the
  235. * refresh button on his browser (without post data).
  236. **/
  237. public function reload(){
  238. $this->redirect(Project::getRequest()->getRequestUri());
  239. }
  240. /**
  241. * Outputs the buffered content, sends content-type and charset header.
  242. */
  243. public function flush(){
  244. $this->sendContentTypeHeader();
  245. if($this->_bufferOutput)
  246. ob_flush();
  247. }
  248. /**
  249. * Sends content type header if charset is not empty.
  250. */
  251. public function sendContentTypeHeader(){
  252. $charset=$this->getCharset();
  253. if($charset!=='')
  254. {
  255. $contentType=$this->_contentType===null?'text/html':$this->_contentType;
  256. $this->appendHeader('Content-Type: '.$contentType.';charset='.$charset);
  257. }
  258. else if($this->_contentType!==null)
  259. $this->appendHeader('Content-Type: '.$this->_contentType.';charset=UTF-8');
  260. }
  261. /**
  262. * Returns the content in the output buffer.
  263. * The buffer will NOT be cleared after calling this method.
  264. * Use {@link clear()} is you want to clear the buffer.
  265. * @return string output that is in the buffer.
  266. */
  267. public function getContents(){
  268. return $this->_bufferOutput?ob_get_contents():'';
  269. }
  270. /**
  271. * Clears any existing buffered content.
  272. */
  273. public function clear() {
  274. if($this->_bufferOutput)
  275. ob_clean();
  276. }
  277. /**
  278. * Sends a header.
  279. * @param string header
  280. */
  281. public function appendHeader($value){
  282. header($value);
  283. }
  284. /**
  285. * Writes a log message into error log.
  286. * This method is simple wrapper of PHP function error_log.
  287. * @param string The error message that should be logged
  288. * @param integer where the error should go
  289. * @param string The destination. Its meaning depends on the message parameter as described above
  290. * @param string The extra headers. It's used when the message parameter is set to 1. This message type uses the same internal function as mail() does.
  291. * @see http://us2.php.net/manual/en/function.error-log.php
  292. */
  293. public function appendLog($message,$messageType=0,$destination='',$extraHeaders='') {
  294. error_log($message,$messageType,$destination,$extraHeaders);
  295. }
  296. }
  297. ?>