includes/captcha/plugins/captcha_abstract.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 10 Mar 2012 00:18:56 +0400
changeset 15 8804e4d3de74
parent 0 ceef6d64c21d
permissions -rw-r--r--
Added online gamers
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package VC
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2006, 2008 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
if (!defined('IN_PHPBB'))
indvdum@0
    15
{
indvdum@0
    16
	exit;
indvdum@0
    17
}
indvdum@0
    18
indvdum@0
    19
indvdum@0
    20
/**
indvdum@0
    21
* This class holds the code shared by the two default 3.0.x CAPTCHAs.
indvdum@0
    22
*
indvdum@0
    23
* @package VC
indvdum@0
    24
*/
indvdum@0
    25
class phpbb_default_captcha
indvdum@0
    26
{
indvdum@0
    27
	var $confirm_id;
indvdum@0
    28
	var $confirm_code;
indvdum@0
    29
	var $code;
indvdum@0
    30
	var $seed;
indvdum@0
    31
	var $attempts = 0;
indvdum@0
    32
	var $type;
indvdum@0
    33
	var $solved = 0;
indvdum@0
    34
	var $captcha_vars = false;
indvdum@0
    35
indvdum@0
    36
	function init($type)
indvdum@0
    37
	{
indvdum@0
    38
		global $config, $db, $user;
indvdum@0
    39
indvdum@0
    40
		// read input
indvdum@0
    41
		$this->confirm_id = request_var('confirm_id', '');
indvdum@0
    42
		$this->confirm_code = request_var('confirm_code', '');
indvdum@0
    43
		$refresh = request_var('refresh_vc', false) && $config['confirm_refresh'];
indvdum@0
    44
indvdum@0
    45
		$this->type = (int) $type;
indvdum@0
    46
indvdum@0
    47
		if (!strlen($this->confirm_id) || !$this->load_code())
indvdum@0
    48
		{
indvdum@0
    49
			// we have no confirm ID, better get ready to display something
indvdum@0
    50
			$this->generate_code();
indvdum@0
    51
		}
indvdum@0
    52
		else if ($refresh)
indvdum@0
    53
		{
indvdum@0
    54
			$this->regenerate_code();
indvdum@0
    55
		}
indvdum@0
    56
	}
indvdum@0
    57
indvdum@0
    58
	function execute_demo()
indvdum@0
    59
	{
indvdum@0
    60
		global $user;
indvdum@0
    61
indvdum@0
    62
		$this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
indvdum@0
    63
		$this->seed = hexdec(substr(unique_id(), 4, 10));
indvdum@0
    64
indvdum@0
    65
		// compute $seed % 0x7fffffff
indvdum@0
    66
		$this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
indvdum@0
    67
indvdum@0
    68
		$captcha = new captcha();
indvdum@0
    69
		define('IMAGE_OUTPUT', 1);
indvdum@0
    70
		$captcha->execute($this->code, $this->seed);
indvdum@0
    71
	}
indvdum@0
    72
indvdum@0
    73
	function execute()
indvdum@0
    74
	{
indvdum@0
    75
		if (empty($this->code))
indvdum@0
    76
		{
indvdum@0
    77
			if (!$this->load_code())
indvdum@0
    78
			{
indvdum@0
    79
				// invalid request, bail out
indvdum@0
    80
				return false;
indvdum@0
    81
			}
indvdum@0
    82
		}
indvdum@0
    83
		$captcha = new captcha();
indvdum@0
    84
		define('IMAGE_OUTPUT', 1);
indvdum@0
    85
		$captcha->execute($this->code, $this->seed);
indvdum@0
    86
	}
indvdum@0
    87
indvdum@0
    88
	function get_template()
indvdum@0
    89
	{
indvdum@0
    90
		global $config, $user, $template, $phpEx, $phpbb_root_path;
indvdum@0
    91
indvdum@0
    92
		if ($this->is_solved())
indvdum@0
    93
		{
indvdum@0
    94
			return false;
indvdum@0
    95
		}
indvdum@0
    96
		else
indvdum@0
    97
		{
indvdum@0
    98
			$link = append_sid($phpbb_root_path . 'ucp.' . $phpEx,  'mode=confirm&amp;confirm_id=' . $this->confirm_id . '&amp;type=' . $this->type);
indvdum@0
    99
			$explain = $user->lang(($this->type != CONFIRM_POST) ? 'CONFIRM_EXPLAIN' : 'POST_CONFIRM_EXPLAIN', '<a href="mailto:' . htmlspecialchars($config['board_contact']) . '">', '</a>');
indvdum@0
   100
indvdum@0
   101
			$template->assign_vars(array(
indvdum@0
   102
				'CONFIRM_IMAGE_LINK'		=> $link,
indvdum@0
   103
				'CONFIRM_IMAGE'				=> '<img src="' . $link . '" />',
indvdum@0
   104
				'CONFIRM_IMG'				=> '<img src="' . $link . '" />',
indvdum@0
   105
				'CONFIRM_ID'				=> $this->confirm_id,
indvdum@0
   106
				'S_CONFIRM_CODE'			=> true,
indvdum@0
   107
				'S_TYPE'					=> $this->type,
indvdum@0
   108
				'S_CONFIRM_REFRESH'			=> ($config['enable_confirm'] && $config['confirm_refresh'] && $this->type == CONFIRM_REG) ? true : false,
indvdum@0
   109
				'L_CONFIRM_EXPLAIN'			=> $explain,
indvdum@0
   110
			));
indvdum@0
   111
indvdum@0
   112
			return 'captcha_default.html';
indvdum@0
   113
		}
indvdum@0
   114
	}
indvdum@0
   115
indvdum@0
   116
	function get_demo_template($id)
indvdum@0
   117
	{
indvdum@0
   118
		global $config, $user, $template, $phpbb_admin_path, $phpEx;
indvdum@0
   119
indvdum@0
   120
		$variables = '';
indvdum@0
   121
indvdum@0
   122
		if (is_array($this->captcha_vars))
indvdum@0
   123
		{
indvdum@0
   124
			foreach ($this->captcha_vars as $captcha_var => $template_var)
indvdum@0
   125
			{
indvdum@0
   126
				$variables .= '&amp;' . rawurlencode($captcha_var) . '=' . request_var($captcha_var, (int) $config[$captcha_var]);
indvdum@0
   127
			}
indvdum@0
   128
		}
indvdum@0
   129
indvdum@0
   130
		// acp_captcha has a delivery function; let's use it
indvdum@0
   131
		$template->assign_vars(array(
indvdum@0
   132
			'CONFIRM_IMAGE'		=> append_sid($phpbb_admin_path . 'index.' . $phpEx, 'captcha_demo=1&amp;mode=visual&amp;i=' . $id . '&amp;select_captcha=' . $this->get_class_name()) . $variables,
indvdum@0
   133
			'CONFIRM_ID'		=> $this->confirm_id,
indvdum@0
   134
		));
indvdum@0
   135
indvdum@0
   136
		return 'captcha_default_acp_demo.html';
indvdum@0
   137
	}
indvdum@0
   138
indvdum@0
   139
	function get_hidden_fields()
indvdum@0
   140
	{
indvdum@0
   141
		$hidden_fields = array();
indvdum@0
   142
indvdum@0
   143
		// this is required for posting.php - otherwise we would forget about the captcha being already solved
indvdum@0
   144
		if ($this->solved)
indvdum@0
   145
		{
indvdum@0
   146
			$hidden_fields['confirm_code'] = $this->confirm_code;
indvdum@0
   147
		}
indvdum@0
   148
		$hidden_fields['confirm_id'] = $this->confirm_id;
indvdum@0
   149
		return $hidden_fields;
indvdum@0
   150
	}
indvdum@0
   151
indvdum@0
   152
	function garbage_collect($type)
indvdum@0
   153
	{
indvdum@0
   154
		global $db, $config;
indvdum@0
   155
indvdum@0
   156
		$sql = 'SELECT DISTINCT c.session_id
indvdum@0
   157
			FROM ' . CONFIRM_TABLE . ' c
indvdum@0
   158
			LEFT JOIN ' . SESSIONS_TABLE . ' s ON (c.session_id = s.session_id)
indvdum@0
   159
			WHERE s.session_id IS NULL' .
indvdum@0
   160
				((empty($type)) ? '' : ' AND c.confirm_type = ' . (int) $type);
indvdum@0
   161
		$result = $db->sql_query($sql);
indvdum@0
   162
indvdum@0
   163
		if ($row = $db->sql_fetchrow($result))
indvdum@0
   164
		{
indvdum@0
   165
			$sql_in = array();
indvdum@0
   166
			do
indvdum@0
   167
			{
indvdum@0
   168
				$sql_in[] = (string) $row['session_id'];
indvdum@0
   169
			}
indvdum@0
   170
			while ($row = $db->sql_fetchrow($result));
indvdum@0
   171
indvdum@0
   172
			if (sizeof($sql_in))
indvdum@0
   173
			{
indvdum@0
   174
				$sql = 'DELETE FROM ' . CONFIRM_TABLE . '
indvdum@0
   175
					WHERE ' . $db->sql_in_set('session_id', $sql_in);
indvdum@0
   176
				$db->sql_query($sql);
indvdum@0
   177
			}
indvdum@0
   178
		}
indvdum@0
   179
		$db->sql_freeresult($result);
indvdum@0
   180
	}
indvdum@0
   181
indvdum@0
   182
	function uninstall()
indvdum@0
   183
	{
indvdum@0
   184
		$this->garbage_collect(0);
indvdum@0
   185
	}
indvdum@0
   186
indvdum@0
   187
	function install()
indvdum@0
   188
	{
indvdum@0
   189
		return;
indvdum@0
   190
	}
indvdum@0
   191
indvdum@0
   192
	function validate()
indvdum@0
   193
	{
indvdum@0
   194
		global $config, $db, $user;
indvdum@0
   195
indvdum@0
   196
		if (empty($user->lang))
indvdum@0
   197
		{
indvdum@0
   198
			$user->setup();
indvdum@0
   199
		}
indvdum@0
   200
indvdum@0
   201
		$error = '';
indvdum@0
   202
		if (!$this->confirm_id)
indvdum@0
   203
		{
indvdum@0
   204
			$error = $user->lang['CONFIRM_CODE_WRONG'];
indvdum@0
   205
		}
indvdum@0
   206
		else
indvdum@0
   207
		{
indvdum@0
   208
			if ($this->check_code())
indvdum@0
   209
			{
indvdum@0
   210
				// $this->delete_code(); commented out to allow posting.php to repeat the question
indvdum@0
   211
				$this->solved = true;
indvdum@0
   212
			}
indvdum@0
   213
			else
indvdum@0
   214
			{
indvdum@0
   215
				$error = $user->lang['CONFIRM_CODE_WRONG'];
indvdum@0
   216
			}
indvdum@0
   217
		}
indvdum@0
   218
indvdum@0
   219
		if (strlen($error))
indvdum@0
   220
		{
indvdum@0
   221
			// okay, incorrect answer. Let's ask a new question.
indvdum@0
   222
			$this->new_attempt();
indvdum@0
   223
			return $error;
indvdum@0
   224
		}
indvdum@0
   225
		else
indvdum@0
   226
		{
indvdum@0
   227
			return false;
indvdum@0
   228
		}
indvdum@0
   229
	}
indvdum@0
   230
indvdum@0
   231
	/**
indvdum@0
   232
	* The old way to generate code, suitable for GD and non-GD. Resets the internal state.
indvdum@0
   233
	*/
indvdum@0
   234
	function generate_code()
indvdum@0
   235
	{
indvdum@0
   236
		global $db, $user;
indvdum@0
   237
indvdum@0
   238
		$this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
indvdum@0
   239
		$this->confirm_id = md5(unique_id($user->ip));
indvdum@0
   240
		$this->seed = hexdec(substr(unique_id(), 4, 10));
indvdum@0
   241
		$this->solved = 0;
indvdum@0
   242
		// compute $seed % 0x7fffffff
indvdum@0
   243
		$this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
indvdum@0
   244
indvdum@0
   245
		$sql = 'INSERT INTO ' . CONFIRM_TABLE . ' ' . $db->sql_build_array('INSERT', array(
indvdum@0
   246
				'confirm_id'	=> (string) $this->confirm_id,
indvdum@0
   247
				'session_id'	=> (string) $user->session_id,
indvdum@0
   248
				'confirm_type'	=> (int) $this->type,
indvdum@0
   249
				'code'			=> (string) $this->code,
indvdum@0
   250
				'seed'			=> (int) $this->seed)
indvdum@0
   251
		);
indvdum@0
   252
		$db->sql_query($sql);
indvdum@0
   253
	}
indvdum@0
   254
indvdum@0
   255
	/**
indvdum@0
   256
	* New Question, if desired.
indvdum@0
   257
	*/
indvdum@0
   258
	function regenerate_code()
indvdum@0
   259
	{
indvdum@0
   260
		global $db, $user;
indvdum@0
   261
indvdum@0
   262
		$this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
indvdum@0
   263
		$this->seed = hexdec(substr(unique_id(), 4, 10));
indvdum@0
   264
		$this->solved = 0;
indvdum@0
   265
		// compute $seed % 0x7fffffff
indvdum@0
   266
		$this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
indvdum@0
   267
indvdum@0
   268
		$sql = 'UPDATE ' . CONFIRM_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
indvdum@0
   269
				'code'			=> (string) $this->code,
indvdum@0
   270
				'seed'			=> (int) $this->seed)) . '
indvdum@0
   271
				WHERE
indvdum@0
   272
				confirm_id = \'' . $db->sql_escape($this->confirm_id) . '\'
indvdum@0
   273
					AND session_id = \'' . $db->sql_escape($user->session_id) . '\'';
indvdum@0
   274
		$db->sql_query($sql);
indvdum@0
   275
	}
indvdum@0
   276
indvdum@0
   277
	/**
indvdum@0
   278
	* New Question, if desired.
indvdum@0
   279
	*/
indvdum@0
   280
	function new_attempt()
indvdum@0
   281
	{
indvdum@0
   282
		global $db, $user;
indvdum@0
   283
indvdum@0
   284
		$this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
indvdum@0
   285
		$this->seed = hexdec(substr(unique_id(), 4, 10));
indvdum@0
   286
		$this->solved = 0;
indvdum@0
   287
		// compute $seed % 0x7fffffff
indvdum@0
   288
		$this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
indvdum@0
   289
indvdum@0
   290
		$sql = 'UPDATE ' . CONFIRM_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
indvdum@0
   291
				'code'			=> (string) $this->code,
indvdum@0
   292
				'seed'			=> (int) $this->seed)) . '
indvdum@0
   293
				, attempts = attempts + 1
indvdum@0
   294
				WHERE
indvdum@0
   295
				confirm_id = \'' . $db->sql_escape($this->confirm_id) . '\'
indvdum@0
   296
					AND session_id = \'' . $db->sql_escape($user->session_id) . '\'';
indvdum@0
   297
		$db->sql_query($sql);
indvdum@0
   298
	}
indvdum@0
   299
indvdum@0
   300
	/**
indvdum@0
   301
	* Look up everything we need for painting&checking.
indvdum@0
   302
	*/
indvdum@0
   303
	function load_code()
indvdum@0
   304
	{
indvdum@0
   305
		global $db, $user;
indvdum@0
   306
indvdum@0
   307
		$sql = 'SELECT code, seed, attempts
indvdum@0
   308
			FROM ' . CONFIRM_TABLE . "
indvdum@0
   309
			WHERE confirm_id = '" . $db->sql_escape($this->confirm_id) . "'
indvdum@0
   310
				AND session_id = '" . $db->sql_escape($user->session_id) . "'
indvdum@0
   311
				AND confirm_type = " . $this->type;
indvdum@0
   312
		$result = $db->sql_query($sql);
indvdum@0
   313
		$row = $db->sql_fetchrow($result);
indvdum@0
   314
		$db->sql_freeresult($result);
indvdum@0
   315
indvdum@0
   316
		if ($row)
indvdum@0
   317
		{
indvdum@0
   318
			$this->code = $row['code'];
indvdum@0
   319
			$this->seed = $row['seed'];
indvdum@0
   320
			$this->attempts = $row['attempts'];
indvdum@0
   321
			return true;
indvdum@0
   322
		}
indvdum@0
   323
indvdum@0
   324
		return false;
indvdum@0
   325
	}
indvdum@0
   326
indvdum@0
   327
	function check_code()
indvdum@0
   328
	{
indvdum@0
   329
		return (strcasecmp($this->code, $this->confirm_code) === 0);
indvdum@0
   330
	}
indvdum@0
   331
indvdum@0
   332
	function delete_code()
indvdum@0
   333
	{
indvdum@0
   334
		global $db, $user;
indvdum@0
   335
indvdum@0
   336
		$sql = 'DELETE FROM ' . CONFIRM_TABLE . "
indvdum@0
   337
			WHERE confirm_id = '" . $db->sql_escape($confirm_id) . "'
indvdum@0
   338
				AND session_id = '" . $db->sql_escape($user->session_id) . "'
indvdum@0
   339
				AND confirm_type = " . $this->type;
indvdum@0
   340
		$db->sql_query($sql);
indvdum@0
   341
	}
indvdum@0
   342
indvdum@0
   343
	function get_attempt_count()
indvdum@0
   344
	{
indvdum@0
   345
		return $this->attempts;
indvdum@0
   346
	}
indvdum@0
   347
indvdum@0
   348
	function reset()
indvdum@0
   349
	{
indvdum@0
   350
		global $db, $user;
indvdum@0
   351
indvdum@0
   352
		$sql = 'DELETE FROM ' . CONFIRM_TABLE . "
indvdum@0
   353
			WHERE session_id = '" . $db->sql_escape($user->session_id) . "'
indvdum@0
   354
				AND confirm_type = " . (int) $this->type;
indvdum@0
   355
		$db->sql_query($sql);
indvdum@0
   356
indvdum@0
   357
		// we leave the class usable by generating a new question
indvdum@0
   358
		$this->generate_code();
indvdum@0
   359
	}
indvdum@0
   360
indvdum@0
   361
	function is_solved()
indvdum@0
   362
	{
indvdum@0
   363
		if (request_var('confirm_code', false) && $this->solved === 0)
indvdum@0
   364
		{
indvdum@0
   365
			$this->validate();
indvdum@0
   366
		}
indvdum@0
   367
		return (bool) $this->solved;
indvdum@0
   368
	}
indvdum@0
   369
indvdum@0
   370
	/**
indvdum@0
   371
	*  API function
indvdum@0
   372
	*/
indvdum@0
   373
	function has_config()
indvdum@0
   374
	{
indvdum@0
   375
		return false;
indvdum@0
   376
	}
indvdum@0
   377
indvdum@0
   378
}
indvdum@0
   379
indvdum@0
   380
?>