3 * A class for an HTTP response on an HTTP request
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
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.
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.
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/>.
24 * The extended headers are taken from phpMyAdmin setup tool, written by
25 * Michal Cihar <michal@cihar.com>, licensed under GNU GPL 2.0.
27 class HttpResponse extends BaseResponse implements Responseable {
29 * Protected constructor
33 protected function __construct () {
34 // Call parent constructor
35 parent::__construct(__CLASS__);
39 * Creates an object of this class
41 * @param $applicationInstance An instance of a manageable application
42 * @return $responseInstance A prepared instance of this class
44 public static final function createHttpResponse (ManageableApplication $applicationInstance) {
46 $responseInstance = new HttpResponse();
48 // Set the application instance
49 $responseInstance->setApplicationInstance($applicationInstance);
51 // Initialize the template engine here
52 $responseInstance->initTemplateEngine($applicationInstance);
54 // Return the prepared instance
55 return $responseInstance;
59 * Initializes the template engine instance
61 * @param $applicationInstance An instance of a manageable application
64 public final function initTemplateEngine (ManageableApplication $applicationInstance) {
65 $this->setTemplateInstance($this->prepareTemplateInstance($applicationInstance));
69 * Adds a cookie to the response
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)
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.
82 public function addCookie ($cookieName, $cookieValue, $encrypted = false, $expires = NULL) {
83 //* DEBUG: */ echo $cookieName.'='.$cookieValue."<br />\n";
84 // Are headers already sent?
86 // Throw an exception here
88 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
91 // Shall we encrypt the cookie?
92 if ($encrypted === true) {
93 // Unsupported at the moment
94 $this->partialStub('Encryption is unsupported at the moment.');
97 // For slow browsers set the cookie array element first
98 $_COOKIE[$cookieName] = $cookieValue;
100 // Get all config entries
101 if (is_null($expires)) {
102 $expires = (time() + $this->getConfigInstance()->getConfigEntry('cookie_expire'));
105 $path = $this->getConfigInstance()->getConfigEntry('cookie_path');
106 $domain = $this->getConfigInstance()->getConfigEntry('cookie_domain');
108 setcookie($cookieName, $cookieValue, $expires);
109 //, $path, $domain, (isset($_SERVER['HTTPS']))
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;
117 // Set the cookie as a header
118 $this->cookies[$cookieName] = $cookieString;
122 * Redirect to a configured URL. The URL can be absolute or relative. In
123 * case of relative URL it will be extended automatically.
125 * @param $configEntry The configuration entry which holds our URL
127 * @throws ResponseHeadersAlreadySentException If headers are already sent
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);
136 // Assign application data
137 $this->getTemplateInstance()->assignApplicationData($this->getApplicationInstance());
139 // Get the url from config
140 $url = $this->getConfigInstance()->getConfigEntry($configEntry . '_url');
143 $url = $this->getTemplateInstance()->compileRawCode($url);
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);
150 // No, then extend it with our base URL
151 $url = $this->getConfigInstance()->getConfigEntry('base_url') . '/' . $url;
154 // Add redirect header
155 $this->addHeader('Location', str_replace('&', '&', $url));
157 // Set correct response status
158 $this->setResponseStatus('301 Moved Permanently');
161 $this->setResponseBody('');
164 $this->flushBuffer();
171 * Expires the given cookie if it is set
173 * @param $cookieName Cookie to expire
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));
182 // Remove it from array
183 unset($_COOKIE[$cookieName]);
188 * Refreshs a given cookie. This will make the cookie live longer
190 * @param $cookieName Cookie to refresh
193 public function refreshCookie ($cookieName) {
194 // Only update existing cookies
195 if (isset($_COOKIE[$cookieName])) {
197 $this->addCookie($cookieName, $_COOKIE[$cookieName], false);
202 * Getter for default command
204 * @return $defaultCommand Default command for this response
206 public function getDefaultCommand () {
207 $defaultCommand = $this->getConfigInstance()->getConfigEntry('default_web_command');
208 return $defaultCommand;