Nasty wordpress hacks: adding CAPTCHA to wp-wall plugin.
Posted: September 27th, 2009 | Author: admin | Filed under: wordpress | Tags: captcha, hack, plugin, si-captcha, wordpress, wp, wp-wall | Comments OffHrm. Just had to add a CAPTCHA to a WP-wall. So the aim here is to combine si-captcha and wp-wall. It’s worth noting that both are excellent plugins, which makes messing about with them a lot easier. It’s a fairly nasty hack, but it works.
1. Install and activate SI CAPTCHA
Si Captcha is rather cleverly designed to allow WP-wall to work despite it (without a captcha). We need to switch this off first:
2. Delete lines 751 – 754 of si-captcha.php
These ones:
At this point, the wall won’t work – si-captcha looks for the captcha field of the form, which isn’t there. Yet.
3. Add the CAPTCHA field to wp-wall plugin.
Add the following to wp-wall-widget.php, which is the ‘add a captcha’ bits of code ripped out of si-captcha.php. Told you it was nasty.
You can add it anywhere in the form (id wallform), wherever you want the CAPTCHA to show up.
//CAPTCHA —————————————————-
global $user_ID, $si_captcha_url, $si_captcha_opt;
global $wpmu;
if( !isset( $_SESSION ) ) { // play nice with other plugins
session_cache_limiter (‘private, must-revalidate’);
session_start();
}
if (function_exists(‘load_plugin_textdomain’)) {
if ($wpmu == 1) {
load_plugin_textdomain(’si-captcha’, false, dirname(plugin_basename(__FILE__)).’/si-captcha-for-wordpress/languages’ );
} else {
load_plugin_textdomain(’si-captcha’, false, dirname(plugin_basename(__FILE__)).’/languages’ );
}
}
// skip the captcha if user is loggged in and the settings allow
if (isset($user_ID) && intval($user_ID) > 0 && $si_captcha_opt['si_captcha_perm'] == ‘true’) {
// skip the CAPTCHA display if the minimum capability is met
if ( current_user_can( $si_captcha_opt['si_captcha_perm_level'] ) ) {
// skip capthca
return true;
}
}
// the captch html
echo ‘<div style=”display:block;” id=”captchaImgDiv”>’;
$si_aria_required = ($si_captcha_opt['si_captcha_aria_required'] == ‘true’) ? ‘ aria-required=”true” ‘ : ”;
echo ‘<div style=”width: 250px; height: 55px; padding-top:10px;”>’;
//captcha html
$label = ’si_image’;
global $si_captcha_url, $si_captcha_opt;
echo ‘<img id=”‘.$label.’” ‘;
echo ($si_captcha_opt['si_captcha_captcha_image_style'] != ”) ? ’style=”‘ . esc_attr( $si_captcha_opt['si_captcha_captcha_image_style'] ).’”‘ : ”;
echo ‘ src=”‘.$si_captcha_url.’/securimage_show.php?sid=’.md5(uniqid(time())).’” alt=”‘;
echo ($si_captcha_opt['si_captcha_tooltip_captcha'] != ”) ? esc_attr( $si_captcha_opt['si_captcha_tooltip_captcha'] ) : esc_attr(__(‘CAPTCHA Image’, ’si-captcha’));
echo ‘” title=”‘;
echo ($si_captcha_opt['si_captcha_tooltip_captcha'] != ”) ? esc_attr( $si_captcha_opt['si_captcha_tooltip_captcha'] ) : esc_attr(__(‘CAPTCHA Image’, ’si-captcha’));
echo ‘” />
<a href=”‘.$si_captcha_url.’/securimage_play.php” title=”‘;
echo ($si_captcha_opt['si_captcha_tooltip_audio'] != ”) ? esc_attr( $si_captcha_opt['si_captcha_tooltip_audio'] ) : esc_attr(__(‘CAPTCHA Audio’, ’si-captcha’));
echo ‘”>
<img src=”‘.$si_captcha_url.’/images/audio_icon.gif” alt=”‘;
echo ($si_captcha_opt['si_captcha_tooltip_audio'] != ”) ? esc_attr( $si_captcha_opt['si_captcha_tooltip_audio'] ) : esc_attr(__(‘CAPTCHA Audio’, ’si-captcha’));
echo ‘” ‘;
//audio style=”border-style:none; margin:0; vertical-align:top;”
echo ($si_captcha_opt['si_captcha_audio_image_style'] != ”) ? ’style=”‘ . esc_attr( $si_captcha_opt['si_captcha_audio_image_style'] ).’”‘ : ”;
echo ‘ onclick=”this.blur()” /></a><br />
<a href=”#” title=”‘;
echo ($si_captcha_opt['si_captcha_tooltip_refresh'] != ”) ? esc_attr( $si_captcha_opt['si_captcha_tooltip_refresh'] ) : esc_attr(__(‘Refresh Image’, ’si-captcha’));
echo ‘” onclick=”document.getElementById(\”.$label.’\').src = \”.$si_captcha_url.’/securimage_show.php?sid=\’ + Math.random(); return false”>
<img src=”‘.$si_captcha_url.’/images/refresh.gif” alt=”‘;
echo ($si_captcha_opt['si_captcha_tooltip_refresh'] != ”) ? esc_attr( $si_captcha_opt['si_captcha_tooltip_refresh'] ) : esc_attr(__(‘Refresh Image’, ’si-captcha’));
echo ‘” ‘;
// refresh style=”border-style:none; margin:0; vertical-align:bottom;”
echo ($si_captcha_opt['si_captcha_refresh_image_style'] != ”) ? ’style=”‘ . esc_attr( $si_captcha_opt['si_captcha_refresh_image_style'] ).’”‘ : ”;
echo ‘ onclick=”this.blur()” /></a>
‘;
echo ‘</div>
<div id=”captchaInputDiv” style=”display:block;” >
<input type=”text” name=”captcha_code” id=”captcha_code” tabindex=”4″ ‘.$si_aria_required.’ style=”width:65px;” ‘;
if ($si_captcha_opt['si_captcha_comment_class'] != ”) {
echo ‘class=”‘.$si_captcha_opt['si_captcha_comment_class'].’” ‘;
}
echo ‘/>
<label for=”captcha_code”><small>’;
echo ($si_captcha_opt['si_captcha_label_captcha'] != ”) ? esc_html( $si_captcha_opt['si_captcha_label_captcha'] ) : esc_html( __(‘CAPTCHA Code’, ’si-captcha’));
echo ‘</small></label>
</div>
</div>’;
//END CAPTCHA ——————————————
?>
4. AJAXIFY
This will make the form work the first time we submit it on each page load. However, one of the nice things about WP-wall is the ajaxiness of the whole thing. We need to make sure that the CAPTCHA cookie is reset each time a message is posted. We can do this by telling the captcha to refresh each time the ‘add a message’ section of the wall is shown / hidden. On line 17 of wp-wall.js, we’re going to add the function that is called when the refresh button is clicked on the si-captcha:
Note that you need to manually set your base url in the above line (where it says ‘THE-BASE-URL). If you’ve change the name of the $label variable in si-captcha, you’ll also need to change the ’si_image’ bit of “document.getElementById(’si_image’)” to match..
5. Done.
That’s it. CAPTCHA added to WP-wall. Sorry it’s not very neat. Seems to work, but no doubt other plugins, various settings in the back end editor, etc will break it.