<?PHP
/************************************************************************/
/* DUNE by NPDS                                                         */
/* ===========================                                          */
/*                                                                      */
/* M. PASCAL aKa EBH (plan.net@free.fr)                                 */
/*                                                                      */
/* NPDS Copyright (c) 2002-2007 by Philippe Brunier                     */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
function Access_Error () {
  include("admin/die.php");
}
if (!eregi("admin.php", $PHP_SELF)) { Access_Error(); }

$result = sql_query("SELECT radminsuper FROM ".$NPDS_Prefix."authors WHERE aid='$aid'");
list($radminsuper) = sql_fetch_row($result);
if ($radminsuper != 1) {
   Access_Error();
} else {
        $date_opt = date(adm_translate("dateforop"));
        $heure_opt = date("h:i a");
        include("header.php");
        GraphicAdmin($hlpfile);
        global $dbname;
        opentable();
        echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"header\">\n";
        echo adm_translate("Optimisation de la base de données ")."&nbsp;:&nbsp;$dbname";
        echo "</td></tr></table>\n";
        echo "<br />\n";
                // Création de la table optimy (si nécessaire)
                $result = sql_query("CREATE TABLE IF NOT EXISTS ".$NPDS_Prefix."optimy (optid INT(11) NOT NULL auto_increment, optgain DECIMAL(10,3), optdate VARCHAR (11) DEFAULT '', opthour VARCHAR (8) DEFAULT '', optcount INT(11) DEFAULT '0', PRIMARY KEY (optid))");
                // Insertion de valeurs d'initialisation de la table (si nécessaire)
                $result = sql_query("SELECT optid FROM ".$NPDS_Prefix."optimy");
                list($idopt) = sql_fetch_row($result);
                if(!$idopt OR ($idopt == ""))
                {
                        $result = sql_query("INSERT INTO ".$NPDS_Prefix."optimy (optid, optgain, optdate, opthour, optcount) VALUES ('1', '', '', '', '0')");
                }
                // Extraction de la date et de l'heure de la précédente optimisation
                $result = sql_query("SELECT optdate, opthour FROM ".$NPDS_Prefix."optimy WHERE optid='1'");
                list($dateopt, $houropt) = sql_fetch_row($result);
                if (!$dateopt OR ($dateopt == "") OR !$houropt OR ($houropt == "")) {
                } else {
                   echo "&nbsp;".adm_translate("Dernière optimisation effectuée le ")." : ".$dateopt." ".adm_translate(" à ")." ".$houropt."<br /><br />\n";
                }
                echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n"
                     ."<tr>\n"
                     ."<td class=\"ongl\" width=\"25%\" align=\"center\">".adm_translate("Table")."</td>\n"
                     ."<td class=\"ongl\" width=\"25%\" align=\"center\">".adm_translate("Taille actuelle")."</td>\n"
                     ."<td class=\"ongl\" width=\"25%\" align=\"center\">".adm_translate("Etat")."</td>\n"
                     ."<td class=\"ongl\" width=\"25%\" align=\"center\">".adm_translate("Gain réalisable")."</td>\n"
                     ."</tr>\n";
                $tot_data = 0;
                $tot_idx = 0;
                $tot_all = 0;
                $result = sql_query("SHOW TABLE STATUS FROM $dbname");
                if(sql_num_rows($result))
                {
                        while($row = sql_fetch_assoc($result))
                        {
                                $rowcolor = tablos();
                                $tot_data = $row['Data_length'];
                                $tot_idx  = $row['Index_length'];
                                $total = ($tot_data + $tot_idx);
                                $total = ($total / 1024);
                                $total = round($total,3);
                                $gain = $row['Data_free'];
                                $gain = ($gain / 1024);
                                $total_gain += $gain;
                                $gain = round($gain,3);
                                $resultat = sql_query("OPTIMIZE TABLE ".$row['Name']." ");
                                if ($gain == 0) {
                                        echo "<tr $rowcolor>\n"
                                                ."<td width=\"25%\" align=\"right\">".$row['Name']."</td>\n"
                                                ."<td width=\"25%\" align=\"right\">".$total." Ko</td>\n"
                                                ."<td width=\"25%\" align=\"center\">".adm_translate("optimisée")."</td>\n"
                                                ."<td width=\"25%\" align=\"center\"> -- </td>\n"
                                                ."</tr>\n";
                                } else {
                                        echo "<tr $rowcolor>\n"
                                                ."<td width=\"25%\" align=\"right\">".$row['Name']."</td>\n"
                                                ."<td width=\"25%\" align=\"right\">".$total." Ko</td>\n"
                                                ."<td class=\"rouge\" width=\"25%\" align=\"center\">".adm_translate("non optimisée")."</td>\n"
                                                ."<td width=\"25%\" align=\"right\">".$gain." Ko</td>\n"
                                                ."</tr>\n";
                                }
                        }
                }
                echo "</table>\n";
                $total_gain = round($total_gain,3);
                echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n"
                     ."<tr>\n"
                     ."<td width=\"25%\" class=\"ongl\">&nbsp;</td>\n"
                     ."<td width=\"25%\" class=\"ongl\">&nbsp;</td>\n"
                     ."<td width=\"25%\" class=\"ongl\" align=\"right\"><br />".adm_translate("Gain total réalisé")." : </td>\n"
                     ."<td width=\"25%\" class=\"ongl\" align=\"center\"><br />".$total_gain." Ko</td>\n"
                     ."</tr>\n"
                     ."</table><br />\n";
        /* --------------------------- Historique des gains ------------------------------ */
        // Extraction du nombre d'optimisation effectuée
                $result = sql_query("SELECT optgain, optcount FROM ".$NPDS_Prefix."optimy WHERE optid='1'");
                list($gainopt, $countopt) = sql_fetch_row($result);
                $newgain = ($gainopt + $total_gain);
                $newcount = ($countopt + 1);
        // Enregistrement du nouveau gain
                $result = sql_query("UPDATE ".$NPDS_Prefix."optimy SET optgain='$newgain', optdate='$date_opt', opthour='$heure_opt', optcount='$newcount' WHERE optid='1'");
        // Lecture des gains précédents et addition
                $result = sql_query("SELECT optgain, optcount FROM ".$NPDS_Prefix."optimy WHERE optid='1'");
                list($gainopt, $countopt) = sql_fetch_row($result);
        // Affichage
                echo "<p align=\"center\">".adm_translate("A ce jour, vous avez effectué ")." ".$countopt." optimisation(s) ".adm_translate(" et réalisé un gain global de ")." ".$gainopt." Ko.</p>\n";
        closetable();
        include("footer.php");
}
?>