source: trunk/upgrade.php @ 4730

Last change on this file since 4730 was 4385, checked in by nikrou, 15 years ago

Feature_1255 :

  • single quotes in queries
  • start using $confdblayer
  • Property svn:eol-style set to LF
File size: 11.3 KB
RevLine 
[1927]1<?php
2// +-----------------------------------------------------------------------+
[2297]3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
[3049]5// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
[2297]6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
[1927]23
24define('PHPWG_ROOT_PATH', './');
25
[2863]26// load config file
[4280]27$config_file = PHPWG_ROOT_PATH.'include/config_database.inc.php';
[2863]28$config_file_contents = @file_get_contents($config_file);
29if ($config_file_contents === false)
[2836]30{
[2863]31  die('Cannot load '.$config_file);
[2836]32}
[2863]33$php_end_tag = strrpos($config_file_contents, '?'.'>');
34if ($php_end_tag === false)
35{
36  die('Cannot find php end tag in '.$config_file);
37}
[2836]38
[1927]39include_once(PHPWG_ROOT_PATH.'include/functions.inc.php');
40include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
41include_once(PHPWG_ROOT_PATH.'admin/include/functions_upgrade.php');
42
[4280]43include(PHPWG_ROOT_PATH.'include/config_database.inc.php');
[1927]44include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
45@include(PHPWG_ROOT_PATH. 'include/config_local.inc.php');
[4385]46include(PHPWG_ROOT_PATH .'include/dblayer/functions_'.$conf['dblayer'].'.inc.php');
[1927]47
[2096]48prepare_conf_upgrade();
[1927]49
50include_once(PHPWG_ROOT_PATH.'include/constants.php');
51define('PREFIX_TABLE', $prefixeTable);
52
53// Database connection
[4325]54$pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'], 
55                              $conf['db_password'], $conf['db_base']) 
56  or my_error('pwg_db_connect', true);
[2512]57
[4325]58pwg_db_check_charset();
59
[1927]60// +-----------------------------------------------------------------------+
61// |                              functions                                |
62// +-----------------------------------------------------------------------+
63
64/**
65 * list all tables in an array
66 *
67 * @return array
68 */
69function get_tables()
70{
71  $tables = array();
[2290]72
[1927]73  $query = '
74SHOW TABLES
75;';
[2884]76  $result = pwg_query($query);
[1927]77
[4325]78  while ($row = pwg_db_fetch_row($result))
[1927]79  {
80    if (preg_match('/^'.PREFIX_TABLE.'/', $row[0]))
81    {
82      array_push($tables, $row[0]);
83    }
84  }
85
86  return $tables;
87}
88
89/**
90 * list all columns of each given table
91 *
92 * @return array of array
93 */
94function get_columns_of($tables)
95{
96  $columns_of = array();
97
98  foreach ($tables as $table)
99  {
100    $query = '
101DESC '.$table.'
102;';
[2884]103    $result = pwg_query($query);
[1927]104
105    $columns_of[$table] = array();
106
[4325]107    while ($row = pwg_db_fetch_row($result))
[1927]108    {
109      array_push($columns_of[$table], $row[0]);
110    }
111  }
112
113  return $columns_of;
114}
115
116/**
117 */
118function print_time($message)
119{
120  global $last_time;
[2290]121
[1927]122  $new_time = get_moment();
123  echo '<pre>['.get_elapsed_time($last_time, $new_time).']';
124  echo ' '.$message;
125  echo '</pre>';
126  flush();
127  $last_time = $new_time;
128}
129
130// +-----------------------------------------------------------------------+
131// |                             playing zone                              |
132// +-----------------------------------------------------------------------+
133
134// echo implode('<br>', get_tables());
135// echo '<pre>'; print_r(get_columns_of(get_tables())); echo '</pre>';
136
137// foreach (get_available_upgrade_ids() as $upgrade_id)
138// {
139//   echo $upgrade_id, '<br>';
140// }
141
142// +-----------------------------------------------------------------------+
[2819]143// |                             language                                  |
144// +-----------------------------------------------------------------------+
145if (isset($_GET['language']))
146{
147  $language = strip_tags($_GET['language']);
148}
149else
150{
151  $language = 'en_UK';
152  // Try to get browser language
153  foreach (get_languages('utf-8') as $language_code => $language_name)
154  {
155    if (substr($language_code,0,2) == @substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2))
156    {
157      $language = $language_code;
158      break;
159    }
160  }
161}
162
[3203]163if ('fr_FR' == $language) {
164  define('PHPWG_DOMAIN', 'fr.piwigo.org');
165}
166else {
167  define('PHPWG_DOMAIN', 'piwigo.org');
168}
169define('PHPWG_URL', 'http://'.PHPWG_DOMAIN);
170
[2836]171load_language( 'common.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
172load_language( 'admin.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
[3203]173load_language( 'install.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
[2836]174load_language( 'upgrade.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
[2819]175
[3203]176// check php version
177if (version_compare(PHP_VERSION, REQUIRED_PHP_VERSION, '<'))
178{
179  include(PHPWG_ROOT_PATH.'install/php5_apache_configuration.php');
180}
181
[2819]182// +-----------------------------------------------------------------------+
[1927]183// |                        template initialization                        |
184// +-----------------------------------------------------------------------+
185
[3203]186include( PHPWG_ROOT_PATH .'include/template.class.php');
[2819]187$template = new Template(PHPWG_ROOT_PATH.'admin/template/goto', 'roma');
[1927]188$template->set_filenames(array('upgrade'=>'upgrade.tpl'));
[3203]189$template->assign(array(
190  'RELEASE' => PHPWG_VERSION,
191  'L_UPGRADE_HELP' => sprintf(l10n('install_help'), PHPWG_URL.'/forum'),
192  )
193);
[1927]194
195// +-----------------------------------------------------------------------+
196// |                            upgrade choice                             |
197// +-----------------------------------------------------------------------+
198
[1999]199$tables = get_tables();
200$columns_of = get_columns_of($tables);
201
[2836]202// find the current release
203if (!in_array('param', $columns_of[PREFIX_TABLE.'config']))
[1927]204{
[2836]205  // we're in branch 1.3, important upgrade, isn't it?
206  if (in_array(PREFIX_TABLE.'user_category', $tables))
[1927]207  {
[2836]208    $current_release = '1.3.1';
[1927]209  }
[2836]210  else
[1927]211  {
[2836]212    $current_release = '1.3.0';
[1927]213  }
[2836]214}
215else if (!in_array(PREFIX_TABLE.'user_cache', $tables))
216{
217  $current_release = '1.4.0';
218}
219else if (!in_array(PREFIX_TABLE.'tags', $tables))
220{
221  $current_release = '1.5.0';
222}
[2862]223else if ( !in_array(PREFIX_TABLE.'plugins', $tables) )
[2836]224{
225  if (!in_array('auto_login_key', $columns_of[PREFIX_TABLE.'user_infos']))
[1927]226  {
[2836]227    $current_release = '1.6.0';
[1927]228  }
229  else
230  {
[2836]231    $current_release = '1.6.2';
[1927]232  }
233}
[2836]234else if (!in_array('md5sum', $columns_of[PREFIX_TABLE.'images']))
235{
236  $current_release = '1.7.0';
237}
238else
239{
240  die('No upgrade required, the database structure is up to date');
241}
[1927]242
243// +-----------------------------------------------------------------------+
244// |                            upgrade launch                             |
245// +-----------------------------------------------------------------------+
[2836]246$page['infos'] = array();
247$page['errors'] = array();
[2863]248$mysql_changes = array();
[1927]249
[2836]250if (isset($_POST['username']) and isset($_POST['password']))
[1927]251{
[2836]252  check_upgrade_access_rights($current_release, $_POST['username'], $_POST['password']);
253}
[2254]254
[2836]255if (isset($_POST['submit']) and check_upgrade())
256{
257  $upgrade_file = PHPWG_ROOT_PATH.'install/upgrade_'.$current_release.'.php';
[1927]258  if (is_file($upgrade_file))
259  {
260    $page['upgrade_start'] = get_moment();
261    $conf['die_on_sql_error'] = false;
262    include($upgrade_file);
263
[4280]264    // Something to add in config_database.inc.php?
[2863]265    if (!empty($mysql_changes))
266    {
267      $config_file_contents = 
268        substr($config_file_contents, 0, $php_end_tag) . "\r\n"
[2865]269        . implode("\r\n" , $mysql_changes) . "\r\n"
[2863]270        . substr($config_file_contents, $php_end_tag);
271
272      if (!@file_put_contents($config_file, $config_file_contents))
273      {
274        array_push($page['infos'],
[4280]275          l10n('in include/config_database.inc.php, before ?>, insert:') . '
[2865]276<p><textarea rows="4" cols="40">'.implode("\r\n" , $mysql_changes).'</textarea></p>'
[2863]277          );
278      }
279    }
280
[2787]281    // Plugins deactivation
282    if (in_array(PREFIX_TABLE.'plugins', $tables))
283    {
[2815]284      deactivate_non_standard_plugins();
[2787]285    }
286
[2102]287    // Create empty local files to avoid log errors
288    create_empty_local_files();
[2254]289
[1927]290    $page['upgrade_end'] = get_moment();
291
[2254]292    $template->assign(
[1927]293      'upgrade',
294      array(
[2836]295        'VERSION' => $current_release,
[1927]296        'TOTAL_TIME' => get_elapsed_time(
297          $page['upgrade_start'],
298          $page['upgrade_end']
299          ),
300        'SQL_TIME' => number_format(
301          $page['queries_time'],
302          3,
303          '.',
304          ' '
305          ).' s',
306        'NB_QUERIES' => $page['count_queries']
307        )
308      );
309
[2819]310    array_push($page['infos'],
311      l10n('perform a maintenance check')
[1927]312      );
313
[3072]314    // Save $page['infos'] in order to restore after maintenance actions
315    $page['infos_sav'] = $page['infos'];
316    $page['infos'] = array();
317
[2812]318    // c13y_upgrade plugin means "check integrity after upgrade", so it
319    // becomes useful just after an upgrade
[2808]320    $query = '
321REPLACE INTO '.PLUGINS_TABLE.'
322  (id, state)
323  VALUES (\'c13y_upgrade\', \'active\')
324;';
325    pwg_query($query);
[2890]326
327    // Delete cache data
328    invalidate_user_cache(true);
329    $template->delete_compiled_templates();
330
331    // Tables Maintenance
332    do_maintenance_all_tables();
333
[3072]334    // Restore $page['infos'] in order to hide informations messages from functions calles
335    // errors messages are not hide
336    $page['infos'] = $page['infos_sav'];
337
[1927]338  }
[2836]339}
340
341// +-----------------------------------------------------------------------+
342// |                          start template output                        |
343// +-----------------------------------------------------------------------+
344else
345{
346  foreach (get_languages('utf-8') as $language_code => $language_name)
[1927]347  {
[2836]348    if ($language == $language_code)
349    {
350      $template->assign('language_selection', $language_code);
351    }
352    $languages_options[$language_code] = $language_name;
[1927]353  }
[2836]354  $template->assign('language_options', $languages_options);
355
356  $template->assign('introduction', array(
357    'CURRENT_RELEASE' => $current_release,
358    'F_ACTION' => 'upgrade.php?language=' . $language));
359
360  if (!check_upgrade())
361  {
362    $template->assign('login', true);
363  }
[1927]364}
365
[2836]366if (count($page['errors']) != 0)
367{
368  $template->assign('errors', $page['errors']);
369}
370
371if (count($page['infos']) != 0)
372{
373  $template->assign('infos', $page['infos']);
374}
375
[1927]376// +-----------------------------------------------------------------------+
377// |                          sending html code                            |
378// +-----------------------------------------------------------------------+
379
380$template->pparse('upgrade');
381?>
Note: See TracBrowser for help on using the repository browser.