]> git.mxchange.org Git - shipsimu.git/blob - inc/classes/main/response/class_HttpResponse.php
Registration stub added, naming convention applied, support for PHP code (keep it...
[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          * Protected constructor
50          *
51          * @return      void
52          */
53         protected function __construct () {
54                 // Call parent constructor
55                 parent::__construct(__CLASS__);
56
57                 // Set part description
58                 $this->setObjectDescription("HTTP-Antwort");
59
60                 // Create unique ID number
61                 $this->createUniqueID();
62
63                 // Clean up a little
64                 $this->removeNumberFormaters();
65                 $this->removeSystemArray();
66         }
67
68         /**
69          * Creates an object of this class
70          *
71          * @param       $appInstance            An instance of a manageable application
72          * @return      $responseInstance       A prepared instance of this class
73          */
74         public final static function createHttpResponse (ManageableApplication $appInstance) {
75                 // Get a new instance
76                 $responseInstance = new HttpResponse();
77
78                 // Initialize the template engine here
79                 $responseInstance->initTemplateEngine($appInstance);
80
81                 // Return the prepared instance
82                 return $responseInstance;
83         }
84
85         /**
86          * Setter for status
87          *
88          * @param       $status         New response status
89          * @return      void
90          */
91         public final function setResponseStatus ($status) {
92                 $this->responseStatus = (string) $status;
93         }
94
95         /**
96          * Add header element
97          *
98          * @param       $name   Name of header element
99          * @param       $value  Value of header element
100          * @return      void
101          */
102         public final function addHeader ($name, $value) {
103                 $this->responseHeaders[$name] = $value;
104         }
105
106         /**
107          * "Writes" data to the response body
108          *
109          * @param       $output         Output we shall sent in the HTTP response
110          * @return      void
111          */
112         public function writeToBody ($output) {
113                 $this->responseBody .= $output;
114         }
115
116         /**
117          * Flushs the cached HTTP response to the outer world
118          *
119          * @param       $foce   Wether we shall force the output or abort if headers are
120          *                                      already sent with an exception
121          * @return      void
122          * @throws      ResponseHeadersAlreadySentException             Thrown if headers are
123          *                                                                                                      already sent
124          */
125         public function flushBuffer ($force=false) {
126                 if ((headers_sent()) && (!$force)) {
127                         // Headers are already sent!
128                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
129                 } elseif (!headers_sent()) {
130                         // Send headers out
131                         header("HTTP/1.0 {$this->responseStatus}");
132
133                         // Used later
134                         $now = gmdate('D, d M Y H:i:s') . ' GMT';
135
136                         // General header for no caching
137                         $this->addHeader('Expired', $now); // rfc2616 - Section 14.21
138                         $this->addHeader('Last-Modified', $now);
139                         $this->addHeader('Cache-Control:', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
140                         $this->addHeader('Pragma:', 'no-cache'); // HTTP/1.0
141
142                         // Define the charset to be used
143                         $this->addHeader('Content-Type:', 'text/html; charset=utf-8');
144
145                         foreach ($this->responseHeaders as $name=>$value) {
146                                 header("{$name}: {$value}");
147                         }
148                 }
149
150                 // Flush the output to the world
151                 $this->getWebOutputInstance()->output($this->responseBody);
152                 $this->reponseBody = "";
153                 $this->responseHeaders = array();
154         }
155
156         /**
157          * Initializes the template engine instance
158          *
159          * @param       $appInstance    An instance of a manageable application
160          * @return      void
161          */
162         public final function initTemplateEngine (ManageableApplication $appInstance) {
163                 $this->templateEngine = $this->prepareTemplateEngine($appInstance);
164         }
165
166         /**
167          * Getter for the template engine instance
168          *
169          * @return      $templateEngine         An instance of the used template engine
170          */
171         public final function getTemplateEngine () {
172                 return $this->templateEngine;
173         }
174 }
175
176 // [EOF]
177 ?>