3 * A generic request class
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 BaseResponse extends BaseFrameworkSystem {
31 private $responseStatus = "200 OK";
34 * Array with all headers
36 private $responseHeaders = array();
39 * Cookies we shall sent out
41 private $cookies = array();
44 * Body of the response
46 private $responseBody = "";
49 * Instance of the template engine
51 private $templateInstance = null;
54 * Fatal resolved messages from filters and so on
56 private $fatalMessages = array();
59 * Protected constructor
61 * @param $className Name of the concrete response
64 protected function __construct ($className) {
65 // Call parent constructor
66 parent::__construct($className);
69 $this->removeNumberFormaters();
70 $this->removeSystemArray();
76 * @param $status New response status
79 public final function setResponseStatus ($status) {
80 $this->responseStatus = (string) $status;
86 * @param $name Name of header element
87 * @param $value Value of header element
90 public final function addHeader ($name, $value) {
91 $this->responseHeaders[$name] = $value;
95 * Reset the header array
99 public final function resetResponseHeaders () {
100 $this->responseHeaders = array();
104 * "Writes" data to the response body
106 * @param $output Output we shall sent in the HTTP response
109 public final function writeToBody ($output) {
110 $this->responseBody .= $output;
114 * Sets the response body to something new
116 * @param $output Output we shall sent in the HTTP response
119 public final function setResponseBody ($output) {
120 $this->responseBody = $output;
124 * Adds a fatal message id to the response. The added messages can then be
125 * processed and outputed to the world
127 * @param $messageId The message id we shall add
130 public final function addFatalMessage ($messageId) {
131 // Adds the resolved message id to the fatal message list
132 $this->fatalMessages[] = $this->getApplicationInstance()->getLanguageInstance()->getMessage($messageId);
136 * Adds a plain fatal message id to the response
138 * @param $message The plain message we shall add
141 public final function addFatalMessagePlain ($message) {
142 // Adds the resolved message id to the fatal message list
143 $this->fatalMessages[] = $message;
147 * Flushs the cached HTTP response to the outer world
149 * @param $force Wether we shall force the output or abort if headers are
150 * already sent with an exception
152 * @throws ResponseHeadersAlreadySentException Thrown if headers are
155 public function flushBuffer ($force = false) {
156 if ((headers_sent()) && ($force === false)) {
157 // Headers are already sent!
158 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
159 } elseif (!headers_sent()) {
161 header("HTTP/1.1 {$this->responseStatus}");
164 $now = gmdate('D, d M Y H:i:s') . ' GMT';
166 // General header for no caching
167 $this->addHeader('Expired', $now); // RFC2616 - Section 14.21
168 $this->addHeader('Last-Modified', $now);
169 $this->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
170 $this->addHeader('Pragma', 'no-cache'); // HTTP/1.0
172 // Define the charset to be used
173 //$this->addHeader('Content-type:', sprintf("text/html; charset=%s", $this->getConfigInstance()->readConfig('header_charset')));
176 foreach ($this->responseHeaders as $name => $value) {
177 header("{$name}: {$value}");
178 //* DEBUG: */ echo "{$name}: {$value}<br />\n";
182 if (count($this->cookies) > 0) {
184 $cookieString = implode(" ", $this->cookies);
185 header("Set-Cookie: {$cookieString}");
188 $this->cookies = array();
192 // Are there some error messages?
193 if (count($this->fatalMessages) == 0) {
194 // Flush the output to the world
195 $this->getWebOutputInstance()->output($this->responseBody);
197 // Display all error messages
198 $this->getApplicationInstance()->handleFatalMessages($this->fatalMessages);
200 // Send the error messages out to the world
201 $this->getWebOutputInstance()->output($this->responseBody);
204 // Clear response header and body
205 $this->setResponseBody("");
206 $this->resetResponseHeaders();