Continued with renaming-season:
[core.git] / framework / main / classes / response / class_BaseResponse.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Response;
4
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
7 use CoreFramework\Registry\Registry;
8
9 /**
10  * A generic request class
11  *
12  * @author              Roland Haeder <webmaster@shipsimu.org>
13  * @version             0.0.0
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
17  *
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.
22  *
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.
27  *
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/>.
30  *
31  * The extended headers are taken from phpMyAdmin setup tool, written by
32  * Michal Cihar <michal@cihar.com>, licensed under GNU GPL 2.0.
33  */
34 class BaseResponse extends BaseFrameworkSystem {
35         /**
36          * Response status
37          */
38         private $responseStatus = '200 OK';
39
40         /**
41          * Array with all headers
42          */
43         private $responseHeaders = array();
44
45         /**
46          * Cookies we shall sent out
47          */
48         private $cookies = array();
49
50         /**
51          * Body of the response
52          */
53         private $responseBody = '';
54
55         /**
56          * Instance of the template engine
57          */
58         private $templateInstance = NULL;
59
60         /**
61          * Response type
62          */
63         private $responseType = 'invalid';
64
65         /**
66          * Protected constructor
67          *
68          * @param       $className      Name of the concrete response
69          * @return      void
70          */
71         protected function __construct ($className) {
72                 // Call parent constructor
73                 parent::__construct($className);
74         }
75
76         /**
77          * Setter for status
78          *
79          * @param       $status         New response status
80          * @return      void
81          */
82         public final function setResponseStatus ($status) {
83                 $this->responseStatus = (string) $status;
84         }
85
86         /**
87          * Add header element
88          *
89          * @param       $name   Name of header element
90          * @param       $value  Value of header element
91          * @return      void
92          */
93         public final function addHeader ($name, $value) {
94                 $this->responseHeaders[$name] = $value;
95         }
96
97         /**
98          * Reset the header array
99          *
100          * @return      void
101          */
102         public final function resetResponseHeaders () {
103                 $this->responseHeaders = array();
104         }
105
106         /**
107          * "Writes" data to the response body
108          *
109          * @param       $output         Output we shall sent in the HTTP response
110          * @return      void
111          */
112         public final function writeToBody ($output) {
113                 $this->responseBody .= $output;
114         }
115
116         /**
117          * Sets the response body to something new
118          *
119          * @param       $output         Output we shall sent in the HTTP response
120          * @return      void
121          */
122         public final function setResponseBody ($output) {
123                 $this->responseBody = $output;
124         }
125
126         /**
127          * Setter for response type
128          *
129          * @param       $responseType   Response type
130          * @return      void
131          */
132         protected final function setResponseType ($responseType) {
133                 $this->responseType = $responseType;
134         }
135
136         /**
137          * Getter for response type
138          *
139          * @param       $responseType   Response type
140          * @return      void
141          */
142         public final function getResponseType () {
143                 return $this->responseType;
144         }
145
146         /**
147          * Adds a fatal message id to the response. The added messages can then be
148          * processed and outputed to the world
149          *
150          * @param       $messageId      The message id we shall add
151          * @return      void
152          */
153         public final function addFatalMessage ($messageId) {
154                 // Adds the resolved message id to the fatal message list
155                 $this->addFatalMessagePlain($this->getApplicationInstance()->getLanguageInstance()->getMessage($messageId));
156         }
157
158         /**
159          * Adds a plain fatal message id to the response
160          *
161          * @param       $message        The plain message we shall add
162          * @return      void
163          */
164         public final function addFatalMessagePlain ($message) {
165                 // Adds the resolved message id to the fatal message list
166                 $this->pushValueToGenericArrayKey('fatal_messages', 'generic', 'message', $message);
167         }
168
169         /**
170          * Flushs the cached HTTP response to the outer world
171          *
172          * @param       $force  Whether we shall force the output or abort if headers are
173          *                                      already sent with an exception
174          * @return      void
175          * @throws      ResponseHeadersAlreadySentException             Thrown if headers are
176          *                                                                                                      already sent
177          */
178         public function flushBuffer ($force = FALSE) {
179                 if ((headers_sent()) && ($force === FALSE)) {
180                         // Headers are already sent!
181                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
182                 } elseif (!headers_sent()) {
183                         // Send headers out
184                         header('HTTP/1.1 ' . $this->responseStatus);
185
186                         // Used later
187                         $now = gmdate('D, d M Y H:i:s') . ' GMT';
188
189                         // General header for no caching
190                         $this->addHeader('Expired', $now); // RFC2616 - Section 14.21
191                         $this->addHeader('Last-Modified', $now);
192                         $this->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
193                         $this->addHeader('Pragma', 'no-cache'); // HTTP/1.0
194
195                         // Define the charset to be used
196                         //$this->addHeader('Content-type:', sprintf("text/html; charset=%s", $this->getConfigInstance()->getConfigEntry('header_charset')));
197
198                         // Send all headers
199                         foreach ($this->responseHeaders as $name => $value) {
200                                 header($name . ': ' . $value);
201                                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('name=' . $name . ',value=' . $value);
202                         } // END - foreach
203
204                         // Send cookies out?
205                         if (count($this->cookies) > 0) {
206                                 // Send all cookies
207                                 $cookieString = implode(' ', $this->cookies);
208                                 header('Set-Cookie: ' . $cookieString);
209
210                                 // Remove them all
211                                 $this->cookies = array();
212                         } // END - if
213                 }
214
215                 // Are there some error messages?
216                 if ((!$this->isValidGenericArrayKey('fatal_messages', 'generic', 'message')) || ($this->countGenericArrayElements('fatal_messages', 'generic', 'message') == 0)) {
217                         // Flush the output to the world
218                         $this->getWebOutputInstance()->output($this->responseBody);
219                 } else {
220                         // Display all error messages
221                         $this->getApplicationInstance()->handleFatalMessages($this->getGenericArrayKey('fatal_messages', 'generic', 'message'));
222
223                         // Send the error messages out to the world
224                         $this->getWebOutputInstance()->output($this->responseBody);
225                 }
226
227                 // Clear response header and body
228                 $this->setResponseBody('');
229                 $this->resetResponseHeaders();
230         }
231
232         /**
233          * "Getter" for default command
234          *
235          * @return      $defaultCommand         Default command for this response
236          */
237         public function determineDefaultCommand () {
238                 // Get application instance
239                 $applicationInstance = Registry::getRegistry()->getInstance('app');
240
241                 // Generate config key
242                 $configKey = sprintf('default_%s_%s_command',
243                         $applicationInstance->getAppShortName(),
244                         $this->getResponseType()
245                 );
246
247                 // Get default command response
248                 $defaultCommand = $this->getConfigInstance()->getConfigEntry($configKey);
249
250                 // Return it
251                 return $defaultCommand;
252         }
253
254 }