<?
//ob_start();

// TODO 2005:
// We should write a cookie the first time someone bids,
// that records their name, email and phone. Then
// when they return, we should pre-pop that info on input.php

$item_id $HTTP_POST_VARS["item_id"];
$name $HTTP_POST_VARS["name"];
$email $HTTP_POST_VARS["email"];
$phone $HTTP_POST_VARS["phone"];
$bid $HTTP_POST_VARS["bid"];
$remember $HTTP_POST_VARS["remember"];

if(!isset(
$HTTP_COOKIE_VARS[$auction_name]) && $remember=='true') {
setcookie("auction_name"$nametime()+259200"/"".networkscience.com");
setcookie("auction_email"$emailtime()+259200"/"".networkscience.com");
setcookie("auction_phone"$phonetime()+259200"/"".networkscience.com");
}

include(
"./randchar.php");
include(
"./inc_dbconn.php");

//addslashes to name just in case
$name addslashes($name);

// make a confirmation number
// 1st part is a random 6 digit number
$first_part randchar(6,"num");

// 2nd part is the 1st 6 characters of the item_id
//$second_part = substr($item_id, 0, 6);
// just use the whole item_id
$second_part $item_id;
$conf_num $first_part $second_part;

// fake token to push the conf number off the address bar
$token randchar(50"alnum");

$connection mysql_connect($server,$username,$password);
mysql_select_db($dbname$connection) or die ("Database not available");
  
// TODO 2005 ::
// We need to check that multiple instances of the
// same name/alias don't make it into the dbase, so:
// 1. query the bid table for the name
// 2. if the name exists, get the e-mail address assoc'd with it
// 3. compare to the email address in this transaction
// 4. if they're the same, proceed normally
// 5. if they're different then someone is using a dup alias
// 6. re-direct to new error page : 'badalias.php'

$query_check "SELECT name, email FROM bids WHERE name='$name'";
$result_check mysql_query($query_check);
$row_check mysql_num_rows($result_check);
$row_check2 mysql_fetch_array($result_check);

if(
$row_check == || $row_check2[1] == $email) {

    
// do query to insert bid info into db
        
$query "INSERT INTO bids VALUES (NULL, '" $item_id "', '" $name "' , '" $email "', '" $phone "', '" $bid "',NULL, '" $conf_num "')";
        
$result mysql_query($query) or die("Query failed" mysql_error());

    
// this should work as error checking
    // if the insert transaction fails, re-direct to error page

    
if(!$result) {

        print 
"<html><head><META HTTP-EQUIV=Refresh CONTENT=\"0; URL=error.php\">"
        print 
"</head>";
        exit;

    } else {

        
// pull the top query from the bids table for this item
        // it SHOULD be the one we just wrote into the db...
        
$query2 "SELECT bid, name FROM bids where item_id='$item_id' order by bid desc limit 1";
        
$result2 mysql_query($query2) or die ("Query failed - get from bids");
        
$row2 mysql_fetch_array($result2);

        
//...but maybe it isn't
        // this might happen where the current bid changes while slowpoke
        // is staring at the input page
        // if it happens, we need to reject their bid and tell them what happened.

        
if ($bid $row2[0]) {
            print 
"<html><head><META HTTP-EQUIV=Refresh CONTENT=\"0; URL=badbid.php\">";
            print 
"</head>";
            exit;

        } else {

            
// write into items table
            
$query3 "UPDATE items SET bid='$row2[0]', name='$row2[1]' WHERE item_id='$item_id'";
            
$result3 mysql_query($query3) or die ("Query failed - write to items");

            
// re-direct to confirm page
            
print "<html><head><META HTTP-EQUIV=Refresh CONTENT=\"0; URL=confirm.php?token=$token&conf_num=$conf_num\">";
            print 
"</head>";
            exit;

        }
    }
} else {
    print 
"<html><head><META HTTP-EQUIV=Refresh CONTENT=\"0; URL=badalias.php\">";
    print 
"</head>";
    exit;
}






?>