<?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['upr-recod-tit'];

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

  if($result2['administrator'] == 0) {puterror($result2['skin'],$TEXT['error-loginform2']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['kfr2'])>255) {puterror($result2['skin'],$TEXT['error-upr10']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['kfr'])>255) {puterror($result2['skin'],$TEXT['error-upr2']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['kfr2'])) {puterror($result2['skin'],$TEXT['error-upr8']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['kfr'])) {puterror($result2['skin'],$TEXT['error-upr8']); require_once "../utils/bottom_adm.php"; exit();}

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

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

  $_POST['kfr'] = rep($_POST['kfr']);
  $_POST['kfr2'] = rep($_POST['kfr2']);

  $_POST['kfr'] = trim($_POST['kfr']);
  $_POST['kfr2'] = trim($_POST['kfr2']);

  if($_POST['comanda'] == "scp") {$log_com = $TEXT['upr-vnesh'];}
    else {$log_com = $TEXT['upr-vnutr'];}

  $query_log1 = "SELECT
     AES_DECRYPT(pwd,'".$_POST['kfr']."') AS pwd
     FROM tbl_upr
     WHERE `pwd` !='' LIMIT 1";
     $result_log1 = mysqli_query($dbcnx,$query_log1);
     if(!$result_log1) {puterror($result2['skin'],$TEXT['error-tbl3']); require_once "../utils/bottom_adm.php"; exit();}
       $result_upr1 = mysqli_fetch_array($result_log1,MYSQLI_ASSOC);

  if(empty($result_upr1['pwd'])) {puterror($result2['skin'],$TEXT['error-upr7']); require_once "../utils/bottom_adm.php"; exit();}

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-upr7'];
  $what = $TEXT['log-upr7'];
  $who = $result2['familiya']." ".$result2['name'];
  //*************************************

  $query = "UPDATE `tbl_upr` SET 
  `pwd` = AES_ENCRYPT(AES_DECRYPT(pwd,'".$_POST['kfr']."'),'".$_POST['kfr2']."')
  WHERE `pwd`!=''";
  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))
           {
           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-tbl3']); require_once "../utils/bottom_adm.php"; exit();}
  require_once "../utils/bottom_adm.php";
?>