source: trunk/upgrade.php @ 26771

Last change on this file since 26771 was 26461, checked in by mistic100, 11 years ago

Update headers to 2014. Happy new year!!

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