source: trunk/upgrade.php @ 2808

Last change on this file since 2808 was 2808, checked in by rub, 15 years ago

Reintroduce c13y_upgrade remove on r2754

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 9.1 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
32include_once(PHPWG_ROOT_PATH.'include/functions.inc.php');
33include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
34include_once(PHPWG_ROOT_PATH.'admin/include/functions_upgrade.php');
35
36include(PHPWG_ROOT_PATH.'include/mysql.inc.php');
37include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
38@include(PHPWG_ROOT_PATH. 'include/config_local.inc.php');
39
40check_upgrade();
41
42prepare_conf_upgrade();
43
44include_once(PHPWG_ROOT_PATH.'include/constants.php');
45define('PREFIX_TABLE', $prefixeTable);
46
47// Database connection
48mysql_connect( $cfgHote, $cfgUser, $cfgPassword ) or die ( "Could not connect to database server" );
49mysql_select_db( $cfgBase ) or die ( "Could not connect to database" );
50if ( version_compare(mysql_get_server_info(), '4.1.0', '>=')
51    and defined('DB_CHARSET') and DB_CHARSET!='' )
52{
53  pwg_query('SET NAMES "'.DB_CHARSET.'"');
54}
55
56// +-----------------------------------------------------------------------+
57// |                            tricky output                              |
58// +-----------------------------------------------------------------------+
59echo '<!-- This is an HTML comment given in order to make IE outputs';
60echo ' the code.'."\n";
61echo ' Indeed, IE doesn\'t start to send output until a limit';
62echo ' of XXX bytes '."\n";
63echo str_repeat( ' ', 80 )."\n";
64echo str_repeat( ' ', 80 )."\n";
65echo str_repeat( ' ', 80 )."\n";
66echo '-->'."\n";
67flush();
68// +-----------------------------------------------------------------------+
69// |                              functions                                |
70// +-----------------------------------------------------------------------+
71
72/**
73 * list all tables in an array
74 *
75 * @return array
76 */
77function get_tables()
78{
79  $tables = array();
80
81  $query = '
82SHOW TABLES
83;';
84  $result = mysql_query($query);
85
86  while ($row = mysql_fetch_row($result))
87  {
88    if (preg_match('/^'.PREFIX_TABLE.'/', $row[0]))
89    {
90      array_push($tables, $row[0]);
91    }
92  }
93
94  return $tables;
95}
96
97/**
98 * list all columns of each given table
99 *
100 * @return array of array
101 */
102function get_columns_of($tables)
103{
104  $columns_of = array();
105
106  foreach ($tables as $table)
107  {
108    $query = '
109DESC '.$table.'
110;';
111    $result = mysql_query($query);
112
113    $columns_of[$table] = array();
114
115    while ($row = mysql_fetch_row($result))
116    {
117      array_push($columns_of[$table], $row[0]);
118    }
119  }
120
121  return $columns_of;
122}
123
124/**
125 */
126function print_time($message)
127{
128  global $last_time;
129
130  $new_time = get_moment();
131  echo '<pre>['.get_elapsed_time($last_time, $new_time).']';
132  echo ' '.$message;
133  echo '</pre>';
134  flush();
135  $last_time = $new_time;
136}
137
138// +-----------------------------------------------------------------------+
139// |                             playing zone                              |
140// +-----------------------------------------------------------------------+
141
142// echo implode('<br>', get_tables());
143// echo '<pre>'; print_r(get_columns_of(get_tables())); echo '</pre>';
144
145// foreach (get_available_upgrade_ids() as $upgrade_id)
146// {
147//   echo $upgrade_id, '<br>';
148// }
149
150// +-----------------------------------------------------------------------+
151// |                        template initialization                        |
152// +-----------------------------------------------------------------------+
153
154$template = new Template(PHPWG_ROOT_PATH.'admin/template/goto');
155$template->set_filenames(array('upgrade'=>'upgrade.tpl'));
156$template->assign('RELEASE', PHPWG_VERSION);
157
158// +-----------------------------------------------------------------------+
159// |                            upgrade choice                             |
160// +-----------------------------------------------------------------------+
161
162$tables = get_tables();
163$columns_of = get_columns_of($tables);
164
165if (!isset($_GET['version']))
166{
167  // find the current release
168  if (!in_array('param', $columns_of[PREFIX_TABLE.'config']))
169  {
170    // we're in branch 1.3, important upgrade, isn't it?
171    if (in_array(PREFIX_TABLE.'user_category', $tables))
172    {
173      $current_release = '1.3.1';
174    }
175    else
176    {
177      $current_release = '1.3.0';
178    }
179  }
180  else if (!in_array(PREFIX_TABLE.'user_cache', $tables))
181  {
182    $current_release = '1.4.0';
183  }
184  else if (!in_array(PREFIX_TABLE.'tags', $tables))
185  {
186    $current_release = '1.5.0';
187  }
188  else if ( !in_array(PREFIX_TABLE.'history_summary', $tables) )
189  {
190    if (!in_array('auto_login_key', $columns_of[PREFIX_TABLE.'user_infos']))
191    {
192      $current_release = '1.6.0';
193    }
194    else
195    {
196      $current_release = '1.6.2';
197    }
198  }
199  else if (!in_array('md5sum', $columns_of[PREFIX_TABLE.'images']))
200  {
201    $current_release = '1.7.0';
202  }
203  else
204  {
205    die('No upgrade required, the database structure is up to date');
206  }
207
208  $template->assign(
209    'introduction',
210    array(
211      'CURRENT_RELEASE' => $current_release,
212      'RUN_UPGRADE_URL' =>
213        PHPWG_ROOT_PATH.'upgrade.php?version='.$current_release,
214      )
215    );
216}
217
218// +-----------------------------------------------------------------------+
219// |                            upgrade launch                             |
220// +-----------------------------------------------------------------------+
221
222else
223{
224  if (in_array('md5sum', $columns_of[PREFIX_TABLE.'images']))
225  {
226    die('No database upgrade required, do not refresh the page');
227  }
228
229  $upgrade_file = PHPWG_ROOT_PATH.'install/upgrade_'.$_GET['version'].'.php';
230  if (is_file($upgrade_file))
231  {
232    $page['infos'] = array();
233    $page['upgrade_start'] = get_moment();
234    $conf['die_on_sql_error'] = false;
235    include($upgrade_file);
236
237    // Plugins deactivation
238    if (in_array(PREFIX_TABLE.'plugins', $tables))
239    {
240      $query = '
241UPDATE '.PREFIX_TABLE.'plugins SET state="inactive" WHERE state="active"
242;';
243      mysql_query($query);
244
245      if (mysql_affected_rows() > 0)
246      {
247        array_push(
248          $page['infos'],
249          'As a precaution, all activated plugins have been deactivated.
250You must check for plugins upgrade before reactiving them.'
251        );
252      }
253    }
254
255    // Create empty local files to avoid log errors
256    create_empty_local_files();
257
258    $page['upgrade_end'] = get_moment();
259
260    $template->assign(
261      'upgrade',
262      array(
263        'VERSION' => $_GET['version'],
264        'TOTAL_TIME' => get_elapsed_time(
265          $page['upgrade_start'],
266          $page['upgrade_end']
267          ),
268        'SQL_TIME' => number_format(
269          $page['queries_time'],
270          3,
271          '.',
272          ' '
273          ).' s',
274        'NB_QUERIES' => $page['count_queries']
275        )
276      );
277
278    array_push(
279      $page['infos'],
280      '[security] delete files "upgrade.php", "upgrade_feed.php", "install.php" and "install"
281directory'
282      );
283
284    array_push(
285      $page['infos'],
286      'in include/mysql.inc.php, remove
287<pre style="background-color:lightgray">
288define(\'PHPWG_IN_UPGRADE\', true);
289</pre>'
290      );
291
292    array_push(
293      $page['infos'],
294      'Perform a maintenance check in [Administration>General>Maintenance]
295if you encounter any problem.'
296      );
297
298    $template->assign('infos', $page['infos']);
299
300    invalidate_user_cache();
301
302    $query = '
303REPLACE INTO '.PLUGINS_TABLE.'
304  (id, state)
305  VALUES (\'c13y_upgrade\', \'active\')
306;';
307    pwg_query($query);
308  }
309  else
310  {
311    die('Hacking attempt');
312  }
313}
314
315// +-----------------------------------------------------------------------+
316// |                          sending html code                            |
317// +-----------------------------------------------------------------------+
318
319$template->pparse('upgrade');
320?>
Note: See TracBrowser for help on using the repository browser.