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

Last change on this file since 29307 was 29307, checked in by mistic100, 10 years ago

compatibility 2.7

File size: 3.9 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_TABLE' , $prefixeTable . 'antiaspi_ip_ban');
22define('ANTIASPI_LOG_TABLE' , $prefixeTable . 'antiaspi_log');
23
24add_event_handler('loc_end_section_init', 'antiaspi');
25
26function antiaspi()
27{
28  global $user, $conf, $page;
29 
30  $antiaspi = array(
31    'diff' => '20 pages in 00:00:10' , // IP bannie si 20 pages différentes vues en 10 secondes
32    'same' => '15 pages in 00:00:30' , // IP bannie si 15 pages identiques vues en 30 secondes
33    'banned during' => '23:59:59' ,    // IP bannie pendant hh:mm:ss
34    'only guest' => true ,             // si true, ne banni pas les utilisateurs enregistrés
35    'only picture' => false ,          // si true, ne compatibilise que les pages d'images
36    'allowed ip' => array()            // tableau d'adresse ip autorisées (robots par exemple)
37  );
38 
39  if (isset($conf['antiaspi']))
40  {
41    $antiaspi = array_merge($antiaspi, $conf['antiaspi']);
42  }
43
44  if (is_admin() or ($antiaspi['only guest'] and !is_a_guest())) return;
45 
46  $Vip = $_SERVER["REMOTE_ADDR"];
47 
48  // Traitement des adresse ip autorisées
49  if (!empty($antiaspi['allowed ip']))
50  {
51    $allowed_ips = str_replace(array('.', '%'), array('\.', '.*?'), $antiaspi['allowed ip']);
52    foreach ($allowed_ips as $ip)
53    {
54      if (preg_match("#" . $ip . "#", $Vip)) return;
55    }
56  }
57
58  // cherche si le visiteur est interdit
59  $query = 'SELECT ip
60    FROM ' . ANTIASPI_TABLE . '
61    WHERE ip="' . $Vip . '"
62    AND date > ADDTIME(NOW(), "-' . $antiaspi['banned during'] . '");';
63
64  $result = pwg_query($query);
65
66  while(list($ip) = pwg_db_fetch_row($result))
67  {
68    // Visiteur trouvé dans les IP interdites
69    die("IP " . $ip . " banned for abuse.");
70  }
71 
72  $diff_conf = explode(' pages in ', $antiaspi['diff']);
73  $same_conf = explode(' pages in ', $antiaspi['same']);
74 
75  // nombre de fois ou le visiteur est passé dans les xxx dernières hh:mm:ss
76  $query = '
77SELECT
78    COUNT(*)
79  FROM ' . ANTIASPI_LOG_TABLE . '
80  WHERE ip="' . $Vip . '"
81    AND occured_on > ADDTIME(NOW(), "-' . $diff_conf[1] . '")
82    ' . ($antiaspi['only picture'] ? 'AND image_id IS NOT NULL' : '') . '
83
84UNION ALL
85
86SELECT
87    COUNT(*)
88  FROM ' . ANTIASPI_LOG_TABLE . '
89  WHERE ip="' . $Vip . '"
90    AND occured_on > ADDTIME(NOW(), "-' . $same_conf[1] . '")
91    AND category_id ' . (isset($page['category']['id']) ? '= ' . $page['category']['id'] : 'IS NULL') . '
92    AND image_id ' . (isset($page['image_id']) ? '= ' . $page['image_id'] : 'IS NULL') . '
93;';
94 
95  $result = pwg_query($query);
96
97  list($diff) = pwg_db_fetch_row($result);
98  list($same) = pwg_db_fetch_row($result);
99 
100  // si limite atteinte  ajouter dans la table des ip interdites.
101  if ($diff >= $diff_conf[0] or $same >= $same_conf[0])
102  {
103    pwg_query('INSERT INTO ' . ANTIASPI_TABLE . ' (id, ip, date) values ("", "' . $Vip . '", NOW())');
104  }
105
106  $insert = '
107INSERT
108  INTO '.ANTIASPI_LOG_TABLE.'
109  SET IP = \''.$Vip.'\'
110    , occured_on = NOW()
111    , image_id = '.(isset($page['image_id']) ? $page['image_id'] : 'NULL').'
112    , category_id = '.(isset($page['category']['id']) ? $page['category']['id'] : 'NULL').'
113;';
114  pwg_query($insert);
115
116  // automatic purge
117  $query = '
118DELETE
119  FROM '.ANTIASPI_LOG_TABLE.'
120  WHERE occured_on < LEAST(SUBTIME(NOW(), \''.$diff_conf[1].'\'), SUBTIME(NOW(), \''.$same_conf[1].'\'))
121;';
122  pwg_query($query);
123}
Note: See TracBrowser for help on using the repository browser.