0c3a4c71a268f9778fc66c4f50be772966ef464a
[shipsimu.git] / inc / classes / main / response / class_BaseResponse.php
1 <?php
2 /**
3  * A generic request class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.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          * Fatal resolved messages from filters and so on
55          */
56         private $fatalMessages = array();
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                 // Clean up a little
69                 $this->removeNumberFormaters();
70                 $this->removeSystemArray();
71         }
72
73         /**
74          * Setter for status
75          *
76          * @param       $status         New response status
77          * @return      void
78          */
79         public final function setResponseStatus ($status) {
80                 $this->responseStatus = (string) $status;
81         }
82
83         /**
84          * Add header element
85          *
86          * @param       $name   Name of header element
87          * @param       $value  Value of header element
88          * @return      void
89          */
90         public final function addHeader ($name, $value) {
91                 $this->responseHeaders[$name] = $value;
92         }
93
94         /**
95          * Reset the header array
96          *
97          * @return      void
98          */
99         public final function resetResponseHeaders () {
100                 $this->responseHeaders = array();
101         }
102
103         /**
104          * "Writes" data to the response body
105          *
106          * @param       $output         Output we shall sent in the HTTP response
107          * @return      void
108          */
109         public final function writeToBody ($output) {
110                 $this->responseBody .= $output;
111         }
112
113         /**
114          * Sets the response body to something new
115          *
116          * @param       $output         Output we shall sent in the HTTP response
117          * @return      void
118          */
119         public final function setResponseBody ($output) {
120                 $this->responseBody = $output;
121         }
122
123         /**
124          * Adds a fatal message id to the response. The added messages can then be
125          * processed and outputed to the world
126          *
127          * @param       $messageId      The message id we shall add
128          * @return      void
129          */
130         public final function addFatalMessage ($messageId) {
131                 // Adds the resolved message id to the fatal message list
132                 $this->fatalMessages[] = $this->getApplicationInstance()->getLanguageInstance()->getMessage($messageId);
133         }
134
135         /**
136          * Adds a plain fatal message id to the response
137          *
138          * @param       $message        The plain message we shall add
139          * @return      void
140          */
141         public final function addFatalMessagePlain ($message) {
142                 // Adds the resolved message id to the fatal message list
143                 $this->fatalMessages[] = $message;
144         }
145
146         /**
147          * Flushs the cached HTTP response to the outer world
148          *
149          * @param       $force  Wether we shall force the output or abort if headers are
150          *                                      already sent with an exception
151          * @return      void
152          * @throws      ResponseHeadersAlreadySentException             Thrown if headers are
153          *                                                                                                      already sent
154          */
155         public function flushBuffer ($force = false) {
156                 if ((headers_sent()) && (!$force)) {
157                         // Headers are already sent!
158                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
159                 } elseif (!headers_sent()) {
160                         // Send headers out
161                         header("HTTP/1.1 {$this->responseStatus}");
162
163                         // Used later
164                         $now = gmdate('D, d M Y H:i:s') . ' GMT';
165
166                         // General header for no caching
167                         $this->addHeader('Expired', $now); // RFC2616 - Section 14.21
168                         $this->addHeader('Last-Modified', $now);
169                         $this->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
170                         $this->addHeader('Pragma', 'no-cache'); // HTTP/1.0
171
172                         // Define the charset to be used
173                         //$this->addHeader('Content-type:', sprintf("text/html; charset=%s", $this->getConfigInstance()->readConfig('header_charset')));
174
175                         // Send all headers
176                         foreach ($this->responseHeaders as $name => $value) {
177                                 header("{$name}: {$value}");
178                                 //* DEBUG: */ echo "{$name}: {$value}<br />\n";
179                         } // END - foreach
180
181                         // Send cookies out?
182                         if (count($this->cookies) > 0) {
183                                 // Send all cookies
184                                 $cookieString = implode(" ", $this->cookies);
185                                 header("Set-Cookie: {$cookieString}");
186
187                                 // Remove them all
188                                 $this->cookies = array();
189                         } // END - if
190                 }
191
192                 // Are there some error messages?
193                 if (count($this->fatalMessages) == 0) {
194                         // Flush the output to the world
195                         $this->getWebOutputInstance()->output($this->responseBody);
196                 } else {
197                         // Display all error messages
198                         $this->getApplicationInstance()->handleFatalMessages($this->fatalMessages);
199
200                         // Send the error messages out to the world
201                         $this->getWebOutputInstance()->output($this->responseBody);
202                 }
203
204                 // Clear response header and body
205                 $this->setResponseBody("");
206                 $this->resetResponseHeaders();
207         }
208 }
209
210 // [EOF]
211 ?>