Continued:
[core.git] / inc / main / classes / controller / class_BaseController.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Controller;
4
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Object\BaseFrameworkSystem;
8 use CoreFramework\Registry\Generic\Registry;
9 use CoreFramework\Registry\Registerable;
10 use CoreFramework\Request\Requestable;
11 use CoreFramework\Response\Responseable;
12
13 /**
14  * A generic controller class. You should extend this base class if you want to
15  * write your own controller. You get the advantage that you can use the pre and
16  * post filters.
17  *
18  * @author              Roland Haeder <webmaster@shipsimu.org>
19  * @version             0.0.0
20  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
21  * @license             GNU GPL 3.0 or any newer version
22  * @link                http://www.shipsimu.org
23  *
24  * This program is free software: you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation, either version 3 of the License, or
27  * (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License
35  * along with this program. If not, see <http://www.gnu.org/licenses/>.
36  */
37 class BaseController extends BaseFrameworkSystem implements Registerable {
38         // Exception constants
39         const EXCEPTION_FILTER_CHAIN_INVALID = 0xf10;
40
41         // Names of controller's own filter chains
42         const FILTER_CHAIN_PRE_COMMAND  = 'controller_pre_command';
43         const FILTER_CHAIN_POST_COMMAND = 'controller_post_command';
44
45         /**
46          * Generic filter chains
47          */
48         private $filterChains = array();
49
50         /**
51          * Protected constructor
52          *
53          * @param       $className      Name of the class
54          * @return      void
55          */
56         protected function __construct ($className) {
57                 // Call parent constructor
58                 parent::__construct($className);
59
60                 // Initialize both filter chains
61                 $this->initFilterChain(self::FILTER_CHAIN_PRE_COMMAND);
62                 $this->initFilterChain(self::FILTER_CHAIN_POST_COMMAND);
63
64                 // Add this controller to the registry
65                 Registry::getRegistry()->addInstance('controller', $this);
66         }
67
68         /**
69          * Executes a command with pre and post filters
70          *
71          * @param       $requestInstance        A Requestable class
72          * @param       $responseInstance       A Responseable class
73          * @return      void
74          */
75         public function executeGenericPrePostCommand (Requestable $requestInstance, Responseable $responseInstance) {
76                 // Get the command instance from the resolver by sending a request instance to the resolver
77                 $commandInstance = $this->getResolverInstance()->resolveCommandByRequest($requestInstance);
78
79                 // Add more filters by the command
80                 $commandInstance->addExtraFilters($this, $requestInstance);
81
82                 // Run the pre filters
83                 $this->executePreFilters($requestInstance, $responseInstance);
84
85                 // This request was valid! :-D
86                 $requestInstance->requestIsValid();
87
88                 // Execute the command
89                 $commandInstance->execute($requestInstance, $responseInstance);
90
91                 // Run the post filters
92                 $this->executePostFilters($requestInstance, $responseInstance);
93
94                 // Flush the response out
95                 $responseInstance->flushBuffer();
96         }
97
98         /**
99          * Handles the given request and response, redirects to login_failed if
100          * UserAuthorizationException is thrown.
101          *
102          * @param       $requestInstance        An instance of a Requestable class
103          * @param       $responseInstance       An instance of a Responseable class
104          * @return      void
105          */
106         public function genericHanleRequestLoginFailedRedirect (Requestable $requestInstance, Responseable $responseInstance) {
107                 // Get the "form action"
108                 $formAction = $requestInstance->getRequestElement('form');
109
110                 // Get command instance from resolver
111                 $commandInstance = $this->getResolverInstance()->resolveCommand($formAction);
112
113                 // Add more filters by the command
114                 $commandInstance->addExtraFilters($this, $requestInstance);
115
116                 // Try to run the pre filters, if auth exceptions come through redirect here
117                 try {
118                         // Run the pre filters
119                         $this->executePreFilters($requestInstance, $responseInstance);
120                 } catch (UserAuthorizationException $e) {
121                         // Redirect to main page
122                         $responseInstance->redirectToConfiguredUrl('login_failed');
123
124                         // Exit here
125                         exit();
126                 }
127
128                 /*
129                  * Is the request still valid? Post filters shall only be executed of
130                  * the request is valid
131                  */
132                 if ($requestInstance->isRequestValid()) {
133                         // Execute the command
134                         $commandInstance->execute($requestInstance, $responseInstance);
135
136                         // Execute *very* generic ppost filters
137                         $this->executePostFilters($requestInstance, $responseInstance);
138                 } // END - if
139
140                 // Flush the buffer out
141                 $responseInstance->flushBuffer();
142         }
143
144         /**
145          * Generic execute of the command: pre and post filters with redirect
146          * but request becomes valid after pre-filters run.
147          *
148          * @param       $requestInstance        An instance of a Requestable class
149          * @param       $responseInstance       An instance of a Responseable class
150          * @return      void
151          */
152         public function genericHanleRequestLoginAreaFailedRedirect (Requestable $requestInstance, Responseable $responseInstance) {
153                 // Get the command instance from the resolver by sending a request instance to the resolver
154                 $commandInstance = $this->getResolverInstance()->resolveCommandByRequest($requestInstance);
155
156                 // Add more filters by the command
157                 $commandInstance->addExtraFilters($this, $requestInstance);
158
159                 // Try to run the pre filters, if auth exceptions come through redirect here
160                 try {
161                         // Run the pre filters
162                         $this->executePreFilters($requestInstance, $responseInstance);
163                 } catch (UserAuthorizationException $e) {
164                         // Redirect to main page
165                         $responseInstance->redirectToConfiguredUrl('login_failed');
166
167                         // Exit here
168                         exit();
169                 }
170
171                 // This request was valid! :-D
172                 $requestInstance->requestIsValid();
173
174                 // Execute the command
175                 $commandInstance->execute($requestInstance, $responseInstance);
176
177                 // Run the pre filters
178                 $this->executePostFilters($requestInstance, $responseInstance);
179
180                 // Flush the response out
181                 $responseInstance->flushBuffer();
182         }
183
184         /**
185          * Private method to initialize a given filter chain
186          *
187          * @param       $filterChain    Name of the filter chain
188          * @return      void
189          */
190         protected function initFilterChain ($filterChain) {
191                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONTROLLER: ' . $filterChain . ' init: START');
192                 $this->filterChains[$filterChain] = ObjectFactory::createObjectByConfiguredName('filter_chain_class');
193                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONTROLLER: ' . $filterChain . ' init: FINISHED');
194         }
195
196         /**
197          * Adds a filter to a given filter chain
198          *
199          * @param       $filterChain    Chain of the filter
200          * @param       $filterInstance         An instance of a filter
201          * @return      void
202          * @throws      InvalidFilterChainException     If the filter chain is invalid
203          */
204         protected function addFilter ($filterChain, Filterable $filterInstance) {
205                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: START');
206
207                 // Test if the filter is there
208                 if (!isset($this->filterChains[$filterChain])) {
209                         // Throw an exception here
210                         throw new InvalidFilterChainException(array($this, $filterChain), self::EXCEPTION_FILTER_CHAIN_INVALID);
211                 } // END - if
212
213                 // Add the filter
214                 $this->filterChains[$filterChain]->addFilter($filterInstance);
215                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: FINISH');
216         }
217
218         /**
219          * Adds a filter to the pre filter chain
220          *
221          * @param       $filterInstance         An instance of a filter
222          * @return      void
223          */
224         public function addPreFilter (Filterable $filterInstance) {
225                 // Add the pre filter
226                 $this->addFilter(self::FILTER_CHAIN_PRE_COMMAND, $filterInstance);
227         }
228
229         /**
230          * Adds a filter to the post filter chain
231          *
232          * @param       $filterInstance         An instance of a filter
233          * @return      void
234          */
235         public function addPostFilter (Filterable $filterInstance) {
236                 // Add the post filter
237                 $this->addFilter(self::FILTER_CHAIN_POST_COMMAND, $filterInstance);
238         }
239
240         /**
241          * Add a shutdown filter
242          *
243          * @param       $filterInstance         A Filterable class
244          * @return      void
245          */
246         public function addShutdownFilter (Filterable $filterInstance) {
247                 $this->addFilter('shutdown', $filterInstance);
248         }
249
250         /**
251          * Executes given filter chain chain
252          *
253          * @param       $filterChain            Chain of the filter to execute
254          * @param       $requestInstance        An instance of a Requestable class
255          * @param       $responseInstance       An instance of a Responseable class
256          * @return      void
257          * @throws      InvalidFilterChainException     If the filter chain is invalid
258          */
259         protected function executeFilters ($filterChain, Requestable $requestInstance, Responseable $responseInstance) {
260                 // Test if the filter is there
261                 if (!isset($this->filterChains[$filterChain])) {
262                         // Throw an exception here
263                         throw new InvalidFilterChainException(array($this, $filterChain), self::EXCEPTION_FILTER_CHAIN_INVALID);
264                 } // END - if
265
266                 // Run all filters
267                 $this->filterChains[$filterChain]->processFilters($requestInstance, $responseInstance);
268         }
269
270         /**
271          * Executes all pre filters
272          *
273          * @param       $requestInstance        An instance of a Requestable class
274          * @param       $responseInstance       An instance of a Responseable class
275          * @return      void
276          */
277         protected function executePreFilters (Requestable $requestInstance, Responseable $responseInstance) {
278                 // Execute all pre filters
279                 $this->executeFilters(self::FILTER_CHAIN_PRE_COMMAND, $requestInstance, $responseInstance);
280         }
281
282         /**
283          * Executes all post filters
284          *
285          * @param       $requestInstance        An instance of a Requestable class
286          * @param       $responseInstance       An instance of a Responseable class
287          * @return      void
288          */
289         protected function executePostFilters (Requestable $requestInstance, Responseable $responseInstance) {
290                 // Execute all post filters
291                 $this->executeFilters(self::FILTER_CHAIN_POST_COMMAND, $requestInstance, $responseInstance);
292         }
293
294         /**
295          * Executes all shutdown filters
296          *
297          * @param       $requestInstance        A Requestable class
298          * @param       $responseInstance       A Responseable class
299          * @return      void
300          */
301         public function executeShutdownFilters (Requestable $requestInstance, Responseable $responseInstance) {
302                 $this->executeFilters('shutdown', $requestInstance, $responseInstance);
303         }
304
305 }