4 * Folders Access Control Lists Management (RFC4314, RFC2086)
6 * @version @package_version@
7 * @author Aleksander Machniak <alec@alec.pl>
10 * Copyright (C) 2011, Kolab Systems AG
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2
14 * as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 class acl extends rcube_plugin
28 public $task = 'settings|addressbook|calendar';
31 private $supported = null;
34 private $specials = array('anyone', 'anonymous');
37 * Plugin initialization
41 $this->rc = rcmail::get_instance();
44 $this->add_hook('folder_form', array($this, 'folder_form'));
45 // kolab_addressbook plugin
46 $this->add_hook('addressbook_form', array($this, 'folder_form'));
47 $this->add_hook('calendar_form_kolab', array($this, 'folder_form'));
49 $this->register_action('plugin.acl', array($this, 'acl_actions'));
50 $this->register_action('plugin.acl-autocomplete', array($this, 'acl_autocomplete'));
54 * Handler for plugin actions (AJAX)
56 function acl_actions()
58 $action = trim(get_input_value('_act', RCUBE_INPUT_GPC));
61 $this->rc->imap_init();
62 $this->rc->imap_connect();
64 // Load localization and configuration
65 $this->add_texts('localization/');
68 if ($action == 'save') {
71 else if ($action == 'delete') {
72 $this->action_delete();
74 else if ($action == 'list') {
79 $this->rc->output->send();
83 * Handler for user login autocomplete request
85 function acl_autocomplete()
89 $search = get_input_value('_search', RCUBE_INPUT_GPC, true);
90 $sid = get_input_value('_id', RCUBE_INPUT_GPC);
93 if ($this->init_ldap()) {
94 $max = (int) $this->rc->config->get('autocomplete_max', 15);
95 $mode = (int) $this->rc->config->get('addressbook_search_mode');
97 $this->ldap->set_pagesize($max);
98 $result = $this->ldap->search('*', $search, $mode);
100 foreach ($result->records as $record) {
101 $user = $record['uid'];
103 if (is_array($user)) {
104 $user = array_filter($user);
110 $user = $record['name'] . ' (' . $user . ')';
117 sort($users, SORT_LOCALE_STRING);
119 $this->rc->output->command('ksearch_query_results', $users, $search, $sid);
120 $this->rc->output->send();
124 * Handler for 'folder_form' hook
126 * @param array $args Hook arguments array (form data)
128 * @return array Hook arguments array
130 function folder_form($args)
132 // Edited folder name (empty in create-folder mode)
133 $mbox_imap = $args['options']['name'];
134 if (!strlen($mbox_imap)) {
138 // Do nothing on protected folders (?)
139 if ($args['options']['protected']) {
144 if ($args['options']['is_root']) {
149 if (!($myrights = $args['options']['rights'])) {
153 // Do nothing if no ACL support
154 if (!$this->rc->imap->get_capability('ACL')) {
158 // Load localization and include scripts
159 $this->load_config();
160 $this->add_texts('localization/', array('deleteconfirm', 'norights',
161 'nouser', 'deleting', 'saving'));
162 $this->include_script('acl.js');
163 $this->rc->output->include_script('list.js');
164 $this->include_stylesheet($this->local_skin_path().'/acl.css');
166 // add Info fieldset if it doesn't exist
167 if (!isset($args['form']['props']['fieldsets']['info']))
168 $args['form']['props']['fieldsets']['info'] = array(
169 'name' => rcube_label('info'),
170 'content' => array());
172 // Display folder rights to 'Info' fieldset
173 $args['form']['props']['fieldsets']['info']['content']['myrights'] = array(
174 'label' => Q($this->gettext('myrights')),
175 'value' => $this->acl2text($myrights)
178 // Return if not folder admin
179 if (!in_array('a', $myrights)) {
184 $this->mbox = $mbox_imap;
185 $this->rc->output->set_env('acl_users_source', (bool) $this->rc->config->get('acl_users_source'));
186 $this->rc->output->set_env('mailbox', $mbox_imap);
187 $this->rc->output->add_handlers(array(
188 'acltable' => array($this, 'templ_table'),
189 'acluser' => array($this, 'templ_user'),
190 'aclrights' => array($this, 'templ_rights'),
193 $this->rc->output->set_env('autocomplete_max', (int)$this->rc->config->get('autocomplete_max', 15));
194 $this->rc->output->set_env('autocomplete_min_length', $this->rc->config->get('autocomplete_min_length'));
195 $this->rc->output->add_label('autocompletechars', 'autocompletemore');
197 $args['form']['sharing'] = array(
198 'name' => Q($this->gettext('sharing')),
199 'content' => $this->rc->output->parse('acl.table', false, false),
206 * Creates ACL rights table
208 * @param array $attrib Template object attributes
210 * @return string HTML Content
212 function templ_table($attrib)
214 if (empty($attrib['id']))
215 $attrib['id'] = 'acl-table';
217 $out = $this->list_rights($attrib);
219 $this->rc->output->add_gui_object('acltable', $attrib['id']);
225 * Creates ACL rights form (rights list part)
227 * @param array $attrib Template object attributes
229 * @return string HTML Content
231 function templ_rights($attrib)
233 // Get supported rights
234 $supported = $this->rights_supported();
236 // depending on server capability either use 'te' or 'd' for deleting msgs
237 $deleteright = implode(array_intersect(str_split('ted'), $supported));
241 $input = new html_checkbox();
244 $attrib['id'] = 'advancedrights';
245 foreach ($supported as $val) {
247 $ul .= html::tag('li', null,
248 $input->show('', array(
249 'name' => "acl[$val]", 'value' => $val, 'id' => $id))
250 . html::label(array('for' => $id, 'title' => $this->gettext('longacl'.$val)),
251 $this->gettext('acl'.$val)));
254 $out = html::tag('ul', $attrib, $ul, html::$common_attrib);
258 $attrib['id'] = 'simplerights';
262 'delete' => $deleteright,
263 'other' => preg_replace('/[lrswi'.$deleteright.']/', '', implode($supported)),
266 foreach ($items as $key => $val) {
268 $ul .= html::tag('li', null,
269 $input->show('', array(
270 'name' => "acl[$val]", 'value' => $val, 'id' => $id))
271 . html::label(array('for' => $id, 'title' => $this->gettext('longacl'.$key)),
272 $this->gettext('acl'.$key)));
275 $out .= "\n" . html::tag('ul', $attrib, $ul, html::$common_attrib);
277 $this->rc->output->set_env('acl_items', $items);
283 * Creates ACL rights form (user part)
285 * @param array $attrib Template object attributes
287 * @return string HTML Content
289 function templ_user($attrib)
291 // Create username input
292 $attrib['name'] = 'acluser';
294 $textfield = new html_inputfield($attrib);
296 $fields['user'] = html::label(array('for' => 'iduser'), $this->gettext('username'))
297 . ' ' . $textfield->show();
299 // Add special entries
300 if (!empty($this->specials)) {
301 foreach ($this->specials as $key) {
302 $fields[$key] = html::label(array('for' => 'id'.$key), $this->gettext($key));
306 $this->rc->output->set_env('acl_specials', $this->specials);
308 // Create list with radio buttons
309 if (count($fields) > 1) {
311 $radio = new html_radiobutton(array('name' => 'usertype'));
312 foreach ($fields as $key => $val) {
313 $ul .= html::tag('li', null, $radio->show($key == 'user' ? 'user' : '',
314 array('value' => $key, 'id' => 'id'.$key))
318 $out = html::tag('ul', array('id' => 'usertype'), $ul, html::$common_attrib);
320 // Display text input alone
322 $out = $fields['user'];
329 * Creates ACL rights table
331 * @param array $attrib Template object attributes
333 * @return string HTML Content
335 private function list_rights($attrib=array())
337 // Get ACL for the folder
338 $acl = $this->rc->imap->get_acl($this->mbox);
340 if (!is_array($acl)) {
344 // Keep special entries (anyone/anonymous) on top of the list
345 if (!empty($this->specials) && !empty($acl)) {
346 foreach ($this->specials as $key) {
347 if (isset($acl[$key])) {
348 $acl_special[$key] = $acl[$key];
354 // Sort the list by username
355 uksort($acl, 'strnatcasecmp');
357 if (!empty($acl_special)) {
358 $acl = array_merge($acl_special, $acl);
361 // Get supported rights and build column names
362 $supported = $this->rights_supported();
364 // depending on server capability either use 'te' or 'd' for deleting msgs
365 $deleteright = implode(array_intersect(str_split('ted'), $supported));
367 // Use advanced or simple (grouped) rights
368 $advanced = $this->rc->config->get('acl_advanced_mode');
372 foreach ($supported as $sup) {
380 'delete' => $deleteright,
381 'other' => preg_replace('/[lrswi'.$deleteright.']/', '', implode($supported)),
386 $attrib['noheader'] = true;
387 $table = new html_table($attrib);
389 // Create table header
390 $table->add_header('user', $this->gettext('identifier'));
391 foreach (array_keys($items) as $key) {
392 $table->add_header('acl'.$key, $this->gettext('shortacl'.$key));
397 foreach ($acl as $user => $rights) {
398 if ($this->rc->imap->conn->user == $user) {
402 // filter out virtual rights (c or d) the server may return
403 $userrights = array_intersect($rights, $supported);
404 $userid = html_identifier($user);
406 if (!empty($this->specials) && in_array($user, $this->specials)) {
407 $user = $this->gettext($user);
410 $table->add_row(array('id' => 'rcmrow'.$userid));
411 $table->add('user', Q($user));
413 foreach ($items as $key => $right) {
414 $in = $this->acl_compare($userrights, $right);
416 case 2: $class = 'enabled'; break;
417 case 1: $class = 'partial'; break;
418 default: $class = 'disabled'; break;
420 $table->add('acl' . $key . ' ' . $class, '');
423 $js_table[$userid] = implode($userrights);
426 $this->rc->output->set_env('acl', $js_table);
427 $this->rc->output->set_env('acl_advanced', $advanced);
429 $out = $table->show();
435 * Handler for ACL update/create action
437 private function action_save()
439 $mbox = trim(get_input_value('_mbox', RCUBE_INPUT_GPC, true)); // UTF7-IMAP
440 $user = trim(get_input_value('_user', RCUBE_INPUT_GPC));
441 $acl = trim(get_input_value('_acl', RCUBE_INPUT_GPC));
442 $oldid = trim(get_input_value('_old', RCUBE_INPUT_GPC));
444 $acl = array_intersect(str_split($acl), $this->rights_supported());
445 $users = $oldid ? array($user) : explode(',', $user);
447 foreach ($users as $user) {
450 if (!empty($this->specials) && in_array($user, $this->specials)) {
451 $username = $this->gettext($user);
454 if (!strpos($user, '@') && ($realm = $this->get_realm())) {
455 $user .= '@' . rcube_idn_to_ascii(preg_replace('/^@/', '', $realm));
460 if (!$acl || !$user || !strlen($mbox)) {
464 if ($user != $_SESSION['username'] && $username != $_SESSION['username']) {
465 if ($this->rc->imap->set_acl($mbox, $user, $acl)) {
466 $ret = array('id' => html_identifier($user),
467 'username' => $username, 'acl' => implode($acl), 'old' => $oldid);
468 $this->rc->output->command('acl_update', $ret);
475 $this->rc->output->show_message($oldid ? 'acl.updatesuccess' : 'acl.createsuccess', 'confirmation');
478 $this->rc->output->show_message($oldid ? 'acl.updateerror' : 'acl.createerror', 'error');
483 * Handler for ACL delete action
485 private function action_delete()
487 $mbox = trim(get_input_value('_mbox', RCUBE_INPUT_GPC, true)); //UTF7-IMAP
488 $user = trim(get_input_value('_user', RCUBE_INPUT_GPC));
490 $user = explode(',', $user);
492 foreach ($user as $u) {
494 if ($this->rc->imap->delete_acl($mbox, $u)) {
495 $this->rc->output->command('acl_remove_row', html_identifier($u));
503 $this->rc->output->show_message('acl.deletesuccess', 'confirmation');
506 $this->rc->output->show_message('acl.deleteerror', 'error');
511 * Handler for ACL list update action (with display mode change)
513 private function action_list()
515 if (in_array('acl_advanced_mode', (array)$this->rc->config->get('dont_override'))) {
519 $this->mbox = trim(get_input_value('_mbox', RCUBE_INPUT_GPC, true)); // UTF7-IMAP
520 $advanced = trim(get_input_value('_mode', RCUBE_INPUT_GPC));
521 $advanced = $advanced == 'advanced' ? true : false;
523 // Save state in user preferences
524 $this->rc->user->save_prefs(array('acl_advanced_mode' => $advanced));
526 $out = $this->list_rights();
528 $out = preg_replace(array('/^<table[^>]+>/', '/<\/table>$/'), '', $out);
530 $this->rc->output->command('acl_list_update', $out);
534 * Creates <UL> list with descriptive access rights
536 * @param array $rights MYRIGHTS result
538 * @return string HTML content
540 function acl2text($rights)
542 if (empty($rights)) {
546 $supported = $this->rights_supported();
549 'name' => 'rcmyrights',
550 'style' => 'margin:0; padding:0 15px;',
553 foreach ($supported as $right) {
554 if (in_array($right, $rights)) {
555 $list[] = html::tag('li', null, Q($this->gettext('acl' . $right)));
559 if (count($list) == count($supported))
560 return Q($this->gettext('aclfull'));
562 return html::tag('ul', $attrib, implode("\n", $list));
566 * Compares two ACLs (according to supported rights)
568 * @param array $acl1 ACL rights array (or string)
569 * @param array $acl2 ACL rights array (or string)
571 * @param int Comparision result, 2 - full match, 1 - partial match, 0 - no match
573 function acl_compare($acl1, $acl2)
575 if (!is_array($acl1)) $acl1 = str_split($acl1);
576 if (!is_array($acl2)) $acl2 = str_split($acl2);
578 $rights = $this->rights_supported();
580 $acl1 = array_intersect($acl1, $rights);
581 $acl2 = array_intersect($acl2, $rights);
582 $res = array_intersect($acl1, $acl2);
585 $cnt2 = count($acl2);
596 * Get list of supported access rights (according to RIGHTS capability)
598 * @return array List of supported access rights abbreviations
600 function rights_supported()
602 if ($this->supported !== null) {
603 return $this->supported;
606 $capa = $this->rc->imap->get_capability('RIGHTS');
608 if (is_array($capa)) {
609 $rights = strtolower($capa[0]);
615 return $this->supported = str_split('lrswi' . $rights . 'pa');
619 * Username realm detection.
621 * @return string Username realm (domain)
623 private function get_realm()
625 // When user enters a username without domain part, realm
626 // alows to add it to the username (and display correct username in the table)
628 if (isset($_SESSION['acl_username_realm'])) {
629 return $_SESSION['acl_username_realm'];
632 // find realm in username of logged user (?)
633 list($name, $domain) = explode('@', $_SESSION['username']);
635 // Use (always existent) ACL entry on the INBOX for the user to determine
636 // whether or not the user ID in ACL entries need to be qualified and how
637 // they would need to be qualified.
638 if (empty($domain)) {
639 $acl = $this->rc->imap->get_acl('INBOX');
640 if (is_array($acl)) {
641 $regexp = '/^' . preg_quote($_SESSION['username'], '/') . '@(.*)$/';
642 foreach (array_keys($acl) as $name) {
643 if (preg_match($regexp, $name, $matches)) {
644 $domain = $matches[1];
651 return $_SESSION['acl_username_realm'] = $domain;
655 * Initializes autocomplete LDAP backend
657 private function init_ldap()
660 return $this->ldap->ready;
663 $config = $this->rc->config->get('acl_users_source');
665 if (empty($config)) {
669 // not an array, use configured ldap_public source
670 if (!is_array($config)) {
671 $ldap_config = (array) $this->rc->config->get('ldap_public');
672 $config = $ldap_config[$config];
675 $uid_field = $this->rc->config->get('acl_users_field', 'mail');
676 $filter = $this->rc->config->get('acl_users_filter');
678 if (empty($uid_field) || empty($config)) {
682 // get name attribute
683 if (!empty($config['fieldmap'])) {
684 $name_field = $config['fieldmap']['name'];
686 // ... no fieldmap, use the old method
687 if (empty($name_field)) {
688 $name_field = $config['name_field'];
691 // add UID field to fieldmap, so it will be returned in a record with name
692 $config['fieldmap'] = array(
693 'name' => $name_field,
697 // search in UID and name fields
698 $config['search_fields'] = array_values($config['fieldmap']);
699 $config['required_fields'] = array($uid_field);
703 $config['filter'] = $filter;
706 $config['vlv'] = false;
708 // Initialize LDAP connection
709 $this->ldap = new rcube_ldap($config,
710 $this->rc->config->get('ldap_debug'),
711 $this->rc->config->mail_domain($_SESSION['imap_host']));
713 return $this->ldap->ready;