<?PHP
/****************************************************************************************/
/* NPDS V - OptimySQL_Databases 0.2a (20025092) .                                       */
/* based on Optimize_database addon by Xavier JULIE (webmaster@securite-internet.org    */
/* M. PASCAL aKa EBH (plan.net@free.fr)                                                 */
/*                                                                                      */
/* 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.                                                     */
/*                                                                                      */
/* ver 0.2a : HTML/CSS (en attente de CLASS admin), suppression de variables (SQL)      */
/*                                                                                      */
/* ver 0.2 : intégration des traductions aux fichiers de langues de NPDS                */
/*                                                                                      */
/* ver 0.1 : adaptation de Optimize_database                                            */
/*                                                                                      */
/****************************************************************************************/
function Access_Error () {
  if (file_exists("meta/meta.php"))
     include ("meta/meta.php");
  echo "<title>NPDS</title></head><body bgcolor=\"white\"><br /><p align=\"center\"><span style=\"font-size: 14px; font-family: Arial; font-weight: bold; color: red;\">";
  echo "Access Denied / Acc&egrave;s Refus&eacute;";
  if (cur_charset=="utf-8") echo" / &#x901A;&#x5165;&#x88AB;&#x5426;&#x8BA4;";
  echo "</span>";
  echo "<br /><br /><span style=\"font-size: 12px; font-family: Arial; font-weight: bold; color: black;\">NPDS - Portal System";
  echo "</span></p></body></html>";
  die();
}
if (!eregi("admin.php", $PHP_SELF)) { Access_Error(); }
global $pdst; $pdst = "0";

$result = mysql_query("SELECT radminsuper FROM authors WHERE aid='$aid'");
list($radminsuper) = mysql_fetch_row($result);
if ($radminsuper != 1) {
   Access_Error();
} else {
        global $language;
        switch($language)
        {
                case english :
                        $date_opt = date("M d Y");
                        $heure_opt = date("h:i a");
                        break;
                default :
                        $date_opt = date("d/m/Y");
                        $heure_opt = date("H:i");
                        break;
        }
        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 = mysql_query("CREATE TABLE IF NOT EXISTS 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 = mysql_query("SELECT optid FROM optimy");
                list($idopt) = mysql_fetch_row($result);
                if(!$idopt OR ($idopt == ""))
                {
                        $result = mysql_query("INSERT INTO optimy (optid, optgain, optdate, opthour, optcount) VALUES ('1', '', '', '', '0')");
                }
                // Extraction de la date et de l'heure de la précédente optimisation
                $result = mysql_query("SELECT optdate, opthour FROM optimy WHERE optid='1'");
                list($dateopt, $houropt) = mysql_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 = mysql_query("SHOW TABLE STATUS FROM ".$dbname." ");
                if(mysql_num_rows($result))
                {
                        while($row = mysql_fetch_array($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 = mysql_query("OPTIMIZE TABLE ".$row[0]." ");
                                if ($gain == 0) {
                                        echo "<tr $rowcolor>\n"
                                                ."<td width=\"25%\" align=\"right\">".$row[0]."</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[0]."</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 = mysql_query("SELECT optgain, optcount FROM optimy WHERE optid='1'");
                list($gainopt, $countopt) = mysql_fetch_row($result);
                $newgain = ($gainopt + $total_gain);
                $newcount = ($countopt + 1);
        // Enregistrement du nouveau gain
                $result = mysql_query("UPDATE optimy SET optgain='$newgain', optdate='$date_opt', opthour='$heure_opt', optcount='$newcount' WHERE optid='1'");
        // Lecture des gains précédents et addition
                $result = mysql_query("SELECT optgain, optcount FROM optimy WHERE optid='1'");
                list($gainopt, $countopt) = mysql_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");
}
?>
