Plugin Directory

Changeset 1590913


Ignore:
Timestamp:
02/07/2017 12:58:08 PM (9 years ago)
Author:
inazo
Message:

New version 1.1 adding database caching for results

Location:
tor-blocker-by-inazo
Files:
97 added
3 edited

Legend:

Unmodified
Added
Removed
  • tor-blocker-by-inazo/trunk/inazo.real.tor.blocker.php

    r1571760 r1590913  
    33Plugin Name: Tor Blocker by Inazo
    44Description: This plugin blocks Tor users by preventing them from viewing your website.
    5 Version:     1.0
     5Version:     1.1
    66Author:      Inazo
    77Author URI:  https://www.kanjian.fr
     
    3535function installPluginTor(){
    3636   
    37     add_option( 'inazo.real.tor.blocker_db_version', '1.0' );       
     37    if (! wp_next_scheduled ( 'inazo_tor_clean_logs' )) {
     38        wp_schedule_event(time(), 'hourly', 'inazo_tor_clean_logs');
     39    }
     40   
     41    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
     42   
     43   
     44    $optionIsOk = get_option('inazo.real.tor.blocker_db_version');
     45   
     46    $currentVersion = '1.1';
     47   
     48    if( !$optionIsOk  ){
     49       
     50        update_option( 'inazo.real.tor.blocker_db_version', $currentVersion );
     51       
     52        global $wpdb;
     53        $table_name = $wpdb->prefix.'inazo_tor_logs';
     54       
     55        if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
     56               
     57            $sqlForCreateTable = 'CREATE TABLE '.$table_name.' (
     58            id_tor_inazo_log int(11) NOT NULL AUTO_INCREMENT,
     59            ip_concern varchar(255) NOT NULL,
     60            is_tor int(1) NOT NULL,
     61            date_of_log datetime NOT NULL,
     62            PRIMARY KEY id_tor_inazo_log (id_tor_inazo_log)
     63            ) '.$wpdb->get_charset_collate().';';
     64       
     65            dbDelta( $sqlForCreateTable );
     66        }
     67    }
     68    else
     69        add_option( 'inazo.real.tor.blocker_db_version', $currentVersion );
     70   
     71        global $wpdb;
     72        $table_name = $wpdb->prefix.'inazo_tor_logs';
     73       
     74        if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
     75           
     76            $sqlForCreateTable = 'CREATE TABLE '.$table_name.' (
     77            id_tor_inazo_log int(11) NOT NULL AUTO_INCREMENT,
     78            ip_concern varchar(255) NOT NULL,
     79            is_tor int(1) NOT NULL,
     80            date_of_log datetime NOT NULL,
     81            PRIMARY KEY id_tor_inazo_log (id_tor_inazo_log)
     82            ) '.$wpdb->get_charset_collate().';';
     83   
     84            dbDelta( $sqlForCreateTable );
     85        }
     86   
     87   
    3888}
    3989
     
    91141}
    92142
     143function isInLog( $ip ){
     144
     145    global $wpdb;
     146   
     147    $toReturn = array( 'num_rows' => '', 'valeurs' => '' );
     148
     149    $sql = $wpdb->prepare('SELECT is_tor FROM '.$wpdb->prefix.'inazo_tor_logs WHERE ip_concern = %s', $ip );
     150    $wpdb->query( $sql );
     151
     152    $toReturn['num_rows'] = $wpdb->num_rows;
     153
     154    if( $wpdb->num_rows > 0 )
     155        $toReturn['valeurs'] = $wpdb->get_results($sql, ARRAY_A);
     156
     157    return $toReturn;
     158}
     159
    93160function checkIsTorConnexion(){
    94161
     162    global $wpdb;
     163   
    95164    $ip = $myIp = $myPort = 0;
    96165   
     
    109178    }
    110179
    111     $isTor = torel_check($ip, $myPort, $myIp);
    112 
    113     // use $istor as needed for altering page behavior:
    114     if( $isTor < 0){
    115     }
    116     else if ($isTor) {
    117        
    118       echo '<strong>Connections from the Tor network are not allowed on this website.</strong>';
    119       wp_die();
     180    $returnsSql = isInLog( $ip );
     181   
     182   
     183   
     184    if( $returnsSql['num_rows'] > 0 ){
     185       
     186        if( $returnsSql['valeurs'][0]['is_tor'] == 1 ){
     187           
     188            echo '<strong>Connections from the Tor network are not allowed on this website.</strong>';
     189          wp_die();
     190        }
     191       
     192    }
     193    else{
     194       
     195        $isTor = torel_check($ip, $myPort, $myIp);
     196
     197        // use $istor as needed for altering page behavior:
     198        if( $isTor < 0){
     199           
     200           
     201                $wpdb->insert( $wpdb->prefix.'inazo_tor_logs', array( 'ip_concern' => $ip, 'is_tor' => 0, 'date_of_log' => current_time('mysql', 1) ), array( '%s', '%d' ) );
     202           
     203        }
     204        else if ($isTor) {
     205           
     206           
     207                $wpdb->insert( $wpdb->prefix.'inazo_tor_logs', array( 'ip_concern' => $ip, 'is_tor' => 1, 'date_of_log' => current_time('mysql', 1) ), array( '%s', '%d' ) );
     208          echo '<strong>Connections from the Tor network are not allowed on this website.</strong>';
     209          wp_die();
     210        }
    120211    }
    121212}
    122213
    123214register_activation_hook( __FILE__, 'installPluginTor' );
     215register_deactivation_hook(__FILE__, 'unactivatePlugin');
     216
     217function unactivatePlugin(){
     218   
     219    wp_clear_scheduled_hook('inazo_tor_clean_logs');
     220}
    124221
    125222add_action( 'setup_theme', 'checkIsTorConnexion' );
     223
     224add_action('inazo_tor_clean_logs', 'inazo_tor_clean_logs_task');
     225
     226function inazo_tor_clean_logs_task() {
     227   
     228    global $wpdb;
     229   
     230    $wpdb->query('DELETE FROM '.$wpdb->prefix.'inazo_tor_logs WHERE (TIMESTAMPDIFF(MINUTE, date_of_log, NOW()) / 60 ) >= 3');
     231}
  • tor-blocker-by-inazo/trunk/readme.txt

    r1571791 r1590913  
    44Tags: tor, security, tor blocker, ip block, ip blocker, ip
    55Requires at least: 4.5.1
    6 Tested up to: 4.7
    7 Stable tag: 1.0
     6Tested up to: 4.7.2
     7Stable tag: 1.1
    88License: GPLv2
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    4545== CHANGELOG ==
    4646
     47= 1.1 =
     48* adding table in database for caching IP results and increase performance
     49* add clear log tables every three hours
     50
    4751= 1.0 =
    4852* init version
  • tor-blocker-by-inazo/trunk/uninstall.php

    r1571760 r1590913  
    1010   
    1111    delete_option('inazo.real.tor.blocker_db_version');
    12    
     12
     13    global $wpdb;
     14    $wpdb->query('DROP TABLE IF EXISTS '.$wpdb->prefix.'inazo_tor_logs');
     15    wp_clear_scheduled_hook('inazo_tor_clean_logs');
    1316?>
Note: See TracChangeset for help on using the changeset viewer.