<?php
/************************************************************************/
/* 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                                 */
/************************************************************************/
defined('MAXIMUS') or die ('Access denied counter MAXIMUS directly...');

$user_agent = get_user_agent();

if ((strstr($user_agent, 'Nav')) || (strstr($user_agent, 'Gold')) || (strstr($user_agent, 'X11')) || (strstr($user_agent, 'Mozilla')) || (strstr($user_agent, 'Netscape')) AND (!strstr($user_agent, 'MSIE')) AND (!strstr($user_agent, 'Konqueror')) AND (!strstr($user_agent, 'Firefox'))) $browser = 'Netscape';
elseif(strstr($user_agent, 'Firefox')) $browser = 'FireFox';
elseif(strstr($user_agent, 'MSIE')) $browser = 'MSIE';
elseif(strstr($user_agent, 'Lynx')) $browser = 'Lynx';
elseif(strstr($user_agent, 'Opera')) $browser = 'Opera';
elseif(strstr($user_agent, 'WebTV')) $browser = 'WebTV';
elseif(strstr($user_agent, 'Konqueror')) $browser = 'Konqueror';
elseif((strstr($user_agent, 'bot')) || (strstr($user_agent, 'Google')) || (strstr($user_agent, 'Slurp')) || (strstr($user_agent, 'Scooter')) || (strstr($user_agent, 'Spider')) || (strstr($user_agent, 'Infoseek'))) $browser = 'Bot';
else $browser = 'Other';

if(strstr($user_agent, 'Win')) $os = 'Windows';
elseif((strstr($user_agent, 'Mac')) || (strstr($user_agent, 'PPC'))) $os = 'Mac';
elseif(strstr($user_agent, 'Linux')) $os = 'Linux';
elseif(strstr($user_agent, 'FreeBSD')) $os = 'FreeBSD';
elseif(strstr($user_agent, 'SunOS')) $os = 'SunOS';
elseif(strstr($user_agent, 'IRIX')) $os = 'IRIX';
elseif(strstr($user_agent, 'BeOS')) $os = 'BeOS';
elseif(strstr($user_agent, 'OS/2')) $os = 'OS/2';
elseif(strstr($user_agent, 'AIX')) $os = 'AIX';
else $os = 'Other';

/* Save on the databases the obtained values */

$GLOBALS['db']->sql_query("UPDATE `".TABLE_COUNTER."` SET `count`=count+1 WHERE (`type`='total' AND `var`='hits') OR (`var`='$browser' AND `type`='browser') OR (`var`='$os' AND `type`='os')");
//update_points(13);

/* Start Detailed Statistics */

$dot        = date("d-m-Y-H");
$now        = explode ("-",$dot);
$nowHour    = $now[3];
$nowYear    = $now[2];
$nowMonth   = $now[1];
$nowDate    = $now[0];
$sql        = "SELECT `year` FROM `".TABLE_STATS_YEAR."` WHERE `year`='$nowYear'";
$resultyear = $GLOBALS['db']->sql_query($sql);
$jml        = $GLOBALS['db']->sql_numrows($resultyear);
if ($jml <= 0) {
	$sql = "INSERT INTO `".TABLE_STATS_YEAR."` VALUES ('$nowYear','0')";
	$GLOBALS['db']->sql_query($sql);
	for ($i=1;$i<=12;$i++) {
		$GLOBALS['db']->sql_query("INSERT INTO `".TABLE_STATS_MONTH."` VALUES ('$nowYear','$i','0')");
		if ($i == 1) $TotalDay = 31;
		if ($i == 2) {
			if (date("L") == true) {
				$TotalDay = 29;
			} else {
				$TotalDay = 28;
			}
		}
		if ($i == 3) $TotalDay = 31;
		if ($i == 4) $TotalDay = 30;
		if ($i == 5) $TotalDay = 31;
		if ($i == 6) $TotalDay = 30;
		if ($i == 7) $TotalDay = 31;
		if ($i == 8) $TotalDay = 31;
		if ($i == 9) $TotalDay = 30;
		if ($i == 10) $TotalDay = 31;
		if ($i == 11) $TotalDay = 30;
		if ($i == 12) $TotalDay = 31;
		for ($k=1;$k<=$TotalDay;$k++) {
			$GLOBALS['db']->sql_query("INSERT INTO `".TABLE_STATS_DATE."` VALUES ('$nowYear','$i','$k','0')");
		}
	}
}
$GLOBALS['db']->sql_freeresult($resultyear);

$sql = "SELECT `hour` FROM `".TABLE_STATS_HOUR."` WHERE (`year`='$nowYear') AND (`month`='$nowMonth') AND (`date`='$nowDate')";
$result  = $GLOBALS['db']->sql_query($sql);
$numrows = $GLOBALS['db']->sql_numrows($result);

if ($numrows <= 0) {
	for ($z = 0;$z<=23;$z++) {
		$GLOBALS['db']->sql_query("INSERT INTO `".TABLE_STATS_HOUR."` VALUES ('$nowYear','$nowMonth','$nowDate','$z','0')");
	}
}
$GLOBALS['db']->sql_freeresult($result);
$GLOBALS['db']->sql_query("UPDATE `".TABLE_STATS_YEAR."` SET `hits`=hits+1 WHERE `year`='$nowYear'");
$GLOBALS['db']->sql_query("UPDATE `".TABLE_STATS_MONTH."` SET `hits`=hits+1 WHERE (`year`='$nowYear') AND (`month`='$nowMonth')");
$GLOBALS['db']->sql_query("UPDATE `".TABLE_STATS_DATE."` SET `hits`=hits+1 WHERE (`year`='$nowYear') AND (`month`='$nowMonth') AND (`date`='$nowDate')");
$GLOBALS['db']->sql_query("UPDATE `".TABLE_STATS_HOUR."` SET `hits`=hits+1 WHERE (`year`='$nowYear') AND (`month`='$nowMonth') AND (`date`='$nowDate') AND (`hour`='$nowHour')");
