]> git.mxchange.org Git - core.git/blob - inc/classes/main/response/image/class_ImageResponse.php
Added some more missing class fields.
[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                 // Set response type
38                 $this->setResponseType('image');
39         }
40
41         /**
42          * Creates an object of this class
43          *
44          * @param       $applicationInstance    An instance of a manageable application
45          * @return      $responseInstance               A prepared instance of this class
46          */
47         public static final function createImageResponse (ManageableApplication $applicationInstance) {
48                 // Get a new instance
49                 $responseInstance = new ImageResponse();
50
51                 // Set the application instance
52                 $responseInstance->setApplicationInstance($applicationInstance);
53
54                 // Initialize the template engine here
55                 $responseInstance->initTemplateEngine($applicationInstance);
56
57                 // Return the prepared instance
58                 return $responseInstance;
59         }
60
61         /**
62          * Initializes the template engine instance
63          *
64          * @param       $applicationInstance    An instance of a manageable application
65          * @return      void
66          */
67         public final function initTemplateEngine (ManageableApplication $applicationInstance) {
68                 // Get config instance
69                 $cfg = $this->getConfigInstance();
70
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                 $cfg->setConfigEntry('code_template_type'     , 'image');
77
78                 // Get a prepared instance
79                 $this->setTemplateInstance($this->prepareTemplateInstance($applicationInstance));
80         }
81
82         /**
83          * Adds a cookie to the response
84          *
85          * @param       $cookieName             Cookie's name
86          * @param       $cookieValue    Value to store in the cookie
87          * @param       $encrypted              Do some extra encryption on the value
88          * @param       $expires                Timestamp of expiration (default: configured)
89          * @return      void
90          * @throws      ResponseHeadersAlreadySentException             If headers are already sent
91          * @todo        Encryption of cookie data not yet supported.
92          * @todo        Why are these parameters conflicting?
93          * @todo        If the return statement is removed and setcookie() commented out,
94          * @todo        this will send only one cookie out, the first one.
95          */
96         public function addCookie ($cookieName, $cookieValue, $encrypted = FALSE, $expires = NULL) {
97                 // Are headers already sent?
98                 if (headers_sent()) {
99                         // Throw an exception here
100                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
101                 } // END - if
102
103                 // Shall we encrypt the cookie?
104                 if ($encrypted === TRUE) {
105                         // Unsupported at the moment
106                         $this->partialStub('Encryption is unsupported at the moment.');
107                 } // END - if
108
109                 // For slow browsers set the cookie array element first
110                 $_COOKIE[$cookieName] = $cookieValue;
111
112                 // Get all config entries
113                 if (is_null($expires)) {
114                         $expires = (time() + $this->getConfigInstance()->getConfigEntry('cookie_expire'));
115                 } // END - if
116
117                 $path = $this->getConfigInstance()->getConfigEntry('cookie_path');
118                 $domain = $this->getConfigInstance()->getConfigEntry('cookie_domain');
119
120                 setcookie($cookieName, $cookieValue, $expires);
121                 //, $path, $domain, (isset($_SERVER['HTTPS']))
122                 return;
123
124                 // Now construct the full header
125                 $cookieString = $cookieName . '=' . $cookieValue . '; ';
126                 $cookieString .= 'expires=' . date('D, d-F-Y H:i:s', $expires) . ' GMT';
127                 // TODO Why is this not always working? $cookieString .= '; path=' . $path . '; domain=' . $domain;
128
129                 // Set the cookie as a header
130                 $this->cookies[$cookieName] = $cookieString;
131         }
132
133         /**
134          * Redirect to a configured URL. The URL can be absolute or relative. In
135          * case of relative URL it will be extended automatically.
136          *
137          * @param       $configEntry    The configuration entry which holds our URL
138          * @return      void
139          * @throws      ResponseHeadersAlreadySentException             If headers are already sent
140          */
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);
146                 } // END - if
147
148                 // Assign application data
149                 $this->getTemplateInstance()->assignApplicationData($this->getApplicationInstance());
150
151                 // Get the url from config
152                 $url = $this->getConfigInstance()->getConfigEntry($configEntry . '_url');
153
154                 // Compile the URL
155                 $url = $this->getTemplateInstance()->compileRawCode($url);
156
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);
161
162                         // No, then extend it with our base URL
163                         $url = $this->getConfigInstance()->getConfigEntry('base_url') . '/' . $url;
164                 } // END - if
165
166                 // Add redirect header
167                 $this->addHeader('Location', str_replace('&amp;', '&', $url));
168
169                 // Set correct response status
170                 $this->setResponseStatus('301 Moved Permanently');
171
172                 // Clear the body
173                 $this->setResponseBody('');
174
175                 // Flush the result
176                 $this->flushBuffer();
177
178                 // All done here...
179                 exit();
180         }
181
182         /**
183          * Flushs the cached HTTP response to the outer world
184          *
185          * @param       $force  Whether we shall force the output or abort if headers are
186          *                                      already sent with an exception
187          * @return      void
188          */
189         public function flushBuffer ($force = FALSE) {
190                 // Finish the image
191                 $this->getImageInstance()->finishImage();
192
193                 // Get image content
194                 $content = $this->getImageInstance()->getContent();
195
196                 // Set it as response body
197                 $this->setResponseBody($content);
198
199                 // Set content type
200                 $this->addHeader('Content-type', 'image/' . $this->getImageInstance()->getImageType());
201
202                 // Call parent method
203                 parent::flushBuffer($force);
204         }
205
206         /**
207          * Expires the given cookie if it is set
208          *
209          * @param       $cookieName             Cookie to expire
210          * @return      void
211          */
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));
217
218                         // Remove it from array
219                         unset($_COOKIE[$cookieName]);
220                 } // END - if
221         }
222
223         /**
224          * Refreshs a given cookie. This will make the cookie live longer
225          *
226          * @param       $cookieName             Cookie to refresh
227          * @return      void
228          */
229         public function refreshCookie ($cookieName) {
230                 // Only update existing cookies
231                 if (isset($_COOKIE[$cookieName])) {
232                         // Update the cookie
233                         $this->addCookie($cookieName, $_COOKIE[$cookieName], FALSE);
234                 } // END - if
235         }
236 }
237
238 // [EOF]
239 ?>