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

Last change on this file since 5387 was 5387, checked in by patdenice, 14 years ago

Fix php5 apache configuration with upgrade.

  • Property svn:eol-style set to LF
File size: 7.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 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('WAITING_TABLE', $prefixeTable.'waiting');
58  define('RATE_TABLE', $prefixeTable.'rate');
59  define('USER_CACHE_TABLE', $prefixeTable.'user_cache');
60  define('USER_CACHE_CATEGORIES_TABLE', $prefixeTable.'user_cache_categories');
61  define('CADDIE_TABLE', $prefixeTable.'caddie');
62  define('UPGRADE_TABLE', $prefixeTable.'upgrade');
63  define('SEARCH_TABLE', $prefixeTable.'search');
64  define('USER_MAIL_NOTIFICATION_TABLE', $prefixeTable.'user_mail_notification');
65  define('TAGS_TABLE', $prefixeTable.'tags');
66  define('IMAGE_TAG_TABLE', $prefixeTable.'image_tag');
67  define('PLUGINS_TABLE', $prefixeTable.'plugins');
68  define('OLD_PERMALINKS_TABLE', $prefixeTable.'old_permalinks');
69  define('THEMES_TABLE', $prefixeTable.'themes');
70  define('LANGUAGES_TABLE', $prefixeTable.'languages');
71}
72
73// Deactivate all non-standard plugins
74function deactivate_non_standard_plugins()
75{
76  global $page;
77
78  $standard_plugins = array(
79    'add_index',
80    'admin_advices',
81    'admin_multi_view',
82    'c13y_upgrade',
83    'event_tracer',
84    'language_switch',
85    'LocalFilesEditor'
86    );
87
88  $query = '
89SELECT id
90FROM '.PREFIX_TABLE.'plugins
91WHERE state = "active"
92AND id NOT IN ("' . implode('","', $standard_plugins) . '")
93;';
94
95  $result = pwg_query($query);
96  $plugins = array();
97  while ($row = pwg_db_fetch_assoc($result))
98  {
99    array_push($plugins, $row['id']);
100  }
101
102  if (!empty($plugins))
103  {
104    $query = '
105UPDATE '.PREFIX_TABLE.'plugins
106SET state="inactive"
107WHERE id IN ("' . implode('","', $plugins) . '")
108;';
109    pwg_query($query);
110
111    array_push($page['infos'],
112      l10n('As a precaution, following plugins have been deactivated. You must check for plugins upgrade before reactiving them:').'<p><i>'.implode(', ', $plugins).'</i></p>');
113  }
114}
115
116// Check access rights
117function check_upgrade_access_rights($current_release, $username, $password)
118{
119  global $conf, $page;
120
121  if(!@get_magic_quotes_gpc())
122  {
123    $username = pwg_db_real_escape_string($username);
124  }
125
126  if (version_compare($current_release, '2.0', '<'))
127  {
128    $username = utf8_decode($username);
129    $password = utf8_decode($password);
130  }
131
132  if (version_compare($current_release, '1.5', '<'))
133  {
134    $query = '
135SELECT password, status
136FROM '.USERS_TABLE.'
137WHERE username = "'.$username.'"
138;';
139  }
140  else
141  {
142    $query = '
143SELECT u.password, ui.status
144FROM '.USERS_TABLE.' AS u
145INNER JOIN '.USER_INFOS_TABLE.' AS ui
146ON u.'.$conf['user_fields']['id'].'=ui.user_id
147WHERE '.$conf['user_fields']['username'].'="'.$username.'"
148;';
149  }
150  $row = pwg_db_fetch_assoc(pwg_query($query));
151
152  if (!isset($conf['pass_convert']))
153  {
154    $conf['pass_convert'] = create_function('$s', 'return md5($s);');
155  }
156
157  if ($row['password'] != $conf['pass_convert']($password))
158  {
159    array_push($page['errors'], l10n('Invalid password!'));
160  }
161  elseif ($row['status'] != 'admin' and $row['status'] != 'webmaster')
162  {
163    array_push($page['errors'], l10n('You do not have access rights to run upgrade'));
164  }
165  else
166  {
167    define('PHPWG_IN_UPGRADE', true);
168  }
169}
170
171/**
172 * which upgrades are available ?
173 *
174 * @return array
175 */
176function get_available_upgrade_ids()
177{
178  $upgrades_path = PHPWG_ROOT_PATH.'install/db';
179
180  $available_upgrade_ids = array();
181
182  if ($contents = opendir($upgrades_path))
183  {
184    while (($node = readdir($contents)) !== false)
185    {
186      if (is_file($upgrades_path.'/'.$node)
187          and preg_match('/^(.*?)-database\.php$/', $node, $match))
188      {
189        array_push($available_upgrade_ids, $match[1]);
190      }
191    }
192  }
193  natcasesort($available_upgrade_ids);
194
195  return $available_upgrade_ids;
196}
197
198
199/**
200 * returns true if there are available upgrade files
201 */
202function check_upgrade_feed()
203{
204  // retrieve already applied upgrades
205  $query = '
206SELECT id
207  FROM '.UPGRADE_TABLE.'
208;';
209  $applied = array_from_query($query, 'id');
210
211  // retrieve existing upgrades
212  $existing = get_available_upgrade_ids();
213
214  // which upgrades need to be applied?
215  return (count(array_diff($existing, $applied)) > 0);
216}
217
218function upgrade_db_connect()
219{
220  global $conf;
221
222  try
223  {
224    $pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'],
225                                  $conf['db_password'], $conf['db_base']);
226  }
227  catch (Exception $e)
228  {
229    my_error(l10n($e->getMessage()), true); 
230  }
231}
232
233?>
Note: See TracBrowser for help on using the repository browser.