includes/captcha/plugins/phpbb_captcha_qa_plugin.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Wed, 07 Mar 2012 23:10:55 +0400
changeset 7 a0100cde92ff
parent 0 ceef6d64c21d
permissions -rw-r--r--
HG ignore
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
global $table_prefix;
indvdum@0
    20
indvdum@0
    21
define('CAPTCHA_QUESTIONS_TABLE',	$table_prefix . 'captcha_questions');
indvdum@0
    22
define('CAPTCHA_ANSWERS_TABLE',		$table_prefix . 'captcha_answers');
indvdum@0
    23
define('CAPTCHA_QA_CONFIRM_TABLE',	$table_prefix . 'qa_confirm');
indvdum@0
    24
indvdum@0
    25
/**
indvdum@0
    26
* And now to something completely different. Let's make a captcha without extending the abstract class.
indvdum@0
    27
* QA CAPTCHA sample implementation
indvdum@0
    28
*
indvdum@0
    29
* @package VC
indvdum@0
    30
*/
indvdum@0
    31
class phpbb_captcha_qa
indvdum@0
    32
{
indvdum@0
    33
	var $confirm_id;
indvdum@0
    34
	var $answer;
indvdum@0
    35
	var $question_ids;
indvdum@0
    36
	var $question_text;
indvdum@0
    37
	var $question_lang;
indvdum@0
    38
	var $question_strict;
indvdum@0
    39
	var $attempts = 0;
indvdum@0
    40
	var $type;
indvdum@0
    41
	// dirty trick: 0 is false, but can still encode that the captcha is not yet validated
indvdum@0
    42
	var $solved = 0;
indvdum@0
    43
indvdum@0
    44
	/**
indvdum@0
    45
	* @param int $type  as per the CAPTCHA API docs, the type
indvdum@0
    46
	*/
indvdum@0
    47
	function init($type)
indvdum@0
    48
	{
indvdum@0
    49
		global $config, $db, $user;
indvdum@0
    50
indvdum@0
    51
		// load our language file
indvdum@0
    52
		$user->add_lang('captcha_qa');
indvdum@0
    53
indvdum@0
    54
		// read input
indvdum@0
    55
		$this->confirm_id = request_var('qa_confirm_id', '');
indvdum@0
    56
		$this->answer = utf8_normalize_nfc(request_var('qa_answer', '', true));
indvdum@0
    57
indvdum@0
    58
		$this->type = (int) $type;
indvdum@0
    59
		$this->question_lang = $user->lang_name;
indvdum@0
    60
indvdum@0
    61
		// we need all defined questions - shouldn't be too many, so we can just grab them
indvdum@0
    62
		// try the user's lang first
indvdum@0
    63
		$sql = 'SELECT question_id
indvdum@0
    64
			FROM ' . CAPTCHA_QUESTIONS_TABLE . "
indvdum@0
    65
			WHERE lang_iso = '" . $db->sql_escape($user->lang_name) . "'";
indvdum@0
    66
		$result = $db->sql_query($sql, 3600);
indvdum@0
    67
indvdum@0
    68
		while ($row = $db->sql_fetchrow($result))
indvdum@0
    69
		{
indvdum@0
    70
			$this->question_ids[$row['question_id']] = $row['question_id'];
indvdum@0
    71
		}
indvdum@0
    72
		$db->sql_freeresult($result);
indvdum@0
    73
indvdum@0
    74
		// fallback to the board default lang
indvdum@0
    75
		if (!sizeof($this->question_ids))
indvdum@0
    76
		{
indvdum@0
    77
			$this->question_lang = $config['default_lang'];
indvdum@0
    78
indvdum@0
    79
			$sql = 'SELECT question_id
indvdum@0
    80
				FROM ' . CAPTCHA_QUESTIONS_TABLE . "
indvdum@0
    81
				WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'";
indvdum@0
    82
			$result = $db->sql_query($sql, 7200);
indvdum@0
    83
indvdum@0
    84
			while ($row = $db->sql_fetchrow($result))
indvdum@0
    85
			{
indvdum@0
    86
				$this->question_ids[$row['question_id']] = $row['question_id'];
indvdum@0
    87
			}
indvdum@0
    88
			$db->sql_freeresult($result);
indvdum@0
    89
		}
indvdum@0
    90
	
indvdum@0
    91
		// okay, if there is a confirm_id, we try to load that confirm's state. If not, we try to find one
indvdum@0
    92
		if (!$this->load_answer() && (!$this->load_confirm_id() || !$this->load_answer()))
indvdum@0
    93
		{
indvdum@0
    94
			// we have no valid confirm ID, better get ready to ask something
indvdum@0
    95
			$this->select_question();
indvdum@0
    96
		}
indvdum@0
    97
	}
indvdum@0
    98
indvdum@0
    99
	/**
indvdum@0
   100
	*  API function
indvdum@0
   101
	*/
indvdum@0
   102
	function &get_instance()
indvdum@0
   103
	{
indvdum@0
   104
		$instance =& new phpbb_captcha_qa();
indvdum@0
   105
indvdum@0
   106
		return $instance;
indvdum@0
   107
	}
indvdum@0
   108
indvdum@0
   109
	/**
indvdum@0
   110
	* See if the captcha has created its tables.
indvdum@0
   111
	*/
indvdum@0
   112
	function is_installed()
indvdum@0
   113
	{
indvdum@0
   114
		global $db, $phpbb_root_path, $phpEx;
indvdum@0
   115
indvdum@0
   116
		if (!class_exists('phpbb_db_tools'))
indvdum@0
   117
		{
indvdum@0
   118
			include("$phpbb_root_path/includes/db/db_tools.$phpEx");
indvdum@0
   119
		}
indvdum@0
   120
		$db_tool = new phpbb_db_tools($db);
indvdum@0
   121
indvdum@0
   122
		return $db_tool->sql_table_exists(CAPTCHA_QUESTIONS_TABLE);
indvdum@0
   123
	}
indvdum@0
   124
indvdum@0
   125
	/**
indvdum@0
   126
	*  API function - for the captcha to be available, it must have installed itself and there has to be at least one question in the board's default lang
indvdum@0
   127
	*/
indvdum@0
   128
	function is_available()
indvdum@0
   129
	{
indvdum@0
   130
		global $config, $db, $phpbb_root_path, $phpEx, $user;
indvdum@0
   131
indvdum@0
   132
		// load language file for pretty display in the ACP dropdown
indvdum@0
   133
		$user->add_lang('captcha_qa');
indvdum@0
   134
indvdum@0
   135
		if (!phpbb_captcha_qa::is_installed())
indvdum@0
   136
		{
indvdum@0
   137
			return false;
indvdum@0
   138
		}
indvdum@0
   139
indvdum@0
   140
		$sql = 'SELECT COUNT(question_id) AS question_count
indvdum@0
   141
			FROM ' . CAPTCHA_QUESTIONS_TABLE . "
indvdum@0
   142
			WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'";
indvdum@0
   143
		$result = $db->sql_query($sql);
indvdum@0
   144
		$row = $db->sql_fetchrow($result);
indvdum@0
   145
		$db->sql_freeresult($result);
indvdum@0
   146
indvdum@0
   147
		return ((bool) $row['question_count']);
indvdum@0
   148
	}
indvdum@0
   149
indvdum@0
   150
	/**
indvdum@0
   151
	*  API function
indvdum@0
   152
	*/
indvdum@0
   153
	function has_config()
indvdum@0
   154
	{
indvdum@0
   155
		return true;
indvdum@0
   156
	}
indvdum@0
   157
indvdum@0
   158
	/**
indvdum@0
   159
	*  API function
indvdum@0
   160
	*/
indvdum@0
   161
	function get_name()
indvdum@0
   162
	{
indvdum@0
   163
		return 'CAPTCHA_QA';
indvdum@0
   164
	}
indvdum@0
   165
indvdum@0
   166
	/**
indvdum@0
   167
	*  API function
indvdum@0
   168
	*/
indvdum@0
   169
	function get_class_name()
indvdum@0
   170
	{
indvdum@0
   171
		return 'phpbb_captcha_qa';
indvdum@0
   172
	}
indvdum@0
   173
indvdum@0
   174
	/**
indvdum@0
   175
	*  API function - not needed as we don't display an image
indvdum@0
   176
	*/
indvdum@0
   177
	function execute_demo()
indvdum@0
   178
	{
indvdum@0
   179
	}
indvdum@0
   180
indvdum@0
   181
	/**
indvdum@0
   182
	*  API function - not needed as we don't display an image
indvdum@0
   183
	*/
indvdum@0
   184
	function execute()
indvdum@0
   185
	{
indvdum@0
   186
	}
indvdum@0
   187
indvdum@0
   188
	/**
indvdum@0
   189
	*  API function - send the question to the template
indvdum@0
   190
	*/
indvdum@0
   191
	function get_template()
indvdum@0
   192
	{
indvdum@0
   193
		global $template;
indvdum@0
   194
indvdum@0
   195
		if ($this->is_solved())
indvdum@0
   196
		{
indvdum@0
   197
			return false;
indvdum@0
   198
		}
indvdum@0
   199
		else
indvdum@0
   200
		{
indvdum@0
   201
			$template->assign_vars(array(
indvdum@0
   202
				'QA_CONFIRM_QUESTION'		=> $this->question_text,
indvdum@0
   203
				'QA_CONFIRM_ID'				=> $this->confirm_id,
indvdum@0
   204
				'S_CONFIRM_CODE'			=> true,
indvdum@0
   205
				'S_TYPE'					=> $this->type,
indvdum@0
   206
			));
indvdum@0
   207
indvdum@0
   208
			return 'captcha_qa.html';
indvdum@0
   209
		}
indvdum@0
   210
	}
indvdum@0
   211
indvdum@0
   212
	/**
indvdum@0
   213
	*  API function - we just display a mockup so that the captcha doesn't need to be installed
indvdum@0
   214
	*/
indvdum@0
   215
	function get_demo_template()
indvdum@0
   216
	{
indvdum@0
   217
		global $config, $db, $template;
indvdum@0
   218
indvdum@0
   219
		if ($this->is_available())
indvdum@0
   220
		{
indvdum@0
   221
			$sql = 'SELECT question_text
indvdum@0
   222
				FROM ' . CAPTCHA_QUESTIONS_TABLE . "
indvdum@0
   223
				WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'";
indvdum@0
   224
			$result = $db->sql_query_limit($sql, 1);
indvdum@0
   225
			if ($row = $db->sql_fetchrow($result))
indvdum@0
   226
			{
indvdum@0
   227
				$template->assign_vars(array(
indvdum@0
   228
					'QA_CONFIRM_QUESTION'		=> $row['question_text'],
indvdum@0
   229
				));
indvdum@0
   230
			}
indvdum@0
   231
			$db->sql_freeresult($result);
indvdum@0
   232
		}
indvdum@0
   233
		return 'captcha_qa_acp_demo.html';
indvdum@0
   234
	}
indvdum@0
   235
indvdum@0
   236
	/**
indvdum@0
   237
	*  API function
indvdum@0
   238
	*/
indvdum@0
   239
	function get_hidden_fields()
indvdum@0
   240
	{
indvdum@0
   241
		$hidden_fields = array();
indvdum@0
   242
indvdum@0
   243
		// this is required - otherwise we would forget about the captcha being already solved
indvdum@0
   244
		if ($this->solved)
indvdum@0
   245
		{
indvdum@0
   246
			$hidden_fields['qa_answer'] = $this->answer;
indvdum@0
   247
		}
indvdum@0
   248
		$hidden_fields['qa_confirm_id'] = $this->confirm_id;
indvdum@0
   249
indvdum@0
   250
		return $hidden_fields;
indvdum@0
   251
	}
indvdum@0
   252
indvdum@0
   253
	/**
indvdum@0
   254
	*  API function
indvdum@0
   255
	*/
indvdum@0
   256
	function garbage_collect($type = 0)
indvdum@0
   257
	{
indvdum@0
   258
		global $db, $config;
indvdum@0
   259
indvdum@0
   260
		$sql = 'SELECT c.confirm_id
indvdum@0
   261
			FROM ' . CAPTCHA_QA_CONFIRM_TABLE . ' c
indvdum@0
   262
			LEFT JOIN ' . SESSIONS_TABLE . ' s
indvdum@0
   263
				ON (c.session_id = s.session_id)
indvdum@0
   264
			WHERE s.session_id IS NULL' .
indvdum@0
   265
				((empty($type)) ? '' : ' AND c.confirm_type = ' . (int) $type);
indvdum@0
   266
		$result = $db->sql_query($sql);
indvdum@0
   267
indvdum@0
   268
		if ($row = $db->sql_fetchrow($result))
indvdum@0
   269
		{
indvdum@0
   270
			$sql_in = array();
indvdum@0
   271
indvdum@0
   272
			do
indvdum@0
   273
			{
indvdum@0
   274
				$sql_in[] = (string) $row['confirm_id'];
indvdum@0
   275
			}
indvdum@0
   276
			while ($row = $db->sql_fetchrow($result));
indvdum@0
   277
indvdum@0
   278
			if (sizeof($sql_in))
indvdum@0
   279
			{
indvdum@0
   280
				$sql = 'DELETE FROM ' . CAPTCHA_QA_CONFIRM_TABLE . '
indvdum@0
   281
					WHERE ' . $db->sql_in_set('confirm_id', $sql_in);
indvdum@0
   282
				$db->sql_query($sql);
indvdum@0
   283
			}
indvdum@0
   284
		}
indvdum@0
   285
		$db->sql_freeresult($result);
indvdum@0
   286
	}
indvdum@0
   287
indvdum@0
   288
	/**
indvdum@0
   289
	*  API function - we don't drop the tables here, as that would cause the loss of all entered questions.
indvdum@0
   290
	*/
indvdum@0
   291
	function uninstall()
indvdum@0
   292
	{
indvdum@0
   293
		$this->garbage_collect(0);
indvdum@0
   294
	}
indvdum@0
   295
indvdum@0
   296
	/**
indvdum@0
   297
	*  API function - set up shop
indvdum@0
   298
	*/
indvdum@0
   299
	function install()
indvdum@0
   300
	{
indvdum@0
   301
		global $db, $phpbb_root_path, $phpEx;
indvdum@0
   302
indvdum@0
   303
		if (!class_exists('phpbb_db_tools'))
indvdum@0
   304
		{
indvdum@0
   305
			include("$phpbb_root_path/includes/db/db_tools.$phpEx");
indvdum@0
   306
		}
indvdum@0
   307
		$db_tool = new phpbb_db_tools($db);
indvdum@0
   308
indvdum@0
   309
		$tables = array(CAPTCHA_QUESTIONS_TABLE, CAPTCHA_ANSWERS_TABLE, CAPTCHA_QA_CONFIRM_TABLE);
indvdum@0
   310
indvdum@0
   311
		$schemas = array(
indvdum@0
   312
				CAPTCHA_QUESTIONS_TABLE		=> array (
indvdum@0
   313
					'COLUMNS' => array(
indvdum@0
   314
						'question_id'	=> array('UINT', Null, 'auto_increment'),
indvdum@0
   315
						'strict'		=> array('BOOL', 0),
indvdum@0
   316
						'lang_id'		=> array('UINT', 0),
indvdum@0
   317
						'lang_iso'		=> array('VCHAR:30', ''),
indvdum@0
   318
						'question_text'	=> array('TEXT_UNI', ''),
indvdum@0
   319
					),
indvdum@0
   320
					'PRIMARY_KEY'		=> 'question_id',
indvdum@0
   321
					'KEYS'				=> array(
indvdum@0
   322
						'lang'			=> array('INDEX', 'lang_iso'),
indvdum@0
   323
					),
indvdum@0
   324
				),
indvdum@0
   325
				CAPTCHA_ANSWERS_TABLE		=> array (
indvdum@0
   326
					'COLUMNS' => array(
indvdum@0
   327
						'question_id'	=> array('UINT', 0),
indvdum@0
   328
						'answer_text'	=> array('STEXT_UNI', ''),
indvdum@0
   329
					),
indvdum@0
   330
					'KEYS'				=> array(
indvdum@0
   331
						'qid'			=> array('INDEX', 'question_id'),
indvdum@0
   332
					),
indvdum@0
   333
				),
indvdum@0
   334
				CAPTCHA_QA_CONFIRM_TABLE		=> array (
indvdum@0
   335
					'COLUMNS' => array(
indvdum@0
   336
						'session_id'	=> array('CHAR:32', ''),
indvdum@0
   337
						'confirm_id'	=> array('CHAR:32', ''),
indvdum@0
   338
						'lang_iso'		=> array('VCHAR:30', ''),
indvdum@0
   339
						'question_id'	=> array('UINT', 0),
indvdum@0
   340
						'attempts'		=> array('UINT', 0),
indvdum@0
   341
						'confirm_type'	=> array('USINT', 0),
indvdum@0
   342
					),
indvdum@0
   343
					'KEYS'				=> array(
indvdum@0
   344
						'session_id'			=> array('INDEX', 'session_id'),
indvdum@0
   345
						'lookup'				=> array('INDEX', array('confirm_id', 'session_id', 'lang_iso')),
indvdum@0
   346
					),
indvdum@0
   347
					'PRIMARY_KEY'		=> 'confirm_id',
indvdum@0
   348
				),
indvdum@0
   349
		);
indvdum@0
   350
indvdum@0
   351
		foreach($schemas as $table => $schema)
indvdum@0
   352
		{
indvdum@0
   353
			if (!$db_tool->sql_table_exists($table))
indvdum@0
   354
			{
indvdum@0
   355
				$db_tool->sql_create_table($table, $schema);
indvdum@0
   356
			}
indvdum@0
   357
		}
indvdum@0
   358
	}
indvdum@0
   359
indvdum@0
   360
	/**
indvdum@0
   361
	*  API function - see what has to be done to validate
indvdum@0
   362
	*/
indvdum@0
   363
	function validate()
indvdum@0
   364
	{
indvdum@0
   365
		global $config, $db, $user;
indvdum@0
   366
indvdum@0
   367
		$error = '';
indvdum@0
   368
		
indvdum@0
   369
		if (!sizeof($this->question_ids))
indvdum@0
   370
		{
indvdum@0
   371
			return false;
indvdum@0
   372
		}
indvdum@0
   373
		
indvdum@0
   374
		if (!$this->confirm_id)
indvdum@0
   375
		{
indvdum@0
   376
			$error = $user->lang['CONFIRM_QUESTION_WRONG'];
indvdum@0
   377
		}
indvdum@0
   378
		else
indvdum@0
   379
		{
indvdum@0
   380
			if ($this->check_answer())
indvdum@0
   381
			{
indvdum@0
   382
				// $this->delete_code(); commented out to allow posting.php to repeat the question
indvdum@0
   383
				$this->solved = true;
indvdum@0
   384
			}
indvdum@0
   385
			else
indvdum@0
   386
			{
indvdum@0
   387
				$error = $user->lang['CONFIRM_QUESTION_WRONG'];
indvdum@0
   388
			}
indvdum@0
   389
		}
indvdum@0
   390
indvdum@0
   391
		if (strlen($error))
indvdum@0
   392
		{
indvdum@0
   393
			// okay, incorrect answer. Let's ask a new question.
indvdum@0
   394
			$this->new_attempt();
indvdum@0
   395
			$this->solved = false;
indvdum@0
   396
indvdum@0
   397
			return $error;
indvdum@0
   398
		}
indvdum@0
   399
		else
indvdum@0
   400
		{
indvdum@0
   401
			return false;
indvdum@0
   402
		}
indvdum@0
   403
	}
indvdum@0
   404
indvdum@0
   405
	/**
indvdum@0
   406
	*  Select a question
indvdum@0
   407
	*/
indvdum@0
   408
	function select_question()
indvdum@0
   409
	{
indvdum@0
   410
		global $db, $user;
indvdum@0
   411
indvdum@0
   412
		if (!sizeof($this->question_ids))
indvdum@0
   413
		{
indvdum@0
   414
			return false;
indvdum@0
   415
		}
indvdum@0
   416
		$this->confirm_id = md5(unique_id($user->ip));
indvdum@0
   417
		$this->question = (int) array_rand($this->question_ids);
indvdum@0
   418
indvdum@0
   419
		$sql = 'INSERT INTO ' . CAPTCHA_QA_CONFIRM_TABLE . ' ' . $db->sql_build_array('INSERT', array(
indvdum@0
   420
			'confirm_id'	=> (string) $this->confirm_id,
indvdum@0
   421
			'session_id'	=> (string) $user->session_id,
indvdum@0
   422
			'lang_iso'		=> (string) $this->question_lang,
indvdum@0
   423
			'confirm_type'	=> (int) $this->type,
indvdum@0
   424
			'question_id'	=> (int) $this->question,
indvdum@0
   425
		));
indvdum@0
   426
		$db->sql_query($sql);
indvdum@0
   427
indvdum@0
   428
		$this->load_answer();
indvdum@0
   429
	}
indvdum@0
   430
indvdum@0
   431
	/**
indvdum@0
   432
	* New Question, if desired.
indvdum@0
   433
	*/
indvdum@0
   434
	function reselect_question()
indvdum@0
   435
	{
indvdum@0
   436
		global $db, $user;
indvdum@0
   437
		
indvdum@0
   438
		if (!sizeof($this->question_ids))
indvdum@0
   439
		{
indvdum@0
   440
			return false;
indvdum@0
   441
		}
indvdum@0
   442
indvdum@0
   443
		$this->question = (int) array_rand($this->question_ids);
indvdum@0
   444
		$this->solved = 0;
indvdum@0
   445
indvdum@0
   446
		$sql = 'UPDATE ' . CAPTCHA_QA_CONFIRM_TABLE . '
indvdum@0
   447
			SET question_id = ' . (int) $this->question . "
indvdum@0
   448
			WHERE confirm_id = '" . $db->sql_escape($this->confirm_id) . "'
indvdum@0
   449
				AND session_id = '" . $db->sql_escape($user->session_id) . "'";
indvdum@0
   450
		$db->sql_query($sql);
indvdum@0
   451
indvdum@0
   452
		$this->load_answer();
indvdum@0
   453
	}
indvdum@0
   454
indvdum@0
   455
	/**
indvdum@0
   456
	* Wrong answer, so we increase the attempts and use a different question.
indvdum@0
   457
	*/
indvdum@0
   458
	function new_attempt()
indvdum@0
   459
	{
indvdum@0
   460
		global $db, $user;
indvdum@0
   461
indvdum@0
   462
		// yah, I would prefer a stronger rand, but this should work
indvdum@0
   463
		$this->question = (int) array_rand($this->question_ids);
indvdum@0
   464
		$this->solved = 0;
indvdum@0
   465
indvdum@0
   466
		$sql = 'UPDATE ' . CAPTCHA_QA_CONFIRM_TABLE . '
indvdum@0
   467
			SET question_id = ' . (int) $this->question . ",
indvdum@0
   468
				attempts = attempts + 1
indvdum@0
   469
			WHERE confirm_id = '" . $db->sql_escape($this->confirm_id) . "'
indvdum@0
   470
				AND session_id = '" . $db->sql_escape($user->session_id) . "'";
indvdum@0
   471
		$db->sql_query($sql);
indvdum@0
   472
indvdum@0
   473
		$this->load_answer();
indvdum@0
   474
	}
indvdum@0
   475
indvdum@0
   476
indvdum@0
   477
	/**
indvdum@0
   478
	* See if there is already an entry for the current session.
indvdum@0
   479
	*/
indvdum@0
   480
	function load_confirm_id()
indvdum@0
   481
	{
indvdum@0
   482
		global $db, $user;
indvdum@0
   483
indvdum@0
   484
		$sql = 'SELECT confirm_id
indvdum@0
   485
			FROM ' . CAPTCHA_QA_CONFIRM_TABLE . " 
indvdum@0
   486
			WHERE 
indvdum@0
   487
				session_id = '" . $db->sql_escape($user->session_id) . "'
indvdum@0
   488
				AND lang_iso = '" . $db->sql_escape($this->question_lang) . "'
indvdum@0
   489
				AND confirm_type = " . $this->type;
indvdum@0
   490
		$result = $db->sql_query_limit($sql, 1);
indvdum@0
   491
		$row = $db->sql_fetchrow($result);
indvdum@0
   492
		$db->sql_freeresult($result);
indvdum@0
   493
indvdum@0
   494
		if ($row)
indvdum@0
   495
		{
indvdum@0
   496
			$this->confirm_id = $row['confirm_id'];
indvdum@0
   497
			return true;
indvdum@0
   498
		}
indvdum@0
   499
		return false;
indvdum@0
   500
	}
indvdum@0
   501
indvdum@0
   502
	/**
indvdum@0
   503
	* Look up everything we need and populate the instance variables.
indvdum@0
   504
	*/
indvdum@0
   505
	function load_answer()
indvdum@0
   506
	{
indvdum@0
   507
		global $db, $user;
indvdum@0
   508
		
indvdum@0
   509
		if (!strlen($this->confirm_id) || !sizeof($this->question_ids))
indvdum@0
   510
		{
indvdum@0
   511
			return false;
indvdum@0
   512
		}
indvdum@0
   513
indvdum@0
   514
		$sql = 'SELECT con.question_id, attempts, question_text, strict
indvdum@0
   515
			FROM ' . CAPTCHA_QA_CONFIRM_TABLE . ' con, ' . CAPTCHA_QUESTIONS_TABLE . " qes
indvdum@0
   516
			WHERE con.question_id = qes.question_id
indvdum@0
   517
				AND confirm_id = '" . $db->sql_escape($this->confirm_id) . "'
indvdum@0
   518
				AND session_id = '" . $db->sql_escape($user->session_id) . "'
indvdum@0
   519
				AND qes.lang_iso = '" . $db->sql_escape($this->question_lang) . "'
indvdum@0
   520
				AND confirm_type = " . $this->type;
indvdum@0
   521
		$result = $db->sql_query($sql);
indvdum@0
   522
		$row = $db->sql_fetchrow($result);
indvdum@0
   523
		$db->sql_freeresult($result);
indvdum@0
   524
indvdum@0
   525
		if ($row)
indvdum@0
   526
		{
indvdum@0
   527
			$this->question = $row['question_id'];
indvdum@0
   528
indvdum@0
   529
			$this->attempts = $row['attempts'];
indvdum@0
   530
			$this->question_strict = $row['strict'];
indvdum@0
   531
			$this->question_text = $row['question_text'];
indvdum@0
   532
indvdum@0
   533
			return true;
indvdum@0
   534
		}
indvdum@0
   535
indvdum@0
   536
		return false;
indvdum@0
   537
	}
indvdum@0
   538
indvdum@0
   539
	/**
indvdum@0
   540
	*  The actual validation
indvdum@0
   541
	*/
indvdum@0
   542
	function check_answer()
indvdum@0
   543
	{
indvdum@0
   544
		global $db;
indvdum@0
   545
indvdum@0
   546
		$answer = ($this->question_strict) ? utf8_normalize_nfc(request_var('qa_answer', '', true)) : utf8_clean_string(utf8_normalize_nfc(request_var('qa_answer', '', true)));
indvdum@0
   547
indvdum@0
   548
		$sql = 'SELECT answer_text
indvdum@0
   549
			FROM ' . CAPTCHA_ANSWERS_TABLE . '
indvdum@0
   550
			WHERE question_id = ' . (int) $this->question;
indvdum@0
   551
		$result = $db->sql_query($sql);
indvdum@0
   552
indvdum@0
   553
		while ($row = $db->sql_fetchrow($result))
indvdum@0
   554
		{
indvdum@0
   555
			$solution = ($this->question_strict) ? $row['answer_text'] : utf8_clean_string($row['answer_text']);
indvdum@0
   556
indvdum@0
   557
			if ($solution === $answer)
indvdum@0
   558
			{
indvdum@0
   559
				$this->solved = true;
indvdum@0
   560
indvdum@0
   561
				break;
indvdum@0
   562
			}
indvdum@0
   563
		}
indvdum@0
   564
		$db->sql_freeresult($result);
indvdum@0
   565
indvdum@0
   566
		return $this->solved;
indvdum@0
   567
	}
indvdum@0
   568
indvdum@0
   569
	/**
indvdum@0
   570
	*  API function - clean the entry
indvdum@0
   571
	*/
indvdum@0
   572
	function delete_code()
indvdum@0
   573
	{
indvdum@0
   574
		global $db, $user;
indvdum@0
   575
indvdum@0
   576
		$sql = 'DELETE FROM ' . CAPTCHA_QA_CONFIRM_TABLE . "
indvdum@0
   577
			WHERE confirm_id = '" . $db->sql_escape($confirm_id) . "'
indvdum@0
   578
				AND session_id = '" . $db->sql_escape($user->session_id) . "'
indvdum@0
   579
				AND confirm_type = " . $this->type;
indvdum@0
   580
		$db->sql_query($sql);
indvdum@0
   581
	}
indvdum@0
   582
indvdum@0
   583
	/**
indvdum@0
   584
	*  API function
indvdum@0
   585
	*/
indvdum@0
   586
	function get_attempt_count()
indvdum@0
   587
	{
indvdum@0
   588
		return $this->attempts;
indvdum@0
   589
	}
indvdum@0
   590
indvdum@0
   591
	/**
indvdum@0
   592
	*  API function
indvdum@0
   593
	*/
indvdum@0
   594
	function reset()
indvdum@0
   595
	{
indvdum@0
   596
		global $db, $user;
indvdum@0
   597
indvdum@0
   598
		$sql = 'DELETE FROM ' . CAPTCHA_QA_CONFIRM_TABLE . "
indvdum@0
   599
			WHERE session_id = '" . $db->sql_escape($user->session_id) . "'
indvdum@0
   600
				AND confirm_type = " . (int) $this->type;
indvdum@0
   601
		$db->sql_query($sql);
indvdum@0
   602
indvdum@0
   603
		// we leave the class usable by generating a new question
indvdum@0
   604
		$this->select_question();
indvdum@0
   605
	}
indvdum@0
   606
indvdum@0
   607
	/**
indvdum@0
   608
	*  API function
indvdum@0
   609
	*/
indvdum@0
   610
	function is_solved()
indvdum@0
   611
	{
indvdum@0
   612
		if (request_var('qa_answer', false) && $this->solved === 0)
indvdum@0
   613
		{
indvdum@0
   614
			$this->validate();
indvdum@0
   615
		}
indvdum@0
   616
indvdum@0
   617
		return (bool) $this->solved;
indvdum@0
   618
	}
indvdum@0
   619
indvdum@0
   620
	/**
indvdum@0
   621
	*  API function - The ACP backend, this marks the end of the easy methods
indvdum@0
   622
	*/
indvdum@0
   623
	function acp_page($id, &$module)
indvdum@0
   624
	{
indvdum@0
   625
		global $db, $user, $auth, $template;
indvdum@0
   626
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
indvdum@0
   627
indvdum@0
   628
		$user->add_lang('acp/board');
indvdum@0
   629
		$user->add_lang('captcha_qa');
indvdum@0
   630
indvdum@0
   631
		if (!$this->is_installed())
indvdum@0
   632
		{
indvdum@0
   633
			$this->install();
indvdum@0
   634
		}
indvdum@0
   635
indvdum@0
   636
		$module->tpl_name = 'captcha_qa_acp';
indvdum@0
   637
		$module->page_title = 'ACP_VC_SETTINGS';
indvdum@0
   638
		$form_key = 'acp_captcha';
indvdum@0
   639
		add_form_key($form_key);
indvdum@0
   640
indvdum@0
   641
		$submit = request_var('submit', false);
indvdum@0
   642
		$question_id = request_var('question_id', 0);
indvdum@0
   643
		$action = request_var('action', '');
indvdum@0
   644
indvdum@0
   645
		// we have two pages, so users might want to navigate from one to the other
indvdum@0
   646
		$list_url = $module->u_action . "&amp;configure=1&amp;select_captcha=" . $this->get_class_name();
indvdum@0
   647
indvdum@0
   648
		$template->assign_vars(array(
indvdum@0
   649
			'U_ACTION'		=> $module->u_action,
indvdum@0
   650
			'QUESTION_ID'	=> $question_id ,
indvdum@0
   651
			'CLASS'			=> $this->get_class_name(),
indvdum@0
   652
		));
indvdum@0
   653
indvdum@0
   654
		// show the list?
indvdum@0
   655
		if (!$question_id && $action != 'add')
indvdum@0
   656
		{
indvdum@0
   657
			$this->acp_question_list($module);
indvdum@0
   658
		}
indvdum@0
   659
		else if ($question_id && $action == 'delete')
indvdum@0
   660
		{
indvdum@0
   661
			if ($this->get_class_name() !== $config['captcha_plugin'] || !$this->acp_is_last($question_id))
indvdum@0
   662
			{
indvdum@0
   663
				if (confirm_box(true))
indvdum@0
   664
				{
indvdum@0
   665
					$this->acp_delete_question($question_id);
indvdum@0
   666
indvdum@0
   667
					trigger_error($user->lang['QUESTION_DELETED'] . adm_back_link($list_url));
indvdum@0
   668
				}
indvdum@0
   669
				else
indvdum@0
   670
				{
indvdum@0
   671
					confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
indvdum@0
   672
						'question_id'		=> $question_id,
indvdum@0
   673
						'action'			=> $action,
indvdum@0
   674
						'configure'			=> 1,
indvdum@0
   675
						'select_captcha'	=> $this->get_class_name(),
indvdum@0
   676
						))
indvdum@0
   677
					);
indvdum@0
   678
				}
indvdum@0
   679
			}
indvdum@0
   680
			else
indvdum@0
   681
			{
indvdum@0
   682
				trigger_error($user->lang['QA_LAST_QUESTION'] . adm_back_link($list_url), E_USER_WARNING);
indvdum@0
   683
			}
indvdum@0
   684
		}
indvdum@0
   685
		else
indvdum@0
   686
		{
indvdum@0
   687
			// okay, show the editor
indvdum@0
   688
			$error = false;
indvdum@0
   689
			$input_question = request_var('question_text', '', true);
indvdum@0
   690
			$input_answers = request_var('answers', '', true);
indvdum@0
   691
			$input_lang = request_var('lang_iso', '', true);
indvdum@0
   692
			$input_strict = request_var('strict', false);
indvdum@0
   693
			$langs = $this->get_languages();
indvdum@0
   694
indvdum@0
   695
			foreach ($langs as $lang => $entry)
indvdum@0
   696
			{
indvdum@0
   697
				$template->assign_block_vars('langs', array(
indvdum@0
   698
					'ISO' => $lang,
indvdum@0
   699
					'NAME' => $entry['name'],
indvdum@0
   700
				));
indvdum@0
   701
			}
indvdum@0
   702
indvdum@0
   703
			$template->assign_vars(array(
indvdum@0
   704
				'U_LIST' => $list_url,
indvdum@0
   705
			));
indvdum@0
   706
indvdum@0
   707
			if ($question_id)
indvdum@0
   708
			{
indvdum@0
   709
				if ($question = $this->acp_get_question_data($question_id))
indvdum@0
   710
				{
indvdum@0
   711
					$answers = (isset($input_answers[$lang])) ? $input_answers[$lang] : implode("\n", $question['answers']);
indvdum@0
   712
indvdum@0
   713
					$template->assign_vars(array(
indvdum@0
   714
						'QUESTION_TEXT'		=> ($input_question) ? $input_question : $question['question_text'],
indvdum@0
   715
						'LANG_ISO'			=> ($input_lang) ? $input_lang : $question['lang_iso'],
indvdum@0
   716
						'STRICT'			=> (isset($_REQUEST['strict'])) ? $input_strict : $question['strict'],
indvdum@0
   717
						'ANSWERS'			=> $answers,
indvdum@0
   718
					));
indvdum@0
   719
				}
indvdum@0
   720
				else
indvdum@0
   721
				{
indvdum@0
   722
					trigger_error($user->lang['FORM_INVALID'] . adm_back_link($list_url));
indvdum@0
   723
				}
indvdum@0
   724
			}
indvdum@0
   725
			else
indvdum@0
   726
			{
indvdum@0
   727
				$template->assign_vars(array(
indvdum@0
   728
					'QUESTION_TEXT'		=> $input_question,
indvdum@0
   729
					'LANG_ISO'			=> $input_lang,
indvdum@0
   730
					'STRICT'			=> $input_strict,
indvdum@0
   731
					'ANSWERS'			=> $input_answers,
indvdum@0
   732
				));
indvdum@0
   733
			}
indvdum@0
   734
indvdum@0
   735
			if ($submit && check_form_key($form_key))
indvdum@0
   736
			{
indvdum@0
   737
				$data = $this->acp_get_question_input();
indvdum@0
   738
indvdum@0
   739
				if (!$this->validate_input($data))
indvdum@0
   740
				{
indvdum@0
   741
					$template->assign_vars(array(
indvdum@0
   742
						'S_ERROR'			=> true,
indvdum@0
   743
					));
indvdum@0
   744
				}
indvdum@0
   745
				else
indvdum@0
   746
				{
indvdum@0
   747
					if ($question_id)
indvdum@0
   748
					{
indvdum@0
   749
						$this->acp_update_question($data, $question_id);
indvdum@0
   750
					}
indvdum@0
   751
					else
indvdum@0
   752
					{
indvdum@0
   753
						$this->acp_add_question($data);
indvdum@0
   754
					}
indvdum@0
   755
indvdum@0
   756
					add_log('admin', 'LOG_CONFIG_VISUAL');
indvdum@0
   757
					trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($list_url));
indvdum@0
   758
				}
indvdum@0
   759
			}
indvdum@0
   760
			else if ($submit)
indvdum@0
   761
			{
indvdum@0
   762
				trigger_error($user->lang['FORM_INVALID'] . adm_back_link($list_url), E_USER_WARNING);
indvdum@0
   763
			}
indvdum@0
   764
		}
indvdum@0
   765
	}
indvdum@0
   766
indvdum@0
   767
	/**
indvdum@0
   768
	*  This handles the list overview
indvdum@0
   769
	*/
indvdum@0
   770
	function acp_question_list(&$module)
indvdum@0
   771
	{
indvdum@0
   772
		global $db, $template;
indvdum@0
   773
indvdum@0
   774
		$sql = 'SELECT *
indvdum@0
   775
			FROM ' . CAPTCHA_QUESTIONS_TABLE;
indvdum@0
   776
		$result = $db->sql_query($sql);
indvdum@0
   777
indvdum@0
   778
		$template->assign_vars(array(
indvdum@0
   779
			'S_LIST'			=> true,
indvdum@0
   780
		));
indvdum@0
   781
indvdum@0
   782
		while ($row = $db->sql_fetchrow($result))
indvdum@0
   783
		{
indvdum@0
   784
			$url = $module->u_action . "&amp;question_id={$row['question_id']}&amp;configure=1&amp;select_captcha=" . $this->get_class_name() . '&amp;';
indvdum@0
   785
indvdum@0
   786
			$template->assign_block_vars('questions', array(
indvdum@0
   787
				'QUESTION_TEXT'		=> $row['question_text'],
indvdum@0
   788
				'QUESTION_ID'		=> $row['question_id'],
indvdum@0
   789
				'QUESTION_LANG'		=> $row['lang_iso'],
indvdum@0
   790
				'U_DELETE'			=> "{$url}action=delete",
indvdum@0
   791
				'U_EDIT'			=> "{$url}action=edit",
indvdum@0
   792
			));
indvdum@0
   793
		}
indvdum@0
   794
		$db->sql_freeresult($result);
indvdum@0
   795
	}
indvdum@0
   796
indvdum@0
   797
	/**
indvdum@0
   798
	*  Grab a question and bring it into a format the editor understands
indvdum@0
   799
	*/
indvdum@0
   800
	function acp_get_question_data($question_id)
indvdum@0
   801
	{
indvdum@0
   802
		global $db;
indvdum@0
   803
indvdum@0
   804
		if ($question_id)
indvdum@0
   805
		{
indvdum@0
   806
			$sql = 'SELECT *
indvdum@0
   807
				FROM ' . CAPTCHA_QUESTIONS_TABLE . '
indvdum@0
   808
				WHERE question_id = ' . $question_id;
indvdum@0
   809
			$result = $db->sql_query($sql);
indvdum@0
   810
			$question = $db->sql_fetchrow($result);
indvdum@0
   811
			$db->sql_freeresult($result);
indvdum@0
   812
indvdum@0
   813
			if (!$question)
indvdum@0
   814
			{
indvdum@0
   815
				return false;
indvdum@0
   816
			}
indvdum@0
   817
indvdum@0
   818
			$question['answers'] = array();
indvdum@0
   819
indvdum@0
   820
			$sql = 'SELECT *
indvdum@0
   821
				FROM ' . CAPTCHA_ANSWERS_TABLE . '
indvdum@0
   822
				WHERE question_id = ' . $question_id;
indvdum@0
   823
			$result = $db->sql_query($sql);
indvdum@0
   824
indvdum@0
   825
			while ($row = $db->sql_fetchrow($result))
indvdum@0
   826
			{
indvdum@0
   827
				$question['answers'][] = $row['answer_text'];
indvdum@0
   828
			}
indvdum@0
   829
			$db->sql_freeresult($result);
indvdum@0
   830
indvdum@0
   831
			return $question;
indvdum@0
   832
		}
indvdum@0
   833
	}
indvdum@0
   834
indvdum@0
   835
	/**
indvdum@0
   836
	*  Grab a question from input and bring it into a format the editor understands
indvdum@0
   837
	*/
indvdum@0
   838
	function acp_get_question_input()
indvdum@0
   839
	{
indvdum@0
   840
		$answers = utf8_normalize_nfc(request_var('answers', '', true));
indvdum@0
   841
		$question = array(
indvdum@0
   842
			'question_text'	=> request_var('question_text', '', true),
indvdum@0
   843
			'strict'		=> request_var('strict', false),
indvdum@0
   844
			'lang_iso'		=> request_var('lang_iso', ''),
indvdum@0
   845
			'answers'		=> (strlen($answers)) ? explode("\n", $answers) : '',
indvdum@0
   846
		);
indvdum@0
   847
indvdum@0
   848
		return $question;
indvdum@0
   849
	}
indvdum@0
   850
indvdum@0
   851
	/**
indvdum@0
   852
	*  Update a question.
indvdum@0
   853
	* param mixed $data : an array as created from acp_get_question_input or acp_get_question_data
indvdum@0
   854
	*/
indvdum@0
   855
	function acp_update_question($data, $question_id)
indvdum@0
   856
	{
indvdum@0
   857
		global $db, $cache;
indvdum@0
   858
indvdum@0
   859
		// easier to delete all answers than to figure out which to update
indvdum@0
   860
		$sql = 'DELETE FROM ' . CAPTCHA_ANSWERS_TABLE . " WHERE question_id = $question_id";
indvdum@0
   861
		$db->sql_query($sql);
indvdum@0
   862
indvdum@0
   863
		$langs = $this->get_languages();
indvdum@0
   864
		$question_ary = $data;
indvdum@0
   865
		$question_ary['lang_id'] = $langs[$question_ary['lang_iso']]['id'];
indvdum@0
   866
		unset($question_ary['answers']);
indvdum@0
   867
indvdum@0
   868
		$sql = 'UPDATE ' . CAPTCHA_QUESTIONS_TABLE . '
indvdum@0
   869
			SET ' . $db->sql_build_array('UPDATE', $question_ary) . "
indvdum@0
   870
			WHERE question_id = $question_id";
indvdum@0
   871
		$db->sql_query($sql);
indvdum@0
   872
indvdum@0
   873
		$this->acp_insert_answers($data, $question_id);
indvdum@0
   874
indvdum@0
   875
		$cache->destroy('sql', CAPTCHA_QUESTIONS_TABLE);
indvdum@0
   876
	}
indvdum@0
   877
indvdum@0
   878
	/**
indvdum@0
   879
	*  Insert a question.
indvdum@0
   880
	* param mixed $data : an array as created from acp_get_question_input or acp_get_question_data
indvdum@0
   881
	*/
indvdum@0
   882
	function acp_add_question($data)
indvdum@0
   883
	{
indvdum@0
   884
		global $db, $cache;
indvdum@0
   885
indvdum@0
   886
		$langs = $this->get_languages();
indvdum@0
   887
		$question_ary = $data;
indvdum@0
   888
indvdum@0
   889
		$question_ary['lang_id'] = $langs[$data['lang_iso']]['id'];
indvdum@0
   890
		unset($question_ary['answers']);
indvdum@0
   891
indvdum@0
   892
		$sql = 'INSERT INTO ' . CAPTCHA_QUESTIONS_TABLE . ' ' . $db->sql_build_array('INSERT', $question_ary);
indvdum@0
   893
		$db->sql_query($sql);
indvdum@0
   894
indvdum@0
   895
		$question_id = $db->sql_nextid();
indvdum@0
   896
indvdum@0
   897
		$this->acp_insert_answers($data, $question_id);
indvdum@0
   898
indvdum@0
   899
		$cache->destroy('sql', CAPTCHA_QUESTIONS_TABLE);
indvdum@0
   900
	}
indvdum@0
   901
indvdum@0
   902
	/**
indvdum@0
   903
	*  Insert the answers.
indvdum@0
   904
	* param mixed $data : an array as created from acp_get_question_input or acp_get_question_data
indvdum@0
   905
	*/
indvdum@0
   906
	function acp_insert_answers($data, $question_id)
indvdum@0
   907
	{
indvdum@0
   908
		global $db, $cache;
indvdum@0
   909
indvdum@0
   910
		foreach ($data['answers'] as $answer)
indvdum@0
   911
		{
indvdum@0
   912
			$answer_ary = array(
indvdum@0
   913
				'question_id'	=> $question_id,
indvdum@0
   914
				'answer_text'	=> $answer,
indvdum@0
   915
			);
indvdum@0
   916
indvdum@0
   917
			$sql = 'INSERT INTO ' . CAPTCHA_ANSWERS_TABLE . ' ' . $db->sql_build_array('INSERT', $answer_ary);
indvdum@0
   918
			$db->sql_query($sql);
indvdum@0
   919
		}
indvdum@0
   920
indvdum@0
   921
		$cache->destroy('sql', CAPTCHA_ANSWERS_TABLE);
indvdum@0
   922
	}
indvdum@0
   923
indvdum@0
   924
	/**
indvdum@0
   925
	*  Delete a question.
indvdum@0
   926
	*/
indvdum@0
   927
	function acp_delete_question($question_id)
indvdum@0
   928
	{
indvdum@0
   929
		global $db, $cache;
indvdum@0
   930
indvdum@0
   931
		$tables = array(CAPTCHA_QUESTIONS_TABLE, CAPTCHA_ANSWERS_TABLE);
indvdum@0
   932
indvdum@0
   933
		foreach ($tables as $table)
indvdum@0
   934
		{
indvdum@0
   935
			$sql = "DELETE FROM $table
indvdum@0
   936
				WHERE question_id = $question_id";
indvdum@0
   937
			$db->sql_query($sql);
indvdum@0
   938
		}
indvdum@0
   939
indvdum@0
   940
		$cache->destroy('sql', $tables);
indvdum@0
   941
	}
indvdum@0
   942
indvdum@0
   943
	/**
indvdum@0
   944
	*  Check if the entered data can be inserted/used
indvdum@0
   945
	* param mixed $data : an array as created from acp_get_question_input or acp_get_question_data
indvdum@0
   946
	*/
indvdum@0
   947
	function validate_input($question_data)
indvdum@0
   948
	{
indvdum@0
   949
		$langs = $this->get_languages();
indvdum@0
   950
indvdum@0
   951
		if (!isset($question_data['lang_iso']) ||
indvdum@0
   952
			!isset($question_data['question_text']) ||
indvdum@0
   953
			!isset($question_data['strict']) ||
indvdum@0
   954
			!isset($question_data['answers']))
indvdum@0
   955
		{
indvdum@0
   956
			return false;
indvdum@0
   957
		}
indvdum@0
   958
indvdum@0
   959
		if (!isset($langs[$question_data['lang_iso']]) ||
indvdum@0
   960
			!strlen($question_data['question_text']) ||
indvdum@0
   961
			!sizeof($question_data['answers']) ||
indvdum@0
   962
			!is_array($question_data['answers']))
indvdum@0
   963
		{
indvdum@0
   964
			return false;
indvdum@0
   965
		}
indvdum@0
   966
indvdum@0
   967
		return true;
indvdum@0
   968
	}
indvdum@0
   969
indvdum@0
   970
	/**
indvdum@0
   971
	* List the installed language packs
indvdum@0
   972
	*/
indvdum@0
   973
	function get_languages()
indvdum@0
   974
	{
indvdum@0
   975
		global $db;
indvdum@0
   976
indvdum@0
   977
		$sql = 'SELECT *
indvdum@0
   978
			FROM ' . LANG_TABLE;
indvdum@0
   979
		$result = $db->sql_query($sql);
indvdum@0
   980
indvdum@0
   981
		$langs = array();
indvdum@0
   982
		while ($row = $db->sql_fetchrow($result))
indvdum@0
   983
		{
indvdum@0
   984
			$langs[$row['lang_iso']] = array(
indvdum@0
   985
				'name'	=> $row['lang_local_name'],
indvdum@0
   986
				'id'	=> (int) $row['lang_id'],
indvdum@0
   987
			);
indvdum@0
   988
		}
indvdum@0
   989
		$db->sql_freeresult($result);
indvdum@0
   990
indvdum@0
   991
		return $langs;
indvdum@0
   992
	}
indvdum@0
   993
	
indvdum@0
   994
	
indvdum@0
   995
	
indvdum@0
   996
	/**
indvdum@0
   997
	*  See if there is a question other than the one we have
indvdum@0
   998
	*/
indvdum@0
   999
	function acp_is_last($question_id)
indvdum@0
  1000
	{
indvdum@0
  1001
		global $config, $db;
indvdum@0
  1002
indvdum@0
  1003
		if ($question_id)
indvdum@0
  1004
		{
indvdum@0
  1005
			$sql = 'SELECT question_id
indvdum@0
  1006
				FROM ' . CAPTCHA_QUESTIONS_TABLE . "
indvdum@0
  1007
				WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'
indvdum@0
  1008
					AND  question_id <> " .  (int) $question_id;
indvdum@0
  1009
			$result = $db->sql_query_limit($sql, 1);
indvdum@0
  1010
			$question = $db->sql_fetchrow($result);
indvdum@0
  1011
			$db->sql_freeresult($result);
indvdum@0
  1012
indvdum@0
  1013
			if (!$question)
indvdum@0
  1014
			{
indvdum@0
  1015
				return true;
indvdum@0
  1016
			}
indvdum@0
  1017
			return false;
indvdum@0
  1018
		}
indvdum@0
  1019
	}
indvdum@0
  1020
}
indvdum@0
  1021
indvdum@0
  1022
?>