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

Commit 7a2caf1

Browse files
author
Manuele Vaccari
committed
Added support for custom recaptcha verification server
1 parent 22d213b commit 7a2caf1

3 files changed

Lines changed: 75 additions & 8 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace EWZ\Bundle\RecaptchaBundle\ReCaptcha\RequestMethod;
4+
5+
use ReCaptcha\RequestMethod;
6+
use ReCaptcha\RequestParameters;
7+
8+
/**
9+
* Sends POST requests to the reCAPTCHA service.
10+
*/
11+
class Post implements RequestMethod
12+
{
13+
/**
14+
* The reCAPTCHA verify server URL.
15+
*
16+
* @var string
17+
*/
18+
private $recaptchaVerifyUrl;
19+
20+
/**
21+
* Constructor
22+
*
23+
* @param string $recaptchaVerifyServer
24+
*/
25+
public function __construct($recaptchaVerifyServer)
26+
{
27+
$this->recaptchaVerifyUrl = ($recaptchaVerifyServer ?: 'https://www.google.com').'/recaptcha/api/siteverify';
28+
}
29+
30+
/**
31+
* Submit the POST request with the specified parameters.
32+
*
33+
* @param RequestParameters $params Request parameters
34+
* @return string Body of the reCAPTCHA response
35+
*/
36+
public function submit(RequestParameters $params)
37+
{
38+
/**
39+
* PHP 5.6.0 changed the way you specify the peer name for SSL context options.
40+
* Using "CN_name" will still work, but it will raise deprecated errors.
41+
*/
42+
$peer_key = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name';
43+
$options = array(
44+
'http' => array(
45+
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
46+
'method' => 'POST',
47+
'content' => $params->toQueryString(),
48+
// Force the peer to validate (not needed in 5.6.0+, but still works)
49+
'verify_peer' => true,
50+
// Force the peer validation to use www.google.com
51+
$peer_key => 'www.google.com',
52+
),
53+
);
54+
$context = stream_context_create($options);
55+
return file_get_contents($this->recaptchaVerifyUrl, false, $context);
56+
}
57+
}

src/Extension/ReCaptcha/RequestMethod/ProxyPost.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,29 @@
1111
class ProxyPost implements RequestMethod
1212
{
1313
/**
14-
* URL to which requests are POSTed.
15-
* @const string
14+
* HTTP Proxy informations.
15+
*
16+
* @var array
1617
*/
17-
const SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
18-
1918
private $httpProxy;
2019

20+
/**
21+
* The reCAPTCHA verify server URL.
22+
*
23+
* @var string
24+
*/
25+
private $recaptchaVerifyUrl;
26+
2127
/**
2228
* Constructor
2329
*
2430
* @param array $httpProxy proxy data to connect to
31+
* @param string $recaptchaVerifyServer
2532
*/
26-
public function __construct(array $httpProxy)
33+
public function __construct(array $httpProxy, $recaptchaVerifyServer)
2734
{
2835
$this->httpProxy = $httpProxy;
36+
$this->recaptchaVerifyUrl = ($recaptchaVerifyServer ?: 'https://www.google.com').'/recaptcha/api/siteverify';
2937
}
3038

3139
/**
@@ -57,6 +65,6 @@ public function submit(RequestParameters $params)
5765
),
5866
);
5967
$context = stream_context_create($options);
60-
return file_get_contents(self::SITE_VERIFY_URL, false, $context);
68+
return file_get_contents($this->recaptchaVerifyUrl, false, $context);
6169
}
6270
}

src/Validator/Constraints/IsTrueValidator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

5+
use EWZ\Bundle\RecaptchaBundle\ReCaptcha\RequestMethod\Post;
56
use EWZ\Bundle\RecaptchaBundle\ReCaptcha\RequestMethod\ProxyPost;
67
use ReCaptcha\ReCaptcha;
78
use Symfony\Component\HttpFoundation\RequestStack;
@@ -119,9 +120,10 @@ public function validate($value, Constraint $constraint)
119120
$answer = $masterRequest->get('g-recaptcha-response');
120121

121122
// Verify user response with Google
122-
$requestMethod = null;
123123
if (null !== $this->httpProxy['host'] && null !== $this->httpProxy['port']) {
124-
$requestMethod = new ProxyPost($this->httpProxy);
124+
$requestMethod = new ProxyPost($this->httpProxy, $this->recaptchaVerifyServer);
125+
} else {
126+
$requestMethod = new Post($this->recaptchaVerifyServer);
125127
}
126128
$recaptcha = new ReCaptcha($this->privateKey, $requestMethod);
127129
$response = $recaptcha->verify($answer, $remoteip);

0 commit comments

Comments
 (0)