source: branches/2.3/upgrade.php @ 20041

Last change on this file since 20041 was 13955, checked in by plg, 13 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
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 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
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';
38$config_file_contents = @file_get_contents($config_file);
39if ($config_file_contents === false)
40{
41  die('Cannot load '.$config_file);
42}
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}
48
49include($config_file);
50include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
51@include(PHPWG_ROOT_PATH. 'local/config/config.inc.php');
52if (isset($conf['local_dir_site']))
53{
54  @include(PHPWG_ROOT_PATH.PWG_LOCAL_DIR. 'config/config.inc.php');
55}
56
57// $conf is not used for users tables - define cannot be re-defined
58define('USERS_TABLE', $prefixeTable.'users');
59include_once(PHPWG_ROOT_PATH.'include/constants.php');
60define('PREFIX_TABLE', $prefixeTable);
61define('UPGRADES_PATH', PHPWG_ROOT_PATH.'install/db');
62
63include_once(PHPWG_ROOT_PATH.'include/functions.inc.php');
64include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
65
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();
78
79  $query = '
80SHOW TABLES
81;';
82  $result = pwg_query($query);
83
84  while ($row = pwg_db_fetch_row($result))
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;';
109    $result = pwg_query($query);
110
111    $columns_of[$table] = array();
112
113    while ($row = pwg_db_fetch_row($result))
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;
127
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// +-----------------------------------------------------------------------+
149// |                             language                                  |
150// +-----------------------------------------------------------------------+
151include(PHPWG_ROOT_PATH . 'admin/include/languages.class.php');
152$languages = new languages('utf-8');
153
154if (isset($_GET['language']))
155{
156  $language = strip_tags($_GET['language']);
157
158  if (!in_array($language, array_keys($languages->fs_languages)))
159  {
160    $language = PHPWG_DEFAULT_LANGUAGE;
161  }
162}
163else
164{
165  $language = 'en_UK';
166  // Try to get browser language
167  foreach ($languages->fs_languages as $language_code => $fs_language)
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
177if ('fr_FR' == $language) {
178  define('PHPWG_DOMAIN', 'fr.piwigo.org');
179}
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}
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}
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}
201else if ('nl_NL' == $language) {
202  define('PHPWG_DOMAIN', 'nl.piwigo.org');
203}
204else {
205  define('PHPWG_DOMAIN', 'piwigo.org');
206}
207define('PHPWG_URL', 'http://'.PHPWG_DOMAIN);
208
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) );
211load_language( 'install.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
212load_language( 'upgrade.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
213
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
220// +-----------------------------------------------------------------------+
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
229list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
230define('CURRENT_DATE', $dbnow);
231
232// +-----------------------------------------------------------------------+
233// |                        template initialization                        |
234// +-----------------------------------------------------------------------+
235
236include( PHPWG_ROOT_PATH .'include/template.class.php');
237$template = new Template(PHPWG_ROOT_PATH.'admin/themes', 'clear');
238$template->set_filenames(array('upgrade'=>'upgrade.tpl'));
239$template->assign(array(
240  'RELEASE' => PHPWG_VERSION,
241  'L_UPGRADE_HELP' => sprintf(l10n('Need help ? Ask your question on <a href="%s">Piwigo message board</a>.'), PHPWG_URL.'/forum'),
242  )
243);
244
245// +-----------------------------------------------------------------------+
246// |                            upgrade choice                             |
247// +-----------------------------------------------------------------------+
248
249$tables = get_tables();
250$columns_of = get_columns_of($tables);
251
252// find the current release
253if (!in_array('param', $columns_of[PREFIX_TABLE.'config']))
254{
255  // we're in branch 1.3, important upgrade, isn't it?
256  if (in_array(PREFIX_TABLE.'user_category', $tables))
257  {
258    $current_release = '1.3.1';
259  }
260  else
261  {
262    $current_release = '1.3.0';
263  }
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}
273else if ( !in_array(PREFIX_TABLE.'plugins', $tables) )
274{
275  if (!in_array('auto_login_key', $columns_of[PREFIX_TABLE.'user_infos']))
276  {
277    $current_release = '1.6.0';
278  }
279  else
280  {
281    $current_release = '1.6.2';
282  }
283}
284else if (!in_array('md5sum', $columns_of[PREFIX_TABLE.'images']))
285{
286  $current_release = '1.7.0';
287}
288else if (!in_array(PREFIX_TABLE.'themes', $tables))
289{
290  $current_release = '2.0.0';
291}
292else if (!in_array('added_by', $columns_of[PREFIX_TABLE.'images']))
293{
294  $current_release = '2.1.0';
295}
296else if (!in_array('rating_score', $columns_of[PREFIX_TABLE.'images']))
297{
298  $current_release = '2.2.0';
299}
300else
301{
302  die('No upgrade required, the database structure is up to date');
303}
304
305// +-----------------------------------------------------------------------+
306// |                            upgrade launch                             |
307// +-----------------------------------------------------------------------+
308$page['infos'] = array();
309$page['errors'] = array();
310$mysql_changes = array();
311
312check_upgrade_access_rights();
313
314if ((isset($_POST['submit']) or isset($_GET['now']))
315  and check_upgrade())
316{
317  $upgrade_file = PHPWG_ROOT_PATH.'install/upgrade_'.$current_release.'.php';
318  if (is_file($upgrade_file))
319  {
320    $page['upgrade_start'] = get_moment();
321    $conf['die_on_sql_error'] = false;
322    include($upgrade_file);
323    conf_update_param('piwigo_db_version', get_branch_from_version(PHPWG_VERSION));
324
325    // Something to add in database.inc.php?
326    if (!empty($mysql_changes))
327    {
328      $config_file_contents = 
329        substr($config_file_contents, 0, $php_end_tag) . "\r\n"
330        . implode("\r\n" , $mysql_changes) . "\r\n"
331        . substr($config_file_contents, $php_end_tag);
332
333      if (!@file_put_contents($config_file, $config_file_contents))
334      {
335        array_push(
336          $page['infos'],
337          sprintf(
338            l10n('In <i>%s</i>, before <b>?></b>, insert:'),
339            PWG_LOCAL_DIR.'config/database.inc.php'
340            )
341          .'<p><textarea rows="4" cols="40">'
342          .implode("\r\n" , $mysql_changes).'</textarea></p>'
343          );
344      }
345    }
346
347    // Plugins deactivation
348    if (in_array(PREFIX_TABLE.'plugins', $tables))
349    {
350      deactivate_non_standard_plugins();
351    }
352
353    deactivate_non_standard_themes();
354
355    $page['upgrade_end'] = get_moment();
356
357    $template->assign(
358      'upgrade',
359      array(
360        'VERSION' => $current_release,
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
375    array_push($page['infos'],
376      l10n('Perform a maintenance check in [Administration>Tools>Maintenance] if you encounter any problem.')
377      );
378
379    // Save $page['infos'] in order to restore after maintenance actions
380    $page['infos_sav'] = $page['infos'];
381    $page['infos'] = array();
382
383    // c13y_upgrade plugin means "check integrity after upgrade", so it
384    // becomes useful just after an upgrade
385    $query = '
386REPLACE INTO '.PLUGINS_TABLE.'
387  (id, state)
388  VALUES (\'c13y_upgrade\', \'active\')
389;';
390    pwg_query($query);
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
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
403  }
404}
405
406// +-----------------------------------------------------------------------+
407// |                          start template output                        |
408// +-----------------------------------------------------------------------+
409else
410{
411  if (!defined('PWG_CHARSET'))
412  {
413    define('PWG_CHARSET', 'utf-8');
414  }
415
416  include_once(PHPWG_ROOT_PATH.'admin/include/languages.class.php');
417  $languages = new languages();
418 
419  foreach ($languages->fs_languages as $language_code => $fs_language)
420  {
421    if ($language == $language_code)
422    {
423      $template->assign('language_selection', $language_code);
424    }
425    $languages_options[$language_code] = $fs_language['name'];
426  }
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  }
437}
438
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
449// +-----------------------------------------------------------------------+
450// |                          sending html code                            |
451// +-----------------------------------------------------------------------+
452
453$template->pparse('upgrade');
454?>
Note: See TracBrowser for help on using the repository browser.