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