source: extensions/AntiAspi/main.inc.php

Last change on this file was 32689, checked in by plg, 3 years ago

compatibility Piwigo 12

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