<?php // pineapple_signup.php  
	
	require_once "./pineapple.php";		// this file has the menu, header and css code for the site
	require_once "./mysql/helper.php";
	
	// This line renders the menu and header for the site
    pineapple_html_begin("Pineapple PNP", "Pineapple Login");

  // If this is the first time the forms is loaded, display the user signup form
	if (!isset($_POST['submitok']))
  {
?>

<h3>New User Registration Form</h3>
<p><font color="orangered" size="+1"><tt><b>*</b></tt></font>
  indicates a required field
</p>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td align="right">Full Name</td>
        <td>
            <input name="newname" type="text" maxlength="100" size="25" />
            <font color="orangered" size="+1"><tt><b>*</b></tt></font>
        </td>
    </tr>
    <tr>
        <td align="right">E-Mail Address</td>
        <td>
            <input name="newemail" type="text" maxlength="100" size="25" />
            <font color="orangered" size="+1"><tt><b>*</b></tt></font>
        </td>
    </tr>
    <tr>
        <td align="right" colspan="2">
            <hr noshade="noshade" />
            <input type="reset" value="Reset Form" />
            <input type="submit" name="submitok" value="   OK   " />
        </td>
    </tr>
</table>
</form>

    <?php
  }
  else
  {
    // Process signup submission
    $db = db_connect("pontech_pineapplepnp");

    if ($_POST['newname']=='' or $_POST['newemail']=='') {
      ?>
        <script>
          alert("One or more required fields were left blank. Please fill them in and try again.");
          history.back();
        </script>
      <?php
      goto php_exit;
    }
	
    // Check for existing user with the new email
    $sql = "SELECT * FROM user WHERE email = '$_POST[newemail]'";
    // execute sql statement
    $result = $db->query($sql);
	
    // get row count
    $num_rows = $result->rowCount();
    if ($num_rows > 0) {
      ?>
        <script>
          alert("An account with your email already exists. Please try again.");
          history.back();
        </script>
      <?php
      goto php_exit;
    }
    
    // append record
    $sql = "INSERT INTO user SET email = '$_POST[newemail]', password = 'NEW-ACCOUNT', fullname = '$_POST[newname]'";
    // execute sql statement
    $result = $db->query($sql);

    // todo: This code exists in two places.  Ghetto (forgot password)
    $_POST['uemail'] = $_POST['newemail'];
    
    $randomstr = substr(md5(time()),0,6);
		
		$message = "Please click on the following link to reset your password
		http://pineapplepnp.com/pineapple_reset_password.php?id=$randomstr";

		mail($_POST['uemail'],"Reset Password", $message, "From:Pineapple PnP <support@pineapplepnp.com>");
		
		// delete record of previous recovery attemp
		$sql = "DELETE FROM password_recover WHERE email = ?";

		// execute sql statement
    $sth = $db->prepare($sql);
    $sth->execute(array($_POST['uemail']));
  
		// append record with random string
		$sql = "INSERT INTO password_recover SET email = ?, resetstr = ?";

		// execute sql statement
    $sth = $db->prepare($sql);
    $sth->execute(array($_POST['uemail'],$randomstr));
    
    // Notify customer service an order has been placed
    $sql = "SELECT user.email FROM user WHERE user.customer_service=1";
    $sth = $db->query ($sql);
    $mailing_list = "";
    while ($row = $sth->fetch (PDO::FETCH_NUM))
    {
      if ($mailing_list == "")
      {
        $mailing_list = $row[0];
      }
      else
      {
        $mailing_list = $mailing_list . ", " . $row[0];
      }
    }
    
    // customer count
    $sql = "SELECT user.email FROM user WHERE user.admin=?";
    $prepared_array = array(0);
    $sth = $db->prepare($sql);
    $success = $sth->execute($prepared_array);
    $customer_count = $sth->rowCount();
    
    $message = "$_POST[newname] has signed up! There are a total of $customer_count Pineapple Pick N Place customers.";
    mail($mailing_list,"New Sign Up!", $message, "From:Pineapple PnP <support@pineapplepnp.com>");
    
		?>
      <h1>User registration successful</h1>
      <p>A message has been sent to your email address to allow you to reset your password.</p>
      <p>Accounts not claimed within 24 hours will be purged.</p>
    <?php
  }

php_exit:

// This is the end of the HTML body, scripting starts after this.
pineapple_script_end();
// This is the end of the HTML file, scripting starts after this.
pineapple_html_end();
?>