-
Notifications
You must be signed in to change notification settings - Fork 0
/
GreCaptchaValidator.php
37 lines (31 loc) · 1.05 KB
/
GreCaptchaValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
/**
* @author Peter Lai
* @email alk03073135@gmail.com
* @licence MIT
* @more details https://www.google.com/recaptcha/intro/index.html
**/
// require recaptcha autoload.php
require_once __DIR__.'/src/autoload.php';
class GreCaptchaValidator extends CValidator
{
// private key
protected $privateKey = '';
// show recaptcha error detail
protected $errorDetail = false;
protected function validateAttribute($object, $attribute)
{
$recaptcha = new \ReCaptcha\ReCaptcha($this->privateKey);
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$resp->isSuccess()) {
if (!$this->errorDetail) {
$message = $this->message !== null ? $this->message : Yii::t('yii', 'The verification code is incorrect.');
} else {
foreach ($resp->getErrorCodes() as $error) {
$message = $this->message.$error.' ';
}
}
$this->addError($object, $attribute, $message);
}
}
}