/
ReCaptchaValidator.php
65 lines (57 loc) · 1.88 KB
/
ReCaptchaValidator.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* @author Laurent Jouanneau
* @copyright 2017 Laurent Jouanneau
*
* @see http://www.jelix.org
* @licence http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
*/
namespace Jelix\Forms\Captcha;
class ReCaptchaValidator implements CaptchaValidatorInterface
{
/**
* called by the widget to initialize some data when the form is generated.
*
* It can returns some data that can be useful for the widget
*
* @return mixed
*/
public function initOnDisplay()
{
return null;
}
/**
* Validate the data coming from the submitted form.
*
* It should returns null if it is ok, or one of jForms::ERRDATA_* constant
*
* @param mixed $value
* @param mixed $internalData
*
* @return null|int
*/
public function validate($value, $internalData)
{
$config = \jApp::config()->recaptcha;
if (!isset($config['secret']) || $config['secret'] == '') {
\jLog::log('secret for recaptcha is missing from the configuration', 'warning');
return \jForms::ERRDATA_INVALID;
}
if (!isset($_POST['g-recaptcha-response'])) {
return \jForms::ERRDATA_REQUIRED;
}
$recaptcha = new \ReCaptcha\ReCaptcha($config['secret']);
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if ($resp->isSuccess()) {
return null;
}
foreach ($resp->getErrorCodes() as $code) {
if ($code == 'missing-input-secret') {
\jLog::log('secret for recaptcha is missing from the google request', 'warning');
} elseif ($code == 'invalid-input-secret') {
\jLog::log('secret for recaptcha is invalid', 'warning');
}
}
return \jForms::ERRDATA_INVALID;
}
}