1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/captcha/plugins/captcha_abstract.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,380 @@
1.4 +<?php
1.5 +/**
1.6 +*
1.7 +* @package VC
1.8 +* @version $Id$
1.9 +* @copyright (c) 2006, 2008 phpBB Group
1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
1.11 +*
1.12 +*/
1.13 +
1.14 +/**
1.15 +* @ignore
1.16 +*/
1.17 +if (!defined('IN_PHPBB'))
1.18 +{
1.19 + exit;
1.20 +}
1.21 +
1.22 +
1.23 +/**
1.24 +* This class holds the code shared by the two default 3.0.x CAPTCHAs.
1.25 +*
1.26 +* @package VC
1.27 +*/
1.28 +class phpbb_default_captcha
1.29 +{
1.30 + var $confirm_id;
1.31 + var $confirm_code;
1.32 + var $code;
1.33 + var $seed;
1.34 + var $attempts = 0;
1.35 + var $type;
1.36 + var $solved = 0;
1.37 + var $captcha_vars = false;
1.38 +
1.39 + function init($type)
1.40 + {
1.41 + global $config, $db, $user;
1.42 +
1.43 + // read input
1.44 + $this->confirm_id = request_var('confirm_id', '');
1.45 + $this->confirm_code = request_var('confirm_code', '');
1.46 + $refresh = request_var('refresh_vc', false) && $config['confirm_refresh'];
1.47 +
1.48 + $this->type = (int) $type;
1.49 +
1.50 + if (!strlen($this->confirm_id) || !$this->load_code())
1.51 + {
1.52 + // we have no confirm ID, better get ready to display something
1.53 + $this->generate_code();
1.54 + }
1.55 + else if ($refresh)
1.56 + {
1.57 + $this->regenerate_code();
1.58 + }
1.59 + }
1.60 +
1.61 + function execute_demo()
1.62 + {
1.63 + global $user;
1.64 +
1.65 + $this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
1.66 + $this->seed = hexdec(substr(unique_id(), 4, 10));
1.67 +
1.68 + // compute $seed % 0x7fffffff
1.69 + $this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
1.70 +
1.71 + $captcha = new captcha();
1.72 + define('IMAGE_OUTPUT', 1);
1.73 + $captcha->execute($this->code, $this->seed);
1.74 + }
1.75 +
1.76 + function execute()
1.77 + {
1.78 + if (empty($this->code))
1.79 + {
1.80 + if (!$this->load_code())
1.81 + {
1.82 + // invalid request, bail out
1.83 + return false;
1.84 + }
1.85 + }
1.86 + $captcha = new captcha();
1.87 + define('IMAGE_OUTPUT', 1);
1.88 + $captcha->execute($this->code, $this->seed);
1.89 + }
1.90 +
1.91 + function get_template()
1.92 + {
1.93 + global $config, $user, $template, $phpEx, $phpbb_root_path;
1.94 +
1.95 + if ($this->is_solved())
1.96 + {
1.97 + return false;
1.98 + }
1.99 + else
1.100 + {
1.101 + $link = append_sid($phpbb_root_path . 'ucp.' . $phpEx, 'mode=confirm&confirm_id=' . $this->confirm_id . '&type=' . $this->type);
1.102 + $explain = $user->lang(($this->type != CONFIRM_POST) ? 'CONFIRM_EXPLAIN' : 'POST_CONFIRM_EXPLAIN', '<a href="mailto:' . htmlspecialchars($config['board_contact']) . '">', '</a>');
1.103 +
1.104 + $template->assign_vars(array(
1.105 + 'CONFIRM_IMAGE_LINK' => $link,
1.106 + 'CONFIRM_IMAGE' => '<img src="' . $link . '" />',
1.107 + 'CONFIRM_IMG' => '<img src="' . $link . '" />',
1.108 + 'CONFIRM_ID' => $this->confirm_id,
1.109 + 'S_CONFIRM_CODE' => true,
1.110 + 'S_TYPE' => $this->type,
1.111 + 'S_CONFIRM_REFRESH' => ($config['enable_confirm'] && $config['confirm_refresh'] && $this->type == CONFIRM_REG) ? true : false,
1.112 + 'L_CONFIRM_EXPLAIN' => $explain,
1.113 + ));
1.114 +
1.115 + return 'captcha_default.html';
1.116 + }
1.117 + }
1.118 +
1.119 + function get_demo_template($id)
1.120 + {
1.121 + global $config, $user, $template, $phpbb_admin_path, $phpEx;
1.122 +
1.123 + $variables = '';
1.124 +
1.125 + if (is_array($this->captcha_vars))
1.126 + {
1.127 + foreach ($this->captcha_vars as $captcha_var => $template_var)
1.128 + {
1.129 + $variables .= '&' . rawurlencode($captcha_var) . '=' . request_var($captcha_var, (int) $config[$captcha_var]);
1.130 + }
1.131 + }
1.132 +
1.133 + // acp_captcha has a delivery function; let's use it
1.134 + $template->assign_vars(array(
1.135 + 'CONFIRM_IMAGE' => append_sid($phpbb_admin_path . 'index.' . $phpEx, 'captcha_demo=1&mode=visual&i=' . $id . '&select_captcha=' . $this->get_class_name()) . $variables,
1.136 + 'CONFIRM_ID' => $this->confirm_id,
1.137 + ));
1.138 +
1.139 + return 'captcha_default_acp_demo.html';
1.140 + }
1.141 +
1.142 + function get_hidden_fields()
1.143 + {
1.144 + $hidden_fields = array();
1.145 +
1.146 + // this is required for posting.php - otherwise we would forget about the captcha being already solved
1.147 + if ($this->solved)
1.148 + {
1.149 + $hidden_fields['confirm_code'] = $this->confirm_code;
1.150 + }
1.151 + $hidden_fields['confirm_id'] = $this->confirm_id;
1.152 + return $hidden_fields;
1.153 + }
1.154 +
1.155 + function garbage_collect($type)
1.156 + {
1.157 + global $db, $config;
1.158 +
1.159 + $sql = 'SELECT DISTINCT c.session_id
1.160 + FROM ' . CONFIRM_TABLE . ' c
1.161 + LEFT JOIN ' . SESSIONS_TABLE . ' s ON (c.session_id = s.session_id)
1.162 + WHERE s.session_id IS NULL' .
1.163 + ((empty($type)) ? '' : ' AND c.confirm_type = ' . (int) $type);
1.164 + $result = $db->sql_query($sql);
1.165 +
1.166 + if ($row = $db->sql_fetchrow($result))
1.167 + {
1.168 + $sql_in = array();
1.169 + do
1.170 + {
1.171 + $sql_in[] = (string) $row['session_id'];
1.172 + }
1.173 + while ($row = $db->sql_fetchrow($result));
1.174 +
1.175 + if (sizeof($sql_in))
1.176 + {
1.177 + $sql = 'DELETE FROM ' . CONFIRM_TABLE . '
1.178 + WHERE ' . $db->sql_in_set('session_id', $sql_in);
1.179 + $db->sql_query($sql);
1.180 + }
1.181 + }
1.182 + $db->sql_freeresult($result);
1.183 + }
1.184 +
1.185 + function uninstall()
1.186 + {
1.187 + $this->garbage_collect(0);
1.188 + }
1.189 +
1.190 + function install()
1.191 + {
1.192 + return;
1.193 + }
1.194 +
1.195 + function validate()
1.196 + {
1.197 + global $config, $db, $user;
1.198 +
1.199 + if (empty($user->lang))
1.200 + {
1.201 + $user->setup();
1.202 + }
1.203 +
1.204 + $error = '';
1.205 + if (!$this->confirm_id)
1.206 + {
1.207 + $error = $user->lang['CONFIRM_CODE_WRONG'];
1.208 + }
1.209 + else
1.210 + {
1.211 + if ($this->check_code())
1.212 + {
1.213 + // $this->delete_code(); commented out to allow posting.php to repeat the question
1.214 + $this->solved = true;
1.215 + }
1.216 + else
1.217 + {
1.218 + $error = $user->lang['CONFIRM_CODE_WRONG'];
1.219 + }
1.220 + }
1.221 +
1.222 + if (strlen($error))
1.223 + {
1.224 + // okay, incorrect answer. Let's ask a new question.
1.225 + $this->new_attempt();
1.226 + return $error;
1.227 + }
1.228 + else
1.229 + {
1.230 + return false;
1.231 + }
1.232 + }
1.233 +
1.234 + /**
1.235 + * The old way to generate code, suitable for GD and non-GD. Resets the internal state.
1.236 + */
1.237 + function generate_code()
1.238 + {
1.239 + global $db, $user;
1.240 +
1.241 + $this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
1.242 + $this->confirm_id = md5(unique_id($user->ip));
1.243 + $this->seed = hexdec(substr(unique_id(), 4, 10));
1.244 + $this->solved = 0;
1.245 + // compute $seed % 0x7fffffff
1.246 + $this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
1.247 +
1.248 + $sql = 'INSERT INTO ' . CONFIRM_TABLE . ' ' . $db->sql_build_array('INSERT', array(
1.249 + 'confirm_id' => (string) $this->confirm_id,
1.250 + 'session_id' => (string) $user->session_id,
1.251 + 'confirm_type' => (int) $this->type,
1.252 + 'code' => (string) $this->code,
1.253 + 'seed' => (int) $this->seed)
1.254 + );
1.255 + $db->sql_query($sql);
1.256 + }
1.257 +
1.258 + /**
1.259 + * New Question, if desired.
1.260 + */
1.261 + function regenerate_code()
1.262 + {
1.263 + global $db, $user;
1.264 +
1.265 + $this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
1.266 + $this->seed = hexdec(substr(unique_id(), 4, 10));
1.267 + $this->solved = 0;
1.268 + // compute $seed % 0x7fffffff
1.269 + $this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
1.270 +
1.271 + $sql = 'UPDATE ' . CONFIRM_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
1.272 + 'code' => (string) $this->code,
1.273 + 'seed' => (int) $this->seed)) . '
1.274 + WHERE
1.275 + confirm_id = \'' . $db->sql_escape($this->confirm_id) . '\'
1.276 + AND session_id = \'' . $db->sql_escape($user->session_id) . '\'';
1.277 + $db->sql_query($sql);
1.278 + }
1.279 +
1.280 + /**
1.281 + * New Question, if desired.
1.282 + */
1.283 + function new_attempt()
1.284 + {
1.285 + global $db, $user;
1.286 +
1.287 + $this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
1.288 + $this->seed = hexdec(substr(unique_id(), 4, 10));
1.289 + $this->solved = 0;
1.290 + // compute $seed % 0x7fffffff
1.291 + $this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
1.292 +
1.293 + $sql = 'UPDATE ' . CONFIRM_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
1.294 + 'code' => (string) $this->code,
1.295 + 'seed' => (int) $this->seed)) . '
1.296 + , attempts = attempts + 1
1.297 + WHERE
1.298 + confirm_id = \'' . $db->sql_escape($this->confirm_id) . '\'
1.299 + AND session_id = \'' . $db->sql_escape($user->session_id) . '\'';
1.300 + $db->sql_query($sql);
1.301 + }
1.302 +
1.303 + /**
1.304 + * Look up everything we need for painting&checking.
1.305 + */
1.306 + function load_code()
1.307 + {
1.308 + global $db, $user;
1.309 +
1.310 + $sql = 'SELECT code, seed, attempts
1.311 + FROM ' . CONFIRM_TABLE . "
1.312 + WHERE confirm_id = '" . $db->sql_escape($this->confirm_id) . "'
1.313 + AND session_id = '" . $db->sql_escape($user->session_id) . "'
1.314 + AND confirm_type = " . $this->type;
1.315 + $result = $db->sql_query($sql);
1.316 + $row = $db->sql_fetchrow($result);
1.317 + $db->sql_freeresult($result);
1.318 +
1.319 + if ($row)
1.320 + {
1.321 + $this->code = $row['code'];
1.322 + $this->seed = $row['seed'];
1.323 + $this->attempts = $row['attempts'];
1.324 + return true;
1.325 + }
1.326 +
1.327 + return false;
1.328 + }
1.329 +
1.330 + function check_code()
1.331 + {
1.332 + return (strcasecmp($this->code, $this->confirm_code) === 0);
1.333 + }
1.334 +
1.335 + function delete_code()
1.336 + {
1.337 + global $db, $user;
1.338 +
1.339 + $sql = 'DELETE FROM ' . CONFIRM_TABLE . "
1.340 + WHERE confirm_id = '" . $db->sql_escape($confirm_id) . "'
1.341 + AND session_id = '" . $db->sql_escape($user->session_id) . "'
1.342 + AND confirm_type = " . $this->type;
1.343 + $db->sql_query($sql);
1.344 + }
1.345 +
1.346 + function get_attempt_count()
1.347 + {
1.348 + return $this->attempts;
1.349 + }
1.350 +
1.351 + function reset()
1.352 + {
1.353 + global $db, $user;
1.354 +
1.355 + $sql = 'DELETE FROM ' . CONFIRM_TABLE . "
1.356 + WHERE session_id = '" . $db->sql_escape($user->session_id) . "'
1.357 + AND confirm_type = " . (int) $this->type;
1.358 + $db->sql_query($sql);
1.359 +
1.360 + // we leave the class usable by generating a new question
1.361 + $this->generate_code();
1.362 + }
1.363 +
1.364 + function is_solved()
1.365 + {
1.366 + if (request_var('confirm_code', false) && $this->solved === 0)
1.367 + {
1.368 + $this->validate();
1.369 + }
1.370 + return (bool) $this->solved;
1.371 + }
1.372 +
1.373 + /**
1.374 + * API function
1.375 + */
1.376 + function has_config()
1.377 + {
1.378 + return false;
1.379 + }
1.380 +
1.381 +}
1.382 +
1.383 +?>
1.384 \ No newline at end of file