admin.php 28 KB

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