<?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>");
}

$op = (isset($_GET['op'])) ? inCode($_GET['op']) : "" ;
$id = (isset($_GET['id'])) ? intval($_GET['id']) : 0 ;
$ok = (isset($_GET['ok'])) ? inCode($_GET['ok']) : false ;
$subop = (isset($_GET['subop'])) ? inCode($_GET['subop']) : "" ;
$period = (isset($_GET['period'])) ? intval($_GET['period']) : 0 ;
//Period: 0=Today, 1=Yesterday, 2=Last Week, 3=Last Month, 4=Last Year, 5=Total

function main($period) {
	global $dblink,$siteConfig;
	
	echo "<div align='center' class='box'><select onChange='location = this.options[this.selectedIndex].value;'>";
		$selected = ($period==0) ? "selected" : "" ;
		echo "<option value='admin.php?page=statistics&period=0'$selected>"._TODAY_."</option>\n";
		$selected = ($period==1) ? "selected" : "" ;
		echo "<option value='admin.php?page=statistics&period=1'$selected>"._YESTERDAY_."</option>\n";
		$selected = ($period==2) ? "selected" : "" ;
		echo "<option value='admin.php?page=statistics&period=2'$selected>"._LAST_WEEK_."</option>\n";
		$selected = ($period==3) ? "selected" : "" ;
		echo "<option value='admin.php?page=statistics&period=3'$selected>"._LAST_MONTH_."</option>\n";
		$selected = ($period==4) ? "selected" : "" ;
		echo "<option value='admin.php?page=statistics&period=4'$selected>"._LAST_YEAR_."</option>\n";
		$selected = ($period==5) ? "selected" : "" ;
		echo "<option value='admin.php?page=statistics&period=5'$selected>"._TOTAL_."</option>\n";
	echo "</select></div>\n";
	
	echo "<table width='100%' cellpadding='0' cellspacing='0' border='0'>\n";
		echo "<tr><td width='49%' valign='top'>\n";
			stat_total_info_l();
		echo "</td><td width='1%'>&nbsp;</td><td width='49%' valign='top'>\n";
			stat_total_info_r();
		echo "</td></tr>\n";
		
		echo "<tr><td colspan='3' style='padding:2px 0;'>\n";
			stat_hits($period);
		echo "</td></tr>\n";
		
		if ($period==0) {
			echo "<tr><td colspan='3' style='padding:2px 0;'>\n";
				stat_graph_days();
			echo "</td></tr>\n";
		
			echo "<tr><td colspan='3' style='padding:2px 0;'>\n";
				stat_today_users();
			echo "</td></tr>\n";
			
			echo "<tr><td colspan='3' style='padding:2px 0;'>\n";
				stat_today_spiders();
			echo "</td></tr>\n";
			
			echo "<tr><td colspan='3' style='padding:2px 0;'>\n";
				stat_today_referer();
			echo "</td></tr>\n";
		}		
		echo "<tr><td width='49%' valign='top' style='padding:2px 0;'>\n";
			stat_timed_users($period);
		echo "</td><td width='1%'>&nbsp;</td><td width='49%' valign='top' style='padding:2px 0;'>\n";
			stat_timed_pages($period);
		echo "</td></tr>\n";
		
		echo "<tr><td width='49%' valign='top' style='padding:2px 0;'>\n";
			stat_timed_searchengines($period);
		echo "</td><td width='1%'>&nbsp;</td><td width='49%' valign='top' style='padding:2px 0;'>\n";
			stat_timed_searchkeywords($period);
		echo "</td></tr>\n";
		
		echo "<tr><td width='49%' valign='top' style='padding:2px 0;'>\n";
			stat_timed_os($period);
		echo "</td><td width='1%'>&nbsp;</td><td width='49%' valign='top' style='padding:2px 0;'>\n";
			stat_timed_browsers($period);
		echo "</td></tr>\n";
		
		echo "<tr><td width='49%' valign='top' style='padding:2px 0;'>\n";
			stat_timed_domains($period);
		echo "</td><td width='1%'>&nbsp;</td><td width='49%' valign='top' style='padding:2px 0;'>\n";
			stat_timed_screenres($period);
		echo "</td></tr>\n";
	echo "</table>";
}

function isinarray($array,$str) {
	$match = false;
	foreach ($array as $key => $value) { if ($key==$str) { return true; } }
	return $match;
}

function stat_hits($period) {
	global $dblink,$siteConfig,$siteInfo,$tzNOW;
	
	$date = $siteInfo['sdate'];
	
	switch ($period) {
		default:
		case 5: //Total
			$periodquery = "";
		break;
		case 4: //Last Year
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -1 YEAR)";
		break;
		case 3: //Last Month
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -1 MONTH)";
		break;
		case 2: //Last Week
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -6 DAY)";
		break;
		case 1: //Yesterday
			$periodquery = "WHERE day = DATE_ADD('$date',INTERVAL -1 DAY)";
		break;
		case 0: //Today
			$periodquery = "WHERE day >= '$date'";
		break;
	}
	//Tot. hits
	$row = $dblink->get_row("SELECT SUM(pageviews) AS totarchive FROM memht_statistics_hits $periodquery");
	$tot_hits = intval($row['totarchive']);
			
	//Tot. unique hits
	$row = $dblink->get_row("SELECT SUM(uniquevisitors) AS totarchive FROM memht_statistics_hits $periodquery");
	$tot_unique_hits = intval($row['totarchive']);
	
	echo "<table width='100%' cellpadding='0' cellspacing='0' border='0'>\n";
		echo "<tr><td width='49%' valign='top' align='center' class='box'>\n";
				echo "<span style='font-size:20px;'><b>$tot_hits</b></span> <span style='font-size:20px; color:#A6301C;'><b>"._PAGESEEN_."</b> <img src='images/info.gif' alt='Info' title=\"header=["._INFORMATIONS_."] body=["._STATS_PAGESEEN_."]\"></span>";
		echo "</td><td width='1%'>&nbsp;</td><td width='49%' valign='top' align='center' class='box'>\n";
				echo "<span style='font-size:20px;'><b>$tot_unique_hits</b></span> <span style='font-size:20px; color:#A6301C;'><b>"._VISITORS_."</b> <img src='images/info.gif' alt='Info' title=\"header=["._INFORMATIONS_."] body=["._STATS_UNIQUEVIS_."]\"></span>";
		echo "</td></tr>\n";
	echo "</table>";	
}

function stat_total_info_l() {
	global $dblink,$siteConfig,$siteInfo,$tzNOW;
	
	//Tot. hits
	$row = $dblink->get_row("SELECT SUM(pageviews) AS totarchive FROM memht_statistics_hits");
	$tot_hits = intval($row['totarchive']);
	
	//Tot. unique hits
	$row = $dblink->get_row("SELECT SUM(uniquevisitors) AS totarchive FROM memht_statistics_hits");
	$tot_unique_hits = intval($row['totarchive']);
	
	//Busiest day
	$row = $dblink->get_row("SELECT pageviews,DATE_FORMAT(day,'%Y.%m.%d') as busiestday FROM memht_statistics_hits ORDER BY pageviews DESC LIMIT 1");
	$mostpageviews = intval($row['pageviews']);
	$busiestday = outCode($row['busiestday']);
	
	$num_nl = $dblink->get_num("SELECT id FROM memht_newsletter");
	
	$num = $den = 0;
	$result = $dblink->get_list("SELECT time,hits FROM memht_statistics_loadtime ORDER BY hits DESC LIMIT 200");
	foreach ($result as $row) {
		$time = $row['time'];
		$hits = $row['hits'];
		
		$num += ($time*$hits);
		$den += $hits;
	}
	$med_load_time = @sprintf("%01.2f",$num/$den);
	
	$num_com_art = $dblink->get_num("SELECT id FROM memht_comments WHERE whr=1 AND moderate=0");
	$num_com_blg = $dblink->get_num("SELECT id FROM memht_comments WHERE whr=5 AND moderate=0");
	$num_com_fil = $dblink->get_num("SELECT id FROM memht_comments WHERE whr=4 AND moderate=0");
	$num_com_gui = $dblink->get_num("SELECT id FROM memht_comments WHERE whr=2 AND moderate=0");
	$num_com_nws = $dblink->get_num("SELECT id FROM memht_comments WHERE whr=3 AND moderate=0");
	$num_com_sur = $dblink->get_num("SELECT id FROM memht_comments WHERE whr=6 AND moderate=0");
	
	echo "<table width='100%' cellpadding='0' cellspacing='1' border='0' class='std_nicetable'>\n";
	echo "<thead>\n";
		echo "<tr><td colspan='2'><b>"._GENERAL_."</b> ("._TOTAL_.")</tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
		echo "<tr><td class='clean'><b>"._PAGESEEN_."</b></td><td class='clean' align='right'>$tot_hits</td></tr>\n";
		echo "<tr><td class='hlight'><b>"._VISITORS_."</b></td><td class='hlight' align='right'>$tot_unique_hits</td></tr>\n";
		echo "<tr><td class='clean'><b>"._BUSIESTDAY_."</b></td><td class='clean' align='right' nowrap><span id='info'>($mostpageviews)</span> $busiestday</td></tr>\n";
		echo "<tr><td class='hlight' nowrap><b>"._MEDIUM_LOAD_TIME_."</b> <span id='info'>(".$siteConfig['maintenance_loadtime']."h)</td><td class='hlight' align='right'>$med_load_time</td></tr>\n";
		echo "<tr><td class='clean'><b>"._SUBSCRIBEDNEWSLETTER_."</b></td><td class='clean' align='right'>$num_nl</td></tr>\n";
	echo "</tbody>\n";
	echo "<thead>\n";
		echo "<tr><td colspan='2'><b>"._COMMENTS_."</b> ("._TOTAL_.")</tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
		echo "<tr><td class='clean'><b>"._ARTICLES_."</b></td><td class='clean' align='right'>$num_com_art</td></tr>\n";
		echo "<tr><td class='hlight'><b>"._BLOG_."</b></td><td class='hlight' align='right'>$num_com_blg</td></tr>\n";
		echo "<tr><td class='clean'><b>"._DOWNLOAD_."</b></td><td class='clean' align='right'>$num_com_fil</td></tr>\n";
		echo "<tr><td class='hlight'><b>"._GUIDE_."</b></td><td class='hlight' align='right'>$num_com_gui</td></tr>\n";
		echo "<tr><td class='clean'><b>"._NEWS_."</b></td><td class='clean' align='right'>$num_com_nws</td></tr>\n";
		echo "<tr><td class='hlight'><b>"._SURVEYS_."</b></td><td class='hlight' align='right'>$num_com_sur</td></tr>\n";
	echo "</tbody>\n";
	echo "</table>\n";
}

function stat_total_info_r() {
	global $dblink;
	
	$num_users = $dblink->get_num("SELECT id FROM memht_utenti");
	$num_users_waiting = $dblink->get_num("SELECT id FROM memht_utenti_attesa");
	$last_user_row = $dblink->get_row("SELECT id,user FROM memht_utenti ORDER BY id DESC LIMIT 1");
	$uid = intval($last_user_row['id']);
	$last_user = outCode($last_user_row['user']);
	
	$num_articles = $dblink->get_num("SELECT id FROM memht_articoli");
	$num_blog = $dblink->get_num("SELECT id FROM memht_blog_posts");
	$num_files = $dblink->get_num("SELECT id FROM memht_download");
	$num_posts = $dblink->get_num("SELECT id FROM memht_forum_posts");
	$num_guide = $dblink->get_num("SELECT id FROM memht_guide");
	$num_links = $dblink->get_num("SELECT id FROM memht_mylinks");
	$num_messages = $dblink->get_num("SELECT id FROM memht_messaggi");
	$num_news = $dblink->get_num("SELECT id FROM memht_news");	
	
	echo "<table width='100%' cellpadding='0' cellspacing='1' border='0' class='std_nicetable'>\n";
	echo "<thead>\n";
		echo "<tr><td colspan='2'><b>"._USERS_."</b> ("._TOTAL_.")</tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
		echo "<tr><td class='clean'><b>"._TOTUSERS_."</b></td><td class='clean' align='right'>$num_users</td></tr>\n";
		echo "<tr><td class='hlight'><b>"._ACCWAITINGACTIVATION_."</b></td><td class='hlight' align='right'>$num_users_waiting</td></tr>\n";
		echo "<tr><td class='clean'><b>"._LASTUSER_."</b></td><td class='clean' align='right'><a href='index.php?page=users&op=userInfo&uid=$uid' title='$last_user'>$last_user</a></td></tr>\n";
	echo "</tbody>\n";
	echo "<thead>\n";
		echo "<tr><td colspan='2'><b>"._CONTENT_."</b> ("._TOTAL_.")</tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
		echo "<tr><td class='clean'><b>"._ARTICLES_."</b></td><td class='clean' align='right'>$num_articles</td></tr>\n";
		echo "<tr><td class='hlight'><b>"._BLOG_POSTS_."</b></td><td class='hlight' align='right'>$num_blog</td></tr>\n";
		echo "<tr><td class='clean'><b>"._FILES_."</b></td><td class='clean' align='right'>$num_files</td></tr>\n";
		echo "<tr><td class='hlight'><b>"._GUIDE_."</b></td><td class='hlight' align='right'>$num_guide</td></tr>\n";
		echo "<tr><td class='clean'><b>"._LINKS_."</b></td><td class='clean' align='right'>$num_links</td></tr>\n";
		echo "<tr><td class='hlight'><b>"._NEWS_."</b></td><td class='hlight' align='right'>$num_news</td></tr>\n";
		echo "<tr><td class='clean'><b>"._MESSAGES_."</b></td><td class='clean' align='right'>$num_messages</td></tr>\n";
		echo "<tr><td class='hlight'><b>"._POSTS_."</b> <span id='info'>(Forum)</span></td><td class='hlight' align='right'>$num_posts</td></tr>\n";
	echo "</tbody>\n";
	echo "</table>\n";
}

function stat_graph_days() {
	global $dblink;
	
	?>
	<style type="text/css">
	.stat_p ul {
		margin:0;
		padding:0;
		position:relative;
		height:200px;
		list-style:none;
	}
	.stat_p li {
		position:absolute;
		bottom:0;
		background-color:#CCC;
		background-image:url(admin/images/tpl/stat_graph_p.jpg);
		background-position:top;
		background-repeat:repeat-x;
		border-right:2px solid #AAA;
		color:#999;
		text-align:center;
		font-size:9px;
	}
	.stat_p li div {
		position:absolute;
		left:0;
		bottom:0;
		width:34px;
		background-color:#DAA3A3;
		background-image:url(admin/images/tpl/stat_graph_u.jpg);
		background-position:top;
		background-repeat:repeat-x;
		border-right:2px solid #D08A8A;
		color:#CB7E7E;
	}
	.stat_p a:hover { color:#FFF; text-decoration:none; }
	.stat_day {
		color:#888;
		font-size:14px;
		text-align:center;
	}
	.stat_month {
		color:#AAA;
		font-size:10px;
		text-align:center;
	}
	</style>
	<?php
	
	//Busiest day (Pageviews)
	$row = $dblink->get_row("SELECT pageviews FROM memht_statistics_hits ORDER BY pageviews DESC LIMIT 30");
	$mostpageviews = intval($row['pageviews']);
	
	//Busiest day (Unique visitors)
	$row = $dblink->get_row("SELECT uniquevisitors FROM memht_statistics_hits ORDER BY uniquevisitors DESC LIMIT 30");
	$mostuniquevisitors = intval($row['uniquevisitors']);
	
	echo "<div class='stat_p'><ul>";
		$spacer = 0;
		$result = $dblink->get_list("SELECT pageviews,uniquevisitors,spiders FROM memht_statistics_hits ORDER BY day DESC LIMIT 25");
		foreach ($result as $row) {
			$pageviews = intval($row['pageviews']);
			$uniquevisitors = intval($row['uniquevisitors']);
			$spiders = intval($row['spiders']);
			if ($spiders==0) { $spiders = "-"; }
			
			$height_p = @intval(($pageviews/$mostpageviews)*100)*1.7+20;
			$height_u = @intval(($uniquevisitors/$mostuniquevisitors)*100)*2.2+20;
			if ($height_p<39) { $height_p = 39; }
			if (($height_u+25)>=$height_p) { $height_u = $height_p-25; }
			
			echo "<li style='height:{$height_p}px; width:34px; left:{$spacer}px;'><a title='header=["._PAGESEEN_."] body=[$pageviews]'>$pageviews</a><br><span style='color:#EEE;'><a title='header=["._SPIDERS_."] body=[$spiders]'>$spiders</a></span><div style='height:{$height_u}px;'><a title='header=["._VISITORS_."] body=[$uniquevisitors]'>$uniquevisitors</a></div></li>";
			$spacer += 36;
		}
	echo "</ul></div>";
	echo "<ul style='list-style:none; padding:0; margin:0;'>";
		$spacer = 0;
		$result = $dblink->get_list("SELECT DATE_FORMAT(day,'%d.%m') as date FROM memht_statistics_hits ORDER BY day DESC LIMIT 25");
		foreach ($result as $row) {
			$date = explode(".",$row['date']);
			$day = $date[0];
			$month = numToMonth($date[1],1);
			
			echo "<li style='width:36px; float:left; padding:0; background:url(admin/images/tpl/stat_graph_day.jpg) 0 0 no-repeat;'><div class='stat_day'>$day</div><div class='stat_month'>$month</div></li>";
			$spacer += 36;
		}
	echo "</ul>";
}

function stat_today_users() {
	global $dblink,$siteConfig;
	
	echo "<table width='100%' cellpadding='0' cellspacing='1' border='0' class='std_nicetable'>\n";
	echo "<thead>\n";
		echo "<tr><td colspan='6'><b>"._USERS_."</b></td></tr>\n";
		echo "<tr><td width='15%'><b>"._USER_."</b></td><td width='15%' nowrap><b>"._LASTLOGIN_."</b></td><td width='10%' nowrap><b>"._LASTPAGE_."</b></td><td width='20%' nowrap><b>"._AGENT_."</b></td><td width='24%' nowrap><b>"._REQURI_."</b></td><td width='1%'>&nbsp;</td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
		
		$n = 0;
		$showed = array();
		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 JOIN memht_utenti AS u ON l.user=u.user WHERE l.user!='' ORDER BY l.date DESC")) {
			foreach ($result as $row) {
				$date = $row['date2'];
				$ip = outCode($row['ip']);
				$user = outCode($row['user']);
				$page = outCode($row['page']);
				$agent = outCode($row['agent']);
				$referer = outCode($row['referer']);
				$requesturi = outCode($row['requesturi']);
				$uid = intval($row['uid']);
				
				if (!in_array($user,$showed)) {
					$showed[] = $user;
					
					$tpage = ($page!="") ? "$page" : "-" ;
					$tagent = ($agent!="") ? "<a title=\"header=["._AGENT_."] body=[$agent]\">".trimString($agent,25,1)."</a>" : "-" ;
					$trequesturi = ($requesturi!="") ? "<a href='".$siteConfig['site_url']."$requesturi' title=\"header=["._REQURI_."] body=[$requesturi]\" target='_blank'>".trimString($requesturi,25,1)."</a>" : "-" ;
					
					$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					
					echo "<tr><td class='$class'><a href='index.php?page=users&op=userInfo&uid=$uid' title='$user'><b>$user</b></a></td><td class='$class' nowrap>$date</td><td class='$class'>$tpage</td><td class='$class'>$tagent</td><td class='$class'>$trequesturi</td><td class='$class' nowrap> <a href='admin.php?page=security&op=findip&ok=true&ip=$ip' title='"._FIND_." $ip'><img src='images/ip.gif' alt='Ip' border='0'></a></td></tr>\n";
					if ($n>=20) { break; }
				}
			}
		} else {
			echo "<tr><td align='center' class='errorText clean' colspan='6'><b>"._EMPTY_."</b></tr>\n";
		}
	echo "</tbody>\n";
	echo "</table>\n";
}

function stat_today_spiders() {
	global $dblink,$siteConfig;
	
	echo "<table width='100%' cellpadding='0' cellspacing='1' border='0' class='std_nicetable'>\n";
	echo "<thead>\n";
		echo "<tr><td colspan='5'><b>"._SPIDERS_."</b></tr>\n";
		echo "<tr><td width='15%'><b>"._NAME_."</b></td><td width='15%'><b>"._IP_."</b></td><td width='50%'><b>"._AGENT_."</b></td><td width='13%'><b>"._DATE_."</b></td><td align='right' width='7%'><b>"._HITS_."</b></td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
		$n = 0;
		if ($result = $dblink->get_list("SELECT *,DATE_FORMAT(lastvisit, '".$siteConfig['timestamp']."') as lastvisit2 FROM memht_statistics_spiders ORDER BY lastvisit DESC LIMIT 20")) {
			foreach ($result as $row) {
				$lastvisit = $row['lastvisit2'];
				$ip = outCode($row['ip']);
				$agent = outCode($row['agent']);
				$hits = intval($row['hits']);
				
				$tagent = ($agent!="") ? "<a title=\"header=["._AGENT_."] body=[$agent]\">".trimString($agent,52,1)."</a>" : "-" ;
				$srow = $dblink->get_row("SELECT spidname FROM memht_spider_ranges WHERE '".ip2num($ip)."' BETWEEN fromip AND toip");
				
				$class = (($n++%2)!=0) ? "hlight" : "clean" ;
				
				echo "<tr><td class='$class'><b>".outCode($srow['spidname'])."</b></td><td class='$class'>$ip</td><td class='$class'>$tagent</td><td class='$class' nowrap>$lastvisit</td><td class='$class' align='right'>$hits</td></tr>\n";
			}
		} else {
			echo "<tr><td align='center' id='errorText' colspan='5' class='clean'><b>"._EMPTY_."</b></tr>\n";
		}
	echo "</tbody>\n";
	echo "</table>\n";
}

function stat_today_referer() {
	global $dblink,$siteConfig;
	
	echo "<table width='100%' cellpadding='0' cellspacing='1' border='0' class='std_nicetable'>\n";
	echo "<thead>\n";
		echo "<tr><td colspan='3'><b>"._REFURL_."</b></tr>\n";
		echo "<tr><td width='18%'><b>"._DATE_."</b></td><td width='77%'><b>"._URL_."</b></td><td align='right' width='5%'><b>"._HITS_."</b></td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
		$n = 0;
		$showed = array();
		if ($result = $dblink->get_list("SELECT l.referer,DATE_FORMAT(l.date, '".$siteConfig['timestamp']."') as date2,(SELECT COUNT(*) FROM memht_statistics_livedata WHERE referer=l.referer) AS hits FROM memht_statistics_livedata AS l WHERE l.referer!='' ORDER BY l.date DESC")) {
			foreach ($result as $row) {
				$date = $row['date2'];
				$referer = outCode($row['referer']);
				$hits = intval($row['hits']);
				
				if (!in_array($referer,$showed)) {
					$showed[] = $referer;
					
					$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					
					echo "<tr><td class='$class' nowrap>$date</td><td class='$class'><a href='$referer' title=\"header=["._REFURL_."] body=[$referer]\">".trimString($referer,75,1)."</td><td class='$class' align='right'>$hits</td></tr>\n";
					if ($n>=20) { break; }
				}
			}
		} else {
			echo "<tr><td align='center' class='errorText clean' colspan='3'><b>"._EMPTY_."</b></tr>\n";
		}
	echo "</tbody>\n";
	echo "</table>\n";
}

function stat_timed_searchengines($period) {
	global $dblink,$siteConfig,$siteInfo,$tzNOW;
	
	$date = $siteInfo['sdate'];
	
	switch ($period) {
		default:
		case 5: //Total
			$periodquery = "";
		break;
		case 4: //Last Year
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -1 YEAR)";
		break;
		case 3: //Last Month
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -1 MONTH)";
		break;
		case 2: //Last Week
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -6 DAY)";
		break;
		case 1: //Yesterday
			$periodquery = "WHERE day = DATE_ADD('$date',INTERVAL -1 DAY)";
		break;
		case 0: //Today
			$periodquery = "WHERE day >= '$date'";
		break;
	}
		
	echo "<table width='100%' cellpadding='0' cellspacing='1' border='0' class='std_nicetable'>\n";
	echo "<thead>\n";
		echo "<tr><td colspan='2'><b>"._SEARCHENGINES_."</b></tr>\n";
		echo "<tr><td><b>"._URL_."</b></td><td align='right' width='5%'><b>"._HITS_."</b></td></tr>\n";		
	echo "</thead>\n";
	echo "<tbody>\n";
		$ma = array();
		if ($result = $dblink->get_list("SELECT engine FROM memht_statistics_searchengines $periodquery ORDER BY hits DESC")) {
			foreach ($result as $row) {
				$engine = outCode($row['engine']);
				
				if ($period<5) { $periodquery2 = $periodquery." AND"; } else { $periodquery2 = "WHERE"; }
				
				if (!isinarray($ma,$engine)) {
					$srow = $dblink->get_row("SELECT SUM(hits) AS thits FROM memht_statistics_searchengines $periodquery2 engine='$engine'");
					$thits = intval($srow['thits']);
				
					$ma += array($engine => $thits);
				}
			}
			array_multisort($ma,SORT_DESC);
			$n = 0;
			foreach ($ma as $engine => $hits) {
				$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					
				echo "<tr><td class='$class' nowrap><b>$engine</b></td><td class='$class' align='right'>$hits</td></tr>\n";
				if ($n>=20) { break; }
			}
			unset($ma);
		} else {
			echo "<tr><td align='center' class='errorText clean' colspan='3'><b>"._EMPTY_."</b></tr>\n";
		}
	echo "</tbody>\n";
	echo "</table>\n";
}

function stat_timed_searchkeywords($period) {
	global $dblink,$siteConfig,$siteInfo,$tzNOW;
	
	$date = $siteInfo['sdate'];
	
	switch ($period) {
		default:
		case 5: //Total
			$periodquery = "";
		break;
		case 4: //Last Year
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -1 YEAR)";
		break;
		case 3: //Last Month
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -1 MONTH)";
		break;
		case 2: //Last Week
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -6 DAY)";
		break;
		case 1: //Yesterday
			$periodquery = "WHERE day = DATE_ADD('$date',INTERVAL -1 DAY)";
		break;
		case 0: //Today
			$periodquery = "WHERE day >= '$date'";
		break;
	}
	
	echo "<table width='100%' cellpadding='0' cellspacing='1' border='0' class='std_nicetable'>\n";
	echo "<thead>\n";
		echo "<tr><td colspan='2'><b>"._KEYWORDS_."</b></tr>\n";
		echo "<tr><td><b>"._NAME_."</b></td><td align='right' width='5%'><b>"._HITS_."</b></td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
		$ma = array();
		if ($result = $dblink->get_list("SELECT keyword FROM memht_statistics_searchkeywords $periodquery ORDER BY hits DESC")) {
			foreach ($result as $row) {
				$keyword = strtolower(outCode($row['keyword']));
				
				if ($period<5) { $periodquery2 = $periodquery." AND"; } else { $periodquery2 = "WHERE"; }
				
				if (!isinarray($ma,$keyword)) {
					$srow = $dblink->get_row("SELECT SUM(hits) AS thits FROM memht_statistics_searchkeywords $periodquery2 keyword='".inCode($keyword)."'");
					$thits = intval($srow['thits']);
				
					$ma += array($keyword => $thits);
				}
			}
			array_multisort($ma,SORT_DESC);
			$n = 0;
			foreach ($ma as $keyword => $hits) {
				$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					
				echo "<tr><td class='$class' nowrap><b><a title=\"header=["._KEYWORDS_."] body=[$keyword]\">".trimString($keyword,40,1)."</a></b></td><td class='$class' align='right'>$hits</td></tr>\n";
				if ($n>=20) { break; }
			}
			unset($ma);
		} else {
			echo "<tr><td align='center' class='errorText clean' colspan='3'><b>"._EMPTY_."</b></tr>\n";
		}
	echo "</tbody>\n";
	echo "</table>\n";
}

function stat_timed_users($period) {
	global $dblink,$siteConfig,$siteInfo,$tzNOW;
	
	$date = $siteInfo['sdate'];
	
	switch ($period) {
		default:
		case 5: //Total
			$periodquery = "";
		break;
		case 4: //Last Year
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -1 YEAR)";
		break;
		case 3: //Last Month
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -1 MONTH)";
		break;
		case 2: //Last Week
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -6 DAY)";
		break;
		case 1: //Yesterday
			$periodquery = "WHERE day = DATE_ADD('$date',INTERVAL -1 DAY)";
		break;
		case 0: //Today
			$periodquery = "WHERE day >= '$date'";
		break;
	}
	
	echo "<table width='100%' cellpadding='0' cellspacing='1' border='0' class='std_nicetable'>\n";
	echo "<thead>\n";
		echo "<tr><td colspan='2'><b>"._USERS_."</b></tr>\n";
		echo "<tr><td><b>"._USER_."</b></td><td align='right' width='5%'><b>"._HITS_."</b></td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
		$ma = array();
		if ($result = $dblink->get_list("SELECT user FROM memht_statistics_users $periodquery ORDER BY hits DESC")) {
			foreach ($result as $row) {
				$user = outCode($row['user']);
				
				if ($period<5) { $periodquery2 = $periodquery." AND"; } else { $periodquery2 = "WHERE"; }
				
				if (!isinarray($ma,$user)) {
					$srow = $dblink->get_row("SELECT SUM(hits) AS thits FROM memht_statistics_users $periodquery2 user='$user'");
					$thits = intval($srow['thits']);
				
					$ma += array($user => $thits);
				}
			}
			array_multisort($ma,SORT_DESC);
			$n = 0;
			foreach ($ma as $user => $hits) {
				$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					
				echo "<tr><td class='$class' nowrap><b>$user</b></td><td class='$class' align='right'>$hits</td></tr>\n";
				if ($n>=20) { break; }
			}
			unset($ma);
		} else {
			echo "<tr><td align='center' class='errorText clean' colspan='3'><b>"._EMPTY_."</b></tr>\n";
		}
	echo "</tbody>\n";
	echo "</table>\n";
}

function stat_timed_pages($period) {
	global $dblink,$siteConfig,$siteInfo,$tzNOW;
	
	$date = $siteInfo['sdate'];
	
	switch ($period) {
		default:
		case 5: //Total
			$periodquery = "";
		break;
		case 4: //Last Year
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -1 YEAR)";
		break;
		case 3: //Last Month
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -1 MONTH)";
		break;
		case 2: //Last Week
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -6 DAY)";
		break;
		case 1: //Yesterday
			$periodquery = "WHERE day = DATE_ADD('$date',INTERVAL -1 DAY)";
		break;
		case 0: //Today
			$periodquery = "WHERE day >= '$date'";
		break;
	}
	
	echo "<table width='100%' cellpadding='0' cellspacing='1' border='0' class='std_nicetable'>\n";
	echo "<thead>\n";
		echo "<tr><td colspan='2'><b>"._PAGES_."</b></tr>\n";
		echo "<tr><td><b>"._NAME_."</b></td><td align='right' width='5%'><b>"._HITS_."</b></td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
		$ma = array();
		if ($result = $dblink->get_list("SELECT page FROM memht_statistics_pages $periodquery ORDER BY hits DESC")) {
			foreach ($result as $row) {
				$page = outCode($row['page']);
				
				if ($period<5) { $periodquery2 = $periodquery." AND"; } else { $periodquery2 = "WHERE"; }
				
				if (!isinarray($ma,$page)) {
					$srow = $dblink->get_row("SELECT SUM(hits) AS thits FROM memht_statistics_pages $periodquery2 page='$page'");
					$thits = intval($srow['thits']);
				
					$ma += array($page => $thits);
				}
			}
			array_multisort($ma,SORT_DESC);
			$n = 0;
			foreach ($ma as $page => $hits) {
				$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					
				echo "<tr><td class='$class' nowrap><b>$page</b></td><td class='$class' align='right'>$hits</td></tr>\n";
				if ($n>=20) { break; }
			}
			unset($ma);
		} else {
			echo "<tr><td align='center' class='errorText clean' colspan='3'><b>"._EMPTY_."</b></tr>\n";
		}
	echo "</tbody>\n";
	echo "</table>\n";
}

function stat_timed_os($period) {
	global $dblink,$siteConfig,$siteInfo,$tzNOW;
	
	$date = $siteInfo['sdate'];
	
	switch ($period) {
		default:
		case 5: //Total
			$periodquery = "";
		break;
		case 4: //Last Year
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -1 YEAR)";
		break;
		case 3: //Last Month
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -1 MONTH)";
		break;
		case 2: //Last Week
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -6 DAY)";
		break;
		case 1: //Yesterday
			$periodquery = "WHERE day = DATE_ADD('$date',INTERVAL -1 DAY)";
		break;
		case 0: //Today
			$periodquery = "WHERE day >= '$date'";
		break;
	}
	
	echo "<table width='100%' cellpadding='0' cellspacing='1' border='0' class='std_nicetable'>\n";
	echo "<thead>\n";
		echo "<tr><td colspan='2'><b>"._OS_."</b></tr>\n";
		echo "<tr><td><b>"._NAME_."</b></td><td align='right' width='5%'><b>"._HITS_."</b></td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
		$ma = array();
		if ($result = $dblink->get_list("SELECT os FROM memht_statistics_os $periodquery ORDER BY hits DESC")) {
			foreach ($result as $row) {
				$os = outCode($row['os']);
				
				if ($period<5) { $periodquery2 = $periodquery." AND"; } else { $periodquery2 = "WHERE"; }
				
				if (!isinarray($ma,$os)) {
					$srow = $dblink->get_row("SELECT SUM(hits) AS thits FROM memht_statistics_os $periodquery2 os='$os'");
					$thits = intval($srow['thits']);
				
					$ma += array($os => $thits);
				}
			}
			array_multisort($ma,SORT_DESC);
			$n = 0;
			foreach ($ma as $os => $hits) {
				$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					
				echo "<tr><td class='$class' nowrap>".os2img($os)." <b>$os</b></td><td class='$class' align='right'>$hits</td></tr>\n";
				if ($n>=20) { break; }
			}
			unset($ma);
		} else {
			echo "<tr><td align='center' class='errorText clean' colspan='3'><b>"._EMPTY_."</b></tr>\n";
		}
	echo "</tbody>\n";
	echo "</table>\n";
}

function stat_timed_browsers($period) {
	global $dblink,$siteConfig,$siteInfo,$tzNOW;
	
	$date = $siteInfo['sdate'];
	
	switch ($period) {
		default:
		case 5: //Total
			$periodquery = "";
		break;
		case 4: //Last Year
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -1 YEAR)";
		break;
		case 3: //Last Month
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -1 MONTH)";
		break;
		case 2: //Last Week
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -6 DAY)";
		break;
		case 1: //Yesterday
			$periodquery = "WHERE day = DATE_ADD('$date',INTERVAL -1 DAY)";
		break;
		case 0: //Today
			$periodquery = "WHERE day >= '$date'";
		break;
	}
	
	echo "<table width='100%' cellpadding='0' cellspacing='1' border='0' class='std_nicetable'>\n";
	echo "<thead>\n";
		echo "<tr><td colspan='2'><b>"._BROWSERS_."</b></tr>\n";
		echo "<tr><td><b>"._NAME_."</b></td><td align='right' width='5%'><b>"._HITS_."</b></td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
		$ma = array();
		if ($result = $dblink->get_list("SELECT browser FROM memht_statistics_browsers $periodquery ORDER BY hits DESC")) {
			foreach ($result as $row) {
				$browser = outCode($row['browser']);
				
				if ($period<5) { $periodquery2 = $periodquery." AND"; } else { $periodquery2 = "WHERE"; }
				
				if (!isinarray($ma,$browser)) {
					$srow = $dblink->get_row("SELECT SUM(hits) AS thits FROM memht_statistics_browsers $periodquery2 browser='$browser'");
					$thits = intval($srow['thits']);
				
					$ma += array($browser => $thits);
				}
			}
			array_multisort($ma,SORT_DESC);
			$n = 0;
			foreach ($ma as $browser => $hits) {
				$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					
				echo "<tr><td class='$class' nowrap>".browser2img($browser)." <b>$browser</b></td><td class='$class' align='right'>$hits</td></tr>\n";
				if ($n>=20) { break; }
			}
			unset($ma);
		} else {
			echo "<tr><td align='center' class='errorText clean' colspan='3'><b>"._EMPTY_."</b></tr>\n";
		}
	echo "</tbody>\n";
	echo "</table>\n";
}

function stat_timed_domains($period) {
	global $dblink,$siteConfig,$siteInfo,$tzNOW;
	
	$date = $siteInfo['sdate'];
	
	switch ($period) {
		default:
		case 5: //Total
			$periodquery = "";
		break;
		case 4: //Last Year
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -1 YEAR)";
		break;
		case 3: //Last Month
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -1 MONTH)";
		break;
		case 2: //Last Week
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -6 DAY)";
		break;
		case 1: //Yesterday
			$periodquery = "WHERE day = DATE_ADD('$date',INTERVAL -1 DAY)";
		break;
		case 0: //Today
			$periodquery = "WHERE day >= '$date'";
		break;
	}
	
	echo "<table width='100%' cellpadding='0' cellspacing='1' border='0' class='std_nicetable'>\n";
	echo "<thead>\n";
		echo "<tr><td colspan='2'><b>"._DOMAINS_."</b></tr>\n";
		echo "<tr><td><b>"._NAME_."</b></td><td align='right' width='5%'><b>"._HITS_."</b></td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
		$ma = array();
		if ($result = $dblink->get_list("SELECT domain FROM memht_statistics_domains $periodquery ORDER BY hits DESC")) {
			foreach ($result as $row) {
				$domain = outCode($row['domain']);
				
				if ($period<5) { $periodquery2 = $periodquery." AND"; } else { $periodquery2 = "WHERE"; }
				
				if (!isinarray($ma,$domain)) {
					$srow = $dblink->get_row("SELECT SUM(hits) AS thits FROM memht_statistics_domains $periodquery2 domain='$domain'");
					$thits = intval($srow['thits']);
				
					$ma += array($domain => $thits);
				}
			}
			array_multisort($ma,SORT_DESC);
			$n = 0;
			foreach ($ma as $domain => $hits) {
				$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					
				echo "<tr><td class='$class' nowrap>".domain2img($domain)." <b>$domain</b></td><td class='$class' align='right'>$hits</td></tr>\n";
				if ($n>=20) { break; }
			}
			unset($ma);
		} else {
			echo "<tr><td align='center' class='errorText clean' colspan='3'><b>"._EMPTY_."</b></tr>\n";
		}
	echo "</tbody>\n";
	echo "</table>\n";
}

function stat_timed_screenres($period) {
	global $dblink,$siteConfig,$siteInfo,$tzNOW;
	
	$date = $siteInfo['sdate'];
	
	switch ($period) {
		default:
		case 5: //Total
			$periodquery = "";
		break;
		case 4: //Last Year
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -1 YEAR)";
		break;
		case 3: //Last Month
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -1 MONTH)";
		break;
		case 2: //Last Week
			$periodquery = "WHERE day >= DATE_ADD('$date',INTERVAL -6 DAY)";
		break;
		case 1: //Yesterday
			$periodquery = "WHERE day = DATE_ADD('$date',INTERVAL -1 DAY)";
		break;
		case 0: //Today
			$periodquery = "WHERE day >= '$date'";
		break;
	}

	echo "<table width='100%' cellpadding='0' cellspacing='1' border='0' class='std_nicetable'>\n";
	echo "<thead>\n";
		echo "<tr><td colspan='2'><b>"._RESOLUTION_."</b></tr>\n";
		echo "<tr><td><b>"._SIZE_."</b></td><td align='right' width='5%'><b>"._HITS_."</b></td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
		$ma = array();
		if ($result = $dblink->get_list("SELECT screenres FROM memht_statistics_screenres $periodquery ORDER BY hits DESC")) {
			foreach ($result as $row) {
				$screenres = outCode($row['screenres']);
				
				if ($period<5) { $periodquery2 = $periodquery." AND"; } else { $periodquery2 = "WHERE"; }
				
				if (!isinarray($ma,$screenres)) {
					$srow = $dblink->get_row("SELECT SUM(hits) AS thits FROM memht_statistics_screenres $periodquery2 screenres='$screenres'");
					$thits = intval($srow['thits']);
				
					$ma += array($screenres => $thits);
				}
			}
			array_multisort($ma,SORT_DESC);
			$n = 0;
			foreach ($ma as $screenres => $hits) {
				$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					
				echo "<tr><td class='$class' nowrap><b>$screenres</b></td><td class='$class' align='right'>$hits</td></tr>\n";
				if ($n>=20) { break; }
			}
			unset($ma);
		} else {
			echo "<tr><td align='center' class='errorText clean' colspan='3'><b>"._EMPTY_."</b></tr>\n";
		}
	echo "</tbody>\n";
	echo "</table>\n";
}

//=============================================================

function ignoreVisitor($id,$subop,$ok=false) {
	global $dblink;
	$id = intval($id);

	if (isset($_POST['uname'])) { $uname = inCode($_POST['uname']); } else { $uname = ""; }
	if (isset($_POST['sip'])) { $sip = inCode($_POST['sip']); } else { $sip = ""; }
	
	echo "<div align='center' class='box'>";
		echo "<a href='admin.php?page=statistics&op=ignore&subop=showIgnored' title='"._LIST_."'><img src='admin/icons/list.png' border='0' title='"._LIST_."' align='top'> "._LIST_."</a>";
		echo " - <a href='admin.php?page=statistics&op=ignore&subop=addIgnored' title='"._ADD_."'><img src='admin/icons/add.png' border='0' title='"._ADD_."' align='top'> "._ADD_."</a>";
	echo "</div>";
	
	switch ($subop) {
		case "addIgnored":		
			if (!$ok) {
				echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>\n";
				echo "<form name='form_spid' method='post' action='admin.php?page=statistics&op=ignore&subop=addIgnored&ok=true'>";
					echo "<tr><td width='25%'><b>"._USERNAME_."</b><td><input type='text' name='uname' size='30' maxlength='255'>\n";
					echo "<tr><td><b>"._IP_."</b></td><td><input type='text' name='sip' size='15' maxlength='15'></td></tr>\n";
					echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._ADD_."'>\n";
				echo "</form>\n";
				echo "</table>\n";
			} else {
				$save = true;
				if ($sip=="") {
					if ($uname=="") {
						$save = false;
					}
				} else if (!validIp($sip)) {
					$save = false;
				}
				
				if ($save) {
					$dblink->query("INSERT INTO memht_statistics_ignore (id,ip,user)
									VALUES (null,'$sip','$uname')");
					echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=statistics&op=ignore&subop=showIgnored'>";
				} else {
					echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
				}
			}
		break;
		
		case "editIgnored":
			if (!$ok) {
				$db_row = $dblink->get_row("SELECT * FROM memht_statistics_ignore WHERE id='$id'");
				$db_username = outCode($db_row['user']);
				$db_ip = outCode($db_row['ip']);
			
				echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>\n";
				echo "<form name='form_spid' method='post' action='admin.php?page=statistics&op=ignore&subop=editIgnored&id=$id&ok=true'>";
					echo "<tr><td width='25%'><b>"._USERNAME_."</b><td><input type='text' name='uname' value=\"$db_username\" size='30' maxlength='255'>\n";
					echo "<tr><td><b>"._IP_."</b></td><td><input type='text' name='sip' value=\"$db_ip\" size='15' maxlength='15'></td></tr>\n";
					echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._MODIFY_."'>\n";
				echo "</form>\n";
				echo "</table>\n";
			} else {
				$save = true;
				if ($sip=="") {
					if ($uname=="") {
						$save = false;
					}
				} else if (!validIp($sip)) {
					$save = false;
				}
		
				if ($save) {
					$dblink->query("UPDATE memht_statistics_ignore SET user='$uname',ip='$sip' WHERE id='$id'");
					echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=statistics&op=ignore&subop=showIgnored'>";
				} else {
					echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
				}
			}
		break;
		
		case "deleteIgnored":
			if ($ok) {
				$dblink->query("DELETE FROM memht_statistics_ignore WHERE id='$id'");
				echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=statistics&op=ignore&subop=showIgnored'>";		
			} else {
				echo "<div align='center'><b>"._SUREDELETE_."</b><br><a href='admin.php?page=statistics&op=ignore&subop=deleteIgnored&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=statistics&op=ignore&subop=showIgnored' title='"._NO_."'>"._NO_."</a></div>";
			}
		break;
		
		case "showIgnored":
		default:
			$n = 0;
			echo "<table width='100%' align='center' border='0' cellspacing='1' cellpadding='0' class='std_nicetable'>\n";
			echo "<thead>\n";
				echo "<tr><td width='50%'>"._USER_."</td><td>"._IP_."</td><td width='1%'>&nbsp;</td></tr>\n";
			echo "</thead>\n";
			echo "<tbody>\n";
				if ($result = $dblink->get_list("SELECT * FROM memht_statistics_ignore")) {
				foreach ($result as $row) {
					$st_id = intval($row['id']);
					$st_username = outCode($row['user']);
					$st_ip = outCode($row['ip']);
					
					$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					if ($st_username=="") { $st_username = "-"; }
					if ($st_ip=="") { $st_ip = "-"; }
					
					echo "<tr><td class='$class'><b>$st_username</b></td><td class='$class'>$st_ip</td><td class='$class' align='right' nowrap><a href='admin.php?page=statistics&op=ignore&subop=editIgnored&id=$st_id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=statistics&op=ignore&subop=deleteIgnored&id=$st_id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></td></tr>\n";
				}
				} else {
					echo "<tr><td colspan='3' align='center' id='errorText' class='clean'><b>"._EMPTY_."</b></td></tr>\n";
				}
			echo "</tbody>\n";
			echo "</table>";
		break;
	}
}

function spiderRanges($id,$subop,$ok=false) {
	global $dblink;
	
	$id = intval($id);

	if (isSet($_POST['spidname'])) { $spidname = incode($_POST['spidname']); } else { $spidname = ""; }
	if (isSet($_POST['fromip'])) { $fromip = incode($_POST['fromip']); } else { $fromip = ""; }
	if (isSet($_POST['toip'])) { $toip = incode($_POST['toip']); } else { $toip = ""; }
	
	openTable();
	
	echo "<div align='center' class='box'>";
		echo "<a href='admin.php?page=statistics&op=spiderRanges&subop=showSpiderRanges' title='"._LIST_."'><img src='admin/icons/list.png' border='0' title='"._LIST_."' align='top'> "._LIST_."</a>";
		echo " - <a href='admin.php?page=statistics&op=spiderRanges&subop=addSpiderRange' title='"._ADD_."'><img src='admin/icons/add.png' border='0' title='"._ADD_."' align='top'> "._ADD_."</a>";
	echo "</div>";
	
	switch ($subop) {
		case "addSpiderRange":		
			if (!$ok) {
				echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>\n";
				echo "<form name='form_spid' method='post' action='admin.php?page=statistics&op=spiderRanges&subop=addSpiderRange&ok=true'>";
					echo "<tr><td width='25%'>"._SPIDERNAME_."<td><input type='text' name='spidname' size='30' maxlength='255'>\n";
					echo "<tr><td>"._IPRANGE_."</td><td><input type='text' name='fromip' size='15' maxlength='15'> - <input type='text' name='toip' size='15' maxlength='15'></td></tr>\n";
					echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._ADD_."'>\n";
				echo "</form>\n";
				echo "</table>\n";
			} else {
				$save = true;
				if ($spidname=="") { $save = false; }
				if (!validIp($fromip)) { $save = false; }
				if (!validIp($toip)) { $save = false; }
		
				if ($save) {
					$dblink->query("INSERT INTO memht_spider_ranges (id,spidname,fromip,toip)
									VALUES (null,'$spidname','".ip2num($fromip)."','".ip2num($toip)."')");
					echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=statistics&op=spiderRanges&subop=showSpiderRanges'>";
				} else {
					echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
				}
			}
		break;
		
		case "editSpiderRange":
			if (!$ok) {
				$db_row = $dblink->get_row("SELECT * FROM memht_spider_ranges WHERE id='$id'");
				$db_spidname = outCode($db_row['spidname']);
				$db_fromip = num2ip($db_row['fromip']);
				$db_toip = num2ip($db_row['toip']);
			
				echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>\n";
				echo "<form name='form_spid' method='post' action='admin.php?page=statistics&op=spiderRanges&subop=editSpiderRange&id=$id&ok=true'>";
					echo "<tr><td width='25%'>"._SPIDERNAME_."<td><input type='text' name='spidname' value='$db_spidname' size='30' maxlength='255'>\n";
					echo "<tr><td>"._IPRANGE_."</td><td><input type='text' name='fromip' value='$db_fromip' size='15' maxlength='15'> - <input type='text' name='toip' value='$db_toip' size='15' maxlength='15'></td></tr>\n";
					echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._MODIFY_."'>\n";
				echo "</form>\n";
				echo "</table>\n";
			} else {
				$save = true;
				if ($spidname=="") { $save = false; }
				if (!validIp($fromip)) { $save = false; }
				if (!validIp($toip)) { $save = false; }
		
				if ($save) {
					$dblink->query("UPDATE memht_spider_ranges SET spidname='$spidname',fromip='".ip2num($fromip)."',toip='".ip2num($toip)."' WHERE id='$id'");
					echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=statistics&op=spiderRanges&subop=showSpiderRanges'>";
				} else {
					echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
				}
			}
		break;
		
		case "deleteSpiderRange":
			if ($ok) {
				$dblink->query("DELETE FROM memht_spider_ranges WHERE id='$id'");
				echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=statistics&op=spiderRanges&subop=showSpiderRanges'>";		
			} else {
				echo "<div align='center'><b>"._SUREDELETESPIDER_."</b><br><a href='admin.php?page=statistics&op=spiderRanges&subop=deleteSpiderRange&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=statistics&op=spiderRanges&subop=showSpiderRanges' title='"._NO_."'>"._NO_."</a></div>";
			}
		break;
		
		case "showSpiderRanges":
		default:
			$n = 0;
			echo "<table width='100%' align='center' border='0' cellspacing='1' cellpadding='0' class='std_nicetable'>\n";
			echo "<thead>\n";
				echo "<tr><td width='30%'>"._NAME_."</td><td width='20%'>"._RANGE_." ("._FROM_.")</td><td width='20%'>"._RANGE_." ("._TO_.")</td><td width='1%'>&nbsp;</td></tr>\n";
			echo "</thead>\n";
			echo "<tbody>\n";
				if ($sr_result = $dblink->get_list("SELECT * FROM memht_spider_ranges ORDER BY spidname")) {
					foreach ($sr_result as $sr_row) {
						$sr_id = intval($sr_row['id']);
						$sr_spidname = outCode($sr_row['spidname']);
						$sr_fromip = num2ip($sr_row['fromip']);
						$sr_toip = num2ip($sr_row['toip']);
						
						$class = (($n++%2)!=0) ? "hlight" : "clean" ;
						
						echo "<tr><td class='$class'><b>$sr_spidname</b></td><td class='$class'>$sr_fromip</td><td class='$class'>$sr_toip</td><td class='$class' align='right' nowrap><a href='admin.php?page=statistics&op=spiderRanges&subop=editSpiderRange&id=$sr_id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=statistics&op=spiderRanges&subop=deleteSpiderRange&id=$sr_id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></td></tr>\n";
					}
				} else {
					echo "<tr><td align='center' id='errorText' colspan='4' class='clean'><b>"._EMPTY_."</b></td></tr>\n";
				}
			echo "</tbody>\n";
			echo "</table>";
		break;
	}
	
	closeTable();
}

require_once("admin/inc/inc_header.php");
	admin_page_title($page);
		
	openTable();
		echo "<div align='center' class='box'>";
			echo "<a href='admin.php?page=statistics' title='"._STATISTICS_."'><img src='admin/icons/stat.png' border='0' title='"._STATISTICS_."' align='top'> "._STATISTICS_."</a>";
			echo " - <a href='admin.php?page=statistics&op=ignore' title='"._IGNORE_."'><img src='admin/icons/users_red.png' border='0' title='"._IGNORE_."' align='top'> "._IGNORE_."</a>";
			echo " - <a href='admin.php?page=statistics&op=spiderRanges' title='"._SPIDER_RANGES_."'><img src='admin/icons/range.png' border='0' title='"._SPIDER_RANGES_."' align='top'> "._SPIDER_RANGES_."</a>";
		echo "</div>";
	closeTable();

	openTable();
	
		switch($op) {			
			case "ignore":
				ignoreVisitor($id,$subop,$ok);
			break;
			
			case "spiderRanges":
				spiderRanges($id,$subop,$ok);
			break;
		
			default:
				main($period);
			break;
		}
	
	closeTable();
require_once("admin/inc/inc_footer.php");

?>