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

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

  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 = ";";
       $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)
          {
           $csv_line = explode($delimiter,$linedump,7);
           $krit_id = trim($csv_line[0]);
           $zh_tema = trim($csv_line[1]);
           $zh_soobschenie = trim(str_replace($search, $replace, $csv_line[2]));
           $position = trim($csv_line[3]);
           $visible = trim($csv_line[4]);
           $data = trim($csv_line[5]);
           $primechanie = trim(str_replace($search, $replace, $csv_line[6]));
           $krit_id = rep($krit_id);
           $zh_tema = rep($zh_tema);
           $zh_soobschenie = rep($zh_soobschenie);
           $position = rep($position);
           $visible = rep($visible);
           $data = rep($data);
           $primechanie = rep($primechanie);

          $n = ($n+1);
          $query = "INSERT INTO `tbl_bd_zhnaniy` (
              `zhurn_id`,
              `krit_id`,
              `zh_str_id`,
              `zh_tema`,
              `zh_soobschenie`,
              `position`,
              `visible`,
              `data`,
              `login_id`,
              `primechanie`)
              VALUES (
              NULL ,
              '".$krit_id."',
              '".$_POST['zh_str_id']."',
              '".$zh_tema."',
              '".$zh_soobschenie."',
              '".$position."',
              '".$visible."',
             '".$data."',
             '".$result2['login_id']."',
             '".$primechanie."' ) ";
            $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?zh_str_id=".$_POST['zh_str_id']."' />
                   </head></html>";
         } else puterror2($TEXT['error-tbl52']);
?>
</body>
</html>