2 /***************************************************************************
5 * begin : Saturday, Feb 13, 2001
6 * copyright : (C) 2001 The phpBB Group
7 * email : support@phpbb.com
9 * $Id: auth.php,v 1.37.2.7 2006/03/06 17:28:51 grahamje Exp $
12 ***************************************************************************/
14 /***************************************************************************
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 ***************************************************************************/
24 $type's accepted (pre-pend with AUTH_):
25 VIEW, READ, POST, REPLY, EDIT, DELETE, STICKY, ANNOUNCE, VOTE, POLLCREATE
27 Possible options ($type/forum_id combinations):
29 * If you include a type and forum_id then a specific lookup will be done and
30 the single result returned
32 * If you set type to AUTH_ALL and specify a forum_id an array of all auth types
35 * If you provide a forum_id a specific lookup on that forum will be done
37 * If you set forum_id to AUTH_LIST_ALL and specify a type an array listing the
38 results for all forums will be returned
40 * If you set forum_id to AUTH_LIST_ALL and type to AUTH_ALL a multidimensional
41 array containing the auth permissions for all types and all forums for that
44 All results are returned as associative arrays, even when a single auth type is
47 If available you can send an array (either one or two dimensional) containing the
48 forum auth levels, this will prevent the auth function having to do its own
51 function auth($type, $forum_id, $userdata, $f_access = '')
58 $a_sql = 'a.auth_view, a.auth_read, a.auth_post, a.auth_reply, a.auth_edit, a.auth_delete, a.auth_sticky, a.auth_announce, a.auth_vote, a.auth_pollcreate';
59 $auth_fields = array('auth_view', 'auth_read', 'auth_post', 'auth_reply', 'auth_edit', 'auth_delete', 'auth_sticky', 'auth_announce', 'auth_vote', 'auth_pollcreate');
63 $a_sql = 'a.auth_view';
64 $auth_fields = array('auth_view');
68 $a_sql = 'a.auth_read';
69 $auth_fields = array('auth_read');
72 $a_sql = 'a.auth_post';
73 $auth_fields = array('auth_post');
76 $a_sql = 'a.auth_reply';
77 $auth_fields = array('auth_reply');
80 $a_sql = 'a.auth_edit';
81 $auth_fields = array('auth_edit');
84 $a_sql = 'a.auth_delete';
85 $auth_fields = array('auth_delete');
89 $a_sql = 'a.auth_announce';
90 $auth_fields = array('auth_announce');
93 $a_sql = 'a.auth_sticky';
94 $auth_fields = array('auth_sticky');
98 $a_sql = 'a.auth_pollcreate';
99 $auth_fields = array('auth_pollcreate');
102 $a_sql = 'a.auth_vote';
103 $auth_fields = array('auth_vote');
113 // If f_access has been passed, or auth is needed to return an array of forums
114 // then we need to pull the auth information on the given forum (or all forums)
116 if ( empty($f_access) )
118 $forum_match_sql = ( $forum_id != AUTH_LIST_ALL ) ? "WHERE a.forum_id = $forum_id" : '';
120 $sql = "SELECT a.forum_id, $a_sql
121 FROM " . FORUMS_TABLE . " a
123 if ( !($result = $db->sql_query($sql)) )
125 message_die(GENERAL_ERROR, 'Failed obtaining forum access control lists', '', __LINE__, __FILE__, $sql);
128 $sql_fetchrow = ( $forum_id != AUTH_LIST_ALL ) ? 'sql_fetchrow' : 'sql_fetchrowset';
130 if ( !($f_access = $db->$sql_fetchrow($result)) )
132 $db->sql_freeresult($result);
135 $db->sql_freeresult($result);
139 // If the user isn't logged on then all we need do is check if the forum
140 // has the type set to ALL, if yes they are good to go, if not then they
144 if ( $userdata['session_logged_in'] )
146 $forum_match_sql = ( $forum_id != AUTH_LIST_ALL ) ? "AND a.forum_id = $forum_id" : '';
148 $sql = "SELECT a.forum_id, $a_sql, a.auth_mod
149 FROM " . AUTH_ACCESS_TABLE . " a, " . USER_GROUP_TABLE . " ug
150 WHERE ug.user_id = ".$userdata['user_id']. "
151 AND ug.user_pending = 0
152 AND a.group_id = ug.group_id
154 if ( !($result = $db->sql_query($sql)) )
156 message_die(GENERAL_ERROR, 'Failed obtaining forum access control lists', '', __LINE__, __FILE__, $sql);
159 if ( $row = $db->sql_fetchrow($result) )
163 if ( $forum_id != AUTH_LIST_ALL)
169 $u_access[$row['forum_id']][] = $row;
172 while( $row = $db->sql_fetchrow($result) );
174 $db->sql_freeresult($result);
177 $is_admin = ( $userdata['user_level'] == ADMIN && $userdata['session_logged_in'] ) ? TRUE : 0;
179 $auth_user = array();
180 for($i = 0; $i < count($auth_fields); $i++)
182 $key = $auth_fields[$i];
185 // If the user is logged on and the forum type is either ALL or REG then the user has access
187 // If the type if ACL, MOD or ADMIN then we need to see if the user has specific permissions
188 // to do whatever it is they want to do ... to do this we pull relevant information for the
189 // user (and any groups they belong to)
191 // Now we compare the users access level against the forums. We assume here that a moderator
192 // and admin automatically have access to an ACL forum, similarly we assume admins meet an
193 // auth requirement of MOD
195 if ( $forum_id != AUTH_LIST_ALL )
197 $value = $f_access[$key];
202 $auth_user[$key] = TRUE;
203 $auth_user[$key . '_type'] = $lang['Auth_Anonymous_Users'];
207 $auth_user[$key] = ( $userdata['session_logged_in'] ) ? TRUE : 0;
208 $auth_user[$key . '_type'] = $lang['Auth_Registered_Users'];
212 $auth_user[$key] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_ACL, $key, $u_access, $is_admin) : 0;
213 $auth_user[$key . '_type'] = $lang['Auth_Users_granted_access'];
217 $auth_user[$key] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access, $is_admin) : 0;
218 $auth_user[$key . '_type'] = $lang['Auth_Moderators'];
222 $auth_user[$key] = $is_admin;
223 $auth_user[$key . '_type'] = $lang['Auth_Administrators'];
227 $auth_user[$key] = 0;
233 for($k = 0; $k < count($f_access); $k++)
235 $value = $f_access[$k][$key];
236 $f_forum_id = $f_access[$k]['forum_id'];
237 $u_access[$f_forum_id] = isset($u_access[$f_forum_id]) ? $u_access[$f_forum_id] : array();
242 $auth_user[$f_forum_id][$key] = TRUE;
243 $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Anonymous_Users'];
247 $auth_user[$f_forum_id][$key] = ( $userdata['session_logged_in'] ) ? TRUE : 0;
248 $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Registered_Users'];
252 $auth_user[$f_forum_id][$key] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_ACL, $key, $u_access[$f_forum_id], $is_admin) : 0;
253 $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Users_granted_access'];
257 $auth_user[$f_forum_id][$key] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access[$f_forum_id], $is_admin) : 0;
258 $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Moderators'];
262 $auth_user[$f_forum_id][$key] = $is_admin;
263 $auth_user[$f_forum_id][$key . '_type'] = $lang['Auth_Administrators'];
267 $auth_user[$f_forum_id][$key] = 0;
275 // Is user a moderator?
277 if ( $forum_id != AUTH_LIST_ALL )
279 $auth_user['auth_mod'] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access, $is_admin) : 0;
283 for($k = 0; $k < count($f_access); $k++)
285 $f_forum_id = $f_access[$k]['forum_id'];
286 $u_access[$f_forum_id] = isset($u_access[$f_forum_id]) ? $u_access[$f_forum_id] : array();
288 $auth_user[$f_forum_id]['auth_mod'] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access[$f_forum_id], $is_admin) : 0;
295 function auth_check_user($type, $key, $u_access, $is_admin)
299 if ( count($u_access) )
301 for($j = 0; $j < count($u_access); $j++)
307 $result = $u_access[$j][$key];
310 $result = $result || $u_access[$j]['auth_mod'];
313 $result = $result || $is_admin;
317 $auth_user = $auth_user || $result;
322 $auth_user = $is_admin;