LEPTON CMS 7.0.0
feel free to keep it strictly simple...
Loading...
Searching...
No Matches
function.extract_permission.php
Go to the documentation of this file.
1<?php
2
18// include secure.php to protect this file and the whole CMS!
19if(!defined("SEC_FILE")){define("SEC_FILE",'/framework/secure.php' );}
20if (defined('LEPTON_PATH')) {
21 include LEPTON_PATH.SEC_FILE;
22} else {
23 $oneback = "../";
24 $root = $oneback;
25 $level = 1;
26 while (($level < 10) && (!file_exists($root.SEC_FILE))) {
27 $root .= $oneback;
28 $level += 1;
29 }
30 if (file_exists($root.SEC_FILE)) {
31 include $root.SEC_FILE;
32 } else {
33 trigger_error(sprintf("[ <b>%s</b> ] Can't include secure.php!", $_SERVER['SCRIPT_NAME']), E_USER_ERROR);
34 }
35}
36// end include secure file
37
38
39/*
40 * Function to work-out a single part of an octal permission value
41 *
42 * @param mixed $octal_value: an octal value as string (i.e. '0777') or real octal integer (i.e. 0777 | 777)
43 * @param string $who: char or string for whom the permission is asked( U[ser] / G[roup] / O[thers] )
44 * @param string $action: char or string with the requested action( r[ead..] / w[rite..] / e|x[ecute..] )
45 * @return boolean
46 */
47function extract_permission( $octal_value, $who, $action )
48{
49 // Make sure that all arguments are set and $octal_value is a real octal-integer
50 if ( ( $who == '' ) || ( $action == '' ) || ( preg_match( '/[^0-7]/', (string) $octal_value ) ) )
51 {
52 // invalid argument, so return false
53 return false;
54 }
55 // convert into a decimal-integer to be sure having a valid value
56 $right_mask = octdec( $octal_value );
57 switch ( strtolower( $action[ 0 ] ) )
58 {
59 // get action from first char of $action, set the $action related bit in $action_mask
60 case 'r':
61 // set read-bit only (2^2)
62 $action_mask = 4;
63 break;
64 case 'w':
65 // set write-bit only (2^1)
66 $action_mask = 2;
67 break;
68 case 'e':
69 case 'x':
70 // set execute-bit only (2^0)
71 $action_mask = 1;
72 break;
73 default:
74 // undefined action name, so return false
75 return false;
76 }
77 switch ( strtolower( $who[ 0 ] ) )
78 {
79 // get who from first char of $who, shift action-mask into the right position
80 case 'u':
81 // shift left 3 bits
82 $action_mask <<= 3;
83 case 'g':
84 // shift left 3 bits
85 $action_mask <<= 3;
86 case 'o':
87 /* NOP */
88 break;
89 default:
90 // undefined who, so return false
91 return false;
92 }
93 // return result of binary-AND
94 return ( ( $right_mask & $action_mask ) != 0 );
95}
extract_permission( $octal_value, $who, $action)