<html>
<head>
<title>Adding a New User</title></head>
<body>
<?php

function isBadEmail($str)
 {
	$atsign = 0;
	$dot = 0;
	$x = 0;
      if(strlen($str)>=4&&strlen($str)<=50){
      $valid_chars = " 0123456789_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.@";
      for($i=0;$i<strlen($str);$i++){
        if(strchr($valid_chars,substr($str,$i,1))==false) $x++;
      }
	 for($i=0;$i<strlen($str);$i++){
        if(strchr("@",substr($str,$i,1))==true) $atsign++;
      }
 	for($i=0;$i<strlen($str);$i++){
        if(strchr(".",substr($str,$i,1))==true) $dot++;
      }
       if ($atsign == 0) return true;
      if($x>0 and $atsign == 1 and $dot >= 1) return true;
    }else{
      return true;
    }
}
/***************************************************************************
 *			newpass.php                   	                   *
 *                     	          				           *
 *   File Info:		  create new user account                          *
 *   Copyright:       Copyright 2003 http://www.128concepts.com            *
 *						                           *
 ***************************************************************************/

###### first time...url will contain PHPSESSID=xxxx #######
###### code below will refresh page to removePHPSESSID#####

$strvar = getenv("QUERY_STRING");
$pos = strpos($strvar,'=');
$strs = substr($strvar,0,$pos);
if ($pos > 0)
{
	include_once("./your-header.inc");
	echo "<b><br><br><br><br><center>Please be patient as we now re-directing you to main store page...<br>You will only see this message once i.e every time you start a new browser window.</center></b><br>";
	include_once("./footer-only.inc");
	include_once("./redirect-me.php");
}else{
 if ($_SERVER['REQUEST_METHOD'] == "POST") 
  {
        # escape data and set variables
        $firstname = addslashes($_POST["firstname"]);
        $lastname = addslashes($_POST["lastname"]);
        $username = addslashes($_POST["username"]);
        $passwrd=addslashes($_POST["passwrd"]);
        $passwrds = addslashes($_POST["passwrds"]);
        $email = addslashes($_POST["email"]);
        $hint=addslashes($_POST["hint"]);

}
if ((!$firstname) or (!$lastname) or (!$username) or (!$passwrd) or (!$passwrds) or (!$email) or (!$hint))
{
include ("config.php");
include ("./your-header.inc");
include ("./tests.php");
$form = "<br><strong><center><img src=images/keybord1.gif border=0><FONT face=Verdana color=darkred size=2>  Please enter your new user details</font></center></strong><br>";
$form.="<br><center><table width=50% cellpadding=2 cellspacing=0 border=0 bgcolor=$backcolor align = center>";
$form.="<tr>";

$form.="<form action =newpass.php";
$form.=" method=\"post\">";
$form.="<tr>";
$form.= "<td valign=top><strong>First Name</strong></td>";
$form.="<td valign=top>";
$form.="<INPUT type=\"text\" name=\"firstname\" size=25 maxlength=25>";
$form.="</td></tr>";
$form.="<tr>";
$form.= "<td valign=top><strong>Last Name</strong></td>";
$form.="<td valign=top>";
$form.="<INPUT type=\"text\" name=\"lastname\" size=25 maxlength=25>";
$form.="</td></tr>";
$form.="<tr>";
$form.= "<td valign=top><strong>User Name</strong></td>";
$form.="<td valign=top>";
$form.="<INPUT type=\"text\" name=\"username\" size=25 maxlength=25>";
$form.="</td></tr>";
$form.="<tr>";
$form.= "<td valign=top><strong>New Password</strong></td>";
$form.="<td valign=top>";
$form.="<INPUT type=\"password\" name=\"passwrd\" size=25 maxlength=25>";
$form.="</td></tr>";
$form.="<tr>";
$form.= "<td valign=top><strong>Confirm Password</strong></td>";
$form.="<td valign=top>";
$form.="<INPUT type=\"password\" name=\"passwrds\" size=25 maxlength=25>";
$form.="</td></tr>";
$form.="<tr>";
$form.= "<td valign=top><strong>Email Address</strong></td>";
$form.="<td valign=top>";
$form.="<INPUT type=\"text\" name=\"email\" size=25 maxlength=25>";
$form.="</td></tr>";
$form.="<tr>";
$form.= "<td valign=top><strong>Hint</strong></td>";
$form.="<td valign=top>";
$form.="<INPUT type=\"text\" name=\"hint\" maxlength=25>";
$form.="</td></tr>";
$form.="<tr>";
$form.="<td align=center colspan=2>";
$form.="<INPUT type=\"submit\" value=\"Add Record\"><INPUT type=\"reset\" value=\"Clear\">";
$form.="</td></tr>";
$form.="</form>";
$form.="</tr></table></center>";
echo $form;
echo("<br><FONT face=Verdana color=#666666 size=2><p align=justify><img src=images/readme.gif border=0>Notice for USERS: The above user form allows you to create a new user record.To add the new user record click the 'Add Record button - to clear click 'Clear button.If you wish not to use any password hint please type n/a in textbox.</p></font>");
		include("./your-footer.inc");
}
else
{
include ("config.php");
include ("./your-header.inc");
include ("./tests.php");
# validation routines
	$baderr = isBadEmail($email);
	if ($baderr) 
	{
echo("<br><br><center><img src=images/line.gif border=0 width=550 height=1><br><FONT face=Verdana color=#666666 
            size=2>Cannot continue adding users records - Bad email format - email format should be xxx@yourdomain.com etc.</font><br>");
	echo("<FONT face=Verdana color=#666666 
            size=2>Go back and add details before continuing to add a new users record!</font><br><img src=images/line.gif border=0 width=550 height=1></center><br><br><br>");
	include("./your-footer.inc");
	exit();	
	}	
	if (strlen($passwrd) != strlen($passwrds)) 
	{
	echo("<br><br><center><img src=images/line.gif border=0 width=550 height=1><br><FONT face=Verdana color=#666666 
            size=2>Cannot continue adding users records - Password lengths should be identical.</font><br>");
	echo("<FONT face=Verdana color=#666666 
            size=2>Go back and add details before continuing to add a new users record!</font><br><img src=images/line.gif border=0 width=550 height=1></center><br><br><br>");
	include("./your-footer.inc");
	exit();	
	}
	if ($passwrd != $passwrds)
	{
	echo("<br><br>Cannot continue adding users records - Password should be identical.<br>");
	echo("Go back and add details before continuing to add a new users record!<br><br><br><br>");
	include("./your-footer.inc");
	exit();	
	}

# check see if user name exists in users table
$table = "users";
$conn = @mysql_connect($server,$user,$password)
	or die("Unable to connect to SQL database...");
$rs = @mysql_select_db($database,$conn) 
	or die("Unable to select the database...Please press 'Create Database' to create new database.");

#check if email is duplicate one

$sql_email = "SELECT * from $table where email = '$email'";
$dup_email = @mysql_query($sql_email,$conn);
$dup_count = @mysql_num_rows($dup_email);
if ($dup_count > 0)
{
	echo("<br><center><img src=images/line.gif border=0 width=470 height=1><br><img src=images/keybord1.gif border=0><font face=verdana size=2> User Email Already Exists<br>The email already exists and you cannot use this email address.<br>Please go back and re-input a new email and try saving again!<br><img src=images/line.gif border=0 width=470 height=1></font><center>");
	include("./your-footer.inc");
	exit();
}
$sql = " SELECT * from $table where username = '$username'order by username";
$rs = mysql_query($sql,$conn)
	or die("Could not execute SQL query!!! Possibly SQL query already been executed.");
$rows = mysql_fetch_row($rs);
if ($rows == 0)
{	
	$npasswrd = $passwrd;
	$passwrd = md5($passwrd);
	$date_added = date("Y-m-d");
	$sql = "INSERT INTO $table (username,firstname,lastname,passwrd,email,hint,date_added) values (\"$username\",\"$firstname\",\"$lastname\",\"$passwrd\",\"$email\",\"$hint\",\"$date_added\")";
	$result = @mysql_query($sql,$conn)
		or die("SQL command failed - could not insert new users record!!!");
	if ($result)
	{
		
		# create email ready to send to user 
		
		$msg = "********* IMPORTANT EMAIL CONFIRMATION ********\n\n";
		$msg.= "Thank you, $firstname $lastname for registering you user details with $sitecompany.\n";
		$msg.="Your user details will be kept on our database for your user login.\n\n";
		$msg.="Your user details you provided are as follows:-\n\n";
		$msg.="---------------------------------------------------------------------\n";
		$msg.="Username: $username.\n";
		$msg.="Password: $npasswrd.\n";
		$msg.="---------------------------------------------------------------------\n\n";
		$msg.="N.B Your password will be encrpyted on our database.\n";
		$msg.="Please keep your password safe, do not disclose your password to anyone.\n\n";
		$msg.="Thank you for registering with $sitecompany.";
		$to = "$email <$email>";
		$extraheader.="From: $admin_name<$admin_mail>\n";
    		$extraheader.="Reply-To: $admin_mail\r\n";
    		$extraheader.="X-Mailer: $admin_name [admin_mail]";

		@mail($email, "User Login Details Confirmation", $msg, $extraheader);
		echo("<br><center><img src=images/keybord1.gif border=0><font face=verdana size=2>  User Details Added<br><br><img src=images/line.gif border=0 width=100% height=1><br>User Name: $username<br>First Name: $firstname<br>Last Name: $lastname<br>Password: ????????<br>Email: $email<br>Hint: $hint<br>Date Added: $date_added<img src=images/line.gif border=0 width=100% height=1></font><center>");
echo("<br><p align=justify><font face=verdana size=2>Notice for USERS: The above user form allows you to create a new user record. To add the new user record click the 'Add Record button - to clear click 'Clear button.</font></p>");

echo "<br><center><a href=shop_showall.php><FONT face=Verdana color=#666666 
            size=2>Product List</font></a></centre>";
echo "<center><a href=shop_delcart.php><FONT face=Verdana color=#666666 
            size=2>Empty Basket</font></a></centre>";
echo "<center><a href=shop_viewcart.php><FONT face=Verdana color=#666666 
            size=2>View Cart</font></a></centre><br><br>"; 
include("./your-footer.inc");	
	}
}
if ($rows > 0)
{
	echo("<br><center><img src=images/line.gif border=0 width=470 height=1><br><img src=images/keybord1.gif><font face=verdana size=2> User Details Already Added<br>The user name already exists and you cannot use this username.<br>Please go back and re-input a new username and try saving again!<br><img src=images/line.gif border=0 width=470 height=1></font><center>");
	include("./your-footer.inc");
}
}
 }
?>
</body>
</html>