| 26 | | private static $access_ids = array(); |
| 27 | | private static $access_names = array(); |
| 28 | | |
| 29 | | /** |
| 30 | | * Static initializer to fill the $access_ids array |
| 31 | | */ |
| 32 | | public static function __static() |
| 33 | | { |
| 34 | | /*self::$access_ids = DB::get_keyvalue( 'SELECT name, id FROM {permissions};' ); |
| 35 | | |
| 36 | | if ( ! isset(self::$access_ids) ) { |
| 37 | | self::$access_ids = array(); |
| 38 | | } |
| 39 | | */ |
| 40 | | self::$access_ids = array( 0 => 'read', 1 => 'write', 2 => 'deny' ); |
| 41 | | self::$access_names = array_flip( self::$access_ids ); |
| 42 | | } |
| 43 | | |
| 44 | | /** |
| 45 | | * Convert a permission access name (read, write, denied) into an ID |
| 46 | | * @param string The access name |
| 47 | | * @return mixed the ID of the permission, or boolean FALSE if it does not exist |
| 48 | | **/ |
| 49 | | public static function access_id( $name ) |
| 50 | | { |
| 51 | | // if $name is numeric, assume it is already an access ID |
| 52 | | if ( is_numeric( $name ) ) { |
| 53 | | return $name; |
| 54 | | } |
| 55 | | return isset( self::$access_ids[$name] ) ? self::$access_ids[$name] : FALSE; |
| 56 | | } |
| 57 | | |
| 58 | | /** |
| 59 | | * Convert a permission access ID into a name |
| 60 | | * @param ID The access ID |
| 61 | | * @return mixed the name of the permission, or boolean FALSE if it does not exist |
| 62 | | **/ |
| 63 | | public static function access_name( $id ) |
| 64 | | { |
| 65 | | // if $id is not numeric, assume it is already an access name |
| 66 | | if ( ! is_numeric( $id ) ) { |
| 67 | | return $id; |
| 68 | | } |
| 69 | | return isset( self::$access_names[$id] ) ? self::$access_names[$id] : FALSE; |
| 70 | | } |
| 71 | | |
| 72 | | /** |
| 73 | | * Check access. Implements hierarchy of access terms. |
| 74 | | * @param mixed $access_given The ID or name of the access given |
| 75 | | * @param mixed $access_check The ID or name of the access to check against |
| | 26 | private static $access_names = array( 'read', 'write' ); |
| | 27 | |
| | 28 | /** |
| | 29 | * Check the permission bitmask for a particular access type. |
| | 30 | * @param mixed $permission The permission bitmask |
| | 31 | * @param mixed $access The name of the access to check against (read, write, delete) |
| 78 | | public static function access_check( $access_given, $access_check ) |
| 79 | | { |
| 80 | | $access_given = self::access_name( $access_given ); |
| 81 | | $access_check = self::access_name( $access_check ); |
| 82 | | |
| 83 | | if ( $access_given == 'deny' ) { |
| 84 | | return false; |
| 85 | | } |
| 86 | | if ( $access_given == 'full' ) { |
| 87 | | return true; |
| 88 | | } |
| 89 | | if ( $access_given == 'read' || $access_given == 'write' ) { |
| 90 | | if ( $access_check == 'full' ) { |
| 91 | | return false; |
| 92 | | } |
| 93 | | if ( $access_given == $access_check ) { |
| 94 | | return true; |
| 95 | | } |
| 96 | | else { |
| 97 | | return false; |
| 98 | | } |
| 99 | | } |
| 100 | | // unknown access |
| 101 | | return false; |
| | 34 | public static function access_check( $permission_flag, $access ) |
| | 35 | { |
| | 36 | $bitmask = new Bitmask( self::$access_names, $permission ); |
| | 37 | |
| | 38 | return $bitmask->$access; |
| 371 | | $access_id = self::access_id( $access ); |
| 372 | | |
| 373 | | $tokens = DB::get_results( 'SELECT token_id FROM {user_token_permissions} WHERE user_id = ? AND permission_id = ?', array( $user_id, $access_id) ); |
| | 310 | |
| | 311 | $result = DB::get_results( 'SELECT token_id, permission_id FROM {user_token_permissions} WHERE user_id = ?', array( $user_id ) ); |
| | 312 | $bitmask = new Bitmask ( self::$access_names ); |
| | 313 | $tokens = array(); |
| | 314 | |
| | 315 | foreach ( $result as $token ) { |
| | 316 | $bitmask->value = $token->permission_id; |
| | 317 | if ( $bitmask->$access ) { |
| | 318 | $tokens[] = $token->token_id; |
| | 319 | } |
| | 320 | } |
| | 333 | $permission_id = DB::get_value( 'SELECT permission_id FROM {group_token_permissions} WHERE group_id=? AND token_id=?', |
| | 334 | array( $group_id, $token_id ) ); |
| | 335 | if ( $permission_id === false ) { |
| | 336 | $permission_id = 0; // default is 'deny' (bitmask 0) |
| | 337 | } |
| | 338 | |
| | 339 | $bitmask = new Bitmask( self::$access_names, $permission_id ); |
| | 340 | |
| | 341 | if ( $access == 'full' || $access == 'deny' ) { |
| | 342 | if ( $access == 'full' ) { |
| | 343 | $access = true; |
| | 344 | } |
| | 345 | else { |
| | 346 | $access = false; |
| | 347 | } |
| | 348 | foreach ( self::$access_names as $access_name ) { |
| | 349 | $bitmask->$access_name = $access; |
| | 350 | } |
| | 351 | } |
| | 352 | else { |
| | 353 | $bitmask->$access = true; |
| | 354 | } |
| | 355 | |
| | 378 | $permission_id = DB::get_value( 'SELECT permission_id FROM {user_token_permissions} WHERE group_id=? AND token_id=?', |
| | 379 | array( $user_id, $token_id ) ); |
| | 380 | if ( $permission_id === false ) { |
| | 381 | $permission_id = 0; // default is 'deny' (bitmask 0) |
| | 382 | } |
| | 383 | |
| | 384 | $bitmask = new Bitmask( self::$access_names, $permission_id ); |
| | 385 | |
| | 386 | if ( $access == 'full' || $access == 'deny' ) { |
| | 387 | if ( $access == 'full' ) { |
| | 388 | $access = true; |
| | 389 | } |
| | 390 | else { |
| | 391 | $access = false; |
| | 392 | } |
| | 393 | foreach ( self::$access_names as $access_name ) { |
| | 394 | $bitmask->$access_name = $access; |
| | 395 | } |
| | 396 | } |
| | 397 | else { |
| | 398 | $bitmask->$access = true; |
| | 399 | } |
| | 400 | |