<?php
//Hochladen von Backup files, die Datenbank wird angepasst werden
include "../includes/Sqlite/Tools.php";
include "../includes/variables.php";
//include "database.php";
$strpath="/usr/conf/";
$file_name= $_FILES["uploadfile"]["name"];
$tmp_file= $_FILES["uploadfile"]["tmp_name"];

function act(){
	$ba= new beroAri();
	$query=$ba->select("select option from activate where id='activate'");
	while($entry = $ba->fetch_array($query)){
		if ($entry['option'] <= 3) $query =  $ba->update("UPDATE activate SET option=3 where id='activate'");
	}
	echo "<script>window.location='../index.php?m=back_res';</script>";
}

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

$extension=explode(".",$file_name,2);

if (($extension[1]!="tar.gz" && $extension[1]!="tgz") && $file_name!="isgw.conf" && $file_name!="isgw.sip" && $file_name!="isgw.gsm"
	&& $file_name!="isgw.isdn" && $file_name!="isgw.dialplan" && $file_name!="hardware.conf" && $file_name!="isgw.cpt"
	&& $file_name!="misc.conf" && $file_name!="isgw.analog" && $file_name!="isgw.causes"){
	echo "<script>alert('This file is not correct: $file_name'); window.location='../index.php?m=back_res';</script>";
}else{
	echo $file_name."\n";
if ($extension[1]=="tar.gz" || $extension[1]=="tgz"){
	if ($file_name!="beroFix-$serial-backup.tar.gz"){
		echo "<script type='text/javascript'>conf = confirm('This seems not to be the default name of a berofix-backup, are you sure you want to proceed to restore this backup file?');
					if (conf==false){
						window.location='../index.php?m=back_res';
					}</script>";
	}
	copy($tmp_file, "/tmp/$file_name");
	exec("/bin/tar -xvvz -f/tmp/$file_name -C /tmp/");

	chdir("/usr/conf");
	if (!is_dir("backup")) mkdir("backup");
	copy("isgw.conf","backup/isgw.conf");
	copy("isgw.sip","backup/isgw.sip");
	copy("isgw.isdn","backup/isgw.isdn");
	copy("isgw.analog","backup/isgw.analog");
	copy("isgw.gsm","backup/isgw.gsm");
	copy("isgw.causes","backup/isgw.causes");
	copy("isgw.cpt","backup/isgw.cpt");
	copy("isgw.dialplan","backup/isgw.dialplan");
	copy("misc.conf","backup/misc.conf");
	copy("hardware.conf","backup/hardware.conf");
	unlink("isgw.conf");
	unlink("isgw.sip");
	unlink("isgw.isdn");
	unlink("isgw.analog");
	unlink("isgw.gsm");
	unlink("isgw.causes");
	unlink("isgw.cpt");
	unlink("isgw.dialplan");
	unlink("misc.conf");
	unlink("hardware.conf");

	if (is_file("/tmp/isgw.conf")) copy("/tmp/isgw.conf","/usr/conf/isgw.conf");
	if (is_file("/tmp/isgw.sip")) copy("/tmp/isgw.sip","/usr/conf/isgw.sip");
	if (is_file("/tmp/isgw.isdn")) copy("/tmp/isgw.isdn","/usr/conf/isgw.isdn");
	if (is_file("/tmp/isgw.analog")) copy("/tmp/isgw.analog","/usr/conf/isgw.analog");
	if (is_file("/tmp/isgw.gsm")) copy("/tmp/isgw.gsm","/usr/conf/isgw.gsm");
	if (is_file("/tmp/isgw.causes")) copy("/tmp/isgw.causes","/usr/conf/isgw.causes");
	if (is_file("/tmp/isgw.cpt")) copy("/tmp/isgw.cpt", "/usr/conf/isgw.cpt");
	if (is_file("/tmp/isgw.dialplan")) copy("/tmp/isgw.dialplan","/usr/conf/isgw.dialplan");
	if (is_file("/tmp/hardware.conf")) copy("/tmp/hardware.conf","/usr/conf/hardware.conf");
	if (is_file("/tmp/misc.conf")) copy("/tmp/misc.conf","/usr/conf/misc.conf");
	include "ini_to_db_functions.php";
	if (!ini_to_db(1)){
		copy("backup/isgw.conf","isgw.conf");
		copy("backup/isgw.sip","isgw.sip");
		copy("backup/isgw.isdn","isgw.isdn");
		copy("backup/isgw.analog","isgw.analog");
		copy("backup/isgw.gsm","isgw.gsm");
		copy("backup/isgw.causes","isgw.causes");
		copy("backup/isgw.cpt","isgw.cpt");
		copy("backup/isgw.dialplan","isgw.dialplan");
		copy("backup/misc.conf","misc.conf");
		copy("backup/hardware.conf","hardware.conf");
     	echo "<script>alert('Failed!!')</script>";
	}
	act();
}

if ($file_name=="isgw.conf"){
	copy($tmp_file,"/tmp/isgw.conf");

	chdir("/usr/conf");
	if (!is_dir("backup")) mkdir("backup");
	copy("isgw.conf","backup/isgw.conf");
	unlink("isgw.conf");


	if (is_file("/tmp/isgw.conf")) copy("/tmp/isgw.conf","/usr/conf/isgw.conf");
	include "ini_to_db_functions.php";
	$db= new beroAri();
	if (!isgwconf_initoDB($db,$strpath,1)){
		copy("backup/isgw.conf","isgw.conf");
     	echo "<script>alert('Failed!!')</script>";
	}
	act();
}
if ($file_name=="isgw.sip"){
	copy($tmp_file,"/tmp/isgw.sip");
	chdir("/usr/conf");
	if (!is_dir("backup")) mkdir("backup");
	copy("isgw.sip","backup/isgw.sip");
	unlink("isgw.sip");

	if (is_file("/tmp/isgw.sip")) copy("/tmp/isgw.sip","/usr/conf/isgw.sip");
	include "ini_to_db_functions.php";
	$db= new beroAri();
	if (!isgwsip_initoDB($db,$strpath,1)){
		copy("backup/isgw.sip","isgw.sip");
     	echo "<script>alert('Failed!!')</script>";
	}
	act();

}
if ($file_name=="isgw.isdn"){
	copy($tmp_file,"/tmp/isgw.isdn");
	chdir("/usr/conf");
	if (!is_dir("backup")) mkdir("backup");
	copy("isgw.isdn","backup/isgw.isdn");
	unlink("isgw.isdn");

	if (is_file("/tmp/isgw.isdn")) copy("/tmp/isgw.isdn","/usr/conf/isgw.isdn");
	include "ini_to_db_functions.php";
	$db= new beroAri();
	if (!isgwisdn_initoDB($db,$strpath,1)){
		copy("backup/isgw.isdn","isgw.isdn");
     	echo "<script>alert('Failed!!')</script>";
	}
	act();
}
if ($file_name=="isgw.analog"){
	copy($tmp_file,"/tmp/isgw.analog");
	chdir("/usr/conf");
	if (!is_dir("backup")) mkdir("backup");
	copy("isgw.analog","backup/isgw.analog");
	unlink("isgw.analog");
	if (is_file("/tmp/isgw.analog")) copy("/tmp/isgw.analog","/usr/conf/isgw.analog");
	include "ini_to_db_functions.php";
	$db= new beroAri();
	if (!isgwanalog_initoDB($db,$strpath,1)){
		copy("backup/isgw.analog","isgw.analog");
     	echo "<script>alert('Failed!!')</script>";
	}
	act();
}
if ($file_name=="isgw.gsm"){
	copy($tmp_file,"/tmp/isgw.gsm");
	chdir("/usr/conf");
	if (!is_dir("backup")) mkdir("backup");
	copy("isgw.gsm","backup/isgw.gsm");
	unlink("isgw.gsm");

	if (is_file("/tmp/isgw.gsm")) copy("/tmp/isgw.gsm","/usr/conf/isgw.gsm");
	include "ini_to_db_functions.php";
	$db= new beroAri();
	if (!isgwgsm_initoDB($db,$strpath,1)){
		copy("backup/isgw.gsm","isgw.gsm");
     	echo "<script>alert('Failed!!')</script>";
	}
	act();
}
if ($file_name=="isgw.causes"){
	copy($tmp_file,"/tmp/isgw.causes");
	chdir("/usr/conf");
	if (!is_dir("backup")) mkdir("backup");
	copy("isgw.causes","backup/isgw.causes");
	unlink("isgw.causes");

	if (is_file("/tmp/isgw.causes")) copy("/tmp/isgw.causes","/usr/conf/isgw.causes");
	include "ini_to_db_functions.php";
	$db= new beroAri();
	if (!isgwcauses_initoDB($db,$strpath,1)){
		copy("backup/isgw.causes","isgw.causes");
     	echo "<script>alert('Failed!!')</script>";
	}
	act();
}
if ($file_name=="isgw.dialplan"){
	copy($tmp_file,"/tmp/isgw.dialplan");
	chdir("/usr/conf");
	if (!is_dir("backup")) mkdir("backup");
	copy("isgw.dialplan","backup/isgw.dialplan");
	unlink("isgw.dialplan");

	if (is_file("/tmp/isgw.dialplan")) copy("/tmp/isgw.dialplan","/usr/conf/isgw.dialplan");
	include "ini_to_db_functions.php";
	$db= new beroAri();
	if (!isgwdialplan_initoDB($db,$strpath,1)){
		copy("backup/isgw.dialplan","isgw.dialplan");
     	echo "<script>alert('Failed!!')</script>";
	}
	act();
}
if ($file_name=="isgw.cpt"){
	copy($tmp_file,"/tmp/isgw.cpt");
	chdir("/usr/conf");
	if (!is_dir("backup")) mkdir("backup");
	copy("isgw.cpt","backup/isgw.cpt");
	unlink("isgw.cpt");
	if (is_file("/tmp/isgw.cpt")) copy("/tmp/isgw.cpt","/usr/conf/isgw.cpt");
	include "ini_to_db_functions.php";
	$db= new beroAri();
	if (!isgwcpt_initoDB($db,$strpath,1)){
		copy("backup/isgw.cpt","isgw.cpt");
     	echo "<script>alert('Failed!!')</script>";
	}
	act();
}
if ($file_name=="hardware.conf"){
	copy($tmp_file,"/tmp/hardware.conf");
	chdir("/usr/conf");
	if (!is_dir("backup")) mkdir("backup");
	copy("hardware.conf","backup/hardware.conf");
	unlink("hardware.conf");
	if (is_file("/tmp/hardware.conf")) copy("/tmp/hardware.conf","/usr/conf/hardware.conf");
	include "ini_to_db_functions.php";
	$db= new beroAri();
	if (!isdncfg_initoDB($db,$strpath,1)){
		copy("backup/hardware.conf","hardware.conf");
     	echo "<script>alert('Failed!!')</script>";
	}
	act();
}
if ($file_name=="misc.conf"){
	copy($tmp_file,"/tmp/misc.conf");
	chdir("/usr/conf");
	if (!is_dir("backup")) mkdir("backup");
	copy("misc.conf","backup/misc.conf");
	unlink("misc.conf");
	if (is_file("/tmp/misc.conf")) copy("/tmp/misc.conf","/usr/conf/misc.conf");
	include "ini_to_db_functions.php";
	$db= new beroAri();
	if (!misc_initoDB($db,$strpath,1)){
		copy("backup/misc.conf","misc.conf");
     	echo "<script>alert('Failed!!')</script>";
	}
	act();
}

/*
exec("/bin/cp ".$tmp_file." /tmp/beroGui.tar.gz");
exec("/bin/tar -xvvz -f/tmp/beroGui.tar.gz -C /tmp/");
$tools=new DB_Sqlite_Tools(db_path);
if (file_exists("/tmp/ari.XML")){
exec("/bin/cp /tmp/ari.XML /usr/conf/");
exec("/bin/rm /usr/conf/ari.db");
$tools->createDBFromXML("/usr/conf/ari.XML","/usr/conf/ari.db");
}
if (file_exists("/tmp/root.XML")){
exec("/bin/cp -r /tmp/root.XML /usr/conf");
exec("/bin/rm /usr/conf/root.db");
$tools->createDBFromXML("/usr/conf/root.XML","/usr/conf/root.db");
}
*/

}

?>
