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__);
39 * Creates an object of this class
41 * @param $applicationInstance An instance of a manageable application
42 * @return $responseInstance A prepared instance of this class
44 public static final function createImageResponse (ManageableApplication $applicationInstance) {
46 $responseInstance = new ImageResponse();
48 // Set the application instance
49 $responseInstance->setApplicationInstance($applicationInstance);
51 // Initialize the template engine here
52 $responseInstance->initTemplateEngine($applicationInstance);
54 // Return the prepared instance
55 return $responseInstance;
59 * Initializes the template engine instance
61 * @param $applicationInstance An instance of a manageable application
64 public final function initTemplateEngine (ManageableApplication $applicationInstance) {
65 // Get config instance
66 $cfg = $this->getConfigInstance();
68 // Set new template engine
69 $cfg->setConfigEntry('html_template_class' , $cfg->getConfigEntry('image_template_class'));
70 $cfg->setConfigEntry('raw_template_extension' , '.img');
71 $cfg->setConfigEntry('code_template_extension', '.xml');
72 $cfg->setConfigEntry('tpl_base_path' , 'templates/images/');
73 $cfg->setConfigEntry('code_template_type' , 'image');
75 // Get a prepared instance
76 $this->setTemplateInstance($this->prepareTemplateInstance($applicationInstance));
80 * Adds a cookie to the response
82 * @param $cookieName Cookie's name
83 * @param $cookieValue Value to store in the cookie
84 * @param $encrypted Do some extra encryption on the value
85 * @param $expires Timestamp of expiration (default: configured)
87 * @throws ResponseHeadersAlreadySentException If headers are already sent
88 * @todo Encryption of cookie data not yet supported.
89 * @todo Why are these parameters conflicting?
90 * @todo If the return statement is removed and setcookie() commented out,
91 * @todo this will send only one cookie out, the first one.
93 public function addCookie ($cookieName, $cookieValue, $encrypted = FALSE, $expires = NULL) {
94 // Are headers already sent?
96 // Throw an exception here
97 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
100 // Shall we encrypt the cookie?
101 if ($encrypted === TRUE) {
102 // Unsupported at the moment
103 $this->partialStub('Encryption is unsupported at the moment.');
106 // For slow browsers set the cookie array element first
107 $_COOKIE[$cookieName] = $cookieValue;
109 // Get all config entries
110 if (is_null($expires)) {
111 $expires = (time() + $this->getConfigInstance()->getConfigEntry('cookie_expire'));
114 $path = $this->getConfigInstance()->getConfigEntry('cookie_path');
115 $domain = $this->getConfigInstance()->getConfigEntry('cookie_domain');
117 setcookie($cookieName, $cookieValue, $expires);
118 //, $path, $domain, (isset($_SERVER['HTTPS']))
121 // Now construct the full header
122 $cookieString = $cookieName . '=' . $cookieValue . '; ';
123 $cookieString .= 'expires=' . date('D, d-F-Y H:i:s', $expires) . ' GMT';
124 // TODO Why is this not always working? $cookieString .= '; path=' . $path . '; domain=' . $domain;
126 // Set the cookie as a header
127 $this->cookies[$cookieName] = $cookieString;
131 * Redirect to a configured URL. The URL can be absolute or relative. In
132 * case of relative URL it will be extended automatically.
134 * @param $configEntry The configuration entry which holds our URL
136 * @throws ResponseHeadersAlreadySentException If headers are already sent
138 public function redirectToConfiguredUrl ($configEntry) {
139 // Is the header not yet sent?
140 if (headers_sent()) {
141 // Throw an exception here
142 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
145 // Assign application data
146 $this->getTemplateInstance()->assignApplicationData($this->getApplicationInstance());
148 // Get the url from config
149 $url = $this->getConfigInstance()->getConfigEntry($configEntry . '_url');
152 $url = $this->getTemplateInstance()->compileRawCode($url);
154 // Do we have a 'http' in front of the URL?
155 if (substr(strtolower($url), 0, 4) != 'http') {
156 // Is there a / in front of the relative URL?
157 if (substr($url, 0, 1) == '/') $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 * Flushs the cached HTTP response to the outer world
182 * @param $force Whether we shall force the output or abort if headers are
183 * already sent with an exception
186 public function flushBuffer ($force = FALSE) {
188 $this->getImageInstance()->finishImage();
191 $content = $this->getImageInstance()->getContent();
193 // Set it as response body
194 $this->setResponseBody($content);
197 $this->addHeader('Content-type', 'image/' . $this->getImageInstance()->getImageType());
199 // Call parent method
200 parent::flushBuffer($force);
204 * Expires the given cookie if it is set
206 * @param $cookieName Cookie to expire
209 public function expireCookie ($cookieName) {
210 // Is the cookie there?
211 if (isset($_COOKIE[$cookieName])) {
212 // Then expire it with 20 minutes past
213 $this->addCookie($cookieName, '', FALSE, (time() - 1200));
215 // Remove it from array
216 unset($_COOKIE[$cookieName]);
221 * Refreshs a given cookie. This will make the cookie live longer
223 * @param $cookieName Cookie to refresh
226 public function refreshCookie ($cookieName) {
227 // Only update existing cookies
228 if (isset($_COOKIE[$cookieName])) {
230 $this->addCookie($cookieName, $_COOKIE[$cookieName], FALSE);
235 * Getter for default command
237 * @return $defaultCommand Default command for this response
239 public function getDefaultCommand () {
240 $defaultCommand = $this->getConfigInstance()->getConfigEntry('default_image_command');
241 return $defaultCommand;