Missing directory db/user/ added, rules checkbox filter finished, directory for filte...
[shipsimu.git] / inc / classes / main / response / class_HttpResponse.php
1 <?php
2 /**
3  * A class for an HTTP response on an HTTP request
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 HttpResponse extends BaseFrameworkSystem implements Responseable {
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          * Body of the response
40          */
41         private $responseBody = "";
42
43         /**
44          * Instance of the template engine
45          */
46         private $templateEngine = null;
47
48         /**
49          * Fatal resolved messages from filters and so on
50          */
51         private $fatalMessages = array();
52
53         /**
54          * Protected constructor
55          *
56          * @return      void
57          */
58         protected function __construct () {
59                 // Call parent constructor
60                 parent::__construct(__CLASS__);
61
62                 // Set part description
63                 $this->setObjectDescription("HTTP response");
64
65                 // Create unique ID number
66                 $this->createUniqueID();
67
68                 // Clean up a little
69                 $this->removeNumberFormaters();
70                 $this->removeSystemArray();
71         }
72
73         /**
74          * Creates an object of this class
75          *
76          * @param       $appInstance            An instance of a manageable application
77          * @return      $responseInstance       A prepared instance of this class
78          */
79         public final static function createHttpResponse (ManageableApplication $appInstance) {
80                 // Get a new instance
81                 $responseInstance = new HttpResponse();
82
83                 // Set the application instance
84                 $responseInstance->setApplicationInstance($appInstance);
85
86                 // Initialize the template engine here
87                 $responseInstance->initTemplateEngine($appInstance);
88
89                 // Return the prepared instance
90                 return $responseInstance;
91         }
92
93         /**
94          * Setter for status
95          *
96          * @param       $status         New response status
97          * @return      void
98          */
99         public final function setResponseStatus ($status) {
100                 $this->responseStatus = (string) $status;
101         }
102
103         /**
104          * Add header element
105          *
106          * @param       $name   Name of header element
107          * @param       $value  Value of header element
108          * @return      void
109          */
110         public final function addHeader ($name, $value) {
111                 $this->responseHeaders[$name] = $value;
112         }
113
114         /**
115          * Reset the header array
116          *
117          * @return      void
118          */
119         public final function resetResponseHeaders () {
120                 $this->responseHeaders = array();
121         }
122
123         /**
124          * "Writes" data to the response body
125          *
126          * @param       $output         Output we shall sent in the HTTP response
127          * @return      void
128          */
129         public function writeToBody ($output) {
130                 $this->responseBody .= $output;
131         }
132
133         /**
134          * Sets the response body to something new
135          *
136          * @param       $output         Output we shall sent in the HTTP response
137          * @return      void
138          */
139         public function setReponseBody ($output) {
140                 $this->responseBody = $output;
141         }
142
143         /**
144          * Flushs the cached HTTP response to the outer world
145          *
146          * @param       $foce   Wether we shall force the output or abort if headers are
147          *                                      already sent with an exception
148          * @return      void
149          * @throws      ResponseHeadersAlreadySentException             Thrown if headers are
150          *                                                                                                      already sent
151          */
152         public function flushBuffer ($force=false) {
153                 if ((headers_sent()) && (!$force)) {
154                         // Headers are already sent!
155                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
156                 } elseif (!headers_sent()) {
157                         // Send headers out
158                         header("HTTP/1.1 {$this->responseStatus}");
159
160                         // Used later
161                         $now = gmdate('D, d M Y H:i:s') . ' GMT';
162
163                         // General header for no caching
164                         $this->addHeader('Expired', $now); // rfc2616 - Section 14.21
165                         $this->addHeader('Last-Modified', $now);
166                         $this->addHeader('Cache-Control:', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
167                         $this->addHeader('Pragma:', 'no-cache'); // HTTP/1.0
168
169                         // Define the charset to be used
170                         //$this->addHeader('Content-Type:', sprintf("text/html; charset=%s", $this->getConfigInstance()->readConfig('header_charset')));
171
172                         foreach ($this->responseHeaders as $name=>$value) {
173                                 header("{$name}: {$value}");
174                         }
175                 }
176
177                 // Are there some error messages?
178                 if (count($this->fatalMessages) == 0) {
179                         // Flush the output to the world
180                         $this->getWebOutputInstance()->output($this->responseBody);
181                 } else {
182                         // Display all error messages
183                         $this->getApplicationInstance()->handleFatalMessages($this->fatalMessages);
184
185                         // Send the error messages out to the world
186                         $this->getWebOutputInstance()->output($this->responseBody);
187                 }
188
189                 // Clear response header and body
190                 $this->setReponseBody("");
191                 $this->resetResponseHeaders();
192         }
193
194         /**
195          * Initializes the template engine instance
196          *
197          * @param       $appInstance    An instance of a manageable application
198          * @return      void
199          */
200         public final function initTemplateEngine (ManageableApplication $appInstance) {
201                 $this->templateEngine = $this->prepareTemplateEngine($appInstance);
202         }
203
204         /**
205          * Getter for the template engine instance
206          *
207          * @return      $templateEngine         An instance of the used template engine
208          */
209         public final function getTemplateEngine () {
210                 return $this->templateEngine;
211         }
212
213         /**
214          * Adds a fatal message id to the response. The added messages can then be
215          * processed and outputed to the world
216          *
217          * @param       $messageId      The message id we shall add
218          * @return      void
219          */
220         public final function addFatalMessage ($messageId) {
221                 // Adds the resolved message id to the fatal message list
222                 $this->fatalMessages[] = $this->getApplicationInstance()->getLanguageInstance()->getMessage($messageId);
223         }
224 }
225
226 // [EOF]
227 ?>