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