<?php
require_once("init.php");

$incdir = dirname(__FILE__);
define("INC", $incdir);

$tmpl = new Koobi("templates/".ADMINTHEME."/stats/");
$tmpl->assign("theme", ADMINTHEME);
$tmpl->assign("langadmin", $langadmin);
$tmpl->assign("area", $area);
$tmpl->assign("kversion", KVERSION);
$tmpl->assign('uid', UID);
$tmpl->assign('pref', $pref);

if( !permission('perm_stats')) { echo error($langadmin['noperms']); exit(); }


if($_REQUEST['action'] == "sitestats" || !$_REQUEST['action'] ) { 
	
	
	
	$sql = new dbc("SELECT SUM(hits) as ALLHITSS FROM " . PREFIX . "_sitestats");
	$row = $sql->fetchrow();
	$allhits = $row->ALLHITSS;
	
	$sql2 = new dbc("SELECT SUM(hits) as HITSTODAY FROM " . PREFIX . "_sitestats WHERE datum='".date("d-m-Y")."'");
	$rowt = $sql2->fetchrow();
	$todayhits = $rowt->HITSTODAY;
	
	$sql3 = new dbc("SELECT SUM(visits) as VISITSALL FROM " . PREFIX . "_sitestats");
	$rowa = $sql3->fetchrow();
	$allvisits = $rowa->VISITSALL;
	
	$sql4 = new dbc("SELECT SUM(visits) as VISITSTODAY FROM " . PREFIX . "_sitestats WHERE datum='".date("d-m-Y")."'");
	$rowto = $sql4->fetchrow();
	$visitstoday = $rowto->VISITSTODAY;
	
	$sql5 = new dbc("SELECT SUM(hits) as HITSMONTH, SUM(visits) as VISITSMONTH  FROM " . PREFIX . "_sitestats WHERE monat='".date("m")."'  AND jahr='".date("Y")."'");
	$rowtm = $sql5->fetchrow();
	$hitstmonth = $rowtm->HITSMONTH;
	$visitstmonth = $rowtm->VISITSMONTH;
	
	//--- Maximale Besucher
	$sql7 = new dbc("SELECT MAX(visits) as VISITSMAX FROM " . PREFIX . "_sitestats");
	$row_visits_max = $sql7->fetchrow();
	$visits_max = $row_visits_max->VISITSMAX;
	
	$sql8 = new dbc("SELECT datum  FROM " . PREFIX . "_sitestats WHERE visits='$visits_max'");
	$row_visits_max_datum = $sql8->fetchrow();
	$visits_max_datum = $row_visits_max_datum->datum;
	
	//--- Maximale Hits
	$sql9 = new dbc("SELECT MAX(hits) as HITSMAX FROM " . PREFIX . "_sitestats");
	$row_hits_max = $sql9->fetchrow();
	$hits_max = $row_hits_max->HITSMAX;
	
	$sql10= new dbc("SELECT datum  FROM " . PREFIX . "_sitestats WHERE hits='$hits_max'");
	$row_hits_max_datum = $sql10->fetchrow();
	$hits_max_datum = $row_hits_max_datum->datum;
	
	//--- Letzter Monat
	$letztermonat  = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
	$sql = new dbc("SELECT SUM(hits) as HITSMAX, SUM(visits) as VISITSMAX FROM " . PREFIX . "_sitestats WHERE monat='".date("m",$letztermonat)."' AND jahr='".date("Y",$letztermonat)."'");
	$row = $sql->fetchrow();
	$letztermonat_hits = $row->HITSMAX;
	$letztermonat_visits = $row->VISITSMAX;
	
	//--- Gestern
	$gestern  = mktime(0, 0, 0, date("m"), date("d")-1, date("Y"));
	$sql = new dbc("SELECT SUM(hits) as HITSGESTERN, SUM(visits) as VISITSGESTERN FROM " . PREFIX . "_sitestats WHERE tag='".date("d",$gestern)."' AND monat='".date("m",$gestern)."' AND jahr='".date("Y",$gestern)."'");
	$row = $sql->fetchrow();
	$gesternhits = $row->HITSGESTERN;
	$gesternvisits = $row->VISITSGESTERN;
	
	//--- Dieses Jahr
	$sql = new dbc("SELECT SUM(hits) as HITSTYEAR, SUM(visits) as VISITSTYEAR FROM " . PREFIX . "_sitestats WHERE  jahr='".date("Y")."'");
	$row = $sql->fetchrow();
	$thisyearhits = $row->HITSTYEAR;
	$thisyearvisits = $row->VISITSTYEAR;
	
	//--- Letztes Jahr
	$ly = date("Y")-1;
	$sql = new dbc("SELECT SUM(hits) as HITSLYEAR, SUM(visits) as VISITSLYEAR FROM " . PREFIX . "_sitestats WHERE  jahr='".$ly."'");
	$row = $sql->fetchrow();
	$lastyearhits = $row->HITSLYEAR;
	$lastyearvisits = $row->VISITSLYEAR;
	
	
	function GetGraph($type, $titel, $m, $y, $art, $was) {
		$daten = array();
		$arec = "";
		$arec2 = "";
		//--- Jahr
		if($type=="jahr"){
			$sql = new dbc("SELECT * FROM " . PREFIX . "_sitestats WHERE jahr='$y'");
			while ($row = $sql->fetchrow()) {
				$sql2 = new dbc("SELECT SUM(hits) AS DATA, SUM(visits) AS DATA2 FROM " . PREFIX . "_sitestats WHERE monat='$row->monat'");
				$row2 = $sql2->fetchrow();
				$daten[$row->monat] = $row2->DATA;
				$daten2[$row->monat] = $row2->DATA2;
			}
			$daten = fArray($daten);
			if($was == "norm"){
				for ($i=1; $i<=12; $i++) {
					$arec .= $i . ";" . $daten[$i] . "|";
				}
			} 
			$daten2 = fArray($daten2);
			if($was == "norm"){
				for ($i=1; $i<=12; $i++) {
					$arec2 .= $i . ";" . $daten2[$i] . "-";
				}
			} 
			
			if ($was == "named"){
				$arec .= "Jan." . ";" . $daten[1] . "|";
				$arec .= "Feb." . ";" . $daten[2] . "|";
				$arec .= "Mar." . ";" . $daten[3] . "|";
				$arec .= "Apr." . ";" . $daten[4] . "|";
				$arec .= "Mai." . ";" . $daten[5] . "|";
				$arec .= "Jun." . ";" . $daten[6] . "|";
				$arec .= "Jul." . ";" . $daten[7] . "|";
				$arec .= "Aug." . ";" . $daten[8] . "|";
				$arec .= "Sep." . ";" . $daten[9] . "|";
				$arec .= "Okt." . ";" . $daten[10] . "|";
				$arec .= "Nov." . ";" . $daten[11] . "|";
				$arec .= "Dez." . ";" . $daten[12] . "|";
				
				$arec2 .= "Jan." . ";" . $daten2[1] . "-";
				$arec2 .= "Feb." . ";" . $daten2[2] . "-";
				$arec2 .= "Mar." . ";" . $daten2[3] . "-";
				$arec2 .= "Apr." . ";" . $daten2[4] . "-";
				$arec2 .= "Mai." . ";" . $daten2[5] . "-";
				$arec2 .= "Jun." . ";" . $daten2[6] . "-";
				$arec2 .= "Jul." . ";" . $daten2[7] . "-";
				$arec2 .= "Aug." . ";" . $daten2[8] . "-";
				$arec2 .= "Sep." . ";" . $daten2[9] . "-";
				$arec2 .= "Okt." . ";" . $daten2[10] . "-";
				$arec2 .= "Nov." . ";" . $daten2[11] . "-";
				$arec2 .= "Dez." . ";" . $daten2[12] . "-";
			} 
			//---- Jahr //
			
		}
		//---- Monat
		if($type=="monat"){
			$sql = new dbc("SELECT * FROM " . PREFIX . "_sitestats WHERE jahr='$y' AND monat='$m'");
			while ($row = $sql->fetchrow()) {
				$sql2 = new dbc("SELECT $art AS DATA, visits AS DATA2 FROM " . PREFIX . "_sitestats WHERE tag='$row->tag'");
				$row2 = $sql2->fetchrow();
				$daten[$row->tag] = $row2->DATA;
				$daten2[$row->tag] = $row2->DATA2;
			}
			
			
			$daten = fArray($daten);
			if($was == "norm"){
				for ($i=1; $i<=31; $i++) {
					$arec .= $i . ";" . $daten[$i] . "|";
				}
			} 
			
			
			$daten2 = fArray($daten2);
			if($was == "norm"){
				for ($i=1; $i<=31; $i++) {
					$arec2 .= $i . ";" . $daten2[$i] . "-";
				}
			} 
			
			//---- Monat //
		}
		$arec = substr($arec,0,strlen($arec)-1);
		$arec2 = substr($arec2,0,strlen($arec2)-1);
		$titel = base64_encode($titel);
		$arec = base64_encode($arec);
		$arec2 = base64_encode($arec2);
		return ("chart.php?width=720&height=180&titel=$titel&daten=$arec&daten2=$arec2");
	}
	function fArray($arr) {
		for ($i=1; $i<=31; $i++) {
			if (!isset($arr[$i])) {
				$arr[$i] = 0;
			}
		}
		return $arr;
	}
	
	if(!$_REQUEST['sy']) $sy = date("Y"); else $sy = $_REQUEST['sy'];
	if(!$_REQUEST['sm']) $sd = date("m"); else $sd = $_REQUEST['sm'];
	
	$tmpl->assign('allhits', $allhits);
	$tmpl->assign('todayhits', $todayhits);
	$tmpl->assign('gesternhits', $gesternhits);
	$tmpl->assign('hitstmonth', $hitstmonth);
	$tmpl->assign('letztermonat_hits', $letztermonat_hits);
	$tmpl->assign('thisyearhits', $thisyearhits);
	$tmpl->assign('lastyearhits', $lastyearhits);
	$tmpl->assign('hits_max', $hits_max);
	$tmpl->assign('hits_max_datum', $hits_max_datum);
	$tmpl->assign('allvisits', $allvisits);
	$tmpl->assign('visitstoday', $visitstoday);
	$tmpl->assign('gesternvisits', $gesternvisits);
	$tmpl->assign('visitstmonth', $visitstmonth);
	$tmpl->assign('letztermonat_visits', $letztermonat_visits);
	$tmpl->assign('thisyearvisits', $thisyearvisits);
	$tmpl->assign('thisyearvisits', $thisyearvisits);
	$tmpl->assign('lastyearvisits', $lastyearvisits);
	$tmpl->assign('visits_max', $visits_max);
	$tmpl->assign('visits_max_datum', $visits_max_datum);
	
	$tmpl->assign('graph1', GetGraph("jahr", $a_lang['texthitsvisits']." ".$sy, "", $sy, "hits", "named"));
	$tmpl->assign('graph2', GetGraph("monat", $a_lang['texthitsvisits']." ".$sd.".".$sy, $sd, $sy, "hits", "norm"));
	$tmpl->assign('starty', date("Y")-6);
	$tmpl->assign('end', date("Y"));
	$tmpl->assign('month', date("m"));
	$tmpl->display('graph.tpl');
}

if ($_REQUEST['action'] == "referer") { 
	
	if($_REQUEST['subaction'] == "del") { $sql = new dbc("DELETE FROM " . PREFIX . "_referer ");}
	
	$sort = ($_REQUEST["sort"] == "") ? "id" : $_REQUEST["sort"];
	$order = ($_REQUEST["order"] == "") ? "DESC" : $_REQUEST["order"];
	
	$limit = ($_REQUEST['pp']!='') ? $_REQUEST['pp'] : '20';
	$sql = new dbc("SELECT id FROM " . PREFIX . "_referer ORDER BY $sort $order");
	$num  = $sql->numrows();
	
	$seiten = ceil($num / $limit);
	$a = prepage() * $limit - $limit;
	
	$sql = new dbc("SELECT id, user_os, user_ua, user_referer, DATE_FORMAT(datum, '%d-%m-%Y %H:%i') AS datum FROM " . PREFIX . "_referer ORDER BY $sort $order limit $a,$limit");
	
	
	if($num > $limit) $tmpl->assign('navi', pagenav($seiten, $lang['aktpos'], " <a class=\"pnav\" href=\"stats.php?pp=$_REQUEST[pp]&amp;sort=$_REQUEST[sort]&amp;order=$_REQUEST[order]&amp;action=referer&amp;page={s}\">{t}</a> "));
	$items = array();
	
	while ($row = $sql->fetchrow()) { array_push($items,$row); }
	
	$tmpl->assign('items', $items);
	$tmpl->display('refs.tpl');
}
?>