<?php if (!defined('PmWiki')) exit();

/*
#===============================================================================
# sessionauthform.php
#===============================================================================
# Copyright (c) 2004 Thomas Weibel. All rights reserved.
#
# This script allows seamless embedding of authentication into your Wiki
#
# To install the script, add the following to your config.php:
#
#  include_once("local/sessionauthform.php");
#
# Configuration variables:
#
# $LoginPage 
#   Defines the Wiki page to use as login form. This page must not exist! It is 
#   created the first time you run the sessionauthform.php script.
#   Default value is Main.Login.
#
# $AuthFormFmt 
#   Defines the look of the authentication form.
#
# These variables can be set in config.php.
#
# This script is based on sessionauth.php which is part of the PmWiki 
# distribution; 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.
#===============================================================================
*/

$AuthFunction = 'SessionAuthForm';

SDV($AuthFormFmt,"<p>&nbsp;</p>
<p>&nbsp;</p>

<form name='authform' method='post'>
<fieldset>
<legend>Login</legend>

<br />

<label for='author' title='Enter your author name here' class='left'>Author</label>
<input id='author' name='author' value='$author' /><br />

<label for='password' title='Enter your password here' class='left'>Password</label>
<input type='password' id='password' name='authpw' value='' /><br />

<div class='right'>
  <input type='submit' value='OK' />
  <input type='reset' value='Reset' />
</div>

</fieldset>
</form>

<p>&nbsp;</p>
<p>&nbsp;</p>");

SDV($LoginPage, "Main.Login");

Markup('authform','directives','/\\(:authform:\\)/', "$AuthFormFmt");

function SessionAuthForm($pagename,$level,$authprompt=true) {
  global $DefaultPasswords,$AllowPassword,$GroupAttributesFmt,$SessionAuthFmt,
    $HTMLStartFmt,$HTMLEndFmt,$LoginPage;
  SDV($GroupAttributesFmt,'$Group/GroupAttributes');
  SDV($AllowPassword,'nopass');
  $page = ReadPage($pagename);
  if (!$page) { return false; }
  $passwd = @$page["passwd$level"];
  if ($passwd=="") { 
    $grouppg = ReadPage(FmtPageName($GroupAttributesFmt,$pagename));
    $passwd = @$grouppg["passwd$level"];
    if ($passwd=='') $passwd = @$DefaultPasswords[$level];
    if ($passwd=='') $passwd = @$page["passwdread"];
    if ($passwd=='') $passwd = @$grouppg["passwdread"];
    if ($passwd=='') $passwd = @$DefaultPasswords['read'];
  }
  if ($passwd=='') return $page;
  if (crypt($AllowPassword,$passwd)==$passwd) return $page;
  @session_start();
  if (@$_POST['authpw']) @$_SESSION['authpw'][$_POST['authpw']]++;
  $authpw = array_keys((array)@$_SESSION['authpw']);
  foreach (array_merge((array)$DefaultPasswords['admin'],(array)$passwd) 
      as $pwchal)
    foreach($authpw as $pwresp)
      if (@crypt($pwresp,$pwchal)==$pwchal) return $page;
  if (!$authprompt) return false;

  if (!PageExists($LoginPage)) {
    $page['text'] = '(:authform:)';
    WritePage($LoginPage, $page);
  }
  HandleBrowse($LoginPage);
  exit;
}

?>