| 1 |
<?php |
| 2 |
class AccuaForm_Validation_Captcha2 extends Validation { |
| 3 |
protected $message = "Error: The reCATPCHA response provided was incorrect. Please re-try."; |
| 4 |
protected $privateKey; |
| 5 |
|
| 6 |
public function __construct($privateKey, $message = "") { |
| 7 |
$this->privateKey = $privateKey; |
| 8 |
if(!empty($message)) |
| 9 |
$this->message = $message; |
| 10 |
} |
| 11 |
|
| 12 |
public function isValid($value) { |
| 13 |
if (!isset($_POST['g-recaptcha-response'])) { |
| 14 |
return false; |
| 15 |
} |
| 16 |
|
| 17 |
$response = stripslashes_deep($_POST['g-recaptcha-response']); |
| 18 |
|
| 19 |
if ($response === '') { |
| 20 |
return false; |
| 21 |
} |
| 22 |
|
| 23 |
$url = 'https://www.google.com/recaptcha/api/siteverify' |
| 24 |
.'?secret='.urlencode($this->privateKey) |
| 25 |
.'&response='.urlencode($response) |
| 26 |
.'&remoteip='.urlencode($_SERVER["REMOTE_ADDR"]); |
| 27 |
|
| 28 |
$ch = curl_init(); |
| 29 |
curl_setopt($ch, CURLOPT_URL, $url); |
| 30 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 31 |
curl_setopt($ch, CURLOPT_TIMEOUT, 20); |
| 32 |
$res = curl_exec($ch); |
| 33 |
curl_close($ch); |
| 34 |
|
| 35 |
if ($res !== false) { |
| 36 |
@ $res = json_decode($res, true); |
| 37 |
if (!empty($res['success'])) { |
| 38 |
return true; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
return false; |
| 43 |
} |
| 44 |
} |
| 45 |
|