Total Complexity | 93 |
Total Lines | 732 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like BaseController 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 BaseController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
57 | class BaseController |
||
58 | { |
||
59 | use FirewallTrait; |
||
60 | use DemoTrait; |
||
61 | |||
62 | /** |
||
63 | * LogPaeser instance. |
||
64 | * |
||
65 | * @var object |
||
66 | */ |
||
67 | protected $parser; |
||
68 | |||
69 | /** |
||
70 | * Messages. |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $messages = []; |
||
75 | |||
76 | /** |
||
77 | * Check page availability. |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected $pageAvailability = [ |
||
82 | |||
83 | // Need to implement Action Logger to make it true. |
||
84 | 'logs' => false, |
||
85 | ]; |
||
86 | |||
87 | /** |
||
88 | * see $this->csrf() |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | protected $csrfField = []; |
||
93 | |||
94 | /** |
||
95 | * Language code. |
||
96 | * |
||
97 | * @var string |
||
98 | */ |
||
99 | protected $locate = 'en'; |
||
100 | |||
101 | /** |
||
102 | * Captcha modules. |
||
103 | * |
||
104 | * @var Interface |
||
|
|||
105 | */ |
||
106 | protected $captcha = []; |
||
107 | |||
108 | /** |
||
109 | * The base URL of the firewall panel. |
||
110 | * |
||
111 | * @var string |
||
112 | */ |
||
113 | public $base = ''; |
||
114 | |||
115 | /** |
||
116 | * Firewall panel base controller. |
||
117 | */ |
||
118 | public function __construct() |
||
119 | { |
||
120 | $firewall = Container::get('firewall'); |
||
121 | |||
122 | if (!($firewall instanceof Firewall)) { |
||
123 | throw new RuntimeException( |
||
124 | 'The Firewall instance should be initialized first.' |
||
125 | ); |
||
126 | } |
||
127 | |||
128 | $this->mode = 'managed'; |
||
129 | $this->kernel = $firewall->getKernel(); |
||
130 | $this->configuration = $firewall->getConfiguration(); |
||
131 | $this->directory = $firewall->getDirectory(); |
||
132 | $this->filename = $firewall->getFilename(); |
||
133 | $this->base = SHIELDON_PANEL_BASE; |
||
134 | |||
135 | if (!empty($this->kernel->logger)) { |
||
136 | |||
137 | // We need to know where the logs stored in. |
||
138 | $logDirectory = $this->kernel->logger->getDirectory(); |
||
139 | |||
140 | // Load ActionLogParser for parsing log files. |
||
141 | $this->parser = new ActionLogParser($logDirectory); |
||
142 | |||
143 | $this->pageAvailability['logs'] = true; |
||
144 | } |
||
145 | |||
146 | $flashMessage = get_session()->get('flash_messages'); |
||
147 | |||
148 | // Flash message, use it when redirecting page. |
||
149 | if (!empty($flashMessage)) { |
||
150 | $this->messages = $flashMessage; |
||
151 | get_session()->remove('flash_messages'); |
||
152 | } |
||
153 | |||
154 | $this->locate = 'en'; |
||
155 | |||
156 | $sessionLang = get_session()->get('shieldon_panel_lang'); |
||
157 | |||
158 | if (!empty($sessionLang)) { |
||
159 | $this->locate = $sessionLang; |
||
160 | } |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Load view file. |
||
165 | * |
||
166 | * @param string $page The page type. (filename) |
||
167 | * @param array $data The variables passed to that page. |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | protected function loadView(string $page, array $data = []): string |
||
172 | { |
||
173 | if (!defined('SHIELDON_VIEW')) { |
||
174 | define('SHIELDON_VIEW', true); |
||
175 | } |
||
176 | |||
177 | $viewFilePath = __DIR__ . '/../../../templates/' . $page . '.php'; |
||
178 | |||
179 | if (!empty($data)) { |
||
180 | extract($data); |
||
181 | } |
||
182 | |||
183 | $output = ''; |
||
184 | |||
185 | if (file_exists($viewFilePath)) { |
||
186 | ob_start(); |
||
187 | require $viewFilePath; |
||
188 | $output = ob_get_contents(); |
||
189 | ob_end_clean(); |
||
190 | } |
||
191 | |||
192 | return $output; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Render the web page with full layout. |
||
197 | * |
||
198 | * @param string $page The page type. (filename) |
||
199 | * @param array $data The variables passed to that page. |
||
200 | * |
||
201 | * @return ResponseInterface |
||
202 | */ |
||
203 | protected function renderPage(string $page, array $data): ResponseInterface |
||
204 | { |
||
205 | $channelName = $this->kernel->driver->getChannel(); |
||
206 | |||
207 | if (empty($channelName)) { |
||
208 | $channelName = 'default'; |
||
209 | } |
||
210 | |||
211 | $body['channel_name'] = $channelName; |
||
212 | $body['mode_name'] = $this->mode; |
||
213 | $body['page_url'] = $this->url(); |
||
214 | $body['content'] = $this->loadView($page, $data); |
||
215 | $body['title'] = $data['title'] ?? ''; |
||
216 | |||
217 | $body['title'] .= ' - ' . __('panel', 'title_site_wide', 'Shieldon Firewall'); |
||
218 | $body['title'] .= ' v' . SHIELDON_FIREWALL_VERSION; |
||
219 | |||
220 | $page = $this->loadView('panel/template', $body); |
||
221 | |||
222 | return $this->respond($page); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Return the response instance. |
||
227 | * |
||
228 | * @param string $body The content body. |
||
229 | * |
||
230 | * @return ResponseInterface |
||
231 | */ |
||
232 | protected function respond(string $body): ResponseInterface |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Include a view file. |
||
244 | * |
||
245 | * @param string $page The page type. (filename) |
||
246 | * @param array $data The variables passed to that page. |
||
247 | * |
||
248 | * @return void |
||
249 | */ |
||
250 | protected function _include(string $page, array $data = []): void |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Response message to front. |
||
265 | * |
||
266 | * @param string $type The message status type. error|success |
||
267 | * @param string $text The message body. |
||
268 | * |
||
269 | * @return void |
||
270 | */ |
||
271 | protected function pushMessage(string $type, string $text): void |
||
272 | { |
||
273 | $class = $type; |
||
274 | |||
275 | if ($type == 'error') { |
||
276 | $class = 'danger'; |
||
277 | } |
||
278 | |||
279 | array_push($this->messages, [ |
||
280 | 'type' => $type, |
||
281 | 'text' => $text, |
||
282 | 'class' => $class, |
||
283 | ]); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Return the relative URL. |
||
288 | * |
||
289 | * @param string $path The page's path. |
||
290 | * @param string $tab Tab. |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | protected function url(string $path = '', string $tab = ''): string |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Output HTML input element with CSRF token. |
||
303 | * |
||
304 | * @return void |
||
305 | */ |
||
306 | public function _csrf(): void |
||
311 | } |
||
312 | } |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Save the configuration settings to the JSON file. |
||
317 | * |
||
318 | * @return void |
||
319 | */ |
||
320 | protected function saveConfig(): void |
||
321 | { |
||
322 | $postParams = get_request()->getParsedBody(); |
||
323 | |||
324 | $configFilePath = $this->directory . '/' . $this->filename; |
||
325 | |||
326 | foreach ($this->csrfField as $csrfInfo) { |
||
327 | if (!empty($csrfInfo['name'])) { |
||
328 | unset_superglobal($csrfInfo['name'], 'post'); |
||
329 | } |
||
330 | } |
||
331 | |||
332 | if (empty($postParams) || !is_array($postParams) || 'managed' !== $this->mode) { |
||
333 | return; |
||
334 | } |
||
335 | |||
336 | $this->saveConfigPrepareSettings($postParams); |
||
337 | |||
338 | // Start checking the availibility of the data driver settings. |
||
339 | $result = true; |
||
340 | $result = $this->saveConfigCheckDataDriver($result); |
||
341 | $result = $this->saveConfigCheckActionLogger($result); |
||
342 | $result = $this->saveConfigCheckIptables($result); |
||
343 | |||
344 | // Only update settings while data driver is correctly connected. |
||
345 | if ($result) { |
||
346 | file_put_contents($configFilePath, json_encode($this->configuration)); |
||
347 | |||
348 | $this->pushMessage('success', |
||
349 | __( |
||
350 | 'panel', |
||
351 | 'success_settings_saved', |
||
352 | 'Settings saved.' |
||
353 | ) |
||
354 | ); |
||
355 | } |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * Echo the setting string to the template. |
||
360 | * |
||
361 | * @param string $field Field. |
||
362 | * @param mixed $defailt Default value. |
||
363 | * |
||
364 | * @return void |
||
365 | */ |
||
366 | protected function _(string $field, $default = ''): void |
||
367 | { |
||
368 | if (is_string($this->getConfig($field)) || is_numeric($this->getConfig($field))) { |
||
369 | |||
370 | if ('demo' === $this->mode) { |
||
371 | |||
372 | // Hide sensitive data because of security concerns. |
||
373 | $hiddenForDemo = [ |
||
374 | 'drivers.redis.auth', |
||
375 | 'drivers.file.directory_path', |
||
376 | 'drivers.sqlite.directory_path', |
||
377 | 'drivers.mysql.dbname', |
||
378 | 'drivers.mysql.user', |
||
379 | 'drivers.mysql.pass', |
||
380 | 'captcha_modules.recaptcha.config.site_key', |
||
381 | 'captcha_modules.recaptcha.config.secret_key', |
||
382 | 'loggers.action.config.directory_path', |
||
383 | 'admin.user', |
||
384 | 'admin.pass', |
||
385 | 'admin.last_modified', |
||
386 | 'messengers.telegram.config.api_key', |
||
387 | 'messengers.telegram.config.channel', |
||
388 | 'messengers.sendgrid.config.api_key', |
||
389 | 'messengers.sendgrid.config.sender', |
||
390 | 'messengers.sendgrid.config.recipients', |
||
391 | 'messengers.line_notify.config.access_token', |
||
392 | 'iptables.config.watching_folder', |
||
393 | 'ip6tables.config.watching_folder', |
||
394 | ]; |
||
395 | |||
396 | if (in_array($field, $hiddenForDemo)) { |
||
397 | echo __('panel', 'field_not_visible', 'Cannot view this field in demo mode.'); |
||
398 | } else { |
||
399 | echo (!empty($this->getConfig($field))) ? $this->getConfig($field) : $default; |
||
400 | } |
||
401 | |||
402 | } else { |
||
403 | echo (!empty($this->getConfig($field))) ? $this->getConfig($field) : $default; |
||
404 | } |
||
405 | } elseif (is_array($this->getConfig($field))) { |
||
406 | |||
407 | if ('demo' === $this->mode) { |
||
408 | $hiddenForDemo = [ |
||
409 | 'messengers.sendgrid.config.recipients' |
||
410 | ]; |
||
411 | |||
412 | if (in_array($field, $hiddenForDemo)) { |
||
413 | echo __('panel', 'field_not_visible', 'Cannot view this field in demo mode.'); |
||
414 | } else { |
||
415 | echo implode("\n", $this->getConfig($field)); |
||
416 | } |
||
417 | |||
418 | } else { |
||
419 | echo implode("\n", $this->getConfig($field)); |
||
420 | } |
||
421 | } |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * Use on HTML checkbox and radio elements. |
||
426 | * |
||
427 | * @param string $value |
||
428 | * @param mixed $valueChecked |
||
429 | * @param bool $isConfig |
||
430 | * |
||
431 | * @return void |
||
432 | */ |
||
433 | protected function checked(string $value, $valueChecked, bool $isConfig = true): void |
||
434 | { |
||
435 | if ($isConfig) { |
||
436 | if ($this->getConfig($value) === $valueChecked) { |
||
437 | echo 'checked'; |
||
438 | } else { |
||
439 | echo ''; |
||
440 | } |
||
441 | } else { |
||
442 | if ($value === $valueChecked) { |
||
443 | echo 'checked'; |
||
444 | } else { |
||
445 | echo ''; |
||
446 | } |
||
447 | } |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * Echo correspondence string on Messenger setting page. |
||
452 | * |
||
453 | * @param string $moduleName |
||
454 | * @param string $echoType |
||
455 | * |
||
456 | * @return void |
||
457 | */ |
||
458 | protected function _m(string $moduleName, string $echoType = 'css'): void |
||
459 | { |
||
460 | if ('css' === $echoType) { |
||
461 | echo $this->getConfig('messengers.' . $moduleName . '.confirm_test') ? 'success' : ''; |
||
462 | } |
||
463 | |||
464 | if ('icon' === $echoType) { |
||
465 | echo $this->getConfig('messengers.' . $moduleName . '.confirm_test') ? '<i class="fas fa-check"></i>' : '<i class="fas fa-exclamation"></i>'; |
||
466 | } |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * Use on HTML select elemets. |
||
471 | * |
||
472 | * @param string $value |
||
473 | * @param mixed $valueChecked |
||
474 | * |
||
475 | * @return void |
||
476 | */ |
||
477 | protected function selected(string $value, $valueChecked): void |
||
478 | { |
||
479 | if ($this->getConfig($value) === $valueChecked) { |
||
480 | echo 'selected'; |
||
481 | } else { |
||
482 | echo ''; |
||
483 | } |
||
484 | } |
||
485 | |||
486 | /** |
||
487 | * Parse the POST fields and set them into configuration data structure. |
||
488 | * Used for saveConfig method only. |
||
489 | * |
||
490 | * @param array $postParams |
||
491 | * |
||
492 | * @return void |
||
493 | */ |
||
494 | private function saveConfigPrepareSettings(array $postParams): void |
||
495 | { |
||
496 | foreach ($postParams as $postKey => $postData) { |
||
497 | if (is_string($postData)) { |
||
498 | if ($postData === 'on') { |
||
499 | $this->setConfig(str_replace('__', '.', $postKey), true); |
||
500 | |||
501 | } elseif ($postData === 'off') { |
||
502 | $this->setConfig(str_replace('__', '.', $postKey), false); |
||
503 | |||
504 | } else { |
||
505 | if ($postKey === 'ip_variable_source') { |
||
506 | $this->setConfig('ip_variable_source.REMOTE_ADDR', false); |
||
507 | $this->setConfig('ip_variable_source.HTTP_CF_CONNECTING_IP', false); |
||
508 | $this->setConfig('ip_variable_source.HTTP_X_FORWARDED_FOR', false); |
||
509 | $this->setConfig('ip_variable_source.HTTP_X_FORWARDED_HOST', false); |
||
510 | $this->setConfig('ip_variable_source.' . $postData, true); |
||
511 | |||
512 | } elseif ($postKey === 'dialog_ui__shadow_opacity') { |
||
513 | $this->setConfig('dialog_ui.shadow_opacity', (string) $postData); |
||
514 | |||
515 | } elseif ($postKey === 'admin__pass') { |
||
516 | if (strlen($postParams['admin__pass']) < 58) { |
||
517 | $this->setConfig('admin.pass', password_hash($postData, PASSWORD_BCRYPT)); |
||
518 | } |
||
519 | } else if ($postKey === 'messengers__sendgrid__config__recipients') { |
||
520 | $this->setConfig( |
||
521 | 'messengers.sendgrid.config.recipients', |
||
522 | preg_split('/\r\n|[\r\n]/', |
||
523 | $postData) |
||
524 | ); |
||
525 | } else { |
||
526 | if (is_numeric($postData)) { |
||
527 | $this->setConfig(str_replace('__', '.', $postKey), (int) $postData); |
||
528 | } else { |
||
529 | $this->setConfig(str_replace('__', '.', $postKey), $postData); |
||
530 | } |
||
531 | } |
||
532 | } |
||
533 | } |
||
534 | } |
||
535 | } |
||
536 | |||
537 | /** |
||
538 | * Check the settings of Action Logger. |
||
539 | * |
||
540 | * @return bool |
||
541 | */ |
||
542 | private function saveConfigCheckActionLogger(bool $result): bool |
||
543 | { |
||
544 | if (!$result) { |
||
545 | return false; |
||
546 | } |
||
547 | |||
548 | // Check Action Logger settings. |
||
549 | $enableActionLogger = $this->getConfig('loggers.action.enable'); |
||
550 | $actionLogDir = rtrim($this->getConfig('loggers.action.config.directory_path'), '\\/ '); |
||
551 | |||
552 | $result = true; |
||
553 | |||
554 | if ($enableActionLogger) { |
||
555 | if (empty($actionLogDir)) { |
||
556 | $actionLogDir = $this->directory . '/action_logs'; |
||
557 | } |
||
558 | |||
559 | $this->setConfig('loggers.action.config.directory_path', $actionLogDir); |
||
560 | |||
561 | if (!is_dir($actionLogDir)) { |
||
562 | $originalUmask = umask(0); |
||
563 | @mkdir($actionLogDir, 0777, true); |
||
564 | umask($originalUmask); |
||
565 | } |
||
566 | |||
567 | if (!is_writable($actionLogDir)) { |
||
568 | $result = false; |
||
569 | $this->pushMessage('error', |
||
570 | __( |
||
571 | 'panel', |
||
572 | 'error_logger_directory_not_writable', |
||
573 | 'Action Logger requires the storage directory writable.' |
||
574 | ) |
||
575 | ); |
||
576 | } |
||
577 | } |
||
578 | |||
579 | return $result; |
||
580 | } |
||
581 | |||
582 | /** |
||
583 | * Check the settings of Iptables. |
||
584 | * |
||
585 | * @return bool |
||
586 | */ |
||
587 | private function saveConfigCheckIptables(bool $result): bool |
||
634 | } |
||
635 | |||
636 | /** |
||
637 | * Check the settings of Data drivers. |
||
638 | * |
||
639 | * @return bool |
||
640 | */ |
||
641 | protected function saveConfigCheckDataDriver(bool $result): bool |
||
789 | } |
||
790 | } |
||
791 | |||
792 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths