2016-09-08 11:02:49 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Helpers;
|
|
|
|
|
|
|
|
use App\Facade\UserConfig;
|
|
|
|
|
|
|
|
class RecaptchaHelper
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Validates the given reCAPTCHA response against the reCAPTCHA servers to see if the user was validated successfully.
|
|
|
|
* @param string $response Response from the g-recaptcha-response POST variable
|
|
|
|
* @param string $clientIpAddress Client's IP address
|
|
|
|
* @return mixed Returns true or false depending on the reCAPTCHA server's response
|
|
|
|
* @throws \Exception Thrown when the reCAPTCHA server could not be reached, or it didn't return a valid JSON response.
|
|
|
|
*/
|
|
|
|
public static function validateResponse($response, $clientIpAddress)
|
|
|
|
{
|
|
|
|
$ch = curl_init(config('services.recaptcha.verify_url'));
|
|
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
|
|
|
|
'secret' => UserConfig::get('recaptcha_secret_key'),
|
|
|
|
'response' => $response,
|
|
|
|
'remoteip' => $clientIpAddress
|
|
|
|
));
|
|
|
|
|
|
|
|
$return = json_decode(curl_exec($ch));
|
|
|
|
|
|
|
|
if (is_null($return))
|
|
|
|
{
|
2017-03-23 04:29:29 +00:00
|
|
|
$message = 'Error while communicating with the reCAPTCHA service.';
|
|
|
|
$curlError = curl_error($ch);
|
|
|
|
|
|
|
|
if (strlen($curlError) > 0)
|
|
|
|
{
|
|
|
|
$message .= ' ' . $curlError;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \Exception($message);
|
2016-09-08 11:02:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $return->success;
|
|
|
|
}
|
|
|
|
}
|