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());
446 if (!empty($this->specials) && in_array($user, $this->specials)) {
447 $username = $this->gettext($user);
450 if (!strpos($user, '@') && ($realm = $this->get_realm())) {
451 $user .= '@' . rcube_idn_to_ascii(preg_replace('/^@/', '', $realm));
456 if ($acl && $user && $user != $_SESSION['username'] && strlen($mbox)) {
457 $result = $this->rc->imap->set_acl($mbox, $user, $acl);
461 $ret = array('id' => html_identifier($user),
462 'username' => $username, 'acl' => implode($acl), 'old' => $oldid);
463 $this->rc->output->command('acl_update', $ret);
464 $this->rc->output->show_message($oldid ? 'acl.updatesuccess' : 'acl.createsuccess', 'confirmation');
467 $this->rc->output->show_message($oldid ? 'acl.updateerror' : 'acl.createerror', 'error');
472 * Handler for ACL delete action
474 private function action_delete()
476 $mbox = trim(get_input_value('_mbox', RCUBE_INPUT_GPC, true)); //UTF7-IMAP
477 $user = trim(get_input_value('_user', RCUBE_INPUT_GPC));
479 $user = explode(',', $user);
481 foreach ($user as $u) {
482 if ($this->rc->imap->delete_acl($mbox, $u)) {
483 $this->rc->output->command('acl_remove_row', html_identifier($u));
491 $this->rc->output->show_message('acl.deletesuccess', 'confirmation');
494 $this->rc->output->show_message('acl.deleteerror', 'error');
499 * Handler for ACL list update action (with display mode change)
501 private function action_list()
503 if (in_array('acl_advanced_mode', (array)$this->rc->config->get('dont_override'))) {
507 $this->mbox = trim(get_input_value('_mbox', RCUBE_INPUT_GPC, true)); // UTF7-IMAP
508 $advanced = trim(get_input_value('_mode', RCUBE_INPUT_GPC));
509 $advanced = $advanced == 'advanced' ? true : false;
511 // Save state in user preferences
512 $this->rc->user->save_prefs(array('acl_advanced_mode' => $advanced));
514 $out = $this->list_rights();
516 $out = preg_replace(array('/^<table[^>]+>/', '/<\/table>$/'), '', $out);
518 $this->rc->output->command('acl_list_update', $out);
522 * Creates <UL> list with descriptive access rights
524 * @param array $rights MYRIGHTS result
526 * @return string HTML content
528 function acl2text($rights)
530 if (empty($rights)) {
534 $supported = $this->rights_supported();
537 'name' => 'rcmyrights',
538 'style' => 'margin:0; padding:0 15px;',
541 foreach ($supported as $right) {
542 if (in_array($right, $rights)) {
543 $list[] = html::tag('li', null, Q($this->gettext('acl' . $right)));
547 if (count($list) == count($supported))
548 return Q($this->gettext('aclfull'));
550 return html::tag('ul', $attrib, implode("\n", $list));
554 * Compares two ACLs (according to supported rights)
556 * @param array $acl1 ACL rights array (or string)
557 * @param array $acl2 ACL rights array (or string)
559 * @param int Comparision result, 2 - full match, 1 - partial match, 0 - no match
561 function acl_compare($acl1, $acl2)
563 if (!is_array($acl1)) $acl1 = str_split($acl1);
564 if (!is_array($acl2)) $acl2 = str_split($acl2);
566 $rights = $this->rights_supported();
568 $acl1 = array_intersect($acl1, $rights);
569 $acl2 = array_intersect($acl2, $rights);
570 $res = array_intersect($acl1, $acl2);
573 $cnt2 = count($acl2);
584 * Get list of supported access rights (according to RIGHTS capability)
586 * @return array List of supported access rights abbreviations
588 function rights_supported()
590 if ($this->supported !== null) {
591 return $this->supported;
594 $capa = $this->rc->imap->get_capability('RIGHTS');
596 if (is_array($capa)) {
597 $rights = strtolower($capa[0]);
603 return $this->supported = str_split('lrswi' . $rights . 'pa');
607 * Username realm detection.
609 * @return string Username realm (domain)
611 private function get_realm()
613 // When user enters a username without domain part, realm
614 // alows to add it to the username (and display correct username in the table)
616 if (isset($_SESSION['acl_username_realm'])) {
617 return $_SESSION['acl_username_realm'];
620 // find realm in username of logged user (?)
621 list($name, $domain) = explode('@', $_SESSION['username']);
623 // Use (always existent) ACL entry on the INBOX for the user to determine
624 // whether or not the user ID in ACL entries need to be qualified and how
625 // they would need to be qualified.
626 if (empty($domain)) {
627 $acl = $this->rc->imap->get_acl('INBOX');
628 if (is_array($acl)) {
629 $regexp = '/^' . preg_quote($_SESSION['username'], '/') . '@(.*)$/';
630 foreach (array_keys($acl) as $name) {
631 if (preg_match($regexp, $name, $matches)) {
632 $domain = $matches[1];
639 return $_SESSION['acl_username_realm'] = $domain;
643 * Initializes autocomplete LDAP backend
645 private function init_ldap()
648 return $this->ldap->ready;
651 $config = $this->rc->config->get('acl_users_source');
653 if (empty($config)) {
657 // not an array, use configured ldap_public source
658 if (!is_array($config)) {
659 $ldap_config = (array) $this->rc->config->get('ldap_public');
660 $config = $ldap_config[$config];
663 $uid_field = $this->rc->config->get('acl_users_field', 'mail');
664 $filter = $this->rc->config->get('acl_users_filter');
666 if (empty($uid_field) || empty($config)) {
670 // get name attribute
671 if (!empty($config['fieldmap'])) {
672 $name_field = $config['fieldmap']['name'];
674 // ... no fieldmap, use the old method
675 if (empty($name_field)) {
676 $name_field = $config['name_field'];
679 // add UID field to fieldmap, so it will be returned in a record with name
680 $config['fieldmap'] = array(
681 'name' => $name_field,
685 // search in UID and name fields
686 $config['search_fields'] = array_values($config['fieldmap']);
687 $config['required_fields'] = array($uid_field);
691 $config['filter'] = $filter;
694 $config['vlv'] = false;
696 // Initialize LDAP connection
697 $this->ldap = new rcube_ldap($config,
698 $this->rc->config->get('ldap_debug'),
699 $this->rc->config->mail_domain($_SESSION['imap_host']));
701 return $this->ldap->ready;