Total Complexity | 41 |
Total Lines | 357 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like PageBase often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PageBase, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | abstract class PageBase extends TaskBase implements IRoutedTask |
||
25 | { |
||
26 | use TemplateOutput; |
||
27 | /** @var string Smarty template to display */ |
||
28 | protected $template = "base.tpl"; |
||
29 | /** @var string HTML title. Currently unused. */ |
||
30 | protected $htmlTitle; |
||
31 | /** @var bool Determines if the page is a redirect or not */ |
||
32 | protected $isRedirecting = false; |
||
33 | /** @var array Queue of headers to be sent on successful completion */ |
||
34 | protected $headerQueue = array(); |
||
35 | /** @var string The name of the route to use, as determined by the request router. */ |
||
36 | private $routeName = null; |
||
37 | /** @var TokenManager */ |
||
38 | protected $tokenManager; |
||
39 | /** @var ContentSecurityPolicyManager */ |
||
40 | private $cspManager; |
||
41 | /** @var string[] Extra JS files to include */ |
||
42 | private $extraJs = array(); |
||
43 | |||
44 | /** |
||
45 | * Sets the route the request will take. Only should be called from the request router or barrier test. |
||
46 | * |
||
47 | * @param string $routeName The name of the route |
||
48 | * @param bool $skipCallableTest Don't use this unless you know what you're doing, and what the implications are. |
||
49 | * |
||
50 | * @throws Exception |
||
51 | * @category Security-Critical |
||
52 | */ |
||
53 | final public function setRoute($routeName, $skipCallableTest = false) |
||
54 | { |
||
55 | // Test the new route is callable before adopting it. |
||
56 | if (!$skipCallableTest && !is_callable(array($this, $routeName))) { |
||
57 | throw new Exception("Proposed route '$routeName' is not callable."); |
||
58 | } |
||
59 | |||
60 | // Adopt the new route |
||
61 | $this->routeName = $routeName; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Gets the name of the route that has been passed from the request router. |
||
66 | * @return string |
||
67 | */ |
||
68 | final public function getRouteName() |
||
69 | { |
||
70 | return $this->routeName; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Performs generic page setup actions |
||
75 | */ |
||
76 | final protected function setupPage() |
||
77 | { |
||
78 | $this->setUpSmarty(); |
||
79 | |||
80 | $siteNoticeText = SiteNotice::get($this->getDatabase()); |
||
81 | |||
82 | $this->assign('siteNoticeText', $siteNoticeText); |
||
83 | |||
84 | $currentUser = User::getCurrent($this->getDatabase()); |
||
85 | $this->assign('currentUser', $currentUser); |
||
86 | $this->assign('loggedIn', (!$currentUser->isCommunityUser())); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Runs the page logic as routed by the RequestRouter |
||
91 | * |
||
92 | * Only should be called after a security barrier! That means only from execute(). |
||
93 | */ |
||
94 | final protected function runPage() |
||
95 | { |
||
96 | $database = $this->getDatabase(); |
||
97 | |||
98 | // initialise a database transaction |
||
99 | if (!$database->beginTransaction()) { |
||
100 | throw new Exception('Failed to start transaction on primary database.'); |
||
101 | } |
||
102 | |||
103 | try { |
||
104 | // run the page code |
||
105 | $this->{$this->getRouteName()}(); |
||
106 | |||
107 | $database->commit(); |
||
108 | } |
||
109 | catch (ApplicationLogicException $ex) { |
||
110 | // it's an application logic exception, so nothing went seriously wrong with the site. We can use the |
||
111 | // standard templating system for this. |
||
112 | |||
113 | // Firstly, let's undo anything that happened to the database. |
||
114 | $database->rollBack(); |
||
115 | |||
116 | // Reset smarty |
||
117 | $this->setUpSmarty(); |
||
118 | |||
119 | // Set the template |
||
120 | $this->setTemplate('exception/application-logic.tpl'); |
||
121 | $this->assign('message', $ex->getMessage()); |
||
122 | |||
123 | // Force this back to false |
||
124 | $this->isRedirecting = false; |
||
125 | $this->headerQueue = array(); |
||
126 | } |
||
127 | catch (OptimisticLockFailedException $ex) { |
||
128 | // it's an optimistic lock failure exception, so nothing went seriously wrong with the site. We can use the |
||
129 | // standard templating system for this. |
||
130 | |||
131 | // Firstly, let's undo anything that happened to the database. |
||
132 | $database->rollBack(); |
||
133 | |||
134 | // Reset smarty |
||
135 | $this->setUpSmarty(); |
||
136 | |||
137 | // Set the template |
||
138 | $this->setTemplate('exception/optimistic-lock-failure.tpl'); |
||
139 | $this->assign('message', $ex->getMessage()); |
||
140 | |||
141 | $this->assign('debugTrace', false); |
||
142 | |||
143 | if ($this->getSiteConfiguration()->getDebuggingTraceEnabled()) { |
||
144 | ob_start(); |
||
145 | var_dump(ExceptionHandler::getExceptionData($ex)); |
||
1 ignored issue
–
show
|
|||
146 | $textErrorData = ob_get_contents(); |
||
147 | ob_end_clean(); |
||
148 | |||
149 | $this->assign('exceptionData', $textErrorData); |
||
150 | $this->assign('debugTrace', true); |
||
151 | } |
||
152 | |||
153 | // Force this back to false |
||
154 | $this->isRedirecting = false; |
||
155 | $this->headerQueue = array(); |
||
156 | } |
||
157 | finally { |
||
158 | // Catch any hanging on transactions |
||
159 | if ($database->hasActiveTransaction()) { |
||
160 | $database->rollBack(); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | // run any finalisation code needed before we send the output to the browser. |
||
165 | $this->finalisePage(); |
||
166 | |||
167 | // Send the headers |
||
168 | $this->sendResponseHeaders(); |
||
169 | |||
170 | // Check we have a template to use! |
||
171 | if ($this->template !== null) { |
||
172 | $content = $this->fetchTemplate($this->template); |
||
173 | ob_clean(); |
||
174 | print($content); |
||
175 | ob_flush(); |
||
176 | |||
177 | return; |
||
178 | } |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Performs final tasks needed before rendering the page. |
||
183 | */ |
||
184 | protected function finalisePage() |
||
185 | { |
||
186 | if ($this->isRedirecting) { |
||
187 | $this->template = null; |
||
188 | |||
189 | return; |
||
190 | } |
||
191 | |||
192 | $this->assign('extraJs', $this->extraJs); |
||
193 | |||
194 | // If we're actually displaying content, we want to add the session alerts here! |
||
195 | $this->assign('alerts', SessionAlert::getAlerts()); |
||
196 | SessionAlert::clearAlerts(); |
||
197 | |||
198 | $this->assign('htmlTitle', $this->htmlTitle); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @return TokenManager |
||
203 | */ |
||
204 | public function getTokenManager() |
||
205 | { |
||
206 | return $this->tokenManager; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @param TokenManager $tokenManager |
||
211 | */ |
||
212 | public function setTokenManager($tokenManager) |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @return ContentSecurityPolicyManager |
||
219 | */ |
||
220 | public function getCspManager(): ContentSecurityPolicyManager |
||
221 | { |
||
222 | return $this->cspManager; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @param ContentSecurityPolicyManager $cspManager |
||
227 | */ |
||
228 | public function setCspManager(ContentSecurityPolicyManager $cspManager): void |
||
229 | { |
||
230 | $this->cspManager = $cspManager; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Sends the redirect headers to perform a GET at the destination page. |
||
235 | * |
||
236 | * Also nullifies the set template so Smarty does not render it. |
||
237 | * |
||
238 | * @param string $page The page to redirect requests to (as used in the UR) |
||
239 | * @param null|string $action The action to use on the page. |
||
240 | * @param null|array $parameters |
||
241 | * @param null|string $script The script (relative to index.php) to redirect to |
||
242 | */ |
||
243 | final protected function redirect($page = '', $action = null, $parameters = null, $script = null) |
||
244 | { |
||
245 | $currentScriptName = WebRequest::scriptName(); |
||
246 | |||
247 | // Are we changing script? |
||
248 | if ($script === null || substr($currentScriptName, -1 * count($script)) === $script) { |
||
249 | $targetScriptName = $currentScriptName; |
||
250 | } |
||
251 | else { |
||
252 | $targetScriptName = $this->getSiteConfiguration()->getBaseUrl() . '/' . $script; |
||
253 | } |
||
254 | |||
255 | $pathInfo = array($targetScriptName); |
||
256 | |||
257 | $pathInfo[1] = $page; |
||
258 | |||
259 | if ($action !== null) { |
||
260 | $pathInfo[2] = $action; |
||
261 | } |
||
262 | |||
263 | $url = implode('/', $pathInfo); |
||
264 | |||
265 | if (is_array($parameters) && count($parameters) > 0) { |
||
266 | $url .= '?' . http_build_query($parameters); |
||
267 | } |
||
268 | |||
269 | $this->redirectUrl($url); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Sends the redirect headers to perform a GET at the new address. |
||
274 | * |
||
275 | * Also nullifies the set template so Smarty does not render it. |
||
276 | * |
||
277 | * @param string $path URL to redirect to |
||
278 | */ |
||
279 | final protected function redirectUrl($path) |
||
280 | { |
||
281 | // 303 See Other = re-request at new address with a GET. |
||
282 | $this->headerQueue[] = 'HTTP/1.1 303 See Other'; |
||
283 | $this->headerQueue[] = "Location: $path"; |
||
284 | |||
285 | $this->setTemplate(null); |
||
286 | $this->isRedirecting = true; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Sets the name of the template this page should display. |
||
291 | * |
||
292 | * @param string $name |
||
293 | * |
||
294 | * @throws Exception |
||
295 | */ |
||
296 | final protected function setTemplate($name) |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Adds an extra JS file to to the page |
||
307 | * |
||
308 | * @param string $path The path (relative to the application root) of the file |
||
309 | */ |
||
310 | final protected function addJs($path){ |
||
311 | if(in_array($path, $this->extraJs)){ |
||
312 | // nothing to do |
||
313 | return; |
||
314 | } |
||
315 | |||
316 | $this->extraJs[] = $path; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Main function for this page, when no specific actions are called. |
||
321 | * @return void |
||
322 | */ |
||
323 | abstract protected function main(); |
||
324 | |||
325 | /** |
||
326 | * Takes a smarty template string and sets the HTML title to that value |
||
327 | * |
||
328 | * @param string $title |
||
329 | * |
||
330 | * @throws SmartyException |
||
331 | */ |
||
332 | final protected function setHtmlTitle($title) |
||
335 | } |
||
336 | |||
337 | public function execute() |
||
338 | { |
||
339 | if ($this->getRouteName() === null) { |
||
340 | throw new Exception('Request is unrouted.'); |
||
341 | } |
||
342 | |||
343 | if ($this->getSiteConfiguration() === null) { |
||
344 | throw new Exception('Page has no configuration!'); |
||
345 | } |
||
346 | |||
347 | $this->setupPage(); |
||
348 | |||
349 | $this->runPage(); |
||
350 | } |
||
351 | |||
352 | public function assignCSRFToken() |
||
353 | { |
||
354 | $token = $this->tokenManager->getNewToken(); |
||
355 | $this->assign('csrfTokenData', $token->getTokenData()); |
||
356 | } |
||
357 | |||
358 | public function validateCSRFToken() |
||
362 | } |
||
363 | } |
||
364 | |||
365 | protected function sendResponseHeaders() |
||
381 | } |
||
382 | } |
||
383 | } |
||
384 |