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.
 
 
 
 
 

875 lines
28 KiB

  1. <?php
  2. use dokuwiki\Extension\AdminPlugin;
  3. use dokuwiki\Utf8\Sort;
  4. /**
  5. * ACL administration functions
  6. *
  7. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  8. * @author Andreas Gohr <andi@splitbrain.org>
  9. * @author Anika Henke <anika@selfthinker.org> (concepts)
  10. * @author Frank Schubert <frank@schokilade.de> (old version)
  11. */
  12. /**
  13. * All DokuWiki plugins to extend the admin function
  14. * need to inherit from this class
  15. */
  16. class admin_plugin_acl extends AdminPlugin
  17. {
  18. public $acl;
  19. protected $ns;
  20. /**
  21. * The currently selected item, associative array with id and type.
  22. * Populated from (in this order):
  23. * $_REQUEST['current_ns']
  24. * $_REQUEST['current_id']
  25. * $ns
  26. * $ID
  27. */
  28. protected $current_item;
  29. protected $who = '';
  30. protected $usersgroups = [];
  31. protected $specials = [];
  32. /**
  33. * return prompt for admin menu
  34. */
  35. public function getMenuText($language)
  36. {
  37. return $this->getLang('admin_acl');
  38. }
  39. /**
  40. * return sort order for position in admin menu
  41. */
  42. public function getMenuSort()
  43. {
  44. return 1;
  45. }
  46. /**
  47. * handle user request
  48. *
  49. * Initializes internal vars and handles modifications
  50. *
  51. * @author Andreas Gohr <andi@splitbrain.org>
  52. */
  53. public function handle()
  54. {
  55. global $AUTH_ACL;
  56. global $ID;
  57. global $auth;
  58. global $config_cascade;
  59. global $INPUT;
  60. // fresh 1:1 copy without replacements
  61. $AUTH_ACL = file($config_cascade['acl']['default']);
  62. // namespace given?
  63. if ($INPUT->str('ns') == '*') {
  64. $this->ns = '*';
  65. } else {
  66. $this->ns = cleanID($INPUT->str('ns'));
  67. }
  68. if ($INPUT->str('current_ns')) {
  69. $this->current_item = ['id' => cleanID($INPUT->str('current_ns')), 'type' => 'd'];
  70. } elseif ($INPUT->str('current_id')) {
  71. $this->current_item = ['id' => cleanID($INPUT->str('current_id')), 'type' => 'f'];
  72. } elseif ($this->ns) {
  73. $this->current_item = ['id' => $this->ns, 'type' => 'd'];
  74. } else {
  75. $this->current_item = ['id' => $ID, 'type' => 'f'];
  76. }
  77. // user or group choosen?
  78. $who = trim($INPUT->str('acl_w'));
  79. if ($INPUT->str('acl_t') == '__g__' && $who) {
  80. $this->who = '@' . ltrim($auth->cleanGroup($who), '@');
  81. } elseif ($INPUT->str('acl_t') == '__u__' && $who) {
  82. $this->who = ltrim($who, '@');
  83. if ($this->who != '%USER%' && $this->who != '%GROUP%') { #keep wildcard as is
  84. $this->who = $auth->cleanUser($this->who);
  85. }
  86. } elseif (
  87. $INPUT->str('acl_t') &&
  88. $INPUT->str('acl_t') != '__u__' &&
  89. $INPUT->str('acl_t') != '__g__'
  90. ) {
  91. $this->who = $INPUT->str('acl_t');
  92. } elseif ($who) {
  93. $this->who = $who;
  94. }
  95. // handle modifications
  96. if ($INPUT->has('cmd') && checkSecurityToken()) {
  97. $cmd = $INPUT->extract('cmd')->str('cmd');
  98. // scope for modifications
  99. if ($this->ns) {
  100. if ($this->ns == '*') {
  101. $scope = '*';
  102. } else {
  103. $scope = $this->ns . ':*';
  104. }
  105. } else {
  106. $scope = $ID;
  107. }
  108. if ($cmd == 'save' && $scope && $this->who && $INPUT->has('acl')) {
  109. // handle additions or single modifications
  110. $this->deleteACL($scope, $this->who);
  111. $this->addOrUpdateACL($scope, $this->who, $INPUT->int('acl'));
  112. } elseif ($cmd == 'del' && $scope && $this->who) {
  113. // handle single deletions
  114. $this->deleteACL($scope, $this->who);
  115. } elseif ($cmd == 'update') {
  116. $acl = $INPUT->arr('acl');
  117. // handle update of the whole file
  118. foreach ($INPUT->arr('del') as $where => $names) {
  119. // remove all rules marked for deletion
  120. foreach ($names as $who)
  121. unset($acl[$where][$who]);
  122. }
  123. // prepare lines
  124. $lines = [];
  125. // keep header
  126. foreach ($AUTH_ACL as $line) {
  127. if ($line[0] == '#') {
  128. $lines[] = $line;
  129. } else {
  130. break;
  131. }
  132. }
  133. // re-add all rules
  134. foreach ($acl as $where => $opt) {
  135. foreach ($opt as $who => $perm) {
  136. if ($who[0] == '@') {
  137. if ($who != '@ALL') {
  138. $who = '@' . ltrim($auth->cleanGroup($who), '@');
  139. }
  140. } elseif ($who != '%USER%' && $who != '%GROUP%') { #keep wildcard as is
  141. $who = $auth->cleanUser($who);
  142. }
  143. $who = auth_nameencode($who, true);
  144. $lines[] = "$where\t$who\t$perm\n";
  145. }
  146. }
  147. // save it
  148. io_saveFile($config_cascade['acl']['default'], implode('', $lines));
  149. }
  150. // reload ACL config
  151. $AUTH_ACL = file($config_cascade['acl']['default']);
  152. }
  153. // initialize ACL array
  154. $this->initAclConfig();
  155. }
  156. /**
  157. * ACL Output function
  158. *
  159. * print a table with all significant permissions for the
  160. * current id
  161. *
  162. * @author Frank Schubert <frank@schokilade.de>
  163. * @author Andreas Gohr <andi@splitbrain.org>
  164. */
  165. public function html()
  166. {
  167. echo '<div id="acl_manager">';
  168. echo '<h1>' . $this->getLang('admin_acl') . '</h1>';
  169. echo '<div class="level1">';
  170. echo '<div id="acl__tree">';
  171. $this->makeExplorer();
  172. echo '</div>';
  173. echo '<div id="acl__detail">';
  174. $this->printDetail();
  175. echo '</div>';
  176. echo '</div>';
  177. echo '<div class="clearer"></div>';
  178. echo '<h2>' . $this->getLang('current') . '</h2>';
  179. echo '<div class="level2">';
  180. $this->printAclTable();
  181. echo '</div>';
  182. echo '<div class="footnotes"><div class="fn">';
  183. echo '<sup><a id="fn__1" class="fn_bot" href="#fnt__1">1)</a></sup>';
  184. echo '<div class="content">' . $this->getLang('p_include') . '</div>';
  185. echo '</div></div>';
  186. echo '</div>';
  187. }
  188. /**
  189. * returns array with set options for building links
  190. *
  191. * @author Andreas Gohr <andi@splitbrain.org>
  192. */
  193. protected function getLinkOptions($addopts = null)
  194. {
  195. $opts = ['do' => 'admin', 'page' => 'acl'];
  196. if ($this->ns) $opts['ns'] = $this->ns;
  197. if ($this->who) $opts['acl_w'] = $this->who;
  198. if (is_null($addopts)) return $opts;
  199. return array_merge($opts, $addopts);
  200. }
  201. /**
  202. * Display a tree menu to select a page or namespace
  203. *
  204. * @author Andreas Gohr <andi@splitbrain.org>
  205. */
  206. protected function makeExplorer()
  207. {
  208. global $conf;
  209. global $ID;
  210. global $lang;
  211. $ns = $this->ns;
  212. if (empty($ns)) {
  213. $ns = dirname(str_replace(':', '/', $ID));
  214. if ($ns == '.') $ns = '';
  215. } elseif ($ns == '*') {
  216. $ns = '';
  217. }
  218. $ns = utf8_encodeFN(str_replace(':', '/', $ns));
  219. $data = $this->makeTree($ns);
  220. // wrap a list with the root level around the other namespaces
  221. array_unshift($data, [
  222. 'level' => 0,
  223. 'id' => '*',
  224. 'type' => 'd',
  225. 'open' => 'true',
  226. 'label' => '[' . $lang['mediaroot'] . ']'
  227. ]);
  228. echo html_buildlist(
  229. $data,
  230. 'acl',
  231. [$this, 'makeTreeItem'],
  232. [$this, 'makeListItem']
  233. );
  234. }
  235. /**
  236. * get a combined list of media and page files
  237. *
  238. * also called via AJAX
  239. *
  240. * @param string $folder an already converted filesystem folder of the current namespace
  241. * @param string $limit limit the search to this folder
  242. * @return array
  243. */
  244. public function makeTree($folder, $limit = '')
  245. {
  246. global $conf;
  247. // read tree structure from pages and media
  248. $data = [];
  249. search($data, $conf['datadir'], 'search_index', ['ns' => $folder], $limit);
  250. $media = [];
  251. search($media, $conf['mediadir'], 'search_index', ['ns' => $folder, 'nofiles' => true], $limit);
  252. $data = array_merge($data, $media);
  253. unset($media);
  254. // combine by sorting and removing duplicates
  255. usort($data, [$this, 'treeSort']);
  256. $count = count($data);
  257. if ($count > 0) for ($i = 1; $i < $count; $i++) {
  258. if ($data[$i - 1]['id'] == $data[$i]['id'] && $data[$i - 1]['type'] == $data[$i]['type']) {
  259. unset($data[$i]);
  260. $i++; // duplicate found, next $i can't be a duplicate, so skip forward one
  261. }
  262. }
  263. return $data;
  264. }
  265. /**
  266. * usort callback
  267. *
  268. * Sorts the combined trees of media and page files
  269. */
  270. public function treeSort($a, $b)
  271. {
  272. // handle the trivial cases first
  273. if ($a['id'] == '') return -1;
  274. if ($b['id'] == '') return 1;
  275. // split up the id into parts
  276. $a_ids = explode(':', $a['id']);
  277. $b_ids = explode(':', $b['id']);
  278. // now loop through the parts
  279. while (count($a_ids) && count($b_ids)) {
  280. // compare each level from upper to lower
  281. // until a non-equal component is found
  282. $cur_result = Sort::strcmp(array_shift($a_ids), array_shift($b_ids));
  283. if ($cur_result) {
  284. // if one of the components is the last component and is a file
  285. // and the other one is either of a deeper level or a directory,
  286. // the file has to come after the deeper level or directory
  287. if ($a_ids === [] && $a['type'] == 'f' && (count($b_ids) || $b['type'] == 'd')) return 1;
  288. if ($b_ids === [] && $b['type'] == 'f' && (count($a_ids) || $a['type'] == 'd')) return -1;
  289. return $cur_result;
  290. }
  291. }
  292. // The two ids seem to be equal. One of them might however refer
  293. // to a page, one to a namespace, the namespace needs to be first.
  294. if ($a_ids === [] && $b_ids === []) {
  295. if ($a['type'] == $b['type']) return 0;
  296. if ($a['type'] == 'f') return 1;
  297. return -1;
  298. }
  299. // Now the empty part is either a page in the parent namespace
  300. // that obviously needs to be after the namespace
  301. // Or it is the namespace that contains the other part and should be
  302. // before that other part.
  303. if ($a_ids === []) return ($a['type'] == 'd') ? -1 : 1;
  304. if ($b_ids === []) return ($b['type'] == 'd') ? 1 : -1;
  305. return 0; //shouldn't happen
  306. }
  307. /**
  308. * Display the current ACL for selected where/who combination with
  309. * selectors and modification form
  310. *
  311. * @author Andreas Gohr <andi@splitbrain.org>
  312. */
  313. protected function printDetail()
  314. {
  315. global $ID;
  316. echo '<form action="' . wl() . '" method="post" accept-charset="utf-8"><div class="no">';
  317. echo '<div id="acl__user">';
  318. echo $this->getLang('acl_perms') . ' ';
  319. $inl = $this->makeSelect();
  320. echo sprintf(
  321. '<input type="text" name="acl_w" class="edit" value="%s" />',
  322. ($inl) ? '' : hsc(ltrim($this->who, '@'))
  323. );
  324. echo '<button type="submit">' . $this->getLang('btn_select') . '</button>';
  325. echo '</div>';
  326. echo '<div id="acl__info">';
  327. $this->printInfo();
  328. echo '</div>';
  329. echo '<input type="hidden" name="ns" value="' . hsc($this->ns) . '" />';
  330. echo '<input type="hidden" name="id" value="' . hsc($ID) . '" />';
  331. echo '<input type="hidden" name="do" value="admin" />';
  332. echo '<input type="hidden" name="page" value="acl" />';
  333. echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
  334. echo '</div></form>';
  335. }
  336. /**
  337. * Print info and editor
  338. *
  339. * also loaded via Ajax
  340. */
  341. public function printInfo()
  342. {
  343. global $ID;
  344. if ($this->who) {
  345. $current = $this->getExactPermisson();
  346. // explain current permissions
  347. $this->printExplanation($current);
  348. // load editor
  349. $this->printAclEditor($current);
  350. } else {
  351. echo '<p>';
  352. if ($this->ns) {
  353. printf($this->getLang('p_choose_ns'), hsc($this->ns));
  354. } else {
  355. printf($this->getLang('p_choose_id'), hsc($ID));
  356. }
  357. echo '</p>';
  358. echo $this->locale_xhtml('help');
  359. }
  360. }
  361. /**
  362. * Display the ACL editor
  363. *
  364. * @author Andreas Gohr <andi@splitbrain.org>
  365. */
  366. protected function printAclEditor($current)
  367. {
  368. global $lang;
  369. echo '<fieldset>';
  370. if (is_null($current)) {
  371. echo '<legend>' . $this->getLang('acl_new') . '</legend>';
  372. } else {
  373. echo '<legend>' . $this->getLang('acl_mod') . '</legend>';
  374. }
  375. echo $this->makeCheckboxes($current, empty($this->ns), 'acl');
  376. if (is_null($current)) {
  377. echo '<button type="submit" name="cmd[save]">' . $lang['btn_save'] . '</button>';
  378. } else {
  379. echo '<button type="submit" name="cmd[save]">' . $lang['btn_update'] . '</button>';
  380. echo '<button type="submit" name="cmd[del]">' . $lang['btn_delete'] . '</button>';
  381. }
  382. echo '</fieldset>';
  383. }
  384. /**
  385. * Explain the currently set permissions in plain english/$lang
  386. *
  387. * @author Andreas Gohr <andi@splitbrain.org>
  388. */
  389. protected function printExplanation($current)
  390. {
  391. global $ID;
  392. global $auth;
  393. $who = $this->who;
  394. $ns = $this->ns;
  395. // prepare where to check
  396. if ($ns) {
  397. if ($ns == '*') {
  398. $check = '*';
  399. } else {
  400. $check = $ns . ':*';
  401. }
  402. } else {
  403. $check = $ID;
  404. }
  405. // prepare who to check
  406. if ($who[0] == '@') {
  407. $user = '';
  408. $groups = [ltrim($who, '@')];
  409. } else {
  410. $user = $who;
  411. $info = $auth->getUserData($user);
  412. if ($info === false) {
  413. $groups = [];
  414. } else {
  415. $groups = $info['grps'];
  416. }
  417. }
  418. // check the permissions
  419. $perm = auth_aclcheck($check, $user, $groups);
  420. // build array of named permissions
  421. $names = [];
  422. if ($perm) {
  423. if ($ns) {
  424. if ($perm >= AUTH_DELETE) $names[] = $this->getLang('acl_perm16');
  425. if ($perm >= AUTH_UPLOAD) $names[] = $this->getLang('acl_perm8');
  426. if ($perm >= AUTH_CREATE) $names[] = $this->getLang('acl_perm4');
  427. }
  428. if ($perm >= AUTH_EDIT) $names[] = $this->getLang('acl_perm2');
  429. if ($perm >= AUTH_READ) $names[] = $this->getLang('acl_perm1');
  430. $names = array_reverse($names);
  431. } else {
  432. $names[] = $this->getLang('acl_perm0');
  433. }
  434. // print permission explanation
  435. echo '<p>';
  436. if ($user) {
  437. if ($ns) {
  438. printf($this->getLang('p_user_ns'), hsc($who), hsc($ns), implode(', ', $names));
  439. } else {
  440. printf($this->getLang('p_user_id'), hsc($who), hsc($ID), implode(', ', $names));
  441. }
  442. } elseif ($ns) {
  443. printf($this->getLang('p_group_ns'), hsc(ltrim($who, '@')), hsc($ns), implode(', ', $names));
  444. } else {
  445. printf($this->getLang('p_group_id'), hsc(ltrim($who, '@')), hsc($ID), implode(', ', $names));
  446. }
  447. echo '</p>';
  448. // add note if admin
  449. if ($perm == AUTH_ADMIN) {
  450. echo '<p>' . $this->getLang('p_isadmin') . '</p>';
  451. } elseif (is_null($current)) {
  452. echo '<p>' . $this->getLang('p_inherited') . '</p>';
  453. }
  454. }
  455. /**
  456. * Item formatter for the tree view
  457. *
  458. * User function for html_buildlist()
  459. *
  460. * @author Andreas Gohr <andi@splitbrain.org>
  461. */
  462. public function makeTreeItem($item)
  463. {
  464. $ret = '';
  465. // what to display
  466. if (!empty($item['label'])) {
  467. $base = $item['label'];
  468. } else {
  469. $base = ':' . $item['id'];
  470. $base = substr($base, strrpos($base, ':') + 1);
  471. }
  472. // highlight?
  473. if (($item['type'] == $this->current_item['type'] && $item['id'] == $this->current_item['id'])) {
  474. $cl = ' cur';
  475. } else {
  476. $cl = '';
  477. }
  478. // namespace or page?
  479. if ($item['type'] == 'd') {
  480. if ($item['open']) {
  481. $img = DOKU_BASE . 'lib/images/minus.gif';
  482. $alt = '−';
  483. } else {
  484. $img = DOKU_BASE . 'lib/images/plus.gif';
  485. $alt = '+';
  486. }
  487. $ret .= '<img src="' . $img . '" alt="' . $alt . '" />';
  488. $ret .= '<a href="' .
  489. wl('', $this->getLinkOptions(['ns' => $item['id'], 'sectok' => getSecurityToken()])) .
  490. '" class="idx_dir' . $cl . '">';
  491. $ret .= $base;
  492. $ret .= '</a>';
  493. } else {
  494. $ret .= '<a href="' .
  495. wl('', $this->getLinkOptions(['id' => $item['id'], 'ns' => '', 'sectok' => getSecurityToken()])) .
  496. '" class="wikilink1' . $cl . '">';
  497. $ret .= noNS($item['id']);
  498. $ret .= '</a>';
  499. }
  500. return $ret;
  501. }
  502. /**
  503. * List Item formatter
  504. *
  505. * @param array $item
  506. * @return string
  507. */
  508. public function makeListItem($item)
  509. {
  510. return '<li class="level' . $item['level'] . ' ' .
  511. ($item['open'] ? 'open' : 'closed') . '">';
  512. }
  513. /**
  514. * Get current ACL settings as multidim array
  515. *
  516. * @author Andreas Gohr <andi@splitbrain.org>
  517. */
  518. public function initAclConfig()
  519. {
  520. global $AUTH_ACL;
  521. global $conf;
  522. $acl_config = [];
  523. $usersgroups = [];
  524. // get special users and groups
  525. $this->specials[] = '@ALL';
  526. $this->specials[] = '@' . $conf['defaultgroup'];
  527. if ($conf['manager'] != '!!not set!!') {
  528. $this->specials = array_merge(
  529. $this->specials,
  530. array_map(
  531. 'trim',
  532. explode(',', $conf['manager'])
  533. )
  534. );
  535. }
  536. $this->specials = array_filter($this->specials);
  537. $this->specials = array_unique($this->specials);
  538. Sort::sort($this->specials);
  539. foreach ($AUTH_ACL as $line) {
  540. $line = trim(preg_replace('/#.*$/', '', $line)); //ignore comments
  541. if (!$line) continue;
  542. $acl = preg_split('/[ \t]+/', $line);
  543. //0 is pagename, 1 is user, 2 is acl
  544. $acl[1] = rawurldecode($acl[1]);
  545. $acl_config[$acl[0]][$acl[1]] = $acl[2];
  546. // store non-special users and groups for later selection dialog
  547. $ug = $acl[1];
  548. if (in_array($ug, $this->specials)) continue;
  549. $usersgroups[] = $ug;
  550. }
  551. $usersgroups = array_unique($usersgroups);
  552. Sort::sort($usersgroups);
  553. Sort::ksort($acl_config);
  554. foreach (array_keys($acl_config) as $pagename) {
  555. Sort::ksort($acl_config[$pagename]);
  556. }
  557. $this->acl = $acl_config;
  558. $this->usersgroups = $usersgroups;
  559. }
  560. /**
  561. * Display all currently set permissions in a table
  562. *
  563. * @author Andreas Gohr <andi@splitbrain.org>
  564. */
  565. protected function printAclTable()
  566. {
  567. global $lang;
  568. global $ID;
  569. echo '<form action="' . wl() . '" method="post" accept-charset="utf-8"><div class="no">';
  570. if ($this->ns) {
  571. echo '<input type="hidden" name="ns" value="' . hsc($this->ns) . '" />';
  572. } else {
  573. echo '<input type="hidden" name="id" value="' . hsc($ID) . '" />';
  574. }
  575. echo '<input type="hidden" name="acl_w" value="' . hsc($this->who) . '" />';
  576. echo '<input type="hidden" name="do" value="admin" />';
  577. echo '<input type="hidden" name="page" value="acl" />';
  578. echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
  579. echo '<div class="table">';
  580. echo '<table class="inline">';
  581. echo '<tr>';
  582. echo '<th>' . $this->getLang('where') . '</th>';
  583. echo '<th>' . $this->getLang('who') . '</th>';
  584. echo '<th>' . $this->getLang('perm') . '<sup><a id="fnt__1" class="fn_top" href="#fn__1">1)</a></sup></th>';
  585. echo '<th>' . $lang['btn_delete'] . '</th>';
  586. echo '</tr>';
  587. foreach ($this->acl as $where => $set) {
  588. foreach ($set as $who => $perm) {
  589. echo '<tr>';
  590. echo '<td>';
  591. if (str_ends_with($where, '*')) {
  592. echo '<span class="aclns">' . hsc($where) . '</span>';
  593. $ispage = false;
  594. } else {
  595. echo '<span class="aclpage">' . hsc($where) . '</span>';
  596. $ispage = true;
  597. }
  598. echo '</td>';
  599. echo '<td>';
  600. if ($who[0] == '@') {
  601. echo '<span class="aclgroup">' . hsc($who) . '</span>';
  602. } else {
  603. echo '<span class="acluser">' . hsc($who) . '</span>';
  604. }
  605. echo '</td>';
  606. echo '<td>';
  607. echo $this->makeCheckboxes($perm, $ispage, 'acl[' . $where . '][' . $who . ']');
  608. echo '</td>';
  609. echo '<td class="check">';
  610. echo '<input type="checkbox" name="del[' . hsc($where) . '][]" value="' . hsc($who) . '" />';
  611. echo '</td>';
  612. echo '</tr>';
  613. }
  614. }
  615. echo '<tr>';
  616. echo '<th class="action" colspan="4">';
  617. echo '<button type="submit" name="cmd[update]">' . $lang['btn_update'] . '</button>';
  618. echo '</th>';
  619. echo '</tr>';
  620. echo '</table>';
  621. echo '</div>';
  622. echo '</div></form>';
  623. }
  624. /**
  625. * Returns the permission which were set for exactly the given user/group
  626. * and page/namespace. Returns null if no exact match is available
  627. *
  628. * @author Andreas Gohr <andi@splitbrain.org>
  629. */
  630. protected function getExactPermisson()
  631. {
  632. global $ID;
  633. if ($this->ns) {
  634. if ($this->ns == '*') {
  635. $check = '*';
  636. } else {
  637. $check = $this->ns . ':*';
  638. }
  639. } else {
  640. $check = $ID;
  641. }
  642. if (isset($this->acl[$check][$this->who])) {
  643. return $this->acl[$check][$this->who];
  644. } else {
  645. return null;
  646. }
  647. }
  648. /**
  649. * adds new acl-entry to conf/acl.auth.php
  650. *
  651. * @author Frank Schubert <frank@schokilade.de>
  652. */
  653. public function addOrUpdateACL($acl_scope, $acl_user, $acl_level)
  654. {
  655. global $config_cascade;
  656. // first make sure we won't end up with 2 lines matching this user and scope. See issue #1115
  657. $this->deleteACL($acl_scope, $acl_user);
  658. $acl_user = auth_nameencode($acl_user, true);
  659. // max level for pagenames is edit
  660. if (strpos($acl_scope, '*') === false) {
  661. if ($acl_level > AUTH_EDIT) $acl_level = AUTH_EDIT;
  662. }
  663. $new_acl = "$acl_scope\t$acl_user\t$acl_level\n";
  664. return io_saveFile($config_cascade['acl']['default'], $new_acl, true);
  665. }
  666. /**
  667. * remove acl-entry from conf/acl.auth.php
  668. *
  669. * @author Frank Schubert <frank@schokilade.de>
  670. */
  671. public function deleteACL($acl_scope, $acl_user)
  672. {
  673. global $config_cascade;
  674. $acl_user = auth_nameencode($acl_user, true);
  675. $acl_pattern = '^' . preg_quote($acl_scope, '/') . '[ \t]+' . $acl_user . '[ \t]+[0-8].*$';
  676. return io_deleteFromFile($config_cascade['acl']['default'], "/$acl_pattern/", true);
  677. }
  678. /**
  679. * print the permission radio boxes
  680. *
  681. * @author Frank Schubert <frank@schokilade.de>
  682. * @author Andreas Gohr <andi@splitbrain.org>
  683. */
  684. protected function makeCheckboxes($setperm, $ispage, $name)
  685. {
  686. global $lang;
  687. static $label = 0; //number labels
  688. $ret = '';
  689. if ($ispage && $setperm > AUTH_EDIT) $setperm = AUTH_EDIT;
  690. foreach ([AUTH_NONE, AUTH_READ, AUTH_EDIT, AUTH_CREATE, AUTH_UPLOAD, AUTH_DELETE] as $perm) {
  691. ++$label;
  692. //general checkbox attributes
  693. $atts = [
  694. 'type' => 'radio',
  695. 'id' => 'pbox' . $label,
  696. 'name' => $name,
  697. 'value' => $perm
  698. ];
  699. //dynamic attributes
  700. if (!is_null($setperm) && $setperm == $perm) $atts['checked'] = 'checked';
  701. if ($ispage && $perm > AUTH_EDIT) {
  702. $atts['disabled'] = 'disabled';
  703. $class = ' class="disabled"';
  704. } else {
  705. $class = '';
  706. }
  707. //build code
  708. $ret .= '<label for="pbox' . $label . '"' . $class . '>';
  709. $ret .= '<input ' . buildAttributes($atts) . ' />&#160;';
  710. $ret .= $this->getLang('acl_perm' . $perm);
  711. $ret .= '</label>';
  712. }
  713. return $ret;
  714. }
  715. /**
  716. * Print a user/group selector (reusing already used users and groups)
  717. *
  718. * @author Andreas Gohr <andi@splitbrain.org>
  719. */
  720. protected function makeSelect()
  721. {
  722. $inlist = false;
  723. $usel = '';
  724. $gsel = '';
  725. if (
  726. $this->who &&
  727. !in_array($this->who, $this->usersgroups) &&
  728. !in_array($this->who, $this->specials)
  729. ) {
  730. if ($this->who[0] == '@') {
  731. $gsel = ' selected="selected"';
  732. } else {
  733. $usel = ' selected="selected"';
  734. }
  735. } else {
  736. $inlist = true;
  737. }
  738. echo '<select name="acl_t" class="edit">';
  739. echo ' <option value="__g__" class="aclgroup"' . $gsel . '>' . $this->getLang('acl_group') . '</option>';
  740. echo ' <option value="__u__" class="acluser"' . $usel . '>' . $this->getLang('acl_user') . '</option>';
  741. if (!empty($this->specials)) {
  742. echo ' <optgroup label="&#160;">';
  743. foreach ($this->specials as $ug) {
  744. if ($ug == $this->who) {
  745. $sel = ' selected="selected"';
  746. $inlist = true;
  747. } else {
  748. $sel = '';
  749. }
  750. if ($ug[0] == '@') {
  751. echo ' <option value="' . hsc($ug) . '" class="aclgroup"' . $sel . '>' . hsc($ug) . '</option>';
  752. } else {
  753. echo ' <option value="' . hsc($ug) . '" class="acluser"' . $sel . '>' . hsc($ug) . '</option>';
  754. }
  755. }
  756. echo ' </optgroup>';
  757. }
  758. if (!empty($this->usersgroups)) {
  759. echo ' <optgroup label="&#160;">';
  760. foreach ($this->usersgroups as $ug) {
  761. if ($ug == $this->who) {
  762. $sel = ' selected="selected"';
  763. $inlist = true;
  764. } else {
  765. $sel = '';
  766. }
  767. if ($ug[0] == '@') {
  768. echo ' <option value="' . hsc($ug) . '" class="aclgroup"' . $sel . '>' . hsc($ug) . '</option>';
  769. } else {
  770. echo ' <option value="' . hsc($ug) . '" class="acluser"' . $sel . '>' . hsc($ug) . '</option>';
  771. }
  772. }
  773. echo ' </optgroup>';
  774. }
  775. echo '</select>';
  776. return $inlist;
  777. }
  778. }