<?php
session_start();
include_once "inc/db_connect.php";
include_once "inc/fnc.php";
header('Content-Type: text/html;charset=utf8');
$err = "";
if(isset($_POST['regok'])){unset($_SESSION['reg_ok']);}
if(!isset($_SESSION['user'])){
	header("Location: index.php?");
	exit();
}else{
	$data_user = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE nick='".$_SESSION['user']."'")) or die(mysql_error());
include 'head.php';
if($data_user['level']<3)
{
echo "<div class='menu'>";
echo' <center>Смена аватара доступна с 3 уровня.</center>';
echo "</div>";
echo "<img src='/img/levelmal.png'>";
include 'foot.php';
exit;
}
?>
<div class="title">Аватарка</div>
<?php
if(isset($_POST["submit"])) 
{ 
  mt_srand(time());
  $filename = "avatars/".mt_rand(0,104857632).".jpg";
  $size = $_FILES['ava']['size'];
  $mime_type = $_FILES['ava']['type'];
  if(move_uploaded_file($_FILES['ava']['tmp_name'],"avatars/".basename($filename)))
  {
    $res = mysql_query("UPDATE `users` SET avatar='".$filename."' WHERE nick='".$_SESSION['user']."'") or die("fuckin query! ".mysql_error());
    if($res){
      echo "<meta http-equiv='refresh' content='0; URL=avatar.php' />";
		exit();
	}else{
      false;
	}
  }
  else{
    echo '<div class="err">Выберите файл</div>';
}
}
?>
<div class="menu"><center>
Ваш текущий аватар:<br/><br/>
<img src="<?=$data_user['avatar']?>" alt="." width="100" height="100" /></center><br/>
<form action="" method="post" enctype="multipart/form-data">
<div class="message">
<input type="file" name="ava" /><br/></div>
<center>
<input type="submit" name="submit" value="Изменить" class="btn" />
</center>
</form>
</div></div>
</div>
</body>
</html>
<?
}
include 'foot.php';
?>