-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for custom recaptcha verification server
- Loading branch information
Manuele Vaccari
committed
May 18, 2018
1 parent
22d213b
commit 7a2caf1
Showing
3 changed files
with
75 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters