What is the sum of 2 and 9? Version: 2.1-SEC Author: Michael Woehrer Author URI: http://sw-guide.de/ */ /* ---------------------------------------------------------------------------- ____________________________________________________ | | | Math Comment Spam Protection | | © Michael Woehrer | |____________________________________________________| © Copyright 2006-2007 Michael Woehrer (michael dot woehrer at gmail dot com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. ---------------------------------------------------------------------------- ACKNOWLEDGEMENTS: - Thanks to Steven Herod (http://www.herod.net/) for his plugin "Did You Pass Math?". I took his idea and extended/improved it by writing a plugin on my own. ---------------------------------------------------------------------------- INSTALLATION, USAGE: Visit the plugin's homepage. ---------------------------------------------------------------------------- */ # Determine installation path: will be under wp-content/plugins or under a subdir of /plugins/ $mcsp_info['install_dir'] = ABSPATH . 'wp-content/plugins'; if ( basename(dirname(__FILE__)) != 'plugins' ) $mcsp_info['install_dir'] .= '/' . basename(dirname(__FILE__)); # Include the class and create object if ( !class_exists('MathCheck') ) include_once($mcsp_info['install_dir'] . '/math-comment-spam-protection.classes.php'); if (class_exists('MathCheck')) { $mcsp_MathCheckObj = new MathCheck; } else { wp_die("Class MatchCheck not found!"); } # Get settings $mcsp_opt = get_option('plugin_mathcommentspamprotection'); /******************************************************************************* * Generate math question ******************************************************************************/ function math_comment_spam_protection() { global $mcsp_MathCheckObj, $mcsp_opt; // Set class options $mcsp_MathCheckObj->opt['input_numbers'] = $mcsp_opt['mcsp_opt_numbers']; // Generate numbers to be displayed and result $mcsp_MathCheckObj->GenerateValues(); $mcsp_info['operand1'] = $mcsp_MathCheckObj->info['operand1']; $mcsp_info['operand2'] = $mcsp_MathCheckObj->info['operand2']; $mcsp_info['result'] = $mcsp_MathCheckObj->info['result']; return $mcsp_info; } /******************************************************************************* * Input validation. ******************************************************************************/ function mcsp_check_input($comment_data) { global $mcsp_MathCheckObj, $user_ID, $mcsp_opt; if ( ( !isset($user_ID) ) && ( $comment_data['comment_type'] == '' ) ) { // Do not check if the user is registered & do not check trackbacks/pingbacks // Get actual result $actual_result = $_POST[ $mcsp_opt['mcsp_opt_fieldname_mathresult'] ]; // Get value user has entered $value_entered = $_POST[ $mcsp_opt['mcsp_opt_fieldname_useranswer'] ]; // Get input validation result $result = $mcsp_MathCheckObj->InputValidation($actual_result, $value_entered); // DIE if there was an error switch ($result) { case 'No answer': mcsp_aux_die( __(stripslashes($mcsp_opt['mcsp_opt_msg_no_answer'])) ); break; case 'Wrong answer': mcsp_aux_die( __(stripslashes($mcsp_opt['mcsp_opt_msg_wrong_answer'])) ); break; } } return $comment_data; } /******************************************************************************* * Apply plugin ******************************************************************************/ add_filter('preprocess_comment', 'mcsp_check_input', 0); ################################################################################################################################## /******************************************************************************* * Admin *******************************************************************************/ // Add admin menu function mcsp_add_options_to_admin() { if (function_exists('add_options_page')) { add_options_page('Math Comment Spam', 'Math Comment Spam', 8, basename(__FILE__), 'mcsp_options_subpanel'); } } // This will add the new item, 'Math Comment Spam', to the Options menu. function mcsp_options_subpanel() { /* Lets add some default options if they don't exist If an option with the specified name already exists, no changes are made to its value or to the database as a whole. add_option() can only add options, not alter them.*/ $tmp_noanswer = '
Error: Please press the back button and fill the required field for spam protection.
'; $tmp_wronganswer = '
Error: You have entered the wrong sum in the spam protection field.
Press the back button and try again.
For details, visit the plugin's homepage.
© Copyright 2006-2007 Michael Wöhrer