|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Shieldon package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Terry L. <[email protected]> |
|
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 <[email protected]> |
|
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\Captcha\CaptchaInterface; |
|
26
|
|
|
|
|
27
|
|
|
/* |
|
28
|
|
|
* Captcha Trait is loaded in Kernel instance only. |
|
29
|
|
|
*/ |
|
30
|
|
|
trait CaptchaTrait |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* Container for captcha addons. |
|
34
|
|
|
* The collection of \Shieldon\Firewall\Captcha\CaptchaInterface |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
public $captcha = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Set a captcha. |
|
42
|
|
|
* |
|
43
|
|
|
* @param CaptchaInterface $instance The captcha instance. |
|
44
|
|
|
* |
|
45
|
|
|
* @return void |
|
46
|
|
|
*/ |
|
47
|
|
|
public function setCaptcha(CaptchaInterface $instance): void |
|
48
|
|
|
{ |
|
49
|
|
|
$class = $this->getClassName($instance); |
|
|
|
|
|
|
50
|
|
|
$this->captcha[$class] = $instance; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Return the result from Captchas. |
|
55
|
|
|
* |
|
56
|
|
|
* @return bool |
|
57
|
|
|
*/ |
|
58
|
|
|
public function captchaResponse(): bool |
|
59
|
|
|
{ |
|
60
|
|
|
foreach ($this->captcha as $captcha) { |
|
61
|
|
|
|
|
62
|
|
|
if (!$captcha->response()) { |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* $sessionLimit @ SessionTrait |
|
69
|
|
|
* sessionHandler() @ SessionTrait |
|
70
|
|
|
*/ |
|
71
|
|
|
if (!empty($this->sessionLimit['count'])) { |
|
72
|
|
|
$this->result = $this->sessionHandler(self::RESPONSE_ALLOW); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Mostly be used in unit testing purpose. |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
public function disableCaptcha(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$this->captcha = []; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|