Copyright upgraded to 2010
[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, 2009, 2010 Core Developer Team
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                         // Unsupported at the moment
94                         $this->partialStub('Encryption is unsupported at the moment.');
95                 } // END - if
96
97                 // For slow browsers set the cookie array element first
98                 $_COOKIE[$cookieName] = $cookieValue;
99
100                 // Get all config entries
101                 if (is_null($expires)) {
102                         $expires = (time() + $this->getConfigInstance()->getConfigEntry('cookie_expire'));
103                 } // END - if
104
105                 $path = $this->getConfigInstance()->getConfigEntry('cookie_path');
106                 $domain = $this->getConfigInstance()->getConfigEntry('cookie_domain');
107
108                 setcookie($cookieName, $cookieValue, $expires);
109                 //, $path, $domain, (isset($_SERVER['HTTPS']))
110                 return;
111
112                 // Now construct the full header
113                 $cookieString = $cookieName . '=' . $cookieValue . '; ';
114                 $cookieString .= 'expires=' . date('D, d-F-Y H:i:s', $expires) . ' GMT';
115                 // $cookieString .= "; path=".$path."; domain=".$domain;
116
117                 // Set the cookie as a header
118                 $this->cookies[$cookieName] = $cookieString;
119         }
120
121         /**
122          * Redirect to a configured URL. The URL can be absolute or relative. In
123          * case of relative URL it will be extended automatically.
124          *
125          * @param       $configEntry    The configuration entry which holds our URL
126          * @return      void
127          * @throws      ResponseHeadersAlreadySentException             If headers are already sent
128          */
129         public function redirectToConfiguredUrl ($configEntry) {
130                 // Is the header not yet sent?
131                 if (headers_sent()) {
132                         // Throw an exception here
133                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
134                 } // END - if
135
136                 // Assign application data
137                 $this->getTemplateInstance()->assignApplicationData($this->getApplicationInstance());
138
139                 // Get the url from config
140                 $url = $this->getConfigInstance()->getConfigEntry($configEntry . '_url');
141
142                 // Compile the URL
143                 $url = $this->getTemplateInstance()->compileRawCode($url);
144
145                 // Do we have a 'http' in front of the URL?
146                 if (substr(strtolower($url), 0, 4) != 'http') {
147                         // Is there a / in front of the relative URL?
148                         if (substr($url, 0, 1) == '/') $url = substr($url, 1);
149
150                         // No, then extend it with our base URL
151                         $url = $this->getConfigInstance()->getConfigEntry('base_url') . '/' . $url;
152                 } // END - if
153
154                 // Add redirect header
155                 $this->addHeader('Location', str_replace('&amp;', '&', $url));
156
157                 // Set correct response status
158                 $this->setResponseStatus('301 Moved Permanently');
159
160                 // Clear the body
161                 $this->setResponseBody('');
162
163                 // Flush the result
164                 $this->flushBuffer();
165
166                 // All done here...
167                 exit();
168         }
169
170         /**
171          * Expires the given cookie if it is set
172          *
173          * @param       $cookieName             Cookie to expire
174          * @return      void
175          */
176         public function expireCookie ($cookieName) {
177                 // Is the cookie there?
178                 if (isset($_COOKIE[$cookieName])) {
179                         // Then expire it with 20 minutes past
180                         $this->addCookie($cookieName, '', false, (time() - 1200));
181
182                         // Remove it from array
183                         unset($_COOKIE[$cookieName]);
184                 } // END - if
185         }
186
187         /**
188          * Refreshs a given cookie. This will make the cookie live longer
189          *
190          * @param       $cookieName             Cookie to refresh
191          * @return      void
192          */
193         public function refreshCookie ($cookieName) {
194                 // Only update existing cookies
195                 if (isset($_COOKIE[$cookieName])) {
196                         // Update the cookie
197                         $this->addCookie($cookieName, $_COOKIE[$cookieName], false);
198                 } // END - if
199         }
200
201         /**
202          * Getter for default command
203          *
204          * @return      $defaultCommand         Default command for this response
205          */
206         public function getDefaultCommand () {
207                 $defaultCommand = $this->getConfigInstance()->getConfigEntry('default_web_command');
208                 return $defaultCommand;
209         }
210 }
211
212 // [EOF]
213 ?>