blue-twilight/app/Helpers/RecaptchaHelper.php

36 lines
1.2 KiB
PHP

<?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))
{
throw new \Exception('Error while communicating with the reCAPTCHA service.');
}
return $return->success;
}
}