<?php
session_start();

if(isset($_GET["log_out"])){
  unset($_SESSION["logged_in"]);
  header('location: /login.php');
  exit;
}

$logins[0]["user"] = "stc_iweb";
$logins[0]["pass"] = "Imms!dataiweb";
$logins[0]["access"] = "main/iweb";
$logins[0]["redirect"] = "main/iweb/iweb.html";

$logins[1]["user"] = "stc_voms";
$logins[1]["pass"] = "Imms!vomsdata";
$logins[1]["access"] = "main/voms";
$logins[1]["redirect"] = "main/voms/voms.html";

$logins[2]["user"] = "hl7";
$logins[2]["pass"] = "hl7!g4tew4y";
$logins[2]["access"] = "main/phchub";
$logins[2]["redirect"] = "main/phchub/phchub.html";

$logins[3]["user"] = "library";
$logins[3]["pass"] = "ish!STC";
$logins[3]["access"] = "main/internal";
$logins[3]["redirect"] = "main/internal/library.html";

$logins[4]["user"] = "iweb_reports";
$logins[4]["pass"] = "rpt!STC";
$logins[4]["access"] = "main/reports";
$logins[4]["redirect"] = "main/reports/iweb/index.htm";

// No need to edit below, except the errors

if(isset($_POST['submit'])){ //is the form submitted?
  if(empty($_POST['user']) || empty($_POST['pass'])){
    echo "Please enter your username and password.";
    exit;
  } //check for empty user name or password
  $is_logged = false; //this is part of the process to see if they have a correct password or not, set to false right here to say no right pass... (will change later)
  foreach($logins as $id => $login){
    $user = $_POST;
    if(($user["user"] == $login["user"]) && ($user["pass"] == $login["pass"])) {
      $is_logged = true;
      $_SESSION["logged_in"] = true;
      $_SESSION["user_id"] = $id;
      $url = $login["redirect"];
    }
  }
  if(!$is_logged){ echo "These login credentials do not match any valid accounts. Please try again."; } //if none of the $logins arrays matched the input, give an error
}
if ($_SESSION["logged_in"]) {
  if (!$url) {
    $url = substr($_SERVER["REQUEST_URI"], 1);
    $url = strstr($url, '?', true) ?: $url;
    if ($url == 'login.php') {
      $url = $logins[$_SESSION["user_id"]]["redirect"];
    }
    if ($logins[$_SESSION["user_id"]]["access"] != substr($url, 0, strlen($logins[$_SESSION["user_id"]]["access"]))) {
      header("HTTP/1.0 404 Not Found");
      readfile("custom_404.htm");
      exit;
    }
    if (!file_exists($url)) {
      header("HTTP/1.0 404 Not Found");
      readfile("custom_404.htm").
      exit;
    }
  } else {
    header("Location: {$logins[$_SESSION["user_id"]]["redirect"]}");
    exit;
  }
  $finfo = new finfo(FILEINFO_MIME);
  $content_type = $finfo->file($url);

  // there is a bug with finfo_file();
  // https://bugs.php.net/bug.php?id=53035
  // hard coding the correct mime types for presently needed file extensions
  $extension = pathinfo($url, PATHINFO_EXTENSION);
  switch($extension){
    case 'css':
      $content_type = 'text/css';
    break;
    case 'js':
      $content_type = 'application/javascript';
    default:
    break;
  }

  header('Content-Type: '.$content_type);
  readfile($url);
  exit;
} else {
?>

<style type="text/css">
	body {
		font-family: Verdana, 'Open Sans', Arial, sans-serif;
		background-color: #ffffff;
	}
	h1 {
		color: #323C66;
		text-align: center;
	}
	table {
		border-radius: 25px;
		border: 2px solid #323C66;
		padding: 20px;
		margin-left:auto;
    		margin-right:auto;
	}
	.container {
		margin: 25px auto;
		position: relative;
	}
	#loginarea form input[type="text"],
	#loginarea form input[type="password"] {
		border: 1px solid #323C66;
		font-size: 12pt;
	}
	#loginarea form input[type="text"]:focus,
	#loginarea form input[type="password"]:focus {
	-webkit-box-shadow: 0 0 2px #323C66 inset;
	-moz-box-shadow: 0 0 2px #323C66 inset;
	-ms-box-shadow: 0 0 2px #323C66 inset;
	-o-box-shadow: 0 0 2px #323C66 inset;
	box-shadow: 0 0 2px #323C66 inset;
	background-color: #fff;
	border: 1px solid #323C66;
	outline: none;
	}
	#loginarea form input[type="submit"] {
		color: #000000;
		cursor: pointer;
		font-size: 13pt;
	}
</style>
<div class="container">
<section id="loginarea">
<h1>Log in to STC's Documentation Portal</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table style="text-align:center;"><tr><td style="line-height:150%;text-align:right;">
<p>Username:&#160;</p>
<p>Password:&#160;</p>
</td>
<td style="line-height:150%;text-align:right;">
<p><input type="text" id="username" required="" name="user" autofocus/></p>
<p><input type="password" id="password" required="" name="pass" /></p>
</td></tr>
<tr><td colspan="2">
<p><br /><input type="submit" name="submit" value="Log in" /></p>
</td></tr>
</table>
<p style="text-align:center;"><img src="/stc_logo.png" alt="STC logo" border="0" /></p>
<p style="text-align:center;font-size:80%;"><a href="index.htm">Return to Documentation Portal Home Page</p>
</section>
</div>
</form>
<?php
}
