<?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-move-tit1'];
  $button = $TEXT['btn_save'];

  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($_POST['ip_id'])) {puterror($result2['skin'],$TEXT['error-addr21']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['ip_id'])>15) {puterror($result2['skin'],$TEXT['error-addr22']); 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();}

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

  $srv_id = $_POST['srv_id'];
  $old_ip_id = $_POST['ip_id'];
  $delimiter = ".";

  $query_iskh = "SELECT * FROM `tbl_ip` WHERE `ip_id`='".$_POST['ip_id']."' ";
  $result_iskh = mysqli_query($dbcnx,$query_iskh);
  if(!$result_iskh) {puterror($result2['skin'],$TEXT['error-tbl2']); require_once "../utils/bottom_adm.php"; exit();}
      $result_iskh2 = mysqli_fetch_array($result_iskh,MYSQLI_ASSOC);

      if($result_iskh2['vkl_ip'] == 1) {$vkl = "checked";}
       else {$vkl = "";}

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

  $query_srv_coun = mysqli_query($dbcnx,"SELECT COUNT(ip_id) AS c_ip_id FROM tbl_ip WHERE vkl_ip = '0' AND srv_id = '".$_POST['srv_id']."'");
   $count_ip = mysqli_fetch_array($query_srv_coun,MYSQLI_ASSOC);
    if($count_ip['c_ip_id'] < 1) {$action = "edit.php";}
     else {$action = "move.php";}

?>
<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_old">
    <?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>
<tr><td class="bold_it_page"><?php echo $TEXT['addr-name']; ?>*</td><td><input size=30 maxlength=255 type=text name=name value='<?php echo $result_iskh2['name']; ?>'></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 $result_iskh2['descr']; ?>'></td></tr>
<tr><td class="bold_it_page"><?php echo $TEXT['addr-ip-addr']; ?>*</td><td><input size=20 maxlength=15 type=text name=ip_addr value='<?php echo $ip_addres; ?>' ></td></tr>
<tr><td class="bold_it_page"><?php echo $TEXT['addr-mac']; ?>*</td><td><input size=20 maxlength=17 type=text name=hw value='<?php echo $result_iskh2['hw']; ?>' ></td></tr>
<tr><td class="bold_it_page"><?php echo $TEXT['srv-vkl']; ?></td><td><input type=checkbox name=vkl_ip <?php echo $vkl; ?>></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><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['addr-move-future']; ?>"><?php echo $TEXT['addr-move-future']; ?></legend>
<table align="right" width="98%" border="0">
    <thead><tr class='tableheadercat'>
            <td><?php echo $TEXT['analiz-msg-text2']; ?></td>
            <td><?php echo $TEXT['addr-name']; ?></td>
            <td><?php echo $TEXT['addr-ip-addr']; ?></td>
            <td><?php echo $TEXT['addr-mac']; ?></td>
            <td><?php echo $TEXT['addr-move-isp']; ?></td>
            </tr></thead><tbody id='tab'>
<?php
// Проверяем есть-ли на указанном сервере старый МАС-адрес
  $query_iskh_hw = "SELECT ip_id, hw, ip_addr, descr, name, vkl_ip FROM tbl_ip WHERE hw = '".$result_iskh2['hw']."' AND srv_id = '".$_POST['srv_id']."' ORDER BY ip_addr ";
  $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)
     {
       while($result_iskh_hw2 = mysqli_fetch_array($result_iskh_hw,MYSQLI_ASSOC))
       {
       $ip_id = $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;
        if($result_iskh_hw2['vkl_ip'] == '0') {$hw_vkl = "<img alt=".$TEXT['srv-vkluchit']." src='../skins/".$result2['skin']."/vikl.png' />";}
         else {$hw_vkl = "<img alt=".$TEXT['srv-vikluchit']." src='../skins/".$result2['skin']."/vkl.png' />";}
?>
<tr>
    <td><?php echo $hw_vkl; ?></td>
    <td class="bold_it" title="<?php echo $result_iskh_hw2['descr']; ?>"><?php echo $result_iskh_hw2['name']; ?></td>
    <td class="bold_it" title="<?php echo $result_iskh_hw2['descr']; ?>"><?php echo $ip_addres2; ?></td>
    <td class="bold_it"><?php echo substr(chunk_split($result_iskh_hw2['hw'],2,":"),0,-1); ?></td>
    <td class="bold_it"><input type="checkbox" name="ip_id2[]" value="<?php echo $ip_id; ?>"></td>
</tr>
<?php
        }
    }
    else
    {
// Если МАС-адрес не попался
  $query_iskh_srv = "SELECT ip_id, hw, ip_addr, descr, name FROM tbl_ip WHERE vkl_ip = '0' AND srv_id = '".$_POST['srv_id']."' ORDER BY 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'];
       $addr2 = explode($delimiter,$result_iskh_srv2['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;
?>
<tr>
    <td class="bold_it"><img alt="<?php echo $TEXT['srv-vkluchit']; ?>" src="../skins/<?php echo $result2['skin']; ?>/vikl.png" /></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_addres2; ?></td>
    <td class="bold_it"><?php echo substr(chunk_split($result_iskh_srv2['hw'],2,":"),0,-1); ?></td>
    <td class="bold_it"><input type="checkbox" name="ip_id2[]" value="<?php echo $ip_id; ?>"></td>
</tr>
<?php
        }
       }
       else
        {
        echo "<tr><td class='bold_it' colspan='5'>".$TEXT['addr-move-no']."</td></tr>";
        ?>
        </tbody>
        </table>
        </fieldset>
          <fieldset>
          <legend align="left" title="<?php echo $TEXT['addr-move-yes']; ?>"><?php echo $TEXT['addr-move-yes']; ?></legend>
          <table align="right" width="98%" border="0">
           <thead><tr class='tableheadercat'>
            <td><?php echo $TEXT['analiz-msg-text2']; ?></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_on = "SELECT hw, ip_addr, descr FROM tbl_ip WHERE vkl_ip = '1' AND srv_id = '".$_POST['srv_id']."' ORDER BY ip_addr ";
        $result_iskh_srv_on = mysqli_query($dbcnx,$query_iskh_srv_on);
         if (!$result_iskh_srv_on) {puterror($result2['skin'],$TEXT['error-tbl2']); require_once "../utils/bottom_adm.php"; exit();}
          if(mysqli_num_rows($result_iskh_srv_on) > 0)
           {
            while($result_iskh_srv2_on = mysqli_fetch_array($result_iskh_srv_on,MYSQLI_ASSOC))
            {

      $addr3 = explode($delimiter,$result_iskh_srv2_on['ip_addr'],4);
       $oct13 = substr($addr3['0'],0,1);
        if($oct13 == '0')
         {
          $oct_res13 = substr($addr3['0'],1,1);
          if($oct_res13 == '0')
           {
         $oct_result13 = substr($addr3['0'],2,3);
           } else {$oct_result13 = substr($addr3['0'],1,3);}
         } else {$oct_result13 = $addr3['0'];}

       $oct23 = substr($addr3['1'],0,1);
        if($oct23 == '0')
         {
          $oct_res23 = substr($addr3['1'],1,1);
           if($oct_res23 == '0') 
            {
            $oct_result23 = substr($addr3['1'],2,3);
            } else {$oct_result23 = substr($addr3['1'],1,3);}
         } else {$oct_result23 = $addr3['1'];}

       $oct33 = substr($addr3['2'],0,1);
        if($oct33 == '0')
         {
         $oct_res33 = substr($addr3['2'],1,1);
          if($oct_res33 == '0')
           {
           $oct_result33 = substr($addr3['2'],2,3);
           } else {$oct_result33 = substr($addr3['2'],1,3);}
         } else {$oct_result33 = $addr3['2'];}

        $oct43 = substr($addr3['3'],0,1);
         if($oct43 == '0')
          {
          $oct_res43 = substr($addr3['3'],1,1);
           if($oct_res43 == '0')
            {
            $oct_result43 = substr($addr3['3'],2,3);
            } else {$oct_result43 = substr($addr3['3'],1,3);}
          } else {$oct_result43 = $addr3['3'];}
        $ip_addres3 = $oct_result13.".".$oct_result23.".".$oct_result33.".".$oct_result43;
            ?>
            <tr>
              <td class="bold_it"><img alt="<?php echo $TEXT['srv-vikluchit']; ?>" src="../skins/<?php echo $result2['skin']; ?>/vkl.png" /></td>
              <td class="bold_it" title="<?php echo $result_iskh_srv2_on['descr']; ?>"><?php echo $ip_addres3; ?></td>
              <td class="bold_it"><?php echo substr(chunk_split($result_iskh_srv2_on['hw'],2,":"),0,-1); ?></td>
            </tr>
          <?php
           }
        }

      }
/////////////
    }
  ////
?>
</tbody>
</table>
</fieldset>
</td></tr>
<tr><td>
   <input type=hidden name=ip_id value='<?php echo $old_ip_id; ?>' />
   <input type=hidden name=srv_id value='<?php echo $srv_id; ?>' />
</td><td></td>
</tr>
</table>
</form>
<?php
  require_once "../utils/bottom_adm.php";
?>