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