1: <?php
2: /**
3: * The Horde_Perms package provides the Horde permissions system.
4: *
5: * Copyright 2001-2012 Horde LLC (http://www.horde.org/)
6: *
7: * See the enclosed file COPYING for license information (LGPL). If you
8: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
9: *
10: * @author Chuck Hagenbuch <chuck@horde.org>
11: * @author Jan Schneider <jan@horde.org>
12: * @category Horde
13: * @package Perms
14: */
15: class Horde_Perms
16: {
17: /**
18: * Existence of object is known - object is shown to user.
19: */
20: const SHOW = 2;
21:
22: /**
23: * Contents of the object can be read.
24: */
25: const READ = 4;
26:
27: /**
28: * Contents of the object can be edited.
29: */
30:
31: const EDIT = 8;
32:
33: /**
34: * The object can be deleted.
35: */
36: const DELETE = 16;
37:
38: /**
39: * A bitmask of all possible permission values.
40: *
41: * Useful for removeXxxPermission(), unsetPerm(), etc.
42: * 30 = SHOW | READ | EDIT | DELETE
43: */
44: const ALL = 30;
45:
46: /**
47: * The root permission.
48: */
49: const ROOT = -1;
50:
51: /**
52: * Cache for integerToArray().
53: *
54: * @var array
55: */
56: static protected $_itaCache = array();
57:
58: /**
59: * Returns an hash of the available permissions.
60: *
61: * @return array The available permissions as a hash.
62: */
63: static public function getPermsArray()
64: {
65: return array(
66: self::SHOW => Horde_Perms_Translation::t("Show"),
67: self::READ => Horde_Perms_Translation::t("Read"),
68: self::EDIT => Horde_Perms_Translation::t("Edit"),
69: self::DELETE => Horde_Perms_Translation::t("Delete")
70: );
71: }
72:
73: /**
74: * Given an integer value of permissions returns an array representation
75: * of the integer.
76: *
77: * @param integer $int The integer representation of permissions.
78: *
79: * @return TODO
80: */
81: static public function integerToArray($int)
82: {
83: if (isset(self::$_itaCache[$int])) {
84: return self::$_itaCache[$int];
85: }
86:
87: self::$_itaCache[$int] = array();
88:
89: /* Get the available perms array. */
90: $perms = self::getPermsArray();
91:
92: /* Loop through each perm and check if its value is included in the
93: * integer representation. */
94: foreach ($perms as $val => $label) {
95: if ($int & $val) {
96: self::$_itaCache[$int][$val] = true;
97: }
98: }
99:
100: return self::$_itaCache[$int];
101: }
102: }
103: