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