1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19: 20: 21: 22: 23:
24: class IMP_Imap_Acl
25: {
26: 27: 28: 29: 30:
31: protected $_cache = array();
32:
33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43:
44: public function getACL(IMP_Mailbox $mbox, $user = false)
45: {
46: $imp_imap = $mbox->imp_imap;
47:
48: if ($imp_imap->access(IMP_Imap::ACCESS_ACL)) {
49: try {
50: if ($user) {
51: return $imp_imap->getMyACLRights($mbox);
52: }
53:
54: $ret = $imp_imap->getACL($mbox);
55: $user = $imp_imap->getParam('username');
56: if (!isset($ret[$user]) &&
57: ($acl = $this->getACL($mbox, true))) {
58: $ret[$user] = $acl;
59: }
60: return $ret;
61: } catch (IMP_Imap_Exception $e) {
62: switch ($e->getCode()) {
63: case $e::NOPERM:
64: throw new IMP_Exception(_("You do not have permission to view the ACLs on this mailbox."));
65: }
66: }
67: }
68:
69: $ret = new Horde_Imap_Client_Data_Acl(implode('', array_keys($this->getRights())));
70: unset($ret[Horde_Imap_Client::ACL_ADMINISTER]);
71:
72: return $user
73: ? $ret
74: : array($imp_imap->getParam('username') => $ret);
75: }
76:
77: 78: 79: 80: 81: 82: 83: 84: 85:
86: public function addRights(IMP_Mailbox $mbox, $user, $rights)
87: {
88: $imp_imap = $mbox->imp_imap;
89:
90: if (!strlen($rights) || !$imp_imap->access(IMP_Imap::ACCESS_ACL)) {
91: return;
92: }
93:
94: try {
95: $imp_imap->setACL($mbox, $user, array(
96: 'action' => 'add',
97: 'rights' => $rights
98: ));
99: } catch (IMP_Imap_Exception $e) {
100: throw new IMP_Exception(sprintf(_("Could not add rights for user \"%s\" for the mailbox \"%s\"."), $user, $mbox));
101: }
102: }
103:
104: 105: 106: 107: 108: 109: 110: 111: 112: 113:
114: public function removeRights(IMP_Mailbox $mbox, $user, $rights)
115: {
116: $imap = $mbox->imp_imap;
117: if (!$imap->access(IMP_Imap::ACCESS_ACL)) {
118: return;
119: }
120:
121: try {
122: if (is_null($rights)) {
123: $imap->deleteACL($mbox, $user);
124: } else {
125: $imap->setACL($mbox, $user, array(
126: 'action' => 'remove',
127: 'rights' => $rights
128: ));
129: }
130: } catch (IMP_Imap_Exception $e) {
131: throw new IMP_Exception(sprintf(_("Could not remove rights for user \"%s\" for the mailbox \"%s\"."), $user, $mbox));
132: }
133: }
134:
135: 136: 137: 138: 139: 140: 141:
142: public function canEdit(IMP_Mailbox $mbox)
143: {
144: $rights = $this->getRightsMbox($mbox, $mbox->imp_imap->getParam('username'));
145: return $rights[Horde_Imap_Client::ACL_ADMINISTER];
146: }
147:
148: 149: 150: 151: 152: 153: 154:
155: public function getRights()
156: {
157: return array(
158: Horde_Imap_Client::ACL_LOOKUP => array(
159: 'desc' => _("User can see the mailbox"),
160: 'title' => _("List")
161: ),
162: Horde_Imap_Client::ACL_READ => array(
163: 'desc' => _("Read messages"),
164: 'title' => _("Read")
165: ),
166: Horde_Imap_Client::ACL_SEEN => array(
167: 'desc' => _("Mark with Seen/Unseen flags"),
168: 'title' => _("Mark (Seen)")
169: ),
170: Horde_Imap_Client::ACL_WRITE => array(
171: 'desc' => _("Mark with other flags (e.g. Important/Answered)"),
172: 'title' => _("Mark (Other)")
173: ),
174: Horde_Imap_Client::ACL_INSERT => array(
175: 'desc' => _("Insert messages"),
176: 'title' => _("Insert")
177: ),
178: Horde_Imap_Client::ACL_POST => array(
179: 'desc' => _("Post to this mailbox (not enforced by IMAP)"),
180: 'title' => _("Post")
181: ),
182: Horde_Imap_Client::ACL_ADMINISTER => array(
183: 'desc' => _("Set permissions for other users"),
184: 'title' => _("Administer")
185: ),
186: Horde_Imap_Client::ACL_CREATEMBOX => array(
187: 'desc' => _("Create subfolders and rename mailbox"),
188: 'title' => _("Create Subfolders/Rename Mailbox")
189: ),
190: Horde_Imap_Client::ACL_DELETEMBOX => array(
191: 'desc' => _("Delete and rename mailbox"),
192: 'title' => _("Delete/Rename Mailbox")
193: ),
194: Horde_Imap_Client::ACL_DELETEMSGS => array(
195: 'desc' => _("Delete messages"),
196: 'title' => _("Delete")
197: ),
198: Horde_Imap_Client::ACL_EXPUNGE => array(
199: 'desc' => _("Purge messages"),
200: 'title' => _("Purge")
201: )
202: );
203: }
204:
205: 206: 207: 208: 209: 210: 211: 212:
213: public function getRightsMbox(IMP_Mailbox $mbox, $user)
214: {
215: $smbox = strval($mbox);
216:
217: if (!isset($this->_cache[$smbox][$user])) {
218: $imp_imap = $mbox->imp_imap;
219: $ob = null;
220:
221: if ($imp_imap->access(IMP_Imap::ACCESS_ACL)) {
222: try {
223: $ob = $imp_imap->listACLRights($mbox, $user);
224: } catch (IMP_Imap_Exception $e) {}
225: }
226:
227: $this->_cache[$smbox][$user] = is_null($ob)
228: ? new Horde_Imap_Client_Data_AclRights()
229: : $ob;
230: }
231:
232: return $this->_cache[$smbox][$user];
233: }
234:
235: }
236: