]> git.vanrenterghem.biz Git - www.vanrenterghem.biz.git/blob - phpBB2_old/admin/admin_ug_auth.php
update brief bio
[www.vanrenterghem.biz.git] / phpBB2_old / admin / admin_ug_auth.php
1 <?php
2 /***************************************************************************
3  *                            admin_ug_auth.php
4  *                            -------------------
5  *   begin                : Saturday, Feb 13, 2001
6  *   copyright            : (C) 2001 The phpBB Group
7  *   email                : support@phpbb.com
8  *
9  *   $Id: admin_ug_auth.php,v 1.13.2.5 2004/03/25 15:57:20 acydburn Exp $
10  *
11  *
12  ***************************************************************************/
14 /***************************************************************************
15  *
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.
20  *
21  ***************************************************************************/
23 define('IN_PHPBB', 1);
25 if( !empty($setmodules) )
26 {
27         $filename = basename(__FILE__);
28         $module['Users']['Permissions'] = $filename . "?mode=user";
29         $module['Groups']['Permissions'] = $filename . "?mode=group";
31         return;
32 }
34 //
35 // Load default header
36 //
37 $no_page_header = TRUE;
39 $phpbb_root_path = "./../";
40 require($phpbb_root_path . 'extension.inc');
41 require('./pagestart.' . $phpEx);
43 $params = array('mode' => 'mode', 'user_id' => POST_USERS_URL, 'group_id' => POST_GROUPS_URL, 'adv' => 'adv');
45 while( list($var, $param) = @each($params) )
46 {
47         if ( !empty($HTTP_POST_VARS[$param]) || !empty($HTTP_GET_VARS[$param]) )
48         {
49                 $$var = ( !empty($HTTP_POST_VARS[$param]) ) ? $HTTP_POST_VARS[$param] : $HTTP_GET_VARS[$param];
50         }
51         else
52         {
53                 $$var = "";
54         }
55 }
57 $user_id = intval($user_id);
58 $group_id = intval($group_id);
59 $adv = intval($adv);
60 $mode = htmlspecialchars($mode);
62 //
63 // Start program - define vars
64 //
65 $forum_auth_fields = array('auth_view', 'auth_read', 'auth_post', 'auth_reply', 'auth_edit', 'auth_delete', 'auth_sticky', 'auth_announce', 'auth_vote', 'auth_pollcreate');
67 $auth_field_match = array(
68         'auth_view' => AUTH_VIEW,
69         'auth_read' => AUTH_READ,
70         'auth_post' => AUTH_POST,
71         'auth_reply' => AUTH_REPLY,
72         'auth_edit' => AUTH_EDIT,
73         'auth_delete' => AUTH_DELETE,
74         'auth_sticky' => AUTH_STICKY,
75         'auth_announce' => AUTH_ANNOUNCE, 
76         'auth_vote' => AUTH_VOTE, 
77         'auth_pollcreate' => AUTH_POLLCREATE);
79 $field_names = array(
80         'auth_view' => $lang['View'],
81         'auth_read' => $lang['Read'],
82         'auth_post' => $lang['Post'],
83         'auth_reply' => $lang['Reply'],
84         'auth_edit' => $lang['Edit'],
85         'auth_delete' => $lang['Delete'],
86         'auth_sticky' => $lang['Sticky'],
87         'auth_announce' => $lang['Announce'], 
88         'auth_vote' => $lang['Vote'], 
89         'auth_pollcreate' => $lang['Pollcreate']);
91 // ---------------
92 // Start Functions
93 //
94 function check_auth($type, $key, $u_access, $is_admin)
95 {
96         $auth_user = 0;
98         if( count($u_access) )
99         {
100                 for($j = 0; $j < count($u_access); $j++)
101                 {
102                         $result = 0;
103                         switch($type)
104                         {
105                                 case AUTH_ACL:
106                                         $result = $u_access[$j][$key];
108                                 case AUTH_MOD:
109                                         $result = $result || $u_access[$j]['auth_mod'];
111                                 case AUTH_ADMIN:
112                                         $result = $result || $is_admin;
113                                         break;
114                         }
116                         $auth_user = $auth_user || $result;
117                 }
118         }
119         else
120         {
121                 $auth_user = $is_admin;
122         }
124         return $auth_user;
126 //
127 // End Functions
128 // -------------
130 if ( isset($HTTP_POST_VARS['submit']) && ( ( $mode == 'user' && $user_id ) || ( $mode == 'group' && $group_id ) ) )
132         $user_level = '';
133         if ( $mode == 'user' )
134         {
135                 //
136                 // Get group_id for this user_id
137                 //
138                 $sql = "SELECT g.group_id, u.user_level
139                         FROM " . USER_GROUP_TABLE . " ug, " . USERS_TABLE . " u, " . GROUPS_TABLE . " g
140                         WHERE u.user_id = $user_id 
141                                 AND ug.user_id = u.user_id 
142                                 AND g.group_id = ug.group_id 
143                                 AND g.group_single_user = " . TRUE;
144                 if ( !($result = $db->sql_query($sql)) )
145                 {
146                         message_die(GENERAL_ERROR, 'Could not select info from user/user_group table', '', __LINE__, __FILE__, $sql);
147                 }
149                 $row = $db->sql_fetchrow($result);
151                 $group_id = $row['group_id'];
152                 $user_level = $row['user_level'];
154                 $db->sql_freeresult($result);
155         }
157         //
158         // Carry out requests
159         //
160         if ( $mode == 'user' && $HTTP_POST_VARS['userlevel'] == 'admin' && $user_level != ADMIN )
161         {
162                 //
163                 // Make user an admin (if already user)
164                 //
165                 if ( $userdata['user_id'] != $user_id )
166                 {
167                         $sql = "UPDATE " . USERS_TABLE . "
168                                 SET user_level = " . ADMIN . "
169                                 WHERE user_id = $user_id";
170                         if ( !($result = $db->sql_query($sql)) )
171                         {
172                                 message_die(GENERAL_ERROR, 'Could not update user level', '', __LINE__, __FILE__, $sql);
173                         }
175                         $sql = "DELETE FROM " . AUTH_ACCESS_TABLE . "
176                                 WHERE group_id = $group_id 
177                                         AND auth_mod = 0";
178                         if ( !($result = $db->sql_query($sql)) )
179                         {
180                                 message_die(GENERAL_ERROR, "Couldn't delete auth access info", "", __LINE__, __FILE__, $sql);
181                         }
183                         //
184                         // Delete any entries in auth_access, they are not required if user is becoming an
185                         // admin
186                         //
187                         $sql = "UPDATE " . AUTH_ACCESS_TABLE . "
188                                 SET auth_view = 0, auth_read = 0, auth_post = 0, auth_reply = 0, auth_edit = 0, auth_delete = 0, auth_sticky = 0, auth_announce = 0
189                                 WHERE group_id = $group_id"; 
190                         if ( !($result = $db->sql_query($sql)) )
191                         {
192                                 message_die(GENERAL_ERROR, "Couldn't update auth access", "", __LINE__, __FILE__, $sql);
193                         }
194                 }
196                 $message = $lang['Auth_updated'] . '<br /><br />' . sprintf($lang['Click_return_userauth'], '<a href="' . append_sid("admin_ug_auth.$phpEx?mode=$mode") . '">', '</a>') . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="' . append_sid("index.$phpEx?pane=right") . '">', '</a>');
197                 message_die(GENERAL_MESSAGE, $message);
198         }
199         else
200         {
201                 if ( $mode == 'user' && $HTTP_POST_VARS['userlevel'] == 'user' && $user_level == ADMIN )
202                 {
203                         //
204                         // Make admin a user (if already admin) ... ignore if you're trying
205                         // to change yourself from an admin to user!
206                         //
207                         if ( $userdata['user_id'] != $user_id )
208                         {
209                                 $sql = "UPDATE " . AUTH_ACCESS_TABLE . "
210                                         SET auth_view = 0, auth_read = 0, auth_post = 0, auth_reply = 0, auth_edit = 0, auth_delete = 0, auth_sticky = 0, auth_announce = 0
211                                         WHERE group_id = $group_id";
212                                 if ( !($result = $db->sql_query($sql)) )
213                                 {
214                                         message_die(GENERAL_ERROR, 'Could not update auth access', '', __LINE__, __FILE__, $sql);
215                                 }
217                                 //
218                                 // Update users level, reset to USER
219                                 //
220                                 $sql = "UPDATE " . USERS_TABLE . "
221                                         SET user_level = " . USER . "
222                                         WHERE user_id = $user_id";
223                                 if ( !($result = $db->sql_query($sql)) )
224                                 {
225                                         message_die(GENERAL_ERROR, 'Could not update user level', '', __LINE__, __FILE__, $sql);
226                                 }
227                         }
229                         $message = $lang['Auth_updated'] . '<br /><br />' . sprintf($lang['Click_return_userauth'], '<a href="' . append_sid("admin_ug_auth.$phpEx?mode=$mode") . '">', '</a>') . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="' . append_sid("index.$phpEx?pane=right") . '">', '</a>');
230                 }
231                 else
232                 {
233         
234                         $change_mod_list = ( isset($HTTP_POST_VARS['moderator']) ) ? $HTTP_POST_VARS['moderator'] : false;
236                         if ( empty($adv) )
237                         {
238                                 $change_acl_list = ( isset($HTTP_POST_VARS['private']) ) ? $HTTP_POST_VARS['private'] : false;
239                         }
240                         else
241                         {
242                                 $change_acl_list = array();
243                                 for($j = 0; $j < count($forum_auth_fields); $j++)
244                                 {
245                                         $auth_field = $forum_auth_fields[$j];
247                                         while( list($forum_id, $value) = @each($HTTP_POST_VARS['private_' . $auth_field]) )
248                                         {
249                                                 $change_acl_list[$forum_id][$auth_field] = $value;
250                                         }
251                                 }
252                         }
254                         $sql = "SELECT * 
255                                 FROM " . FORUMS_TABLE . " f
256                                 ORDER BY forum_order";
257                         if ( !($result = $db->sql_query($sql)) )
258                         {
259                                 message_die(GENERAL_ERROR, "Couldn't obtain forum information", "", __LINE__, __FILE__, $sql);
260                         }
262                         $forum_access = array();
263                         while( $row = $db->sql_fetchrow($result) )
264                         {
265                                 $forum_access[] = $row;
266                         }
267                         $db->sql_freeresult($result);
269                         $sql = ( $mode == 'user' ) ? "SELECT aa.* FROM " . AUTH_ACCESS_TABLE . " aa, " . USER_GROUP_TABLE . " ug, " . GROUPS_TABLE. " g WHERE ug.user_id = $user_id AND g.group_id = ug.group_id AND aa.group_id = ug.group_id AND g.group_single_user = " . TRUE : "SELECT * FROM " . AUTH_ACCESS_TABLE . " WHERE group_id = $group_id";
270                         if ( !($result = $db->sql_query($sql)) )
271                         {
272                                 message_die(GENERAL_ERROR, "Couldn't obtain user/group permissions", "", __LINE__, __FILE__, $sql);
273                         }
275                         $auth_access = array();
276                         while( $row = $db->sql_fetchrow($result) )
277                         {
278                                 $auth_access[$row['forum_id']] = $row;
279                         }
280                         $db->sql_freeresult($result);
282                         $forum_auth_action = array();
283                         $update_acl_status = array();
284                         $update_mod_status = array();
286                         for($i = 0; $i < count($forum_access); $i++)
287                         {
288                                 $forum_id = $forum_access[$i]['forum_id'];
290                                 if ( 
291                                         ( isset($auth_access[$forum_id]['auth_mod']) && $change_mod_list[$forum_id]['auth_mod'] != $auth_access[$forum_id]['auth_mod'] ) || 
292                                         ( !isset($auth_access[$forum_id]['auth_mod']) && !empty($change_mod_list[$forum_id]['auth_mod']) ) 
293                                 )
294                                 {
295                                         $update_mod_status[$forum_id] = $change_mod_list[$forum_id]['auth_mod'];
297                                         if ( !$update_mod_status[$forum_id] )
298                                         {
299                                                 $forum_auth_action[$forum_id] = 'delete';
300                                         }
301                                         else if ( !isset($auth_access[$forum_id]['auth_mod']) )
302                                         {
303                                                 $forum_auth_action[$forum_id] = 'insert';
304                                         }
305                                         else
306                                         {
307                                                 $forum_auth_action[$forum_id] = 'update';
308                                         }
309                                 }
311                                 for($j = 0; $j < count($forum_auth_fields); $j++)
312                                 {
313                                         $auth_field = $forum_auth_fields[$j];
315                                         if( $forum_access[$i][$auth_field] == AUTH_ACL && isset($change_acl_list[$forum_id][$auth_field]) )
316                                         {
317                                                 if ( ( empty($auth_access[$forum_id]['auth_mod']) && 
318                                                         ( isset($auth_access[$forum_id][$auth_field]) && $change_acl_list[$forum_id][$auth_field] != $auth_access[$forum_id][$auth_field] ) || 
319                                                         ( !isset($auth_access[$forum_id][$auth_field]) && !empty($change_acl_list[$forum_id][$auth_field]) ) ) ||
320                                                         !empty($update_mod_status[$forum_id])
321                                                 )
322                                                 {
323                                                         $update_acl_status[$forum_id][$auth_field] = ( !empty($update_mod_status[$forum_id]) ) ? 0 :  $change_acl_list[$forum_id][$auth_field];
325                                                         if ( isset($auth_access[$forum_id][$auth_field]) && empty($update_acl_status[$forum_id][$auth_field]) && $forum_auth_action[$forum_id] != 'insert' && $forum_auth_action[$forum_id] != 'update' )
326                                                         {
327                                                                 $forum_auth_action[$forum_id] = 'delete';
328                                                         }
329                                                         else if ( !isset($auth_access[$forum_id][$auth_field]) && !( $forum_auth_action[$forum_id] == 'delete' && empty($update_acl_status[$forum_id][$auth_field]) ) )
330                                                         {
331                                                                 $forum_auth_action[$forum_id] = 'insert';
332                                                         }
333                                                         else if ( isset($auth_access[$forum_id][$auth_field]) && !empty($update_acl_status[$forum_id][$auth_field]) ) 
334                                                         {
335                                                                 $forum_auth_action[$forum_id] = 'update';
336                                                         }
337                                                 }
338                                                 else if ( ( empty($auth_access[$forum_id]['auth_mod']) && 
339                                                         ( isset($auth_access[$forum_id][$auth_field]) && $change_acl_list[$forum_id][$auth_field] == $auth_access[$forum_id][$auth_field] ) ) && $forum_auth_action[$forum_id] == 'delete' )
340                                                 {
341                                                         $forum_auth_action[$forum_id] = 'update';
342                                                 }
343                                         }
344                                 }
345                         }
347                         //
348                         // Checks complete, make updates to DB
349                         //
350                         $delete_sql = '';
351                         while( list($forum_id, $action) = @each($forum_auth_action) )
352                         {
353                                 if ( $action == 'delete' )
354                                 {
355                                         $delete_sql .= ( ( $delete_sql != '' ) ? ', ' : '' ) . $forum_id;
356                                 }
357                                 else
358                                 {
359                                         if ( $action == 'insert' )
360                                         {
361                                                 $sql_field = '';
362                                                 $sql_value = '';
363                                                 while ( list($auth_type, $value) = @each($update_acl_status[$forum_id]) )
364                                                 {
365                                                         $sql_field .= ( ( $sql_field != '' ) ? ', ' : '' ) . $auth_type;
366                                                         $sql_value .= ( ( $sql_value != '' ) ? ', ' : '' ) . $value;
367                                                 }
368                                                 $sql_field .= ( ( $sql_field != '' ) ? ', ' : '' ) . 'auth_mod';
369                                                 $sql_value .= ( ( $sql_value != '' ) ? ', ' : '' ) . ( ( !isset($update_mod_status[$forum_id]) ) ? 0 : $update_mod_status[$forum_id]);
371                                                 $sql = "INSERT INTO " . AUTH_ACCESS_TABLE . " (forum_id, group_id, $sql_field) 
372                                                         VALUES ($forum_id, $group_id, $sql_value)";
373                                         }
374                                         else
375                                         {
376                                                 $sql_values = '';
377                                                 while ( list($auth_type, $value) = @each($update_acl_status[$forum_id]) )
378                                                 {
379                                                         $sql_values .= ( ( $sql_values != '' ) ? ', ' : '' ) . $auth_type . ' = ' . $value;
380                                                 }
381                                                 $sql_values .= ( ( $sql_values != '' ) ? ', ' : '' ) . 'auth_mod = ' . ( ( !isset($update_mod_status[$forum_id]) ) ? 0 : $update_mod_status[$forum_id]);
383                                                 $sql = "UPDATE " . AUTH_ACCESS_TABLE . " 
384                                                         SET $sql_values 
385                                                         WHERE group_id = $group_id 
386                                                                 AND forum_id = $forum_id";
387                                         }
388                                         if( !($result = $db->sql_query($sql)) )
389                                         {
390                                                 message_die(GENERAL_ERROR, "Couldn't update private forum permissions", "", __LINE__, __FILE__, $sql);
391                                         }
392                                 }
393                         }
395                         if ( $delete_sql != '' )
396                         {
397                                 $sql = "DELETE FROM " . AUTH_ACCESS_TABLE . " 
398                                         WHERE group_id = $group_id 
399                                                 AND forum_id IN ($delete_sql)";
400                                 if( !($result = $db->sql_query($sql)) )
401                                 {
402                                         message_die(GENERAL_ERROR, "Couldn't delete permission entries", "", __LINE__, __FILE__, $sql);
403                                 }
404                         }
406                         $l_auth_return = ( $mode == 'user' ) ? $lang['Click_return_userauth'] : $lang['Click_return_groupauth'];
407                         $message = $lang['Auth_updated'] . '<br /><br />' . sprintf($l_auth_return, '<a href="' . append_sid("admin_ug_auth.$phpEx?mode=$mode") . '">', '</a>') . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="' . append_sid("index.$phpEx?pane=right") . '">', '</a>');
408                 }
410                 //
411                 // Update user level to mod for appropriate users
412                 // 
413                 $sql = "SELECT u.user_id 
414                         FROM " . AUTH_ACCESS_TABLE . " aa, " . USER_GROUP_TABLE . " ug, " . USERS_TABLE . " u  
415                         WHERE ug.group_id = aa.group_id 
416                                 AND u.user_id = ug.user_id 
417                                 AND u.user_level NOT IN (" . MOD . ", " . ADMIN . ") 
418                         GROUP BY u.user_id 
419                         HAVING SUM(aa.auth_mod) > 0";
420                 if ( !($result = $db->sql_query($sql)) )
421                 {
422                         message_die(GENERAL_ERROR, "Couldn't obtain user/group permissions", "", __LINE__, __FILE__, $sql);
423                 }
425                 $set_mod = '';
426                 while( $row = $db->sql_fetchrow($result) )
427                 {
428                         $set_mod .= ( ( $set_mod != '' ) ? ', ' : '' ) . $row['user_id'];
429                 }
430                 $db->sql_freeresult($result);
432                 //
433                 // Update user level to user for appropriate users
434                 // 
435                 switch ( SQL_LAYER )
436                 {
437                         case 'postgresql':
438                                 $sql = "SELECT u.user_id 
439                                         FROM " . USERS_TABLE . " u, " . USER_GROUP_TABLE . " ug, " . AUTH_ACCESS_TABLE . " aa
440                                         WHERE ug.user_id = u.user_id 
441                                                 AND aa.group_id = ug.group_id 
442                                                 AND u.user_level NOT IN (" . USER . ", " . ADMIN . ")
443                                         GROUP BY u.user_id 
444                                         HAVING SUM(aa.auth_mod) = 0 
445                                         UNION (
446                                                 SELECT u.user_id  
447                                                 FROM " . USERS_TABLE . " u 
448                                                 WHERE NOT EXISTS ( 
449                                                         SELECT aa.auth_mod 
450                                                         FROM " . USER_GROUP_TABLE . " ug, " . AUTH_ACCESS_TABLE . " aa 
451                                                         WHERE ug.user_id = u.user_id 
452                                                                 AND aa.group_id = ug.group_id
453                                                 )
454                                                 AND u.user_level NOT IN (" . USER . ", " . ADMIN . ")  
455                                                 GROUP BY u.user_id
456                                         )";
457                                 break;
458                         case 'oracle':
459                                 $sql = "SELECT u.user_id 
460                                         FROM " . USERS_TABLE . " u, " . USER_GROUP_TABLE . " ug, " . AUTH_ACCESS_TABLE . " aa 
461                                         WHERE ug.user_id = u.user_id(+)
462                                                 AND aa.group_id = ug.group_id(+) 
463                                                 AND u.user_level NOT IN (" . USER . ", " . ADMIN . ")
464                                         GROUP BY u.user_id 
465                                         HAVING SUM(aa.auth_mod) = 0";
466                                 break;
467                         default:
468                                 $sql = "SELECT u.user_id 
469                                         FROM ( ( " . USERS_TABLE . " u  
470                                         LEFT JOIN " . USER_GROUP_TABLE . " ug ON ug.user_id = u.user_id ) 
471                                         LEFT JOIN " . AUTH_ACCESS_TABLE . " aa ON aa.group_id = ug.group_id ) 
472                                         WHERE u.user_level NOT IN (" . USER . ", " . ADMIN . ")
473                                         GROUP BY u.user_id 
474                                         HAVING SUM(aa.auth_mod) = 0";
475                                 break;
476                 }
477                 if ( !($result = $db->sql_query($sql)) )
478                 {
479                         message_die(GENERAL_ERROR, "Couldn't obtain user/group permissions", "", __LINE__, __FILE__, $sql);
480                 }
482                 $unset_mod = "";
483                 while( $row = $db->sql_fetchrow($result) )
484                 {
485                         $unset_mod .= ( ( $unset_mod != '' ) ? ', ' : '' ) . $row['user_id'];
486                 }
487                 $db->sql_freeresult($result);
489                 if ( $set_mod != '' )
490                 {
491                         $sql = "UPDATE " . USERS_TABLE . " 
492                                 SET user_level = " . MOD . " 
493                                 WHERE user_id IN ($set_mod)";
494                         if( !($result = $db->sql_query($sql)) )
495                         {
496                                 message_die(GENERAL_ERROR, "Couldn't update user level", "", __LINE__, __FILE__, $sql);
497                         }
498                 }
500                 if ( $unset_mod != '' )
501                 {
502                         $sql = "UPDATE " . USERS_TABLE . " 
503                                 SET user_level = " . USER . " 
504                                 WHERE user_id IN ($unset_mod)";
505                         if( !($result = $db->sql_query($sql)) )
506                         {
507                                 message_die(GENERAL_ERROR, "Couldn't update user level", "", __LINE__, __FILE__, $sql);
508                         }
509                 }
511                 message_die(GENERAL_MESSAGE, $message);
512         }
514 else if ( ( $mode == 'user' && ( isset($HTTP_POST_VARS['username']) || $user_id ) ) || ( $mode == 'group' && $group_id ) )
516         if ( isset($HTTP_POST_VARS['username']) )
517         {
518                 $this_userdata = get_userdata($HTTP_POST_VARS['username'], true);
519                 if ( !is_array($this_userdata) )
520                 {
521                         message_die(GENERAL_MESSAGE, $lang['No_such_user']);
522                 }
523                 $user_id = $this_userdata['user_id'];
524         }
526         //
527         // Front end
528         //
529         $sql = "SELECT * 
530                 FROM " . FORUMS_TABLE . " f
531                 ORDER BY forum_order";
532         if ( !($result = $db->sql_query($sql)) )
533         {
534                 message_die(GENERAL_ERROR, "Couldn't obtain forum information", "", __LINE__, __FILE__, $sql);
535         }
537         $forum_access = array();
538         while( $row = $db->sql_fetchrow($result) )
539         {
540                 $forum_access[] = $row;
541         }
542         $db->sql_freeresult($result);
544         if( empty($adv) )
545         {
546                 for($i = 0; $i < count($forum_access); $i++)
547                 {
548                         $forum_id = $forum_access[$i]['forum_id'];
550                         $forum_auth_level[$forum_id] = AUTH_ALL;
552                         for($j = 0; $j < count($forum_auth_fields); $j++)
553                         {
554                                 $forum_access[$i][$forum_auth_fields[$j]] . ' :: ';
555                                 if ( $forum_access[$i][$forum_auth_fields[$j]] == AUTH_ACL )
556                                 {
557                                         $forum_auth_level[$forum_id] = AUTH_ACL;
558                                         $forum_auth_level_fields[$forum_id][] = $forum_auth_fields[$j];
559                                 }
560                         }
561                 }
562         }
564         $sql = "SELECT u.user_id, u.username, u.user_level, g.group_id, g.group_name, g.group_single_user FROM " . USERS_TABLE . " u, " . GROUPS_TABLE . " g, " . USER_GROUP_TABLE . " ug WHERE ";
565         $sql .= ( $mode == 'user' ) ? "u.user_id = $user_id AND ug.user_id = u.user_id AND g.group_id = ug.group_id" : "g.group_id = $group_id AND ug.group_id = g.group_id AND u.user_id = ug.user_id";
566         if ( !($result = $db->sql_query($sql)) )
567         {
568                 message_die(GENERAL_ERROR, "Couldn't obtain user/group information", "", __LINE__, __FILE__, $sql);
569         }
570         $ug_info = array();
571         while( $row = $db->sql_fetchrow($result) )
572         {
573                 $ug_info[] = $row;
574         }
575         $db->sql_freeresult($result);
577         $sql = ( $mode == 'user' ) ? "SELECT aa.*, g.group_single_user FROM " . AUTH_ACCESS_TABLE . " aa, " . USER_GROUP_TABLE . " ug, " . GROUPS_TABLE. " g WHERE ug.user_id = $user_id AND g.group_id = ug.group_id AND aa.group_id = ug.group_id AND g.group_single_user = 1" : "SELECT * FROM " . AUTH_ACCESS_TABLE . " WHERE group_id = $group_id";
578         if ( !($result = $db->sql_query($sql)) )
579         {
580                 message_die(GENERAL_ERROR, "Couldn't obtain user/group permissions", "", __LINE__, __FILE__, $sql);
581         }
583         $auth_access = array();
584         $auth_access_count = array();
585         while( $row = $db->sql_fetchrow($result) )
586         {
587                 $auth_access[$row['forum_id']][] = $row; 
588                 $auth_access_count[$row['forum_id']]++;
589         }
590         $db->sql_freeresult($result);
592         $is_admin = ( $mode == 'user' ) ? ( ( $ug_info[0]['user_level'] == ADMIN && $ug_info[0]['user_id'] != ANONYMOUS ) ? 1 : 0 ) : 0;
594         for($i = 0; $i < count($forum_access); $i++)
595         {
596                 $forum_id = $forum_access[$i]['forum_id'];
598                 unset($prev_acl_setting);
599                 for($j = 0; $j < count($forum_auth_fields); $j++)
600                 {
601                         $key = $forum_auth_fields[$j];
602                         $value = $forum_access[$i][$key];
604                         switch( $value )
605                         {
606                                 case AUTH_ALL:
607                                 case AUTH_REG:
608                                         $auth_ug[$forum_id][$key] = 1;
609                                         break;
611                                 case AUTH_ACL:
612                                         $auth_ug[$forum_id][$key] = ( !empty($auth_access_count[$forum_id]) ) ? check_auth(AUTH_ACL, $key, $auth_access[$forum_id], $is_admin) : 0;
613                                         $auth_field_acl[$forum_id][$key] = $auth_ug[$forum_id][$key];
615                                         if ( isset($prev_acl_setting) )
616                                         {
617                                                 if ( $prev_acl_setting != $auth_ug[$forum_id][$key] && empty($adv) )
618                                                 {
619                                                         $adv = 1;
620                                                 }
621                                         }
623                                         $prev_acl_setting = $auth_ug[$forum_id][$key];
625                                         break;
627                                 case AUTH_MOD:
628                                         $auth_ug[$forum_id][$key] = ( !empty($auth_access_count[$forum_id]) ) ? check_auth(AUTH_MOD, $key, $auth_access[$forum_id], $is_admin) : 0;
629                                         break;
631                                 case AUTH_ADMIN:
632                                         $auth_ug[$forum_id][$key] = $is_admin;
633                                         break;
635                                 default:
636                                         $auth_ug[$forum_id][$key] = 0;
637                                         break;
638                         }
639                 }
641                 //
642                 // Is user a moderator?
643                 //
644                 $auth_ug[$forum_id]['auth_mod'] = ( !empty($auth_access_count[$forum_id]) ) ? check_auth(AUTH_MOD, 'auth_mod', $auth_access[$forum_id], 0) : 0;
645         }
646         
647         $i = 0;
648         @reset($auth_ug);
649         while( list($forum_id, $user_ary) = @each($auth_ug) )
650         {
651                 if ( empty($adv) )
652                 {
653                         if ( $forum_auth_level[$forum_id] == AUTH_ACL )
654                         {
655                                 $allowed = 1;
657                                 for($j = 0; $j < count($forum_auth_level_fields[$forum_id]); $j++)
658                                 {
659                                         if ( !$auth_ug[$forum_id][$forum_auth_level_fields[$forum_id][$j]] )
660                                         {
661                                                 $allowed = 0;
662                                         }
663                                 }
665                                 $optionlist_acl = '<select name="private[' . $forum_id . ']">';
667                                 if ( $is_admin || $user_ary['auth_mod'] )
668                                 {
669                                         $optionlist_acl .= '<option value="1">' . $lang['Allowed_Access'] . '</option>';
670                                 }
671                                 else if ( $allowed )
672                                 {
673                                         $optionlist_acl .= '<option value="1" selected="selected">' . $lang['Allowed_Access'] . '</option><option value="0">'. $lang['Disallowed_Access'] . '</option>';
674                                 }
675                                 else
676                                 {
677                                         $optionlist_acl .= '<option value="1">' . $lang['Allowed_Access'] . '</option><option value="0" selected="selected">' . $lang['Disallowed_Access'] . '</option>';
678                                 }
680                                 $optionlist_acl .= '</select>';
681                         }
682                         else
683                         {
684                                 $optionlist_acl = '&nbsp;';
685                         }
686                 }
687                 else
688                 {
689                         for($j = 0; $j < count($forum_access); $j++)
690                         {
691                                 if ( $forum_access[$j]['forum_id'] == $forum_id )
692                                 {
693                                         for($k = 0; $k < count($forum_auth_fields); $k++)
694                                         {
695                                                 $field_name = $forum_auth_fields[$k];
697                                                 if( $forum_access[$j][$field_name] == AUTH_ACL )
698                                                 {
699                                                         $optionlist_acl_adv[$forum_id][$k] = '<select name="private_' . $field_name . '[' . $forum_id . ']">';
701                                                         if( isset($auth_field_acl[$forum_id][$field_name]) && !($is_admin || $user_ary['auth_mod']) )
702                                                         {
703                                                                 if( !$auth_field_acl[$forum_id][$field_name] )
704                                                                 {
705                                                                         $optionlist_acl_adv[$forum_id][$k] .= '<option value="1">' . $lang['ON'] . '</option><option value="0" selected="selected">' . $lang['OFF'] . '</option>';
706                                                                 }
707                                                                 else
708                                                                 {
709                                                                         $optionlist_acl_adv[$forum_id][$k] .= '<option value="1" selected="selected">' . $lang['ON'] . '</option><option value="0">' . $lang['OFF'] . '</option>';
710                                                                 }
711                                                         }
712                                                         else
713                                                         {
714                                                                 if( $is_admin || $user_ary['auth_mod'] )
715                                                                 {
716                                                                         $optionlist_acl_adv[$forum_id][$k] .= '<option value="1">' . $lang['ON'] . '</option>';
717                                                                 }
718                                                                 else
719                                                                 {
720                                                                         $optionlist_acl_adv[$forum_id][$k] .= '<option value="1">' . $lang['ON'] . '</option><option value="0" selected="selected">' . $lang['OFF'] . '</option>';
721                                                                 }
722                                                         }
724                                                         $optionlist_acl_adv[$forum_id][$k] .= '</select>';
726                                                 }
727                                         }
728                                 }
729                         }
730                 }
732                 $optionlist_mod = '<select name="moderator[' . $forum_id . ']">';
733                 $optionlist_mod .= ( $user_ary['auth_mod'] ) ? '<option value="1" selected="selected">' . $lang['Is_Moderator'] . '</option><option value="0">' . $lang['Not_Moderator'] . '</option>' : '<option value="1">' . $lang['Is_Moderator'] . '</option><option value="0" selected="selected">' . $lang['Not_Moderator'] . '</option>';
734                 $optionlist_mod .= '</select>';
736                 $row_class = ( !( $i % 2 ) ) ? 'row2' : 'row1';
737                 $row_color = ( !( $i % 2 ) ) ? $theme['td_color1'] : $theme['td_color2'];
739                 $template->assign_block_vars('forums', array(
740                         'ROW_COLOR' => '#' . $row_color,
741                         'ROW_CLASS' => $row_class,
742                         'FORUM_NAME' => $forum_access[$i]['forum_name'],
744                         'U_FORUM_AUTH' => append_sid("admin_forumauth.$phpEx?f=" . $forum_access[$i]['forum_id']),
746                         'S_MOD_SELECT' => $optionlist_mod)
747                 );
749                 if( !$adv )
750                 {
751                         $template->assign_block_vars('forums.aclvalues', array(
752                                 'S_ACL_SELECT' => $optionlist_acl)
753                         );
754                 }
755                 else
756                 {
757                         for($j = 0; $j < count($forum_auth_fields); $j++)
758                         {
759                                 $template->assign_block_vars('forums.aclvalues', array(
760                                         'S_ACL_SELECT' => $optionlist_acl_adv[$forum_id][$j])
761                                 );
762                         }
763                 }
765                 $i++;
766         }
767         @reset($auth_user);
768         
769         if ( $mode == 'user' )
770         {
771                 $t_username = $ug_info[0]['username'];
772                 $s_user_type = ( $is_admin ) ? '<select name="userlevel"><option value="admin" selected="selected">' . $lang['Auth_Admin'] . '</option><option value="user">' . $lang['Auth_User'] . '</option></select>' : '<select name="userlevel"><option value="admin">' . $lang['Auth_Admin'] . '</option><option value="user" selected="selected">' . $lang['Auth_User'] . '</option></select>';
773         }
774         else
775         {
776                 $t_groupname = $ug_info[0]['group_name'];
777         }
779         $name = array();
780         $id = array();
781         for($i = 0; $i < count($ug_info); $i++)
782         {
783                 if( ( $mode == 'user' && !$ug_info[$i]['group_single_user'] ) || $mode == 'group' )
784                 {
785                         $name[] = ( $mode == 'user' ) ? $ug_info[$i]['group_name'] :  $ug_info[$i]['username'];
786                         $id[] = ( $mode == 'user' ) ? intval($ug_info[$i]['group_id']) : intval($ug_info[$i]['user_id']);
787                 }
788         }
790         if( count($name) )
791         {
792                 $t_usergroup_list = '';
793                 for($i = 0; $i < count($ug_info); $i++)
794                 {
795                         $ug = ( $mode == 'user' ) ? 'group&amp;' . POST_GROUPS_URL : 'user&amp;' . POST_USERS_URL;
797                         $t_usergroup_list .= ( ( $t_usergroup_list != '' ) ? ', ' : '' ) . '<a href="' . append_sid("admin_ug_auth.$phpEx?mode=$ug=" . $id[$i]) . '">' . $name[$i] . '</a>';
798                 }
799         }
800         else
801         {
802                 $t_usergroup_list = $lang['None'];
803         }
805         $s_column_span = 2; // Two columns always present
806         if( !$adv )
807         {
808                 $template->assign_block_vars('acltype', array(
809                         'L_UG_ACL_TYPE' => $lang['Simple_Permission'])
810                 );
811                 $s_column_span++;
812         }
813         else
814         {
815                 for($i = 0; $i < count($forum_auth_fields); $i++)
816                 {
817                         $cell_title = $field_names[$forum_auth_fields[$i]];
819                         $template->assign_block_vars('acltype', array(
820                                 'L_UG_ACL_TYPE' => $cell_title)
821                         );
822                         $s_column_span++;
823                 }
824         }
826         //
827         // Dump in the page header ...
828         //
829         include('./page_header_admin.'.$phpEx);
831         $template->set_filenames(array(
832                 "body" => 'admin/auth_ug_body.tpl')
833         );
835         $adv_switch = ( empty($adv) ) ? 1 : 0;
836         $u_ug_switch = ( $mode == 'user' ) ? POST_USERS_URL . "=" . $user_id : POST_GROUPS_URL . "=" . $group_id;
837         $switch_mode = append_sid("admin_ug_auth.$phpEx?mode=$mode&amp;" . $u_ug_switch . "&amp;adv=$adv_switch");
838         $switch_mode_text = ( empty($adv) ) ? $lang['Advanced_mode'] : $lang['Simple_mode'];
839         $u_switch_mode = '<a href="' . $switch_mode . '">' . $switch_mode_text . '</a>';
841         $s_hidden_fields = '<input type="hidden" name="mode" value="' . $mode . '" /><input type="hidden" name="adv" value="' . $adv . '" />';
842         $s_hidden_fields .= ( $mode == 'user' ) ? '<input type="hidden" name="' . POST_USERS_URL . '" value="' . $user_id . '" />' : '<input type="hidden" name="' . POST_GROUPS_URL . '" value="' . $group_id . '" />';
844         if ( $mode == 'user' )
845         {
846                 $template->assign_block_vars('switch_user_auth', array());
848                 $template->assign_vars(array(
849                         'USERNAME' => $t_username,
850                         'USER_LEVEL' => $lang['User_Level'] . " : " . $s_user_type,
851                         'USER_GROUP_MEMBERSHIPS' => $lang['Group_memberships'] . ' : ' . $t_usergroup_list)
852                 );
853         }
854         else
855         {
856                 $template->assign_block_vars("switch_group_auth", array());
858                 $template->assign_vars(array(
859                         'USERNAME' => $t_groupname,
860                         'GROUP_MEMBERSHIP' => $lang['Usergroup_members'] . ' : ' . $t_usergroup_list)
861                 );
862         }
864         $template->assign_vars(array(
865                 'L_USER_OR_GROUPNAME' => ( $mode == 'user' ) ? $lang['Username'] : $lang['Group_name'],
867                 'L_AUTH_TITLE' => ( $mode == 'user' ) ? $lang['Auth_Control_User'] : $lang['Auth_Control_Group'],
868                 'L_AUTH_EXPLAIN' => ( $mode == 'user' ) ? $lang['User_auth_explain'] : $lang['Group_auth_explain'],
869                 'L_MODERATOR_STATUS' => $lang['Moderator_status'],
870                 'L_PERMISSIONS' => $lang['Permissions'],
871                 'L_SUBMIT' => $lang['Submit'],
872                 'L_RESET' => $lang['Reset'], 
873                 'L_FORUM' => $lang['Forum'], 
875                 'U_USER_OR_GROUP' => append_sid("admin_ug_auth.$phpEx"),
876                 'U_SWITCH_MODE' => $u_switch_mode,
878                 'S_COLUMN_SPAN' => $s_column_span,
879                 'S_AUTH_ACTION' => append_sid("admin_ug_auth.$phpEx"), 
880                 'S_HIDDEN_FIELDS' => $s_hidden_fields)
881         );
883 else
885         //
886         // Select a user/group
887         //
888         include('./page_header_admin.'.$phpEx);
890         $template->set_filenames(array(
891                 'body' => ( $mode == 'user' ) ? 'admin/user_select_body.tpl' : 'admin/auth_select_body.tpl')
892         );
894         if ( $mode == 'user' )
895         {
896                 $template->assign_vars(array(
897                         'L_FIND_USERNAME' => $lang['Find_username'],
899                         'U_SEARCH_USER' => append_sid("../search.$phpEx?mode=searchuser"))
900                 );
901         }
902         else
903         {
904                 $sql = "SELECT group_id, group_name
905                         FROM " . GROUPS_TABLE . "
906                         WHERE group_single_user <> " . TRUE;
907                 if ( !($result = $db->sql_query($sql)) )
908                 {
909                         message_die(GENERAL_ERROR, "Couldn't get group list", "", __LINE__, __FILE__, $sql);
910                 }
912                 if ( $row = $db->sql_fetchrow($result) )
913                 {
914                         $select_list = '<select name="' . POST_GROUPS_URL . '">';
915                         do
916                         {
917                                 $select_list .= '<option value="' . $row['group_id'] . '">' . $row['group_name'] . '</option>';
918                         }
919                         while ( $row = $db->sql_fetchrow($result) );
920                         $select_list .= '</select>';
921                 }
923                 $template->assign_vars(array(
924                         'S_AUTH_SELECT' => $select_list)
925                 );
926         }
928         $s_hidden_fields = '<input type="hidden" name="mode" value="' . $mode . '" />';
930         $l_type = ( $mode == 'user' ) ? 'USER' : 'AUTH';
932         $template->assign_vars(array(
933                 'L_' . $l_type . '_TITLE' => ( $mode == 'user' ) ? $lang['Auth_Control_User'] : $lang['Auth_Control_Group'],
934                 'L_' . $l_type . '_EXPLAIN' => ( $mode == 'user' ) ? $lang['User_auth_explain'] : $lang['Group_auth_explain'],
935                 'L_' . $l_type . '_SELECT' => ( $mode == 'user' ) ? $lang['Select_a_User'] : $lang['Select_a_Group'],
936                 'L_LOOK_UP' => ( $mode == 'user' ) ? $lang['Look_up_User'] : $lang['Look_up_Group'],
938                 'S_HIDDEN_FIELDS' => $s_hidden_fields, 
939                 'S_' . $l_type . '_ACTION' => append_sid("admin_ug_auth.$phpEx"))
940         );
944 $template->pparse('body');
946 include('./page_footer_admin.'.$phpEx);
948 ?>