1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Core\Service; |
6
|
|
|
|
7
|
|
|
use eZ\Publish\API\Repository\ContentTypeService; |
8
|
|
|
use eZ\Publish\API\Repository\Values\Content\Location; |
9
|
|
|
use eZ\Publish\API\Repository\Values\ContentType\ContentType; |
10
|
|
|
use Netgen\InformationCollection\API\Service\CaptchaService as CaptchaServiceInterface; |
11
|
|
|
use Netgen\InformationCollection\API\Service\CaptchaValue; |
12
|
|
|
use Netgen\InformationCollection\API\Value\Captcha\ReCaptcha; |
13
|
|
|
use Netgen\InformationCollection\API\Value\Captcha\NullObject; |
14
|
|
|
|
15
|
|
|
class CaptchaService implements CaptchaServiceInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $config; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \eZ\Publish\API\Repository\ContentTypeService |
24
|
|
|
*/ |
25
|
|
|
protected $contentTypeService; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* CaptchaService constructor. |
29
|
|
|
* |
30
|
|
|
* @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService |
31
|
|
|
* @param array $config |
32
|
|
|
*/ |
33
|
|
|
public function __construct(ContentTypeService $contentTypeService, $config = []) |
34
|
|
|
{ |
35
|
|
|
$this->config = $config; |
36
|
|
|
$this->contentTypeService = $contentTypeService; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function isEnabled(Location $location): bool |
43
|
|
|
{ |
44
|
|
|
$config = $this->getConfig($location); |
45
|
|
|
|
46
|
|
|
return $config['enabled']; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function getSiteKey(Location $location): string |
53
|
|
|
{ |
54
|
|
|
$config = $this->getConfig($location); |
55
|
|
|
|
56
|
|
|
return $config['site_key']; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function getCaptcha(Location $location): CaptchaValue |
63
|
|
|
{ |
64
|
|
|
$config = $this->getConfig($location); |
65
|
|
|
|
66
|
|
|
if ($config['enabled']) { |
67
|
|
|
$reCaptcha = new \ReCaptcha\ReCaptcha($config['secret']); |
68
|
|
|
|
69
|
|
|
if (!empty($config['options'])) { |
70
|
|
|
if (!empty($config['options']['hostname'])) { |
71
|
|
|
$reCaptcha->setExpectedHostname($config['options']['hostname']); |
72
|
|
|
} |
73
|
|
|
if (!empty($config['options']['apk_package_name'])) { |
74
|
|
|
$reCaptcha->setExpectedApkPackageName($config['options']['apk_package_name']); |
75
|
|
|
} |
76
|
|
|
if (!empty($config['options']['action'])) { |
77
|
|
|
$reCaptcha->setExpectedAction($config['options']['action']); |
78
|
|
|
} |
79
|
|
|
if (!empty($config['options']['score_threshold'])) { |
80
|
|
|
$reCaptcha->setScoreThreshold($config['options']['score_threshold']); |
81
|
|
|
} |
82
|
|
|
if (!empty($config['options']['challenge_timeout'])) { |
83
|
|
|
$reCaptcha->setChallengeTimeout($config['options']['challenge_timeout']); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return new ReCaptcha($reCaptcha); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return new NullObject(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns filtered config for current Location. |
95
|
|
|
* |
96
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
97
|
|
|
* |
98
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
protected function getConfig(Location $location) |
103
|
|
|
{ |
104
|
|
|
$contentTypeConfig = $this->getConfigForContentType( |
105
|
|
|
$this->getContentType($location) |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
return (array)array_replace($this->config, $contentTypeConfig); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns filtered config for current ContentType. |
113
|
|
|
* |
114
|
|
|
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
protected function getConfigForContentType(ContentType $contentType) |
119
|
|
|
{ |
120
|
|
|
if ($this->hasConfigForContentType($contentType)) { |
121
|
|
|
return $this->config['override_by_type'][$contentType->identifier]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return []; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Checks if override exist for given ContentType. |
129
|
|
|
* |
130
|
|
|
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType |
131
|
|
|
* |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
protected function hasConfigForContentType(ContentType $contentType) |
135
|
|
|
{ |
136
|
|
|
if (!empty($this->config['override_by_type'])) { |
137
|
|
|
if (in_array($contentType->identifier, array_keys($this->config['override_by_type']), true)) { |
138
|
|
|
return true; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Helper method for retrieving ContentType from Location. |
147
|
|
|
* |
148
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
149
|
|
|
* |
150
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
151
|
|
|
* |
152
|
|
|
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentType |
153
|
|
|
*/ |
154
|
|
|
protected function getContentType(Location $location) |
155
|
|
|
{ |
156
|
|
|
return $this->contentTypeService |
157
|
|
|
->loadContentType($location->contentInfo->contentTypeId); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|