phpBB
Statistics
| Revision:

root / tags / milestone_3 / phpBB / develop / add_permissions.php

History | View | Annotate | Download (11.5 kB)

1
<?php
2
// -------------------------------------------------------------
3
//
4
// $Id: add_permissions.php 5071 2005-01-21 19:20:26Z acydburn $
5
//
6
// FILENAME  : add_permissions.php
7
// STARTED   : Sat Nov 06, 2004
8
// COPYRIGHT : © 2004 phpBB Group
9
// WWW       : http://www.phpbb.com/
10
// LICENCE   : GPL vs2.0 [ see /docs/COPYING ] 
11
// 
12
// -------------------------------------------------------------
13
14
//
15
// Security message:
16
//
17
// This script is potentially dangerous.
18
// Remove or comment the next line (die(".... ) to enable this script.
19
// Do NOT FORGET to either remove this script or disable it after you have used it.
20
//
21
die("Please read the first lines of this script for instructions on how to enable it");
22
23
24
// This script adds missing permissions
25
$db = $dbhost = $dbuser = $dbpasswd = $dbport = $dbname = '';
26
27
define('IN_PHPBB', 1);
28
define('ANONYMOUS', 1);
29
$phpEx = substr(strrchr(__FILE__, '.'), 1);
30
$phpbb_root_path='./../';
31
include($phpbb_root_path . 'config.'.$phpEx);
32
require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.'.$phpEx);
33
require($phpbb_root_path . 'includes/db/' . $dbms . '.'.$phpEx);
34
include($phpbb_root_path . 'includes/functions.'.$phpEx);
35
36
define('ACL_NO', 0);
37
define('ACL_YES', 1);
38
define('ACL_UNSET', -1);
39
40
define('ACL_GROUPS_TABLE', $table_prefix.'auth_groups');
41
define('ACL_OPTIONS_TABLE', $table_prefix.'auth_options');
42
define('ACL_USERS_TABLE', $table_prefix.'auth_users');
43
define('GROUPS_TABLE', $table_prefix.'groups');
44
define('USERS_TABLE', $table_prefix.'users');
45
46
$cache                = new acm();
47
$db                        = new sql_db();
48
49
// Connect to DB
50
$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false);
51
52
// auth => is_local, is_global
53
$f_permissions = array(
54
        'f_'                => array(1, 0),
55
        'f_list'        => array(1, 0),
56
        'f_read'        => array(1, 0),
57
        'f_post'        => array(1, 0),
58
        'f_reply'        => array(1, 0),
59
        'f_quote'        => array(1, 0),
60
        'f_edit'        => array(1, 0),
61
        'f_user_lock'        => array(1, 0),
62
        'f_delete'        => array(1, 0),
63
        'f_bump'        => array(1, 0),
64
        'f_poll'        => array(1, 0),
65
        'f_vote'        => array(1, 0),
66
        'f_votechg'        => array(1, 0),
67
        'f_announce'=> array(1, 0),
68
        'f_sticky'        => array(1, 0),
69
        'f_attach'        => array(1, 0),
70
        'f_download'=> array(1, 0),
71
        'f_icons'        => array(1, 0),
72
        'f_html'        => array(1, 0),
73
        'f_bbcode'        => array(1, 0),
74
        'f_smilies'        => array(1, 0),
75
        'f_img'                => array(1, 0),
76
        'f_flash'        => array(1, 0),
77
        'f_sigs'        => array(1, 0),
78
        'f_search'        => array(1, 0),
79
        'f_email'        => array(1, 0),
80
        'f_rate'        => array(1, 0),
81
        'f_print'        => array(1, 0),
82
        'f_ignoreflood'        => array(1, 0),
83
        'f_postcount'        => array(1, 0),
84
        'f_moderate'=> array(1, 0),
85
        'f_report'        => array(1, 0),
86
        'f_subscribe'        => array(1, 0),
87
);
88
89
$m_permissions = array(
90
        'm_'                => array(1, 1),
91
        'm_edit'        => array(1, 1),
92
        'm_delete'        => array(1, 1),
93
        'm_move'        => array(1, 1),
94
        'm_lock'        => array(1, 1),
95
        'm_split'        => array(1, 1),
96
        'm_merge'        => array(1, 1),
97
        'm_approve'        => array(1, 1),
98
        'm_unrate'        => array(1, 1),
99
        'm_auth'        => array(1, 1),
100
        'm_ip'                => array(1, 1),
101
        'm_info'        => array(1, 1),
102
);
103
104
$a_permissions = array(
105
        'a_'                => array(0, 1),
106
        'a_server'        => array(0, 1),
107
        'a_defaults'=> array(0, 1),
108
        'a_board'        => array(0, 1),
109
        'a_cookies'        => array(0, 1),
110
        'a_clearlogs'        => array(0, 1),
111
        'a_words'        => array(0, 1),
112
        'a_icons'        => array(0, 1),
113
        'a_bbcode'        => array(0, 1),
114
        'a_attach'        => array(0, 1),
115
        'a_email'        => array(0, 1),
116
        'a_styles'        => array(0, 1),
117
        'a_user'        => array(0, 1),
118
        'a_useradd'        => array(0, 1),
119
        'a_userdel'        => array(0, 1),
120
        'a_ranks'        => array(0, 1),
121
        'a_ban'                => array(0, 1),
122
        'a_names'        => array(0, 1),
123
        'a_group'        => array(0, 1),
124
        'a_groupadd'=> array(0, 1),
125
        'a_groupdel'=> array(0, 1),
126
        'a_forum'        => array(0, 1),
127
        'a_forumadd'=> array(0, 1),
128
        'a_forumdel'=> array(0, 1),
129
        'a_prune'        => array(0, 1),
130
        'a_auth'        => array(0, 1),
131
        'a_authmods'=> array(0, 1),
132
        'a_authadmins'        => array(0, 1),
133
        'a_authusers'        => array(0, 1),
134
        'a_authgroups'        => array(0, 1),
135
        'a_authdeps'=> array(0, 1),
136
        'a_backup'        => array(0, 1),
137
        'a_restore'        => array(0, 1),
138
        'a_search'        => array(0, 1),
139
        'a_events'        => array(0, 1),
140
        'a_cron'        => array(0, 1),
141
);
142
143
$u_permissions = array(
144
        'u_'                        => array(0, 1),
145
        'u_sendemail'        => array(0, 1),
146
        'u_readpm'                => array(0, 1),
147
        'u_sendpm'                => array(0, 1),
148
        'u_sendim'                => array(0, 1),
149
        'u_hideonline'        => array(0, 1),
150
        'u_viewonline'        => array(0, 1),
151
        'u_viewprofile'        => array(0, 1),
152
        'u_chgavatar'        => array(0, 1),
153
        'u_chggrp'                => array(0, 1),
154
        'u_chgemail'        => array(0, 1),
155
        'u_chgname'                => array(0, 1),
156
        'u_chgpasswd'        => array(0, 1),
157
        'u_chgcensors'        => array(0, 1),
158
        'u_search'                => array(0, 1),
159
        'u_savedrafts'        => array(0, 1),
160
        'u_download'        => array(0, 1),
161
        'u_attach'                => array(0, 1),
162
        'u_sig'                        => array(0, 1),
163
        'u_pm_attach'        => array(0, 1),
164
        'u_pm_html'                => array(0, 1),
165
        'u_pm_bbcode'        => array(0, 1),
166
        'u_pm_smilies'        => array(0, 1),
167
        'u_pm_download'        => array(0, 1),
168
        'u_pm_report'        => array(0, 1),
169
        'u_pm_edit'                => array(0, 1),
170
        'u_pm_printpm'        => array(0, 1),
171
        'u_pm_emailpm'        => array(0, 1),
172
        'u_pm_forward'        => array(0, 1),
173
        'u_pm_delete'        => array(0, 1),
174
        'u_pm_img'                => array(0, 1),
175
        'u_pm_flash'        => array(0, 1),
176
);
177
178
echo "<p><b>Determining existing permissions</b></p>\n";
179
180
$sql = 'SELECT auth_option_id, auth_option FROM ' . ACL_OPTIONS_TABLE;
181
$result = $db->sql_query($sql);
182
183
$remove_auth_options = array();
184
while ($row = $db->sql_fetchrow($result))
185
{
186
        if (!in_array($row['auth_option'], array_keys(${substr($row['auth_option'], 0, 2) . 'permissions'})))
187
        {
188
                $remove_auth_options[$row['auth_option']] = $row['auth_option_id'];
189
        }
190
        unset(${substr($row['auth_option'], 0, 2) . 'permissions'}[$row['auth_option']]);
191
}
192
$db->sql_freeresult($result);
193
194
if (sizeof($remove_auth_options))
195
{
196
        $db->sql_query('DELETE FROM ' . ACL_USERS_TABLE . ' WHERE auth_option_id IN (' . implode(', ', $remove_auth_options) . ')');
197
        $db->sql_query('DELETE FROM ' . ACL_GROUPS_TABLE . ' WHERE auth_option_id IN (' . implode(', ', $remove_auth_options) . ')');
198
        $db->sql_query('DELETE FROM ' . ACL_OPTIONS_TABLE . ' WHERE auth_option_id IN (' . implode(', ', $remove_auth_options) . ')');
199
200
        echo '<p><b>Removed the following auth options... [<i>' . implode(', ', array_keys($remove_auth_options)) . "</i>]</b></p>\n\n";
201
}
202
203
$prefixes = array('f_', 'a_', 'm_', 'u_');
204
205
foreach ($prefixes as $prefix)
206
{
207
        $var = $prefix . 'permissions';
208
        if (sizeof($$var))
209
        {
210
                foreach ($$var as $auth_option => $l_ary)
211
                {
212
                        $sql_ary = array(
213
                                'auth_option'        => $auth_option,
214
                                'is_local'                => $l_ary[0],
215
                                'is_global'                => $l_ary[1]
216
                        );
217
218
                        $db->sql_query('INSERT INTO ' . ACL_OPTIONS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
219
                
220
                        echo "<p><b>Adding $auth_option...</b></p>\n";
221
222
                        mass_auth('group', 0, 'guests', $auth_option, ACL_NO);
223
                        mass_auth('group', 0, 'inactive', $auth_option, ACL_NO);
224
                        mass_auth('group', 0, 'inactive_coppa', $auth_option, ACL_NO);
225
                        mass_auth('group', 0, 'registered_coppa', $auth_option, ACL_NO);
226
                        mass_auth('group', 0, 'registered', $auth_option, (($prefix != 'm_' && $prefix != 'a_') ? ACL_YES : ACL_NO));
227
                        mass_auth('group', 0, 'super_moderators', $auth_option, (($prefix != 'a_') ? ACL_YES : ACL_NO));
228
                        mass_auth('group', 0, 'administrators', $auth_option, ACL_YES);
229
                        mass_auth('group', 0, 'bots', $auth_option, (($prefix != 'm_' && $prefix != 'a_') ? ACL_YES : ACL_NO));
230
                }
231
        }
232
}
233
234
$sql = 'UPDATE ' . USERS_TABLE . " SET user_permissions = ''";
235
$db->sql_query($sql);
236
237
$cache->destroy('acl_options');
238
$cache->save();
239
240
echo "<p><b>Done</b></p>\n";
241
 
242
/*
243
        $ug_type = user|group
244
        $forum_id = forum ids (array|int|0) -> 0 == all forums
245
        $ug_id = [int] user_id|group_id : [string] usergroup name
246
        $acl_list = [string] acl entry : [array] acl entries
247
        $setting = ACL_YES|ACL_NO|ACL_UNSET
248
*/
249
function mass_auth($ug_type, $forum_id, $ug_id, $acl_list, $setting)
250
{
251
        global $db;
252
        static $acl_option_ids, $group_ids;
253
254
        if ($ug_type == 'group' && is_string($ug_id))
255
        {
256
                if (!isset($group_ids[$ug_id]))
257
                {
258
                        $sql = 'SELECT group_id FROM ' . GROUPS_TABLE . " 
259
                                WHERE group_name = '" . strtoupper($ug_id) . "'";
260
                        $result = $db->sql_query_limit($sql, 1);
261
                        $id = (int) $db->sql_fetchfield('group_id', 0, $result);
262
                        $db->sql_freeresult($result);
263
264
                        if (!$id)
265
                        {
266
                                return;
267
                        }
268
269
                        $group_ids[$ug_id] = $id;
270
                }
271
272
                $ug_id = (int) $group_ids[$ug_id];
273
        }
274
275
        // Build correct parameters
276
        $auth = array();
277
278
        if (!is_array($acl_list))
279
        {
280
                $auth = array($acl_list => $setting);
281
        }
282
        else
283
        {
284
                foreach ($acl_list as $auth_option)
285
                {
286
                        $auth[$auth_option] = $setting;
287
                }
288
        }
289
        unset($acl_list);
290
291
        if (!is_array($forum_id))
292
        {
293
                $forum_id = array($forum_id);
294
        }
295
296
        // Set any flags as required
297
        foreach ($auth as $auth_option => $acl_setting)
298
        {
299
                $flag = substr($auth_option, 0, strpos($auth_option, '_') + 1);
300
                if (empty($auth[$flag]))
301
                {
302
                        $auth[$flag] = $acl_setting;
303
                }
304
        }
305
306
        if (!is_array($acl_option_ids) || empty($acl_option_ids))
307
        {
308
                $sql = 'SELECT auth_option_id, auth_option
309
                        FROM ' . ACL_OPTIONS_TABLE;
310
                $result = $db->sql_query($sql);
311
312
                while ($row = $db->sql_fetchrow($result))
313
                {
314
                        $acl_option_ids[$row['auth_option']] = $row['auth_option_id'];
315
                }
316
                $db->sql_freeresult($result);
317
        }
318
319
        $sql_forum = 'AND a.forum_id IN (' . implode(', ', array_map('intval', $forum_id)) . ')';
320
321
        $sql = ($ug_type == 'user') ? 'SELECT o.auth_option_id, o.auth_option, a.forum_id, a.auth_setting FROM ' . ACL_USERS_TABLE . ' a, ' . ACL_OPTIONS_TABLE . " o WHERE a.auth_option_id = o.auth_option_id $sql_forum AND a.user_id = $ug_id" : 'SELECT o.auth_option_id, o.auth_option, a.forum_id, a.auth_setting FROM ' . ACL_GROUPS_TABLE . ' a, ' . ACL_OPTIONS_TABLE . " o WHERE a.auth_option_id = o.auth_option_id $sql_forum AND a.group_id = $ug_id";
322
        $result = $db->sql_query($sql);
323
324
        $cur_auth = array();
325
        while ($row = $db->sql_fetchrow($result))
326
        {
327
                $cur_auth[$row['forum_id']][$row['auth_option_id']] = $row['auth_setting'];
328
        }
329
        $db->sql_freeresult($result);
330
331
        $table = ($ug_type == 'user') ? ACL_USERS_TABLE : ACL_GROUPS_TABLE;
332
        $id_field  = $ug_type . '_id';
333
334
        $sql_ary = array();
335
        foreach ($forum_id as $forum)
336
        {
337
                foreach ($auth as $auth_option => $setting)
338
                {
339
                        $auth_option_id = $acl_option_ids[$auth_option];
340
341
                        if (!$auth_option_id)
342
                        {
343
                                continue;
344
                        }
345
346
                        switch ($setting)
347
                        {
348
                                case ACL_UNSET:
349
                                        if (isset($cur_auth[$forum][$auth_option_id]))
350
                                        {
351
                                                $sql_ary['delete'][] = "DELETE FROM $table 
352
                                                        WHERE forum_id = $forum
353
                                                                AND auth_option_id = $auth_option_id
354
                                                                AND $id_field = $ug_id";
355
                                        }
356
                                        break;
357
358
                                default:
359
                                        if (!isset($cur_auth[$forum][$auth_option_id]))
360
                                        {
361
                                                $sql_ary['insert'][] = "$ug_id, $forum, $auth_option_id, $setting";
362
                                        }
363
                                        else if ($cur_auth[$forum][$auth_option_id] != $setting)
364
                                        {
365
                                                $sql_ary['update'][] = "UPDATE " . $table . " 
366
                                                        SET auth_setting = $setting 
367
                                                        WHERE $id_field = $ug_id 
368
                                                                AND forum_id = $forum 
369
                                                                AND auth_option_id = $auth_option_id";
370
                                        }
371
                        }
372
                }
373
        }
374
        unset($cur_auth);
375
376
        $sql = '';
377
        foreach ($sql_ary as $sql_type => $sql_subary)
378
        {
379
                switch ($sql_type)
380
                {
381
                        case 'insert':
382
                                switch (SQL_LAYER)
383
                                {
384
                                        case 'mysql':
385
                                        case 'mysql4':
386
                                                $sql = 'VALUES ' . implode(', ', preg_replace('#^(.*?)$#', '(\1)', $sql_subary));
387
                                                break;
388
389
                                        case 'mssql':
390
                                        case 'sqlite':
391
                                                $sql = implode(' UNION ALL ', preg_replace('#^(.*?)$#', 'SELECT \1', $sql_subary));
392
                                                break;
393
394
                                        default:
395
                                                foreach ($sql_subary as $sql)
396
                                                {
397
                                                        $sql = "INSERT INTO $table ($id_field, forum_id, auth_option_id, auth_setting) VALUES ($sql)";
398
                                                        $result = $db->sql_query($sql);
399
                                                        $sql = '';
400
                                                }
401
                                }
402
403
                                if ($sql != '')
404
                                {
405
                                        $sql = "INSERT INTO $table ($id_field, forum_id, auth_option_id, auth_setting) $sql";
406
                                        $result = $db->sql_query($sql);
407
                                }
408
                                break;
409
410
                        case 'update':
411
                        case 'delete':
412
                                foreach ($sql_subary as $sql)
413
                                {
414
                                        $result = $db->sql_query($sql);
415
                                        $sql = '';
416
                                }
417
                                break;
418
                }
419
                unset($sql_ary[$sql_type]);
420
        }
421
        unset($sql_ary);
422
423
}
424
425
?>