<?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(empty($_POST['pas_id'])) puterror2($TEXT['meet-err-edit2']);
  if(strlen($_POST['pas_id'])>11) puterror2($TEXT['meet-err-edit3']);
  if(empty($_POST['email'])) puterror2($TEXT['pass-mail-err1']);
  if(strlen($_POST['email'])>255) puterror2($TEXT['pass-mail-err1']);

 $pattern = array ('/;;;$/','/;;$/','/;$/');
 $_POST['email'] = preg_replace($pattern,'',$_POST['email']);

// if(!preg_match("|^[-0-9a-z_\.]+@[-0-9a-z_^\.]+\.[a-z]{2,6}$|i", $_POST['email'])) puterror2($TEXT['pass-mail-err1']);

  $_POST['pas_str_id'] = rep($_POST['pas_str_id']);
  $_POST['pas_id'] = rep($_POST['pas_id']);
  $_POST['email'] = rep($_POST['email']);

  if($_POST['uv'] == "on")
   {
    $srch_pas = " AND `stat` = '1' ";
   }
   else $srch_pas = " AND `stat` = '2' ";

  $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'];

  $query3 = "SELECT
                   tbl_pass_keep.*,
                   YEAR(tbl_pass_keep.best_before) AS bb_year,
                   MONTH(tbl_pass_keep.best_before) AS bb_month,
                   DAY(tbl_pass_keep.best_before) AS bb_day,
                   AES_DECRYPT(tbl_pass_keep.dom_pass,'".$kfr_tmp."') AS dom_pwd,
                   AES_DECRYPT(tbl_pass_keep.mail_pass,'".$kfr_tmp."') AS mail_pwd,
                   AES_DECRYPT(tbl_pass_keep.dom_lgn,'".$kfr_tmp."') AS dom_login,
                   AES_DECRYPT(tbl_pass_keep.mail_lgn,'".$kfr_tmp."') AS mail_login
                   FROM `tbl_pass_keep`
                   WHERE tbl_pass_keep.pas_id = '".$_POST['pas_id']."' ";
   $tmp3 = mysql_query($query3);
    if(!$tmp3) puterror2($TEXT['error-tbl52']);
     $tmp_pos = mysql_fetch_array($tmp3,MYSQL_ASSOC);
     $full_name = $tmp_pos['fio'];
     $user_mail = $tmp_pos['mail_login'];
     $user_dom = $tmp_pos['dom_login'];
     $passwd = $tmp_pos['dom_pwd'];
     $passwd2 = $tmp_pos['mail_pwd'];

     if($result_primechanie !== "")
      {
       $result_primechanie = str_replace("  ","&nbsp;&nbsp;",$tmp_pos['primechanie']);
       $primechanie = "<br /><hr width='20%' align='left' />".$TEXT['know-prim'].": ".$result_primechanie;
       }

     $best_before = $tmp_pos['best_before'];

     if(!empty($user_mail))
      {
       if($tmp_pos['mail_form'] == "") $mail_form = "".$TEXT['support34']."(e-mail): <b>".$user_mail."</b><br />";
        else $mail_form = "".$tmp_pos['mail_form'].": <b>".$user_mail."</b><br />";
      } else $mail_form = "";

     if(!empty($passwd2))
      {
       if($tmp_pos['mail_pass_form'] == "") $mail_pass_form = "".$TEXT['install-pswd']."(e-mail): <b>".$passwd2."</b><br />";
         else $mail_pass_form = "".$tmp_pos['mail_pass_form'].": <b>".$passwd2."</b><br />";
     } else $mail_pass_form = "";

    $date_day3 = $tmp_pos['bb_day'];
    $date_month3 = $tmp_pos['bb_month'];
    $date_year3 = $tmp_pos['bb_year'];
     if($tmp_pos['bb_year'] == "" || $tmp_pos['bb_year'] == "0000-00-00" || $tmp_pos['bb_year'] == "0") $bb_year = "";
      else $bb_year = $TEXT['pass-bb'].": <b>".$date_day3." ".$month[$date_month3]." ".$date_year3." ".$year."</b><br />";

    $date_day2 = date("d");
    $date_month2 = date("m");
    $date_year2 = date("Y");
    $data_podp = date("$date_day2 $month[$date_month2] $date_year2 $year H:i:s");

switch($_POST['typepar'])
  {
case 1:
   {
     $address = $_POST['email'];
     $podpis = $data_podp;
     $tema = $TEXT['pass-mail'];
     $message = "<b>".$full_name."</b><br /><br />".$TEXT['support34'].": <b>".$user_dom."</b><br />".$TEXT['install-pswd'].": <b>".$passwd."</b>".$bb_year."<br /><br /><hr />".$podpis;
     $podpis = stripslashes($podpis);
     $tema = stripslashes($tema);
     $msg = nl2br(stripslashes($message));

     @mail($address, $tema, $msg, join("\r\n", array(
     "From: phonebook@".$_SERVER['SERVER_NAME']."",
     "MIME-Version: 1.0",
     "Content-type: text/html; charset=utf-8",
     "Content-Transfer-Encoding: 8bit",
     "Date: ".date("D, d-M-Y H:i:s", time())."",
     "X-Mailer: Phone Book"
     )));
   }
break;
case 2:
  {
    $address = $_POST['email'];
    $podpis = $data_podp;
    $tema = $TEXT['pass-mail'];
    $message = "<b>".$full_name."</b><br /><br />".$mail_form."".$mail_pass_form."".$bb_year."<br /><br /><hr />".$podpis."";
    $podpis = stripslashes($podpis);
    $tema = stripslashes($tema);
    $msg = nl2br(stripslashes($message));

    @mail($address, $tema, $msg, join("\r\n", array(
    "From: phonebook@".$_SERVER['SERVER_NAME']."",
    "MIME-Version: 1.0",
    "Content-type: text/html; charset=utf-8",
    "Content-Transfer-Encoding: 8bit",
    "Date: ".date("D, d-M-Y H:i:s", time())."",
    "X-Mailer: Phone Book"
    )));
  }
break;
case 3:
 {
    $address = $_POST['email'];
    $podpis = $data_podp;
    $tema = $TEXT['pass-mail'];
    $message = "<b>".$full_name."</b><br /><br />".$TEXT['support34'].": <b>".$user_dom."</b><br />".$TEXT['install-pswd'].": <b>".$passwd."</b><br />".$mail_form."".$mail_pass_form."".$bb_year." ".$primechanie."<br /><br /><hr />".$podpis."";
    $podpis = stripslashes($podpis);
    $tema = stripslashes($tema);
    $msg = nl2br(stripslashes($message));

    @mail($address, $tema, $msg, join("\r\n", array(
    "From: phonebook@".$_SERVER['SERVER_NAME']."",
    "MIME-Version: 1.0",
    "Content-type: text/html; charset=utf-8",
    "Content-Transfer-Encoding: 8bit",
    "Date: ".date("D, d-M-Y H:i:s", time())."",
    "X-Mailer: Phone Book"
    )));
  }
break;
}

//*************************************
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['pass-mail-log'];
  $what = $TEXT['pass-mail']."&laquo;".$full_name."&raquo;".$TEXT['pass-mail-log2']."&laquo;".$_POST['email']."&raquo;";
  $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=".$_POST['pas_str_id']."&amp;pas_id=".$_POST['pas_id']."&amp;k=".$_POST['k']."' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
?>
</body>
</html>