Index: /extensions/AntiAspi/main.inc.php
===================================================================
--- /extensions/AntiAspi/main.inc.php	(revision 28322)
+++ /extensions/AntiAspi/main.inc.php	(revision 28323)
@@ -2,8 +2,8 @@
 /*
 Plugin Name: AntiAspi
-Version: 2.0.b
+Version: auto
 Description: Bloque les aspirateurs de sites
-Plugin URI: http://phpwebgallery.net/ext/extension_view.php?eid=225
-Author: P@t
+Plugin URI: http://piwigo.org/ext/extension_view.php?eid=225
+Author: P@t + plg
 Author URI: http://www.gauchon.com
 */
@@ -13,5 +13,29 @@
 global $prefixeTable;
 
+// +-----------------------------------------------------------------------+
+// | Define plugin constants                                               |
+// +-----------------------------------------------------------------------+
+
+define('ANTIASPI_ID', basename(dirname(__FILE__)));
+define('ANTIASPI_PATH', PHPWG_PLUGINS_PATH.ANTIASPI_ID.'/');
+define('ANTIASPI_VERSION', 'auto');
 define('ANTIASPI_TABLE' , $prefixeTable . 'antiaspi_ip_ban');
+define('ANTIASPI_LOG_TABLE' , $prefixeTable . 'antiaspi_log');
+
+// init the plugin
+add_event_handler('init', 'antiaspi_init');
+
+/**
+ * plugin initialization
+ *   - check for upgrades
+ *   - load language
+ */
+function antiaspi_init()
+{
+  // apply upgrade if needed
+  include_once(ANTIASPI_PATH.'maintain.inc.php');
+  $maintain = new AntiAspi_maintain(ANTIASPI_ID);
+  $maintain->autoUpdate(ANTIASPI_VERSION, 'install');
+}
 
 add_event_handler('loc_end_section_init', 'antiaspi');
@@ -67,20 +91,22 @@
   
   // nombre de fois ou le visiteur est passé dans les xxx dernières hh:mm:ss
-  $query = 'SELECT COUNT(*)
-    FROM ' . HISTORY_TABLE . '
-    WHERE ip="' . $Vip . '"
-    AND date = CURDATE()
-    AND time > ADDTIME(CURTIME(), "-' . $diff_conf[1] . '")
+  $query = '
+SELECT
+    COUNT(*)
+  FROM ' . ANTIASPI_LOG_TABLE . '
+  WHERE ip="' . $Vip . '"
+    AND occured_on > ADDTIME(NOW(), "-' . $diff_conf[1] . '")
     ' . ($antiaspi['only picture'] ? 'AND image_id IS NOT NULL' : '') . '
 
-    UNION ALL
+UNION ALL
 
-    SELECT COUNT(*)
-    FROM ' . HISTORY_TABLE . '
-    WHERE ip="' . $Vip . '"
-    AND date = CURDATE()
-    AND time > ADDTIME(CURTIME(), "-' . $same_conf[1] . '")
+SELECT
+    COUNT(*)
+  FROM ' . ANTIASPI_LOG_TABLE . '
+  WHERE ip="' . $Vip . '"
+    AND occured_on > ADDTIME(NOW(), "-' . $same_conf[1] . '")
     AND category_id ' . (isset($page['category']['id']) ? '= ' . $page['category']['id'] : 'IS NULL') . '
-    AND image_id ' . (isset($page['image_id']) ? '= ' . $page['image_id'] : 'IS NULL') . ';';
+    AND image_id ' . (isset($page['image_id']) ? '= ' . $page['image_id'] : 'IS NULL') . '
+;';
   
   $result = pwg_query($query);
@@ -94,5 +120,22 @@
     pwg_query('INSERT INTO ' . ANTIASPI_TABLE . ' (id, ip, date) values ("", "' . $Vip . '", NOW())');
   }
+
+  $insert = '
+INSERT
+  INTO '.ANTIASPI_LOG_TABLE.'
+  SET IP = \''.$Vip.'\'
+    , occured_on = NOW()
+    , image_id = '.(isset($page['image_id']) ? $page['image_id'] : 'NULL').'
+    , category_id = '.(isset($page['category']['id']) ? $page['category']['id'] : 'NULL').'
+;';
+  pwg_query($insert);
+
+  // automatic purge
+  $query = '
+DELETE
+  FROM '.ANTIASPI_LOG_TABLE.'
+  WHERE occured_on < LEAST(SUBTIME(NOW(), \''.$diff_conf[1].'\'), SUBTIME(NOW(), \''.$same_conf[1].'\'))
+;';
+  pwg_query($query);
 }
-
 ?>
Index: /extensions/AntiAspi/maintain.inc.php
===================================================================
--- /extensions/AntiAspi/maintain.inc.php	(revision 28322)
+++ /extensions/AntiAspi/maintain.inc.php	(revision 28323)
@@ -1,32 +1,72 @@
 <?php
-
 if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
 
-function plugin_install()
+class AntiAspi_maintain extends PluginMaintain
 {
-  global $prefixeTable;
+  private $installed = false;
 
-  $query = 'SHOW TABLES LIKE "' . $prefixeTable . 'antiaspi_ip_ban"';
-  $result = pwg_query($query);
-  if (!mysql_fetch_row($result))
+  function __construct($plugin_id)
   {
-    $q = "CREATE TABLE " . $prefixeTable . "antiaspi_ip_ban (
-      id int(11) NOT NULL auto_increment,
-      ip char(50) NOT NULL default '',
-      date char(20) NOT NULL default '',
-      PRIMARY KEY (id),
-      KEY ip (ip))
-    DEFAULT CHARACTER SET utf8";
-    pwg_query($q);
+    parent::__construct($plugin_id);
+  }
+
+  function install($plugin_version, &$errors=array())
+  {
+    global $prefixeTable;
+
+    // table antiaspi_ip_ban to store list of banned IP addresses
+    $result = pwg_query('SHOW TABLES LIKE "'.$prefixeTable.'antiaspi_ip_ban";');
+    if (!pwg_db_num_rows($result))
+    {
+      $query = "
+CREATE TABLE " . $prefixeTable . "antiaspi_ip_ban (
+  id int(11) NOT NULL auto_increment,
+  ip char(50) NOT NULL default '',
+  date char(20) NOT NULL default '',
+  PRIMARY KEY (id),
+  KEY ip (ip)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8
+;";
+      pwg_query($query);
+    }
+
+    // table antiaspi_log to store a specific "short-term" log
+    // (ENGINE=MEMORY, specific)
+    $result = pwg_query('SHOW TABLES LIKE "'.$prefixeTable.'antiaspi_log";');
+    if (!pwg_db_num_rows($result))
+    {
+      $query = "
+CREATE TABLE " . $prefixeTable . "antiaspi_log (
+  IP varchar(15) NOT NULL default '',
+  occured_on datetime NOT NULL,
+  image_id mediumint(8) default NULL,
+  category_id smallint(5) default NULL
+) ENGINE=MEMORY DEFAULT CHARSET=utf8
+;";
+      pwg_query($query);
+    }
+
+    $this->installed = true;
+  }
+
+  function activate($plugin_version, &$errors=array())
+  {
+    if (!$this->installed)
+    {
+      $this->install($plugin_version, $errors);
+    }
+  }
+
+  function deactivate()
+  {
+  }
+
+  function uninstall()
+  {
+    global $prefixeTable;
+
+    pwg_query('DROP TABLE '.$prefixeTable.'antiaspi_ip_ban;');
+    pwg_query('DROP TABLE '.$prefixeTable.'antiaspi_log;');
   }
 }
-
-function plugin_uninstall()
-{
-  global $prefixeTable;
-
-	$q = 'DROP TABLE ' . $prefixeTable . 'antiaspi_ip_ban;';
-  pwg_query($q);
-}
-
 ?>
