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 response");
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 * Reset the header array
119 public final function resetResponseHeaders () {
120 $this->responseHeaders = array();
124 * "Writes" data to the response body
126 * @param $output Output we shall sent in the HTTP response
129 public function writeToBody ($output) {
130 $this->responseBody .= $output;
134 * Sets the response body to something new
136 * @param $output Output we shall sent in the HTTP response
139 public function setReponseBody ($output) {
140 $this->responseBody = $output;
144 * Flushs the cached HTTP response to the outer world
146 * @param $foce Wether we shall force the output or abort if headers are
147 * already sent with an exception
149 * @throws ResponseHeadersAlreadySentException Thrown if headers are
152 public function flushBuffer ($force=false) {
153 if ((headers_sent()) && (!$force)) {
154 // Headers are already sent!
155 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
156 } elseif (!headers_sent()) {
158 header("HTTP/1.1 {$this->responseStatus}");
161 $now = gmdate('D, d M Y H:i:s') . ' GMT';
163 // General header for no caching
164 $this->addHeader('Expired', $now); // rfc2616 - Section 14.21
165 $this->addHeader('Last-Modified', $now);
166 $this->addHeader('Cache-Control:', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
167 $this->addHeader('Pragma:', 'no-cache'); // HTTP/1.0
169 // Define the charset to be used
170 $this->addHeader('Content-Type:', 'text/html; charset=utf-8');
172 foreach ($this->responseHeaders as $name=>$value) {
173 header("{$name}: {$value}");
177 // Are there some error messages?
178 if (count($this->fatalMessages) == 0) {
179 // Flush the output to the world
180 $this->getWebOutputInstance()->output($this->responseBody);
182 // Display all error messages
183 $this->getApplicationInstance()->handleFatalMessages($this->fatalMessages);
185 // Send the error messages out to the world
186 $this->getWebOutputInstance()->output($this->responseBody);
189 // Clear response header and body
190 $this->setReponseBody("");
191 $this->resetResponseHeaders();
195 * Initializes the template engine instance
197 * @param $appInstance An instance of a manageable application
200 public final function initTemplateEngine (ManageableApplication $appInstance) {
201 $this->templateEngine = $this->prepareTemplateEngine($appInstance);
205 * Getter for the template engine instance
207 * @return $templateEngine An instance of the used template engine
209 public final function getTemplateEngine () {
210 return $this->templateEngine;
214 * Adds a fatal message id to the response. The added messages can then be
215 * processed and outputed to the world
217 * @param $messageId The message id we shall add
220 public final function addFatalMessage ($messageId) {
221 // Adds the resolved message id to the fatal message list
222 $this->fatalMessages[] = $this->getApplicationInstance()->getLanguageInstance()->getMessage($messageId);