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