<?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";
  ?>
  <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($_POST['pas_str_id'])) puterror2($TEXT['error-zhurn1']);
  if(strlen($_POST['pas_str_id'])>11) puterror2($TEXT['error-zhurn2']);
  $_POST['pas_str_id'] = rep($_POST['pas_str_id']);

  $action = "local";
  switch($_POST['typepar'])
  {
    case 1:
    $action = "local";
    break;
    case 2:
    $action = "keepass";
    break;
  }

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

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

  if($_FILES['impfl']['size'] > 1024*10*1024) puterror2($TEXT['pass-err13']);
  $dis_keys = mysql_query("ALTER TABLE DISABLE KEYS");

    if (!empty($_FILES['impfl']['tmp_name']))
      {
       $delimiter = ";";
       $delimiter_keepass=",";
       $handle = file($_FILES['impfl']['tmp_name']);
       $size = filesize($_FILES['impfl']['tmp_name'])/1024;
       $numrows = count($handle);
       $rows = Array();
       $n = 0;

       foreach ($handle as $line_num => $line)
        {
         $line_total = ($line_num + 1);
         $linedump=trim($line);
         if(!($linedump ==''))
         {
          if($line_num > 0)
          {
           if($action == "local")
           {
           $csv_line = explode($delimiter,$linedump,18);
           $fio = trim($csv_line[0]);
           $usr_id = trim($csv_line[1]);
           $dom_lgn = trim($csv_line[2]);
           $dp_tmp = trim($csv_line[3]);
           $mail_lgn = trim($csv_line[4]);
           $mp_tmp = trim($csv_line[5]);
           $primechanie = trim(str_replace($search, $replace, $csv_line[6]));
           $visible = trim($csv_line[7]);
           $data_create = trim($csv_line[8]);
           $data_edt = trim($csv_line[9]);
           $best_before = trim($csv_line[10]);
           $login_id = trim($csv_line[11]);
           $login_edt_id = trim($csv_line[12]);
           $mast_edt = trim($csv_line[13]);
           $mail_form  = trim($csv_line[14]);
           $mail_pass_form = trim($csv_line[15]);
           $dom_form  = trim($csv_line[16]);
           $dom_pass_form = trim($csv_line[17]);
           $fio = rep($fio);
           $usr_id = rep($usr_id);
           $dom_lgn = rep($dom_lgn);
           $mail_lgn = rep($mail_lgn);
           $primechanie = rep2($primechanie);
           $visible = rep($visible);
           $data_create = rep($data_create);
           $data_edt = rep($data_edt);
           $best_before = rep($best_before);
           $login_id = rep($login_id);
           $login_edt_id = rep($login_edt_id);
           $mast_edt = rep($mast_edt);
           $mail_form = rep($mail_form);
           $mail_pass_form = rep($mail_pass_form);
           $dom_form = rep($dom_form);
           $dom_pass_form = rep($dom_pass_form);

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

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

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

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

           if(!empty($data_create)) $data_create = "'".$data_create."'";
            else $data_create = "NOW()";

           if(!empty($login_id)) $login_id = $login_id;
            else $login_id = $result2['login_id'];

           if(!empty($mast_edt)) $mast_edt = $mast_edt;
            else $mast_edt = "no";

           if(!empty($visible)) $visible = $visible;
            else $visible = "show";

           if($visible == "show")
            {
              $dom_pass = "AES_ENCRYPT('".$dp_tmp."','".$kfr_tmp1."')";
              $mail_pass = "AES_ENCRYPT('".$mp_tmp."','".$kfr_tmp1."')";
              $dom_login = "AES_ENCRYPT('".$dom_lgn."','".$kfr_tmp1."')";
              $mail_login = "AES_ENCRYPT('".$mail_lgn."','".$kfr_tmp1."')";
            }
            else
            {
              $dom_pass = "AES_ENCRYPT('".$dp_tmp."','".$kfr_tmp2."')";
              $mail_pass = "AES_ENCRYPT('".$mp_tmp."','".$kfr_tmp2."')";
              $dom_login = "AES_ENCRYPT('".$dom_lgn."','".$kfr_tmp2."')";
              $mail_login = "AES_ENCRYPT('".$mail_lgn."','".$kfr_tmp2."')";
             }
          }
          elseif($action == "keepass")
           {
           $linedump = str_replace($search2, $replace2, $linedump);
           $csv_line = explode($delimiter_keepass,$linedump,6);
           $fio = trim($csv_line[0]);
           $usr_id = "";
           $dom_lgn = trim($csv_line[1]);
           $dp_tmp = trim($csv_line[2]);
//           $mail_lgn = trim($csv_line[3]);
//           $mp_tmp = trim($csv_line[4]);
           $primechanie = trim(str_replace($search, $replace, $csv_line[3]));
           $visible = "hide";
           $data_create = trim(str_ireplace("T", " ", $csv_line[4]));
           $data_edt = trim(str_ireplace("T", " ", $csv_line[5]));
           $best_before = trim(str_ireplace("2999-12-28T23:59:59", "0000-00-00", $csv_line[6]));
           $login_id = $result2['login_id'];
           $login_edt_id = "";
           $mast_edt = "no";
           $mail_form  = $TEXT['support34']."(e-mail)";
           $mail_pass_form = $TEXT['install-pswd']."(e-mail)";
           $dom_form  = $TEXT['support34'];
           $dom_pass_form = $TEXT['install-pswd'];
           $fio = rep($fio);
           $usr_id = rep($usr_id);
           $dom_lgn = rep($dom_lgn);
           $mail_lgn = rep($mail_lgn);
           $primechanie = rep2($primechanie);
           $visible = rep($visible);
           $data_create = rep($data_create);
           $data_edt = rep($data_edt);
           $best_before = rep($best_before);
           $login_id = rep($login_id);
           $login_edt_id = rep($login_edt_id);
           $mast_edt = rep($mast_edt);
           $mail_form = rep($mail_form);
           $mail_pass_form = rep($mail_pass_form);
           $dom_form = rep($dom_form);
           $dom_pass_form = rep($dom_pass_form);

           if(!empty($data_create)) $data_create = "'".$data_create."'";
            else $data_create = "NOW()";

           if($visible == "show")
            {
              $dom_pass = "AES_ENCRYPT('".$dp_tmp."','".$kfr_tmp1."')";
              $mail_pass = "AES_ENCRYPT('".$mp_tmp."','".$kfr_tmp1."')";
              $dom_login = "AES_ENCRYPT('".$dom_lgn."','".$kfr_tmp1."')";
              $mail_login = "AES_ENCRYPT('".$mail_lgn."','".$kfr_tmp1."')";
            }
            else
            {
              $dom_pass = "AES_ENCRYPT('".$dp_tmp."','".$kfr_tmp2."')";
              $mail_pass = "AES_ENCRYPT('".$mp_tmp."','".$kfr_tmp2."')";
              $dom_login = "AES_ENCRYPT('".$dom_lgn."','".$kfr_tmp2."')";
              $mail_login = "AES_ENCRYPT('".$mail_lgn."','".$kfr_tmp2."')";
             }
          }

          $n = ($n+1);
          $query = "INSERT INTO `tbl_pass_keep` (
              `pas_id` ,
              `pas_str_id`,
              `fio`,
              `usr_id`,
              `dom_lgn`,
              `dom_pass`,
              `mail_lgn`,
              `mail_pass`,
              `primechanie`,
              `visible`,
              `data_create`,
              `data_edt`,
              `best_before`,
              `login_id`,
              `login_edt_id`,
              `mast_edt`,
              `korzina`,
              `mail_form`,
              `mail_pass_form`,
              `pic_tr`,
              `dom_form`,
              `dom_pass_form`)
              VALUES (
              NULL ,
              '".$_POST['pas_str_id']."',
              '".$fio."',
              '".$usr_id."',
             $dom_login,
             $dom_pass,
             $mail_login,
             $mail_pass,
             '".$primechanie."',
             '".$visible."',
             $data_create,
             '".$data_edt."',
             '".$best_before."',
            '".$login_id."',
            '".$login_edt_id."',
            '".$mast_edt."',
            '0',
            '".$mail_form."',
            '".$mail_pass_form."',
            '',
            '".$dom_form."',
            '".$dom_pass_form."') ";
            $result = mysql_query($query);
           if(!$result) puterror2($TEXT['error-tbl52']);
           }
        }
      }
      }
      unlink($_FILES['impfl']['tmp_name']);

   $en_keys = mysql_query("ALTER TABLE ENABLE KEYS");
  //*************************************
  //Информация для лога
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-import1'];
  $what = $TEXT['log-import2']."(".round($size, 2)." kb), ".$TEXT['log-import3']."(".$numrows."), ".$TEXT['log-import4']."(".$n.")";
  $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']."' />
                   </head></html>";
         } else puterror2($TEXT['error-tbl52']);
?>
</body>
</html>