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