<?php
session_start();
      chdir("..");
	include "./includes/variables.php";
	include "./misc/database.php";
	include "password_func.php";
    include '/usr/local/php/beroConf.php';
	$ba= new beroAri();
	require_once("./includes/lang.php");


      if(trim($HTTP_POST_VARS['user']) != "" && trim($HTTP_POST_VARS['password']) != "")
       {

          $user = htmlentities($HTTP_POST_VARS['user'], ENT_QUOTES);

          $password = $HTTP_POST_VARS['password'];

          $result = $ba->select('SELECT * FROM users where user=\''.$user.'\'');

          if($row = $ba->fetch_array($result)){

              if(tep_validate_password($password,$row['password'])){

                $_SESSION['beroari_username'] = $row['user'];
                $_SESSION['beroari_group'] = $row['group_'];
		 		$_SESSION['beroari_time']=time();


				if ($row['group_']=='users'){
                 	echo "<SCRIPT LANGUAGE='javascript'> location.href = '../index.php?m=callsf';</SCRIPT>";
				}else{
					//App filesystem installieren wenn kein vorhanden ist
					$bc = new beroConf('root');

					$result=$ba->select("SELECT vnum FROM version");
					//Die Tabelle version exisitiert in der Datenbank
					$versionen=file("/usr/local/www/berogui/includes/versionen.php");
					if ($result!=false){
						$row = $ba->fetch_array($result);
						$sql="";
						$version="";
						$show=false;
						foreach ($versionen as $zeile){
							if ($show){
								if (strpos($zeile,"VERSION")===false) $sql.=$zeile;
							}
							//Wenn die Versionsnummer übereinstimmt mit der Versionsnummer der Datei, werden die SQL Statements
							//ab diese Zeile gespeichert
							if (strpos($zeile,"VERSION")!==false){
								$values=split(' ',$zeile);
								$values[1]=trim($values[1]);
								$version=$values[1];
								if ($values[1]==$row['vnum']){
									$show=true;
								}
							 }

							 if ($version==13 && $show==true){
							 	$query = $ba->select(' SELECT * from dialplan ORDER BY id');
							 	while ($entry = $ba->fetch_array($query)) {
							 		if ($entry['isdnid']!="" && !in_array($entry['isdnid'],$misdnids)){
							 			array_push($misdnids,$entry['isdnid']);
										$q = $ba->select("select fromId, dest, src, id, newDest, newSrc from dialplan where isdnid='".$entry['isdnid']."' and direction='isdn-sip'");
										$en=$ba->fetch_array($q);
										$q1 = $ba->select("select toId, dest, src, id, newDest, newSrc from dialplan where isdnid='".$entry['isdnid']."' and direction='sip-isdn'");
										$en1=$ba->fetch_array($q1);
										$ba->update("UPDATE dialplan set newDest='".$entry['isdnid'].$en1['newDest']."', newSrc='".$en1['newSrc']."' WHERE id='".$en['id']."'");
										$ba->update("UPDATE dialplan set newDest='\\\\1', newSrc='\\\\1' WHERE id ='".$en1['id']."'");
									}
								}
								$version=14;
							 }
						}
						//echo $sql;
						$sql=trim($sql);
						if ($sql!="")	$ba->exec_query($sql);
						$ba->exec_query("UPDATE version SET vnum='".$version."'");
					//Die Tabelle version exisitiert nicht in der Datenbank
					}else{

						$sql="";
						$version="";
						foreach ($versionen as $zeile){
							if (strpos($zeile,"VERSION")===false) $sql.=$zeile;
							else{
								$values=split(' ',$zeile);
								$values[1]=trim($values[1]);
								$version=$values[1];

							}


							if ($version==13){
							 	$query = $ba->select(' SELECT * from dialplan ORDER BY id');
							 	while ($entry = $ba->fetch_array($query)) {
							 		if ($entry['isdnid']!="" && !in_array($entry['isdnid'],$misdnids)){
							 			array_push($misdnids,$entry['isdnid']);
										$q = $ba->select("select fromId, dest, src, id, newDest, newSrc from dialplan where isdnid='".$entry['isdnid']."' and direction='isdn-sip'");
										$en=$ba->fetch_array($q);
										$q1 = $ba->select("select toId, dest, src, id, newDest, newSrc from dialplan where isdnid='".$entry['isdnid']."' and direction='sip-isdn'");
										$en1=$ba->fetch_array($q1);
										$ba->update("UPDATE dialplan set newDest='".$entry['isdnid'].$en1['newDest']."', newSrc='".$en1['newSrc']."' WHERE id='".$en['id']."'");
										$ba->update("UPDATE dialplan set newDest='\\\\1', newSrc='\\\\1' WHERE id ='".$en1['id']."'");
									}
								}
								$version=14;
							 }
						}

						$ba->exec_query($sql);
						$ba->exec_query("UPDATE version SET vnum='".$version."'");
					}

					//Dantenbank Überprüfung ob ISDN/Analog Daten gespeichert sind
					//(Wenn nicht, werden die Line Interfaces gelesen).
					$query_isdn=$ba->select("select lif, type from isdn group by lif");
					$query_analog=$ba->select("select lif, type from analog group by lif");
					$query_gsm=$ba->select("select lif, type from gsm group by lif");
					if (sqlite_num_rows($query_isdn)==0 && sqlite_num_rows($query_analog)==0 && sqlite_num_rows($query_gsm)==0){
						$ba=new beroAri();
						$res=file_get_contents('/proc/li0');
						preg_match("/type: (?<li0>.*)/",$res,$li0);
						$li0=$li0['li0'];
						if ($li0=="xhfc-4su"){
							for ($i=0;$i<=3;$i++){
								$ba->insert_("INSERT INTO isdn (type,lif,master, config, port_type) VALUES('bf4S0','0','1','','BRI');");
							}
						}elseif($li0=="hfc-e1"){
							$ba->insert_("INSERT INTO isdn (type,lif,master, config, port_type) VALUES('bf1E1','0','1','','PRI');");
						}elseif($li0=="hfc-2e1"){
							for ($i=0;$i<=1;$i++){
								$ba->insert_("INSERT INTO isdn (type,lif,master, config, port_type) VALUES('bf2E1','0','1','','PRI');");
							}
						}elseif($li0=="telit-2GSM"){
							for ($i=0;$i<=1;$i++){
								$ba->insert_("INSERT INTO gsm (type,lif,master, config, port_type) VALUES('bf2GSM','0','1','','GSM');");
							}
						}elseif($li0=="si-4fxs"){
							for ($i=0;$i<=3;$i++){
								$ba->insert_("INSERT INTO analog (type,lif,master, config, port_type) VALUES('bf4FXS','0','1','','FXS');");
							}
						}elseif($li0=="si-4fxo"){
							for ($i=0;$i<=3;$i++){
								$ba->insert_("INSERT INTO analog (type,lif,master, config, port_type) VALUES('bf4FXO','0','1','','FXO');");
							}
						}elseif($li0=="xhfcSi-2s02fxs"){
							for ($i=0;$i<=1;$i++){
								$ba->insert_("INSERT INTO isdn (type,lif,master, config, port_type) VALUES('bf2S02FXS','0','1','','BRI');");
							}
							for ($i=0;$i<=1;$i++){
								$ba->insert_("INSERT INTO analog (type,lif,master, config, port_type) VALUES('bf2S02FXS','0','1','','FXS');");
							}
						}
						$res=file_get_contents('/proc/li1');
						preg_match("/type: (?<li1>.*)/",$res,$li1);
						$li1=$li1['li1'];
						if ($li1=="xhfc-4su"){
							for ($i=0;$i<=3;$i++){
								$ba->insert_("INSERT INTO isdn (type,lif,master, config, port_type) VALUES('bf4S0','1','0','', 'BRI');");
							}
						}elseif($li1=="hfc-e1"){
							$ba->insert_("INSERT INTO isdn (type,lif,master, config, port_type) VALUES('bf1E1','1','0','', 'PRI');");
						}elseif($li1=="hfc-2e1"){
							for ($i=0;$i<=1;$i++){
								$ba->insert_("INSERT INTO isdn (type,lif,master, config, port_type) VALUES('bf2E1','1','0','','PRI');");
							}
						}elseif($li1=="telit-2GSM"){
							for ($i=0;$i<=1;$i++){
								$ba->insert_("INSERT INTO gsm (type,lif,master, config, port_type) VALUES('bf2GSM','1','1','','GSM');");
							}
						}elseif($li1=="si-4fxs"){
							for ($i=0;$i<=3;$i++){
								$ba->insert_("INSERT INTO analog (type,lif,master, config, port_type) VALUES('bf4FXS','1','0','','FXS');");
							}
						}elseif($li1=="si-4fxo"){
							for ($i=0;$i<=3;$i++){
								$ba->insert_("INSERT INTO analog (type,lif,master, config, port_type) VALUES('bf4FXO','1','0','','FXO);");
							}
						}elseif($li1=="xhfcSi-2s02fxs"){
							for ($i=0;$i<=1;$i++){
								$ba->insert_("INSERT INTO isdn (type,lif,master, config, port_type) VALUES('bf2S02FXS','1','0','','BRI');");
							}
							for ($i=0;$i<=1;$i++){
								$ba->insert_("INSERT INTO analog (type,lif,master, config, port_type) VALUES('bf2S02FXS','1','0','','FXS');");
							}
						}


						$q=$ba->select("SELECT type, lif FROM isdn union SELECT type, lif FROM analog union SELECT type, lif FROM gsm order by lif");
						$i=0;
						while ($ent=$ba->fetch_array($q)){
							$li[$i]=$ent['lif'];
							$type[$i]=$ent['type'];
							$i++;
						}
						$res=file_get_contents('/proc/cmdline');
						preg_match("/.*revision=(?<revision>.*)/",$res,$revision);
						$revision=$revision['revision'];
						if (($type[0]=="bf4S0" || $type[0]=="bf2S02FXS" || $type[0]=="bf4FXS" || $type[0]=="bf4FXO") && !isset($type[1]) && $revision!="1.80" && $revision!="1.8"){							$ba->select("select type, lif from isdn group by type,lif order by lif");
							$ba->select("INSERT into key_val VALUES('portswitch',1)");
						}

						if ($ba->is_error()){
							echo "<script>alert(".$ba->error().");</script>";
						}
						$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 LANGUAGE='javascript'> location.href = '../index.php?m=state';</SCRIPT>";
					}else{
						echo "<SCRIPT LANGUAGE='javascript'> location.href = '../index.php?m=state';</SCRIPT>";
					}
					//echo "<SCRIPT LANGUAGE='javascript'> location.href = '../index.php?m=state';</SCRIPT>";
				}

              }else{

                  echo "<script>alert('".LOGIN_PASS_FALSE_DIS."');</script>";
		  echo "<SCRIPT LANGUAGE='javascript'> location.href = './login.php?m=sip';</SCRIPT>";

              }

          }else{

              echo "<script>alert('".LOGIN_USER_NOT_EXIST_DIS."');</script>";

		  echo "<SCRIPT LANGUAGE='javascript'> location.href = './login.php?m=sip';</SCRIPT>";
          }


      }else{

         	 echo "<script>alert('".LOGIN_PASS_USER_EMPTY_DIS."')</script>";
		 echo "<SCRIPT LANGUAGE='javascript'> location.href = './login.php?m=sip';</SCRIPT>";

      }
?>
