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