<?php 
include ($_SERVER["DOCUMENT_ROOT"]."/Site/checkuser.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;
    }

$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"];

$moduleurl = $_SERVER["REQUEST_URI"];
$urlcut = strpos($moduleurl, "&");
if($urlcut > 0)
{
	$moduleurl = substr($moduleurl,0,$urlcut);
}
else
{
	if(strpos($moduleurl, "?") == 0)
	{
		$moduleurl = $_SERVER["REQUEST_URI"]."?site=1"; 	
	}
	if($_SERVER["REQUEST_URI"] == "/" || is_null($_SERVER["REQUEST_URI"]))
	{
		$moduleurl = "/index.php?site=1";
	}	
}


switch($uebergabe2)
{
	case 1:
		echo "<b>Proxy repaired successfully!";
		break;
	default:
		break;
}

switch($uebergabe)
{
case 0:

echo "
<table>
    <tr>
        <td><h1>Welcome ".$_SESSION["user_vorname"]." ".$_SESSION["user_nachname"]."!</h1><br></td>
        <td><img src=\"Modules/UserControl/avatar/".$_SESSION["user_avatar"].".png\" width=\"90\" height=\"90\"></img></td>
    </tr>
</table>

<h2>Your Userstatistics:</h2>

<form action=\"".$PHP_SELF."\" method=\"post\"> 
<select name=\"dirsort\" size=1>        
    <option "; if($sort=="ASC") echo "selected"; echo " value=\"ASC\">Up</option>
    <option "; if($sort=="DESC") echo "selected"; echo " value=\"DESC\">Down</option>     
</select>

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

<select name=\"sortby\" size=1>        
    <option "; if($by=="id") echo "selected"; echo " value=\"id\">ID</option>  
    <option "; if($by=="sent") echo "selected"; echo " value=\"sent\">Sent</option> 
    <option "; if($by=="received") echo "selected"; echo " value=\"received\">Received</option>
    <option "; if($by=="ip") echo "selected"; echo " value=\"ip\">IP</option>   
</select>
<input type=\"submit\" value=\"go!\"> 
</form>";

    // 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 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) 
    {
        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($ar["client"] == $_SESSION["user_nickname"])
                {
                    echo "<tr><td>",date("r",$ar["start"]),"</td><td>",$stop,"</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($ar["client"] == $_SESSION["user_nickname"])
                {
                    echo "<tr><td>",$ar["month"],"</td><td>",$ar["year"],"</td><td>",$timestring,
                    "</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($ar["client"] == $_SESSION["user_nickname"])
                {
                    echo "<tr><td>",$ar["year"],"</td><td>",$timestring,
                    "</td><td align=right>", $sentbytes,"</td><td align=right>", $receivedbytes,
                    "</td></tr>\n";
                }
            }
            else
            {
            }
        }
    }
    else
    {
        echo "<br>";
    }
    echo "</table>";
	mysql_close();

echo "<br>

    <h2>VPN Config:</h2>
    <form action=\"/Modules/UserControl/getcert.php\" method=\"post\"> 

    <tr>
        <td><input type=\"hidden\" name=\"VPNId\" size=\"20\" value=".$_SESSION["user_vpnid"]."></td>
    </tr> 
	<tr>
        <td><input type=\"hidden\" name=\"Id\" size=\"20\" value=".$_SESSION["user_id"]."></td>
    </tr> 
    <tr>
        <td><input type=\"hidden\" name=\"Type\" size=\"20\" value=\"ZIP\"></td>
    </tr> 
	<tr>
        <td><input type=\"hidden\" name=\"path\" size=\"50\" value=\"".$moduleurl."\"></td>
    </tr> 
    <input type=\"submit\" value=\"Download config\">
    </form>
	<p>Die SchulVPN Client Software kann unter \"Services\" heruntergeladen werden!</p>

	<br>";

	if(db_getconfval("withusersquid")=="1")
	{
	echo "<h2>Proxy tot?:</h2>";
	
    if (isset ($_REQUEST["proxy"])) 
    { 
        echo "<b>Proxy repaired successfully!</b>"; 
    } 

    function dec_to_hex($dec)
	{
		$sign = ""; // suppress errors
		if( $dec < 0){ $sign = "-"; $dec = abs($dec); }

		$hex = Array( 0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5,
		              6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 'A',
		              11 => 'B', 12 => 'C', 13 => 'D', 14 => 'E',   
		              15 => 'F' );
		   
		do
		{
		    $h = $hex[($dec%16)] . $h;
		    $dec /= 16;
		}
		while( $dec >= 1 );
	   
		return $sign . $h;
	} 

	$zeile = exec(escapeshellcmd("cat ".db_getconfval("sqddir")."/vpnid"));
	$zeile = dec_to_hex($zeile);

	echo " 
	<p> Funktioniert der SchulVPN Proxy nicht mehr? Dann klicke auf \"Repair Proxy\". </p>
	<p> Info: Jeder klick dieses Buttons wird in einer Datenbank mitgeloggt! </p>
    <form action=\"/Modules/UserControl/deadproxy.php\" method=\"post\"> 

    <tr>
        <td><input type=\"hidden\" name=\"VPNId\" size=\"20\" value=".$_SESSION["user_vpnid"]."></td>
    </tr> 
    <tr>
        <td><input type=\"hidden\" name=\"Id\" size=\"20\" value=".$_SESSION["user_id"]."></td>
    </tr> 
	<tr>
        <td><input type=\"hidden\" name=\"IP\" size=\"20\" value=".getenv("REMOTE_ADDR")."></td>
    </tr>
	<tr>
        <td><input type=\"hidden\" name=\"RepID\" size=\"20\" value=".$zeile."></td>
    </tr>  
	<input type=\"hidden\" name=\"path\" size=\"50\" value=".$moduleurl.">
    <input type=\"submit\" value=\"Repair Proxy!\">
    </form>

    <br>";
	}
	
	echo "
   <h2>User Information:</h2><br>
    Benutzer Id: ".$_SESSION["user_id"]."<br> 
	VPN Id: ".$_SESSION["user_vpnid"]."<br>
    Nickname: ".$_SESSION["user_nickname"]."<br> 
    Nachname: ".$_SESSION["user_nachname"]."<br> 
    Vorname: ".$_SESSION["user_vorname"]."<br>  
	Email Adress: ".$_SESSION["user_email"]."<br>
	Novell Login (Email): ".$_SESSION["user_novlogin"]."<br>
	Current IP: ".getenv("REMOTE_ADDR")."<br> 
    <form action=\"".$moduleurl."&uebergabe=2\" method=\"post\"> 
    <input type=\"submit\" value=\"Change Userinformation\">
    </form>";
	break;

case 2:
	include($_SERVER["DOCUMENT_ROOT"]."/Modules/UserControl/change.php");
	break;

default:
	break;    
}
