source: branches/2.3/upgrade.php @ 19600

Last change on this file since 19600 was 13955, checked in by plg, 12 years ago

bug 2610 fixed: make sure the $_GETlang or $_GETlanguage is in the
list of available languages.

  • Property svn:eol-style set to LF
File size: 13.3 KB
RevLine 
[1927]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[8728]5// | Copyright(C) 2008-2011 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
[8722]27if (is_file(PHPWG_ROOT_PATH .'local/config/multisite.inc.php'))
28{
29  include(PHPWG_ROOT_PATH .'local/config/multisite.inc.php');
30  define('PWG_LOCAL_DIR', $conf['local_dir_site']);
31}
32else
33{
34  define('PWG_LOCAL_DIR', 'local/');
35}
36
37$config_file = PHPWG_ROOT_PATH.PWG_LOCAL_DIR.'config/database.inc.php';
[2863]38$config_file_contents = @file_get_contents($config_file);
39if ($config_file_contents === false)
[2836]40{
[2863]41  die('Cannot load '.$config_file);
[2836]42}
[2863]43$php_end_tag = strrpos($config_file_contents, '?'.'>');
44if ($php_end_tag === false)
45{
46  die('Cannot find php end tag in '.$config_file);
47}
[2836]48
[8722]49include($config_file);
[1927]50include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
[5215]51@include(PHPWG_ROOT_PATH. 'local/config/config.inc.php');
[8722]52if (isset($conf['local_dir_site']))
53{
54  @include(PHPWG_ROOT_PATH.PWG_LOCAL_DIR. 'config/config.inc.php');
55}
[1927]56
[5387]57// $conf is not used for users tables - define cannot be re-defined
58define('USERS_TABLE', $prefixeTable.'users');
[1927]59include_once(PHPWG_ROOT_PATH.'include/constants.php');
60define('PREFIX_TABLE', $prefixeTable);
[5982]61define('UPGRADES_PATH', PHPWG_ROOT_PATH.'install/db');
[1927]62
[6110]63include_once(PHPWG_ROOT_PATH.'include/functions.inc.php');
64include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
65
[1927]66// +-----------------------------------------------------------------------+
67// |                              functions                                |
68// +-----------------------------------------------------------------------+
69
70/**
71 * list all tables in an array
72 *
73 * @return array
74 */
75function get_tables()
76{
77  $tables = array();
[2290]78
[1927]79  $query = '
80SHOW TABLES
81;';
[2884]82  $result = pwg_query($query);
[1927]83
[4325]84  while ($row = pwg_db_fetch_row($result))
[1927]85  {
86    if (preg_match('/^'.PREFIX_TABLE.'/', $row[0]))
87    {
88      array_push($tables, $row[0]);
89    }
90  }
91
92  return $tables;
93}
94
95/**
96 * list all columns of each given table
97 *
98 * @return array of array
99 */
100function get_columns_of($tables)
101{
102  $columns_of = array();
103
104  foreach ($tables as $table)
105  {
106    $query = '
107DESC '.$table.'
108;';
[2884]109    $result = pwg_query($query);
[1927]110
111    $columns_of[$table] = array();
112
[4325]113    while ($row = pwg_db_fetch_row($result))
[1927]114    {
115      array_push($columns_of[$table], $row[0]);
116    }
117  }
118
119  return $columns_of;
120}
121
122/**
123 */
124function print_time($message)
125{
126  global $last_time;
[2290]127
[1927]128  $new_time = get_moment();
129  echo '<pre>['.get_elapsed_time($last_time, $new_time).']';
130  echo ' '.$message;
131  echo '</pre>';
132  flush();
133  $last_time = $new_time;
134}
135
136// +-----------------------------------------------------------------------+
137// |                             playing zone                              |
138// +-----------------------------------------------------------------------+
139
140// echo implode('<br>', get_tables());
141// echo '<pre>'; print_r(get_columns_of(get_tables())); echo '</pre>';
142
143// foreach (get_available_upgrade_ids() as $upgrade_id)
144// {
145//   echo $upgrade_id, '<br>';
146// }
147
148// +-----------------------------------------------------------------------+
[2819]149// |                             language                                  |
150// +-----------------------------------------------------------------------+
[5387]151include(PHPWG_ROOT_PATH . 'admin/include/languages.class.php');
152$languages = new languages('utf-8');
153
[2819]154if (isset($_GET['language']))
155{
156  $language = strip_tags($_GET['language']);
[13955]157
158  if (!in_array($language, array_keys($languages->fs_languages)))
159  {
160    $language = PHPWG_DEFAULT_LANGUAGE;
161  }
[2819]162}
163else
164{
165  $language = 'en_UK';
166  // Try to get browser language
[9518]167  foreach ($languages->fs_languages as $language_code => $fs_language)
[2819]168  {
169    if (substr($language_code,0,2) == @substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2))
170    {
171      $language = $language_code;
172      break;
173    }
174  }
175}
176
[3203]177if ('fr_FR' == $language) {
178  define('PHPWG_DOMAIN', 'fr.piwigo.org');
179}
[5234]180else if ('it_IT' == $language) {
181  define('PHPWG_DOMAIN', 'it.piwigo.org');
182}
183else if ('de_DE' == $language) {
184  define('PHPWG_DOMAIN', 'de.piwigo.org');
185}
186else if ('es_ES' == $language) {
187  define('PHPWG_DOMAIN', 'es.piwigo.org');
188}
[4816]189else if ('pl_PL' == $language) {
190  define('PHPWG_DOMAIN', 'pl.piwigo.org');
191}
192else if ('zh_CN' == $language) {
193  define('PHPWG_DOMAIN', 'cn.piwigo.org');
194}
[5234]195else if ('hu_HU' == $language) {
196  define('PHPWG_DOMAIN', 'hu.piwigo.org');
197}
198else if ('ru_RU' == $language) {
199  define('PHPWG_DOMAIN', 'ru.piwigo.org');
200}
[6152]201else if ('nl_NL' == $language) {
202  define('PHPWG_DOMAIN', 'nl.piwigo.org');
203}
[3203]204else {
205  define('PHPWG_DOMAIN', 'piwigo.org');
206}
207define('PHPWG_URL', 'http://'.PHPWG_DOMAIN);
208
[2836]209load_language( 'common.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
210load_language( 'admin.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
[3203]211load_language( 'install.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
[2836]212load_language( 'upgrade.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
[2819]213
[3203]214// check php version
215if (version_compare(PHP_VERSION, REQUIRED_PHP_VERSION, '<'))
216{
217  include(PHPWG_ROOT_PATH.'install/php5_apache_configuration.php');
218}
219
[2819]220// +-----------------------------------------------------------------------+
[5387]221// |                          database connection                          |
222// +-----------------------------------------------------------------------+
223include_once(PHPWG_ROOT_PATH.'admin/include/functions_upgrade.php');
224include(PHPWG_ROOT_PATH .'include/dblayer/functions_'.$conf['dblayer'].'.inc.php');
225
226upgrade_db_connect();
227pwg_db_check_charset();
228
[5982]229list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
230define('CURRENT_DATE', $dbnow);
231
[5387]232// +-----------------------------------------------------------------------+
[1927]233// |                        template initialization                        |
234// +-----------------------------------------------------------------------+
235
[3203]236include( PHPWG_ROOT_PATH .'include/template.class.php');
[9596]237$template = new Template(PHPWG_ROOT_PATH.'admin/themes', 'clear');
[1927]238$template->set_filenames(array('upgrade'=>'upgrade.tpl'));
[3203]239$template->assign(array(
240  'RELEASE' => PHPWG_VERSION,
[5207]241  'L_UPGRADE_HELP' => sprintf(l10n('Need help ? Ask your question on <a href="%s">Piwigo message board</a>.'), PHPWG_URL.'/forum'),
[3203]242  )
243);
[1927]244
245// +-----------------------------------------------------------------------+
246// |                            upgrade choice                             |
247// +-----------------------------------------------------------------------+
248
[1999]249$tables = get_tables();
250$columns_of = get_columns_of($tables);
251
[2836]252// find the current release
253if (!in_array('param', $columns_of[PREFIX_TABLE.'config']))
[1927]254{
[2836]255  // we're in branch 1.3, important upgrade, isn't it?
256  if (in_array(PREFIX_TABLE.'user_category', $tables))
[1927]257  {
[2836]258    $current_release = '1.3.1';
[1927]259  }
[2836]260  else
[1927]261  {
[2836]262    $current_release = '1.3.0';
[1927]263  }
[2836]264}
265else if (!in_array(PREFIX_TABLE.'user_cache', $tables))
266{
267  $current_release = '1.4.0';
268}
269else if (!in_array(PREFIX_TABLE.'tags', $tables))
270{
271  $current_release = '1.5.0';
272}
[2862]273else if ( !in_array(PREFIX_TABLE.'plugins', $tables) )
[2836]274{
275  if (!in_array('auto_login_key', $columns_of[PREFIX_TABLE.'user_infos']))
[1927]276  {
[2836]277    $current_release = '1.6.0';
[1927]278  }
279  else
280  {
[2836]281    $current_release = '1.6.2';
[1927]282  }
283}
[2836]284else if (!in_array('md5sum', $columns_of[PREFIX_TABLE.'images']))
285{
286  $current_release = '1.7.0';
287}
[5982]288else if (!in_array(PREFIX_TABLE.'themes', $tables))
289{
290  $current_release = '2.0.0';
291}
[9595]292else if (!in_array('added_by', $columns_of[PREFIX_TABLE.'images']))
293{
294  $current_release = '2.1.0';
295}
[12296]296else if (!in_array('rating_score', $columns_of[PREFIX_TABLE.'images']))
297{
298  $current_release = '2.2.0';
299}
[2836]300else
301{
302  die('No upgrade required, the database structure is up to date');
303}
[1927]304
305// +-----------------------------------------------------------------------+
306// |                            upgrade launch                             |
307// +-----------------------------------------------------------------------+
[2836]308$page['infos'] = array();
309$page['errors'] = array();
[2863]310$mysql_changes = array();
[1927]311
[6110]312check_upgrade_access_rights();
[2254]313
[6110]314if ((isset($_POST['submit']) or isset($_GET['now']))
315  and check_upgrade())
[2836]316{
317  $upgrade_file = PHPWG_ROOT_PATH.'install/upgrade_'.$current_release.'.php';
[1927]318  if (is_file($upgrade_file))
319  {
320    $page['upgrade_start'] = get_moment();
321    $conf['die_on_sql_error'] = false;
322    include($upgrade_file);
[11511]323    conf_update_param('piwigo_db_version', get_branch_from_version(PHPWG_VERSION));
[1927]324
[5213]325    // Something to add in database.inc.php?
[2863]326    if (!empty($mysql_changes))
327    {
328      $config_file_contents = 
329        substr($config_file_contents, 0, $php_end_tag) . "\r\n"
[2865]330        . implode("\r\n" , $mysql_changes) . "\r\n"
[2863]331        . substr($config_file_contents, $php_end_tag);
332
333      if (!@file_put_contents($config_file, $config_file_contents))
334      {
[5982]335        array_push(
336          $page['infos'],
337          sprintf(
338            l10n('In <i>%s</i>, before <b>?></b>, insert:'),
[8722]339            PWG_LOCAL_DIR.'config/database.inc.php'
[5982]340            )
341          .'<p><textarea rows="4" cols="40">'
342          .implode("\r\n" , $mysql_changes).'</textarea></p>'
343          );
[2863]344      }
345    }
346
[2787]347    // Plugins deactivation
348    if (in_array(PREFIX_TABLE.'plugins', $tables))
349    {
[2815]350      deactivate_non_standard_plugins();
[2787]351    }
352
[9597]353    deactivate_non_standard_themes();
354
[1927]355    $page['upgrade_end'] = get_moment();
356
[2254]357    $template->assign(
[1927]358      'upgrade',
359      array(
[2836]360        'VERSION' => $current_release,
[1927]361        'TOTAL_TIME' => get_elapsed_time(
362          $page['upgrade_start'],
363          $page['upgrade_end']
364          ),
365        'SQL_TIME' => number_format(
366          $page['queries_time'],
367          3,
368          '.',
369          ' '
370          ).' s',
371        'NB_QUERIES' => $page['count_queries']
372        )
373      );
374
[2819]375    array_push($page['infos'],
[6335]376      l10n('Perform a maintenance check in [Administration>Tools>Maintenance] if you encounter any problem.')
[1927]377      );
378
[3072]379    // Save $page['infos'] in order to restore after maintenance actions
380    $page['infos_sav'] = $page['infos'];
381    $page['infos'] = array();
382
[2812]383    // c13y_upgrade plugin means "check integrity after upgrade", so it
384    // becomes useful just after an upgrade
[2808]385    $query = '
386REPLACE INTO '.PLUGINS_TABLE.'
387  (id, state)
388  VALUES (\'c13y_upgrade\', \'active\')
389;';
390    pwg_query($query);
[2890]391
392    // Delete cache data
393    invalidate_user_cache(true);
394    $template->delete_compiled_templates();
395
396    // Tables Maintenance
397    do_maintenance_all_tables();
398
[3072]399    // Restore $page['infos'] in order to hide informations messages from functions calles
400    // errors messages are not hide
401    $page['infos'] = $page['infos_sav'];
402
[1927]403  }
[2836]404}
405
406// +-----------------------------------------------------------------------+
407// |                          start template output                        |
408// +-----------------------------------------------------------------------+
409else
410{
[5982]411  if (!defined('PWG_CHARSET'))
[1927]412  {
[5982]413    define('PWG_CHARSET', 'utf-8');
414  }
415
416  include_once(PHPWG_ROOT_PATH.'admin/include/languages.class.php');
417  $languages = new languages();
418 
[9518]419  foreach ($languages->fs_languages as $language_code => $fs_language)
[5982]420  {
[2836]421    if ($language == $language_code)
422    {
423      $template->assign('language_selection', $language_code);
424    }
[9518]425    $languages_options[$language_code] = $fs_language['name'];
[1927]426  }
[2836]427  $template->assign('language_options', $languages_options);
428
429  $template->assign('introduction', array(
430    'CURRENT_RELEASE' => $current_release,
431    'F_ACTION' => 'upgrade.php?language=' . $language));
432
433  if (!check_upgrade())
434  {
435    $template->assign('login', true);
436  }
[1927]437}
438
[2836]439if (count($page['errors']) != 0)
440{
441  $template->assign('errors', $page['errors']);
442}
443
444if (count($page['infos']) != 0)
445{
446  $template->assign('infos', $page['infos']);
447}
448
[1927]449// +-----------------------------------------------------------------------+
450// |                          sending html code                            |
451// +-----------------------------------------------------------------------+
452
453$template->pparse('upgrade');
454?>
Note: See TracBrowser for help on using the repository browser.