aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/includes/auth.php
blob: 8e75eada671c3738f9942d27ef88ee8122a190fb (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
412
413
414
<?php
/***************************************************************************  
 *                                 auth.php
 *                            -------------------                         
 *   begin                : Saturday, Feb 13, 2001 
 *   copyright            : (C) 2001 The phpBB Group        
 *   email                : support@phpbb.com                           
 *                                                          
 *   $Id$                                                           
 *                                                            
 * 
 ***************************************************************************/ 


/***************************************************************************  
 *                                                     
 *   This program is free software; you can redistribute it and/or modify    
 *   it under the terms of the GNU General Public License as published by   
 *   the Free Software Foundation; either version 2 of the License, or  
 *   (at your option) any later version.                      
 *                                                          
 * 
 ***************************************************************************/ 

/*
	$type's accepted (eventually!):
	VIEW, READ, POST, REPLY, EDIT, DELETE, VOTE, VOTECREATE

	Possible options to send to auth (not all are functional yet!):

	* If you include a type then a specific lookup will
	be done and the single result returned

	* If you set type to ALL an array of all auth types
	will be returned

	* If you provide a forum_id a specific lookup on that
	forum will be done

	* If you set forum_id to LIST_ALL an array of all
	forums to which the user has access of type will be returned
	<- used for index and search? (type VIEW and READ respectively)
	
	* If you set forum_id to LIST_ALL and type to ALL a 
	multidimensional array containing the auth permissions
	for all types and all forums for that user is returned

	* If you set $userdata to ALL, then the permissions of all
	users listed in the auth_access table will be returned for 
	the given type and forum_id <- use to check for moderators?

	All results are returned as associative arrays, even
	when a single auth type is specified

*/
function auth($type, $forum_id, $userdata, $f_access = -1)
{
	global $db;

	switch($type)
	{
		case AUTH_ALL:
			$a_sql = "aa.auth_view, aa.auth_read, aa.auth_post, aa.auth_reply, aa.auth_edit, aa.auth_delete, aa.auth_votecreate, aa.auth_vote";
			$auth_fields = array("auth_view", "auth_read", "auth_post", "auth_reply", "auth_edit", "auth_delete", "auth_votecreate", "auth_vote");
			break;
		case AUTH_VIEW:
			$a_sql = "aa.auth_view";
			$auth_fields = array("auth_view");
			break;
		case AUTH_READ:
			$a_sql = "aa.auth_read";
			$auth_fields = array("auth_read");
			break;
		case AUTH_POST:
			$a_sql = "aa.auth_post";
			$auth_fields = array("auth_post");
			break;
		case AUTH_REPLY:
			$a_sql = "aa.auth_reply";
			$auth_fields = array("auth_reply");
			break;
		case AUTH_EDIT:
			$a_sql = "aa.auth_edit";
			$auth_fields = array("auth_edit");
			break;
		case AUTH_DELETE:
			$a_sql = "aa.auth_delete";
			$auth_fields = array("auth_delete");
			break;
		case AUTH_VOTECREATE:
			$a_sql = "aa.auth_votecreate";
			$auth_fields = array("auth_votecreate");
			break;
		case AUTH_VOTE:
			$a_sql = "aa.auth_vote";
			$auth_fields = array("auth_vote");
			break;
		default:
			break;
	}

	//
	// If f_access has been passed, or auth
	// is needed to return an array of forums
	// then we need to pull the auth information
	// on the given forum (or all forums)
	//
	if($f_access == -1)
	{
		$forum_match_sql = ($forum_id != AUTH_LIST_ALL) ? "WHERE aa.forum_id = $forum_id" : "";
		$sql = "SELECT aa.forum_id, $a_sql 
			FROM ".AUTH_FORUMS_TABLE." aa 
			$forum_match_sql";
		$af_result = $db->sql_query($sql);

		if(!$af_result)
		{
			error_die(QUERY_ERROR, "Failed obtaining forum access control lists");
		}
		else
		{
			if(!$db->sql_numrows($af_result))
			{
				error_die(GENERAL_ERROR, "No forum access control lists exist!");
			}
			else
			{
				$f_access = $db->sql_fetchrowset($af_result);
			}
		}
	}
	else
	{

	}

	//
	// If the user isn't logged on then
	// all we need do is check if the forum
	// has the type set to ALL, if yes then
	// they're good to go, if not then they
	// are denied access
	//
	$auth_user = array();

	if(!$userdata['session_logged_in'])
	{
		for($j = 0; $j < count($auth_fields); $j++)
		{
			$key = $auth_fields[$j];

			if($forum_id != AUTH_LIST_ALL)
			{
				$auth_user[$key] = ($f_access[$key] == AUTH_ALL) ? 1 : 0;
			}
			else
			{
				for($i = 0; $i < count($f_access); $i++)
				{
					$forum_id = $f_access[$i]['forum_id'];
					$auth_user[$forum_id][$key] = ($f_access[$i][$key] == AUTH_ALL) ? 1 : 0;
				}
			}
		}
	}
	else 
	{
		$forum_match_sql = ($forum_id != AUTH_LIST_ALL) ? "AND aa.forum_id = $forum_id" : "";
		$sql = "SELECT aa.forum_id, $a_sql, aa.auth_mod, g.group_single_user  
			FROM ".AUTH_ACCESS_TABLE." aa, " . USER_GROUP_TABLE. " ug, " . GROUPS_TABLE. " g 
			WHERE ug.user_id = ".$userdata['user_id']. " 
				AND g.group_id = ug.group_id 
				AND aa.group_id = ug.group_id 
				$forum_match_sql";
		$au_result = $db->sql_query($sql);
		if(!$au_result)
		{
			error_die(QUERY_ERROR, "Failed obtaining forum access control lists");
		}

		$num_u_access = $db->sql_numrows($au_result);
		if($num_u_access)
		{
			$u_access = $db->sql_fetchrowset($au_result);
		}

		$is_admin = ($userdata['user_level'] == ADMIN) ? 1 : 0;
		$auth_user = array();

		for($i = 0; $i < count($auth_fields); $i++)
		{
			$key = $auth_fields[$i];
			$value = ($forum_id != AUTH_LIST_ALL) ? $f_access[$key] : $f_access[$k][$key];

			if(!$num_u_access)
			{
				//
				// If no rows for this user where
				// returned then auth is only true
				// if the key has a value of ALL || REG
				//
				if($forum_id != AUTH_LIST_ALL)
				{
					$auth_user[$key] = ($value == AUTH_ALL || $value == AUTH_REG) ? 1 : 0;
				}
				else
				{
					for($k = 0; $k < count($f_access); $k++)
					{
						$f_forum_id = $f_access[$k]['forum_id'];
						$auth_user[$f_forum_id][$key] = ($value == AUTH_ALL || $value == AUTH_REG) ? 1 : 0;
					}
				}
			}
			else 
			{
				//
				// If the user is logged on and the forum type is either
				// ALL or REG then the user has access
				//
				// If the type if ACL, MOD or ADMIN then we need to see
				// if the user has specific permissions to do whatever it
				// is they want to do ... to do this we pull relevant
				// information for the user (and any groups they belong to)
				//
				// Now we compare the users access level against the forums
				// We assume here that a moderator and admin automatically
				// have access to an ACL forum, similarly we assume admins
				// meet an auth requirement of MOD
				//
				// The access level assigned to a single user automatically 
				// takes precedence over any levels granted by that user being
				// a member of a multi-user usergroup, eg. a user who is banned
				// from a forum won't gain access to it even if they belong to
				// a group which has access (and vice versa). This check is
				// done via the single_user check
				//
				// PS : I appologise for the fantastically clear and hugely
				// readable code here ;) Simple gist is, if this row of
				// auth_access doesn't represent a single user then OR the
				// contents of relevant auth_access levels against the current
				// level (allows maximum group privileges to be assigned). If
				// the row does represent a single user then forget any previous
				// group results and instead set the auth to whatever the OR'd
				// contents of the access levels are.
				//

				switch($value)
				{
					case AUTH_ALL:
						if($forum_id != AUTH_LIST_ALL)
						{
							$auth_user[$key] = 1;
						}
						else
						{
							for($k = 0; $k < count($f_access); $k++)
							{
								$f_forum_id = $f_access[$k]['forum_id'];
								$auth_user[$f_forum_id][$key] = 1;
							}
						}
						break;

					case AUTH_REG:
						if($forum_id != AUTH_LIST_ALL)
						{
							$auth_user[$key] = 1;
						}
						else
						{
							for($k = 0; $k < count($f_access); $k++)
							{
								$f_forum_id = $f_access[$k]['forum_id'];
								$auth_user[$f_forum_id][$key] = 1;
							}
						}
						break;

					case AUTH_ACL:
						if($forum_id != AUTH_LIST_ALL)
						{
							$auth_user[$key] = auth_check_user(AUTH_ACL, $key, $u_access, $is_admin);
						}
						else
						{
							for($k = 0; $k < count($f_access); $k++)
							{
								$f_forum_id = $f_access[$k]['forum_id'];
								$auth_user[$f_forum_id][$key] = auth_check_user(AUTH_ACL, $key, $u_access, $is_admin);
							}
						}
						break;
		
					case AUTH_MOD:
						if($forum_id != AUTH_LIST_ALL)
						{
							$auth_user[$key] = auth_check_user(AUTH_ACL, $key, $u_access, $is_admin);
						}
						else
						{
							for($k = 0; $k < count($f_access); $k++)
							{
								$f_forum_id = $f_access[$k]['forum_id'];
								$auth_user[$f_forum_id][$key] = auth_check_user(AUTH_ACL, $key, $u_access, $is_admin);
							}
						}
						break;
	
					case AUTH_ADMIN:
						if($forum_id != AUTH_LIST_ALL)
						{
							$auth_user[$key] = $is_admin;
						}
						else
						{
							for($k = 0; $k < count($f_access); $k++)
							{
								$f_forum_id = $f_access[$k]['forum_id'];
								$auth_user[$f_forum_id][$key] = $is_admin;
							}
						}
						break;

					default:
						if($forum_id != AUTH_LIST_ALL)
						{
							$auth_user[$key] = 0;
						}
						else
						{
							for($k = 0; $k < count($f_access); $k++)
							{
								$f_forum_id = $f_access[$k]['forum_id'];
								$auth_user[$f_forum_id][$key] = 0;
							}
						}
						break;
				}
			}
		}
		//
		// Is user a moderator?
		//
		if($forum_id != AUTH_LIST_ALL)
		{
			$auth_user['auth_mod'] = auth_check_user(AUTH_MOD, 'auth_mod', $u_access, $is_admin);
		}
		else
		{
			for($k = 0; $k < count($f_access); $k++)
			{
				$f_forum_id = $f_access[$k]['forum_id'];
				$auth_user[$f_forum_id]['auth_mod'] = auth_check_user(AUTH_MOD, 'auth_mod', $u_access, $is_admin);
			}
		}

		//
		// Is user an admin (this is
		// really redundant at this time)
		//
		if($forum_id != AUTH_LIST_ALL)
		{
			$auth_user['auth_admin'] = $is_admin;
		}
		else
		{
			for($k = 0; $k < count($f_access); $k++)
			{
				$f_forum_id = $f_access[$k]['forum_id'];
				$auth_user[$f_forum_id]['auth_admin'] = $is_admin;
			}
		}
	}

	return $auth_user;

}

function auth_check_user($type, $key, $u_access, $is_admin)
{

	$single_user = 0;
	$auth_user = 0;

	for($j = 0; $j < count($u_access); $j++)
	{
		if(!$single_user)
		{
			$single_user = $u_access[$j]['group_single_user'];
			
			$result = 0;
			switch($type)
			{
				case AUTH_ACL:
					$result = $u_access[$j][$key];

				case AUTH_MOD:
					$result = $result || $u_access[$j]['auth_mod'];

				case AUTH_ADMIN:
					$result = $result || $is_admin;
					break;
			}

			$auth_user = (!$single_user) ? ( $auth_user || $result ) : $result;

		}
	}
	
	return $auth_user;
}

?>