<?php
set_magic_quotes_runtime(0);
include ("txt/vars.php");
$link=mysql_connect("$mysqlhost","$mysqllogin","$mysqlpass") or die("Could not connect");
mysql_select_db($mysqldb) or die("Could not select database");
include ("auth_user.php");
if (isset($_POST['com'])) {
$com=$_POST['com'];
}
if (!isset($com) || $com==" ") {
die('Не выбран лот!');
} 
$bid=$_POST['bid'];
if (!isset($bid)) {
echo "<center><b>Не указана ставка!</b></center>";
echo "<meta http-equiv='refresh' content='2 url=commodity.php?com=$com'>";
exit;
}
$com=mysql_escape_string($com);
$bid=mysql_escape_string($bid);
$nowtime=time();
$query="select * from goods where (first='$com')";
$res=mysql_query($query);
$number_of_rows=mysql_num_rows($res);
if ($number_of_rows<=0) {
echo "<center><b>Такой товар не существует!</b></center>";
echo "<meta http-equiv='refresh' content='2 url=index.php'>";
exit;
}
$row=mysql_fetch_assoc($res);
$name_of_good=$row["name"];
$name_of_good=stripslashes($name_of_good);
$srok=$row["srok"];
$type=$row["type"];
$price=$row["price"];
$mprice=$row["mprice"];
$quantity=$row["quantity"];
$bid_of_good=$row["bid"];
$seller=$row["seller"];
$buyer=$row["buyer"];
$id=$row["first"];
$stay=$srok-$nowtime;
if ($stay<=0) {
echo "<center><b>Время данного лота истекло!</b></center>";
echo "<meta http-equiv='refresh' content='2 url=commodity.php?com=$com'>";
exit;
}
$query="select name,email from users where (crcname='$seller')";
$res_seller=mysql_query($query);
$row_seller=mysql_fetch_assoc($res_seller);
$email_of_seller=$row_seller["email"];
$name_of_seller=$row_seller["name"];
$query="select name,email from users where (crcname='$crc_username')";
$res_buyer=mysql_query($query);
$row_buyer=mysql_fetch_assoc($res_buyer);
$email_of_buyer=$row_buyer["email"];
$name_of_buyer=$row_buyer["name"];

$host=getenv("HTTP_HOST");

$query="select admin_email,topic_to_seller,text_to_seller,topic_to_buyer,text_to_buyer from mail_set where (first='1')";
$res_mail_set=mysql_query($query);
$row_mail=mysql_fetch_assoc($res_mail_set);
$admin_email=$row_mail["admin_email"];
$admin_email=stripslashes($admin_email);
$topic_to_seller=$row_mail["topic_to_seller"];
$topic_to_seller=stripslashes($topic_to_seller);
$topic_to_seller=str_replace("*name_of_good*",$name_of_good,$topic_to_seller);
$text_to_seller=$row_mail["text_to_seller"];
$text_to_seller=stripslashes($text_to_seller);
$text_to_seller=str_replace("*name_of_seller*",$name_of_seller,$text_to_seller);
$text_to_seller=str_replace("*name_of_buyer*",$name_of_buyer,$text_to_seller);
$text_to_seller=str_replace("*name_of_good*",$name_of_good,$text_to_seller);
$text_to_seller=str_replace("*host*",$host,$text_to_seller);
$text_to_seller=str_replace("*bid*",$bid,$text_to_seller);
$text_to_seller=str_replace("*email_of_buyer*",$email_of_buyer,$text_to_seller);
$topic_to_buyer=$row_mail["topic_to_buyer"];
$topic_to_buyer=stripslashes($topic_to_buyer);
$topic_to_buyer=str_replace("*name_of_good*",$name_of_good,$topic_to_buyer);
$text_to_buyer=$row_mail["text_to_buyer"];
$text_to_buyer=stripslashes($text_to_buyer);
$text_to_buyer=str_replace("*name_of_buyer*",$name_of_buyer,$text_to_buyer);
$text_to_buyer=str_replace("*name_of_good*",$name_of_good,$text_to_buyer);
$text_to_buyer=str_replace("*host*",$host,$text_to_buyer);
$text_to_buyer=str_replace("*bid*",$bid,$text_to_buyer);
$text_to_buyer=str_replace("*name_of_seller*",$name_of_seller,$text_to_buyer);

if ($type=="one") {
if ($bid_of_good==0 && $bid<$price) {
echo "<center><b>Ставка должна быть не менее начальной цены!</b></center>";
echo "<meta http-equiv='refresh' content='2 url=commodity.php?com=$com'>";
exit;
}
if ($bid_of_good>=$price && $bid<=$bid_of_good) {
echo "<center><b>Ставка должна быть более текущей ставки!</b></center>";
echo "<meta http-equiv='refresh' content='2 url=commodity.php?com=$com'>";
exit;
}
$query="update goods set bid='$bid',buyer='$crc_username' where (first='$com')";
if ($bid>=$mprice) {
$query="update goods set bid='$bid',buyer='$crc_username',srok='$nowtime' where (first='$com')";
echo "<script language='JavaScript'>\n";
echo "alert(\"Поздравляем! Вы выйграли аукцион по данному товару! Продавец получит от нас уведомление.\");\n";
echo "</script>";
@mail("$email_of_seller","$topic_to_seller","$text_to_seller","From: $email_of_buyer\r\nReply-To: $email_of_buyer\r\nContent-Type: text/html; charset=windows-1251\r\nContent-Transfer-Encoding: 8bit");
@mail("$email_of_buyer","$topic_to_buyer","$text_to_buyer","From: $admin_email\r\nReply-To: $admin_email\r\nContent-Type: text/html; charset=windows-1251\r\nContent-Transfer-Encoding: 8bit");
}
mysql_query($query);
}
if ($type=="mult" || $type=="unlim") {
if ($quantity<1) {
echo "<center><b>Извините, данный товар закончился!</b></center>";
echo "<meta http-equiv='refresh' content='2 url=commodity.php?com=$com'>";
exit;
}
if ($bid!=$price) {
echo "<center><b>Ставка должна быть равна цене, так как продаются одинаковые товары!</b></center>";
echo "<meta http-equiv='refresh' content='2 url=commodity.php?com=$com'>";
exit;
}
$freq_of_buyer=substr_count($buyer,"<a href=\"profile.php?user=$crc_username\">$name_of_buyer</a>");
if ($freq_of_buyer>10) {
echo "<center><b>Вы слишком часто покупаете данный товар! Свяжитесь с продавцом для оптовых поставок.</b></center>";
echo "<meta http-equiv='refresh' content='2 url=commodity.php?com=$com'>";
exit;
}
if ($buyer=="no") {
$buyers="<a href=\"profile.php?user=$crc_username\">$name_of_buyer</a> ";
}
else {
$buyers=$buyer."<a href=\"profile.php?user=$crc_username\">$name_of_buyer</a> ";
}
if ($type=="mult") {
$new_quantity=$quantity-1;
}
else {
$new_quantity=$quantity;
}
$query="update goods set buyer='$buyers',quantity='$new_quantity' where (first='$com')";
mysql_query($query);
echo "<script language='JavaScript'>\n";
echo "alert(\"Поздравляем! Вы купили данный товар! Продавец получит от нас уведомление.\");\n";
echo "</script>";
@mail("$email_of_seller","$topic_to_seller","$text_to_seller","From: $email_of_buyer\r\nReply-To: $email_of_buyer\r\nContent-Type: text/html; charset=windows-1251\r\nContent-Transfer-Encoding: 8bit");
@mail("$email_of_buyer","$topic_to_buyer","$text_to_buyer","From: $admin_email\r\nReply-To: $admin_email\r\nContent-Type: text/html; charset=windows-1251\r\nContent-Transfer-Encoding: 8bit");
}
mysql_close ($link);
?>
<script language="JavaScript">
location.href="<?php echo "commodity.php?com=$com"; ?>";
</script>