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