1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-05-31 19:49:20 +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:
Andreas Fischer 2009-09-03 12:45:46 +00:00
parent 5b004f31b5
commit 46e66c0401

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