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