Code base synced, updated
[mailer.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          * Flushs the cached HTTP response to the outer world
137          *
138          * @param       $force  Wether we shall force the output or abort if headers are
139          *                                      already sent with an exception
140          * @return      void
141          * @throws      ResponseHeadersAlreadySentException             Thrown if headers are
142          *                                                                                                      already sent
143          */
144         public function flushBuffer ($force = false) {
145                 if ((headers_sent()) && (!$force)) {
146                         // Headers are already sent!
147                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
148                 } elseif (!headers_sent()) {
149                         // Send headers out
150                         header("HTTP/1.1 {$this->responseStatus}");
151
152                         // Used later
153                         $now = gmdate('D, d M Y H:i:s') . ' GMT';
154
155                         // General header for no caching
156                         $this->addHeader('Expired', $now); // RFC2616 - Section 14.21
157                         $this->addHeader('Last-Modified', $now);
158                         $this->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
159                         $this->addHeader('Pragma', 'no-cache'); // HTTP/1.0
160
161                         // Define the charset to be used
162                         //$this->addHeader('Content-type:', sprintf("text/html; charset=%s", $this->getConfigInstance()->readConfig('header_charset')));
163
164                         // Send all headers
165                         foreach ($this->responseHeaders as $name => $value) {
166                                 header("{$name}: {$value}");
167                                 //* DEBUG: */ echo "{$name}: {$value}<br />\n";
168                         } // END - foreach
169
170                         // Send cookies out?
171                         if (count($this->cookies) > 0) {
172                                 // Send all cookies
173                                 $cookieString = implode(" ", $this->cookies);
174                                 header("Set-Cookie: {$cookieString}");
175
176                                 // Remove them all
177                                 $this->cookies = array();
178                         } // END - if
179                 }
180
181                 // Are there some error messages?
182                 if (count($this->fatalMessages) == 0) {
183                         // Flush the output to the world
184                         $this->getWebOutputInstance()->output($this->responseBody);
185                 } else {
186                         // Display all error messages
187                         $this->getApplicationInstance()->handleFatalMessages($this->fatalMessages);
188
189                         // Send the error messages out to the world
190                         $this->getWebOutputInstance()->output($this->responseBody);
191                 }
192
193                 // Clear response header and body
194                 $this->setResponseBody("");
195                 $this->resetResponseHeaders();
196         }
197 }
198
199 // [EOF]
200 ?>