You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

acl.php 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. <?php
  2. /**
  3. * Folders Access Control Lists Management (RFC4314, RFC2086)
  4. *
  5. * @author Aleksander Machniak <alec@alec.pl>
  6. *
  7. * Copyright (C) 2011-2012, Kolab Systems AG
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see http://www.gnu.org/licenses/.
  21. */
  22. class acl extends rcube_plugin
  23. {
  24. public $task = 'settings|addressbook|calendar';
  25. private $rc;
  26. private $supported = null;
  27. private $mbox;
  28. private $ldap;
  29. private $specials = array('anyone', 'anonymous');
  30. /**
  31. * Plugin initialization
  32. */
  33. function init()
  34. {
  35. $this->rc = rcmail::get_instance();
  36. // Register hooks
  37. $this->add_hook('folder_form', array($this, 'folder_form'));
  38. // kolab_addressbook plugin
  39. $this->add_hook('addressbook_form', array($this, 'folder_form'));
  40. $this->add_hook('calendar_form_kolab', array($this, 'folder_form'));
  41. // Plugin actions
  42. $this->register_action('plugin.acl', array($this, 'acl_actions'));
  43. $this->register_action('plugin.acl-autocomplete', array($this, 'acl_autocomplete'));
  44. }
  45. /**
  46. * Handler for plugin actions (AJAX)
  47. */
  48. function acl_actions()
  49. {
  50. $action = trim(rcube_utils::get_input_value('_act', rcube_utils::INPUT_GPC));
  51. // Connect to IMAP
  52. $this->rc->storage_init();
  53. // Load localization and configuration
  54. $this->add_texts('localization/');
  55. $this->load_config();
  56. if ($action == 'save') {
  57. $this->action_save();
  58. }
  59. else if ($action == 'delete') {
  60. $this->action_delete();
  61. }
  62. else if ($action == 'list') {
  63. $this->action_list();
  64. }
  65. // Only AJAX actions
  66. $this->rc->output->send();
  67. }
  68. /**
  69. * Handler for user login autocomplete request
  70. */
  71. function acl_autocomplete()
  72. {
  73. $this->load_config();
  74. $search = rcube_utils::get_input_value('_search', rcube_utils::INPUT_GPC, true);
  75. $reqid = rcube_utils::get_input_value('_reqid', rcube_utils::INPUT_GPC);
  76. $users = array();
  77. $keys = array();
  78. if ($this->init_ldap()) {
  79. $max = (int) $this->rc->config->get('autocomplete_max', 15);
  80. $mode = (int) $this->rc->config->get('addressbook_search_mode');
  81. $this->ldap->set_pagesize($max);
  82. $result = $this->ldap->search('*', $search, $mode);
  83. foreach ($result->records as $record) {
  84. $user = $record['uid'];
  85. if (is_array($user)) {
  86. $user = array_filter($user);
  87. $user = $user[0];
  88. }
  89. if ($user) {
  90. $display = rcube_addressbook::compose_search_name($record);
  91. $user = array('name' => $user, 'display' => $display);
  92. $users[] = $user;
  93. $keys[] = $display ?: $user['name'];
  94. }
  95. }
  96. if ($this->rc->config->get('acl_groups')) {
  97. $prefix = $this->rc->config->get('acl_group_prefix');
  98. $group_field = $this->rc->config->get('acl_group_field', 'name');
  99. $result = $this->ldap->list_groups($search, $mode);
  100. foreach ($result as $record) {
  101. $group = $record['name'];
  102. $group_id = is_array($record[$group_field]) ? $record[$group_field][0] : $record[$group_field];
  103. if ($group) {
  104. $users[] = array('name' => ($prefix ?: '') . $group_id, 'display' => $group, 'type' => 'group');
  105. $keys[] = $group;
  106. }
  107. }
  108. }
  109. }
  110. if (count($users)) {
  111. // sort users index
  112. asort($keys, SORT_LOCALE_STRING);
  113. // re-sort users according to index
  114. foreach ($keys as $idx => $val) {
  115. $keys[$idx] = $users[$idx];
  116. }
  117. $users = array_values($keys);
  118. }
  119. $this->rc->output->command('ksearch_query_results', $users, $search, $reqid);
  120. $this->rc->output->send();
  121. }
  122. /**
  123. * Handler for 'folder_form' hook
  124. *
  125. * @param array $args Hook arguments array (form data)
  126. *
  127. * @return array Hook arguments array
  128. */
  129. function folder_form($args)
  130. {
  131. $mbox_imap = $args['options']['name'];
  132. $myrights = $args['options']['rights'];
  133. // Edited folder name (empty in create-folder mode)
  134. if (!strlen($mbox_imap)) {
  135. return $args;
  136. }
  137. /*
  138. // Do nothing on protected folders (?)
  139. if ($args['options']['protected']) {
  140. return $args;
  141. }
  142. */
  143. // Get MYRIGHTS
  144. if (empty($myrights)) {
  145. return $args;
  146. }
  147. // Load localization and include scripts
  148. $this->load_config();
  149. $this->specials = $this->rc->config->get('acl_specials', $this->specials);
  150. $this->add_texts('localization/', array('deleteconfirm', 'norights',
  151. 'nouser', 'deleting', 'saving', 'newuser', 'editperms'));
  152. $this->rc->output->add_label('save', 'cancel');
  153. $this->include_script('acl.js');
  154. $this->rc->output->include_script('list.js');
  155. $this->include_stylesheet($this->local_skin_path().'/acl.css');
  156. // add Info fieldset if it doesn't exist
  157. if (!isset($args['form']['props']['fieldsets']['info']))
  158. $args['form']['props']['fieldsets']['info'] = array(
  159. 'name' => $this->rc->gettext('info'),
  160. 'content' => array());
  161. // Display folder rights to 'Info' fieldset
  162. $args['form']['props']['fieldsets']['info']['content']['myrights'] = array(
  163. 'label' => rcube::Q($this->gettext('myrights')),
  164. 'value' => $this->acl2text($myrights)
  165. );
  166. // Return if not folder admin
  167. if (!in_array('a', $myrights)) {
  168. return $args;
  169. }
  170. // The 'Sharing' tab
  171. $this->mbox = $mbox_imap;
  172. $this->rc->output->set_env('acl_users_source', (bool) $this->rc->config->get('acl_users_source'));
  173. $this->rc->output->set_env('mailbox', $mbox_imap);
  174. $this->rc->output->add_handlers(array(
  175. 'acltable' => array($this, 'templ_table'),
  176. 'acluser' => array($this, 'templ_user'),
  177. 'aclrights' => array($this, 'templ_rights'),
  178. ));
  179. $this->rc->output->set_env('autocomplete_max', (int)$this->rc->config->get('autocomplete_max', 15));
  180. $this->rc->output->set_env('autocomplete_min_length', $this->rc->config->get('autocomplete_min_length'));
  181. $this->rc->output->add_label('autocompletechars', 'autocompletemore');
  182. $args['form']['sharing'] = array(
  183. 'name' => rcube::Q($this->gettext('sharing')),
  184. 'content' => $this->rc->output->parse('acl.table', false, false),
  185. );
  186. return $args;
  187. }
  188. /**
  189. * Creates ACL rights table
  190. *
  191. * @param array $attrib Template object attributes
  192. *
  193. * @return string HTML Content
  194. */
  195. function templ_table($attrib)
  196. {
  197. if (empty($attrib['id']))
  198. $attrib['id'] = 'acl-table';
  199. $out = $this->list_rights($attrib);
  200. $this->rc->output->add_gui_object('acltable', $attrib['id']);
  201. return $out;
  202. }
  203. /**
  204. * Creates ACL rights form (rights list part)
  205. *
  206. * @param array $attrib Template object attributes
  207. *
  208. * @return string HTML Content
  209. */
  210. function templ_rights($attrib)
  211. {
  212. // Get supported rights
  213. $supported = $this->rights_supported();
  214. // give plugins the opportunity to adjust this list
  215. $data = $this->rc->plugins->exec_hook('acl_rights_supported',
  216. array('rights' => $supported, 'folder' => $this->mbox, 'labels' => array()));
  217. $supported = $data['rights'];
  218. // depending on server capability either use 'te' or 'd' for deleting msgs
  219. $deleteright = implode(array_intersect(str_split('ted'), $supported));
  220. $out = '';
  221. $ul = '';
  222. $input = new html_checkbox();
  223. // Advanced rights
  224. $attrib['id'] = 'advancedrights';
  225. foreach ($supported as $key => $val) {
  226. $id = "acl$val";
  227. $ul .= html::tag('li', null,
  228. $input->show('', array(
  229. 'name' => "acl[$val]", 'value' => $val, 'id' => $id))
  230. . html::label(array('for' => $id, 'title' => $this->gettext('longacl'.$val)),
  231. $this->gettext('acl'.$val)));
  232. }
  233. $out = html::tag('ul', $attrib, $ul, html::$common_attrib);
  234. // Simple rights
  235. $ul = '';
  236. $attrib['id'] = 'simplerights';
  237. $items = array(
  238. 'read' => 'lrs',
  239. 'write' => 'wi',
  240. 'delete' => $deleteright,
  241. 'other' => preg_replace('/[lrswi'.$deleteright.']/', '', implode($supported)),
  242. );
  243. // give plugins the opportunity to adjust this list
  244. $data = $this->rc->plugins->exec_hook('acl_rights_simple',
  245. array('rights' => $items, 'folder' => $this->mbox, 'labels' => array(), 'titles' => array()));
  246. foreach ($data['rights'] as $key => $val) {
  247. $id = "acl$key";
  248. $ul .= html::tag('li', null,
  249. $input->show('', array(
  250. 'name' => "acl[$val]", 'value' => $val, 'id' => $id))
  251. . html::label(array('for' => $id, 'title' => $data['titles'][$key] ?: $this->gettext('longacl'.$key)),
  252. $data['labels'][$key] ?: $this->gettext('acl'.$key)));
  253. }
  254. $out .= "\n" . html::tag('ul', $attrib, $ul, html::$common_attrib);
  255. $this->rc->output->set_env('acl_items', $data['rights']);
  256. return $out;
  257. }
  258. /**
  259. * Creates ACL rights form (user part)
  260. *
  261. * @param array $attrib Template object attributes
  262. *
  263. * @return string HTML Content
  264. */
  265. function templ_user($attrib)
  266. {
  267. // Create username input
  268. $attrib['name'] = 'acluser';
  269. $textfield = new html_inputfield($attrib);
  270. $fields['user'] = html::label(array('for' => $attrib['id']), $this->gettext('username'))
  271. . ' ' . $textfield->show();
  272. // Add special entries
  273. if (!empty($this->specials)) {
  274. foreach ($this->specials as $key) {
  275. $fields[$key] = html::label(array('for' => 'id'.$key), $this->gettext($key));
  276. }
  277. }
  278. $this->rc->output->set_env('acl_specials', $this->specials);
  279. // Create list with radio buttons
  280. if (count($fields) > 1) {
  281. $ul = '';
  282. $radio = new html_radiobutton(array('name' => 'usertype'));
  283. foreach ($fields as $key => $val) {
  284. $ul .= html::tag('li', null, $radio->show($key == 'user' ? 'user' : '',
  285. array('value' => $key, 'id' => 'id'.$key))
  286. . $val);
  287. }
  288. $out = html::tag('ul', array('id' => 'usertype', 'class' => $attrib['class']), $ul, html::$common_attrib);
  289. }
  290. // Display text input alone
  291. else {
  292. $out = $fields['user'];
  293. }
  294. return $out;
  295. }
  296. /**
  297. * Creates ACL rights table
  298. *
  299. * @param array $attrib Template object attributes
  300. *
  301. * @return string HTML Content
  302. */
  303. private function list_rights($attrib=array())
  304. {
  305. // Get ACL for the folder
  306. $acl = $this->rc->storage->get_acl($this->mbox);
  307. if (!is_array($acl)) {
  308. $acl = array();
  309. }
  310. // Keep special entries (anyone/anonymous) on top of the list
  311. if (!empty($this->specials) && !empty($acl)) {
  312. foreach ($this->specials as $key) {
  313. if (isset($acl[$key])) {
  314. $acl_special[$key] = $acl[$key];
  315. unset($acl[$key]);
  316. }
  317. }
  318. }
  319. // Sort the list by username
  320. uksort($acl, 'strnatcasecmp');
  321. if (!empty($acl_special)) {
  322. $acl = array_merge($acl_special, $acl);
  323. }
  324. // Get supported rights and build column names
  325. $supported = $this->rights_supported();
  326. // give plugins the opportunity to adjust this list
  327. $data = $this->rc->plugins->exec_hook('acl_rights_supported',
  328. array('rights' => $supported, 'folder' => $this->mbox, 'labels' => array()));
  329. $supported = $data['rights'];
  330. // depending on server capability either use 'te' or 'd' for deleting msgs
  331. $deleteright = implode(array_intersect(str_split('ted'), $supported));
  332. // Use advanced or simple (grouped) rights
  333. $advanced = $this->rc->config->get('acl_advanced_mode');
  334. if ($advanced) {
  335. $items = array();
  336. foreach ($supported as $sup) {
  337. $items[$sup] = $sup;
  338. }
  339. }
  340. else {
  341. $items = array(
  342. 'read' => 'lrs',
  343. 'write' => 'wi',
  344. 'delete' => $deleteright,
  345. 'other' => preg_replace('/[lrswi'.$deleteright.']/', '', implode($supported)),
  346. );
  347. // give plugins the opportunity to adjust this list
  348. $data = $this->rc->plugins->exec_hook('acl_rights_simple',
  349. array('rights' => $items, 'folder' => $this->mbox, 'labels' => array()));
  350. $items = $data['rights'];
  351. }
  352. // Create the table
  353. $attrib['noheader'] = true;
  354. $table = new html_table($attrib);
  355. // Create table header
  356. $table->add_header('user', $this->gettext('identifier'));
  357. foreach (array_keys($items) as $key) {
  358. $label = $data['labels'][$key] ?: $this->gettext('shortacl'.$key);
  359. $table->add_header(array('class' => 'acl'.$key, 'title' => $label), $label);
  360. }
  361. $js_table = array();
  362. foreach ($acl as $user => $rights) {
  363. if ($this->rc->storage->conn->user == $user) {
  364. continue;
  365. }
  366. // filter out virtual rights (c or d) the server may return
  367. $userrights = array_intersect($rights, $supported);
  368. $userid = rcube_utils::html_identifier($user);
  369. if (!empty($this->specials) && in_array($user, $this->specials)) {
  370. $user = $this->gettext($user);
  371. }
  372. $table->add_row(array('id' => 'rcmrow'.$userid));
  373. $table->add('user', html::a(array('id' => 'rcmlinkrow'.$userid), rcube::Q($user)));
  374. foreach ($items as $key => $right) {
  375. $in = $this->acl_compare($userrights, $right);
  376. switch ($in) {
  377. case 2: $class = 'enabled'; break;
  378. case 1: $class = 'partial'; break;
  379. default: $class = 'disabled'; break;
  380. }
  381. $table->add('acl' . $key . ' ' . $class, '');
  382. }
  383. $js_table[$userid] = implode($userrights);
  384. }
  385. $this->rc->output->set_env('acl', $js_table);
  386. $this->rc->output->set_env('acl_advanced', $advanced);
  387. $out = $table->show();
  388. return $out;
  389. }
  390. /**
  391. * Handler for ACL update/create action
  392. */
  393. private function action_save()
  394. {
  395. $mbox = trim(rcube_utils::get_input_value('_mbox', rcube_utils::INPUT_POST, true)); // UTF7-IMAP
  396. $user = trim(rcube_utils::get_input_value('_user', rcube_utils::INPUT_POST));
  397. $acl = trim(rcube_utils::get_input_value('_acl', rcube_utils::INPUT_POST));
  398. $oldid = trim(rcube_utils::get_input_value('_old', rcube_utils::INPUT_POST));
  399. $acl = array_intersect(str_split($acl), $this->rights_supported());
  400. $users = $oldid ? array($user) : explode(',', $user);
  401. $result = 0;
  402. foreach ($users as $user) {
  403. $user = trim($user);
  404. $prefix = $this->rc->config->get('acl_groups') ? $this->rc->config->get('acl_group_prefix') : '';
  405. if ($prefix && strpos($user, $prefix) === 0) {
  406. $username = $user;
  407. }
  408. else if (!empty($this->specials) && in_array($user, $this->specials)) {
  409. $username = $this->gettext($user);
  410. }
  411. else if (!empty($user)) {
  412. if (!strpos($user, '@') && ($realm = $this->get_realm())) {
  413. $user .= '@' . rcube_utils::idn_to_ascii(preg_replace('/^@/', '', $realm));
  414. }
  415. // Make sure it's valid email address to prevent from "disappearing folder"
  416. // issue in Cyrus IMAP e.g. when the acl user identifier contains spaces inside.
  417. if (strpos($user, '@') && !rcube_utils::check_email($user, false)) {
  418. $user = null;
  419. }
  420. $username = $user;
  421. }
  422. if (!$acl || !$user || !strlen($mbox)) {
  423. continue;
  424. }
  425. $user = $this->mod_login($user);
  426. $username = $this->mod_login($username);
  427. if ($user != $_SESSION['username'] && $username != $_SESSION['username']) {
  428. if ($this->rc->storage->set_acl($mbox, $user, $acl)) {
  429. $ret = array('id' => rcube_utils::html_identifier($user),
  430. 'username' => $username, 'acl' => implode($acl), 'old' => $oldid);
  431. $this->rc->output->command('acl_update', $ret);
  432. $result++;
  433. }
  434. }
  435. }
  436. if ($result) {
  437. $this->rc->output->show_message($oldid ? 'acl.updatesuccess' : 'acl.createsuccess', 'confirmation');
  438. }
  439. else {
  440. $this->rc->output->show_message($oldid ? 'acl.updateerror' : 'acl.createerror', 'error');
  441. }
  442. }
  443. /**
  444. * Handler for ACL delete action
  445. */
  446. private function action_delete()
  447. {
  448. $mbox = trim(rcube_utils::get_input_value('_mbox', rcube_utils::INPUT_POST, true)); //UTF7-IMAP
  449. $user = trim(rcube_utils::get_input_value('_user', rcube_utils::INPUT_POST));
  450. $user = explode(',', $user);
  451. foreach ($user as $u) {
  452. $u = trim($u);
  453. if ($this->rc->storage->delete_acl($mbox, $u)) {
  454. $this->rc->output->command('acl_remove_row', rcube_utils::html_identifier($u));
  455. }
  456. else {
  457. $error = true;
  458. }
  459. }
  460. if (!$error) {
  461. $this->rc->output->show_message('acl.deletesuccess', 'confirmation');
  462. }
  463. else {
  464. $this->rc->output->show_message('acl.deleteerror', 'error');
  465. }
  466. }
  467. /**
  468. * Handler for ACL list update action (with display mode change)
  469. */
  470. private function action_list()
  471. {
  472. if (in_array('acl_advanced_mode', (array)$this->rc->config->get('dont_override'))) {
  473. return;
  474. }
  475. $this->mbox = trim(rcube_utils::get_input_value('_mbox', rcube_utils::INPUT_GPC, true)); // UTF7-IMAP
  476. $advanced = trim(rcube_utils::get_input_value('_mode', rcube_utils::INPUT_GPC));
  477. $advanced = $advanced == 'advanced';
  478. // Save state in user preferences
  479. $this->rc->user->save_prefs(array('acl_advanced_mode' => $advanced));
  480. $out = $this->list_rights();
  481. $out = preg_replace(array('/^<table[^>]+>/', '/<\/table>$/'), '', $out);
  482. $this->rc->output->command('acl_list_update', $out);
  483. }
  484. /**
  485. * Creates <UL> list with descriptive access rights
  486. *
  487. * @param array $rights MYRIGHTS result
  488. *
  489. * @return string HTML content
  490. */
  491. function acl2text($rights)
  492. {
  493. if (empty($rights)) {
  494. return '';
  495. }
  496. $supported = $this->rights_supported();
  497. $list = array();
  498. $attrib = array(
  499. 'name' => 'rcmyrights',
  500. 'style' => 'margin:0; padding:0 15px;',
  501. );
  502. foreach ($supported as $right) {
  503. if (in_array($right, $rights)) {
  504. $list[] = html::tag('li', null, rcube::Q($this->gettext('acl' . $right)));
  505. }
  506. }
  507. if (count($list) == count($supported))
  508. return rcube::Q($this->gettext('aclfull'));
  509. return html::tag('ul', $attrib, implode("\n", $list));
  510. }
  511. /**
  512. * Compares two ACLs (according to supported rights)
  513. *
  514. * @param array $acl1 ACL rights array (or string)
  515. * @param array $acl2 ACL rights array (or string)
  516. *
  517. * @param int Comparison result, 2 - full match, 1 - partial match, 0 - no match
  518. */
  519. function acl_compare($acl1, $acl2)
  520. {
  521. if (!is_array($acl1)) $acl1 = str_split($acl1);
  522. if (!is_array($acl2)) $acl2 = str_split($acl2);
  523. $rights = $this->rights_supported();
  524. $acl1 = array_intersect($acl1, $rights);
  525. $acl2 = array_intersect($acl2, $rights);
  526. $res = array_intersect($acl1, $acl2);
  527. $cnt1 = count($res);
  528. $cnt2 = count($acl2);
  529. if ($cnt1 == $cnt2)
  530. return 2;
  531. else if ($cnt1)
  532. return 1;
  533. else
  534. return 0;
  535. }
  536. /**
  537. * Get list of supported access rights (according to RIGHTS capability)
  538. *
  539. * @return array List of supported access rights abbreviations
  540. */
  541. function rights_supported()
  542. {
  543. if ($this->supported !== null) {
  544. return $this->supported;
  545. }
  546. $capa = $this->rc->storage->get_capability('RIGHTS');
  547. if (is_array($capa)) {
  548. $rights = strtolower($capa[0]);
  549. }
  550. else {
  551. $rights = 'cd';
  552. }
  553. return $this->supported = str_split('lrswi' . $rights . 'pa');
  554. }
  555. /**
  556. * Username realm detection.
  557. *
  558. * @return string Username realm (domain)
  559. */
  560. private function get_realm()
  561. {
  562. // When user enters a username without domain part, realm
  563. // allows to add it to the username (and display correct username in the table)
  564. if (isset($_SESSION['acl_username_realm'])) {
  565. return $_SESSION['acl_username_realm'];
  566. }
  567. // find realm in username of logged user (?)
  568. list($name, $domain) = explode('@', $_SESSION['username']);
  569. // Use (always existent) ACL entry on the INBOX for the user to determine
  570. // whether or not the user ID in ACL entries need to be qualified and how
  571. // they would need to be qualified.
  572. if (empty($domain)) {
  573. $acl = $this->rc->storage->get_acl('INBOX');
  574. if (is_array($acl)) {
  575. $regexp = '/^' . preg_quote($_SESSION['username'], '/') . '@(.*)$/';
  576. foreach (array_keys($acl) as $name) {
  577. if (preg_match($regexp, $name, $matches)) {
  578. $domain = $matches[1];
  579. break;
  580. }
  581. }
  582. }
  583. }
  584. return $_SESSION['acl_username_realm'] = $domain;
  585. }
  586. /**
  587. * Initializes autocomplete LDAP backend
  588. */
  589. private function init_ldap()
  590. {
  591. if ($this->ldap) {
  592. return $this->ldap->ready;
  593. }
  594. // get LDAP config
  595. $config = $this->rc->config->get('acl_users_source');
  596. if (empty($config)) {
  597. return false;
  598. }
  599. // not an array, use configured ldap_public source
  600. if (!is_array($config)) {
  601. $ldap_config = (array) $this->rc->config->get('ldap_public');
  602. $config = $ldap_config[$config];
  603. }
  604. $uid_field = $this->rc->config->get('acl_users_field', 'mail');
  605. $filter = $this->rc->config->get('acl_users_filter');
  606. if (empty($uid_field) || empty($config)) {
  607. return false;
  608. }
  609. // get name attribute
  610. if (!empty($config['fieldmap'])) {
  611. $name_field = $config['fieldmap']['name'];
  612. }
  613. // ... no fieldmap, use the old method
  614. if (empty($name_field)) {
  615. $name_field = $config['name_field'];
  616. }
  617. // add UID field to fieldmap, so it will be returned in a record with name
  618. $config['fieldmap']['name'] = $name_field;
  619. $config['fieldmap']['uid'] = $uid_field;
  620. // search in UID and name fields
  621. // $name_field can be in a form of <field>:<modifier> (#1490591)
  622. $name_field = preg_replace('/:.*$/', '', $name_field);
  623. $search = array_unique(array($name_field, $uid_field));
  624. $config['search_fields'] = $search;
  625. $config['required_fields'] = array($uid_field);
  626. // set search filter
  627. if ($filter) {
  628. $config['filter'] = $filter;
  629. }
  630. // disable vlv
  631. $config['vlv'] = false;
  632. // Initialize LDAP connection
  633. $this->ldap = new rcube_ldap($config,
  634. $this->rc->config->get('ldap_debug'),
  635. $this->rc->config->mail_domain($_SESSION['imap_host']));
  636. return $this->ldap->ready;
  637. }
  638. /**
  639. * Modify user login according to 'login_lc' setting
  640. */
  641. protected function mod_login($user)
  642. {
  643. $login_lc = $this->rc->config->get('login_lc');
  644. if ($login_lc === true || $login_lc == 2) {
  645. $user = mb_strtolower($user);
  646. }
  647. // lowercase domain name
  648. else if ($login_lc && strpos($user, '@')) {
  649. list($local, $domain) = explode('@', $user);
  650. $user = $local . '@' . mb_strtolower($domain);
  651. }
  652. return $user;
  653. }
  654. }