3 * A class for an image response on an HTTP request
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.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 ImageResponse extends BaseResponse implements Responseable {
29 * Protected constructor
33 protected function __construct () {
34 // Call parent constructor
35 parent::__construct(__CLASS__);
38 $this->setResponseType('image');
42 * Creates an object of this class
44 * @param $applicationInstance An instance of a manageable application
45 * @return $responseInstance A prepared instance of this class
47 public static final function createImageResponse (ManageableApplication $applicationInstance) {
49 $responseInstance = new ImageResponse();
51 // Set the application instance
52 $responseInstance->setApplicationInstance($applicationInstance);
54 // Initialize the template engine here
55 $responseInstance->initTemplateEngine($applicationInstance);
57 // Init web output instance
58 $responseInstance->initWebOutputInstance();
60 // Return the prepared instance
61 return $responseInstance;
65 * Initializes the template engine instance
67 * @param $applicationInstance An instance of a manageable application
70 public final function initTemplateEngine (ManageableApplication $applicationInstance) {
71 // Get config instance
72 $cfg = $this->getConfigInstance();
74 // Set new template engine
75 $cfg->setConfigEntry('html_template_class' , $cfg->getConfigEntry('image_template_class'));
76 $cfg->setConfigEntry('raw_template_extension' , '.img');
77 $cfg->setConfigEntry('code_template_extension', '.xml');
78 $cfg->setConfigEntry('tpl_base_path' , 'templates/images/');
79 $cfg->setConfigEntry('code_template_type' , 'image');
81 // Get a prepared instance
82 $this->setTemplateInstance($this->prepareTemplateInstance($applicationInstance));
86 * Adds a cookie to the response
88 * @param $cookieName Cookie's name
89 * @param $cookieValue Value to store in the cookie
90 * @param $encrypted Do some extra encryption on the value
91 * @param $expires Timestamp of expiration (default: configured)
93 * @throws ResponseHeadersAlreadySentException If headers are already sent
94 * @todo Encryption of cookie data not yet supported.
95 * @todo Why are these parameters conflicting?
96 * @todo If the return statement is removed and setcookie() commented out,
97 * @todo this will send only one cookie out, the first one.
99 public function addCookie ($cookieName, $cookieValue, $encrypted = FALSE, $expires = NULL) {
100 // Are headers already sent?
101 if (headers_sent()) {
102 // Throw an exception here
103 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
106 // Shall we encrypt the cookie?
107 if ($encrypted === TRUE) {
108 // Unsupported at the moment
109 $this->partialStub('Encryption is unsupported at the moment.');
112 // For slow browsers set the cookie array element first
113 $_COOKIE[$cookieName] = $cookieValue;
115 // Get all config entries
116 if (is_null($expires)) {
117 $expires = (time() + $this->getConfigInstance()->getConfigEntry('cookie_expire'));
120 $path = $this->getConfigInstance()->getConfigEntry('cookie_path');
121 $domain = $this->getConfigInstance()->getConfigEntry('cookie_domain');
123 setcookie($cookieName, $cookieValue, $expires);
124 //, $path, $domain, (isset($_SERVER['HTTPS']))
127 // Now construct the full header
128 $cookieString = $cookieName . '=' . $cookieValue . '; ';
129 $cookieString .= 'expires=' . date('D, d-F-Y H:i:s', $expires) . ' GMT';
130 // TODO Why is this not always working? $cookieString .= '; path=' . $path . '; domain=' . $domain;
132 // Set the cookie as a header
133 $this->cookies[$cookieName] = $cookieString;
137 * Redirect to a configured URL. The URL can be absolute or relative. In
138 * case of relative URL it will be extended automatically.
140 * @param $configEntry The configuration entry which holds our URL
142 * @throws ResponseHeadersAlreadySentException If headers are already sent
144 public function redirectToConfiguredUrl ($configEntry) {
145 // Is the header not yet sent?
146 if (headers_sent()) {
147 // Throw an exception here
148 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
151 // Assign application data
152 $this->getTemplateInstance()->assignApplicationData($this->getApplicationInstance());
154 // Get the url from config
155 $url = $this->getConfigInstance()->getConfigEntry($configEntry . '_url');
158 $url = $this->getTemplateInstance()->compileRawCode($url);
160 // Do we have a 'http' in front of the URL?
161 if (substr(strtolower($url), 0, 4) != 'http') {
162 // Is there a / in front of the relative URL?
163 if (substr($url, 0, 1) == '/') $url = substr($url, 1);
165 // No, then extend it with our base URL
166 $url = $this->getConfigInstance()->getConfigEntry('base_url') . '/' . $url;
169 // Add redirect header
170 $this->addHeader('Location', str_replace('&', '&', $url));
172 // Set correct response status
173 $this->setResponseStatus('301 Moved Permanently');
176 $this->setResponseBody('');
179 $this->flushBuffer();
186 * Flushs the cached HTTP response to the outer world
188 * @param $force Whether we shall force the output or abort if headers are
189 * already sent with an exception
192 public function flushBuffer ($force = FALSE) {
194 $this->getImageInstance()->finishImage();
197 $content = $this->getImageInstance()->getContent();
199 // Set it as response body
200 $this->setResponseBody($content);
203 $this->addHeader('Content-type', 'image/' . $this->getImageInstance()->getImageType());
205 // Call parent method
206 parent::flushBuffer($force);
210 * Expires the given cookie if it is set
212 * @param $cookieName Cookie to expire
215 public function expireCookie ($cookieName) {
216 // Is the cookie there?
217 if (isset($_COOKIE[$cookieName])) {
218 // Then expire it with 20 minutes past
219 $this->addCookie($cookieName, '', FALSE, (time() - 1200));
221 // Remove it from array
222 unset($_COOKIE[$cookieName]);
227 * Refreshs a given cookie. This will make the cookie live longer
229 * @param $cookieName Cookie to refresh
232 public function refreshCookie ($cookieName) {
233 // Only update existing cookies
234 if (isset($_COOKIE[$cookieName])) {
236 $this->addCookie($cookieName, $_COOKIE[$cookieName], FALSE);