forked from phalcon/incubator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReCaptcha.php
120 lines (109 loc) · 4.01 KB
/
ReCaptcha.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Patrick Florek <[email protected]> |
+------------------------------------------------------------------------+
*/
/**
* The reCAPTCHA Validator
*
* @link https://www.google.com/recaptcha/intro/index.html
* @link https://developers.google.com/recaptcha/
*
* @package Phalcon\Validation\Validator
*/
namespace Phalcon\Validation\Validator;
use Phalcon\Validation;
use Phalcon\Http\Request;
use Phalcon\Validation\Message;
use Phalcon\Validation\Validator;
/**
* Phalcon\Validation\Validator\ReCaptcha
*
* Verifies a value to a reCAPTCHA challenge
*
* <code>
* use Phalcon\Validation\Validator;
*
* $validator->add('g-recaptcha-response', new Validator([
* 'message' => 'The captcha is not valid',
* 'secret' => 'your_site_key'
* ]));
* </code>
*
* @link https://developers.google.com/recaptcha/intro
* @package Phalcon\Validation\Validator
*/
class ReCaptcha extends Validator
{
/**
* API request URL
*/
const RECAPTCHA_URL = 'https://www.google.com/recaptcha/api/siteverify';
/**
* Response error code reference
* @var array $messages
*/
protected $messages = [
'missing-input-secret' => 'The secret parameter is missing.',
'invalid-input-secret' => 'The secret parameter is invalid or malformed.',
'missing-input-response' => 'The response parameter is missing.',
'invalid-input-response' => 'The response parameter is invalid or malformed.',
];
/**
* {@inheritdoc}
*
* @param Validation $validation
* @param string $attribute
*
* @return bool
*/
public function validate(Validation $validation, $attribute)
{
$secret = $this->getOption('secret');
$value = $validation->getValue($attribute);
$request = $validation->getDI()->get('request');
$remoteIp = $request->getClientAddress(false);
if (!empty($value)) {
$curl = curl_init(self::RECAPTCHA_URL);
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => [
'secret' => $secret,
'response' => $value,
'remoteip' => $remoteIp
]
]);
$response = json_decode(curl_exec($curl), true);
curl_close($curl);
}
if (empty($response['success'])) {
$label = $this->getOption('label');
if (empty($label)) {
$label = $validation->getLabel($attribute);
}
$message = $this->getOption('message');
$replacePairs = [':field', $label];
if (empty($message) && !empty($response['error-codes'])) {
$message = $this->messages[$response['error-codes']];
}
if (empty($message)) {
$message = $validation->getDefaultMessage('ReCaptcha');
}
$validation->appendMessage(new Message(strtr($message, $replacePairs), $attribute, 'ReCaptcha'));
return false;
}
return true;
}
}