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

Commit 8f4f3c1

Browse files
author
Manuele Vaccari
committed
Added proxy functionality based on previous code
1 parent 34a23c2 commit 8f4f3c1

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 though a proxy.
10+
*/
11+
class ProxyPost implements RequestMethod
12+
{
13+
/**
14+
* URL to which requests are POSTed.
15+
* @const string
16+
*/
17+
const SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
18+
19+
private $httpProxy;
20+
21+
/**
22+
* Constructor
23+
*
24+
* @param array $httpProxy proxy data to connect to
25+
*/
26+
public function __construct(array $httpProxy)
27+
{
28+
$this->httpProxy = $httpProxy;
29+
}
30+
31+
/**
32+
* Submit the POST request with the specified parameters.
33+
*
34+
* @param RequestParameters $params Request parameters
35+
* @return string Body of the reCAPTCHA response
36+
*/
37+
public function submit(RequestParameters $params)
38+
{
39+
/**
40+
* PHP 5.6.0 changed the way you specify the peer name for SSL context options.
41+
* Using "CN_name" will still work, but it will raise deprecated errors.
42+
*/
43+
$peer_key = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name';
44+
$options = array(
45+
'http' => array(
46+
'header' => "Content-type: application/x-www-form-urlencoded\r\n".sprintf('Proxy-Authorization: Basic %s', base64_encode($this->httpProxy['auth'])),
47+
'method' => 'POST',
48+
'content' => $params->toQueryString(),
49+
// Force the peer to validate (not needed in 5.6.0+, but still works)
50+
'verify_peer' => true,
51+
// Force the peer validation to use www.google.com
52+
$peer_key => 'www.google.com',
53+
54+
'proxy' => sprintf('tcp://%s:%s', $this->httpProxy['host'], $this->httpProxy['port']),
55+
// While this is a non-standard request format, some proxy servers require it.
56+
'request_fulluri' => true,
57+
),
58+
);
59+
$context = stream_context_create($options);
60+
return file_get_contents(self::SITE_VERIFY_URL, false, $context);
61+
}
62+
}

src/Validator/Constraints/IsTrueValidator.php

Lines changed: 6 additions & 1 deletion
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\ProxyPost;
56
use ReCaptcha\ReCaptcha;
67
use Symfony\Component\HttpFoundation\RequestStack;
78
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
@@ -118,7 +119,11 @@ public function validate($value, Constraint $constraint)
118119
$answer = $masterRequest->get('g-recaptcha-response');
119120

120121
// Verify user response with Google
121-
$recaptcha = new ReCaptcha($this->privateKey);
122+
$requestMethod = null;
123+
if (null !== $this->httpProxy['host'] && null !== $this->httpProxy['port']) {
124+
$requestMethod = new ProxyPost($this->httpProxy);
125+
}
126+
$recaptcha = new ReCaptcha($this->privateKey, $requestMethod);
122127
$response = $recaptcha->verify($answer, $remoteip);
123128

124129
if (!$response->isSuccess()) {

0 commit comments

Comments
 (0)