3 namespace Org\Mxchange\CoreFramework\Response;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
7 use Org\Mxchange\CoreFramework\Registry\Registry;
10 * A generic request class
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 <<<<<<< HEAD:framework/main/classes/response/class_BaseResponse.php
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
18 >>>>>>> Some updates::inc/main/classes/response/class_BaseResponse.php
19 * @license GNU GPL 3.0 or any newer version
20 * @link http://www.shipsimu.org
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 * The extended headers are taken from phpMyAdmin setup tool, written by
36 * Michal Cihar <michal@cihar.com>, licensed under GNU GPL 2.0.
38 abstract class BaseResponse extends BaseFrameworkSystem {
42 private $responseStatus = '200 OK';
45 * Array with all headers
47 private $responseHeaders = array();
50 * Cookies we shall sent out
52 private $cookies = array();
55 * Body of the response
57 private $responseBody = '';
60 * Instance of the template engine
62 private $templateInstance = NULL;
67 private $responseType = 'invalid';
70 * Protected constructor
72 * @param $className Name of the concrete response
75 protected function __construct ($className) {
76 // Call parent constructor
77 parent::__construct($className);
83 * @param $status New response status
86 public final function setResponseStatus ($status) {
87 $this->responseStatus = (string) $status;
93 * @param $name Name of header element
94 * @param $value Value of header element
97 public final function addHeader ($name, $value) {
98 $this->responseHeaders[$name] = $value;
102 * Reset the header array
106 public final function resetResponseHeaders () {
107 $this->responseHeaders = array();
111 * "Writes" data to the response body
113 * @param $output Output we shall sent in the HTTP response
116 public final function writeToBody ($output) {
117 $this->responseBody .= $output;
121 * Sets the response body to something new
123 * @param $output Output we shall sent in the HTTP response
126 public final function setResponseBody ($output) {
127 $this->responseBody = $output;
131 * Setter for response type
133 * @param $responseType Response type
136 protected final function setResponseType ($responseType) {
137 $this->responseType = $responseType;
141 * Getter for response type
143 * @param $responseType Response type
146 public final function getResponseType () {
147 return $this->responseType;
151 * Adds a fatal message id to the response. The added messages can then be
152 * processed and outputed to the world
154 * @param $messageId The message id we shall add
157 public final function addFatalMessage ($messageId) {
158 // Get application instance
159 $applicationInstance = Registry::getRegistry()->getInstance('app');
161 // Adds the resolved message id to the fatal message list
162 $this->addFatalMessagePlain($applicationInstance()->getLanguageInstance()->getMessage($messageId));
166 * Adds a plain fatal message id to the response
168 * @param $message The plain message we shall add
171 public final function addFatalMessagePlain ($message) {
172 // Adds the resolved message id to the fatal message list
173 $this->pushValueToGenericArrayKey('fatal_messages', 'generic', 'message', $message);
177 * Flushs the cached HTTP response to the outer world
179 * @param $force Whether we shall force the output or abort if headers are
180 * already sent with an exception
182 * @throws ResponseHeadersAlreadySentException Thrown if headers are
185 public function flushBuffer ($force = false) {
186 // Get application instance
187 $applicationInstance = Registry::getRegistry()->getInstance('app');
189 // Headers already sent?
190 if ((headers_sent()) && ($force === false)) {
191 // Headers are already sent!
192 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
193 } elseif (!headers_sent()) {
195 header('HTTP/1.1 ' . $this->responseStatus);
198 $now = gmdate('D, d M Y H:i:s') . ' GMT';
200 // General header for no caching
201 $this->addHeader('Expired', $now); // RFC2616 - Section 14.21
202 $this->addHeader('Last-Modified', $now);
203 $this->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
204 $this->addHeader('Pragma', 'no-cache'); // HTTP/1.0
206 // Define the charset to be used
207 //$this->addHeader('Content-type:', sprintf("text/html; charset=%s", $this->getConfigInstance()->getConfigEntry('header_charset')));
210 foreach ($this->responseHeaders as $name => $value) {
211 header($name . ': ' . $value);
212 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('name=' . $name . ',value=' . $value);
216 if (count($this->cookies) > 0) {
218 $cookieString = implode(' ', $this->cookies);
219 header('Set-Cookie: ' . $cookieString);
222 $this->cookies = array();
226 // Are there some error messages?
227 if ((!$this->isValidGenericArrayKey('fatal_messages', 'generic', 'message')) || ($this->countGenericArrayElements('fatal_messages', 'generic', 'message') == 0)) {
228 // Flush the output to the world
229 $this->getWebOutputInstance()->output($this->responseBody);
231 // Display all error messages
232 $applicationInstance()->handleFatalMessages($this->getGenericArrayKey('fatal_messages', 'generic', 'message'));
234 // Send the error messages out to the world
235 $this->getWebOutputInstance()->output($this->responseBody);
238 // Clear response header and body
239 $this->setResponseBody('');
240 $this->resetResponseHeaders();
244 * "Getter" for default command
246 * @return $defaultCommand Default command for this response
248 public function determineDefaultCommand () {
249 // Get application instance
250 $applicationInstance = Registry::getRegistry()->getInstance('app');
252 // Generate config key
253 $configKey = sprintf('default_%s_%s_command',
254 $applicationInstance->getAppShortName(),
255 $this->getResponseType()
258 // Get default command response
259 $defaultCommand = $this->getConfigInstance()->getConfigEntry($configKey);
262 return $defaultCommand;