您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

acl.php 25KB

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