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
|
<?php
// +-----------------------------------------------------------------------+
// | PhpWebGallery - a PHP based picture gallery |
// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
// +-----------------------------------------------------------------------+
// | branch : BSF (Best So Far)
// | file : $RCSfile$
// | last update : $Date$
// | last modifier : $Author$
// | revision : $Revision$
// +-----------------------------------------------------------------------+
// | 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 |
// | |
// | This program is distributed in the hope that it will be useful, but |
// | WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
// | General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the Free Software |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA. |
// +-----------------------------------------------------------------------+
if (!defined('PHPWG_ROOT_PATH'))
{
die ("Hacking attempt!");
}
include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
// +-----------------------------------------------------------------------+
// | Check Access and exit when user status is not ok |
// +-----------------------------------------------------------------------+
check_status(ACCESS_ADMINISTRATOR);
// +-----------------------------------------------------------------------+
// | variable initialization |
// +-----------------------------------------------------------------------+
// if the category is not correct (not numeric, not private)
if (isset($_GET['cat']) and is_numeric($_GET['cat']))
{
$query = '
SELECT status
FROM '.CATEGORIES_TABLE.'
WHERE id = '.$_GET['cat'].'
;';
list($status) = mysql_fetch_array(pwg_query($query));
if ('private' == $status)
{
$page['cat'] = $_GET['cat'];
}
}
if (!isset($page['cat']))
{
$query = '
SELECT id
FROM '.CATEGORIES_TABLE.'
WHERE status = \'private\'
LIMIT 0,1
;';
list($page['cat']) = mysql_fetch_array(pwg_query($query));
}
// +-----------------------------------------------------------------------+
// | form submission |
// +-----------------------------------------------------------------------+
if (isset($_POST) and false)
{
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
if (isset($_POST['deny_groups_submit'])
and isset($_POST['deny_groups'])
and count($_POST['deny_groups']) > 0)
{
// if you forbid access to a category, all sub-categories become
// automatically forbidden
$query = '
DELETE
FROM '.GROUP_ACCESS_TABLE.'
WHERE group_id IN ('.implode(',', $_POST['deny_groups']).')
AND cat_id IN ('.implode(',', get_subcat_ids(array($page['cat']))).')
;';
pwg_query($query);
}
else if (isset($_POST['grant_groups_submit'])
and isset($_POST['grant_groups'])
and count($_POST['grant_groups']) > 0)
{
$query = '
SELECT id
FROM '.CATEGORIES_TABLE.'
WHERE id IN ('.implode(',', get_uppercat_ids(array($page['cat']))).')
AND status = \'private\'
;';
$private_uppercats = array_from_query($query, 'id');
// We must not reinsert already existing lines in group_access table
$granteds = array();
foreach ($private_uppercats as $cat_id)
{
$granteds[$cat_id] = array();
}
$query = '
SELECT group_id, cat_id
FROM '.GROUP_ACCESS_TABLE.'
WHERE cat_id IN ('.implode(',', $private_uppercats).')
AND group_id IN ('.implode(',', $_POST['grant_groups']).')
;';
$result = pwg_query($query);
while ($row = mysql_fetch_array($result))
{
array_push($granteds[$row['cat_id']], $row['group_id']);
}
$inserts = array();
foreach ($private_uppercats as $cat_id)
{
$group_ids = array_diff($_POST['grant_groups'], $granteds[$cat_id]);
foreach ($group_ids as $group_id)
{
array_push($inserts, array('group_id' => $group_id,
'cat_id' => $cat_id));
}
}
mass_inserts(GROUP_ACCESS_TABLE, array('group_id','cat_id'), $inserts);
}
else if (isset($_POST['deny_users_submit'])
and isset($_POST['deny_users'])
and count($_POST['deny_users']) > 0)
{
// if you forbid access to a category, all sub-categories become
// automatically forbidden
$query = '
DELETE
FROM '.USER_ACCESS_TABLE.'
WHERE user_id IN ('.implode(',', $_POST['deny_users']).')
AND cat_id IN ('.implode(',', get_subcat_ids(array($page['cat']))).')
;';
pwg_query($query);
}
else if (isset($_POST['grant_users_submit'])
and isset($_POST['grant_users'])
and count($_POST['grant_users']) > 0)
{
$query = '
SELECT id
FROM '.CATEGORIES_TABLE.'
WHERE id IN ('.implode(',', get_uppercat_ids(array($page['cat']))).')
AND status = \'private\'
;';
$private_uppercats = array_from_query($query, 'id');
// We must not reinsert already existing lines in user_access table
$granteds = array();
foreach ($private_uppercats as $cat_id)
{
$granteds[$cat_id] = array();
}
$query = '
SELECT user_id, cat_id
FROM '.USER_ACCESS_TABLE.'
WHERE cat_id IN ('.implode(',', $private_uppercats).')
AND user_id IN ('.implode(',', $_POST['grant_users']).')
;';
$result = pwg_query($query);
while ($row = mysql_fetch_array($result))
{
array_push($granteds[$row['cat_id']], $row['user_id']);
}
$inserts = array();
foreach ($private_uppercats as $cat_id)
{
$user_ids = array_diff($_POST['grant_users'], $granteds[$cat_id]);
foreach ($user_ids as $user_id)
{
array_push($inserts, array('user_id' => $user_id,
'cat_id' => $cat_id));
}
}
mass_inserts(USER_ACCESS_TABLE, array('user_id','cat_id'), $inserts);
}
// +-----------------------------------------------------------------------+
// | template initialization |
// +-----------------------------------------------------------------------+
$template->set_filenames(array('cat_perm'=>'admin/cat_perm.tpl'));
$template->assign_vars(
array(
'CATEGORIES_NAV' =>
get_cat_display_name_from_id(
$page['cat'],
'admin.php?page=cat_modify&cat_id='
),
'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=cat_perm',
'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=cat_perm&cat='.$page['cat']
)
);
// +-----------------------------------------------------------------------+
// | form construction |
// +-----------------------------------------------------------------------+
// groups denied are the groups not granted. So we need to find all groups
// minus groups granted to find groups denied.
$groups = array();
$query = '
SELECT id, name
FROM '.GROUPS_TABLE.'
ORDER BY name ASC
;';
$result = pwg_query($query);
while ($row = mysql_fetch_array($result))
{
$groups[$row['id']] = $row['name'];
}
$query = '
SELECT group_id
FROM '.GROUP_ACCESS_TABLE.'
WHERE cat_id = '.$page['cat'].'
;';
$group_granted_ids = array_from_query($query, 'group_id');
// groups granted to access the category
foreach ($group_granted_ids as $group_id)
{
$template->assign_block_vars(
'group_granted',
array(
'NAME'=>$groups[$group_id],
'ID'=>$group_id
)
);
}
// groups denied
foreach (array_diff(array_keys($groups), $group_granted_ids) as $group_id)
{
$template->assign_block_vars(
'group_denied',
array(
'NAME'=>$groups[$group_id],
'ID'=>$group_id
)
);
}
// users...
$users = array();
$query = '
SELECT '.$conf['user_fields']['id'].' AS id,
'.$conf['user_fields']['username'].' AS username
FROM '.USERS_TABLE.'
;';
$result = pwg_query($query);
while($row = mysql_fetch_array($result))
{
$users[$row['id']] = $row['username'];
}
$query = '
SELECT user_id
FROM '.USER_ACCESS_TABLE.'
WHERE cat_id = '.$page['cat'].'
;';
$user_granted_direct_ids = array_from_query($query, 'user_id');
foreach ($user_granted_direct_ids as $user_id)
{
$template->assign_block_vars(
'user_granted',
array(
'NAME'=>$users[$user_id],
'ID'=>$user_id
)
);
}
$user_granted_indirect_ids = array();
if (count($group_granted_ids) > 0)
{
$granted_groups = array();
$query = '
SELECT user_id, group_id
FROM '.USER_GROUP_TABLE.'
WHERE group_id IN ('.implode(',', $group_granted_ids).')
';
$result = pwg_query($query);
while ($row = mysql_fetch_array($result))
{
if (!isset($granted_groups[$row['group_id']]))
{
$granted_groups[$row['group_id']] = array();
}
array_push($granted_groups[$row['group_id']], $row['user_id']);
}
$user_granted_by_group_ids = array();
foreach ($granted_groups as $group_users)
{
$user_granted_by_group_ids = array_merge($user_granted_by_group_ids,
$group_users);
}
$user_granted_by_group_ids = array_unique($user_granted_by_group_ids);
$user_granted_indirect_ids = array_diff($user_granted_by_group_ids,
$user_granted_direct_ids);
foreach ($user_granted_indirect_ids as $user_id)
{
$group = '';
foreach ($granted_groups as $group_id => $group_users)
{
if (in_array($user_id, $group_users))
{
$group = $groups[$group_id];
break;
}
}
$template->assign_block_vars(
'user_granted_indirect',
array(
'NAME'=>$users[$user_id],
'GROUP'=>$group
)
);
}
}
$user_denied_ids = array_diff(array_keys($users),
$user_granted_indirect_ids,
$user_granted_direct_ids);
foreach ($user_denied_ids as $user_id)
{
$template->assign_block_vars(
'user_denied',
array(
'NAME'=>$users[$user_id],
'ID'=>$user_id
)
);
}
// +-----------------------------------------------------------------------+
// | sending html code |
// +-----------------------------------------------------------------------+
$template->assign_var_from_handle('ADMIN_CONTENT', 'cat_perm');
?>
|