<?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 (stristr(htmlentities($_SERVER['PHP_SELF']), "inc_statistics.php")) {
	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>");
}
	
global $dblink,$siteConfig,$visitorInfo,$siteInfo,$userInfo,$page,$tzNOW;

if (!$visitorInfo['ignoreinstats']) {
	if (!($siteConfig['searchengineinstat']==0 AND $visitorInfo['is_spider'])) {
		$tos = os($visitorInfo['agent']);
		$tbrowser = browser($visitorInfo['agent']);
		$tsengine = searchengine($visitorInfo['referer']);
		$tkeyword = getSearchQuery($visitorInfo['referer']);
		$tdomain = ipcountry2domain($visitorInfo['ip']);
		if ($tdomain=="localhost" OR validIp($tdomain)) { $tdomain = ""; }
		$row = $dblink->get_row("SELECT $tzNOW as today");
		$date = explode(" ",$row['today']);
		$date = $date[0];
		
		//Os
		if ($tos!="") {
			if ($srow = $dblink->get_row("SELECT id FROM memht_statistics_os WHERE os='$tos' AND day='$date' LIMIT 1")) {
				$id = intval($srow['id']);
				
				$dblink->query("UPDATE memht_statistics_os SET hits=hits+1 WHERE id=$id");
			} else {
				$dblink->query("INSERT INTO memht_statistics_os (id,day,os,hits) VALUES (null,'$date','$tos','1')");
			}
		}
		
		//Browser
		if ($tbrowser!="") {
			if ($srow = $dblink->get_row("SELECT id FROM memht_statistics_browsers WHERE browser='$tbrowser' AND day='$date' LIMIT 1")) {
				$id = intval($srow['id']);
				
				$dblink->query("UPDATE memht_statistics_browsers SET hits=hits+1 WHERE id=$id");
			} else {
				$dblink->query("INSERT INTO memht_statistics_browsers (id,day,browser,hits) VALUES (null,'$date','$tbrowser','1')");
			}
		}
			
		//SearchEngine
		if ($tsengine!="") {
			if ($srow = $dblink->get_row("SELECT id FROM memht_statistics_searchengines WHERE engine='$tsengine' AND day='$date' LIMIT 1")) {
				$id = intval($srow['id']);
				
				$dblink->query("UPDATE memht_statistics_searchengines SET hits=hits+1 WHERE id=$id");
			} else {
				$dblink->query("INSERT INTO memht_statistics_searchengines (id,day,engine,hits) VALUES (null,'$date','$tsengine','1')");
			}
		}
		
		//Users
		if ($userInfo['user']!="") {
			if ($srow = $dblink->get_row("SELECT id FROM memht_statistics_users WHERE user='".$userInfo['user']."' AND day='$date' LIMIT 1")) {
				$id = intval($srow['id']);
				
				$dblink->query("UPDATE memht_statistics_users SET hits=hits+1 WHERE id=$id");
			} else {
				$dblink->query("INSERT INTO memht_statistics_users (id,day,user,hits) VALUES (null,'$date','".$userInfo['user']."','1')");
			}
		}
			
		//Keywords
		if ($tkeyword!="") {
			if ($srow = $dblink->get_row("SELECT id FROM memht_statistics_searchkeywords WHERE keyword='$tkeyword' AND day='$date' LIMIT 1")) {
				$id = intval($srow['id']);
				
				$dblink->query("UPDATE memht_statistics_searchkeywords SET hits=hits+1 WHERE id=$id");
			} else {
				$dblink->query("INSERT INTO memht_statistics_searchkeywords (id,day,keyword,hits) VALUES (null,'$date','$tkeyword','1')");
			}
		}
		
		//Domains
		if ($tdomain!="") {
			if ($srow = $dblink->get_row("SELECT id FROM memht_statistics_domains WHERE domain='$tdomain' AND day='$date' LIMIT 1")) {
				$id = intval($srow['id']);
				
				$dblink->query("UPDATE memht_statistics_domains SET hits=hits+1 WHERE id=$id");
			} else {
				$dblink->query("INSERT INTO memht_statistics_domains (id,day,domain,hits) VALUES (null,'$date','$tdomain','1')");
			}
		}
		
		//Pages
		if ($page!="") {
			if ($srow = $dblink->get_row("SELECT id FROM memht_statistics_pages WHERE page='$page' AND day='$date' LIMIT 1")) {
				$id = intval($srow['id']);
				
				$dblink->query("UPDATE memht_statistics_pages SET hits=hits+1 WHERE id=$id");
			} else {
				$dblink->query("INSERT INTO memht_statistics_pages (id,day,page,hits) VALUES (null,'$date','$page','1')");
			}
		}
			
		//Screen resolution
		if (isset($_COOKIE['stats_res'])) {
			$visitorInfo['resolution'] = preg_replace('`[^0-9x]`is','',$_COOKIE['stats_res']);
			if ($srow = $dblink->get_row("SELECT id FROM memht_statistics_screenres WHERE screenres='".$visitorInfo['resolution']."' AND day='$date' LIMIT 1")) {
				$id = intval($srow['id']);
				
				$dblink->query("UPDATE memht_statistics_screenres SET hits=hits+1 WHERE id=$id");
			} else {
				$dblink->query("INSERT INTO memht_statistics_screenres (id,day,screenres,hits) VALUES (null,'$date','".$visitorInfo['resolution']."','1')");
			}
		}
	}
	
	//Pageviews, Uniquevisitors and Spiders hits
	if ($srow = $dblink->get_row("SELECT id FROM memht_statistics_hits WHERE day='$date'")) {
		$id = intval($srow['id']);
		
		$query = "UPDATE memht_statistics_hits SET pageviews=pageviews+1";
		if (!isset($_COOKIE['unique']) AND $visitorInfo['cookies_on']) {
			setcookie("unique",time(),time()+3600); //1hour
			$query .= ",uniquevisitors=uniquevisitors+1";
		}
		if ($visitorInfo['is_spider']) {
			$query .= ",spiders=spiders+1";
		}
		$query .= " WHERE id=$id";
		$dblink->query($query);
	} else {
		$spider = ($visitorInfo['is_spider']) ? 1 : 0 ;
		$dblink->query("INSERT INTO memht_statistics_hits (id,day,pageviews,uniquevisitors,spiders)
						VALUES (null,'$date','1','1','$spider')");
	}
}

?>