<?php
//Factory Reset, Datenbank löschen und neu erzeugen
chdir("..");

include "./includes/variables.php";
include "./misc/database.php";
include "../../php/beroConf.php";
//Reset ari.db
if (isset($_POST['keep_sip_set'])){
	$ba=new beroAri();
	$q=$ba->select('select * from sip;');
	$inserts=array();
	$i=0;
	while ($entry=sqlite_fetch_array($q, SQLITE_ASSOC)){
		$fields="";
		$values="";
		foreach ($entry as $key=>$val){
			($fields=="") ? $fields=$key : $fields.=",".$key;
			($values=="") ? $values="'".$val."'" : $values.=",'".$val."'";
		}
		$inserts[$i++]=" Insert into sip ($fields) VALUES ($values); ";
	}
}
system("/bin/rm /usr/conf/ari.db");
$ba=new beroAri();
if (isset($_POST['keep_sip_set'])) $ba->create_database();

if ($ba->is_error()){
	echo "<script>alert(\"".$ba->error()."\");</script>";
}

	//reset also rootdb versions
	$bc=new beroConf('root');
	$bc->exec_sql("delete from root where key not in('root-version','lan-mtu','lan-iface','lan-ipaddr','lan-netmask','lan-gateway','lan-dhcp','msp-iface','msp-ipaddr','app-image','app-status','ntphost','cloud_enable','cloud_key');");
	if (!isset($_POST['keep_set'])){
		$bc->set('root','lan-iface','eth0');
		$bc->set('root','lan-ipaddr','10.0.0.2');
		$bc->set('root','lan-netmask','255.0.0.0');
		$bc->set('root','lan-gateway','10.0.0.1');
		$bc->set('root','lan-dhcp','no');
		$bc->set('root','lan-mtu','1500');
	}
	if (!isset($_POST['keep_cloud_set'])){
		$bc->exec_sql("delete from root where key in('cloud_enable','cloud_key');");
	}
	$bc->set('root','msp-iface','eth1');
	$bc->set('root','msp-ipaddr','auto');
	$bc->set('root','app-status','running');
	$bc->set('root','ntphost','');

	if (isset($_POST['keep_sip_set'])) foreach ($inserts as $sql) $ba->insert_($sql);

	@unlink("/usr/conf/apiusers.conf");
    $handle = fopen("/usr/conf/apiusers.conf","w");
    fputs($handle, "admin:". "admin");
    fclose($handle);
	//reset rootdb done

	if ($_POST['v']=='Wizard')
		echo "<script>window.location.href=\"wizard.php\"</script>";
	else
		echo "<script>window.location.href=\"../includes/create_files.php\"</script>";



?>
