mirror of
https://github.com/phpbb/phpbb.git
synced 2025-05-07 16:15:22 +02:00
Some cleanup.
git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@10091 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
parent
5b004f31b5
commit
46e66c0401
@ -17,12 +17,11 @@ if (!defined('IN_PHPBB'))
|
||||
}
|
||||
|
||||
global $table_prefix;
|
||||
|
||||
define('CAPTCHA_QUESTIONS_TABLE', $table_prefix . 'captcha_questions');
|
||||
define('CAPTCHA_ANSWERS_TABLE', $table_prefix . 'captcha_answers');
|
||||
define('CAPTCHA_QA_CONFIRM_TABLE', $table_prefix . 'qa_confirm');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* And now to something completely different. Let's make a captcha without extending the abstract class.
|
||||
* QA CAPTCHA sample implementation
|
||||
@ -51,27 +50,37 @@ class phpbb_captcha_qa
|
||||
|
||||
// load our language file
|
||||
$user->add_lang('captcha_qa');
|
||||
|
||||
// read input
|
||||
$this->confirm_id = request_var('qa_confirm_id', '');
|
||||
$this->answer = request_var('qa_answer', '', true);
|
||||
|
||||
$this->type = (int) $type;
|
||||
$this->question_lang = $user->data['user_lang'];
|
||||
|
||||
// we need all defined questions - shouldn't be too many, so we can just grab them
|
||||
// try the user's lang first
|
||||
$sql = 'SELECT question_id FROM ' . CAPTCHA_QUESTIONS_TABLE . ' WHERE lang_iso = \'' . $db->sql_escape($user->data['user_lang']) . '\'';
|
||||
$sql = 'SELECT question_id
|
||||
FROM ' . CAPTCHA_QUESTIONS_TABLE . "
|
||||
WHERE lang_iso = '" . $db->sql_escape($user->data['user_lang']) . "'";
|
||||
$result = $db->sql_query($sql, 3600);
|
||||
|
||||
while ($row = $db->sql_fetchrow($result))
|
||||
{
|
||||
$this->question_ids[$row['question_id']] = $row['question_id'];
|
||||
}
|
||||
$db->sql_freeresult($result);
|
||||
|
||||
// fallback to the board default lang
|
||||
if (!sizeof($this->question_ids))
|
||||
{
|
||||
$this->question_lang = $config['default_lang'];
|
||||
$sql = 'SELECT question_id FROM ' . CAPTCHA_QUESTIONS_TABLE . ' WHERE lang_iso = \'' . $db->sql_escape($config['default_lang']) . '\'';
|
||||
|
||||
$sql = 'SELECT question_id
|
||||
FROM ' . CAPTCHA_QUESTIONS_TABLE . "
|
||||
WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'";
|
||||
$result = $db->sql_query($sql, 7200);
|
||||
|
||||
while ($row = $db->sql_fetchrow($result))
|
||||
{
|
||||
$this->question_ids[$row['question_id']] = $row['question_id'];
|
||||
@ -93,6 +102,7 @@ class phpbb_captcha_qa
|
||||
function &get_instance()
|
||||
{
|
||||
$instance =& new phpbb_captcha_qa();
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
@ -108,6 +118,7 @@ class phpbb_captcha_qa
|
||||
include("$phpbb_root_path/includes/db/db_tools.$phpEx");
|
||||
}
|
||||
$db_tool = new phpbb_db_tools($db);
|
||||
|
||||
return $db_tool->sql_table_exists(CAPTCHA_QUESTIONS_TABLE);
|
||||
}
|
||||
|
||||
@ -125,14 +136,17 @@ class phpbb_captcha_qa
|
||||
{
|
||||
return false;
|
||||
}
|
||||
$sql = 'SELECT COUNT(question_id) as count FROM ' . CAPTCHA_QUESTIONS_TABLE . ' WHERE lang_iso = \'' . $db->sql_escape($config['default_lang']) . '\'';
|
||||
|
||||
$sql = 'SELECT COUNT(question_id) as count
|
||||
FROM ' . CAPTCHA_QUESTIONS_TABLE . "
|
||||
WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'";
|
||||
$result = $db->sql_query($sql);
|
||||
$row = $db->sql_fetchrow($result);
|
||||
$db->sql_freeresult($result);
|
||||
|
||||
return ((bool) $row['count']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* API function
|
||||
*/
|
||||
@ -141,7 +155,6 @@ class phpbb_captcha_qa
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* API function
|
||||
*/
|
||||
@ -158,7 +171,6 @@ class phpbb_captcha_qa
|
||||
return 'phpbb_captcha_qa';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* API function - not needed as we don't display an image
|
||||
*/
|
||||
@ -218,6 +230,7 @@ class phpbb_captcha_qa
|
||||
$hidden_fields['qa_answer'] = $this->answer;
|
||||
}
|
||||
$hidden_fields['qa_confirm_id'] = $this->confirm_id;
|
||||
|
||||
return $hidden_fields;
|
||||
}
|
||||
|
||||
@ -230,7 +243,8 @@ class phpbb_captcha_qa
|
||||
|
||||
$sql = 'SELECT DISTINCT c.session_id
|
||||
FROM ' . CAPTCHA_QA_CONFIRM_TABLE . ' c
|
||||
LEFT JOIN ' . SESSIONS_TABLE . ' s ON (c.session_id = s.session_id)
|
||||
LEFT JOIN ' . SESSIONS_TABLE . ' s
|
||||
ON (c.session_id = s.session_id)
|
||||
WHERE s.session_id IS NULL' .
|
||||
((empty($type)) ? '' : ' AND c.confirm_type = ' . (int) $type);
|
||||
$result = $db->sql_query($sql);
|
||||
@ -238,6 +252,7 @@ class phpbb_captcha_qa
|
||||
if ($row = $db->sql_fetchrow($result))
|
||||
{
|
||||
$sql_in = array();
|
||||
|
||||
do
|
||||
{
|
||||
$sql_in[] = (string) $row['session_id'];
|
||||
@ -274,6 +289,7 @@ class phpbb_captcha_qa
|
||||
include("$phpbb_root_path/includes/db/db_tools.$phpEx");
|
||||
}
|
||||
$db_tool = new phpbb_db_tools($db);
|
||||
|
||||
$tables = array(CAPTCHA_QUESTIONS_TABLE, CAPTCHA_ANSWERS_TABLE, CAPTCHA_QA_CONFIRM_TABLE);
|
||||
|
||||
$schemas = array(
|
||||
@ -325,7 +341,6 @@ class phpbb_captcha_qa
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* API function - see what has to be done to validate
|
||||
*/
|
||||
@ -334,6 +349,7 @@ class phpbb_captcha_qa
|
||||
global $config, $db, $user;
|
||||
|
||||
$error = '';
|
||||
|
||||
if (!$this->confirm_id)
|
||||
{
|
||||
$error = $user->lang['CONFIRM_QUESTION_WRONG'];
|
||||
@ -356,6 +372,7 @@ class phpbb_captcha_qa
|
||||
// okay, incorrect answer. Let's ask a new question.
|
||||
$this->new_attempt();
|
||||
$this->solved = false;
|
||||
|
||||
return $error;
|
||||
}
|
||||
else
|
||||
@ -382,8 +399,8 @@ class phpbb_captcha_qa
|
||||
'question_id' => (int) $this->question,
|
||||
));
|
||||
$db->sql_query($sql);
|
||||
$this->load_answer();
|
||||
|
||||
$this->load_answer();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -395,14 +412,13 @@ class phpbb_captcha_qa
|
||||
|
||||
$this->question = (int) array_rand($this->question_ids);
|
||||
$this->solved = 0;
|
||||
// compute $seed % 0x7fffffff
|
||||
|
||||
$sql = 'UPDATE ' . CAPTCHA_QA_CONFIRM_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
|
||||
'question' => (int) $this->question,)) . '
|
||||
WHERE
|
||||
confirm_id = \'' . $db->sql_escape($this->confirm_id) . '\'
|
||||
AND session_id = \'' . $db->sql_escape($user->session_id) . '\'';
|
||||
$sql = 'UPDATE ' . CAPTCHA_QA_CONFIRM_TABLE . '
|
||||
SET question_id = ' . (int) $this->question . "
|
||||
WHERE confirm_id = '" . $db->sql_escape($this->confirm_id) . "'
|
||||
AND session_id = '" . $db->sql_escape($user->session_id) . "'";
|
||||
$db->sql_query($sql);
|
||||
|
||||
$this->load_answer();
|
||||
}
|
||||
|
||||
@ -416,15 +432,14 @@ class phpbb_captcha_qa
|
||||
// yah, I would prefer a stronger rand, but this should work
|
||||
$this->question = (int) array_rand($this->question_ids);
|
||||
$this->solved = 0;
|
||||
// compute $seed % 0x7fffffff
|
||||
|
||||
$sql = 'UPDATE ' . CAPTCHA_QA_CONFIRM_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
|
||||
'question_id' => (int) $this->question)) . ',
|
||||
$sql = 'UPDATE ' . CAPTCHA_QA_CONFIRM_TABLE . '
|
||||
SET question_id = ' . (int) $this->question . ",
|
||||
attempts = attempts + 1
|
||||
WHERE
|
||||
confirm_id = \'' . $db->sql_escape($this->confirm_id) . '\'
|
||||
AND session_id = \'' . $db->sql_escape($user->session_id) . '\'';
|
||||
WHERE confirm_id = '" . $db->sql_escape($this->confirm_id) . "'
|
||||
AND session_id = '" . $db->sql_escape($user->session_id) . "'";
|
||||
$db->sql_query($sql);
|
||||
|
||||
$this->load_answer();
|
||||
}
|
||||
|
||||
@ -453,8 +468,10 @@ class phpbb_captcha_qa
|
||||
$this->attempts = $row['attempts'];
|
||||
$this->question_strict = $row['strict'];
|
||||
$this->question_text = $row['question_text'];
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -471,16 +488,20 @@ class phpbb_captcha_qa
|
||||
FROM ' . CAPTCHA_ANSWERS_TABLE . '
|
||||
WHERE question_id = ' . (int) $this->question;
|
||||
$result = $db->sql_query($sql);
|
||||
|
||||
while ($row = $db->sql_fetchrow($result))
|
||||
{
|
||||
$solution = ($this->question_strict) ? $row['answer_text'] : utf8_clean_string($row['answer_text'] );
|
||||
$solution = ($this->question_strict) ? $row['answer_text'] : utf8_clean_string($row['answer_text']);
|
||||
|
||||
if ($solution === $answer)
|
||||
{
|
||||
$this->solved = true;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
$db->sql_freeresult($result);
|
||||
|
||||
return $this->solved;
|
||||
}
|
||||
|
||||
@ -531,10 +552,10 @@ class phpbb_captcha_qa
|
||||
{
|
||||
$this->validate();
|
||||
}
|
||||
|
||||
return (bool) $this->solved;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* API function - The ACP backend, this marks the end of the easy methods
|
||||
*/
|
||||
@ -550,6 +571,7 @@ class phpbb_captcha_qa
|
||||
{
|
||||
$this->install();
|
||||
}
|
||||
|
||||
$module->tpl_name = 'captcha_qa_acp';
|
||||
$module->page_title = 'ACP_VC_SETTINGS';
|
||||
$form_key = 'acp_captcha';
|
||||
@ -578,6 +600,7 @@ class phpbb_captcha_qa
|
||||
if (confirm_box(true))
|
||||
{
|
||||
$this->acp_delete_question($question_id);
|
||||
|
||||
trigger_error($user->lang['QUESTION_DELETED'] . adm_back_link($list_url));
|
||||
}
|
||||
else
|
||||
@ -600,6 +623,7 @@ class phpbb_captcha_qa
|
||||
$input_lang = request_var('lang_iso', '', true);
|
||||
$input_strict = request_var('strict', false);
|
||||
$langs = $this->get_languages();
|
||||
|
||||
foreach ($langs as $lang => $entry)
|
||||
{
|
||||
$template->assign_block_vars('langs', array(
|
||||
@ -611,11 +635,13 @@ class phpbb_captcha_qa
|
||||
$template->assign_vars(array(
|
||||
'U_LIST' => $list_url,
|
||||
));
|
||||
|
||||
if ($question_id)
|
||||
{
|
||||
if ($question = $this->acp_get_question_data($question_id))
|
||||
{
|
||||
$answers = (isset($input_answers[$lang])) ? $input_answers[$lang] : implode("\n", $question['answers']);
|
||||
|
||||
$template->assign_vars(array(
|
||||
'QUESTION_TEXT' => ($input_question) ? $input_question : $question['question_text'],
|
||||
'LANG_ISO' => ($input_lang) ? $input_lang : $question['lang_iso'],
|
||||
@ -630,7 +656,6 @@ class phpbb_captcha_qa
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
$template->assign_vars(array(
|
||||
'QUESTION_TEXT' => $input_question,
|
||||
'LANG_ISO' => $input_lang,
|
||||
@ -642,6 +667,7 @@ class phpbb_captcha_qa
|
||||
if ($submit && check_form_key($form_key))
|
||||
{
|
||||
$data = $this->acp_get_question_input();
|
||||
|
||||
if (!$this->validate_input($data))
|
||||
{
|
||||
$template->assign_vars(array(
|
||||
@ -669,7 +695,6 @@ class phpbb_captcha_qa
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This handles the list overview
|
||||
*/
|
||||
@ -738,7 +763,6 @@ class phpbb_captcha_qa
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Grab a question from input and bring it into a format the editor understands
|
||||
*/
|
||||
@ -795,7 +819,7 @@ class phpbb_captcha_qa
|
||||
$question_ary['lang_id'] = $langs[$data['lang_iso']]['id'];
|
||||
unset($question_ary['answers']);
|
||||
|
||||
$sql = 'INSERT INTO ' . CAPTCHA_QUESTIONS_TABLE . $db->sql_build_array('INSERT', $question_ary);
|
||||
$sql = 'INSERT INTO ' . CAPTCHA_QUESTIONS_TABLE . ' ' . $db->sql_build_array('INSERT', $question_ary);
|
||||
$db->sql_query($sql);
|
||||
|
||||
$question_id = $db->sql_nextid();
|
||||
@ -820,14 +844,13 @@ class phpbb_captcha_qa
|
||||
'answer_text' => $answer,
|
||||
);
|
||||
|
||||
$sql = 'INSERT INTO ' . CAPTCHA_ANSWERS_TABLE . $db->sql_build_array('INSERT', $answer_ary);
|
||||
$sql = 'INSERT INTO ' . CAPTCHA_ANSWERS_TABLE . ' ' . $db->sql_build_array('INSERT', $answer_ary);
|
||||
$db->sql_query($sql);
|
||||
}
|
||||
|
||||
$cache->destroy('sql', CAPTCHA_ANSWERS_TABLE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete a question.
|
||||
*/
|
||||
@ -847,7 +870,6 @@ class phpbb_captcha_qa
|
||||
$cache->destroy('sql', $tables);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if the entered data can be inserted/used
|
||||
* param mixed $data : an array as created from acp_get_question_input or acp_get_question_data
|
||||
@ -881,13 +903,11 @@ class phpbb_captcha_qa
|
||||
{
|
||||
global $db;
|
||||
|
||||
$langs = array();
|
||||
|
||||
$sql = 'SELECT *
|
||||
FROM ' . LANG_TABLE;
|
||||
|
||||
$result = $db->sql_query($sql);
|
||||
|
||||
$langs = array();
|
||||
while ($row = $db->sql_fetchrow($result))
|
||||
{
|
||||
$langs[$row['lang_iso']] = array(
|
||||
@ -899,7 +919,6 @@ class phpbb_captcha_qa
|
||||
|
||||
return $langs;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user