<?PHP
/************************************************************************/
/* DUNE by NPDS                                                         */
/* ===========================                                          */
/*                                                                      */
/* Session and log Viewer Copyright (c) 2008 - Tribal-Dolphin           */
/*                                                                      */
/* 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.       */
/************************************************************************/
if (!function_exists("Access_Error")) { die(""); }
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();
}
global $language, $ModPath, $ModStart;
$old_language=$language;
include_once("modules/upload/upload.conf.php");
if ($DOCUMENTROOT=="") {
   global $DOCUMENT_ROOT;
   if ($DOCUMENT_ROOT) {
      $DOCUMENTROOT=$DOCUMENT_ROOT;
   } else {
      $DOCUMENTROOT=$_SERVER['DOCUMENT_ROOT'];
   }
}
$FileSecure = $DOCUMENTROOT.$racine."/logs/security.log";
$FileUpload = $DOCUMENTROOT.$rep_log;
$RepTempFil = $DOCUMENT_ROOT.$rep_cache;

$language=$old_language;
include ("modules/$ModPath/lang/session-log-$language.php");
$ThisFile="admin.php?op=Extend-Admin-SubModule&amp;ModPath=".$ModPath."&amp;ModStart=".$ModStart;

function action_log($ThisFile,$classe) {
   echo "<p align=\"center\"><a href=\"".$ThisFile."&amp;subop=vidlog&amp;log=$classe\" class=\"noir\">".SessionLog_translate("Vider le fichier")."</a> | ";
   echo "<a href=\"".$ThisFile."&amp;subop=mailog&amp;log=$classe\" class=\"noir\">".SessionLog_translate("Recevoir le fichier par mail")."</a> | ";
   echo "<a href=\"".$ThisFile."&amp;subop=vidtemp\" class=\"noir\">".SessionLog_translate("Effacer les fichiers temporaires")."</a></p>";
}

opentable();
   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"header\" width=\"30%\">\n";
   echo SessionLog_translate("Gestion des Logs");
   echo "</td><td class=\"header\">";
   echo "&nbsp;<a href=\"".$ThisFile."&subop=session\" class=\"box\">".SessionLog_translate("Liste des Sessions")."</a> | ";
   echo "<a href=\"".$ThisFile."&subop=security\" class=\"box\">".SessionLog_translate("Liste des Logs")." : ".SessionLog_translate("SECURITE")."</a>";
   if ($FileUpload!=$FileSecure) {
      echo " | <a href=\"".$ThisFile."&subop=upload\" class=\"box\">".SessionLog_translate("Liste des Logs")." : ".SessionLog_translate("TELECHARGEMENT")."</a>";
   }
   echo "</td></tr></table>\n";
   echo "<br />\n";

   // Voir les sessions
   if ($subop=="session") {
      echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"header\">\n";
      echo SessionLog_translate("Liste des Sessions");
      echo "</td></tr></table>\n";
      echo "<br />\n";
      echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\">";
      echo "<tr class=\"header\"><td width=\"30%\">".SessionLog_translate("Nom")."</td><td width=\"5%\">".SessionLog_translate("@ IP")."</td><td>".SessionLog_translate("@ IP résolue")."</td></tr>";
      $result=sql_query("SELECT username, host_addr, guest FROM ".$NPDS_Prefix."session");
      while (list($username, $host_addr, $guest)=sql_fetch_row($result)) {
         $rowcolor=tablos();
         if ($username==$host_addr) {global $anonymous; $username=$anonymous;}
         echo "<tr $rowcolor><td>".$username."</td><td>".$host_addr."</td><td>".gethostbyaddr($host_addr)."</td></tr>";
      }
      echo "</table>\n";
   }

   // Détails @IP
   if ($subop=="info") {
      echo "<h3><p align=\"center\">".SessionLog_translate("Informations sur l'IP")." <i>".$theip."</i></p></h3>";
      $hostname = gethostbyaddr($theip);
      if ($theip != $hostname) {
         $domfai = split("[.]",$hostname);
         $prov = $domfai[count($domfai)-2].'.'.$domfai[count($domfai)-1];
         if ($prov == "co.jp" or $prov == "co.uk" ) {
            $provider = $domfai[sizeof($domfai)-3].".".$prov;
         } else {
            $provider = $prov;
         }
      } else {
         $hostname = "";
      }
      echo "<p align=\"center\">";
      OpenTable2();
      echo SessionLog_translate("@ IP")."</td>";
      echo "<td align=\"center\">".SessionLog_translate("@ IP résolue")."</td>";
      echo "<td align=\"center\">".SessionLog_translate("Fournisseur")."</td>";
      $rowcolor=tablos();
      echo "</tr><tr $rowcolor>";
      echo "<td align=\"center\">".$theip."</td>";
      echo "<td align=\"center\">".$hostname."</td>";
      echo "<td align=\"center\">".$provider;
      CloseTable2();
      $subop="security";
      echo "</p><br />";
   }

   // Vider les Logs
   if ($subop=="vidlog") {
      if ($log=="security") {
         if (file_exists($FileSecure)) {
            @fopen($FileSecure, "w");
            @fclose($FileSecure);
         }
      }
      if ($log=="upload") {
         if (file_exists($FileUpload)) {
            @fopen($FileUpload, "w");
            @fclose($FileUpload);
         }
      }
   }

   // Email du contenu des Logs
   if ($subop=="mailog") {
      if ($log=="security") {
         if (file_exists($FileSecure)) {
            $fd = fopen($FileSecure, "r");
            while (!feof ($fd)) {
               $Mylog .= fgets($fd, 4096);
            }
            @fclose($FileSecure);
         }
      }
      if ($log=="upload") {
         if (file_exists($FileUpload)) {
            $fd = fopen($FileUpload, "r");
            while (!feof ($fd)) {
               $Mylog .= fgets($fd, 4096);
            }
            @fclose($FileUpload);
         }
      }
      $subject = SessionLog_translate("Fichier de Log de")." ".$sitename;
      send_email($adminmail, $subject, $Mylog, "", true, "text");
   }

   // Vider le répertoire temporaire
   if ($subop=="vidtemp") {
      if (is_dir($RepTempFil)) {
         $dh = opendir($RepTempFil);
         $i = 0;
         while($filename = readdir($dh)) {
            if ($filename === '.' OR $filename === '..' OR $filename === 'index.html') continue;
            @unlink($RepTempFil.$filename);
         }
      }
   }

   // Voir le contenu du fichier security.log
   if ($subop=="security") {
      if (file_exists($FileSecure)) {
         if (filesize($FileSecure) != 0) {
            $fd = fopen($FileSecure, "r");
            while (!feof ($fd)) {
               $rowcolor=tablos();
               $buffer = fgets($fd, 4096);
               if (strlen($buffer)>10) {
                  if (eregi("Upload", $buffer)) {
                     $UpLog.="<tr $rowcolor><td>".$buffer."</td></tr>";
                  } else {
                    $ip=substr(strrchr($buffer,"=>"),2);
                    $SecLog.="<tr $rowcolor><td>".$buffer."</td><td><a href=\"".$ThisFile."&amp;subop=info&amp;theip=".$ip."\" class=\"noir\">".SessionLog_translate("Infos")."</a></td></tr>";
                  }
               }
            }
            fclose($fd);
         }
      }
      echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"header\">\n";
      echo SessionLog_translate("Liste des Logs")." ".SessionLog_translate("SECURITE")." : <i>security.log</i>";
      echo "</td></tr></table>\n";
      opentable();
      echo $SecLog;
      closetable();
      echo "<br /><br />";
      echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"header\">\n";
      echo SessionLog_translate("Liste des Logs")." ".SessionLog_translate("TELECHARGEMENT")." : <i>security.log</i>";
      echo "</td></tr></table>\n";
      opentable();
      echo $UpLog;
      closetable();
      action_log($ThisFile,"security");
   }

   // Voir le contenu du fichier d'upload si différent de security.log (upload.conf.php)
   if ($subop=="upload") {
      if ($FileUpload!=$FileSecure) {
         if (file_exists($FileUpload)) {
            if (filesize($FileUpload) != 0) {
               $fd = fopen($FileUpload, "r");
               while (!feof ($fd)) {
                  $rowcolor=tablos();
                  $buffer = fgets($fd, 4096);
                  $UpLog.="<tr $rowcolor><td>".$buffer."</td></tr>";
               }
               fclose($fd);
            }
         }
         echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"header\">\n";
         echo SessionLog_translate("Liste des Logs")." ".SessionLog_translate("TELECHARGEMENT")." : <i>$FileUpload</i>";
         echo "</td></tr></table>\n";
         opentable();
         echo $UpLog;
         closetable();
         action_log($ThisFile,"upload");
      }
   }
closetable();
?>