<?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['pass-exp'];
  $helppage = "";

  require_once "../config.php";
  require_once "top.php";

  if(is_file ("../admin/lang/".$lang."/trans.php")) include "../admin/lang/".$lang."/trans.php";
   else include "../admin/lang/ru/trans.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(strlen($_GET['pas_str_id'])>11) puterror2($TEXT['error-zhurn2']);

  $_GET['pas_str_id'] = rep($_GET['pas_str_id']);

  if($_GET['pas_str_id'] == "") $pas_str_id_tmp = "`pas_str_id` > 0";
   else $pas_str_id_tmp =  "`pas_str_id` = '".$_GET['pas_str_id']."'";
if(!empty($_GET['pas_str_id']))
   {
   $resourcebr = "SELECT strukt_name FROM tbl_pass_strukt WHERE pas_str_id=".$_GET['pas_str_id']."";
    $resultbr=mysql_query($resourcebr);
     $result_bread=mysql_fetch_array($resultbr);
   }

  $search = array("\r\n",";");
  $replace = array("<br />","t---z");

  $exp_path = "../NormDoc/pass-".translate($result_bread['strukt_name'])."-".date("YMd").".csv";

  $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 = "SELECT * FROM `tbl_pass_keep` WHERE $pas_str_id_tmp";
     $result = mysql_query($query);
     if(!$result) puterror3($TEXT['error-tbl52']);
       if(mysql_num_rows($result)>0)
         {
         $file = fopen($exp_path,"a");
         $exp_head = $TEXT['search-nazv'].";".$TEXT['pass-spr-usr'].";".$TEXT['support34'].";".$TEXT['install-pswd'].";".$TEXT['support34']."(e-mail);".$TEXT['install-pswd']."(e-mail);".$TEXT['know-prim'].";".$TEXT['meet-view'].$TEXT['pass-tp'].";".$TEXT['pass-data1'].";".$TEXT['pass-data2'].";".$TEXT['pass-bb'].";".$TEXT['zhurn-autor'].";".$TEXT['pass-editor'].";".$TEXT['pass-edt-true'].";".$TEXT['pass-field1'].";".$TEXT['pass-field2'].";".$TEXT['pass-field1']."1;".$TEXT['pass-field2']."1\r\n";
         fwrite($file, $exp_head, strlen($exp_head));

          while($result_exps = mysql_fetch_array($result,MYSQL_ASSOC))
          {
            if($result_exps['visible'] == "hide") {
            $query_iskh_p = "SELECT AES_DECRYPT(tbl_pass_keep.dom_pass,'".$kfr_tmp2."') AS dom_pwd, AES_DECRYPT(tbl_pass_keep.dom_lgn,'".$kfr_tmp2."') AS dom_login, AES_DECRYPT(tbl_pass_keep.mail_pass,'".$kfr_tmp2."') AS mail_pwd, AES_DECRYPT(tbl_pass_keep.mail_lgn,'".$kfr_tmp2."') AS mail_login FROM `tbl_pass_keep` WHERE `pas_id` = '".$result_exps['pas_id']."' LIMIT 1";
            $result_iskh_p = mysql_query($query_iskh_p);
                if(!$result_iskh_p) puterror2($TEXT['error-tbl54']);
                $result_iskh_p1 = mysql_fetch_array($result_iskh_p,MYSQL_ASSOC);
                $key_dom = $result_iskh_p1['dom_pwd'];
                $key_mail = $result_iskh_p1['mail_pwd'];
                $login_usr = $result_iskh_p1['dom_login'];
                $login_mail = $result_iskh_p1['mail_login'];
            } else
               {
                $query_iskh_p = "SELECT AES_DECRYPT(tbl_pass_keep.dom_pass,'".$kfr_tmp1."') AS dom_pwd, AES_DECRYPT(tbl_pass_keep.dom_lgn,'".$kfr_tmp1."') AS dom_login, AES_DECRYPT(tbl_pass_keep.mail_pass,'".$kfr_tmp1."') AS mail_pwd, AES_DECRYPT(tbl_pass_keep.mail_lgn,'".$kfr_tmp1."') AS mail_login FROM `tbl_pass_keep` WHERE `pas_id` = '".$result_exps['pas_id']."' LIMIT 1";
                $result_iskh_p = mysql_query($query_iskh_p);
                 if(!$result_iskh_p) puterror2($TEXT['error-tbl54']);
                 $result_iskh_p1 = mysql_fetch_array($result_iskh_p,MYSQL_ASSOC);
                 $key_dom = $result_iskh_p1['dom_pwd'];
                 $key_mail = $result_iskh_p1['mail_pwd'];
                 $login_usr = $result_iskh_p1['dom_login'];
                 $login_mail = $result_iskh_p1['mail_login'];
               }
           $primechanie = str_replace($search, $replace, $result_exps['primechanie']);
           $exp_string =  $result_exps['fio'].";".$result_exps['usr_id'].";".$login_usr.";".$key_dom.";".$login_mail.";".$key_mail.";".$primechanie.";".$result_exps['visible'].";".$result_exps['data_create'].";".$result_exps['data_edt'].";".$result_exps['best_before'].";".$result_exps['login_id'].";".$result_exps['login_edt_id'].";".$result_exps['mast_edt'].";".$result_exps['mail_form'].";".$result_exps['mail_pass_form'].";".$result_exps['dom_form'].";".$result_exps['dom_pass_form']."\r\n";

  	       fwrite($file, $exp_string, strlen($exp_string));
          }
          fclose($file);
          echo "<table align='center' class='table' border='0' width='30%' >
           <tr class='cntrrslt'><td><img src='../img/ok.png'></td></tr>
           <tr class='cntrrslt'><td>".$TEXT['pass-exp-result']."</td></tr>
          </table>";
          echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=pass_transfer.php?trans=".$exp_path."' />
                   </head></html>";
         } else puterror2($TEXT['search-err4']);
?>
</body>
</html>