3 namespace Org\Mxchange\CoreFramework\Response;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Manager\ManageableApplication;
7 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
8 use Org\Mxchange\CoreFramework\Response\Responseable;
11 * A class for an HTML response on an HTML request
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 * The extended headers are taken from phpMyAdmin setup tool, written by
33 * Michal Cihar <michal@cihar.com>, licensed under GNU GPL 2.0.
35 class HtmlResponse extends BaseResponse implements Responseable {
37 * Protected constructor
41 protected function __construct () {
42 // Call parent constructor
43 parent::__construct(__CLASS__);
46 $this->setResponseType('http');
50 * Creates an object of this class
52 * @return $responseInstance A prepared instance of this class
54 public static final function createHtmlResponse () {
56 $responseInstance = new HtmlResponse();
58 // Return the prepared instance
59 return $responseInstance;
63 * Initializes the template engine instance
65 * @param $applicationInstance An instance of a manageable application
68 public final function initTemplateEngine (ManageableApplication $applicationInstance) {
69 $this->setTemplateInstance($this->prepareTemplateInstance($applicationInstance));
73 * Adds a cookie to the response
75 * @param $cookieName Cookie's name
76 * @param $cookieValue Value to store in the cookie
77 * @param $encrypted Do some extra encryption on the value
78 * @param $expires Timestamp of expiration (default: configured)
80 * @throws ResponseHeadersAlreadySentException If headers are already sent
81 * @todo Encryption of cookie data not yet supported.
82 * @todo Why are these parameters conflicting?
83 * @todo If the return statement is removed and setcookie() commented out,
84 * @todo this will send only one cookie out, the first one.
86 public function addCookie ($cookieName, $cookieValue, $encrypted = false, $expires = NULL) {
87 //* DEBUG: */ echo $cookieName.'='.$cookieValue."<br />\n";
88 // Are headers already sent?
90 // Throw an exception here
92 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
95 // Shall we encrypt the cookie?
96 if ($encrypted === true) {
97 // Unsupported at the moment
98 $this->partialStub('Encryption is unsupported at the moment.');
101 // For slow browsers set the cookie array element first
102 $_COOKIE[$cookieName] = $cookieValue;
104 // Get all config entries
105 if (is_null($expires)) {
106 $expires = (time() + $this->getConfigInstance()->getConfigEntry('cookie_expire'));
109 $path = $this->getConfigInstance()->getConfigEntry('cookie_path');
110 $domain = $this->getConfigInstance()->getConfigEntry('cookie_domain');
112 setcookie($cookieName, $cookieValue, $expires);
113 //, $path, $domain, (isset($_SERVER['HTTPS']))
116 // Now construct the full header
117 $cookieString = $cookieName . '=' . $cookieValue . '; ';
118 $cookieString .= 'expires=' . date('D, d-F-Y H:i:s', $expires) . ' GMT';
119 // $cookieString .= "; path=".$path."; domain=".$domain;
121 // Set the cookie as a header
122 $this->cookies[$cookieName] = $cookieString;
126 * Redirect to a configured URL. The URL can be absolute or relative. In
127 * case of relative URL it will be extended automatically.
129 * @param $configEntry The configuration entry which holds our URL
131 * @throws ResponseHeadersAlreadySentException If headers are already sent
133 public function redirectToConfiguredUrl ($configEntry) {
134 // Get application instance
135 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
137 // Is the header not yet sent?
138 if (headers_sent()) {
139 // Throw an exception here
140 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
143 // Assign application data
144 $this->getTemplateInstance()->assignApplicationData($applicationInstance);
146 // Get the url from config
147 $url = $this->getConfigInstance()->getConfigEntry($configEntry . '_url');
150 $url = $this->getTemplateInstance()->compileRawCode($url);
152 // Do we have a 'http' in front of the URL?
153 if (substr(strtolower($url), 0, 4) != 'http') {
154 // Is there a / in front of the relative URL?
155 if (substr($url, 0, 1) == '/') {
156 $url = substr($url, 1);
159 // No, then extend it with our base URL
160 $url = $this->getConfigInstance()->getConfigEntry('base_url') . '/' . $url;
163 // Add redirect header
164 $this->addHeader('Location', str_replace('&', '&', $url));
166 // Set correct response status
167 $this->setResponseStatus('301 Moved Permanently');
170 $this->setResponseBody('');
173 $this->flushBuffer();
180 * Expires the given cookie if it is set
182 * @param $cookieName Cookie to expire
185 public function expireCookie ($cookieName) {
186 // Is the cookie there?
187 if (isset($_COOKIE[$cookieName])) {
188 // Then expire it with 20 minutes past
189 $this->addCookie($cookieName, '', false, (time() - 1200));
191 // Remove it from array
192 unset($_COOKIE[$cookieName]);
197 * Refreshs a given cookie. This will make the cookie live longer
199 * @param $cookieName Cookie to refresh
202 public function refreshCookie ($cookieName) {
203 // Only update existing cookies
204 if (isset($_COOKIE[$cookieName])) {
206 $this->addCookie($cookieName, $_COOKIE[$cookieName], false);