Index: extensions/GuestBook/main.inc.php
===================================================================
--- extensions/GuestBook/main.inc.php	(revision 21292)
+++ extensions/GuestBook/main.inc.php	(revision 24889)
@@ -4,5 +4,5 @@
 Version: auto
 Description: Add a guestbook to the gallery
-Plugin URI: http://piwigo.org/ext/extension_view.php?eid=609
+Plugin URI: auto
 Author: Mistic
 Author URI: http://www.strangeplanet.fr
@@ -14,99 +14,70 @@
 
 
-define('GUESTBOOK_PATH' ,  PHPWG_PLUGINS_PATH . basename(dirname(__FILE__)) . '/');
-define('GUESTBOOK_TABLE' , $prefixeTable . 'guestbook');
-define('GUESTBOOK_ADMIN',  get_root_url().'admin.php?page=plugin-' . basename(dirname(__FILE__)));
-define('GUESTBOOK_URL',    get_absolute_root_url() . make_index_url(array('section' => 'guestbook')));
+defined('GUESTBOOK_ID') or define('GUESTBOOK_ID', basename(dirname(__FILE__)));
+define('GUESTBOOK_PATH' ,   PHPWG_PLUGINS_PATH . GUESTBOOK_ID . '/');
+define('GUESTBOOK_TABLE' ,  $prefixeTable . 'guestbook');
+define('GUESTBOOK_ADMIN',   get_root_url().'admin.php?page=plugin-' . GUESTBOOK_ID);
+define('GUESTBOOK_URL',     get_absolute_root_url() . make_index_url(array('section' => 'guestbook')));
+define('GUESTBOOK_VERSION', 'auto');
 
-add_event_handler('init', 'gb_init');
 
-function gb_init()
+include_once(GUESTBOOK_PATH . 'include/events.inc.php');
+
+add_event_handler('init', 'guestbook_init');
+
+// admin page
+if (defined('IN_ADMIN'))
 {
-  global $conf;
+  add_event_handler('get_admin_plugin_menu_links', 'gb_admin_menu');
+}
+
+// menu entry
+add_event_handler('blockmanager_apply', 'gb_menubar_apply', EVENT_HANDLER_PRIORITY_NEUTRAL+10);
+
+// guestbook section
+add_event_handler('loc_end_section_init', 'gb_section_init');
+add_event_handler('loc_end_index', 'gb_index');
+
+// stuff
+// add_event_handler('get_stuffs_modules', 'gb_register_stuffs_module')
+
+
+function guestbook_init()
+{
+  global $conf, $pwg_loaded_plugins;
   
-  load_language('plugin.lang', GUESTBOOK_PATH);
-  $conf['guestbook'] = unserialize($conf['guestbook']);
-  
-  // menubar
-  if (script_basename() != 'admin')
+  // apply upgrade if needed
+  if (
+    GUESTBOOK_VERSION == 'auto' or
+    $pwg_loaded_plugins[GUESTBOOK_ID]['version'] == 'auto' or
+    version_compare($pwg_loaded_plugins[GUESTBOOK_ID]['version'], GUESTBOOK_VERSION, '<')
+  )
   {
-    add_event_handler('blockmanager_apply', 'gb_menubar_apply', EVENT_HANDLER_PRIORITY_NEUTRAL+10);
-  }
-  else
-  {
-    add_event_handler('get_admin_plugin_menu_links', 'gb_admin_menu');
+    // call install function
+    include_once(GUESTBOOK_PATH . 'include/install.inc.php');
+    guestbook_install();
+    
+    // update plugin version in database
+    if ( $pwg_loaded_plugins[GUESTBOOK_ID]['version'] != 'auto' and GUESTBOOK_VERSION != 'auto' )
+    {
+      $query = '
+UPDATE '. PLUGINS_TABLE .'
+SET version = "'. GUESTBOOK_VERSION .'"
+WHERE id = "'. GUESTBOOK_ID .'"';
+      pwg_query($query);
+      
+      $pwg_loaded_plugins[GUESTBOOK_ID]['version'] = GUESTBOOK_VERSION;
+      
+      if (defined('IN_ADMIN'))
+      {
+        $_SESSION['page_infos'][] = 'GuestBook updated to version '. GUESTBOOK_VERSION;
+      }
+    }
   }
   
-  // guestbook section
-  add_event_handler('loc_end_section_init', 'gb_section_init');
-  add_event_handler('loc_end_index', 'gb_index');
+  // load plugin language file
+  load_language('plugin.lang', GUESTBOOK_PATH);
   
-  // stuff
-  // add_event_handler('get_stuffs_modules', 'gb_register_stuffs_module')
+  // prepare plugin configuration
+  $conf['guestbook'] = unserialize($conf['guestbook']);
 }
-
-function gb_menubar_apply($menu_ref_arr)
-{
-  $menu = &$menu_ref_arr[0];
-  
-  if ( ($block = $menu->get_block('mbMenu')) != null )
-  {
-    array_push($block->data, array(
-      'URL' => GUESTBOOK_URL,
-      'TITLE' => l10n('GuestBook'),
-      'NAME' => l10n('GuestBook')
-    ));
-  }
-}
-
-function gb_section_init()
-{
-  global $tokens, $page, $conf;
-
-  if ($tokens[0] == 'guestbook')
-  {
-    add_event_handler('loc_begin_page_header', 'gb_page_header');
-    
-    $page['section'] = 'guestbook';
-    $page['title'] = l10n('GuestBook');
-    $page['section_title'] = '<a href="'.get_gallery_home_url().'">'.l10n('Home').'</a>'.$conf['level_separator'].l10n('GuestBook');
-  }
-}
-
-function gb_page_header()
-{
-  global $page;
-  $page['body_id'] = 'theGuestBook';
-}
-
-function gb_index() 
-{
-  global $template, $page, $conf;
-
-  if (isset($page['section']) and $page['section'] == 'guestbook')
-  {
-    include(GUESTBOOK_PATH . '/include/guestbook.inc.php');
-  }
-}
-
-/*function gb_register_stuffs_module($modules)
-{
-  array_push($modules, array(
-    'path' => GUESTBOOK_PATH . '/stuffs_module',
-    'name' => GB_NAME,
-    'description' => l10n('gb_stuffs_desc'),
-  ));
-
-  return $modules;
-}*/
-
-function gb_admin_menu($menu) 
-{
-  array_push($menu, array(
-    'NAME' => 'GuestBook',
-    'URL' => GUESTBOOK_ADMIN,
-  ));
-  return $menu;
-}
-
-?>
