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

  $title=$titlepage = $TEXT['book-title'];

  if(is_file ("../lang/".$lang."/trans.php")) {require_once "../lang/".$lang."/trans.php";}
   else {require_once "../lang/ru/trans.php";}

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

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

  if(empty($_POST['book_id'])) {puterror($result2['skin'],$TEXT['error-book5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['book_id'])>12) {puterror($result2['skin'],$TEXT['error-book6']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['book_name'])) {puterror($result2['skin'],$TEXT['error-book1']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['book_name'])>255) {puterror($result2['skin'],$TEXT['error-book2']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['isbn'])>255) {puterror($result2['skin'],$TEXT['error-book9']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['opisanie'])>3000) {puterror($result2['skin'],$TEXT['error-book10']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['oglavlenie'])>30000){puterror($result2['skin'],$TEXT['error-book11']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['zhanr_id'])>5){puterror($result2['skin'],$TEXT['error-zhanr6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['cicle_id'])>5) {puterror($result2['skin'],$TEXT['error-cicle6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['prev_books'])>12) {puterror($result2['skin'],$TEXT['error-cicle6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['tip_izd_id'])>5) {puterror($result2['skin'],$TEXT['error-tip-izd6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['bibl_id'])>5) {puterror($result2['skin'],$TEXT['error-libr6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['photo_obr'])>1) {puterror($result2['skin'],$TEXT['error-pic2']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['zal_id'])>5) {puterror($result2['skin'],$TEXT['error-zal6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['shkaf_id'])>5) {puterror($result2['skin'],$TEXT['error-shkaf6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['polka_id'])>5) {puterror($result2['skin'],$TEXT['error-polka6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['visible'])>5) {puterror($result2['skin'],$TEXT['error-new11']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['data_izdaniya'])>4) {puterror($result2['skin'],$TEXT['error-book21']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['data_postupleniya'])>10) {puterror($result2['skin'],$TEXT['error-book22']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['old_photo'])>255) {puterror($result2['skin'],$TEXT['error-pic4']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['old_ebook'])>255) {puterror($result2['skin'],$TEXT['error-pic4']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['clear_photo'])>5) {puterror($result2['skin'],$TEXT['error-new12']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['clear_e_book'])>5) {puterror($result2['skin'],$TEXT['error-new12']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['ser_id'])) {puterror($result2['skin'],$TEXT['error-ser5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['ser_id'])>5) {puterror($result2['skin'],$TEXT['error-ser6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['book_num'])>3) {puterror($result2['skin'],$TEXT['error-book23']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['bl_id'])) {puterror($result2['skin'],$TEXT['error-booklang5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['bl_id'])>5) {puterror($result2['skin'],$TEXT['error-booklang6']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['bl2_id'])) {puterror($result2['skin'],$TEXT['error-booklang5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['bl2_id'])>5) {puterror($result2['skin'],$TEXT['error-booklang6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['paper_copy'])>5) {puterror($result2['skin'],$TEXT['error-new13']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['remote_copy'])>5) {puterror($result2['skin'],$TEXT['error-new13']); require_once "../utils/bottom_adm.php"; exit();}

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

  $_POST['book_id'] = rep($_POST['book_id']);
  $_POST['book_name'] = rep($_POST['book_name']);
  $_POST['isbn'] = rep($_POST['isbn']);
  $_POST['opisanie'] = rep($_POST['opisanie']);
  $_POST['oglavlenie'] = rep($_POST['oglavlenie']);
  $_POST['zhanr_id'] = rep($_POST['zhanr_id']);
  $_POST['cicle_id'] = rep($_POST['cicle_id']);
  $_POST['prev_books'] = rep($_POST['prev_books']);
  $_POST['tip_izd_id'] = rep($_POST['tip_izd_id']);
  $_POST['bibl_id'] = rep($_POST['bibl_id']);
  $_POST['photo_obr'] = rep($_POST['photo_obr']);
  $_POST['zal_id'] = rep($_POST['zal_id']);
  $_POST['shkaf_id'] = rep($_POST['shkaf_id']);
  $_POST['polka_id'] = rep($_POST['polka_id']);
  $_POST['data_izdaniya'] = rep($_POST['data_izdaniya']);
  $_POST['data_postupleniya'] = rep($_POST['data_postupleniya']);
  $_POST['old_photo'] = rep($_POST['old_photo']);
  $_POST['old_ebook'] = rep($_POST['old_ebook']);
  $_POST['clear_photo'] = rep($_POST['clear_photo']);
  $_POST['clear_e_book'] = rep($_POST['clear_e_book']);
  $_POST['ser_id'] = rep($_POST['ser_id']);
  $_POST['book_num'] = rep($_POST['book_num']);
  $_POST['bl_id'] = rep($_POST['bl_id']);
  $_POST['bl2_id'] = rep($_POST['bl2_id']);

  if(empty($_POST['book_num'])) {$book_num = 0;}
   else
    {
     $bnt = strlen($_POST['book_num']);
      if($bnt == 1) {$book_num = "00".$_POST['book_num'];}
       elseif($bnt == 2) {$book_num = "0".$_POST['book_num'];}
        else {$book_num = $_POST['book_num'];}
    }

  if($_POST['e_book_10'] == "on" && !empty($_POST['book_num']) && $_POST['book_num'] !== 0) {$book_num_up10 = "0".$_POST['book_num'];}
   else {$book_num_up10 = $_POST['book_num'];}

  if(empty($_POST['data_postupleniya'])) {$data_postupleniya = "`data_postupleniya` = CURRENT_DATE(), ";}
   else {$data_postupleniya = "`data_postupleniya` = '".$_POST['data_postupleniya']."', ";}

  if($_POST['zakupka'] == "on") {
   $zakupka = "`vidano` = '3',";
   $user_id = "`user_id` = ".$result2['login_id'].",";
   $data = "`data_vidachi` = CURRENT_DATE(),";
   $data_postupleniya = "`data_postupleniya` = '1970-01-01',";
  }
   else
   {
    //$zakupka = 0;
    //$user_id = 0;
    //$data = "'1970-01-01'";
   }

  if(empty($_POST['data_izdaniya'])) {$data_izdaniya = "DATE_FORMAT(NOW(),'%Y')";}
   else {$data_izdaniya = $_POST['data_izdaniya'];}

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

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

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

  $first_bt = substr($_POST['book_name'], 0,1);
    if(preg_match("/[A-Z]/i", $first_bt)) {$first_b = substr($_POST['book_name'], 0,1);}
       else {$first_b = substr($_POST['book_name'], 0,2);}

  //Проверим надо удалять картинку или нет
  if($_POST['clear_photo'] == "on")
  {
  $smallimage = "";
  if(is_file($_POST['old_photo'])) {unlink($_POST['old_photo']);}
//  unlink($_POST['old_photo']);
  }
    else
    {
  if($_FILES['photo']['size'] > 1024*2*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'], ".");
       $trans_name = translate($_POST['book_name']);
       $name = $trans_name;
       $name_ = "b_pho_".$trans_name;
       $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($_POST['photo_obr'] == '1') {
                 if(is_file($_POST['old_photo'])) {unlink($_POST['old_photo']);}
                 $smallimage = $image;
            }
            if($_POST['photo_obr'] == '2') {
                 if(!resizeimg($image, $smallimage, 195, 275))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($_POST['old_photo'])) {unlink($_POST['old_photo']);}
                if(is_file($image)) {unlink($image);}
            }
            if($_POST['photo_obr'] == '3') {
                 if(!resizeimg($image, $smallimage, 275, 195))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($_POST['old_photo'])) {unlink($_POST['old_photo']);}
                if(is_file($image)) {unlink($image);}
            }
             if($_POST['photo_obr'] == '4') {
                 if(!resizeimg($image, $smallimage, 195, 275))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($_POST['old_photo'])) {unlink($_POST['old_photo']);}
                if(is_file($image)) {unlink($image);}
            }
             if($_POST['photo_obr'] == '5') {
                 if(!resizeimg($image, $smallimage, 275, 195))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($_POST['old_photo'])) {unlink($_POST['old_photo']);}
                if(is_file($image)) {unlink($image);}
            }
             if($_POST['photo_obr'] == '6') {
                 if(!resizeimg_l_r($image, $smallimage, 195, 275))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($_POST['old_photo'])) {unlink($_POST['old_photo']);}
                if(is_file($image)) {unlink($image);}
            }
             if($_POST['photo_obr'] == '7') {
                 if(!resizeimg_r($image, $smallimage, 275, 195))
                {
                  puterror($result2['skin'],$TEXT['error-gdlib']); require_once "../utils/bottom_adm.php"; exit();
                }
                if(is_file($_POST['old_photo'])) {unlink($_POST['old_photo']);}
                if(is_file($image)) {unlink($image);}
            }
     }
      else {$smallimage = $_POST['old_photo'];}
      }

    $query11 = "SELECT `cicle` FROM `tbl_cicle` WHERE `cicle_id` = '".$_POST['cicle_id']."' LIMIT 1 ";
    $result11 = mysqli_query($dbcnx,$query11);
        if(!$result11) {puterror($result2['skin'],$TEXT['error-tbl5']); require_once "../utils/bottom_adm.php"; exit();}
        while($result_cicle = mysqli_fetch_array($result11))
            {
            if($result_cicle['cicle'] != '' && $result_cicle['cicle'] != '---') {$book_cicle_name = translate("_".rep_back($result_cicle['cicle'])."_".$book_num_up10."_");}
            else {$book_cicle_name = "_";}
            }

    $query111 = "SELECT `cicle` FROM `tbl_cicle` WHERE `cicle_id` = '".$_POST['prev_books']."' LIMIT 1 ";
    $result111 = mysqli_query($dbcnx,$query111);
        if(!$result111) {puterror($result2['skin'],$TEXT['error-tbl5']); require_once "../utils/bottom_adm.php"; exit();}
        while($result_prev_books = mysqli_fetch_array($result111))
            {
            if($result_prev_books['cicle'] != '' && $result_prev_books['cicle'] != '---') {$book_prev_books = $_POST['prev_books'];}
            else {$book_prev_books = "0";}
            }

  //Проверим надо удалять книгу или нет
  if($_POST['clear_e_book'] == "on")
  {
  $ebook = "";
  unlink($_POST['old_ebook']);
  }
    else
    {
    if($_FILES['e_book']['size'] > 1024*200*1024) {puterror($result2['skin'],$TEXT['error-pic1']); require_once "../utils/bottom_adm.php"; exit();}
      if(!empty($_FILES['e_book']['tmp_name']))
      {
       $ext = strrchr($_FILES['e_book']['name'], ".");
       $trans_book = translate($_POST['book_name']);
       $name2 = "book".$book_cicle_name.$trans_book;
       $ebook = "../ebooks/".$name2.$ext;
       if(is_file($_POST['old_ebook'])) {unlink($_POST['old_ebook']);}

          if(copy($_FILES['e_book']['tmp_name'], $ebook))
            {
            unlink($_FILES['e_book']['tmp_name']);
            chmod($ebook, 0644);
            }
       }
        else {$ebook = $_POST['old_ebook'];}
      }

      //Проверим надо переименовывать книгу или нет
      if($_POST['chname_e_book'] == "on")
        {
        $date_for_rne = "_".date("YmdHis",time());
        $ebook_name_now = substr(strrchr($_POST['old_ebook'], "/"), 1);
        $ext_rne = strrchr($ebook_name_now, ".");
        $trans_book_rne = translate($_POST['book_name']);
        $name2_rne = "book".$book_cicle_name.$trans_book_rne;
        $ebook_nrne = "../ebooks/".$name2_rne.$ext_rne;
        $rename_ebook = "../ebooks/".$name2_rne.$date_for_rne.$ext_rne;
        if(is_file($_POST['old_ebook'])) 
          {
          if(is_file($ebook_nrne)) 
            {
             rename($_POST['old_ebook'],$rename_ebook);
             $ebook = $rename_ebook;
             $to_log_ebook_ch_filename = $TEXT['book-chname2'].$ebook_name_now.$TEXT['log-na'].substr(strrchr($rename_ebook, "/"), 1);
            }
            else 
               {
                rename($_POST['old_ebook'],$ebook_nrne);
                $ebook = $ebook_nrne;
                $to_log_ebook_ch_filename = $TEXT['book-chname2'].$ebook_name_now.$TEXT['log-na'].substr(strrchr($ebook_nrne, "/"), 1);
               }
          }
        }
        else {$to_log_ebook_ch_filename = "";}
      //Конец блока переименования файла

  //Проверим надо удалять аудио книгу или нет
  if($_POST['clear_a_book'] == "on")
  {
  $abook = "";
  unlink($_POST['old_abook']);
  }
    else
    {
    if($_FILES['a_book']['size'] > 1024*700*1024) {puterror($result2['skin'],$TEXT['error-pic7']); require_once "../utils/bottom_adm.php"; exit();}
      if(!empty($_FILES['a_book']['tmp_name']))
      {
       $exta = strrchr($_FILES['a_book']['name'], ".");
       $trans_abook = translate($_POST['book_name']);
       $namea2 = "audio_book".$book_cicle_name.$trans_abook;
       $abook = "../abooks/".$namea2.$exta;
       if(is_file($_POST['old_abook'])) {unlink($_POST['old_abook']);}

          if(copy($_FILES['a_book']['tmp_name'], $abook))
            {
            unlink($_FILES['a_book']['tmp_name']);
            chmod($abook, 0644);
            }
       }
      else {$abook = $_POST['old_abook'];}
    }

      //Проверим надо переименовывать аудио книгу или нет
      if($_POST['chname_a_book'] == "on")
        {
        $date_for_rna = "_".date("YmdHis",time());
        $abook_name_now = substr(strrchr($_POST['old_abook'], "/"), 1);
        $ext_rna = strrchr($abook_name_now, ".");
        $trans_book_rna = translate($_POST['book_name']);
        $name2_rna = "audio_book".$book_cicle_name.$trans_book_rna;
        $abook_nrna = "../abooks/".$name2_rna.$ext_rna;
        $rename_abook = "../abooks/".$name2_rna.$date_for_rna.$ext_rna;
        if(is_file($_POST['old_abook'])) 
          {
          if(is_file($abook_nrna)) 
            {
             rename($_POST['old_abook'],$rename_abook);
             $abook = $rename_abook;
             $to_log_abook_ch_filename = $TEXT['book-chname2'].$abook_name_now.$TEXT['log-na'].substr(strrchr($rename_abook, "/"), 1);
            }
            else 
               {
                rename($_POST['old_abook'],$abook_nrna);
                $abook = $abook_nrna;
                $to_log_abook_ch_filename = $TEXT['book-chname2'].$abook_name_now.$TEXT['log-na'].substr(strrchr($abook_nrna, "/"), 1);
               }
          }
        }
        else {$to_log_abook_ch_filename = "";}
      //Конец блока переименования файла

  //Проверим надо удалять CD-ROM к книге или нет
  if($_POST['clear_cd_book'] == "on")
  {
  $cdbook = "";
  unlink($_POST['old_cdbook']);
  }
    else
    {
  if($_FILES['cd_book']['size'] > 1024*700*1024) {puterror($result2['skin'],$TEXT['error-pic7']); require_once "../utils/bottom_adm.php"; exit();}
    if (!empty($_FILES['cd_book']['tmp_name']))
      {
       $extcd = strrchr($_FILES['cd_book']['name'], ".");
       $trans_cdbook = translate($_POST['book_name']);
       $namecd2 = "cdrom_book".$book_cicle_name.$trans_cdbook;
       $cdbook = "../cdbooks/".$namecd2.$extcd;
       if(is_file($_POST['old_cdbook'])) {unlink($_POST['old_cdbook']);}

          if(copy($_FILES['cd_book']['tmp_name'], $cdbook))
            {
            unlink($_FILES['cd_book']['tmp_name']);
            chmod($cdbook, 0644);
            }
       }
      else {$cdbook = $_POST['old_cdbook'];}
      }

      //Проверим надо переименовывать cd-rom или нет
      if($_POST['chname_cd_book'] == "on")
        {
        $date_for_rncd = "_".date("YmdHis",time());
        $cdbook_name_now = substr(strrchr($_POST['old_cdbook'], "/"), 1);
        $ext_rncd = strrchr($cdbook_name_now, ".");
        $trans_book_rncd = translate($_POST['book_name']);
        $name2_rncd = "cdrom_book".$book_cicle_name.$trans_book_rncd;
        $cdbook_nrncd = "../cdbooks/".$name2_rncd.$ext_rncd;
        $rename_cdbook = "../cdbooks/".$name2_rncd.$date_for_rncd.$ext_rncd;
        if(is_file($_POST['old_cdbook'])) 
          {
          if(is_file($cdbook_nrncd)) 
            {
             rename($_POST['old_cdbook'],$rename_cdbook);
             $cdbook = $rename_cdbook;
             $to_log_cdbook_ch_filename = $TEXT['book-chname2'].$cdbook_name_now.$TEXT['log-na'].substr(strrchr($rename_cdbook, "/"), 1);
            }
            else 
               {
                rename($_POST['old_cdbook'],$cdbook_nrncd);
                $cdbook = $cdbook_nrncd;
                $to_log_cdbook_ch_filename = $TEXT['book-chname2'].$cdbook_name_now.$TEXT['log-na'].substr(strrchr($cdbook_nrncd, "/"), 1);
               }
          }
        }
        else {$to_log_cdbook_ch_filename = "";}
      //Конец блока переименования файла

  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-book3'];
  $what = $_POST['book_name'].$to_log_ebook_ch_filename.$to_log_abook_ch_filename.$to_log_cdbook_ch_filename."; ".$TEXT['log-id'].$_POST['book_id'];
  $who = $result2['familiya']." ".$result2['name'];
  //*************************************

  $query = "UPDATE `tbl_book` SET
`photo_book` = '$smallimage',
`bibl_id` = '".$_POST['bibl_id']."',
`tip_izd_id` = '".$_POST['tip_izd_id']."',
`cicle_id` = '".$_POST['cicle_id']."',
`zhanr_id` = '".$_POST['zhanr_id']."',
`book_name` = '".$_POST['book_name']."',
`zal_id` = '".$_POST['zal_id']."',
`e_book` = '$ebook',
`shkaf_id` = '".$_POST['shkaf_id']."',
`polka_id` = '".$_POST['polka_id']."',
`data_izdaniya` = $data_izdaniya,
$data_postupleniya
`isbn` = '".$_POST['isbn']."',
`opisanie` = '".$_POST['opisanie']."',
`oglavlenie` = '".$_POST['oglavlenie']."',
`first_bt` = '$first_b',
`ser_id` = '".$_POST['ser_id']."',
`book_num` = '$book_num',
$zakupka
$user_id
$data
`bl_id` = '".$_POST['bl_id']."',
`bl2_id` = '".$_POST['bl2_id']."',
`paper_copy` = '$papcopy',
`remote_copy` = '$remote_copy',
`a_book` = '$abook',
`cd_book` = '$cdbook',
`prev_books` = '$book_prev_books'
 WHERE `book_id`='".$_POST['book_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))
           {
           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";
?>