source: trunk/upgrade.php @ 4280

Last change on this file since 4280 was 4280, checked in by nikrou, 14 years ago

Feature 928 resolved.
Replace include/mysql.inc.php by include/config_database.inc.php to be independant of database engine
Rename database configurations variables by ones in $conf array.

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