<?php
  ///////////////////////////////////////////////////
  //2010 (C)
  //Гришаев Дмитрий
  //Карпухин Павел
  ///////////////////////////////////////////////////

  $lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
  if(is_file ("../lang/".$lang."/".$lang.".php")) {include "../lang/".$lang."/".$lang.".php";}
     else {include "../lang/ru/ru.php";}

  $title = $TEXT['global-import-tit'];

  require_once "../config_a.php";
  require_once "../utils/functions.php";
  require_once "../utils/top_adm.php";

  if($result2['administrator'] == 0) {puterror($result2['skin'],$TEXT['error-loginform2']); require_once "../utils/bottom_adm.php"; exit();}

  if(empty($_POST['srv_id'])) {puterror($result2['skin'],$TEXT['error-srv21']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['srv_id'])>5) {puterror($result2['skin'],$TEXT['error-srv22']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['typefile'])) {puterror($result2['skin'],$TEXT['import_err2']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['typefile'])>1) {puterror($result2['skin'],$TEXT['import_err3']); require_once "../utils/bottom_adm.php"; exit();}

  $_POST['srv_id'] = rep($_POST['srv_id']);
  $_POST['typefile'] = rep($_POST['typefile']);

  $typefile = "linux";
    switch($_POST['typefile'])
      {
      case 1:
      $typefile = "linux";
      break;
      case 2:
      $typefile = "microsoft";
      break;
      case 3:
      $typefile = "svoy";
      break;
      }

  if($_FILES['impfl']['size'] > 1024*10*1024) {puterror($result2['skin'],$TEXT['import_err1']); require_once "../utils/bottom_adm.php"; exit();}
  $dis_keys = mysqli_query($dbcnx,"ALTER TABLE DISABLE KEYS");
  $clr_tbl = mysqli_query($dbcnx,"DELETE FROM `tbl_ip` WHERE `srv_id` = '".$_POST['srv_id']."' ");

    if (!empty($_FILES['impfl']['tmp_name']))
      {
       $delimiter = ";";
       $delimiter2 = ",";
       $delimiter3 = ".";
       //$enclosure = "\r\n";
       $handle = file($_FILES['impfl']['tmp_name']);
       $size = filesize($_FILES['impfl']['tmp_name'])/1024;
       $numrows = count($handle);
       $rows = Array();
       $begin = "begin";
       $end = "end";
       $begin_addr = "{";
       $end_addr = "}";
       $host_name = "host";
       $hw_addr = "hardware ethernet";
       $hw_from = ":";
       $hw_to = "";
       $fix_addr = "fixed-address";
       $descr_ip = "#";
       $name = "";
       $hw = "";
       $ip_addr = "";
       $descr = "";
       $str_bufer = "";
       $flak = 0;
       $end_ip = 0;
       $n = 0;

       foreach ($handle as $line_num => $line) 
        {
         $line_total = ($line_num + 1);
         $linedump=trim($line);
         if (!($linedump ==''))
         {
         if($typefile == "linux")
         {
          if(!(stripos($linedump,$descr_ip,0) === false) && $flak === 1)
          {
           $flak = 0;
          }
          if($flak == 8 || $flak == 0)
          {
          $flak = 0;
          $str_bufer = $linedump;
          $flak = 1;
          $name = "";
          $hw = "";
          $ip_addr = "";
          $descr = "";
          $end_ip = 0;
          }
          if($flak > 0)
          {
              $vkl = "1";
              if(!(($st_host = stripos($linedump,$host_name,0)) === false) && $name == '')
              {
                 if(($end_host = stripos($linedump,$begin_addr,$st_host + 4)) === false)
                 {
                   $end_host = strlen($linedump);
                 }
                 $name = substr($linedump,$st_host + 4,$end_host - ($st_host + 4));
                 $name = strtolower(trim($name));
                  if(!(($st_descr = stripos($str_bufer,$descr_ip,0)) === false) && $name != '')
                  {
                    $descr = trim(substr($str_bufer,$st_descr +1,(strlen($str_bufer) -1)));
                    if($descr === '') {$descr = "";}
                  }
              }
              if(!(($st_hw = stripos($linedump,$hw_addr,0)) === false) && $hw == '')
              {
                 if(($end_hw = stripos($linedump,$delimiter,$st_hw + 17)) === false)
                 {
                   $end_hw = strlen($linedump);
                 }
                 $hw = substr($linedump,$st_hw + 17,$end_hw - ($st_hw + 17));
                 $hw = strtoupper(trim($hw));
                 $hw = str_replace($hw_from,$hw_to,$hw);

              }
              if(!(($st_ip = stripos($linedump,$fix_addr,0)) === false) && $ip_addr == '')
              {
                 if(($end_ip = stripos($linedump,$delimiter,$st_ip + 13)) === false)
                 {
                   $end_ip = strlen($linedump);
                 }
                 $ip_addr = substr($linedump,$st_ip + 13,$end_ip - ($st_ip + 13));
                 $ip_addr = trim($ip_addr);
                 $addr = explode($delimiter3,$ip_addr,4);
                  $oct1 = strlen($addr['0']);
                   if($oct1 == 1) {$oct_res1 = "00".$addr['0'];}
                    elseif($oct1 == 2) {$oct_res1 = "0".$addr['0'];}
                     else {$oct_res1 = $addr['0'];}
                  $oct2 = strlen($addr['1']);
                   if($oct2 == 1) {$oct_res2 = "00".$addr['1'];}
                    elseif($oct2 == 2) {$oct_res2 = "0".$addr['1'];}
                     else {$oct_res2 = $addr['1'];}
                  $oct3 = strlen($addr['2']);
                   if($oct3 == 1) {$oct_res3 = "00".$addr['2'];}
                    elseif($oct3 == 2) {$oct_res3 = "0".$addr['2'];}
                     else {$oct_res3 = $addr['2'];}
                  $oct4 = strlen($addr['3']);
                   if($oct4 == 1) {$oct_res4 = "00".$addr['3'];}
                    elseif($oct4 == 2) {$oct_res4 = "0".$addr['3'];}
                     else {$oct_res4 = $addr['3'];}
                $ip_addr = $oct_res1.".".$oct_res2.".".$oct_res3.".".$oct_res4;
              }
              if(!(stripos($linedump,$end_addr,0) === false))
              {
                $end_ip = 1;
              }
          }
         }
         if($typefile == "microsoft")
         {
          if($line_num > 0)
          {
           $csv_line = explode($delimiter2,$linedump,4);
           $name = strtolower(trim($csv_line[1]));
           $hw = strtoupper(trim($csv_line[2]));
           $ip_addr = trim($csv_line[0]);
           $addr = explode($delimiter3,$ip_addr,4);
            $oct1 = strlen($addr['0']);
             if($oct1 == 1) {$oct_res1 = "00".$addr['0'];}
              elseif($oct1 == 2) {$oct_res1 = "0".$addr['0'];}
               else {$oct_res1 = $addr['0'];}
             $oct2 = strlen($addr['1']);
              if($oct2 == 1) {$oct_res2 = "00".$addr['1'];}
               elseif($oct2 == 2) {$oct_res2 = "0".$addr['1'];}
                else {$oct_res2 = $addr['1'];}
             $oct3 = strlen($addr['2']);
              if($oct3 == 1) {$oct_res3 = "00".$addr['2'];}
               elseif($oct3 == 2) {$oct_res3 = "0".$addr['2'];}
                else {$oct_res3 = $addr['2'];}
             $oct4 = strlen($addr['3']);
              if($oct4 == 1) {$oct_res4 = "00".$addr['3'];}
               elseif($oct4 == 2) {$oct_res4 = "0".$addr['3'];}
                else {$oct_res4 = $addr['3'];}
           $ip_addr = $oct_res1.".".$oct_res2.".".$oct_res3.".".$oct_res4;
           $descr = trim($csv_line[3]);
           if($descr === '') {$descr = "";}
           $vkl = "1";
           $end_ip = 1;
          }
         }
         if($typefile == "svoy")
         {
          if($line_num > 0)
          {
           $csv_line = explode($delimiter2,$linedump,5);
           $name = strtolower(trim($csv_line[1]));
           $hw = strtoupper(trim($csv_line[2]));
           $ip_addr = trim($csv_line[0]);
           $addr = explode($delimiter3,$ip_addr,4);
            $oct1 = strlen($addr['0']);
             if($oct1 == 1) {$oct_res1 = "00".$addr['0'];}
              elseif($oct1 == 2) {$oct_res1 = "0".$addr['0'];}
               else {$oct_res1 = $addr['0'];}
             $oct2 = strlen($addr['1']);
              if($oct2 == 1) {$oct_res2 = "00".$addr['1'];}
               elseif($oct2 == 2) {$oct_res2 = "0".$addr['1'];}
                else {$oct_res2 = $addr['1'];}
             $oct3 = strlen($addr['2']);
              if($oct3 == 1) {$oct_res3 = "00".$addr['2'];}
               elseif($oct3 == 2) {$oct_res3 = "0".$addr['2'];}
                else {$oct_res3 = $addr['2'];}
             $oct4 = strlen($addr['3']);
              if($oct4 == 1) {$oct_res4 = "00".$addr['3'];}
               elseif($oct4 == 2) {$oct_res4 = "0".$addr['3'];}
                else {$oct_res4 = $addr['3'];}
           $ip_addr = $oct_res1.".".$oct_res2.".".$oct_res3.".".$oct_res4;
           $descr = trim($csv_line[3]);
           if($descr === '') {$descr = "";}
           $vkl = trim($csv_line[4]);
           if($vkl === '') {$vkl = "0";}
            else {$vkl = $vkl;}
           $end_ip = 1;
          }
         }
          if($name !== '' && $hw !== '' && $ip_addr !== '' && $end_ip == 1)
          {
          $n = ($n+1);
          $query = "INSERT INTO tbl_ip (
          `ip_id`,
          `srv_id`,
          `name`,
          `ip_addr`,
          `hw`,
          `descr`,
          `vkl_ip`,
          `data`,
          `state_addr`,
          `last_acc`)
          VALUES (
          NULL,
          '".$_POST['srv_id']."',
          '$name',
          '$ip_addr',
          '$hw',
          '$descr',
          '$vkl',
          CURRENT_DATE(),
          '0',
          '1970-01-01')";
          $result = mysqli_query($dbcnx,$query);
           if(!$result) {puterror($result2['skin'],mysqli_error($dbcnx)); require_once "../utils/bottom_adm.php"; exit();}

          $flak = 8;
          }
        }
      }
      }
      unlink($_FILES['impfl']['tmp_name']);

   $en_keys = mysqli_query($dbcnx,"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['familiya']." ".$result2['name'];
  //*************************************
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NULL , '$ip' , '$who' , '$doing' , '$what')";
        if(mysqli_query($dbcnx,$query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=../addresses/index.php' />
                   </head></html>";
           } else {puterror($result2['skin'],$TEXT['error-log']); require_once "../utils/bottom_adm.php"; exit();}
  require_once "../utils/bottom_adm.php";
?>