<?php
class Stats
{
	function getChart()
	{
		$jahr  = (isset($_POST['jahr']) && !empty($_POST['jahr'])) ? abs((int)$_POST['jahr']) : date('Y');
		$monat = (isset($_POST['monat']) && !empty($_POST['monat'])) ? abs((int)$_POST['monat']) : date('m');
		$day_count = date('t');
		
		$_POST['jahr'] = $jahr;
		$_POST['monat'] = $monat;
		
		for($y=1; $y<=12; $y++)
		{
			$res_mon[$y] = $GLOBALS['db']->ArrQuery("SELECT  SUM(Gesamt_Wert) AS Alle,SUM(Hits) AS Hits FROM  " . PREFIX . "_counter_werte WHERE Jahr='" . $jahr . "' AND Monat='".$y."'");
		}
		
		for($x=1; $x<=$day_count; $x++)
		{
			$res_day[$x] = $GLOBALS['db']->ArrQuery("SELECT  SUM(Gesamt_Wert) AS Alle,SUM(Hits) AS Hits FROM  " . PREFIX . "_counter_werte WHERE Jahr='" . $jahr . "' AND Tag='" . $x. "' AND Monat='" . $monat . "'");
		}
		
		$res_w98 = $GLOBALS['db']->RowQuery("SELECT  COUNT(Id) AS Wert FROM  " . PREFIX . "_counter_referer WHERE Os='Windows 98'");
		$res_wNT = $GLOBALS['db']->RowQuery("SELECT  COUNT(Id) AS Wert FROM  " . PREFIX . "_counter_referer WHERE Os='Windows NT'");
		$res_wXP = $GLOBALS['db']->RowQuery("SELECT  COUNT(Id) AS Wert FROM  " . PREFIX . "_counter_referer WHERE Os='Windows XP'");
		$res_wXP64 = $GLOBALS['db']->RowQuery("SELECT  COUNT(Id) AS Wert FROM  " . PREFIX . "_counter_referer WHERE Os='Windows XP Professional x64 Edition'");
		$res_wVISTA = $GLOBALS['db']->RowQuery("SELECT  COUNT(Id) AS Wert FROM  " . PREFIX . "_counter_referer WHERE Os='Windows Vista'");
		$res_wWIN = $GLOBALS['db']->RowQuery("SELECT  COUNT(Id) AS Wert FROM  " . PREFIX . "_counter_referer WHERE Os='Windows'");
		$res_wMAC = $GLOBALS['db']->RowQuery("SELECT  COUNT(Id) AS Wert FROM  " . PREFIX . "_counter_referer WHERE Os='Mac OS'");
		$res_wLINUX = $GLOBALS['db']->RowQuery("SELECT  COUNT(Id) AS Wert FROM  " . PREFIX . "_counter_referer WHERE Os='Linux'");
		$res_wUNIX = $GLOBALS['db']->RowQuery("SELECT  COUNT(Id) AS Wert FROM  " . PREFIX . "_counter_referer WHERE Os='Unix'");
		$res_wOTHER = $GLOBALS['db']->RowQuery("SELECT  COUNT(Id) AS Wert FROM  " . PREFIX . "_counter_referer WHERE Os='unknown'");
	
		$GLOBALS['tmpl']->assign('w98', $res_w98->Wert);
		$GLOBALS['tmpl']->assign('NT', $res_wNT->Wert);
		$GLOBALS['tmpl']->assign('XP', $res_wXP->Wert);
		$GLOBALS['tmpl']->assign('XP64', $res_wXP64->Wert);
		$GLOBALS['tmpl']->assign('VISTA', $res_wVISTA->Wert);
		$GLOBALS['tmpl']->assign('WIN', $res_wWIN->Wert);
		$GLOBALS['tmpl']->assign('MAC', $res_wMAC->Wert);
		$GLOBALS['tmpl']->assign('LINUX', $res_wLINUX->Wert);
		$GLOBALS['tmpl']->assign('UNIX', $res_wUNIX->Wert);
		$GLOBALS['tmpl']->assign('OTHER', $res_wOTHER->Wert);
		$GLOBALS['tmpl']->assign('res_mon', $res_mon);
		$GLOBALS['tmpl']->assign('res_day', $res_day);
		$GLOBALS['tmpl']->assign('start', date('Y')-10);
		$GLOBALS['tmpl']->assign('end', date('Y'));
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Stats']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/stats/overview.tpl'));
	}
	
	function getReferer()
	{
		$where_not = 'http://' . $_SERVER['HTTP_HOST'] . str_replace('admin/index.php','',$_SERVER['PHP_SELF']);
		$db_sort      = " ORDER BY Datum_Int ASC";
		$nav_sort   = "&amp;sort=name_asc";
		$datesort = '';
		$def_search_n = '';
		$def_search = '';
		$namesort = '';
		$ossort = '';
		$browsersort = '';
		$ipsort = '';
		
		if(isset($_GET['del']) && $_GET['del']==1 && perm('referer_del'))
		{
			$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_counter_referer");
		}
		
		switch(@$_REQUEST['sort'])
		{
			case '': default:
			case'date_desc':$db_sort='ORDER BY Datum_Int DESC';$nav_sort='&amp;sort=date_desc';$datesort='date_asc';break;
			case'date_asc':$db_sort='ORDER BY Datum_Int ASC';$nav_sort='&amp;sort=date_asc';$datesort='date_desc';break;
			
			case'name_asc':$db_sort = 'ORDER BY Referer ASC';$nav_sort = '&amp;sort=name_asc';$namesort = 'name_desc';break;
			case'name_desc':$db_sort='ORDER BY Referer DESC';$nav_sort='&amp;sort=name_desc';$namesort='name_asc';break;
			
			case'os_asc':$db_sort = 'ORDER BY Os ASC';$nav_sort = '&amp;sort=os_asc';$ossortt = 'os_desc';break;
			case'os_desc':$db_sort='ORDER BY Os DESC';$nav_sort ='&amp;sort=os_desc';$ossort='os_asc';break;
			
			case'browser_asc':$db_sort = 'ORDER BY Os ASC';$nav_sort = '&amp;sort=browser_asc';$browsersort = 'browser_desc';break;
			case'browser_desc':$db_sort='ORDER BY Os DESC';$nav_sort ='&amp;sort=browser_desc';$browsersort ='browser_asc';break;
			
			case'ip_asc':$db_sort = 'ORDER BY IPAdresse ASC';$nav_sort = '&amp;sort=ip_asc';$ipsort = 'ip_desc';break;
			case'ip_desc':$db_sort='ORDER BY IPAdresse DESC';$nav_sort ='&amp;sort=ip_desc';$ipsort ='ip_asc';break;
		}
		
		if(isset($_REQUEST['q']) && !empty($_REQUEST['q']) && $_REQUEST['q'] != 'empty' && strlen($_REQUEST['q'])>=3)
		{
			$pattern = preg_replace('/([^-A-Za-zÀ-ÖØ-öø-ÿ0-9,_.:\/&=? ]*)/', '', $_REQUEST['q']);
			$_REQUEST['q']  = $pattern;
			$def_search_n   = "&amp;q=" . urlencode($pattern);
			$def_search     = " AND (Ua='{$pattern}' OR Os='{$pattern}' OR Referer LIKE '%{$pattern}%' OR IPAdresse='{$pattern}') ";
		}
		
		$GLOBALS['tmpl']->assign('ipsort', $ipsort);
		$GLOBALS['tmpl']->assign('browsersort', $browsersort);
		$GLOBALS['tmpl']->assign('ossort', $ossort);
		$GLOBALS['tmpl']->assign('namesort', $namesort);
		$GLOBALS['tmpl']->assign('datesort', $datesort);

		$query = $GLOBALS['db']->Query("SELECT Id FROM " . PREFIX . "_counter_referer WHERE Referer NOT LIKE '{$where_not}%%' {$def_search} {$db_sort}");
		$num = $query->numrows();
		
		$limit = getLimit(25);
		$seiten = ceil($num / $limit);
		$a = prepage() * $limit - $limit;
		
		$referer = array();
		$sql = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_counter_referer WHERE Referer NOT LIKE '{$where_not}%%' {$def_search} {$db_sort} LIMIT $a,$limit");
		while($row = $sql->fetchrow())
		{
			
			array_push($referer,$row);
		}
		
		// Seiten-Navigation
		if ($num > $limit) 
		{
			$GLOBALS['tmpl']->assign('Navi', pagenav($seiten, '', " <a class=\"page_navigation\" href=\"index.php?do=stats&sub=referer{$def_search_n}{$nav_sort}&amp;pp={$limit}&amp;page={s}&amp;noframes=1\">{t}</a> "));
		}
		
		
		$GLOBALS['tmpl']->assign('referer', $referer);
		$GLOBALS['tmpl']->assign('limit', $limit);
		$GLOBALS['tmpl']->assign('def_sort_n', $nav_sort);
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/stats/referer.tpl'));
	}
}
?>