<?php if (!defined('PmWiki')) exit();
/*  Copyright 2005 Patrick R. Michaud (pmichaud@pobox.com)
    This file is part of PmWiki; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.  See pmwiki.php for full details.

    This script adds "fmt=authtable" to the (:pagelist:) directive,
    to display the passwords and authorizations set for individual
    pages.
*/

$RecipeInfo['AuthTable']['Version'] = '1.2';

SDV($FPLFunctions['authtable'], 'FPLAuthList');

$HTMLStylesFmt['fplauthtable'] = '
  td.auth-none { background: #ffffff; text-align: center; }
  td.auth-site { background: #d0ffd0; text-align: center; }
  td.auth-group { background: #80e880; text-align: center; }
  td.auth-page { background: #00c000; text-align: center; }
';

function FPLAuthList($pagename, &$matches, $opt) {
  global $FPLAuthListOpt, $DefaultPasswords, $PCache, $AllowPassword;
  SDVA($FPLAuthListOpt, array('readf'=>'1000', 'order' => 'name',
    'attrlist' => 
      implode(',', array_diff(array_keys($DefaultPasswords), array('admin')))));
  $opt = array_merge($FPLAuthListOpt, $opt);
  $matches = MakePageList($pagename, $opt, 0);
  if (@$opt['count']) array_splice($matches, $opt['count']);
  $attrlist = preg_split('/[\s,]+/', $opt['attrlist']);
  $out = array();
  $out[] = "<table class='fplauthtable' border='1' cellspacing='0'>";
  $out[] = "\n  <tr><th>Page Name</th>";
  foreach($attrlist as $a) $out[] = "<th>$a</th>";
  $out[] = "</tr>";
  foreach($matches as $pn) {
    if (!PageExists($pn)) continue;
    $out[] = 
      FmtPageName("\n  <tr><td><a href='\$PageUrl?action=attr'>\$FullName</a></td>", $pn);
    foreach($attrlist as $a) {
      $pa = @$PCache[$pn]['=pwsource'][$a];
      if ($pa == '') { $class = "auth-none"; $pa = '---'; }
      elseif (strncmp($pa, 'cascade:', 8) == 0) {
        $pa = '('.substr($pa, 8).')';
        $class = 'auth-none';
      } else {
        $class = "auth-$pa";
        $pw = $PCache[$pn]['=passwd'][$a][0];
        if (crypt($AllowPassword, $pw) == $pw) $pa = 'NOPASS';
      }
      $out[] = "\n    <td class='$class'>$pa</td>";
    }
    $out[] = "</tr>";
  }
  $out[] = "</table>";
  return implode('', $out);
}
$EnablePostAttrClearSession = 0;