<?php require_once('Connections/TOM.php'); ?>
<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
}

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
  //to fully log out a visitor we need to clear the session varialbles
  $_SESSION['MM_Username'] = NULL;
  $_SESSION['MM_UserGroup'] = NULL;
  $_SESSION['PrevUrl'] = NULL;
  unset($_SESSION['MM_Username']);
  unset($_SESSION['MM_UserGroup']);
  unset($_SESSION['PrevUrl']);
	
  $logoutGoTo = "index.php";
  if ($logoutGoTo) {
    header("Location: $logoutGoTo");
    exit;
  }
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_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;
}
}

$maxRows_InitialInfo = 1;
$pageNum_InitialInfo = 0;
if (isset($_GET['pageNum_InitialInfo'])) {
  $pageNum_InitialInfo = $_GET['pageNum_InitialInfo'];
}
$startRow_InitialInfo = $pageNum_InitialInfo * $maxRows_InitialInfo;

$colname_InitialInfo = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_InitialInfo = $_SESSION['MM_Username'];
}
$colname_InitialInfo = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_InitialInfo = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_TOM, $TOM);
$query_InitialInfo = sprintf("SELECT * FROM users WHERE username = '%s'", $colname_InitialInfo);
$InitialInfo = mysql_query($query_InitialInfo, $TOM) or die(mysql_error());
$row_InitialInfo = mysql_fetch_assoc($InitialInfo);
$totalRows_InitialInfo = mysql_num_rows($InitialInfo);

$colname_PrivateKey = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_PrivateKey = $_SESSION['MM_Username'];
}
$colname_PrivateKey = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_PrivateKey = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_TOM, $TOM);
$query_PrivateKey = sprintf("SELECT RSA.modulus, RSA.pubkey, RSA.privkey, RSA.puzzle_ID, users.username, users.puzzlenum FROM RSA, users WHERE users.username = '%s' AND RSA.puzzle_ID = users.puzzlenum", $colname_PrivateKey);
$PrivateKey = mysql_query($query_PrivateKey, $TOM) or die(mysql_error());
$row_PrivateKey = mysql_fetch_assoc($PrivateKey);
$totalRows_PrivateKey = mysql_num_rows($PrivateKey);

$colname_Success = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_Success = $_SESSION['MM_Username'];
}

$maxRows_userinfo = 1;
$pageNum_userinfo = 0;
if (isset($_GET['pageNum_userinfo'])) {
  $pageNum_userinfo = $_GET['pageNum_userinfo'];
}
$startRow_userinfo = $pageNum_userinfo * $maxRows_userinfo;

$colname_userinfo = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_userinfo = $_SESSION['MM_Username'];
}
?><!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>The Problem</title>
<link href="admin.css" rel="stylesheet" type="text/css" />
</head>

<body>
<h1>Here is your starting information:</h1>
<h2>Modulus: <?php echo $row_PrivateKey['modulus']; ?> </h2>
<h2>Public Key:  <?php echo $row_PrivateKey['pubkey']; ?></h2>
<p>&nbsp;</p>
<h5>Please enter your calculated private key:</h5>
<form id="private" name="private" method="post" action="">
  <p>
    <label for="private">Calculated Private Key:</label>
    <br />
    <input type="text" name="private" id="private" />
  </p>
  <p>
    <input name="doKeycheck" type="submit" id="doKeycheck" value="Submit" />
  </p>
</form>	
<?php
	$to = 'ppporch@theporch.com; ppporch@yahoo.com; ppporch@gmail.com';
	$subject = 'GC1D4JV Solved by '.$colname_userinfo;
	$message = 'From: GC1D4JV@theporch.com'."\n\n";
	$message .= $colname_userinfo.' Solved the puzzle and is eligable to log the cache.';
?>
<?php
if (isset($_POST['private'])) {
if (trim($row_PrivateKey['privkey']) == trim($_POST['private'])) {
	echo '<ul>';
	echo "<li class=success>That is correct!!!</li>";
	echo "<li class=success>The cache is located at N 36 01.095 W 086 46.401</li>";
	echo "<li class=success>You may now log the cache after you find it.</li>";
	mysql_select_db($database_TOM, $TOM);
      $query = "UPDATE users SET solved = '1' WHERE username = '$colname_InitialInfo'";
      $result = mysql_query($query) or die(mysql_error());
	mail($to, $subject, $message);
	echo '</ul>';	
	} else {
	echo '<ul>';
	echo "<li class=warning>That is not correct. Try again.</li>";
	echo '</ul>';
	}
}
?>
<p>&nbsp; </p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;<a href="<?php echo $logoutAction ?>">Log out</a></p>
<p>&nbsp; </p>
</body>
</html>
<?php
mysql_free_result($InitialInfo);

mysql_free_result($PrivateKey);

?>
