<script LANGUAGE="JavaScript">

function confirmSubmit()
{
var agree=confirm("Note !!! If you continue all previous configurations\non the berofix will be deleted. Press ok if want to proceed!\nNew Config have to be activated (indicated by red activate\nbutton) after you login to the berofix.");
if (agree)
	return true ;
else
	return false ;
}
// -->
</script>


<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="icon" href="../berogui/includes/images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="http:../berogui/includes/template_css.css">
<title>3CX Wizard</title></head><body>
<script language="JavaScript" type="text/javascript" src="../berogui/includes/notes/wz_tooltip.js"></script>
</head>

<?php
/*
3CX.php
*/
		$debug=1;
        include("../../php/beroConf.php");
        include("../berogui/misc/database.php");
        include("../berogui/includes/isgwtelnet.php");

        define (EXTLINES,"Select your Outgoing ports. This means the Ports, which<br>are connected to your PSTN Provider. If you select more then<br>one, the ports are going to be grouped. Note that this wizard<br>supports only one Group.");
        define (COUNTRY,"Select your Country Settings. Tones would<br>be configured according to your country");
        define (S3CXIP,"Enter the IP-Address of the 3CX Phone System");
        define (S3CXAUTHID,"Enter Auth-ID of the port on the 3CX Phone Systems");
        define (S3CXAUTHPWD,"Enter the AuthPassword for the above Auth-ID");
        define (FXOCOUNTRY,"Normaly you can select your Country. In special cases<br>you have to select special settings like FCC/TBR21 etc.<br>if you detect any problems.");
        define (FXODID,"Enter optional the DID of your select outgoing FXO Ports.<br>These DID would be signalled to the 3CX to identify<br>from which FXO Port the call is originated");
        define (CNS,"In case your ISDN Ports supports CLIP_NO_SCRRENING,<br>you have to activate it to use this feature.");
        define (CD,"In case your ISDN Ports supports Call Deflection or<br>Partial Rerounting, you have to activate it to use this feature.");
        define (PROTSET,"Linetype PTP Point to Point for DIDs<br>Linetype PTMP PointToMultiPoint for MSNs");
        define (FXS,"Enter optional a MSN or DID for this FXS Ports. All Calls to<br>this specific MSN/DID would be automatic redirected to<br>this Port (ISDN->Analog) on the berofix internaly. Vice<br>versa all Calls from this FXS Port would be redirected<br>to your external ISDN Ports(Analog->ISDN)<br>using this specific MSN/DID as CallerID/OAD.");
        define (GSMGENERAL,"Enter GSM specific settings for each GSM Port.<br>-SIM-PIN: Enter the PIN of the SIM card assigned to this Port, leave empty if no PIN is not set<br>-SMSC: Enter optional the SMS Service Center Number (i.e. +491760000443 for O2-Germany)<br>-Extension: Enter optional the phone number of the SIM card assigned to this port");
        define (GSMPREFIX,"Enter optional a list of prefixes (i.e. 0176,0177). <br>All Calls starting with that prefix would be automatic routed to this GSM Port (SIP->GSM).<br>If prefixes of Port 1 and 2 are the same, GSM Port 1 and 2 are going to be configurred as a GSM Portgroup");

        $sent=$_GET['sent'];
		if ($sent=='yes'){
			$lif0ports=$_GET['lif0ports'];
			$lif1ports=$_GET['lif1ports'];
			$proto=$_GET['proto'];
			$tones=$_GET['tones'];
			$cns=$_GET['cns'];
			$cd=$_GET['cd'];
			$ipaddress=$_GET['ip'];
			$auth=$_GET['auth'];
			$pwd=$_GET['pwd'];
			//FXS
			$did=$_GET['did'];
			//FXO
			$fxodid=$_GET['fxodid'];
			$fxocountry=$_GET['FXOcountry'];
			//GSM
			$gsmpin=$_GET['gsmpin'];
			$gsmsmsc=$_GET['gsmsmsc'];
			$gsmext=$_GET['gsmext'];
			$gsmprefix =$_GET['gsmprefix'];
		}

		//Validate Values:
		if($sent=="yes"){
			$err=0;
			$errip=0;
			$errip=0;
			$errauth=0;
			if ((count($lif0ports)==0) && (count($lif1ports)==0)) {
				//echo "select ports please";
				$err="1";
				$errports=1;
			}
			$string = $ipaddress;
			if (!preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/',$string)) {
				//echo "IP address is bad.";
				$err=1;
				$errip=1;
			}
			if($auth==""){
				//echo "auth=leer";
				//$err=1;
				$errauth=1;
			}
			if(strlen($auth)>=21){
				//echo "auth>21";
				//$err=1;
				$errauth=1;
			}
			if ($err==0){
				createconfigfiles($lif0ports,$lif1ports,$proto,$tones,$cns,$cd,$ipaddress,$auth,$pwd,$did,$fxodid,$fxocountry,$gsmpin,$gsmsmsc,$gsmext,$gsmprefix);
			}
		}


	//HEADER
	include "../berogui/includes/variables.php";

	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=\"../berogui/includes/images/bg_top.png\"></td>
		</tr>
		<tr>
		<td background=\"../berogui/includes/images/bg_left.png\" style='background-repeat:repeat-y;' width=\"30\"><img src=\"../berogui/includes/images/trans.gif\" height=\"10\" width=\"39\"></td>
		<td nowrap><p align='left'><img src=\"../berogui/includes/images/berofixlogo.png\"/></p><center><hr size= \"1\" noshade>
		<br>";
		echo "<font face='Loma,Sans,Arial' size='5' color='#918e8c'>3CX Wizard</font><br>";
		if($err!=0){
			echo "<font face='Loma,Sans,Arial' size='3' color='#FF0000'>Error occured. Please checked the marked fileds below.</font><br>";
		}
		echo "<form action='3CX.php'>";
		$supportedconfig=0;

		$lif=getlif();
		//External BRI
		if($lif[0]=="bf2S02FXS" && $lif[1]=="unknown")$supportedconfig=1; //
		if($lif[0]=="bf2S02FXS" && $lif[1]=="bf4FXS")$supportedconfig=1; //
		if($lif[0]=="bf2S02FXS" && $lif[1]=="bf4S0")$supportedconfig=1; //
		if($lif[0]=="bf2S02FXS" && $lif[1]=="bf2S02FXS")$supportedconfig=1; //
		if($lif[0]=="bf2S02FXS" && $lif[1]=="bf2GSM")$supportedconfig=1; //
		if($lif[0]=="bf2S02FXS" && $lif[1]=="bf4FXO")$supportedconfig=1; //


		if($lif[0]=="bf4S0" && $lif[1]=="bf2S02FXS")$supportedconfig=1;	 //
		if($lif[0]=="bf4S0" && $lif[1]=="bf4S0")$supportedconfig=1;
		if($lif[0]=="bf4S0" && $lif[1]=="bf4FXS")$supportedconfig=1; //
		if($lif[0]=="bf4S0" && $lif[1]=="unknown")$supportedconfig=1;//
		if($lif[0]=="bf4S0" && $lif[1]=="bf2GSM")$supportedconfig=1; //
		if($lif[0]=="bf4S0" && $lif[1]=="bf4FXO")$supportedconfig=1; //

		//External PRI
		if($lif[0]=="bf2E1" && $lif[1]=="unknown")$supportedconfig=1;
		if($lif[0]=="bf2E1" && $lif[1]=="bf2E1")$supportedconfig=1;
		if($lif[0]=="bf2E1" && $lif[1]=="bf1E1")$supportedconfig=1;
		if($lif[0]=="bf2E1" && $lif[1]=="bf4S0")$supportedconfig=1;
		if($lif[0]=="bf2E1" && $lif[1]=="bf2S02FXS")$supportedconfig=1;//
		if($lif[0]=="bf2E1" && $lif[1]=="bf4FXS")$supportedconfig=1;
		if($lif[0]=="bf2E1" && $lif[1]=="bf4FXO")$supportedconfig=1; //
		if($lif[0]=="bf2E1" && $lif[1]=="bf2GSM")$supportedconfig=1; //

		if($lif[0]=="bf1E1" && $lif[1]=="unknown")$supportedconfig=1; //
		if($lif[0]=="bf1E1" && $lif[1]=="bf4S0")$supportedconfig=1; //
		if($lif[0]=="bf1E1" && $lif[1]=="bf1E1")$supportedconfig=1;
		if($lif[0]=="bf1E1" && $lif[1]=="bf2E1")$supportedconfig=1; //
		if($lif[0]=="bf1E1" && $lif[1]=="bf2S02FXS")$supportedconfig=1; //
		if($lif[0]=="bf1E1" && $lif[1]=="bf4FXS")$supportedconfig=1; //
		if($lif[0]=="bf1E1" && $lif[1]=="bf4FXO")$supportedconfig=1; //
		if($lif[0]=="bf1E1" && $lif[1]=="bf2GSM")$supportedconfig=1; //
		//External FXO
		if($lif[0]=="bf4FXO" && $lif[1]=="unknown")$supportedconfig=1; //
		if($lif[0]=="bf4FXO" && $lif[1]=="bf4FXO")$supportedconfig=1;  //
		if($lif[0]=="bf4FXO" && $lif[1]=="bf2S02FXS")$supportedconfig=1; //
		if($lif[0]=="bf4FXO" && $lif[1]=="bf4FXS")$supportedconfig=1;//
		if($lif[0]=="bf4FXO" && $lif[1]=="bf4S0")$supportedconfig=1; //
		if($lif[0]=="bf4FXO" && $lif[1]=="bf2GSM")$supportedconfig=1; //
		//External bf4GSM
		if($lif[0]=="bf2GSM" && $lif[1]=="unknown")$supportedconfig=1; //
		if($lif[0]=="bf2GSM" && $lif[1]=="bf4FXO")$supportedconfig=1;
		if($lif[0]=="bf2GSM" && $lif[1]=="bf2S02FXS")$supportedconfig=1; //
		if($lif[0]=="bf2GSM" && $lif[1]=="bf4FXS")$supportedconfig=1;
		if($lif[0]=="bf2GSM" && $lif[1]=="bf4S0")$supportedconfig=1; //
		if($lif[0]=="bf2GSM" && $lif[1]=="bf2GSM")$supportedconfig=1; //

		//echo $lif[0] . " ". $lif[1];

if (!$supportedconfig){
	echo "<font face='Loma,Sans,Arial' size='3' color='#FF0000'>Wizard doesn't support this Hardware configuration.<br> Wizard not useable</font><br>";
}else{
	echo "<br/>";
	echo "<table class='contenttocc' width='98%'><tr style='height:28'><th colspan='2' ><div nowrap>PSTN Outgoing options</div></th></tr>";

	//HEADER
	for ($i = 0; $i <= 2 ; $i++)
	{
		if($lif[$i]=="bf4S0"){
			$types[$i]="BRI";
			$ports[$i]="4";
		}elseif($lif[$i]=="bf1E1"){
			$types[$i]="PRI";
			$ports[$i]="1";
		}elseif($lif[$i]=="bf2E1"){
			$types[$i]="PRI";
			$ports[$i]="2";
		}elseif($lif[$i]=="bf2S02FXS"){
			$types[$i]="BRI/FXS";
			$ports[$i]="2";
		}elseif($lif[$i]=="bf4FXS"){
			$types[$i]="FXS";
			$ports[$i]="4";
		}elseif($lif[$i]=="bf4FXO"){
			$types[$i]="FXO";
			$ports[$i]="4";
		}elseif($lif[$i]=="bf2GSM"){
			$types[$i]="GSM";
			$ports[$i]="2";
		}elseif($lif[$i]=="unknown"){
			$types[$i]="unknown";
			$ports[$i]="0";
		}
	}
		// 1. Group Ports
		$check ="<input type='hidden' name='sent' value='yes'>";
		$check.="Available Ports Lif0:";
		for ($i = 1; $i <=  $ports[0] ; $i++){
					$checked="";
					if(count($lif0ports)!=0){
						foreach ($lif0ports as $pr){
							if($pr==$i){
						//		echo "checked1:" . $i . "\n";
								$checked="checked";
							}
						}
					}
					//echo "lif0:" . $i . "\n";
					$check.="<input type='checkbox' name='lif0ports[]' value='" . $i . "'" . $checked . "> " . $i . "";
		}
		$check.="<br>";



		if(($types[1]==$types[0]) ||( $types[0]=="BRI" && $types[1]=="BRI/FXS" ) ||( $types[0]=="BRI/FXS" && $types[1]=="BRI" )){
			$check.="Available Ports Lif1:";
			for ($i = $ports[0]+1; $i <=  $ports[0]+$ports[1] ; $i++){
					$checked="";
					if(count($lif1ports)!=0){
						foreach ($lif1ports as $pr){
							if($pr==$i){
						//		echo "checked1:" . $i . "\n";
								$checked="checked";
							}
						}
					}
					//echo "lif1:" . $i . "\n";
					$check.="<input type='checkbox' name='lif1ports[]' value='" . $i . "'" . $checked . "> " . $i  . "";
			}
		}

		if($errports)$bgcolor="style='background-color:#FF0000'";
		if ($types[0]=="BRI")$stext="ISDN BRI";
		if ($types[0]=="PRI")$stext="ISDN PRI";
		if ($types[0]=="BRI/FXS")$stext="ISDN BRI";
		if ($types[0]=="FXO")$stext="analog FXO";
		if ($types[0]=="GSM")$stext="GSM";


		$strwidth="50%";
		if ($types[0]=="GSM")
		{
			$strwidth="40%";
		}


		$strtool="onmouseover=\"Tip('" .EXTLINES . "')\" onmouseout='UnTip()'";
		$ret .= "<tr " . $bgcolor . " " . $strtool . " style='height:28'><td nowrap style='width:" . $strwidth . "' align='left'>Select your external " . $stext . " lines.</td><td align='center'><div nowrap>" . $check . "</div></td></tr>";
		//<td class="tm"  onmouseover="return escape('Change szenario to Bypass')"
		$bgcolor="";
		echo $ret;
		//

		if($types[0]=="FXO"){
				$port=1;
				for ($i = 0; $i < 2; $i++)
				{
					if ($types[$i]=="FXO" || $types[$i]=="FXO"){
						for ($z = 1; $z < $ports[$i]+1; $z++)
						{
                            //$ret .= "<tr " . $bgcolor . "><td align='left' style='width:50%'>Enter MSN/DID to redirect to/from FXS Port'" . $port .  "'</td><td style='text-align:center'> <input type=text name='did[]' value='" . $did[$port-1] . "' size='30'>  </td></tr>";
                            $strtool="onmouseover=\"Tip('" .FXODID . "')\" onmouseout='UnTip()'";
							$ret1.= "<tr " . $strtool . " style='height:27'><td style='text-align:left'><div nowrap>DID of FXO Port: " . $port .  "&nbsp </div></td><td style='text-align:left'> <input type=text name='fxodid[]' value='" . $fxodid[$port-1] . "' size='30'>  </td></tr>";
							$port++;
						}
					}
				}

				echo $ret1;
		}
		if($types[0]=="GSM"){
				$gport=1;
				for ($i = 0; $i < 2; $i++)
				{
					if ($types[$i]=="GSM" || $types[$i]=="GSM"){
						for ($z = 1; $z < $ports[$i]+1; $z++)
						{
                            $strtool="onmouseover=\"Tip('" . GSMGENERAL . "')\" onmouseout='UnTip()'";
							$ret1 .= "<tr " . $bgcolor . " " . $strtool . "><td align='left' style='width:40%'><div nowrap>Enter settings for GSM Port '" . $gport .  "'</div></td><td style='text-align:left' ><div nowrap>Sim-PIN:<input type=text name='gsmpin[]' value='" . $gsmpin[$gsmport-1] . "' size='2'>SMSC:<input type=text name='gsmsmsc[]' value='" . $gsmsmsc[$gsmport-1] . "' size='12'>Extension:<input type=text name='gsmext[]' value='" . $gsmext[$gsmport-1] . "' size='12'>  </div></td></tr>";
							$gport++;
						}
					}
				}

				echo $ret1;
		}


		// 2. Protocol Settings

		if (($types[0]=="BRI") || ($types[0]=="BRI/FXS")) {
			$select="<select name='proto'>";
			$selected="";
			if($proto==='ptp')$selected="selected";
			$select.="<option name='ptp' size='40' value='ptp'" . $selected . ">PTP</option>";
			$selected="";
			if($proto=="ptmp")$selected="selected";
			$select.="<option name='ptmp' size='40' value='ptmp'" . $selected . ">PTMP</option></select>";
			$strtool="onmouseover=\"Tip('" . PROTSET . "')\" onmouseout='UnTip()'";
			$ret = "<tr " . $strtool . " style='height:28'><td align='left'><div nowrap>Are lines Point to Point (PTP) or Point to Multi Point (PMTP) ?</div></td><td align='center'><div nowrap>" . $select . "</div></td></tr>";
			echo $ret;
		}
		// 2.1 FXO Country Settings in case
		if($types[0]=="FXO"){
			//.FXOCOUNTRY
			$strtool="onmouseover=\"Tip('" . FXOCOUNTRY . "')\" onmouseout='UnTip()'";
			//<select name='FXOcountry'><option value='FCC'>FCC</option><option value='TBR21'>TBR21</option><option value='1TR110_DE'>1TR110_DE</option><option value='ARGENTINA'>ARGENTINA</option><option value='AUSTRALIA'>AUSTRALIA</option><option value='AUSTRIA'>AUSTRIA</option><option value='BAHRAIN'>BAHRAIN</option><option value='BELGIUM'>BELGIUM</option><option value='BRAZIL'>BRAZIL</option><option value='BULGARIA'>BULGARIA</option><option value='CANADA'>CANADA</option><option value='CHILE'>CHILE</option><option value='CHINA'>CHINA</option><option value='COLOMBIA'>COLOMBIA</option><option value='CROATIA'>CROATIA</option><option value='CYPRUS'>CYPRUS</option><option value='CZECH'>CZECH</option><option value='DENMARK'>DENMARK</option><option value='ECUADOR'>ECUADOR</option><option value='EGYPT'>EGYPT</option><option value='ELSALVADOR'>ELSALVADOR</option><option value='FINLAND'>FINLAND</option><option value='FRANCE'>FRANCE</option><option value='GERMANY'>GERMANY</option><option value='GREECE'>GREECE</option><option value='GUAM'>GUAM</option><option value='HONGKONG'>HONGKONG</option><option value='HUNGARY'>HUNGARY</option><option value='ICELAND'>ICELAND</option><option value='INDIA'>INDIA</option><option value='INDONESIA'>INDONESIA</option><option value='IRELAND'>IRELAND</option><option value='ISRAEL'>ISRAEL</option><option value='ITALY'>ITALY</option><option value='JAPAN'>JAPAN</option><option value='JORDAN'>JORDAN</option><option value='KAZAKHSTAN'>KAZAKHSTAN</option><option value='KUWAIT'>KUWAIT</option><option value='LATVIA'>LATVIA</option><option value='LEBANON'>LEBANON</option><option value='LUXEMBOURG'>LUXEMBOURG</option><option value='MACAO'>MACAO</option><option value='MALAYSIA'>MALAYSIA</option><option value='MALTA'>MALTA</option><option value='MEXICO'>MEXICO</option><option value='MOROCCO'>MOROCCO</option><option value='NETHERLANDS'>NETHERLANDS</option><option value='NEWZEALAND'>NEWZEALAND</option><option value='NIGERIA'>NIGERIA</option><option value='NORWAY'>NORWAY</option><option value='OMAN'>OMAN</option><option value='PAKISTAN'>PAKISTAN</option><option value='PERU'>PERU</option><option value='PHILIPPINES'>PHILIPPINES</option><option value='POLAND'>POLAND</option><option value='PORTUGAL'>PORTUGAL</option><option value='ROMANIA'>ROMANIA</option><option value='RUSSIA'>RUSSIA</option><option value='SAUDIARABIA'>SAUDIARABIA</option><option value='SINGAPORE'>SINGAPORE</option><option value='SLOVAKIA'>SLOVAKIA</option><option value='SLOVENIA'>SLOVENIA</option><option value='SOUTHAFRICA'>SOUTHAFRICA</option><option value='SOUTHKOREA'>SOUTHKOREA</option><option value='SPAIN'>SPAIN</option><option value='SWEDEN'>SWEDEN</option><option value='SWITZERLAND'>SWITZERLAND</option><option value='SYRIA'>SYRIA</option><option value='TAIWAN'>TAIWAN</option><option value='THAILAND'>THAILAND</option><option value='UAE'>UAE</option><option value='UK'>UK</option><option value='USA'>USA</option><option value='YEMEN'>YEMEN</option></select>
			$select1="<select name='FXOcountry'><option value='FCC'>FCC</option><option value='TBR21'>TBR21</option><option value='1TR110_DE'>1TR110_DE</option><option value='ARGENTINA'>ARGENTINA</option><option value='AUSTRALIA'>AUSTRALIA</option><option value='AUSTRIA'>AUSTRIA</option><option value='BAHRAIN'>BAHRAIN</option><option value='BELGIUM'>BELGIUM</option><option value='BRAZIL'>BRAZIL</option><option value='BULGARIA'>BULGARIA</option><option value='CANADA'>CANADA</option><option value='CHILE'>CHILE</option><option value='CHINA'>CHINA</option><option value='COLOMBIA'>COLOMBIA</option><option value='CROATIA'>CROATIA</option><option value='CYPRUS'>CYPRUS</option><option value='CZECH'>CZECH</option><option value='DENMARK'>DENMARK</option><option value='ECUADOR'>ECUADOR</option><option value='EGYPT'>EGYPT</option><option value='ELSALVADOR'>ELSALVADOR</option><option value='FINLAND'>FINLAND</option><option value='FRANCE'>FRANCE</option><option value='GERMANY'>GERMANY</option><option value='GREECE'>GREECE</option><option value='GUAM'>GUAM</option><option value='HONGKONG'>HONGKONG</option><option value='HUNGARY'>HUNGARY</option><option value='ICELAND'>ICELAND</option><option value='INDIA'>INDIA</option><option value='INDONESIA'>INDONESIA</option><option value='IRELAND'>IRELAND</option><option value='ISRAEL'>ISRAEL</option><option value='ITALY'>ITALY</option><option value='JAPAN'>JAPAN</option><option value='JORDAN'>JORDAN</option><option value='KAZAKHSTAN'>KAZAKHSTAN</option><option value='KUWAIT'>KUWAIT</option><option value='LATVIA'>LATVIA</option><option value='LEBANON'>LEBANON</option><option value='LUXEMBOURG'>LUXEMBOURG</option><option value='MACAO'>MACAO</option><option value='MALAYSIA'>MALAYSIA</option><option value='MALTA'>MALTA</option><option value='MEXICO'>MEXICO</option><option value='MOROCCO'>MOROCCO</option><option value='NETHERLANDS'>NETHERLANDS</option><option value='NEWZEALAND'>NEWZEALAND</option><option value='NIGERIA'>NIGERIA</option><option value='NORWAY'>NORWAY</option><option value='OMAN'>OMAN</option><option value='PAKISTAN'>PAKISTAN</option><option value='PERU'>PERU</option><option value='PHILIPPINES'>PHILIPPINES</option><option value='POLAND'>POLAND</option><option value='PORTUGAL'>PORTUGAL</option><option value='ROMANIA'>ROMANIA</option><option value='RUSSIA'>RUSSIA</option><option value='SAUDIARABIA'>SAUDIARABIA</option><option value='SINGAPORE'>SINGAPORE</option><option value='SLOVAKIA'>SLOVAKIA</option><option value='SLOVENIA'>SLOVENIA</option><option value='SOUTHAFRICA'>SOUTHAFRICA</option><option value='SOUTHKOREA'>SOUTHKOREA</option><option value='SPAIN'>SPAIN</option><option value='SWEDEN'>SWEDEN</option><option value='SWITZERLAND'>SWITZERLAND</option><option value='SYRIA'>SYRIA</option><option value='TAIWAN'>TAIWAN</option><option value='THAILAND'>THAILAND</option><option value='UAE'>UAE</option><option value='UK'>UK</option><option value='USA'>USA</option><option value='YEMEN'>YEMEN</option></select>";
			$ret = "<tr " . $strtool . " style='height:28'><td align='left'>Country specific FXO settings ?</td><td align='left'>" . $select1 . "</td></tr>";
			echo $ret;
		}

		//3. Tonset
		//$tones
		if($types[0]!="GSM"){
			exec('/bin/cat /usr/conf/isgw.tones | /bin/grep "^\[.*\]$" ',$ausgabe);
			$select="<select name='tones'>";
			foreach ($ausgabe as $value){

				if ($tones==""){
					if ($value=="[de]"){
							$select.="<option value='".$value."' selected>".$value."</option>";
					}elseif($value!="[general]"){
							$select.="<option value='".$value."'>".$value."</option>";
					}
				}else{
					if($value==$tones){
						$select.="<option value='".$value."' selected>".$value."</option>";
					}else{
						$select.="<option value='".$value."'>".$value."</option>";
					}
				}
			}
			$select.="</select>";
			$strtool="onmouseover=\"Tip('" . COUNTRY . "')\" onmouseout='UnTip()'";
			$ret = "<tr " . $strtool . " style='height:28'><td align='left'><div nowrap>For wich country should the tonset be configured ?</div></td><td align='left'>" . $select . "</td></tr>";
			echo $ret;
		}
		// 4. CLip No Sreeening

		if ($types[0]=="BRI" || $types[0]=="BRI/FXS" || $types[0]=="PRI") {
			if($cns=="yes"){
				$radio="<input type=radio name='cns' value='yes' checked>yes";
				$radio.="<input type=radio name='cns' value='no' >no";
			}else{
				$radio="<input type=radio name='cns' value='yes' >yes";
				$radio.="<input type=radio name='cns' value='no' checked>no";
			}
			$strtool="onmouseover=\"Tip('" . CNS . "')\" onmouseout='UnTip()'";

			$ret = "<tr " . $strtool . " style='height:28'><td align='left'><div nowrap>Is \"CLIP No Screening\" activated on the lines ?</div></td><td align='left'>" . $radio . "</td></tr>";
			echo $ret;

			// 5. Call Deflection

			if($cd=="yes"){
				$radio="<input type=radio name='cd' value='yes' checked>yes";
				$radio.="<input type=radio name='cd' value='no'>no";
			}else{
				$radio="<input type=radio name='cd' value='yes'>yes";
				$radio.="<input type=radio name='cd' value='no' checked>no";
			}

			$strtool="onmouseover=\"Tip('" . CD . "')\" onmouseout='UnTip()'";
			$ret = "<tr " . $strtool . "><td align='left'><div nowrap>Is \"Call Deflection Partion Rerouting\" activated on the lines ?</div></td><td align='left'>" . $radio . "</td></tr>";
			echo $ret;
		}
		$ret="</table><br/>";
		echo $ret;

		//PSTN Internal
			//Header
		$strtool="onmouseover=\"Tip('" . FXS . "')\" onmouseout='UnTip()'";
		if($types[0]!="FXO" && $types[0]!="GSM"){

			if ($types[0]=="FXS" || $types[1]=="FXS" || $types[0]=="BRI/FXS" || $types[1]=="BRI/FXS" ){

				$ret = "<br/><br/><table class='contenttocc' width='98%'><tr><th style='height:28' colspan='2'><div nowrap>PSTN to FXS Redirection (for example analog Fax devices)</div></th></tr>";
				//if($errip)$bgcolor="style='background-color:#FF0000'";
				$port=1;

				for ($i = 0; $i < 2; $i++)
				{
					if ($types[$i]=="BRI/FXS" || $types[$i]=="FXS"){
						for ($z = 1; $z < $ports[$i]+1; $z++)
						{
							$ret .= "<tr " . $bgcolor . " " . $strtool . "><td align='left' style='width:50%'><div nowrap>Enter MSN/DID to redirect to/from FXS Port'" . $port .  "'</div></td><td style='text-align:center'> <input type=text name='did[]' value='" . $did[$port-1] . "' size='30'>  </td></tr>";
							$port++;
						}
					}
				}
				$ret.="</table><br/>";
				echo $ret;

			}
		} //end FXO
		 //GSM
		if($types[0]!="GSM"){
				if ($types[1]=="GSM"){
					$ret = "<br/><br/><table class='contenttocc' width='98%'><tr><th style='height:28' colspan='2'>GSM Settings</th></tr>";
					//if($errip)$bgcolor="style='background-color:#FF0000'";
					$gsmport=1;


							for ($z = 1; $z < 3; $z++)
							{
								$strtool="onmouseover=\"Tip('" . GSMGENERAL . "')\" onmouseout='UnTip()'";
								$ret .= "<tr " . $bgcolor . " " . $strtool . "><td align='left' style='width:40%'><div nowrap>Enter settings for GSM Port '" . $gsmport .  "'</div></td><td style='text-align:left' ><div nowrap>Sim-PIN:<input type=text name='gsmpin[]' value='" . $gsmpin[$gsmport-1] . "' size='2'>SMSC:<input type=text name='gsmsmsc[]' value='" . $gsmsmsc[$gsmport-1] . "' size='12'>Extension:<input type=text name='gsmext[]' value='" . $gsmext[$gsmport-1] . "' size='12'>  </div></td></tr>";
								$strtool="onmouseover=\"Tip('" . GSMPREFIX . "')\" onmouseout='UnTip()'";
								$ret .= "<tr " . $bgcolor . " " . $strtool . "><td align='left' style='width:40%'><div nowrap>Route following Prefixes directly to GSM Port '" . $gsmport .  "'</div></td><td style='text-align:left'> <div nowrap><input type=text name='gsmprefix[]' value='" . $gsmprefix[$gsmport-1] . "' size='50'></div></td></tr>";
								$gsmport++;
							}



				$ret.="</table><br/>";
				echo $ret;
				}
		}// end GSM
		//3CX Connectivity
		$strtool="onmouseover=\"Tip('" . S3CXIP . "')\" onmouseout='UnTip()'";
		$ret = "<br/><br/><table class='contenttocc' width='98%'><tr><th style='height:28' colspan='2'><div nowrap>3CX options</div></th></tr>";
		if($errip)$bgcolor="style='background-color:#FF0000'";
		$ret .= "<tr " . $bgcolor . " " . $strtool . "><td align='left' style='width:50%'><div nowrap>IP-Address of the 3CX Phone System machine:</div></td><td style='text-align:left'> <input type=text name='ip' value='" . $ipaddress . "' size='30'>  </td></tr>";
		$bgcolor="";
		if($errauth)$bgcolor="style='background-color:#FF0000'";
		$strtool="onmouseover=\"Tip('" . S3CXAUTHID . "')\" onmouseout='UnTip()'";
		$ret .= "<tr " . $bgcolor . " " . $strtool . "><td align='left'><div nowrap>Auth-ID of the port created for gateway (eg 10000):</div></td><td style='text-align:left'> <input type=text name='auth' value='" . $auth . "' size='30'>  </td></tr>";
		$bgcolor="";
		$strtool="onmouseover=\"Tip('" . S3CXAUTHPWD . "')\" onmouseout='UnTip()'";
		$ret .= "<tr " . $strtool . "><td align='left'><div nowrap>AuthPassword of this port:</div></td><td style='text-align:left'> <input type=password name='pwd' value='" . $pwd . "' size='30'>  </td></tr>";

		$ret.="</table><br/>";
		echo $ret;


		//FOOTER

		$onclick="onClick='return confirmSubmit()'";
		$ret = "<br/><br/><table class='contenttocc' width='98%'><tr><th colspan='2'><input type='submit' value='Save' " . $onclick . "></th></tr>";
		$ret.="</table></form><br/>";
		echo $ret;
}//supportedconfig
		echo "<hr size= \"1\" noshade><br/><table align='right'>
			<tr><td></td></tr>
			<tr><td><a href=\"http://www.beronet.com\" target=\"_blank\" onfocus=\"if (this.blur()) { this.blur(); }\">
						<img src=\"../berogui/includes/images/beroLogo.gif\" align=\"right\" border=\"0\">
					</a>
			</td></tr></table>";

		echo "</center></td></tr>
		<tr><td colspan=\"2\" style=\"background-repeat:no-repeat\" background=\"../berogui/includes/images/bg_bottom.png\">
			<img src=\"../berogui/includes/images/trans.gif\" border=\"0\" height=\"39\" width=\"10\">
			<div style=\"color: white;\" align=\"right\">" . $Version . "&nbsp;&nbsp;&nbsp;</div>
		</td></tr></body></html>";



	function createconfigfiles($lif0,$lif1,$prot,$tones,$cns,$cd,$ipaddress,$user,$pwd,$did,$fxodid,$fxocountry,$gsmpin,$gsmsmsc,$gsmext,$gsmprefix){

		$debug=0;

		$strpath="/usr/conf/";
		$db=new beroAri($strpath . "ari.db");
        if(!$error){
			$ssql="delete from isdn";
			$res=$db->delete($ssql);
			$ssql="delete from analog";
			$res=$db->delete($ssql);
			$ssql="delete from sip";
			$res=$db->delete($ssql);
			$ssql="delete from dialplan";
			$res=$db->delete($ssql);
			$ssql="delete from callprogress";
			$res=$db->delete($ssql);
			$ssql="delete from callprogress_entries";
			$res=$db->delete($ssql);
			$ssql="delete from gsm";
			$res=$db->delete($ssql);
		}else{
			echo "dberror";
		}
		$lif=getlif();
		for ($i = 0; $i <= 2 ; $i++)
		{
			if($lif[$i]=="bf4S0"){
				$types[$i]="BRI";
				$ports[$i]="4";
			}elseif($lif[$i]=="bf1E1"){
				$types[$i]="PRI";
				$ports[$i]="1";
			}elseif($lif[$i]=="bf2E1"){
				$types[$i]="PRI";
				$ports[$i]="2";
			}elseif($lif[$i]=="bf2S02FXS"){
				$types[$i]="BRI";
				$ports[$i]="2";
			}elseif($lif[$i]=="bf4FXS"){
				$types[$i]="FXS";
				$ports[$i]="4";
			}elseif($lif[$i]=="bf4FXO"){
				$types[$i]="FXO";
				$ports[$i]="4";
			}elseif($lif[$i]=="bf2GSM"){
				$types[$i]="GSM";
				$ports[$i]="2";
			}elseif($lif[$i]=="unknown"){
				$types[$i]="unknown";
				$ports[$i]="0";
			}
		}
		//print_r($lif0);
		//print_r($lif1);
		//CREATE TABLE isdn ( type VARCHAR(30), lif VARCHAR(1), port INTEGER PRIMARY KEY, gr VARCHAR(60) DEFAULT 'NULL', ptp VARCHAR(1) NOT NULL DEFAULT '1', ntteHW VARCHAR(2) NOT NULL DEFAULT 'te', ntteSW VARCHAR(2) NOT NULL DEFAULT 'te', config VARCHAR(255), lt VARCHAR(1) NOT NULL DEFAULT '1', clock VARCHAR(1) NOT NULL DEFAULT '0', chansel VARCHAR(20) NOT NULL DEFAULT 'standard', tones VARCHAR(20) NOT NULL DEFAULT '[de]', odt INTEGER NOT NULL DEFAULT '3', odt_empty_dad INTEGER NOT NULL DEFAULT '15', master INTEGER NOT NULL DEFAULT '1', overlap_dialing VARCHAR(3) NOT NULL DEFAULT 'no', qsig VARCHAR (3) NOT NULL DEFAULT 'no');
		//"INSERT INTO isdn VALUES('bf4S0',1,6,'NULL',1,'te','te','',1,0,'standard','[de]',3,15,1,'no','no');
		//INSERT INTO isdn VALUES('bf2E1',0,1,'test',1,'te','te','ec=1,ectl=15,dnumplan=0,rnumplan=0,onumplan=0,cpnnumplan=0,unknownprefix=,internationalprefix=00,nationalprefix=0,privateprefix=,localprefix=,bearer_cap=SPEECH,screen=0,cd=0,pres=0',1,1,'standard','[de]',3,15,1,'no','no');
		$id=1;
		$a_pt=1;
		$i_pt=1;
		for ($li = 0; $li <=1 ; $li++)
		{
			if($li==0){
				$selectedports=$lif0;
				$master="1";
				if (($lif[$li]=="bf4S0") || ($lif[$li]=="bf2S02FXS") || ($lif[$li]=="bf4FXO")) {
					//$ssql="update key_val set value='1' where key='portswitch'";
					$db->set('portswitch','1');
					if($debug){
						echo ">>>PORTSWITCH SET 1<br>";
					}
				}
			}else{
				$selectedports=$lif1;
				$master="0";
				if(($lif[$li]!="unknown") && ($lif[$li]!="bf2GSM")){
					//$ssql="update key_val set value='0' where key='portswitch'";
					$db->set('portswitch','0');
					if($debug){
						echo ">>>PORTSWITCH SET 0<br>";
					}
				}
				if(($lif[$li]=="bf2GSM")){
					$master="1";
				}

				//echo $ssql ."<br>";
			}
			if($li==1){
				$ptloopstop=$ports[$li-1]+$ports[$li];
				$ptloopstart=$ports[$li-1]+1;
			}else{
				$ptloopstop=$ports[$li];
				$ptloopstart=1;
			}


			for ($pt = $ptloopstart; $pt <=$ptloopstop ; $pt++)
			{
				//Extern Ports

				if($debug)echo ">>>lif:" . $li . " " . $lif[$li] . "<br>";
				if ($lif[$li]=="bf1E1" || $lif[$li]=="bf2E1" || $lif[$li]=="bf4S0" || $lif[$li]=="bf2S02FXS" ){
					$group=checkgroup($selectedports,$pt);
					$ptp=checkptp($selectedports,$pt,$prot);
					if($clockset!="1"){
						$clock=getclock($selectedports,$pt);
						if($clock=="1")$clockset="1";
					}else{
						$clock="0";
					}
					$proto='te';
					$configstring="ec=1";
					$configstring.=",clir_on_oad=nonum";
					if($cns=="yes") $configstring.=",onumplan=4";
					if($cd=="yes") $configstring.=",cd=1";
					$master=1;
					if($li==1)$master=0;
					$ssql ="Insert into isdn ('type','lif','port','gr','ptp','ntteHW','ntteSW',";
					$ssql.="'config','clock','tones','master','port_type') " ;
					$ssql.="values";
					$ssql.="('" . $lif[$li] . "','" . $li ."','" . $i_pt. "','" . $group ."','" . $ptp ."','" . $proto ."','" . $proto ."',";
					$ssql.="'" . $configstring .  "','" . $clock . "','" . $tones . "','" .$master .  "','" . $types[$li] . "')";
					$res=$db->update($ssql);
					if($debug){
						echo ">>>ISDN<br>";
						echo $ssql ."<br>";
					}
					$i_pt++;

				}elseif($lif[$li]=="bf4FXO"){
					//$gname="'FXO" . $fxoport ."'";
					if($li==1)$master=0;
					$group=checkgroup($selectedports,$pt);
					$configstring="clir_on_oad=nonum";
					$ssql ="Insert into analog ('type','lif','port','gr','config','master','port_type',";
					$ssql.="'overlap_dial_timeout_empty_dad','overlap_dial_timeout','tones','fxo_connect',";
					$ssql.="'fxo_wait_for_oad','fxo_dialtone_passthrough','fxo_call_ending_signal',clip,cnip,overlap_dialing) ";
					$ssql.="values";
					$ssql.="('" . $lif[$li] . "','" . $li ."','" . $a_pt. "','" . $group ."','" . $configstring ."','" . $master . "','FXO',";
					$ssql.="15,3,'" . $tones . "','instant','wait',0,'unobtainable','" . $fxodid[$pt-1] . "','',0)";
					$res=$db->update($ssql);
					if($debug){
						echo ">>>analog_FXO<br>";
						echo $ssql ."<br>";
					}

					//echo "apt " . $a_pt ."\n";
					/*
					if ($group!="NULL"){
						if($fxodid[$pt-1]=="")$fxodid[$pt-1]="\\\\1";
						$configstring="";
						$ssql ="Insert into dialplan ('id','fromDirection','todirection','fromId','toId','dest','newDest',";
						$ssql.="'src','newSrc','config') " ;
						$ssql.="values";
						$ssql.="(" . $id . ",'analog','sip','" . $a_pt . "','p:" . $user ."','(.*)','\\\\1',";
						$ssql.="'(.*)','\\\\1','" . $configstring . "')";
						$res=$db->update($ssql);
						if($debug){
							echo ">>>DIALPLAN FXO<br>";
							echo $ssql ."<br>";
							//echo $pt ."<br>";
							//echo $fxodid[$pt-1] ."<br>";
						}
						$id++;
					}
					*/
					$a_pt++;

				//NEW GSM if bf2gsm is lif1
				}elseif($lif[$li]=="bf2GSM"){
					$gsmport++;
					/*CREATE TABLE gsm ( type VARCHAR(30), lif VARCHAR(1), port INTEGER PRIMARY KEY, g
					r VARCHAR(60) DEFAULT 'NULL', config VARCHAR(255), master INTEGER NOT NULL DEFAU
					LT '1', port_type VARCHAR(4) NOT NULL DEFAULT '', pin INTEGER DEFAULT NULL, smsc
 					VARCHAR(70) NOT NULL DEFAULT '', sms_extension VARCHAR(70) NOT NULL DEFAULT '',
 					extension VARCHAR(70) NOT NULL DEFAULT '');*/

					if ($lif[0]=="bf2GSM")
					{
						$group=checkgroup($selectedports,$pt);
						$ssql="INSERT INTO gsm ('type','lif','port','gr','config','master','port_type','pin','smsc','sms_extension','extension')";
						$ssql.=" VALUES('" . $lif[$li] . "'," . $li ."," . $gsmport. ",'" . $group . "','',1,'GSM','" . $gsmpin[$gsmport-1] . "','" . $gsmsmsc[$gsmport-1] . "','','" . $gsmext[$gsmport-1] . "')";
						$res=$db->update($ssql);
						if($debug){
							echo ">>>GSM>>>>> <br>";
							echo $ssql ."<br>";
						}
					}else{
						if(($gsmprefix[0]==$gsmprefix[1])&&($gsmprefix[0]!="")){
							$gsmgroupname="'GSMGROUP'";
						}else{
							$gsmgroupname="'GSM" . $gsmport . "'";
						}
						$ssql="INSERT INTO gsm ('type','lif','port','gr','config','master','port_type','pin','smsc','sms_extension','extension')";
						$ssql.=" VALUES('" . $lif[$li] . "'," . $li ."," . $gsmport. "," . $gsmgroupname . ",'',1,'GSM','" . $gsmpin[$gsmport-1] . "','" . $gsmsmsc[$gsmport-1] . "','','" . $gsmext[$gsmport-1] . "')";
						$res=$db->update($ssql);
						if($debug){
							echo ">>>GSM>>>>> <br>";
							echo $ssql ."<br>";
						}
					}


				}

				//Intern Ports currently FXS

				if ($lif[$li]=="bf2S02FXS" || $lif[$li]=="bf4FXS"){
					$fxsport++;
					/*analog Ports
					CREATE TABLE analog ( type VARCHAR(30), lif VARCHAR(1), port INTEGER PRIMARY KEY, gr VARCHAR(60) DEFAULT 'NULL', config VARCHAR(255),
					master INTEGER NOT NULL DEFAULT '1', port_type VARCHAR(4) NOT NULL DEFAULT '', overlap_dial_timeout_empty_dad INTEGER NOT NULL DEFAULT '15',
					overlap_dial_timeout INTEGER NOT NULL DEFAULT '3', tones VARCHAR(20) NOT NULL DEFAULT '[de]', fxo_connect VARCHAR(20) NOT NULL DEFAULT 'instant',
					fxo_wait_for_oad VARCHAR(20) NOT NULL DEFAULT 'wait', fxo_dialtone_passthrough INTEGER NOT NULL DEFAULT '0', fxo_call_ending_signal VARCHAR(20) NOT NULL DEFAULT 'unobtainable');
					INSERT INTO analog VALUES('bf2S02FXS',0,1,'NULL','',1,'FXS',15,3,'[de]','instant','wait',0,'unobtainable');
					*/
					if (($lif[0]!="bf4FXO") && ($lif[0]!="bf2GSM")){
						$gname="'FXS" . $fxsport ."'";
					}else{
						$gname="'NULL'";
					}
						$configstring="";
						if($li==1)$master=0;
						$ssql ="Insert into analog ('type','lif','port','gr','config','master','port_type',";
						$ssql.="'overlap_dial_timeout_empty_dad','overlap_dial_timeout','tones','fxo_connect',";
						$ssql.="'fxo_wait_for_oad','fxo_dialtone_passthrough','fxo_call_ending_signal') ";
						$ssql.="values";
						$ssql.="('" . $lif[$li] . "','" . $li ."','" . $a_pt. "'," . $gname . ",'" . $configstring ."','" . $master . "','FXS',";
						$ssql.="15,3,'" . $tones . "','instant','wait',0,'unobtainable')";
						$res=$db->update($ssql);
						if($debug){
							echo ">>>analog_FXS<br>";
							echo $ssql ."<br>";
						}
					//echo "apt " . $a_pt ."\n";

					$a_pt++;

				}

			}

		}//END For LOOP LIF

		if($lif[0]=="bf4FXO")$db->set('country',$fxocountry);


		/*
		SIP Part
		CREATE TABLE sip ( id INTEGER PRIMARY KEY , name VARCHAR(60), server_address VARCHAR(60), user VARCHAR(60), secret VARCHAR(60), type VARCHAR(60) NOT NULL DEFAULT  '', def INTEGER DEFAULT 0, config VARCHAR(255), stunserver VARCHAR(25) NOT NULL DEFAULT '', externip VARCHAR(25) NOT NULL DEFAULT '');
		INSERT INTO sip VALUES(1,'3CX','172.20.22.105','3cxuser','3cxpass','both',0,'ea=1,t38=1,ie_on_sip=0,clir_on_sip=,dtmfmode=rfc2833','','');
		*/

		//'t38=1,ie_on_sip=0,wait_for_cancel=0,clir_on_sip=nonum,dtmfmode=rfc2833,sip_src_setting=oad,sip_from_user_setting=${account_username},sip_from_displayname_setting=${new_source},sip_p_asserted_identity_setting=${oad2},codecs=pcmu:pcma:g729:gsm,ea=1'


		//$configstring="t38=1,ie_on_sip=0,wait_for_cancel=0,clir_on_sip=nonum,dtmfmode=rfc2833,sip_src_setting=oad,sip_from_user_setting=\${account_username},sip_from_displayname_setting=\${new_source},codecs=pcmu:pcma:g729:gsm,ea=1";
		$configstring="dtmfmode=rfc2833,codecs=pcmu:pcma:g729:gsm,from_sip_src_setting=from_display,sip_from_user_setting=\${account_username},sip_from_displayname_setting=\${new_source}";

		$ssql ="Insert into sip ('id','name','server_address','user','secret','type','config','register_option')";
		$ssql.="values";
		$ssql.="('1','" . $user ."','" . $ipaddress. "','" . $user ."','" . $pwd ."','1','" . $configstring ."','validate')";
		$res=$db->update($ssql);
		if($debug){
			echo ">>>SIP<br>";
			echo $ssql ."<br>";

		}

		/*
		$strsql="Insert into dialplan ('id','fromDirection','toDirection','fromId','toId','dest','newDest','src','newSrc','config','isdnid','posid') ";
		$strsql.="values (" . $i . ",";


		DIALPLAN
		create table dialplan ( id INTEGER PRIMARY KEY, direction VARCHAR(15), fromId VARCHAR(60), toId VARCHAR(60), dest VARCHAR(60), newDest VARCHAR(100), src VARCHAR(60), newSrc VARCHAR(100), config VARCHAR(255), isdnid VARCHAR(100) NOT NULL DEFAULT '');
		INSERT INTO dialplan VALUES(1,'isdn-sip','g:Group1','p:3CX1','(.*)','\\1','(.*)','\\1','','');
		INSERT INTO dialplan VALUES(2,'sip-isdn','d:3CX1','g:Group1','(.*)','\\1','(.*)','\\1','','');
		*/
		//analog routing
		$configstring="";
		$counter=1;
		//$id=1;
		//echo $lif[0];
		if($lif[0]=="bf4FXO"){
			$extern="analog";
		}elseif($lif[0]=="bf2S02FXS" || $lif[0]=="bf4S0" || $lif[0]=="bf1E1" || $lif[0]=="bf2E1"){
			$extern="isdn";
		}elseif($lif[0]=="bf2GSM"){
			$extern="gsm";
		}
		if ($extern!="analog")
		{
			if($did!=null){
				foreach ($did as $DID){

					if(!$DID==""){
						$guid="ISDNZUISDN" . "000000000000" . $id;
						$ssql ="Insert into dialplan ('id','fromDirection','todirection','fromId','toId','dest','newDest',";
						$ssql.="'src','newSrc','config','isdnid') " ;
						$ssql.="values";
						$ssql.=" (" . $id . ",'" . $extern . "','analog','g:Group1','g:FXS" . $counter . "','(" . $DID . ")','\\\\1',";
						$ssql.="'(.*)','\\\\1','" . $configstring . "','" . $guid . "')";
						$res=$db->update($ssql);
						if($debug){
							echo ">>>DIALPLAN_ISDN-ISDN<br>";
							echo $ssql ."<br>";
						}
						$id++;
						$guid="ISDNZUISDN" . "000000000000" . $id;
						$ssql ="Insert into dialplan ('id','fromDirection','todirection','fromId','toId','dest','newDest',";
						$ssql.="'src','newSrc','config','isdnid') " ;
						$ssql.="values ";
						$ssql.=" (" . $id . ",'analog','" . $extern . "','g:FXS" . $counter . "','g:Group1','(.*)','\\\\1',";
						$ssql.="'(.*)','" . $DID . "','" . $configstring . "','" . $guid . "')";
						$res=$db->update($ssql);
						if($debug){
							echo $ssql ."<br>";
							echo "<<<DIALPLAN_ISDN-ISDN<br>";
						}
						$id++;
					}
					$counter++;
				}
			}
		}


		//if($lif[0]!="bf4FXO"){
			$configstring="";
			//if($extern=="isdn")$configstring="display_name_setting=2";
			$ssql ="Insert into dialplan ('id','fromDirection','todirection','fromId','toId','dest','newDest',";
			$ssql.="'src','newSrc','config') " ;
			$ssql.="values";
			$ssql.="(" . $id . ",'" . $extern . "','sip','g:Group1','p:" . $user ."','(.*)','\\\\1',";
			$ssql.="'(.*)','\\\\1','" . $configstring . "')";
			$res=$db->update($ssql);
			if($debug){
				echo "<<<DIALPLAN_SIP->ISDN<br>";
				echo $ssql ."<br>";
			}
			$id++;
		//} enc if $lif[0]!

		//if gsm auf LIF1 und prefix

		if(($lif[0]!="bf2GSM")&&($lif[1]=="bf2GSM")){

			$configstring="";

			if(($gsmprefix[0]==$gsmprefix[1])&&($gsmprefix[0]!="")){
				$prefixarray=explode(";",$gsmprefix[0]);
					$ii=1;
					foreach($prefixarray as $prefix){
						if($ii==1){
							$prefixstring= "(" . $prefix .".*" ;
						}else{
							$prefixstring.= "|" . $prefix .".*" ;
						}
						$ii++;
					}
					$prefixstring.=")";
					$ssql ="Insert into dialplan ('id','fromDirection','todirection','fromId','toId','dest','newDest',";
					$ssql.="'src','newSrc','config') " ;
					$ssql.="values";
					$ssql.="(" . $id . ",'sip','GSM','d:" . $user ."','g:GSMGROUP','" . $prefixstring . "','\\\\1',";
					$ssql.="'(.*)','\\\\1','" . $configstring . "')";
					$res=$db->update($ssql);
					if($debug){
						echo ">>>DIALPLAN_GSM->CALL_LCR<br>";
						echo $ssql ."<br>";
					}
					$id++;
					$ssql ="Insert into dialplan ('id','fromDirection','todirection','fromId','toId','dest','newDest',";
					$ssql.="'src','newSrc','config') " ;
					$ssql.="values";
					$ssql.="(" . $id . ",'GSM','SIP','g:GSMGROUP','d:" . $user ."','(.*)','\\\\1',";
					$ssql.="'(.*)','\\\\1','" . $configstring . "')";
					$res=$db->update($ssql);
					if($debug){
						echo ">>>DIALPLAN_GSM->CALL_LCR<br>";
						echo $ssql ."<br>";
					}
					$id++;
			}else{
				for ($i = 1; $i < 3; $i++){
					$prefixarray=explode(";",$gsmprefix[$i-1]);
					$ii=1;
					foreach($prefixarray as $prefix){
						if($ii==1){
							$prefixstring= "(" . $prefix .".*" ;
						}else{
							$prefixstring.= "|" . $prefix .".*" ;
						}
						$ii++;
					}
					$prefixstring.=")";
					if($prefixstring!="(.*)"){
						$ssql ="Insert into dialplan ('id','fromDirection','todirection','fromId','toId','dest','newDest',";
						$ssql.="'src','newSrc','config') " ;
						$ssql.="values";
						$ssql.="(" . $id . ",'sip','GSM','d:" . $user ."','g:GSM" . $i . "','" . $prefixstring . "','\\\\1',";
						$ssql.="'(.*)','\\\\1','" . $configstring . "')";
						$res=$db->update($ssql);
						if($debug){
							echo ">>>DIALPLAN_GSM->CALL_LCR<br>";
							echo $ssql ."<br>";
						}
						$id++;
					}

					$ssql ="Insert into dialplan ('id','fromDirection','todirection','fromId','toId','dest','newDest',";
						$ssql.="'src','newSrc','config') " ;
						$ssql.="values";
						$ssql.="(" . $id . ",'GSM','SIP','g:GSM" . $i . "','d:" . $user ."','(.*)','\\\\1',";
						$ssql.="'(.*)','\\\\1','" . $configstring . "')";
						$res=$db->update($ssql);
						if($debug){
							echo ">>>DIALPLAN_GSM->CALL_LCR<br>";
							echo $ssql ."<br>";
						}
						$id++;
				}

			}
		}

		//Dialplan Outbound Call Forward
		/*if ($extern=="isdn")
		{
			$configstring="wait_for_cancel=0,s2i_cpt=3CXCF_TABLE";
			$ssql ="Insert into dialplan ('id','fromDirection','todirection','fromId','toId','dest','newDest',";
			$ssql.="'src','newSrc','config') " ;
			$ssql.="values";
			$ssql.="(" . $id . ",'sip','" . $extern . "','d:" . $user ."','g:Group1','CF(.*)','\\\\1',";
			$ssql.="'(.*)','\\\\1','" . $configstring . "')";
			$id++;
			$res=$db->update($ssql);
			if($debug){
				echo ">>>DIALPLAN_SIP->CALL_FW<br>";
				echo $ssql ."<br>";
			}
			//Call progress Table;
			$ssql1="INSERT INTO callprogress VALUES(1,'3CXCF_TABLE','SIP-ISDN')";
			$res=$db->update($ssql1);
			$ssql2="INSERT INTO callprogress_entries VALUES(1,'te','none','','PROCEEDING','any_state',1)";
			$res=$db->update($ssql2);
			$ssql3="INSERT INTO callprogress_entries VALUES(2,'te','none','','PROGRESS','any_state',1)";
			$res=$db->update($ssql3);
			$ssql4="INSERT INTO callprogress_entries VALUES(3,'te',180,'','ALERTING','any_state',1)";
			$res=$db->update($ssql4);
			if($debug){
				echo ">>>DIALPLAN_SIP->Analog<br>";
				echo $ssql1 ."<br>";
				echo $ssql2 ."<br>";
				echo $ssql3 ."<br>";
				echo $ssql4 ."<br>";
			}
		}*/

		//Dialplan Outbound Call
		$configstring="";
		$ssql ="Insert into dialplan ('id','fromDirection','todirection','fromId','toId','dest','newDest',";
		$ssql.="'src','newSrc','config') " ;
		$ssql.="values";
		$ssql.="(" . $id . ",'sip','" . $extern . "','d:" . $user ."','g:Group1','(.*)','\\\\1',";
		$ssql.="'(.*)','\\\\1','" . $configstring . "')";
		$res=$db->update($ssql);
		if($debug){
			echo ">>>DIALPLAN_SIP->CALL<br>";
			echo $ssql ."<br>";
		}



        if(!$debug){
			/*$strsql="Update activate set option='0'";
			$res=$db->delete($strsql);
			ob_end_flush();
			echo "<script>window.location.href=\"../berogui/includes/create_files.php\"</script>";
			flush();
			sleep(2);
			*/
			$strsql="Update activate set option='3'";
			$res=$db->delete($strsql);
			echo "<script>window.location.href=\"../berogui/includes/login.php\"</script>";
			//exec('/sbin/reboot');

        }



	}
	function getconfigstring(){

	}
	function getclock($selected,$port){
		if($selected!=Null){
			foreach($selected as $value){
				if($port==$value){
					return "1";
				}
			}
		}
		return "0";
	}

	function checkgroup($selected,$port){
		if($selected!=Null){
			foreach($selected as $value){
				if($port==$value){
					return "Group1";
				}
			}
		}
		return 'NULL';
	}
	function checkptp($selected,$port,$proto){
		if($selected!=Null){
			if($proto=="ptmp"){
				foreach($selected as $value){
					if($port==$value){
						return '0';
					}
				}
				return '1';
			}else{
					return '1';
			}
		}
	}


	function getlif(){
		exec('/bin/cat /proc/li*',$resl);
                $i=0;
                foreach ($resl as $zeile){
                        preg_match("/type: (?<li>.+)/",$zeile,$lif);

                        if (($lif[li]!="unknown") && ($lif[li]!="")){

								if ($lif[li]=="si-4fxo") $type="bf4FXO";
								if ($lif[li]=="si-4fxs") $type="bf4FXS";
								if ($lif[li]=="xhfcSi-2s02fxs") $type="bf2S02FXS";
								if ($lif[li]=="xhfc-4su") $type="bf4S0";
								if ($lif[li]=="hfc-e1") $type="bf1E1";
								if ($lif[li]=="hfc-2e1") $type="bf2E1";
								if ($lif[li]=="telit-1GSM") $type="bf1GSM";
								if ($lif[li]=="telit-2GSM") $type="bf2GSM";
								if ($lif[li]=="falc-2e1/t1") $type="bf2GSM";

                                $result[$i]=$type;
                                //echo "lif:" . $i . ";type:" . $type . "\n<br>" ;
                                $i++;
                        }else{
							$type="unknown";
							$result[$i]=$type;
						}

                }
                return $result;
	}
?>
