[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/lib/plugins/acl/ -> admin.php (source)

   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
  11  if(!defined('DOKU_INC')) die();
  12  
  13  if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
  14  require_once (DOKU_PLUGIN.'admin.php');
  15  
  16  /**
  17   * All DokuWiki plugins to extend the admin function
  18   * need to inherit from this class
  19   */
  20  class 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>';