aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/develop/add_permissions.php
blob: cb6afb94edb4cac7a4046d3e8a9c71c4aaf005a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

//
// Security message:
//
// This script is potentially dangerous.
// Remove or comment the next line (die(".... ) to enable this script.
// Do NOT FORGET to either remove this script or disable it after you have used it.
//
die("Please read the first lines of this script for instructions on how to enable it");


// This script adds missing permissions
$db = $dbhost = $dbuser = $dbpasswd = $dbport = $dbname = '';

define('IN_PHPBB', 1);
define('ANONYMOUS', 1);
$phpEx = substr(strrchr(__FILE__, '.'), 1);
$phpbb_root_path='./../';
include($phpbb_root_path . 'config.'.$phpEx);
require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.'.$phpEx);
require($phpbb_root_path . 'includes/db/' . $dbms . '.'.$phpEx);
include($phpbb_root_path . 'includes/functions.'.$phpEx);

define('ACL_NEVER', 0);
define('ACL_YES', 1);
define('ACL_NO', -1);

define('ACL_GROUPS_TABLE', $table_prefix.'acl_groups');
define('ACL_OPTIONS_TABLE', $table_prefix.'acl_options');
define('ACL_USERS_TABLE', $table_prefix.'acl_users');
define('GROUPS_TABLE', $table_prefix.'groups');
define('USERS_TABLE', $table_prefix.'users');

$cache		= new acm();
$db			= new sql_db();

// Connect to DB
$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false);

// auth => is_local, is_global
$f_permissions = array(
	'f_'		=> array(1, 0),
	'f_list'	=> array(1, 0),
	'f_read'	=> array(1, 0),
	'f_post'	=> array(1, 0),
	'f_reply'	=> array(1, 0),
	'f_edit'	=> array(1, 0),
	'f_user_lock'	=> array(1, 0),
	'f_delete'	=> array(1, 0),
	'f_bump'	=> array(1, 0),
	'f_poll'	=> array(1, 0),
	'f_vote'	=> array(1, 0),
	'f_votechg'	=> array(1, 0),
	'f_announce'=> array(1, 0),
	'f_announce_global'	=> array(1, 0),
	'f_sticky'	=> array(1, 0),
	'f_attach'	=> array(1, 0),
	'f_download'=> array(1, 0),
	'f_icons'	=> array(1, 0),
	'f_bbcode'	=> array(1, 0),
	'f_smilies'	=> array(1, 0),
	'f_img'		=> array(1, 0),
	'f_flash'	=> array(1, 0),
	'f_sigs'	=> array(1, 0),
	'f_search'	=> array(1, 0),
	'f_email'	=> array(1, 0),
	'f_print'	=> array(1, 0),
	'f_ignoreflood'	=> array(1, 0),
	'f_postcount'	=> array(1, 0),
	'f_noapprove'=> array(1, 0),
	'f_report'	=> array(1, 0),
	'f_subscribe'	=> array(1, 0),
);

$m_permissions = array(
	'm_'		=> array(1, 1),
	'm_edit'	=> array(1, 1),
	'm_delete'	=> array(1, 1),
	'm_move'	=> array(1, 1),
	'm_lock'	=> array(1, 1),
	'm_split'	=> array(1, 1),
	'm_merge'	=> array(1, 1),
	'm_approve'	=> array(1, 1),
	'm_unrate'	=> array(1, 1),
	'm_auth'	=> array(1, 1),
	'm_ip'		=> array(1, 1),
	'm_info'	=> array(1, 1),
);

$a_permissions = array(
	'a_'		=> array(0, 1),
	'a_server'	=> array(0, 1),
	'a_board'	=> array(0, 1),
	'a_clearlogs'	=> array(0, 1),
	'a_words'	=> array(0, 1),
	'a_icons'	=> array(0, 1),
	'a_bbcode'	=> array(0, 1),
	'a_attach'	=> array(0, 1),
	'a_email'	=> array(0, 1),
	'a_styles'	=> array(0, 1),
	'a_user'	=> array(0, 1),
	'a_useradd'	=> array(0, 1),
	'a_userdel'	=> array(0, 1),
	'a_ranks'	=> array(0, 1),
	'a_ban'		=> array(0, 1),
	'a_names'	=> array(0, 1),
	'a_group'	=> array(0, 1),
	'a_groupadd'=> array(0, 1),
	'a_groupdel'=> array(0, 1),
	'a_forum'	=> array(0, 1),
	'a_forumadd'=> array(0, 1),
	'a_forumdel'=> array(0, 1),
	'a_prune'	=> array(0, 1),
	'a_auth'	=> array(0, 1),
	'a_authmods'=> array(0, 1),
	'a_authadmins'	=> array(0, 1),
	'a_authusers'	=> array(0, 1),
	'a_authgroups'	=> array(0, 1),
	'a_authdeps'=> array(0, 1),
	'a_backup'	=> array(0, 1),
	'a_restore'	=> array(0, 1),
	'a_search'	=> array(0, 1),
	'a_events'	=> array(0, 1),
	'a_cron'	=> array(0, 1),
);

$u_permissions = array(
	'u_'			=> array(0, 1),
	'u_sendemail'	=> array(0, 1),
	'u_readpm'		=> array(0, 1),
	'u_sendpm'		=> array(0, 1),
	'u_sendim'		=> array(0, 1),
	'u_hideonline'	=> array(0, 1),
	'u_viewonline'	=> array(0, 1),
	'u_viewprofile'	=> array(0, 1),
	'u_chgavatar'	=> array(0, 1),
	'u_chggrp'		=> array(0, 1),
	'u_chgemail'	=> array(0, 1),
	'u_chgname'		=> array(0, 1),
	'u_chgpasswd'	=> array(0, 1),
	'u_chgcensors'	=> array(0, 1),
	'u_search'		=> array(0, 1),
	'u_savedrafts'	=> array(0, 1),
	'u_download'	=> array(0, 1),
	'u_attach'		=> array(0, 1),
	'u_sig'			=> array(0, 1),
	'u_emoji'		=> array(0, 1),
	'u_pm_attach'	=> array(0, 1),
	'u_pm_bbcode'	=> array(0, 1),
	'u_pm_smilies'	=> array(0, 1),
	'u_pm_download'	=> array(0, 1),
	'u_pm_edit'		=> array(0, 1),
	'u_pm_printpm'	=> array(0, 1),
	'u_pm_emailpm'	=> array(0, 1),
	'u_pm_forward'	=> array(0, 1),
	'u_pm_delete'	=> array(0, 1),
	'u_pm_img'		=> array(0, 1),
	'u_pm_flash'	=> array(0, 1),
);

echo "<p><b>Determining existing permissions</b></p>\n";

$sql = 'SELECT auth_option_id, auth_option FROM ' . ACL_OPTIONS_TABLE;
$result = $db->sql_query($sql);

$remove_auth_options = array();
while ($row = $db->sql_fetchrow($result))
{
	if (!in_array($row['auth_option'], array_keys(${substr($row['auth_option'], 0, 2) . 'permissions'})))
	{
		$remove_auth_options[$row['auth_option']] = $row['auth_option_id'];
	}
	unset(${substr($row['auth_option'], 0, 2) . 'permissions'}[$row['auth_option']]);
}
$db->sql_freeresult($result);

if (count($remove_auth_options))
{
	$db->sql_query('DELETE FROM ' . ACL_USERS_TABLE . ' WHERE auth_option_id IN (' . implode(', ', $remove_auth_options) . ')');
	$db->sql_query('DELETE FROM ' . ACL_GROUPS_TABLE . ' WHERE auth_option_id IN (' . implode(', ', $remove_auth_options) . ')');
	$db->sql_query('DELETE FROM ' . ACL_OPTIONS_TABLE . ' WHERE auth_option_id IN (' . implode(', ', $remove_auth_options) . ')');

	echo '<p><b>Removed the following auth options... [<i>' . implode(', ', array_keys($remove_auth_options)) . "</i>]</b></p>\n\n";
}

$prefixes = array('f_', 'a_', 'm_', 'u_');

foreach ($prefixes as $prefix)
{
	$var = $prefix . 'permissions';
	if (count(${$var}))
	{
		foreach (${$var} as $auth_option => $l_ary)
		{
			$sql_ary = array(
				'auth_option'	=> $auth_option,
				'is_local'		=> $l_ary[0],
				'is_global'		=> $l_ary[1]
			);

			$db->sql_query('INSERT INTO ' . ACL_OPTIONS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));

			echo "<p><b>Adding $auth_option...</b></p>\n";

			mass_auth('group', 0, 'guests', $auth_option, ACL_NEVER);
			mass_auth('group', 0, 'inactive', $auth_option, ACL_NEVER);
			mass_auth('group', 0, 'inactive_coppa', $auth_option, ACL_NEVER);
			mass_auth('group', 0, 'registered_coppa', $auth_option, ACL_NEVER);
			mass_auth('group', 0, 'registered', $auth_option, (($prefix != 'm_' && $prefix != 'a_') ? ACL_YES : ACL_NEVER));
			mass_auth('group', 0, 'global_moderators', $auth_option, (($prefix != 'a_') ? ACL_YES : ACL_NEVER));
			mass_auth('group', 0, 'administrators', $auth_option, ACL_YES);
			mass_auth('group', 0, 'bots', $auth_option, (($prefix != 'm_' && $prefix != 'a_') ? ACL_YES : ACL_NEVER));
		}
	}
}

$sql = 'UPDATE ' . USERS_TABLE . " SET user_permissions = ''";
$db->sql_query($sql);

$cache->destroy('_acl_options');

echo "<p><b>Done</b></p>\n";

/*
	$ug_type = user|group
	$forum_id = forum ids (array|int|0) -> 0 == all forums
	$ug_id = [int] user_id|group_id : [string] usergroup name
	$acl_list = [string] acl entry : [array] acl entries
	$setting = ACL_YES|ACL_NEVER|ACL_NO
*/
function mass_auth($ug_type, $forum_id, $ug_id, $acl_list, $setting)
{
	global $db;
	static $acl_option_ids, $group_ids;

	if ($ug_type == 'group' && is_string($ug_id))
	{
		if (!isset($group_ids[$ug_id]))
		{
			$sql = 'SELECT group_id FROM ' . GROUPS_TABLE . " 
				WHERE group_name = '" . strtoupper($ug_id) . "'";
			$result = $db->sql_query_limit($sql, 1);
			$id = (int) $db->sql_fetchfield('group_id', 0, $result);
			$db->sql_freeresult($result);

			if (!$id)
			{
				return;
			}

			$group_ids[$ug_id] = $id;
		}

		$ug_id = (int) $group_ids[$ug_id];
	}

	// Build correct parameters
	$auth = array();

	if (!is_array($acl_list))
	{
		$auth = array($acl_list => $setting);
	}
	else
	{
		foreach ($acl_list as $auth_option)
		{
			$auth[$auth_option] = $setting;
		}
	}
	unset($acl_list);

	if (!is_array($forum_id))
	{
		$forum_id = array($forum_id);
	}

	// Set any flags as required
	foreach ($auth as $auth_option => $acl_setting)
	{
		$flag = substr($auth_option, 0, strpos($auth_option, '_') + 1);
		if (empty($auth[$flag]))
		{
			$auth[$flag] = $acl_setting;
		}
	}

	if (!is_array($acl_option_ids) || empty($acl_option_ids))
	{
		$sql = 'SELECT auth_option_id, auth_option
			FROM ' . ACL_OPTIONS_TABLE;
		$result = $db->sql_query($sql);

		while ($row = $db->sql_fetchrow($result))
		{
			$acl_option_ids[$row['auth_option']] = $row['auth_option_id'];
		}
		$db->sql_freeresult($result);
	}

	$sql_forum = 'AND a.forum_id IN (' . implode(', ', array_map('intval', $forum_id)) . ')';

	$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";
	$result = $db->sql_query($sql);

	$cur_auth = array();
	while ($row = $db->sql_fetchrow($result))
	{
		$cur_auth[$row['forum_id']][$row['auth_option_id']] = $row['auth_setting'];
	}
	$db->sql_freeresult($result);

	$table = ($ug_type == 'user') ? ACL_USERS_TABLE : ACL_GROUPS_TABLE;
	$id_field  = $ug_type . '_id';

	$sql_ary = array();
	foreach ($forum_id as $forum)
	{
		foreach ($auth as $auth_option => $setting)
		{
			$auth_option_id = $acl_option_ids[$auth_option];

			if (!$auth_option_id)
			{
				continue;
			}

			switch ($setting)
			{
				case ACL_NO:
					if (isset($cur_auth[$forum][$auth_option_id]))
					{
						$sql_ary['delete'][] = "DELETE FROM $table 
							WHERE forum_id = $forum
								AND auth_option_id = $auth_option_id
								AND $id_field = $ug_id";
					}
					break;

				default:
					if (!isset($cur_auth[$forum][$auth_option_id]))
					{
						$sql_ary['insert'][] = "$ug_id, $forum, $auth_option_id, $setting";
					}
					else if ($cur_auth[$forum][$auth_option_id] != $setting)
					{
						$sql_ary['update'][] = "UPDATE " . $table . " 
							SET auth_setting = $setting 
							WHERE $id_field = $ug_id 
								AND forum_id = $forum 
								AND auth_option_id = $auth_option_id";
					}
			}
		}
	}
	unset($cur_auth);

	$sql = '';
	foreach ($sql_ary as $sql_type => $sql_subary)
	{
		switch ($sql_type)
		{
			case 'insert':
				switch ($db->get_sql_layer())
				{
					case 'sqlite3':
						$sql = implode(' UNION ALL ', preg_replace('#^(.*?)$#', 'SELECT \1', $sql_subary));
						break;

					default:
						foreach ($sql_subary as $sql)
						{
							$sql = "INSERT INTO $table ($id_field, forum_id, auth_option_id, auth_setting) VALUES ($sql)";
							$db->sql_query($sql);
							$sql = '';
						}
				}

				if ($sql != '')
				{
					$sql = "INSERT INTO $table ($id_field, forum_id, auth_option_id, auth_setting) $sql";
					$db->sql_query($sql);
				}
				break;

			case 'update':
			case 'delete':
				foreach ($sql_subary as $sql)
				{
					$db->sql_query($sql);
					$sql = '';
				}
				break;
		}
		unset($sql_ary[$sql_type]);
	}
	unset($sql_ary);

}