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