<?php
session_start();
include($_SERVER["DOCUMENT_ROOT"]."/Site/checkadmin.php");
include($_SERVER["DOCUMENT_ROOT"]."/Site/mysql.php");
include_once ($_SERVER["DOCUMENT_ROOT"]."/Admin/Modules/VPNconfig/config.inc");
include_once ($_SERVER["DOCUMENT_ROOT"]."/Admin/Modules/VPNconfig/include/functions.inc");
include_once ($_SERVER["DOCUMENT_ROOT"]."/Admin/Modules/VPNconfig/include/openssl-functions.inc");
include_once ($_SERVER["DOCUMENT_ROOT"]."/Admin/Modules/VPNconfig/include/openvpn-functions.inc");


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

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[0] =$_REQUEST['id'];
$sExt = '.pem';
if (isset ($_REQUEST['Type']))
 if     ($_REQUEST['Type'] == 'CRT') $sExt = '.pem';
 elseif ($_REQUEST['Type'] == 'KEY') $sExt = '.key';
 elseif ($_REQUEST['Type'] == 'CSR') $sExt = '.csr';
 elseif ($_REQUEST['Type'] == 'ZIP') $sExt = '.zip';

if ($sExt == '.zip')
{
 include_once ($_SERVER["DOCUMENT_ROOT"]."/Admin/Modules/VPNconfig/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[0], $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[0]) .'.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[0], $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[0]) . $sExt);
 //html_dump ('$GLOBALS', $GLOBALS);
}

?>
