Updating/inserting points finished (basicly), flushing needed database updates moved...
[core.git] / inc / classes / main / response / http / 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 BaseResponse implements Responseable {
28         /**
29          * Protected constructor
30          *
31          * @return      void
32          */
33         protected function __construct () {
34                 // Call parent constructor
35                 parent::__construct(__CLASS__);
36         }
37
38         /**
39          * Creates an object of this class
40          *
41          * @param       $appInstance            An instance of a manageable application
42          * @return      $responseInstance       A prepared instance of this class
43          */
44         public final static function createHttpResponse (ManageableApplication $appInstance) {
45                 // Get a new instance
46                 $responseInstance = new HttpResponse();
47
48                 // Set the application instance
49                 $responseInstance->setApplicationInstance($appInstance);
50
51                 // Initialize the template engine here
52                 $responseInstance->initTemplateEngine($appInstance);
53
54                 // Return the prepared instance
55                 return $responseInstance;
56         }
57
58         /**
59          * Initializes the template engine instance
60          *
61          * @param       $appInstance    An instance of a manageable application
62          * @return      void
63          */
64         public final function initTemplateEngine (ManageableApplication $appInstance) {
65                 $this->setTemplateInstance($this->prepareTemplateInstance($appInstance));
66         }
67
68         /**
69          * Adds a cookie to the response
70          *
71          * @param       $cookieName             Cookie's name
72          * @param       $cookieValue    Value to store in the cookie
73          * @param       $encrypted              Do some extra encryption on the value
74          * @param       $expires                Timestamp of expiration (default: configured)
75          * @return      void
76          * @throws      ResponseHeadersAlreadySentException             If headers are already sent
77          * @todo        Encryption of cookie data not yet supported.
78          * @todo        Why are these parameters conflicting?
79          * @todo        If the return statement is removed and setcookie() commented out,
80          * @todo        this will send only one cookie out, the first one.
81          */
82         public function addCookie ($cookieName, $cookieValue, $encrypted = false, $expires = null) {
83                 //* DEBUG: */ echo $cookieName."=".$cookieValue."<br />\n";
84                 // Are headers already sent?
85                 if (headers_sent()) {
86                         // Throw an exception here
87                         //* DEBUG: */ return;
88                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
89                 } // END - if
90
91                 // Shall we encrypt the cookie?
92                 if ($encrypted === true) {
93                 } // END - if
94
95                 // For slow browsers set the cookie array element first
96                 $_COOKIE[$cookieName] = $cookieValue;
97
98                 // Get all config entries
99                 if (is_null($expires)) {
100                         $expires = (time() + $this->getConfigInstance()->readConfig('cookie_expire'));
101                 } // END - if
102
103                 $path = $this->getConfigInstance()->readConfig('cookie_path');
104                 $domain = $this->getConfigInstance()->readConfig('cookie_domain');
105
106                 setcookie($cookieName, $cookieValue, $expires);
107                 //, $path, $domain, (isset($_SERVER['HTTPS']))
108                 return;
109
110                 // Now construct the full header
111                 $cookieString = $cookieName . "=" . $cookieValue . "; ";
112                 $cookieString .= "expires=" . date("D, d-F-Y H:i:s", $expires) . " GMT";
113                 // $cookieString .= "; path=".$path."; domain=".$domain;
114
115                 // Set the cookie as a header
116                 $this->cookies[$cookieName] = $cookieString;
117         }
118
119         /**
120          * Redirect to a configured URL. The URL can be absolute or relative. In
121          * case of relative URL it will be extended automatically.
122          *
123          * @param       $configEntry    The configuration entry which holds our URL
124          * @return      void
125          * @throws      ResponseHeadersAlreadySentException             If headers are already sent
126          */
127         public function redirectToConfiguredUrl ($configEntry) {
128                 // Is the header not yet sent?
129                 if (headers_sent()) {
130                         // Throw an exception here
131                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
132                 } // END - if
133
134                 // Assign application data
135                 $this->getTemplateInstance()->assignApplicationData($this->getApplicationInstance());
136
137                 // Get the url from config
138                 $url = $this->getConfigInstance()->readConfig($configEntry);
139
140                 // Compile the URL
141                 $url = $this->getTemplateInstance()->compileRawCode($url);
142
143                 // Do we have a "http" in front of the URL?
144                 if (substr(strtolower($url), 0, 4) != "http") {
145                         // Is there a / in front of the relative URL?
146                         if (substr($url, 0, 1) == "/") $url = substr($url, 1);
147
148                         // No, then extend it with our base URL
149                         $url = $this->getConfigInstance()->readConfig('base_url') . "/" . $url;
150                 } // END - if
151
152                 // Add redirect header
153                 $this->addHeader("Location", $url);
154
155                 // Set correct response status
156                 $this->setResponseStatus("301 Moved Permanently");
157
158                 // Clear the body
159                 $this->setResponseBody("");
160
161                 // Flush the result
162                 $this->flushBuffer();
163
164                 // All done here...
165                 exit();
166         }
167
168         /**
169          * Expires the given cookie if it is set
170          *
171          * @param       $cookieName             Cookie to expire
172          * @return      void
173          */
174         public function expireCookie ($cookieName) {
175                 // Is the cookie there?
176                 if (isset($_COOKIE[$cookieName])) {
177                         // Then expire it with 20 minutes past
178                         $this->addCookie($cookieName, "", false, (time() - 1200));
179
180                         // Remove it from array
181                         unset($_COOKIE[$cookieName]);
182                 } // END - if
183         }
184
185         /**
186          * Refreshs a given cookie. This will make the cookie live longer
187          *
188          * @param       $cookieName             Cookie to refresh
189          * @return      void
190          */
191         public function refreshCookie ($cookieName) {
192                 // Only update existing cookies
193                 if (isset($_COOKIE[$cookieName])) {
194                         // Update the cookie
195                         $this->addCookie($cookieName, $_COOKIE[$cookieName], false);
196                 } // END - if
197         }
198
199         /**
200          * Getter for default command
201          *
202          * @return      $defaultCommand         Default command for this response
203          */
204         public function getDefaultCommand () {
205                 $defaultCommand = $this->getConfigInstance()->readConfig('default_web_command');
206                 return $defaultCommand;
207         }
208 }
209
210 // [EOF]
211 ?>