<?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['param_edit'];

  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(empty($_POST['cl_id'])) {puterror($result2['skin'],$TEXT['param_err7']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['cl_id'])>12) {puterror($result2['skin'],$TEXT['param_err8']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['tip'])) {puterror($result2['skin'],$TEXT['param_err1']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['tip'])>255) {puterror($result2['skin'],$TEXT['param_err2']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['paramdays'])>20) {puterror($result2['skin'],$TEXT['param_err3']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['sizecount'])>20) {puterror($result2['skin'],$TEXT['param_err4']); require_once "../utils/bottom_adm.php"; exit();}

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

  $_POST['cl_id'] = rep($_POST['cl_id']);
  $_POST['tip'] = rep($_POST['tip']);
  $_POST['paramdays'] = rep($_POST['paramdays']);
  $_POST['sizecount'] = rep($_POST['sizecount']);
  $_POST['paramon'] = rep($_POST['paramon']);
  $_POST['sizeon'] = rep($_POST['sizeon']);

  if($_POST['paramon'] == "on" && $_POST['sizeon'] == "on") {puterror($result2['skin'],$TEXT['param_err5']); require_once "../utils/bottom_adm.php"; exit();}

  if($_POST['paramon'] == "on") {$by_data = $_POST['paramdays'];}
    else {$by_data = "0";}

  if($_POST['sizeon'] == "on") {$by_count = $_POST['sizecount'];}
    else {$by_count = "0";}

  if($_POST['tip'] == "tbl_logs" && $_POST['paramon'] == "on") {$col_name = "data";}
   elseif($_POST['tip'] == "tbl_logs" && $_POST['sizeon'] == "on") {$col_name = "log_id";}

  $query10 = "SELECT `col_tbl_name`, `by_data`, `by_count` FROM `tbl_clear` WHERE `cl_id`='".$_POST['cl_id']."' ";
  $result10 = mysqli_query($dbcnx,$query10);
  if (!$result10) {puterror($result2['skin'],$TEXT['error-tbl8']); require_once "../utils/bottom_adm.php"; exit();}
      $result_lib = mysqli_fetch_array($result10,MYSQLI_ASSOC);
      if($_POST['tip']!=$result_lib['col_tbl_name']) {
          $query9 = "SELECT `col_tbl_name` FROM `tbl_clear` WHERE `col_tbl_name`='".$_POST['tip']."' ";
            $result9 = mysqli_query($dbcnx,$query9);
              if(!$result9) {puterror($result2['skin'],$TEXT['error-tbl8']); 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_tbl = "`col_tbl_name` = '".$_POST['tip']."', ";}
        }
       else {$name_tbl = "";}

  //*************************************
  //Информация для лога
  if($_POST['tip'] !== $result_lib['col_tbl_name']) {$log_tip = $TEXT['analiz-tab']." &laquo;".$result_lib['col_tbl_name']."&raquo;".$TEXT['log-nd8']." &laquo;".$_POST['tip']."&raquo;,<br />";}
   else {$log_tip = $TEXT['analiz-tab']." &laquo;".$_POST['tip']."&raquo;,";}

  if($by_data !== $result_lib['by_data']) {$log_day = $TEXT['param_days']." &laquo;".$result_lib['by_data']."&raquo;".$TEXT['log-nd8']." &laquo;".$by_data."&raquo;,<br />";}
   else {$log_day = "";}

   if($by_count !== $result_lib['by_count']) {$log_count = $TEXT['size-count2']." &laquo;".$result_lib['by_count']."&raquo;".$TEXT['log-nd8']." &laquo;".$by_count."&raquo;,<br />";}
   else {$log_count = "";}

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-praram2'];
  $what = $log_tip." ".$log_day." ".$log_count." ".$TEXT['log-id'].$_POST['cl_id'];;
  $who = $result2['familiya']." ".$result2['name'];
  //*************************************

  $query = "UPDATE `tbl_clear` SET $name_tbl `col_name` = '$col_name', `by_data` = '$by_data', `by_count` = '$by_count' WHERE `cl_id`='".$_POST['cl_id']."' LIMIT 1";
  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-tbl8']); require_once "../utils/bottom_adm.php"; exit();}
  require_once "../utils/bottom_adm.php";
?>