-
Notifications
You must be signed in to change notification settings - Fork 2
/
field.php
153 lines (135 loc) · 5 KB
/
field.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
<?php
require_once INCLUDE_DIR . 'class.dynamic_forms.php';
require_once INCLUDE_DIR . 'class.forms.php';
class reCaptchaField extends FormField {
static $widget = 'reCaptchaWidget';
static $plugin_config;
function getPluginConfig() {
return static::$plugin_config;
}
function validateEntry($value) {
static $validation = array();
parent::validateEntry($value);
// ValidateEntry may be called twice, which is a problem
$id = $this->get('id');
$config = $this->getPluginConfig()->getInfo();
if (!isset($validation[$id])) {
list($code, $json) = $this->http_post(
'https://www.google.com/recaptcha/api/siteverify',
array(
'secret' => $config['private'],
'response' => $value,
'remoteip' => $_SERVER['REMOTE_ADDR'],
));
if ($code !== 200) {
$response = array(
'error-codes' => array('no-response'),
);
}
else {
$response = JsonDataParser::decode($json);
}
$I = &$validation[$id];
if (!($I['valid'] = $response['success'])) {
$errors = array();
foreach ($response['error-codes'] as $code) {
switch ($code) {
case 'missing-input-response':
$errors[] = sprintf(__('%s is a required field'),
$this->getLabel() ?: __('This'));
break;
case 'invalid-input-response':
$errors[] = "Your response doesn't look right. Please try again";
break;
case 'no-response':
$errors[] = "Unable to communicate with the reCaptcha server";
}
}
$I['errors'] = $errors;
}
}
if (!$validation[$id]['valid'])
foreach ($validation[$id]['errors'] as $e)
$this->_errors[] = $e;
}
protected function http_post($url, array $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'osTicket/'.THIS_VERSION);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
if (isset($_SERVER['HTTPS_PROXY']))
curl_setopt($ch, CURLOPT_PROXY, $_SERVER['HTTPS_PROXY']);
$result=curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array($code, $result);
}
function getConfigurationOptions() {
return array(
'theme' => new ChoiceField(array(
'label' => 'reCaptcha Theme',
'choices' => array('dark' => 'Dark', 'light' => 'Light'),
'default' => 'light',
)),
'type' => new ChoiceField(array(
'label' => 'reCaptcha Type',
'choices' => array('image' => 'Image', 'audio' => 'Audio'),
'default' => 'image',
)),
'size' => new ChoiceField(array(
'label' => 'reCaptcha Type',
'choices' => array('compact' => 'Compact', 'normal' => 'Normal'),
'default' => 'normal',
)),
);
}
//doesn't work
/*
function getMedia() {
return array(
'js' => array(
'//www.google.com/recaptcha/api.js?hl='
. Internationalization::getCurrentLanguage()
),
);
}
*/
}
class reCaptchaWidget extends Widget {
function render() {
$fconfig = $this->field->getConfiguration();
$pconfig = $this->field->getPluginConfig()->getInfo();
?>
<script src="//www.google.com/recaptcha/api.js?hl="></script>
<div class="g-recaptcha"
data-sitekey="<?php echo $pconfig['public']; ?>"
data-theme="<?php echo $fconfig['theme'] ?: 'light'; ?>"
data-type="<?php echo $fconfig['type'] ?: 'image'; ?>"
data-size="<?php echo $fconfig['size'] ?: 'normal'; ?>"
></div>
<?php
}
function getValue() {
if (!($data = $this->field->getSource()))
return null;
if (!isset($data['g-recaptcha-response']))
return null;
return $data['g-recaptcha-response'];
}
}
require_once 'config.php';
class reCaptchaPlugin extends Plugin {
var $config_class = 'reCaptchaConfig';
function bootstrap() {
reCaptchaField::$plugin_config = $this->getConfig();
FormField::addFieldTypes(__('Verification'), function() {
return array(
'recaptcha' => array('Google reCAPTCHA', 'reCaptchaField')
);
});
}
}