<?php
  ///////////////////////////////////////////////////
  //Журнал изменений
  //2010 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

  $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
  if(is_file ("../lang/".$lang.".php")) include "../lang/".$lang.".php";
     else include "../lang/ru.php";

  $title=$titlepage = $TEXT['zhurn-edit'];
  $helppage = $TEXT['zhurn-edit-help'];

  require_once "../config.php";
  require_once "top.php";
  ?>
  <title><?php echo $title; ?></title>
  <link rel="StyleSheet" type="text/css" href="../utils/grid.css" />
  </head>
  <body>
  <?php
  if($result2['administrator'] == 0) puterror2($TEXT['error-supp11']);

  if(empty($_POST['zh_str_id'])) puterror2($TEXT['error-zhurn1']);
  if(strlen($_POST['zh_str_id'])>11) puterror2($TEXT['error-zhurn2']);
  if(empty($_POST['tema'])) puterror2($TEXT['error-zhurn3']);
  if(strlen($_POST['tema'])>255) puterror2($TEXT['error-zhurn4']);
  if(empty($_POST['zh_soobschenie'])) puterror2($TEXT['error-zhurn5']);
  if(strlen($_POST['zh_soobschenie'])>4000) puterror2($TEXT['error-zhurn6']);
  if(empty($_POST['position'])) puterror2($TEXT['error-zhurn7']);
  if(strlen($_POST['position'])>6) puterror2($TEXT['error-zhurn8']);
  if(empty($_POST['krit_id'])) puterror2("123".$TEXT['error-zhurn9']);
  if(strlen($_POST['krit_id'])>11) puterror2($TEXT['error-zhurn10']);
  if(empty($_POST['zhurn_id'])) puterror2($TEXT['error-zhurn11']);
  if(strlen($_POST['zhurn_id'])>11) puterror2($TEXT['error-zhurn12']);
  if(strlen($_POST['k'])>10) puterror2($TEXT['error-zhurn15']);

  $_POST['zh_str_id'] = rep($_POST['zh_str_id']);
  $_POST['tema'] = rep($_POST['tema']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['krit_id'] = rep($_POST['krit_id']);
  $_POST['zhurn_id'] = rep($_POST['zhurn_id']);
//  $_POST['zh_soobschenie'] = addslashes($_POST['zh_soobschenie']);
  $_POST['zh_soobschenie'] = rep2($_POST['zh_soobschenie']);

  //*************************************
  $query_zh = "SELECT * FROM `tbl_zhurnal` WHERE `zhurn_id` = ".$_POST['zhurn_id']."";
  $result_zh = mysql_query($query_zh);
  if(!$result_zh) puterror2($TEXT['error-tbl41']);
   $result_log = mysql_fetch_array($result_zh,MYSQL_ASSOC);

  if($result_log['zh_tema'] !== $_POST['tema'])
  {
    $log_tema = $TEXT['log-otd5']."&laquo;".$result_log['zh_tema']."&raquo;".$TEXT['log-zhurn5']."&laquo;".$_POST['tema']."&raquo;<br />";
    $log_tema_visible = "&laquo;".$_POST['tema']."&raquo;, ";
  }
    else
      {
      $log_tema = "";
      $log_tema_visible = "&laquo;".$result_log['zh_tema']."&raquo;, ";
       }

  if($result_log['position'] !== $_POST['position']) $log_pos = $TEXT['log-zhurn6'].$log_tema_visible.$TEXT['log-zhurn7']."&laquo;".$result_log['position']."&raquo;".$TEXT['log-zhurn5']."&laquo;".$_POST['position']."&raquo;<br />";
    else $log_pos = "";

  if($result_log['zh_soobschenie'] !== $_POST['zh_soobschenie']) $log_par = $TEXT['log-zhurn6'].$log_tema_visible.$TEXT['log-zhurn8']."&laquo;".$result_log['zh_soobschenie']."&raquo;".$TEXT['log-zhurn5']."&laquo;".$_POST['zh_soobschenie']."&raquo;<br />";
    else $log_par = "";

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-zhurn2'];
  $what = $log_tema." ".$log_pos.$log_par.$TEXT['log-id'].$_POST['zhurn_id'];
  $who = $result2['fullname'];
  //*************************************
  $query = "UPDATE `tbl_zhurnal` SET `zh_tema` = '".$_POST['tema']."', `zh_soobschenie` = '".$_POST['zh_soobschenie']."', `position` = '".$_POST['position']."', `krit_id` = '".$_POST['krit_id']."', `data`= NOW() WHERE `zhurn_id`='".$_POST['zhurn_id']."' ";
  if(mysql_query($query))
  {
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysql_query($query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=zhurnal.php?zh_str_id=".$_POST['zh_str_id']."&amp;zhurn_id=".$_POST['zhurn_id']."&amp;k=".$_POST['k']."' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
  } else puterror2($TEXT['error-tbl41']);
?>
</body>
</html>