<?php 

include 'ipp.php';

if (($_SERVER[REQUEST_METHOD] != 'POST') && ($_SERVER[REQUEST_METHOD] != 'GET')) {
  menu ('ipp.menu.dat', 'Login', 'ipp.css', '', '');
  echo "Invalid Client Request<br>\n";
  menu_end ();
  exit ();
}

if ($_SERVER[REQUEST_METHOD] == 'GET') { 

  $ID = checkID ();

  menu ('ipp.menu.dat', 'Login', 'ipp.css', $ID['link'], $ID['proj']);

  $pass = $ID['pass'];
  $name = $ID['name'];
  echo "<p>You are currently logged in as $name.<br>\n";
  echo "Click here to log out.\n";
  echo "<form action=\"Login.php\" method=\"POST\">\n";
  echo "<input type=\"hidden\" name=\"pass\" value=\"$pass\">\n";
  echo "<input type=\"submit\" name=\"logout\" value=\"logout\">\n";
  echo "</form></p>\n\n";
  
  menu_end ();
  exit ();
} 

if ($_SERVER[REQUEST_METHOD] == 'POST') { 
  if (key_exists (login, $_POST)) {

    $ID = checkLogin ();

    menu ('ipp.menu.dat', 'Login', 'ipp.css', $ID['link'], $ID['proj']);
    echo "Login Accepted\n";
    menu_end();
    exit ();
  }

  if (key_exists (logout, $_POST)) {
    menu ('ipp.menu.dat', 'Login', 'ipp.css', '', '');
    echo "You are now logged out<br>\n";
    logintext ();
    loginform ();
    menu_end ();
    exit ();
  }

  // missing a valid POST state (login or logout)
  menu ('ipp.menu.dat', 'Login', 'ipp.css', '', '');
  echo "Invalid Client Post Request<br>\n";
  foreach ($_POST as $key => $value) {
    echo "$key : $value<br>\n";
  }
  menu_end ();
  exit ();
}

?>
