source: trunk/upgrade.php @ 6110

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

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

File size: 12.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
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// +-----------------------------------------------------------------------+
23
24define('PHPWG_ROOT_PATH', './');
25
26// load config file
27$config_file = PHPWG_ROOT_PATH.'local/config/database.inc.php';
28$config_file_contents = @file_get_contents($config_file);
29if ($config_file_contents === false)
30{
31  die('Cannot load '.$config_file);
32}
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}
38
39include(PHPWG_ROOT_PATH.'local/config/database.inc.php');
40include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
41@include(PHPWG_ROOT_PATH. 'local/config/config.inc.php');
42
43// $conf is not used for users tables - define cannot be re-defined
44define('USERS_TABLE', $prefixeTable.'users');
45include_once(PHPWG_ROOT_PATH.'include/constants.php');
46define('PREFIX_TABLE', $prefixeTable);
47define('UPGRADES_PATH', PHPWG_ROOT_PATH.'install/db');
48
49include_once(PHPWG_ROOT_PATH.'include/functions.inc.php');
50include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
51
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();
64
65  $query = '
66SHOW TABLES
67;';
68  $result = pwg_query($query);
69
70  while ($row = pwg_db_fetch_row($result))
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;';
95    $result = pwg_query($query);
96
97    $columns_of[$table] = array();
98
99    while ($row = pwg_db_fetch_row($result))
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;
113
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// +-----------------------------------------------------------------------+
135// |                             language                                  |
136// +-----------------------------------------------------------------------+
137include(PHPWG_ROOT_PATH . 'admin/include/languages.class.php');
138$languages = new languages('utf-8');
139
140if (isset($_GET['language']))
141{
142  $language = strip_tags($_GET['language']);
143}
144else
145{
146  $language = 'en_UK';
147  // Try to get browser language
148  foreach ($languages->fs_languages as $language_code => $language_name)
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
158if ('fr_FR' == $language) {
159  define('PHPWG_DOMAIN', 'fr.piwigo.org');
160}
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}
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}
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}
182else {
183  define('PHPWG_DOMAIN', 'piwigo.org');
184}
185define('PHPWG_URL', 'http://'.PHPWG_DOMAIN);
186
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) );
189load_language( 'install.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
190load_language( 'upgrade.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
191
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
198// +-----------------------------------------------------------------------+
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
207list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
208define('CURRENT_DATE', $dbnow);
209
210// +-----------------------------------------------------------------------+
211// |                        template initialization                        |
212// +-----------------------------------------------------------------------+
213
214include( PHPWG_ROOT_PATH .'include/template.class.php');
215$template = new Template(PHPWG_ROOT_PATH.'admin/themes', 'roma');
216$template->set_filenames(array('upgrade'=>'upgrade.tpl'));
217$template->assign(array(
218  'RELEASE' => PHPWG_VERSION,
219  'L_UPGRADE_HELP' => sprintf(l10n('Need help ? Ask your question on <a href="%s">Piwigo message board</a>.'), PHPWG_URL.'/forum'),
220  )
221);
222
223// +-----------------------------------------------------------------------+
224// |                            upgrade choice                             |
225// +-----------------------------------------------------------------------+
226
227$tables = get_tables();
228$columns_of = get_columns_of($tables);
229
230// find the current release
231if (!in_array('param', $columns_of[PREFIX_TABLE.'config']))
232{
233  // we're in branch 1.3, important upgrade, isn't it?
234  if (in_array(PREFIX_TABLE.'user_category', $tables))
235  {
236    $current_release = '1.3.1';
237  }
238  else
239  {
240    $current_release = '1.3.0';
241  }
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}
251else if ( !in_array(PREFIX_TABLE.'plugins', $tables) )
252{
253  if (!in_array('auto_login_key', $columns_of[PREFIX_TABLE.'user_infos']))
254  {
255    $current_release = '1.6.0';
256  }
257  else
258  {
259    $current_release = '1.6.2';
260  }
261}
262else if (!in_array('md5sum', $columns_of[PREFIX_TABLE.'images']))
263{
264  $current_release = '1.7.0';
265}
266else if (!in_array(PREFIX_TABLE.'themes', $tables))
267{
268  $current_release = '2.0.0';
269}
270else
271{
272  die('No upgrade required, the database structure is up to date');
273}
274
275// +-----------------------------------------------------------------------+
276// |                            upgrade launch                             |
277// +-----------------------------------------------------------------------+
278$page['infos'] = array();
279$page['errors'] = array();
280$mysql_changes = array();
281
282check_upgrade_access_rights();
283
284if ((isset($_POST['submit']) or isset($_GET['now']))
285  and check_upgrade())
286{
287  $upgrade_file = PHPWG_ROOT_PATH.'install/upgrade_'.$current_release.'.php';
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
294    // Something to add in database.inc.php?
295    if (!empty($mysql_changes))
296    {
297      $config_file_contents = 
298        substr($config_file_contents, 0, $php_end_tag) . "\r\n"
299        . implode("\r\n" , $mysql_changes) . "\r\n"
300        . substr($config_file_contents, $php_end_tag);
301
302      if (!@file_put_contents($config_file, $config_file_contents))
303      {
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          );
313      }
314    }
315
316    // Plugins deactivation
317    if (in_array(PREFIX_TABLE.'plugins', $tables))
318    {
319      deactivate_non_standard_plugins();
320    }
321
322    $page['upgrade_end'] = get_moment();
323
324    $template->assign(
325      'upgrade',
326      array(
327        'VERSION' => $current_release,
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
342    array_push($page['infos'],
343      l10n('Perform a maintenance check in [Administration>Specials>Maintenance] if you encounter any problem.')
344      );
345
346    // Save $page['infos'] in order to restore after maintenance actions
347    $page['infos_sav'] = $page['infos'];
348    $page['infos'] = array();
349
350    // c13y_upgrade plugin means "check integrity after upgrade", so it
351    // becomes useful just after an upgrade
352    $query = '
353REPLACE INTO '.PLUGINS_TABLE.'
354  (id, state)
355  VALUES (\'c13y_upgrade\', \'active\')
356;';
357    pwg_query($query);
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
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
370  }
371}
372
373// +-----------------------------------------------------------------------+
374// |                          start template output                        |
375// +-----------------------------------------------------------------------+
376else
377{
378  if (!defined('PWG_CHARSET'))
379  {
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  {
388    if ($language == $language_code)
389    {
390      $template->assign('language_selection', $language_code);
391    }
392    $languages_options[$language_code] = $language_name;
393  }
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  }
404}
405
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
416// +-----------------------------------------------------------------------+
417// |                          sending html code                            |
418// +-----------------------------------------------------------------------+
419
420$template->pparse('upgrade');
421?>
Note: See TracBrowser for help on using the repository browser.