<?php
/**
 * $Source: /home/cvs/nukescripts/kernel/secureadmin.php,v $
 * $Revision: 1.1.0a $
 * $Author: cyril $
 * $Date: 2006/06/19 21:03:12 $
 *
 */
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* 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.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
if (!defined('ADMIN_MAX')) {
	die ("Access Denied");
}
if (is_file('kernel/nsn.php')) {
	include_once('kernel/nsn.php');
}
include_once ('./mainfile.php');
global  $cookie;

$logs_ipadmin2 = getenv("REMOTE_ADDR");
$logs_hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$logs_ipadmin  = getenv("REMOTE_ADDR");
$userinfo      = getusrinfo($user);
$xusername     = stripslashes($userinfo['username']);

$sqlIPsession = "SELECT `uname`, `time`, `host_addr` FROM `".TABLE_SESSION."` WHERE `uname`='$xusername'";
if ( !($rawIPsession = $GLOBALS['db']->sql_query($sqlIPsession)) ){
	query_die(GENERAL_ERROR, 'Could not obtain Admin session WebSite', '', __LINE__, __FILE__, $sqlIPsession);
}

$IPsession = $GLOBALS['db']->sql_fetchrow($rawIPsession);
$IPsession = stripslashes($IPsession['host_addr']);

$sqlgraf = "SELECT `active_journal`, `active_mail`, `graphic_admin`, `messagesa`, `imagesa`, `ip_fixe`, `active_ban`, `num_ban` from `".TABLE_NSNSECUREADMIN_CONFIG."` ";
if ( !($graf = $GLOBALS['db']->sql_query($sqlgraf)) ){
	query_die(GENERAL_ERROR, 'Could not obtain Admin nsnsecure_config WebSite', '', __LINE__, __FILE__, $sqlgraf);
}

while ($ror = $GLOBALS['db']->sql_fetchrow($graf)) {
$active_journal = $GLOBALS['db']->XMFbin($ror['active_journal']);
$active_mail    = $GLOBALS['db']->XMFbin($ror['active_mail']);
$grafic_admin   = $GLOBALS['db']->XMFbin($ror['graphic_admin']);
$messagesa      = stripslashes($ror['messagesa']);
$imagesa        = stripslashes($ror['imagesa']);
$ip_fixe        = $GLOBALS['db']->XMFbin($ror['ip_fixe']);
$active_ban     = $GLOBALS['db']->XMFbin($ror['active_ban']);
$num_ban        = $GLOBALS['db']->XMFnum($ror['num_ban']);}

if ($active_ban) {
	$sqltestIP = "SELECT `idlogs`,`logs_ip`,`logs_hostname`,`logs_date`,`logs_time`,`logs_name`,`logs_agent`,`logs_os`,`logs_ban` FROM `".TABLE_NSNSECUREADMIN_LOGS."`";
if ( !($testIP = $GLOBALS['db']->sql_query($sqltestIP)) ){
	query_die(GENERAL_ERROR, 'Could not obtain Admin nsnsecure_logs WebSite', '', __LINE__, __FILE__, $sqltestIP);
}

while ($ror2 = $GLOBALS['db']->sql_fetchrow($testIP)) {
$idlogs        = $GLOBALS['db']->XMFnum($ror2['idlogs']);
$logs_ip       = stripslashes($ror2['logs_ip']);
$logs_hostname = stripslashes($ror2['logs_hostname']);
$logs_date     = stripslashes($ror2['logs_date']);
$logs_time     = stripslashes($ror2['logs_time']);
$logs_name     = stripslashes($ror2['logs_name']);
$logs_agent    = stripslashes($ror2['logs_agent']);
$logs_os       = stripslashes($ror2['logs_os']);
$logs_ban      = $GLOBALS['db']->XMFnum($ror2['logs_ban']);
$bantime       = time();

if ( defined('USER_ALLOW') and ($logs_ipadmin == $logs_ip) AND ($logs_ban < $num_ban)){
	$logs_ban++;
	$update = $GLOBALS['db']->sql_query("UPDATE `".TABLE_NSNSECUREADMIN_LOGS."` SET `logs_ip`='$logs_ip', `logs_hostname`='$logs_hostname',`logs_date`='$logs_date',`logs_time`='$logs_time',logs_name='$logs_name',`logs_agent`='$logs_agent',`logs_os`='$logs_os',`logs_ban`='$logs_ban' WHERE `logs_ip`='$logs_ipadmin'");
}

if ( ($logs_ipadmin == $logs_ip) AND ($logs_ban < $num_ban) AND ($IPsession != $logs_ipadmin2)){
	$logs_ban++;
	$update = $GLOBALS['db']->sql_query("UPDATE `".TABLE_NSNSECUREADMIN_LOGS."` SET `logs_ip`='$logs_ip', `logs_hostname`='$logs_hostname',`logs_date`='$logs_date',`logs_time`='$logs_time',`logs_name`='Anonymous',`logs_agent`='$logs_agent',`logs_os`='$logs_os',`logs_ban`='$logs_ban' WHERE `logs_ip`='$logs_ipadmin'");
}

if (($logs_ipadmin == $logs_ip) AND ($logs_ban >= $num_ban)) {
	$result = $GLOBALS['db']->sql_query("INSERT INTO `".TABLE_NSNST_BLOCKED_IPS."` VALUES ('$logs_ip','$userinfo[user_id]','$userinfo[username]','Exclusion NSN Secure Admin',$bantime,'Exclusion NSN Secure Admin','10','admin.php','','$logs_ip','$logs_ip','','','')");
}

$sqlgetIP = "SELECT `ip_addr` FROM `".TABLE_NSNST_BLOCKED_IPS."`";
if ( !($getIP = $GLOBALS['db']->sql_query($sqlgetIP)) )
{ query_die(GENERAL_ERROR, 'Could not obtain Admin nsnst_blocked_ips WebSite', '', __LINE__, __FILE__, $sqltestIP);}
while($rip = $GLOBALS['db']->sql_fetchrow($getIP)) {
$ip_addr = stripslashes($rip['$ip_addr']);

	if ($logs_ipadmin == $ip_addr) {
		redirect_url("index.php");
	}

	}
}

$sqlnsnsec = "SELECT `name`, `ipfixe`, `host` FROM `".TABLE_NSNSECUREADMIN."` WHERE `name`='$xusername'";
if ( !($nsnsec = $GLOBALS['db']->sql_query($sqlnsnsec)) )
{ query_die(GENERAL_ERROR, 'Could not obtain Admin nsnsecureadmin WebSite', '', __LINE__, __FILE__, $sqlnsnsec);}
$row           = $GLOBALS['db']->sql_fetchrow($nsnsec);
$xname_user    = stripslashes($row['name']);
$ipfixesec     = stripslashes($row['ipfixe']);
$hostsec       = stripslashes($row['host']);
$logs_hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);

if ($ipfixesec == "") {
	$logs_ipadmin = "";
}
if ($hostsec == "") {
	$logs_hostname = "";
}

if (!defined('NSN_SECURE_ADMIN')) {
	$xname_user = ""; $userinfo['username']= "" ; $logs_ipadmin2= ""; $IPsession= ""; $logs_ipadmin = ""; $row['ipfixe']= "";$logs_hostname= ""; $row['host']= "";
}

if (( $xname_user == $cookie[1])  and ($logs_ipadmin2 == $IPsession) and ($logs_ipadmin == $ipfixesec) and ($logs_hostname == $hostsec) ) {
$GLOBALS['db']->sql_query("DELETE FROM `".TABLE_NSNSECUREADMIN_LOGS."` WHERE `logs_ip`='$IPsession'");

return;

} else {
$index = 1;

incoh();
global $cookie;
$sqlat = "SELECT `active_journal`, `active_mail` FROM `".TABLE_NSNSECUREADMIN_CONFIG."` ";
if ( !($resultat = $GLOBALS['db']->sql_query($sqlat)) )
{ query_die(GENERAL_ERROR, 'Could not obtain Admin nsnsecureadmin_config WebSite', '', __LINE__, __FILE__, $sqlat);}
$logs           = $GLOBALS['db']->sql_fetchrow($resultat);
$active_journal = $GLOBALS['db']->XMFbin($logs['active_journal']);
$active_mail    = $GLOBALS['db']->XMFbin($logs['active_mail']);

if ( $active_journal == 1 ) {
include_once('includes/counter.php');
$logs_ip      = getenv('REMOTE_ADDR');
$logs_hostname= gethostbyaddr($_SERVER['REMOTE_ADDR']);
$today        = getdate();
$month        = $GLOBALS['db']->XMFnum($today['mon']);
$mday         = $GLOBALS['db']->XMFnum($today['mday']);
$year         = $GLOBALS['db']->XMFnum($today['year']);
$minutes      = $GLOBALS['db']->XMFnum($today['minutes']);
$seconds      = $GLOBALS['db']->XMFnum($today['seconds']);
$hours        = $GLOBALS['db']->XMFnum($today['hours']);
$logs_date    = "$mday-$month-$year";
$logs_time    = "$hours:$minutes:$seconds ";
$logs_name    = $cookie[1];
$logs_browser = $browser;
$logs_os      = $os;
if (!$update) {
$result = $GLOBALS['db']->sql_query("INSERT INTO `".TABLE_NSNSECUREADMIN_LOGS."` VALUES (NULL,'$logs_ip','$logs_hostname', '$logs_date','$logs_time','$logs_name','$logs_browser','$logs_os','$logs_ban')");}
echo "<center><br /><b>IP ($logs_ip), Host ($logs_hostname) , Name ($logs_name), OS ($logs_os), Agent ($logs_browser), Date ($logs_date), Time ($logs_time) .</b></center>";
}

if ( $active_mail == 1 ) {
 $msg = "$sitename\n\n";
 $msg .= "IP : $logs_ip - Host: $logs_hostname - Name : $logs_date - Os : $logs_os - Agent : $logs_browser - Date : $logs_date - Time : $logs_time";
 $to = "$adminmail";
 $subject = "Alerte Intrusion";
 $mailheaders = "From: $adminmail\n";
 $mailheaders .= "Reply-To: $adminmail\n\n";
 mail($to, $subject, $msg, $mailheaders);
}
$messagesa = stripslashes($logs['messagesa']);
echo "<center><font size=\"6\">$messagesa</font></center>";
}
$imagesa = stripslashes($logs['imagesa']);
if ($imagesa == "") {
	br('2');
	echo "<center><img src=\"images/stop.png\" alt='' />";
	br('2');
} else {
	br('2');
	echo "<center><img src=\"$imagesa\"><br /><br /><br /><img src=\"images/stop.png\" alt='' />";
	br('2');
}
echo "<meta http-equiv=\"refresh\" content=\"1;url=index.php\">"
	."<meta http-equiv=\"Pragma\" content=\"no-cache\">";
incof();
}