<?php
@session_start();
if((isset($_SESSION['beroari_time']))&&(($_SESSION['beroari_time']+1200)<time())){
	@session_unset();
	@session_destroy();
	echo "<script>window.opener.location='../index.php'</script>";
	echo "<script>this.window.close();</script>";
}else{

	$_SESSION['beroari_time']=time();
if ($_GET['option']=="BEROFIX-MIB"){
	$file=file("/usr/local/conf/BEROFIX-MIB.txt");
	$filename="BEROFIX-MIB.txt";
}elseif ($_GET['option']=="certificate"){
	$file=file("/usr/conf/cafile.pem");
	$filename="cafile.pem";
}else{
	if (file_exists("/usr/conf/isgw.conf")){
		$files.=" isgw.conf";
	}
	if (file_exists("/usr/conf/isgw.sip")){
		$files.=" isgw.sip";
	}
	if (file_exists("/usr/conf/isgw.isdn")){
		$files.=" isgw.isdn";
	}
	if (file_exists("/usr/conf/isgw.analog")){
		$files.=" isgw.analog";
	}
	if (file_exists("/usr/conf/isgw.gsm")){
		$files.=" isgw.gsm";
	}
	if (file_exists("/usr/conf/isgw.causes")){
		$files.=" isgw.causes";
	}
	if (file_exists("/usr/conf/isgw.causes")){
		$files.=" isgw.cpt";
	}
	if (file_exists("/usr/conf/isgw.dialplan")){
		$files.=" isgw.dialplan";
	}
	if (file_exists("/usr/conf/hardware.conf")) {
		 $files.=" hardware.conf";
	}
	if (file_exists("/usr/conf/misc.conf")) {
		 $files.=" misc.conf";
	}

	/*
	 * Seriennummer der Karte herausfinden
	 */
	$res=file_get_contents('/proc/cmdline');
	preg_match("/[^a-zA-Z0-9=]*?d=(?<serial>.+) /",$res,$serial);
	$serial=$serial['serial'];

	exec("/bin/tar -cvz -f/tmp/beroFix-$serial-backup.tar.gz -C /usr/conf ".$files);
	$file = file("/tmp/beroFix-$serial-backup.tar.gz");
	$filename="beroFix-$serial-backup.tar.gz";
}
	$file2 = implode("", $file);
	header("Content-Type: application/force-download");
	header("Content-Disposition: attachment; filename=".$filename."\r\n\r\n");
	header("Content-Length: ".strlen($file2)."\n\n");
	echo $file2;
	exit;
}
?>

