6 * @copyright (c) 2006, 2008 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
21 * This class holds the code shared by the two default 3.0.x CAPTCHAs.
25 class phpbb_default_captcha
34 var $captcha_vars = false;
38 global $config, $db, $user;
41 $this->confirm_id = request_var('confirm_id', '');
42 $this->confirm_code = request_var('confirm_code', '');
43 $refresh = request_var('refresh_vc', false) && $config['confirm_refresh'];
45 $this->type = (int) $type;
47 if (!strlen($this->confirm_id) || !$this->load_code())
49 // we have no confirm ID, better get ready to display something
50 $this->generate_code();
54 $this->regenerate_code();
58 function execute_demo()
62 $this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
63 $this->seed = hexdec(substr(unique_id(), 4, 10));
65 // compute $seed % 0x7fffffff
66 $this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
68 $captcha = new captcha();
69 define('IMAGE_OUTPUT', 1);
70 $captcha->execute($this->code, $this->seed);
75 if (empty($this->code))
77 if (!$this->load_code())
79 // invalid request, bail out
83 $captcha = new captcha();
84 define('IMAGE_OUTPUT', 1);
85 $captcha->execute($this->code, $this->seed);
88 function get_template()
90 global $config, $user, $template, $phpEx, $phpbb_root_path;
92 if ($this->is_solved())
98 $link = append_sid($phpbb_root_path . 'ucp.' . $phpEx, 'mode=confirm&confirm_id=' . $this->confirm_id . '&type=' . $this->type);
99 $explain = $user->lang(($this->type != CONFIRM_POST) ? 'CONFIRM_EXPLAIN' : 'POST_CONFIRM_EXPLAIN', '<a href="mailto:' . htmlspecialchars($config['board_contact']) . '">', '</a>');
101 $template->assign_vars(array(
102 'CONFIRM_IMAGE_LINK' => $link,
103 'CONFIRM_IMAGE' => '<img src="' . $link . '" />',
104 'CONFIRM_IMG' => '<img src="' . $link . '" />',
105 'CONFIRM_ID' => $this->confirm_id,
106 'S_CONFIRM_CODE' => true,
107 'S_TYPE' => $this->type,
108 'S_CONFIRM_REFRESH' => ($config['enable_confirm'] && $config['confirm_refresh'] && $this->type == CONFIRM_REG) ? true : false,
109 'L_CONFIRM_EXPLAIN' => $explain,
112 return 'captcha_default.html';
116 function get_demo_template($id)
118 global $config, $user, $template, $phpbb_admin_path, $phpEx;
122 if (is_array($this->captcha_vars))
124 foreach ($this->captcha_vars as $captcha_var => $template_var)
126 $variables .= '&' . rawurlencode($captcha_var) . '=' . request_var($captcha_var, (int) $config[$captcha_var]);
130 // acp_captcha has a delivery function; let's use it
131 $template->assign_vars(array(
132 'CONFIRM_IMAGE' => append_sid($phpbb_admin_path . 'index.' . $phpEx, 'captcha_demo=1&mode=visual&i=' . $id . '&select_captcha=' . $this->get_class_name()) . $variables,
133 'CONFIRM_ID' => $this->confirm_id,
136 return 'captcha_default_acp_demo.html';
139 function get_hidden_fields()
141 $hidden_fields = array();
143 // this is required for posting.php - otherwise we would forget about the captcha being already solved
146 $hidden_fields['confirm_code'] = $this->confirm_code;
148 $hidden_fields['confirm_id'] = $this->confirm_id;
149 return $hidden_fields;
152 function garbage_collect($type)
156 $sql = 'SELECT DISTINCT c.session_id
157 FROM ' . CONFIRM_TABLE . ' c
158 LEFT JOIN ' . SESSIONS_TABLE . ' s ON (c.session_id = s.session_id)
159 WHERE s.session_id IS NULL' .
160 ((empty($type)) ? '' : ' AND c.confirm_type = ' . (int) $type);
161 $result = $db->sql_query($sql);
163 if ($row = $db->sql_fetchrow($result))
168 $sql_in[] = (string) $row['session_id'];
170 while ($row = $db->sql_fetchrow($result));
174 $sql = 'DELETE FROM ' . CONFIRM_TABLE . '
175 WHERE ' . $db->sql_in_set('session_id', $sql_in);
176 $db->sql_query($sql);
179 $db->sql_freeresult($result);
184 $this->garbage_collect(0);
194 global $config, $db, $user;
196 if (empty($user->lang))
202 if (!$this->confirm_id)
204 $error = $user->lang['CONFIRM_CODE_WRONG'];
208 if ($this->check_code())
210 // $this->delete_code(); commented out to allow posting.php to repeat the question
211 $this->solved = true;
215 $error = $user->lang['CONFIRM_CODE_WRONG'];
221 // okay, incorrect answer. Let's ask a new question.
222 $this->new_attempt();
232 * The old way to generate code, suitable for GD and non-GD. Resets the internal state.
234 function generate_code()
238 $this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
239 $this->confirm_id = md5(unique_id($user->ip));
240 $this->seed = hexdec(substr(unique_id(), 4, 10));
242 // compute $seed % 0x7fffffff
243 $this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
245 $sql = 'INSERT INTO ' . CONFIRM_TABLE . ' ' . $db->sql_build_array('INSERT', array(
246 'confirm_id' => (string) $this->confirm_id,
247 'session_id' => (string) $user->session_id,
248 'confirm_type' => (int) $this->type,
249 'code' => (string) $this->code,
250 'seed' => (int) $this->seed)
252 $db->sql_query($sql);
256 * New Question, if desired.
258 function regenerate_code()
262 $this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
263 $this->seed = hexdec(substr(unique_id(), 4, 10));
265 // compute $seed % 0x7fffffff
266 $this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
268 $sql = 'UPDATE ' . CONFIRM_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
269 'code' => (string) $this->code,
270 'seed' => (int) $this->seed)) . '
272 confirm_id = \'' . $db->sql_escape($this->confirm_id) . '\'
273 AND session_id = \'' . $db->sql_escape($user->session_id) . '\'';
274 $db->sql_query($sql);
278 * New Question, if desired.
280 function new_attempt()
284 $this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
285 $this->seed = hexdec(substr(unique_id(), 4, 10));
287 // compute $seed % 0x7fffffff
288 $this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
290 $sql = 'UPDATE ' . CONFIRM_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
291 'code' => (string) $this->code,
292 'seed' => (int) $this->seed)) . '
293 , attempts = attempts + 1
295 confirm_id = \'' . $db->sql_escape($this->confirm_id) . '\'
296 AND session_id = \'' . $db->sql_escape($user->session_id) . '\'';
297 $db->sql_query($sql);
301 * Look up everything we need for painting&checking.
307 $sql = 'SELECT code, seed, attempts
308 FROM ' . CONFIRM_TABLE . "
309 WHERE confirm_id = '" . $db->sql_escape($this->confirm_id) . "'
310 AND session_id = '" . $db->sql_escape($user->session_id) . "'
311 AND confirm_type = " . $this->type;
312 $result = $db->sql_query($sql);
313 $row = $db->sql_fetchrow($result);
314 $db->sql_freeresult($result);
318 $this->code = $row['code'];
319 $this->seed = $row['seed'];
320 $this->attempts = $row['attempts'];
327 function check_code()
329 return (strcasecmp($this->code, $this->confirm_code) === 0);
332 function delete_code()
336 $sql = 'DELETE FROM ' . CONFIRM_TABLE . "
337 WHERE confirm_id = '" . $db->sql_escape($confirm_id) . "'
338 AND session_id = '" . $db->sql_escape($user->session_id) . "'
339 AND confirm_type = " . $this->type;
340 $db->sql_query($sql);
343 function get_attempt_count()
345 return $this->attempts;
352 $sql = 'DELETE FROM ' . CONFIRM_TABLE . "
353 WHERE session_id = '" . $db->sql_escape($user->session_id) . "'
354 AND confirm_type = " . (int) $this->type;
355 $db->sql_query($sql);
357 // we leave the class usable by generating a new question
358 $this->generate_code();
363 if (request_var('confirm_code', false) && $this->solved === 0)
367 return (bool) $this->solved;
373 function has_config()