includes/captcha/plugins/phpbb_captcha_qa_plugin.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/captcha/plugins/phpbb_captcha_qa_plugin.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,1022 @@
     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 +global $table_prefix;
    1.23 +
    1.24 +define('CAPTCHA_QUESTIONS_TABLE',	$table_prefix . 'captcha_questions');
    1.25 +define('CAPTCHA_ANSWERS_TABLE',		$table_prefix . 'captcha_answers');
    1.26 +define('CAPTCHA_QA_CONFIRM_TABLE',	$table_prefix . 'qa_confirm');
    1.27 +
    1.28 +/**
    1.29 +* And now to something completely different. Let's make a captcha without extending the abstract class.
    1.30 +* QA CAPTCHA sample implementation
    1.31 +*
    1.32 +* @package VC
    1.33 +*/
    1.34 +class phpbb_captcha_qa
    1.35 +{
    1.36 +	var $confirm_id;
    1.37 +	var $answer;
    1.38 +	var $question_ids;
    1.39 +	var $question_text;
    1.40 +	var $question_lang;
    1.41 +	var $question_strict;
    1.42 +	var $attempts = 0;
    1.43 +	var $type;
    1.44 +	// dirty trick: 0 is false, but can still encode that the captcha is not yet validated
    1.45 +	var $solved = 0;
    1.46 +
    1.47 +	/**
    1.48 +	* @param int $type  as per the CAPTCHA API docs, the type
    1.49 +	*/
    1.50 +	function init($type)
    1.51 +	{
    1.52 +		global $config, $db, $user;
    1.53 +
    1.54 +		// load our language file
    1.55 +		$user->add_lang('captcha_qa');
    1.56 +
    1.57 +		// read input
    1.58 +		$this->confirm_id = request_var('qa_confirm_id', '');
    1.59 +		$this->answer = utf8_normalize_nfc(request_var('qa_answer', '', true));
    1.60 +
    1.61 +		$this->type = (int) $type;
    1.62 +		$this->question_lang = $user->lang_name;
    1.63 +
    1.64 +		// we need all defined questions - shouldn't be too many, so we can just grab them
    1.65 +		// try the user's lang first
    1.66 +		$sql = 'SELECT question_id
    1.67 +			FROM ' . CAPTCHA_QUESTIONS_TABLE . "
    1.68 +			WHERE lang_iso = '" . $db->sql_escape($user->lang_name) . "'";
    1.69 +		$result = $db->sql_query($sql, 3600);
    1.70 +
    1.71 +		while ($row = $db->sql_fetchrow($result))
    1.72 +		{
    1.73 +			$this->question_ids[$row['question_id']] = $row['question_id'];
    1.74 +		}
    1.75 +		$db->sql_freeresult($result);
    1.76 +
    1.77 +		// fallback to the board default lang
    1.78 +		if (!sizeof($this->question_ids))
    1.79 +		{
    1.80 +			$this->question_lang = $config['default_lang'];
    1.81 +
    1.82 +			$sql = 'SELECT question_id
    1.83 +				FROM ' . CAPTCHA_QUESTIONS_TABLE . "
    1.84 +				WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'";
    1.85 +			$result = $db->sql_query($sql, 7200);
    1.86 +
    1.87 +			while ($row = $db->sql_fetchrow($result))
    1.88 +			{
    1.89 +				$this->question_ids[$row['question_id']] = $row['question_id'];
    1.90 +			}
    1.91 +			$db->sql_freeresult($result);
    1.92 +		}
    1.93 +	
    1.94 +		// okay, if there is a confirm_id, we try to load that confirm's state. If not, we try to find one
    1.95 +		if (!$this->load_answer() && (!$this->load_confirm_id() || !$this->load_answer()))
    1.96 +		{
    1.97 +			// we have no valid confirm ID, better get ready to ask something
    1.98 +			$this->select_question();
    1.99 +		}
   1.100 +	}
   1.101 +
   1.102 +	/**
   1.103 +	*  API function
   1.104 +	*/
   1.105 +	function &get_instance()
   1.106 +	{
   1.107 +		$instance =& new phpbb_captcha_qa();
   1.108 +
   1.109 +		return $instance;
   1.110 +	}
   1.111 +
   1.112 +	/**
   1.113 +	* See if the captcha has created its tables.
   1.114 +	*/
   1.115 +	function is_installed()
   1.116 +	{
   1.117 +		global $db, $phpbb_root_path, $phpEx;
   1.118 +
   1.119 +		if (!class_exists('phpbb_db_tools'))
   1.120 +		{
   1.121 +			include("$phpbb_root_path/includes/db/db_tools.$phpEx");
   1.122 +		}
   1.123 +		$db_tool = new phpbb_db_tools($db);
   1.124 +
   1.125 +		return $db_tool->sql_table_exists(CAPTCHA_QUESTIONS_TABLE);
   1.126 +	}
   1.127 +
   1.128 +	/**
   1.129 +	*  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
   1.130 +	*/
   1.131 +	function is_available()
   1.132 +	{
   1.133 +		global $config, $db, $phpbb_root_path, $phpEx, $user;
   1.134 +
   1.135 +		// load language file for pretty display in the ACP dropdown
   1.136 +		$user->add_lang('captcha_qa');
   1.137 +
   1.138 +		if (!phpbb_captcha_qa::is_installed())
   1.139 +		{
   1.140 +			return false;
   1.141 +		}
   1.142 +
   1.143 +		$sql = 'SELECT COUNT(question_id) AS question_count
   1.144 +			FROM ' . CAPTCHA_QUESTIONS_TABLE . "
   1.145 +			WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'";
   1.146 +		$result = $db->sql_query($sql);
   1.147 +		$row = $db->sql_fetchrow($result);
   1.148 +		$db->sql_freeresult($result);
   1.149 +
   1.150 +		return ((bool) $row['question_count']);
   1.151 +	}
   1.152 +
   1.153 +	/**
   1.154 +	*  API function
   1.155 +	*/
   1.156 +	function has_config()
   1.157 +	{
   1.158 +		return true;
   1.159 +	}
   1.160 +
   1.161 +	/**
   1.162 +	*  API function
   1.163 +	*/
   1.164 +	function get_name()
   1.165 +	{
   1.166 +		return 'CAPTCHA_QA';
   1.167 +	}
   1.168 +
   1.169 +	/**
   1.170 +	*  API function
   1.171 +	*/
   1.172 +	function get_class_name()
   1.173 +	{
   1.174 +		return 'phpbb_captcha_qa';
   1.175 +	}
   1.176 +
   1.177 +	/**
   1.178 +	*  API function - not needed as we don't display an image
   1.179 +	*/
   1.180 +	function execute_demo()
   1.181 +	{
   1.182 +	}
   1.183 +
   1.184 +	/**
   1.185 +	*  API function - not needed as we don't display an image
   1.186 +	*/
   1.187 +	function execute()
   1.188 +	{
   1.189 +	}
   1.190 +
   1.191 +	/**
   1.192 +	*  API function - send the question to the template
   1.193 +	*/
   1.194 +	function get_template()
   1.195 +	{
   1.196 +		global $template;
   1.197 +
   1.198 +		if ($this->is_solved())
   1.199 +		{
   1.200 +			return false;
   1.201 +		}
   1.202 +		else
   1.203 +		{
   1.204 +			$template->assign_vars(array(
   1.205 +				'QA_CONFIRM_QUESTION'		=> $this->question_text,
   1.206 +				'QA_CONFIRM_ID'				=> $this->confirm_id,
   1.207 +				'S_CONFIRM_CODE'			=> true,
   1.208 +				'S_TYPE'					=> $this->type,
   1.209 +			));
   1.210 +
   1.211 +			return 'captcha_qa.html';
   1.212 +		}
   1.213 +	}
   1.214 +
   1.215 +	/**
   1.216 +	*  API function - we just display a mockup so that the captcha doesn't need to be installed
   1.217 +	*/
   1.218 +	function get_demo_template()
   1.219 +	{
   1.220 +		global $config, $db, $template;
   1.221 +
   1.222 +		if ($this->is_available())
   1.223 +		{
   1.224 +			$sql = 'SELECT question_text
   1.225 +				FROM ' . CAPTCHA_QUESTIONS_TABLE . "
   1.226 +				WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'";
   1.227 +			$result = $db->sql_query_limit($sql, 1);
   1.228 +			if ($row = $db->sql_fetchrow($result))
   1.229 +			{
   1.230 +				$template->assign_vars(array(
   1.231 +					'QA_CONFIRM_QUESTION'		=> $row['question_text'],
   1.232 +				));
   1.233 +			}
   1.234 +			$db->sql_freeresult($result);
   1.235 +		}
   1.236 +		return 'captcha_qa_acp_demo.html';
   1.237 +	}
   1.238 +
   1.239 +	/**
   1.240 +	*  API function
   1.241 +	*/
   1.242 +	function get_hidden_fields()
   1.243 +	{
   1.244 +		$hidden_fields = array();
   1.245 +
   1.246 +		// this is required - otherwise we would forget about the captcha being already solved
   1.247 +		if ($this->solved)
   1.248 +		{
   1.249 +			$hidden_fields['qa_answer'] = $this->answer;
   1.250 +		}
   1.251 +		$hidden_fields['qa_confirm_id'] = $this->confirm_id;
   1.252 +
   1.253 +		return $hidden_fields;
   1.254 +	}
   1.255 +
   1.256 +	/**
   1.257 +	*  API function
   1.258 +	*/
   1.259 +	function garbage_collect($type = 0)
   1.260 +	{
   1.261 +		global $db, $config;
   1.262 +
   1.263 +		$sql = 'SELECT c.confirm_id
   1.264 +			FROM ' . CAPTCHA_QA_CONFIRM_TABLE . ' c
   1.265 +			LEFT JOIN ' . SESSIONS_TABLE . ' s
   1.266 +				ON (c.session_id = s.session_id)
   1.267 +			WHERE s.session_id IS NULL' .
   1.268 +				((empty($type)) ? '' : ' AND c.confirm_type = ' . (int) $type);
   1.269 +		$result = $db->sql_query($sql);
   1.270 +
   1.271 +		if ($row = $db->sql_fetchrow($result))
   1.272 +		{
   1.273 +			$sql_in = array();
   1.274 +
   1.275 +			do
   1.276 +			{
   1.277 +				$sql_in[] = (string) $row['confirm_id'];
   1.278 +			}
   1.279 +			while ($row = $db->sql_fetchrow($result));
   1.280 +
   1.281 +			if (sizeof($sql_in))
   1.282 +			{
   1.283 +				$sql = 'DELETE FROM ' . CAPTCHA_QA_CONFIRM_TABLE . '
   1.284 +					WHERE ' . $db->sql_in_set('confirm_id', $sql_in);
   1.285 +				$db->sql_query($sql);
   1.286 +			}
   1.287 +		}
   1.288 +		$db->sql_freeresult($result);
   1.289 +	}
   1.290 +
   1.291 +	/**
   1.292 +	*  API function - we don't drop the tables here, as that would cause the loss of all entered questions.
   1.293 +	*/
   1.294 +	function uninstall()
   1.295 +	{
   1.296 +		$this->garbage_collect(0);
   1.297 +	}
   1.298 +
   1.299 +	/**
   1.300 +	*  API function - set up shop
   1.301 +	*/
   1.302 +	function install()
   1.303 +	{
   1.304 +		global $db, $phpbb_root_path, $phpEx;
   1.305 +
   1.306 +		if (!class_exists('phpbb_db_tools'))
   1.307 +		{
   1.308 +			include("$phpbb_root_path/includes/db/db_tools.$phpEx");
   1.309 +		}
   1.310 +		$db_tool = new phpbb_db_tools($db);
   1.311 +
   1.312 +		$tables = array(CAPTCHA_QUESTIONS_TABLE, CAPTCHA_ANSWERS_TABLE, CAPTCHA_QA_CONFIRM_TABLE);
   1.313 +
   1.314 +		$schemas = array(
   1.315 +				CAPTCHA_QUESTIONS_TABLE		=> array (
   1.316 +					'COLUMNS' => array(
   1.317 +						'question_id'	=> array('UINT', Null, 'auto_increment'),
   1.318 +						'strict'		=> array('BOOL', 0),
   1.319 +						'lang_id'		=> array('UINT', 0),
   1.320 +						'lang_iso'		=> array('VCHAR:30', ''),
   1.321 +						'question_text'	=> array('TEXT_UNI', ''),
   1.322 +					),
   1.323 +					'PRIMARY_KEY'		=> 'question_id',
   1.324 +					'KEYS'				=> array(
   1.325 +						'lang'			=> array('INDEX', 'lang_iso'),
   1.326 +					),
   1.327 +				),
   1.328 +				CAPTCHA_ANSWERS_TABLE		=> array (
   1.329 +					'COLUMNS' => array(
   1.330 +						'question_id'	=> array('UINT', 0),
   1.331 +						'answer_text'	=> array('STEXT_UNI', ''),
   1.332 +					),
   1.333 +					'KEYS'				=> array(
   1.334 +						'qid'			=> array('INDEX', 'question_id'),
   1.335 +					),
   1.336 +				),
   1.337 +				CAPTCHA_QA_CONFIRM_TABLE		=> array (
   1.338 +					'COLUMNS' => array(
   1.339 +						'session_id'	=> array('CHAR:32', ''),
   1.340 +						'confirm_id'	=> array('CHAR:32', ''),
   1.341 +						'lang_iso'		=> array('VCHAR:30', ''),
   1.342 +						'question_id'	=> array('UINT', 0),
   1.343 +						'attempts'		=> array('UINT', 0),
   1.344 +						'confirm_type'	=> array('USINT', 0),
   1.345 +					),
   1.346 +					'KEYS'				=> array(
   1.347 +						'session_id'			=> array('INDEX', 'session_id'),
   1.348 +						'lookup'				=> array('INDEX', array('confirm_id', 'session_id', 'lang_iso')),
   1.349 +					),
   1.350 +					'PRIMARY_KEY'		=> 'confirm_id',
   1.351 +				),
   1.352 +		);
   1.353 +
   1.354 +		foreach($schemas as $table => $schema)
   1.355 +		{
   1.356 +			if (!$db_tool->sql_table_exists($table))
   1.357 +			{
   1.358 +				$db_tool->sql_create_table($table, $schema);
   1.359 +			}
   1.360 +		}
   1.361 +	}
   1.362 +
   1.363 +	/**
   1.364 +	*  API function - see what has to be done to validate
   1.365 +	*/
   1.366 +	function validate()
   1.367 +	{
   1.368 +		global $config, $db, $user;
   1.369 +
   1.370 +		$error = '';
   1.371 +		
   1.372 +		if (!sizeof($this->question_ids))
   1.373 +		{
   1.374 +			return false;
   1.375 +		}
   1.376 +		
   1.377 +		if (!$this->confirm_id)
   1.378 +		{
   1.379 +			$error = $user->lang['CONFIRM_QUESTION_WRONG'];
   1.380 +		}
   1.381 +		else
   1.382 +		{
   1.383 +			if ($this->check_answer())
   1.384 +			{
   1.385 +				// $this->delete_code(); commented out to allow posting.php to repeat the question
   1.386 +				$this->solved = true;
   1.387 +			}
   1.388 +			else
   1.389 +			{
   1.390 +				$error = $user->lang['CONFIRM_QUESTION_WRONG'];
   1.391 +			}
   1.392 +		}
   1.393 +
   1.394 +		if (strlen($error))
   1.395 +		{
   1.396 +			// okay, incorrect answer. Let's ask a new question.
   1.397 +			$this->new_attempt();
   1.398 +			$this->solved = false;
   1.399 +
   1.400 +			return $error;
   1.401 +		}
   1.402 +		else
   1.403 +		{
   1.404 +			return false;
   1.405 +		}
   1.406 +	}
   1.407 +
   1.408 +	/**
   1.409 +	*  Select a question
   1.410 +	*/
   1.411 +	function select_question()
   1.412 +	{
   1.413 +		global $db, $user;
   1.414 +
   1.415 +		if (!sizeof($this->question_ids))
   1.416 +		{
   1.417 +			return false;
   1.418 +		}
   1.419 +		$this->confirm_id = md5(unique_id($user->ip));
   1.420 +		$this->question = (int) array_rand($this->question_ids);
   1.421 +
   1.422 +		$sql = 'INSERT INTO ' . CAPTCHA_QA_CONFIRM_TABLE . ' ' . $db->sql_build_array('INSERT', array(
   1.423 +			'confirm_id'	=> (string) $this->confirm_id,
   1.424 +			'session_id'	=> (string) $user->session_id,
   1.425 +			'lang_iso'		=> (string) $this->question_lang,
   1.426 +			'confirm_type'	=> (int) $this->type,
   1.427 +			'question_id'	=> (int) $this->question,
   1.428 +		));
   1.429 +		$db->sql_query($sql);
   1.430 +
   1.431 +		$this->load_answer();
   1.432 +	}
   1.433 +
   1.434 +	/**
   1.435 +	* New Question, if desired.
   1.436 +	*/
   1.437 +	function reselect_question()
   1.438 +	{
   1.439 +		global $db, $user;
   1.440 +		
   1.441 +		if (!sizeof($this->question_ids))
   1.442 +		{
   1.443 +			return false;
   1.444 +		}
   1.445 +
   1.446 +		$this->question = (int) array_rand($this->question_ids);
   1.447 +		$this->solved = 0;
   1.448 +
   1.449 +		$sql = 'UPDATE ' . CAPTCHA_QA_CONFIRM_TABLE . '
   1.450 +			SET question_id = ' . (int) $this->question . "
   1.451 +			WHERE confirm_id = '" . $db->sql_escape($this->confirm_id) . "'
   1.452 +				AND session_id = '" . $db->sql_escape($user->session_id) . "'";
   1.453 +		$db->sql_query($sql);
   1.454 +
   1.455 +		$this->load_answer();
   1.456 +	}
   1.457 +
   1.458 +	/**
   1.459 +	* Wrong answer, so we increase the attempts and use a different question.
   1.460 +	*/
   1.461 +	function new_attempt()
   1.462 +	{
   1.463 +		global $db, $user;
   1.464 +
   1.465 +		// yah, I would prefer a stronger rand, but this should work
   1.466 +		$this->question = (int) array_rand($this->question_ids);
   1.467 +		$this->solved = 0;
   1.468 +
   1.469 +		$sql = 'UPDATE ' . CAPTCHA_QA_CONFIRM_TABLE . '
   1.470 +			SET question_id = ' . (int) $this->question . ",
   1.471 +				attempts = attempts + 1
   1.472 +			WHERE confirm_id = '" . $db->sql_escape($this->confirm_id) . "'
   1.473 +				AND session_id = '" . $db->sql_escape($user->session_id) . "'";
   1.474 +		$db->sql_query($sql);
   1.475 +
   1.476 +		$this->load_answer();
   1.477 +	}
   1.478 +
   1.479 +
   1.480 +	/**
   1.481 +	* See if there is already an entry for the current session.
   1.482 +	*/
   1.483 +	function load_confirm_id()
   1.484 +	{
   1.485 +		global $db, $user;
   1.486 +
   1.487 +		$sql = 'SELECT confirm_id
   1.488 +			FROM ' . CAPTCHA_QA_CONFIRM_TABLE . " 
   1.489 +			WHERE 
   1.490 +				session_id = '" . $db->sql_escape($user->session_id) . "'
   1.491 +				AND lang_iso = '" . $db->sql_escape($this->question_lang) . "'
   1.492 +				AND confirm_type = " . $this->type;
   1.493 +		$result = $db->sql_query_limit($sql, 1);
   1.494 +		$row = $db->sql_fetchrow($result);
   1.495 +		$db->sql_freeresult($result);
   1.496 +
   1.497 +		if ($row)
   1.498 +		{
   1.499 +			$this->confirm_id = $row['confirm_id'];
   1.500 +			return true;
   1.501 +		}
   1.502 +		return false;
   1.503 +	}
   1.504 +
   1.505 +	/**
   1.506 +	* Look up everything we need and populate the instance variables.
   1.507 +	*/
   1.508 +	function load_answer()
   1.509 +	{
   1.510 +		global $db, $user;
   1.511 +		
   1.512 +		if (!strlen($this->confirm_id) || !sizeof($this->question_ids))
   1.513 +		{
   1.514 +			return false;
   1.515 +		}
   1.516 +
   1.517 +		$sql = 'SELECT con.question_id, attempts, question_text, strict
   1.518 +			FROM ' . CAPTCHA_QA_CONFIRM_TABLE . ' con, ' . CAPTCHA_QUESTIONS_TABLE . " qes
   1.519 +			WHERE con.question_id = qes.question_id
   1.520 +				AND confirm_id = '" . $db->sql_escape($this->confirm_id) . "'
   1.521 +				AND session_id = '" . $db->sql_escape($user->session_id) . "'
   1.522 +				AND qes.lang_iso = '" . $db->sql_escape($this->question_lang) . "'
   1.523 +				AND confirm_type = " . $this->type;
   1.524 +		$result = $db->sql_query($sql);
   1.525 +		$row = $db->sql_fetchrow($result);
   1.526 +		$db->sql_freeresult($result);
   1.527 +
   1.528 +		if ($row)
   1.529 +		{
   1.530 +			$this->question = $row['question_id'];
   1.531 +
   1.532 +			$this->attempts = $row['attempts'];
   1.533 +			$this->question_strict = $row['strict'];
   1.534 +			$this->question_text = $row['question_text'];
   1.535 +
   1.536 +			return true;
   1.537 +		}
   1.538 +
   1.539 +		return false;
   1.540 +	}
   1.541 +
   1.542 +	/**
   1.543 +	*  The actual validation
   1.544 +	*/
   1.545 +	function check_answer()
   1.546 +	{
   1.547 +		global $db;
   1.548 +
   1.549 +		$answer = ($this->question_strict) ? utf8_normalize_nfc(request_var('qa_answer', '', true)) : utf8_clean_string(utf8_normalize_nfc(request_var('qa_answer', '', true)));
   1.550 +
   1.551 +		$sql = 'SELECT answer_text
   1.552 +			FROM ' . CAPTCHA_ANSWERS_TABLE . '
   1.553 +			WHERE question_id = ' . (int) $this->question;
   1.554 +		$result = $db->sql_query($sql);
   1.555 +
   1.556 +		while ($row = $db->sql_fetchrow($result))
   1.557 +		{
   1.558 +			$solution = ($this->question_strict) ? $row['answer_text'] : utf8_clean_string($row['answer_text']);
   1.559 +
   1.560 +			if ($solution === $answer)
   1.561 +			{
   1.562 +				$this->solved = true;
   1.563 +
   1.564 +				break;
   1.565 +			}
   1.566 +		}
   1.567 +		$db->sql_freeresult($result);
   1.568 +
   1.569 +		return $this->solved;
   1.570 +	}
   1.571 +
   1.572 +	/**
   1.573 +	*  API function - clean the entry
   1.574 +	*/
   1.575 +	function delete_code()
   1.576 +	{
   1.577 +		global $db, $user;
   1.578 +
   1.579 +		$sql = 'DELETE FROM ' . CAPTCHA_QA_CONFIRM_TABLE . "
   1.580 +			WHERE confirm_id = '" . $db->sql_escape($confirm_id) . "'
   1.581 +				AND session_id = '" . $db->sql_escape($user->session_id) . "'
   1.582 +				AND confirm_type = " . $this->type;
   1.583 +		$db->sql_query($sql);
   1.584 +	}
   1.585 +
   1.586 +	/**
   1.587 +	*  API function
   1.588 +	*/
   1.589 +	function get_attempt_count()
   1.590 +	{
   1.591 +		return $this->attempts;
   1.592 +	}
   1.593 +
   1.594 +	/**
   1.595 +	*  API function
   1.596 +	*/
   1.597 +	function reset()
   1.598 +	{
   1.599 +		global $db, $user;
   1.600 +
   1.601 +		$sql = 'DELETE FROM ' . CAPTCHA_QA_CONFIRM_TABLE . "
   1.602 +			WHERE session_id = '" . $db->sql_escape($user->session_id) . "'
   1.603 +				AND confirm_type = " . (int) $this->type;
   1.604 +		$db->sql_query($sql);
   1.605 +
   1.606 +		// we leave the class usable by generating a new question
   1.607 +		$this->select_question();
   1.608 +	}
   1.609 +
   1.610 +	/**
   1.611 +	*  API function
   1.612 +	*/
   1.613 +	function is_solved()
   1.614 +	{
   1.615 +		if (request_var('qa_answer', false) && $this->solved === 0)
   1.616 +		{
   1.617 +			$this->validate();
   1.618 +		}
   1.619 +
   1.620 +		return (bool) $this->solved;
   1.621 +	}
   1.622 +
   1.623 +	/**
   1.624 +	*  API function - The ACP backend, this marks the end of the easy methods
   1.625 +	*/
   1.626 +	function acp_page($id, &$module)
   1.627 +	{
   1.628 +		global $db, $user, $auth, $template;
   1.629 +		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
   1.630 +
   1.631 +		$user->add_lang('acp/board');
   1.632 +		$user->add_lang('captcha_qa');
   1.633 +
   1.634 +		if (!$this->is_installed())
   1.635 +		{
   1.636 +			$this->install();
   1.637 +		}
   1.638 +
   1.639 +		$module->tpl_name = 'captcha_qa_acp';
   1.640 +		$module->page_title = 'ACP_VC_SETTINGS';
   1.641 +		$form_key = 'acp_captcha';
   1.642 +		add_form_key($form_key);
   1.643 +
   1.644 +		$submit = request_var('submit', false);
   1.645 +		$question_id = request_var('question_id', 0);
   1.646 +		$action = request_var('action', '');
   1.647 +
   1.648 +		// we have two pages, so users might want to navigate from one to the other
   1.649 +		$list_url = $module->u_action . "&amp;configure=1&amp;select_captcha=" . $this->get_class_name();
   1.650 +
   1.651 +		$template->assign_vars(array(
   1.652 +			'U_ACTION'		=> $module->u_action,
   1.653 +			'QUESTION_ID'	=> $question_id ,
   1.654 +			'CLASS'			=> $this->get_class_name(),
   1.655 +		));
   1.656 +
   1.657 +		// show the list?
   1.658 +		if (!$question_id && $action != 'add')
   1.659 +		{
   1.660 +			$this->acp_question_list($module);
   1.661 +		}
   1.662 +		else if ($question_id && $action == 'delete')
   1.663 +		{
   1.664 +			if ($this->get_class_name() !== $config['captcha_plugin'] || !$this->acp_is_last($question_id))
   1.665 +			{
   1.666 +				if (confirm_box(true))
   1.667 +				{
   1.668 +					$this->acp_delete_question($question_id);
   1.669 +
   1.670 +					trigger_error($user->lang['QUESTION_DELETED'] . adm_back_link($list_url));
   1.671 +				}
   1.672 +				else
   1.673 +				{
   1.674 +					confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
   1.675 +						'question_id'		=> $question_id,
   1.676 +						'action'			=> $action,
   1.677 +						'configure'			=> 1,
   1.678 +						'select_captcha'	=> $this->get_class_name(),
   1.679 +						))
   1.680 +					);
   1.681 +				}
   1.682 +			}
   1.683 +			else
   1.684 +			{
   1.685 +				trigger_error($user->lang['QA_LAST_QUESTION'] . adm_back_link($list_url), E_USER_WARNING);
   1.686 +			}
   1.687 +		}
   1.688 +		else
   1.689 +		{
   1.690 +			// okay, show the editor
   1.691 +			$error = false;
   1.692 +			$input_question = request_var('question_text', '', true);
   1.693 +			$input_answers = request_var('answers', '', true);
   1.694 +			$input_lang = request_var('lang_iso', '', true);
   1.695 +			$input_strict = request_var('strict', false);
   1.696 +			$langs = $this->get_languages();
   1.697 +
   1.698 +			foreach ($langs as $lang => $entry)
   1.699 +			{
   1.700 +				$template->assign_block_vars('langs', array(
   1.701 +					'ISO' => $lang,
   1.702 +					'NAME' => $entry['name'],
   1.703 +				));
   1.704 +			}
   1.705 +
   1.706 +			$template->assign_vars(array(
   1.707 +				'U_LIST' => $list_url,
   1.708 +			));
   1.709 +
   1.710 +			if ($question_id)
   1.711 +			{
   1.712 +				if ($question = $this->acp_get_question_data($question_id))
   1.713 +				{
   1.714 +					$answers = (isset($input_answers[$lang])) ? $input_answers[$lang] : implode("\n", $question['answers']);
   1.715 +
   1.716 +					$template->assign_vars(array(
   1.717 +						'QUESTION_TEXT'		=> ($input_question) ? $input_question : $question['question_text'],
   1.718 +						'LANG_ISO'			=> ($input_lang) ? $input_lang : $question['lang_iso'],
   1.719 +						'STRICT'			=> (isset($_REQUEST['strict'])) ? $input_strict : $question['strict'],
   1.720 +						'ANSWERS'			=> $answers,
   1.721 +					));
   1.722 +				}
   1.723 +				else
   1.724 +				{
   1.725 +					trigger_error($user->lang['FORM_INVALID'] . adm_back_link($list_url));
   1.726 +				}
   1.727 +			}
   1.728 +			else
   1.729 +			{
   1.730 +				$template->assign_vars(array(
   1.731 +					'QUESTION_TEXT'		=> $input_question,
   1.732 +					'LANG_ISO'			=> $input_lang,
   1.733 +					'STRICT'			=> $input_strict,
   1.734 +					'ANSWERS'			=> $input_answers,
   1.735 +				));
   1.736 +			}
   1.737 +
   1.738 +			if ($submit && check_form_key($form_key))
   1.739 +			{
   1.740 +				$data = $this->acp_get_question_input();
   1.741 +
   1.742 +				if (!$this->validate_input($data))
   1.743 +				{
   1.744 +					$template->assign_vars(array(
   1.745 +						'S_ERROR'			=> true,
   1.746 +					));
   1.747 +				}
   1.748 +				else
   1.749 +				{
   1.750 +					if ($question_id)
   1.751 +					{
   1.752 +						$this->acp_update_question($data, $question_id);
   1.753 +					}
   1.754 +					else
   1.755 +					{
   1.756 +						$this->acp_add_question($data);
   1.757 +					}
   1.758 +
   1.759 +					add_log('admin', 'LOG_CONFIG_VISUAL');
   1.760 +					trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($list_url));
   1.761 +				}
   1.762 +			}
   1.763 +			else if ($submit)
   1.764 +			{
   1.765 +				trigger_error($user->lang['FORM_INVALID'] . adm_back_link($list_url), E_USER_WARNING);
   1.766 +			}
   1.767 +		}
   1.768 +	}
   1.769 +
   1.770 +	/**
   1.771 +	*  This handles the list overview
   1.772 +	*/
   1.773 +	function acp_question_list(&$module)
   1.774 +	{
   1.775 +		global $db, $template;
   1.776 +
   1.777 +		$sql = 'SELECT *
   1.778 +			FROM ' . CAPTCHA_QUESTIONS_TABLE;
   1.779 +		$result = $db->sql_query($sql);
   1.780 +
   1.781 +		$template->assign_vars(array(
   1.782 +			'S_LIST'			=> true,
   1.783 +		));
   1.784 +
   1.785 +		while ($row = $db->sql_fetchrow($result))
   1.786 +		{
   1.787 +			$url = $module->u_action . "&amp;question_id={$row['question_id']}&amp;configure=1&amp;select_captcha=" . $this->get_class_name() . '&amp;';
   1.788 +
   1.789 +			$template->assign_block_vars('questions', array(
   1.790 +				'QUESTION_TEXT'		=> $row['question_text'],
   1.791 +				'QUESTION_ID'		=> $row['question_id'],
   1.792 +				'QUESTION_LANG'		=> $row['lang_iso'],
   1.793 +				'U_DELETE'			=> "{$url}action=delete",
   1.794 +				'U_EDIT'			=> "{$url}action=edit",
   1.795 +			));
   1.796 +		}
   1.797 +		$db->sql_freeresult($result);
   1.798 +	}
   1.799 +
   1.800 +	/**
   1.801 +	*  Grab a question and bring it into a format the editor understands
   1.802 +	*/
   1.803 +	function acp_get_question_data($question_id)
   1.804 +	{
   1.805 +		global $db;
   1.806 +
   1.807 +		if ($question_id)
   1.808 +		{
   1.809 +			$sql = 'SELECT *
   1.810 +				FROM ' . CAPTCHA_QUESTIONS_TABLE . '
   1.811 +				WHERE question_id = ' . $question_id;
   1.812 +			$result = $db->sql_query($sql);
   1.813 +			$question = $db->sql_fetchrow($result);
   1.814 +			$db->sql_freeresult($result);
   1.815 +
   1.816 +			if (!$question)
   1.817 +			{
   1.818 +				return false;
   1.819 +			}
   1.820 +
   1.821 +			$question['answers'] = array();
   1.822 +
   1.823 +			$sql = 'SELECT *
   1.824 +				FROM ' . CAPTCHA_ANSWERS_TABLE . '
   1.825 +				WHERE question_id = ' . $question_id;
   1.826 +			$result = $db->sql_query($sql);
   1.827 +
   1.828 +			while ($row = $db->sql_fetchrow($result))
   1.829 +			{
   1.830 +				$question['answers'][] = $row['answer_text'];
   1.831 +			}
   1.832 +			$db->sql_freeresult($result);
   1.833 +
   1.834 +			return $question;
   1.835 +		}
   1.836 +	}
   1.837 +
   1.838 +	/**
   1.839 +	*  Grab a question from input and bring it into a format the editor understands
   1.840 +	*/
   1.841 +	function acp_get_question_input()
   1.842 +	{
   1.843 +		$answers = utf8_normalize_nfc(request_var('answers', '', true));
   1.844 +		$question = array(
   1.845 +			'question_text'	=> request_var('question_text', '', true),
   1.846 +			'strict'		=> request_var('strict', false),
   1.847 +			'lang_iso'		=> request_var('lang_iso', ''),
   1.848 +			'answers'		=> (strlen($answers)) ? explode("\n", $answers) : '',
   1.849 +		);
   1.850 +
   1.851 +		return $question;
   1.852 +	}
   1.853 +
   1.854 +	/**
   1.855 +	*  Update a question.
   1.856 +	* param mixed $data : an array as created from acp_get_question_input or acp_get_question_data
   1.857 +	*/
   1.858 +	function acp_update_question($data, $question_id)
   1.859 +	{
   1.860 +		global $db, $cache;
   1.861 +
   1.862 +		// easier to delete all answers than to figure out which to update
   1.863 +		$sql = 'DELETE FROM ' . CAPTCHA_ANSWERS_TABLE . " WHERE question_id = $question_id";
   1.864 +		$db->sql_query($sql);
   1.865 +
   1.866 +		$langs = $this->get_languages();
   1.867 +		$question_ary = $data;
   1.868 +		$question_ary['lang_id'] = $langs[$question_ary['lang_iso']]['id'];
   1.869 +		unset($question_ary['answers']);
   1.870 +
   1.871 +		$sql = 'UPDATE ' . CAPTCHA_QUESTIONS_TABLE . '
   1.872 +			SET ' . $db->sql_build_array('UPDATE', $question_ary) . "
   1.873 +			WHERE question_id = $question_id";
   1.874 +		$db->sql_query($sql);
   1.875 +
   1.876 +		$this->acp_insert_answers($data, $question_id);
   1.877 +
   1.878 +		$cache->destroy('sql', CAPTCHA_QUESTIONS_TABLE);
   1.879 +	}
   1.880 +
   1.881 +	/**
   1.882 +	*  Insert a question.
   1.883 +	* param mixed $data : an array as created from acp_get_question_input or acp_get_question_data
   1.884 +	*/
   1.885 +	function acp_add_question($data)
   1.886 +	{
   1.887 +		global $db, $cache;
   1.888 +
   1.889 +		$langs = $this->get_languages();
   1.890 +		$question_ary = $data;
   1.891 +
   1.892 +		$question_ary['lang_id'] = $langs[$data['lang_iso']]['id'];
   1.893 +		unset($question_ary['answers']);
   1.894 +
   1.895 +		$sql = 'INSERT INTO ' . CAPTCHA_QUESTIONS_TABLE . ' ' . $db->sql_build_array('INSERT', $question_ary);
   1.896 +		$db->sql_query($sql);
   1.897 +
   1.898 +		$question_id = $db->sql_nextid();
   1.899 +
   1.900 +		$this->acp_insert_answers($data, $question_id);
   1.901 +
   1.902 +		$cache->destroy('sql', CAPTCHA_QUESTIONS_TABLE);
   1.903 +	}
   1.904 +
   1.905 +	/**
   1.906 +	*  Insert the answers.
   1.907 +	* param mixed $data : an array as created from acp_get_question_input or acp_get_question_data
   1.908 +	*/
   1.909 +	function acp_insert_answers($data, $question_id)
   1.910 +	{
   1.911 +		global $db, $cache;
   1.912 +
   1.913 +		foreach ($data['answers'] as $answer)
   1.914 +		{
   1.915 +			$answer_ary = array(
   1.916 +				'question_id'	=> $question_id,
   1.917 +				'answer_text'	=> $answer,
   1.918 +			);
   1.919 +
   1.920 +			$sql = 'INSERT INTO ' . CAPTCHA_ANSWERS_TABLE . ' ' . $db->sql_build_array('INSERT', $answer_ary);
   1.921 +			$db->sql_query($sql);
   1.922 +		}
   1.923 +
   1.924 +		$cache->destroy('sql', CAPTCHA_ANSWERS_TABLE);
   1.925 +	}
   1.926 +
   1.927 +	/**
   1.928 +	*  Delete a question.
   1.929 +	*/
   1.930 +	function acp_delete_question($question_id)
   1.931 +	{
   1.932 +		global $db, $cache;
   1.933 +
   1.934 +		$tables = array(CAPTCHA_QUESTIONS_TABLE, CAPTCHA_ANSWERS_TABLE);
   1.935 +
   1.936 +		foreach ($tables as $table)
   1.937 +		{
   1.938 +			$sql = "DELETE FROM $table
   1.939 +				WHERE question_id = $question_id";
   1.940 +			$db->sql_query($sql);
   1.941 +		}
   1.942 +
   1.943 +		$cache->destroy('sql', $tables);
   1.944 +	}
   1.945 +
   1.946 +	/**
   1.947 +	*  Check if the entered data can be inserted/used
   1.948 +	* param mixed $data : an array as created from acp_get_question_input or acp_get_question_data
   1.949 +	*/
   1.950 +	function validate_input($question_data)
   1.951 +	{
   1.952 +		$langs = $this->get_languages();
   1.953 +
   1.954 +		if (!isset($question_data['lang_iso']) ||
   1.955 +			!isset($question_data['question_text']) ||
   1.956 +			!isset($question_data['strict']) ||
   1.957 +			!isset($question_data['answers']))
   1.958 +		{
   1.959 +			return false;
   1.960 +		}
   1.961 +
   1.962 +		if (!isset($langs[$question_data['lang_iso']]) ||
   1.963 +			!strlen($question_data['question_text']) ||
   1.964 +			!sizeof($question_data['answers']) ||
   1.965 +			!is_array($question_data['answers']))
   1.966 +		{
   1.967 +			return false;
   1.968 +		}
   1.969 +
   1.970 +		return true;
   1.971 +	}
   1.972 +
   1.973 +	/**
   1.974 +	* List the installed language packs
   1.975 +	*/
   1.976 +	function get_languages()
   1.977 +	{
   1.978 +		global $db;
   1.979 +
   1.980 +		$sql = 'SELECT *
   1.981 +			FROM ' . LANG_TABLE;
   1.982 +		$result = $db->sql_query($sql);
   1.983 +
   1.984 +		$langs = array();
   1.985 +		while ($row = $db->sql_fetchrow($result))
   1.986 +		{
   1.987 +			$langs[$row['lang_iso']] = array(
   1.988 +				'name'	=> $row['lang_local_name'],
   1.989 +				'id'	=> (int) $row['lang_id'],
   1.990 +			);
   1.991 +		}
   1.992 +		$db->sql_freeresult($result);
   1.993 +
   1.994 +		return $langs;
   1.995 +	}
   1.996 +	
   1.997 +	
   1.998 +	
   1.999 +	/**
  1.1000 +	*  See if there is a question other than the one we have
  1.1001 +	*/
  1.1002 +	function acp_is_last($question_id)
  1.1003 +	{
  1.1004 +		global $config, $db;
  1.1005 +
  1.1006 +		if ($question_id)
  1.1007 +		{
  1.1008 +			$sql = 'SELECT question_id
  1.1009 +				FROM ' . CAPTCHA_QUESTIONS_TABLE . "
  1.1010 +				WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'
  1.1011 +					AND  question_id <> " .  (int) $question_id;
  1.1012 +			$result = $db->sql_query_limit($sql, 1);
  1.1013 +			$question = $db->sql_fetchrow($result);
  1.1014 +			$db->sql_freeresult($result);
  1.1015 +
  1.1016 +			if (!$question)
  1.1017 +			{
  1.1018 +				return true;
  1.1019 +			}
  1.1020 +			return false;
  1.1021 +		}
  1.1022 +	}
  1.1023 +}
  1.1024 +
  1.1025 +?>
  1.1026 \ No newline at end of file