<?php 
	include($_SERVER["DOCUMENT_ROOT"]."/Config/_dbconfig_.php");
    @$y = mysql_connect($MYSQL_HOST,$MYSQL_USER,$MYSQL_PASS);
    @$x = mysql_select_db($MYSQL_DATABASE);
    if (empty($x))  
    {
        echo "ERROR 0x01, Failed to connect to database!<br>";
        exit;
    }

    $by    = "id";  //$_GET["by"];
	if($_POST["dirsort"] != "")
	{
		$sort = mysql_real_escape_string($_POST["dirsort"]);
	}
	else
	{
		$sort = $_POST["dirsort"];
	}
	if($_POST["table"] != "")
	{
		$table = mysql_real_escape_string($_POST["table"]);
	}
	else
	{
		$table = $_POST["table"];
	}

function Potenz($sent_sum)
{
    if($sent_sum>1024)
    {
	$sent_sum_anz=round($sent_sum/1024,2) . "kiB";

        if($sent_sum>1024*1024)
	{
	    $sent_sum_anz=round($sent_sum/(1024*1024),2) . "MiB";
	    
	    if($sent_sum>1024*1024*1024)
	    {
		$sent_sum_anz=round($sent_sum/(1024*1024*1024),2) . "GiB";
	    }
	 }
    }
    else
    {
	$sent_sum_anz=$sent_sum . "Byte";
    }
      return $sent_sum_anz;
}
?> 

<!-- MAIN CONTENT BEGINN ################################################################################ -->
<!-- MAIN -->
<div id="main">	
	<div class="wrapper">
		<br />
		<form action="<?php echo $PHP_SELF; ?>" method="post"> 
			<select name="dirsort" size=1>        
				<option <?php if($sort=="ASC") echo "selected";?> value="ASC">Up</option>
				<option <?php if($sort=="DESC") echo "selected";?> value="DESC">Down</option>     
			</select>

			<select name="table" size=1>        
				<option <?php if($table=="year") echo "selected";?> value="year">Year</option>
				<option <?php if($table=="month") echo "selected";?> value="month">Month</option>  
				<option <?php if($table=="connections") echo "selected";?> value="connections">All</option>  
			</select>

			<input type="submit" value="go!" /> 
		</form>

		<!-- Sorting Leiste Start-->
		<?  
			// MYsql befehl basteln
			$sql = "SELECT * FROM $table ORDER BY $by $sort;";
			$result = mysql_query($sql);

			echo "<table border=1>";
			if($table=='connections')
			{
				echo "<tr><td>Start Time</td><td>Stop Time</td><td>Client IP</td><td>Download 
				</td><td>Upload</td></tr>\n";
			}
			else if($table=='month')
			{
				echo "<tr><td>Month</td><td>Year</td><td>Connection Time</td><td>Download 
				</td><td>Upload</td></tr>\n";
			}
			else if($table=='year')
			{
				echo "<tr><td>Year</td><td>Connection Time</td><td>Download 
				</td><td>Upload</td></tr>\n";
			}
			else
			{
			}
		// ------------------------------------------------ Mysql lesen
			if ($result) 
			{
		//Zusammenzaehlen
			$time_sum=0;
			$month=0;
			$year=0;

			$sent_sum=0;
			$received_sum=0;

				while ($ar=mysql_fetch_array($result,MYSQL_ASSOC)) 
				{
					if($table=='connections')
					{
						if($ar["stop"] != 0)
						{
							$stop = date("r",$ar["stop"]);
						}
						else
						{
							$stop = "running session";
						}
						echo "<tr><td align=right>",date("r",$ar["start"]),"</td><td align=right>",$stop,"</td><td>",$ar["ip"],
						"</td><td align=right>",Potenz($ar["sent"]),"</td><td align=right>",Potenz($ar["received"]),
						"</td></tr>\n";
					}
					else if($table=='month')
					{
				if($month!=$ar["month"] && $month!=0)
				{
					$hour = floor($time_sum/3600);
					$min  = floor($time_sum/60 - $hour *60);
					$sec  = $time_sum-$hour*3600-$min*60;
					$timestring = $hour .":". $min .":". $sec;

					$sent_sum_anz = Potenz($sent_sum);
					$received_sum_anz = Potenz($received_sum);

					echo "<tr><td>",$month,"</td><td>",$year,"</td><td>",$timestring,
					"</td><td align=right>",$sent_sum_anz,"</td><td align=right>",$received_sum_anz,
					"</td></tr>\n";
					$sent_sum=0;
					$received_sum=0;
					$time_sum=0;
				}
				else
				{
				}
				$month=$ar["month"];
				$year=$ar["year"];
				$time_sum += $ar["time"];
				$sent_sum += $ar["sent"];
				$received_sum += $ar["received"];
					}
					else if($table=='year')
					{
				if($year!=$ar["year"] && $year!=0)
				{
					$hour = floor($time_sum/3600);
					$min  = floor($time_sum/60 - $hour *60);
					$sec  = $time_sum-$hour*3600-$min*60;
					$timestring = $hour .":". $min .":". $sec;

					$sent_sum_anz = Potenz($sent_sum);
					$received_sum_anz = Potenz($received_sum);

					echo "<tr><td>",$year,"</td><td>",$timestring,
					"</td><td align=right>",$sent_sum_anz,"</td><td align=right>",$received_sum_anz,
					"</td></tr>\n";
					$sent_sum=0;
					$received_sum=0;
					$time_sum=0;
				}
				else
				{
				}
				$year=$ar["year"];
				$time_sum += $ar["time"];
				$sent_sum += $ar["sent"];
				$received_sum += $ar["received"];
					}
					else
					{
					}
				}
		//letzen Datensatz ausgeben
			$hour = floor($time_sum/3600);
			$min  = floor($time_sum/60 - $hour *60);
			$sec  = $time_sum-$hour*3600-$min*60;
			$timestring = $hour .":". $min .":". $sec;

			$sent_sum_anz = Potenz($sent_sum);
			$received_sum_anz = Potenz($received_sum);


			if($table=='year')
			{
				echo "<tr><td>",$year,"</td><td>",$timestring,
				"</td><td align=right>",$sent_sum_anz,"</td><td align=right>",$received_sum_anz,
				"</td></tr>\n";
			}
			else if($table=='month')
			{
				echo "<tr><td>",$month,"</td><td>",$year,"</td><td>",$timestring,
				"</td><td align=right>",$sent_sum_anz,"</td><td align=right>",$received_sum_anz,
				"</td></tr>\n";
			}
			$sent_sum=0;
			$received_sum=0;
			$time_sum=0;
		//---------------------------------
			}
			else
			{
				echo "<br>";
			}
			echo "</table>";
			mysql_close();
		?>	      
	</div>
</div>
<!-- END CONTENT BEGINN ################################################################################# -->
