]> 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\Helper\Application\ApplicationHelper;
8 use Org\Mxchange\CoreFramework\Manager\ManageableApplication;
9 use Org\Mxchange\CoreFramework\Middleware\Debug\DebugMiddleware;
10 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
11 use Org\Mxchange\CoreFramework\Response\Responseable;
12
13 /**
14  * A class for an HTML response on an HTML request
15  *
16  * @author              Roland Haeder <webmaster@shipsimu.org>
17  * @version             0.0.0
18  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
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 HtmlResponse extends BaseResponse implements Responseable {
39         /**
40          * Protected constructor
41          *
42          * @return      void
43          */
44         private function __construct () {
45                 // Call parent constructor
46                 parent::__construct(__CLASS__);
47
48                 // Set response type
49                 $this->setResponseType('http');
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 createHtmlResponse () {
58                 // Get a new instance
59                 $responseInstance = new HtmlResponse();
60
61                 // Return the prepared instance
62                 return $responseInstance;
63         }
64
65         /**
66          * Adds a cookie to the response
67          *
68          * @param       $cookieName             Cookie's name
69          * @param       $cookieValue    Value to store in the cookie
70          * @param       $encrypted              Do some extra encryption on the value
71          * @param       $expires                Timestamp of expiration (default: configured)
72          * @return      void
73          * @throws      ResponseHeadersAlreadySentException             If headers are already sent
74          * @todo        Encryption of cookie data not yet supported.
75          * @todo        If the return statement is removed and setcookie() commented out,
76          * @todo        this will send only one cookie out, the first one.
77          */
78         public function addCookie (string $cookieName, $cookieValue, bool $encrypted = FALSE, int $expires = NULL) {
79                 //* DEBUG: */ echo $cookieName.'='.$cookieValue."<br />\n";
80                 // Are headers already sent?
81                 if (headers_sent()) {
82                         // Throw an exception here
83                         //* DEBUG: */ return;
84                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
85                 }
86
87                 // Shall we encrypt the cookie?
88                 if ($encrypted) {
89                         // Unsupported at the moment
90                         DebugMiddleware::getSelfInstance()->partialStub('Encryption is unsupported at the moment.');
91                 }
92
93                 // For slow browsers set the cookie array element first
94                 $_COOKIE[$cookieName] = $cookieValue;
95
96                 // Get all config entries
97                 if (is_null($expires)) {
98                         $expires = (time() + FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('cookie_expire'));
99                 }
100
101                 $path = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('cookie_path');
102                 $domain = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('cookie_domain');
103
104                 setcookie($cookieName, $cookieValue, $expires);
105                 //, $path, $domain, (isset($_SERVER['HTTPS']))
106                 return;
107
108                 // Now construct the full header
109                 $cookieString = $cookieName . '=' . $cookieValue . '; ';
110                 $cookieString .= 'expires=' . date('D, d-F-Y H:i:s', $expires) . ' GMT';
111                 // $cookieString .= "; path=".$path."; domain=".$domain;
112
113                 // Set the cookie as a header
114                 $this->cookies[$cookieName] = $cookieString;
115         }
116
117         /**
118          * Redirect to a configured URL. The URL can be absolute or relative. In
119          * case of relative URL it will be extended automatically.
120          *
121          * @param       $configEntry    The configuration entry which holds our URL
122          * @return      void
123          * @throws      ResponseHeadersAlreadySentException             If headers are already sent
124          */
125         public function redirectToConfiguredUrl ($configEntry) {
126                 // Get application instance
127                 $applicationInstance = ApplicationHelper::getSelfInstance();
128
129                 // Is the header not yet sent?
130                 if (headers_sent()) {
131                         // Throw an exception here
132                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
133                 }
134
135                 // Assign application data
136                 $this->getTemplateInstance()->assignApplicationData($applicationInstance);
137
138                 // Get the url from config
139                 $url = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configEntry . '_url');
140
141                 // Compile the URL
142                 $url = $this->getTemplateInstance()->compileRawCode($url);
143
144                 // Do we have a 'http' in front of the URL?
145                 if (substr(strtolower($url), 0, 4) != 'http') {
146                         // Is there a / in front of the relative URL?
147                         if (substr($url, 0, 1) == '/') {
148                                 $url = substr($url, 1);
149                         }
150
151                         // No, then extend it with our base URL
152                         $url = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('base_url') . '/' . $url;
153                 }
154
155                 // Add redirect header
156                 $this->addHeader('Location', str_replace('&amp;', '&', $url));
157
158                 // Set correct response status
159                 $this->setResponseStatus('301 Moved Permanently');
160
161                 // Clear the body
162                 $this->setResponseBody('');
163
164                 // Flush the result
165                 $this->flushBuffer();
166
167                 // All done here...
168                 exit();
169         }
170
171         /**
172          * Expires the given cookie if it is set
173          *
174          * @param       $cookieName             Cookie to expire
175          * @return      void
176          */
177         public function expireCookie (string $cookieName) {
178                 // Is the cookie there?
179                 if (isset($_COOKIE[$cookieName])) {
180                         // Then expire it with 20 minutes past
181                         $this->addCookie($cookieName, '', false, (time() - 1200));
182
183                         // Remove it from array
184                         unset($_COOKIE[$cookieName]);
185                 }
186         }
187
188         /**
189          * Refreshs a given cookie. This will make the cookie live longer
190          *
191          * @param       $cookieName             Cookie to refresh
192          * @return      void
193          */
194         public function refreshCookie (string $cookieName) {
195                 // Only update existing cookies
196                 if (isset($_COOKIE[$cookieName])) {
197                         // Update the cookie
198                         $this->addCookie($cookieName, $_COOKIE[$cookieName], false);
199                 }
200         }
201
202 }