From: Andreas F. <ba...@ph...> - 2009-09-03 12:47:03
|
Author: bantu Date: Thu Sep 3 13:45:46 2009 New Revision: 10091 Log: Some cleanup. Modified: branches/phpBB-3_0_0/phpBB/includes/captcha/plugins/phpbb_captcha_qa_plugin.php Modified: branches/phpBB-3_0_0/phpBB/includes/captcha/plugins/phpbb_captcha_qa_plugin.php ============================================================================== *** branches/phpBB-3_0_0/phpBB/includes/captcha/plugins/phpbb_captcha_qa_plugin.php (original) --- branches/phpBB-3_0_0/phpBB/includes/captcha/plugins/phpbb_captcha_qa_plugin.php Thu Sep 3 13:45:46 2009 *************** *** 17,28 **** } 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 --- 17,27 ---- } 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,77 **** // 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']) . '\''; $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']) . '\''; $result = $db->sql_query($sql, 7200); while ($row = $db->sql_fetchrow($result)) { $this->question_ids[$row['question_id']] = $row['question_id']; --- 50,86 ---- // 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']) . "'"; $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']) . "'"; $result = $db->sql_query($sql, 7200); + while ($row = $db->sql_fetchrow($result)) { $this->question_ids[$row['question_id']] = $row['question_id']; *************** *** 93,98 **** --- 102,108 ---- function &get_instance() { $instance =& new phpbb_captcha_qa(); + return $instance; } *************** *** 108,138 **** 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); } ! /** * 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 */ function is_available() { global $config, $db, $phpbb_root_path, $phpEx, $user; ! // load language file for pretty display in the ACP dropdown $user->add_lang('captcha_qa'); ! if (!phpbb_captcha_qa::is_installed()) { return false; } ! $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 */ --- 118,152 ---- 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); } ! /** * 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 */ function is_available() { global $config, $db, $phpbb_root_path, $phpEx, $user; ! // load language file for pretty display in the ACP dropdown $user->add_lang('captcha_qa'); ! if (!phpbb_captcha_qa::is_installed()) { return false; } ! ! $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,147 **** return true; } - /** * API function */ --- 155,160 ---- *************** *** 158,164 **** return 'phpbb_captcha_qa'; } - /** * API function - not needed as we don't display an image */ --- 171,176 ---- *************** *** 179,185 **** function get_template() { global $template; ! if ($this->is_solved()) { return false; --- 191,197 ---- function get_template() { global $template; ! if ($this->is_solved()) { return false; *************** *** 218,223 **** --- 230,236 ---- $hidden_fields['qa_answer'] = $this->answer; } $hidden_fields['qa_confirm_id'] = $this->confirm_id; + return $hidden_fields; } *************** *** 230,236 **** $sql = 'SELECT DISTINCT c.session_id FROM ' . CAPTCHA_QA_CONFIRM_TABLE . ' c ! 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); --- 243,250 ---- $sql = 'SELECT DISTINCT c.session_id FROM ' . CAPTCHA_QA_CONFIRM_TABLE . ' c ! 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,243 **** --- 252,258 ---- if ($row = $db->sql_fetchrow($result)) { $sql_in = array(); + do { $sql_in[] = (string) $row['session_id']; *************** *** 274,281 **** 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( CAPTCHA_QUESTIONS_TABLE => array ( 'COLUMNS' => array( --- 289,297 ---- 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( CAPTCHA_QUESTIONS_TABLE => array ( 'COLUMNS' => array( *************** *** 315,321 **** 'PRIMARY_KEY' => 'confirm_id', ), ); ! foreach($schemas as $table => $schema) { if (!$db_tool->sql_table_exists($table)) --- 331,337 ---- 'PRIMARY_KEY' => 'confirm_id', ), ); ! foreach($schemas as $table => $schema) { if (!$db_tool->sql_table_exists($table)) *************** *** 325,339 **** } } - /** * API function - see what has to be done to validate */ function validate() { global $config, $db, $user; ! $error = ''; if (!$this->confirm_id) { $error = $user->lang['CONFIRM_QUESTION_WRONG']; --- 341,355 ---- } } /** * API function - see what has to be done to validate */ function validate() { global $config, $db, $user; ! $error = ''; + if (!$this->confirm_id) { $error = $user->lang['CONFIRM_QUESTION_WRONG']; *************** *** 356,361 **** --- 372,378 ---- // okay, incorrect answer. Let's ask a new question. $this->new_attempt(); $this->solved = false; + return $error; } else *************** *** 373,389 **** $this->confirm_id = md5(unique_id($user->ip)); $this->question = (int) array_rand($this->question_ids); ! $sql = 'INSERT INTO ' . CAPTCHA_QA_CONFIRM_TABLE . ' ' . $db->sql_build_array('INSERT', array( ! 'confirm_id' => (string) $this->confirm_id, ! 'session_id' => (string) $user->session_id, ! 'lang_iso' => (string) $this->question_lang, ! 'confirm_type' => (int) $this->type, ! 'question_id' => (int) $this->question, )); $db->sql_query($sql); - $this->load_answer(); } /** --- 390,406 ---- $this->confirm_id = md5(unique_id($user->ip)); $this->question = (int) array_rand($this->question_ids); ! $sql = 'INSERT INTO ' . CAPTCHA_QA_CONFIRM_TABLE . ' ' . $db->sql_build_array('INSERT', array( ! 'confirm_id' => (string) $this->confirm_id, ! 'session_id' => (string) $user->session_id, ! 'lang_iso' => (string) $this->question_lang, ! 'confirm_type' => (int) $this->type, ! 'question_id' => (int) $this->question, )); $db->sql_query($sql); + $this->load_answer(); } /** *************** *** 395,408 **** $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) . '\''; $db->sql_query($sql); $this->load_answer(); } --- 412,424 ---- $this->question = (int) array_rand($this->question_ids); $this->solved = 0; ! $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,430 **** // 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)) . ', ! attempts = attempts + 1 ! 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(); } --- 432,445 ---- // yah, I would prefer a stronger rand, but this should work $this->question = (int) array_rand($this->question_ids); $this->solved = 0; ! $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) . "'"; $db->sql_query($sql); + $this->load_answer(); } *************** *** 434,440 **** function load_answer() { global $db, $user; ! $sql = 'SELECT con.question_id, attempts, question_text, strict FROM ' . CAPTCHA_QA_CONFIRM_TABLE . ' con, ' . CAPTCHA_QUESTIONS_TABLE . " qes WHERE con.question_id = qes.question_id --- 449,455 ---- function load_answer() { global $db, $user; ! $sql = 'SELECT con.question_id, attempts, question_text, strict FROM ' . CAPTCHA_QA_CONFIRM_TABLE . ' con, ' . CAPTCHA_QUESTIONS_TABLE . " qes WHERE con.question_id = qes.question_id *************** *** 453,460 **** --- 468,477 ---- $this->attempts = $row['attempts']; $this->question_strict = $row['strict']; $this->question_text = $row['question_text']; + return true; } + return false; } *************** *** 464,486 **** function check_answer() { global $db; ! $answer = ($this->question_strict) ? request_var('qa_answer', '', true) : utf8_clean_string(request_var('qa_answer', '', true)); ! $sql = 'SELECT answer_text ! 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'] ); if ($solution === $answer) { $this->solved = true; break; } } $db->sql_freeresult($result); return $this->solved; } --- 481,507 ---- function check_answer() { global $db; ! $answer = ($this->question_strict) ? request_var('qa_answer', '', true) : utf8_clean_string(request_var('qa_answer', '', true)); ! $sql = 'SELECT answer_text ! 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']); ! if ($solution === $answer) { $this->solved = true; + break; } } $db->sql_freeresult($result); + return $this->solved; } *************** *** 531,540 **** { $this->validate(); } return (bool) $this->solved; } ! ! /** * API function - The ACP backend, this marks the end of the easy methods */ --- 552,561 ---- { $this->validate(); } + return (bool) $this->solved; } ! /** * API function - The ACP backend, this marks the end of the easy methods */ *************** *** 550,555 **** --- 571,577 ---- { $this->install(); } + $module->tpl_name = 'captcha_qa_acp'; $module->page_title = 'ACP_VC_SETTINGS'; $form_key = 'acp_captcha'; *************** *** 558,571 **** $submit = request_var('submit', false); $question_id = request_var('question_id', 0); $action = request_var('action', ''); ! // we have two pages, so users might want to navigate from one to the other $list_url = $module->u_action . "&configure=1&select_captcha=" . $this->get_class_name(); ! $template->assign_vars(array( ! 'U_ACTION' => $module->u_action, ! 'QUESTION_ID' => $question_id , ! 'CLASS' => $this->get_class_name(), )); // show the list? --- 580,593 ---- $submit = request_var('submit', false); $question_id = request_var('question_id', 0); $action = request_var('action', ''); ! // we have two pages, so users might want to navigate from one to the other $list_url = $module->u_action . "&configure=1&select_captcha=" . $this->get_class_name(); ! $template->assign_vars(array( ! 'U_ACTION' => $module->u_action, ! 'QUESTION_ID' => $question_id , ! 'CLASS' => $this->get_class_name(), )); // show the list? *************** *** 578,583 **** --- 600,606 ---- if (confirm_box(true)) { $this->acp_delete_question($question_id); + trigger_error($user->lang['QUESTION_DELETED'] . adm_back_link($list_url)); } else *************** *** 600,605 **** --- 623,629 ---- $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( *************** *** 607,621 **** 'NAME' => $entry['name'], )); } ! $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'], --- 631,647 ---- 'NAME' => $entry['name'], )); } ! $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,647 **** } else { - $template->assign_vars(array( ! 'QUESTION_TEXT' => $input_question, ! 'LANG_ISO' => $input_lang, ! 'STRICT' => $input_strict, ! 'ANSWERS' => $input_answers, )); } ! if ($submit && check_form_key($form_key)) { $data = $this->acp_get_question_input(); if (!$this->validate_input($data)) { $template->assign_vars(array( --- 656,673 ---- } else { $template->assign_vars(array( ! 'QUESTION_TEXT' => $input_question, ! 'LANG_ISO' => $input_lang, ! 'STRICT' => $input_strict, ! 'ANSWERS' => $input_answers, )); } ! if ($submit && check_form_key($form_key)) { $data = $this->acp_get_question_input(); + if (!$this->validate_input($data)) { $template->assign_vars(array( *************** *** 658,664 **** { $this->acp_add_question($data); } ! trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($list_url)); } } --- 684,690 ---- { $this->acp_add_question($data); } ! trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($list_url)); } } *************** *** 668,674 **** } } } - /** * This handles the list overview --- 694,699 ---- *************** *** 676,682 **** function acp_question_list(&$module) { global $db, $template; ! $sql = 'SELECT * FROM ' . CAPTCHA_QUESTIONS_TABLE; $result = $db->sql_query($sql); --- 701,707 ---- function acp_question_list(&$module) { global $db, $template; ! $sql = 'SELECT * FROM ' . CAPTCHA_QUESTIONS_TABLE; $result = $db->sql_query($sql); *************** *** 688,694 **** while ($row = $db->sql_fetchrow($result)) { $url = $module->u_action . "&question_id={$row['question_id']}&configure=1&select_captcha=" . $this->get_class_name() . '&'; ! $template->assign_block_vars('questions', array( 'QUESTION_TEXT' => $row['question_text'], 'QUESTION_ID' => $row['question_id'], --- 713,719 ---- while ($row = $db->sql_fetchrow($result)) { $url = $module->u_action . "&question_id={$row['question_id']}&configure=1&select_captcha=" . $this->get_class_name() . '&'; ! $template->assign_block_vars('questions', array( 'QUESTION_TEXT' => $row['question_text'], 'QUESTION_ID' => $row['question_id'], *************** *** 737,744 **** return $question; } } ! ! /** * Grab a question from input and bring it into a format the editor understands */ --- 762,768 ---- return $question; } } ! /** * Grab a question from input and bring it into a format the editor understands */ *************** *** 780,786 **** $cache->destroy('sql', CAPTCHA_QUESTIONS_TABLE); } ! /** * Insert a question. * param mixed $data : an array as created from acp_get_question_input or acp_get_question_data --- 804,810 ---- $cache->destroy('sql', CAPTCHA_QUESTIONS_TABLE); } ! /** * Insert a question. * param mixed $data : an array as created from acp_get_question_input or acp_get_question_data *************** *** 795,801 **** $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); $db->sql_query($sql); $question_id = $db->sql_nextid(); --- 819,825 ---- $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); $db->sql_query($sql); $question_id = $db->sql_nextid(); *************** *** 804,810 **** $cache->destroy('sql', CAPTCHA_QUESTIONS_TABLE); } ! /** * Insert the answers. * param mixed $data : an array as created from acp_get_question_input or acp_get_question_data --- 828,834 ---- $cache->destroy('sql', CAPTCHA_QUESTIONS_TABLE); } ! /** * Insert the answers. * param mixed $data : an array as created from acp_get_question_input or acp_get_question_data *************** *** 812,818 **** function acp_insert_answers($data, $question_id) { global $db, $cache; ! foreach ($data['answers'] as $answer) { $answer_ary = array( --- 836,842 ---- function acp_insert_answers($data, $question_id) { global $db, $cache; ! foreach ($data['answers'] as $answer) { $answer_ary = array( *************** *** 820,832 **** 'answer_text' => $answer, ); ! $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. --- 844,855 ---- 'answer_text' => $answer, ); ! $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. *************** *** 846,853 **** $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 --- 869,875 ---- $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 *************** *** 873,879 **** return true; } ! /** * List the installed language packs */ --- 895,901 ---- return true; } ! /** * List the installed language packs */ *************** *** 881,893 **** { global $db; - $langs = array(); - $sql = 'SELECT * FROM ' . LANG_TABLE; - $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { $langs[$row['lang_iso']] = array( --- 903,913 ---- { global $db; $sql = 'SELECT * FROM ' . LANG_TABLE; $result = $db->sql_query($sql); + $langs = array(); while ($row = $db->sql_fetchrow($result)) { $langs[$row['lang_iso']] = array( *************** *** 899,905 **** return $langs; } - } ?> \ No newline at end of file --- 919,924 ---- |