Index: /trunk/admin/include/functions_install.inc.php
===================================================================
--- /trunk/admin/include/functions_install.inc.php	(revision 5981)
+++ /trunk/admin/include/functions_install.inc.php	(revision 5982)
@@ -34,5 +34,5 @@
  * @return void
  */
-function execute_sqlfile($filepath, $replaced, $replacing)
+function execute_sqlfile($filepath, $replaced, $replacing, $dblayer)
 {
   $sql_lines = file($filepath);
@@ -55,10 +55,9 @@
       if (!preg_match('/^DROP TABLE/i', $query))
       {
-        global $install_charset_collate;
-        if ( !empty($install_charset_collate) )
+        if ('mysql' == $dblayer)
         {
           if ( preg_match('/^(CREATE TABLE .*)[\s]*;[\s]*/im', $query, $matches) )
           {
-            $query = $matches[1].' '.$install_charset_collate.';';
+            $query = $matches[1].' DEFAULT CHARACTER SET utf8'.';';
           }
         }
@@ -126,9 +125,9 @@
 
 /**
- * Automatically activate all themes in the "themes" directory.
+ * Automatically activate all core themes in the "themes" directory.
  *
  * @return void
  */
-function activate_all_themes()
+function activate_core_themes()
 {
   include_once(PHPWG_ROOT_PATH.'admin/include/themes.class.php');
@@ -136,5 +135,8 @@
   foreach ($themes->fs_themes as $theme_id => $fs_theme)
   {
-    $themes->perform_action('activate', $theme_id);
+    if (in_array($theme_id, array('Sylvia', 'clear', 'dark')))
+    {
+      $themes->perform_action('activate', $theme_id);
+    }
   }
 }
@@ -144,8 +146,9 @@
   try
   {
-    $pwg_db_link = pwg_db_connect($_POST['dbhost'], $_POST['dbuser'], 
-                                  $_POST['dbpasswd'], $_POST['dbname']);
- 
-    return $pwg_db_link;
+    $pwg_db_link = pwg_db_connect($_POST['dbhost'], $_POST['dbuser'], $_POST['dbpasswd'], $_POST['dbname']);
+    if ($pwg_db_link)
+    {
+      pwg_db_check_version();
+    }
   }
   catch (Exception $e)
@@ -153,5 +156,4 @@
     array_push( $errors, l10n($e->getMessage()));
   }
-  return false;
 }
 ?>
Index: /trunk/admin/include/functions_upgrade.php
===================================================================
--- /trunk/admin/include/functions_upgrade.php	(revision 5981)
+++ /trunk/admin/include/functions_upgrade.php	(revision 5982)
@@ -77,6 +77,4 @@
 
   $standard_plugins = array(
-    'add_index',
-    'admin_advices',
     'admin_multi_view',
     'c13y_upgrade',
@@ -222,6 +220,9 @@
   try
   {
-    $pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'],
-                                  $conf['db_password'], $conf['db_base']);
+    $pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'], $conf['db_password'], $conf['db_base']);
+    if ($pwg_db_link)
+    {
+      pwg_db_check_version();
+    }
   }
   catch (Exception $e)
@@ -231,3 +232,32 @@
 }
 
+/**
+ *  Get languages defined in the language directory
+ */  
+function get_fs_languages($target_charset = null)
+{
+  if ( empty($target_charset) )
+  {
+    $target_charset = get_pwg_charset();
+  }
+  $target_charset = strtolower($target_charset);
+  
+  $dir = opendir(PHPWG_ROOT_PATH.'language');
+  
+  while ($file = readdir($dir))
+  {
+    $path = PHPWG_ROOT_PATH.'language/'.$file;
+    if (!is_link($path) and is_dir($path) and file_exists($path.'/iso.txt'))
+    {
+      list($language_name) = @file($path.'/iso.txt');
+      
+      $languages[$file] = convert_charset($language_name, $target_charset);
+    }
+  }
+  closedir($dir);
+  @asort($languages);
+  
+  return $languages;
+}
+
 ?>
Index: /trunk/include/dblayer/functions_mysql.inc.php
===================================================================
--- /trunk/include/dblayer/functions_mysql.inc.php	(revision 5981)
+++ /trunk/include/dblayer/functions_mysql.inc.php	(revision 5982)
@@ -53,16 +53,24 @@
 function pwg_db_check_charset() 
 {
-  defined('PWG_CHARSET') and defined('DB_CHARSET')
-    or fatal_error('PWG_CHARSET and/or DB_CHARSET is not defined');
-  if ( version_compare(mysql_get_server_info(), '4.1.0', '>=') )
-  {
-    if (DB_CHARSET!='')
-    {
-      pwg_query('SET NAMES "'.DB_CHARSET.'"');
-    }
-  }
-  elseif ( strtolower(PWG_CHARSET)!='iso-8859-1' )
-  {
-    fatal_error('PWG supports only iso-8859-1 charset on MySql version '.mysql_get_server_info());
+  $db_charset = 'utf8';
+  if (defined('DB_CHARSET') and DB_CHARSET != '')
+  {
+    $db_charset = DB_CHARSET;
+  }
+  pwg_query('SET NAMES "'.$db_charset.'"');
+}
+
+function pwg_db_check_version()
+{
+  $current_mysql = pwg_get_db_version();
+  if (version_compare($current_mysql, REQUIRED_MYSQL_VERSION, '<'))
+  {
+    fatal_error(
+      sprintf(
+        'your MySQL version is too old, you have "%s" and you need at least "%s"',
+        $current_mysql,
+        REQUIRED_MYSQL_VERSION
+        )
+      );
   }
 }
Index: /trunk/include/functions.inc.php
===================================================================
--- /trunk/include/functions.inc.php	(revision 5981)
+++ /trunk/include/functions.inc.php	(revision 5982)
@@ -695,5 +695,5 @@
   while ($row = pwg_db_fetch_assoc($result))
   {
-    if (file_exists($conf['themes_dir'].'/'.$row['id'].'/'.'themeconf.inc.php'))
+    if (check_theme_installed($row['id']))
     {
       $themes[ $row['id'] ] = $row['name'];
@@ -705,4 +705,11 @@
 
   return $themes;
+}
+
+function check_theme_installed($theme_id)
+{
+  global $conf;
+
+  return file_exists($conf['themes_dir'].'/'.$theme_id.'/'.'themeconf.inc.php');
 }
 
@@ -1149,6 +1156,10 @@
 function get_pwg_charset()
 {
-  defined('PWG_CHARSET') or fatal_error('PWG_CHARSET undefined');
-  return PWG_CHARSET;
+  $pwg_charset = 'utf-8';
+  if (defined('PWG_CHARSET'))
+  {
+    $pwg_charset = PWG_CHARSET;
+  }
+  return $pwg_charset;
 }
 
Index: /trunk/include/functions_user.inc.php
===================================================================
--- /trunk/include/functions_user.inc.php	(revision 5981)
+++ /trunk/include/functions_user.inc.php	(revision 5982)
@@ -849,5 +849,19 @@
 function get_default_theme()
 {
-  return get_default_user_value('theme', PHPWG_DEFAULT_TEMPLATE);
+  $theme = get_default_user_value('theme', PHPWG_DEFAULT_TEMPLATE);
+  if (check_theme_installed($theme))
+  {
+    return $theme;
+  }
+  
+  // let's find the first available theme
+  $active_themes = get_pwg_themes();
+  foreach (array_keys(get_pwg_themes()) as $theme_id)
+  {
+    if (check_theme_installed($theme_id))
+    {
+      return $theme_id;
+    }
+  }
 }
 
Index: /trunk/install.php
===================================================================
--- /trunk/install.php	(revision 5981)
+++ /trunk/install.php	(revision 5982)
@@ -263,32 +263,6 @@
 if ( isset( $_POST['install'] ))
 {
-  if ($pwg_db_link = install_db_connect($infos, $errors))
-  {
-    $required_version = constant('REQUIRED_'.strtoupper($dblayer).'_VERSION');
-    if ( version_compare(pwg_get_db_version(), $required_version, '>=') )
-    {
-      $pwg_charset = 'utf-8';
-      $pwg_db_charset = 'utf8';
-      if ($dblayer=='mysql')
-      {
-        $install_charset_collate = "DEFAULT CHARACTER SET $pwg_db_charset";
-        pwg_query('SET NAMES "'.$pwg_db_charset.'"');
-      }
-      else 
-      {
-        $install_charset_collate = '';
-      }
-    }
-    else
-    {
-      $pwg_charset = 'iso-8859-1';
-      $pwg_db_charset = 'latin1';
-      $install_charset_collate = '';
-      if ( !array_key_exists($language, $languages->get_fs_languages($pwg_charset) ) )
-      {
-        $language='en_UK';
-      }
-    }
-  }
+  install_db_connect($infos, $errors);
+  pwg_db_check_charset();
 
   $webmaster = trim(preg_replace( '/\s{2,}/', ' ', $admin_name ));
@@ -321,6 +295,6 @@
 
 define(\'PHPWG_INSTALLED\', true);
-define(\'PWG_CHARSET\', \''.$pwg_charset.'\');
-define(\'DB_CHARSET\', \''.$pwg_db_charset.'\');
+define(\'PWG_CHARSET\', \'utf-8\');
+define(\'DB_CHARSET\', \'utf8\');
 define(\'DB_COLLATE\', \'\');
 
@@ -351,5 +325,6 @@
       PHPWG_ROOT_PATH.'install/piwigo_structure-'.$dblayer.'.sql',
       DEFAULT_PREFIX_TABLE,
-      $prefixeTable
+      $prefixeTable,
+      $dblayer
       );
     // We fill the tables with basic informations
@@ -357,5 +332,6 @@
       PHPWG_ROOT_PATH.'install/config.sql',
       DEFAULT_PREFIX_TABLE,
-      $prefixeTable
+      $prefixeTable,
+      $dblayer
       );
 
@@ -367,5 +343,5 @@
 
     // fill languages table
-    foreach ($languages->get_fs_languages($pwg_charset) as $language_code => $language_name)
+    foreach ($languages->get_fs_languages() as $language_code => $language_name)
     {
       $languages->perform_action('activate', $language_code);
@@ -379,7 +355,7 @@
     if (!defined('PWG_CHARSET'))
     {
-      define('PWG_CHARSET', $pwg_charset);
-    }
-    activate_all_themes();
+      define('PWG_CHARSET', 'utf-8');
+    }
+    activate_core_themes();
 
     $insert = array(
Index: /trunk/install/db/65-database.php
===================================================================
--- /trunk/install/db/65-database.php	(revision 5981)
+++ /trunk/install/db/65-database.php	(revision 5982)
@@ -166,5 +166,5 @@
   $query = 'SHOW TABLES LIKE "'.$prefixeTable.'%"';
   $result = pwg_query($query);
-  while ( $row=pwg_db_fetch_assoc($result) )
+  while ( $row=pwg_db_fetch_row($result) )
   {
     array_push($all_tables, $row[0]);
Index: /trunk/install/db/86-database.php
===================================================================
--- /trunk/install/db/86-database.php	(revision 5981)
+++ /trunk/install/db/86-database.php	(revision 5982)
@@ -27,38 +27,8 @@
 }
 
-$upgrade_description = 'Automatically activate core themes and used themes.';
+$upgrade_description = 'Automatically activate core themes.';
 
-$themes_core = array('Sylvia', 'dark', 'clear');
-
-$query = '
-SELECT
-    DISTINCT(theme)
-  FROM '.PREFIX_TABLE.'user_infos
-;';
-$themes_used = array_from_query($query, 'theme');
-
-$query = '
-SELECT
-    id
-  FROM '.PREFIX_TABLE.'themes
-;';
-$themes_active = array_from_query($query, 'id');
-
-
-$themes_to_activate = array_diff(
-  array_unique(array_merge($themes_used, $themes_core)),
-  $themes_active
-  );
-
-// echo '<pre>'; print_r($themes_to_activate); echo '</pre>'; exit();
-
-foreach ($themes_to_activate as $theme)
-{
-  $query = '
-INSERT INTO '.PREFIX_TABLE.'themes
-  (id) VALUES(\''.$theme.'\'
-;';
-  pwg_query($query);
-}
+include_once(PHPWG_ROOT_PATH . 'admin/include/functions_install.inc.php');
+activate_core_themes();
 
 echo
Index: /trunk/install/upgrade_1.7.0.php
===================================================================
--- /trunk/install/upgrade_1.7.0.php	(revision 5981)
+++ /trunk/install/upgrade_1.7.0.php	(revision 5982)
@@ -33,9 +33,4 @@
   }
 }
-
-define('UPGRADES_PATH', PHPWG_ROOT_PATH.'install/db');
-
-list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
-define('CURRENT_DATE', $dbnow);
 
 // +-----------------------------------------------------------------------+
@@ -89,5 +84,5 @@
 echo '<pre>';
 
-for ($upgrade_id = 61; ; $upgrade_id++)
+for ($upgrade_id = 61; $upgrade_id <= 79; $upgrade_id++)
 {
   if (!file_exists(UPGRADES_PATH.'/'.$upgrade_id.'-database.php'))
@@ -120,4 +115,4 @@
 
 // now we upgrade from 2.0.0
-// include_once(PHPWG_ROOT_PATH.'install/upgrade_2.0.0.php');
+include_once(PHPWG_ROOT_PATH.'install/upgrade_2.0.0.php');
 ?>
Index: /trunk/install/upgrade_2.0.0.php
===================================================================
--- /trunk/install/upgrade_2.0.0.php	(revision 5982)
+++ /trunk/install/upgrade_2.0.0.php	(revision 5982)
@@ -0,0 +1,118 @@
+<?php
+// +-----------------------------------------------------------------------+
+// | Piwigo - a PHP based picture gallery                                  |
+// +-----------------------------------------------------------------------+
+// | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
+// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
+// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
+// +-----------------------------------------------------------------------+
+// | This program is free software; you can redistribute it and/or modify  |
+// | it under the terms of the GNU General Public License as published by  |
+// | the Free Software Foundation                                          |
+// |                                                                       |
+// | This program is distributed in the hope that it will be useful, but   |
+// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
+// | General Public License for more details.                              |
+// |                                                                       |
+// | You should have received a copy of the GNU General Public License     |
+// | along with this program; if not, write to the Free Software           |
+// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
+// | USA.                                                                  |
+// +-----------------------------------------------------------------------+
+
+if (!defined('PHPWG_ROOT_PATH'))
+{
+  die ('This page cannot be loaded directly, load upgrade.php');
+}
+else
+{
+  if (!defined('PHPWG_IN_UPGRADE') or !PHPWG_IN_UPGRADE)
+  {
+    die ('Hacking attempt!');
+  }
+}
+
+// +-----------------------------------------------------------------------+
+// |             Fill upgrade table without applying upgrade               |
+// +-----------------------------------------------------------------------+
+
+// retrieve already applied upgrades
+$query = '
+SELECT id
+  FROM '.PREFIX_TABLE.'upgrade
+;';
+$applied = array_from_query($query, 'id');
+
+// retrieve existing upgrades
+$existing = get_available_upgrade_ids();
+
+// which upgrades need to be applied?
+$to_apply = array_diff($existing, $applied);
+$inserts = array();
+foreach ($to_apply as $upgrade_id)
+{
+  if ($upgrade_id >= 81)
+  {
+    break;
+  }
+  
+  array_push(
+    $inserts,
+    array(
+      'id' => $upgrade_id,
+      'applied' => CURRENT_DATE,
+      'description' => '[migration from 2.0.0 to '.PHPWG_VERSION.'] not applied',
+      )
+    );
+}
+
+if (!empty($inserts))
+{
+  mass_inserts(
+    '`'.UPGRADE_TABLE.'`',
+    array_keys($inserts[0]),
+    $inserts
+    );
+}
+
+// +-----------------------------------------------------------------------+
+// |                          Perform upgrades                             |
+// +-----------------------------------------------------------------------+
+
+ob_start();
+echo '<pre>';
+
+for ($upgrade_id = 81; $upgrade_id <= 90; $upgrade_id++)
+{
+  if (!file_exists(UPGRADES_PATH.'/'.$upgrade_id.'-database.php'))
+  {
+    break;
+  }
+  
+  unset($upgrade_description);
+
+  echo "\n\n";
+  echo '=== upgrade '.$upgrade_id."\n";
+
+  // include & execute upgrade script. Each upgrade script must contain
+  // $upgrade_description variable which describe briefly what the upgrade
+  // script does.
+  include(UPGRADES_PATH.'/'.$upgrade_id.'-database.php');
+
+  // notify upgrade
+  $query = '
+INSERT INTO `'.PREFIX_TABLE.'upgrade`
+  (id, applied, description)
+  VALUES
+  (\''.$upgrade_id.'\', NOW(), \'[migration from 2.0.0 to '.PHPWG_VERSION.'] '.$upgrade_description.'\')
+;';
+  pwg_query($query);
+}
+
+echo '</pre>';
+ob_end_clean();
+
+// now we upgrade from 2.1.0
+// include_once(PHPWG_ROOT_PATH.'install/upgrade_2.1.0.php');
+?>
Index: /trunk/upgrade.php
===================================================================
--- /trunk/upgrade.php	(revision 5981)
+++ /trunk/upgrade.php	(revision 5982)
@@ -48,4 +48,5 @@
 include_once(PHPWG_ROOT_PATH.'include/constants.php');
 define('PREFIX_TABLE', $prefixeTable);
+define('UPGRADES_PATH', PHPWG_ROOT_PATH.'install/db');
 
 // +-----------------------------------------------------------------------+
@@ -202,6 +203,8 @@
 
 upgrade_db_connect();
-
 pwg_db_check_charset();
+
+list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
+define('CURRENT_DATE', $dbnow);
 
 // +-----------------------------------------------------------------------+
@@ -261,4 +264,8 @@
   $current_release = '1.7.0';
 }
+else if (!in_array(PREFIX_TABLE.'themes', $tables))
+{
+  $current_release = '2.0.0';
+}
 else
 {
@@ -297,10 +304,13 @@
       if (!@file_put_contents($config_file, $config_file_contents))
       {
-        array_push($page['infos'],
-		   l10n_args('In <i>%s</i>, before <b>?></b>, insert:', 
-			     'local/config/database.inc.php') . 
-		   '<p><textarea rows="4" cols="40">' .
-		   implode("\r\n" , $mysql_changes).'</textarea></p>'
-		   );
+        array_push(
+          $page['infos'],
+          sprintf(
+            l10n('In <i>%s</i>, before <b>?></b>, insert:'),
+            'local/config/database.inc.php'
+            )
+          .'<p><textarea rows="4" cols="40">'
+          .implode("\r\n" , $mysql_changes).'</textarea></p>'
+          );
       }
     }
@@ -368,5 +378,13 @@
 else
 {
-  foreach (get_languages('utf-8') as $language_code => $language_name)
+  if (!defined('PWG_CHARSET'))
+  {
+    define('PWG_CHARSET', 'utf-8');
+  }
+
+  include_once(PHPWG_ROOT_PATH.'admin/include/languages.class.php');
+  $languages = new languages();
+  
+  foreach ($languages->fs_languages as $language_code => $language_name)
   {
     if ($language == $language_code)
