source: trunk/upgrade.php @ 2865

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