3 * A class for an image response on an HTTP request
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.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 * Instance of the image
31 private $imageInstance = null;
34 * Protected constructor
38 protected function __construct () {
39 // Call parent constructor
40 parent::__construct(__CLASS__);
44 * Creates an object of this class
46 * @param $appInstance An instance of a manageable application
47 * @return $responseInstance A prepared instance of this class
49 public final static function createImageResponse (ManageableApplication $appInstance) {
51 $responseInstance = new ImageResponse();
53 // Set the application instance
54 $responseInstance->setApplicationInstance($appInstance);
56 // Initialize the template engine here
57 $responseInstance->initTemplateEngine($appInstance);
59 // Return the prepared instance
60 return $responseInstance;
64 * Initializes the template engine instance
66 * @param $appInstance An instance of a manageable application
69 public final function initTemplateEngine (ManageableApplication $appInstance) {
70 // Get config instance
71 $cfg = $this->getConfigInstance();
73 // Set new template engine
74 $cfg->setConfigEntry('template_class' , $cfg->readConfig('image_template_class'));
75 $cfg->setConfigEntry('raw_template_extension' , ".img");
76 $cfg->setConfigEntry('code_template_extension', ".itp");
77 $cfg->setConfigEntry('tpl_base_path' , "templates/images/");
78 $cfg->setConfigEntry('code_template_type' , "image");
80 // Get a prepared instance
81 $this->setTemplateInstance($this->prepareTemplateInstance($appInstance));
85 * Adds a cookie to the response
87 * @param $cookieName Cookie's name
88 * @param $cookieValue Value to store in the cookie
89 * @param $encrypted Do some extra encryption on the value
90 * @param $expires Timestamp of expiration (default: configured)
92 * @throws ResponseHeadersAlreadySentException If headers are already sent
93 * @todo Encryption of cookie data not yet supported.
94 * @todo Why are these parameters conflicting?
95 * @todo If the return statement is removed and setcookie() commented out,
96 * @todo this will send only one cookie out, the first one.
98 public function addCookie ($cookieName, $cookieValue, $encrypted = false, $expires = null) {
99 // Are headers already sent?
100 if (headers_sent()) {
101 // Throw an exception here
102 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
105 // Shall we encrypt the cookie?
106 if ($encrypted === true) {
109 // For slow browsers set the cookie array element first
110 $_COOKIE[$cookieName] = $cookieValue;
112 // Get all config entries
113 if (is_null($expires)) {
114 $expires = (time() + $this->getConfigInstance()->readConfig('cookie_expire'));
117 $path = $this->getConfigInstance()->readConfig('cookie_path');
118 $domain = $this->getConfigInstance()->readConfig('cookie_domain');
120 setcookie($cookieName, $cookieValue, $expires);
121 //, $path, $domain, (isset($_SERVER['HTTPS']))
124 // Now construct the full header
125 $cookieString = $cookieName . "=" . $cookieValue . "; ";
126 $cookieString .= "expires=" . date("D, d-F-Y H:i:s", $expires) . " GMT";
127 // $cookieString .= "; path=".$path."; domain=".$domain;
129 // Set the cookie as a header
130 $this->cookies[$cookieName] = $cookieString;
134 * Redirect to a configured URL. The URL can be absolute or relative. In
135 * case of relative URL it will be extended automatically.
137 * @param $configEntry The configuration entry which holds our URL
139 * @throws ResponseHeadersAlreadySentException If headers are already sent
141 public function redirectToConfiguredUrl ($configEntry) {
142 // Is the header not yet sent?
143 if (headers_sent()) {
144 // Throw an exception here
145 throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
148 // Assign application data
149 $this->getTemplateInstance()->assignApplicationData($this->getApplicationInstance());
151 // Get the url from config
152 $url = $this->getConfigInstance()->readConfig($configEntry);
155 $url = $this->getTemplateInstance()->compileRawCode($url);
157 // Do we have a "http" in front of the URL?
158 if (substr(strtolower($url), 0, 4) != "http") {
159 // Is there a / in front of the relative URL?
160 if (substr($url, 0, 1) == "/") $url = substr($url, 1);
162 // No, then extend it with our base URL
163 $url = $this->getConfigInstance()->readConfig('base_url') . "/" . $url;
166 // Add redirect header
167 $this->addHeader("Location", $url);
169 // Set correct response status
170 $this->setResponseStatus("301 Moved Permanently");
173 $this->setResponseBody("");
176 $this->flushBuffer();
183 * Flushs the cached HTTP response to the outer world
185 * @param $force Wether we shall force the output or abort if headers are
186 * already sent with an exception
189 public function flushBuffer ($force = false) {
191 $this->getImageInstance()->finishImage();
194 $content = $this->getImageInstance()->getContent();
196 // Set it as response body
197 $this->setResponseBody($content);
200 $this->addHeader('Content-type', "image/".$this->getImageInstance()->getImageType());
202 // Call parent method
203 parent::flushBuffer($force);
207 * Expires the given cookie if it is set
209 * @param $cookieName Cookie to expire
212 public function expireCookie ($cookieName) {
213 // Is the cookie there?
214 if (isset($_COOKIE[$cookieName])) {
215 // Then expire it with 20 minutes past
216 $this->addCookie($cookieName, "", false, (time() - 1200));
218 // Remove it from array
219 unset($_COOKIE[$cookieName]);
224 * Refreshs a given cookie. This will make the cookie live longer
226 * @param $cookieName Cookie to refresh
229 public function refreshCookie ($cookieName) {
230 // Only update existing cookies
231 if (isset($_COOKIE[$cookieName])) {
233 $this->addCookie($cookieName, $_COOKIE[$cookieName], false);
238 * Setter for image instanxe
240 * @param $imageInstance An instance of an image
243 public final function setImageInstance (BaseImage $imageInstance) {
244 $this->imageInstance = $imageInstance;
248 * Getter for image instanxe
250 * @return $imageInstance An instance of an image
252 public final function getImageInstance () {
253 return $this->imageInstance;
257 * Getter for default command
259 * @return $defaultCommand Default command for this response
261 public function getDefaultCommand () {
262 $defaultCommand = $this->getConfigInstance()->readConfig('default_image_command');
263 return $defaultCommand;