<?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-tit2'];
  $delimiter=".";

  $button = $TEXT['btn_filter'];
  $action = "moveform2.php";

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

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

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

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

if($result2['administrator'] == 0 && $result2['user'] == 0) {puterror($result2['skin'],$TEXT['error-loginform2']); 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();}
if(strlen($_POST['podset'])>15) {puterror($result2['skin'],$TEXT['error-podset2']); require_once "../utils/bottom_adm.php"; exit();}
if(strlen($_POST['etazh_id'])>5) {puterror($result2['skin'],$TEXT['error-etazh6']); require_once "../utils/bottom_adm.php"; exit();}
if(strlen($_POST['house_id'])>5) {puterror($result2['skin'],$TEXT['error-house6']); require_once "../utils/bottom_adm.php"; exit();}
if(strlen($_POST['obor_id'])>5) {puterror($result2['skin'],$TEXT['error-oborud6']); require_once "../utils/bottom_adm.php"; exit();}
if(strlen($_POST['srv_ch'])>5) {puterror($result2['skin'],$TEXT['error-srv15']); require_once "../utils/bottom_adm.php"; exit();}
if(strlen($_POST['house_ch'])>5) {puterror($result2['skin'],$TEXT['error-srv15']); require_once "../utils/bottom_adm.php"; exit();}
if(strlen($_POST['etazh_ch'])>5) {puterror($result2['skin'],$TEXT['error-srv15']); require_once "../utils/bottom_adm.php"; exit();}
if(strlen($_POST['obor_ch'])>5) {puterror($result2['skin'],$TEXT['error-srv15']); require_once "../utils/bottom_adm.php"; exit();}
if(strlen($_POST['ip_id'])>255) {puterror($result2['skin'],$TEXT['error-addr22']); require_once "../utils/bottom_adm.php"; exit();}

$_POST['srv_ch'] = rep($_POST['srv_ch']);
$_POST['srv_id'] = rep($_POST['srv_id']);
$_POST['etazh_id'] = rep($_POST['etazh_id']);
$_POST['house_id'] = rep($_POST['house_id']);
$_POST['obor_id'] = rep($_POST['obor_id']);
$_POST['podset'] = rep($_POST['podset']);
$_POST['ip_id'] = rep($_POST['ip_id']);

$ipmass = Array(",","ю","Ю");
$_POST['podset'] = trim(str_replace($ipmass,".",$_POST['podset']));

 if(!empty($_POST['podset']))
  {
  $podset=$_POST['podset'];
  $addr = explode($delimiter,$podset,4);
  $oct1 = strlen($addr['0']);
   if($oct1 == 1) {$oct_res1 = "00".$addr['0'];}
    elseif($oct1 == 2) {$oct_res1 = "0".$addr['0'];}
     else
      {
       if($addr['0'] == "") {$oct_res1 = "";}
        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
      {
       if($addr['1'] == "") {$oct_res2 = "";}
        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
      {
       if($addr['2'] == "") {$oct_res3 = "";}
        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
      {
       if($addr['3'] == "") {$oct_res4 = "";}
        else {$oct_res4 = ".".$addr['3'];}
      }
   $podset2 = $oct_res1.$oct_res2.$oct_res3.$oct_res4;
   $podset = " AND tbl_podset.podset REGEXP '".$podset2."' ";
  } else {$podset = "";}

  if($_POST['srv_ch'] == "on")
   {
    $srv_id=" tbl_podset.srv_id = '".$_POST['srv_id']."' ";
    $srv_ch="checked";
   }
   else
   {
    $srv_id=" tbl_podset.pods_id !='0' ";
    $srv_ch="";
   }

  if($_POST['house_ch'] == "on")
   {
    $house_id=" AND tbl_podset.house_id = '".$_POST['house_id']."' ";
    $house_ch="checked";
   }
   else
   {
    $house_id="";
    $house_ch="";
   }

  if($_POST['etazh_ch'] == "on")
   {
    $etazh_id=" AND tbl_podset.etazh_id = '".$_POST['etazh_id']."' ";
    $etazh_ch="checked";
   }
   else
   {
    $etazh_id="";
    $etazh_ch="";
   }

  if($_POST['obor_ch'] == "on")
   {
    $obor_id=" AND tbl_podset.obor_id = '".$_POST['obor_id']."' ";
    $obor_ch="checked";
   }
   else
   {
    $obor_id="";
    $obor_ch="";
   }

?>
<form enctype='multipart/form-data' action=<?php echo $action; ?> method=post >
<?php
if($result2['administrator'] == 1) {echo "<table align='center' class='table' border='0' cellpadding='0' cellspacing='0' width='70%'>";}
 else {echo "<table align='center' class='table' border='0' cellpadding='0' cellspacing='0' width='60%'>";}
?>
<tr><td>
<fieldset>
<legend align="left" title="<?php echo $TEXT['log-tit7']; ?>"><?php echo $TEXT['btn_filter']; ?></legend>
<table class="table" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr class="button2">
    <?php 
    if($result2['administrator'] == 1)
    {
    echo "<td>".$TEXT['srv-name']."<input type=checkbox name=srv_ch ".$srv_ch." /></td>";
    }
    ?>
    <td><?php echo $TEXT['tbl-house']; ?><input type=checkbox name=house_ch <?php echo $house_ch; ?>></td>
    <td><?php echo $TEXT['tbl-etazh']; ?><input type=checkbox name=etazh_ch <?php echo $etazh_ch; ?>></td>
    <td><?php echo $TEXT['tbl-oborud']; ?><input type=checkbox name=obor_ch <?php echo $obor_ch; ?>></td>
    <td><?php echo $TEXT['podset-name']; ?></td>
</tr>
<tr>
    <?php
    if($result2['administrator'] == 1)
    {
    echo "<td><select name='srv_id'>";
        $query1 = "SELECT srv_id, name, descr FROM `tbl_srv` 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'] == $_POST['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;
                }
    echo "</select></td>";
    }
    ?>
    <td><select name="house_id">
    <?php
        $query20 = "SELECT * FROM `tbl_house` ORDER BY house";
        $house_tmp = mysqli_query($dbcnx,$query20);
          if(!$house_tmp) {puterror($result2['skin'],$TEXT['error-tbl10']); require_once "../utils/bottom_adm.php"; exit();}
              while($house = mysqli_fetch_array($house_tmp,MYSQLI_ASSOC))
                {
                 if($house['house_id'] == $_POST['house_id'])
                   {
                     $chk_house = "<option selected value=".$house['house_id'].">".$house['house'];
                   }
                  else
                   {
                     $chk_house = "<option value=".$house['house_id'].">".$house['house'];
                   }
                echo $chk_house;
                }
    ?>
    </select></td>
    <td><select name="etazh_id">
    <?php
        $query30 = "SELECT * FROM `tbl_etazh` ORDER BY etazh";
        $etazh_tmp = mysqli_query($dbcnx,$query30);
          if(!$etazh_tmp) {puterror($result2['skin'],$TEXT['error-tbl11']); require_once "../utils/bottom_adm.php"; exit();}
              while($etazh = mysqli_fetch_array($etazh_tmp,MYSQLI_ASSOC))
                {
                 if($etazh['etazh_id'] == $_POST['etazh_id'])
                   {
                     $chk_etazh = "<option selected value=".$etazh['etazh_id'].">".$etazh['etazh'];
                   }
                  else
                   {
                     $chk_etazh = "<option value=".$etazh['etazh_id'].">".$etazh['etazh'];
                   }
                echo $chk_etazh;
                }
    ?>
    </select></td>
    <td><select name="obor_id">
    <?php
        $query40 = "SELECT * FROM `tbl_oborud` ORDER BY oborud";
        $oborud_tmp = mysqli_query($dbcnx,$query40);
          if(!$oborud_tmp) {puterror($result2['skin'],$TEXT['error-tbl12']); require_once "../utils/bottom_adm.php"; exit();}
              while($oborud = mysqli_fetch_array($oborud_tmp,MYSQLI_ASSOC))
                {
                 if($oborud['obor_id'] == $_POST['obor_id'])
                   {
                     $chk_obor = "<option selected value=".$oborud['obor_id'].">".$oborud['oborud'];
                   }
                  else
                   {
                     $chk_obor = "<option value=".$oborud['obor_id'].">".$oborud['oborud'];
                   }
                echo $chk_obor;
                }
    ?>
    </select></td>
    <td><input size=15 maxlength=15 type=text name=podset value='<?php echo $_POST['podset']; ?>'></td>
    <td><input class="button" type=submit value='<?php echo $button; ?>'>
    <input type="hidden" name="ip_id" value="<?php echo $_POST['ip_id']; ?>">
    </td>
</tr>
</table>
</fieldset>
</td></tr>
</table>
</form>
<?php
if($result2['administrator'] == 1)
{
echo "<table align='center' border='0' cellpadding='0' cellspacing='0' width='70%'>";
echo "<tr align='right'>";
echo "<td><img alt=".$TEXT['add-new']." align='bottom' src='../skins/".$result2['skin']."/add.png' /><a class='bold_it' href='addform.php' title='".$TEXT['add-new-tit']."'>".$TEXT['add-new']."</a></td>";
echo "</tr>";
echo "</table>";
}

if($result2['administrator'] == 1) {echo "<table align='center' class='table' border='1' cellpadding='3' cellspacing='0' width='70%'>";}
 else {echo "<table align='center' class='table' border='1' cellpadding='3' cellspacing='0' width='60%'>";}

     $query10 = "SELECT 
         tbl_podset.*,
         tbl_house.*,
         tbl_etazh.*,
         tbl_oborud.*,
         tbl_srv.srv_id,
         tbl_srv.name AS srv_name,
         tbl_srv.descr AS srv_descr
         FROM tbl_podset
         JOIN tbl_house ON tbl_podset.house_id=tbl_house.house_id
         JOIN tbl_etazh ON tbl_podset.etazh_id=tbl_etazh.etazh_id
         JOIN tbl_oborud ON tbl_podset.obor_id=tbl_oborud.obor_id
         JOIN tbl_srv ON tbl_podset.srv_id=tbl_srv.srv_id
         WHERE ".$srv_id." ".$house_id." ".$etazh_id." ".$obor_id." ".$podset."
         ORDER BY tbl_srv.name, tbl_house.house, tbl_etazh.etazh, tbl_oborud.oborud, tbl_podset.podset ";
     $result10 = mysqli_query($dbcnx,$query10);
     if(!$result10) {puterror($result2['skin'],$TEXT['error-tbl13']); require_once "../utils/bottom_adm.php"; exit();}
       if(mysqli_num_rows($result10)>0)
         {
           echo "<thead>
              <tr class='tableheadercat'>";
           if($result2['administrator'] == 1) {echo "<td>".$TEXT['srv-name']."</td>";}
           echo "<td>".$TEXT['tbl-house']."</td>
                 <td>".$TEXT['tbl-etazh']."</td>
                 <td>".$TEXT['tbl-oborud']."</td>
                 <td>".$TEXT['tbl-podset']."</td>
                 <td>".$TEXT['podset-count']."</td>";
            if($result2['administrator'] == 1) {echo "<td colspan=2>".$TEXT['tbl-action']."</td>";}
            echo "</tr>
             </thead><tbody id='tab'>";
             while($result_podsets = mysqli_fetch_array($result10,MYSQLI_ASSOC))
               {
              $edit_pic = "<img alt='картинка' src='../skins/".$result2['skin']."/edit.png' />";
              $edit = $edit_pic."&nbsp;<a href=../podset/editform.php?pods_id=".$result_podsets['pods_id']." title='".$TEXT['edit-tit']."'>".$TEXT['edit-new']."</a>";
              $srv_name = $result_podsets['srv_name']."(".$result_podsets['srv_descr'].")";
              $delete_pic = "<img alt='Удалить' src='../skins/".$result2['skin']."/delete.png' />";
              $delete = $delete_pic."&nbsp;<a href=../podset/delete.php?pods_id=".$result_podsets['pods_id']." title='".$TEXT['delete-tit']."'>".$TEXT['delete-new']."</a>";

              $query_podset = mysqli_query($dbcnx,"SELECT COUNT(ip_id) AS c_ip_id FROM tbl_ip WHERE srv_id='".$result_podsets['srv_id']."' AND ip_addr LIKE '%".$result_podsets['podset']."%' AND vkl_ip='1' ");
	        $coun_podset = mysqli_fetch_array($query_podset,MYSQLI_ASSOC);

              $addr = explode($delimiter,$result_podsets['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'];}
              $podset_show = $oct_result1.".".$oct_result2.".".$oct_result3.".".$oct_result4;
              $srch_pods = $podset_show;

              if($result_podsets['begin_subn'] !== '')
              {
               $bld_n = "<span class='upd_bld'>";
               $bld_c = "</span>";
              } else
              {
               $bld_n = "";
               $bld_c = "";
              }

              if($coun_podset['c_ip_id'] > 254)
              {
              $podset_show = $podset_show;
              $rows = 10000;
              }
              else
               {
               $podset_show = "<a href='newoneform3.php?pods_id=".$result_podsets['pods_id']."&amp;ip_id=".$_POST['ip_id']."' title='".$TEXT['addr-move-dest']."'>".$podset_show."</a>";
               $rows = 255;
               }

           echo "<tr>";
              if($result2['administrator'] == 1) {echo "<td>".$srv_name."</td>";}
              echo "<td>".$result_podsets['house']."</td>";
              echo "<td>".$result_podsets['etazh']."</td>";
              echo "<td title='".$TEXT['addr-prefix'].": ".$result_podsets['prefix']."'>".$result_podsets['oborud']."</td>";
              echo "<td>".$bld_n.$podset_show.$bld_c."</td>";
              echo "<td><a href='../addresses/index.php?ip_addr=".$srch_pods."&amp;srv_id=".$result_podsets['srv_id']."&amp;srv_ch=on&amp;rows=".$rows."' target='_blank' title='".$TEXT['addr-rashinf']."'>".$coun_podset['c_ip_id'].$TEXT['log-grp-zadr4']."</a></td>";
              if($result2['administrator'] == 1) {echo "<td align='center'>".$edit."</td>";}
              if($result2['administrator'] == 1) {echo "<td align='center'>".$delete."</td>";}
            echo "</tr>";
               }
         }
echo "</tbody>";
echo "</table>";
require_once "../utils/bottom_adm.php";
?>