<?php require_once('Connections/TOM.php'); ?>
<?php
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($TOM, $theValue) : mysqli_real_escape_string($TOM, $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;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

$var1_checkUser = "0";
if (isset($_POST['username'])) {
  $var1_checkUser = $_POST['username'];
}
mysqli_select_db($TOM, $database_TOM);
$query_checkUser = sprintf("SELECT users.username FROM users WHERE users.username = %s", GetSQLValueString($var1_checkUser, "text"));
$checkUser = mysqli_query($TOM, $query_checkUser) or die(mysqli_error($TOM));
$row_checkUser = mysqli_fetch_assoc($checkUser);
$totalRows_checkUser = mysqli_num_rows($checkUser);
// assume that no match was found
$alreadyRegistered = false;

// check whether recordset found any matches
if ($totalRows_checkUser > 0) {
// if found already registered
$alreadyRegistered = true;
}
else {
// go ahead with the record insert
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "newuser")) {
  $insertSQL = sprintf("INSERT INTO users (username, puzzlenum) VALUES (%s, %s)",
                       GetSQLValueString($_POST['username'], "text"),
                       GetSQLValueString($_POST['puzzlenum'], "int"));
                       $puzzle = $_POST['puzzlenum'];


  $updateSQL = sprintf("UPDATE RSA SET used = 1 WHERE puzzle_ID = %s", GetSQLValueString($_POST['puzzlenum'], "int"));


 mysqli_select_db($TOM, $database_TOM);
  $Result1 = mysqli_query($TOM, $insertSQL) or die(mysqli_error($TOM));
  $Result2 = mysqli_query($TOM, $updateSQL) or die(mysqli_error($TOM));

  $insertGoTo = "index.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
}
mysqli_select_db($TOM, $database_TOM);
$query_listPuzzles = "SELECT RSA.puzzle_ID, RSA.modulus, RSA.used FROM RSA WHERE RSA.used = '0' LIMIT 10";
$listPuzzles = mysqli_query($TOM, $query_listPuzzles) or die(mysqli_error($TOM));
$row_listPuzzles = mysqli_fetch_assoc($listPuzzles);
$totalRows_listPuzzles = mysqli_num_rows($listPuzzles);


?><!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>Add a New User</title>
<link href="admin.css" rel="stylesheet" type="text/css" />
</head>

<body>
<h1><img src="publickey.jpg" alt="RSA" width="178" height="241" class="floatRight" />The purpose of this page is to enter your Geocaching log name into a database and have it associated with a puzzle that you select. Please enter your Geocaching log name and select the modulus you desire to choose as your puzzle. </h1>
<?php
if ($_POST && $alreadyRegistered) {
echo '<p class="warning">'.$_POST['username'].' '.'is already registered</p>';
}
?>
<form id="newuser" name="newuser" method="POST" action="<?php echo $editFormAction; ?>">
  <p>
    <label>Geocaching log name: <br />
    <input name="username" type="text" id="username" />
    </label>
  </p>
  <p>
    <label>Puzzle Modulus: <br />
    <select name="puzzlenum" id="puzzlenum">
      <?php
do {  
?>
      <option value="<?php echo $row_listPuzzles['puzzle_ID']?>"><?php echo $row_listPuzzles['modulus']?></option>
      <?php
} while ($row_listPuzzles = mysqli_fetch_assoc($listPuzzles));
  $rows = mysqli_num_rows($listPuzzles);
  if($rows > 0) {
      mysqli_data_seek($listPuzzles, 0);
      $row_listPuzzles = mysqli_fetch_assoc($listPuzzles);
  }
?>
    </select>
    </label>
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit" />
  </p>
  <input type="hidden" name="MM_insert" value="newuser">
</form>


<p>&nbsp;</p>
</body>
</html>
<?php
mysqli_free_result($listPuzzles);

mysqli_free_result($checkUser);
?>

