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