source: trunk/admin/cat_perm.php @ 1004

Last change on this file since 1004 was 1004, checked in by nikrou, 18 years ago

Improve security of sessions:

  • use only cookies to store session id on client side
  • use default php session system with database handler to store sessions on server side
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2006-01-15 13:45:42 +0000 (Sun, 15 Jan 2006) $
10// | last modifier : $Author: nikrou $
11// | revision      : $Revision: 1004 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28if (!defined('PHPWG_ROOT_PATH'))
29{
30  die ("Hacking attempt!");
31}
32include_once(PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php');
33
34// +-----------------------------------------------------------------------+
35// |                       variable initialization                         |
36// +-----------------------------------------------------------------------+
37
38// if the category is not correct (not numeric, not private)
39if (isset($_GET['cat']) and is_numeric($_GET['cat']))
40{
41  $query = '
42SELECT status
43  FROM '.CATEGORIES_TABLE.'
44  WHERE id = '.$_GET['cat'].'
45;';
46  list($status) = mysql_fetch_array(pwg_query($query));
47 
48  if ('private' == $status)
49  {
50    $page['cat'] = $_GET['cat'];
51  }
52}
53
54if (!isset($page['cat']))
55{
56  $query = '
57SELECT id
58  FROM '.CATEGORIES_TABLE.'
59  WHERE status = \'private\'
60  LIMIT 0,1
61;';
62
63  list($page['cat']) = mysql_fetch_array(pwg_query($query));
64}
65
66// +-----------------------------------------------------------------------+
67// |                           form submission                             |
68// +-----------------------------------------------------------------------+
69
70if (isset($_POST) and false)
71{
72  echo '<pre>';
73  print_r($_POST);
74  echo '</pre>';
75}
76
77if (isset($_POST['deny_groups_submit'])
78         and isset($_POST['deny_groups'])
79         and count($_POST['deny_groups']) > 0)
80{
81  // if you forbid access to a category, all sub-categories become
82  // automatically forbidden
83  $query = '
84DELETE
85  FROM '.GROUP_ACCESS_TABLE.'
86  WHERE group_id IN ('.implode(',', $_POST['deny_groups']).')
87    AND cat_id IN ('.implode(',', get_subcat_ids(array($page['cat']))).')
88;';
89  pwg_query($query);
90}
91else if (isset($_POST['grant_groups_submit'])
92         and isset($_POST['grant_groups'])
93         and count($_POST['grant_groups']) > 0)
94{
95  $query = '
96SELECT id
97  FROM '.CATEGORIES_TABLE.'
98  WHERE id IN ('.implode(',', get_uppercat_ids(array($page['cat']))).')
99  AND status = \'private\'
100;';
101  $private_uppercats = array_from_query($query, 'id');
102
103  // We must not reinsert already existing lines in group_access table
104  $granteds = array();
105  foreach ($private_uppercats as $cat_id)
106  {
107    $granteds[$cat_id] = array();
108  }
109 
110  $query = '
111SELECT group_id, cat_id
112  FROM '.GROUP_ACCESS_TABLE.'
113  WHERE cat_id IN ('.implode(',', $private_uppercats).')
114    AND group_id IN ('.implode(',', $_POST['grant_groups']).')
115;';
116  $result = pwg_query($query);
117  while ($row = mysql_fetch_array($result))
118  {
119    array_push($granteds[$row['cat_id']], $row['group_id']);
120  }
121
122  $inserts = array();
123 
124  foreach ($private_uppercats as $cat_id)
125  {
126    $group_ids = array_diff($_POST['grant_groups'], $granteds[$cat_id]);
127    foreach ($group_ids as $group_id)
128    {
129      array_push($inserts, array('group_id' => $group_id,
130                                 'cat_id' => $cat_id));
131    }
132  }
133
134  mass_inserts(GROUP_ACCESS_TABLE, array('group_id','cat_id'), $inserts);
135}
136else if (isset($_POST['deny_users_submit'])
137         and isset($_POST['deny_users'])
138         and count($_POST['deny_users']) > 0)
139{
140  // if you forbid access to a category, all sub-categories become
141  // automatically forbidden
142  $query = '
143DELETE
144  FROM '.USER_ACCESS_TABLE.'
145  WHERE user_id IN ('.implode(',', $_POST['deny_users']).')
146    AND cat_id IN ('.implode(',', get_subcat_ids(array($page['cat']))).')
147;';
148  pwg_query($query);
149}
150else if (isset($_POST['grant_users_submit'])
151         and isset($_POST['grant_users'])
152         and count($_POST['grant_users']) > 0)
153{
154  $query = '
155SELECT id
156  FROM '.CATEGORIES_TABLE.'
157  WHERE id IN ('.implode(',', get_uppercat_ids(array($page['cat']))).')
158  AND status = \'private\'
159;';
160  $private_uppercats = array_from_query($query, 'id');
161
162  // We must not reinsert already existing lines in user_access table
163  $granteds = array();
164  foreach ($private_uppercats as $cat_id)
165  {
166    $granteds[$cat_id] = array();
167  }
168 
169  $query = '
170SELECT user_id, cat_id
171  FROM '.USER_ACCESS_TABLE.'
172  WHERE cat_id IN ('.implode(',', $private_uppercats).')
173    AND user_id IN ('.implode(',', $_POST['grant_users']).')
174;';
175  $result = pwg_query($query);
176  while ($row = mysql_fetch_array($result))
177  {
178    array_push($granteds[$row['cat_id']], $row['user_id']);
179  }
180
181  $inserts = array();
182 
183  foreach ($private_uppercats as $cat_id)
184  {
185    $user_ids = array_diff($_POST['grant_users'], $granteds[$cat_id]);
186    foreach ($user_ids as $user_id)
187    {
188      array_push($inserts, array('user_id' => $user_id,
189                                 'cat_id' => $cat_id));
190    }
191  }
192
193  mass_inserts(USER_ACCESS_TABLE, array('user_id','cat_id'), $inserts);
194}
195
196// +-----------------------------------------------------------------------+
197// |                       template initialization                         |
198// +-----------------------------------------------------------------------+
199
200$template->set_filenames(array('cat_perm'=>'admin/cat_perm.tpl'));
201
202$template->assign_vars(
203  array(
204    'CATEGORIES_NAV' =>
205      get_cat_display_name_from_id(
206        $page['cat'],
207        'admin.php?page=cat_modify&amp;cat_id='
208        ),
209    'U_HELP' => PHPWG_ROOT_PATH.'/popuphelp.php?page=cat_perm',
210    'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=cat_perm&amp;cat='.$page['cat']
211    )
212  );
213
214// +-----------------------------------------------------------------------+
215// |                          form construction                            |
216// +-----------------------------------------------------------------------+
217
218// groups denied are the groups not granted. So we need to find all groups
219// minus groups granted to find groups denied.
220
221$groups = array();
222
223$query = '
224SELECT id, name
225  FROM '.GROUPS_TABLE.'
226;';
227$result = pwg_query($query);
228
229while ($row = mysql_fetch_array($result))
230{
231  $groups[$row['id']] = $row['name'];
232}
233
234$query = '
235SELECT group_id
236  FROM '.GROUP_ACCESS_TABLE.'
237  WHERE cat_id = '.$page['cat'].'
238;';
239$group_granted_ids = array_from_query($query, 'group_id');
240
241// groups granted to access the category
242foreach ($group_granted_ids as $group_id)
243{
244  $template->assign_block_vars(
245    'group_granted',
246    array(
247      'NAME'=>$groups[$group_id],
248      'ID'=>$group_id
249      )
250    );
251}
252
253// groups denied
254foreach (array_diff(array_keys($groups), $group_granted_ids) as $group_id)
255{
256  $template->assign_block_vars(
257    'group_denied',
258    array(
259      'NAME'=>$groups[$group_id],
260      'ID'=>$group_id
261      )
262    );
263}
264
265// users...
266$users = array();
267
268$query = '
269SELECT '.$conf['user_fields']['id'].' AS id,
270       '.$conf['user_fields']['username'].' AS username
271  FROM '.USERS_TABLE.'
272  WHERE '.$conf['user_fields']['id'].' != '.$conf['guest_id'].'
273;';
274$result = pwg_query($query);
275while($row = mysql_fetch_array($result))
276{
277  $users[$row['id']] = $row['username'];
278}
279
280$query = '
281SELECT user_id
282  FROM '.USER_ACCESS_TABLE.'
283  WHERE cat_id = '.$page['cat'].'
284;';
285$user_granted_direct_ids = array_from_query($query, 'user_id');
286
287foreach ($user_granted_direct_ids as $user_id)
288{
289  $template->assign_block_vars(
290    'user_granted',
291    array(
292      'NAME'=>$users[$user_id],
293      'ID'=>$user_id
294      )
295    );
296}
297
298$user_granted_indirect_ids = array();
299if (count($group_granted_ids) > 0)
300{
301  $granted_groups = array();
302
303  $query = '
304SELECT user_id, group_id
305  FROM '.USER_GROUP_TABLE.'
306  WHERE group_id IN ('.implode(',', $group_granted_ids).')
307';
308  $result = pwg_query($query);
309  while ($row = mysql_fetch_array($result))
310  {
311    if (!isset($granted_groups[$row['group_id']]))
312    {
313      $granted_groups[$row['group_id']] = array();
314    }
315    array_push($granted_groups[$row['group_id']], $row['user_id']);
316  }
317
318  $user_granted_by_group_ids = array();
319
320  foreach ($granted_groups as $group_users)
321  {
322    $user_granted_by_group_ids = array_merge($user_granted_by_group_ids,
323                                             $group_users);
324  }
325  $user_granted_by_group_ids = array_unique($user_granted_by_group_ids);
326 
327 
328  $user_granted_indirect_ids = array_diff($user_granted_by_group_ids,
329                                          $user_granted_direct_ids);
330 
331  foreach ($user_granted_indirect_ids as $user_id)
332  {
333    $group = '';
334   
335    foreach ($granted_groups as $group_id => $group_users)
336    {
337      if (in_array($user_id, $group_users))
338      {
339        $group = $groups[$group_id];
340        break;
341      }
342    }
343   
344    $template->assign_block_vars(
345      'user_granted_indirect',
346      array(
347        'NAME'=>$users[$user_id],
348        'GROUP'=>$group
349        )
350      );
351  }
352}
353
354$user_denied_ids = array_diff(array_keys($users),
355                              $user_granted_indirect_ids,
356                              $user_granted_direct_ids);
357
358foreach ($user_denied_ids as $user_id)
359{
360  $template->assign_block_vars(
361    'user_denied',
362    array(
363      'NAME'=>$users[$user_id],
364      'ID'=>$user_id
365      )
366    );
367}
368
369
370// +-----------------------------------------------------------------------+
371// |                           sending html code                           |
372// +-----------------------------------------------------------------------+
373$template->assign_var_from_handle('ADMIN_CONTENT', 'cat_perm');
374?>
Note: See TracBrowser for help on using the repository browser.