<?php
set_magic_quotes_runtime(0);
include ("txt/vars.php");
$username=$_POST['username'];
$username=str_replace ('"',"'",$username);
$username=substr ($username,0,30);
$username=trim ($username);
$username=htmlspecialchars ($username);
$username=mysql_escape_string($username);
$username=eregi_replace ("[\']","",$username);

$password=$_POST['password'];
$password=str_replace ('"',"'",$password);
$password=substr ($password,0,100);
$password=trim ($password);
$password=htmlspecialchars ($password);
$password=mysql_escape_string($password);
$password=eregi_replace ("[\']","",$password);

$email=$_POST['email'];
$email=str_replace ('"',"'",$email);
$email=substr ($email,0,70);
$email=trim ($email);
$email=htmlspecialchars ($email);
$email=mysql_escape_string($email);

$firstname=$_POST['firstname'];
$firstname=str_replace ('"',"'",$firstname);
$firstname=substr ($firstname,0,30);
$firstname=trim ($firstname);
$firstname=htmlspecialchars ($firstname);
$firstname=mysql_escape_string($firstname);

$lastname=$_POST['lastname'];
$lastname=str_replace ('"',"'",$lastname);
$lastname=substr ($lastname,0,50);
$lastname=trim ($lastname);
$lastname=htmlspecialchars ($lastname);
$lastname=mysql_escape_string($lastname);

$country=$_POST['country'];
$country=str_replace ('"',"'",$country);
$country=substr ($country,0,50);
$country=trim ($country);
$country=htmlspecialchars ($country);
$country=mysql_escape_string($country);

$city=$_POST['city'];
$city=str_replace ('"',"'",$city);
$city=substr ($city,0,50);
$city=trim ($city);
$city=htmlspecialchars ($city);
$city=mysql_escape_string($city);

$img_string=$_POST['img_string'];
$img_string=mysql_escape_string($img_string);

if (getenv('HTTP_X_FORWARDED_FOR')) {
$ip=getenv('HTTP_X_FORWARDED_FOR');
}
else {
$ip=getenv('REMOTE_ADDR');
}
$os=getenv("HTTP_USER_AGENT");
$d=date("m");
$check=$d.$ip.$os;
$check=md5($check);
$check=substr($check,0,6);

$link=mysql_connect("$mysqlhost","$mysqllogin","$mysqlpass") or die("Could not connect");
mysql_select_db($mysqldb) or die("Could not select database");
$query="select name from users where (name='$username')";
$result=mysql_query($query);
$num_of_such_users=mysql_num_rows($result);
if ($username!="" && $password!="" && $email!="" && $firstname!="" && $lastname!="" && $country!="" && $city!="" && eregi ("@",$email) && $num_of_such_users<=0) {
SetCookie ("cookname_auc",$username,time()+31536000);
SetCookie ("cookpass_auc",$password,time()+31536000);
}

if ($username=="" || $password=="" || $email=="" || $firstname=="" || $lastname=="" || $country=="" || $city=="") {
echo "<center><b>Заполните все поля!</b></center>";
echo "<meta http-equiv='refresh' content='2 url=reg.php'>";
exit;
}
if ($img_string!=$check) {
echo "<center><b>Вы ввели неправильную строку на картинке!</b></center>";
echo "<meta http-equiv='refresh' content='2 url=reg.php'>";
exit;
}
$len=strlen($email);
if (!eregi ("@",$email) || $len<5) {
echo "<center><b>Вы ввели ошибочный e-mail!</b></center>";
echo "<meta http-equiv='refresh' content='2 url=reg.php'>";
exit;
}
if ($num_of_such_users>0) {
echo "<center><b>Пользователь с именем $username уже зарегистрирован!</b></center>";
echo "<meta http-equiv='refresh' content='2 url=reg.php'>";
exit;
}
$pass2=md5 ($password);
$crc_username=crc32($username);
$query="insert into users(name,pass,email,firstname,lastname,country,city,crcname) values ('$username','$pass2','$email','$firstname','$lastname','$country','$city','$crc_username')";
mysql_query($query);
$host=getenv("HTTP_HOST");
$query="select admin_email,reg_topic,reg_text from mail_set where (first='1')";
$res=mysql_query($query);
$row=mysql_fetch_assoc($res);
$admin_email=$row["admin_email"];
$admin_email=stripslashes($admin_email);
$reg_topic=$row["reg_topic"];
$reg_topic=stripslashes($reg_topic);
$reg_topic=str_replace("*host*",$host,$reg_topic);
$reg_text=$row["reg_text"];
$reg_text=stripslashes($reg_text);
$reg_text=str_replace("*firstname*",$firstname,$reg_text);
$reg_text=str_replace("*lastname*",$lastname,$reg_text);
$reg_text=str_replace("*host*",$host,$reg_text);
$reg_text=str_replace("*username*",$username,$reg_text);
$reg_text=str_replace("*password*",$password,$reg_text);
@mail("$email","$reg_topic","$reg_text","From: $admin_email\r\nReply-To: $admin_email\r\nContent-Type: text/html; charset=windows-1251\r\nContent-Transfer-Encoding: 8bit");
@mail("$admin_email","Зарегистрирован пользователь $username", "Зарегистрирован пользователь $username (Пароль: $password E-mail: $email Имя: $firstname Фамилия: $lastname Страна: $country Город: $city IP: $ip Система: $os)", "From: $email\r\nContent-Type: text/plain; charset=windows-1251\r\nContent-Transfer-Encoding: 8bit");
mysql_close ($link);
?>
<script language="JavaScript">
location.href="cp.php";
</script>