xref: /dokuwiki/lib/plugins/acl/admin.php (revision c3f4fb63f0c18cc70b32c658198dff3cc105c682)
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
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-03-15',
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        foreach($AUTH_ACL as $line){
464            $line = trim(preg_replace('/#.*$/','',$line)); //ignore comments
465            if(!$line) continue;
466
467            $acl = preg_split('/\s+/',$line);
468            //0 is pagename, 1 is user, 2 is acl
469
470            $acl[1] = rawurldecode($acl[1]);
471            $acl_config[$acl[0]][$acl[1]] = $acl[2];
472
473            // store non-special users and groups for later selection dialog
474            $ug = $acl[1];
475            if($ug == '@ALL') continue;
476            if($ug == $conf['superuser']) continue;
477            if($ug == $conf['manager']) continue;
478            $usersgroups[] = $ug;
479        }
480
481        $usersgroups = array_unique($usersgroups);
482        sort($usersgroups);
483        uksort($acl_config,array($this,'_sort_names'));
484
485        $this->acl = $acl_config;
486        $this->usersgroups = $usersgroups;
487    }
488
489    /**
490     * Custom function to sort the ACLs by namespace names
491     *
492     * @todo This maybe could be improved to resemble the real tree structure?
493     */
494    function _sort_names($a,$b){
495        $ca = substr_count($a,':');
496        $cb = substr_count($b,':');
497        if($ca < $cb){
498            return -1;
499        }elseif($ca > $cb){
500            return 1;
501        }else{
502            return strcmp($a,$b);
503        }
504    }
505
506    /**
507     * Display all currently set permissions in a table
508     *
509     * @author Andreas Gohr <andi@splitbrain.org>
510     */
511    function _html_table(){
512        global $lang;
513        global $ID;
514
515        echo '<form action="'.wl().'" method="post" accept-charset="utf-8"><div class="no">'.NL;
516        if($this->ns){
517            echo '<input type="hidden" name="ns" value="'.hsc($this->ns).'" />'.NL;
518        }else{
519            echo '<input type="hidden" name="id" value="'.hsc($ID).'" />'.NL;
520        }
521        echo '<input type="hidden" name="acl_w" value="'.hsc($this->who).'" />'.NL;
522        echo '<input type="hidden" name="do" value="admin" />'.NL;
523        echo '<input type="hidden" name="page" value="acl" />'.NL;
524        echo '<table class="inline">';
525        echo '<tr>';
526        echo '<th>'.$this->getLang('where').'</th>';
527        echo '<th>'.$this->getLang('who').'</th>';
528        echo '<th>'.$this->getLang('perm').'</th>';
529        echo '<th>'.$lang['btn_delete'].'</th>';
530        echo '</tr>';
531        foreach($this->acl as $where => $set){
532            foreach($set as $who => $perm){
533                echo '<tr>';
534                echo '<td>';
535                if(substr($where,-1) == '*'){
536                    echo '<span class="aclns">'.hsc($where).'</span>';
537                    $ispage = false;
538                }else{
539                    echo '<span class="aclpage">'.hsc($where).'</span>';
540                    $ispage = true;
541                }
542                echo '</td>';
543
544                echo '<td>';
545                if($who{0} == '@'){
546                    echo '<span class="aclgroup">'.hsc($who).'</span>';
547                }else{
548                    echo '<span class="acluser">'.hsc($who).'</span>';
549                }
550                echo '</td>';
551
552                echo '<td>';
553                echo $this->_html_checkboxes($perm,$ispage,'acl['.hsc($where).']['.hsc($who).']');
554                echo '</td>';
555
556                echo '<td align="center">';
557                echo '<input type="checkbox" name="del['.hsc($where).']" value="'.hsc($who).'" />';
558                echo '</td>';
559                echo '</tr>';
560            }
561        }
562
563        echo '<tr>';
564        echo '<th align="right" colspan="4">';
565        echo '<input type="submit" value="'.$lang['btn_update'].'" name="cmd[update]" class="button" />';
566        echo '</th>';
567        echo '</tr>';
568        echo '</table>';
569        echo '</div></form>'.NL;
570    }
571
572
573    /**
574     * Returns the permission which were set for exactly the given user/group
575     * and page/namespace. Returns null if no exact match is available
576     *
577     * @author Andreas Gohr <andi@splitbrain.org>
578     */
579    function _get_exact_perm(){
580        global $ID;
581        if($this->ns){
582            if($this->ns == '*'){
583                $check = '*';
584            }else{
585                $check = $this->ns.':*';
586            }
587        }else{
588            $check = $ID;
589        }
590
591        if(isset($this->acl[$check][auth_nameencode($this->who,true)])){
592            return $this->acl[$check][auth_nameencode($this->who,true)];
593        }else{
594            return null;
595        }
596    }
597
598    /**
599     * adds new acl-entry to conf/acl.auth.php
600     *
601     * @author  Frank Schubert <frank@schokilade.de>
602     */
603    function _acl_add($acl_scope, $acl_user, $acl_level){
604        $acl_config = file_get_contents(DOKU_CONF.'acl.auth.php');
605        $acl_user = auth_nameencode($acl_user,true);
606
607        // max level for pagenames is edit
608        if(strpos($acl_scope,'*') === false) {
609            if($acl_level > AUTH_EDIT) $acl_level = AUTH_EDIT;
610        }
611
612
613        $new_acl = "$acl_scope\t$acl_user\t$acl_level\n";
614
615        $new_config = $acl_config.$new_acl;
616
617        return io_saveFile(DOKU_CONF.'acl.auth.php', $new_config);
618    }
619
620    /**
621     * remove acl-entry from conf/acl.auth.php
622     *
623     * @author  Frank Schubert <frank@schokilade.de>
624     */
625    function _acl_del($acl_scope, $acl_user){
626        $acl_config = file(DOKU_CONF.'acl.auth.php');
627        $acl_user = auth_nameencode($acl_user,true);
628
629        $acl_pattern = '^'.preg_quote($acl_scope,'/').'\s+'.$acl_user.'\s+[0-8].*$';
630
631        // save all non!-matching
632        $new_config = preg_grep("/$acl_pattern/", $acl_config, PREG_GREP_INVERT);
633
634        return io_saveFile(DOKU_CONF.'acl.auth.php', join('',$new_config));
635    }
636
637    /**
638     * print the permission radio boxes
639     *
640     * @author  Frank Schubert <frank@schokilade.de>
641     * @author  Andreas Gohr <andi@splitbrain.org>
642     */
643    function _html_checkboxes($setperm,$ispage,$name){
644        global $lang;
645
646        static $label = 0; //number labels
647        $ret = '';
648
649        if($ispage && $setperm > AUTH_EDIT) $perm = AUTH_EDIT;
650
651        foreach(array(AUTH_NONE,AUTH_READ,AUTH_EDIT,AUTH_CREATE,AUTH_UPLOAD,AUTH_DELETE) as $perm){
652            $label += 1;
653
654            //general checkbox attributes
655            $atts = array( 'type'  => 'radio',
656                           'id'    => 'pbox'.$label,
657                           'name'  => $name,
658                           'value' => $perm );
659            //dynamic attributes
660            if(!is_null($setperm) && $setperm == $perm) $atts['checked']  = 'checked';
661            if($ispage && $perm > AUTH_EDIT){
662                $atts['disabled'] = 'disabled';
663                $class = ' class="disabled"';
664            }else{
665                $class = '';
666            }
667
668            //build code
669            $ret .= '<label for="pbox'.$label.'" title="'.$this->getLang('acl_perm'.$perm).'"'.$class.'>';
670            $ret .= '<input '.html_attbuild($atts).' />&nbsp;';
671            $ret .= $this->getLang('acl_perm'.$perm);
672            $ret .= '</label>'.NL;
673        }
674        return $ret;
675    }
676
677    /**
678     * Print a user/group selector (reusing already used users and groups)
679     *
680     * @author  Andreas Gohr <andi@splitbrain.org>
681     */
682    function _html_select(){
683        global $conf;
684        $inlist = false;
685
686        $specials = array('@ALL','@'.$conf['defaultgroup']);
687        if($conf['manager'] && $conf['manager'] != '!!not set!!') $specials[] = $conf['manager'];
688
689
690        if($this->who &&
691           !in_array($this->who,$this->usersgroups) &&
692           !in_array($this->who,$specials)){
693
694            if($this->who{0} == '@'){
695                $gsel = ' selected="selected"';
696            }else{
697                $usel   = ' selected="selected"';
698            }
699        }else{
700            $usel = '';
701            $gsel = '';
702            $inlist = true;
703        }
704
705
706        echo '<select name="acl_t" class="edit">'.NL;
707        echo '  <option value="__g__" class="aclgroup"'.$gsel.'>'.$this->getLang('acl_group').':</option>'.NL;
708        echo '  <option value="__u__"  class="acluser"'.$usel.'>'.$this->getLang('acl_user').':</option>'.NL;
709        echo '  <optgroup label="&nbsp;">'.NL;
710        foreach($specials as $ug){
711            if($ug == $this->who){
712                $sel    = ' selected="selected"';
713                $inlist = true;
714            }else{
715                $sel = '';
716            }
717
718            if($ug{0} == '@'){
719                    echo '  <option value="'.hsc($ug).'" class="aclgroup"'.$sel.'>'.hsc($ug).'</option>'.NL;
720            }else{
721                    echo '  <option value="'.hsc($ug).'" class="acluser"'.$sel.'>'.hsc($ug).'</option>'.NL;
722            }
723        }
724        echo '  </optgroup>'.NL;
725        echo '  <optgroup label="&nbsp;">'.NL;
726        foreach($this->usersgroups as $ug){
727            if($ug == $this->who){
728                $sel    = ' selected="selected"';
729                $inlist = true;
730            }else{
731                $sel = '';
732            }
733
734            if($ug{0} == '@'){
735                    echo '  <option value="'.hsc($ug).'" class="aclgroup"'.$sel.'>'.hsc($ug).'</option>'.NL;
736            }else{
737                    echo '  <option value="'.hsc($ug).'" class="acluser"'.$sel.'>'.hsc($ug).'</option>'.NL;
738            }
739        }
740        echo '  </optgroup>'.NL;
741        echo '</select>'.NL;
742        return $inlist;
743    }
744}
745