<?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['global-title'];
  $button = $TEXT['btn_run'];
  $action = "run_addr.php";

  require_once "config.php";
  require_once "utils/functions.php";
  require_once "utils/top.php";
  $delimiter =".";

  $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'];

//// проверка на наличие обновлений - перенёс в bottom.php
//if($result2['chk_upd'] == "yes"){
// $vis_upd = update_check("http://tel-spr.narod.ru/manydhcp-upd.txt",$int_proxy);
//  if($vis_upd !== trim($TEXT['global-now-version'])) {echo "<div id='formaupdate'>".$TEXT['global-new-version-yes']."<a href='http://tel-spr.narod.ru/manydhcp/changelog.html' target=_blank >".$vis_upd."</a></div>";}
//  if($vis_upd == "") {echo "<div id='formaupdate'>".$TEXT['global-new-version-no']."</div>";}
//}

  if($result2['administrator'] == 0 && $result2['user'] == 0) {puterror2($result2['skin'],$TEXT['error-loginform2']); require_once "utils/bottom.php"; exit();}
  if(strlen($_GET['k'])>8) {puterror($result2['skin'],$TEXT['error-paging1']); require_once "../utils/bottom_adm.php"; exit();}
  $_GET['k'] = rep($_GET['k']);
  if(empty($_GET['k'])) {$_GET['k']=0;}
  $stop_r = $_GET['k'];
  if($stop_r == 0) {$lim_ogr = "20";}
   else {$lim_ogr = $stop_r.",20";}

  $ktblvis = $stop_r+20;
  $str = $ktblvis/20;

  $query_cl = "SELECT * FROM `tbl_clear`";
  $result_cl = mysqli_query($dbcnx,$query_cl);
    if(!$result_cl) {puterror2($result2['skin'],$TEXT['error-tbl8']); require_once "utils/bottom.php"; exit();}
     if(mysqli_num_rows($result_cl)>0)
         {
          while($result_cl2 = mysqli_fetch_array($result_cl,MYSQLI_ASSOC))
           {
           $tbl_name = $result_cl2['col_tbl_name'];
           $tbl_col = $result_cl2['col_name'];
           $tbl_day = $result_cl2['by_data'];
           $tbl_rows = $result_cl2['by_count'];

           $query_log_coun = mysqli_query($dbcnx,"SELECT COUNT($tbl_col) AS ca_tbl FROM $tbl_name");
           $coun_log = mysqli_fetch_array($query_log_coun,MYSQLI_ASSOC);
            if($coun_log['ca_tbl'] > 0)
           {
           $query10 = "SELECT ADDDATE((SELECT DATE((SELECT MAX($tbl_col) FROM $tbl_name))),-$tbl_day)";
           $result10_del_cl1 = mysqli_query($dbcnx,$query10);
            if(!$result10_del_cl1) {puterror2($result2['skin'],$TEXT['error-tbl8']); require_once "utils/bottom.php"; exit();}
             $r2 = mysqli_fetch_array($result10_del_cl1,MYSQLI_ASSOC);
             if($result_cl2['by_data'] > 0)
             {
             //удалить всё если отклонение в днях больше указанного
              $query_del_cl1 = "DELETE FROM `$tbl_name` WHERE `".$tbl_col."` < '".$r2['0']."'";
               $result_del_cl1 = mysqli_query($dbcnx,$query_del_cl1);
                if(!$result_del_cl1) {puterror2($result2['skin'],$TEXT['error-tbl8']); require_once "utils/bottom.php"; exit();}
             }
             if($result_cl2['by_count'] > 0)
             {
             if($coun_log['ca_tbl'] > $tbl_rows)
             {
             //удалить все если отклонение в количестве строк больше указанного
             $del_rows = $coun_log - $tbl_rows;
               $query_del_cl2 = "DELETE FROM `$tbl_name` ORDER BY data LIMIT $del_rows";
                $result_del_cl2 = mysqli_query($dbcnx,$query_del_cl2);
                if(!$result_del_cl2) {puterror2($result2['skin'],$TEXT['error-tbl8']); require_once "utils/bottom.php"; exit();}
              }
             }
           }
        }
      }
?>
<form enctype='multipart/form-data' action=<?php echo $action; ?> method=post>
<table border="0" width="100%" align="center">
<tr>
<td width="25%" valign="top">
<fieldset>
<legend align="left" title="<?php echo $TEXT['addr-srv-tit']; ?>"><?php echo $TEXT['addr-srv']; ?></legend>
<table align="right" width="98%" border="0">
<?php
     $query_body = "SELECT srv_id, name, descr, sshport FROM `tbl_srv` WHERE vkl = '1' ORDER BY name ";
     $result_body = mysqli_query($dbcnx,$query_body);
     if(!$result_body) {puterror2($result2['skin'],$TEXT['error-tbl1']); require_once "utils/bottom.php"; exit();}
       $result_rows = mysqli_num_rows($result_body);
       if($result_rows  > 0)
         {
          while($result_body_srv = mysqli_fetch_array($result_body,MYSQLI_ASSOC))
          {
           if(empty($_POST['familiya'])) {$fam = "";}
            else {$fam = "&familiya=".$_POST['familiya']."";}

           $srv_name = $result_body_srv['name'];
           $srv_id = $result_body_srv['srv_id'];
           
           if(trim($result_body_srv['descr'] !== ""))
           {
           $check_ssh = fsockopen($result_body_srv['name'],$result_body_srv['sshport'],$errno,$errstr,3);
            if(!$check_ssh) {$ssh_conn = "<img src='skins/".$result2['skin']."/vikl.png' />";}
             else
             {
             $ssh_conn = "<img src='skins/".$result2['skin']."/vkl.png' />";
             fclose($check_ssh);
             }
           ?>
           <tr>
              <td class="bold_it_page" ><?php echo $srv_name; ?>&nbsp;(<?php echo $result_body_srv['descr']; ?>)</td>
              <td><?php echo $ssh_conn; ?></td>
<!--              <td><input type="checkbox" name=srv[<?php echo $srv_id; ?>] ></td>-->
           </tr>
           <?php
           }
           else
           {
            $check_ssh = fsockopen($result_body_srv['name'],$result_body_srv['sshport'],$errno,$errstr,3);
            if(!$check_ssh) {$ssh_conn = "<img src='skins/".$result2['skin']."/vikl.png' />";}
             else
             {
             $ssh_conn = "<img src='skins/".$result2['skin']."/vkl.png' />";
             fclose($check_ssh);
             }
            ?>
           <tr>
              <td class="bold_it_page" ><?php echo $srv_name; ?></td>
              <td><?php echo $ssh_conn; ?></td>
<!--              <td><input type="checkbox" name=srv[<?php echo $srv_id; ?>] ></td>-->
           </tr>
           <?php
           }
          }
         }
         ?>
           
           <tr>
              <td class="bold_it_page" ><hr />
                  <select name="srv_id">
         <?php
         $query_body_chk = "SELECT srv_id, name, descr FROM `tbl_srv` WHERE vkl = '1' ORDER BY name ";
         $result_body_chk = mysqli_query($dbcnx,$query_body_chk);
         if(!$result_body_chk) {puterror2($result2['skin'],$TEXT['error-tbl1']); require_once "utils/bottom.php"; exit();}
           $result_rows_chk = mysqli_num_rows($result_body_chk);
            if($result_rows_chk  > 0)
            {
             while($result_body_srv_chk = mysqli_fetch_array($result_body_chk,MYSQLI_ASSOC))
             {
              $chk_servers = "<option value=".$result_body_srv_chk['srv_id'].">".$result_body_srv_chk['name']."&nbsp;(".$result_body_srv_chk['descr'].")";
              echo $chk_servers;  
             }
            }
           ?>
           </select></td>
           <td><hr /><input type="checkbox" name="srv_used" ></td>
           </tr>
           <tr><td title="<?php echo $TEXT['upr-key-fr']; ?>" class="bold_it_page" ><input size=22 maxlength=255 type=password name=kfr ></td><td><img alt="<?php echo $TEXT['upr-key-fr']; ?>" src='skins/<?php echo $result2['skin']?>/pass16.png' /></td></tr>
      <tr><td class="bold_it_page" ><input class="button2" type=submit value='<?php echo $button; ?>'></td><td></td></tr>
</table>
</fieldset>
</td>
<td width="75%">
<fieldset>
<legend align="left" title="<?php echo $TEXT['addr-sost-tit']; ?>"><?php echo $TEXT['addr-sost']; ?></legend>
<table align="left" 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['tbl-date']; ?></td>
            <td><?php echo $TEXT['addr-last-ping']; ?></td>
            <td><?php echo $TEXT['srv-vkl-tit']; ?></td>
            <td><?php echo $TEXT['global-action']; ?></td>
        </tr></thead><tbody id='tab'>
<?php
$query_body2 = "SELECT
    tbl_ip.ip_id,
    tbl_ip.name,
    tbl_ip.data,
    tbl_ip.ip_addr,
    tbl_ip.hw,
    tbl_ip.descr,
    tbl_ip.vkl_ip,
    YEAR(tbl_ip.data) AS data_year,
    MONTH(tbl_ip.data) AS data_month,
    DAY(tbl_ip.data) AS data_day,
    YEAR(tbl_ip.last_acc) AS data2_year,
    MONTH(tbl_ip.last_acc) AS data2_month,
    DAY(tbl_ip.last_acc) AS data2_day,
    tbl_ip.state_addr,
    tbl_srv.srv_id,
    tbl_srv.vkl
    FROM tbl_ip
    JOIN tbl_srv ON tbl_ip.srv_id=tbl_srv.srv_id
    WHERE tbl_ip.state_addr = '1' AND tbl_ip.vkl_ip = '1' AND tbl_srv.vkl = '1'
    ORDER BY tbl_ip.data, tbl_ip.ip_addr LIMIT ".$lim_ogr."";
 $result_body2 = mysqli_query($dbcnx,$query_body2);
     if(!$result_body2) {puterror2($result2['skin'],mysqli_error($dbcnx)); require_once "utils/bottom.php"; exit();}
       $result_rows2 = mysqli_num_rows($result_body2);
       if($result_rows2 > 0)
         {
          while($result_body_ip = mysqli_fetch_array($result_body2,MYSQLI_ASSOC))
          {
          $online = "<img alt='online' src='skins/".$result2['skin']."/online.png' />";
          $offline = "<img alt='offline' src='skins/".$result2['skin']."/offline.png' />";
          $edit = "<a href=addresses/editform.php?ip_id=".$result_body_ip['ip_id']." title='".$TEXT['edit-tit']."'>".$TEXT['edit-new']."</a>";

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

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

          $date_day = $result_body_ip['data_day'];
          $date_month = $result_body_ip['data_month'];
          $date_year = $result_body_ip['data_year'];
          if($result_body_ip['data_year'] == "" || $result_body_ip['data_year'] == "0000" || $result_body_ip['data_year'] == "1970") {$data_r = "---";}
           else {$data_r = $date_day." ".$month[$date_month]." ".$date_year." ".$TEXT['year'];}

          $date_day2 = $result_body_ip['data2_day'];
          $date_month2 = $result_body_ip['data2_month'];
          $date_year2 = $result_body_ip['data2_year'];
          if($result_body_ip['data2_year'] == "" || $result_body_ip['data2_year'] == "0000" || $result_body_ip['data2_year'] == "1970") {$data_r2 = "---";}
           else {$data_r2 = $date_day2." ".$month[$date_month2]." ".$date_year2." ".$TEXT['year'];}

             $addr = explode($delimiter,$result_body_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;
?>
<tr>
    <td class="bold_it"><img alt="<?php echo $TEXT['global-vopros']; ?>" src="skins/<?php echo $result2['skin']; ?>/wait.png" /></td>
    <td class="bold_it" title="<?php echo $result_body_ip['descr']; ?>"><?php echo $result_body_ip['name']; ?></td>
    <td class="bold_it" title="<?php echo $result_body_ip['descr']; ?>"><?php echo $ip_addres; ?></td>
    <td class="bold_it"><?php echo substr(chunk_split($result_body_ip['hw'],2,":"),0,-1); ?></td>
    <td class="bold_it"><?php echo $data_r; ?></td>
    <td class="bold_it"><?php echo $data_r2; ?></td>
    <td class="bold_it"><?php echo $strvisible; ?></td>
    <td><?php echo $edit; ?></td>
</tr>
<?php
          }
        }
   $query_count = mysqli_query($dbcnx,"SELECT COUNT(tbl_ip.ip_id) AS c_ip_id FROM tbl_ip JOIN tbl_srv ON tbl_ip.srv_id=tbl_srv.srv_id WHERE tbl_ip.state_addr = '1' AND tbl_ip.vkl_ip = '1' AND tbl_srv.vkl = '1'");
    $coun_rows = mysqli_fetch_array($query_count,MYSQLI_ASSOC);
?>
</tbody>
<tfoot>
<tr class='tableheadercat'>
<td colspan="8" align="right"><?php echo $TEXT['addr-found']; ?><?php echo $result_rows2; ?><?php echo $TEXT['addr-found-obg']; ?>&nbsp;&frasl;&nbsp;<?php echo $TEXT['size-razm']; ?> - <?php echo $coun_rows['c_ip_id']; ?><?php echo $TEXT['addr-found-obg']; ?></td>
</tr>
</tfoot>
</table>
 <?php
            $pag = $coun_rows['c_ip_id']/20;
               if($pag<=1) {$pag = ceil($pag);}
                else
                {
                 ?>
                  <table align="center" border="0" cellpadding="0" cellspacing="0" width="98%" >
                   <tr align="center">
                    <td>
                <?php
                $pag=ceil($pag);
                $strn = $str-5;
                if($strn<1) {$strn=1;}
                $strk = $str+5;
                for($i=$strn; $i<=$pag; $i++)
                 {
                  if($i<=$strk)
                  {
                    if($i==$str)
                    {
                      $k = $i*20-20;
                      $kvis=$k+20;
                      $nvis=$k;
                      echo $pg = " <a class='zagolovok' href='main.php?k=".$k."' title='".$TEXT['tbl-details4'].$nvis.$TEXT['tbl-details3'].$kvis."'>".$i."</a>";
                    }
                    else
                    {
                    $k = $i*20-20;
                    $kvis=$k+20;
                    $nvis=$k;
                    echo $pg = " <a href='main.php?k=".$k."' title='".$TEXT['tbl-details4'].$nvis.$TEXT['tbl-details3'].$kvis."'>[".$i."]</a>";
                    }
                  }
                 }
                 ?>
                    <select name='k' onchange=window.location.href=this.options[this.selectedIndex].value;>
                    <?php
                    for($j=1; $j<=$pag; $j++)
                    {
                     $k = $j*20-20;
                     if($k == $_GET['k']) {echo "<option selected value=main.php?k=".$k.">".$j."</option>";}
                       else {echo "<option value=main.php?k=".$k.">".$j."</option>";}
                    }
                    ?>
                    </select>
                    </td>
                   </tr>
                  </table>
                 <?php
                }
?>
</fieldset>
</td>
</tr>
</table>
</form>
<?php
  require_once "utils/bottom.php";
?>