source: trunk/upgrade.php @ 6110

Last change on this file since 6110 was 6110, checked in by patdenice, 14 years ago

Upgrade can be launched automaticaly if user is already connected as webmaster.

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