3 * A class for an HTTP response on an HTTP request
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * The extended headers are taken from phpMyAdmin setup tool, written by
25 * Michal Cihar <michal@cihar.com>, licensed under GNU GPL 2.0.
27 class HttpResponse extends BaseFrameworkSystem implements Responseable {
31 private $responseStatus = "200 OK";
34 * Array with all headers
36 private $responseHeaders = array();
39 * Body of the response
41 private $responseBody = "";
44 * Instance of the template engine
46 private $templateEngine = null;
49 * Protected constructor
53 protected function __construct () {
54 // Call parent constructor
55 parent::__construct(__CLASS__);
57 // Set part description
58 $this->setObjectDescription("HTTP-Antwort");
60 // Create unique ID number
61 $this->createUniqueID();
64 $this->removeNumberFormaters();
65 $this->removeSystemArray();
69 * Creates an object of this class
71 * @param $appInstance An instance of a manageable application
72 * @return $responseInstance A prepared instance of this class
74 public final static function createHttpResponse (ManageableApplication $appInstance) {
76 $responseInstance = new HttpResponse();
78 // Initialize the template engine here
79 $responseInstance->initTemplateEngine($appInstance);
81 // Return the prepared instance
82 return $responseInstance;
88 * @param $status New response status
91 public final function setResponseStatus ($status) {
92 $this->responseStatus = (string) $status;
98 * @param $name Name of header element
99 * @param $value Value of header element
102 public final function addHeader ($name, $value) {
103 $this->responseHeaders[$name] = $value;
107 * "Writes" data to the response body
109 * @param $output Output we shall sent in the HTTP response
112 public function writeToBody ($output) {
113 $this->responseBody .= $output;
117 * Flushs the cached HTTP response to the outer world
119 * @param $foce Wether we shall force the output or abort if headers are
120 * already sent with an exception
122 * @throws ResponseHeadersAlreadySentException Thrown if headers are
125 public function flushBuffer ($force=false) {
126 if ((headers_sent()) && (!$force)) {
127 // Headers are already sent!
128 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
129 } elseif (!headers_sent()) {
131 header("HTTP/1.0 {$this->responseStatus}");
134 $now = gmdate('D, d M Y H:i:s') . ' GMT';
136 // General header for no caching
137 header('Expires: ' . $now); // rfc2616 - Section 14.21
138 header('Last-Modified: ' . $now);
139 header('Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
140 header('Pragma: no-cache'); // HTTP/1.0
142 // whether to show html header?
143 if ($action != 'download') {
145 // Define the charset to be used
146 header('Content-Type: text/html; charset=utf-8');
148 foreach ($this->responseHeaders as $name=>$value) {
149 header("{$name}: {$value}");
153 // Flush the output to the world
154 $this->getWebOutputInstance()->output($this->responseBody);
155 $this->reponseBody = "";
156 $this->responseHeaders = array();
160 * Initializes the template engine instance
162 * @param $appInstance An instance of a manageable application
165 public final function initTemplateEngine (ManageableApplication $appInstance) {
166 $this->templateEngine = $this->prepareTemplateEngine($appInstance);
170 * Getter for the template engine instance
172 * @return $templateEngine An instance of the used template engine
174 public final function getTemplateEngine () {
175 return $this->templateEngine;