<?php
session_start();
include($_SERVER["DOCUMENT_ROOT"]."/Site/checkuser.php");
define ("LOCAL_SECURITY_KEY", true);
include_once ($_SERVER["DOCUMENT_ROOT"]."/Modules/UserControl/config.inc");
include_once ($_SERVER["DOCUMENT_ROOT"]."/Modules/UserControl/include/functions.inc");
include_once ($_SERVER["DOCUMENT_ROOT"]."/Modules/UserControl/include/openssl-functions.inc");
include_once ($_SERVER["DOCUMENT_ROOT"]."/Modules/UserControl/include/openvpn-functions.inc");

//#################################################################################################################

//echo "#".$_SESSION["user_vpnid"]."#";
//echo "#".$_REQUEST['VPNId']."#";
if($_SESSION["user_vpnid"] != $_REQUEST['VPNId'])
{
    echo "Request fail...";
    die;
}

openvpn_load_server_conf ();
if (!isset ($openvpn['Server'])) html_error ("The OpenVPN server has no configuration file");
openvpn_load_status ();
if (!isset ($openvpn['Server']['Title'])) html_error ("The OpenVPN server has no status file");

openssl_load_database ();

// Check permissions :)
// ...

//$GETidDEC = sscanf ($GETid, '%X');
$GETidDEC = hexdec  ($_SESSION["user_vpnid"]);
//$GETidDEC[0] = $_SESSION["user_vpnid"];
$sExt = '.zip';

//echo "ID=".$GETidDEC;


if ($sExt == '.zip')
{
    
 include_once ($_SERVER["DOCUMENT_ROOT"]."/Modules/UserControl/include/zip.lib.php");
 $zipData = new zipfile ();

 // Add the certificate files
 foreach (array ('.pem', '.key', '.csr') as $Ext2zip)
 {
  if ($config['Download']['ZIP'][$Ext2zip])
  {
   $File2zip  = openssl_get_filename ($GETidDEC, $Ext2zip);
   $File2name = basename ($File2zip, '.pem');
   // Cheat the extension
   if ($Ext2zip == '.pem') $File2name .= '.crt';

   if (is_file ($File2zip))
    $zipData->addFile (implode ('', file ($File2zip)), $File2name);
  }
 }

 // Add the extra files
 foreach ($config['Download']['ZIP']['Others'] as $File2zip)
 {
  if (is_file (db_getconfval("publicdown")."/". $File2zip))
   $zipData->addFile (implode ('', file (db_getconfval("publicdown")."/". $File2zip)), $File2zip);
 }

 // Flush the ZIP file to the client
 html_download_data ($zipData -> file (), openssl_hex_serial ($GETidDEC) .'.zip');
 //echo "<a href=getfile.php?dir=$zipData -> file () target=_blank>Direktlink zur Datei</a>
}
else
{
//    echo "ID: " .$GETidDEC[0] . "Ext: " .$sExt;
 if (($sCertificateFile = openssl_get_filename ($GETidDEC, $sExt)) == '') html_error ("Can't find requested file..FU");

 // Cheat the extension
 if ($sExt == '.pem') $sExt = '.crt';

 // Download
 html_download ($sCertificateFile, openssl_hex_serial ($GETidDEC) . $sExt);
 //html_dump ('$GLOBALS', $GLOBALS);
}

?>
