<?php

if (!defined('N')) {
	echo('You probably meant <a href="'.preg_replace('/\.php$/','',$_SERVER['REQUEST_URI']).'">this</a>.');
	exit(0);
}

$lc=localeconv();
require '../lib/fnum.php';
require '../lib/realfloat.php';// had to resort to this because php 7.3 has a bug: when echoing a float, it uses the decimal separator of the locale that was set with setlocale, making a mess when the separator is different than "." with stuff expecting it to be "."
require '../lib/supplangs.php';

echo('</nav>
<div class="scrwide">
<div class="scrwidein">
<section class="sectcontm">
<h3>'._('Statistics for the last 30 days').'</h3>
<script language="JavaScript">
</script>
'.N);

function sp($num,$sing,$plur) {
	if ($num==1)
		return($sing);
	else
		return($plur);
}

$link=mysqli_connect($conf['db_host'],$conf['db_user_name'],$conf['db_user_password'],$conf['db_name'],$conf['db_port'],$conf['db_socket']) or muorimeglio(_('Couldn’t connect to database: ').mysqli_connect_error().' ['.mysqli_connect_errno().']',false);
mysqli_set_charset($link,'utf8mb4');

$now=time();
$tdstart=gmmktime(0,0,0,gmdate('n',$now),gmdate('j',$now),gmdate('Y',$now));
$limit=$tdstart-(30*24*60*60);
$res=mysqli_query($link,'SELECT MAX(Visits) AS MaxVisits, MAX(Hits) AS MaxHits FROM ZStats WHERE TS >= '.$limit) or muorimeglio(__LINE__.': '.mysqli_error($link),true);
$row=mysqli_fetch_assoc($res);
$maxvisits=$row['MaxVisits'];
$maxhits=$row['MaxHits'];
$res=mysqli_query($link,'SELECT * FROM ZStats WHERE TS >= '.$limit.' ORDER BY TS DESC') or muorimeglio(__LINE__.': '.mysqli_error($link),true);
$hpmap=array(
	'home'=>_('Guide'),
	'instances'=>_('Instances'),
	'users'=>_('Users'),
	'links'=>_('Links'),
	'about'=>_('About'),
	'stats'=>_('Statistics'),
	'contribute'=>_('Contribute'),
	'404'=>'404'
);
while ($row=mysqli_fetch_assoc($res)) {
	$hl=explode(';',$row['HitsLang']);
	$buf=array();
	foreach($hl as $key=>$val) {
		$val=explode(':',$val);
		if (array_key_exists($val[0],$supplangs))// needed when you (temporarily) subtract a lang code from $supplangs in lib/supplangs.php
			$buf[$supplangs[$val[0]]['trname']]=$val[1];
	}
	arsort($buf);
	$hl='';
	foreach($buf as $key=>$val) $hl.=$key.': '.fnum($val,$lc,0).'; ';
	$hl=_('Hits per language').': '.substr($hl,0,-2);

	$hp=explode(';',$row['HitsPage']);
	$buf=array();
	foreach($hp as $key=>$val) {
		$val=explode(':',$val);
		$buf[$hpmap[$val[0]]]=$val[1];
	}
	arsort($buf);
	$hp='';
	foreach($buf as $key=>$val) $hp.=$key.': '.fnum($val,$lc,0).'; ';
	$hp=_('Hits per page').': '.substr($hp,0,-2);

	echo('<div class="info1st">'.gmdate('d/m/Y',$row['TS']).': '.fnum($row['Visits'],$lc,0).' <span class="visitslab">'.sp($row['Visits'],_('visit'),_('visits')).'</span>, '.fnum($row['Hits'],$lc,0).' <span class="hitslab">'.sp($row['Hits'],_('hit'),_('hits')).'</span></div>'.N);
	echo('<div class="info">'.$hl.'</div>'.N);
	echo('<div class="info">'.$hp.'</div>'.N);
	echo('<div class="visits" style="width:'.rf($row['Visits']*100/$maxvisits).'%"></div>'.N);
	echo('<div class="hits" style="width:'.rf($row['Hits']*100/$maxhits).'%"></div>'.N);
}

mysqli_close($link);

echo('</section>
</div>
</div>'.N);

?>
