Skip to content

Commit

Permalink
Added support for custom recaptcha verification server
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuele Vaccari committed May 18, 2018
1 parent 22d213b commit 7a2caf1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 8 deletions.
57 changes: 57 additions & 0 deletions src/Extension/ReCaptcha/RequestMethod/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace EWZ\Bundle\RecaptchaBundle\ReCaptcha\RequestMethod;

use ReCaptcha\RequestMethod;
use ReCaptcha\RequestParameters;

/**
* Sends POST requests to the reCAPTCHA service.
*/
class Post implements RequestMethod
{
/**
* The reCAPTCHA verify server URL.
*
* @var string
*/
private $recaptchaVerifyUrl;

/**
* Constructor
*
* @param string $recaptchaVerifyServer
*/
public function __construct($recaptchaVerifyServer)
{
$this->recaptchaVerifyUrl = ($recaptchaVerifyServer ?: 'https://www.google.com').'/recaptcha/api/siteverify';
}

/**
* Submit the POST request with the specified parameters.
*
* @param RequestParameters $params Request parameters
* @return string Body of the reCAPTCHA response
*/
public function submit(RequestParameters $params)
{
/**
* PHP 5.6.0 changed the way you specify the peer name for SSL context options.
* Using "CN_name" will still work, but it will raise deprecated errors.
*/
$peer_key = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name';
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => $params->toQueryString(),
// Force the peer to validate (not needed in 5.6.0+, but still works)
'verify_peer' => true,
// Force the peer validation to use www.google.com
$peer_key => 'www.google.com',
),
);
$context = stream_context_create($options);
return file_get_contents($this->recaptchaVerifyUrl, false, $context);
}
}
20 changes: 14 additions & 6 deletions src/Extension/ReCaptcha/RequestMethod/ProxyPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,29 @@
class ProxyPost implements RequestMethod
{
/**
* URL to which requests are POSTed.
* @const string
* HTTP Proxy informations.
*
* @var array
*/
const SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';

private $httpProxy;

/**
* The reCAPTCHA verify server URL.
*
* @var string
*/
private $recaptchaVerifyUrl;

/**
* Constructor
*
* @param array $httpProxy proxy data to connect to
* @param string $recaptchaVerifyServer
*/
public function __construct(array $httpProxy)
public function __construct(array $httpProxy, $recaptchaVerifyServer)
{
$this->httpProxy = $httpProxy;
$this->recaptchaVerifyUrl = ($recaptchaVerifyServer ?: 'https://www.google.com').'/recaptcha/api/siteverify';
}

/**
Expand Down Expand Up @@ -57,6 +65,6 @@ public function submit(RequestParameters $params)
),
);
$context = stream_context_create($options);
return file_get_contents(self::SITE_VERIFY_URL, false, $context);
return file_get_contents($this->recaptchaVerifyUrl, false, $context);
}
}
6 changes: 4 additions & 2 deletions src/Validator/Constraints/IsTrueValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace EWZ\Bundle\RecaptchaBundle\Validator\Constraints;

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

// Verify user response with Google
$requestMethod = null;
if (null !== $this->httpProxy['host'] && null !== $this->httpProxy['port']) {
$requestMethod = new ProxyPost($this->httpProxy);
$requestMethod = new ProxyPost($this->httpProxy, $this->recaptchaVerifyServer);
} else {
$requestMethod = new Post($this->recaptchaVerifyServer);
}
$recaptcha = new ReCaptcha($this->privateKey, $requestMethod);
$response = $recaptcha->verify($answer, $remoteip);
Expand Down

0 comments on commit 7a2caf1

Please sign in to comment.