1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Extension "sf_event_mgt" for TYPO3 CMS. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the |
9
|
|
|
* LICENSE.txt file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace DERHANSEN\SfEventMgt\Service; |
13
|
|
|
|
14
|
|
|
use DERHANSEN\SfEventMgt\Exception\InvalidCaptchaConfigurationException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* CaptchaConfiguration which supports hCaptcha and reCaptcha |
18
|
|
|
*/ |
19
|
|
|
class CaptchaConfigurationService |
20
|
|
|
{ |
21
|
|
|
private const RESPONSE_FIELD = [ |
22
|
|
|
'hCaptcha' => 'h-captcha-response', |
23
|
|
|
'reCaptcha' => 'g-recaptcha-response', |
24
|
|
|
]; |
25
|
|
|
private const PUBLIC_KEY_FIELD = [ |
26
|
|
|
'hCaptcha' => 'publicKey', |
27
|
|
|
'reCaptcha' => 'siteKey', |
28
|
|
|
]; |
29
|
|
|
private const PRIVATE_KEY_FIELD = [ |
30
|
|
|
'hCaptcha' => 'privateKey', |
31
|
|
|
'reCaptcha' => 'secretKey', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
protected bool $enabled = false; |
35
|
|
|
protected string $type = ''; |
36
|
|
|
protected string $apiScript = ''; |
37
|
|
|
protected string $verificationServer = ''; |
38
|
|
|
protected string $publicKey = ''; |
39
|
|
|
protected string $privateKey = ''; |
40
|
|
|
protected string $responseField = ''; |
41
|
|
|
|
42
|
|
|
public function __construct(array $captchaSettings = []) |
43
|
|
|
{ |
44
|
|
|
$this->enabled = (bool)($captchaSettings['enabled'] ?? false); |
45
|
|
|
$this->type = $captchaSettings['type'] ?? ''; |
46
|
|
|
|
47
|
|
|
if ($this->isValidCaptchaType($this->type)) { |
48
|
|
|
$this->apiScript = $captchaSettings[$this->type]['apiScript'] ?? ''; |
49
|
|
|
$this->verificationServer = $captchaSettings[$this->type]['verificationServer'] ?? ''; |
50
|
|
|
$this->publicKey = $captchaSettings[$this->type][self::PUBLIC_KEY_FIELD[$this->type]] ?? ''; |
51
|
|
|
$this->privateKey = $captchaSettings[$this->type][self::PRIVATE_KEY_FIELD[$this->type]] ?? ''; |
52
|
|
|
$this->responseField = self::RESPONSE_FIELD[$this->type] ?? ''; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->validateSettings(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getEnabled(): bool |
59
|
|
|
{ |
60
|
|
|
return $this->enabled; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getType(): string |
64
|
|
|
{ |
65
|
|
|
return $this->type; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getApiScript(): string |
69
|
|
|
{ |
70
|
|
|
return $this->apiScript; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getVerificationServer(): string |
74
|
|
|
{ |
75
|
|
|
return $this->verificationServer; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getPublicKey(): string |
79
|
|
|
{ |
80
|
|
|
return $this->publicKey; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getPrivateKey(): string |
84
|
|
|
{ |
85
|
|
|
return $this->privateKey; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getResponseField(): string |
89
|
|
|
{ |
90
|
|
|
return $this->responseField; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function isValidCaptchaType(string $type): bool |
94
|
|
|
{ |
95
|
|
|
return in_array($type, ['hCaptcha', 'reCaptcha']); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Checks, if all properties contain valid values |
100
|
|
|
*/ |
101
|
|
|
private function validateSettings(): void |
102
|
|
|
{ |
103
|
|
|
// If captcha is not enabled, there is no need to verify the settings |
104
|
|
|
if (!$this->enabled) { |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($this->type === '' || !$this->isValidCaptchaType($this->type)) { |
109
|
|
|
throw new InvalidCaptchaConfigurationException( |
110
|
|
|
'Invalid captcha type settings. Valid values are "hCaptcha" and "reCaptcha', |
111
|
|
|
1631962901 |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
if ($this->apiScript === '' || !filter_var($this->apiScript, FILTER_VALIDATE_URL)) { |
115
|
|
|
throw new InvalidCaptchaConfigurationException( |
116
|
|
|
'Invalid apiScript setting.', |
117
|
|
|
1631962907 |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
if ($this->verificationServer === '' || !filter_var($this->verificationServer, FILTER_VALIDATE_URL)) { |
121
|
|
|
throw new InvalidCaptchaConfigurationException( |
122
|
|
|
'Invalid verificationServer setting.', |
123
|
|
|
1631962990 |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
if ($this->publicKey === '') { |
127
|
|
|
throw new InvalidCaptchaConfigurationException( |
128
|
|
|
'Invalid publicKey/siteKey setting.', |
129
|
|
|
1631964323 |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
if ($this->privateKey === '') { |
133
|
|
|
throw new InvalidCaptchaConfigurationException( |
134
|
|
|
'Invalid privateKey/secretKey setting.', |
135
|
|
|
1631964328 |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|