<?
/***************************************************************************
 *			relogs.php                   	                   *
 *                     	          				           *
 *   File Info:		  Handles user login                               *
 *   Copyright:       Copyright 2003 http://www.128concepts.com            *
 *						                           *
 ***************************************************************************/
include ("./config.php");
include ("./tests.php");

$bads = "false";
@session_start();
$login = "false";
if ((session_is_registered("admin")) && ($_SESSION['admin'] != "admin")) 
{
	$users = $_SESSION['admin'];
	$login = "user";
        $bads = "true";
}
if ((session_is_registered("admin")) && ($_SESSION['admin'] = "admin"))
{
	$admins = $_SESSION['admin'];	
	$login = "admin";
        $bads = "true";
}

if ($login == "admin" or $login == "user")
{
	echo "<br><center><TABLE cellSpacing=0 cellPadding=0 border=0><TR>
      <TD background=./images/border_cnr_tl.gif><IMG height=8 alt= src=./images/px.gif width=8 border=0></TD>
      <TD background=./images/border_top.gif><IMG height=8 alt= src=./images/px.gif width=120 border=0></TD>
      <TD background=./images/border_cnr_tr.gif><IMG height=8 alt= src=./images/px.gif width=8          border=0></TD></TR><TR><TD background=./images/border_left.gif>
       <IMG height=60 alt= src=./images/px.gif width=8 border=0></TD>
       <TD width=120>
       <TABLE cellSpacing=0 cellPadding=4 width=480 border=0>
       <TBODY><tr><td align=center><center><img src=images/mouses.gif border=0><font face = Verdana size =4 color=navy>Logging In</font></center></td></tr><TR><tr><td align=center>........................................................................................</td></tr>";
	if ($users != NULL && $login == "user")
	echo "<tr>
       <TD align=center><font face = Verdana size = 2>User '$users' - Already logged in</font></td></tr>";
	if ($admins != NULL && $login == "admin")
	echo "<tr>
       <TD align=center><font face = Verdana size = 2>User '$admins' - Already logged in</font></td></tr>";

    	echo"<tr><td align=center>Click here to go<a href = \"javascript:history.back()\"> back</a>";
	echo "</td></tr><tr><td align=center>........................................................................................</td></tr><tr><td align=center><b><font face = Verdana size = 1 color = #CCCCCC>$footer</font></td></tr>";
echo "</TBODY></TABLE></TD><TD background=./images/border_right.gif>
      <IMG height=60 alt= src=./images/px.gif width=8 border=0></TD></TR>
      <TR><TD background=./images/border_cnr_bl.gif>
      <IMG height=8 alt= src=./images/px.gif width=8 border=0></TD>
      <TD background=./images/border_bottom.gif>
      <IMG height=8 alt= src=./images/px.gif width=121 border=0></TD>
      <TD background=./images/border_cnr_br.gif>
      <IMG height=8 alt= src=./images/px.gif 
      width=8 border=0></TD></TR></TABLE></center><br><br>";
}
 if ($_SERVER['REQUEST_METHOD'] == "POST") 
  {
        # escape data and set variables
        $do = addslashes($_POST["do"]);
        $passwords = addslashes($_POST["password"]);
        $email = addslashes($_POST["email"]);
}

if ($bads == "false"){
# handle case for users to checkout without registering
if ($do == "unreg")
{
echo"
	<script>
	window.setTimeout('changeurl();',1000); 
	function changeurl(){window.location='./shop_checkout.php?unregistered'}
	</script>";
}

# handle case to send user to new password

if ($do == "new")
{
	echo"
	<script>
	window.setTimeout('changeurl();',1000); 
	function changeurl(){window.location='./newpass.php'}
	</script>";
}

if ($do == "return")
{
	
// check email was entered or valid
		if ((!ereg("^[a-zA-Z0-9_.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $email))or(empty($email)))
		{
			echo "<br><center><TABLE cellSpacing=0 cellPadding=0 border=0><TR>
      <TD background=./images/border_cnr_tl.gif><IMG height=8 alt= src=./images/px.gif width=8 border=0></TD>
      <TD background=./images/border_top.gif><IMG height=8 alt= src=./images/px.gif width=120 border=0></TD>
      <TD background=./images/border_cnr_tr.gif><IMG height=8 alt= src=./images/px.gif width=8          border=0></TD></TR><TR><TD background=./images/border_left.gif>
       <IMG height=60 alt= src=./images/px.gif width=8 border=0></TD>
       <TD width=120>
       <TABLE cellSpacing=0 cellPadding=4 width=480 border=0>
       <TBODY><tr><td align=center><center><img src=images/mouses.gif border=0><font face = Verdana size =4 color=navy>Logging In</font></center></td></tr><TR><tr><td align=center>........................................................................................</td></tr>";
			echo "<tr><td align= center><font color=#990000 face=verdana size=1><b>Either no email was entered or it was not valid!</b></font></td></tr>";
			$again=1;
		}//end validate email

		// link back if error occurred
		if ($again==1)
		{
			echo"<tr><td align=center><font face=verdana size=1>Click <a href = \"javascript:history.back()\"><b>here</b></font></a> to try again</td></tr>";
			echo "</td></tr><tr><td align=center>........................................................................................</td></tr><tr><td align=center><b><font face = Verdana size = 1 color = #CCCCCC>$footer</font></td></tr>";
echo "</TBODY></TABLE></TD><TD background=./images/border_right.gif>
      <IMG height=60 alt= src=./images/px.gif width=8 border=0></TD></TR>
      <TR><TD background=./images/border_cnr_bl.gif>
      <IMG height=8 alt= src=./images/px.gif width=8 border=0></TD>
      <TD background=./images/border_bottom.gif>
      <IMG height=8 alt= src=./images/px.gif width=121 border=0></TD>
      <TD background=./images/border_cnr_br.gif>
      <IMG height=8 alt= src=./images/px.gif 
      width=8 border=0></TD></TR></TABLE></center><br><br>";
			$bads = "true";
		} // end ($again==1)	

if ($email && $passwords && $bads == "false")
{
$table= "users";
$sel_item_totalprice = $sel_item_qty * $sel_item_price;
$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 'mydata'...Please press 'Create Database' to create new database.");
$pass = md5($passwords);

$sql = "select * from $table where email = '$email' and passwrd = '$pass'";

$rs = mysql_query($sql,$conn)
	or die("Could not execute SQL query!!! Possibly SQL query already been executed.");
$num = mysql_numrows($rs);
if ($num != 0)
{
$row = mysql_fetch_row($rs);
$username = $row[0];
$admin = $username;
session_register("admin");
$_SESSION['admin'] = $admin;

echo "<br><center><TABLE cellSpacing=0 cellPadding=0 border=0><TR>
      <TD background=./images/border_cnr_tl.gif><IMG height=8 alt= src=./images/px.gif width=8 border=0></TD>
      <TD background=./images/border_top.gif><IMG height=8 alt= src=./images/px.gif width=120 border=0></TD>
      <TD background=./images/border_cnr_tr.gif><IMG height=8 alt= src=./images/px.gif width=8          border=0></TD></TR><TR><TD background=./images/border_left.gif>
       <IMG height=60 alt= src=./images/px.gif width=8 border=0></TD>
       <TD width=120>
       <TABLE cellSpacing=0 cellPadding=4 width=480 border=0>
       <TBODY><tr><td align=center><center><img src=images/mouses.gif border=0><font face = Verdana size =4 color=navy>Logging In</font></center></td></tr><TR><tr><td align=center>........................................................................................</td></tr>";
echo "<tr><td align=center><font face=verdana size=1 color = navy>Welcome user $admin - you now logged in</font></td></tr>";
if ($_SESSION['admin'] != "admin")
	{

		
		######## update users online  ##########
		$table = "setting";
		$usql = "SELECT online from $table";
		$rest = @mysql_query($usql,$conn)
		or die("Unable to extract number of online users....");
		while ($row = mysql_fetch_array($rest))
		{
			$users = $row["online"];
		}
		$users += 1;
		$wsql = "UPDATE $table set online = '$users'";
		$upduser = @mysql_query($wsql,$conn)
		or die("Unable to update number of users online");
		$table = "userlogs";
		$date_added = date("Y-m-d");
		$timeins = date("H:i dS F");
		$timein = "Time: $timeins";
		$viewer = getenv("HTTP_USER_AGENT");
		$browser = "Other Browser";
		if (preg_match("/MSIE/i","$viewer"))
		{
			$browser = "Internet Explorer";
		}
		else if (preg_match("/Netscape/i","$viewer"))
		{
			$browser = "Netscape";
		}
		else if (preg_match("/Opera/i","$viewer"))
		{
			$browser = "Opera";
		}
		if ($REMOTE_ADDR != null)
		{
			$ip = "$REMOTE_ADDR";
		}
		else
		{
			$ip = "n/a";
		}
		if ($HTTP_REFERER != null)
		{
			$referer = "$HTTP_REFERER";
		}
		else
		{
			$referer = "n/a";
		}
		$browse = $browser;
		$sqlnew = "INSERT INTO $table (sessid,username,timein,timeout,ip,referer,browser,date_added) values( \"$PHPSESSID\",'$admin','$timein','n/a','$ip','$referer','$browse','$date_added')";
		
		# create user log
		 if ((session_is_registered("admin")) and ($_SESSION['admin'] != "admin"))
		{
			$results=mysql_query($sqlnew,$conn)
				or die("Unable to excute the SQL query!!! Possibly SQL command already executed");
			if ($results)
			{
				if ($_SESSION['admin'] != "admin")
				echo("<tr><td align=center><font face=verdana size=1>RESULT: User log record has been created.<br>Please be patient as we transfer you to another page.</font></td></tr>"); 
				echo "</td></tr><tr><td align=center>........................................................................................</td></tr><tr><td align=center><b><font face = Verdana size = 1 color = #CCCCCC>$footer</font></td></tr>";
echo "</TBODY></TABLE></TD><TD background=./images/border_right.gif>
      <IMG height=60 alt= src=./images/px.gif width=8 border=0></TD></TR>
      <TR><TD background=./images/border_cnr_bl.gif>
      <IMG height=8 alt= src=./images/px.gif width=8 border=0></TD>
      <TD background=./images/border_bottom.gif>
      <IMG height=8 alt= src=./images/px.gif width=121 border=0></TD>
      <TD background=./images/border_cnr_br.gif>
      <IMG height=8 alt= src=./images/px.gif 
      width=8 border=0></TD></TR></TABLE></center><br><br>";
				echo"
				<script>
				window.setTimeout('changeurl();',3000); 
				function changeurl(){window.location='./shop_checkout.php?login'}
				</script>";
			}
		}
	}
}
else
{

echo "<br><center><TABLE cellSpacing=0 cellPadding=0 border=0><TR>
      <TD background=./images/border_cnr_tl.gif><IMG height=8 alt= src=./images/px.gif width=8 border=0></TD>
      <TD background=./images/border_top.gif><IMG height=8 alt= src=./images/px.gif width=120 border=0></TD>
      <TD background=./images/border_cnr_tr.gif><IMG height=8 alt= src=./images/px.gif width=8          border=0></TD></TR><TR><TD background=./images/border_left.gif>
       <IMG height=60 alt= src=./images/px.gif width=8 border=0></TD>
       <TD width=120>
       <TABLE cellSpacing=0 cellPadding=4 width=480 border=0>
       <TBODY><tr><td align=center><center><img src=images/mouses.gif border=0><font face = Verdana size =4 color=navy>Logging In</font></center></td></tr><TR><tr><td align=center>........................................................................................</td></tr>";
	echo "<tr><td align=center><font color=navy face=verdana size=2> Login error! - password or email not correct</font></td></tr>";
    	echo"<tr><td align=center>Click here to go<a href = \"javascript:history.back()\"> back</a>";
	echo "</td></tr><tr><td align=center>........................................................................................</td></tr><tr><td align=center><b><font face = Verdana size = 1 color = #CCCCCC>$footer</font></td></tr>";
echo "</TBODY></TABLE></TD><TD background=./images/border_right.gif>
      <IMG height=60 alt= src=./images/px.gif width=8 border=0></TD></TR>
      <TR><TD background=./images/border_cnr_bl.gif>
      <IMG height=8 alt= src=./images/px.gif width=8 border=0></TD>
      <TD background=./images/border_bottom.gif>
      <IMG height=8 alt= src=./images/px.gif width=121 border=0></TD>
      <TD background=./images/border_cnr_br.gif>
      <IMG height=8 alt= src=./images/px.gif 
      width=8 border=0></TD></TR></TABLE></center><br><br>";
	 $bads = "true";
}
}
}
if ((!session_is_registered("admin")) and ($bads == "false"))
{
 echo"
   <form method=\"post\" action=\"reglog.php?\">
  <br><table align=\"center\" width=\"350\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
            <tr><td bgcolor=\"$colour_2\">
  <table cellpadding=\"2\" border=\"0\" cellspacing=\"1\" width=\"100%\" align=\"center\">
    <tr bgcolor=\"$colour_1\"> 
      <td width=\"70%\" colspan=\"2\" height=\"20\" background=\"images/bevel_bg.gif\"><b>USER LOGIN / REGISTER:</b></td>
      </tr>
    <tr bgcolor=\"$colour_1\"> 
      <td width=\"45%\">&nbsp;</td>
      <td width=\"55%\"><input type=\"radio\" name=\"do\" value=\"unreg\"> I want to checkout <br>(without registering)</td>
    </tr>
    <tr bgcolor=\"$colour_1\"> 
      <td width=\"45%\">&nbsp;</td>
      <td width=\"55%\"><input type=\"radio\" name=\"do\" value=\"new\"> I am a new customer <br>(I need to register)</td>
    </tr>
    <tr bgcolor=\"$colour_1\"> 
      <td width=\"45%\">&nbsp;</td> 
      <td width=\"55%\"><input type=\"radio\" name=\"do\" value=\"return\" checked> I am a returning customer and my password is:</td>
    </tr>
        <tr bgcolor=\"$colour_1\"> 
      <td width=\"45%\">Email address:</td>
      <td width=\"55%\"><input name=\"email\" class=\"textbox\" type=\"text\" size=\"25\" maxlength=\"40\"></td>
    </tr>
    <tr bgcolor=\"$colour_1\"> 
      <td width=\"45%\">Password:</td>
      <td width=\"55%\"><input name=\"password\" class=\"textbox\" type=\"password\"name=\"password\" size=\"25\" maxlength=\"40\"></td>
    </tr>
    <tr bgcolor=\"$colour_1\"> 
      <td width=\"45%\">&nbsp;</td>
	<td width=\"55%\"><a href=\"forgot.php\" target=\"_self\">I forgot my password!</a><br><br><INPUT type=\"image\" name=\"submit\" border=\"0\" src=\"images/contshop_en.gif\"></td>
     </tr>
  </table></td></tr></table>
</form>";   
}
}
?>
