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
|
<?php
/*
* Session Management for PHP3
*
* Copyright (c) 1998-2000 NetUSE AG
* Boris Erdmann, Kristian Koehntopp
*
* $Id: perm.inc,v 1.2 2000/07/12 18:22:35 kk Exp $
*
*/
class Perm {
var $classname = "Perm";
## Hash ("Name" => Permission-Bitmask)
var $permissions = array ();
##
## Permission code
##
function check($p) {
global $auth;
if (! $this->have_perm($p)) {
if (! isset($auth->auth["perm"]) ) {
$auth->auth["perm"] = "";
}
$this->perm_invalid($auth->auth["perm"], $p);
exit();
}
}
function have_perm($p) {
global $auth;
if (! isset($auth->auth["perm"]) ) {
$auth->auth["perm"] = "";
}
$pageperm = split(",", $p);
$userperm = split(",", $auth->auth["perm"]);
list ($ok0, $pagebits) = $this->permsum($pageperm);
list ($ok1, $userbits) = $this->permsum($userperm);
$has_all = (($userbits & $pagebits) == $pagebits);
if (!($has_all && $ok0 && $ok1) ) {
return false;
} else {
return true;
}
}
##
## Permission helpers.
##
function permsum($p) {
global $auth;
if (!is_array($p)) {
return array(false, 0);
}
$perms = $this->permissions;
$r = 0;
reset($p);
while(list($key, $val) = each($p)) {
if (!isset($perms[$val])) {
return array(false, 0);
}
$r |= $perms[$val];
}
return array(true, $r);
}
## Look for a match within an list of strints
## I couldn't figure out a way to do this generally using ereg().
function perm_islisted($perms, $look_for) {
$permlist = explode( ",", $perms );
while( list($a,$b) = each($permlist) ) {
if( $look_for == $b ) { return true; };
};
return false;
}
## Return a complete <select> tag for permission
## selection.
function perm_sel($name, $current = "", $class = "") {
reset($this->permissions);
$ret = sprintf("<select multiple name=\"%s[]\"%s>\n",
$name,
($class!="")?" class=$class":"");
while(list($k, $v) = each($this->permissions)) {
$ret .= sprintf(" <option%s%s>%s\n",
$this->perm_islisted($current,$k)?" selected":"",
($class!="")?" class=$class":"",
$k);
}
$ret .= "</select>";
return $ret;
}
##
## Dummy Method. Must be overridden by user.
##
function perm_invalid($does_have, $must_have) {
printf("Access denied.\n");
}
}
?>
|