view register.inc.php @ 120:2c594958050e

Make test vote keys "testN" where N >= 1
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 22 Oct 2013 14:22:26 +0300
parents c6b9041078ec
children aeebfedb5709
line wrap: on
line source

<?
$mode = stGetRequestItem("mode", "start");

$botCheckIDs = "aBcdefghIjklmnopqrsTuvxyz0123456";
$botCheckOPs = "bit";
$botCheckROPs = "+-*";


function stPrintFormData($button, $mode = "start")
{
  echo
  stGetFormStart("register").
  " ".stGetFormSubmitInput("continue", $button)."\n";

  stPrintFormHiddenInput("mode", $mode);
  
  foreach (array("name", "groups", "email", "oneliner", "hash", "botcheck") as $name)
    stPrintFormHiddenInput($name, stGetRequestItem($name));

  echo "</form>\n";
}


function intValueToHash($val)
{
  global $botCheckIDs;
  $str = "";
  do
  {
    $str = $botCheckIDs[$val & 31].$str;
    $val >>= 5;
  }
  while ($val > 0);
  return $str;
}


function intHashToValue($hash)
{
  global $botCheckIDs;
  for ($val = 0, $i = 0; $i < strlen($hash); $i++)
  {
    $val *= 32;
    $n = strpos($botCheckIDs, $hash[$i]);
    if ($n !== FALSE)
      $val += $n;
    else
      return -2;
  }
  return $val;
}


function splitHash($hash)
{
  global $botCheckOPs;
  return preg_split("/([".$botCheckOPs."])/", $hash, -1, PREG_SPLIT_DELIM_CAPTURE);
}


function hashToCheckStr($hash)
{
  global $botCheckOPs, $botCheckROPs;
  $out = "";
  
  foreach (splitHash($hash) as $val)
  {
    $i = strpos($botCheckOPs, $val);
    if ($i !== FALSE)
      $out .= " ".$botCheckROPs[$i]." ";
    else
      $out .= intHashToValue($val);
  }
  return $out;
}


function hashToAnswer($hash)
{
  eval("\$res = ".hashToCheckStr($hash).";");
  return $res;
}

stCheckRegistrationAvailable();

// Check if registration is enabled
if (!stChkSetting("allowRegister"))
{
?>
<h1>Sorry, registration disabled!</h1>
<p>
Registration to the event is not enabled at this time.
</p>
<?
}
else
if ($maxAttendeesHard > 0 && $numAttendees >= $maxAttendeesHard)
{
?>
<h1>Sorry, registration disabled!</h1>
<p>
Registration to the event is not available at this time due to
number of attendees limit having been reached. <b>:(</b>
</p>
<?
}
else
if ($mode == "start")
{
  $botCheckHash =
    intValueToHash(rand(1,5)).
    $botCheckOPs[rand(0,2)].
    intValueToHash(rand(1,5)).
    $botCheckOPs[rand(0,2)].
    intValueToHash(5 * rand(1,5));

  echo
  "<h1>Registration</h1>\n".
  stGetFormStart("register").
  " ".stGetFormHiddenInput("mode", "check")."\n".
  " ".stGetFormHiddenInput("hash", $botCheckHash)."\n".
  " <table>\n";
  stPrintFormTextInput("Handle:", "(elite)", 30, 30, "name");
  stPrintFormTextInput("Group(s):", "(elite crew^supahmen)", 40, 64, "groups");
  stPrintFormTextInput("E-mail:", "(to be informed of location etc)", 40, 64, "email");
  stPrintFormTextInput("Oneliner:", "(whatever)", 64, 64, "oneliner");
  stPrintFormTextInput(hashToCheckStr($botCheckHash)." = ", "(I.Q. / robot check".
  //" [".hashToAnswer($botCheckHash)."]".
  ")", 20, 20, "botcheck", "autocomplete=\"off\"");
  echo
  "  <tr><td colspan=\"2\"></td><td>".stGetFormSubmitInput("register", "Register")."</td></tr>\n".
  " </table>\n".
  "</form>\n";

  echo stGetSetting("registerInfoText");
}
else
if ($mode == "check")
{
  if (stChkDataItem("name") || strlen(stGetRequestItem("name")) < 3)
    stError("Handle / name not given, or too short.");

  if (stChkDataItem("hash"))
    stError("Invalid data.");

  $email = stGetRequestItem("email");
  if (stGetSetting("requireEMail"))
  {
    if (stChkDataItem("email") || strlen($email) < 4)
      stError("E-mail address not given, or it is too short.");
  }

  if (strlen($email) > 0 && (strpos($email, "@") === FALSE || strpos($email, ".") === FALSE))
    stError("E-mail address not in proper format.");

  $hash = stGetRequestItem("hash");
  $answer = stGetRequestItem("botcheck");
  if (hashToAnswer($hash) != intval($answer))
    stError("Incorrect answer to I.Q. / bot check.");

  if ($errorSet)
  {
    echo "<p>Following errors occured:</p>\n".
    "<ul>\n".$errorMsg."</ul>\n";
    stPrintFormData("Go back");
  }
  else
  {
    $sql = stPrepareSQL(
      "INSERT INTO attendees (regtime,name,groups,oneliner,email) VALUES (%d,%S,%S,%S,%S)",
      time(), "name", "groups", "oneliner", "email");

    if (stExecSQL($sql) !== FALSE)
    {
      echo "<h1>Registration successful</h1>\n".
      stGetSetting("registerPostText");

      if (stChkDataItem("email"))
        echo stGetSetting("registerPostNoEmail");
    }
    else
    {
      echo "<h1>An error occured.</h1>\n";
      echo "<p>Oh noes! SQL error happenstance!</p>";
    }
  }
}
?>