<?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['addr-title-add'];

  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();}

  if(empty($_GET['pods_id'])) {puterror($result2['skin'],$TEXT['error-podset5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_GET['pods_id'])>5) {puterror($result2['skin'],$TEXT['error-podset6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_GET['a'])>5) {puterror($result2['skin'],$TEXT['error-srv15']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_GET['etazh_id'])>5) {puterror($result2['skin'],$TEXT['error-etazh6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_GET['house_id'])>5) {puterror($result2['skin'],$TEXT['error-house6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_GET['obor_id'])>5) {puterror($result2['skin'],$TEXT['error-oborud6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_GET['srv_ch'])>5) {puterror($result2['skin'],$TEXT['error-srv15']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_GET['house_ch'])>5) {puterror($result2['skin'],$TEXT['error-srv15']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_GET['etazh_ch'])>5) {puterror($result2['skin'],$TEXT['error-srv15']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_GET['obor_ch'])>5) {puterror($result2['skin'],$TEXT['error-srv15']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_GET['podset'])>15) {puterror($result2['skin'],$TEXT['error-podset2']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_GET['addr3'])>3) {puterror($result2['skin'],$TEXT['error-addr24']); require_once "../utils/bottom_adm.php"; exit();}

  $_GET['pods_id'] = rep($_GET['pods_id']);
  $_GET['a'] = rep($_GET['a']);
  $_GET['etazh_id'] = rep($_GET['etazh_id']);
  $_GET['house_id'] = rep($_GET['house_id']);
  $_GET['obor_id'] = rep($_GET['obor_id']);
  $_GET['podset'] = rep($_GET['podset']);
  $_GET['addr3'] = rep($_GET['addr3']);

  if($_GET['a'] == "3") {$ckl_ch_on = "checked";}
   else {$ckl_ch_on = "";}

  $delimiter = ".";
  $action = "add_addr.php";
  $button = $TEXT['btn_save'];
  $a = $_GET['a'];

  $query_iskh = "SELECT `podset`, `srv_id`, `prefix` FROM `tbl_podset` WHERE `pods_id`='".$_GET['pods_id']."' ";
  $result_iskh = mysqli_query($dbcnx,$query_iskh);
  if(!$result_iskh) {puterror($result2['skin'],$TEXT['error-tbl13']); require_once "../utils/bottom_adm.php"; exit();}
      $result_iskh2 = mysqli_fetch_array($result_iskh,MYSQLI_ASSOC);

      $addr = explode($delimiter,$result_iskh2['podset'],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_addres1 = $oct_result1.".".$oct_result2.".".$oct_result3.".".$oct_result4;

// Проверяем есть-ли на указанном сервере отключенные адреса
  $query_iskh_hw = "SELECT ip_id, ip_addr FROM tbl_ip WHERE ip_addr REGEXP '".$result_iskh2['podset']."' AND srv_id = '".$result_iskh2['srv_id']."' AND `vkl_ip` = '0' ORDER BY ip_addr LIMIT 1";
  $result_iskh_hw = mysqli_query($dbcnx,$query_iskh_hw);
   if(!$result_iskh_hw) {puterror($result2['skin'],$TEXT['error-tbl2']); require_once "../utils/bottom_adm.php"; exit();}
    if(mysqli_num_rows($result_iskh_hw) > 0)
     {
       $result_iskh_hw2 = mysqli_fetch_array($result_iskh_hw,MYSQLI_ASSOC);

       $ip_id_osn = $result_iskh_hw2['ip_id'];
       $addr2 = explode($delimiter,$result_iskh_hw2['ip_addr'],4);
       $oct12 = substr($addr2['0'],0,1);
        if($oct12 == '0')
         {
          $oct_res12 = substr($addr2['0'],1,1);
          if($oct_res12 == '0')
           {
         $oct_result12 = substr($addr2['0'],2,3);
           } else {$oct_result12 = substr($addr2['0'],1,3);}
         } else {$oct_result12 = $addr2['0'];}

       $oct22 = substr($addr2['1'],0,1);
        if($oct22 == '0')
         {
          $oct_res22 = substr($addr2['1'],1,1);
           if($oct_res22 == '0')
            {
            $oct_result22 = substr($addr2['1'],2,3);
            } else {$oct_result22 = substr($addr2['1'],1,3);}
         } else {$oct_result22 = $addr2['1'];}

       $oct32 = substr($addr2['2'],0,1);
        if($oct32 == '0')
         {
         $oct_res32 = substr($addr2['2'],1,1);
          if($oct_res32 == '0')
           {
           $oct_result32 = substr($addr2['2'],2,3);
           } else {$oct_result32 = substr($addr2['2'],1,3);}
         } else {$oct_result32 = $addr2['2'];}

        $oct42 = substr($addr2['3'],0,1);
         if($oct42 == '0')
          {
          $oct_res42 = substr($addr2['3'],1,1);
           if($oct_res42 == '0')
            {
            $oct_result42 = substr($addr2['3'],2,3);
            } else {$oct_result42 = substr($addr2['3'],1,3);}
          } else {$oct_result42 = $addr2['3'];}
        $ip_addres2 = $oct_result12.".".$oct_result22.".".$oct_result32.".".$oct_result42;
        $ip_addres = $ip_addres2;
     }
     else 
     {
      $s = 10;
      $x = 0;
      for($s=10; $s<255; $s++)
      {
       $p_addr4 = strlen($s);
          if($p_addr4 == 1) {$p_addr4_result = "00".$s;}
           elseif($p_addr4 == 2) {$p_addr4_result = "0".$s;}
            else {$p_addr4_result = $s;}
       $srch_addr = $result_iskh2['podset'].$p_addr4_result;

     //Проверяем есть-ли в заданном диапазоне дырки
     $query_no_addr = "SELECT ip_addr FROM tbl_ip WHERE ip_addr = '".$srch_addr."' LIMIT 1";
     $result_no_addr = mysqli_query($dbcnx,$query_no_addr);
      if(!$result_no_addr) {puterror($result2['skin'],$TEXT['error-tbl2']); require_once "../utils/bottom_adm.php"; exit();}
        if(mysqli_num_rows($result_no_addr) == 0)
         {
          $x = 1;
          $no_addr2 = explode($delimiter,$srch_addr,4);
          $oct_no_addr12 = substr($no_addr2['0'],0,1);
          if($oct_no_addr12 == '0')
          {
          $oct_res_no_addr12 = substr($no_addr2['0'],1,1);
           if($oct_res_no_addr12 == '0')
            {
          $oct_result_no_addr12 = substr($no_addr2['0'],2,3);
            } else {$oct_result_no_addr12 = substr($no_addr2['0'],1,3);}
           } else {$oct_result_no_addr12 = $no_addr2['0'];}

         $oct_no_addr22 = substr($no_addr2['1'],0,1);
          if($oct_no_addr22 == '0')
          {
          $oct_res_no_addr22 = substr($no_addr2['1'],1,1);
           if($oct_res_no_addr22 == '0')
            {
            $oct_result_no_addr22 = substr($no_addr2['1'],2,3);
            } else {$oct_result_no_addr22 = substr($no_addr2['1'],1,3);}
           } else {$oct_result_no_addr22 = $no_addr2['1'];}

         $oct_no_addr32 = substr($no_addr2['2'],0,1);
          if($oct_no_addr32 == '0')
          {
          $oct_res_no_addr32 = substr($no_addr2['2'],1,1);
           if($oct_res_no_addr32 == '0')
           {
           $oct_result_no_addr32 = substr($no_addr2['2'],2,3);
           } else {$oct_result_no_addr32 = substr($no_addr2['2'],1,3);}
          } else {$oct_result_no_addr32 = $no_addr2['2'];}

          $oct_no_addr42 = substr($no_addr2['3'],0,1);
           if($oct_no_addr42 == '0')
           {
           $oct_res_no_addr42 = substr($no_addr2['3'],1,1);
            if($oct_res_no_addr42 == '0')
            {
            $oct_result_no_addr42 = substr($no_addr2['3'],2,3);
            } else {$oct_result_no_addr42 = substr($no_addr2['3'],1,3);}
           } else {$oct_result_no_addr42 = $no_addr2['3'];}
        $ip_addres3 = $oct_result_no_addr12.".".$oct_result_no_addr22.".".$oct_result_no_addr32.".".$oct_result_no_addr42;
        $ip_addres = $ip_addres3;
        if($x == 1) {break;}
         }
      $ip_addres = $ip_addres1;
      }
    }

?>
<form enctype='multipart/form-data' action=<?php echo $action; ?> method=post>
<table border="0" width="100%" align="center">
<tr>
<td width="50%" valign="top">
<fieldset>
<legend align="left" title="<?php echo $TEXT['addr-move-now']; ?>"><?php echo $TEXT['addr-move-now']; ?></legend>
<table width="98%">
<tr><td class="bold_it_page"><?php echo $TEXT['srv-name']; ?></td>
    <td><select name="srv_id">
    <?php
        $query1 = "SELECT srv_id, name, descr, autorit FROM `tbl_srv` WHERE vkl = '1' ORDER BY name";
        $srv_tmp = mysqli_query($dbcnx,$query1);
          if(!$srv_tmp) {puterror($result2['skin'],$TEXT['error-tbl1']); require_once "../utils/bottom_adm.php"; exit();}
              while($srv = mysqli_fetch_array($srv_tmp,MYSQLI_ASSOC))
                {
                 if($srv['srv_id'] == $result_iskh2['srv_id'])
                   {
                   if($srv['descr'] == '')
                    {
                     $chk = "<option selected value=".$srv['srv_id'].">".$srv['name'];
                    } else {$chk = "<option selected value=".$srv['srv_id'].">".$srv['name']."(".$srv['descr'].")";}
                   }
                  else
                   {
                     if($srv['descr'] == '')
                    {
                     $chk = "<option value=".$srv['srv_id'].">".$srv['name'];
                    } else {$chk = "<option value=".$srv['srv_id'].">".$srv['name']."(".$srv['descr'].")";}
                   }
                echo $chk;
                }
    ?>
    </select></td></tr>
<?php
if($ip_addres3 !== "") {echo "<tr><td class='bold_it_page'>".$TEXT['addr-name']."*</td><td><input size=30 maxlength=255 type=text name=name value='".$result_iskh2['prefix'].$oct_result3.$s."'></td></tr>";}
 else {echo "<tr><td class='bold_it_page'>".$TEXT['addr-name']."*</td><td><input size=30 maxlength=255 type=text name=name value='".$result_iskh2['prefix'].$oct_result3.$_GET['addr3']."'></td></tr>";}
?>
<tr><td class="bold_it_page"><?php echo $TEXT['addr-descr']; ?></td><td><input size=30 maxlength=255 type=text name=descr value='<?php echo $_GET['descr']; ?>'></td></tr>
<?php
if($_GET['addr3'] > 255) {echo "<tr><td class='bold_it_page'>".$TEXT['addr-ip-addr']."*</td><td><input size=20 maxlength=15 type=text name=ip_addr value='".$ip_addres."' ></td></tr>";}
 elseif($ip_addres3 !== "") {echo "<tr><td class='bold_it_page'>".$TEXT['addr-ip-addr']."*</td><td><input size=20 maxlength=15 type=text name=ip_addr value='".$ip_addres."' ></td></tr>";}
  else {echo "<tr><td class='bold_it_page'>".$TEXT['addr-ip-addr']."*</td><td><input size=20 maxlength=15 type=text name=ip_addr value='".$ip_addres.$_GET['addr3']."' ></td></tr>";}
?>
<tr><td class="bold_it_page"><?php echo $TEXT['addr-mac']; ?>*</td><td><input size=20 maxlength=17 type=text name=hw ></td></tr>
<tr><td class="bold_it_page"><?php echo $TEXT['srv-vkl']; ?></td><td><input type=checkbox name=vkl_ip checked></td></tr>
<tr><td class="bold_it_page"><?php echo $TEXT['addr-to-sams']; ?></td><td><input type=checkbox name=sams_ch ></td></tr>
<tr><td class="bold_it_page"><?php echo $TEXT['addr-to-cicle']; ?></td><td><input type=checkbox name=cicle_ch <?php echo $ckl_ch_on; ?>></td></tr>
<tr><td><input class="button" type=submit value='<?php echo $button; ?>'></td><td></td></tr>
</table>
</fieldset>
</td>
<td width="50%">
<fieldset>
<legend align="left" title="<?php echo $TEXT['grp-addr-vibtit']; ?>"><?php echo $TEXT['grp-addr-vibtit']; ?></legend>
<table align="right" width="98%" border="0">
    <thead><tr class='tableheadercat'>
            <td><?php echo $TEXT['addr-move-isp']; ?></td>
            <td><?php echo $TEXT['addr-name']; ?></td>
            <td><?php echo $TEXT['addr-ip-addr']; ?></td>
            <td><?php echo $TEXT['addr-mac']; ?></td>
            </tr></thead><tbody id='tab'>
<?php
// Выводим список существующих в заданном диапазоне
  $query_iskh_srv = "SELECT ip_id, hw, ip_addr, descr, name, vkl_ip FROM tbl_ip WHERE ip_addr REGEXP '".$result_iskh2['podset']."' AND srv_id = '".$result_iskh2['srv_id']."' ORDER BY vkl_ip, ip_addr ";
  $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();}
    if(mysqli_num_rows($result_iskh_srv) > 0)
     {
       while($result_iskh_srv2 = mysqli_fetch_array($result_iskh_srv,MYSQLI_ASSOC))
       {
       $ip_id = $result_iskh_srv2['ip_id'];
       $addr3 = explode($delimiter,$result_iskh_srv2['ip_addr'],4);
       $oct52 = substr($addr3['0'],0,1);
        if($oct52 == '0')
         {
          $oct_res52 = substr($addr3['0'],1,1);
          if($oct_res52 == '0')
           {
         $oct_result52 = substr($addr3['0'],2,3);
           } else {$oct_result52 = substr($addr3['0'],1,3);}
         } else {$oct_result52 = $addr3['0'];}

       $oct62 = substr($addr3['1'],0,1);
        if($oct62 == '0')
         {
          $oct_res62 = substr($addr3['1'],1,1);
           if($oct_res62 == '0')
            {
            $oct_result62 = substr($addr3['1'],2,3);
            } else {$oct_result62 = substr($addr3['1'],1,3);}
         } else {$oct_result62 = $addr3['1'];}

       $oct72 = substr($addr3['2'],0,1);
        if($oct72 == '0')
         {
         $oct_res72 = substr($addr3['2'],1,1);
          if($oct_res72 == '0')
           {
           $oct_result72 = substr($addr3['2'],2,3);
           } else {$oct_result72 = substr($addr3['2'],1,3);}
         } else {$oct_result72 = $addr3['2'];}

        $oct82 = substr($addr3['3'],0,1);
         if($oct82 == '0')
          {
          $oct_res82 = substr($addr3['3'],1,1);
           if($oct_res82 == '0')
            {
            $oct_result82 = substr($addr3['3'],2,3);
            } else {$oct_result82 = substr($addr3['3'],1,3);}
          } else {$oct_result82 = $addr3['3'];}
        $ip_addres3 = $oct_result52.".".$oct_result62.".".$oct_result72.".".$oct_result82;
        if($result_iskh_srv2['vkl_ip'] == '0') {$srv2_vkl = "<img alt=".$TEXT['srv-vkluchit']." src='../skins/".$result2['skin']."/vikl.png' />";}
         else {$srv2_vkl = "<img alt=".$TEXT['srv-vikluchit']." src='../skins/".$result2['skin']."/vkl.png' />";}

        if($ip_id_osn == $ip_id)
        {
 ?>
<tr>
    <td><?php echo $srv2_vkl; ?></td>
    <td class="bold_it" title="<?php echo $result_iskh_srv2['descr']; ?>"><?php echo $result_iskh_srv2['name']; ?></td>
    <td class="bold_it" title="<?php echo $result_iskh_srv2['descr']; ?>"><?php echo $ip_addres3; ?></td>
    <td class="bold_it"><?php echo substr(chunk_split($result_iskh_srv2['hw'],2,":"),0,-1); ?></td>
</tr>
<?php
        }
 else
     {
 ?>
<tr>
    <td><?php echo $srv2_vkl; ?></td>
    <td class="tabledata" title="<?php echo $result_iskh_srv2['descr']; ?>"><?php echo $result_iskh_srv2['name']; ?></td>
    <td class="tabledata" title="<?php echo $result_iskh_srv2['descr']; ?>"><?php echo $ip_addres3; ?></td>
    <td class="tabledata"><?php echo substr(chunk_split($result_iskh_srv2['hw'],2,":"),0,-1); ?></td>
</tr>
<?php
     }
   }
 }
?>
</tbody>
</table>
</fieldset>
<input type="hidden" name="a" value="<?php echo $a; ?>" />
<input type="hidden" name="etazh_id" value="<?php echo $_GET['etazh_id']; ?>" />
<input type="hidden" name="house_id" value="<?php echo $_GET['house_id']; ?>" />
<input type="hidden" name="obor_id" value="<?php echo $_GET['obor_id']; ?>" />
<input type="hidden" name="house_ch" value="<?php echo $_GET['house_ch']; ?>" />
<input type="hidden" name="etazh_ch" value="<?php echo $_GET['etazh_ch']; ?>" />
<input type="hidden" name="obor_ch" value="<?php echo $_GET['obor_ch']; ?>" />
<input type="hidden" name="podset" value="<?php echo $_GET['podset']; ?>" />
<input type="hidden" name="pods_id" value="<?php echo $_GET['pods_id']; ?>" />
</td></tr>
</table>
</form>
<?php
  require_once "../utils/bottom_adm.php";
?>