source: trunk/admin/include/functions_upgrade.php @ 12922

Last change on this file since 12922 was 12922, checked in by mistic100, 12 years ago

update Piwigo headers to 2012, last change before the expected (or not) apocalypse

  • Property svn:eol-style set to LF
File size: 9.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2012 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
24function check_upgrade()
25{
26  if (defined('PHPWG_IN_UPGRADE'))
27  {
28    return PHPWG_IN_UPGRADE;
29  }
30  return false;
31}
32
33// concerning upgrade, we use the default tables
34function prepare_conf_upgrade()
35{
36  global $prefixeTable;
37
38  // $conf is not used for users tables
39  // define cannot be re-defined
40  define('CATEGORIES_TABLE', $prefixeTable.'categories');
41  define('COMMENTS_TABLE', $prefixeTable.'comments');
42  define('CONFIG_TABLE', $prefixeTable.'config');
43  define('FAVORITES_TABLE', $prefixeTable.'favorites');
44  define('GROUP_ACCESS_TABLE', $prefixeTable.'group_access');
45  define('GROUPS_TABLE', $prefixeTable.'groups');
46  define('HISTORY_TABLE', $prefixeTable.'history');
47  define('HISTORY_SUMMARY_TABLE', $prefixeTable.'history_summary');
48  define('IMAGE_CATEGORY_TABLE', $prefixeTable.'image_category');
49  define('IMAGES_TABLE', $prefixeTable.'images');
50  define('SESSIONS_TABLE', $prefixeTable.'sessions');
51  define('SITES_TABLE', $prefixeTable.'sites');
52  define('USER_ACCESS_TABLE', $prefixeTable.'user_access');
53  define('USER_GROUP_TABLE', $prefixeTable.'user_group');
54  define('USERS_TABLE', $prefixeTable.'users');
55  define('USER_INFOS_TABLE', $prefixeTable.'user_infos');
56  define('USER_FEED_TABLE', $prefixeTable.'user_feed');
57  define('RATE_TABLE', $prefixeTable.'rate');
58  define('USER_CACHE_TABLE', $prefixeTable.'user_cache');
59  define('USER_CACHE_CATEGORIES_TABLE', $prefixeTable.'user_cache_categories');
60  define('CADDIE_TABLE', $prefixeTable.'caddie');
61  define('UPGRADE_TABLE', $prefixeTable.'upgrade');
62  define('SEARCH_TABLE', $prefixeTable.'search');
63  define('USER_MAIL_NOTIFICATION_TABLE', $prefixeTable.'user_mail_notification');
64  define('TAGS_TABLE', $prefixeTable.'tags');
65  define('IMAGE_TAG_TABLE', $prefixeTable.'image_tag');
66  define('PLUGINS_TABLE', $prefixeTable.'plugins');
67  define('OLD_PERMALINKS_TABLE', $prefixeTable.'old_permalinks');
68  define('THEMES_TABLE', $prefixeTable.'themes');
69  define('LANGUAGES_TABLE', $prefixeTable.'languages');
70}
71
72// Deactivate all non-standard plugins
73function deactivate_non_standard_plugins()
74{
75  global $page;
76
77  $standard_plugins = array(
78    'admin_multi_view',
79    'c13y_upgrade',
80    'event_tracer',
81    'language_switch',
82    'LocalFilesEditor'
83    );
84
85  $query = '
86SELECT id
87FROM '.PREFIX_TABLE.'plugins
88WHERE state = \'active\'
89AND id NOT IN (\'' . implode('\',\'', $standard_plugins) . '\')
90;';
91
92  $result = pwg_query($query);
93  $plugins = array();
94  while ($row = pwg_db_fetch_assoc($result))
95  {
96    array_push($plugins, $row['id']);
97  }
98
99  if (!empty($plugins))
100  {
101    $query = '
102UPDATE '.PREFIX_TABLE.'plugins
103SET state=\'inactive\'
104WHERE id IN (\'' . implode('\',\'', $plugins) . '\')
105;';
106    pwg_query($query);
107
108    array_push($page['infos'],
109      l10n('As a precaution, following plugins have been deactivated. You must check for plugins upgrade before reactiving them:').'<p><i>'.implode(', ', $plugins).'</i></p>');
110  }
111}
112
113// Deactivate all non-standard themes
114function deactivate_non_standard_themes()
115{
116  global $page, $conf;
117
118  $standard_themes = array(
119    'clear',
120    'Sylvia',
121    'dark',
122    );
123
124  $query = '
125SELECT
126    id,
127    name
128  FROM '.PREFIX_TABLE.'themes
129  WHERE id NOT IN (\''.implode("','", $standard_themes).'\')
130;';
131  $result = pwg_query($query);
132  $theme_ids = array();
133  $theme_names = array();
134  while ($row = pwg_db_fetch_assoc($result))
135  {
136    array_push($theme_ids, $row['id']);
137    array_push($theme_names, $row['name']);
138  }
139
140  if (!empty($theme_ids))
141  {
142    $query = '
143DELETE
144  FROM '.PREFIX_TABLE.'themes
145  WHERE id IN (\''.implode("','", $theme_ids).'\')
146;';
147    pwg_query($query);
148
149    array_push($page['infos'],
150      l10n('As a precaution, following themes have been deactivated. You must check for themes upgrade before reactiving them:').'<p><i>'.implode(', ', $theme_names).'</i></p>');
151
152    // what is the default theme?
153    $query = '
154SELECT theme
155  FROM '.PREFIX_TABLE.'user_infos
156  WHERE user_id = '.$conf['default_user_id'].'
157;';
158    list($default_theme) = pwg_db_fetch_row(pwg_query($query));
159
160    // if the default theme has just been deactivated, let's set another core theme as default
161    if (in_array($default_theme, $theme_ids))
162    {
163      $query = '
164UPDATE '.PREFIX_TABLE.'user_infos
165  SET theme = \'Sylvia\'
166  WHERE user_id = '.$conf['default_user_id'].'
167;';
168      pwg_query($query);
169    }
170  }
171}
172
173// Check access rights
174function check_upgrade_access_rights()
175{
176  global $conf, $page, $current_release;
177
178  if (version_compare($current_release, '2.0', '>=') and isset($_COOKIE[session_name()]))
179  {
180    // Check if user is already connected as webmaster
181    session_start();
182    if (!empty($_SESSION['pwg_uid']))
183    {
184      $query = '
185SELECT status
186  FROM '.USER_INFOS_TABLE.'
187  WHERE user_id = '.$_SESSION['pwg_uid'].'
188;';
189      pwg_query($query);
190
191      $row = pwg_db_fetch_assoc(pwg_query($query));
192      if (isset($row['status']) and $row['status'] == 'webmaster')
193      {
194        define('PHPWG_IN_UPGRADE', true);
195        return;
196      }
197    }
198  }
199
200  if (!isset($_POST['username']) or !isset($_POST['password']))
201  {
202    return;
203  }
204
205  $username = $_POST['username'];
206  $password = $_POST['password'];
207
208  if(!@get_magic_quotes_gpc())
209  {
210    $username = pwg_db_real_escape_string($username);
211  }
212
213  if (version_compare($current_release, '2.0', '<'))
214  {
215    $username = utf8_decode($username);
216    $password = utf8_decode($password);
217  }
218
219  if (version_compare($current_release, '1.5', '<'))
220  {
221    $query = '
222SELECT password, status
223FROM '.USERS_TABLE.'
224WHERE username = \''.$username.'\'
225;';
226  }
227  else
228  {
229    $query = '
230SELECT u.password, ui.status
231FROM '.USERS_TABLE.' AS u
232INNER JOIN '.USER_INFOS_TABLE.' AS ui
233ON u.'.$conf['user_fields']['id'].'=ui.user_id
234WHERE '.$conf['user_fields']['username'].'=\''.$username.'\'
235;';
236  }
237  $row = pwg_db_fetch_assoc(pwg_query($query));
238
239  if (!isset($conf['pass_convert']))
240  {
241    $conf['pass_convert'] = create_function('$s', 'return md5($s);');
242  }
243
244  if ($row['password'] != $conf['pass_convert']($password))
245  {
246    array_push($page['errors'], l10n('Invalid password!'));
247  }
248  elseif ($row['status'] != 'admin' and $row['status'] != 'webmaster')
249  {
250    array_push($page['errors'], l10n('You do not have access rights to run upgrade'));
251  }
252  else
253  {
254    define('PHPWG_IN_UPGRADE', true);
255  }
256}
257
258/**
259 * which upgrades are available ?
260 *
261 * @return array
262 */
263function get_available_upgrade_ids()
264{
265  $upgrades_path = PHPWG_ROOT_PATH.'install/db';
266
267  $available_upgrade_ids = array();
268
269  if ($contents = opendir($upgrades_path))
270  {
271    while (($node = readdir($contents)) !== false)
272    {
273      if (is_file($upgrades_path.'/'.$node)
274          and preg_match('/^(.*?)-database\.php$/', $node, $match))
275      {
276        array_push($available_upgrade_ids, $match[1]);
277      }
278    }
279  }
280  natcasesort($available_upgrade_ids);
281
282  return $available_upgrade_ids;
283}
284
285
286/**
287 * returns true if there are available upgrade files
288 */
289function check_upgrade_feed()
290{
291  // retrieve already applied upgrades
292  $query = '
293SELECT id
294  FROM '.UPGRADE_TABLE.'
295;';
296  $applied = array_from_query($query, 'id');
297
298  // retrieve existing upgrades
299  $existing = get_available_upgrade_ids();
300
301  // which upgrades need to be applied?
302  return (count(array_diff($existing, $applied)) > 0);
303}
304
305function upgrade_db_connect()
306{
307  global $conf;
308
309  try
310  {
311    $pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'], $conf['db_password'], $conf['db_base']);
312    if ($pwg_db_link)
313    {
314      pwg_db_check_version();
315    }
316  }
317  catch (Exception $e)
318  {
319    my_error(l10n($e->getMessage()), true); 
320  }
321}
322?>
Note: See TracBrowser for help on using the repository browser.