Navigation | Overlay |
---|---|
t Navigate files | h Toggle hits |
y Change url to tip of branch | m Toggle misses |
b / v Jump to prev/next hit line | p Toggle partial |
z / x Jump to prev/next missed or partial line | 1..9 Toggle flags |
shift + o Open current page in GitHub | a Toggle all on |
/ or ? Show keyboard shortcuts dialog | c Toggle context lines or commits |
1 |
<?php
|
|
2 |
/**
|
|
3 |
* This file is part of the Shieldon package.
|
|
4 |
*
|
|
5 |
* (c) Terry L. <contact@terryl.in>
|
|
6 |
*
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
8 |
* file that was distributed with this source code.
|
|
9 |
*
|
|
10 |
* php version 7.1.0
|
|
11 |
*
|
|
12 |
* @category Web-security
|
|
13 |
* @package Shieldon
|
|
14 |
* @author Terry Lin <contact@terryl.in>
|
|
15 |
* @copyright 2019 terrylinooo
|
|
16 |
* @license https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
|
|
17 |
* @link https://github.com/terrylinooo/shieldon
|
|
18 |
* @see https://shieldon.io
|
|
19 |
*/
|
|
20 |
|
|
21 |
declare(strict_types=1); |
|
22 |
|
|
23 |
namespace Shieldon\Firewall\Kernel; |
|
24 |
|
|
25 |
use Shieldon\Firewall\Kernel; |
|
26 |
use Shieldon\Firewall\Captcha\CaptchaInterface; |
|
27 |
|
|
28 |
/*
|
|
29 |
* Captcha Trait is loaded in Kernel instance only.
|
|
30 |
*/
|
|
31 |
trait CaptchaTrait |
|
32 |
{
|
|
33 |
/**
|
|
34 |
* Public methods | Desctiotion
|
|
35 |
* ----------------------|---------------------------------------------
|
|
36 |
* setCaptcha | Set a captcha.
|
|
37 |
* captchaResponse | Return the result from Captchas.
|
|
38 |
* disableCaptcha | Mostly be used in unit testing purpose.
|
|
39 |
* ----------------------|---------------------------------------------
|
|
40 |
*/
|
|
41 |
|
|
42 |
/**
|
|
43 |
* Container for captcha addons.
|
|
44 |
* The collection of \Shieldon\Firewall\Captcha\CaptchaInterface
|
|
45 |
*
|
|
46 |
* @var array
|
|
47 |
*/
|
|
48 |
public $captcha = []; |
|
49 |
|
|
50 |
/**
|
|
51 |
* Get a class name without namespace string.
|
|
52 |
*
|
|
53 |
* @param object $instance Class
|
|
54 |
*
|
|
55 |
* @return string
|
|
56 |
*/
|
|
57 |
abstract protected function getClassName($instance): string; |
|
58 |
|
|
59 |
/**
|
|
60 |
* Deal with online sessions.
|
|
61 |
*
|
|
62 |
* @param int $statusCode The response code.
|
|
63 |
*
|
|
64 |
* @return int The response code.
|
|
65 |
*/
|
|
66 |
abstract protected function sessionHandler($statusCode): int; |
|
67 |
|
|
68 |
/**
|
|
69 |
* Save and return the result identifier.
|
|
70 |
* This method is for passing value from traits.
|
|
71 |
*
|
|
72 |
* @param int $resultCode The result identifier.
|
|
73 |
*
|
|
74 |
* @return int
|
|
75 |
*/
|
|
76 |
abstract protected function setResultCode(int $resultCode): int; |
|
77 |
|
|
78 |
/**
|
|
79 |
* Set a captcha.
|
|
80 |
*
|
|
81 |
* @param CaptchaInterface $instance The captcha instance.
|
|
82 |
*
|
|
83 |
* @return void
|
|
84 |
*/
|
|
85 | 3 |
public function setCaptcha(CaptchaInterface $instance): void |
86 |
{
|
|
87 | 3 |
$class = $this->getClassName($instance); |
88 | 3 |
$this->captcha[$class] = $instance; |
89 |
}
|
|
90 |
|
|
91 |
/**
|
|
92 |
* Return the result from Captchas.
|
|
93 |
*
|
|
94 |
* @return bool
|
|
95 |
*/
|
|
96 | 3 |
public function captchaResponse(): bool |
97 |
{
|
|
98 | 3 |
foreach ($this->captcha as $captcha) { |
99 |
|
|
100 | 3 |
if (!$captcha->response()) { |
101 | 3 |
return false; |
102 |
}
|
|
103 |
}
|
|
104 |
|
|
105 |
/**
|
|
106 |
* $sessionLimit @ SessionTrait
|
|
107 |
* sessionHandler() @ SessionTrait
|
|
108 |
*/
|
|
109 | 3 |
if (!empty($this->sessionLimit['count'])) { |
110 | 3 |
return (bool) $this->setResultCode( |
111 | 3 |
$this->sessionHandler(Kernel::RESPONSE_ALLOW) |
112 |
);
|
|
113 |
}
|
|
114 |
|
|
115 | 3 |
return true; |
116 |
}
|
|
117 |
|
|
118 |
/**
|
|
119 |
* Mostly be used in unit testing purpose.
|
|
120 |
*
|
|
121 |
* @return void
|
|
122 |
*/
|
|
123 | 3 |
public function disableCaptcha(): void |
124 |
{
|
|
125 | 3 |
$this->captcha = []; |
126 |
}
|
|
127 |
}
|
Read our documentation on viewing source code .