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: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456:
<?php
class Authorizations
{
const AUTH_PARENT_PRIORITY = 0x01;
const AUTH_CHILD_PRIORITY = 0x02;
public static function build_auth_array_from_form()
{
$array_auth_all = array();
$sum_auth = 0;
$nbr_arg = func_num_args();
$idselect = '';
if (gettype(func_get_arg($nbr_arg - 1)) == 'string')
{
$idselect = func_get_arg(--$nbr_arg);
}
$admin_auth_default = true;
if ($nbr_arg > 1)
{
$admin_auth_default = func_get_arg($nbr_arg - 1);
if (!is_bool($admin_auth_default))
$admin_auth_default = true;
else
$nbr_arg--;
}
for ($i = 0; $i < $nbr_arg; $i++)
self::get_auth_array(func_get_arg($i), $idselect, $array_auth_all, $sum_auth);
ksort($array_auth_all);
return $array_auth_all;
}
public static function auth_array_simple($bit_value, $idselect, $admin_auth_default = true)
{
$array_auth_all = array();
$sum_auth = 0;
self::get_auth_array($bit_value, $idselect, $array_auth_all, $sum_auth);
if ($admin_auth_default)
{
$array_auth_all['r2'] = $sum_auth;
}
ksort($array_auth_all);
return $array_auth_all;
}
public static function generate_select($auth_bit, $array_auth = array(), $array_ranks_default = array(), $idselect = '', $disabled = false, $disabled_advanced_auth = false, $disabled_ranks = array())
{
$lang = LangLoader::get('main');
$array_ranks = array(
User::VISITOR_LEVEL => $lang['guest'],
User::MEMBER_LEVEL => $lang['member'],
User::MODERATOR_LEVEL => $lang['modo'],
User::ADMIN_LEVEL => $lang['admin']
);
$idselect = ((string)$idselect == '') ? $auth_bit : $idselect;
$tpl = new FileTemplate('framework/groups_auth.tpl');
$tpl->put_all(array(
'C_ADVANCED_AUTH' => !$disabled_advanced_auth,
'IDSELECT' => $idselect,
'DISABLED_SELECT' => (empty($disabled) ? 'if (disabled == 0)' : ''),
'L_USERS' => $lang['member_s'],
'L_ADD_USER' => $lang['add_member'],
'L_REQUIRE_PSEUDO' => $lang['require_pseudo'],
'L_RANKS' => $lang['ranks'],
'L_GROUPS' => $lang['groups'],
'L_GO' => $lang['go'],
'L_ADVANCED_AUTHORIZATION' => $lang['advanced_authorization'],
'L_SELECT_ALL' => $lang['select_all'],
'L_SELECT_NONE' => $lang['select_none'],
'L_EXPLAIN_SELECT_MULTIPLE' => $lang['explain_select_multiple']
));
$j = -1;
foreach ($array_ranks as $idrank => $group_name)
{
if ($idrank == 2)
{
$tpl->assign_block_vars('ranks_list', array(
'ID' => $j,
'IDRANK' => $idrank,
'RANK_NAME' => $group_name,
'DISABLED' => '',
'SELECTED' => ' selected="selected"'
));
}
else
{
$selected = '';
if ( array_key_exists('r' . $idrank, $array_auth) && ((int)$array_auth['r' . $idrank] & (int)$auth_bit) !== 0 && empty($disabled))
{
$selected = ' selected="selected"';
}
$selected = (isset($array_ranks_default[$idrank]) && $array_ranks_default[$idrank] === true && empty($disabled)) ? 'selected="selected"' : $selected;
$tpl->assign_block_vars('ranks_list', array(
'C_DISABLED' => !empty($disabled) || in_array($idrank, $disabled_ranks),
'ID' => $j,
'IDRANK' => $idrank,
'RANK_NAME' => $group_name,
'SELECTED' => $selected
));
}
$j++;
}
$groups_name = GroupsService::get_groups_names();
foreach ($groups_name as $idgroup => $group_name)
{
$selected = '';
if (array_key_exists($idgroup, $array_auth) && ((int)$array_auth[$idgroup] & (int)$auth_bit) !== 0 && empty($disabled))
{
$selected = ' selected="selected"';
}
$tpl->assign_block_vars('groups_list', array(
'C_DISABLED' => !empty($disabled),
'IDGROUP' => $idgroup,
'GROUP_NAME' => $group_name,
'SELECTED' => $selected
));
}
$array_auth_members = array();
if (is_array($array_auth))
{
foreach ($array_auth as $type => $auth)
{
if (TextHelper::substr($type, 0, 1) == 'm')
{
if (array_key_exists($type, $array_auth) && ((int)$array_auth[$type] & (int)$auth_bit) !== 0)
$array_auth_members[$type] = $auth;
}
}
}
$advanced_auth = count($array_auth_members) > 0;
$tpl->put_all(array(
'C_ADVANCED_AUTH_OPEN' => $advanced_auth,
'C_NO_GROUP' => count($groups_name) == 0
));
if ($advanced_auth)
{
$result = PersistenceContext::get_querier()->select_rows(DB_TABLE_MEMBER, array('user_id, display_name'), 'WHERE user_id IN :user_ids', array('user_ids' => str_replace('m', '', array_keys($array_auth_members))));
while ($row = $result->fetch())
{
$tpl->assign_block_vars('members_list', array(
'USER_ID' => $row['user_id'],
'LOGIN' => $row['display_name']
));
}
$result->dispose();
}
return $tpl->render();
}
public static function check_auth($type, $value, $array_auth, $bit)
{
if (!is_int($value))
return false;
switch ($type)
{
case RANK_TYPE:
if ($value <= 2 && $value >= -1)
return @$array_auth['r' . $value] & $bit;
else
return false;
case GROUP_TYPE:
if ($value >= 1)
return !empty($array_auth[$value]) ? $array_auth[$value] & $bit : false;
else
return false;
case USER_TYPE:
if ($value >= 1)
return !empty($array_auth['m' . $value]) ? $array_auth['m' . $value] & $bit : false;
else
return false;
default:
return false;
}
}
public static function merge_auth($parent, $child, $auth_bit, $mode)
{
$merged = array();
if (!is_array($child))
{
return $parent;
}
if ($mode == self::AUTH_PARENT_PRIORITY)
{
$parent_guest_auth = isset($parent['r-1']) ? $parent['r-1'] : 0;
$parent_member_auth = isset($parent['r0']) ? $parent['r0'] : 0;
foreach ($parent as $key => $value)
{
if ($bit = ($value & $auth_bit) || $parent_guest_auth || $parent_member_auth)
{
if (!empty($child[$key]) || ($parent_guest_auth && !empty($child['r-1'])) || ($parent_guest_auth && !empty($child['r-1']) && $parent_member_auth && !empty($child['r0'])))
{
$merged[$key] = $auth_bit;
}
else
{
$merged[$key] = 0;
}
}
else
{
$merged[$key] = $bit;
}
unset($child[$key]);
}
foreach ($child as $key => $value)
{
if (!empty($value) || ($parent_guest_auth && !empty($merged['r-1'])))
{
$merged[$key] = $parent_guest_auth;
}
if (!empty($value) || ($parent_guest_auth && !empty($merged['r-1']) && $parent_member_auth && !empty($merged['r0'])))
{
$merged[$key] = $parent_member_auth;
}
}
}
elseif ($mode == self::AUTH_CHILD_PRIORITY)
{
foreach ($parent as $key => $value)
{
$merged[$key] = $value & $auth_bit;
}
foreach ($child as $key => $value)
{
$merged[$key] = $value & $auth_bit;
}
}
return $merged;
}
public static function capture_and_shift_bit_auth($auth, $original_bit, $final_bit = 1)
{
if ($final_bit == 0)
die('<strong>Error :</strong> The destination bit must not be void.');
$result = $auth;
if ($original_bit > $final_bit)
{
$quotient = log($original_bit / $final_bit, 2);
foreach ($auth as $user_kind => $auth_values)
{
$result[$user_kind] = ($auth_values & $original_bit) >> $quotient;
}
}
elseif ($original_bit < $final_bit)
{
$quotient = log($final_bit / $original_bit, 2);
foreach ($auth as $user_kind => $auth_values)
{
$result[$user_kind] = ($auth_values & $original_bit) << $quotient;
}
}
else
{
foreach ($auth as $user_kind => $auth_values)
{
$result[$user_kind] = $auth_values & $original_bit;
}
}
return $result;
}
private static function get_auth_array($bit_value, $idselect, &$array_auth_all, &$sum_auth)
{
$idselect = ($idselect == '') ? $bit_value : $idselect;
$array_auth_groups = !empty($_REQUEST['groups_auth' . $idselect]) ? $_REQUEST['groups_auth' . $idselect] : '';
if (!empty($array_auth_groups))
{
$sum_auth += $bit_value;
if (is_array($array_auth_groups))
{
$array_level = array(0 => 'r-1', 1 => 'r0', 2 => 'r1');
$min_auth = 3;
foreach ($array_level as $level => $key)
{
if (in_array($key, $array_auth_groups))
{
$min_auth = $level;
}
else
{
if ($min_auth < $level)
$array_auth_groups[] = $key;
}
}
foreach ($array_auth_groups as $value)
{
if ($value == "" || $value == 'r2')
{
continue;
}
if (isset($array_auth_all[$value]))
{
$array_auth_all[$value] += $bit_value;
}
else
{
$array_auth_all[$value] = $bit_value;
}
}
}
}
$array_auth_members = !empty($_REQUEST['members_auth' . $idselect]) ? $_REQUEST['members_auth' . $idselect] : '';
if (!empty($array_auth_members))
{
if (is_array($array_auth_members))
{
foreach ($array_auth_members as $key => $value)
{
if ($value == "")
{
continue;
}
if (isset($array_auth_all['m' . $value]))
{
$array_auth_all['m' . $value] += $bit_value;
}
else
{
$array_auth_all['m' . $value] = $bit_value;
}
}
}
}
}
}
?>