<?php
///////////////////////////////////////////////////
//2010 (C)
//Карпухин Павел
///////////////////////////////////////////////////
require_once "../utils/chlang.php";

$title=$titlepage = $TEXT['menu-btn35-title'];

require_once "../config_a.php";
require_once "../utils/functions.php";
require_once "../utils/top_adm.php";
require_once "../utils/trans/".$lang.".php";

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

if(empty($_POST['bl_id'])) {puterror($result2['skin'],$TEXT['error-no-id']); require_once "../utils/bottom_adm.php"; exit();}
if(strlen($_POST['bl_id'])>5) {puterror($result2['skin'],$TEXT['error-id-long']); require_once "../utils/bottom_adm.php"; exit();}
if(empty($_POST['booklang'])) {puterror($result2['skin'],$TEXT['error-booklang1']); require_once "../utils/bottom_adm.php"; exit();}
if(strlen($_POST['booklang'])>255) {puterror($result2['skin'],$TEXT['error-booklang2']); require_once "../utils/bottom_adm.php"; exit();}
if(strlen($_POST['position'])>5) {puterror($result2['skin'],$TEXT['error-booklang10']); require_once "../utils/bottom_adm.php"; exit();}

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

$_POST['booklang'] = rep($_POST['booklang']);
$_POST['bl_id'] = rep($_POST['bl_id']);
$_POST['position'] = rep($_POST['position']);
$_POST['position'] = intval($_POST['position']);

$query10 = "SELECT `booklang`, `position` FROM `tbl_booklang` WHERE `bl_id`='".$_POST['bl_id']."' ";
 $result10 = mysqli_query($dbcnx,$query10);
  if(!$result10) {puterror($result2['skin'],$TEXT['error-tbl23']); require_once "../utils/bottom_adm.php"; exit();}
    $result_lib = mysqli_fetch_array($result10);
      if($_POST['booklang']!=$result_lib['booklang']) {
          $query9 = "SELECT `booklang` FROM `tbl_booklang` WHERE `booklang`='".$_POST['booklang']."' AND `use_prog` = '1' ";
            $result9 = mysqli_query($dbcnx,$query9);
              if(!$result9) {puterror($result2['skin'],$TEXT['error-tbl23']); 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_booklang = "`booklang` = '".$_POST['booklang']."', ";}
        }
       else {$name_booklang = "";}

  //Проверим надо удалять картинку или нет
  if($_POST['delphoto'] == "on")
  {
  $image = "";
  //Удаляем старые картинки
  unlink($_POST['old_pho']);
  }
  else
   {
    //Проверяем размер файла с фото
    if($_FILES['photo']['size'] > 1024*1*1024) {puterror($result2['skin'],$TEXT['error-pic3']); require_once "../utils/bottom_adm.php"; exit();}
     if(!empty($_FILES['photo']['tmp_name']))
     {
     $ext = strrchr($_FILES['photo']['name'], ".");
     $name = "lang_".translate($_POST['booklang']);
     $image = "../photo/$name".date("YmdHis",time())."$ext";

     if(copy($_FILES['photo']['tmp_name'],$image))
      {
      unlink($_FILES['photo']['tmp_name']);
      chmod($image, 0644);
      }
     }
     else {$image = $_POST['old_pho'];}
   }

  //*************************************
  //Информация для лога
  if($result_lib['booklang'] !== $_POST['booklang']) {$log_bl = $TEXT['log-booklang4']."&laquo;".$result_lib['booklang']."&raquo;".$TEXT['log-na']."&laquo;".$_POST['booklang']."&raquo;<br />";}
    else {$log_bl = "";}

  if($result_lib['booklang'] !== $_POST['booklang']) {$log_bl_visible = "&laquo;".$_POST['booklang']."&raquo; ";}
    else {$log_bl_visible = "&laquo;".$result_lib['booklang']."&raquo; ";}

  if($result_lib['position'] !== $_POST['position']) {$log_pos = $TEXT['log-booklang5'].$log_bl_visible.$TEXT['log-booklang6']."&laquo;".$result_lib['position']."&raquo;".$TEXT['log-na']."&laquo;".$_POST['position']."&raquo;<br />";}
    else {$log_pos = "";}

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-booklang3'];
  $what = $log_bl." ".$log_pos.$TEXT['log-id'].$_POST['bl_id'];
  $who = $result2['familiya']." ".$result2['name'];
  //*************************************
  $query = "UPDATE `tbl_booklang` SET
   $name_booklang
   `picture` = '".$image."',
   `position` = ".$_POST['position']." 
    WHERE `bl_id`='".$_POST['bl_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-tbl23']); require_once "../utils/bottom_adm.php"; exit();}
require_once "../utils/bottom_adm.php";
?>