|
1 <?php |
|
2 /** |
|
3 * |
|
4 * @package VC |
|
5 * @version $Id$ |
|
6 * @copyright (c) 2006, 2008 phpBB Group |
|
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
8 * |
|
9 */ |
|
10 |
|
11 /** |
|
12 * @ignore |
|
13 */ |
|
14 if (!defined('IN_PHPBB')) |
|
15 { |
|
16 exit; |
|
17 } |
|
18 |
|
19 if (!class_exists('phpbb_default_captcha')) |
|
20 { |
|
21 // we need the classic captcha code for tracking solutions and attempts |
|
22 include($phpbb_root_path . 'includes/captcha/plugins/captcha_abstract.' . $phpEx); |
|
23 } |
|
24 |
|
25 /** |
|
26 * @package VC |
|
27 */ |
|
28 class phpbb_recaptcha extends phpbb_default_captcha |
|
29 { |
|
30 var $recaptcha_server = 'http://www.google.com/recaptcha/api'; |
|
31 var $recaptcha_server_secure = 'https://www.google.com/recaptcha/api'; // class constants :( |
|
32 |
|
33 // We are opening a socket to port 80 of this host and send |
|
34 // the POST request asking for verification to the path specified here. |
|
35 var $recaptcha_verify_server = 'www.google.com'; |
|
36 var $recaptcha_verify_path = '/recaptcha/api/verify'; |
|
37 |
|
38 var $challenge; |
|
39 var $response; |
|
40 |
|
41 // PHP4 Constructor |
|
42 function phpbb_recaptcha() |
|
43 { |
|
44 $this->recaptcha_server = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? $this->recaptcha_server_secure : $this->recaptcha_server; |
|
45 } |
|
46 |
|
47 function init($type) |
|
48 { |
|
49 global $config, $db, $user; |
|
50 |
|
51 $user->add_lang('captcha_recaptcha'); |
|
52 parent::init($type); |
|
53 $this->challenge = request_var('recaptcha_challenge_field', ''); |
|
54 $this->response = request_var('recaptcha_response_field', ''); |
|
55 } |
|
56 |
|
57 function &get_instance() |
|
58 { |
|
59 $instance =& new phpbb_recaptcha(); |
|
60 return $instance; |
|
61 } |
|
62 |
|
63 function is_available() |
|
64 { |
|
65 global $config, $user; |
|
66 $user->add_lang('captcha_recaptcha'); |
|
67 return (isset($config['recaptcha_pubkey']) && !empty($config['recaptcha_pubkey'])); |
|
68 } |
|
69 |
|
70 /** |
|
71 * API function |
|
72 */ |
|
73 function has_config() |
|
74 { |
|
75 return true; |
|
76 } |
|
77 |
|
78 function get_name() |
|
79 { |
|
80 return 'CAPTCHA_RECAPTCHA'; |
|
81 } |
|
82 |
|
83 function get_class_name() |
|
84 { |
|
85 return 'phpbb_recaptcha'; |
|
86 } |
|
87 |
|
88 function acp_page($id, &$module) |
|
89 { |
|
90 global $config, $db, $template, $user; |
|
91 |
|
92 $captcha_vars = array( |
|
93 'recaptcha_pubkey' => 'RECAPTCHA_PUBKEY', |
|
94 'recaptcha_privkey' => 'RECAPTCHA_PRIVKEY', |
|
95 ); |
|
96 |
|
97 $module->tpl_name = 'captcha_recaptcha_acp'; |
|
98 $module->page_title = 'ACP_VC_SETTINGS'; |
|
99 $form_key = 'acp_captcha'; |
|
100 add_form_key($form_key); |
|
101 |
|
102 $submit = request_var('submit', ''); |
|
103 |
|
104 if ($submit && check_form_key($form_key)) |
|
105 { |
|
106 $captcha_vars = array_keys($captcha_vars); |
|
107 foreach ($captcha_vars as $captcha_var) |
|
108 { |
|
109 $value = request_var($captcha_var, ''); |
|
110 if ($value) |
|
111 { |
|
112 set_config($captcha_var, $value); |
|
113 } |
|
114 } |
|
115 |
|
116 add_log('admin', 'LOG_CONFIG_VISUAL'); |
|
117 trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($module->u_action)); |
|
118 } |
|
119 else if ($submit) |
|
120 { |
|
121 trigger_error($user->lang['FORM_INVALID'] . adm_back_link($module->u_action)); |
|
122 } |
|
123 else |
|
124 { |
|
125 foreach ($captcha_vars as $captcha_var => $template_var) |
|
126 { |
|
127 $var = (isset($_REQUEST[$captcha_var])) ? request_var($captcha_var, '') : ((isset($config[$captcha_var])) ? $config[$captcha_var] : ''); |
|
128 $template->assign_var($template_var, $var); |
|
129 } |
|
130 |
|
131 $template->assign_vars(array( |
|
132 'CAPTCHA_PREVIEW' => $this->get_demo_template($id), |
|
133 'CAPTCHA_NAME' => $this->get_class_name(), |
|
134 'U_ACTION' => $module->u_action, |
|
135 )); |
|
136 |
|
137 } |
|
138 } |
|
139 |
|
140 // not needed |
|
141 function execute_demo() |
|
142 { |
|
143 } |
|
144 |
|
145 // not needed |
|
146 function execute() |
|
147 { |
|
148 } |
|
149 |
|
150 function get_template() |
|
151 { |
|
152 global $config, $user, $template; |
|
153 |
|
154 if ($this->is_solved()) |
|
155 { |
|
156 return false; |
|
157 } |
|
158 else |
|
159 { |
|
160 $explain = $user->lang(($this->type != CONFIRM_POST) ? 'CONFIRM_EXPLAIN' : 'POST_CONFIRM_EXPLAIN', '<a href="mailto:' . htmlspecialchars($config['board_contact']) . '">', '</a>'); |
|
161 |
|
162 $template->assign_vars(array( |
|
163 'RECAPTCHA_SERVER' => $this->recaptcha_server, |
|
164 'RECAPTCHA_PUBKEY' => isset($config['recaptcha_pubkey']) ? $config['recaptcha_pubkey'] : '', |
|
165 'RECAPTCHA_ERRORGET' => '', |
|
166 'S_RECAPTCHA_AVAILABLE' => $this->is_available(), |
|
167 'S_CONFIRM_CODE' => true, |
|
168 'S_TYPE' => $this->type, |
|
169 'L_CONFIRM_EXPLAIN' => $explain, |
|
170 )); |
|
171 |
|
172 return 'captcha_recaptcha.html'; |
|
173 } |
|
174 } |
|
175 |
|
176 function get_demo_template($id) |
|
177 { |
|
178 return $this->get_template(); |
|
179 } |
|
180 |
|
181 function get_hidden_fields() |
|
182 { |
|
183 $hidden_fields = array(); |
|
184 |
|
185 // this is required for posting.php - otherwise we would forget about the captcha being already solved |
|
186 if ($this->solved) |
|
187 { |
|
188 $hidden_fields['confirm_code'] = $this->code; |
|
189 } |
|
190 $hidden_fields['confirm_id'] = $this->confirm_id; |
|
191 return $hidden_fields; |
|
192 } |
|
193 |
|
194 function uninstall() |
|
195 { |
|
196 $this->garbage_collect(0); |
|
197 } |
|
198 |
|
199 function install() |
|
200 { |
|
201 return; |
|
202 } |
|
203 |
|
204 function validate() |
|
205 { |
|
206 if (!parent::validate()) |
|
207 { |
|
208 return false; |
|
209 } |
|
210 else |
|
211 { |
|
212 return $this->recaptcha_check_answer(); |
|
213 } |
|
214 } |
|
215 |
|
216 // Code from here on is based on recaptchalib.php |
|
217 /* |
|
218 * This is a PHP library that handles calling reCAPTCHA. |
|
219 * - Documentation and latest version |
|
220 * http://recaptcha.net/plugins/php/ |
|
221 * - Get a reCAPTCHA API Key |
|
222 * http://recaptcha.net/api/getkey |
|
223 * - Discussion group |
|
224 * http://groups.google.com/group/recaptcha |
|
225 * |
|
226 * Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net |
|
227 * AUTHORS: |
|
228 * Mike Crawford |
|
229 * Ben Maurer |
|
230 * |
|
231 * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
232 * of this software and associated documentation files (the "Software"), to deal |
|
233 * in the Software without restriction, including without limitation the rights |
|
234 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
235 * copies of the Software, and to permit persons to whom the Software is |
|
236 * furnished to do so, subject to the following conditions: |
|
237 * |
|
238 * The above copyright notice and this permission notice shall be included in |
|
239 * all copies or substantial portions of the Software. |
|
240 * |
|
241 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
242 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
243 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
244 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
245 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
246 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
247 * THE SOFTWARE. |
|
248 */ |
|
249 |
|
250 /** |
|
251 * Submits an HTTP POST to a reCAPTCHA server |
|
252 * @param string $host |
|
253 * @param string $path |
|
254 * @param array $data |
|
255 * @param int port |
|
256 * @return array response |
|
257 */ |
|
258 function _recaptcha_http_post($host, $path, $data, $port = 80) |
|
259 { |
|
260 $req = $this->_recaptcha_qsencode ($data); |
|
261 |
|
262 $http_request = "POST $path HTTP/1.0\r\n"; |
|
263 $http_request .= "Host: $host\r\n"; |
|
264 $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n"; |
|
265 $http_request .= "Content-Length: " . strlen($req) . "\r\n"; |
|
266 $http_request .= "User-Agent: reCAPTCHA/PHP/phpBB\r\n"; |
|
267 $http_request .= "\r\n"; |
|
268 $http_request .= $req; |
|
269 |
|
270 $response = ''; |
|
271 if (false == ($fs = @fsockopen($host, $port, $errno, $errstr, 10))) |
|
272 { |
|
273 trigger_error('Could not open socket', E_USER_ERROR); |
|
274 } |
|
275 |
|
276 fwrite($fs, $http_request); |
|
277 |
|
278 while (!feof($fs)) |
|
279 { |
|
280 // One TCP-IP packet |
|
281 $response .= fgets($fs, 1160); |
|
282 } |
|
283 fclose($fs); |
|
284 $response = explode("\r\n\r\n", $response, 2); |
|
285 |
|
286 return $response; |
|
287 } |
|
288 |
|
289 /** |
|
290 * Calls an HTTP POST function to verify if the user's guess was correct |
|
291 * @param array $extra_params an array of extra variables to post to the server |
|
292 * @return ReCaptchaResponse |
|
293 */ |
|
294 function recaptcha_check_answer($extra_params = array()) |
|
295 { |
|
296 global $config, $user; |
|
297 |
|
298 //discard spam submissions |
|
299 if ($this->challenge == null || strlen($this->challenge) == 0 || $this->response == null || strlen($this->response) == 0) |
|
300 { |
|
301 return $user->lang['RECAPTCHA_INCORRECT']; |
|
302 } |
|
303 |
|
304 $response = $this->_recaptcha_http_post($this->recaptcha_verify_server, $this->recaptcha_verify_path, |
|
305 array( |
|
306 'privatekey' => $config['recaptcha_privkey'], |
|
307 'remoteip' => $user->ip, |
|
308 'challenge' => $this->challenge, |
|
309 'response' => $this->response |
|
310 ) + $extra_params |
|
311 ); |
|
312 |
|
313 $answers = explode("\n", $response[1]); |
|
314 |
|
315 if (trim($answers[0]) === 'true') |
|
316 { |
|
317 $this->solved = true; |
|
318 return false; |
|
319 } |
|
320 else |
|
321 { |
|
322 return $user->lang['RECAPTCHA_INCORRECT']; |
|
323 } |
|
324 } |
|
325 |
|
326 /** |
|
327 * Encodes the given data into a query string format |
|
328 * @param $data - array of string elements to be encoded |
|
329 * @return string - encoded request |
|
330 */ |
|
331 function _recaptcha_qsencode($data) |
|
332 { |
|
333 $req = ''; |
|
334 foreach ($data as $key => $value) |
|
335 { |
|
336 $req .= $key . '=' . urlencode(stripslashes($value)) . '&'; |
|
337 } |
|
338 |
|
339 // Cut the last '&' |
|
340 $req = substr($req, 0, strlen($req) - 1); |
|
341 return $req; |
|
342 } |
|
343 } |
|
344 |
|
345 ?> |