<?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" />
  <link rel="StyleSheet" type="text/css" href="../utils/reader.css" />
  </head>
  <body leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" bottommargin="0" topmargin="0" >
  <script type="text/javascript" language="JavaScript" src="../lang/calendar-<?php echo $lang; ?>.js"></script>
  <?php
   if($result2['administrator'] == 0 && $result2['master'] == 0) puterror2($TEXT['error-supp11']);

  if($result2['master'] == 1 && $result2['administrator'] == 0) $mast_vis = " AND visible='show' ";
   else $mast_vis = "";

  if(!isset($button)) $button = $TEXT['support16'];
  if(!isset($action)) $action = "pass_mail.php";
  if(!isset($button2)) $button2 = $TEXT['search-btn'];
  if(!isset($action2)) $action2 = "pass_mailform.php";
  if(!isset($button3)) $button3 = $TEXT['pass-btn'];

  if(!empty($_POST['pas_str_id'])) $_GET['pas_str_id'] = $_POST['pas_str_id'];
   else $_GET['pas_str_id'] = $_GET['pas_str_id'];

  if(!empty($_POST['pas_id'])) $_GET['pas_id'] = $_POST['pas_id'];
   else $_GET['pas_id'] = $_GET['pas_id'];

  if(!empty($_POST['uv'])) $_GET['uv'] = $_POST['uv'];
   else $_GET['uv'] = $_GET['uv'];

  if(!empty($_POST['k'])) $_GET['k'] = $_POST['k'];
   else $_GET['k'] = $_GET['k'];

  if(empty($_GET['pas_str_id'])) puterror2($TEXT['error-zhurn1']);
  if(strlen($_GET['pas_str_id'])>11) puterror2($TEXT['error-zhurn2']);
  if(empty($_GET['pas_id'])) puterror2($TEXT['error-zhurn11']);
  if(strlen($_GET['pas_id'])>11) puterror2($TEXT['error-zhurn12']);
  if(strlen($_GET['k'])>8) puterror2($TEXT['error-zhurn15']);
  $old_pas_str_id = $_GET['pas_str_id'];
  $old_pas_id = $_GET['pas_id'];
  $old_k = $_GET['k'];

  if(!empty($_POST['kfr'])) 
  {
    if($_POST['uv'] == "on")
   {
    $query_iskh_p = "SELECT AES_DECRYPT(dom_pass,'".$_POST['kfr']."') AS dom_pwd FROM `tbl_pass_keep` WHERE `visible` = 'show'";
      $result_iskh_p = mysql_query($query_iskh_p);
        if(!$result_iskh_p) puterror2($TEXT['error-tbl53']);
          $result_iskh_p2 = mysql_fetch_array($result_iskh_p,MYSQL_ASSOC);
          if(empty($result_iskh_p2['dom_pwd'])) 
           {
              echo "<table class='tabledata' align='center'>
                <tr><td class='z1'>".$TEXT['pass-key']."</td></tr>
                </table>
                <br />
                <form enctype='multipart/form-data' name='form' action=".$action2." method=post>
                <table class='tabledata' align='center' border='0'>
                <tr><td><input type='password' size=40 maxlength=255 name='kfr' /></td></tr>
                <tr><td align='center' ><input class='button' type=submit value='".$button3."' />
                <input type=hidden name=pas_str_id value='".$_GET['pas_str_id']."' />
                <input type=hidden name=pas_id value='".$_GET['pas_id']."' />
                <input type=hidden name=k value='".$_GET['k']."' />
                <input type=hidden name=uv value='".$_GET['uv']."' />
                </td></tr>
                </table>
                </form>";
              puterror3($TEXT['pass-err12']);
              exit();
          }

    $query_upd = "INSERT INTO tbl_p_tmp (`ptmp_id`, `usr_id`, `p_tmp`, `stat`) VALUES (NULL, '".$result2['login_id']."', '".$_POST['kfr']."', 1)";
     if(mysql_query($query_upd)) echo "<br /><table class='statssilka' align='center' border='0' width='30%'><tr><td><img class='tipsimg' alt='tips' src='../img/active.png' />".$TEXT['pass-key3']."</td></tr></table>";
      else echo "<table class='statssilka' align='center' border='0' width='60%'><tr><td><img class='tipsimg' alt='tips' src='../img/warn.png' />".$TEXT['pass-err10']."</td></tr></table>";
  }
  else 
   {
     $query_iskh_p = "SELECT AES_DECRYPT(dom_pass,'".$_POST['kfr']."') AS dom_pwd FROM `tbl_pass_keep` WHERE `visible` = 'hide'";
      $result_iskh_p = mysql_query($query_iskh_p);
        if(!$result_iskh_p) puterror2($TEXT['error-tbl53']);
          $result_iskh_p2 = mysql_fetch_array($result_iskh_p,MYSQL_ASSOC);
          if(empty($result_iskh_p2['dom_pwd'])) 
           {
              echo "<table class='tabledata' align='center'>
                <tr><td class='z1'>".$TEXT['pass-key']."</td></tr>
                </table>
                <br />
                <form enctype='multipart/form-data' name='form' action=".$action2." method=post>
                <table class='tabledata' align='center' border='0'>
                <tr><td><input type='password' size=40 maxlength=255 name='kfr' /></td></tr>
                <tr><td align='center' ><input class='button' type=submit value='".$button3."' />
                <input type=hidden name=pas_str_id value='".$_GET['pas_str_id']."' />
                <input type=hidden name=pas_id value='".$_GET['pas_id']."' />
                <input type=hidden name=k value='".$_GET['k']."' />
                <input type=hidden name=uv value='".$_GET['uv']."' />
                </td></tr>
                </table>
                </form>";
              puterror3($TEXT['pass-err12']);
              exit();
          }

    $query_upd = "INSERT INTO tbl_p_tmp (`ptmp_id`, `usr_id`, `p_tmp`, `stat`) VALUES (NULL, '".$result2['login_id']."', '".$_POST['kfr']."', 2)";
     if(mysql_query($query_upd)) echo "<br /><table class='statssilka' align='center' border='0' width='30%'><tr><td><img class='tipsimg' alt='tips' src='../img/active.png' />".$TEXT['pass-key2']."</td></tr></table>";
      else echo "<table class='statssilka' align='center' border='0' width='60%'><tr><td><img class='tipsimg' alt='tips' src='../img/warn.png' />".$TEXT['pass-err10']."</td></tr></table>";
     
    }
  }
  
  if($_GET['uv'] == "on")
   {
    $query_count = mysql_query("SELECT COUNT(ptmp_id) FROM tbl_p_tmp WHERE usr_id = '".$result2['login_id']."' AND stat = '1' ");
       $coun_rows = mysql_result($query_count, 0);
       if($coun_rows == 0) {
       echo "<table class='tabledata' align='center'>
                <tr><td class='z1'>".$TEXT['pass-key']."</td></tr>
                </table>
                <br />
                <form enctype='multipart/form-data' name='form' action=".$action2." method=post>
                <table class='tabledata' align='center' border='0'>
                <tr><td><input type='password' size=40 maxlength=255 name='kfr' /></td></tr>
                <tr><td align='center' ><input class='button' type=submit value='".$button3."' />
                <input type=hidden name=pas_str_id value='".$_GET['pas_str_id']."' />
                <input type=hidden name=pas_id value='".$_GET['pas_id']."' />
                <input type=hidden name=k value='".$_GET['k']."' />
                <input type=hidden name=uv value='".$_GET['uv']."' />
                </td></tr>
                </table>
                </form>";
           exit();
           }
        else $srch_pas = " AND `stat` = '1' ";
   }
    else
     {
      $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) {
       echo "<table class='tabledata' align='center'>
                <tr><td class='z1'>".$TEXT['pass-key']."</td></tr>
                </table>
                <br />
                <form enctype='multipart/form-data' name='form' action=".$action2." method=post>
                <table class='tabledata' align='center' border='0'>
                <tr><td><input type='password' size=40 maxlength=255 name='kfr' /></td></tr>
                <tr><td align='center' ><input class='button' type=submit value='".$button3."' />
                <input type=hidden name=pas_str_id value='".$_GET['pas_str_id']."' />
                <input type=hidden name=pas_id value='".$_GET['pas_id']."' />
                <input type=hidden name=k value='".$_GET['k']."' />
                <input type=hidden name=uv value='".$_GET['uv']."' />
                </td></tr>
                </table>
                </form>";
           exit();
           }
        else $srch_pas = " AND `stat` = '2' ";
     }

  $query3 = "SELECT tbl_pass_keep.fio FROM `tbl_pass_keep` WHERE tbl_pass_keep.pas_id = ".$_GET['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'];

if(empty($_GET['user_mail']))
 {
  if(!empty($_POST['user_mail'])) $user_mail = $_POST['user_mail'].";";
   else $user_mail = "";
 }
 else $user_mail = $_GET['user_mail'];

if(!empty($_GET['user_id'])) {
 $query1 = "SELECT
      tbl_users.user_id,
      tbl_users.familiya,
      tbl_users.name,
      tbl_users.otchestvo,
      tbl_users.mail,
      tbl_domain.domain
      FROM tbl_users
      JOIN tbl_domain ON tbl_users.dom_id=tbl_domain.dom_id
     WHERE tbl_users.user_id = '".$_GET['user_id']."'";
    $result1 = mysql_query($query1);
     if(!$result1) puterror3($TEXT['error-tbl2']);
     $res_mail = mysql_fetch_array($result1);
     $user_fio = $res_mail['familiya']." ".$res_mail['name']." ".$res_mail['otchestvo'];
     $user_mail_tmp = $res_mail['mail']."@".$res_mail['domain'];
}
$user_mail .= $user_mail_tmp;
?>
<table class="tabledata" align="center" border="0">
  <tr><td class="z1"><?php echo $TEXT['pass-invite']; ?></td></tr>
  <tr><td class="help"></td></tr>
</table>
<form enctype='multipart/form-data' action=<?php echo $action2; ?> method=post>
<table class="table" border="0" align="center" cellpadding="0" cellspacing="0" width="30%">
<tr><td>
<fieldset class="filterr">
<legend align="left" title="<?php echo $TEXT['pass-flt-tip']; ?>"><?php echo $TEXT['support104']; ?></legend>
<table class="table" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr class="zag2"><td><?php echo $TEXT['search-name2']; ?></td><td></td></tr>
<tr>
<td><input size=30 maxlength=125 type=text name=familiya title='<?php echo $TEXT['pass-flt-tip']; ?>' value='<?php echo $familiya; ?>'></td>
<td>
    <input type=hidden name=pas_str_id value='<?php echo $_GET['pas_str_id']; ?>' />
    <input type=hidden name=pas_id value='<?php echo $_GET['pas_id']; ?>' />
    <input type=hidden name=k value='<?php echo $_GET['k']; ?>' />
    <input type=hidden name=uv value='<?php echo $_GET['uv']; ?>' />
    <input type=hidden name=user_mail value='<?php echo $user_mail; ?>' />
    <input class="button" type=submit value='<?php echo $button2; ?>' />
</td>
</tr>
</table>
</fieldset>
</td></tr>
</table>
</form>
<br />
<?php
if(empty($_POST['familiya'])) $_POST['familiya'] = " ";
 else {
           echo "<table class='passusersrch' border='1' cellpadding='3' cellspacing='0' width='70%' align='center' >
            <tr class='tableheadercat' border='1'>
            <td>".$TEXT['pass-usr-fio']."</td>
            <td>".$TEXT['user-e-mail']."</td>
            <td>".$TEXT['pass-action']."</td>
            </tr>";

  $query1 = "SELECT 
 tbl_users.user_id,
 tbl_users.familiya,
 tbl_users.name,
 tbl_users.otchestvo,
 tbl_users.mail,
 tbl_domain.domain
 FROM tbl_users
 JOIN tbl_domain ON tbl_users.dom_id=tbl_domain.dom_id
 WHERE LOWER(tbl_users.familiya) LIKE LOWER('%".$_POST['familiya']."%')";
  $result1 = mysql_query($query1);
  if(!$result1) puterror3($TEXT['error-tbl2']);
    while($res_mail = mysql_fetch_array($result1))
     {
          echo " <tr class='support23' onmouseover=\"this.style.backgroundColor='#cccccc';\" onmouseout=\"this.style.backgroundColor='#ffffff';\">
             <td><a href=# onClick=\"window.open('../user.php?user_id=".$res_mail['user_id']."','popup','top=".$TEXT['user-top'].",left=".$TEXT['user-left'].",width=".$TEXT['user-width'].",height=".$TEXT['user-height'].",status=no,scrollbars=no,location=no,menubar=no,toolbar=no')\" title ='".$TEXT['global-usr']."'>".$res_mail['familiya']." ".$res_mail['name']." ".$res_mail['otchestvo']."</a></td>
             <td>".$res_mail['mail']."@".$res_mail['domain']."</td>
            <td><a href='pass_mailform.php?user_id=".$res_mail['user_id'],"&amp;pas_str_id=".$_GET['pas_str_id']."&amp;pas_id=".$_GET['pas_id']."&amp;k=".$_GET['k']."&amp;uv=".$_GET['uv']."&amp;user_mail=".$user_mail."' title='".$TEXT['pass-use-tit'],"'>".$TEXT['pass-use']."</a></td>
           </tr>";
     }
     echo "</table>";
   }
$uv = $_GET['uv'];
$user_mail = str_ireplace(";;",";",$user_mail);
?>
<form enctype='multipart/form-data' name='rprt' action=<?php echo $action; ?> method=post>
<table class="tabledata" align="center">
<tr><td class="support22"><?php echo $TEXT['pass-invite-to']; ?>:</td><td class="support"><input class="inputmandatory" size=50 maxlength=255 type=text name=email value='<?php echo $user_mail; ?>'/></td><td></td></tr>
<tr><td class="support22"><?php echo $TEXT['pass-invite4']; ?>:</td><td class="support"><?php echo $full_name; ?></td><td></td></tr>
<tr><td class="support22"><?php echo $TEXT['pass-invite2']; ?>:</td><td class="support"><input type=radio name=typepar value=1 checked /></td><td></td></tr>
<tr><td class="support22"><?php echo $TEXT['pass-invite1']; ?>:</td><td class="support"><input type=radio name=typepar value=2 /></td><td></td></tr>
<tr><td class="support22"><?php echo $TEXT['pass-invite3']; ?>:</td><td class="support"><input type=radio name=typepar value=3 /></td><td></td></tr>
<tr><td colspan="2"><input class="button" type=submit value='<?php echo $button; ?>' />
  <input type=hidden name=pas_str_id value='<?php echo $old_pas_str_id; ?>' />
  <input type=hidden name=pas_id value='<?php echo $old_pas_id; ?>' />
  <input type=hidden name=k value='<?php echo $old_k; ?>' />
  <input type=hidden name=uv value='<?php echo $uv; ?>' />
</td>
<td></td>
</tr>
</table>
</form>
</body>
</html>