| Total Complexity | 76 |
| Total Lines | 879 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Kernel 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 Kernel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 84 | class Kernel |
||
| 85 | { |
||
| 86 | use IpTrait; |
||
| 87 | use FilterTrait; |
||
| 88 | use ComponentTrait; |
||
| 89 | use RuleTrait; |
||
| 90 | use LimitSessionTrait; |
||
| 91 | |||
| 92 | // Reason codes (allow) |
||
| 93 | const REASON_IS_SEARCH_ENGINE = 100; |
||
| 94 | const REASON_IS_GOOGLE = 101; |
||
| 95 | const REASON_IS_BING = 102; |
||
| 96 | const REASON_IS_YAHOO = 103; |
||
| 97 | const REASON_IS_SOCIAL_NETWORK = 110; |
||
| 98 | const REASON_IS_FACEBOOK = 111; |
||
| 99 | const REASON_IS_TWITTER = 112; |
||
| 100 | |||
| 101 | // Reason codes (deny) |
||
| 102 | const REASON_TOO_MANY_SESSIONS = 1; |
||
| 103 | const REASON_TOO_MANY_ACCESSES = 2; // (not used) |
||
| 104 | const REASON_EMPTY_JS_COOKIE = 3; |
||
| 105 | const REASON_EMPTY_REFERER = 4; |
||
| 106 | |||
| 107 | const REASON_REACHED_LIMIT_DAY = 11; |
||
| 108 | const REASON_REACHED_LIMIT_HOUR = 12; |
||
| 109 | const REASON_REACHED_LIMIT_MINUTE = 13; |
||
| 110 | const REASON_REACHED_LIMIT_SECOND = 14; |
||
| 111 | |||
| 112 | const REASON_INVALID_IP = 40; |
||
| 113 | const REASON_DENY_IP = 41; |
||
| 114 | const REASON_ALLOW_IP = 42; |
||
| 115 | |||
| 116 | const REASON_COMPONENT_IP = 81; |
||
| 117 | const REASON_COMPONENT_RDNS = 82; |
||
| 118 | const REASON_COMPONENT_HEADER = 83; |
||
| 119 | const REASON_COMPONENT_USERAGENT = 84; |
||
| 120 | const REASON_COMPONENT_TRUSTED_ROBOT = 85; |
||
| 121 | |||
| 122 | const REASON_MANUAL_BAN = 99; |
||
| 123 | |||
| 124 | // Action codes |
||
| 125 | const ACTION_DENY = 0; |
||
| 126 | const ACTION_ALLOW = 1; |
||
| 127 | const ACTION_TEMPORARILY_DENY = 2; |
||
| 128 | const ACTION_UNBAN = 9; |
||
| 129 | |||
| 130 | // Result codes |
||
| 131 | const RESPONSE_DENY = 0; |
||
| 132 | const RESPONSE_ALLOW = 1; |
||
| 133 | const RESPONSE_TEMPORARILY_DENY = 2; |
||
| 134 | const RESPONSE_LIMIT_SESSION = 3; |
||
| 135 | |||
| 136 | const LOG_LIMIT = 3; |
||
| 137 | const LOG_PAGEVIEW = 11; |
||
| 138 | const LOG_BLACKLIST = 98; |
||
| 139 | const LOG_CAPTCHA = 99; |
||
| 140 | |||
| 141 | const KERNEL_DIR = __DIR__; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Driver for storing data. |
||
| 145 | * |
||
| 146 | * @var \Shieldon\Firewall\Driver\DriverProvider |
||
| 147 | */ |
||
| 148 | public $driver; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Logger instance. |
||
| 152 | * |
||
| 153 | * @var ActionLogger |
||
| 154 | */ |
||
| 155 | public $logger; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * The closure functions that will be executed in this->run() |
||
| 159 | * |
||
| 160 | * @var array |
||
| 161 | */ |
||
| 162 | protected $closures = []; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * default settings |
||
| 166 | * |
||
| 167 | * @var array |
||
| 168 | */ |
||
| 169 | protected $properties = []; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * This is for creating data tables automatically |
||
| 173 | * Turn it off, if you don't want to check data tables every connection. |
||
| 174 | * |
||
| 175 | * @var bool |
||
| 176 | */ |
||
| 177 | protected $autoCreateDatabase = true; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Container for captcha addons. |
||
| 181 | * The collection of \Shieldon\Firewall\Captcha\CaptchaInterface |
||
| 182 | * |
||
| 183 | * @var array |
||
| 184 | */ |
||
| 185 | public $captcha = []; |
||
| 186 | |||
| 187 | /** |
||
| 188 | * The ways Shieldon send a message to when someone has been blocked. |
||
| 189 | * The collection of \Shieldon\Messenger\Messenger\MessengerInterface |
||
| 190 | * |
||
| 191 | * @var array |
||
| 192 | */ |
||
| 193 | protected $messenger = []; |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Result. |
||
| 197 | * |
||
| 198 | * @var int |
||
| 199 | */ |
||
| 200 | protected $result = 1; |
||
| 201 | |||
| 202 | /** |
||
| 203 | * URLs that are excluded from Shieldon's protection. |
||
| 204 | * |
||
| 205 | * @var array |
||
| 206 | */ |
||
| 207 | protected $excludedUrls = []; |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Which type of configuration source that Shieldon firewall managed? |
||
| 211 | * |
||
| 212 | * @var string |
||
| 213 | */ |
||
| 214 | protected $firewallType = 'self'; // managed | config | self | demo |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Custom dialog UI settings. |
||
| 218 | * |
||
| 219 | * @var array |
||
| 220 | */ |
||
| 221 | protected $dialogUI = []; |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Strict mode. |
||
| 225 | * |
||
| 226 | * Set by `strictMode()` only. The default value of this propertry is undefined. |
||
| 227 | * |
||
| 228 | * @var bool|null |
||
| 229 | */ |
||
| 230 | protected $strictMode; |
||
| 231 | |||
| 232 | /** |
||
| 233 | * The directory in where the frontend template files are placed. |
||
| 234 | * |
||
| 235 | * @var string |
||
| 236 | */ |
||
| 237 | protected $templateDirectory = ''; |
||
| 238 | |||
| 239 | /** |
||
| 240 | * The message that will be sent to the third-party API. |
||
| 241 | * |
||
| 242 | * @var string |
||
| 243 | */ |
||
| 244 | protected $msgBody = ''; |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Shieldon constructor. |
||
| 248 | * |
||
| 249 | * @param ServerRequestInterface|null $request A PSR-7 server request. |
||
| 250 | * |
||
| 251 | * @return void |
||
| 252 | */ |
||
| 253 | public function __construct(?ServerRequestInterface $request = null, ?ResponseInterface $response = null) |
||
| 254 | { |
||
| 255 | // Load helper functions. This is the must. |
||
| 256 | new Helpers(); |
||
| 257 | |||
| 258 | $request = $request ?? HttpFactory::createRequest(); |
||
| 259 | $response = $response ?? HttpFactory::createResponse(); |
||
| 260 | $session = HttpFactory::createSession(); |
||
| 261 | |||
| 262 | $this->properties = get_default_properties(); |
||
| 263 | $this->setCaptcha(new Foundation()); |
||
| 264 | |||
| 265 | Container::set('request', $request); |
||
| 266 | Container::set('response', $response); |
||
| 267 | Container::set('session', $session); |
||
| 268 | Container::set('shieldon', $this); |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Log actions. |
||
| 273 | * |
||
| 274 | * @param int $actionCode The code number of the action. |
||
| 275 | * |
||
| 276 | * @return void |
||
| 277 | */ |
||
| 278 | protected function log(int $actionCode): void |
||
| 279 | { |
||
| 280 | if (!$this->logger) { |
||
| 281 | return; |
||
| 282 | } |
||
| 283 | |||
| 284 | $logData = []; |
||
| 285 | $logData['ip'] = $this->getIp(); |
||
| 286 | $logData['session_id'] = get_session()->get('id'); |
||
| 287 | $logData['action_code'] = $actionCode; |
||
| 288 | $logData['timesamp'] = time(); |
||
| 289 | |||
| 290 | $this->logger->add($logData); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Run, run, run! |
||
| 295 | * |
||
| 296 | * Check the rule tables first, if an IP address has been listed. |
||
| 297 | * Call function filter() if an IP address is not listed in rule tables. |
||
| 298 | * |
||
| 299 | * @return int The response code. |
||
| 300 | */ |
||
| 301 | protected function process(): int |
||
| 302 | { |
||
| 303 | $this->driver->init($this->autoCreateDatabase); |
||
| 304 | |||
| 305 | $this->initComponents(); |
||
| 306 | |||
| 307 | /* |
||
| 308 | |-------------------------------------------------------------------------- |
||
| 309 | | Stage - Looking for rule table. |
||
| 310 | |-------------------------------------------------------------------------- |
||
| 311 | */ |
||
| 312 | |||
| 313 | if ($this->IsRuleExist()) { |
||
| 314 | return $this->result; |
||
| 315 | } |
||
| 316 | |||
| 317 | /* |
||
| 318 | |-------------------------------------------------------------------------- |
||
| 319 | | Statge - Detect popular search engine. |
||
| 320 | |-------------------------------------------------------------------------- |
||
| 321 | */ |
||
| 322 | |||
| 323 | if ($this->isTrustedBot()) { |
||
| 324 | return $this->result; |
||
| 325 | } |
||
| 326 | |||
| 327 | if ($this->isFakeRobot()) { |
||
| 328 | return $this->result; |
||
| 329 | } |
||
| 330 | |||
| 331 | /* |
||
| 332 | |-------------------------------------------------------------------------- |
||
| 333 | | Stage - IP component. |
||
| 334 | |-------------------------------------------------------------------------- |
||
| 335 | */ |
||
| 336 | |||
| 337 | if ($this->isIpComponent()) { |
||
| 338 | return $this->result; |
||
| 339 | } |
||
| 340 | |||
| 341 | /* |
||
| 342 | |-------------------------------------------------------------------------- |
||
| 343 | | Stage - Check all other components. |
||
| 344 | |-------------------------------------------------------------------------- |
||
| 345 | */ |
||
| 346 | |||
| 347 | foreach ($this->component as $component) { |
||
| 348 | |||
| 349 | // check if is a a bad robot already defined in settings. |
||
| 350 | if ($component->isDenied()) { |
||
| 351 | |||
| 352 | // @since 0.1.8 |
||
| 353 | $this->action( |
||
| 354 | self::ACTION_DENY, |
||
| 355 | $component->getDenyStatusCode() |
||
| 356 | ); |
||
| 357 | |||
| 358 | return $this->result = self::RESPONSE_DENY; |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | /* |
||
| 363 | |-------------------------------------------------------------------------- |
||
| 364 | | Stage - Filters |
||
| 365 | |-------------------------------------------------------------------------- |
||
| 366 | | This IP address is not listed in rule table, let's detect it. |
||
| 367 | | |
||
| 368 | */ |
||
| 369 | |||
| 370 | if (array_search(true, $this->filterStatus)) { |
||
| 371 | return $this->result = $this->sessionHandler($this->filter()); |
||
| 372 | } |
||
| 373 | |||
| 374 | return $this->result = $this->sessionHandler(self::RESPONSE_ALLOW); |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Start an action for this IP address, allow or deny, and give a reason for it. |
||
| 379 | * |
||
| 380 | * @param int $actionCode - 0: deny, 1: allow, 9: unban. |
||
| 381 | * @param string $reasonCode |
||
| 382 | * @param string $assignIp |
||
| 383 | * |
||
| 384 | * @return void |
||
| 385 | */ |
||
| 386 | protected function action(int $actionCode, int $reasonCode, string $assignIp = ''): void |
||
| 428 | } |
||
| 429 | } |
||
| 430 | |||
| 431 | // @codeCoverageIgnoreEnd |
||
| 432 | |||
| 433 | /* |
||
| 434 | | ------------------------------------------------------------------- |
||
| 435 | | Public APIs |
||
| 436 | | ------------------------------------------------------------------- |
||
| 437 | */ |
||
| 438 | |||
| 439 | |||
| 440 | |||
| 441 | /** |
||
| 442 | * Set a captcha. |
||
| 443 | * |
||
| 444 | * @param CaptchaInterface $instance |
||
| 445 | * |
||
| 446 | * @return void |
||
| 447 | */ |
||
| 448 | public function setCaptcha(CaptchaInterface $instance): void |
||
| 449 | { |
||
| 450 | $class = $this->getClassName($instance); |
||
| 451 | $this->captcha[$class] = $instance; |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Set a data driver. |
||
| 456 | * |
||
| 457 | * @param DriverProvider $driver Query data from the driver you choose to use. |
||
| 458 | * |
||
| 459 | * @return void |
||
| 460 | */ |
||
| 461 | public function setDriver(DriverProvider $driver): void |
||
| 462 | { |
||
| 463 | $this->driver = $driver; |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Set a action log logger. |
||
| 468 | * |
||
| 469 | * @param ActionLogger $logger |
||
| 470 | * |
||
| 471 | * @return void |
||
| 472 | */ |
||
| 473 | public function setLogger(ActionLogger $logger): void |
||
| 474 | { |
||
| 475 | $this->logger = $logger; |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Set a messenger |
||
| 480 | * |
||
| 481 | * @param MessengerInterfa $instance |
||
| 482 | * |
||
| 483 | * @return void |
||
| 484 | */ |
||
| 485 | public function setMessenger(MessengerInterface $instance): void |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Strict mode. |
||
| 493 | * This option will take effects to all components. |
||
| 494 | * |
||
| 495 | * @param bool $bool Set true to enble strict mode, false to disable it overwise. |
||
| 496 | * |
||
| 497 | * @return void |
||
| 498 | */ |
||
| 499 | public function setStrict(bool $bool) |
||
| 502 | } |
||
| 503 | |||
| 504 | |||
| 505 | |||
| 506 | /** |
||
| 507 | * For first time installation only. This is for creating data tables automatically. |
||
| 508 | * Turning it on will check the data tables exist or not at every single pageview, |
||
| 509 | * it's not good for high traffic websites. |
||
| 510 | * |
||
| 511 | * @param bool $bool |
||
| 512 | * |
||
| 513 | * @return void |
||
| 514 | */ |
||
| 515 | public function createDatabase(bool $bool) |
||
| 516 | { |
||
| 517 | $this->autoCreateDatabase = $bool; |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Set a data channel. |
||
| 522 | * |
||
| 523 | * This will create databases for the channel. |
||
| 524 | * |
||
| 525 | * @param string $channel Specify a channel. |
||
| 526 | * |
||
| 527 | * @return void |
||
| 528 | */ |
||
| 529 | public function setChannel(string $channel) |
||
| 530 | { |
||
| 531 | if (!$this->driver instanceof DriverProvider) { |
||
| 532 | throw new LogicException('setChannel method requires setDriver set first.'); |
||
| 533 | } else { |
||
| 534 | $this->driver->setChannel($channel); |
||
| 535 | } |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Return the result from Captchas. |
||
| 540 | * |
||
| 541 | * @return bool |
||
| 542 | */ |
||
| 543 | public function captchaResponse(): bool |
||
| 544 | { |
||
| 545 | foreach ($this->captcha as $captcha) { |
||
| 546 | |||
| 547 | if (!$captcha->response()) { |
||
| 548 | return false; |
||
| 549 | } |
||
| 550 | } |
||
| 551 | |||
| 552 | if (!empty($this->sessionLimit['count'])) { |
||
| 553 | $this->result = $this->sessionHandler(self::RESPONSE_ALLOW); |
||
| 554 | } |
||
| 555 | |||
| 556 | return true; |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Ban an IP. |
||
| 561 | * |
||
| 562 | * @param string $ip A valid IP address. |
||
| 563 | * |
||
| 564 | * @return void |
||
| 565 | */ |
||
| 566 | public function ban(string $ip = ''): void |
||
| 575 | ); |
||
| 576 | } |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Unban an IP. |
||
| 580 | * |
||
| 581 | * @param string $ip A valid IP address. |
||
| 582 | * |
||
| 583 | * @return void |
||
| 584 | */ |
||
| 585 | public function unban(string $ip = ''): void |
||
| 586 | { |
||
| 587 | if ('' === $ip) { |
||
| 588 | $ip = $this->ip; |
||
| 589 | } |
||
| 590 | |||
| 591 | $this->action( |
||
| 592 | self::ACTION_UNBAN, |
||
| 593 | self::REASON_MANUAL_BAN, $ip |
||
| 594 | ); |
||
| 595 | $this->log(self::ACTION_UNBAN); |
||
| 596 | |||
| 597 | $this->result = self::RESPONSE_ALLOW; |
||
| 598 | } |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Set a property setting. |
||
| 602 | * |
||
| 603 | * @param string $key The key of a property setting. |
||
| 604 | * @param mixed $value The value of a property setting. |
||
| 605 | * |
||
| 606 | * @return void |
||
| 607 | */ |
||
| 608 | public function setProperty(string $key = '', $value = '') |
||
| 609 | { |
||
| 610 | if (isset($this->properties[$key])) { |
||
| 611 | $this->properties[$key] = $value; |
||
| 612 | } |
||
| 613 | } |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Set the property settings. |
||
| 617 | * |
||
| 618 | * @param array $settings The settings. |
||
| 619 | * |
||
| 620 | * @return void |
||
| 621 | */ |
||
| 622 | public function setProperties(array $settings): void |
||
| 623 | { |
||
| 624 | foreach (array_keys($this->properties) as $k) { |
||
| 625 | if (isset($settings[$k])) { |
||
| 626 | $this->properties[$k] = $settings[$k]; |
||
| 627 | } |
||
| 628 | } |
||
| 629 | } |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Limt online sessions. |
||
| 633 | * |
||
| 634 | * @param int $count |
||
| 635 | * @param int $period |
||
| 636 | * |
||
| 637 | * @return void |
||
| 638 | */ |
||
| 639 | public function limitSession(int $count = 1000, int $period = 300): void |
||
| 640 | { |
||
| 641 | $this->sessionLimit = [ |
||
| 642 | 'count' => $count, |
||
| 643 | 'period' => $period |
||
| 644 | ]; |
||
| 645 | } |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Customize the dialog UI. |
||
| 649 | * |
||
| 650 | * @return void |
||
| 651 | */ |
||
| 652 | public function setDialogUI(array $settings): void |
||
| 655 | } |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Set the frontend template directory. |
||
| 659 | * |
||
| 660 | * @param string $directory |
||
| 661 | * |
||
| 662 | * @return void |
||
| 663 | */ |
||
| 664 | public function setTemplateDirectory(string $directory) |
||
| 665 | { |
||
| 666 | if (!is_dir($directory)) { |
||
| 667 | throw new InvalidArgumentException('The template directory does not exist.'); |
||
| 668 | } |
||
| 669 | $this->templateDirectory = $directory; |
||
| 670 | } |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Get a template PHP file. |
||
| 674 | * |
||
| 675 | * @param string $type The template type. |
||
| 676 | * |
||
| 677 | * @return string |
||
| 678 | */ |
||
| 679 | protected function getTemplate(string $type): string |
||
| 680 | { |
||
| 681 | $directory = self::KERNEL_DIR . '/../../templates/frontend'; |
||
| 682 | |||
| 683 | if (!empty($this->templateDirectory)) { |
||
| 684 | $directory = $this->templateDirectory; |
||
| 685 | } |
||
| 686 | |||
| 687 | $path = $directory . '/' . $type . '.php'; |
||
| 688 | |||
| 689 | if (!file_exists($path)) { |
||
| 690 | throw new RuntimeException( |
||
| 691 | sprintf( |
||
| 692 | 'The templeate file is missing. (%s)', |
||
| 693 | $path |
||
| 694 | ) |
||
| 695 | ); |
||
| 696 | } |
||
| 697 | |||
| 698 | return $path; |
||
| 699 | } |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Get a class name without namespace string. |
||
| 703 | * |
||
| 704 | * @param object $instance Class |
||
| 705 | * |
||
| 706 | * @return void |
||
| 707 | */ |
||
| 708 | protected function getClassName($instance): string |
||
| 709 | { |
||
| 710 | $class = get_class($instance); |
||
| 711 | return substr($class, strrpos($class, '\\') + 1); |
||
| 712 | } |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Respond the result. |
||
| 716 | * |
||
| 717 | * @return ResponseInterface |
||
| 718 | */ |
||
| 719 | public function respond(): ResponseInterface |
||
| 804 | } |
||
| 805 | |||
| 806 | /** |
||
| 807 | * Run, run, run! |
||
| 808 | * |
||
| 809 | * Check the rule tables first, if an IP address has been listed. |
||
| 810 | * Call function filter() if an IP address is not listed in rule tables. |
||
| 811 | * |
||
| 812 | * @return |
||
| 813 | */ |
||
| 814 | public function run(): int |
||
| 815 | { |
||
| 816 | if (!isset($this->driver)) { |
||
| 817 | throw new RuntimeException( |
||
| 818 | 'Must register at least one data driver.' |
||
| 819 | ); |
||
| 820 | } |
||
| 821 | |||
| 822 | // Ignore the excluded urls. |
||
| 823 | if (!empty($this->excludedUrls)) { |
||
| 824 | foreach ($this->excludedUrls as $url) { |
||
| 825 | if (0 === strpos(get_request()->getUri()->getPath(), $url)) { |
||
| 826 | return $this->result = self::RESPONSE_ALLOW; |
||
| 827 | } |
||
| 828 | } |
||
| 829 | } |
||
| 830 | |||
| 831 | // Execute closure functions. |
||
| 832 | foreach ($this->closures as $closure) { |
||
| 833 | $closure(); |
||
| 834 | } |
||
| 835 | |||
| 836 | $result = $this->process(); |
||
| 837 | |||
| 838 | if ($result !== self::RESPONSE_ALLOW) { |
||
| 839 | |||
| 840 | // Current session did not pass the CAPTCHA, it is still stuck in CAPTCHA page. |
||
| 841 | $actionCode = self::LOG_CAPTCHA; |
||
| 842 | |||
| 843 | // If current session's respone code is RESPONSE_DENY, record it as `blacklist_count` in our logs. |
||
| 844 | // It is stuck in warning page, not CAPTCHA. |
||
| 845 | if ($result === self::RESPONSE_DENY) { |
||
| 846 | $actionCode = self::LOG_BLACKLIST; |
||
| 847 | } |
||
| 848 | |||
| 849 | if ($result === self::RESPONSE_LIMIT_SESSION) { |
||
| 850 | $actionCode = self::LOG_LIMIT; |
||
| 851 | } |
||
| 852 | |||
| 853 | $this->log($actionCode); |
||
| 854 | |||
| 855 | } else { |
||
| 856 | |||
| 857 | $this->log(self::LOG_PAGEVIEW); |
||
| 858 | } |
||
| 859 | |||
| 860 | |||
| 861 | if (!empty($this->msgBody)) { |
||
| 862 | |||
| 863 | // @codeCoverageIgnoreStart |
||
| 864 | |||
| 865 | try { |
||
| 866 | foreach ($this->messenger as $messenger) { |
||
| 867 | $messenger->setTimeout(2); |
||
| 868 | $messenger->send($this->msgBody); |
||
| 869 | } |
||
| 870 | } catch (RuntimeException $e) { |
||
| 871 | // Do not throw error, becasue the third-party services might be unavailable. |
||
| 872 | } |
||
| 873 | |||
| 874 | // @codeCoverageIgnoreEnd |
||
| 875 | } |
||
| 876 | |||
| 877 | |||
| 878 | return $result; |
||
| 879 | } |
||
| 880 | |||
| 881 | |||
| 882 | |||
| 883 | |||
| 884 | |||
| 885 | /** |
||
| 886 | * Set the URLs you want them to be excluded them from protection. |
||
| 887 | * |
||
| 888 | * @param array $urls The list of URL want to be excluded. |
||
| 889 | * |
||
| 890 | * @return void |
||
| 891 | */ |
||
| 892 | public function setExcludedUrls(array $urls = []): void |
||
| 893 | { |
||
| 894 | $this->excludedUrls = $urls; |
||
| 895 | } |
||
| 896 | |||
| 897 | /** |
||
| 898 | * Set a closure function. |
||
| 899 | * |
||
| 900 | * @param string $key The name for the closure class. |
||
| 901 | * @param Closure $closure An instance will be later called. |
||
| 902 | * |
||
| 903 | * @return void |
||
| 904 | */ |
||
| 905 | public function setClosure(string $key, Closure $closure): void |
||
| 906 | { |
||
| 907 | $this->closures[$key] = $closure; |
||
| 908 | } |
||
| 909 | |||
| 910 | /** |
||
| 911 | * Print a JavasSript snippet in your webpages. |
||
| 912 | * |
||
| 913 | * This snippet generate cookie on client's browser,then we check the |
||
| 914 | * cookie to identify the client is a rebot or not. |
||
| 915 | * |
||
| 916 | * @return string |
||
| 917 | */ |
||
| 918 | public function outputJsSnippet(): string |
||
| 938 | } |
||
| 939 | |||
| 940 | /** |
||
| 941 | * Get current visior's path. |
||
| 942 | * |
||
| 943 | * @return string |
||
| 944 | */ |
||
| 945 | public function getCurrentUrl(): string |
||
| 946 | { |
||
| 947 | return get_request()->getUri()->getPath(); |
||
| 948 | } |
||
| 949 | |||
| 950 | /** |
||
| 951 | * Displayed on Firewall Panel, tell you current what type of current |
||
| 952 | * configuration is used for. |
||
| 953 | * |
||
| 954 | * @param string $type The type of configuration. |
||
| 955 | * demo | managed | config |
||
| 956 | * |
||
| 957 | * @return void |
||
| 958 | */ |
||
| 959 | public function managedBy(string $type = ''): void |
||
| 963 | } |
||
| 964 | } |
||
| 965 | } |
||
| 966 |