<?php
/***************************************************************************
 *			logouts.php                   	                   *
 *                     	          				           *
 *   File Info:		  Logout User                                      *
 *   Copyright:       Copyright 2003 http://www.128concepts.com            *
 *						                           *
***************************************************************************/


include ("config.php");
include ("./tests.php");

######### used for alignment purposes ##########

if (($usecat == "YES" and $showcat == "ON") or ($usecat == "NO" and $showcat == "ON")) 
	echo "<br>";

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

$strvar = getenv("QUERY_STRING");
$pos = strpos($strvar,'=');
if ($pos > 0)
{
	if ($noshow == "NO")
	{
	echo "<table width=450 border=1 cellspacing=0 cellpadding=0 height=150>
<tr><td width=450 bgcolor=#CCCCCC height=10>$header</td></tr>
<tr><td width=450 bgcolor=#FFFFFF height=140>";
	echo "<font face=verdana size=2 color=#666666><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></font><br></td></tr></table>";}
	include_once("./redirect-me.php");
	exit();
}

echo "<center><img src=images/mouses.gif border=0><font face = Verdana size =4 color=navy>Logging Out</font><center><br><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=450 border=0>
       <TBODY>";
echo "<tr><td align=center><center><img src=images/logoff2.gif></center></td></tr>";
$old_user = $_SESSION['admin'];  // store  to test if they *were* logged in
  $oldsess = $PHPSESSID;
  $result = session_unregister("admin");

   if (!empty($old_user))
  {
    if ($result)
    { 
	// If they were logged in and are now successfully logged out 

	  echo "<tr><td align=center><font face=verdana size=2 color=#666666>You have successfully logged out user: '$old_user' !</font></td></tr>";

		
	# code for updating the user log to show time stamp when log out
	
	$table = "userlogs";
	$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.");
	$sql = "select * from $table where username = '$old_user' and sessid = '$oldsess'";
	 $results=mysql_query($sql,$conn)
		or die("Unable to excute the SQL query!!! Possibly SQL command already executed");
	$nrow = mysql_numrows($results);
	$table = "setting";
	
	if ($nrow > 0)
	{
		$table = "userlogs";
		$timeouts = date("H:i dS F");
		$timeout = "Time: $timeouts";
		$sqlone = "UPDATE $table set timeout = '$timeout'";
		//$ress=mysql_query($sqlone,$conn)
		//or die("Unable to excute the SQL query!!! Possibly SQL command already executed");
		session_unregister("admin");	
		$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"];
		}
		if ($users >= 1)
			$users -= 1;
		$wsql = "UPDATE $table set online = $users";
		$upduser = @mysql_query($wsql,$conn)
			or die("Unable to update number of users online");
	}
	$table = "products";
	$sql = "SELECT * from $table";
	$products = mysql_query($sql,$conn);
	$prods = mysql_num_rows($products);

	$items = array();
	$count = 1;

	// only show items when there are items to show !!!
	if ($prods >= 1)
	{
		while ($row = mysql_fetch_array($products))
		{
	
		$items[$count] = $row["pid"];
		$count += 1;
		}
		srand(microtime() * 1000000);
		$num = rand(1,$prods);
		if ($num == 0) $num = 1;
		$sqls = "SELECT * from $table where pid = $items[$num]";
		$details = mysql_query($sqls,$conn);

		while ($rows = mysql_fetch_array($details))
		{
			$pid = $rows["pid"];
			$pitem = $rows["pitem"];
			$pdes = $rows["pdes"];
		}
		$table = "stocks";
		$sql = " SELECT pid,sitem,scat,spic,stock,color,size from $table where pid=$items[$num]";
		$rs = @mysql_query($sql,$conn)
		or die("Could not execute SQL query!!! Possibly SQL query already been executed.");

		$row=mysql_fetch_array($rs);
		$pics = $row["spic"];
		$scat=$row["scat"];
		$stocknum = $row["stock"];
	}
	else
	{
		$pitem = "N/A";
		$pdes = "N/A";
		$scat = "N/A";
	}
	if (strlen($pics) < 1)
	{	
		$pics="./images/nopic.jpg";
	}
	if (fopen($pics,"r") == NULL)
		$pics = "./images/nopic.jpg";
	if (strlen($scat) < 1)
	{
		$scat = "N/A";
	}
	echo "<tr><td align=center><font face=verdana size=1 color=navy>We recommend the following product item</font></td></tr>";
echo "<tr><td align=center><img src=$pics border=0 alt=Picture></td></tr>";
if ($prods >= 1)
echo "<tr><td><font face=verdana size=1><b>Product Item ID:</b>	$pid <a href = ./shop_showitem.php?pid=$pid></font><font color = $highlight><u>Show Item</u></font></a>";
else
echo "<tr><td><font face=verdana size=1><b>Product Item ID:</b>	N/A";	
echo "</td></tr><tr><td><font face=verdana size=1><b>Product Item   :</b>    $pitem</font>";
echo "</td></tr><tr><td><font face=verdana size=1><b>Product Description:</b> $pdes</font></td></tr>";
echo "<tr><td><font face=verdana size=1><b>Product Category:</b> $scat</font></td></tr>";
if ($stocknum >=1)
echo "<tr><td><font face=verdana size=1><b>Stock: </b> $stocknum</td></tr>";
if ($stocknum < 1)
echo "<tr><td><font face=verdana size=1><b>Stock: </b> <font face=verdana size=1 color=red>Out of Stock!</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>";
	
echo"<br><center><font face=verdana size=1 color=#000080><a href=javascript:history.back()><img src=images/previous-page.gif border = 0 height=18 alt=Previous></a></font></center><br>";
	echo("<center><img src=images/lines.gif border=0 width=450 height=4><br><font face = Verdana size = 1 color = #0000CC>Log Out Time has been updated to user log record</font><br><img src=images/lines.gif border=0 width=450 height=4><br></center>");
    }
    else
    {
     // If they were logged in and could not be logged out

echo "<center><img src=images/mouses.gif border=0><font face = Verdana size =4 color=navy>Logging Out</font><center><br><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=450 border=0>
       <TBODY>";
	echo "<tr><td align=center><center><img src=images/logoff2.gif></center></td></tr>";
	echo "<tr><td align=center><font face=verdana size=2 color=#666666>Could not log you out! To try again please click <a href='logout.php'>here</a>.</td></tr><tr><td align=center>If this problem persists please close all browser windows.</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>";
	
echo"<br><center><font face=verdana size=1 color=#000080><a href=javascript:history.back()><img src=images/previous-page.gif border = 0 height=18 alt=Previous></a></font></center><br>";

	
  } 
  }
else
 {
  // If they weren't logged in but came to this page somehow by mistake
	$table = "products";
	$sql = "SELECT * from $table";
	$products = mysql_query($sql,$conn);
	$prods = mysql_num_rows($products);

	$items = array();
	$count = 1;
	
	// only show items when there are items to show !!!
	if ($prods >= 1)
	{
		while ($row = mysql_fetch_array($products))
		{
	
			$items[$count] = $row["pid"];
			$count += 1;
		}
		srand(microtime() * 1000000);
		$num = rand(1,$prods);
		if ($num == 0) $num = 1;
			$sqls = "SELECT * from $table where pid = $items[$num]";
		$details = mysql_query($sqls,$conn);

		while ($rows = mysql_fetch_array($details))
		{
			$pid = $rows["pid"];
			$pitem = $rows["pitem"];
			$pdes = $rows["pdes"];
		}
		$table = "stocks";
		$sql = " SELECT pid,sitem,scat,spic,stock,color,size from $table where pid=$items[$num]";
		$rs = @mysql_query($sql,$conn)
		or die("Could not execute SQL query!!! Possibly SQL query already been executed.");

		$row=mysql_fetch_array($rs);
		$pics = $row["spic"];
		$scat=$row["scat"];
		$stocknum = $row["stock"];
	}
	else
	{
		$pitem = "N/A";
		$pdes = "N/A";
		$scat = "N/A";
		
	}
	if (strlen($pics) < 1)
	{	
		$pics="./images/nopic.jpg";
	}
	if (fopen($pics,"r") == NULL)
		$pics = "./images/nopic.jpg";
	if (strlen($scat) < 1)
	{
		$scat = "N/A";
	}
  echo "<tr><td align=center><font face=verdana size=1 color=#666666>You were not logged in and so have not been logged out.</td><tr><tr><td align=center>Click here to <a href='reglog.php'><u>login!</u></a></font></p></td></tr>";
echo "<tr><td align=center><font face=verdana size=1 color=navy>We recommend the following product item</font></td></tr>";
echo "<tr><td align=center><img src=$pics border=0 alt=Picture></td></tr>";
if ($prods >= 1)
echo "<tr><td><font face=verdana size=1><b>Product Item ID:</b>	$pid <a href = ./shop_showitem.php?pid=$pid></font><font color = $highlight><u>Show Item</u></font></a>";
else
echo "<tr><td><font face=verdana size=1><b>Product Item ID:</b>	N/A";	
echo "</td></tr><tr><td><font face=verdana size=1><b>Product Item   :</b>    $pitem</font>";
echo "</td></tr><tr><td><font face=verdana size=1><b>Product Description:</b> $pdes</font></td></tr>";
echo "<tr><td><font face=verdana size=1><b>Product Category:</b> $scat</font></td></tr>";
if ($stocknum >=1)
echo "<tr><td><font face=verdana size=1><b>Stock: </b> $stocknum</td></tr>";
if ($stocknum < 1)
echo "<tr><td><font face=verdana size=1><b>Stock: </b> <font face=verdana size=1 color=red>Out of Stock!</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>";
	
echo"<br><center><font face=verdana size=1 color=#000080><a href=javascript:history.back()><img src=images/previous-page.gif border = 0 height=18 alt=Previous></a></font></center><br>";
	
}

?>