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, this is free software
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 $appInstance An instance of a manageable application
42 * @return $responseInstance A prepared instance of this class
44 public final static function createHttpResponse (ManageableApplication $appInstance) {
46 $responseInstance = new HttpResponse();
48 // Set the application instance
49 $responseInstance->setApplicationInstance($appInstance);
51 // Initialize the template engine here
52 $responseInstance->initTemplateEngine($appInstance);
54 // Return the prepared instance
55 return $responseInstance;
59 * Initializes the template engine instance
61 * @param $appInstance An instance of a manageable application
64 public final function initTemplateEngine (ManageableApplication $appInstance) {
65 $this->setTemplateInstance($this->prepareTemplateInstance($appInstance));
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) {
95 // For slow browsers set the cookie array element first
96 $_COOKIE[$cookieName] = $cookieValue;
98 // Get all config entries
99 if (is_null($expires)) {
100 $expires = (time() + $this->getConfigInstance()->readConfig('cookie_expire'));
103 $path = $this->getConfigInstance()->readConfig('cookie_path');
104 $domain = $this->getConfigInstance()->readConfig('cookie_domain');
106 setcookie($cookieName, $cookieValue, $expires);
107 //, $path, $domain, (isset($_SERVER['HTTPS']))
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;
115 // Set the cookie as a header
116 $this->cookies[$cookieName] = $cookieString;
120 * Redirect to a configured URL. The URL can be absolute or relative. In
121 * case of relative URL it will be extended automatically.
123 * @param $configEntry The configuration entry which holds our URL
125 * @throws ResponseHeadersAlreadySentException If headers are already sent
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);
134 // Assign application data
135 $this->getTemplateInstance()->assignApplicationData($this->getApplicationInstance());
137 // Get the url from config
138 $url = $this->getConfigInstance()->readConfig($configEntry);
141 $url = $this->getTemplateInstance()->compileRawCode($url);
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);
148 // No, then extend it with our base URL
149 $url = $this->getConfigInstance()->readConfig('base_url') . "/" . $url;
152 // Add redirect header
153 $this->addHeader("Location", $url);
155 // Set correct response status
156 $this->setResponseStatus("301 Moved Permanently");
159 $this->setResponseBody("");
162 $this->flushBuffer();
169 * Expires the given cookie if it is set
171 * @param $cookieName Cookie to expire
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));
180 // Remove it from array
181 unset($_COOKIE[$cookieName]);
186 * Refreshs a given cookie. This will make the cookie live longer
188 * @param $cookieName Cookie to refresh
191 public function refreshCookie ($cookieName) {
192 // Only update existing cookies
193 if (isset($_COOKIE[$cookieName])) {
195 $this->addCookie($cookieName, $_COOKIE[$cookieName], false);
200 * Getter for default command
202 * @return $defaultCommand Default command for this response
204 public function getDefaultCommand () {
205 $defaultCommand = $this->getConfigInstance()->readConfig('default_web_command');
206 return $defaultCommand;