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