| Total Complexity | 82 | 
| Total Lines | 763 | 
| Duplicated Lines | 0 % | 
| Changes | 3 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like Firewall 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 Firewall, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 59 | class Firewall  | 
            ||
| 60 | { | 
            ||
| 61 | use FirewallTrait;  | 
            ||
| 62 | use XssProtectionTrait;  | 
            ||
| 63 | |||
| 64 | /**  | 
            ||
| 65 | * Collection of PSR-7 or PSR-15 middlewares.  | 
            ||
| 66 | *  | 
            ||
| 67 | * @var array  | 
            ||
| 68 | */  | 
            ||
| 69 | protected $middlewares = [];  | 
            ||
| 70 | |||
| 71 | /**  | 
            ||
| 72 | * Constructor.  | 
            ||
| 73 | */  | 
            ||
| 74 | public function __construct(?ServerRequestInterface $request = null, ?ResponseInterface $response = null)  | 
            ||
| 75 |     { | 
            ||
| 76 |         Container::set('firewall', $this); | 
            ||
| 77 | |||
| 78 | $this->kernel = new Kernel($request, $response);  | 
            ||
| 79 | }  | 
            ||
| 80 | |||
| 81 | /**  | 
            ||
| 82 | * Set up the path of the configuration file.  | 
            ||
| 83 | *  | 
            ||
| 84 | * @param string $source The path.  | 
            ||
| 85 | * @param string $type The type.  | 
            ||
| 86 | *  | 
            ||
| 87 | * @return void  | 
            ||
| 88 | */  | 
            ||
| 89 | public function configure(string $source, string $type = 'json')  | 
            ||
| 90 |     { | 
            ||
| 91 |         if ($type === 'json') { | 
            ||
| 92 | $this->directory = rtrim($source, '\\/');  | 
            ||
| 93 | $configFilePath = $this->directory . '/' . $this->filename;  | 
            ||
| 94 | |||
| 95 |             if (file_exists($configFilePath)) { | 
            ||
| 96 | $jsonString = file_get_contents($configFilePath);  | 
            ||
| 97 | |||
| 98 |             } else { | 
            ||
| 99 | $jsonString = file_get_contents(__DIR__ . '/../../config.json');  | 
            ||
| 100 | |||
| 101 |                 if (defined('PHP_UNIT_TEST')) { | 
            ||
| 102 | $jsonString = file_get_contents(__DIR__ . '/../../tests/config.json');  | 
            ||
| 103 | }  | 
            ||
| 104 | }  | 
            ||
| 105 | |||
| 106 | $this->configuration = json_decode($jsonString, true);  | 
            ||
| 107 |             $this->kernel->managedBy('managed'); | 
            ||
| 108 | |||
| 109 |         } elseif ($type === 'php') { | 
            ||
| 110 | $this->configuration = require $source;  | 
            ||
| 111 |             $this->kernel->managedBy('config'); | 
            ||
| 112 | }  | 
            ||
| 113 | |||
| 114 | $this->setup();  | 
            ||
| 115 | }  | 
            ||
| 116 | |||
| 117 | /**  | 
            ||
| 118 | * Add middlewares and use them before going into Shieldon kernal.  | 
            ||
| 119 | *  | 
            ||
| 120 | * @param MiddlewareInterface $middleware A PSR-15 middlewares.  | 
            ||
| 121 | *  | 
            ||
| 122 | * @return void  | 
            ||
| 123 | */  | 
            ||
| 124 | public function add(MiddlewareInterface $middleware)  | 
            ||
| 125 |     { | 
            ||
| 126 | $this->middlewares[] = $middleware;  | 
            ||
| 127 | }  | 
            ||
| 128 | |||
| 129 | /**  | 
            ||
| 130 | * Setup everything we need.  | 
            ||
| 131 | *  | 
            ||
| 132 | * @return void  | 
            ||
| 133 | */  | 
            ||
| 134 | public function setup(): void  | 
            ||
| 135 |     { | 
            ||
| 136 |         $this->status = $this->getOption('daemon'); | 
            ||
| 137 | |||
| 138 | $this->setDriver();  | 
            ||
| 139 | |||
| 140 | $this->setChannel();  | 
            ||
| 141 | |||
| 142 | $this->setIpSource();  | 
            ||
| 143 | |||
| 144 | $this->setLogger();  | 
            ||
| 145 | |||
| 146 | $this->setFilters();  | 
            ||
| 147 | |||
| 148 | $this->setComponents();  | 
            ||
| 149 | |||
| 150 | $this->setCaptchas();  | 
            ||
| 151 | |||
| 152 | $this->setSessionLimit();  | 
            ||
| 153 | |||
| 154 | $this->setCronJob();  | 
            ||
| 155 | |||
| 156 | $this->setExcludedUrls();  | 
            ||
| 157 | |||
| 158 | $this->setXssProtection();  | 
            ||
| 159 | |||
| 160 | $this->setAuthentication();  | 
            ||
| 161 | |||
| 162 | $this->setDialogUI();  | 
            ||
| 163 | |||
| 164 | $this->setMessengers();  | 
            ||
| 165 | |||
| 166 | $this->setMessageEvents();  | 
            ||
| 167 | |||
| 168 | $this->setDenyAttempts();  | 
            ||
| 169 | |||
| 170 | $this->setIptablesWatchingFolder();  | 
            ||
| 171 | }  | 
            ||
| 172 | |||
| 173 | /**  | 
            ||
| 174 | * Just, run!  | 
            ||
| 175 | *  | 
            ||
| 176 | * @return ResponseInterface  | 
            ||
| 177 | */  | 
            ||
| 178 | public function run(): ResponseInterface  | 
            ||
| 215 | }  | 
            ||
| 216 | |||
| 217 | /**  | 
            ||
| 218 | * Set the channel ID.  | 
            ||
| 219 | *  | 
            ||
| 220 | * @return void  | 
            ||
| 221 | */  | 
            ||
| 222 | protected function setChannel(): void  | 
            ||
| 228 | }  | 
            ||
| 229 | }  | 
            ||
| 230 | |||
| 231 | /**  | 
            ||
| 232 | * Set a data driver for Shieldon use.  | 
            ||
| 233 | *  | 
            ||
| 234 | * @return void  | 
            ||
| 235 | */  | 
            ||
| 236 | protected function setDriver(): void  | 
            ||
| 237 |     { | 
            ||
| 238 |         $driverType = $this->getOption('driver_type'); | 
            ||
| 239 | |||
| 240 |         switch ($driverType) { | 
            ||
| 241 | |||
| 242 | case 'redis':  | 
            ||
| 243 | |||
| 244 |                 $redisSetting = $this->getOption('redis', 'drivers'); | 
            ||
| 245 | |||
| 246 |                 try { | 
            ||
| 247 | |||
| 248 | $host = '127.0.0.1';  | 
            ||
| 249 | $port = 6379;  | 
            ||
| 250 | |||
| 251 |                     if (!empty($redisSetting['host'])) { | 
            ||
| 252 | $host = $redisSetting['host'];  | 
            ||
| 253 | }  | 
            ||
| 254 | |||
| 255 |                     if (!empty($redisSetting['port'])) { | 
            ||
| 256 | $port = $redisSetting['port'];  | 
            ||
| 257 | }  | 
            ||
| 258 | |||
| 259 | // Create a Redis instance.  | 
            ||
| 260 | $redis = new Redis();  | 
            ||
| 261 | $redis->connect($host, $port);  | 
            ||
| 262 | |||
| 263 |                     if (!empty($redisSetting['auth'])) { | 
            ||
| 264 | |||
| 265 | // @codeCoverageIgnoreStart  | 
            ||
| 266 | $redis->auth($redisSetting['auth']);  | 
            ||
| 267 | // @codeCoverageIgnoreEnd  | 
            ||
| 268 | }  | 
            ||
| 269 | |||
| 270 | // Use Redis data driver.  | 
            ||
| 271 | $this->kernel->add(new Driver\RedisDriver($redis));  | 
            ||
| 272 | |||
| 273 | // @codeCoverageIgnoreStart  | 
            ||
| 274 |                 } catch(RedisException $e) { | 
            ||
| 275 | $this->status = false;  | 
            ||
| 276 | |||
| 277 | echo $e->getMessage();  | 
            ||
| 278 | }  | 
            ||
| 279 | // @codeCoverageIgnoreEnd  | 
            ||
| 280 | |||
| 281 | break;  | 
            ||
| 282 | |||
| 283 | case 'file':  | 
            ||
| 284 | |||
| 285 |                 $fileSetting = $this->getOption('file', 'drivers'); | 
            ||
| 286 | |||
| 287 |                 if (empty($fileSetting['directory_path'])) { | 
            ||
| 288 | $fileSetting['directory_path'] = $this->directory;  | 
            ||
| 289 | }  | 
            ||
| 290 | |||
| 291 | // Use File data driver.  | 
            ||
| 292 | $this->kernel->add(new Driver\FileDriver($fileSetting['directory_path']));  | 
            ||
| 293 | |||
| 294 | break;  | 
            ||
| 295 | |||
| 296 | case 'sqlite':  | 
            ||
| 297 | |||
| 298 |                 $sqliteSetting = $this->getOption('sqlite', 'drivers'); | 
            ||
| 299 | |||
| 300 |                 if (empty($sqliteSetting['directory_path'])) { | 
            ||
| 301 | $sqliteSetting['directory_path'] = '';  | 
            ||
| 302 | $this->status = false;  | 
            ||
| 303 | }  | 
            ||
| 304 | |||
| 305 |                 try { | 
            ||
| 306 | |||
| 307 | // Specific the sqlite file location.  | 
            ||
| 308 | $sqliteLocation = $sqliteSetting['directory_path'] . '/shieldon.sqlite3';  | 
            ||
| 309 | |||
| 310 | // Create a PDO instance.  | 
            ||
| 311 |                     $pdoInstance = new PDO('sqlite:' . $sqliteLocation); | 
            ||
| 312 | |||
| 313 | // Use Sqlite data driver.  | 
            ||
| 314 | $this->kernel->add(new Driver\SqliteDriver($pdoInstance));  | 
            ||
| 315 | |||
| 316 | // @codeCoverageIgnoreStart  | 
            ||
| 317 |                 } catch(PDOException $e) { | 
            ||
| 318 | $this->status = false;  | 
            ||
| 319 | |||
| 320 | echo $e->getMessage();  | 
            ||
| 321 | }  | 
            ||
| 322 | // @codeCoverageIgnoreEnd  | 
            ||
| 323 | |||
| 324 | break;  | 
            ||
| 325 | |||
| 326 | case 'mysql':  | 
            ||
| 327 | default:  | 
            ||
| 328 | |||
| 329 |                 $mysqlSetting = $this->getOption('mysql', 'drivers'); | 
            ||
| 330 | |||
| 331 |                 try { | 
            ||
| 332 | |||
| 333 | // Create a PDO instance.  | 
            ||
| 334 | $pdoInstance = new PDO(  | 
            ||
| 335 | 'mysql:host='  | 
            ||
| 336 | . $mysqlSetting['host'] . ';dbname='  | 
            ||
| 337 | . $mysqlSetting['dbname'] . ';charset='  | 
            ||
| 338 | . $mysqlSetting['charset']  | 
            ||
| 339 | , (string) $mysqlSetting['user']  | 
            ||
| 340 | , (string) $mysqlSetting['pass']  | 
            ||
| 341 | );  | 
            ||
| 342 | |||
| 343 | // Use MySQL data driver.  | 
            ||
| 344 | $this->kernel->add(new Driver\MysqlDriver($pdoInstance));  | 
            ||
| 345 | |||
| 346 | // @codeCoverageIgnoreStart  | 
            ||
| 347 |                 } catch(PDOException $e) { | 
            ||
| 348 | echo $e->getMessage();  | 
            ||
| 349 | }  | 
            ||
| 350 | // @codeCoverageIgnoreEnd  | 
            ||
| 351 | // end switch.  | 
            ||
| 352 | }  | 
            ||
| 353 | }  | 
            ||
| 354 | |||
| 355 | /**  | 
            ||
| 356 | * Set up the action logger.  | 
            ||
| 357 | *  | 
            ||
| 358 | * @return void  | 
            ||
| 359 | */  | 
            ||
| 360 | protected function setLogger(): void  | 
            ||
| 367 | }  | 
            ||
| 368 | }  | 
            ||
| 369 | }  | 
            ||
| 370 | |||
| 371 | /**  | 
            ||
| 372 | * If you use CDN, please choose the real IP source.  | 
            ||
| 373 | *  | 
            ||
| 374 | * @return void  | 
            ||
| 375 | */  | 
            ||
| 376 | protected function setIpSource(): void  | 
            ||
| 377 |     { | 
            ||
| 378 |         $ipSourceType = $this->getOption('ip_variable_source'); | 
            ||
| 379 | $serverParams = get_request()->getServerParams();  | 
            ||
| 380 | |||
| 381 | /**  | 
            ||
| 382 | * REMOTE_ADDR: general  | 
            ||
| 383 | * HTTP_CF_CONNECTING_IP: Cloudflare  | 
            ||
| 384 | * HTTP_X_FORWARDED_FOR: Google Cloud CDN, Google Load-balancer, AWS.  | 
            ||
| 385 | * HTTP_X_FORWARDED_HOST: KeyCDN, or other CDN providers not listed here.  | 
            ||
| 386 | *  | 
            ||
| 387 | */  | 
            ||
| 388 | $key = array_search(true, $ipSourceType);  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 389 | $ip = $serverParams[$key];  | 
            ||
| 390 | |||
| 391 |         if (empty($ip)) { | 
            ||
| 392 | // @codeCoverageIgnoreStart  | 
            ||
| 393 |             throw new RuntimeException('IP source is not set correctly.'); | 
            ||
| 394 | // @codeCoverageIgnoreEnd  | 
            ||
| 395 | }  | 
            ||
| 396 | |||
| 397 | $this->kernel->setIp($ip);  | 
            ||
| 398 | }  | 
            ||
| 399 | |||
| 400 | /**  | 
            ||
| 401 | * Set the filiters.  | 
            ||
| 402 | *  | 
            ||
| 403 | * @return void  | 
            ||
| 404 | */  | 
            ||
| 405 | protected function setFilters(): void  | 
            ||
| 406 |     { | 
            ||
| 407 |         $sessionSetting   = $this->getOption('session', 'filters'); | 
            ||
| 408 |         $cookieSetting    = $this->getOption('cookie', 'filters'); | 
            ||
| 409 |         $refererSetting   = $this->getOption('referer', 'filters'); | 
            ||
| 410 |         $frequencySetting = $this->getOption('frequency', 'filters'); | 
            ||
| 411 | |||
| 412 | $filterConfig = [  | 
            ||
| 413 | 'session' => $sessionSetting['enable'],  | 
            ||
| 414 | 'cookie' => $cookieSetting['enable'],  | 
            ||
| 415 | 'referer' => $refererSetting['enable'],  | 
            ||
| 416 | 'frequency' => $frequencySetting['enable'],  | 
            ||
| 417 | ];  | 
            ||
| 418 | |||
| 419 | $this->kernel->setFilters($filterConfig);  | 
            ||
| 420 | |||
| 421 |         $this->kernel->setProperty('limit_unusual_behavior', [ | 
            ||
| 422 | 'session' => $sessionSetting['config']['quota'] ?? 5,  | 
            ||
| 423 | 'cookie' => $cookieSetting['config']['quota'] ?? 5,  | 
            ||
| 424 | 'referer' => $refererSetting['config']['quota'] ?? 5,  | 
            ||
| 425 | ]);  | 
            ||
| 426 | |||
| 427 |         // if ($frequencySetting['enable']) { | 
            ||
| 428 | $frequencyQuota = [  | 
            ||
| 429 | 's' => $frequencySetting['config']['quota_s'] ?? 2,  | 
            ||
| 430 | 'm' => $frequencySetting['config']['quota_m'] ?? 10,  | 
            ||
| 431 | 'h' => $frequencySetting['config']['quota_h'] ?? 30,  | 
            ||
| 432 | 'd' => $frequencySetting['config']['quota_d'] ?? 60,  | 
            ||
| 433 | ];  | 
            ||
| 434 | |||
| 435 |         $this->kernel->setProperty('time_unit_quota', $frequencyQuota); | 
            ||
| 436 | |||
| 437 |         // if ($cookieSetting['enable']) { | 
            ||
| 438 | $cookieName = $cookieSetting['config']['cookie_name'] ?? 'ssjd';  | 
            ||
| 439 | $cookieDomain = $cookieSetting['config']['cookie_domain'] ?? '';  | 
            ||
| 440 | $cookieValue = $cookieSetting['config']['cookie_value'] ?? '1';  | 
            ||
| 441 | |||
| 442 |         $this->kernel->setProperty('cookie_name', $cookieName); | 
            ||
| 443 |         $this->kernel->setProperty('cookie_domain', $cookieDomain); | 
            ||
| 444 |         $this->kernel->setProperty('cookie_value', $cookieValue); | 
            ||
| 445 | |||
| 446 |         // if ($refererSetting['enable']) { | 
            ||
| 447 |         $this->kernel->setProperty('interval_check_referer', $refererSetting['config']['time_buffer']); | 
            ||
| 448 | |||
| 449 |         // if ($sessionSetting['enable']) { | 
            ||
| 450 |         $this->kernel->setProperty('interval_check_session', $sessionSetting['config']['time_buffer']); | 
            ||
| 451 | |||
| 452 | }  | 
            ||
| 453 | |||
| 454 | /**  | 
            ||
| 455 | * Set the components.  | 
            ||
| 456 | *  | 
            ||
| 457 | * @return void  | 
            ||
| 458 | */  | 
            ||
| 459 | protected function setComponents(): void  | 
            ||
| 460 |     { | 
            ||
| 461 |         $ipSetting = $this->getOption('ip', 'components'); | 
            ||
| 462 |         $rdnsSetting = $this->getOption('rdns', 'components'); | 
            ||
| 463 |         $headerSetting = $this->getOption('header', 'components'); | 
            ||
| 464 |         $userAgentSetting = $this->getOption('user_agent', 'components'); | 
            ||
| 465 |         $trustedBotSetting = $this->getOption('trusted_bot', 'components'); | 
            ||
| 466 | |||
| 467 |         if ($ipSetting['enable']) { | 
            ||
| 468 | $componentIp = new Component\Ip();  | 
            ||
| 469 | $this->kernel->add($componentIp);  | 
            ||
| 470 | $this->ipManager();  | 
            ||
| 471 | }  | 
            ||
| 472 | |||
| 473 |         if ($trustedBotSetting['enable']) { | 
            ||
| 474 | $componentTrustedBot = new Component\TrustedBot();  | 
            ||
| 475 | |||
| 476 |             if ($trustedBotSetting['strict_mode']) { | 
            ||
| 477 | $componentTrustedBot->setStrict(true);  | 
            ||
| 478 | }  | 
            ||
| 479 | |||
| 480 | // This component will only allow popular search engline.  | 
            ||
| 481 | // Other bots will go into the checking process.  | 
            ||
| 482 | $this->kernel->add($componentTrustedBot);  | 
            ||
| 483 | }  | 
            ||
| 484 | |||
| 485 |         if ($headerSetting['enable']) { | 
            ||
| 486 | $componentHeader = new Component\Header();  | 
            ||
| 487 | |||
| 488 | // Deny all vistors without common header information.  | 
            ||
| 489 |             if ($headerSetting['strict_mode']) { | 
            ||
| 490 | $componentHeader->setStrict(true);  | 
            ||
| 491 | }  | 
            ||
| 492 | |||
| 493 | $this->kernel->add($componentHeader);  | 
            ||
| 494 | }  | 
            ||
| 495 | |||
| 496 |         if ($userAgentSetting['enable']) { | 
            ||
| 497 | $componentUserAgent = new Component\UserAgent();  | 
            ||
| 498 | |||
| 499 | // Deny all vistors without user-agent information.  | 
            ||
| 500 |             if ($userAgentSetting['strict_mode']) { | 
            ||
| 501 | $componentUserAgent->setStrict(true);  | 
            ||
| 502 | }  | 
            ||
| 503 | |||
| 504 | $this->kernel->add($componentUserAgent);  | 
            ||
| 505 | }  | 
            ||
| 506 | |||
| 507 |         if ($rdnsSetting['enable']) { | 
            ||
| 508 | $componentRdns = new Component\Rdns();  | 
            ||
| 509 | |||
| 510 | // Visitors with empty RDNS record will be blocked.  | 
            ||
| 511 | // IP resolved hostname (RDNS) and IP address must conform with each other.  | 
            ||
| 512 |             if ($rdnsSetting['strict_mode']) { | 
            ||
| 513 | $componentRdns->setStrict(true);  | 
            ||
| 514 | }  | 
            ||
| 515 | |||
| 516 | $this->kernel->add($componentRdns);  | 
            ||
| 517 | }  | 
            ||
| 518 | }  | 
            ||
| 519 | |||
| 520 | /**  | 
            ||
| 521 | * Set the Captcha modules.  | 
            ||
| 522 | *  | 
            ||
| 523 | * @return void  | 
            ||
| 524 | */  | 
            ||
| 525 | protected function setCaptchas(): void  | 
            ||
| 526 |     { | 
            ||
| 527 |         $recaptchaSetting = $this->getOption('recaptcha', 'captcha_modules'); | 
            ||
| 528 |         $imageSetting = $this->getOption('image', 'captcha_modules'); | 
            ||
| 529 | |||
| 530 |         if ($recaptchaSetting['enable']) { | 
            ||
| 531 | |||
| 532 | $googleRecaptcha = [  | 
            ||
| 533 | 'key' => $recaptchaSetting['config']['site_key'],  | 
            ||
| 534 | 'secret' => $recaptchaSetting['config']['secret_key'],  | 
            ||
| 535 | 'version' => $recaptchaSetting['config']['version'],  | 
            ||
| 536 | 'lang' => $recaptchaSetting['config']['lang'],  | 
            ||
| 537 | ];  | 
            ||
| 538 | |||
| 539 | $this->kernel->add(new Captcha\Recaptcha($googleRecaptcha));  | 
            ||
| 540 | }  | 
            ||
| 541 | |||
| 542 |         if ($imageSetting['enable']) { | 
            ||
| 543 | |||
| 544 | $type = $imageSetting['config']['type'] ?? 'alnum';  | 
            ||
| 545 | $length = $imageSetting['config']['length'] ?? 8;  | 
            ||
| 546 | |||
| 547 |             switch ($type) { | 
            ||
| 548 | case 'numeric':  | 
            ||
| 549 | $imageCaptchaConfig['pool'] = '0123456789';  | 
            ||
| 550 | break;  | 
            ||
| 551 | |||
| 552 | case 'alpha':  | 
            ||
| 553 | $imageCaptchaConfig['pool'] = '0123456789abcdefghijklmnopqrstuvwxyz';  | 
            ||
| 554 | break;  | 
            ||
| 555 | |||
| 556 | case 'alnum':  | 
            ||
| 557 | default:  | 
            ||
| 558 | $imageCaptchaConfig['pool'] = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';  | 
            ||
| 559 | }  | 
            ||
| 560 | |||
| 561 | $imageCaptchaConfig['word_length'] = $length;  | 
            ||
| 562 | |||
| 563 | $this->kernel->add(new Captcha\ImageCaptcha($imageCaptchaConfig));  | 
            ||
| 564 | }  | 
            ||
| 565 | }  | 
            ||
| 566 | |||
| 567 | /**  | 
            ||
| 568 | * Set the messenger modules.  | 
            ||
| 569 | *  | 
            ||
| 570 | * @return void  | 
            ||
| 571 | */  | 
            ||
| 572 | protected function setMessengers(): void  | 
            ||
| 573 |     { | 
            ||
| 574 | // // The ID list of the messenger modules.  | 
            ||
| 575 | $messengerList = [  | 
            ||
| 576 | 'telegram',  | 
            ||
| 577 | 'line_notify',  | 
            ||
| 578 | 'sendgrid',  | 
            ||
| 579 | 'native_php_mail',  | 
            ||
| 580 | 'smtp',  | 
            ||
| 581 | 'mailgun',  | 
            ||
| 582 | 'rocket_chat',  | 
            ||
| 583 | 'slack',  | 
            ||
| 584 | 'slack_webhook',  | 
            ||
| 585 | ];  | 
            ||
| 586 | |||
| 587 |         foreach ($messengerList as $messenger) { | 
            ||
| 588 | $setting = $this->getOption($messenger, 'messengers');  | 
            ||
| 589 | |||
| 590 |             if (is_array($setting)) { | 
            ||
| 591 | |||
| 592 | // Initialize messenger instances from the factory/  | 
            ||
| 593 |                 if (MessengerFactory::check($messenger, $setting)) { | 
            ||
| 594 | |||
| 595 | $this->kernel->add(  | 
            ||
| 596 | MessengerFactory::getInstance(  | 
            ||
| 597 | // The ID of the messenger module in the configuration.  | 
            ||
| 598 | $messenger,  | 
            ||
| 599 | // The settings of the messenger module in the configuration.  | 
            ||
| 600 | $setting  | 
            ||
| 601 | )  | 
            ||
| 602 | );  | 
            ||
| 603 | }  | 
            ||
| 604 | }  | 
            ||
| 605 | |||
| 606 | unset($setting);  | 
            ||
| 607 | }  | 
            ||
| 608 | }  | 
            ||
| 609 | |||
| 610 | /**  | 
            ||
| 611 | * Set message events.  | 
            ||
| 612 | *  | 
            ||
| 613 | * @return void  | 
            ||
| 614 | */  | 
            ||
| 615 | protected function setMessageEvents(): void  | 
            ||
| 616 |     { | 
            ||
| 617 |         $setting = $this->getOption('failed_attempts_in_a_row', 'events'); | 
            ||
| 618 | |||
| 619 | $notifyDataCircle = false;  | 
            ||
| 620 | $notifySystemFirewall = false;  | 
            ||
| 621 | |||
| 622 |         if ($setting['data_circle']['messenger']) { | 
            ||
| 623 | $notifyDataCircle = true;  | 
            ||
| 624 | }  | 
            ||
| 625 | |||
| 626 |         if ($setting['system_firewall']['messenger']) { | 
            ||
| 627 | $notifyDataCircle = true;  | 
            ||
| 628 | }  | 
            ||
| 629 | |||
| 630 |         $this->kernel->setProperty('deny_attempt_notify', [ | 
            ||
| 631 | 'data_circle' => $notifyDataCircle,  | 
            ||
| 632 | 'system_firewall' => $notifySystemFirewall,  | 
            ||
| 633 | ]);  | 
            ||
| 634 | }  | 
            ||
| 635 | |||
| 636 | /**  | 
            ||
| 637 | * Set deny attempts.  | 
            ||
| 638 | *  | 
            ||
| 639 | * @return void  | 
            ||
| 640 | */  | 
            ||
| 641 | protected function setDenyAttempts(): void  | 
            ||
| 642 |     { | 
            ||
| 643 |         $setting = $this->getOption('failed_attempts_in_a_row', 'events'); | 
            ||
| 644 | |||
| 645 | $enableDataCircle = false;  | 
            ||
| 646 | $enableSystemFirewall = false;  | 
            ||
| 647 | |||
| 648 |         if ($setting['data_circle']['enable']) { | 
            ||
| 649 | $enableDataCircle = true;  | 
            ||
| 650 | }  | 
            ||
| 651 | |||
| 652 |         if ($setting['system_firewall']['enable']) { | 
            ||
| 653 | $enableSystemFirewall = true;  | 
            ||
| 654 | }  | 
            ||
| 655 | |||
| 656 |         $this->kernel->setProperty('deny_attempt_enable', [ | 
            ||
| 657 | 'data_circle' => $enableDataCircle,  | 
            ||
| 658 | 'system_firewall' => $enableSystemFirewall,  | 
            ||
| 659 | ]);  | 
            ||
| 660 | |||
| 661 |         $this->kernel->setProperty('deny_attempt_buffer', [ | 
            ||
| 662 | 'data_circle' => $setting['data_circle']['buffer'] ?? 10,  | 
            ||
| 663 | 'system_firewall' => $setting['data_circle']['buffer'] ?? 10,  | 
            ||
| 664 | ]);  | 
            ||
| 665 | |||
| 666 | // Check the time of the last failed attempt. @since 0.2.0  | 
            ||
| 667 |         $recordAttempt = $this->getOption('record_attempt'); | 
            ||
| 668 | |||
| 669 | $detectionPeriod = $recordAttempt['detection_period'] ?? 5;  | 
            ||
| 670 | $timeToReset = $recordAttempt['time_to_reset'] ?? 1800;  | 
            ||
| 671 | |||
| 672 |         $this->kernel->setProperty('record_attempt_detection_period', $detectionPeriod); | 
            ||
| 673 |         $this->kernel->setProperty('reset_attempt_counter', $timeToReset); | 
            ||
| 674 | }  | 
            ||
| 675 | |||
| 676 | /**  | 
            ||
| 677 | * Set iptables working folder.  | 
            ||
| 678 | *  | 
            ||
| 679 | * @return void  | 
            ||
| 680 | */  | 
            ||
| 681 | protected function setIptablesWatchingFolder(): void  | 
            ||
| 682 |     { | 
            ||
| 683 |         $iptablesSetting = $this->getOption('config', 'iptables'); | 
            ||
| 684 |         $this->kernel->setProperty('iptables_watching_folder',  $iptablesSetting['watching_folder']); | 
            ||
| 685 | }  | 
            ||
| 686 | |||
| 687 | /**  | 
            ||
| 688 | * Set the online session limit.  | 
            ||
| 689 | *  | 
            ||
| 690 | * @return void  | 
            ||
| 691 | */  | 
            ||
| 692 | protected function setSessionLimit(): void  | 
            ||
| 693 |     { | 
            ||
| 694 |         $sessionLimitSetting = $this->getOption('online_session_limit'); | 
            ||
| 695 | |||
| 696 |         if ($sessionLimitSetting['enable']) { | 
            ||
| 697 | |||
| 698 | $onlineUsers = $sessionLimitSetting['config']['count'] ?? 100;  | 
            ||
| 699 | $alivePeriod = $sessionLimitSetting['config']['period'] ?? 300;  | 
            ||
| 700 | |||
| 701 | $this->kernel->limitSession($onlineUsers, $alivePeriod);  | 
            ||
| 702 | }  | 
            ||
| 703 | }  | 
            ||
| 704 | |||
| 705 | /**  | 
            ||
| 706 | * Set the cron job.  | 
            ||
| 707 | * This is triggered by the pageviews, not system cron job.  | 
            ||
| 708 | *  | 
            ||
| 709 | * @return void  | 
            ||
| 710 | */  | 
            ||
| 711 | protected function setCronJob(): void  | 
            ||
| 712 |     { | 
            ||
| 713 |         $cronjobSetting = $this->getOption('reset_circle', 'cronjob'); | 
            ||
| 714 | |||
| 715 |         if ($cronjobSetting['enable']) { | 
            ||
| 716 | |||
| 717 | $nowTime = time();  | 
            ||
| 718 | |||
| 719 | $lastResetTime = $cronjobSetting['config']['last_update'];  | 
            ||
| 720 | |||
| 721 |             if (!empty($lastResetTime) ) { | 
            ||
| 722 | $lastResetTime = strtotime($lastResetTime);  | 
            ||
| 723 |             } else { | 
            ||
| 724 | // @codeCoverageIgnoreStart  | 
            ||
| 725 |                 $lastResetTime = strtotime(date('Y-m-d 00:00:00')); | 
            ||
| 726 | // @codeCoverageIgnoreEnd  | 
            ||
| 727 | }  | 
            ||
| 728 | |||
| 729 |             if (($nowTime - $lastResetTime) > $cronjobSetting['config']['period']) { | 
            ||
| 730 | |||
| 731 |                 $updateResetTime = date('Y-m-d 00:00:00'); | 
            ||
| 732 | |||
| 733 | // Update new reset time.  | 
            ||
| 734 |                 $this->setConfig('cronjob.reset_circle.config.last_update', $updateResetTime); | 
            ||
| 735 | $this->updateConfig();  | 
            ||
| 736 | |||
| 737 | // Remove all logs.  | 
            ||
| 738 | $this->kernel->driver->rebuild();  | 
            ||
| 739 | }  | 
            ||
| 740 | }  | 
            ||
| 741 | }  | 
            ||
| 742 | |||
| 743 | /**  | 
            ||
| 744 | * Set the URLs that want to be excluded from Shieldon protection.  | 
            ||
| 745 | *  | 
            ||
| 746 | * @return void  | 
            ||
| 747 | */  | 
            ||
| 748 | protected function setExcludedUrls(): void  | 
            ||
| 749 |     { | 
            ||
| 750 |         $excludedUrls = $this->getOption('excluded_urls'); | 
            ||
| 751 | |||
| 752 |         if (!empty($excludedUrls)) { | 
            ||
| 753 | $list = array_column($excludedUrls, 'url');  | 
            ||
| 754 | |||
| 755 | $this->kernel->setExcludedUrls($list);  | 
            ||
| 756 | }  | 
            ||
| 757 | }  | 
            ||
| 758 | |||
| 759 | |||
| 760 | |||
| 761 | /**  | 
            ||
| 762 | * WWW-Athentication.  | 
            ||
| 763 | *  | 
            ||
| 764 | * @return void  | 
            ||
| 765 | */  | 
            ||
| 766 | protected function setAuthentication(): void  | 
            ||
| 767 |     { | 
            ||
| 768 |         $authenticateList = $this->getOption('www_authenticate'); | 
            ||
| 769 | |||
| 770 |         if (is_array($authenticateList)) { | 
            ||
| 771 | $this->add(new Middleware\httpAuthentication($authenticateList));  | 
            ||
| 772 | }  | 
            ||
| 773 | }  | 
            ||
| 774 | |||
| 775 | /**  | 
            ||
| 776 | * IP manager.  | 
            ||
| 777 | */  | 
            ||
| 778 | protected function ipManager()  | 
            ||
| 779 |     { | 
            ||
| 780 |         $ipList = $this->getOption('ip_manager'); | 
            ||
| 781 | |||
| 782 | $allowedList = [];  | 
            ||
| 783 | $deniedList = [];  | 
            ||
| 784 | |||
| 785 |         if (!empty($ipList)) { | 
            ||
| 786 |             foreach ($ipList as $ip) { | 
            ||
| 787 | |||
| 788 |                 if (0 === strpos($this->kernel->getCurrentUrl(), $ip['url']) ) { | 
            ||
| 789 | |||
| 790 |                     if ('allow' === $ip['rule']) { | 
            ||
| 791 | $allowedList[] = $ip['ip'];  | 
            ||
| 792 | }  | 
            ||
| 793 | |||
| 794 |                     if ('deny' === $ip['rule']) { | 
            ||
| 795 | $deniedList[] = $ip['ip'];  | 
            ||
| 796 | }  | 
            ||
| 797 | }  | 
            ||
| 798 | }  | 
            ||
| 799 | }  | 
            ||
| 800 | |||
| 801 |         if (!empty($allowedList)) { | 
            ||
| 802 | $this->kernel->component['Ip']->setAllowedItems($allowedList);  | 
            ||
| 803 | }  | 
            ||
| 804 | |||
| 805 |         if (!empty($deniedList)) { | 
            ||
| 806 | $this->kernel->component['Ip']->setDeniedItems($deniedList);  | 
            ||
| 807 | }  | 
            ||
| 808 | }  | 
            ||
| 809 | |||
| 810 | /**  | 
            ||
| 811 | * Set dialog UI.  | 
            ||
| 812 | *  | 
            ||
| 813 | * @return void  | 
            ||
| 814 | */  | 
            ||
| 815 | protected function setDialogUI()  | 
            ||
| 822 | }  | 
            ||
| 823 | }  | 
            ||
| 824 | |||
| 825 | |||
| 826 | }  | 
            ||
| 827 |