<?php
  ///////////////////////////////////////////////////
  //Администрирование пользователей
  //2009 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////
  require_once "../utils/chlang.php";

  $title=$titlepage = $TEXT['login-title-edit'];
  require_once "../config_a.php";
  require_once "../utils/functions.php";
  require_once "../utils/top_adm_film.php";
  require_once "../utils/photo_functions.php";

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

  if(empty($_POST['login_id'])) {puterror($result2['skin'],$TEXT['error-login15']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['login_id'])>15) {puterror($result2['skin'],$TEXT['error-login14']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(empty($_POST['login'])) {puterror($result2['skin'],$TEXT['error-login1']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['login'])>255) {puterror($result2['skin'],$TEXT['error-login2']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['familiya'])>255) {puterror($result2['skin'],$TEXT['error-login4']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['oldpassword'])>100) {puterror($result2['skin'],$TEXT['error-login5']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['password'])>100) {puterror($result2['skin'],$TEXT['error-login5']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['password2'])>100) {puterror($result2['skin'],$TEXT['error-login6']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['name'])>255) {puterror($result2['skin'],$TEXT['error-login8']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['administrator'])>5) {puterror($result2['skin'],$TEXT['error-login9']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['user'])>5) {puterror($result2['skin'],$TEXT['error-login20']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['otchestvo'])>255) {puterror($result2['skin'],$TEXT['error-login7']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['phone'])>40) {puterror($result2['skin'],$TEXT['error-login9']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['dom_id'])>5) {puterror($result2['skin'],$TEXT['error-domain6']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['icq'])>20) {puterror($result2['skin'],$TEXT['error-login18']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['address'])>255) {puterror($result2['skin'],$TEXT['error-login20']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['skin'])>100) {puterror($result2['skin'],$TEXT['error-login21']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['skin2'])>100) {puterror($result2['skin'],$TEXT['error-login21']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['mail'])>40) {puterror($result2['skin'],$TEXT['error-login22']); require_once "../utils/bottom_adm_film.php"; exit();}
  //if(strlen($_POST['old_dom_id'])>5) {puterror($result2['skin'],$TEXT['error-domain6']); require_once "../utils/bottom_adm_film.php"; exit();}
  if(strlen($_POST['delphoto'])>5) {puterror($result2['skin'],$TEXT['error-new11']); require_once "../utils/bottom_adm_film.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_film.php"; exit();}

  $_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['icq'] = rep($_POST['icq']);
  $_POST['address'] = rep($_POST['address']);
  $_POST['skin'] = rep($_POST['skin']);
  $_POST['skin2'] = rep($_POST['skin2']);
  $_POST['administrator'] = rep($_POST['administrator']);
  $_POST['user'] = rep($_POST['user']);
  //rep ($_POST['old_dom_id']);
  $_POST['oldpassword'] = rep($_POST['oldpassword']);

  //$new_dom = $_POST['dom_id'];
  
  //Проверим надо удалять картинку или нет
  if($_POST['delphoto'] == "on")
  {
  $smallimage = "";
  //Удаляем старые картинки
      unlink($_POST['old_pho']);
  }
    else
    {

  if($_FILES['photo']['size'] > 1024*1*1024) {puterror($result2['skin'],$TEXT['error-pic3']); require_once "../utils/bottom_adm_film.php"; exit();}
    if (!empty($_FILES['photo']['tmp_name']))
      {
       $ext = strrchr($_FILES['photo']['name'], ".");
       $name = $_POST['login'];
       $name_ = "sm_".$_POST['login'];
       $image = "../photo/$name".date("YmdHis",time())."$ext";
       $smallimage = "../photo/$name_".date("YmdHis",time())."$ext";

          if (copy($_FILES['photo']['tmp_name'],  $image))
            {
            unlink($_FILES['photo']['tmp_name']);
            chmod($image, 0644);
            }
            if(!resizeimg($image, $smallimage, 130, 185))
           {
            puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm_film.php"; exit();
           }
           if(is_file($_POST['old_pho'])) {unlink($_POST['old_pho']);}
           if(is_file($image)) {unlink($image);}
     }
      else {$smallimage = $_POST['old_pho'];}
      }
  
  if($_POST['administrator'] == "on") {$administrator = "1";}
    else {$administrator = "0";}

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

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

$query_iskh = "SELECT * FROM `tbl_logins` WHERE `login_id`='".$_POST['login_id']."' ";
  $result_iskh = mysqli_query($dbcnx,$query_iskh);
  if(!$result_iskh) {puterror($result2['skin'],$TEXT['error-tbl7']); require_once "../utils/bottom_adm_film.php"; exit();}
    $result_iskh2 = mysqli_fetch_array($result_iskh);
    $ident = $_POST['login_id'];
    $oldident = $_COOKIE['ident'];

  if(!empty($iduser)) {$new_user_id = ", `usr_id` = '".$_POST['user_id']."'";}
    else {$new_user_id = "";}

  //Проверяем правильность пароля и вычисляем его для хранения в базе
  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_film.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_film.php"; exit();}
     }
     else {puterror($result2['skin'],$TEXT['error-login17']); require_once "../utils/bottom_adm_film.php"; exit();}
    }
    else {$passwd = "";}
 }
  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-login3'];
  $what = $TEXT['login-login']."-".$result_iskh2['login']."; ".$TEXT['log-login8']."-".$result_iskh2['familiya']." ".$result_iskh2['name']." ".$result_iskh2['otchestvo']."; ".$TEXT['log-id'].$_POST['login_id'];
  $who = $result2['familiya']." ".$result2['name'];
  //*************************************
  $query = "UPDATE `tbl_logins` SET 
  `login` = '".$_POST['login']."',
`photo_user` = '$smallimage',
`familiya` = '".$_POST['familiya']."',
`name` = '".$_POST['name']."', 
`otchestvo` = '".$_POST['otchestvo']."', 
`phone` = '".$_POST['phone']."', 
`e_mail` = '".$_POST['mail']."', 
`dom_id` = '".$_POST['dom_id']."', 
`icq` = '".$_POST['icq']."', 
`address_user` = '".$_POST['address']."',
".$passwd."
`administrator` = '$administrator',
`user` = '$user',
`skin` = '".$_POST['skin']."',
`skin2` = '".$_POST['skin2']."'
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=../film-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_film.php"; exit();}
  } else {puterror($result2['skin'],$TEXT['error-login18']); require_once "../utils/bottom_adm_film.php"; exit();}
  require_once "../utils/bottom_adm_film.php";
?>