<?php

require "include/bittorrent.php";
dbconn(false);
loggedinorreturn();

/**
* Hier die Berechtigung der Klasse eintragen zu dieser Seite
*/
if (get_user_class() < UC_SYSOP) {
    stderr("Error", "Wo willst du Knilch eigendlich hin?? Das geht Dich hier garnix an!!");
    exit;
}

if (isset($_GET['Do']) && isset($_GET['table'])) {
$Do = ($_GET['Do'] === "T") ? sqlesc($_GET['Do']) : ""; 
if(!ereg('[^A-Za-z_]+', $_GET['table'])) {
$Table = '`'.$_GET['table'].'`';
}else{
print("Schweine Hund :) !");
exit;
}

$sql = "OPTIMIZE TABLE $Table";
if (preg_match('@^(CHECK|ANALYZE|REPAIR|OPTIMIZE)[[:space:]]TABLE[[:space:]]'.$Table.'$@i', $sql)) {
//alles ok ? dann Tu es
@mysql_query($sql) or die("<b>Irgendwas ist nict richtig!</b>.\n<br />Query: " . $sql . "<br />\nFehler: (" . mysql_errno() . ") " . htmlspecialchars(mysql_error()));
//Wenn optimiert zurück zur Seitenübersicht
$return_url = "mysql_overview.php?Do=F";
header("Location: http://" . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF'])
/*. "/" . $return_url);*/
. $return_url);

exit;
}
}
//bytes Einheit und Anzeige
$GLOBALS["byteUnits"] = array('Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB');

////////////////// FUNKTIONEN LISTE /////////////////////////

function formatByteDown($value, $limes = 2, $comma = 0)
{
$dh = pow(10, $comma);
$li = pow(10, $limes);
$return_value = $value;
$unit = $GLOBALS['byteUnits'][0];

for ( $d = 6, $ex = 15; $d >= 1; $d--, $ex-=3 ) {
if (isset($GLOBALS['byteUnits'][$d]) && $value >= $li * pow(10, $ex)) {
$value = round($value / ( pow(1024, $d) / $dh) ) /$dh;
$unit = $GLOBALS['byteUnits'][$d];
break 1;
} // ende if
} // ende for

if ($unit != $GLOBALS['byteUnits'][0]) {
$return_value = number_format($value, $comma, '.', ',');
} else {
$return_value = number_format($value, 0, '.', ',');
}

return array($return_value, $unit);
} // Ende 'formatByteDown' Funktion
////////////////// ENDE FUNKTIONEN LIST /////////////////////////


stdhead("Stats");
begin_frame ("Mysql Datenbank Übersicht mit Optimierungs Funktion");

?>

<!-- Start Tabelle -->

<table align="center" class="tableoutborder" id="torrenttable" border="0" cellspacing="1" cellpadding="3">

<!-- Start Tabelle headers -->
<tr>
 <td class="tabletitle" align="center"><b>Tabellen Name</b></td>
 <td class="tabletitle" align="center"><b>Grösse</b></td>
 <td class="tabletitle" align="center"><b>Datensätze</b></td>
 <td class="tabletitle" align="center"><b>Grösse pro Satz</b></td>
 <td class="tabletitle" align="center"><b>Daten Länge</b></td>
 <td class="tabletitle" align="center"><b>Index Länge</b></td>
 <td class="tabletitle" align="center"><b>Überschuss</b></td>

<!-- <td class="tabletitle" align="center"><b>Max_data_length</b></td> -->
<!-- <th>Auto_increment</b></td> -->
<!-- <td class="tabletitle" align="center"><b>Timings</b></td> -->

</tr>

<!-- Ende tabelle headers -->

<?
$count = 0;

$res = @mysql_query('SHOW TABLE STATUS FROM `source`') or Die(mysql_error());
while ($row = mysql_fetch_array($res)) {
list($formatted_Avg, $formatted_Abytes) = formatByteDown($row['Avg_row_length']);
list($formatted_Dlength, $formatted_Dbytes) = formatByteDown($row['Data_length']);
list($formatted_Ilength, $formatted_Ibytes) = formatByteDown($row['Index_length']);
list($formatted_Dfree, $formatted_Fbytes) = formatByteDown($row['Data_free']);
$tablesize = ($row['Data_length']) + ($row['Index_length']);
list($formatted_Tsize, $formatted_Tbytes) = formatByteDown($tablesize, 3, ($tablesize > 0) ? 1 : 0);

$thispage = "?Do=T&table=".$row['Name'];
$overhead = ($formatted_Dfree > 0) ? "<a href=mysql_overview.php".$thispage."><font color='red'><b>".$formatted_Dfree." ".$formatted_Fbytes."</b></font></a>" : $formatted_Dfree." ".$formatted_Fbytes;

echo "<tr><td class=tablea><b>{$row['Name']}</b></td>".
"<td class=tableb>{$formatted_Tsize} {$formatted_Tbytes}</td>".
"<td class=tablea>{$row['Rows']}</td>".
"<td class=tableb>{$formatted_Avg} {$formatted_Abytes}</td>".
"<td class=tablea>{$formatted_Dlength} {$formatted_Dbytes}</td>".
"<td class=tableb>{$formatted_Ilength} {$formatted_Ibytes}</td>".
"<td class=tablea>{$overhead}</td></tr>".
"<tr align=\"right\"><td class=tablea colspan=\"7\"><i><b>Datensatz Format:</b></i> {$row['Row_format']}".
"<br /><i><b>Erstellt am:</b></i> {$row['Create_time']}".
"<br /><i><b>Update am:</b></i> {$row['Update_time']}".
"<br /><i><b>Prüfe Zeit:</b></i> {$row['Check_time']}</td></tr>";
$count++;

}
echo "<tr><td class=tabletitle><b>Tables: {$count}</b></td><td class=tabletitle colspan=\"6\" align=\"right\">Bei den <font color=\"red\"><b>ROT</b></font> makierten Tabellen wäre eine Optimierung empfehlenswert!!!!</td></tr>";
?>
<!-- Ende tabelle -->
</table>

<?
stdfoot();
?> 