]> git.mxchange.org Git - core.git/blob - framework/main/classes/response/image/class_ImageResponse.php
Continued:
[core.git] / framework / main / classes / response / image / class_ImageResponse.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Response;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Image\BaseImage;
8 use Org\Mxchange\CoreFramework\Manager\ManageableApplication;
9 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
10 use Org\Mxchange\CoreFramework\Response\Responseable;
11
12 /**
13  * A class for an image response on an HTTP request
14  *
15  * @author              Roland Haeder <webmaster@shipsimu.org>
16  * @version             0.0.0
17  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
18  * @license             GNU GPL 3.0 or any newer version
19  * @link                http://www.shipsimu.org
20  *
21  * This program is free software: you can redistribute it and/or modify
22  * it under the terms of the GNU General Public License as published by
23  * the Free Software Foundation, either version 3 of the License, or
24  * (at your option) any later version.
25  *
26  * This program is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  * GNU General Public License for more details.
30  *
31  * You should have received a copy of the GNU General Public License
32  * along with this program. If not, see <http://www.gnu.org/licenses/>.
33  *
34  * The extended headers are taken from phpMyAdmin setup tool, written by
35  * Michal Cihar <michal@cihar.com>, licensed under GNU GPL 2.0.
36  */
37 class ImageResponse extends BaseResponse implements Responseable {
38         /**
39          * Instance of the image
40          */
41         private $imageInstance = NULL;
42
43         /**
44          * Protected constructor
45          *
46          * @return      void
47          */
48         protected function __construct () {
49                 // Call parent constructor
50                 parent::__construct(__CLASS__);
51
52                 // Set response type
53                 $this->setResponseType('image');
54         }
55
56         /**
57          * Creates an object of this class
58          *
59          * @return      $responseInstance       A prepared instance of this class
60          */
61         public static final function createImageResponse () {
62                 // Get a new instance
63                 $responseInstance = new ImageResponse();
64
65                 // Return the prepared instance
66                 return $responseInstance;
67         }
68
69         /**
70          * Setter for image instance
71          *
72          * @param       $imageInstance  An instance of an image
73          * @return      void
74          */
75         public final function setImageInstance (BaseImage $imageInstance) {
76                 $this->imageInstance = $imageInstance;
77         }
78
79         /**
80          * Getter for image instance
81          *
82          * @return      $imageInstance  An instance of an image
83          */
84         public final function getImageInstance () {
85                 return $this->imageInstance;
86         }
87
88         /**
89          * Initializes the template engine instance
90          *
91          * @param       $applicationInstance    An instance of a manageable application
92          * @return      void
93          */
94         public final function initTemplateEngine (ManageableApplication $applicationInstance) {
95                 // Get config instance
96                 $cfg = FrameworkBootstrap::getConfigurationInstance();
97
98                 // Set new template engine
99                 $cfg->setConfigEntry('html_template_class'    , $cfg->getConfigEntry('image_template_class'));
100                 $cfg->setConfigEntry('raw_template_extension' , '.img');
101                 $cfg->setConfigEntry('code_template_extension', '.xml');
102                 $cfg->setConfigEntry('tpl_base_path'          , 'templates/images/');
103                 // @TODO Please fix this
104                 $cfg->setConfigEntry('code_template_type'     , 'image');
105
106                 // Get a prepared instance
107                 $this->setTemplateInstance($this->prepareTemplateInstance($applicationInstance));
108         }
109
110         /**
111          * Adds a cookie to the response
112          *
113          * @param       $cookieName             Cookie's name
114          * @param       $cookieValue    Value to store in the cookie
115          * @param       $encrypted              Do some extra encryption on the value
116          * @param       $expires                Timestamp of expiration (default: configured)
117          * @return      void
118          * @throws      ResponseHeadersAlreadySentException             If headers are already sent
119          * @todo        Encryption of cookie data not yet supported.
120          * @todo        Why are these parameters conflicting?
121          * @todo        If the return statement is removed and setcookie() commented out,
122          * @todo        this will send only one cookie out, the first one.
123          */
124         public function addCookie ($cookieName, $cookieValue, $encrypted = false, $expires = NULL) {
125                 // Are headers already sent?
126                 if (headers_sent()) {
127                         // Throw an exception here
128                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
129                 } // END - if
130
131                 // Shall we encrypt the cookie?
132                 if ($encrypted === true) {
133                         // Unsupported at the moment
134                         $this->partialStub('Encryption is unsupported at the moment.');
135                 } // END - if
136
137                 // For slow browsers set the cookie array element first
138                 $_COOKIE[$cookieName] = $cookieValue;
139
140                 // Get all config entries
141                 if (is_null($expires)) {
142                         $expires = (time() + FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('cookie_expire'));
143                 } // END - if
144
145                 $path = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('cookie_path');
146                 $domain = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('cookie_domain');
147
148                 setcookie($cookieName, $cookieValue, $expires);
149                 //, $path, $domain, (isset($_SERVER['HTTPS']))
150                 return;
151
152                 // Now construct the full header
153                 $cookieString = $cookieName . '=' . $cookieValue . '; ';
154                 $cookieString .= 'expires=' . date('D, d-F-Y H:i:s', $expires) . ' GMT';
155                 // TODO Why is this not always working? $cookieString .= '; path=' . $path . '; domain=' . $domain;
156
157                 // Set the cookie as a header
158                 $this->cookies[$cookieName] = $cookieString;
159         }
160
161         /**
162          * Redirect to a configured URL. The URL can be absolute or relative. In
163          * case of relative URL it will be extended automatically.
164          *
165          * @param       $configEntry    The configuration entry which holds our URL
166          * @return      void
167          * @throws      ResponseHeadersAlreadySentException             If headers are already sent
168          */
169         public function redirectToConfiguredUrl ($configEntry) {
170                 // Get application instance
171                 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
172
173                 // Is the header not yet sent?
174                 if (headers_sent()) {
175                         // Throw an exception here
176                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
177                 } // END - if
178
179                 // Assign application data
180                 $this->getTemplateInstance()->assignApplicationData($applicationInstance);
181
182                 // Get the url from config
183                 $url = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configEntry . '_url');
184
185                 // Compile the URL
186                 $url = $this->getTemplateInstance()->compileRawCode($url);
187
188                 // Do we have a 'http' in front of the URL?
189                 if (substr(strtolower($url), 0, 4) != 'http') {
190                         // Is there a / in front of the relative URL?
191                         if (substr($url, 0, 1) == '/') $url = substr($url, 1);
192
193                         // No, then extend it with our base URL
194                         $url = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('base_url') . '/' . $url;
195                 } // END - if
196
197                 // Add redirect header
198                 $this->addHeader('Location', str_replace('&amp;', '&', $url));
199
200                 // Set correct response status
201                 $this->setResponseStatus('301 Moved Permanently');
202
203                 // Clear the body
204                 $this->setResponseBody('');
205
206                 // Flush the result
207                 $this->flushBuffer();
208
209                 // All done here...
210                 exit();
211         }
212
213         /**
214          * Flushs the cached HTTP response to the outer world
215          *
216          * @param       $force  Whether we shall force the output or abort if headers are
217          *                                      already sent with an exception
218          * @return      void
219          */
220         public function flushBuffer ($force = false) {
221                 // Finish the image
222                 $this->getImageInstance()->finishImage();
223
224                 // Get image content
225                 $content = $this->getImageInstance()->getContent();
226
227                 // Set it as response body
228                 $this->setResponseBody($content);
229
230                 // Set content type
231                 $this->addHeader('Content-type', 'image/' . $this->getImageInstance()->getImageType());
232
233                 // Call parent method
234                 parent::flushBuffer($force);
235         }
236
237         /**
238          * Expires the given cookie if it is set
239          *
240          * @param       $cookieName             Cookie to expire
241          * @return      void
242          */
243         public function expireCookie ($cookieName) {
244                 // Is the cookie there?
245                 if (isset($_COOKIE[$cookieName])) {
246                         // Then expire it with 20 minutes past
247                         $this->addCookie($cookieName, '', false, (time() - 1200));
248
249                         // Remove it from array
250                         unset($_COOKIE[$cookieName]);
251                 } // END - if
252         }
253
254         /**
255          * Refreshs a given cookie. This will make the cookie live longer
256          *
257          * @param       $cookieName             Cookie to refresh
258          * @return      void
259          */
260         public function refreshCookie ($cookieName) {
261                 // Only update existing cookies
262                 if (isset($_COOKIE[$cookieName])) {
263                         // Update the cookie
264                         $this->addCookie($cookieName, $_COOKIE[$cookieName], false);
265                 } // END - if
266         }
267
268 }