<?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) puterror2($TEXT['error-supp11']);

  if(empty($_GET['pas_str_id'])) puterror2($TEXT['meet-err-edit2']);
  if(strlen($_GET['pas_str_id'])>11) puterror2($TEXT['meet-err-edit3']);
  if(strlen($_GET['k'])>8) puterror2($TEXT['error-paging1']);
  if(strlen($_GET['show'])>4) puterror2($TEXT['pass-err14']);
  if(strlen($_GET['edit'])>4) puterror2($TEXT['pass-err14']);

  $query_iskh = "SELECT `p_tmp` FROM `tbl_p_tmp` WHERE `usr_id` = '".$result2['login_id']."' AND stat = '1' 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_tmp1 = $result_iskh2['p_tmp'];

  $query_iskh2 = "SELECT `p_tmp` FROM `tbl_p_tmp` WHERE `usr_id` = '".$result2['login_id']."' AND stat = '2' LIMIT 1";
    $result_iskh2 = mysql_query($query_iskh2);
      if(!$result_iskh2) puterror2($TEXT['error-tbl53']);
        $result_iskh3 = mysql_fetch_array($result_iskh2,MYSQL_ASSOC);
        $kfr_tmp2 = $result_iskh3['p_tmp'];

  $query_strukt_iskh = mysql_query("SELECT `strukt_name` FROM `tbl_pass_strukt`  WHERE `pas_str_id` = '".$_GET['pas_str_id']."' LIMIT 1 ");
    if(!$query_strukt_iskh) puterror2($TEXT['error-tbl51']);
     $result_strukt_iskh = mysql_fetch_array($query_strukt_iskh,MYSQL_ASSOC);
     $result_strukt_name = $result_strukt_iskh['strukt_name'];

  if($_GET['edit'] == "on") $edt = "yes";
   else $edt = "no";

  if($_GET['show'] == "on") {
      $visible = "show";
      $log_fr = $TEXT['pass-log4']."&laquo;".$result_strukt_name."&raquo;".$TEXT['log-id'].$_GET['pas_str_id'];

  $query = "SELECT `pas_id`, `visible` FROM `tbl_pass_keep` WHERE `pas_str_id` = '".$_GET['pas_str_id']."'";
     $result = mysql_query($query);
     if(!$result) puterror3($TEXT['error-tbl54']);
       if(mysql_num_rows($result)>0)
         {
          while($result_exps = mysql_fetch_array($result,MYSQL_ASSOC))
          {
            if($result_exps['visible'] == "hide") {
            $query_iskh_p = "UPDATE `tbl_pass_keep` SET `dom_pass` = AES_ENCRYPT(AES_DECRYPT(dom_pass,'".$kfr_tmp2."'),'".$kfr_tmp1."'), `dom_lgn` = AES_ENCRYPT(AES_DECRYPT(dom_lgn,'".$kfr_tmp2."'),'".$kfr_tmp1."'), `mail_pass` = AES_ENCRYPT(AES_DECRYPT(mail_pass,'".$kfr_tmp2."'),'".$kfr_tmp1."'), `mail_lgn` = AES_ENCRYPT(AES_DECRYPT(mail_lgn,'".$kfr_tmp2."'),'".$kfr_tmp1."'), `visible` = '".$visible."', `mast_edt` = '".$edt."' WHERE `pas_id` = '".$result_exps['pas_id']."' ";
            $result_iskh_p = mysql_query($query_iskh_p);
              if(!$result_iskh_p) puterror2($TEXT['error-tbl52']);
            } else
               {
                $query_iskh_p2 = "UPDATE `tbl_pass_keep` SET `visible` = '".$visible."', `mast_edt` = '".$edt."' WHERE `pas_id` = '".$result_exps['pas_id']."' ";
                $result_iskh_p2 = mysql_query($query_iskh_p2);
                 if(!$result_iskh_p2) puterror2($TEXT['error-tbl52']);
               }
          }
        }

      }
   else {
       $visible = "hide";
       $log_fr = $TEXT['pass-log5']."&laquo;".$result_strukt_name."&raquo;".$TEXT['log-id'].$_GET['pas_str_id'];

  $query = "SELECT `pas_id`, `visible` FROM `tbl_pass_keep` WHERE `pas_str_id` = '".$_GET['pas_str_id']."'";
     $result = mysql_query($query);
     if(!$result) puterror3($TEXT['error-tbl54']);
       if(mysql_num_rows($result)>0)
         {
          while($result_exps = mysql_fetch_array($result,MYSQL_ASSOC))
          {
            if($result_exps['visible'] == "hide") {
            $query_iskh_p = "UPDATE `tbl_pass_keep` SET `visible` = '".$visible."', `mast_edt` = '".$edt."' WHERE `pas_id` = '".$result_exps['pas_id']."' ";
            $result_iskh_p = mysql_query($query_iskh_p);
              if(!$result_iskh_p) puterror2($TEXT['error-tbl52']);
            } else
               {
                $query_iskh_p2 = "UPDATE `tbl_pass_keep` SET `dom_pass` = AES_ENCRYPT(AES_DECRYPT(dom_pass,'".$kfr_tmp1."'),'".$kfr_tmp2."'), `dom_lgn` = AES_ENCRYPT(AES_DECRYPT(dom_lgn,'".$kfr_tmp1."'),'".$kfr_tmp2."'), `mail_pass` = AES_ENCRYPT(AES_DECRYPT(mail_pass,'".$kfr_tmp1."'),'".$kfr_tmp2."'), `mail_lgn` = AES_ENCRYPT(AES_DECRYPT(mail_lgn,'".$kfr_tmp1."'),'".$kfr_tmp2."'), `visible` = '".$visible."', `mast_edt` = '".$edt."' WHERE `pas_id` = '".$result_exps['pas_id']."' ";
                $result_iskh_p2 = mysql_query($query_iskh_p2);
                 if(!$result_iskh_p2) puterror2($TEXT['error-tbl52']);
               }
          }
        }

       }

    $query_strukt = mysql_query("UPDATE `tbl_pass_strukt` SET `visible` = '".$visible."' WHERE `pas_str_id` = '".$_GET['pas_str_id']."' ");
    if(!$query_strukt) puterror2($TEXT['error-tbl51']);
     else
      {
        //*************************************
        $ip = $_SERVER['REMOTE_ADDR'];
        $doing = $TEXT['pass-log'];
        $what = $log_fr;
        $who = $result2['fullname'];
        //*************************************
        $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=".$_GET['pas_str_id']."&amp;k=".$_POST['k']."' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
    }
?>
</body>
</html>