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

Last change on this file since 29735 was 29735, checked in by plg, 10 years ago

bug fixed: no need to check the table piwigo_plugins, this table always exists at this point

  • 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-2014 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    'AdminTools',
79    'TakeATour',
80    'language_switch',
81    'LocalFilesEditor'
82    );
83
84  $query = '
85SELECT id
86FROM '.PREFIX_TABLE.'plugins
87WHERE state = \'active\'
88AND id NOT IN (\'' . implode('\',\'', $standard_plugins) . '\')
89;';
90
91  $result = pwg_query($query);
92  $plugins = array();
93  while ($row = pwg_db_fetch_assoc($result))
94  {
95    $plugins[] = $row['id'];
96  }
97
98  if (!empty($plugins))
99  {
100    $query = '
101UPDATE '.PREFIX_TABLE.'plugins
102SET state=\'inactive\'
103WHERE id IN (\'' . implode('\',\'', $plugins) . '\')
104;';
105    pwg_query($query);
106
107    $page['infos'][] = l10n('As a precaution, following plugins have been deactivated. You must check for plugins upgrade before reactiving them:')
108                        .'<p><i>'.implode(', ', $plugins).'</i></p>';
109  }
110}
111
112// Deactivate all non-standard themes
113function deactivate_non_standard_themes()
114{
115  global $page, $conf;
116
117  $standard_themes = array(
118    'clear',
119    'Sylvia',
120    'dark',
121    'elegant',
122    'smartpocket',
123    );
124
125  $query = '
126SELECT
127    id,
128    name
129  FROM '.PREFIX_TABLE.'themes
130  WHERE id NOT IN (\''.implode("','", $standard_themes).'\')
131;';
132  $result = pwg_query($query);
133  $theme_ids = array();
134  $theme_names = array();
135  while ($row = pwg_db_fetch_assoc($result))
136  {
137    $theme_ids[] = $row['id'];
138    $theme_names[] = $row['name'];
139  }
140
141  if (!empty($theme_ids))
142  {
143    $query = '
144DELETE
145  FROM '.PREFIX_TABLE.'themes
146  WHERE id IN (\''.implode("','", $theme_ids).'\')
147;';
148    pwg_query($query);
149
150    $page['infos'][] = l10n('As a precaution, following themes have been deactivated. You must check for themes upgrade before reactiving them:')
151                        .'<p><i>'.implode(', ', $theme_names).'</i></p>';
152
153    // what is the default theme?
154    $query = '
155SELECT theme
156  FROM '.PREFIX_TABLE.'user_infos
157  WHERE user_id = '.$conf['default_user_id'].'
158;';
159    list($default_theme) = pwg_db_fetch_row(pwg_query($query));
160
161    // if the default theme has just been deactivated, let's set another core theme as default
162    if (in_array($default_theme, $theme_ids))
163    {
164      $query = '
165UPDATE '.PREFIX_TABLE.'user_infos
166  SET theme = \'elegant\'
167  WHERE user_id = '.$conf['default_user_id'].'
168;';
169      pwg_query($query);
170    }
171  }
172}
173
174// Deactivate all templates
175function deactivate_templates()
176{
177  $query = '
178  UPDATE '.PREFIX_TABLE.'config
179    SET value = \''. array() .'\'
180  WHERE param = \'extents_for_templates\';';
181}
182
183// Check access rights
184function check_upgrade_access_rights()
185{
186  global $conf, $page, $current_release;
187
188  if (version_compare($current_release, '2.0', '>=') and isset($_COOKIE[session_name()]))
189  {
190    // Check if user is already connected as webmaster
191    session_start();
192    if (!empty($_SESSION['pwg_uid']))
193    {
194      $query = '
195SELECT status
196  FROM '.USER_INFOS_TABLE.'
197  WHERE user_id = '.$_SESSION['pwg_uid'].'
198;';
199      pwg_query($query);
200
201      $row = pwg_db_fetch_assoc(pwg_query($query));
202      if (isset($row['status']) and $row['status'] == 'webmaster')
203      {
204        define('PHPWG_IN_UPGRADE', true);
205        return;
206      }
207    }
208  }
209
210  if (!isset($_POST['username']) or !isset($_POST['password']))
211  {
212    return;
213  }
214
215  $username = $_POST['username'];
216  $password = $_POST['password'];
217
218  if(!@get_magic_quotes_gpc())
219  {
220    $username = pwg_db_real_escape_string($username);
221  }
222
223  if (version_compare($current_release, '2.0', '<'))
224  {
225    $username = utf8_decode($username);
226    $password = utf8_decode($password);
227  }
228
229  if (version_compare($current_release, '1.5', '<'))
230  {
231    $query = '
232SELECT password, status
233FROM '.USERS_TABLE.'
234WHERE username = \''.$username.'\'
235;';
236  }
237  else
238  {
239    $query = '
240SELECT u.password, ui.status
241FROM '.USERS_TABLE.' AS u
242INNER JOIN '.USER_INFOS_TABLE.' AS ui
243ON u.'.$conf['user_fields']['id'].'=ui.user_id
244WHERE '.$conf['user_fields']['username'].'=\''.$username.'\'
245;';
246  }
247  $row = pwg_db_fetch_assoc(pwg_query($query));
248
249  if (!$conf['password_verify']($password, $row['password']))
250  {
251    $page['errors'][] = l10n('Invalid password!');
252  }
253  elseif ($row['status'] != 'admin' and $row['status'] != 'webmaster')
254  {
255    $page['errors'][] = l10n('You do not have access rights to run upgrade');
256  }
257  else
258  {
259    define('PHPWG_IN_UPGRADE', true);
260  }
261}
262
263/**
264 * which upgrades are available ?
265 *
266 * @return array
267 */
268function get_available_upgrade_ids()
269{
270  $upgrades_path = PHPWG_ROOT_PATH.'install/db';
271
272  $available_upgrade_ids = array();
273
274  if ($contents = opendir($upgrades_path))
275  {
276    while (($node = readdir($contents)) !== false)
277    {
278      if (is_file($upgrades_path.'/'.$node)
279          and preg_match('/^(.*?)-database\.php$/', $node, $match))
280      {
281        $available_upgrade_ids[] = $match[1];
282      }
283    }
284  }
285  natcasesort($available_upgrade_ids);
286
287  return $available_upgrade_ids;
288}
289
290
291/**
292 * returns true if there are available upgrade files
293 */
294function check_upgrade_feed()
295{
296  // retrieve already applied upgrades
297  $query = '
298SELECT id
299  FROM '.UPGRADE_TABLE.'
300;';
301  $applied = array_from_query($query, 'id');
302
303  // retrieve existing upgrades
304  $existing = get_available_upgrade_ids();
305
306  // which upgrades need to be applied?
307  return (count(array_diff($existing, $applied)) > 0);
308}
309
310function upgrade_db_connect()
311{
312  global $conf;
313
314  try
315  {
316    pwg_db_connect($conf['db_host'], $conf['db_user'],
317                   $conf['db_password'], $conf['db_base']);
318    pwg_db_check_version();
319  }
320  catch (Exception $e)
321  {
322    my_error(l10n($e->getMessage()), true); 
323  }
324}
325?>
Note: See TracBrowser for help on using the repository browser.