3196607122ad78ba2e0d4cbaa91440719f654bfc
[core.git] / inc / classes / main / response / image / class_ImageResponse.php
1 <?php
2 /**
3  * A class for an image response on an HTTP request
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
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
10  *
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.
15  *
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.
20  *
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/>.
23  *
24  * The extended headers are taken from phpMyAdmin setup tool, written by
25  * Michal Cihar <michal@cihar.com>, licensed under GNU GPL 2.0.
26  */
27 class ImageResponse extends BaseResponse implements Responseable {
28         /**
29          * Protected constructor
30          *
31          * @return      void
32          */
33         protected function __construct () {
34                 // Call parent constructor
35                 parent::__construct(__CLASS__);
36         }
37
38         /**
39          * Creates an object of this class
40          *
41          * @param       $applicationInstance    An instance of a manageable application
42          * @return      $responseInstance               A prepared instance of this class
43          */
44         public static final function createImageResponse (ManageableApplication $applicationInstance) {
45                 // Get a new instance
46                 $responseInstance = new ImageResponse();
47
48                 // Set the application instance
49                 $responseInstance->setApplicationInstance($applicationInstance);
50
51                 // Initialize the template engine here
52                 $responseInstance->initTemplateEngine($applicationInstance);
53
54                 // Return the prepared instance
55                 return $responseInstance;
56         }
57
58         /**
59          * Initializes the template engine instance
60          *
61          * @param       $applicationInstance    An instance of a manageable application
62          * @return      void
63          */
64         public final function initTemplateEngine (ManageableApplication $applicationInstance) {
65                 // Get config instance
66                 $cfg = $this->getConfigInstance();
67
68                 // Set new template engine
69                 $cfg->setConfigEntry('web_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');
74
75                 // Get a prepared instance
76                 $this->setTemplateInstance($this->prepareTemplateInstance($applicationInstance));
77         }
78
79         /**
80          * Adds a cookie to the response
81          *
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)
86          * @return      void
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.
92          */
93         public function addCookie ($cookieName, $cookieValue, $encrypted = FALSE, $expires = NULL) {
94                 // Are headers already sent?
95                 if (headers_sent()) {
96                         // Throw an exception here
97                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
98                 } // END - if
99
100                 // Shall we encrypt the cookie?
101                 if ($encrypted === TRUE) {
102                         // Unsupported at the moment
103                         $this->partialStub('Encryption is unsupported at the moment.');
104                 } // END - if
105
106                 // For slow browsers set the cookie array element first
107                 $_COOKIE[$cookieName] = $cookieValue;
108
109                 // Get all config entries
110                 if (is_null($expires)) {
111                         $expires = (time() + $this->getConfigInstance()->getConfigEntry('cookie_expire'));
112                 } // END - if
113
114                 $path = $this->getConfigInstance()->getConfigEntry('cookie_path');
115                 $domain = $this->getConfigInstance()->getConfigEntry('cookie_domain');
116
117                 setcookie($cookieName, $cookieValue, $expires);
118                 //, $path, $domain, (isset($_SERVER['HTTPS']))
119                 return;
120
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;
125
126                 // Set the cookie as a header
127                 $this->cookies[$cookieName] = $cookieString;
128         }
129
130         /**
131          * Redirect to a configured URL. The URL can be absolute or relative. In
132          * case of relative URL it will be extended automatically.
133          *
134          * @param       $configEntry    The configuration entry which holds our URL
135          * @return      void
136          * @throws      ResponseHeadersAlreadySentException             If headers are already sent
137          */
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);
143                 } // END - if
144
145                 // Assign application data
146                 $this->getTemplateInstance()->assignApplicationData($this->getApplicationInstance());
147
148                 // Get the url from config
149                 $url = $this->getConfigInstance()->getConfigEntry($configEntry . '_url');
150
151                 // Compile the URL
152                 $url = $this->getTemplateInstance()->compileRawCode($url);
153
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);
158
159                         // No, then extend it with our base URL
160                         $url = $this->getConfigInstance()->getConfigEntry('base_url') . '/' . $url;
161                 } // END - if
162
163                 // Add redirect header
164                 $this->addHeader('Location', str_replace('&amp;', '&', $url));
165
166                 // Set correct response status
167                 $this->setResponseStatus('301 Moved Permanently');
168
169                 // Clear the body
170                 $this->setResponseBody('');
171
172                 // Flush the result
173                 $this->flushBuffer();
174
175                 // All done here...
176                 exit();
177         }
178
179         /**
180          * Flushs the cached HTTP response to the outer world
181          *
182          * @param       $force  Whether we shall force the output or abort if headers are
183          *                                      already sent with an exception
184          * @return      void
185          */
186         public function flushBuffer ($force = FALSE) {
187                 // Finish the image
188                 $this->getImageInstance()->finishImage();
189
190                 // Get image content
191                 $content = $this->getImageInstance()->getContent();
192
193                 // Set it as response body
194                 $this->setResponseBody($content);
195
196                 // Set content type
197                 $this->addHeader('Content-type', 'image/' . $this->getImageInstance()->getImageType());
198
199                 // Call parent method
200                 parent::flushBuffer($force);
201         }
202
203         /**
204          * Expires the given cookie if it is set
205          *
206          * @param       $cookieName             Cookie to expire
207          * @return      void
208          */
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));
214
215                         // Remove it from array
216                         unset($_COOKIE[$cookieName]);
217                 } // END - if
218         }
219
220         /**
221          * Refreshs a given cookie. This will make the cookie live longer
222          *
223          * @param       $cookieName             Cookie to refresh
224          * @return      void
225          */
226         public function refreshCookie ($cookieName) {
227                 // Only update existing cookies
228                 if (isset($_COOKIE[$cookieName])) {
229                         // Update the cookie
230                         $this->addCookie($cookieName, $_COOKIE[$cookieName], FALSE);
231                 } // END - if
232         }
233
234         /**
235          * Getter for default command
236          *
237          * @return      $defaultCommand         Default command for this response
238          */
239         public function getDefaultCommand () {
240                 $defaultCommand = $this->getConfigInstance()->getConfigEntry('default_image_command');
241                 return $defaultCommand;
242         }
243 }
244
245 // [EOF]
246 ?>