<?php
  require_once "../lib/init.php";

  // process form
  if (@$_REQUEST['submit']) {

    // error checking
    $errorsAndAlerts = "";
    if (!@$_REQUEST['fullname'])                { $errorsAndAlerts .= "You must enter your full name!<br/>\n"; }
    if (!@$_REQUEST['email'])                   { $errorsAndAlerts .= "You must enter your email!<br/>\n"; }
    else if(!isValidEmail(@$_REQUEST['email'])) { $errorsAndAlerts .= "Please enter a valid email (example: user@example.com)<br/>\n"; }
    if (!@$_REQUEST['username'])                { $errorsAndAlerts .= "You must choose a username!<br/>\n"; }
    if (!@$_REQUEST['password'])                { $errorsAndAlerts .= "You must choose a password!<br/>\n"; }

    // check for duplicate usernames and emails
    if (!$errorsAndAlerts) {
      $count = mysql_select_count_from('accounts', "`username` = '".mysql_escape($_REQUEST['username'])."'");
      if ($count > 0) { $errorsAndAlerts .= "That username is already in use, please choose another!<br/>\n"; }
    }

    // turn off strict mysql error checking for: STRICT_ALL_TABLES
    mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)

    // add user
    if (!$errorsAndAlerts) {
      mysql_query("INSERT INTO `{$TABLE_PREFIX}accounts` SET
                      fullname         = '".mysql_escape( $_REQUEST['fullname'] )."',
                      email            = '".mysql_escape( $_REQUEST['email'] )."',
                      username         = '".mysql_escape( $_REQUEST['username'] )."',
                      password         = '".mysql_escape( $_REQUEST['password'] )."',

                      disabled         = '0',
                      isAdmin          = '0',
                      expiresDate      = '0000-00-00 00:00:00',
                      neverExpires     = '1',
                      createdDate      = NOW(),
                      updatedDate      = NOW(),
                      createdByUserNum = '0',
                      updatedByUserNum = '0'")
      or die("MySQL Error Creating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
      $userNum = mysql_insert_id();

      // create accesslist entry
      // replace 'news' with the table you want the user to access
      // replace '6' with the access level they should have: 0=none, 6=author, 9=editor
      // replace '1' with the max listings they are allowed
      mysql_query("INSERT INTO `{$TABLE_PREFIX}_accesslist`
                               (userNum,  tableName,        accessLevel, maxRecords, randomSaveId)
                        VALUES ($userNum, 'all',            '1',         NULL,       '1234567890'),
                               ($userNum, 'news',           '6',         1,          '1234567890')") or die("MySQL Error Creating Access List:<br/>\n". htmlspecialchars(mysql_error()) . "\n");

      //
      $errorsAndAlerts = "Thanks!  We've created an account for you!";
      $_REQUEST = array(); // clear form values
    }

  }

?><!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=utf-8" />
<title></title>
<style type="text/css">
  body, td { font-family: arial }
</style>
</head>
<body>

<form method="post" action="">
<input type="hidden" name="submit" value="1" />

<h1>Sample User Signup Form</h1>

<?php if (@$errorsAndAlerts): ?>
  <div style="color: red; font-weight: bold; font-size: 16px; font-family: arial;"><br/>
    <?php echo $errorsAndAlerts; ?><br/><br/>
  </div>
<?php endif ?>

<table border="0" cellspacing="0" cellpadding="2">
 <tr>
  <td width="150">Full Name</td>
  <td><input class="text-input medium-input" type="text" name="fullname" value="<?php echo htmlspecialchars(@$_REQUEST['fullname']); ?>" size="50" /></td>
 </tr>
 <tr>
  <td width="150">Email</td>
  <td><input class="text-input medium-input" type="text" name="email" value="<?php echo htmlspecialchars(@$_REQUEST['email']); ?>" size="50" /></td>
 </tr>
 <tr>
  <td width="150">Username</td>
  <td><input class="text-input medium-input" type="text" name="username" value="<?php echo htmlspecialchars(@$_REQUEST['username']); ?>" size="50" /></td>
 </tr>
 <tr>
  <td width="150">Password</td>
  <td><input class="text-input medium-input" type="text" name="password" value="<?php echo htmlspecialchars(@$_REQUEST['password']); ?>" size="50" /></td>
 </tr>

 <tr>
  <td colspan="2" align="center">
    <br/>
    <input class="button" type="submit" name="submit" value="Sign up &gt;&gt;">
  </td>
 </tr>
</table>


</body>
</html>
