source: trunk/upgrade.php @ 5387

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

Fix php5 apache configuration with upgrade.

File size: 12.2 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_once(PHPWG_ROOT_PATH.'include/functions.inc.php');
40include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
41
42include(PHPWG_ROOT_PATH.'local/config/database.inc.php');
43include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
44@include(PHPWG_ROOT_PATH. 'local/config/config.inc.php');
45
46// $conf is not used for users tables - define cannot be re-defined
47define('USERS_TABLE', $prefixeTable.'users');
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();
63
64  $query = '
65SHOW TABLES
66;';
67  $result = pwg_query($query);
68
69  while ($row = pwg_db_fetch_row($result))
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;';
94    $result = pwg_query($query);
95
96    $columns_of[$table] = array();
97
98    while ($row = pwg_db_fetch_row($result))
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;
112
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// +-----------------------------------------------------------------------+
134// |                             language                                  |
135// +-----------------------------------------------------------------------+
136include(PHPWG_ROOT_PATH . 'admin/include/languages.class.php');
137$languages = new languages('utf-8');
138
139if (isset($_GET['language']))
140{
141  $language = strip_tags($_GET['language']);
142}
143else
144{
145  $language = 'en_UK';
146  // Try to get browser language
147  foreach ($languages->fs_languages as $language_code => $language_name)
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
157if ('fr_FR' == $language) {
158  define('PHPWG_DOMAIN', 'fr.piwigo.org');
159}
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}
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}
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}
181else {
182  define('PHPWG_DOMAIN', 'piwigo.org');
183}
184define('PHPWG_URL', 'http://'.PHPWG_DOMAIN);
185
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) );
188load_language( 'install.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
189load_language( 'upgrade.lang', '', array('language'=>$language, 'target_charset'=>'utf-8', 'no_fallback' => true) );
190
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
197// +-----------------------------------------------------------------------+
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// +-----------------------------------------------------------------------+
208// |                        template initialization                        |
209// +-----------------------------------------------------------------------+
210
211include( PHPWG_ROOT_PATH .'include/template.class.php');
212$template = new Template(PHPWG_ROOT_PATH.'admin/themes', 'roma');
213$template->set_filenames(array('upgrade'=>'upgrade.tpl'));
214$template->assign(array(
215  'RELEASE' => PHPWG_VERSION,
216  'L_UPGRADE_HELP' => sprintf(l10n('Need help ? Ask your question on <a href="%s">Piwigo message board</a>.'), PHPWG_URL.'/forum'),
217  )
218);
219
220// +-----------------------------------------------------------------------+
221// |                            upgrade choice                             |
222// +-----------------------------------------------------------------------+
223
224$tables = get_tables();
225$columns_of = get_columns_of($tables);
226
227// find the current release
228if (!in_array('param', $columns_of[PREFIX_TABLE.'config']))
229{
230  // we're in branch 1.3, important upgrade, isn't it?
231  if (in_array(PREFIX_TABLE.'user_category', $tables))
232  {
233    $current_release = '1.3.1';
234  }
235  else
236  {
237    $current_release = '1.3.0';
238  }
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}
248else if ( !in_array(PREFIX_TABLE.'plugins', $tables) )
249{
250  if (!in_array('auto_login_key', $columns_of[PREFIX_TABLE.'user_infos']))
251  {
252    $current_release = '1.6.0';
253  }
254  else
255  {
256    $current_release = '1.6.2';
257  }
258}
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}
267
268// +-----------------------------------------------------------------------+
269// |                            upgrade launch                             |
270// +-----------------------------------------------------------------------+
271$page['infos'] = array();
272$page['errors'] = array();
273$mysql_changes = array();
274
275if (isset($_POST['username']) and isset($_POST['password']))
276{
277  check_upgrade_access_rights($current_release, $_POST['username'], $_POST['password']);
278}
279
280if (isset($_POST['submit']) and check_upgrade())
281{
282  $upgrade_file = PHPWG_ROOT_PATH.'install/upgrade_'.$current_release.'.php';
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
289    // Something to add in database.inc.php?
290    if (!empty($mysql_changes))
291    {
292      $config_file_contents = 
293        substr($config_file_contents, 0, $php_end_tag) . "\r\n"
294        . implode("\r\n" , $mysql_changes) . "\r\n"
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'],
300                   l10n_args('in <i>%s</i>, before <b>?></b>, insert:', 
301                             'local/config/database.inc.php') . 
302                   '<p><textarea rows="4" cols="40">' .
303                   implode("\r\n" , $mysql_changes).'</textarea></p>'
304                   );
305      }
306    }
307
308    // Plugins deactivation
309    if (in_array(PREFIX_TABLE.'plugins', $tables))
310    {
311      deactivate_non_standard_plugins();
312    }
313
314    $page['upgrade_end'] = get_moment();
315
316    $template->assign(
317      'upgrade',
318      array(
319        'VERSION' => $current_release,
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
334    array_push($page['infos'],
335      l10n('Perform a maintenance check in [Administration>Specials>Maintenance] if you encounter any problem.')
336      );
337
338    // Save $page['infos'] in order to restore after maintenance actions
339    $page['infos_sav'] = $page['infos'];
340    $page['infos'] = array();
341
342    // c13y_upgrade plugin means "check integrity after upgrade", so it
343    // becomes useful just after an upgrade
344    $query = '
345REPLACE INTO '.PLUGINS_TABLE.'
346  (id, state)
347  VALUES (\'c13y_upgrade\', \'active\')
348;';
349    pwg_query($query);
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
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
362  }
363}
364
365// +-----------------------------------------------------------------------+
366// |                          start template output                        |
367// +-----------------------------------------------------------------------+
368else
369{
370  foreach (get_languages('utf-8') as $language_code => $language_name)
371  {
372    if ($language == $language_code)
373    {
374      $template->assign('language_selection', $language_code);
375    }
376    $languages_options[$language_code] = $language_name;
377  }
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  }
388}
389
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
400// +-----------------------------------------------------------------------+
401// |                          sending html code                            |
402// +-----------------------------------------------------------------------+
403
404$template->pparse('upgrade');
405?>
Note: See TracBrowser for help on using the repository browser.