<?php
  ///////////////////////////////////////////////////
  //2011 (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['menu-btn38'];

  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['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['typefile'] = rep($_POST['typefile']);

  $delimiter = ".";
  $delimiter2 = ";";

    switch($_POST['typefile'])
      {
      case 1:
      $delimiter2 = ";";
      break;
      case 2:
      $delimiter2 = ",";
      break;
      }

  echo "<table align='center' border='1' cellpadding='3' cellspacing='0' width='90%'>
      <thead><tr class='tableheadercat'>
            <td>".$TEXT['addr-ip-addr']."</td>
            <td>".$TEXT['addr-mac']."</td>
            <td>".$TEXT['addr-name']."</td>
            <td>".$TEXT['menu-port']."</td>
            <td>".$TEXT['menu-vlan']."</td>
            <td>".$TEXT['addr-descr']."</td>
            <td>".$TEXT['srv-name']."</td>
            <td>".$TEXT['srv-vkl-tit']."</td>
            </tr></thead><tbody id='tab'>";

  if($_FILES['impfl']['size'] > 1024*10*1024) {puterror($result2['skin'],$TEXT['import_err1']); require_once "../utils/bottom_adm.php"; exit();}
    if(!empty($_FILES['impfl']['tmp_name']))
      {
       $handle = file($_FILES['impfl']['tmp_name']);
       $size = filesize($_FILES['impfl']['tmp_name'])/1024;
       $numrows = count($handle);
       foreach ($handle as $line_num => $line) 
        {
         $line_total = ($line_num + 1);
         $linedump=trim($line);
         if(!($linedump ==''))
         {
          if($line_num > 0)
          {
           $csv_line = explode($delimiter2,$linedump,4);
           $vlan_num = trim($csv_line[0]);
           $vlan_name = trim($csv_line[1]);  
           $mac = strtoupper(trim($csv_line[2]));
           $port = strtoupper(trim($csv_line[3]));
           $type_prt = trim($csv_line[5]);

           $hwmass = Array(":","-");
           $mac=trim(strtoupper(str_replace($hwmass,"",$mac)));
           if(!preg_match("|^[0-9A-F]+$|i",$mac)) {puterror($result2['skin'],$TEXT['error-addr9']); require_once "../utils/bottom_adm.php"; exit();}
           if(strlen($mac) !== 12) {puterror($result2['skin'],$TEXT['error-addr9']); require_once "../utils/bottom_adm.php"; exit();}

$query_iskh_srv = "SELECT 
     tbl_ip.*,
     tbl_srv.srv_id,
     tbl_srv.name AS srv_name,
     tbl_srv.descr AS srv_descr
     FROM tbl_ip
     JOIN tbl_srv ON tbl_ip.srv_id=tbl_srv.srv_id
     WHERE tbl_ip.hw = '".$mac."'
     ORDER BY tbl_ip.name";
      $result_iskh_srv = mysqli_query($dbcnx,$query_iskh_srv);
        if(!$result_iskh_srv) {puterror($result2['skin'],$TEXT['error-tbl2']); require_once "../utils/bottom_adm.php"; exit();}
         $result_iskh_srv2 = mysqli_fetch_array($result_iskh_srv,MYSQLI_ASSOC);

      $addr = explode($delimiter,$result_iskh_srv2['ip_addr'],4);
       $oct1 = substr($addr['0'],0,1);
        if($oct1 == '0')
         {
          $oct_res1 = substr($addr['0'],1,1);
          if($oct_res1 == '0')
           {
         $oct_result1 = substr($addr['0'],2,3);
           } else {$oct_result1 = substr($addr['0'],1,3);}
         } else {$oct_result1 = $addr['0'];}

       $oct2 = substr($addr['1'],0,1);
        if($oct2 == '0')
         {
          $oct_res2 = substr($addr['1'],1,1);
           if($oct_res2 == '0')
            {
            $oct_result2 = substr($addr['1'],2,3);
            } else {$oct_result2 = substr($addr['1'],1,3);}
         } else {$oct_result2 = $addr['1'];}

       $oct3 = substr($addr['2'],0,1);
        if($oct3 == '0')
         {
         $oct_res3 = substr($addr['2'],1,1);
          if($oct_res3 == '0')
           {
           $oct_result3 = substr($addr['2'],2,3);
           } else {$oct_result3 = substr($addr['2'],1,3);}
         } else {$oct_result3 = $addr['2'];}

        $oct4 = substr($addr['3'],0,1);
         if($oct4 == '0')
          {
          $oct_res4 = substr($addr['3'],1,1);
           if($oct_res4 == '0')
            {
            $oct_result4 = substr($addr['3'],2,3);
            } else {$oct_result4 = substr($addr['3'],1,3);}
          } else {$oct_result4 = $addr['3'];}
        $ip_addres = $oct_result1.".".$oct_result2.".".$oct_result3.".".$oct_result4;

        $online = "<img alt='".$TEXT['addr-vikluchit']."' src='../skins/".$result2['skin']."/online.png' />";
        $offline = "<img alt='".$TEXT['addr-vkluchit']."' src='../skins/".$result2['skin']."/offline.png' />";

        if($result_iskh_srv2['descr'] == "") {$descr = "---";}
         else {$descr = $result_iskh_srv2['descr'];}

        if($result_iskh_srv2['vkl_ip'] == '1') {$strvisible = $online;}
         else {$strvisible = $offline;}

        $hw = substr(chunk_split($result_iskh_srv2['hw'],2,":"),0,-1);
        $hw = "<a href='../logs/index.php?what=".$result_iskh_srv2['hw']."' target=_blank >".$hw."</a>";

    echo "<tr>
           <td>".$ip_addres."</td>
           <td>".$hw."</td>
           <td>".$result_iskh_srv2['name']."</td>
           <td>".$port." (".$type_prt.")</td>
           <td>".$vlan_num." (".$vlan_name.")</td>
           <td>".$descr."</td>
           <td>".$result_iskh_srv2['srv_name']."&nbsp;(".$result_iskh_srv2['srv_descr'].")</td>
           <td>".$strvisible."</td>
           </tr>";
        }
     }
  }
}
echo "</tbody>";
echo "<tfoot>
<tr class='tableheadercat'>
<td colspan='8' align='right'>".$TEXT['log-import2']."(".round($size, 2)." kb), ".$TEXT['log-import3']."(".$numrows.")</td>
</tr>
</tfoot></table>";
require_once "../utils/bottom_adm.php";
?>