source: trunk/upgrade.php @ 2747

Last change on this file since 2747 was 2747, checked in by patdenice, 16 years ago
  • Install process now looks like goto/roma.
  • Move install.tpl to goto template.
  • add charset utf8 to fatal error function.
  • Check php version on install and upgrade, and die if < 5.
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 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.'template/yoga');
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
200  {
201    die('No upgrade required, the database structure is up to date');
202  }
203
204  $template->assign(
205    'introduction',
206    array(
207      'CURRENT_RELEASE' => $current_release,
208      'RUN_UPGRADE_URL' =>
209        PHPWG_ROOT_PATH.'upgrade.php?version='.$current_release,
210      )
211    );
212}
213
214// +-----------------------------------------------------------------------+
215// |                            upgrade launch                             |
216// +-----------------------------------------------------------------------+
217
218else
219{
220  if (in_array(PREFIX_TABLE.'history_summary', $tables))
221  {
222    die('No database upgrade required, do not refresh the page');
223  }
224
225  $upgrade_file = PHPWG_ROOT_PATH.'install/upgrade_'.$_GET['version'].'.php';
226  if (is_file($upgrade_file))
227  {
228    $page['infos'] = array();
229    $page['upgrade_start'] = get_moment();
230    $conf['die_on_sql_error'] = false;
231    include($upgrade_file);
232
233    // Available upgrades must be ignored after a fresh installation. To
234    // make PWG avoid upgrading, we must tell it upgrades have already been
235    // made.
236    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
237    define('CURRENT_DATE', $dbnow);
238    $datas = array();
239    foreach (get_available_upgrade_ids() as $upgrade_id)
240    {
241      array_push(
242        $datas,
243        array(
244          'id'          => $upgrade_id,
245          'applied'     => CURRENT_DATE,
246          'description' => 'upgrade included in migration',
247          )
248        );
249    }
250    mass_inserts(
251      UPGRADE_TABLE,
252      array_keys($datas[0]),
253      $datas
254      );
255
256    // Create empty local files to avoid log errors
257    create_empty_local_files();
258
259    $page['upgrade_end'] = get_moment();
260
261    $template->assign(
262      'upgrade',
263      array(
264        'VERSION' => $_GET['version'],
265        'TOTAL_TIME' => get_elapsed_time(
266          $page['upgrade_start'],
267          $page['upgrade_end']
268          ),
269        'SQL_TIME' => number_format(
270          $page['queries_time'],
271          3,
272          '.',
273          ' '
274          ).' s',
275        'NB_QUERIES' => $page['count_queries']
276        )
277      );
278
279    array_push(
280      $page['infos'],
281      '[security] delete files "upgrade.php", "upgrade_feed.php", "install.php" and "install"
282directory'
283      );
284
285    array_push(
286      $page['infos'],
287      'in include/mysql.inc.php, remove
288<pre style="background-color:lightgray">
289define(\'PHPWG_IN_UPGRADE\', true);
290</pre>'
291      );
292
293    array_push(
294      $page['infos'],
295      'Perform a maintenance check in [Administration>General>Maintenance]
296if you encounter any problem.'
297      );
298
299    $template->assign('infos', $page['infos']);
300
301    $query = '
302UPDATE '.USER_CACHE_TABLE.'
303  SET need_update = \'true\'
304;';
305
306    pwg_query($query);
307    $query = '
308REPLACE INTO '.PLUGINS_TABLE.'
309  (id, state)
310  VALUES (\'c13y_upgrade\', \'active\')
311;';
312    pwg_query($query);
313  }
314  else
315  {
316    die('Hacking attempt');
317  }
318}
319
320// +-----------------------------------------------------------------------+
321// |                          sending html code                            |
322// +-----------------------------------------------------------------------+
323
324$template->pparse('upgrade');
325?>
Note: See TracBrowser for help on using the repository browser.