forked from malganis93/BrlReCaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReCaptcha.php
194 lines (166 loc) · 3.9 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* Created by PhpStorm.
* User: lars
* Date: 18.03.15
* Time: 00:33.
*/
namespace Zf3Php7Recaptcha;
use Traversable;
use Zf3Php7Recaptcha\Service\ReCaptchaService;
use Zend\Captcha\AbstractAdapter;
use Zend\Validator\Exception;
/**
* Class ReCaptcha.
*/
class ReCaptcha extends AbstractAdapter
{
/**
* @var \Zf3Php7Recaptcha\Service\ReCaptchaService
*/
protected $service;
/**#@+
* Error codes
*/
const MISSING_VALUE = 'missingValue';
const BAD_CAPTCHA = 'badCaptcha';
/**#@-*/
/**
* Error messages.
*
* @var array
*/
protected $messageTemplates = array(
self::MISSING_VALUE => 'Missing captcha fields',
self::BAD_CAPTCHA => 'Captcha value is wrong. %value%',
);
/**
* @return \Zf3Php7Recaptcha\Service\ReCaptchaService
*/
public function getService()
{
return $this->service;
}
/**
* @param mixed $service
*/
public function setService($service)
{
$this->service = $service;
}
/**
* @param $key
*
* @return $this
*/
public function setPrivkey($key)
{
$this->getService()->setPrivateKey($key);
return $this;
}
/**
* @return string
*/
public function getPrivkey()
{
return $this->getService()->getPrivateKey();
}
/**
* @param $key
*
* @return $this
*/
public function setPubkey($key)
{
$this->getService()->setPublicKey($key);
return $this;
}
/**
* @return string
*/
public function getPubkey()
{
return $this->getService()->getPublicKey();
}
/**
* @param $theme
* @return $this
*/
public function setTheme($theme)
{
$this->getService()->setTheme($theme);
return $this;
}
/**
* @return string
*/
public function getTheme()
{
return $this->getService()->getTheme();
}
/**
* Constructor.
*
* @param null|array|Traversable $options
*/
public function __construct($options = null)
{
$this->setService(new ReCaptchaService());
parent::__construct($options);
if (!empty($options)) {
if (array_key_exists('private_key', $options)) {
$this->getService()->setPrivateKey($options['private_key']);
}
if (array_key_exists('public_key', $options)) {
$this->getService()->setPublicKey($options['public_key']);
}
if (array_key_exists('theme', $options)) {
$this->getService()->setTheme($options['theme']);
}
$this->setOptions($options);
}
}
/**
* Generate a new captcha.
*
* @return string new captcha ID
*/
public function generate()
{
//Method is in interface, but not required for recaptcha, since google handles the generation
return '';
}
/**
* Returns true if and only if $value meets the validation requirements.
*
* If $value fails validation, then this method returns false, and
* getMessages() will return an array of messages that explain why the
* validation failed.
*
* @param mixed $value
* @param mixed $context Param for ZF2.4
*
* @return bool
*
* @throws Exception\RuntimeException If validation of $value is impossible
*/
public function isValid($value, $context = null)
{
if (!$value) {
$this->error(self::MISSING_VALUE);
}
$result = $this->getService()->verify($value);
if (!$result) {
$this->error(self::BAD_CAPTCHA, $this->getService()->getErrorCode());
return false;
}
return true;
}
/**
* @return string
*/
public function getHelperName()
{
return 'zf3Php7ReCaptcha';
}
}