Skip to content

Instantly share code, notes, and snippets.

@malcom malcom/wp-mc.php
Created Sep 17, 2009

Embed
What would you like to do?
WP Math Captcha
<?php
/*
Plugin Name: WP Math Captcha
Plugin URI: http://projects.malcom.pl/plugins/wordpress/wp-mc.xhtml
Description: Simple math captcha for wordpress comments.
Version: 0.3
Author: Marcin 'Malcom' Malich
Author URI: http://malcom.pl
*/
register_activation_hook(__FILE__, 'wpmc_activate');
add_filter('preprocess_comment', 'wpmc_preprocess_comment');
if (WPLANG === 'pl_PL') {
define('WPMC_LABEL', 'Wynik działania %s');
define('WPMC_ERROR_MSG', 'Błąd: proszę wprowadzić poprawny wynik działania.');
} else {
define('WPMC_LABEL', 'Result of operation %s');
define('WPMC_ERROR_MSG', 'Error: please enter a valid result of math operation.');
}
function wpmc_activate() {
$conf = array (
'salt' => uniqid()
);
add_option('wpmc_count', 0);
add_option('wpmc_config', $conf);
}
function wpmc_genhash($data) {
$conf = (object) get_option('wpmc_config', 0);
return sha1($conf->salt . $data);
}
function wpmc_preprocess_comment($incoming_comment) {
if (is_user_logged_in())
return $incoming_comment;
// pingback, trackback
if ($incoming_comment['comment_type'] != '')
return $incoming_comment;
if (wpmc_genhash($_POST['wpmc']) != $_POST['wpmch']) {
update_option('wpmc_count', get_option('wpmc_count', 0) + 1);
wp_die(WPMC_ERROR_MSG);
}
return $incoming_comment;
}
function wpmc_form_input() {
if (is_user_logged_in())
return;
srand(time());
$a = rand(1,9);
$b = rand(1,9);
$c = rand(1,9);
$os = "$a * $b + $c";
$oh = wpmc_genhash($a * $b + $c);
?>
<p>
<input type="hidden" name="wpmch" id="wpmch" value="<?php echo $oh; ?>" />
<input type="text" name="wpmc" id="wpmc" value="" size="22" />
<label for="wpmc"><small><?php printf(WPMC_LABEL, $os); ?> <?php _e('(required)'); ?></small></label></p>
</p>
<?php
}
function wpmc_count() {
echo get_option('wpmc_count', 0);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.