]> git.mxchange.org Git - core.git/blob - framework/main/classes/response/html/class_HtmlResponse.php
Continued:
[core.git] / framework / main / classes / response / html / class_HtmlResponse.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 HTML response on an HTML 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 HtmlResponse 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('http');
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 createHtmlResponse () {
56                 // Get a new instance
57                 $responseInstance = new HtmlResponse();
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                 $this->setTemplateInstance($this->prepareTemplateInstance($applicationInstance));
71         }
72
73         /**
74          * Adds a cookie to the response
75          *
76          * @param       $cookieName             Cookie's name
77          * @param       $cookieValue    Value to store in the cookie
78          * @param       $encrypted              Do some extra encryption on the value
79          * @param       $expires                Timestamp of expiration (default: configured)
80          * @return      void
81          * @throws      ResponseHeadersAlreadySentException             If headers are already sent
82          * @todo        Encryption of cookie data not yet supported.
83          * @todo        Why are these parameters conflicting?
84          * @todo        If the return statement is removed and setcookie() commented out,
85          * @todo        this will send only one cookie out, the first one.
86          */
87         public function addCookie ($cookieName, $cookieValue, $encrypted = false, $expires = NULL) {
88                 //* DEBUG: */ echo $cookieName.'='.$cookieValue."<br />\n";
89                 // Are headers already sent?
90                 if (headers_sent()) {
91                         // Throw an exception here
92                         //* DEBUG: */ return;
93                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
94                 } // END - if
95
96                 // Shall we encrypt the cookie?
97                 if ($encrypted === true) {
98                         // Unsupported at the moment
99                         $this->partialStub('Encryption is unsupported at the moment.');
100                 } // END - if
101
102                 // For slow browsers set the cookie array element first
103                 $_COOKIE[$cookieName] = $cookieValue;
104
105                 // Get all config entries
106                 if (is_null($expires)) {
107                         $expires = (time() + FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('cookie_expire'));
108                 } // END - if
109
110                 $path = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('cookie_path');
111                 $domain = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('cookie_domain');
112
113                 setcookie($cookieName, $cookieValue, $expires);
114                 //, $path, $domain, (isset($_SERVER['HTTPS']))
115                 return;
116
117                 // Now construct the full header
118                 $cookieString = $cookieName . '=' . $cookieValue . '; ';
119                 $cookieString .= 'expires=' . date('D, d-F-Y H:i:s', $expires) . ' GMT';
120                 // $cookieString .= "; path=".$path."; domain=".$domain;
121
122                 // Set the cookie as a header
123                 $this->cookies[$cookieName] = $cookieString;
124         }
125
126         /**
127          * Redirect to a configured URL. The URL can be absolute or relative. In
128          * case of relative URL it will be extended automatically.
129          *
130          * @param       $configEntry    The configuration entry which holds our URL
131          * @return      void
132          * @throws      ResponseHeadersAlreadySentException             If headers are already sent
133          */
134         public function redirectToConfiguredUrl ($configEntry) {
135                 // Get application instance
136                 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
137
138                 // Is the header not yet sent?
139                 if (headers_sent()) {
140                         // Throw an exception here
141                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
142                 } // END - if
143
144                 // Assign application data
145                 $this->getTemplateInstance()->assignApplicationData($applicationInstance);
146
147                 // Get the url from config
148                 $url = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configEntry . '_url');
149
150                 // Compile the URL
151                 $url = $this->getTemplateInstance()->compileRawCode($url);
152
153                 // Do we have a 'http' in front of the URL?
154                 if (substr(strtolower($url), 0, 4) != 'http') {
155                         // Is there a / in front of the relative URL?
156                         if (substr($url, 0, 1) == '/') {
157                                 $url = substr($url, 1);
158                         } // END - if
159
160                         // No, then extend it with our base URL
161                         $url = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('base_url') . '/' . $url;
162                 } // END - if
163
164                 // Add redirect header
165                 $this->addHeader('Location', str_replace('&amp;', '&', $url));
166
167                 // Set correct response status
168                 $this->setResponseStatus('301 Moved Permanently');
169
170                 // Clear the body
171                 $this->setResponseBody('');
172
173                 // Flush the result
174                 $this->flushBuffer();
175
176                 // All done here...
177                 exit();
178         }
179
180         /**
181          * Expires the given cookie if it is set
182          *
183          * @param       $cookieName             Cookie to expire
184          * @return      void
185          */
186         public function expireCookie ($cookieName) {
187                 // Is the cookie there?
188                 if (isset($_COOKIE[$cookieName])) {
189                         // Then expire it with 20 minutes past
190                         $this->addCookie($cookieName, '', false, (time() - 1200));
191
192                         // Remove it from array
193                         unset($_COOKIE[$cookieName]);
194                 } // END - if
195         }
196
197         /**
198          * Refreshs a given cookie. This will make the cookie live longer
199          *
200          * @param       $cookieName             Cookie to refresh
201          * @return      void
202          */
203         public function refreshCookie ($cookieName) {
204                 // Only update existing cookies
205                 if (isset($_COOKIE[$cookieName])) {
206                         // Update the cookie
207                         $this->addCookie($cookieName, $_COOKIE[$cookieName], false);
208                 } // END - if
209         }
210
211 }