3 namespace CoreFramework\Response;
5 // Import framework stuff
6 use CoreFramework\Manager\ManageableApplication;
9 * A class for an HTML response on an HTML request
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 * The extended headers are taken from phpMyAdmin setup tool, written by
31 * Michal Cihar <michal@cihar.com>, licensed under GNU GPL 2.0.
33 class HtmlResponse extends BaseResponse implements Responseable {
35 * Protected constructor
39 protected function __construct () {
40 // Call parent constructor
41 parent::__construct(__CLASS__);
44 $this->setResponseType('html');
48 * Creates an object of this class
50 * @param $applicationInstance An instance of a manageable application
51 * @return $responseInstance A prepared instance of this class
53 public static final function createHtmlResponse (ManageableApplication $applicationInstance) {
55 $responseInstance = new HtmlResponse();
57 // Set the application instance
58 $responseInstance->setApplicationInstance($applicationInstance);
60 // Initialize the template engine here
61 $responseInstance->initTemplateEngine($applicationInstance);
63 // Init web output instance
64 $responseInstance->initWebOutputInstance();
66 // Return the prepared instance
67 return $responseInstance;
71 * Initializes the template engine instance
73 * @param $applicationInstance An instance of a manageable application
76 public final function initTemplateEngine (ManageableApplication $applicationInstance) {
77 $this->setTemplateInstance($this->prepareTemplateInstance($applicationInstance));
81 * Adds a cookie to the response
83 * @param $cookieName Cookie's name
84 * @param $cookieValue Value to store in the cookie
85 * @param $encrypted Do some extra encryption on the value
86 * @param $expires Timestamp of expiration (default: configured)
88 * @throws ResponseHeadersAlreadySentException If headers are already sent
89 * @todo Encryption of cookie data not yet supported.
90 * @todo Why are these parameters conflicting?
91 * @todo If the return statement is removed and setcookie() commented out,
92 * @todo this will send only one cookie out, the first one.
94 public function addCookie ($cookieName, $cookieValue, $encrypted = FALSE, $expires = NULL) {
95 //* DEBUG: */ echo $cookieName.'='.$cookieValue."<br />\n";
96 // Are headers already sent?
98 // Throw an exception here
100 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
103 // Shall we encrypt the cookie?
104 if ($encrypted === TRUE) {
105 // Unsupported at the moment
106 $this->partialStub('Encryption is unsupported at the moment.');
109 // For slow browsers set the cookie array element first
110 $_COOKIE[$cookieName] = $cookieValue;
112 // Get all config entries
113 if (is_null($expires)) {
114 $expires = (time() + $this->getConfigInstance()->getConfigEntry('cookie_expire'));
117 $path = $this->getConfigInstance()->getConfigEntry('cookie_path');
118 $domain = $this->getConfigInstance()->getConfigEntry('cookie_domain');
120 setcookie($cookieName, $cookieValue, $expires);
121 //, $path, $domain, (isset($_SERVER['HTTPS']))
124 // Now construct the full header
125 $cookieString = $cookieName . '=' . $cookieValue . '; ';
126 $cookieString .= 'expires=' . date('D, d-F-Y H:i:s', $expires) . ' GMT';
127 // $cookieString .= "; path=".$path."; domain=".$domain;
129 // Set the cookie as a header
130 $this->cookies[$cookieName] = $cookieString;
134 * Redirect to a configured URL. The URL can be absolute or relative. In
135 * case of relative URL it will be extended automatically.
137 * @param $configEntry The configuration entry which holds our URL
139 * @throws ResponseHeadersAlreadySentException If headers are already sent
141 public function redirectToConfiguredUrl ($configEntry) {
142 // Is the header not yet sent?
143 if (headers_sent()) {
144 // Throw an exception here
145 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
148 // Assign application data
149 $this->getTemplateInstance()->assignApplicationData($this->getApplicationInstance());
151 // Get the url from config
152 $url = $this->getConfigInstance()->getConfigEntry($configEntry . '_url');
155 $url = $this->getTemplateInstance()->compileRawCode($url);
157 // Do we have a 'http' in front of the URL?
158 if (substr(strtolower($url), 0, 4) != 'http') {
159 // Is there a / in front of the relative URL?
160 if (substr($url, 0, 1) == '/') {
161 $url = substr($url, 1);
164 // No, then extend it with our base URL
165 $url = $this->getConfigInstance()->getConfigEntry('base_url') . '/' . $url;
168 // Add redirect header
169 $this->addHeader('Location', str_replace('&', '&', $url));
171 // Set correct response status
172 $this->setResponseStatus('301 Moved Permanently');
175 $this->setResponseBody('');
178 $this->flushBuffer();
185 * Expires the given cookie if it is set
187 * @param $cookieName Cookie to expire
190 public function expireCookie ($cookieName) {
191 // Is the cookie there?
192 if (isset($_COOKIE[$cookieName])) {
193 // Then expire it with 20 minutes past
194 $this->addCookie($cookieName, '', FALSE, (time() - 1200));
196 // Remove it from array
197 unset($_COOKIE[$cookieName]);
202 * Refreshs a given cookie. This will make the cookie live longer
204 * @param $cookieName Cookie to refresh
207 public function refreshCookie ($cookieName) {
208 // Only update existing cookies
209 if (isset($_COOKIE[$cookieName])) {
211 $this->addCookie($cookieName, $_COOKIE[$cookieName], FALSE);