<?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['grp-addr-vibtit'];

  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.php"; exit();}

  $button = $TEXT['btn_run'];
  $action = "mover.php";
if(strlen($_POST['srv_id'])>5) {puterror($result2['skin'],$TEXT['error-srv22']); 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_id2'])>5) {puterror($result2['skin'],$TEXT['error-srv22']); require_once "../utils/bottom_adm.php"; exit();}
if(empty($_POST['srv_id2'])) {puterror($result2['skin'],$TEXT['error-srv21']); require_once "../utils/bottom_adm.php"; exit();}
if(strlen($_POST['addr'])>15) {puterror($result2['skin'],$TEXT['error-addr2']); require_once "../utils/bottom_adm.php"; exit();}
if(strlen($_POST['name'])>255) {puterror($result2['skin'],$TEXT['error-addr2']); require_once "../utils/bottom_adm.php"; exit();}
if(strlen($_POST['descr'])>255) {puterror($result2['skin'],$TEXT['error-addr3']); require_once "../utils/bottom_adm.php"; exit();}
if(strlen($_POST['vkl_ip'])>5) {puterror($result2['skin'],$TEXT['error-srv15']); require_once "../utils/bottom_adm.php"; exit();}
if(strlen($_POST['vikl_ip'])>5) {puterror($result2['skin'],$TEXT['error-srv15']); require_once "../utils/bottom_adm.php"; exit();}
if($_POST['srv_id'] == $_POST['srv_id2']) {puterror($result2['skin'],$TEXT['grp-srv-error1']); require_once "../utils/bottom_adm.php"; exit();}

$_POST['srv_id'] = rep($_POST['srv_id']);
$_POST['srv_id2'] = rep($_POST['srv_id2']);
$_POST['addr'] = rep($_POST['addr']);
$_POST['name'] = rep($_POST['name']);
$_POST['descr'] = rep($_POST['descr']);
$_POST['vkl_ip'] = rep($_POST['vkl_ip']);
$_POST['vikl_ip'] = rep($_POST['vikl_ip']);

$srv_id = $_POST['srv_id'];
$srv_id2 = $_POST['srv_id2'];
$delimiter =".";
$ipmass = Array(",","ю","Ю","..","...");
$_POST['addr'] = trim(str_replace($ipmass,".",$_POST['addr']));

if(!empty($_POST['addr']))
{
$addr = explode($delimiter,$_POST['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'];}
 $sour = $oct_res1.".".$oct_res2.".".$oct_res3.".".$oct_res4;
 $addr = " AND tbl_ip.ip_addr REGEXP '".$sour."' ";
 $log_addr = $_POST['addr'].", ";
 }else {$addr = "";}

 if(!empty($_POST['name']))
 {
 $name = " AND LOWER(tbl_ip.name) LIKE LOWER('%".$_POST['name']."%') ";
 $log_name = $_POST['name'].", ";
 } else {$name = "";}

 if(!empty($_POST['descr']))
 {
 $descr = " AND LOWER(tbl_ip.descr) LIKE LOWER('%".$_POST['descr']."%') ";
 $log_descr = $_POST['descr'].", ";
 } else {$descr = "";}

if($_POST['vkl_ip'] == "on" && $_POST['vikl_ip'] == "on" || $_POST['vkl_ip'] !== "on" && $_POST['vikl_ip'] !== "on") {$vkl = "";}
 elseif($_POST['vkl_ip'] == "on" && $_POST['vikl_ip'] !== "on" ) {$vkl = " AND tbl_ip.vkl_ip = '1'";}
  elseif($_POST['vkl_ip'] !== "on" && $_POST['vikl_ip'] == "on" ) {$vkl = " AND tbl_ip.vkl_ip = '0'";}

?>
<form enctype='multipart/form-data' action=<?php echo $action; ?> method=post >
<table align="center" class="table" border="0" cellpadding="0" cellspacing="0" width="80%">
<tr>
 <td class="upd"><?php echo $TEXT['global-warn2']; ?></td>
</tr>
</table>
<br />
<table align="center" border="0" cellpadding="3" cellspacing="0" width="70%">
<?php
echo "<thead><tr class='tableheadercat'>
            <td>".$TEXT['srv-vkl-tit']."</td>
            <td>".$TEXT['addr-ip-addr']."</td>
            <td>".$TEXT['addr-mac']."</td>
            <td>".$TEXT['addr-name']."</td>
            <td>".$TEXT['addr-descr']."</td>
            <td>".$TEXT['srv-name']."</td>
            <td>".$TEXT['grp-addr-vibran']."</td>
            </tr></thead><tbody id='tab'>";

     $query_body = "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.srv_id = '".$_POST['srv_id']."' ".$addr." ".$name." ".$descr." ".$srv." ".$vkl."
     ORDER BY tbl_srv.name, tbl_ip.ip_addr";
     $result_body = mysqli_query($dbcnx,$query_body);
     if(!$result_body) {puterror($result2['skin'],mysqli_error($dbcnx)); require_once "../utils/bottom_adm.php"; exit();}
       $result_rows2 = mysqli_num_rows($result_body);
       if($result_rows2 > 0)
         {
           while($result_body_addr = mysqli_fetch_array($result_body,MYSQLI_ASSOC))
               {
               $ip_id = $result_body_addr['ip_id'];

              $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_body_addr['descr'] == "") {$descr = "---";}
               else {$descr = $result_body_addr['descr'];}

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

              $addr = explode($delimiter,$result_body_addr['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;

              if(trim($result_body_addr['srv_descr'] !== ""))
              {
           ?>
           <tr>
              <td><?php echo $vkl; ?></td>
              <td><?php echo $ip_addres; ?></td>
              <td><?php echo substr(chunk_split($result_body_addr['hw'],2,":"),0,-1); ?></td>
              <td><?php echo $result_body_addr['name']; ?></td>
              <td><?php echo $descr; ?></td>
              <td><?php echo $result_body_addr['srv_name']; ?>&nbsp;(<?php echo $result_body_addr['srv_descr']; ?>)</td>
              <td><input type="checkbox" name="ip_id[]" value="<?php echo $ip_id; ?>" checked></td>
           </tr>
           <?php
              }
              else
              {
           ?>
           <tr>
              <td><?php echo $vkl; ?></td>
              <td><?php echo $ip_addres; ?></td>
              <td><?php echo $result_body_addr['hw']; ?></td>
              <td><?php echo $result_body_addr['name']; ?> <?php echo $ip_id; ?></td>
              <td><?php echo $descr; ?></td>
              <td><?php echo $result_body_addr['srv_name']; ?></td>
              <td><input type="checkbox" name="ip_id[]" value="<?php echo $ip_id; ?>" checked></td>
           </tr>
           <?php
               }
              }
         }
          else
         {
         ?>
         <tr><td colspan="10" class="bold_it"><?php echo $TEXT['search-err4']; ?></td></tr>
         <?php
         }        
?>
</tbody>
<tfoot>
<tr class='tableheadercat'>
<td colspan="10" align="right"><?php echo $TEXT['addr-found']; ?><?php echo $result_rows2; ?><?php echo $TEXT['addr-found-obg']; ?></td>
</tr>
</tfoot>
</table>
<br />
<table align="center" class="table" border="0" cellpadding="0" cellspacing="0" width="70%">
<tr>
<td colspan="10" align="left">
    <input type=hidden name=srv_id value='<?php echo $srv_id; ?>' />
    <input type=hidden name=srv_id2 value='<?php echo $srv_id2; ?>' />
    <input class="button" type=submit value='<?php echo $button; ?>' />
</td>
</tr>
</table>
</form>
<?php
  require_once "../utils/bottom_adm.php";
?>