<?php
  ///////////////////////////////////////////////////
  //2010 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

  $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
  if(is_file ("../lang/".$lang."/".$lang.".php")) {include "../lang/".$lang."/".$lang.".php";}
     else {include "../lang/ru/ru.php";}

  $title = $TEXT['login-title-edit'];

  require_once "../config_a.php";
  require_once "../utils/functions.php";
  require_once "../utils/top_adm.php";

  if($result2['administrator'] == 0 && $result2['login_id'] !== $_POST['login_id']) {puterror($result2['skin'],$TEXT['error-loginform2']); require_once "../utils/bottom_adm.php"; exit();}

  if(empty($_POST['login_id'])) {puterror($result2['skin'],$TEXT['error-login15']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['login_id'])>15) {puterror($result2['skin'],$TEXT['error-login14']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['login'])) {puterror($result2['skin'],$TEXT['error-login1']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['login'])>255) {puterror($result2['skin'],$TEXT['error-login2']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['familiya'])>255) {puterror($result2['skin'],$TEXT['error-login4']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['oldpassword'])>100) {puterror($result2['skin'],$TEXT['error-login5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['password'])>100) {puterror($result2['skin'],$TEXT['error-login5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['password2'])>100) {puterror($result2['skin'],$TEXT['error-login6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['name'])>255) {puterror($result2['skin'],$TEXT['error-login8']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['administrator'])>5) {puterror($result2['skin'],$TEXT['error-login9']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['user'])>5) {puterror($result2['skin'],$TEXT['error-login20']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['otchestvo'])>255) {puterror($result2['skin'],$TEXT['error-login7']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['phone'])>40) {puterror($result2['skin'],$TEXT['error-login9']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['dom_id'])>5) {puterror($result2['skin'],$TEXT['error-domain6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['skin'])>100) {puterror($result2['skin'],$TEXT['error-login21']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['mail'])>40) {puterror($result2['skin'],$TEXT['error-login22']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['fltr_size'])>4) {puterror($result2['skin'],$TEXT['error-login23']); require_once "../utils/bottom_adm.php"; exit();}

 if(isset($_POST['login'])) {$login=trim($_POST['login']);}
    else {$login="";}
     if(empty($login)) {puterror($result2['skin'],$TEXT['error-login3']); require_once "../utils/bottom_adm.php"; exit();}

  $_POST['login_id'] = rep($_POST['login_id']);
  $_POST['login'] = rep($_POST['login']);
  $_POST['familiya'] = rep($_POST['familiya']);
  $_POST['name'] = rep($_POST['name']);
  $_POST['otchestvo'] = rep($_POST['otchestvo']);
  $_POST['password'] = rep($_POST['password']);
  $_POST['password2'] = rep($_POST['password2']);
  $_POST['phone'] = rep($_POST['phone']);
  $_POST['mail'] = rep($_POST['mail']);
  $_POST['dom_id'] = rep($_POST['dom_id']);
  $_POST['skin'] = rep($_POST['skin']);
  $_POST['administrator'] = rep($_POST['administrator']);
  $_POST['user'] = rep($_POST['user']);
  $_POST['oldpassword'] = rep($_POST['oldpassword']);
  $_POST['fltr_size'] = rep($_POST['fltr_size']);
  if($_POST['fltr_size'] < 1) {$_POST['fltr_size'] = 0;}

  if($result2['administrator'] == 1)
  {
  if($_POST['administrator'] == "on") {$administrator = "1";}
    else {$administrator = "0";}
  }
  else
  {
    $administrator = "0";
  }
  
  if($_POST['user'] == "on") {$user = "1";}
    else {$user = "0";}

  if($_POST['chk_upd'] == "on") {$chk_upd = "yes";}
    else {$chk_upd = "no";}

  if($_POST['chk_maint'] == "on") {$chk_maint = "yes";}
    else {$chk_maint = "no";}

  if(empty($_POST['familiya'])) {$_POST['familiya'] = "";}
    else {$_POST['familiya'] = $_POST['familiya'];}

  if(empty($_POST['name'])) {$_POST['name'] = "";}
    else {$_POST['name'] = $_POST['name'];}

  if(empty($_POST['otchestvo'])) {$_POST['otchestvo'] = "";}
    else {$_POST['otchestvo'] = $_POST['otchestvo'];}

  $query10 = "SELECT * FROM `tbl_logins` WHERE `login_id`='".$_POST['login_id']."' ";
  $result10 = mysqli_query($dbcnx,$query10);
  if(!$result10) {puterror($result2['skin'],$TEXT['error-tbl4']); require_once "../utils/bottom_adm.php"; exit();}
      $result_lib = mysqli_fetch_array($result10,MYSQLI_ASSOC);
      if($_POST['login']!=$result_lib['login']) {
          $query9 = "SELECT `login` FROM `tbl_logins` WHERE `login`='".$_POST['login']."' ";
            $result9 = mysqli_query($dbcnx,$query9);
              if(!$result9) {puterror($result2['skin'],$TEXT['error-tbl4']); require_once "../utils/bottom_adm.php"; exit();}
                 if(mysqli_num_rows($result9)>0) {puterror($result2['skin'],$TEXT['error-double']); require_once "../utils/bottom_adm.php"; exit();}
                   else {$name_login = "`login` = '".$_POST['login']."', ";}
        }
       else {$name_login = "";}

      $ident = $_POST['login_id'];
      $oldident = $_COOKIE['ident'];

  if($chk_maint == "yes" && $result_lib['maintenance'] == "no") {$log_maint = ", ".$TEXT['global-enable']."- ".$TEXT['login-title-maintenance']."";}
    elseif($chk_maint == "no" && $result_lib['maintenance'] == "yes") {$log_maint = ", ".$TEXT['global-disable']."- ".$TEXT['login-title-maintenance']."";}
        else {$log_maint = ""; }
 
  //Проверяем правильность пароля и вычисляем его для хранения в базе
  if($result2['administrator'] == 1)
  {
   if(!empty($_POST['password2']))
    {
    if($_POST['password'] == $_POST['password2']) {$passwd = " `pass` = '".md5($_POST['password'])."', ";}
     else {puterror($result2['skin'],$TEXT['error-login19']); require_once "../utils/bottom_adm.php"; exit();}
    } else {$passwd = "";}
  }
  else
  {
  if(!empty($_POST['oldpassword']))
    {
     $passwrd = md5($_POST['oldpassword']);
     if($passwrd == $result2['pass'])
     {
       if($_POST['password'] == $_POST['password2']) {$passwd = " `pass` = '".md5($_POST['password'])."', ";}
          else {puterror($result2['skin'],$TEXT['error-login19']); require_once "../utils/bottom_adm.php"; exit();}
     }
     else {puterror($result2['skin'],$TEXT['error-login17']); require_once "../utils/bottom_adm.php"; exit();}
    }
    else {$passwd = "";}
  }

  //*************************************
  //Информация для лога

  if($result_lib['login'] !== $_POST['login']) {$log_login = $TEXT['log-login4'].$result_lib['login'].$TEXT['log-nd8'].$_POST['login']."<br />";}
    else {$log_login = "";}

  if($result_lib['login'] !== $_POST['login']) {$log_login_visible = $_POST['login'];}
    else {$log_login_visible = $result_lib['login'];}

  if($result_lib['familiya'] !== $_POST['familiya']) {$log_familiya = $TEXT['log-login6'].$log_login_visible." ".$TEXT['log-login11'].$result_lib['familiya'].$TEXT['log-nd8'].$_POST['familiya']."<br />";}
    else {$log_familiya = "";}

  if($result_lib['name'] !== $_POST['name']) {$log_name = $TEXT['log-login6'].$log_login_visible." ".$TEXT['log-login5'].$result_lib['name'].$TEXT['log-nd8'].$_POST['name']."<br />";}
    else {$log_name = "";}

  if($result_lib['otchestvo'] !== $_POST['otchestvo']) {$log_otchestvo = $TEXT['log-login6'].$log_login_visible." ".$TEXT['log-login12'].$result_lib['otchestvo'].$TEXT['log-nd8'].$_POST['otchestvo']."<br />";}
    else {$log_otchestvo = "";}

  if($result_lib['fltr_size'] !== $_POST['fltr_size']) {$log_fltr_size = $TEXT['log-login6'].$log_login_visible." ".$TEXT['log-login14'].$result_lib['fltr_size'].$TEXT['log-nd8'].$_POST['fltr_size']."<br />";}
    else {$log_fltr_size = "";}

 if($passwd !== "") {$log_passwd = $TEXT['log-login6']."&laquo;".$log_login_visible."&raquo; (".$result_lib['familiya']." ".$result_lib['name']." ".$result_lib['otchestvo'].")".$TEXT['log-login7']."<br />";}
    elseif($passwd == "" && !empty($_POST['password2'])) {$log_passwd = $TEXT['log-login6']."&raquo;".$log_login_visible."&laquo; (".$result_lib['familiya']." ".$result_lib['name']." ".$result_lib['otchestvo'].")".$TEXT['log-login13']."<br />";}
       else {$log_passwd = "";}

  if($result_lib['administrator'] !== $administrator) {$log_administrator = $TEXT['log-login6'].$log_login_visible." ".$TEXT['log-login9'].$result_lib['administrator'].$TEXT['log-nd8'].$administrator."<br />";}
    else {$log_administrator = "";}

  if($result_lib['user'] !== $user) {$log_user = $TEXT['log-login6'].$log_login_visible." ".$TEXT['log-login10'].$result_lib['user'].$TEXT['log-nd8'].$user."<br />";}
    else {$log_user = "";}

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-login3'];
  $what = $log_login_visible.", ".$log_login." ".$log_familiya." ".$log_name." ".$log_otchestvo." ".$log_fltr_size." ".$log_passwd." ".$log_administrator." ".$log_user." ".$TEXT['log-id'].$_POST['login_id'].$log_maint;
  $who = $result2['familiya']." ".$result2['name'];
  //*************************************

  $query = "UPDATE `tbl_logins` SET 
  ".$name_login."
  `familiya` = '".$_POST['familiya']."',
  `name` = '".$_POST['name']."',
  `otchestvo` = '".$_POST['otchestvo']."',
  `phone` = '".$_POST['phone']."',
  `e_mail` = '".$_POST['mail']."',
  `dom_id` = '".$_POST['dom_id']."',
  `fltr_size` = '".$_POST['fltr_size']."',
  ".$passwd."
  `administrator` = '$administrator',
  `user` = '$user',
  `skin` = '".$_POST['skin']."',
  `chk_upd` = '".$chk_upd."',
  `maintenance` = '".$chk_maint."'
  WHERE `login_id`='".$_POST['login_id']."'";
  if(mysqli_query($dbcnx,$query))
  {
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysqli_query($dbcnx,$query2))
           {
             if($oldident == $ident)
             {
             // Автоматически перелогиниваемся
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=../index.php' />
                   </head></html>";
             }
             else
              {
               // Автоматически осуществляем переход на главную страницу
               echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=index.php' />
                   </head></html>";
              }
           } else {puterror($result2['skin'],$TEXT['error-log']); require_once "../utils/bottom_adm.php"; exit();}
  } else {puterror($result2['skin'],$TEXT['error-tbl4']); require_once "../utils/bottom_adm.php"; exit();}
  require_once "../utils/bottom_adm.php";
?>