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