<?php
/*-------------------------------------------------------+
| PHP-Fusion Content Management System
| Copyright © 2002 - 2008 Nick Jones
| http://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Filename: webstats_admin.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).
+--------------------------------------------------------*/
require_once "../../maincore.php";
require_once THEMES."templates/admin_header.php";

include INFUSIONS."did_webstats_panel/infusion_db.php";

if (!checkrights("DWS") || !defined("iAUTH") || $_GET['aid'] != iAUTH) { redirect("../index.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";
}

if (file_exists("update.php")){
	$result = dbquery("SELECT inf_version FROM ".DB_PREFIX."infusions WHERE inf_folder = 'did_webstats_panel' LIMIT 1");
	while ($row = dbarray($result)){
		$version = $row['inf_version'];
	}
	if ($version != "1.4"){
		opentable($locale['Dws_admin_024']);
		echo	"<a href='update.php?aid=".$_GET['aid']."'><div style='text-align: center'><br />".$locale['Dws_admin_025']."<br /><br /></div></a>";
		closetable();
	} else {
		opentable($locale['Dws_admin_024']);
		echo	"<div style='text-align: center; color: red;'><br />".$locale['Dws_admin_028']."<br /><br /></div>";
		closetable();
	}
}

if (isset($_GET['menu'])){
	$get_menu = stripinput($_GET['menu']);
}
else {
	$get_menu = "panel";
}

$get_aid = stripinput($_GET['aid']);

$months = array();
for ($mid = 1; $mid <= 12; $mid++){
	$months[$mid] = date("F", mktime(0, 0, 0, $mid, 1, 1970));
}

opentable($locale['Dws_admin1']);

echo	"<table><tr>
			<th style='width: 10%'><a class='side' href='webstats_admin.php?aid=".$get_aid."&menu=panel'>".$locale['Dws_admin_navi_1']."</a></th>
			<th style='width: 10%'><a class='side' href='webstats_admin.php?aid=".$get_aid."&menu=estats'>".$locale['Dws_admin_navi_2']."</a></th>
			<th style='width: 10%'><a class='side' href='webstats_admin.php?aid=".$get_aid."&menu=sitemap'>".$locale['Dws_admin_navi_4']."</a></th>
			<th style='width: 10%'><a class='side' href='webstats_admin.php?aid=".$get_aid."&menu=settings'>".$locale['Dws_admin_navi_3']."</a></th>
		</tr></tr></table>";
echo	"<hr class='side-hr' />";
		
If (isset($get_menu)){
	switch($get_menu){
		//+-----------------------------------------------------------------------------------------------------------------+\\
		//| --- PANEL ANZEIGEN ---																									|\\
		//+-----------------------------------------------------------------------------------------------------------------+\\
		case "panel":
			echo	"<div style='width: 180px; margin: auto;'>";
			$nocount = 1;
			include "did_webstats_panel.php";
			echo	"</div>";
		break;
		//+-----------------------------------------------------------------------------------------------------------------+\\
		//| --- ERWEITERTE STATISTIK ---																									|\\
		//+-----------------------------------------------------------------------------------------------------------------+\\
		case "estats":
			
			if (isset($_GET['year']) && isnum($_GET['year'])){
				$get_year = $_GET['year'];
			}
			if (isset($_GET['month']) && isnum($_GET['month'])){
				$get_month = $_GET['month'];
			}
			
			echo	$locale['Dws_admin_005'].":<table><tr>";
			
			$result1 = dbquery("SELECT * FROM ".DB_DID_WEBSTATS."settings WHERE id='1' LIMIT 1");
			while ($data1 = dbarray($result1)) {
				$beginyear = $data1['beginyear'];
				$beginmonth = $data1['beginmonth'];
				$beginday = $data1['beginday'];
				$barcolor = $data1['barcolor'];
			}
			echo	"<table>
						<tr>
							<td align='left;' width='40'>".$locale['Dws_admin_006'].":</td>
							<td align='left;'>";
			If ($beginyear == date('Y') && !isset($_GET['year'])){
				echo	"&nbsp;<a href='webstats_admin.php?aid=".$get_aid."&menu=estats&year=".$beginyear."' style='color:red;'>".$beginyear."</a>&nbsp;";
				$get_year = $beginyear;
			} else {
				for ($i = $beginyear; $i <= date('Y'); $i++){
					echo	"&nbsp;<a href='webstats_admin.php?aid=".$get_aid."&menu=estats&year=".$i."' style='";
					if (isset($get_year) && $get_year == $i){
						echo	"color:red;";
					}
					echo	"'>".$i."</a>&nbsp;";
				}
			}
			echo	"</td>
					</tr>";
			
			if (isset($get_year)){
			
				echo	"<tr>
							<td align='left;'>".$locale['Dws_admin_007'].":</td>
							<td align='left;'>";
				
				if ($beginyear == date("Y") && $beginmonth == date("n")){
				   	$get_month = $beginmonth;
				}
				
				if ($get_year != date("Y")){
					if ($get_year == $beginyear){
						$j =  $beginmonth;
						$endm = 12;
					} else {
						$j = 1;
						$endm = 12;
					}
				} 
				else {
					if ($get_year == $beginyear){
						$j =  $beginmonth;
					} else {
						$j = 1;
					}
					$endm = date("n");
				}
				while ($j <= $endm){
					echo	"&nbsp;<a href='webstats_admin.php?aid=".$get_aid."&menu=estats&year=".$get_year."&month=".$j."' style='";
					if (isset($get_month) && $get_month == $j){
						echo	"color:red;";
					}
					echo	"'>".$months[$j]."</a>&nbsp;";
					$j++;
				}
				echo	"</td>
						</tr>";
			}
			echo	"</table>";
			
			if (isset($get_month)){ 
			
				echo	"<hr class='side-hr' />";

					$datevis = array();

					$datevispc = array();

					if (strlen($get_month) == 1){
						$getmonth = "0".$get_month;
					} else {
						$getmonth = $get_month;
					}
					
					$actdateminone = date('t', strtotime("01-".$getmonth."-".$get_year));
					if ($get_month == date('m')){
						$actdateminone = date('d');
					}
					
					for ($i = 0; $i <= $actdateminone; $i++){
					
						if (strlen($get_month) == 1){
							$getmonthdouble = "0".$get_month;
						} else {
							$getmonthdouble = $get_month;
						}
						$subtract = $get_year.$getmonthdouble.$actdateminone - $i;
						$result7 = dbquery("SELECT * FROM ".DB_DID_WEBSTATS."visits WHERE date = '".$subtract."'");
						$row = dbarray($result7);
						$datevis[$i] = $row['visits'];
					}
					
					$maxcount = max($datevis);
					$var = 50/$maxcount;
					
					if ($get_month == date('m') && $get_year == date('Y')){
						$daycount = date('j');
					} else {
						$daycount = date('t', strtotime("01-".$getmonth."-".$get_year));
					}
					
					echo	$locale['Dws_admin_029'].":
							<table>
								<tr>";
								
								$l = $daycount-1;
								while($l >= 0){
									echo	"<th><div style='background-color: #".$barcolor.";height:".(round($datevis[$l]*$var)+1)."px;width:20px;font-size:9px;position:relative;bottom: -".((51-round($datevis[$l]*$var)+1)/2)."px;'>".$datevis[$l]."</div></th>";
									$l = $l-1;
								}
						echo	"</tr>";
							
					$l = $daycount-1;
					$no = "yes";
					while($l >= 0){
						if ($datevis[$l] == 0 && $no != "no"){		
							echo		"<tr>
											<th height='10'></th>
										</tr>";
							$no = "no";
						}
						$l = $l-1;
					}
					$result8 = dbquery("SELECT * FROM ".DB_DID_WEBSTATS."visits WHERE date BETWEEN '".($get_year.$getmonth."00")."' AND '".($get_year.$getmonth."99")."'");
					if (dbrows($result8) == 0){
						echo	"<tr>
									<th height='30'></th>
								</tr>";
					}

					echo		"<tr>";
								
								$l = 1;
								while($l <= $daycount){
									If ($l == date('d') and $get_month == date('m') and $get_year == date('Y')){
										echo	"<th style='color: #".$barcolor."'>".$l."</th>";
									} else {
										echo	"<th>".$l."</th>";
									}
									$l++;
								}
						echo	"</tr>
							</table>";
			}
		break;
		//+-----------------------------------------------------------------------------------------------------------------+\\
		//| --- SITEMAP ---																									|\\
		//+-----------------------------------------------------------------------------------------------------------------+\\
		case "sitemap":
			
			$result1 = dbquery("SELECT * FROM ".DB_DID_WEBSTATS."settings WHERE id='1' LIMIT 1");
			while ($data1 = dbarray($result1)) {
				$barcolor = $data1['barcolor'];
			}
			
			echo	"<table border cellpadding='0' cellspacing='0' width='100%'>
						<tr>
							<th>".$locale['Dws_admin_023']."</th>
							<th width='200'>".$locale['Dws_panel_004']."</th>
						</tr>";
			
			$sitemap = 	array();
			$maxclicks = 0;
			$i = 1;
			$result_sitemap = dbquery("SELECT * FROM ".DB_DID_WEBSTATS."sitemap ORDER BY clicks DESC");
			while($row_sitemap = dbarray($result_sitemap)){
				$sitemap[$i]['path'] = $row_sitemap['path'];
				$sitemap[$i]['clicks'] = $row_sitemap['clicks'];
				if ($row_sitemap['clicks'] > $maxclicks) {
					$maxclicks = $row_sitemap['clicks'];
				}
				$i++;
			}
			
			for ($i = 1; $i <= dbrows($result_sitemap); $i++){
				echo	"<tr>
							<td>".$sitemap[$i]['path']."</td>
							<td><div style='background-color: #".$barcolor."; width: ". round(( $sitemap[$i]['clicks'] / $maxclicks ) * 200) ."px;'>".$sitemap[$i]['clicks']."</div></td>
						</tr>";
			}
			
			echo	"</table>";
		break;
		//+-----------------------------------------------------------------------------------------------------------------+\\
		//| --- SETTINGS ---																									|\\
		//+-----------------------------------------------------------------------------------------------------------------+\\
		case "settings":
			
			if (isset($_GET['action']) && $_GET['action'] == "update"){
				if(isset($_POST['lastweek']) && $_POST['lastweek'] == "on"){
					$post_lastweek = 1;
				} else {
					$post_lastweek = 0;
				}
				if(isset($_POST['clickslastdays']) && $_POST['clickslastdays'] == "on"){
					$post_lastdays = stripinput($_POST['lastdays']);
				} else {
					$post_lastdays = 0;
				}
				if (isset($_POST['clicksperhour']) && $_POST['clicksperhour'] == "on"){
					$post_clicksperhour = 1;
				} else {
					$post_clicksperhour = 0;
				}
				if (isset($_POST['clickspervisitor']) && $_POST['clickspervisitor'] == "on"){
					$post_clickspervisit = 1;
				} else {
					$post_clickspervisit = 0;
				}
				if (isset($_POST['showallvisitors']) && $_POST['showallvisitors'] == "on"){
					$post_showallvisitors = 1;
				} else {
					$post_showallvisitors = 0;
				}
				if(isset($_POST['puplicstats']) && $_POST['puplicstats'] == "on"){
					$post_puplicstats = 1;
				} else {
					$post_puplicstats = 0;
				}
				if (isset($_POST['active']) && $_POST['active'] == "on"){
					$post_active = 1;
				} else {
					$post_active = 0;
				}
				if (isset($_POST['barcolor']) && $_POST['barcolor'] != ""){
					$post_barcolor = stripinput($_POST['barcolor']);
				} else {
					$post_barcolor = "0099cc";
				}
				if (isset($_POST['sitemap_getvars']) && $_POST['sitemap_getvars'] != ""){
					$post_sitemap_getvars = stripinput($_POST['sitemap_getvars']);
				} else {
					$post_sitemap_getvars = "";
				}
				
				
				dbquery("UPDATE ".DB_DID_WEBSTATS."settings SET lastweek = '".$post_lastweek."', clickslastdays = '".$post_lastdays."', clicksperhour = '".$post_clicksperhour."', clickspervisit = '".$post_clickspervisit."', showallvisitors = '".$post_showallvisitors."', active = '".$post_active."', puplicstats = '".$post_puplicstats."', barcolor = '".$post_barcolor."', sitemap_getvars = '".$post_sitemap_getvars."' WHERE id = 1");
			}
		
			$statsettings = dbquery("SELECT * FROM ".DB_DID_WEBSTATS."settings WHERE id = 1 LIMIT 1");
			while ($statsetting = dbarray($statsettings)) {
				$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'],
					"sitemap_getvars" => $statsetting['sitemap_getvars']
				);
			$webset_getvars = $websettings['sitemap_getvars'];	
			}
			echo	$locale['Dws_admin_008'].":
					<table><form action='webstats_admin.php?aid=".$get_aid."&menu=settings&action=update' method='post'>
						<tr>
							<td rowspan='3'>
								<div style='width: 180px;'>";
								$nocount = 1;
								include "did_webstats_panel.php";
								echo	"</div>
							<td height='120'><input type='checkbox'";
			if ($websettings['lastweek'] == 1){
				echo	" checked ";
			}
			echo	"name='lastweek'/>".$locale['Dws_admin_009']."</td>
						</tr>
						<tr>
							<td height='70'>
								<input type='checkbox' name='clickslastdays'";
			if ($websettings['clickslastdays'] > 0){
				echo	" checked ";
				switch($websettings['clickslastdays']){
					case 1:
						$opt1 = "selected";
						$opt2 = "";
						$opt3 = "";
					break;
					case 2:
						$opt1 = "";
						$opt2 = "selected";
						$opt3 = "";
					break;
					case 3:
						$opt1 = "";
						$opt2 = "";
						$opt3 = "selected";
					break;
				}
			} else {
				$opt1 = "";
				$opt2 = "";
				$opt3 = "";
			}
			echo	"/>".$locale['Dws_admin_010']."<br />
								<select name='lastdays'>
									<option ".$opt1." value='1'>".$locale['Dws_admin_018']."</option>
									<option ".$opt2." value='2'>".$locale['Dws_admin_019']."</option>
									<option ".$opt3." value='3'>".$locale['Dws_admin_020']."</option>
								</select>&nbsp;".$locale['Dws_admin_011']."
							</td>
						</tr>
						<tr>
							<td height='50'>
								<input type='checkbox' name='clicksperhour'";
			if ($websettings['clicksperhour'] == 1){
				echo	" checked ";
			}
			echo	"/>".$locale['Dws_admin_012']."<br />
								<input type='checkbox' name='clickspervisitor'";
			if ($websettings['clickspervisit'] == 1){
				echo	" checked ";
			}
			echo	"/>".$locale['Dws_admin_013']."<br />
								<input type='checkbox' name='showallvisitors'";
			if ($websettings['showallvisitors'] == 1){
				echo	" checked ";
			}
			echo	"/>".$locale['Dws_admin_021']."<br />
							</td>
						</tr>";
					
					
			echo	"<tr><td colspan='2'><hr class='side-hr' /></td></tr>
					".$locale['Dws_admin_015'].":<br />
						<tr>
							<td><div style='padding: 10px;'>
						".$locale['Dws_admin_022'].":</td><td><input type='text' name='barcolor' value='".$websettings['barcolor']."' maxlength='6' size='6' /></td>
						</tr>
						<tr>
							<td colspan='2'><input type='checkbox' name='active' ";
			if ($websettings['active'] == 1){
				echo	" checked ";
			}
			echo	"/> ".$locale['Dws_admin_016']."</td>
						</tr>
						<tr>
							<td colspan='2'><input type='checkbox' name='puplicstats' ";
			if ($websettings['puplicstats'] == 1){
				echo	" checked ";
			}
			echo	"/> ".$locale['Dws_admin_017']."</td>
						</div>
						</tr>
						</table>
						<table>
						<tr><td colspan='2'><hr class='side-hr' /></td></tr>
						<tr>
							<td valign='top' width='300'>".$locale['Dws_admin_027']."</td>
							<td>".$locale['Dws_admin_026'].":<br /><textarea rows='7' cols='40' name='sitemap_getvars'>";
				echo	$webset_getvars;
				echo		"</textarea></td>
						</tr>
						<tr>
							<td colspan='2'><input type='submit' value='".$locale['Dws_admin_014']."'></td>
						</tr>
					</form>
					</table>";
		break;
	}
}
else {
echo	"<div style='width: 180px;'>";
$nocount = 1;
include "did_webstats_panel.php";
echo	"</div>";
}

echo	"";

closetable();

require_once THEMES."templates/footer.php";
?>