Skip to content
This repository was archived by the owner on Feb 25, 2026. It is now read-only.

Commit 247e998

Browse files
author
Manuele Vaccari
committed
Changed to use google/recaptcha library
1 parent 6b3ea4f commit 247e998

1 file changed

Lines changed: 8 additions & 79 deletions

File tree

src/Validator/Constraints/IsTrueValidator.php

Lines changed: 8 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
namespace EWZ\Bundle\RecaptchaBundle\Validator\Constraints;
44

5+
use ReCaptcha\ReCaptcha;
56
use Symfony\Component\HttpFoundation\RequestStack;
7+
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
68
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
79
use Symfony\Component\Validator\Constraint;
810
use Symfony\Component\Validator\ConstraintValidator;
9-
use Symfony\Component\Validator\Exception\ValidatorException;
1011

1112
class IsTrueValidator extends ConstraintValidator
1213
{
@@ -55,13 +56,13 @@ class IsTrueValidator extends ConstraintValidator
5556
/**
5657
* Trusted Roles
5758
*
58-
* @var Array
59+
* @var array
5960
*/
6061
protected $trusted_roles;
6162

6263
/**
6364
* The reCAPTCHA verify server URL.
64-
*
65+
*
6566
* @var string
6667
*/
6768
protected $recaptchaVerifyServer;
@@ -74,6 +75,7 @@ class IsTrueValidator extends ConstraintValidator
7475
* @param bool $verifyHost
7576
* @param AuthorizationCheckerInterface|null $authorizationChecker
7677
* @param array $trusted_roles
78+
* @param string $apiHost
7779
*/
7880
public function __construct(
7981
$enabled,
@@ -116,88 +118,15 @@ public function validate($value, Constraint $constraint)
116118
$answer = $masterRequest->get('g-recaptcha-response');
117119

118120
// Verify user response with Google
119-
$response = $this->checkAnswer($this->privateKey, $remoteip, $answer);
121+
$recaptcha = new ReCaptcha($this->privateKey);
122+
$response = $recaptcha->verify($answer, $remoteip);
120123

121-
if ($response['success'] !== true) {
124+
if (!$response->isSuccess()) {
122125
$this->context->addViolation($constraint->message);
123126
}
124127
// Perform server side hostname check
125128
elseif ($this->verifyHost && $response['hostname'] !== $masterRequest->getHost()) {
126129
$this->context->addViolation($constraint->invalidHostMessage);
127130
}
128131
}
129-
130-
/**
131-
* Calls an HTTP POST function to verify if the user's guess was correct.
132-
*
133-
* @param string $privateKey
134-
* @param string $remoteip
135-
* @param string $answer
136-
*
137-
* @throws ValidatorException When missing remote ip
138-
*
139-
* @return bool
140-
*/
141-
private function checkAnswer($privateKey, $remoteip, $answer)
142-
{
143-
if ($remoteip == null || $remoteip == '') {
144-
throw new ValidatorException('For security reasons, you must pass the remote ip to reCAPTCHA');
145-
}
146-
147-
// discard spam submissions
148-
if ($answer == null || strlen($answer) == 0) {
149-
return false;
150-
}
151-
152-
$response = $this->httpGet($this->recaptchaVerifyServer, '/recaptcha/api/siteverify', array(
153-
'secret' => $privateKey,
154-
'remoteip' => $remoteip,
155-
'response' => $answer,
156-
));
157-
158-
return json_decode($response, true);
159-
}
160-
161-
/**
162-
* Submits an HTTP POST to a reCAPTCHA server.
163-
*
164-
* @param string $host
165-
* @param string $path
166-
* @param array $data
167-
*
168-
* @return array response
169-
*/
170-
private function httpGet($host, $path, $data)
171-
{
172-
$host = sprintf('%s%s?%s', $host, $path, http_build_query($data));
173-
174-
$context = $this->getResourceContext();
175-
176-
return file_get_contents($host, false, $context);
177-
}
178-
179-
/**
180-
* @return null|resource
181-
*/
182-
private function getResourceContext()
183-
{
184-
if (null === $this->httpProxy['host'] || null === $this->httpProxy['port']) {
185-
return null;
186-
}
187-
188-
$options = array();
189-
foreach (array('http', 'https') as $protocol) {
190-
$options[$protocol] = array(
191-
'method' => 'GET',
192-
'proxy' => sprintf('tcp://%s:%s', $this->httpProxy['host'], $this->httpProxy['port']),
193-
'request_fulluri' => true,
194-
);
195-
196-
if (null !== $this->httpProxy['auth']) {
197-
$options[$protocol]['header'] = sprintf('Proxy-Authorization: Basic %s', base64_encode($this->httpProxy['auth']));
198-
}
199-
}
200-
201-
return stream_context_create($options);
202-
}
203132
}

0 commit comments

Comments
 (0)