<?php

function liAssignment ($ba,$table,$li){
	$r=$ba->select("select distinct(type) from $table where lif='$li'");
	return sqlite_fetch_single($r);
}

session_start();
if ((isset($_SESSION['beroari_username']))&&(($_SESSION['beroari_time']+1200)>time())){
	$_SESSION['beroari_time']=time();

	include "./includes/variables.php";
	include "misc/database.php";
	include "./includes/check.php";

	include "includes/Sqlite/Tools.php";

	include "includes/password_func.php";

	$m=$_GET["m"];
	$f=$_GET["f"];

	switch($m){
		case "": require_once("modules/state.php"); $state=new State(); break;
		case "state": require_once("modules/state.php"); $state=new State(); break;
		case "wizard": require_once("misc/wizard.php"); $wizard=new Wizard(); break;
		case "state_calls": require_once("modules/state_calls.php"); $sc=new State_calls(); break;
		case "state_statistic": require_once("modules/state_statistic.php"); $ss=new State_Statistic(); break;
		case "ddebug": require_once("modules/ddebug.php"); $ddebug=new Dialplandebug(); break;
		case "ftrace": require_once("modules/ftrace.php"); $ftrace=new Fulltrace(); break;
		case "pstn_hw": require_once("modules/pstn_hw.php"); $p_hw = new Pstn_hw(); break;
		case "net_conf": require_once("modules/net_conf.php"); $net = new Net_conf(); break;
		case "time": require_once("modules/time_conf.php"); $time = new Time_conf(); break;
		case "dialplan": require_once("modules/dialplan.php"); $e = new Dialplan(); break;
		case "sec": require_once("modules/security.php"); $sec= new Sec(); break;
		case "up_tool": require_once("modules/update_tool.php"); $up_tool= new Up_tool(); break;
		case "prov": require_once("modules/provisioning.php"); $prov= new Provisioning(); break;
		case "info": require_once("modules/info.php"); $info=new Info(); break;
		case "rebres": require_once( "modules/rebres.php"); $rebres=new Rebres(); break;
		case "gen": require_once("modules/general.php"); $gen= new General(); break;
		case "sipg": require_once("modules/sip_general.php"); $sipg = new Sip_general(); break;
		case "shell": if ($_SESSION['beroari_username']=="beroAdmin"){
						require_once("modules/shell.php");

						$sh=new Shell();
						if ($_GET['m']=="shell")
						print $sh->display("m=shell,f=display,a=display");
					}
					break;
		case "remoteM": require_once("modules/remoteManagement.php"); $remoteM=new remoteManagement(); break;
		case "isdn-pri": require_once("modules/isdn.php"); $isdn = new Isdn(); break;
		case "isdn-bri": require_once("modules/isdn.php"); $isdn = new Isdn(); break;
		case "analog-fxo": require_once("modules/analog.php"); $analog = new Analog(); break;
		case "analog-fxs": require_once("modules/analog.php"); $analog = new Analog(); break;
		case "gsm": require_once("modules/gsm.php"); $gsm = new Gsm(); break;
		case "gsmGen": require_once("modules/gsm_general.php"); $gsmGen = new GsmGeneral(); break;
		case "sms": require_once("modules/sms.php"); $sms = new Sms(); break;
		case "sip": require_once("modules/sip.php"); $sip= new Sip(); break;
		case "cdr": require_once("modules/cdr.php"); $cdr=new Cdr(); break;
		case "berofos": require_once("modules/berofos.php"); $berofos=new Berofos(); break;
		case "back_res": require_once("modules/back_res.php"); $br= new Back_res(); break;
		case "causes": require_once("modules/causes.php"); $causes= new Isgw_causes(); break;
		case "callprogress": require_once("modules/callprogress.php"); $callprogress= new Call_progress(); break;
		case "callprogress_ent": require_once("modules/callprogress.php"); $callprogress= new Call_progress(); break;
		case "uninstall": require_once("modules/uninstall.php"); $uninstall= new Uninstall(); break;
	}




	//Das GET Parameter "m" bestimmt welches Modul angezeigt wird
	if ($m=="dialplan") {
		print $e->display("m=dialplan,f=display,a=display");
	} elseif ($m=="sip") {
		print $sip->display("m=sip,f=display,a=display");
	} elseif ($m=="sipg") {
		print $sipg->display("m=sipg,f=display,a=display");
	} elseif ($m=="isdn-pri") {
		print $isdn->display("m=isdn,f=display,a=display");
	} elseif ($m=="isdn-bri") {
		print $isdn->display("m=isdn,f=display,a=display");
	} elseif ($m=="analog-fxs") {
		print $analog->display("m=analog,f=display,a=display");
	} elseif ($m=="analog-fxo") {
		print $analog->display("m=analog,f=display,a=display");
	} elseif ($m=="gsmGen") {
		print $gsmGen->display();
	} elseif ($m=="gsm") {
		print $gsm->display("m=gsm,f=display,a=display");
	} elseif ($m=="sms") {
		print $sms->display();
	} elseif ($m=="pstn_hw") {
		print $p_hw->display("m=pstn_hw,f=display,a=display");
	} elseif ($m=="net_conf") {
		print $net->display("m=net_conf,f=display,a=display");
	} elseif ($m=="time") {
		print $time->display("m=time,f=display,a=display");
	} elseif ($m=="back_res"){
		print $br->display("m=back_res,f=display,a=display");
	} elseif ($m=="ddebug") {
		print $ddebug->display("m=ddebug,f=display,a=display");
	} elseif ($m=="causes") {
		print $causes->display("m=causes,f=display,a=display");
	} elseif ($m=="callprogress") {
		print $callprogress->display("m=callprogress,f=display,a=display");
	} elseif ($m=="ftrace") {
		print $ftrace->display("m=ftrace,f=display,a=display");
	} elseif ($m=="rebres") {
		print $rebres->display("m=rebres,f=display,a=display");
	} elseif ($m=="info") {
		print $info->display("m=info,f=display,a=display");
	} elseif ($m=="cdr") {
		print $cdr->display("m=cdr,f=display,a=display");
	} elseif ($m=="state") {
		print $state->display("m=state,f=display,a=display");
	} elseif ($m=="remoteM") {
		print $remoteM->display("m=remoteM,f=display,a=display");
	} elseif ($m=="state_calls") {
		print $sc->display("m=state_calls,f=display,a=display");
	} elseif ($m=="state_statistic") {
		print $ss->display("m=state_statistic,f=display,a=display");
	//Wenn der GET Parameter "m" nicht gesetzt ist, wir das Modul "State" per default angezeigt
	} elseif (!$m) {
		print $state->display("m=state,f=display,a=display");
	} elseif ($m=="gen"){
		print $gen->display("m=gen,f=display,a=display");
	} elseif ($m=="sec"){
		print $sec->display("m=sec,f=display,a=display");
	} elseif ($m=="up_tool"){
		print $up_tool->display("m=up_tool,f=display,a=display");
	} elseif ($m=="prov"){
		print $prov->display("m=prov,f=display,a=display");
	} elseif ($m=="berofos"){
		print $berofos->display("m=berofos,f=display,a=display");
	} elseif ($m=="wizard"){
		print $wizard->display();
	} elseif ($m=="uninstall"){
		print $uninstall->display("");
	}

	//Die Funktion action öffnet ein Popup für das Anlegen von neuen Einträgen
	if (($f=="action")&($m=="dialplan")) {
		print $e->action("m=dialplan,f=action,a=add");
	}elseif (($f=="action")&($m=="sip")){
		print $sip->action("m=sip,f=action,a=add");
	}elseif (($f=="action")&($m=="isdn-pri")){
		print $isdn->action("m=isdn,f=action,a=add");
	}elseif (($f=="action")&($m=="isdn-bri")){
		print $isdn->action("m=isdn,f=action,a=add");
	}elseif (($f=="action")&($m=="analog-fxs")){
		print $analog->action("m=analog,f=action,a=add");
	}elseif (($f=="action")&($m=="analog-fxo")){
		print $analog->action("m=analog,f=action,a=add");
	}elseif (($f=="action")&($m=="gsm")){
		print $gsm->action("m=gsm,f=action,a=add");
	}

	/* Überprüfung ab eine Hardwareänderung stattgefunden hat,
	 * Der Benutzer wird durch eine Meldung darauf hingewiesen,
	 * dass ein Factory reset erforderlich ist
	 */

	if ($_GET['action']!='do_install'){
		$ba = new beroAri();


		$res=file_get_contents('/proc/cmdline');
		preg_match("/.*revision=(?<revision>.*)/",$res,$revision);
		$revision=$revision['revision'];

		$res=file_get_contents('/proc/li0');
		preg_match("/type: (?<li0>.*)/",$res,$li0);
		$li0=$li0['li0'];

		$res=file_get_contents('/proc/li1');
		preg_match("/type: (?<li1>.*)/",$res,$li1);
		$li1=$li1['li1'];



		if (($revision=="1.80" || $revision=="1.8") && ($li0=="xhfcSi-2s02fxs" || $li1=="xhfcSi-2s02fxs" || $li0=="si-4fxs" || $li1=="si-4fxs")){
			print ("<script>document.getElementById('hardware_ch').innerHTML+=\" ".HARDWARE_SUPPORT_DIS."\";</script>");
		}

		if (liAssignment($ba,'isdn',1)=="bf4S0" && $li1!="xhfc-4su"){
			($_POST['action']=='updatedb') ? Helper::deleteModul(1,liAssignment($ba,'isdn',1),"isdn")
			: print ("<script>document.getElementById('hardware_ch').innerHTML=\"".HARDWARE_CHANGED_LI1_DIS."<a href='index.php?m=rebres&li1Old=bf4S0'>Click here</a>\";</script>");
		}elseif(liAssignment($ba,'isdn',1)=="bf1E1" && $li1!="hfc-e1"){
			($_POST['action']=='updatedb') ? Helper::deleteModul(1,liAssignment($ba,'isdn',1),"isdn")
			: print ("<script>document.getElementById('hardware_ch').innerHTML=\"".HARDWARE_CHANGED_LI1_DIS."<a href='index.php?m=rebres&li1Old=bf1E1'>Click here</a>\";</script>");
		}elseif(liAssignment($ba,'isdn',1)=="bf2E1" && $li1!="hfc-2e1"){
			($_POST['action']=='updatedb') ? Helper::deleteModul(1,liAssignment($ba,'isdn',1),"isdn")
			: print ("<script>document.getElementById('hardware_ch').innerHTML=\"".HARDWARE_CHANGED_LI1_DIS."<a href='index.php?m=rebres&li1Old=bf2E1'>Click here</a>\";</script>");
		}elseif(liAssignment($ba,'gsm',1)=="bf2GSM" && $li1!="telit-2GSM"){
			($_POST['action']=='updatedb') ? Helper::deleteModul(1,liAssignment($ba,'gsm',1),"gsm")
			: print ("<script>document.getElementById('hardware_ch').innerHTML=\"".HARDWARE_CHANGED_LI1_DIS."<a href='index.php?m=rebres&li1Old=bf2GSM'>Click here</a>\";</script>");
		}elseif(liAssignment($ba,'analog',1)=="bf4FXS" && $li1!="si-4fxs"){
			($_POST['action']=='updatedb') ? Helper::deleteModul(1,liAssignment($ba,'analog',1),"analog")
			: print ("<script>document.getElementById('hardware_ch').innerHTML=\"".HARDWARE_CHANGED_LI1_DIS."<a href='index.php?m=rebres&li1Old=bf4FXS'>Click here</a>\";</script>");
		}elseif(liAssignment($ba,'analog',1)=="bf4FXO" && $li1!="si-4fxo"){
			($_POST['action']=='updatedb') ? Helper::deleteModul(1,liAssignment($ba,'analog',1),"analog")
			: print ("<script>document.getElementById('hardware_ch').innerHTML=\"".HARDWARE_CHANGED_LI1_DIS."<a href='index.php?m=rebres&li1Old=bf4FXO'>Click here</a>\";</script>");
		}elseif(liAssignment($ba,'isdn',1)=="bf2S02FXS" && $li1!="xhfcSi-2s02fxs"){
			if ($_POST['action']=='updatedb'){ Helper::deleteModul(1,liAssignment($ba,'isdn',1),"isdn"); Helper::deleteModul(1,liAssignment($ba,'analog',1),"analog"); }
			else print ("<script>document.getElementById('hardware_ch').innerHTML=\"".HARDWARE_CHANGED_LI1_DIS."<a href='index.php?m=rebres&li1Old=bf2S02FXS'>Click here</a>\";</script>");
		}
		if ((liAssignment($ba,'isdn',1)=="" && liAssignment($ba,'analog',1)=="" && liAssignment($ba,'gsm',1)=="") && $li1!="unknown"){
			($_POST['action']=='updatedb') ? Helper::createModul()
			: print ("<script>document.getElementById('hardware_ch').innerHTML=\"".HARDWARE_CHANGED_LI1_DIS."<a href='index.php?m=rebres&li1Old=none'>Click here</a>\";</script>");
		}

		if (liAssignment($ba,'isdn',0)=="bf4S0" && $li0!="xhfc-4su"){
			print ("<script>document.getElementById('hardware_ch').innerHTML=\"".HARDWARE_CHANGED_DIS."\";</script>");
		}elseif(liAssignment($ba,'isdn',0)=="bf1E1" && $li0!="hfc-e1"){
			print ("<script>document.getElementById('hardware_ch').innerHTML=\"".HARDWARE_CHANGED_DIS."\";</script>");
		}elseif(liAssignment($ba,'isdn',0)=="bf2E1" && $li0!="hfc-2e1"){
			print ("<script>document.getElementById('hardware_ch').innerHTML=\"".HARDWARE_CHANGED_DIS."\";</script>");
		}elseif(liAssignment($ba,'gsm',0)=="bf2GSM" && $li0!="telit-2GSM"){
			print ("<script>document.getElementById('hardware_ch').innerHTML=\"".HARDWARE_CHANGED_DIS."\";</script>");
		}elseif(liAssignment($ba,'analog',0)=="bf4FXS" && $li0!="si-4fxs"){
			print ("<script>document.getElementById('hardware_ch').innerHTML=\"".HARDWARE_CHANGED_DIS."\";</script>");
		}elseif(liAssignment($ba,'analog',0)=="bf4FXO" && $li0!="si-4fxo"){
			print ("<script>document.getElementById('hardware_ch').innerHTML=\"".HARDWARE_CHANGED_DIS."\";</script>");
		}elseif(liAssignment($ba,'isdn',0)=="bf2S02FXS" && $li0!="xhfcSi-2s02fxs"){
			print ("<script>document.getElementById('hardware_ch').innerHTML=\"".HARDWARE_CHANGED_DIS."\";</script>");
		}
		if ((liAssignment($ba,'isdn',0)=="" && liAssignment($ba,'analog',0)=="" && liAssignment($ba,'gsm',0)=="") && $li0!="unknown"){
			print ("<script>document.getElementById('hardware_ch').innerHTML=\"".HARDWARE_CHANGED_DIS."\";</script>");
		}

	}

	$jumper=file("/proc/RESET_JUMPER");
	if ($jumper[0]==1){
		print ("<script>document.getElementById('reset_jumper').innerHTML='Reset jumper present. Please remove it after you have configured your network settings';</script>");
	}

//Karte oder Box neustarten
if (($_POST['v']=="Yes" && $_POST['action']=="reboot")){
	Helper::reboot();

}

chdir("/usr/local/www/berogui");
require_once("./includes/footer.php");

//Prüfung ob die Session noch gültig ist. Wenn nicht wird der Benutzer darauf hingewiesen.
}elseif(($_SESSION['beroari_time'])&&(($_SESSION['beroari_time']+1200)<time())){

	include "./includes/variables.php";
	include "misc/database.php";
	$ba = new beroAri();
	require_once("./includes/lang.php");
	session_unset();
	session_destroy();
	print '<html><head><link rel="stylesheet" type="text/css" href="http:./includes/template_css.css" />';
	print "<title>beroAri</title></head><body>";

	print "<br><table align=\"center\" bgcolor=\"#f0f0ec\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"500\">
	    <colgroup>
	    <col width=\"39\"></col>
	    </colgroup>
		<tbody><tr>
		<td colspan=\"2\"><img src=\"./includes/images/bg_top.png\"></td>
		</tr>
		<tr>
		<td background=\"./includes/images/bg_left.png\" style='background-repeat:repeat-y;' width=\"30\"><img src=\"./includes/images/trans.gif\" height=\"10\" width=\"39\"></td>
		<td><p align='left'><img src=\"./includes/images/berofixlogo.png\"/></p><center><hr size= \"1\" noshade>";

	echo SESSION_EXPIRED."<br>";
	echo $_SESSION['beroari_time']."<br>";
	echo '<a href="./includes/login.php">'.LOGIN.'</a>';

	print "<hr size= \"1\" noshade><br/>
	<table align='right'>
	<tr><td>
	<a href='http://wiki.beronet.com/' id='help'><img src='".IMAGE_HELP."' onmouseover=\"this.src='".IMAGE_HELP_HOVER."'\" onmouseout=\"this.src='".IMAGE_HELP."';\"/></a>
	</td>
	</tr>
	<tr>
	<td>
	<a href=\"http://www.beronet.com\" target=\"_blank\" onfocus=\"if (this.blur()) { this.blur(); }\">
	<img src=\"./includes/images/beroLogo.gif\" align=\"right\" border=\"0\">
	</a>
	</td>
	</tr>
	</table>";
	print "</center></td></tr>
		<tr>
		<td colspan=\"2\" style=\"background-repeat:no-repeat\" background=\"./includes/images/bg_bottom.png\">
		<img src=\"./includes/images/trans.gif\" border=\"0\" height=\"39\" width=\"10\">
		<div style=\"color: white;\" align=\"right\">" . APPVERSION . "&nbsp;&nbsp;&nbsp;</div>
		</td></tr>";
	print "</body></html>";
}else{
	print "<SCRIPT LANGUAGE='javascript'> location.href = './includes/login.php?m=sip';</SCRIPT><br>";

}

?>
