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 * Fatal resolved messages from filters and so on
51 private $fatalMessages = array();
54 * Protected constructor
58 protected function __construct () {
59 // Call parent constructor
60 parent::__construct(__CLASS__);
62 // Set part description
63 $this->setObjectDescription("HTTP-Antwort");
65 // Create unique ID number
66 $this->createUniqueID();
69 $this->removeNumberFormaters();
70 $this->removeSystemArray();
74 * Creates an object of this class
76 * @param $appInstance An instance of a manageable application
77 * @return $responseInstance A prepared instance of this class
79 public final static function createHttpResponse (ManageableApplication $appInstance) {
81 $responseInstance = new HttpResponse();
83 // Set the application instance
84 $responseInstance->setApplicationInstance($appInstance);
86 // Initialize the template engine here
87 $responseInstance->initTemplateEngine($appInstance);
89 // Return the prepared instance
90 return $responseInstance;
96 * @param $status New response status
99 public final function setResponseStatus ($status) {
100 $this->responseStatus = (string) $status;
106 * @param $name Name of header element
107 * @param $value Value of header element
110 public final function addHeader ($name, $value) {
111 $this->responseHeaders[$name] = $value;
115 * "Writes" data to the response body
117 * @param $output Output we shall sent in the HTTP response
120 public function writeToBody ($output) {
121 $this->responseBody .= $output;
125 * Flushs the cached HTTP response to the outer world
127 * @param $foce Wether we shall force the output or abort if headers are
128 * already sent with an exception
130 * @throws ResponseHeadersAlreadySentException Thrown if headers are
133 public function flushBuffer ($force=false) {
134 if ((headers_sent()) && (!$force)) {
135 // Headers are already sent!
136 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
137 } elseif (!headers_sent()) {
139 header("HTTP/1.0 {$this->responseStatus}");
142 $now = gmdate('D, d M Y H:i:s') . ' GMT';
144 // General header for no caching
145 $this->addHeader('Expired', $now); // rfc2616 - Section 14.21
146 $this->addHeader('Last-Modified', $now);
147 $this->addHeader('Cache-Control:', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
148 $this->addHeader('Pragma:', 'no-cache'); // HTTP/1.0
150 // Define the charset to be used
151 $this->addHeader('Content-Type:', 'text/html; charset=utf-8');
153 foreach ($this->responseHeaders as $name=>$value) {
154 header("{$name}: {$value}");
158 // Flush the output to the world
159 $this->getWebOutputInstance()->output($this->responseBody);
160 $this->reponseBody = "";
161 $this->responseHeaders = array();
165 * Initializes the template engine instance
167 * @param $appInstance An instance of a manageable application
170 public final function initTemplateEngine (ManageableApplication $appInstance) {
171 $this->templateEngine = $this->prepareTemplateEngine($appInstance);
175 * Getter for the template engine instance
177 * @return $templateEngine An instance of the used template engine
179 public final function getTemplateEngine () {
180 return $this->templateEngine;
184 * Adds a fatal message id to the response. The added messages can then be
185 * processed and outputed to the world
187 * @param $messageId The message id we shall add
190 public final function addFatalMessage ($messageId) {
191 // Adds the resolved message id to the fatal message list
192 $this->fatalMessages[] = $this->getApplicationInstance()->getLanguageInstance()->getMessage($messageId);