/
IsTrueValidator.php
executable file
·151 lines (134 loc) · 4.26 KB
/
IsTrueValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
namespace EWZ\Bundle\RecaptchaBundle\Validator\Constraints;
use EWZ\Bundle\RecaptchaBundle\Extension\ReCaptcha\RequestMethod\Post;
use EWZ\Bundle\RecaptchaBundle\Extension\ReCaptcha\RequestMethod\ProxyPost;
use ReCaptcha\ReCaptcha;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class IsTrueValidator extends ConstraintValidator
{
/**
* Enable recaptcha?
*
* @var bool
*/
protected $enabled;
/**
* Recaptcha Private Key.
*
* @var string
*/
protected $privateKey;
/**
* Request Stack.
*
* @var RequestStack
*/
protected $requestStack;
/**
* HTTP Proxy informations.
*
* @var array
*/
protected $httpProxy;
/**
* Enable serverside host check.
*
* @var bool
*/
protected $verifyHost;
/**
* Authorization Checker
*
* @var AuthorizationChecker
*/
protected $authorizationChecker;
/**
* Trusted Roles
*
* @var array
*/
protected $trustedRoles;
/**
* The reCAPTCHA verify server URL.
*
* @var string
*/
protected $recaptchaVerifyServer;
/**
* The timeout for the reCAPTCHA verification.
*
* @var int|null
*/
private $timeout;
/**
* @param bool $enabled
* @param string $privateKey
* @param RequestStack $requestStack
* @param array $httpProxy
* @param bool $verifyHost
* @param AuthorizationCheckerInterface|null $authorizationChecker
* @param array $trustedRoles
* @param string $apiHost
* @param int|null $timeout
*/
public function __construct(
$enabled,
$privateKey,
RequestStack $requestStack,
array $httpProxy,
$verifyHost,
AuthorizationCheckerInterface $authorizationChecker = null,
array $trustedRoles = array(),
$apiHost = 'www.google.com',
$timeout = null)
{
$this->enabled = $enabled;
$this->privateKey = $privateKey;
$this->requestStack = $requestStack;
$this->httpProxy = $httpProxy;
$this->verifyHost = $verifyHost;
$this->authorizationChecker = $authorizationChecker;
$this->trustedRoles = $trustedRoles;
$this->recaptchaVerifyServer = 'https://'.$apiHost;
$this->timeout = $timeout;
}
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
// if recaptcha is disabled, always valid
if (!$this->enabled) {
return;
}
// if we have an authorized role
if ($this->authorizationChecker
&& count($this->trustedRoles) > 0
&& $this->authorizationChecker->isGranted($this->trustedRoles)) {
return;
}
// define variable for recaptcha check answer
$masterRequest = $this->requestStack->getMasterRequest();
$remoteip = $masterRequest->getClientIp();
$answer = $masterRequest->get('g-recaptcha-response');
// Verify user response with Google
if (null !== $this->httpProxy['host'] && null !== $this->httpProxy['port']) {
$requestMethod = new ProxyPost($this->httpProxy, $this->recaptchaVerifyServer, $this->timeout);
} else {
$requestMethod = new Post($this->recaptchaVerifyServer, $this->timeout);
}
$recaptcha = new ReCaptcha($this->privateKey, $requestMethod);
$response = $recaptcha->verify($answer, $remoteip);
if (!$response->isSuccess()) {
$this->context->addViolation($constraint->message);
}
// Perform server side hostname check
elseif ($this->verifyHost && $response->getHostname() !== $masterRequest->getHost()) {
$this->context->addViolation($constraint->invalidHostMessage);
}
}
}