<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	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, or
	(at your opinion) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

if (!defined("_LOAD_PAGE_")) {
	die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Error:</b> This file cannot be opened directly!</td></tr></table>");
}

if (isset($_GET['op'])) { $op = inCode($_GET['op']); } else { $op = ""; }
if (isset($_GET['id'])) { $id = inCode($_GET['id']); } else { $id = ""; }
if (isset($_GET['ok'])) { $ok = inCode($_GET['ok']); } else { $ok = false; }

if (isset($_POST['p_ip'])) { $p_ip = inCode($_POST['p_ip']); } else { $p_ip = ""; }
if (isset($_POST['p_fip'])) { $p_fip =inCode( $_POST['p_fip']); } else { $p_fip = ""; }
if (isset($_POST['p_user'])) { $p_user = inCode($_POST['p_user']); } else { $p_user = ""; }
if (isset($_POST['p_range'])) { $p_range = inCode($_POST['p_range']); } else { $p_range = ""; }
if (isset($_POST['p_permanent'])) { $p_permanent = inCode($_POST['p_permanent']); } else { $p_permanent = ""; }
if (isset($_POST['p_date'])) { $p_date = inCode($_POST['p_date']); } else { $p_date = ""; }
if (isset($_POST['p_reason'])) { $p_reason = inCode($_POST['p_reason']); } else { $p_reason = ""; }

if (isset($_POST['ip'])) { $ip = inCode($_POST['ip']); } else { if (isset($_GET['ip'])) { $ip = inCode($_GET['ip']); } else { $ip = ""; } }

if (isset($_GET['pg'])) { $pg = inCode($_GET['pg']); } else { $pg = 1; }
$ofsppg = 50; //Items per page
$ofsbgn = ($pg*$ofsppg)-$ofsppg;

function bannedList($ofsbgn,$ofsppg,$pg) {
	global $dblink,$siteConfig;
	
	$n = 0;
	echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
	echo "<thead>\n";
	echo "<tr><td>"._IP_."</td><td>"._USER_."</td><td width='10%' align='center'>"._REASON_."</td><td width='20%'>"._AUTHOR_."</td><td width='20%'>"._BANUNTIL_."</td><td width='1%'>&nbsp;</td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
	if ($result = $dblink->get_list("SELECT b.*,DATE_FORMAT(b.date, '".$siteConfig['timestamp']."') as date,DATE_FORMAT(b.bandate, '".$siteConfig['timestamp']."') as bandate,u.id AS uid FROM memht_banned AS b LEFT JOIN memht_utenti AS u ON b.user=u.user ORDER BY b.id DESC LIMIT $ofsbgn,$ofsppg")) {
		foreach ($result as $row) {
			$b_id = intval($row['id']);
			$uid = intval($row['uid']);
			$b_ip = num2ip($row['ip']);
			$b_fip = num2ip($row['fip']);
			$b_user = outCode($row['user']);
			$b_range = intval($row['range']);
			$b_permanent = intval($row['permanent']);
			$b_date = $row['date'];
			$b_reason = outCode($row['reason']);
			$b_author = outCode($row['author']);
			$b_bandate = $row['bandate'];
			
			if ($b_permanent==1) { $b_date = _PERMANENT_; }
			$f_ip = ($b_range==1) ? $b_ip." - ".$b_fip : $b_ip ;
			
			$class = (($n++%2)!=0) ? "hlight" : "clean" ;
			
			$info = ($b_reason!="") ? "<img src='images/note.gif' alt='Note' border='0' title=\"header=["._REASON_."] body=[$b_reason]\">" : "" ;
			$usertxt = ($uid>0) ? "<a href='index.php?page=users&op=userInfo&uid=$uid' title='$b_user'>$b_user</a>" : "" ;
			
			echo "<tr><td class='$class'>".ipcountry2img($b_ip)." <b>$f_ip</b></td><td class='$class'>{$usertxt}</td><td class='$class' align='center'>$info</td><td class='$class'>$b_author</td><td class='$class'>$b_date</td><td class='$class' nowrap><a href='admin.php?page=security&op=editBan&id=$b_id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=security&op=deleteBan&id=$b_id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></td></tr>\n";
		}
	} else {
		echo "<tr><td colspan='6' align='center' id='errorText' class='clean'><b>"._NOIP_."</b></td></tr>";
	}
	echo "</tbody>\n";
	echo "</table>";
	
	//Pages
	include_once("inc/class/paginationSystem.class.php");
	$ps = new paginationSystem();
	$ps->items = $ofsppg;
	$ps->actpg = $pg;
	$ps->query = "SELECT id FROM memht_banned";
	$ps->url = "admin.php?page=security&pg={{N}}";
	$ps->show();
}

function add($p_ip,$p_fip,$p_user,$p_range,$p_permanent,$p_date,$p_reason,$ok=false) {
	global $dblink,$admin,$tzNOW;

	if (isset($_GET['ip'])) { $g_ip = inCode($_GET['ip']); } else { $g_ip = ""; }
	if (isset($_GET['user'])) { $g_user = inCode($_GET['user']); } else { $g_user = ""; }
	
	?>
	<link rel="stylesheet" href="inc/javascript/jquery/plugins/datepicker/flora.datepicker.css" type="text/css">
	<script type="text/javascript" src="inc/javascript/jquery/plugins/datepicker/ui.datepicker.js"></script>

	<script type="text/javascript">
		$(document).ready(function(){
			$('#selectdate').datepicker();
		});
	</script>
	<?php
	
	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form_banip' method='post' action='admin.php?page=security&op=banip&ok=true'>";
			echo "<tr><td><b>"._IPADDRESS_."</b></td><td><input type='text' name='p_ip' size='15' maxlength='15' value='$g_ip'> - <input type='text' name='p_fip' size='15' maxlength='15'> <span id='info'>"._ONLYIFRANGE_."</span></td></tr>\n";
			echo "<tr><td><b>"._USERNAME_."</b></td><td><input type='text' name='p_user' size='20' maxlength='20' value='$g_user'></td></tr>\n";
			echo "<tr><td><b>"._IP_RANGE_."</b></td><td>";
				echo "<select name='p_range'>\n";
					echo "<option value='0' selected>"._SINGLEIP_."</option>\n";
					echo "<option value='1'>"._RANGE_."</option>\n";
				echo "</select></td></tr>\n";
			
			echo "<tr><td><b>"._PERMANENT_."</b></td><td>";
				echo "<select name='p_permanent'>\n";
					echo "<option value='1' selected>"._YES_."</option>\n";
					echo "<option value='0'>"._NO_."</option>\n";
				echo "</select>\n";				
			echo "</td></tr>";
			echo "<tr><td><b>"._BANUNTIL_."</b></td><td><input type='text' name='p_date' size='10' maxlength='16' id='selectdate'> <span id='info'>"._ONLYIFNOTPERMANENT_."</span>";
			
		echo "</td></tr>\n";
			
		echo "<tr><td valign='top'><b>"._REASON_."</b></td><td>";
			textarea("p_reason","100%","200px",1,"fulladmin");
		echo "</td></tr><tr><td colspan='2'><input type='submit' name='Submit' value='"._ADD_."'></td></tr>\n";
		
		echo "</form>\n";
		echo "</table>\n";
	} else {		
		$save = true;
		if ($p_ip=="") { $save = false; }
		if ($p_range==1 AND $p_fip=="") { $save = false; }
		if ($p_permanent==0 AND $p_date=="") { $save = false; }
	
		if ($save) {
			$dblink->query("INSERT INTO memht_banned (id,ip,fip,user,range,permanent,date,reason,author,bandate)
							VALUES (null,'".ip2num($p_ip)."','".ip2num($p_fip)."','$p_user','$p_range','$p_permanent','$p_date','$p_reason','$admin',$tzNOW)");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=security'>";
		} else {
			echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
		}
	}
}

function edit($id,$p_ip,$p_fip,$p_user,$p_range,$p_permanent,$p_date,$p_reason,$ok=false) {
	global $dblink,$siteConfig;

	$id = intval($id);
	
	?>
	<link rel="stylesheet" href="inc/javascript/jquery/plugins/datepicker/flora.datepicker.css" type="text/css">
	<script type="text/javascript" src="inc/javascript/jquery/plugins/datepicker/ui.datepicker.js"></script>

	<script type="text/javascript">
		$(document).ready(function(){
			$('#selectdate').datepicker();
		});
	</script>
	<?php

	$row = $dblink->get_row("SELECT *,DATE_FORMAT(date, '%Y-%m-%d') as date,DATE_FORMAT(bandate, '".$siteConfig['timestamp']."') as bandate FROM memht_banned WHERE id='$id'");
	$b_ip = num2ip($row['ip']);
	$b_fip = num2ip($row['fip']);
	$b_user = outCode($row['user']);
	$b_range = intval($row['range']);
	$b_permanent = intval($row['permanent']);
	$b_date = $row['date'];
	$b_reason = outCode($row['reason']);
	$b_author = outCode($row['author']);
	$b_bandate = $row['bandate'];

	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form_banip' method='post' action='admin.php?page=security&op=editBan&id=$id&ok=true'>";
			echo "<tr><td><b>"._IPADDRESS_."</b></td><td><input type='text' name='p_ip' size='15' maxlength='15' value=\"$b_ip\"> - <input type='text' name='p_fip' size='15' maxlength='15' value=\"$b_fip\"> <span id='info'>"._ONLYIFRANGE_."</span></td></tr>\n";
			echo "<tr><td><b>"._USERNAME_."</b></td><td><input type='text' name='p_user' size='20' maxlength='20' value=\"$b_user\"></td></tr>\n";
			echo "<tr><td><b>"._IP_RANGE_."</b></td><td>";
				echo "<select name='p_range'>\n";
					if ($b_range==0) {
						echo "<option value='0' selected>"._SINGLEIP_."</option>\n";
						echo "<option value='1'>"._RANGE_."</option>\n";
					} else {
						echo "<option value='0'>"._SINGLEIP_."</option>\n";
						echo "<option value='1' selected>"._RANGE_."</option>\n";
					}
				echo "</select></td></tr>\n";
			
			echo "<tr><td><b>"._PERMANENT_."</b></td><td>";
				echo "<select name='p_permanent'>\n";
					if ($b_permanent==0) {
						echo "<option value='1'>"._YES_."</option>\n";
						echo "<option value='0' selected>"._NO_."</option>\n";
					} else {
						echo "<option value='1' selected>"._YES_."</option>\n";
						echo "<option value='0'>"._NO_."</option>\n";
					}
				echo "</select>\n";				
			echo "</td></tr>";
			
			echo "<tr><td><b>"._BANUNTIL_."</b></td><td><input type='text' name='p_date' value=\"$b_date\" size='10' maxlength='16' id='selectdate'> <span id='info'>"._ONLYIFNOTPERMANENT_."</span>";			
		echo "<tr><td valign='top'><b>"._REASON_."</b></td><td>";
		
		textarea("p_reason","100%","200px",1,"fulladmin",$b_reason);
		
		echo "</td></tr><tr><td colspan='2'><input type='submit' name='Submit' value='"._MODIFY_."'></td></tr>\n";
		
		echo "</form>\n";
		echo "</table>\n";
	} else {		
		$save = true;
		if ($p_ip=="") { $save = false; }
		if ($p_range==1 AND $p_fip=="") { $save = false; }
		if ($p_permanent==0 AND $p_date=="") { $save = false; }
	
		if ($save) {
			$dblink->query("UPDATE memht_banned SET ip='".ip2num($p_ip)."',fip='".ip2num($p_fip)."',user='$p_user',range='$p_range',permanent='$p_permanent',date='$p_date',reason='$p_reason' WHERE id='$id'");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=security'>";
		} else {
			echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
		}
	}
}

function delete($id,$ok=false) {
	global $dblink;
	if ($ok) {
		$dblink->query("DELETE FROM memht_banned WHERE id='$id'");
		echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=security'>";		
	} else {
		echo "<div align='center'><b>"._SUREDELETEIP_."</b><br><a href='admin.php?page=security&op=deleteBan&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=security' title='"._NO_."'>"._NO_."</a></div>";
	}
}

function iptrack($ofsbgn,$ofsppg,$pg) {
	global $dblink,$siteConfig;
	
	echo "<div align='center' class='box'>";
			echo "<a href='admin.php?page=security&op=iptrack' title='"._HOME_."'><img src='admin/icons/table.png' border='0' title='"._HOME_."' align='top'> "._HOME_."</a>";
			echo " - <a href='admin.php?page=security&op=iptrackadmin' title='"._ADMINISTRATION_."'><img src='admin/icons/table.png' border='0' title='"._ADMINISTRATION_."' align='top'> "._ADMINISTRATION_."</a>";
	echo "</div>";
	
	$n = 0;
	echo "<table width='100%' cellspacing='1' cellpadding='0' class='std_nicetable'>\n";
	echo "<thead>\n";
	echo "<tr><td>"._IP_."</td><td>"._USER_."</td><td>"._PAGE_."</td><td>"._AGENT_."</td><td>"._REFURL_."</td><td>"._REQURI_."</td><td width='1%'>&nbsp;</td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
	$result = $dblink->get_list("SELECT l.*,DATE_FORMAT(l.date, '".$siteConfig['timestamp']."') as date2,u.id AS uid FROM memht_statistics_livedata AS l LEFT JOIN memht_utenti AS u ON l.user=u.user ORDER BY l.date DESC LIMIT $ofsbgn,$ofsppg");
	foreach ($result as $row) {
		$date = $row['date2'];
		$ip = outCode($row['ip']);
		$tuser = outCode($row['user']);
		$tpage = outCode($row['page']);
		$agent = outCode($row['agent']);
		$referer = outCode($row['referer']);
		$requesturi = outCode($row['requesturi']);
		$isbot = intval($row['isbot']);
		$cookies = intval($row['cookies']);
		$uid = intval($row['uid']);
		
		$agent = str_replace("[","{",$agent);
		$agent = str_replace("]","}",$agent);
		
		$tban = ($tuser=="") ? "admin.php?page=security&op=banip&ip=$ip" : "admin.php?page=security&op=banip&ip=$ip&user=$tuser" ;
		$tuser = ($uid>0) ? "<a href='index.php?page=users&op=userInfo&uid=$uid' title='$tuser'>$tuser</a>" : "-" ;
		$tagent = ($agent!="") ? "<a title=\"header=["._AGENT_."] body=[$agent]\">".trimString($agent,20,1)."</a>" : "-" ;
		$treferer = ($referer!="") ? "<a href=\"$referer\" title=\"header=["._REFURL_."] body=[$referer]\" target='_blank'>".trimString($referer,20,1)."</a>" : "-" ;
		$trequesturi = ($requesturi!="") ? "<a href=\"".$siteConfig['site_url']."$requesturi\" title=\"header=["._REQURI_."] body=[$requesturi]\" target='_blank'>".trimString($requesturi,20,1)."</a>" : "-" ;
		$tcookies = ($cookies==1) ? "cookies_on.png" : "cookies_off.png" ;
		
		$class = (($n++%2)!=0) ? "hlight" : "clean" ;
		if ($isbot==1) { $class = "warning"; }
		if (banned($ip)) { $class = "banned"; }
		
		echo "<tr>";
			echo "<td class='$class' width='15%' nowrap>".ipcountry2img($ip)." <a href='admin.php?page=security&op=findip&ok=true&ip=$ip' title='Whois $ip'><b>".getIpText($ip)."</b></a></td>";
			echo "<td class='$class' width='10%' nowrap><b>$tuser</b></td>";
			echo "<td class='$class' width='10%' nowrap>$tpage</td>";
			echo "<td class='$class' width='25%'>$tagent</td>";
			echo "<td class='$class' width='20%'>$treferer</td>";
			echo "<td class='$class' width='20%'>$trequesturi</td>";
			echo "<td class='$class' width='1%' nowrap><img src='images/{$tcookies}' alt='Cookies' title='Cookies'> <img src='images/date.png' alt='"._DATE_."' title='header=["._DATE_."] body=[$date]'> <a href='$tban' title='"._BAN_." $ip'><img src='images/user_red.gif' border='0'></a></td>";
		echo "</tr>\n";
	}
	echo "</tbody>\n";
	echo "</table>\n";
	
	//Pages
	include_once("inc/class/paginationSystem.class.php");
	$ps = new paginationSystem();
	$ps->items = $ofsppg;
	$ps->actpg = $pg;
	$ps->query = "SELECT id FROM memht_statistics_livedata";
	$ps->url = "admin.php?page=security&op=iptrack&pg={{N}}";
	$ps->show();
}

function iptrackAdmin($ofsbgn,$ofsppg,$pg) {
	global $dblink,$siteConfig;
	
	echo "<div align='center' class='box'>";
			echo "<a href='admin.php?page=security&op=iptrack' title='"._HOME_."'><img src='admin/icons/table.png' border='0' title='"._HOME_."' align='top'> "._HOME_."</a>";
			echo " - <a href='admin.php?page=security&op=iptrackadmin' title='"._ADMINISTRATION_."'><img src='admin/icons/table.png' border='0' title='"._ADMINISTRATION_."' align='top'> "._ADMINISTRATION_."</a>";
	echo "</div>";
	
	$n = 0;
	echo "<table width='100%' cellspacing='1' cellpadding='0' class='std_nicetable'>\n";
	echo "<thead>\n";
	echo "<tr><td>"._IP_."</td><td>"._USER_."</td><td>"._PAGE_."</td><td>"._AGENT_."</td><td>"._REFURL_."</td><td>"._REQURI_."</td><td width='1%'>&nbsp;</td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
	$result = $dblink->get_list("SELECT *,DATE_FORMAT(date, '".$siteConfig['timestamp']."') as date2 FROM memht_statistics_administration ORDER BY date DESC LIMIT $ofsbgn,$ofsppg");
	foreach ($result as $row) {
		$date = $row['date2'];
		$ip = outCode($row['ip']);
		$tadmin = outCode($row['admin']);
		$tpage = outCode($row['page']);
		$agent = outCode($row['agent']);
		$referer = outCode($row['referer']);
		$requesturi = outCode($row['requesturi']);
		
		$agent = str_replace("[","{",$agent);
		$agent = str_replace("]","}",$agent);
		
		$tban = ($tadmin=="") ? "admin.php?page=security&op=banip&ip=$ip" : "admin.php?page=security&op=banip&ip=$ip&user=$tadmin" ;
		$tagent = ($agent!="") ? "<a title=\"header=["._AGENT_."] body=[$agent]\">".trimString($agent,20,1)."</a>" : "-" ;
		$treferer = ($referer!="") ? "<a href=\"$referer\" title=\"header=["._REFURL_."] body=[$referer]\" target='_blank'>".trimString($referer,20,1)."</a>" : "-" ;
		$trequesturi = ($requesturi!="") ? "<a href=\"".$siteConfig['site_url']."$requesturi\" title=\"header=["._REQURI_."] body=[$requesturi]\" target='_blank'>".trimString($requesturi,20,1)."</a>" : "-" ;
		
		$class = (($n++%2)!=0) ? "hlight" : "clean" ;
		if (banned($ip)) { $class = "banned"; }
		
		echo "<tr>";
			echo "<td class='$class' width='15%' nowrap>".ipcountry2img($ip)." <a href='admin.php?page=security&op=findip&ok=true&ip=$ip' title='Whois $ip'><b>$ip</b></a></td>";
			echo "<td class='$class' width='10%' nowrap><b>$tadmin</b></td>";
			echo "<td class='$class' width='10%' nowrap>$tpage</td>";
			echo "<td class='$class' width='25%'>$tagent</td>";
			echo "<td class='$class' width='20%'>$treferer</td>";
			echo "<td class='$class' width='20%'>$trequesturi</td>";
			echo "<td class='$class' width='1%' nowrap><img src='images/date.png' alt='"._DATE_."' title='header=["._DATE_."] body=[$date]'> <a href='$tban' title='"._BAN_." $ip'><img src='images/user_red.gif' border='0'></a></td>";
		echo "</tr>\n";
	}
	echo "</tbody>\n";
	echo "</table>\n";
	
	//Pages
	include_once("inc/class/paginationSystem.class.php");
	$ps = new paginationSystem();
	$ps->items = $ofsppg;
	$ps->actpg = $pg;
	$ps->query = "SELECT id FROM memht_statistics_administration";
	$ps->url = "admin.php?page=security&op=iptrackadmin&pg={{N}}";
	$ps->show();
}

function getIpText($val) {
	global $dblink;
	
	if ($row = $dblink->get_row("SELECT spidname FROM memht_spider_ranges WHERE '".ip2num($val)."' BETWEEN fromip AND toip")) {
		return "<span style='color:#999999;'>".outCode($row['spidname'])." spider</span>";
	} else {
		return "$val";
	}
}


function findip($ip,$ok=false) {
	global $dblink,$siteConfig;
	
	$ip = trim($ip);
	
	if (!$ok) {
		echo "<table align='center'>";	
			echo "<form name='admin_cfg' method='post' action='admin.php?page=security&op=findip&ok=true'>\n";
			echo "<tr><td><input type='text' name='ip' size='20' maxlength='20'></td><td><input type='submit' name='Submit' value='"._FINDIP_."'></td></tr>\n";
			echo "</form>\n";
		echo "</table>";
	} else {
		if (validIp($ip)) {
			$found = false;
			if ($result = $dblink->get_list("SELECT l.*,DATE_FORMAT(l.date, '".$siteConfig['timestamp']."') as date2,u.id AS uid FROM memht_statistics_livedata AS l LEFT JOIN memht_utenti AS u ON l.user=u.user WHERE l.ip='$ip' ORDER BY l.date DESC LIMIT 200")) {
				$found = true;
				$n = 0;
				echo "<table width='100%' cellspacing='1' cellpadding='0' class='std_nicetable'>\n";
				echo "<thead>\n";
				echo "<tr><td>"._IP_."</td><td>"._USER_."</td><td>"._PAGE_."</td><td>"._AGENT_."</td><td>"._REFURL_."</td><td>"._REQURI_."</td><td width='1%'>&nbsp;</td></tr>\n";
				echo "</thead>\n";
				echo "<tbody>\n";
				foreach ($result as $row) {
					$date = $row['date2'];
					$ip = outCode($row['ip']);
					$tuser = outCode($row['user']);
					$tpage = outCode($row['page']);
					$agent = outCode($row['agent']);
					$referer = outCode($row['referer']);
					$requesturi = outCode($row['requesturi']);
					$isbot = intval($row['isbot']);
					$cookies = intval($row['cookies']);
					$tcookies = ($cookies==1) ? "cookies_on.png" : "cookies_off.png" ;
					$uid = intval($row['uid']);
					
					$agent = str_replace("[","{",$agent);
					$agent = str_replace("]","}",$agent);
					
					$tban = ($tuser=="") ? "admin.php?page=security&op=banip&ip=$ip" : "admin.php?page=security&op=banip&ip=$ip&user=$tuser" ;
					$tuser = ($uid>0) ? "<a href='index.php?page=users&op=userInfo&uid=$uid' title='$tuser'>$tuser</a>" : "-" ;
					$tagent = ($agent!="") ? "<a title=\"header=["._AGENT_."] body=[$agent]\">".trimString($agent,20,1)."</a>" : "-" ;
					$treferer = ($referer!="") ? "<a href=\"$referer\" title=\"header=["._REFURL_."] body=[$referer]\" target='_blank'>".trimString($referer,20,1)."</a>" : "-" ;
					$trequesturi = ($requesturi!="") ? "<a href=\"".$siteConfig['site_url']."$requesturi\" title=\"header=["._REQURI_."] body=[$requesturi]\" target='_blank'>".trimString($requesturi,20,1)."</a>" : "-" ;
					
					$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					if ($isbot==1) { $class = "warning"; }
					if (banned($ip)) { $class = "banned"; }
					
					echo "<tr>";
						echo "<td class='$class' width='15%' nowrap>".ipcountry2img($ip)." <a href='".dnsLink($ip)."' title='Whois $ip' target='_blank'><b>".getIpText($ip)."</b></a></td>";
						echo "<td class='$class' width='10%' nowrap><b>$tuser</b></td>";
						echo "<td class='$class' width='10%' nowrap>$tpage</td>";
						echo "<td class='$class' width='25%'>$tagent</td>";
						echo "<td class='$class' width='20%'>$treferer</td>";
						echo "<td class='$class' width='20%'>$trequesturi</td>";
						echo "<td class='$class' width='1%' nowrap><img src='images/{$tcookies}' alt='Cookies' title='Cookies'> <img src='images/date.png' alt='"._DATE_."' title='header=["._DATE_."] body=[$date]'> <a href='$tban' title='"._BAN_." $ip'><img src='images/user_red.gif' border='0'></a></td>";
					echo "</tr>\n";
				}
				echo "</tbody>\n";
				echo "</table>\n";
			}
		
		
			if ($result = $dblink->get_list("SELECT *,DATE_FORMAT(date, '".$siteConfig['timestamp']."') as date2 FROM memht_statistics_administration WHERE ip='$ip' ORDER BY date DESC LIMIT 100")) {
				$found = true;
				$n = 0;
				echo "<table width='100%' cellspacing='1' cellpadding='0' class='std_nicetable' style='margin-top:4px;'>\n";
				echo "<thead>\n";
				echo "<tr><td colspan='7'>"._ADMINISTRATION_."</td></tr>";
				echo "<tr><td>"._IP_."</td><td>"._USER_."</td><td>"._PAGE_."</td><td>"._AGENT_."</td><td>"._REFURL_."</td><td>"._REQURI_."</td><td width='1%'>&nbsp;</td></tr>\n";
				echo "</thead>\n";
				echo "<tbody>\n";
				foreach ($result as $row) {
					$date = $row['date2'];
					$ip = outCode($row['ip']);
					$tadmin = outCode($row['admin']);
					$tpage = outCode($row['page']);
					$agent = outCode($row['agent']);
					$referer = outCode($row['referer']);
					$requesturi = outCode($row['requesturi']);
					
					$agent = str_replace("[","{",$agent);
					$agent = str_replace("]","}",$agent);
					
					$tban = ($tadmin=="") ? "admin.php?page=security&op=banip&ip=$ip" : "admin.php?page=security&op=banip&ip=$ip&user=$tadmin" ;
					$tagent = ($agent!="") ? "<a title=\"header=["._AGENT_."] body=[$agent]\">".trimString($agent,20,1)."</a>" : "-" ;
					$treferer = ($referer!="") ? "<a href='$referer' title=\"header=["._REFURL_."] body=[$referer]\" target='_blank'>".trimString($referer,20,1)."</a>" : "-" ;
					$trequesturi = ($requesturi!="") ? "<a href=\"".$siteConfig['site_url']."$requesturi\" title=\"header=["._REQURI_."] body=[$requesturi]\" target='_blank'>".trimString($requesturi,20,1)."</a>" : "-" ;
					
					$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					if (banned($ip)) { $class = "banned"; }
					
					echo "<tr>";
						echo "<td class='$class' width='15%' nowrap>".ipcountry2img($ip)." <a href='".dnsLink($ip)."' title='Whois $ip' target='_blank'><b>$ip</b></a></td>";
						echo "<td class='$class' width='10%' nowrap><b>$tadmin</b></td>";
						echo "<td class='$class' width='10%' nowrap>$tpage</td>";
						echo "<td class='$class' width='25%'>$tagent</td>";
						echo "<td class='$class' width='20%'>$treferer</td>";
						echo "<td class='$class' width='20%'>$trequesturi</td>";
						echo "<td class='$class' width='1%' nowrap><img src='images/date.png' alt='"._DATE_."' title='header=["._DATE_."] body=[$date]'> <a href='$tban' title='"._BAN_." $ip'><img src='images/user_red.gif' border='0'></a></td>";
					echo "</tr>\n";
				}
				echo "</tbody>\n";
				echo "</table>\n";
			}
		
		
			if ($result = $dblink->get_list("SELECT *,DATE_FORMAT(regdate, '".$siteConfig['timestamp']."') as regdate,DATE_FORMAT(lastlogin, '".$siteConfig['timestamp']."') as lastlogin FROM memht_utenti WHERE rank < 3 AND lastip='$ip'")) {
				$found = true;
				$n = 0;
				echo "<table width='100%' cellspacing='1' cellpadding='0' class='std_nicetable' style='margin-top:4px;'>\n";
				echo "<thead>\n";
				echo "<tr><td colspan='5'>"._USERS_."</td></tr>";
				echo "<tr><td width='25%'>"._USERNAME_."</td><td width='25%'>"._EMAIL_."</td><td width='25%'>"._REGISTERED_."</td><td width='20%'>"._LASTLOGIN_."</td><td width='1%'>&nbsp;</td></tr>\n";
				echo "</thead>\n";
				echo "<tbody>\n";
				foreach ($result as $row) {
					$uuser = outCode($row['user']);
					$uemail = outCode($row['email']);
					$uregdate = $row['regdate'];
					$ulastlogin = $row['lastlogin'];
					$uid = intval($row['id']);
					
					$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					
					echo "<tr><td class='$class'><a href='index.php?page=users&op=userInfo&uid=$uid' title='$uuser'><b>$uuser</b></a></td><td class='$class'>$uemail</td><td class='$class'>$uregdate</td><td class='$class'>$ulastlogin</td><td class='$class' align='right' nowrap><a href='admin.php?page=security&op=banip&ip=$ip&user=$uuser' title='"._BAN_." $ip ($uuser)'>&nbsp;<img src='images/user_red.gif' border='0'></a></td></tr>\n";		
				}
				echo "</tbody>\n";
				echo "</table>\n";
			}
			
			if ($result = $dblink->get_list("SELECT *,DATE_FORMAT(lastlogin, '".$siteConfig['timestamp']."') as lastlogin FROM memht_utenti WHERE rank>2 AND lastip='$ip'")) {
				$found = true;
				$n = 0;
				echo "<table width='100%' cellspacing='1' cellpadding='0' class='std_nicetable' style='margin-top:4px;'>\n";
				echo "<thead>\n";
				echo "<tr><td colspan='4'>"._ADMIN_."</td></tr>";
				echo "<tr><td width='25%'><b>"._USERNAME_."</b></td><td width='25%'><b>"._EMAIL_."</b></td><td width='25%'><b>"._LASTLOGIN_."</b></td><td width='1%'>&nbsp;</td></tr>\n";
				echo "</thead>\n";
				echo "<tbody>\n";
				foreach ($result as $row) {
					$uuser = outCode($row['user']);
					$uemail = outCode($row['email']);
					$ulastlogin = $row['lastlogin'];	
					$uid = intval($row['id']);
					
					$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					
					echo "<tr><td class='$class'><img src='images/bullet_paper.gif' alt='Bullet' border='0'> <a href='index.php?page=users&op=userInfo&uid=$uid' title='$uuser'><b>$uuser</b></a></td><td class='$class'>$uemail</td><td class='$class'>$ulastlogin</td><td class='$class' align='right' nowrap><a href='admin.php?page=security&op=banip&ip=$ip&user=$uuser' title='"._BAN_." $ip ($uuser)'><img src='images/user_red.gif' border='0'></a></td></tr>\n";		
				}
				echo "</tbody>\n";
				echo "</table>\n";
			}
			
			
			if ($result = $dblink->get_list("SELECT m.*,DATE_FORMAT(m.data, '".$siteConfig['timestamp']."') as data,u.id AS uid FROM memht_messagebox AS m LEFT JOIN memht_utenti AS u ON m.autore=u.user WHERE m.ip='$ip' ORDER BY m.id DESC")) {
				$found = true;
				$n = 0;
				echo "<table width='100%' cellspacing='1' cellpadding='0' class='std_nicetable' style='margin-top:4px;'>\n";
				echo "<thead>\n";
				echo "<tr><td colspan='4'>"._MESSAGEBOX_."</td></tr>";
				echo "<tr><td width='15%'>"._AUTHOR_."</td><td width='15%'>"._DATE_."</td><td width='65%'>"._TEXT_."</td><td width='1%'>&nbsp;</td></tr>\n";
				echo "</thead>\n";
				echo "<tbody>\n";
				foreach ($result as $row) {
					$mid = intval($row['id']);
					$muser = outCode($row['autore']);
					$mdate = $row['data'];
					$mtext = bb2html(outCode($row['testo']));
					$uid = intval($row['uid']);
					
					$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					
					echo "<tr><td class='$class' valign='top'><nobr><img src='images/bullet_paper.gif' alt='Bullet' border='0'> <a href='index.php?page=users&op=userInfo&uid=$uid' title='$muser'><b>$muser</b></a></td><td class='$class' valign='top' nowrap>$mdate</td><td class='$class'>$mtext</td><td class='$class' align='right'><a href='admin.php?page=messagebox&op=deleteBan&id=$mid' title='"._DELETE_."'><img src='images/delete.gif' title='"._DELETE_."' border='0'></a></td></tr>\n";		
				}
				echo "</tbody>\n";
				echo "</table>\n";
			}
			
			if (!$found) {
				echo "<div align='center' id='errorText' class='box'><b>"._IPNOTFOUND_."</b></div>";
			}
			
			//Show whois link
			echo "<br><div align='center' id='info'><a href='".dnsLink($ip)."' target='_blank' title='"._FIND_." $ip "._ON_." whois'>"._FIND_." $ip "._ON_." whois</a></div>";
			//
		} else {
			echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
		}
	}
}

require_once("admin/inc/inc_header.php");
	admin_page_title($page);
														
	openTable();
	if (isAuth($userid,2)) {
	
		echo "<div align='center' class='box'>";
			echo "<a href='admin.php?page=security' title='"._SHOW_BANNED_."'><img src='admin/icons/security.png' border='0' title='"._SHOW_BANNED_."' align='top'> "._SHOW_BANNED_."</a>";
			echo " - <a href='admin.php?page=security&op=banip' title='"._BAN_IP_."'><img src='admin/icons/security_add.png' border='0' title='"._BAN_IP_."' align='top'> "._BAN_IP_."</a>";
			echo " - <a href='admin.php?page=security&op=iptrack' title='"._TRACKED_IPS_."'><img src='admin/icons/table.png' border='0' title='"._TRACKED_IPS_."' align='top'> "._TRACKED_IPS_."</a>";
			echo " - <a href='admin.php?page=security&op=findip' title='"._FIND_IP_."'><img src='admin/icons/search.png' border='0' title='"._FIND_IP_."' align='top'> "._FIND_IP_."</a>";
		echo "</div>";
	closeTable();
	
	openTable();
	
		switch($op) {
			case "banip":
				add($p_ip,$p_fip,$p_user,$p_range,$p_permanent,$p_date,$p_reason,$ok);
			break;
			
			case "editBan":
				edit($id,$p_ip,$p_fip,$p_user,$p_range,$p_permanent,$p_date,$p_reason,$ok);
			break;
			
			case "deleteBan":
				delete($id,$ok);
			break;
			
			case "iptrack":
				iptrack($ofsbgn,$ofsppg,$pg);
			break;
			
			case "iptrackadmin":
				iptrackAdmin($ofsbgn,$ofsppg,$pg);
			break;
			
			case "findip":
				findip($ip,$ok);
			break;
		
			default:
				bannedList($ofsbgn,$ofsppg,$pg);
			break;
		}
	} else {
		echo "<div align='center' id='errorText'><b>"._ACCESSDENIED_."</b></div>";
	}
	
	closeTable();
require_once("admin/inc/inc_footer.php");

?>