PageRenderTime 71ms CodeModel.GetById 61ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/upload/includes/functions_group.php

http://torrentpier2.googlecode.com/
PHP | 230 lines | 195 code | 33 blank | 2 comment | 9 complexity | dfb8dc0101f20b99c5e12486b8668a11 MD5 | raw file
  1<?php
  2
  3if (!defined('BB_ROOT')) die(basename(__FILE__));
  4
  5function update_user_level ($user_id)
  6{
  7	global $datastore;
  8
  9	if (is_array($user_id))
 10	{
 11		$user_id = join(',', $user_id);
 12	}
 13	$user_groups_in = ($user_id !== 'all') ? "AND ug.user_id IN($user_id)" : '';
 14	$users_in       = ($user_id !== 'all') ? "AND  u.user_id IN($user_id)" : '';
 15
 16	$tmp_table = 'tmp_levels';
 17
 18	DB()->query("
 19		CREATE TEMPORARY TABLE $tmp_table (
 20			user_id MEDIUMINT NOT NULL DEFAULT '0',
 21			user_level TINYINT NOT NULL DEFAULT '0',
 22			PRIMARY KEY (user_id)
 23		) ENGINE = MEMORY
 24	");
 25
 26	DB()->query("
 27		REPLACE INTO $tmp_table (user_id, user_level)
 28			SELECT u.user_id, ". USER ."
 29			FROM ". BB_USERS ." u
 30			WHERE user_level NOT IN(". USER .",". ADMIN .")
 31				$users_in
 32		UNION
 33			SELECT DISTINCT ug.user_id, ". GROUP_MEMBER ."
 34			FROM ". BB_GROUPS ." g, ". BB_USER_GROUP ." ug
 35			WHERE g.group_single_user = 0
 36				AND ug.group_id = g.group_id
 37				AND ug.user_pending = 0
 38					$user_groups_in
 39		UNION
 40			SELECT DISTINCT ug.user_id, ". MOD ."
 41			FROM ". BB_AUTH_ACCESS ." aa, ". BB_USER_GROUP ." ug
 42			WHERE aa.forum_perm & ". BF_AUTH_MOD ."
 43				AND ug.group_id = aa.group_id
 44				AND ug.user_pending = 0
 45					$user_groups_in
 46	");
 47
 48	DB()->query("
 49		UPDATE ". BB_USERS ." u, $tmp_table lev SET
 50			u.user_level = lev.user_level
 51		WHERE lev.user_id = u.user_id
 52			AND u.user_level NOT IN(". ADMIN .")
 53				$users_in
 54	");
 55
 56	DB()->query("DROP TEMPORARY TABLE $tmp_table");
 57
 58	update_user_permissions($user_id);
 59	delete_orphan_usergroups();
 60	$datastore->update('moderators');
 61}
 62
 63function delete_group ($group_id)
 64{
 65	$group_id = (int) $group_id;
 66
 67	DB()->query("
 68		DELETE ug, g, aa
 69		FROM ". BB_USER_GROUP ." ug
 70		LEFT JOIN ". BB_GROUPS ." g ON(g.group_id = $group_id)
 71		LEFT JOIN ". BB_AUTH_ACCESS ." aa ON(aa.group_id = $group_id)
 72		WHERE ug.group_id = $group_id
 73	");
 74
 75	update_user_level('all');
 76}
 77
 78function add_user_into_group ($group_id, $user_id, $user_pending = 0, $user_time = TIMENOW)
 79{
 80	$args = DB()->build_array('INSERT', array(
 81		'group_id'     => (int) $group_id,
 82		'user_id'      => (int) $user_id,
 83		'user_pending' => (int) $user_pending,
 84		'user_time'    => (int) $user_time,
 85	));
 86	DB()->query("REPLACE INTO ". BB_USER_GROUP . $args);
 87
 88	if (!$user_pending)
 89	{
 90		update_user_level($user_id);
 91	}
 92}
 93
 94function delete_user_group ($group_id, $user_id)
 95{
 96	DB()->query("
 97		DELETE FROM ". BB_USER_GROUP ."
 98		WHERE user_id = ". (int) $user_id ."
 99			AND group_id = ". (int) $group_id ."
100	");
101
102	update_user_level($user_id);
103}
104
105function create_user_group ($user_id)
106{
107    DB()->query("INSERT INTO ". BB_GROUPS ." (group_single_user) VALUES (1)");
108
109    $group_id = (int) DB()->sql_nextid();
110    $user_id  = (int) $user_id;
111
112    DB()->query("INSERT INTO ". BB_USER_GROUP ." (user_id, group_id, user_time) VALUES ($user_id, $group_id, ". TIMENOW .")");
113
114    return $group_id;
115}
116
117function get_group_data ($group_id)
118{
119	if ($group_id === 'all')
120	{
121		$sql = "SELECT g.*, u.username AS moderator_name, aa.group_id AS auth_mod
122			FROM ". BB_GROUPS ." g
123			LEFT JOIN ". BB_USERS ." u ON(g.group_moderator = u.user_id)
124			LEFT JOIN ". BB_AUTH_ACCESS ." aa ON(aa.group_id = g.group_id AND aa.forum_perm & ". BF_AUTH_MOD .")
125			WHERE g.group_single_user = 0
126			GROUP BY g.group_id
127			ORDER BY g.group_name";
128	}
129	else
130	{
131		$sql = "SELECT g.*, u.username AS moderator_name, aa.group_id AS auth_mod
132			FROM ". BB_GROUPS ." g
133			LEFT JOIN ". BB_USERS ." u ON(g.group_moderator = u.user_id)
134			LEFT JOIN ". BB_AUTH_ACCESS ." aa ON(aa.group_id = g.group_id AND aa.forum_perm & ". BF_AUTH_MOD .")
135			WHERE g.group_id = ". (int) $group_id ."
136				AND g.group_single_user = 0
137			LIMIT 1";
138	}
139	$method = ($group_id === 'all') ? 'fetch_rowset' : 'fetch_row';
140	return DB()->$method($sql);
141}
142
143function delete_permissions ($group_id = null, $user_id = null, $cat_id = null)
144{
145	$group_id = get_id_csv($group_id);
146	$user_id  = get_id_csv($user_id);
147	$cat_id   = get_id_csv($cat_id);
148
149	$forums_join_sql = ($cat_id) ? "
150		INNER JOIN ". BB_FORUMS ." f ON(a.forum_id = f.forum_id AND f.cat_id IN($cat_id))
151	" : '';
152
153	if ($group_id)
154	{
155		DB()->query("DELETE a FROM ". BB_AUTH_ACCESS ." a $forums_join_sql WHERE a.group_id IN($group_id)");
156	}
157	if ($user_id)
158	{
159		DB()->query("DELETE a FROM ". BB_AUTH_ACCESS_SNAP ." a $forums_join_sql WHERE a.user_id IN($user_id)");
160	}
161}
162
163function store_permissions ($group_id, $auth_ary)
164{
165	if (empty($auth_ary) || !is_array($auth_ary)) return;
166
167	$values = array();
168
169	foreach ($auth_ary as $forum_id => $permission)
170	{
171		$values[] = array(
172			'group_id'   => (int) $group_id,
173			'forum_id'   => (int) $forum_id,
174			'forum_perm' => (int) $permission,
175		);
176	}
177	$values = DB()->build_array('MULTI_INSERT', $values);
178
179	DB()->query("INSERT INTO ". BB_AUTH_ACCESS . $values);
180}
181
182function update_user_permissions ($user_id = 'all')
183{
184	if (is_array($user_id))
185	{
186		$user_id = join(',', $user_id);
187	}
188	$delete_in = ($user_id !== 'all') ? " WHERE user_id IN($user_id)" : '';
189	$users_in  = ($user_id !== 'all') ? "AND ug.user_id IN($user_id)" : '';
190
191	DB()->query("DELETE FROM ". BB_AUTH_ACCESS_SNAP . $delete_in);
192
193	DB()->query("
194		INSERT INTO ". BB_AUTH_ACCESS_SNAP ."
195			(user_id, forum_id, forum_perm)
196		SELECT
197			ug.user_id, aa.forum_id, BIT_OR(aa.forum_perm)
198		FROM
199			". BB_USER_GROUP  ." ug,
200			". BB_GROUPS      ." g,
201			". BB_AUTH_ACCESS ." aa
202		WHERE
203			    ug.user_pending = 0
204				$users_in
205			AND g.group_id = ug.group_id
206			AND aa.group_id = g.group_id
207		GROUP BY
208			ug.user_id, aa.forum_id
209	");
210}
211
212function delete_orphan_usergroups ()
213{
214	// GROUP_SINGLE_USER without AUTH_ACCESS
215	DB()->query("
216		DELETE g
217		FROM ". BB_GROUPS ." g
218		LEFT JOIN ". BB_AUTH_ACCESS ." aa USING(group_id)
219		WHERE g.group_single_user = 1
220			AND aa.group_id IS NULL
221	");
222
223	// orphan USER_GROUP (against GROUP table)
224	DB()->query("
225		DELETE ug
226		FROM ". BB_USER_GROUP ." ug
227		LEFT JOIN ". BB_GROUPS ." g USING(group_id)
228		WHERE g.group_id IS NULL
229	");
230}