<?php
  ///////////////////////////////////////////////////
  //2012 (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 && $result2['master'] == 0) puterror2($TEXT['error-supp11']);

  if(empty($_POST['pas_str_id'])) puterror2($TEXT['meet-err-edit2']);
  if(strlen($_POST['pas_str_id'])>11) puterror2($TEXT['meet-err-edit3']);
  if(strlen($_POST['parent'])>11) puterror2($TEXT['meet-err-edit3']);
  if(empty($_POST['pas_id'])) puterror2($TEXT['meet-err-edit2']);
  if(strlen($_POST['pas_id'])>11) puterror2($TEXT['meet-err-edit3']);
  if(empty($_POST['fio'])) puterror2($TEXT['pass-err1']);
  if(strlen($_POST['fio'])>255) puterror2($TEXT['pass-err2']);
  if(empty($_POST['dom_lgn'])) puterror2($TEXT['pass-err4']);
  if(strlen($_POST['dom_lgn'])>255) puterror2($TEXT['pass-err5']);
  if(empty($_POST['dom_pass'])) puterror2($TEXT['pass-err6']);
  if(strlen($_POST['dom_pass'])>255) puterror2($TEXT['pass-err7']);
  if(strlen($_POST['mail_lgn'])>255) puterror2($TEXT['pass-err8']);
  if(strlen($_POST['mail_pass'])>255) puterror2($TEXT['pass-err9']);
  if(strlen($_POST['primechanie'])>1000) puterror2($TEXT['pass-err3']);
  if(strlen($_POST['usr_id'])>11) puterror2($TEXT['meet-err-edit3']);
  if(strlen($_POST['old_usr_id'])>11) puterror2($TEXT['meet-err-edit3']);
  if(strlen($_POST['mail_form'])>100) puterror2($TEXT['pass-err15']);
  if(strlen($_POST['mail_pass_form'])>100) puterror2($TEXT['pass-err16']);
  if(strlen($_POST['dom_form'])>100) puterror2($TEXT['pass-err15']);
  if(strlen($_POST['dom_pass_form'])>100) puterror2($TEXT['pass-err16']);

  if(!empty($_POST['best_before']))
  {
    if(strlen($_POST['best_before'])>10) puterror($TEXT['meet-err-edit4']);
    if(!preg_match("/^[\d]{4}[\-][\d]{2}[\-][\d]{2}$/", $_POST['best_before'])) puterror($TEXT['meet-err-edit4']);
  }
    else $_POST['best_before'] = "";

  $_POST['pas_str_id'] = rep($_POST['pas_str_id']);
  $_POST['parent'] = rep($_POST['parent']);
  $_POST['pas_id'] = rep($_POST['pas_id']);
  $_POST['fio'] = rep($_POST['fio']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['dom_lgn'] = rep2($_POST['dom_lgn']);
  $_POST['mail_lgn'] = rep2($_POST['mail_lgn']);
  $_POST['dom_pass'] = rep($_POST['dom_pass']);
  $_POST['mail_pass'] = rep($_POST['mail_pass']);
  $_POST['primechanie'] = rep($_POST['primechanie']);
  $_POST['best_before'] = rep($_POST['best_before']);
  $_POST['usr_id'] = rep($_POST['usr_id']);
  $_POST['old_usr_id'] = rep($_POST['old_usr_id']);
  $_POST['mail_form'] = rep($_POST['mail_form']);
  $_POST['mail_pass_form'] = rep($_POST['mail_pass_form']);
  $_POST['dom_form'] = rep($_POST['dom_form']);
  $_POST['dom_pass_form'] = rep($_POST['dom_pass_form']);

  if($_POST['visible'] == "on")
   {
    $visible = " visible = 'show', ";
    $srch_pas = " AND `stat` = '1' ";
   }
    else
     {
      $visible = " visible = 'hide', ";

      $query_count = mysql_query("SELECT COUNT(ptmp_id) FROM tbl_p_tmp WHERE usr_id = '".$result2['login_id']."' AND stat = '2' ");
       $coun_rows = mysql_result($query_count, 0);
       if($coun_rows == 0) {puterror2($TEXT['pass-err10']); exit();}
        else $srch_pas = " AND `stat` = '2' ";
     }

  if($_POST['mast_edt'] == "on")
   {
    $mast_edt = " mast_edt = 'yes' ";
    $visible = " visible = 'show', ";
    $srch_pas = " AND `stat` = '1' ";
   }
   else $mast_edt = " mast_edt = 'no' ";

  $query_iskh = "SELECT `p_tmp` FROM `tbl_p_tmp` WHERE `usr_id` = '".$result2['login_id']."' $srch_pas LIMIT 1";
  $result_iskh = mysql_query($query_iskh);
  if(!$result_iskh) puterror2($TEXT['error-tbl53']);
   $result_iskh2 = mysql_fetch_array($result_iskh,MYSQL_ASSOC);
   $kfr_tmp = $result_iskh2['p_tmp'];

if($_POST['mail_pass'] !== "") $mail_pwd = " mail_pass = AES_ENCRYPT('".$_POST['mail_pass']."','".$kfr_tmp."'), ";
    else $mail_pwd = "";

if($_POST['mail_lgn'] !== "") $mail_lgn = " mail_lgn = AES_ENCRYPT('".$_POST['mail_lgn']."','".$kfr_tmp."'), ";
    else $mail_lgn = "";

if($_POST['usr_using'] !== "on")
{
 $usr_id = " usr_id = '', ";
}
  else 
   {
    if(!empty($_POST['usr_id'])) $usr_id = " usr_id = '".$_POST['usr_id']."', ";
     else $usr_id = "";
  }

if($_POST['movechb'] == "on") $parnt = " pas_str_id = '".$_POST['parent']."', ";
   else $parnt = "";

if(!empty($_POST['mail_form'])) $mail_form = $_POST['mail_form'];
    else $mail_form = $TEXT['support34']."(e-mail)";

if(!empty($_POST['mail_pass_form'])) $mail_pass_form = $_POST['mail_pass_form'];
    else $mail_pass_form = $TEXT['install-pswd']."(e-mail)";

if(!empty($_POST['dom_form'])) $dom_form = $_POST['dom_form'];
    else $dom_form = $TEXT['support34'];

if(!empty($_POST['dom_pass_form'])) $dom_pass_form = $_POST['dom_pass_form'];
    else $dom_pass_form = $TEXT['install-pswd'];

  if($_POST['clear_picture'] == "on")
  {
  $image = "";
      unlink($_POST['old_photo']);
  }
    else
    {
    if($_FILES['photo']['size'] > 1024*2*1024) puterror2($TEXT['error-pic3']);
    if (!empty($_FILES['photo']['tmp_name']))
      {
       $ext = strrchr($_FILES['photo']['name'], ".");
       $name = $_POST['strukt_name'];
       $krit = "pass_zap_";
       $image = "../photo/$krit".date("YmdHis",time())."$ext";

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

  //*************************************
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['pass-log'];
  $what = $TEXT['pass-log2']."&laquo;".$_POST['fio']."&raquo;".$TEXT['log-id'].$_POST['pas_id'];
  $who = $result2['fullname'];
  //*************************************
  $query = "UPDATE `tbl_pass_keep` SET
      ".$parnt."
      fio = '".$_POST['fio']."',
      ".$usr_id."
      dom_lgn = AES_ENCRYPT('".$_POST['dom_lgn']."','".$kfr_tmp."'),
      dom_pass = AES_ENCRYPT('".$_POST['dom_pass']."','".$kfr_tmp."'),
      ".$mail_lgn."
      ".$mail_pwd."
      primechanie = '".$_POST['primechanie']."',
      ".$visible."
      data_edt = NOW(),
      best_before = '".$_POST['best_before']."',
      login_edt_id = '".$result2['login_id']."',
      ".$mast_edt.",
      mail_form = '".$mail_form."',
      mail_pass_form = '".$mail_pass_form."',
      pic_tr = '$image',
      dom_form = '".$dom_form."',
      dom_pass_form = '".$dom_pass_form."'
      WHERE pas_id='".$_POST['pas_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=pass_keep.php?pas_str_id=".$_POST['pas_str_id']."&amp;pas_id=".$_POST['pas_id']."&amp;k=".$_POST['k']."' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
  } else puterror2($TEXT['error-tbl52']);
?>
</body>
</html>