forked from mblaschke/TYPO3-jm-recaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.tx_jmrecaptcha.php
312 lines (268 loc) · 10.1 KB
/
class.tx_jmrecaptcha.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
/***************************************************************
* Copyright notice
*
* (c) 2009 Markus Blaschke, TEQneers GmbH & Co. KG ([email protected])
* (c) 2007 Jens Mittag ([email protected])
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Plugin 'reCAPTCHA' for the 'jm_recaptcha' extension.
*
* @author Markus Blaschke, TEQneers GmbH & Co. KG <[email protected]>
* @author Jens Mittag <[email protected]>
*/
class tx_jmrecaptcha extends \TYPO3\CMS\Frontend\Plugin\AbstractPlugin {
public $prefixId = 'tx_jmrecaptcha'; // Same as class name
public $scriptRelPath = 'class.tx_jmrecaptcha.php'; // Path to this script relative to the extension dir.
public $extKey = 'jm_recaptcha'; // The extension key.
public $extConf = NULL;
public $conf;
/**
* @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
*/
public $typoscriptFrontendController;
public function main($content, $conf) {
}
/**
* Build reCAPTCHA Frontend HTML-Code
*
* @param string $error Error Code (optional)
* @return string reCAPTCHA HTML-Code
*/
public function getReCaptcha($error = NULL) {
$this->pi_loadLL();
$this->initConfiguration();
if ($this->conf['captcha_type'] === 'nocaptcha') {
return $this->renderNoCaptcha();
} elseif ($this->conf['captcha_type'] === 'recaptcha') {
return $this->renderReCaptcha($error);
}
return NULL;
}
protected function initConfiguration() {
$this->typoscriptFrontendController = $GLOBALS['TSFE'];
$this->conf = $this->typoscriptFrontendController->tmpl->setup['plugin.']['tx_jmrecaptcha.'];
if ($this->conf['use_ssl'] || !empty($_SERVER["HTTPS"]) || $_SERVER['SERVER_PORT'] == 443) {
$this->conf['server'] = rtrim($this->conf['api_server_secure'], '/');
} else {
$this->conf['server'] = rtrim($this->conf['api_server'], '/');
}
}
/**
* @return string
*/
protected function renderNoCaptcha() {
return '<div class="g-recaptcha" data-sitekey="' . htmlspecialchars($this->conf['public_key']) . '"></div>';
}
/**
* @param $error
* @return string
*/
protected function renderReCaptcha($error) {
// were any errors given in the last query?
$err_parameter = '';
if ($error) {
$err_parameter = '&error=' . $error;
}
###############################
# reCaptcha Options
###############################
// Default settings
$recaptchaOptions = array(
'lang' => self::jsQuote('en'),
);
// Theme
if (!empty($this->conf['theme'])) {
$recaptchaOptions['theme'] = self::jsQuote($this->conf['theme']);
}
// TabIndex
if (!empty($this->conf['tabindex'])) {
$recaptchaOptions['tabindex'] = self::jsQuote($this->conf['tabindex']);
}
// TabIndex
if (!empty($this->conf['custom_theme_widget'])) {
$recaptchaOptions['custom_theme_widget'] = self::jsQuote($this->conf['custom_theme_widget']);
}
// Language detection
if (!empty($this->conf['lang'])) {
// language from plugin configuration
$recaptchaOptions['lang'] = self::jsQuote($this->conf['lang']);
} elseif (!empty($this->typoscriptFrontendController->tmpl->setup['config.']['language'])) {
// automatic language detection (TYPO3 settings)
$recaptchaOptions['lang'] = self::jsQuote($this->typoscriptFrontendController->tmpl->setup['config.']['language']);
}
// Custom translations
$customTranslations = array();
if (!empty($this->conf['custom_translations.'])) {
foreach ($this->conf['custom_translations.'] as $translationKey => $translationValue) {
$customTranslations[] = $translationKey . ' : ' . self::jsQuote($translationValue);
}
}
if (!empty($customTranslations)) {
$recaptchaOptions['custom_translations'] = ' { ' . implode(',', $customTranslations) . ' } ';
}
// Build option string
$recaptchaOptionsTmp = array();
foreach ($recaptchaOptions as $optionKey => $optionValue) {
$recaptchaOptionsTmp[] = $optionKey . ' : ' . $optionValue;
}
$recaptchaOptions = implode(', ', $recaptchaOptionsTmp);
###############################
# reCaptcha HTML
###############################
$content = '<script type="text/javascript">var RecaptchaOptions = { ' . $recaptchaOptions . ' };</script>';
$content .= '<script type="text/javascript" src="' . htmlspecialchars($this->conf['server'] . '/challenge?k=' . $this->conf['public_key'] . $err_parameter) . '"></script>';
$content .= '<noscript><iframe class="recaptcha-noscript-frame" src="' . htmlspecialchars($this->conf['server'] . '/noscript?k=' . $this->conf['public_key']) . '" height="300" width="500" frameborder="0"></iframe><br /><p>' . $this->pi_getLL('enter_code_here') . '</p><textarea class="recaptcha-noscript-textarea" name="recaptcha_challenge_field" rows="3" cols="40"></textarea><input type="hidden" name="recaptcha_response_field" value="manual_challenge" /></noscript>';
return $content;
}
/**
* Quote js-param-value
*
* @param string $value Value
* @return string Quoted value
*/
protected static function jsQuote($value) {
return '\'' . addslashes((string)$value) . '\'';
}
/**
* Validate reCAPTCHA challenge/response
*
* @return array
* @throws Exception
*/
public function validateReCaptcha() {
$this->initConfiguration();
if ($this->conf['captcha_type'] === 'nocaptcha') {
$data = array(
'remoteip' => $_SERVER['REMOTE_ADDR'],
'response' => trim((string)\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('g-recaptcha-response')),
'secret' => $this->conf['private_key'],
);
if (empty($data['response'])) {
return FALSE;
}
} elseif ($this->conf['captcha_type'] === 'recaptcha') {
$data = array(
'remoteip' => $_SERVER['REMOTE_ADDR'],
'challenge' => trim((string)\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('recaptcha_challenge_field')),
'response' => trim((string)\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('recaptcha_response_field')),
'privatekey' => $this->conf['private_key'],
);
if (empty($data['challenge']) || empty($data['response'])) {
return FALSE;
}
} else {
throw new \Exception('Invalid captcha_type', 1430388724);
}
$result = $this->queryVerificationServer($data);
if (!$result) {
return false;
}
// depending on the result, return appropriate status object
$ret = array();
if ($result[0]) {
$ret['verified'] = true;
$ret['error'] = NULL;
} else {
$ret['verified'] = false;
$ret['error'] = $result[1];
}
return $ret;
}
/**
* Query reCAPTCHA server for captcha-verification
* @param array $data
* @return array Array with verified- (boolean) and error-code (string)
* @throws Exception
*/
protected function queryVerificationServer($data) {
// if google as verify server and the old recaptcha type are used, fall back to google's old verification url
if ($this->conf['verify_server'] === 'https://www.google.com/recaptcha/api/siteverify' && $this->conf['captcha_type'] === 'recaptcha') {
$this->conf['verify_server'] = 'http://www.google.com/recaptcha/api/verify';
}
// find first occurence of '//' inside server string
$verifyServerInfo = parse_url($this->conf['verify_server']);
if (empty($verifyServerInfo)) {
return array(false, 'recaptcha-not-reachable');
}
if ($this->conf['captcha_type'] === 'nocaptcha') {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $this->conf['verify_server']);
curl_setopt($ch,CURLOPT_POST, count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS, \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl('', $data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($proxy_host = $GLOBALS['TYPO3_CONF_VARS']['HTTP']['proxy_host']) {
if (!($proxy_port = $GLOBALS['TYPO3_CONF_VARS']['HTTP']['proxy_port'])) {
throw new \TYPO3\CMS\Core\Exception('Proxy host set, but no proxy port. Please specify proxy port too');
}
curl_setopt($ch, CURLOPT_PROXY, $proxy_host . ':' . $proxy_port);
}
$response = json_decode(curl_exec($ch), TRUE);
$result = array(
$response['success'],
isset($response['error-codes']) ? $response['error-codes'][0] : ''
);
curl_close($ch);
} elseif ($this->conf['captcha_type'] === 'recaptcha') {
$paramStr = \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl('', $data);
$ret = \TYPO3\CMS\Core\Utility\GeneralUtility::getURL($this->conf['verify_server'] . '?' . $paramStr);
$result = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode("\n", $ret);
$result[0] = $result[0] == 'true';
} else {
throw new \Exception('Invalid captcha_type', 1430388724);
}
return $result;
}
/**
* QueryServer encode values
*
* @param array $data
* @return array Encoded data
*/
protected static function qsencode($data) {
$ret = "";
foreach ($data as $key => $value) {
$ret .= $key . '=' . urlencode(stripslashes($value)) . '&';
}
$ret = rtrim($ret, '&');
return $ret;
}
/**
* Get extension configuration (by name)
*
* @param string $name Configuration settings name
* @param mixed $defaultValue Default value (if configuration doesn't exists)
* @return mixed
*/
protected function getExtConf($name, $defaultValue = NULL) {
if ($this->extConf === NULL) {
// Load ext conf
$this->extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['jm_recaptcha']);
if (!is_array($this->extConf)) {
$this->extConf = array();
}
}
$ret = $defaultValue;
if (!empty($this->extConf[$name])) {
$ret = $this->extConf[$name];
}
return $ret;
}
}