<?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['menu-btn27-title'];

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

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

if(empty($_POST['srv_id'])) {$srv_id = $_GET['srv_id'];}
 else {$srv_id = $_POST['srv_id'];}

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

$srv_id = rep($srv_id);
$_POST['srv_ch'] = rep($_POST['srv_ch']);
$_POST['srv_fltr'] = rep($_POST['srv_fltr']);
$delimiter = ".";

if(empty($srv_id)) {$srv = "";}
 else {
     if($_POST['srv_ch'] == "on") {
       if($_POST['srv_fltr'] == "on") {
           $srv = " tbl_ip.srv_id = '".$srv_id."' AND ";
           $srv2 ="";}
        else {
            $srv ="";
            $srv2 ="";}
     }
      else {$srv = " tbl_ip.srv_id = '".$srv_id."' AND ";
           if($_POST['srv_fltr'] == "on") {$srv2 ="";}
           else {$srv2 =" AND a.srv_id=tbl_ip.srv_id";}
      }
 }

if($_POST['srv_ch'] == "on") {$srv_old = "";}
      else {$srv_old = $srv_id;}

echo "<table align='center' width='80%'>
      <thead><tr class='tableheadercat'>
      <td>".$TEXT['srv-vkl-tit']."</td>
      <td>".$TEXT['analiz-msg-text2']."</td>
      <td>".$TEXT['addr-del']."</td>
      <td>".$TEXT['addr-last-ping']."</td>
      <td>".$TEXT['addr-ip-addr']."</td>
      <td>".$TEXT['addr-mac']."</td>
      <td>".$TEXT['addr-descr']."</td>
      <td>".$TEXT['srv-name']."</td>
      </tr></thead><tbody id='tab'>";

$query_ip = "SELECT
tbl_ip.*,
YEAR(tbl_ip.last_acc) AS lst_year,
MONTH(tbl_ip.last_acc) AS lst_month,
DAY(tbl_ip.last_acc) AS lst_day,
tbl_srv.name AS srv_name,
tbl_srv.descr AS srv_descr,
(SELECT COUNT(hw) FROM tbl_ip a WHERE a.hw=tbl_ip.hw $srv2) AS maccount
FROM tbl_ip
JOIN tbl_srv ON tbl_srv.srv_id=tbl_ip.srv_id 
WHERE $srv
(SELECT COUNT(hw) FROM tbl_ip a WHERE a.hw=tbl_ip.hw $srv2) > 1
ORDER BY tbl_ip.hw, tbl_ip.ip_addr ";
 $res_ip = mysqli_query($dbcnx,$query_ip);
  if(!$res_ip) {puterror($result2['skin'],$TEXT['error-tbl2']); require_once "../utils/bottom_adm.php"; exit();}
   while($result_ip = mysqli_fetch_array($res_ip,MYSQLI_ASSOC))
    {
     if($result_ip['maccount'] > 1)
      {
        $online = "<img alt='online' src='../skins/".$result2['skin']."/online.png' />";
        $offline = "<img alt='offline' src='../skins/".$result2['skin']."/offline.png' />";
        $warn = "<img alt='wait' src='../skins/".$result2['skin']."/wait.png' />";
        $nowarn = "<img alt='active' src='../skins/".$result2['skin']."/active.png' />";
        $delete = "<a href=double_del.php?ip_id=".$result_ip['ip_id']."&amp;srv_id=".$srv_old." title='".$TEXT['delete-tit']."'><img alt='Удалить' src='../skins/".$result2['skin']."/delete.png' /></a>";

        if($result_ip['state_addr'] == '1') {$warnin = $warn;}
         else {$warnin = $nowarn;}

        if($result_ip['vkl_ip'] == '1') {$vkl = $online;}
         else 
         {
         $vkl = $offline;
         $warnin = $offline;
         }

        if($result_ip['vkl_ip'] == '0') {$strvisible = "<a href=on.php?ip_id=".$result_ip['ip_id']."&amp;srv_id=".$srv_old." title='".$TEXT['srv-vkluchit']."'>".$vkl."</a>";}
         else {$strvisible = "<a href=off.php?ip_id=".$result_ip['ip_id']."&amp;srv_id=".$srv_old." title='".$TEXT['srv-vikluchit']."'>".$vkl."</a>";}

      $addr = explode($delimiter,$result_ip['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;
        $srv_n = $result_ip['srv_name']." (".$result_ip['srv_descr'].")";

          $month["1"] = $TEXT['month01'];
          $month["2"] = $TEXT['month02'];
          $month["3"] = $TEXT['month03'];
          $month["4"] = $TEXT['month04'];
          $month["5"] = $TEXT['month05'];
          $month["6"] = $TEXT['month06'];
          $month["7"] = $TEXT['month07'];
          $month["8"] = $TEXT['month08'];
          $month["9"] = $TEXT['month09'];
          $month["01"] = $TEXT['month01'];
          $month["02"] = $TEXT['month02'];
          $month["03"] = $TEXT['month03'];
          $month["04"] = $TEXT['month04'];
          $month["05"] = $TEXT['month05'];
          $month["06"] = $TEXT['month06'];
          $month["07"] = $TEXT['month07'];
          $month["08"] = $TEXT['month08'];
          $month["09"] = $TEXT['month09'];
          $month["10"] = $TEXT['month10'];
          $month["11"] = $TEXT['month11'];
          $month["12"] = $TEXT['month12'];

          $date_day = $result_ip['lst_day'];
          $date_month = $result_ip['lst_month'];
          $date_year = $result_ip['lst_year'];
          $year = $TEXT['year'];

          if($result_ip['lst_year'] == "" || $result_ip['lst_year'] == "0" || $result_ip['lst_year'] == "1970") {$data_r = "---";}
           else {$data_r = $date_day." ".$month[$date_month]." ".$date_year." ".$year;}

          if(trim($result_ip['descr']) == "") {$descr_ip = "---";}
           else {$descr_ip = $result_ip['descr'];}
       ?>
       <tr>
        <td><?php echo $strvisible; ?> <?php echo $result_ip['maccount']; ?></td>
        <td class="bold_it" align="center"><?php echo $warnin; ?></td>
        <td class="bold_it" align="center"><?php echo $delete; ?></td>
        <td class="bold_it" ><?php echo $data_r; ?></td>
        <td class="bold_it" ><?php echo $ip_addres; ?></td>
        <td class="bold_it" ><?php echo substr(chunk_split($result_ip['hw'],2,":"),0,-1); ?></td>
        <td class="bold_it" ><?php echo $descr_ip; ?></td>
        <td class="bold_it" ><?php echo $srv_n; ?></td>
       </tr>
       <?php
      }
    }
    echo "<tr><td class='bold_it' colspan='8'>".$TEXT['error-grp6']."</td></tr>";
    echo "</tbody></table>";
require_once "../utils/bottom_adm.php";
?>