source: trunk/upgrade.php @ 5459

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

Fix php5 apache configuration with upgrade.

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