From: Henry S. <kel...@ph...> - 2009-07-20 10:42:51
|
Author: Kellanved Date: Mon Jul 20 10:42:27 2009 New Revision: 9803 Log: cleaning 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 Mon Jul 20 10:42:27 2009 *************** *** 24,29 **** --- 24,30 ---- /** + * And now to something completely different. Let's make a captcha without extending the abstract class. * QA CAPTCHA sample implementation * * @package VC *************** *** 39,47 **** var $question_strict; var $attempts = 0; var $type; var $solved = 0; - var $captcha_vars = false; function init($type) { global $config, $db, $user; --- 40,51 ---- var $question_strict; var $attempts = 0; var $type; + // dirty trick: 0 is false, but can still encode that the captcha is not yet validated var $solved = 0; + /** + * @param int $type as per the CAPTCHA API docs, the type + */ function init($type) { global $config, $db, $user; *************** *** 53,59 **** $this->type = (int) $type; $this->question_lang = $user->data['user_lang']; ! $sql = 'SELECT question_id FROM ' . QUESTIONS_TABLE . ' WHERE lang_iso = \'' . $db->sql_escape($user->data['user_lang']) . '\''; $result = $db->sql_query($sql, 3600); while ($row = $db->sql_fetchrow($result)) --- 57,64 ---- $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 ' . QUESTIONS_TABLE . ' WHERE lang_iso = \'' . $db->sql_escape($user->data['user_lang']) . '\''; $result = $db->sql_query($sql, 3600); while ($row = $db->sql_fetchrow($result)) *************** *** 61,66 **** --- 66,72 ---- $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']; *************** *** 73,93 **** $db->sql_freeresult($result); } if (!strlen($this->confirm_id) || !$this->load_answer()) { ! // we have no confirm ID, better get ready to display something $this->select_question(); } } ! function &get_instance() { $instance =& new phpbb_captcha_qa(); return $instance; } ! function is_installed() { global $db, $phpbb_root_path, $phpEx; --- 79,104 ---- $db->sql_freeresult($result); } + // okay, if there is a confirm_id, we try to load that confirm's state if (!strlen($this->confirm_id) || !$this->load_answer()) { ! // we have no valid confirm ID, better get ready to ask something $this->select_question(); } } ! /** ! * API function ! */ function &get_instance() { $instance =& new phpbb_captcha_qa(); return $instance; } ! /** ! * See if the captcha has created its tables. ! */ function is_installed() { global $db, $phpbb_root_path, $phpEx; *************** *** 100,105 **** --- 111,119 ---- return $db_tool->sql_table_exists(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; *************** *** 117,144 **** return ((bool) $row['count']); } function get_name() { return 'CAPTCHA_QA'; } function get_class_name() { return 'phpbb_captcha_qa'; } function execute_demo() { } function execute() { } function get_template() { ! global $config, $user, $template, $phpEx, $phpbb_root_path; $template->assign_vars(array( 'CONFIRM_QUESTION' => $this->question_text, --- 131,173 ---- return ((bool) $row['count']); } + /** + * API function + */ function get_name() { return 'CAPTCHA_QA'; } + /** + * API function + */ function get_class_name() { return 'phpbb_captcha_qa'; } + /** + * API function - not needed as we don't display an image + */ function execute_demo() { } + /** + * API function - not needed as we don't display an image + */ function execute() { } + /** + * API function - send the question to the template + */ function get_template() { ! global $template; $template->assign_vars(array( 'CONFIRM_QUESTION' => $this->question_text, *************** *** 150,160 **** return 'captcha_qa.html'; } ! function get_demo_template($id) { return 'captcha_qa_acp_demo.html'; } function get_hidden_fields() { $hidden_fields = array(); --- 179,195 ---- return 'captcha_qa.html'; } ! /** ! * API function - we just display a mockup so that the captcha doesn't need to be installed ! */ ! function get_demo_template() { return 'captcha_qa_acp_demo.html'; } + /** + * API function + */ function get_hidden_fields() { $hidden_fields = array(); *************** *** 168,173 **** --- 203,211 ---- return $hidden_fields; } + /** + * API function + */ function garbage_collect($type) { global $db, $config; *************** *** 198,208 **** --- 236,252 ---- $db->sql_freeresult($result); } + /** + * API function - we don't drop the tables here, as that would cause the loss of all entered questions. + */ function uninstall() { $this->garbage_collect(0); } + /** + * API function - set up shop + */ function install() { global $db, $phpbb_root_path, $phpEx; *************** *** 226,231 **** --- 270,276 ---- 'PRIMARY_KEY' => 'question_id', 'KEYS' => array( 'question_id' => array('INDEX', array('question_id', 'lang_iso')), + 'lang_iso' => array('INDEX', 'lang_iso'), ), ), ANSWERS_TABLE => array ( *************** *** 254,260 **** ), ); - foreach($schemas as $table => $schema) { if (!$db_tool->sql_table_exists($table)) --- 299,304 ---- *************** *** 265,270 **** --- 309,317 ---- } + /** + * API function - see what has to be done to validate + */ function validate() { global $config, $db, $user; *************** *** 342,353 **** } /** ! * New Question, if desired. */ function new_attempt() { global $db, $user; $this->question = (int) array_rand($this->question_ids); $this->solved = 0; // compute $seed % 0x7fffffff --- 389,401 ---- } /** ! * Wrong answer, so we increase the attempts and use a different question. */ function new_attempt() { global $db, $user; + // 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 *************** *** 363,369 **** } /** ! * Look up everything we need. */ function load_answer() { --- 411,417 ---- } /** ! * Look up everything we need and populate the instance variables. */ function load_answer() { *************** *** 392,397 **** --- 440,448 ---- return false; } + /** + * The actual validation + */ function check_answer() { global $db; *************** *** 415,420 **** --- 466,474 ---- return $this->solved; } + /** + * API function - clean the entry + */ function delete_code() { global $db, $user; *************** *** 426,436 **** --- 480,496 ---- $db->sql_query($sql); } + /** + * API function + */ function get_attempt_count() { return $this->attempts; } + /** + * API function + */ function reset() { global $db, $user; *************** *** 443,449 **** // we leave the class usable by generating a new question $this->generate_code(); } ! function is_solved() { if (request_var('answer', false) && $this->solved === 0) --- 503,512 ---- // we leave the class usable by generating a new question $this->generate_code(); } ! ! /** ! * API function ! */ function is_solved() { if (request_var('answer', false) && $this->solved === 0) *************** *** 453,458 **** --- 516,525 ---- return (bool) $this->solved; } + + /** + * API function - The ACP backend, this marks the end of the easy methods + */ function acp_page($id, &$module) { global $db, $user, $auth, $template; *************** *** 474,479 **** --- 541,547 ---- $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( *************** *** 481,487 **** 'QUESTION_ID' => $question_id , 'CLASS' => $this->get_class_name(), )); ! if (!$question_id && $action != 'add') { $this->acp_question_list($module); --- 549,556 ---- 'QUESTION_ID' => $question_id , 'CLASS' => $this->get_class_name(), )); ! ! // show the list? if (!$question_id && $action != 'add') { $this->acp_question_list($module); *************** *** 506,512 **** } else { ! $error = false; $input_question = request_var('question_text', ''); $input_answers = request_var('answers', ''); --- 575,581 ---- } else { ! // okay, show the editor $error = false; $input_question = request_var('question_text', ''); $input_answers = request_var('answers', ''); *************** *** 582,587 **** --- 651,660 ---- } } + + /** + * This handles the list overview + */ function acp_question_list(&$module) { global $db, $template; *************** *** 606,612 **** } $db->sql_freeresult($result); } ! function acp_get_question_data($question_id) { global $db; --- 679,688 ---- } $db->sql_freeresult($result); } ! ! /** ! * Grab a question and bring it into a format the editor understands ! */ function acp_get_question_data($question_id) { global $db; *************** *** 639,644 **** --- 715,723 ---- } + /** + * Grab a question from input and bring it into a format the editor understands + */ function acp_get_question_input() { global $db; *************** *** 653,664 **** return $question; } ! ! function acp_update_question($data, $question_id) { global $db; $sql = "DELETE FROM " . ANSWERS_TABLE . " WHERE question_id = $question_id"; $db->sql_query($sql); $langs = $this->get_languages(); --- 732,746 ---- return $question; } ! /** ! * Update a question. ! * param mixed $data : an array as created from acp_get_question_input or acp_get_question_data ! */ function acp_update_question($data, $question_id) { global $db; + // easier to delete all answers than to figure out which to update $sql = "DELETE FROM " . ANSWERS_TABLE . " WHERE question_id = $question_id"; $db->sql_query($sql); $langs = $this->get_languages(); *************** *** 671,676 **** --- 753,762 ---- $this->acp_insert_answers($data, $question_id); } + /** + * Insert a question. + * param mixed $data : an array as created from acp_get_question_input or acp_get_question_data + */ function acp_add_question($data) { global $db; *************** *** 686,691 **** --- 772,781 ---- $this->acp_insert_answers($data, $question_id); } + /** + * Insert the answers. + * param mixed $data : an array as created from acp_get_question_input or acp_get_question_data + */ function acp_insert_answers($data, $question_id) { global $db; *************** *** 702,708 **** } ! function acp_delete_question($question_id) { global $db; --- 792,800 ---- } ! /** ! * Delete a question. ! */ function acp_delete_question($question_id) { global $db; *************** *** 716,721 **** --- 808,817 ---- } + /** + * 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 + */ function validate_input($question_data) { $langs = $this->get_languages(); *************** *** 736,741 **** --- 832,840 ---- return true; } + /** + * List the installed language packs + */ function get_languages() { global $db; |