#!/usr/bin/perl
# session_login.cgi
# Display the login form used in session login mode

require 5.004;
require "./preferences.pl";
require "$Libdir/util.pl";
require $Cgilib;

&ReadParse();

open(CONF, "<$Miniconfig");
$ses = join("",grep(/^session=/,<CONF>));
close(CONF);
unless ($ses =~ /^session=1/) {
  print &PrintHeader();
  &Error("Logout capability has not been enabled. See Settings/Web Server/Authentication");
}

print "Set-Cookie: user=x; path=/\n" if ($in{'logout'});
$host = $ENV{'HTTP_HOST'}; $host =~ s/:\d+//g;
$msg  = "";

if (defined($in{'failed'})) { $msg = "<h3>Login failed. Please try again.</h3><p>\n"; }
elsif ($in{'logout'})       { $msg = "<h3>Logout successful. Use the form below to login again.</h3><p>\n"; }
elsif ($in{'timed_out'})    { $msg = "<h3>".'Session timed out after '.int($in{'timed_out'}/60)." minutes of activity.</h3><p>\n"; }

print <<EOF;
Content-type: text/html

<html>
<head>
 <title>nPulse Login</title>
</head>

<body bgcolor="#000000" text="#ffffff"><center>$msg
<form method="post" action="/session_login.cgi">
 <input type=hidden name=page value="$in{'page'}">
 <table border width="40%">
  <tr>
   <td bgcolor="#333399"><b>Please Login to nPulse</b></td>
  </tr>
  <tr>
   <td bgcolor="#cccccc">
    <table cellpadding=3>
     <tr>
      <td colspan=2 align=center><font color="#000000">You must enter a username and password to login to the nPulse server on $host</font></td>
     </tr>
     <tr>
      <td align="right"><font color="#000000"><b>Username</b></font></td>
      <td><input name=user size=20 value="$in{'failed'}"></td>
     </tr>
     <tr>
      <td align="right"><font color="#000000"><b>Password</b></font></td>
      <td><input name=pass size=20 type=password></td>
     </tr>
     <tr>
      <td colspan=2 align=center><input type=submit value='Login'><input type=reset value='Clear'></td>
     </tr>
    </table>
   </td>
  </tr>
 </table>
</form>
</center>
</body>
</html>
EOF
