<?php
/*-------------------------------------------------------+
| PHP-Fusion Content Management System
| Copyright © 2002 - 2008 Nick Jones
| http://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Filename: did_webstats_panel.php
| Author: Titus (diphputz) Kaptein
| Website: http://www.diphputz.de/
| © 2012 by Titus Kaptein
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html. Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/
if (!defined("IN_FUSION")) { die("Access Denied"); }

include INFUSIONS."did_webstats_panel/infusion_db.php";

// Check if locale file is available matching the current site locale setting.
if (file_exists(INFUSIONS."did_webstats_panel/locale/".$settings['locale'].".php")) {
	// Load the locale file matching the current site locale setting.
	include INFUSIONS."did_webstats_panel/locale/".$settings['locale'].".php";
} else {
	// Load the infusion's default locale file.
	include INFUSIONS."did_webstats_panel/locale/English.php";
}

//|--------------------------------------------------------------------------------------------------------------------------------------------|\\
//|--- OPEN STATS PANEL ABFRAGE                                                                                                            |\\
//|--------------------------------------------------------------------------------------------------------------------------------------------|\\


if (dbcount("(online_user)", DB_ONLINE, (iMEMBER ? "online_user='".$userdata['user_id']."'" : "online_user='0' AND online_ip='".USER_IP."'")) == 1) {
	$result = dbquery(
		"UPDATE ".DB_ONLINE." SET online_lastactive='".time()."', online_ip='".USER_IP."'
		WHERE ".(iMEMBER ? "online_user='".$userdata['user_id']."'" : "online_user='0' AND online_ip='".USER_IP."'"));
} else {
	$result = dbquery(
		"INSERT INTO ".DB_ONLINE." (online_user, online_ip, online_ip_type, online_lastactive) 
		VALUES ('".(iMEMBER ? $userdata['user_id'] : 0)."', '".USER_IP."', '".USER_IP_TYPE."', '".time()."')");
}
$result = dbquery("DELETE FROM ".DB_ONLINE." WHERE online_lastactive<".(time()-60)."");


//|--------------------------------------------------------------------------------------------------------------------------------------------|\\
//|--- GLOBALE VARIABLEN FESTLEGEN                                                                                                             |\\
//|--------------------------------------------------------------------------------------------------------------------------------------------|\\

$actdate = date('Y').date('m').date('d');
$actstd = date('H') + ($settings['serveroffset'] * 3600);
$actpage = $_SERVER['REQUEST_URI'];
if (stripos($_SERVER['REQUEST_URI'], "?")){
	$actpage_noget = substr($_SERVER['REQUEST_URI'],0 ,stripos($_SERVER['REQUEST_URI'], "?"));
} else {
	$actpage_noget = $actpage;
}
$actip = $_SERVER['REMOTE_ADDR'];
$statset = array("active" => 0, "puplicstats" => 0);

//|--------------------------------------------------------------------------------------------------------------------------------------------|\\
//|--- Settings laden                                                                                                                          |\\
//|--------------------------------------------------------------------------------------------------------------------------------------------|\\

$statsettings = dbquery("SELECT * FROM ".DB_DID_WEBSTATS."settings");
while ($statseting = dbarray($statsettings)) {
	$statset = array(
		"active" => $statseting['active'],
		"puplicstats" => $statseting['puplicstats'],
	);
}

$reswebsettings = dbquery("SELECT * FROM ".DB_DID_WEBSTATS."settings");
while ($statsetting = dbarray($reswebsettings)) {
	$websettings = array(
		"lastweek" => $statsetting['lastweek'],
		"clickslastdays" => $statsetting['clickslastdays'],
		"clicksperhour" => $statsetting['clicksperhour'],
		"clickspervisit" => $statsetting['clickspervisit'],
		"active" => $statsetting['active'],
		"puplicstats" => $statsetting['puplicstats'],
		"showallvisitors" => $statsetting['showallvisitors'],
		"barcolor" => $statsetting['barcolor'],
		"sitemapgetvars" => $statsetting['sitemap_getvars']
	);
}

//|--------------------------------------------------------------------------------------------------------------------------------------------|\\
//|--- ADMINISTRATIONSPARAMETER VERARBEITEN                                                                                           		   |\\
//|--------------------------------------------------------------------------------------------------------------------------------------------|\\

if (isset($_GET['aid']) && isset($nocount) && $_GET['aid'] == iAUTH && $nocount == 1){
	$statset['active'] = 0;	
}
if (isset($_GET['aid']) && isset($nocount) && $_GET['aid'] == iAUTH && $nocount == 1){
	$statset['puplicstats'] = 1;	
}

//|--------------------------------------------------------------------------------------------------------------------------------------------|\\
//|--- DATEN DES NEUEN WEBSEITENAUFRUFS VERARBEITEN                                                                                            |\\
//|--------------------------------------------------------------------------------------------------------------------------------------------|\\

if ($statset['active'] == 1){

	$result0 = dbquery("SELECT * FROM ".DB_DID_WEBSTATS."visits WHERE date = '".$actdate."'");
	$num = dbrows($result0);

	if ($num == 0){
		$result01 = dbquery("INSERT INTO ".DB_DID_WEBSTATS."visits (`date`, `hits`) 
							VALUES ('".$actdate."', 1);");
	}
	else {
		$result01 = dbquery("UPDATE ".DB_DID_WEBSTATS."visits SET hits = hits + 1 WHERE date = '".$actdate."'");
	}
	
	$result00 = dbquery("SELECT * FROM ".DB_DID_WEBSTATS."ips WHERE date = '".$actdate."' and ip = '".substr(md5($actip), date('N'), 15)."'");
	$num1 = dbrows($result00);
	if ($num1 == 0) {
		$result00 = dbquery("UPDATE ".DB_DID_WEBSTATS."visits SET visits = visits + 1 WHERE date = '".$actdate."'");
		$result00 = dbquery("INSERT INTO ".DB_DID_WEBSTATS."ips (`id`, `date`, `ip`) 
							VALUES (NULL, '".$actdate."', '".substr(md5($actip), date('N'), 15)."');");
	}

	//>--- sitemap daten auswerten ---<\\
	
	$counted = false;
	
	if ($websettings['sitemapgetvars'] != ""){
		$getpages_pre = explode("\n", $websettings['sitemapgetvars']."\n", -1);
		foreach ($getpages_pre as $getpages_sec) {
			$page_name = substr($getpages_sec, 0, stripos($getpages_sec, ":"));
			$get_var_name = preg_replace("/[^a-z_]/", "", substr($getpages_sec, stripos($getpages_sec, ":")+1) );
			if (stripos($actpage, $page_name) !== false){
				if (isset($_GET[$get_var_name])){
					$result_sitemap = dbquery("SELECT * FROM ".DB_DID_WEBSTATS."sitemap WHERE path = '".$actpage."' LIMIT 1");
					if (dbrows($result_sitemap) == 0){
						$result00 = dbquery("INSERT INTO ".DB_DID_WEBSTATS."sitemap (`path`, `name`, `clicks`) 
											VALUES ('".$actpage."', '', 1);");
					} else {
						$result00 = dbquery("UPDATE ".DB_DID_WEBSTATS."sitemap SET clicks = clicks + 1 WHERE path = '".$actpage."'");
					}
					$counted = true;
				}
			}
			else {
			}
		}
	}
	if (!$counted){
		$result_sitemap = dbquery("SELECT * FROM ".DB_DID_WEBSTATS."sitemap WHERE path = '".$actpage_noget."' LIMIT 1");
		if (dbrows($result_sitemap) == 0){
			$result00 = dbquery("INSERT INTO ".DB_DID_WEBSTATS."sitemap (`path`, `name`, `clicks`) 
								VALUES ('".$actpage_noget."', '', 1);");
		} else {
			$result00 = dbquery("UPDATE ".DB_DID_WEBSTATS."sitemap SET clicks = clicks + 1 WHERE path = '".$actpage_noget."'");
		}
	}
}

//|--------------------------------------------------------------------------------------------------------------------------------------------|\\
//|--- Alte IPs Löschen                                                                                                                		   |\\
//|--------------------------------------------------------------------------------------------------------------------------------------------|\\

$result_del = dbquery("DELETE FROM ".DB_DID_WEBSTATS."ips WHERE date != '".$actdate."'");

//|--------------------------------------------------------------------------------------------------------------------------------------------|\\
//|--- DATENAUSGABE VORBEREITEN                                                                                                                |\\
//|--------------------------------------------------------------------------------------------------------------------------------------------|\\

if ($statset['puplicstats'] == 1){

$num_vis = array();
$num_cli = array();

$result = dbquery("SELECT id,SUM(hits) FROM ".DB_DID_WEBSTATS."visits");
while ($data = dbarray($result)) {
	$allhits = $data['SUM(hits)'];
}

$result = dbquery("SELECT id,SUM(visits) FROM ".DB_DID_WEBSTATS."visits");
while ($data = dbarray($result)) {
	$allvisits = $data['SUM(visits)'];
}

for ($n = 0; $n <=2; $n++){
	$num_vis[$n] = 0;
		$num_cli[$n] = 0;
	$result0 = dbquery("SELECT * FROM ".DB_DID_WEBSTATS."visits WHERE date = '".($actdate - $n)."'");
	while ($data4 = dbarray($result0)) {
		$num_vis[$n] = $data4['visits'];
		$num_cli[$n] = $data4['hits'];
	}
}

//|---

if ($allvisits != 0){
	$hitpervis = $allhits / $allvisits;
}
else {
	$hitpervis = 0;
}

$hitpervis = round($hitpervis, 2);

//|---

$result6 = dbquery("SELECT * FROM ".DB_DID_WEBSTATS."visits ORDER BY 'id' ASC LIMIT 1");
while ($data6 = dbarray($result6)) {
	$begindate = $data6['date'];
}
$counttime = ($begindate - $actdate)+($actstd/24);
if ($counttime == 0){
	$counttime = 0.001;
}
$hitsperstd = $allhits/$counttime;
$hitsperstd = $hitsperstd/24;
$hitsperstd = round($hitsperstd, 2);

if ($hitsperstd < 0){
	$hitsperstd = $hitsperstd * -1;
}

//|---

$wday = date('w')+7;

$datevis = array();

$datevispc = array();

$wochentag = array();
$wochentag['1'] = "MO";
$wochentag['2'] = "DI";
$wochentag['3'] = "MI";
$wochentag['4'] = "DO";
$wochentag['5'] = "FR";
$wochentag['6'] = "SA";
$wochentag['7'] = "SO";
$wochentag['8'] = "MO";
$wochentag['9'] = "DI";
$wochentag['10'] = "MI";
$wochentag['11'] = "DO";
$wochentag['12'] = "FR";
$wochentag['13'] = "SA";
$wochentag['14'] = "SO";

 
$actdatemini = array();
for ($i = 0; $i <= 6; $i++){
	if (date('d') <= $i){
	   	$actdatemini[$i] = ( date('Y').date('m').date('t', time()-$i*24*60*60) ) - (($i - date('d')) + 100);
	} else {
	  	$actdatemini[$i] = ( date('Y').date('m').date('d') ) - $i;
	}
}

for ($i = 0; $i <= 6; $i++){
	$datevis[$i] = 0;
	$result7 = dbquery("SELECT * FROM ".DB_DID_WEBSTATS."visits WHERE date = '".$actdatemini[$i]."' LIMIT 1");
	while ($row7 = dbarray($result7)) {
		$datevis[$i] = $row7['visits'];
	}
}

$maxcount = max($datevis);
$vari = 50/$maxcount;

$barcolor = "#".$websettings['barcolor'];

//|--------------------------------------------------------------------------------------------------------------------------------------------|\\
//|--- DATENAUSGABE                                                                                                                            |\\
//|--------------------------------------------------------------------------------------------------------------------------------------------|\\

openside($locale['Dws_panel_001']);
echo	"<div style=''>";

//USER ONLINE PANEL AUSGABE
	
$result = dbquery(
	"SELECT ton.online_user, tu.user_id, tu.user_name, tu.user_status FROM ".DB_ONLINE." ton
	LEFT JOIN ".DB_USERS." tu ON ton.online_user=tu.user_id"
);
$guests = 0; $members = array();
while ($data = dbarray($result)) {
	if ($data['online_user'] == "0") {
		$guests++;
	} else {
		$members[] = array($data['user_id'], $data['user_name'], $data['user_status']);
	}
}
echo THEME_BULLET." ".$locale['global_011'].": ".$guests."<br /><br />\n";
echo THEME_BULLET." ".$locale['global_012'].": ".count($members)."<br />\n";
if (count($members)) {
	$i = 1;
	foreach($members as $member) {
		echo "<span class='side'>".profile_link($member[0], $member[1], $member[2])."</span>";
		if ($i != count($members)) { echo ",\n"; } else { echo "<br />\n"; }
		$i++;
	}
}
echo "<br />\n".THEME_BULLET." ".$locale['global_014'].": ".number_format(dbcount("(user_id)", DB_USERS, "user_status<='1'"))."<br />\n";
if (iADMIN && checkrights("M") && $settings['admin_activation'] == "1") {
	echo THEME_BULLET." <a href='".ADMIN."members.php".$aidlink."&amp;status=2'>".$locale['global_015']."</a>";
	echo ": ".dbcount("(user_id)", DB_USERS, "user_status='2'")."<br />\n";
}
$data = dbarray(dbquery("SELECT user_id, user_name, user_status FROM ".DB_USERS." WHERE user_status='0' ORDER BY user_joined DESC LIMIT 0,1"));
echo THEME_BULLET." ".$locale['global_016'].": <span class='side'>".profile_link($data['user_id'], $data['user_name'], $data['user_status'])."</span><br /><br />\n";
	
//USER ONLINE PANEL ENDE
	
//DID WEBSTATS AUSGABE
	
if ($websettings['clickslastdays'] > 0){ 
	echo	"<table>
			<tr>
				<td></td>
				<td>".$locale['Dws_panel_003']."</td>
				<td>".$locale['Dws_panel_004']."</td>
			</tr>
			<tr>
				<td>".$locale['Dws_panel_005'].":</td>
				<td>".$num_vis[0]."</td>
				<td>".$num_cli[0]."</td>
			</tr>";
	if ($websettings['clickslastdays'] > 1){
		echo	"<tr>
					<td>".$locale['Dws_panel_006'].":</td>
					<td>".$num_vis[1]."</td>
					<td>".$num_cli[1]."</td>
				</tr>";
	}
	if ($websettings['clickslastdays'] > 2){ 
		echo	"<tr>
					<td>".$locale['Dws_panel_007'].":</td>
					<td>".$num_vis[2]."</td>
					<td>".$num_cli[2]."</td>
				</tr>";
	}
	echo	"</table>
			<hr class='side-hr' />";
}
if ($websettings['clicksperhour'] == 1){ 
	echo	$locale['Dws_panel_008'].": ".$hitsperstd."<br />";
}
if ($websettings['clickspervisit'] == 1){ 
	echo	$locale['Dws_panel_009'].": ".$hitpervis."<br />";
}
if ($websettings['showallvisitors'] == 1){ 
	echo	$locale['Dws_panel_010'].": ".$allvisits;
}

if ($websettings['clicksperhour'] == 1 || $websettings['clickspervisit'] == 1 || $websettings['showallvisitors'] == 1){
	echo	"<hr class='side-hr' />";
}
	
//DID WEBSTATS AUSGABE ENDE

//|--------------------------------------------------------------------------------------------------------------------------------------------|\\
//|--- COPYRIGHT		                                                                                                                       |\\
//| Editing or deleting the copyright statement below is strictly forbidden and will be brought into court!									   |\\
//|--------------------------------------------------------------------------------------------------------------------------------------------|\\

echo	"<div style='text-align:center;'>
			<a href='http://diphputz.de/'>&copy;</a>
		</div>
		</div>";

closeside();

}

?>