3 namespace CoreFramework\Response;
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
7 use CoreFramework\Registry\Registry;
10 * A generic request class
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 * The extended headers are taken from phpMyAdmin setup tool, written by
32 * Michal Cihar <michal@cihar.com>, licensed under GNU GPL 2.0.
34 class BaseResponse extends BaseFrameworkSystem {
38 private $responseStatus = '200 OK';
41 * Array with all headers
43 private $responseHeaders = array();
46 * Cookies we shall sent out
48 private $cookies = array();
51 * Body of the response
53 private $responseBody = '';
56 * Instance of the template engine
58 private $templateInstance = NULL;
63 private $responseType = 'invalid';
66 * Protected constructor
68 * @param $className Name of the concrete response
71 protected function __construct ($className) {
72 // Call parent constructor
73 parent::__construct($className);
79 * @param $status New response status
82 public final function setResponseStatus ($status) {
83 $this->responseStatus = (string) $status;
89 * @param $name Name of header element
90 * @param $value Value of header element
93 public final function addHeader ($name, $value) {
94 $this->responseHeaders[$name] = $value;
98 * Reset the header array
102 public final function resetResponseHeaders () {
103 $this->responseHeaders = array();
107 * "Writes" data to the response body
109 * @param $output Output we shall sent in the HTTP response
112 public final function writeToBody ($output) {
113 $this->responseBody .= $output;
117 * Sets the response body to something new
119 * @param $output Output we shall sent in the HTTP response
122 public final function setResponseBody ($output) {
123 $this->responseBody = $output;
127 * Setter for response type
129 * @param $responseType Response type
132 protected final function setResponseType ($responseType) {
133 $this->responseType = $responseType;
137 * Getter for response type
139 * @param $responseType Response type
142 public final function getResponseType () {
143 return $this->responseType;
147 * Adds a fatal message id to the response. The added messages can then be
148 * processed and outputed to the world
150 * @param $messageId The message id we shall add
153 public final function addFatalMessage ($messageId) {
154 // Get application instance
155 $applicationInstance = Registry::getRegistry()->getInstance('app');
157 // Adds the resolved message id to the fatal message list
158 $this->addFatalMessagePlain($applicationInstance()->getLanguageInstance()->getMessage($messageId));
162 * Adds a plain fatal message id to the response
164 * @param $message The plain message we shall add
167 public final function addFatalMessagePlain ($message) {
168 // Adds the resolved message id to the fatal message list
169 $this->pushValueToGenericArrayKey('fatal_messages', 'generic', 'message', $message);
173 * Flushs the cached HTTP response to the outer world
175 * @param $force Whether we shall force the output or abort if headers are
176 * already sent with an exception
178 * @throws ResponseHeadersAlreadySentException Thrown if headers are
181 public function flushBuffer ($force = false) {
182 // Get application instance
183 $applicationInstance = Registry::getRegistry()->getInstance('app');
185 // Headers already sent?
186 if ((headers_sent()) && ($force === false)) {
187 // Headers are already sent!
188 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
189 } elseif (!headers_sent()) {
191 header('HTTP/1.1 ' . $this->responseStatus);
194 $now = gmdate('D, d M Y H:i:s') . ' GMT';
196 // General header for no caching
197 $this->addHeader('Expired', $now); // RFC2616 - Section 14.21
198 $this->addHeader('Last-Modified', $now);
199 $this->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
200 $this->addHeader('Pragma', 'no-cache'); // HTTP/1.0
202 // Define the charset to be used
203 //$this->addHeader('Content-type:', sprintf("text/html; charset=%s", $this->getConfigInstance()->getConfigEntry('header_charset')));
206 foreach ($this->responseHeaders as $name => $value) {
207 header($name . ': ' . $value);
208 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('name=' . $name . ',value=' . $value);
212 if (count($this->cookies) > 0) {
214 $cookieString = implode(' ', $this->cookies);
215 header('Set-Cookie: ' . $cookieString);
218 $this->cookies = array();
222 // Are there some error messages?
223 if ((!$this->isValidGenericArrayKey('fatal_messages', 'generic', 'message')) || ($this->countGenericArrayElements('fatal_messages', 'generic', 'message') == 0)) {
224 // Flush the output to the world
225 $this->getWebOutputInstance()->output($this->responseBody);
227 // Display all error messages
228 $applicationInstance()->handleFatalMessages($this->getGenericArrayKey('fatal_messages', 'generic', 'message'));
230 // Send the error messages out to the world
231 $this->getWebOutputInstance()->output($this->responseBody);
234 // Clear response header and body
235 $this->setResponseBody('');
236 $this->resetResponseHeaders();
240 * "Getter" for default command
242 * @return $defaultCommand Default command for this response
244 public function determineDefaultCommand () {
245 // Get application instance
246 $applicationInstance = Registry::getRegistry()->getInstance('app');
248 // Generate config key
249 $configKey = sprintf('default_%s_%s_command',
250 $applicationInstance->getAppShortName(),
251 $this->getResponseType()
254 // Get default command response
255 $defaultCommand = $this->getConfigInstance()->getConfigEntry($configKey);
258 return $defaultCommand;