source: trunk/upgrade.php @ 2297

Last change on this file since 2297 was 2297, checked in by plg, 16 years ago

Modification: new header on PHP files, PhpWebGallery renamed Piwigo.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 10.7 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// | PhpWebGallery - a PHP based picture gallery                           |
25// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
26// | Copyright (C) 2003-2008 PhpWebGallery Team - http://phpwebgallery.net |
27// +-----------------------------------------------------------------------+
28// | file          : $Id: upgrade.php 2297 2008-04-04 22:57:23Z plg $
29// | last update   : $Date: 2008-04-04 22:57:23 +0000 (Fri, 04 Apr 2008) $
30// | last modifier : $Author: plg $
31// | revision      : $Revision: 2297 $
32// +-----------------------------------------------------------------------+
33// | This program is free software; you can redistribute it and/or modify  |
34// | it under the terms of the GNU General Public License as published by  |
35// | the Free Software Foundation                                          |
36// |                                                                       |
37// | This program is distributed in the hope that it will be useful, but   |
38// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
39// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
40// | General Public License for more details.                              |
41// |                                                                       |
42// | You should have received a copy of the GNU General Public License     |
43// | along with this program; if not, write to the Free Software           |
44// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
45// | USA.                                                                  |
46// +-----------------------------------------------------------------------+
47
48define('PHPWG_ROOT_PATH', './');
49
50include_once(PHPWG_ROOT_PATH.'include/functions.inc.php');
51include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
52include_once(PHPWG_ROOT_PATH.'admin/include/functions_upgrade.php');
53include(PHPWG_ROOT_PATH.'include/template.class.php');
54
55include(PHPWG_ROOT_PATH.'include/mysql.inc.php');
56include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
57@include(PHPWG_ROOT_PATH. 'include/config_local.inc.php');
58
59check_upgrade();
60
61prepare_conf_upgrade();
62
63include_once(PHPWG_ROOT_PATH.'include/constants.php');
64define('PREFIX_TABLE', $prefixeTable);
65
66// Database connection
67mysql_connect( $cfgHote, $cfgUser, $cfgPassword )
68or die ( "Could not connect to database server" );
69mysql_select_db( $cfgBase )
70or die ( "Could not connect to database" );
71// +-----------------------------------------------------------------------+
72// |                            tricky output                              |
73// +-----------------------------------------------------------------------+
74echo '<!-- This is an HTML comment given in order to make IE outputs';
75echo ' the code.'."\n";
76echo ' Indeed, IE doesn\'t start to send output until a limit';
77echo ' of XXX bytes '."\n";
78echo str_repeat( ' ', 80 )."\n";
79echo str_repeat( ' ', 80 )."\n";
80echo str_repeat( ' ', 80 )."\n";
81echo '-->'."\n";
82flush();
83// +-----------------------------------------------------------------------+
84// |                              functions                                |
85// +-----------------------------------------------------------------------+
86
87/**
88 * list all tables in an array
89 *
90 * @return array
91 */
92function get_tables()
93{
94  $tables = array();
95
96  $query = '
97SHOW TABLES
98;';
99  $result = mysql_query($query);
100
101  while ($row = mysql_fetch_row($result))
102  {
103    if (preg_match('/^'.PREFIX_TABLE.'/', $row[0]))
104    {
105      array_push($tables, $row[0]);
106    }
107  }
108
109  return $tables;
110}
111
112/**
113 * list all columns of each given table
114 *
115 * @return array of array
116 */
117function get_columns_of($tables)
118{
119  $columns_of = array();
120
121  foreach ($tables as $table)
122  {
123    $query = '
124DESC '.$table.'
125;';
126    $result = mysql_query($query);
127
128    $columns_of[$table] = array();
129
130    while ($row = mysql_fetch_row($result))
131    {
132      array_push($columns_of[$table], $row[0]);
133    }
134  }
135
136  return $columns_of;
137}
138
139/**
140 */
141function print_time($message)
142{
143  global $last_time;
144
145  $new_time = get_moment();
146  echo '<pre>['.get_elapsed_time($last_time, $new_time).']';
147  echo ' '.$message;
148  echo '</pre>';
149  flush();
150  $last_time = $new_time;
151}
152
153// +-----------------------------------------------------------------------+
154// |                             playing zone                              |
155// +-----------------------------------------------------------------------+
156
157// echo implode('<br>', get_tables());
158// echo '<pre>'; print_r(get_columns_of(get_tables())); echo '</pre>';
159
160// foreach (get_available_upgrade_ids() as $upgrade_id)
161// {
162//   echo $upgrade_id, '<br>';
163// }
164
165// +-----------------------------------------------------------------------+
166// |                        template initialization                        |
167// +-----------------------------------------------------------------------+
168
169$template = new Template(PHPWG_ROOT_PATH.'template/yoga');
170$template->set_filenames(array('upgrade'=>'upgrade.tpl'));
171$template->assign('RELEASE', PHPWG_VERSION);
172
173// +-----------------------------------------------------------------------+
174// |                            upgrade choice                             |
175// +-----------------------------------------------------------------------+
176
177$tables = get_tables();
178$columns_of = get_columns_of($tables);
179
180if (!isset($_GET['version']))
181{
182  // find the current release
183  if (!in_array('param', $columns_of[PREFIX_TABLE.'config']))
184  {
185    // we're in branch 1.3, important upgrade, isn't it?
186    if (in_array(PREFIX_TABLE.'user_category', $tables))
187    {
188      $current_release = '1.3.1';
189    }
190    else
191    {
192      $current_release = '1.3.0';
193    }
194  }
195  else if (!in_array(PREFIX_TABLE.'user_cache', $tables))
196  {
197    $current_release = '1.4.0';
198  }
199  else if (!in_array(PREFIX_TABLE.'tags', $tables))
200  {
201    $current_release = '1.5.0';
202  }
203  else if ( !in_array(PREFIX_TABLE.'history_summary', $tables) )
204  {
205    if (!in_array('auto_login_key', $columns_of[PREFIX_TABLE.'user_infos']))
206    {
207      $current_release = '1.6.0';
208    }
209    else
210    {
211      $current_release = '1.6.2';
212    }
213  }
214  else
215  {
216    die('No upgrade required, the database structure is up to date');
217  }
218
219  $template->assign(
220    'introduction',
221    array(
222      'CURRENT_RELEASE' => $current_release,
223      'RUN_UPGRADE_URL' =>
224        PHPWG_ROOT_PATH.'upgrade.php?version='.$current_release,
225      )
226    );
227}
228
229// +-----------------------------------------------------------------------+
230// |                            upgrade launch                             |
231// +-----------------------------------------------------------------------+
232
233else
234{
235  if (in_array(PREFIX_TABLE.'history_summary', $tables))
236  {
237    die('No database upgrade required, do not refresh the page');
238  }
239
240  $upgrade_file = PHPWG_ROOT_PATH.'install/upgrade_'.$_GET['version'].'.php';
241  if (is_file($upgrade_file))
242  {
243    $page['infos'] = array();
244    $page['upgrade_start'] = get_moment();
245    $conf['die_on_sql_error'] = false;
246    include($upgrade_file);
247
248    // Available upgrades must be ignored after a fresh installation. To
249    // make PWG avoid upgrading, we must tell it upgrades have already been
250    // made.
251    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
252    define('CURRENT_DATE', $dbnow);
253    $datas = array();
254    foreach (get_available_upgrade_ids() as $upgrade_id)
255    {
256      array_push(
257        $datas,
258        array(
259          'id'          => $upgrade_id,
260          'applied'     => CURRENT_DATE,
261          'description' => 'upgrade included in migration',
262          )
263        );
264    }
265    mass_inserts(
266      UPGRADE_TABLE,
267      array_keys($datas[0]),
268      $datas
269      );
270
271    // Create empty local files to avoid log errors
272    create_empty_local_files();
273
274    $page['upgrade_end'] = get_moment();
275
276    $template->assign(
277      'upgrade',
278      array(
279        'VERSION' => $_GET['version'],
280        'TOTAL_TIME' => get_elapsed_time(
281          $page['upgrade_start'],
282          $page['upgrade_end']
283          ),
284        'SQL_TIME' => number_format(
285          $page['queries_time'],
286          3,
287          '.',
288          ' '
289          ).' s',
290        'NB_QUERIES' => $page['count_queries']
291        )
292      );
293
294    array_push(
295      $page['infos'],
296      '[security] delete files "upgrade.php", "upgrade_feed.php", "install.php" and "install"
297directory'
298      );
299
300    array_push(
301      $page['infos'],
302      'in include/mysql.inc.php, remove
303<pre style="background-color:lightgray">
304define(\'PHPWG_IN_UPGRADE\', true);
305</pre>'
306      );
307
308    array_push(
309      $page['infos'],
310      'Perform a maintenance check in [Administration>General>Maintenance]
311if you encounter any problem.'
312      );
313
314    $template->assign('infos', $page['infos']);
315
316    $query = '
317UPDATE '.USER_CACHE_TABLE.'
318  SET need_update = \'true\'
319;';
320
321    pwg_query($query);
322    $query = '
323REPLACE INTO '.PLUGINS_TABLE.'
324  (id, state)
325  VALUES (\'c13y_upgrade\', \'active\')
326;';
327    pwg_query($query);
328  }
329  else
330  {
331    die('Hacking attempt');
332  }
333}
334
335// +-----------------------------------------------------------------------+
336// |                          sending html code                            |
337// +-----------------------------------------------------------------------+
338
339$template->pparse('upgrade');
340?>
Note: See TracBrowser for help on using the repository browser.