3 namespace Org\Mxchange\CoreFramework\Response;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Registry\Registry;
7 use Org\Mxchange\CoreFramework\Response\Responseable;
10 * A class for an HTML response on an HTML request
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 <<<<<<< HEAD:framework/main/classes/response/html/class_HtmlResponse.php
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
18 >>>>>>> Some updates::inc/main/classes/response/html/class_HtmlResponse.php
19 * @license GNU GPL 3.0 or any newer version
20 * @link http://www.shipsimu.org
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 * The extended headers are taken from phpMyAdmin setup tool, written by
36 * Michal Cihar <michal@cihar.com>, licensed under GNU GPL 2.0.
38 class HtmlResponse extends BaseResponse implements Responseable {
40 * Protected constructor
44 protected function __construct () {
45 // Call parent constructor
46 parent::__construct(__CLASS__);
49 $this->setResponseType('http');
53 * Creates an object of this class
55 * @return $responseInstance A prepared instance of this class
57 public static final function createHtmlResponse () {
59 $responseInstance = new HtmlResponse();
61 // Return the prepared instance
62 return $responseInstance;
66 * Initializes the template engine instance
68 * @param $applicationInstance An instance of a manageable application
71 public final function initTemplateEngine (ManageableApplication $applicationInstance) {
72 $this->setTemplateInstance($this->prepareTemplateInstance($applicationInstance));
76 * Adds a cookie to the response
78 * @param $cookieName Cookie's name
79 * @param $cookieValue Value to store in the cookie
80 * @param $encrypted Do some extra encryption on the value
81 * @param $expires Timestamp of expiration (default: configured)
83 * @throws ResponseHeadersAlreadySentException If headers are already sent
84 * @todo Encryption of cookie data not yet supported.
85 * @todo Why are these parameters conflicting?
86 * @todo If the return statement is removed and setcookie() commented out,
87 * @todo this will send only one cookie out, the first one.
89 public function addCookie ($cookieName, $cookieValue, $encrypted = false, $expires = NULL) {
90 //* DEBUG: */ echo $cookieName.'='.$cookieValue."<br />\n";
91 // Are headers already sent?
93 // Throw an exception here
95 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
98 // Shall we encrypt the cookie?
99 if ($encrypted === true) {
100 // Unsupported at the moment
101 $this->partialStub('Encryption is unsupported at the moment.');
104 // For slow browsers set the cookie array element first
105 $_COOKIE[$cookieName] = $cookieValue;
107 // Get all config entries
108 if (is_null($expires)) {
109 $expires = (time() + $this->getConfigInstance()->getConfigEntry('cookie_expire'));
112 $path = $this->getConfigInstance()->getConfigEntry('cookie_path');
113 $domain = $this->getConfigInstance()->getConfigEntry('cookie_domain');
115 setcookie($cookieName, $cookieValue, $expires);
116 //, $path, $domain, (isset($_SERVER['HTTPS']))
119 // Now construct the full header
120 $cookieString = $cookieName . '=' . $cookieValue . '; ';
121 $cookieString .= 'expires=' . date('D, d-F-Y H:i:s', $expires) . ' GMT';
122 // $cookieString .= "; path=".$path."; domain=".$domain;
124 // Set the cookie as a header
125 $this->cookies[$cookieName] = $cookieString;
129 * Redirect to a configured URL. The URL can be absolute or relative. In
130 * case of relative URL it will be extended automatically.
132 * @param $configEntry The configuration entry which holds our URL
134 * @throws ResponseHeadersAlreadySentException If headers are already sent
136 public function redirectToConfiguredUrl ($configEntry) {
137 // Get application instance
138 $applicationInstance = Registry::getRegistry()->getInstance('app');
140 // Is the header not yet sent?
141 if (headers_sent()) {
142 // Throw an exception here
143 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
146 // Assign application data
147 $this->getTemplateInstance()->assignApplicationData($applicationInstance());
149 // Get the url from config
150 $url = $this->getConfigInstance()->getConfigEntry($configEntry . '_url');
153 $url = $this->getTemplateInstance()->compileRawCode($url);
155 // Do we have a 'http' in front of the URL?
156 if (substr(strtolower($url), 0, 4) != 'http') {
157 // Is there a / in front of the relative URL?
158 if (substr($url, 0, 1) == '/') {
159 $url = substr($url, 1);
162 // No, then extend it with our base URL
163 $url = $this->getConfigInstance()->getConfigEntry('base_url') . '/' . $url;
166 // Add redirect header
167 $this->addHeader('Location', str_replace('&', '&', $url));
169 // Set correct response status
170 $this->setResponseStatus('301 Moved Permanently');
173 $this->setResponseBody('');
176 $this->flushBuffer();
183 * Expires the given cookie if it is set
185 * @param $cookieName Cookie to expire
188 public function expireCookie ($cookieName) {
189 // Is the cookie there?
190 if (isset($_COOKIE[$cookieName])) {
191 // Then expire it with 20 minutes past
192 $this->addCookie($cookieName, '', false, (time() - 1200));
194 // Remove it from array
195 unset($_COOKIE[$cookieName]);
200 * Refreshs a given cookie. This will make the cookie live longer
202 * @param $cookieName Cookie to refresh
205 public function refreshCookie ($cookieName) {
206 // Only update existing cookies
207 if (isset($_COOKIE[$cookieName])) {
209 $this->addCookie($cookieName, $_COOKIE[$cookieName], false);