<?php
require('functions.php');
$config = loadConfig();
unlock_all_files();
session_start();
if(o_login_check($_SESSION['user'],$_SESSION['password'],false)) {
header('Content-Type: application/json');
$file = file('log.txt');
if($_GET['context']=='map') {
$country = array();
if($_GET['data']=='calls') {
foreach($file AS $call) {
$call = explode(', ',$call);
$country[] = $call[14];
}
$country = array_count_values($country);
$country['sum'] = array_sum($country);
echo json_encode($country);
}
if($_GET['data']=='users') {
$ips = array();
foreach($file AS $call) {
$call = explode(', ',$call);
if(!in_array($call[1],$ips)) {
$country[] = $call[14];
}
$ips[] = $call[1];
}
$country = array_count_values($country);
$country['sum'] = array_sum($country);
echo json_encode($country);
}
if($_GET['data']=='products') {
	$products = array();
	$file = file('ecommerce.txt');
	foreach($file AS $line) {
		$line = explode(', ',$line);
		$products[trim($line[5])]++;
	}
	$products['sum'] = array_sum($products);
	echo json_encode($products);
}
if($_GET['data']=='product') {
	$products = array();
	$file = file('ecommerce.txt');
	foreach($file AS $line) {
		$line = explode(', ',$line);
		if(trim($line[1])==$_GET['product']) $products[trim($line[5])]++;
	}
	$products['sum'] = array_sum($products);
	echo json_encode($products);
}
}
if($_GET['context']=='daytimeline') {
$days = array();
$alloweddays = array();
for($i = 0; $i <= $_GET['back']; $i++) {
$alloweddays[] = date("d.m.Y",(isset($_GET['reference']) ? strtotime($_GET['reference']) : time())-($i*86400));
}
for ($i = 0; $i <= $_GET['back']; $i++) {
	$days[$alloweddays[$i]] = 0;
}
if(!isset($_GET['sortedby'])) {
	foreach($file AS $call) {
		$call = explode(', ',$call);
		$call[9] = substr($call[9],0,10);
		if(in_array(date("d.m.Y",strtotime($call[9])), $alloweddays) AND ($_GET['data']=='calls' OR $_GET['data']=='used_time')) {
			$days[substr($call[9],0,10)]++;
		}
		if(!in_array($call[1],$ips) AND in_array(date("d.m.Y",strtotime($call[9])), $alloweddays) AND $_GET['data']=='users') {
			$ips[] = $call[1];
			$days[date("d.m.Y",strtotime($call[9]))]++;
		}
		if($_GET['data']=='used_time' AND in_array(date("d.m.Y",strtotime($call[9]),$alloweddays))) $timeperday[date("d.m.Y",strtotime($call[9]))] = $timeperday[date("d.m.Y",strtotime($call[9]))] + $call[8];
	}
	if($_GET['data']=='used_time') {
		foreach(array_keys($days) AS $day) {
			if(isset($timeperday[$day])) $timeperday[$day] = $timeperday[$day] / $days[$day]; else $timeperday[$day] = 0;
		}
		$days = $timeperday;
	}
	echo json_encode($days);
}
if($_GET['sortedby']=='country') {
foreach($file AS $call) {
$call = explode(', ',$call);
if($call[14]==$_GET['country'] AND in_array(date("d.m.Y",strtotime($call[9])), $alloweddays) AND $_GET['data']=='calls') {
$days[date("d.m.Y",strtotime($call[9]))]++;
}
if(!in_array($call[1],$ips) AND $call[14]==$_GET['country'] AND in_array(date("d.m.Y",strtotime($call[9])), $alloweddays) AND $_GET['data']=='users') {
$ips[] = $call[1];
$days[date("d.m.Y",strtotime($call[9]))]++;
}
}
if($_GET['data']=='products') {
	$file = file('ecommerce.txt');
	foreach($file AS $line) {
		$line = explode(', ',$line);
		if($_GET['country']==trim($line[5]) AND in_array(substr($line[0],0,10), $alloweddays)) $days[substr($line[0],0,10)]++;;
	}
}
$days['max'] = max($days) or $days['max'] = 0;
echo json_encode($days);
}
}
if($_GET['context']=='monthtimeline') {
	$months = array('Januar'=>0,
	'Februar'=>0,
	'März'=>0,
	'April'=>0,
	'Mai'=>0,
	'Juni'=>0,
	'Juli'=>0,
	'August'=>0,
	'September'=>0,
	'Oktober'=>0,
	'November'=>0,
	'Dezember'=>0);
	foreach($file AS $call) {
		$call = explode(', ',$call);
		$time = explode('.',$call[9])[1];
		$time = ($time[0]=='0' ? substr($time,1) : $time);
		$month = str_replace('9','September',str_replace('8','August',str_replace('7','Juli',str_replace('6','Juni',str_replace('5','Mai',str_replace('4','April',str_replace('3','März',str_replace('2','Februar',str_replace('1','Januar',str_replace('12','Dezember',str_replace('11','November',str_replace('10','Oktober',$time))))))))))));
		if($_GET['data']=='calls') if(substr(explode('.',$call[9])[2],0,strpos(explode('.',$call[9])[2],' '))==$_GET['reference']) $months[$month]++;
		if($_GET['data']=='users') {
			if(!in_array($call[1],$ips[$month])) {
				$months[$month]++;
				$ips[$month][] = $call[1];
			}
		}
	}
	echo json_encode($months);
}
if($_GET['context']=='yeartimeline') {
	$allowedyears = array();
	for($i = 0; $i <= $_GET['back']; $i++) {
		$allowedyears[] = date("Y",(isset($_GET['reference']) ? strtotime($_GET['reference']) : time())-($i*31536000));
	}
	for ($i = 0; $i <= $_GET['back']; $i++) {
		$years[$allowedyears[$i]] = 0;
	}
		foreach($file AS $call) {
			$call = explode(', ',$call);
			$year = explode('.',$call[9])[2];
			$year = substr($year,0,strpos($year,' '));
			if($_GET['data']=='calls' AND $year==$_GET['reference'] AND in_array($year,$allowedyears)) $years[$year]++;
			if($_GET['data']=='users' AND $year==$_GET['reference'] AND in_array($year,$allowedyears) AND !in_array($call[1],$ips[$year])) {
				$years[$year]++;
				$ips[$year][] = $call[1];
			}
		}
		echo json_encode($years);
}
if($_GET['context']=='time') {
if($_GET['data']=='live') {
$data = unserialize(file_get_contents('liveupdate.txt'));
foreach(array_keys($data) AS $key) {
if(intval($data[$key]["timestamp"] + 12) <= time()) {
unset($data[$key]);
}
}
foreach(array_keys($data) AS $key) {
$data[$key]['browser'] = get_browser($data[$key]['useragent'])->browser;
$data[$key]['platform'] = get_browser($data[$key]['useragent'])->platform;
$data[$key]['startstring'] = date("d.m.Y H:i",$data[$key]['start']);
$data[$key]['timestring'] = date("d.m.Y H:i",$data[$key]['timestamp']);
}
echo json_encode($data);
}
}
}
else {
echo 'failed';
}
if($_GET['context']=='day' AND $_GET['data']=='general') {
	echo json_encode(array('users'=>visitors_per_day_strict($_GET['reference']),'views'=>visitors_per_day($_GET['reference'])));
}
?>