bd7524890de030d6f633c3fd2de06423fd691750
[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          * Protected constructor
55          *
56          * @param       $className      Name of the concrete response
57          * @return      void
58          */
59         protected function __construct ($className) {
60                 // Call parent constructor
61                 parent::__construct($className);
62         }
63
64         /**
65          * Setter for status
66          *
67          * @param       $status         New response status
68          * @return      void
69          */
70         public final function setResponseStatus ($status) {
71                 $this->responseStatus = (string) $status;
72         }
73
74         /**
75          * Add header element
76          *
77          * @param       $name   Name of header element
78          * @param       $value  Value of header element
79          * @return      void
80          */
81         public final function addHeader ($name, $value) {
82                 $this->responseHeaders[$name] = $value;
83         }
84
85         /**
86          * Reset the header array
87          *
88          * @return      void
89          */
90         public final function resetResponseHeaders () {
91                 $this->responseHeaders = array();
92         }
93
94         /**
95          * "Writes" data to the response body
96          *
97          * @param       $output         Output we shall sent in the HTTP response
98          * @return      void
99          */
100         public final function writeToBody ($output) {
101                 $this->responseBody .= $output;
102         }
103
104         /**
105          * Sets the response body to something new
106          *
107          * @param       $output         Output we shall sent in the HTTP response
108          * @return      void
109          */
110         public final function setResponseBody ($output) {
111                 $this->responseBody = $output;
112         }
113
114         /**
115          * Adds a fatal message id to the response. The added messages can then be
116          * processed and outputed to the world
117          *
118          * @param       $messageId      The message id we shall add
119          * @return      void
120          */
121         public final function addFatalMessage ($messageId) {
122                 // Adds the resolved message id to the fatal message list
123                 $this->addFatalMessagePlain($this->getApplicationInstance()->getLanguageInstance()->getMessage($messageId));
124         }
125
126         /**
127          * Adds a plain fatal message id to the response
128          *
129          * @param       $message        The plain message we shall add
130          * @return      void
131          */
132         public final function addFatalMessagePlain ($message) {
133                 // Adds the resolved message id to the fatal message list
134                 $this->pushValueToGenericArrayKey('fatal_messages', 'generic', 'message', $message);
135         }
136
137         /**
138          * Flushs the cached HTTP response to the outer world
139          *
140          * @param       $force  Whether we shall force the output or abort if headers are
141          *                                      already sent with an exception
142          * @return      void
143          * @throws      ResponseHeadersAlreadySentException             Thrown if headers are
144          *                                                                                                      already sent
145          */
146         public function flushBuffer ($force = FALSE) {
147                 if ((headers_sent()) && ($force === FALSE)) {
148                         // Headers are already sent!
149                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
150                 } elseif (!headers_sent()) {
151                         // Send headers out
152                         header('HTTP/1.1 ' . $this->responseStatus);
153
154                         // Used later
155                         $now = gmdate('D, d M Y H:i:s') . ' GMT';
156
157                         // General header for no caching
158                         $this->addHeader('Expired', $now); // RFC2616 - Section 14.21
159                         $this->addHeader('Last-Modified', $now);
160                         $this->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
161                         $this->addHeader('Pragma', 'no-cache'); // HTTP/1.0
162
163                         // Define the charset to be used
164                         //$this->addHeader('Content-type:', sprintf("text/html; charset=%s", $this->getConfigInstance()->getConfigEntry('header_charset')));
165
166                         // Send all headers
167                         foreach ($this->responseHeaders as $name => $value) {
168                                 header($name . ': ' . $value);
169                                 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('name=' . $name . ',value=' . $value);
170                         } // END - foreach
171
172                         // Send cookies out?
173                         if (count($this->cookies) > 0) {
174                                 // Send all cookies
175                                 $cookieString = implode(' ', $this->cookies);
176                                 header('Set-Cookie: ' . $cookieString);
177
178                                 // Remove them all
179                                 $this->cookies = array();
180                         } // END - if
181                 }
182
183                 // Are there some error messages?
184                 if ((!$this->isValidGenericArrayKey('fatal_messages', 'generic', 'message')) || ($this->countGenericArrayElements('fatal_messages', 'generic', 'message') == 0)) {
185                         // Flush the output to the world
186                         $this->getWebOutputInstance()->output($this->responseBody);
187                 } else {
188                         // Display all error messages
189                         $this->getApplicationInstance()->handleFatalMessages($this->getGenericArrayKey('fatal_messages', 'generic', 'message'));
190
191                         // Send the error messages out to the world
192                         $this->getWebOutputInstance()->output($this->responseBody);
193                 }
194
195                 // Clear response header and body
196                 $this->setResponseBody('');
197                 $this->resetResponseHeaders();
198         }
199 }
200
201 // [EOF]
202 ?>