|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Shopware\Storefront\Framework\Captcha; |
|
4
|
|
|
|
|
5
|
|
|
use Shopware\Core\PlatformRequest; |
|
6
|
|
|
use Shopware\Core\System\SalesChannel\SalesChannelContext; |
|
7
|
|
|
use Shopware\Core\System\SystemConfig\SystemConfigService; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
10
|
|
|
use Symfony\Component\Validator\ConstraintViolation; |
|
11
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @package storefront |
|
15
|
|
|
*/ |
|
16
|
|
|
class BasicCaptcha extends AbstractCaptcha |
|
17
|
|
|
{ |
|
18
|
|
|
public const CAPTCHA_NAME = 'basicCaptcha'; |
|
19
|
|
|
public const CAPTCHA_REQUEST_PARAMETER = 'shopware_basic_captcha_confirm'; |
|
20
|
|
|
public const BASIC_CAPTCHA_SESSION = 'basic_captcha_session'; |
|
21
|
|
|
public const INVALID_CAPTCHA_CODE = 'captcha.basic-captcha-invalid'; |
|
22
|
|
|
|
|
23
|
|
|
private RequestStack $requestStack; |
|
24
|
|
|
|
|
25
|
|
|
private SystemConfigService $systemConfigService; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @internal |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(RequestStack $requestStack, SystemConfigService $systemConfigService) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->requestStack = $requestStack; |
|
33
|
|
|
$this->systemConfigService = $systemConfigService; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function supports(Request $request, array $captchaConfig): bool |
|
40
|
|
|
{ |
|
41
|
|
|
/** @var SalesChannelContext|null $context */ |
|
42
|
|
|
$context = $request->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT); |
|
43
|
|
|
$salesChannelId = $context ? $context->getSalesChannelId() : null; |
|
44
|
|
|
|
|
45
|
|
|
$activeCaptchas = $this->systemConfigService->get('core.basicInformation.activeCaptchasV2', $salesChannelId); |
|
46
|
|
|
|
|
47
|
|
|
if (empty($activeCaptchas) || !\is_array($activeCaptchas)) { |
|
48
|
|
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $request->isMethod(Request::METHOD_POST) |
|
52
|
|
|
&& \in_array(self::CAPTCHA_NAME, array_keys($activeCaptchas), true) |
|
53
|
|
|
&& $activeCaptchas[self::CAPTCHA_NAME]['isActive']; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function isValid(Request $request, array $captchaConfig): bool |
|
60
|
|
|
{ |
|
61
|
|
|
$basicCaptchaValue = $request->get(self::CAPTCHA_REQUEST_PARAMETER); |
|
62
|
|
|
|
|
63
|
|
|
if ($basicCaptchaValue === null) { |
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$session = $this->requestStack->getSession(); |
|
68
|
|
|
$captchaSession = $session->get($request->get('formId') . self::BASIC_CAPTCHA_SESSION); |
|
69
|
|
|
$session->remove($request->get('formId') . self::BASIC_CAPTCHA_SESSION); |
|
70
|
|
|
|
|
71
|
|
|
if ($captchaSession === null) { |
|
72
|
|
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return strtolower($basicCaptchaValue) === strtolower($captchaSession); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
|
|
public function shouldBreak(): bool |
|
82
|
|
|
{ |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritdoc} |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getName(): string |
|
90
|
|
|
{ |
|
91
|
|
|
return self::CAPTCHA_NAME; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getViolations(): ConstraintViolationList |
|
98
|
|
|
{ |
|
99
|
|
|
$violations = new ConstraintViolationList(); |
|
100
|
|
|
$violations->add(new ConstraintViolation( |
|
101
|
|
|
'', |
|
102
|
|
|
'', |
|
103
|
|
|
[], |
|
104
|
|
|
'', |
|
105
|
|
|
'/' . self::CAPTCHA_REQUEST_PARAMETER, |
|
106
|
|
|
'', |
|
107
|
|
|
null, |
|
108
|
|
|
self::INVALID_CAPTCHA_CODE |
|
109
|
|
|
)); |
|
110
|
|
|
|
|
111
|
|
|
return $violations; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|