<?php
include($_SERVER["DOCUMENT_ROOT"]."/Site/checkadmin.php");

include ($_SERVER["DOCUMENT_ROOT"]."/Config/_dbconfig_.php");
    @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;
    }

    $sort  = mysql_real_escape_string($_POST["dirsort"]); //$_GET["sort"];
    $by    = mysql_real_escape_string($_POST["sortby"]);  //$_GET["by"];
    $table = mysql_real_escape_string($_POST["table"]);   //$_GET["table"];
    $client= mysql_real_escape_string($_POST["client"]);

?>

<!-- MAIN CONTENT BEGINN ################################################################################ -->
<form action="<?php echo $PHP_SELF; ?>" method="post"> 
<br>
<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>

<select name="sortby" size=1>        
    <option <?php if($by=="id") echo "selected";?> value="id">ID</option>
    <option <?php if($by=="client") echo "selected";?> value="client">Client</option> 
    <option <?php if($by=="sent") echo "selected";?> value="sent">Sent</option> 
    <option <?php if($by=="received") echo "selected";?> value="received">Received</option>
    <option <?php if($by=="ip") echo "selected";?> value="ip">IP</option>   
</select>
Search Client:<input type="text" name="client" size="20" <?php if($client !="") echo "value=".$client;?> ></td>
<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 "<br><table border=1>";
    if($table=='connections')
    {
        echo "<tr><td>Start Time</td><td>Stop Time</td><td>Client Name</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>Client Name</td><td>Download 
        </td><td>Upload</td></tr>\n";
    }
    else if($table=='year')
    {
        echo "<tr><td>Year</td><td>Connection Time</td><td>Client Name</td><td>Download 
        </td><td>Upload</td></tr>\n";
    }
    else
    {
    }
// ------------------------------------------------ Mysql lesen
    if ($result) 
    {
        while ($ar=mysql_fetch_array($result,MYSQL_ASSOC)) 
        {
            if($ar["sent"]>1024)
            {
                $sentbytes = round($ar["sent"]/1024,2) . " KiB"; 
                if($ar["sent"]>1048576)
                {
                    $sentbytes = round($ar["sent"]/1024/1024,2) . " MiB";
                    if($ar["sent"]>1073741824)
                    {
                        $sentbytes = round($ar["sent"]/1024/1024/1024,2) . " GiB";
                    }
                }
            }        
            else
            {
                $sentbytes = $ar["sent"]. " Byte";
            }

            if($ar["received"]>1024)
            {
                $receivedbytes = round($ar["received"]/1024,2) . " KiB";
                if($ar["received"]>1048576)
                {
                    $receivedbytes = round($ar["received"]/1024/1024,2) . " MiB";
                    if($ar["received"]>1073741824)
                    {
                        $receivedbytes = round($ar["received"]/1024/1024/1024,2) . " GiB";
                    }
                }
            }     
            else
            {
                $receivedbytes = $ar["received"]. " Byte";
            }

            if($table=='connections')
            {
                if($ar["stop"] != 0)
                {
                    $stop = date("r",$ar["stop"]);
                }
                else
                {
                    $stop = "running session";
                }

                if($client == "" || $ar["client"] == $client)
                {
                    echo "<tr><td>",date("r",$ar["start"]),"</td><td>",$stop,"</td><td>",$ar["client"],
                    "</td><td>",$ar["ip"],
                    "</td><td align=right>",$sentbytes,
                    "</td><td align=right>",$receivedbytes,
                    "</td></tr>\n";
                }
            }
            else if($table=='month')
            {
                $hour = floor($ar["time"]/3600);
                $min  = floor($ar["time"]/60 - $hour *60);
                $sec  = $ar["time"]-$hour*3600-$min*60;
                $timestring = $hour .":". $min .":". $sec;

                if($client == "" || $ar["client"] == $client)
                {
                    echo "<tr><td>",$ar["month"],"</td><td>",$ar["year"],"</td><td>",$timestring,
                    "</td><td>",$ar["client"],
                    "</td><td align=right>",$sentbytes,"</td><td align=right>",$receivedbytes,
                    "</td></tr>\n";
                }
            }
            else if($table=='year')
            {
                $hour = floor($ar["time"]/3600);
                $min  = floor($ar["time"]/60 - $hour *60);
                $sec  = $ar["time"]-$hour*3600-$min*60;
                $timestring = $hour .":". $min .":". $sec;
                
                if($client == "" || $ar["client"] == $client)
                {
                    echo "<tr><td>",$ar["year"],"</td><td>",$timestring,
                    "</td><td>",$ar["client"],
                    "</td><td align=right>",$sentbytes,"</td><td align=right>",$receivedbytes,
                    "</td></tr>\n";
                }
            }
            else
            {
            }
        }
    }
    else
    {
        echo "<br>";
    }
    echo "</table>";
	mysql_close();
?>	      

