source: extensions/AntiAspi/main.inc.php @ 28566

Last change on this file since 28566 was 28323, checked in by plg, 10 years ago

huge speed improvement: use a dedicated short-term visit log instead of history table

File size: 4.2 KB
Line 
1<?php
2/*
3Plugin Name: AntiAspi
4Version: auto
5Description: Bloque les aspirateurs de sites
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=225
7Author: P@t + plg
8Author URI: http://www.gauchon.com
9*/
10
11if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
12
13global $prefixeTable;
14
15// +-----------------------------------------------------------------------+
16// | Define plugin constants                                               |
17// +-----------------------------------------------------------------------+
18
19define('ANTIASPI_ID', basename(dirname(__FILE__)));
20define('ANTIASPI_PATH', PHPWG_PLUGINS_PATH.ANTIASPI_ID.'/');
21define('ANTIASPI_VERSION', 'auto');
22define('ANTIASPI_TABLE' , $prefixeTable . 'antiaspi_ip_ban');
23define('ANTIASPI_LOG_TABLE' , $prefixeTable . 'antiaspi_log');
24
25// init the plugin
26add_event_handler('init', 'antiaspi_init');
27
28/**
29 * plugin initialization
30 *   - check for upgrades
31 *   - load language
32 */
33function antiaspi_init()
34{
35  // apply upgrade if needed
36  include_once(ANTIASPI_PATH.'maintain.inc.php');
37  $maintain = new AntiAspi_maintain(ANTIASPI_ID);
38  $maintain->autoUpdate(ANTIASPI_VERSION, 'install');
39}
40
41add_event_handler('loc_end_section_init', 'antiaspi');
42
43function antiaspi()
44{
45  global $user, $conf, $page;
46 
47  $antiaspi = array(
48    'diff' => '20 pages in 00:00:10' , // IP bannie si 20 pages différentes vues en 10 secondes
49    'same' => '15 pages in 00:00:30' , // IP bannie si 15 pages identiques vues en 30 secondes
50    'banned during' => '23:59:59' ,    // IP bannie pendant hh:mm:ss
51    'only guest' => true ,             // si true, ne banni pas les utilisateurs enregistrés
52    'only picture' => false ,          // si true, ne compatibilise que les pages d'images
53    'allowed ip' => array()            // tableau d'adresse ip autorisées (robots par exemple)
54  );
55 
56  if (isset($conf['antiaspi']))
57  {
58    $antiaspi = array_merge($antiaspi, $conf['antiaspi']);
59  }
60
61  if (is_admin() or ($antiaspi['only guest'] and !is_a_guest())) return;
62 
63  $Vip = $_SERVER["REMOTE_ADDR"];
64 
65  // Traitement des adresse ip autorisées
66  if (!empty($antiaspi['allowed ip']))
67  {
68    $allowed_ips = str_replace(array('.', '%'), array('\.', '.*?'), $antiaspi['allowed ip']);
69    foreach ($allowed_ips as $ip)
70    {
71      if (preg_match("#" . $ip . "#", $Vip)) return;
72    }
73  }
74
75  // cherche si le visiteur est interdit
76  $query = 'SELECT ip
77    FROM ' . ANTIASPI_TABLE . '
78    WHERE ip="' . $Vip . '"
79    AND date > ADDTIME(NOW(), "-' . $antiaspi['banned during'] . '");';
80
81  $result = pwg_query($query);
82
83  while(list($ip) = pwg_db_fetch_row($result))
84  {
85    // Visiteur trouvé dans les IP interdites
86    die("IP " . $ip . " banned for abuse.");
87  }
88 
89  $diff_conf = explode(' pages in ', $antiaspi['diff']);
90  $same_conf = explode(' pages in ', $antiaspi['same']);
91 
92  // nombre de fois ou le visiteur est passé dans les xxx dernières hh:mm:ss
93  $query = '
94SELECT
95    COUNT(*)
96  FROM ' . ANTIASPI_LOG_TABLE . '
97  WHERE ip="' . $Vip . '"
98    AND occured_on > ADDTIME(NOW(), "-' . $diff_conf[1] . '")
99    ' . ($antiaspi['only picture'] ? 'AND image_id IS NOT NULL' : '') . '
100
101UNION ALL
102
103SELECT
104    COUNT(*)
105  FROM ' . ANTIASPI_LOG_TABLE . '
106  WHERE ip="' . $Vip . '"
107    AND occured_on > ADDTIME(NOW(), "-' . $same_conf[1] . '")
108    AND category_id ' . (isset($page['category']['id']) ? '= ' . $page['category']['id'] : 'IS NULL') . '
109    AND image_id ' . (isset($page['image_id']) ? '= ' . $page['image_id'] : 'IS NULL') . '
110;';
111 
112  $result = pwg_query($query);
113
114  list($diff) = pwg_db_fetch_row($result);
115  list($same) = pwg_db_fetch_row($result);
116 
117  // si limite atteinte  ajouter dans la table des ip interdites.
118  if ($diff >= $diff_conf[0] or $same >= $same_conf[0])
119  {
120    pwg_query('INSERT INTO ' . ANTIASPI_TABLE . ' (id, ip, date) values ("", "' . $Vip . '", NOW())');
121  }
122
123  $insert = '
124INSERT
125  INTO '.ANTIASPI_LOG_TABLE.'
126  SET IP = \''.$Vip.'\'
127    , occured_on = NOW()
128    , image_id = '.(isset($page['image_id']) ? $page['image_id'] : 'NULL').'
129    , category_id = '.(isset($page['category']['id']) ? $page['category']['id'] : 'NULL').'
130;';
131  pwg_query($insert);
132
133  // automatic purge
134  $query = '
135DELETE
136  FROM '.ANTIASPI_LOG_TABLE.'
137  WHERE occured_on < LEAST(SUBTIME(NOW(), \''.$diff_conf[1].'\'), SUBTIME(NOW(), \''.$same_conf[1].'\'))
138;';
139  pwg_query($query);
140}
141?>
Note: See TracBrowser for help on using the repository browser.