<?php
  ///////////////////////////////////////////////////
  // Вход в систему
  ///////////////////////////////////////////////////
  require_once "utils/chlang_osn.php";
  require_once "utils/functions.php";
  require_once "config.php";

?>
<html>
<head>
<meta charset="utf-8" />
<meta name="ROBOTS" content="NOINDEX, NOFOLLOW" />
<title><?php echo $TEXT['global-logout-tit']; ?></title>
<link rel="StyleSheet" type="text/css" href="skins/base/reader.css" />
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
<?php
  
  if(empty($_POST['login'])) {puterror3('',$TEXT['error-login1']); exit();}
  if(empty($_POST['password'])) {puterror3('',$TEXT['error-login23']); exit();}
  if(strlen($_POST['login'])>200) {puterror3('',$TEXT['error-login2']); exit();}
  if(strlen($_POST['password'])>200) {puterror3('',$TEXT['error-login5']); exit();}

  if(isset($_POST['login'])) {$login=trim($_POST['login']);}
    else {$login="";}
     if(empty($login)) {puterror3('',$TEXT['error-login3']); exit();}

  if(isset($_POST['password'])) {$password=trim($_POST['password']);}
    else {$password="";}
     if(empty($password)) {puterror3('',$TEXT['error-login10']); exit();}

  $_POST['login'] = str_replace("'","`",$_POST['login']);
  $_POST['password'] = str_replace("'","`",$_POST['password']);
  $_POST['login'] = htmlspecialchars(stripslashes($_POST['login']));
  $_POST['password'] = htmlspecialchars(stripslashes($_POST['password']));

  $query = "SELECT login_id, login, pass FROM `tbl_logins` WHERE `login`='".$_POST['login']."' ";
  $result = mysqli_query($dbcnx,$query);
  if(!$result) {puterror3("'',".$TEXT['error-tbl7']); exit();}
      $result2 = mysqli_fetch_array($result);
      $ident = $result2['login_id'];
  //Проверяем правильность пароля и вычисляем его для хранения в базе
  $passwrd = md5($_POST['password']);
     if($passwrd == $result2['pass'])
     {
       setcookie("passwrd", $passwrd, time()+3600*8);
       setcookie("ident", $ident, time()+3600*8);

       echo "<html><head>
             <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=main.php'>
             </head></html>";
     }
     else {puterror3('',$TEXT['error-loginform1']); exit();}
?>
</body>
</html>