<?php
  ///////////////////////////////////////////////////
  //Размеры таблиц базы данных
  ///////////////////////////////////////////////////

  $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['size-text'];
  $helppage = $TEXT['optim-help'];

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

  if($result2['administrator'] == 0) {puterror($result2['skin'],$TEXT['error-loginform2']); require_once "../utils/bottom_adm.php"; exit();}

?>
<table align="center" class="table" border="0" cellpadding="0" cellspacing="0" width="90%" >
<tr><td>
<fieldset>
<legend align="left" title="<?php echo $TEXT['size-tit']; ?>"><?php echo $TEXT['size-text']; ?></legend>
<table align="center" class="table" border="1" cellpadding="0" cellspacing="0" width="100%" >
           <thead>
           <tr class="tableheadercat">
             <td><?php echo $TEXT['analiz-tab']; ?></td>
             <td><?php echo $TEXT['size-count']; ?></td>
             <td><?php echo $TEXT['size-ind']; ?></td>
             <td><?php echo $TEXT['size-data']; ?></td>
             <td><?php echo $TEXT['size-razm']; ?></td>
             <td><?php echo $TEXT['size-frag']; ?></td>
             <td><?php echo $TEXT['tbl-action']; ?></td>
           </tr>
           </thead>
           <tbody id="tab">
<?php
    $tbls = mysqli_query($dbcnx,"SHOW TABLES FROM $dbname");
     while($all = mysqli_fetch_array($tbls))
      {
      $i =$all['0'];
        $query = "SHOW TABLE STATUS LIKE '$i' ";
          $result = mysqli_query($dbcnx,$query);
            if(!$result) {puterror2($TEXT['error-analiz']);}
              while($result_tbsize = mysqli_fetch_array($result,MYSQLI_ASSOC))
              {
              $result_ind = $result_tbsize['Index_length'];
              $result_data = $result_tbsize['Data_length'];
              $result_tb = $result_tbsize['Data_length'] + $result_tbsize['Index_length'];
              $result_db  += $result_tb;
              $result_indall  += $result_ind;
              $result_dataall  += $result_data;
              $result_rows += $result_tbsize['Rows'];
              if($result_tbsize['Data_length']>0) 
              {
              $fragment = $result_tbsize['Data_free']/($result_tbsize['Data_length']/100);
              $fragment = round($fragment, 0);
              }
              else {$fragment = 0;}
              $defrag = "<a href=optimize.php?tbl=".$i." title='".$TEXT['optim-tit3']."'>".$TEXT['optim_text2']."</a>";

              if($result_tb > 1024)
              {
              $result_tb = (float)($result_tb/1024);
              if($result_tb > 1024)
                {
                 $result_tb = (float)($result_tb/1024);
                 if($result_tb > 1024)
                 {
                     $result_tb = (float)($result_tb/1024);
                     $result_tb = round($result_tb, 2);
                     $result_tb = $result_tb.$TEXT['size-gb'];
                 } else
                    {
                       $result_tb = (float)($result_tb);
                       $result_tb = round($result_tb, 2);
                       $result_tb = $result_tb.$TEXT['size-mb'];
                    }
                }
                 else
                  {
                  $result_tb = round($result_tb, 2);
                  $result_tb = $result_tb.$TEXT['size-kb'];
                  }
              }
              else {$result_tb = $result_tb.$TEXT['size-b'];}

              if($result_ind > 1024)
              {
              $result_ind = (float)($result_ind/1024);
              if($result_ind > 1024)
                {
                 $result_ind = (float)($result_ind/1024);
                 if($result_ind > 1024)
                 {
                     $result_ind = (float)($result_ind/1024);
                     $result_ind = round($result_ind, 2);
                     $result_ind = $result_ind.$TEXT['size-gb'];
                 } else
                    {
                       $result_ind = (float)($result_ind);
                       $result_ind = round($result_ind, 2);
                       $result_ind = $result_ind.$TEXT['size-mb'];
                    }
                }
                 else
                  {
                  $result_ind = round($result_ind, 2);
                  $result_ind = $result_ind.$TEXT['size-kb'];
                  }
              }
              else {$result_ind = $result_ind.$TEXT['size-b'];}

              if($result_data > 1024)
              {
              $result_data = (float)($result_data/1024);
              if($result_data > 1024)
                {
                 $result_data = (float)($result_data/1024);
                 if($result_data > 1024)
                 {
                     $result_data = (float)($result_data/1024);
                     $result_data = round($result_data, 2);
                     $result_data = $result_data.$TEXT['size-gb'];
                 } else
                    {
                       $result_data = (float)($result_data);
                       $result_data = round($result_data, 2);
                       $result_data = $result_data.$TEXT['size-mb'];
                    }
                }
                 else
                  {
                  $result_data = round($result_data, 2);
                  $result_data = $result_data.$TEXT['size-kb'];
                  }
              }
              else {$result_data = $result_data.$TEXT['size-b'];}

      ?>
           <tr>
              <td><?php echo $all['0']; ?></td>
              <td><?php echo $result_tbsize['Rows']; ?></td>
              <td><?php echo $result_ind; ?></td>
              <td><?php echo $result_data; ?></td>
              <td><?php echo $result_tb; ?></td>
              <td><?php echo $fragment; ?></td>
              <td><?php echo $defrag; ?></td>
           </tr>
      <?php
           }
     }
           if($result_db > 1024)
              {
              $result_db = (float)($result_db/1024);
              if($result_db > 1024)
                {
                 $result_db = (float)($result_db/1024);
                 if($result_db> 1024)
                 {
                     $result_db = (float)($result_db/1024);
                     $result_db = round($result_db, 2);
                     $result_db = $result_db.$TEXT['size-gb'];
                 } else
                    {
                       $result_db = (float)($result_db);
                       $result_db = round($result_db, 2);
                       $result_db = $result_db.$TEXT['size-mb'];
                    }
                }
                 else
                  {
                  $result_db = round($result_db, 2);
                  $result_db = $result_db.$TEXT['size-kb'];
                  }
              }
              else {$result_db = $result_db.$TEXT['size-b'];}

                if($result_indall > 1024)
              {
              $result_indall = (float)($result_indall/1024);
              if($result_indall > 1024)
                {
                 $result_indall = (float)($result_indall/1024);
                 if($result_indall > 1024)
                 {
                     $result_indall = (float)($result_indall/1024);
                     $result_indall = round($result_indall, 2);
                     $result_indall = $result_indall.$TEXT['size-gb'];
                 } else
                    {
                       $result_indall = (float)($result_indall);
                       $result_indall = round($result_indall, 2);
                       $result_indall = $result_indall.$TEXT['size-mb'];
                    }
                }
                 else
                  {
                  $result_indall = round($result_indall, 2);
                  $result_indall = $result_indall.$TEXT['size-kb'];
                  }
              }
              else {$result_indall = $result_indall.$TEXT['size-b'];}

                if($result_dataall > 1024)
              {
              $result_dataall = (float)($result_dataall/1024);
              if($result_dataall > 1024)
                {
                 $result_dataall = (float)($result_dataall/1024);
                 if($result_dataall > 1024)
                 {
                     $result_dataall = (float)($result_dataall/1024);
                     $result_dataall = round($result_dataall, 2);
                     $result_dataall = $result_dataall.$TEXT['size-gb'];
                 } else
                    {
                       $result_dataall = (float)($result_dataall);
                       $result_dataall = round($result_dataall, 2);
                       $result_dataall = $result_dataall.$TEXT['size-mb'];
                    }
                }
                 else
                  {
                  $result_dataall = round($result_dataall, 2);
                  $result_dataall = $result_dataall.$TEXT['size-kb'];
                  }
              }
              else {$result_dataall = $result_dataall.$TEXT['size-b'];}
                ?>
           </tbody>
           <tfoot>
           <tr class="tableheadercat">
             <td align="right"><?php echo $TEXT['size-itogo']; ?></td>
             <td><?php echo $TEXT['size-count2']; ?> = <?php echo $result_rows; ?></td>
             <td><?php echo $result_indall; ?></td>
             <td><?php echo $result_dataall; ?></td>
             <td><?php echo $result_db; ?></td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
           </tr>
           </tfoot>
                 <?php
            //*************************************
            //Информация для лога
            $ip = $_SERVER['REMOTE_ADDR'];
            $doing = $TEXT['bd-size1'];
            $what = $TEXT['bd-size2']." (".$result_db.")";
            $who = $result2['familiya']." ".$result2['name'];
            //*************************************
            $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
            if(!mysqli_query($dbcnx,$query2)) {puterror($result2['skin'],$TEXT['error-log']); require_once "../utils/bottom_adm.php"; exit();}
?>
</table>
</fieldset>
</td></tr>
</table>
<?php
  require_once "../utils/bottom_adm.php";
?>