<?php require_once('Connections/TOM.php'); ?>
<?php
if (isset($_POST['password'])) { $_POST['password'] = sha1($_POST['password']); }
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
global $TOM;
  $theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($theValue) : mysqli_real_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_rsUsers = "-1";
if (isset($_POST['userid'])) {
  $colname_rsUsers = $_POST['userid'];
}
mysqli_select_db($TOM, $database_TOM);
$query_rsUsers = sprintf("SELECT * FROM users WHERE userid = %s", GetSQLValueString($colname_rsUsers, "int"));
$rsUsers = mysqli_query($TOM, $query_rsUsers) or die(mysqli_error($TOM));
$row_rsUsers = mysqli_fetch_assoc($rsUsers);
$totalRows_rsUsers = mysqli_num_rows($rsUsers);
?><?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['username'])) {
  $loginUsername=$_POST['username'];
  $password=$_POST['password'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "success.php";
  $MM_redirectLoginFailed = "loginfailed.php";
  $MM_redirecttoReferrer = false;
  mysqli_select_db($TOM, $database_TOM);
  
  $LoginRS__query=sprintf("SELECT username, password FROM users WHERE username=%s AND password=%s",
    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
   
  $LoginRS = mysqli_query($TOM, $LoginRS__query) or die(mysqli_error($TOM));
  $loginFoundUser = mysqli_num_rows($LoginRS);
  if ($loginFoundUser) {
     $loginStrGroup = "";
    
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	      

    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link href="admin.css" rel="stylesheet" type="text/css" />
</head>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-2922014-6");
pageTracker._trackPageview();
} catch(err) {}</script>
<body>
<h1 align="left">Welcome fellow cachers to GC1BVEA </h1>
<h1 align="left"><img src="publickey.jpg" alt="public key" width="178" height="241" class="floatRight" /></h1>
<form ACTION="<?php echo $loginFormAction; ?>" id="login" name="login" method="POST">
  <p>
    <label for="textfield">Geocaching Login Name:</label>
    <br />
    <input name="username" type="text" id="username" />
  </p>
  <p>
    <label for="label">Password</label>
    <br />
    <input name="password" type="password" id="label" />
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit" />
  </p>
</form>
</body>

</html>
<?php
mysqli_free_result($rsUsers);
?>
