Hello
I have a plugin that displays the latest results on searching
The problem someone spam the results.
I want to block IP automatically when someone searches for over three times the rate of time
For example: someone search 3 times for a time of 10 seconds then Plugin block its IP automatically.
Thank you
This is the code of the Searches results plugin that I have:
<?php
class CensoredSearchesWidget {
function CensoredSearchesWidget() {
add_action('template_redirect', array( &$this, 'template_redirect'));
add_action('widgets_init', array( &$this, 'widgets_init' ));
}
function template_redirect() {
if (is_search()) {
$query = $this->strtolower(trim( get_search_query()));
$options = get_option('recent_searches_widget');
if (!is_array($options)) {
$options = $this->get_default_options();
}
$max = $options['max'];
$data = get_option('recent_searches_widget_data', array());
if (!is_array($data)) {
if (isset( $options['data'])) {
$data = $options['data'];
unset($options['data']);
update_option('recent_searches_widget', $options);
}
if (!is_array($data)) {
$data = array();
}
}
$pos = array_search($query, $data);
if ($pos !== false) {
if ($pos != 0) {
$data = array_merge(array_slice($data, 0, $pos), array($query), array_slice($data, $pos + 1));
}
} else {
array_unshift($data, $query);
if (count($data) > $max) {
array_pop($data);
}
}
update_option('recent_searches_widget_data', $data);
}
}
function widgets_init() {
$widget_ops = array(
'classname' => 'widget_rsw',
'description' => __('Shows recent searches', 'recent-searches-widget'),
);
wp_register_sidebar_widget( 'CensoredSearchesWidget', __('Recent Searches', 'recent-searches-widget'), array( &$this, 'widget_rsw' ), $widget_ops );
wp_register_widget_control( 'CensoredSearchesWidget', __('Recent Searches', 'recent-searches-widget'), array( &$this, 'widget_rsw_control' ) );
}
function widget_rsw( $args ) {
extract( $args );
if (empty($title)) {
$title = ' ';
}
$options = get_option('recent_searches_widget', array() );
echo $before_widget . $before_title . $options['title'] . $after_title, "\n";
$this->show_recent_searches( "<ul>\n<li>", "</li>\n</ul>", "</li>\n<li>" );
echo $after_widget;
}
function show_recent_searches( $before_list, $after_list, $between_items ) {
$options = get_option( 'recent_searches_widget' );
if ( !is_array( $options ) ) {
$options = $this->get_default_options();
}
$data = get_option( 'recent_searches_widget_data' );
if ( !is_array( $data ) ) {
if ( isset( $options['data'] ) ) {
$data = $options['data'];
}
if ( !is_array( $data ) ) {
$data = array();
}
}
if ( count( $data ) > 0 ) {
echo $before_list;
$first = true;
foreach ( $data as $search ) {
$found=false;
$banned = explode("\n", $options['filter']);
foreach ($banned as $filter) {
if (mb_strlen($search, mb_detect_encoding($search)) <= 47) {
if ($filter!="") {
// preg_match("/".str_replace(array("?", "$", "/", "*", "^"), array("\?", "\$", "\/", "\*", "\^"), trim($filter))."/i", trim($search))
if (substr_count(strtolower(trim($search)), strtolower(trim($filter)))>=1) {
$found=true;
}
}
} else $found = true;
}
if ($found===false) {
if ( $first ) {
$first = false;
} else {
echo $between_items;
}
echo '<a href="', get_search_link( $search ), '"';
if ( $options['nofollow'] ) {
echo ' rel="nofollow"';
}
echo '>', wp_specialchars( $search ), '</a>';
}
}
echo $after_list, "\n";
}
}
function widget_rsw_control() {
$options = $newoptions = get_option('recent_searches_widget', array() );
if ( count( $options ) == 0 ) {
$options = $this->get_default_options();
update_option( 'recent_searches_widget', $options );
}
if ( isset( $_POST['rsw-submit'] ) ) {
$options['title'] = strip_tags( stripslashes( $_POST['rsw-title'] ) );
$options['max'] = (int)( $_POST['rsw-max'] );
$options['filter'] = stripslashes($_POST["rsw-filter"]);
if ( count( $options['data'] ) > $options['max'] ) {
$options['data'] = array_slice( $options['data'], 0, $options['max'] );
}
update_option( 'recent_searches_widget', $options );
}
$title = attribute_escape( $options['title'] );
$max = attribute_escape( $options['max'] );
$nofollow = $options['nofollow'];
$filter = $options['filter'];
?>
<p><label for="rsw-title"><?php _e('Title:', 'recent-searches-widget'); ?> <input class="widefat" id="rsw-title" name="rsw-title" type="text" value="<?php echo $title; ?>" /></label></p>
<p><label for="rsw-title"><textarea id="rsw-filter" name="rsw-filter" style="width: 100%; height: 175px;"><?php echo $filter; ?></textarea></label></p>
<p><label for="rsw-max"><?php _e('Max searches:', 'recent-searches-widget'); ?> <input id="rsw-max" name="rsw-max" type="text" size="3" maxlength="5" value="<?php echo $max; ?>" /></label></p>
<input type="hidden" id="rsw-submit" name="rsw-submit" value="1" />
<?php
}
function strtolower( $str ) {
if ( function_exists( 'mb_strtolower' ) ) {
return mb_strtolower( $str );
} else {
return strtolower( $str );
}
}
function get_default_options() {
return array(
'title' => '',
'max' => 10,
'nofollow' => true,
);
}
}
if ( !function_exists( 'esc_html' ) ) {
function esc_html( $text ) {
return wp_specialchars( $text );
}
}
if ( !function_exists( 'esc_attr' ) ) {
function esc_attr( $text ) {
return attribute_escape( $text );
}
}
if ( !function_exists( 'get_search_link' ) ) {
function get_search_link( $query = '' ) {
global $wp_rewrite;
if ( empty($query) )
$search = get_search_query();
else
$search = stripslashes($query);
$permastruct = $wp_rewrite->get_search_permastruct();
if ( empty( $permastruct ) ) {
$link = home_url('?s=' . urlencode($search) );
} else {
$search = urlencode($search);
$search = str_replace('%2F', '/', $search);
$link = str_replace( '%search%', $search, $permastruct );
$link = trailingslashit( get_option( 'home' ) ) . user_trailingslashit( $link, 'search' );
}
return apply_filters( 'search_link', $link, $search );
}
}
$wp_recent_searches_widget = new CensoredSearchesWidget();
function rsw_show_recent_searches( $before_list = "<ul>\n<li>", $after_list = "</li>\n</ul>", $between_items = "</li>\n<li>" ) {
global $wp_recent_searches_widget;
$wp_recent_searches_widget->show_recent_searches( $before_list, $after_list, $between_items );
}
?>
Gabriel Reguly answers:
Hi Combize,
Thanks for the bigger prize.
Do you want to really block the IP or just remove it from the statistics?
Regards,
Gabriel
combize comments:
Send me your Skype here
combize comments:
Type it here
combize comments:
How long does it take you to answer?
combize comments:
Someone else please
Is not serious
Gabriel Reguly comments:
Hi Combize,
I was out for lunch :-)
My skype name is gabrielreguly.
Regards,
Gabriel
Gabriel Reguly comments:
Hi Eliran hason,
For the benefit of all, and to show what has been done, I'll post the code here.
<?php
class CensoredSearchesWidget {
function CensoredSearchesWidget() {
add_action('template_redirect', array( &$this, 'template_redirect'));
add_action('widgets_init', array( &$this, 'widgets_init' ));
}
function template_redirect() {
$time = microtime(true);
$logged_ips = (array) get_option( 'recent_searches_widget_log' );
$proxy = '';
if ( getenv( 'HTTP_CLIENT_IP' ) ) {
$proxy = $_SERVER['REMOTE_ADDR'];
$ip = getenv( 'HTTP_CLIENT_IP' ) ;
} elseif( $_SERVER['HTTP_X_FORWARDED_FOR'] ) {
$proxy = $_SERVER['REMOTE_ADDR'];
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
if ( ! empty( $proxy ) ) {
$ip = $ip . $proxy;
}
$key = md5( $ip );
if ( isset( $logged_ips[$key] ) ) {
if ( (int) $logged_ips[$key]['count'] > 3 ) {
wp_die('Die spammer!');
}
}
if (is_search()) {
if ( ! isset( $logged_ips[$key] ) ) {
$logged_ips[$key] = array (
'count' => '1',
'ip' => $ip,
'time' => $time,
);
} else {
if ( (float) ( $time - $logged_ips[$key]['time'] ) <= 10000 ) {
$logged_ips[$key]['count'] = (int) $logged_ips[$key]['count'] + 1;
} else {
$logged_ips[$key]['count'] = 1;
}
}
foreach( $logged_ips as $key => $values ) {
if ( (float) ( $time - $values['time'] ) > 604800000 ) { // 7 days
unset( $logged_ips[ $key ] );
}
}
update_option( 'recent_searches_widget_log', $logged_ips );
$query = $this->strtolower(trim( get_search_query()));
$options = get_option('recent_searches_widget');
if (!is_array($options)) {
$options = $this->get_default_options();
}
$max = $options['max'];
$data = get_option('recent_searches_widget_data', array());
if (!is_array($data)) {
if (isset( $options['data'])) {
$data = $options['data'];
unset($options['data']);
update_option('recent_searches_widget', $options);
}
if (!is_array($data)) {
$data = array();
}
}
$pos = array_search($query, $data);
if ($pos !== false) {
if ($pos != 0) {
$data = array_merge(array_slice($data, 0, $pos), array($query), array_slice($data, $pos + 1));
}
} else {
array_unshift($data, $query);
if (count($data) > $max) {
array_pop($data);
}
}
update_option('recent_searches_widget_data', $data);
}
}
function widgets_init() {
$widget_ops = array(
'classname' => 'widget_rsw',
'description' => __('Shows recent searches', 'recent-searches-widget'),
);
wp_register_sidebar_widget( 'CensoredSearchesWidget', __('Recent Searches', 'recent-searches-widget'), array( &$this, 'widget_rsw' ), $widget_ops );
wp_register_widget_control( 'CensoredSearchesWidget', __('Recent Searches', 'recent-searches-widget'), array( &$this, 'widget_rsw_control' ) );
}
function widget_rsw( $args ) {
extract( $args );
if (empty($title)) {
$title = ' ';
}
$options = get_option('recent_searches_widget', array() );
echo $before_widget . $before_title . $options['title'] . $after_title, "\n";
$this->show_recent_searches( "<ul>\n<li>", "</li>\n</ul>", "</li>\n<li>" );
echo $after_widget;
}
function show_recent_searches( $before_list, $after_list, $between_items ) {
$options = get_option( 'recent_searches_widget' );
if ( !is_array( $options ) ) {
$options = $this->get_default_options();
}
$data = get_option( 'recent_searches_widget_data' );
if ( !is_array( $data ) ) {
if ( isset( $options['data'] ) ) {
$data = $options['data'];
}
if ( !is_array( $data ) ) {
$data = array();
}
}
if ( count( $data ) > 0 ) {
echo $before_list;
$first = true;
foreach ( $data as $search ) {
$found=false;
$banned = explode("\n", $options['filter']);
foreach ($banned as $filter) {
if (mb_strlen($search, mb_detect_encoding($search)) <= 47) {
if ($filter!="") {
// preg_match("/".str_replace(array("?", "$", "/", "*", "^"), array("\?", "\$", "\/", "\*", "\^"), trim($filter))."/i", trim($search))
if (substr_count(strtolower(trim($search)), strtolower(trim($filter)))>=1) {
$found=true;
}
}
} else $found = true;
}
if ($found===false) {
if ( $first ) {
$first = false;
} else {
echo $between_items;
}
echo '<a href="', get_search_link( $search ), '"';
if ( $options['nofollow'] ) {
echo ' rel="nofollow"';
}
echo '>', wp_specialchars( $search ), '</a>';
}
}
echo $after_list, "\n";
}
}
function widget_rsw_control() {
$options = $newoptions = get_option('recent_searches_widget', array() );
if ( count( $options ) == 0 ) {
$options = $this->get_default_options();
update_option( 'recent_searches_widget', $options );
}
if ( isset( $_POST['rsw-submit'] ) ) {
$options['title'] = strip_tags( stripslashes( $_POST['rsw-title'] ) );
$options['max'] = (int)( $_POST['rsw-max'] );
$options['filter'] = stripslashes($_POST["rsw-filter"]);
if ( count( $options['data'] ) > $options['max'] ) {
$options['data'] = array_slice( $options['data'], 0, $options['max'] );
}
update_option( 'recent_searches_widget', $options );
}
$title = attribute_escape( $options['title'] );
$max = attribute_escape( $options['max'] );
$nofollow = $options['nofollow'];
$filter = $options['filter'];
?>
<p><label for="rsw-title"><?php _e('Title:', 'recent-searches-widget'); ?> <input class="widefat" id="rsw-title" name="rsw-title" type="text" value="<?php echo $title; ?>" /></label></p>
<p><label for="rsw-title"><textarea id="rsw-filter" name="rsw-filter" style="width: 100%; height: 175px;"><?php echo $filter; ?></textarea></label></p>
<p><label for="rsw-max"><?php _e('Max searches:', 'recent-searches-widget'); ?> <input id="rsw-max" name="rsw-max" type="text" size="3" maxlength="5" value="<?php echo $max; ?>" /></label></p>
<input type="hidden" id="rsw-submit" name="rsw-submit" value="1" />
<?php
}
function strtolower( $str ) {
if ( function_exists( 'mb_strtolower' ) ) {
return mb_strtolower( $str );
} else {
return strtolower( $str );
}
}
function get_default_options() {
return array(
'title' => '',
'max' => 10,
'nofollow' => true,
);
}
}
if ( !function_exists( 'esc_html' ) ) {
function esc_html( $text ) {
return wp_specialchars( $text );
}
}
if ( !function_exists( 'esc_attr' ) ) {
function esc_attr( $text ) {
return attribute_escape( $text );
}
}
if ( !function_exists( 'get_search_link' ) ) {
function get_search_link( $query = '' ) {
global $wp_rewrite;
if ( empty($query) )
$search = get_search_query();
else
$search = stripslashes($query);
$permastruct = $wp_rewrite->get_search_permastruct();
if ( empty( $permastruct ) ) {
$link = home_url('?s=' . urlencode($search) );
} else {
$search = urlencode($search);
$search = str_replace('%2F', '/', $search);
$link = str_replace( '%search%', $search, $permastruct );
$link = trailingslashit( get_option( 'home' ) ) . user_trailingslashit( $link, 'search' );
}
return apply_filters( 'search_link', $link, $search );
}
}
$wp_recent_searches_widget = new CensoredSearchesWidget();
function rsw_show_recent_searches( $before_list = "<ul>\n<li>", $after_list = "</li>\n</ul>", $between_items = "</li>\n<li>" ) {
global $wp_recent_searches_widget;
$wp_recent_searches_widget->show_recent_searches( $before_list, $after_list, $between_items );
}
?>
Regards,
Gabriel
combize comments:
Yes, Good job
Thank you
combize comments:
Do not pay him
is not finished the job yet
Gabriel Reguly comments:
Hi Eliran hason,
Indeed there is an error at the time calculation
Please replace
if ( (float) ( $time - $logged_ips[$key]['time'] ) <= 10000 ) {
with
if ( (float) ( $time - $logged_ips[$key]['time'] ) <= 10 ) { // 10 seconds
Also, replace
if ( (float) ( $time - $values['time'] ) > 604800000 ) { // 7 dayw
with
if ( (float) ( $time - $values['time'] ) > 604800 ) { // 7 days ( 60 seconds x 60 minutes x 24 hours x 7 days )
That shall fix the code.
Reagards,
Gabriel