3 namespace Org\Mxchange\CoreFramework\Response;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
7 use Org\Mxchange\CoreFramework\Response\Responseable;
10 * A class for an image response on an HTTP request
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 * The extended headers are taken from phpMyAdmin setup tool, written by
32 * Michal Cihar <michal@cihar.com>, licensed under GNU GPL 2.0.
34 class ImageResponse extends BaseResponse implements Responseable {
36 * Protected constructor
40 protected function __construct () {
41 // Call parent constructor
42 parent::__construct(__CLASS__);
45 $this->setResponseType('image');
49 * Creates an object of this class
51 * @return $responseInstance A prepared instance of this class
53 public static final function createImageResponse () {
55 $responseInstance = new ImageResponse();
57 // Return the prepared instance
58 return $responseInstance;
62 * Initializes the template engine instance
64 * @param $applicationInstance An instance of a manageable application
67 public final function initTemplateEngine (ManageableApplication $applicationInstance) {
68 // Get config instance
69 $cfg = $this->getConfigInstance();
71 // Set new template engine
72 $cfg->setConfigEntry('html_template_class' , $cfg->getConfigEntry('image_template_class'));
73 $cfg->setConfigEntry('raw_template_extension' , '.img');
74 $cfg->setConfigEntry('code_template_extension', '.xml');
75 $cfg->setConfigEntry('tpl_base_path' , 'templates/images/');
76 // @TODO Please fix this
77 $cfg->setConfigEntry('code_template_type' , 'image');
79 // Get a prepared instance
80 $this->setTemplateInstance($this->prepareTemplateInstance($applicationInstance));
84 * Adds a cookie to the response
86 * @param $cookieName Cookie's name
87 * @param $cookieValue Value to store in the cookie
88 * @param $encrypted Do some extra encryption on the value
89 * @param $expires Timestamp of expiration (default: configured)
91 * @throws ResponseHeadersAlreadySentException If headers are already sent
92 * @todo Encryption of cookie data not yet supported.
93 * @todo Why are these parameters conflicting?
94 * @todo If the return statement is removed and setcookie() commented out,
95 * @todo this will send only one cookie out, the first one.
97 public function addCookie ($cookieName, $cookieValue, $encrypted = false, $expires = NULL) {
98 // Are headers already sent?
100 // Throw an exception here
101 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
104 // Shall we encrypt the cookie?
105 if ($encrypted === true) {
106 // Unsupported at the moment
107 $this->partialStub('Encryption is unsupported at the moment.');
110 // For slow browsers set the cookie array element first
111 $_COOKIE[$cookieName] = $cookieValue;
113 // Get all config entries
114 if (is_null($expires)) {
115 $expires = (time() + $this->getConfigInstance()->getConfigEntry('cookie_expire'));
118 $path = $this->getConfigInstance()->getConfigEntry('cookie_path');
119 $domain = $this->getConfigInstance()->getConfigEntry('cookie_domain');
121 setcookie($cookieName, $cookieValue, $expires);
122 //, $path, $domain, (isset($_SERVER['HTTPS']))
125 // Now construct the full header
126 $cookieString = $cookieName . '=' . $cookieValue . '; ';
127 $cookieString .= 'expires=' . date('D, d-F-Y H:i:s', $expires) . ' GMT';
128 // TODO Why is this not always working? $cookieString .= '; path=' . $path . '; domain=' . $domain;
130 // Set the cookie as a header
131 $this->cookies[$cookieName] = $cookieString;
135 * Redirect to a configured URL. The URL can be absolute or relative. In
136 * case of relative URL it will be extended automatically.
138 * @param $configEntry The configuration entry which holds our URL
140 * @throws ResponseHeadersAlreadySentException If headers are already sent
142 public function redirectToConfiguredUrl ($configEntry) {
143 // Get application instance
144 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
146 // Is the header not yet sent?
147 if (headers_sent()) {
148 // Throw an exception here
149 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
152 // Assign application data
153 $this->getTemplateInstance()->assignApplicationData($applicationInstance);
155 // Get the url from config
156 $url = $this->getConfigInstance()->getConfigEntry($configEntry . '_url');
159 $url = $this->getTemplateInstance()->compileRawCode($url);
161 // Do we have a 'http' in front of the URL?
162 if (substr(strtolower($url), 0, 4) != 'http') {
163 // Is there a / in front of the relative URL?
164 if (substr($url, 0, 1) == '/') $url = substr($url, 1);
166 // No, then extend it with our base URL
167 $url = $this->getConfigInstance()->getConfigEntry('base_url') . '/' . $url;
170 // Add redirect header
171 $this->addHeader('Location', str_replace('&', '&', $url));
173 // Set correct response status
174 $this->setResponseStatus('301 Moved Permanently');
177 $this->setResponseBody('');
180 $this->flushBuffer();
187 * Flushs the cached HTTP response to the outer world
189 * @param $force Whether we shall force the output or abort if headers are
190 * already sent with an exception
193 public function flushBuffer ($force = false) {
195 $this->getImageInstance()->finishImage();
198 $content = $this->getImageInstance()->getContent();
200 // Set it as response body
201 $this->setResponseBody($content);
204 $this->addHeader('Content-type', 'image/' . $this->getImageInstance()->getImageType());
206 // Call parent method
207 parent::flushBuffer($force);
211 * Expires the given cookie if it is set
213 * @param $cookieName Cookie to expire
216 public function expireCookie ($cookieName) {
217 // Is the cookie there?
218 if (isset($_COOKIE[$cookieName])) {
219 // Then expire it with 20 minutes past
220 $this->addCookie($cookieName, '', false, (time() - 1200));
222 // Remove it from array
223 unset($_COOKIE[$cookieName]);
228 * Refreshs a given cookie. This will make the cookie live longer
230 * @param $cookieName Cookie to refresh
233 public function refreshCookie ($cookieName) {
234 // Only update existing cookies
235 if (isset($_COOKIE[$cookieName])) {
237 $this->addCookie($cookieName, $_COOKIE[$cookieName], false);