<?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['menu-btn34-title'];

  require_once "../config_sams.php";
  require_once "../utils/functions.php";
  require_once "../utils/top_sams.php";

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

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

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

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

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

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

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

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

  if(strlen($_POST['rows'])>12) {puterror($result2['skin'],$TEXT['error-log10']); 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();}
  if(strlen($_POST['domain'])>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['ip_addr'])>15) {puterror($result2['skin'],$TEXT['error-addr5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['nick'])>40) {puterror($result2['skin'],$TEXT['error-addr7']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['vkl'])>1) {puterror($result2['skin'],$TEXT['error-srv15']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['vkl_ch'])>5) {puterror($result2['skin'],$TEXT['error-srv15']); require_once "../utils/bottom_adm.php"; exit();}

  $_POST['domain'] = rep($_POST['domain']);
  $_POST['descr'] = rep($_POST['descr']);
  $_POST['ip_addr'] = rep($_POST['ip_addr']);
  $_POST['nick'] = rep($_POST['nick']);
  $_POST['vkl'] = rep($_POST['vkl']);
  $_POST['vkl_ch'] = rep($_POST['vkl_ch']);
  $_POST['rows'] = rep($_POST['rows']);
  $_POST['rows'] = intval($_POST['rows']);

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

  $delimiter=".";
  $name=$_POST['name'];

  $domain =trim($_POST['domain']);
  $nick=trim($_POST['nick']);
  $descr=$_POST['descr'];
  $rows=$_POST['rows'];
  if(empty($_POST['vkl'])) {$_POST['vkl'] = 0;}

  if($_POST['vkl_ch'] == "on")
   {
    $vkl="AND squidusers.enabled = '".$_POST['vkl']."' ";
    $vkl_ch="checked";
    $vkl_opt = $_POST['vkl'];
   }
   else
   {
    $vkl="";
    $vkl_ch="";
    $vkl_opt = $_POST['vkl'];
   }

  $query2 = mysqli_query($dbcnx,"SELECT COUNT(id) AS c_id FROM squidusers");
    $coun = mysqli_fetch_array($query2,MYSQLI_ASSOC);
?>
<form enctype='multipart/form-data' action=<?php echo $action; ?> method=post >
<table align="center" class="table" border="0" cellpadding="0" cellspacing="0" width="90%">
<tr><td>
<fieldset>
<legend align="left" title="<?php echo $TEXT['log-tit7']; ?>"><?php echo $TEXT['btn_filter']; ?>&nbsp;(<?php echo $TEXT['log-addr8']; ?><?php echo $coun['c_id']; ?>)</legend>
<table class="table" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr class="button">
    <td><?php echo $TEXT['addr-ip-addr']; ?></td>
    <td><?php echo $TEXT['login-login']; ?></td>
    <td><?php echo $TEXT['menu-btn12']; ?></td>
    <td><?php echo $TEXT['login-fullname']; ?></td>
    <td><?php echo $TEXT['addr-vkl-tit']; ?><input type=checkbox name=vkl_ch <?php echo $vkl_ch; ?>></td>
    <td><?php echo $TEXT['size-count']; ?></td>
</tr>
<tr><td><input size=13 maxlength=15 type=text name=ip_addr value='<?php echo $_POST['ip_addr']; ?>'></td>
    <td><input size=15 maxlength=17 type=text name=nick value='<?php echo $nick; ?>'></td>
    <td><input size=12 maxlength=255 type=text name=domain value='<?php echo $domain; ?>'></td>
    <td><input size=20 maxlength=255 type=text name=descr value='<?php echo $descr; ?>'></td>
    <td>
        <select name=vkl>
          <option value='1' <?php if ($vkl_opt == 1) {echo "selected";} ?>><?php echo $TEXT['addr-vkluchit2']; ?>
          <option value='0' <?php if ($vkl_opt == 0) {echo "selected";} ?>><?php echo $TEXT['addr-vikluchit2']; ?>
       </select>
    </td>
    <td><input size=8 maxlength=11 type=text name=rows title='<?php echo $TEXT['log-tit12']; ?>' value='<?php echo $rows; ?>'></td>
    <td><input type="hidden" name="skin" value="<?php echo $skin_tmp; ?>" />
            <input class="button" type="submit" value="<?php echo $button; ?>" /></td>
</tr>
</table>
</fieldset>
</td></tr>
</table>
</form>
<table align="center" border="1" cellpadding="3" cellspacing="0" width="90%">
<?php
echo "<thead><tr class='tableheadercat'>
            <td>".$TEXT['srv-vkl-tit']."</td>
            <td>".$TEXT['addr-ip-addr']."</td>
            <td>".$TEXT['login-login']."</td>
            <td>".$TEXT['menu-btn12']."</td>
            <td>".$TEXT['login-fullname']."</td>
            <td>".$TEXT['ondisk_dost']." / ".$TEXT['ondisk_use']."</td>
            </tr></thead><tbody id='tab'>";

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

   if(!empty($_POST['descr'])) {$descr = "AND LOWER(name) LIKE LOWER('%".$_POST['descr']."%') OR LOWER(family) LIKE LOWER('%".$_POST['descr']."%')  OR LOWER('soname') LIKE LOWER('%".$_POST['descr']."%')";}
    else {$descr = "";}

   if(!empty($_POST['ip_addr'])) {$ip_addr = "AND ip REGEXP '".$_POST['ip_addr']."' ";}
    else {$ip_addr = "";}

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

     $query_body = "SELECT `ip`, `nick`, `name`, `family`, `soname`, `enabled`, `quotes`, `size`, `domain` FROM `squidusers` WHERE id !=0 ".$domain." ".$descr." ".$ip_addr." ".$nick." ".$vkl." ORDER BY `ip` LIMIT ".$_POST['rows']."";
     $result_body = mysqli_query($dbcnx,$query_body);
     if(!$result_body) {echo mysqli_error($dbcnx); exit();}
      $result_rows2 = mysqli_num_rows($result_body);
       if($result_rows2 > 0)
        {
           while($result_body_addr = mysqli_fetch_array($result_body,MYSQLI_ASSOC))
               {

              $online = "<img alt='".$TEXT['addr-vikluchit']."' src='../skins/base/online.png' />";
              $offline = "<img alt='".$TEXT['addr-vkluchit']."' src='../skins/base/offline.png' />";
              $warn = "<img alt='".$TEXT['addr-vkluchit']."' src='../skins/base/warn.png' />";

              $size = round($result_body_addr['size']/1024/1024);
              $size = $size." ".$TEXT['size-mb'];

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

              if($size > intval($result_body_addr['quotes']) && intval($result_body_addr['quotes']) !== 0) {$strvisible = $warn;}
              $fio = trim($result_body_addr['family']." ".$result_body_addr['name']." ".$result_body_addr['soname']);
              if($fio == "") {$fio = "---";}
               else {$fio = $fio;}

           ?>
           <tr>
              <td><?php echo $strvisible; ?></td>
              <td><?php echo $result_body_addr['ip']; ?></td>
              <td><?php echo $result_body_addr['nick']; ?></td>
              <td><?php echo $result_body_addr['domain']; ?></td>
              <td><?php echo $fio; ?></td>
              <td><?php echo $result_body_addr['quotes']; ?> <?php echo $TEXT['size-mb']; ?> / <?php echo $size; ?></td>
           </tr>
           <?php
 
              }
         }
          else
         {
         ?>
         <tr><td colspan="6" class="bold_it"><?php echo $TEXT['search-err4']; ?></td></tr>
         <?php
         }        
?>
</tbody>
<tfoot>
<tr class='tableheadercat'>
<td colspan="11" align="right"><?php echo $TEXT['addr-found']; ?><?php echo $result_rows2; ?><?php echo $TEXT['addr-found-obg']; ?></td>
</tr>
</tfoot>
</table>
<?php
  require_once "../utils/bottom_sams.php";
?>