#!/usr/bin/perl

use CGI qw("param");
require "./wiz_resources";
require "../config/main.pl";
require "../config/variables.pl";
require "../write_back.pl";

$data_path = "$main_path/default/data";
$username_file = "$main_path/$script_location";

open (USERFILE, "<$username_file/.htpass/.controller0") or &error("Unable to open the Main Password file");
$line1=<USERFILE>;
chomp $line1;
close(USERFILE);

($user_name, $enc_password) = split(/:/,$line1,2);

$action=param("action");
$action="display" if ($action ne "login");

if ($action eq "display") {
  header("Logging In","","login");
  headline("Log in","","","You must enter your username and password for this system into the boxes below in order to progress further.  These should have been provided to you by $company; if you haven't got any, please contact <a href=\"mailto:$support_email\">$company support</a>");
  display_form();
print <<EOF;
<b>Please note:</b> your web browser must have the "cookies" feature enabled to use this site.  This will retain your details on your machine so that you don't need to keep telling us who you are when we need to check if you're allowed to work on a particular site. That could get really annoying for every page change!
<p>
<font color="#ff0000">If you are editing your site from an insecure computer</font> (a shared machine, a schools machine that the students have access to, a machine in a cyber caf&eacute; and so on) please use the <b>log out</b> function as soon as you've finished updating your site.
EOF
} else {
  $loginname=param("username");
  $password=param("password");
  if (($loginname eq "") || ($password eq "")){
      header("Unsuccessful login","wipe","login");
      headline("Error: unsuccessful login","red","","You did not supply both pieces of information needed to log in to the system. Please try again.");
      display_form();
  } else {
    get_user_details($loginname);
    if ($password eq $pass) {
      header("Logged in","in","login");
      headline("Logged in!","","","You have successfully logged in to the system; please <a href=\"$default_cgi/edit\">click here to continue</a>");
    } else {
      header("Unsuccessful login","wipe","login");
      headline("Error: unsuccessful login","red","","Unfortunately you have not supplied the correct details to log in to the system.  Please try again.");
      display_form();
    }
  }
}

footer();

sub display_form {
print <<EOF;
<p align="center">
<form method="POST" action="$default_cgi/login">
<hr>
<input type="hidden" name="action" value="login">
<table cellpadding="0" cellspacing="0" border="0">
<tr><td align="right">Username:&nbsp;</td><td><input type="hidden" name="username" value="$user_name" size="32" maxlength="32">$user_name</td></tr>
<tr><td align="right">Password:&nbsp;</td><td><input type="hidden" name="password" value="$alias_password" size="32" maxlength="32">*********</td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="log in"></td></tr></table>
<hr>
</form>
</p><p>
EOF
}
