<?php
$DEBUG=1;

function error($str, $line)
{
    echo "FAILURE:".$str." in line:".$line."\n";
    exit(-$line);
}

include_once("/usr/local/www/berogui/misc/database.php");
function ini_to_db($value=0){
	global $DEBUG;
	$strpath="/usr/conf/";
	//$strpath="";
	$db=new beroAri($strpath . "ari.db");
        //1.0 & 2.0
        if ($DEBUG) echo "Parsing isgw.conf\n";
	$result=isgwconf_initoDB($db,$strpath,$value);
	if (!$result) return false;
        if ($DEBUG) echo "Parsing hardware.conf\n";
	$result=isdncfg_initoDB($db,$strpath,$value);
	if (!$result) return false;
        if ($DEBUG) echo "Parsing isgw.isdn\n";
	$result=isgwisdn_initoDB($db,$strpath,$value);
        if (!$result) return false;
        if ($DEBUG) echo "Parsing isgw.dialplan\n";
	$result=isgwdialplan_initoDB($db,$strpath,$value);
	if (!$result) return false;
        if ($DEBUG) echo "Parsing isgw.sip\n";
	$result=isgwsip_initoDB($db,$strpath,$value);
        if (!$result) return false;
        //late 1.0 and 2.0
        if ($DEBUG) echo "Parsing misc.conf\n";
	$result=misc_initoDB($db,$strpath,$value);
	if (!$result) return false;
        if ($DEBUG) echo "Parsing isgw.causes\n";
	$result=isgwcauses_initoDB($db,$strpath,$value);
	if (!$result) return false;
        //2.0 only
        if ($DEBUG) echo "Parsing isgw.analog\n";
        $result=isgwanalog_initoDB($db,$strpath,$value);
        if (!$result) return false;
        if ($DEBUG) echo "Parsing isgw.cpt\n";
	$result=isgwcpt_initoDB($db,$strpath,$value);
        if (!$result) return false;
        //2.2 only
        if ($DEBUG) echo "Parsing isgw.gsm\n";
        $result=isgwgsm_initoDB($db,$strpath,$value);
        if (!$result) return false;

	return true;

}
function make_parseable_inifile($sFile,$dFile){

	$handle = @fopen($sFile, "r"); // Open file form read.
	$whandle=fopen($dFile,"w");
	if ($handle) {
		$i=0;
		while (!feof($handle)) // Loop til end of file.
		{
			$buffer = fgets($handle, 4096); // Read a line.
			if(strpos($buffer,"config")!==false){
				$eqpos=strpos($buffer,"=");
				$value=trim(substr($buffer,$eqpos+1));
				$buffer="config=\"" . $value . "\"\n";
				//echo $buffer ."\n";
			}
			if(strpos($buffer,"tones")!==false){
				$buffer=str_replace("[","",$buffer);
				$buffer=str_replace("]","",$buffer);
				//echo $buffer . "\n";

			}
			if(strpos($buffer,"overlap_dialing")!==false){
				$buffer=str_replace("yes","1",$buffer);
				$buffer=str_replace("no","0",$buffer);
				//echo $buffer . "\n";

			}
			if(strpos($buffer,"sip_over_tcp")!==false){
				$buffer=str_replace("yes","1",$buffer);
				$buffer=str_replace("no","0",$buffer);
				//echo $buffer . "\n";

			}
			if(strpos($buffer,"time_from_isdn")!==false){
				$buffer=str_replace("yes","1",$buffer);
				$buffer=str_replace("no","0",$buffer);
				//echo $buffer . "\n";

			}
			if(strpos($buffer,"qsig")!==false){
				$buffer=str_replace("yes","1",$buffer);
				$buffer=str_replace("no","0",$buffer);
				//echo $buffer . "\n";

			}
			if(strpos($buffer,"lan-dhcp")!==false){
				$buffer=str_replace("yes","1",$buffer);
				$buffer=str_replace("no","0",$buffer);
				//echo $buffer . "\n";

			}
			if(strpos($buffer,"use-boot-prov")!==false){
				$buffer=str_replace("yes","1",$buffer);
				$buffer=str_replace("no","0",$buffer);
				//echo $buffer . "\n";

			}
			if(strpos($buffer,"bnfos-hb-on-boot")!==false){
				$buffer=str_replace("yes","1",$buffer);
				$buffer=str_replace("no","0",$buffer);
				//echo $buffer . "\n";

			}

			$wbuffer=fwrite($whandle,$buffer);
			$i++;
			//echo $i . ":"  .$buffer . "\n";
		}
	fclose($handle); // Close the file.
	}
	return;

}

function isgwconf_initoDB($db,$strpath,$value=0){
	global $DEBUG;

	if (!file_exists($strpath . "isgw.conf"))error("no isgw.conf found", __LINE__);

	$result=parse_ini_file($strpath . "isgw.conf",true);

	foreach	($result as $section=>$values){

		if ($section=="general"){


			$statusport=$result[$section][statusport];
			$logserver=$result[$section][logserver];
			$logging_active=$result[$section][logging_active];
			$loglevel=$result[$section][loglevel];


			$syslog_facility=$result[$section][syslog_facility];
			if($syslog_facility=="")$syslog_facility="16";
			if($db->get('syslog_facility')){
				$strsql="update key_val set value='" . $syslog_facility  . "' where key='syslog_facility'";
			}else{
				$strsql="Insert into key_val ('key','value') VALUES ('syslog_facility', $syslog_facility)";
			}
			if($DEBUG) echo "isgwconf->DB: Key_val:" .$strsql ." \n";
			$res=$db->insert_($strsql);
			if (!$res) error("isgconf->DB (syslog_facility)", __LINE__);

			$rtp_traffic=$result[$section][rtp_traffic];
			if($rtp_traffic=="")$rtp_traffic="0";
			if($db->get('rtp_traffic')){
				$strsql="update key_val set value='" . $rtp_traffic  . "' where key='rtp_traffic'";
			}else{
				$strsql="Insert into key_val ('key','value') VALUES ('rtp_traffic', $rtp_traffic)";
			}
			if($DEBUG) echo "isgwconf->DB: Key_val:" .$strsql ." \n";
			$res=$db->insert_($strsql);
			if (!$res) error("isgwconf->DB(rtp_traffic)", __LINE__);
			//pcm_server
			$pcm_server=$result[$section][pcm_server];
			if($pcm_server=="")$pcm_server="0";
			if($db->get('pcm_server')){
				$strsql="update key_val set value='" . $pcm_server  . "' where key='pcm_server'";
			}else{
				$strsql="Insert into key_val ('key','value') VALUES ('pcm_server', '$pcm_server')";
			}
			if($DEBUG) echo "isgwconf->DB: Key_val:" .$strsql ." \n";
			$res=$db->insert_($strsql);
			if (!$res) error("isgwconf->DB(pcm_server)", __LINE__);
			//pcm_server_port
			$pcm_server_port=$result[$section][pcm_server_port];
			if($pcm_server_port=="")$pcm_server_port="54329";
			if($db->get('pcm_server_port')){
				$strsql="update key_val set value='" . $pcm_server_port  . "' where key='pcm_server_port'";
			}else{
				$strsql="Insert into key_val ('key','value') VALUES ('pcm_server_port', $pcm_server_port)";
			}
			if($DEBUG) echo "isgwconf->DB: Key_val:" .$strsql ." \n";
			$res=$db->insert_($strsql);
			if (!$res) error("isgwconf->DB(pcm_server_port)", __LINE__);
			//logcdr
			$logcdr=$result[$section][logcdr];
			if($logcdr=="")$logcdr="0";
			if($db->get('logcdr')){
				$strsql="update key_val set value='" . $logcdr  . "' where key='logcdr'";
			}else{
				$strsql="Insert into key_val ('key','value') VALUES ('logcdr', $logcdr)";
			}
			if($DEBUG) echo "isgwconf->DB: Key_val:" .$strsql ." \n";
			$res=$db->insert_($strsql);
			if (!$res) error("isgwconf->DB(logcdr)", __LINE__);

			//reject_calls_under_load
			$reject_calls_under_load=$result[$section][reject_calls_under_load];
			if ($reject_calls_under_load=="") $reject_calls_under_load=0;
			$db->set("reject_calls_under_load",$reject_calls_under_load);
			if($DEBUG) echo "isgwconf->DB: Key_val:reject_calls_under_load=" .$reject_calls_under_load ." \n";

			//$msp_bridging=$result[$section][msp_bridging];


			$strsql ="Insert into general ('id','logserver','logging_active','msp_device_ip','loglevel') Values";
			$strsql.="(1,";
			$strsql.="'" . $logserver . "',";
			$strsql.="'" . $logging_active . "',";
			$strsql.="'192.168.173.1',";
			$strsql.="'". $loglevel . "')";



			if($DEBUG) echo "isgwconf->DB: " .$strsql ." \n";
			$res=$db->delete("delete from general");
			$res=$db->insert_($strsql);
			if (!$res) error("isgwconf->DB(general)", __LINE__);

		}
	}
	return 1;
}
function isgwdialplan_initoDB($db,$strpath,$value=0){
	global $DEBUG;
	if (!file_exists($strpath . "isgw.dialplan")){
		if ($value==0) error("isgw.dialplan not found", __LINE__);
		else return false;
	}
	$handle = @fopen($strpath ."isgw.dialplan", "r"); // Open file form read.
	$res=$db->delete("delete from dialplan");
	if ($handle) {
		$i=0;
		$posid=0;
		while (!feof($handle)) // Loop til end of file.
		{
			$buffer = fgets($handle, 4096); // Read a line.
			$ss=0;
			if($i>=1){
				$result=split("\t",$buffer);
				$z=0;
				if(count($result)>"1"){
					$isdnid="";
					if(substr($result[4],0,10)=="ISDNZUISDN"){
						$isdnid=substr($result[4],0,23);
						$result[4]=substr($result[4],23);
						$ss=1;
					}
					if(substr($result[5],0,10)=="ISDNZUISDN"){
						$isdnid=substr($result[5],0,23);
						$result[5]=substr($result[5],23);
						$ss=0;
					}
					//echo "configstring " . $result[8] . "\n";
					//echo "configstring " . $result[0] . "\n";
					//echo "configstring " . $result[1] . "\n";

					if($ss==0){
						$posid=$posid+1;
						$strsql="Insert into dialplan ('id','fromDirection','toDirection','fromId','toId','dest','newDest','src','newSrc','config','isdnid','posid') ";
						$strsql.="values (" . $i . ",";
						$strsql.="'" . $result[0] . "',";
						$strsql.="'" . $result[1] . "',";
						$strsql.="'" . $result[2] . "',";
						$strsql.="'" . $result[3] . "',";
						$strsql.="'" . $result[4] . "',";
						$strsql.="'" . $result[5] . "',";
						$strsql.="'" . $result[6] . "',";
						$strsql.="'" . $result[7] . "',";
						$strsql.="'" . $result[8] . "',";
						$strsql.="'" . $isdnid . "',";
						$strsql.="'" . $posid. "')";

						$strsql=str_replace("\\","\\\\",$strsql);
						$strsql=str_replace('""','\"\"',$strsql);



					}else{
						$strsql="update dialplan set 'toID'= '" . $result[3] . "', toDirection='".$result[1]."' where isdnid='" . $isdnid . "'";
					}
					if($DEBUG)echo "isgwdialplan: " .$strsql . "\n";
					$res=$db->insert_($strsql);
					if (!$res) error("isgwdialplan->DB", __LINE__);

				}
			}
			$i++;

		}//end while

	}
	return 1;
}
function isgwcpt_initoDB($db,$strpath,$value=0){
	global $DEBUG;
	if (!file_exists($strpath . "isgw.cpt")){
		if ($value==0) error("isgw.cpt not found", __LINE__);
		else return false;
	}
	$handle = @fopen($strpath ."isgw.cpt", "r"); // Open file form read.
	$res=$db->delete("delete from callprogress");
	$res=$db->delete("delete from callprogress_entries");
	if ($handle) {
		$i=0;
		$j=1;
		while (!feof($handle)) // Loop til end of file.
		{
			$buffer = fgets($handle, 4096); // Read a line.
			$ss=0;
			$result=split("\t",$buffer);
			$z=0;


				if(substr($result[0],0,1)=="["){
					$i++;
					$section=substr($result[0],1,strlen($result[0])-3);
					$res=preg_split("/:/",$section);
					if ($res[0]=="s2i") $cptDir='SIP-ISDN';
					elseif ($res[0]=="i2s") $cptDir='ISDN-SIP';
					$strsql="Insert into callprogress ('id','direction','name') ";
					$strsql.="values (" . $i . ",";
					$strsql.="'" . $cptDir . "',";
					$strsql.="'" . $res[1] . "')";
					if($DEBUG)echo "isgwcpt: " .$strsql . "\n";
					$res=$db->insert_($strsql);
					if (!$res) error("isgw.cpt: tables name insert failed", __LINE__);
				}elseif(count($result)>2){
					if ($cptDir=='SIP-ISDN'){
						$strsql="Insert into callprogress_entries ('id','type','chann_det','isdn_event','sip_resp','resp_text','id_name') ";
						$strsql.="values (" . $j++ . ",";
						$strsql.="'" . $result[0] . "',";
						$strsql.="'" . $result[1] . "',";
						$strsql.="'" . $result[2] . "',";
						$strsql.="'" . $result[3] . "',";
						$strsql.="'" . $result[4] . "',";
						$strsql.="'" . $i . "')";
						if($DEBUG)echo "isgwcpt:" .$strsql . "\n";
					}else if ($cptDir=='ISDN-SIP'){
						$strsql="Insert into callprogress_entries ('id','type','isdn_event','resp_text','sip_resp', 'chann_det','id_name') ";
						$strsql.="values (" . $j++ . ",";
						$strsql.="'" . $result[0] . "',";
						$strsql.="'" . $result[1] . "',";
						$strsql.="'" . $result[2] . "',";
						$strsql.="'" . $result[3] . "',";
						$strsql.="'',";
						$strsql.="'" . $i . "')";
						if($DEBUG)echo "isgwcpt:" .$strsql . "\n";
					}
					$res=$db->insert_($strsql);
					if (!$res) error("isgwcpt: table entries insert failed", __LINE__);
				}




		}//end while

	}
	return 1;
}
function isgwsip_initoDB($db,$strpath,$value=0){
	global $DEBUG;
	if (!file_exists($strpath . "isgw.sip")){
		if ($value==0) error("no isgw.sip found", __LINE__);
		else return false;
	}
	$result=make_parseable_inifile($strpath ."isgw.sip",$strpath ."isgw.sip.tmp");

	$result=parse_ini_file($strpath ."isgw.sip.tmp",true);
	$res=$db->delete("delete from sip_general");
	$res=$db->delete("delete from sip");
	$i=0;
	foreach	($result as $section=>$values){
		if ($section=="general"){
			$sip_address=split(":",$result[$section][sip_address]);
			$bindport=$sip_address[2];
			$udpports=$result[$section][udpports];
			$externip=$result[$section][externip];
			$tos_sip=$result[$section][tos_sip];
			$tos_rtp=$result[$section][tos_rtp];
			$preresolve_addresses=$result[$section][preresolve_addresses];
			$rtp_port_selection=$result[$section][rtp_port_selection];
			$sip_over_tcp=$result[$section][sip_over_tcp];

			if($sip_over_tcp=="")$sip_over_tcp="0";
			if($tos_sip=="")$tos_sip="160";
			if($tos_rtp=="")$tos_rtp="160";
			if($preresolve_addresses=="")$preresolve_addresses="1";
			$db->set("tos_rtp",$tos_rtp);
			$db->set("tos_sip",$tos_sip);
			$db->set("preresolve_addresses",$preresolve_addresses);
			$db->set("rtp_port_selection",$rtp_port_selection);
			$strsql="update key_val set value='" . $sip_over_tcp  . "' where key='sip_over_tcp'";
			if($DEBUG) echo "isgwsip->DB: " . $section . "Key_val:" .$strsql ." \n";
			$res=$db->insert_($strsql);
			if (!$res) error("isgwsip->DB(sip_over_tcp)", __LINE__);

			$strsql ="Insert into sip_general ('id','udpports','bindport','externip') values (";
			$strsql.="1,";
			$strsql.="'" . $udpports . "',";
			$strsql.="'" . $bindport . "',";
			$strsql.="'" . $externip . "')";
			if($DEBUG) echo "isgwsip->DB: " . $section . ":" .$strsql ." \n";
			$res=$db->insert_($strsql);
			if ((strpos($res,"failed")!==false) || !$res)error("isgwsip->DB(udpports,bindport,externip)", __LINE__);

			//if ($res!=str_replace("failed","",$res))error("", __LINE__);
			//if (preg_match("/failed/",$res) || !$res)error("", __LINE__);
		}else{
			$i++;
			$address=$result[$section][address];

			$type=$result[$section][register];

			if($type==""){
				$type=$result[$section][type];
				if($type==""){
					$type=0;
				}else{
					if($type=="both"){
						$type=1;
					}else{
						$type=0;
					}
				}
			}

			$user=$result[$section][user];
			$secret=$result[$section][secret];
			$regrefresh=$result[$section][regrefresh];
			if($regrefresh=="") $regrefresh="60";
			$hard_reregister=$result[$section][hard_reregister];
			if($hard_reregister=="") $hard_reregister="0";
			$externip=$result[$section][externip];
			$config=$result[$section][config];
			$register_option=$result[$section][register_option];
			$default=$result[$section]['default'];
			if ($default!=1 || $default!=0)$default=0;

			$strsql ="Insert into sip ('id','name','server_address',";
			$strsql.="'user','secret','type','config','def','stunserver',";
			$strsql.="'externip','register_option','regrefresh','hard_reregister')";
			$strsql.=" values (";
			$strsql.=$i .",";
			$strsql.="'" . $section . "',";
			$strsql.="'" . $address . "',";
			$strsql.="'" . $user . "',";
			$strsql.="'" . $secret . "',";
			$strsql.="'" . $type . "',";
			$strsql.="'" . $config . "',";
			$strsql.= $default . ",";
			$strsql.="'',";
			$strsql.="'" . $externip  .  "',";
			$strsql.="'" . $register_option  .  "',";
			$strsql.="'" . $regrefresh . "',";
			$strsql.="'" . $hard_reregister . "')";
			if($DEBUG) echo "isgwsip->DB: " . $section . ":" .$strsql ." \n";
			$res=$db->insert_($strsql);
			if ((strpos($res,"failed")!==false) || !$res)error("isgwsip->DB(name,server_address,user,secret,type,config,def,stunserver,externip,register_option,regrefresh)", __LINE__);

		}

	}
	$res=unlink($strpath ."isgw.sip.tmp");
	return 1;
}

function isgwisdn_initoDB($db,$strpath,$value=0){
	global $DEBUG;

	if (!file_exists($strpath . "isgw.isdn"))error("no isgw.isdn found", __LINE__);

	$result=make_parseable_inifile($strpath ."isgw.isdn",$strpath ."isgw.isdn.tmp");
	$result=parse_ini_file($strpath ."isgw.isdn.tmp",true);

	foreach	($result as $section=>$values){
		if($section!="general"){
		$ports=$result[$section][ports];
		$chansel=$result[$section][chansel];
		$chansel_dir=$result[$section][chansel_direction];
		$config= $result[$section][config];
		$tones= $result[$section][tones];
		$portpullup= $result[$section][portpullup];
		$odt=$result[$section][interdigit_timeout];

		if($odt=="")$odt=$result[$section][interdigit_timeout];
		if($odt=="")$odt=3;

		$odial=$result[$section][overlap_dialing];
		$qsig=$result[$section][qsig];
		if($qsig=="")$qsig="0";
		if ($qsig==1)
		{
			$qsig="1";
		}else{
			$qsig="0";
		}
		if($odial=="")$odial="0";
		if ($odial==1)
		{
			$odial="1";
		}else{
			$odial="0";
		}

		$odtwe=$result[$section][interdigit_timeout_initial];
		if($odtwe=="")$odtwe=$result[$section][interdigit_timeout_initial];
		if($odtwe=="")$odtwe=15;

		$port=split(",",$ports);

		foreach ($port as $a){

			$strsql ="Update isdn set ";
			$strsql.="chansel ='". $chansel . "'";
			$strsql.=",chansel_dir ='". $chansel_dir . "'";
			$strsql.=",gr='" . $section . "'";
			$strsql.=",config='" . $config . "'";
			$strsql.=",tones='[" . $tones . "]'";
			$strsql.=",odt=" . $odt;
			$strsql.=",odt_empty_dad=" . $odtwe;
			$strsql.=",portpullup='" . $portpullup . "'";
			$strsql.=",qsig='" . $qsig . "'";
			$strsql.=",overlap_dialing='" . $odial . "'";
			$strsql.=" where port=" .$a;
			if($DEBUG) echo "ISGWISDN->DB: " . $section . ":" .$strsql ." \n";
			$res=$db->update($strsql);
			if (!$res) error("ISGWISDN->DB(chansel,gr,config,tones,odt,odt_empty_dad,portpullup,qsig,overlap_dialing)", __LINE__);
		}
		}else{
			$time_from_isdn=$result[$section][time_from_isdn];
			if($time_from_isdn=="")$time_from_isdn="no";
			$dbroot=new beroAri($strpath . "root.db");
			$strsql="Update root set value='" . $time_from_isdn . "' where key='time_from_isdn'";
			if($DEBUG) echo "ROOTDB:TIME_FROM_ISDN->DB: " .$strsql . "\n";
			$res=$dbroot->update($strsql);

			if (!$res){
				if ($value==0) error("TIME_FROM_ISDN", __LINE__);
				else return false;
			}else{
				if($time_from_isdn!="0"){
					$strsql="Update root set value='' where key='ntphost'";
					if($DEBUG) echo "ROOTDB:TIME_FROM_ISDN->DB: " .$strsql . "\n";
					$res=$dbroot->update($strsql);
					if (!$res)error("TIME_FROM_ISDN(ntphost)", __LINE__);
				}
			}
		}
	}
	$res=unlink($strpath ."isgw.isdn.tmp");
	return 1;
}

function isgwanalog_initoDB($db,$strpath,$value=0){
	global $DEBUG;

	if (!file_exists($strpath . "isgw.analog"))error("no isgw.analog found", __LINE__);

	$result=make_parseable_inifile($strpath ."isgw.analog",$strpath ."isgw.analog.tmp");
	$result=parse_ini_file($strpath ."isgw.analog.tmp",true);

	foreach	($result as $section=>$values){
		if($section!="general"){
			$ports=$result[$section][ports];
			$config= $result[$section][config];
		$overlap_dial_timeout_empty_dad=$result[$section][interdigit_timeout_initial];
		$overlap_dial_timeout= $result[$section][interdigit_timeout];
		$overlap_dialing= $result[$section][overlap_dialing];
		$fxo_connect= $result[$section][fxo_connect];
		$fxo_wait_for_oad=$result[$section][fxo_wait_for_oad];
		$tones=$result[$section][tones];
		$clip=$result[$section][clip];
		$cnip=$result[$section][cnip];
		$fxo_dialtone_passthrough=$result[$section][fxo_dialtone_passthrough];
		$fxo_call_ending_signal=$result[$section][fxo_call_ending_signal];
			$port=split(",",$ports);

			foreach ($port as $a){

				$strsql ="Update analog set ";
				$strsql.="gr='" . $section . "'";
				$strsql.=",overlap_dial_timeout_empty_dad='" . $overlap_dial_timeout_empty_dad . "'";
				$strsql.=",overlap_dial_timeout='" . $overlap_dial_timeout . "'";
				$strsql.=",overlap_dialing='" . $overlap_dialing . "'";
				$strsql.=",fxo_connect='" . $fxo_connect . "'";
				$strsql.=",fxo_wait_for_oad='" . $fxo_wait_for_oad . "'";
				$strsql.=",tones='" . $tones . "'";
				$strsql.=",clip='" . $clip . "'";
				$strsql.=",cnip='" . $cnip . "'";
				$strsql.=",fxo_dialtone_passthrough='" . $fxo_dialtone_passthrough . "'";
				$strsql.=",fxo_call_ending_signal='" . $fxo_call_ending_signal . "'";
				$strsql.=",config='" . $config . "'";
				$strsql.=" where port=" .$a;
				if($DEBUG) echo "ISGWANALOG->DB: " . $section . ":" .$strsql ." \n";
				$res=$db->update($strsql);
				if (!$res) error("ISGWANALOG->DB", __LINE__);
			}
		}
	}
	$res=unlink($strpath ."isgw.analog.tmp");
	return 1;
}
function isgwgsm_initoDB($db,$strpath,$value=0){
	global $DEBUG;

	if (!file_exists($strpath . "isgw.gsm"))error("no isgw.gsm found", __LINE__);

	$result=make_parseable_inifile($strpath ."isgw.gsm",$strpath ."isgw.gsm.tmp");
	$result=parse_ini_file($strpath ."isgw.gsm.tmp",true);

	foreach	($result as $section=>$values){
		if($section!="general"){
			$ports=$result[$section][ports];
			$config= $result[$section][config];
			$sms_extension= $result[$section][sms_extensions];
			$extension= $result[$section][extensions];

			$port=split(",",$ports);
			$i=0;
			foreach ($port as $a){

				$strsql ="Update gsm set ";
				$strsql.="gr='" . $section . "'";
				$strsql.=",config='" . $config . "'";
				$strsql.=",sms_extension='" . $sms_extension . "'";
				$strsql.=",extension='" . $extension . "'";
				$strsql.=" where port=" .$a;
				$i++;
				if($DEBUG) echo "ISGWGSM->DB: " . $section . ":" .$strsql ." \n";
				$res=$db->update($strsql);
				if (!$res) error("ISGWGSM->DB", __LINE__);
			}
		}
	}
	$res=unlink($strpath ."isgw.gsm.tmp");
	return 1;
}

function isgwcauses_initoDB($db,$strpath,$value=0){
	global $DEBUG;

	if (!file_exists($strpath . "isgw.causes"))error("no isgw.causes", __LINE__);

	$causes=@file($strpath ."isgw.causes");
	if ($causes!=NULL){
		$strsql="Delete from isgw_causes";
		if($DEBUG) echo "ISGWCAUSES->DB: " .$strsql ." \n";
		$res=$db->update($strsql);
		foreach ($causes as $key => $line){
			if (substr_count($line,";",0,1)==0 && $line!="" && $line!="\r" && $line!="\n" && $line!="\r\n"){
			$line=str_replace(array("\r\n", "\n", "\r"),"",$line);
			$values=explode("\t",$line);
			$strsql ="INSERT INTO isgw_causes (direction, to_cause, from_cause, sip_response_message)
					VALUES ('".$values[0]."-".$values[1]."','".$values[2]."', '".$values[3]."', '".$values[4]."')";;
			if($DEBUG) echo "ISGWCAUSES->DB: " .$strsql ." \n";
			$res=$db->update($strsql);
			}
		}
	}
/*
	$strsql="Delete from key_val where key='causes_active'";
	if($DEBUG) echo "ISGWCAUSES->DB: " .$strsql ." \n";
	$res=$db->update($strsql);
	$strsql="Insert into key_val ('key','value') Values ('causes_active','" . $causes_active . "')";
	if($DEBUG) echo "ISGWCAUSES->DB: " .$strsql ." \n";
	$res=$db->update($strsql);
	if (!$res) error("", __LINE__);
*/
	return 1;
}

function misc_initoDB($db,$strpath,$value=0){
	global $DEBUG;
	if (!file_exists($strpath . "misc.conf")){
		if($DEBUG) echo "NO MISC.CONF:\n";
		error("no misdn.conf", __LINE__);
	}

	$result=make_parseable_inifile($strpath ."misc.conf",$strpath ."misc.conf.tmp");
	$misc=parse_ini_file($strpath ."misc.conf.tmp",true);

		$dbroot=new beroAri($strpath . "root.db");
	$dbari=new beroAri($strpath . "ari.db");
	foreach	($misc as $section=>$values){
		//Time Settings
		check_and_update_root("ntphost",$misc[$section][ntphost],$dbroot,0);
		check_and_update_root("timezone",$misc[$section][timezone],$dbroot,0);
		check_and_update_root("summer_winter_time",$misc[$section][summer_winter_time],$dbari,0);

		//berofos settings
		check_and_update_root("ip_berofos",$misc[$section]['bnfos-ipaddr'],$dbari,0);
		check_and_update_root("mac_berofos",$misc[$section]['bnfos-mac'],$dbari,0);
		check_and_update_root("interval_berofos",$misc[$section]['bnfos-intv'],$dbari,0);
		check_and_update_root("use_boot_bnfoswdog",$misc[$section]['bnfos-hb-on-boot'],$dbroot,1);

		//LAN Settings
		check_and_update_root("lan-iface",$misc[$section]['lan-iface'],$dbroot,1);
		check_and_update_root("lan-ipaddr",$misc[$section]['lan-ipaddr'],$dbroot,1);
		check_and_update_root("lan-netmask",$misc[$section]['lan-netmask'],$dbroot,1);
		check_and_update_root("lan-gateway",$misc[$section]['lan-gateway'],$dbroot,1);
		check_and_update_root("lan-nameserver",$misc[$section]['lan-nameserver'],$dbroot,1);
		check_and_update_root("lan-dhcp",$misc[$section]['lan-dhcp'],$dbroot,1);
		check_and_update_root("lan-mtu",$misc[$section]['lan-mtu'],$dbroot,1);
		check_and_update_root("routing",$misc[$section]['routing'],$dbroot,0);

		//VLAN settings
		check_and_update_root("vlan-enable",$misc[$section]['vlan-enable'],$dbroot,0);
		check_and_update_root("vlan-id",$misc[$section]['vlan-id'],$dbroot,0);
		check_and_update_root("vlan-prio",$misc[$section]['vlan-prio'],$dbroot,0);

		//GSM SMS
		check_and_update_root("smgw-enabled",$misc[$section]['smgw-enabled'],$dbroot,0);
		check_and_update_root("smtp_host",$misc[$section]['smtp_host'],$dbroot,0);
		check_and_update_root("smtp_port",$misc[$section]['smtp_port'],$dbroot,0);
		check_and_update_root("smtp_user",$misc[$section]['smtp_user'],$dbroot,0);
		check_and_update_root("smtp_pass",$misc[$section]['smtp_pass'],$dbroot,0);
		check_and_update_root("smtp_from",$misc[$section]['smtp_from'],$dbroot,0);
		check_and_update_root("smtp_rcpt",$misc[$section]['smtp_rcpt'],$dbroot,0);
		check_and_update_root("pop3_host",$misc[$section]['pop3_host'],$dbroot,0);
		check_and_update_root("pop3_port",$misc[$section]['pop3_port'],$dbroot,0);
		check_and_update_root("pop3_user",$misc[$section]['pop3_user'],$dbroot,0);
		check_and_update_root("pop3_pass",$misc[$section]['pop3_pass'],$dbroot,0);
		check_and_update_root("smgw_use_ssl",$misc[$section]['smgw_use_ssl'],$dbroot,0);

		//MSP Settings
		check_and_update_root("msp-iface",$misc[$section]['msp-iface'],$dbroot,1);
		check_and_update_root("msp-ipaddr",$misc[$section]['msp-ipaddr'],$dbroot,1);

		//Provisioning
		check_and_update_root("use-boot-prov",$misc[$section]['use-boot-prov'],$dbroot,1);
		check_and_update_root("tftp-host",$misc[$section]['tftp-host'],$dbroot,0);
		check_and_update_root("tftp-url",$misc[$section]['tftp-url'],$dbroot,0);
		check_and_update_root("provisioning-option",$misc[$section]['provisioning-option'],$dbroot,1);
		check_and_update_root("userapp-enabled",$misc[$section]['userapp-enabled'],$dbroot,0);
		check_and_update_root("beroAvahi-enabled",$misc[$section]['beroAvahi-enabled'],$dbroot,0);

		//Logging
		check_and_update_root("system-check-enabled",$misc[$section]['system-check-enabled'],$dbroot,0);

		//Security
		check_and_update_root("safeguard",$misc[$section]['safeguard'],$dbroot,0);
		($misc[$section]['http_acl']=="") ? $http_acl="0.0.0.0/0" : $http_acl=$misc[$section]['http_acl'];
		check_and_update_root("http_acl",$http_acl,$dbari,0);
		($misc[$section]['sip_acl']=="") ? $sip_acl="0.0.0.0/0" : $sip_acl=$misc[$section]['sip_acl'];
		check_and_update_root("sip_acl",$sip_acl,$dbari,0);
		($misc[$section]['rtp_acl']=="") ? $rtp_acl="0.0.0.0/0" : $rtp_acl=$misc[$section]['rtp_acl'];
		check_and_update_root("rtp_acl",$rtp_acl,$dbari,0);
		($misc[$section]['bfdetect_acl']=="") ? $bfdetect_acl="0.0.0.0/0" : $bfdetect_acl=$misc[$section]['bfdetect_acl'];
		check_and_update_root("bfdetect_acl",$bfdetect_acl,$dbari,0);
		($misc[$section]['ssh_acl']=="") ? $ssh_acl="127.0.0.1/32" : $ssh_acl=$misc[$section]['ssh_acl'];
		check_and_update_root("ssh_acl",$ssh_acl,$dbari,0);
		$telnet_acl=$misc[$section]['telnet_acl'];
		if($telnet_acl=="") $telnet_acl="127.0.0.1/32";
		if ($telnet_acl!="") {
				$strsql="update general set telnet_acl='" . $telnet_acl . "'";
				if($DEBUG) echo "MISC.CONF:".$strsql."\n";
				$dbari->update($strsql);
		}

		//user
		$adminpass=$misc[$section]['admin-pass'];
		$adminuser=$misc[$section]['admin-user'];
		if (($adminpass!="") || ($adminuser=="admin")) {
				$strsql="update users set password='" . $adminpass . "' where user='" . $adminuser . "'";
				if($DEBUG) echo "MISC.CONF:".$strsql."\n";
				$dbari->update($strsql);
		}
		//misc
		check_and_update_root("show_app",$misc[$section]['sessions-disable'],$dbari,0);


		//API Password
		$apipass=$misc[$section]['api-pass'];
		if ($apipass!=""){
         @unlink("/usr/conf/apiusers.conf");
         $handle = fopen("/usr/conf/apiusers.conf","w");
         fputs($handle, $apipass . "\n");
         if($DEBUG) echo "MISC.CONF:api-pass=".$apipass."\n";
         fclose($handle);
        }

		//Cloud Settings
		check_and_update_root("cloud_enable",$misc[$section]['cloud_enable'],$dbroot,0);
		check_and_update_root("cloud_key",$misc[$section]['cloud_key'],$dbroot,0);

		//SNMP
		check_and_update_root("snmp-enabled",$misc[$section]['snmp-enabled'],$dbroot,0);

		//check_and_update_root("app-image",$misc[$section]['app-image'],$dbroot,1);
		//check_and_update_root("app-status",$misc[$section]['app-status'],$dbroot,1);
		//check_and_update_root("root-version",$misc[$section]['root-version'],$dbroot,1);

	}
	$res=unlink($strpath ."misc.conf.tmp");
	return 1;

}
function check_and_update_root($key,$value,$db,$check){
	global $DEBUG;


	//Sessions
	if ($key=="show_app"){
		$table="set_app";
		$strsql="Update " . $table . " set '" . $key . "'='" . $value . "'";
		if($DEBUG) echo "MISC.CONF:".$strsql."\n";
		$db->update($strsql);
		return;
	}
	//berofos & summer_wintertime
	if (($key=="ip_berofos") || ($key=="mac_berofos") || ($key=="interval_berofos")  || ($key=="summer_winter_time")
		|| ($key=="http_acl") || ($key=="sip_acl") || ($key=="rtp_acl") || ($key=="bfdetect_acl") || ($key=="ssh_acl")) {
		$table="key_val";

		$strsql="Delete from " . $table . " where key='" . $key . "'";

		$db->update($strsql);
		$strsql="Insert into " . $table . " ('key','value') Values ('" . $key . "','" . $value . "')";
		//$strsql="Update " . $table . " set value='" . $value . "' where key='" . $key . "'";
		if($DEBUG) echo "MISC.CONF:".$strsql."\n";
		$db->update($strsql);
		return;
	}
	//General root DB
	$table="root";

	if($check==1){

		if ($value!=""){

			if(value_exists($db,$key)){
				$strsql="Update " . $table . " set value='" . $value . "' where key='" . $key . "'";
			}else{
				$strsql="Insert into " . $table . " ('key','value') Values ('" . $key . "','" . $value . "')";
			}
			//$db->update($strsql);
			//$strsql="Delete from " . $table . " where key='" . $key . "'";
			//$db->update($strsql);
			//$strsql="Insert into " . $table . " ('key','value') Values ('" . $key . "','" . $value . "')";
			//$strsql="Update " . $table . " set value='" . $value . "' where key='" . $key . "'";
			if($DEBUG) echo "MISC.CONF:".$strsql."\n";
			$db->update($strsql);
		}
	}else{
		if(value_exists($db,$key)){
				$strsql="Update " . $table . " set value='" . $value . "' where key='" . $key . "'";
		}else{
				$strsql="Insert into " . $table . " ('key','value') Values ('" . $key . "','" . $value . "')";
		}
		//$strsql="Delete from " . $table . " where key='" . $key . "'";
		//$db->update($strsql);
		//$strsql="Insert into " . $table . " ('key','value') Values ('" . $key . "','" . $value . "')";
		//$strsql="Update " . $table . " set value='" . $value . "' where key='" . $key . "'";
		if($DEBUG) echo "MISC.CONF:".$strsql."\n";
		$db->update($strsql);
	}
	return;
}

function value_exists($db,$key){

			$sql = "SELECT * FROM root WHERE key = '$key'";
			$res = $db->select($sql);
			if (!$res) {
					return false;
			}
			$row = sqlite_fetch_array($res);
			if (!$row) {
					return false;
			}
			return true;
}


function isdncfg_initoDB($db,$strpath,$value=0){
global $DEBUG;
//echo "exist" . $strpath . "isdn.conf:" . file_exists($strpath . "isdn.conf") ."\n";
//$result=make_parseable_inifile($strpath . "isdn.conf",$strpath ."isdn.conf.tmp")

$result=make_parseable_inifile($strpath . "hardware.conf",$strpath ."hardware.conf.tmp");

$res=$db->delete("delete from isdn");
$res=$db->delete("delete from analog");
$res=$db->delete("delete from gsm");
$cfg = parse_ini_file($strpath ."hardware.conf.tmp",true);

static $lifport_isdn=0;
static $lifport_analog=0;
static $lifport_gsm=0;
foreach	($cfg as $section=>$values){
		unset ($master);
		unset($type);
		unset($country);
		unset($boostringer);
		unset($clock);
		unset($ntteSW);
		unset($termination);
		unset ($ptp);
		unset ($portswitch);
		unset ($pin);
		unset ($smsc);

		switch ($section){

			case "lif0":
				$lifnr=0;
				$loop=0;
				break;
			case "lif1":
				$lifnr=1;
				$loop=0;
				break;
			case "pcm":
				if($DEBUG)echo "Nothing Todo for section: " . $section . "\n";
				$loop=1;
				break;
			default:
				if($DEBUG)echo "Unkown sectioname: " . $section . "\n";
				$loop=1;
				break;
		}


		if($loop)continue;

		$type=$cfg[$section][type];
		if (($type!="bf4S0" && $type!="bf1E1" && $type!="bf2E1" && $type!="bf2S02FXS" && $type!="bf4FXS" && $type!="bf4FXO" && $type!="bf2GSM")){
			if($DEBUG)echo "Unkown module: " . $type . "\n";
			continue;
		}

		$ports=getports($type);

		$master=$cfg[$section][master];
		//echo "master " . $master . "\n";
		$clock=$cfg[$section][clock];
		//echo $clock . "\n";
		if ($master==0)$clock="0";

		$ntteSW=$cfg[$section][ntteSW];
		$ntteSW=split(",",$ntteSW);

		if ($type!="bf4FXS" && $type!="bf4FXO" && $type!="bf2GSM"){
			$ntteSW=getdefaultValue("NTTESW",$ntteSW,$ports);
			$clock=getdefaultValue("CLOCK",$clock,$ports);
		}
		$porttype="";
		if ($type=="bf4S0" || $type=="bf2S02FXS")
		{
			$termination=$cfg[$section][termination];
			$ptp=$cfg[$section][ptp];
			$portswitch=$cfg[$section][portswitch];
			$ntteHW=$cfg[$section][ntteHW];

			$termination=split(",",$termination);
			$ptp=split(",",$ptp);
			$ntteHW=split(",",$ntteHW);
			$termination=getdefaultValue("TERMINATION",$termination,$ports);
			$ptp=getdefaultValue("PTP",$ptp,$ports);
			$portswitch=getdefaultValue("PORTSWITCH",$portswitch,$ports);
			$ntteHW=getdefaultValue("NTTEHW",$ntteHW,$ports);
			$porttype="BRI";
			if ($type=="bf2S02FXS") $boostringer=$cfg[$section][boostringer];


			//echo "1portswitch=" .$portswitch ."\n";
			//echo "type=" .$type . "ports=" . $ports ."\n";
		}else if($type=="bf1E1"){
			$termination=array("1");
			$ptp=array("1");
			$ntteHW=$ntteSW;
			$porttype="PRI";

		}else if($type=="bf2E1"){
			$termination=array("1","1");
			$ptp=array("1","1");
			$ntteHW=$ntteSW;
			$porttype="PRI";
		}else if($type=="bf2GSM"){
			$pin=$cfg[$section][pin];
			$smsc=$cfg[$section][smsc];

			$smsc=explode(",",$smsc);
			$pin=explode(",",$pin);
			$porttype="GSM";
		}else if($type=="bf4FXS"){
			$portswitch=$cfg[$section][portswitch];
			$portswitch=getdefaultValue("PORTSWITCH",$portswitch,$ports);
			$country=$cfg[$section][country];
			$boostringer=$cfg[$section][boostringer];
			$porttype="FXS";
		}else if($type=="bf4FXO"){
			$portswitch=$cfg[$section][portswitch];
			$portswitch=getdefaultValue("PORTSWITCH",$portswitch,$ports);
			$country=$cfg[$section][country];
			$porttype="FXO";
		}


		if 	($type=="bf2GSM"){
			for ($i = 0; $i < 2; $i++)
			{
				$port=$i+1;
				$lifport_gsm++;
				$strsql="INSERT INTO gsm ('type','lif','port','gr','pin','smsc','port_type')";
				$strsql.="VALUES('" . $type . "'," . $lifnr ."," .  $lifport_gsm .",'NULL'";
				$strsql.= ",'" . $pin[$i] . "','" . $smsc[$i] . "','GSM')";

				if($DEBUG)echo "isdncfg->DB: " . $lifport . ":" . $strsql . "\n";
				$res=$db->insert_($strsql);

				if (!$res && !$DEBUG ) error("hardware.conf->DB(bf2GSM, GSM)", __LINE__);

			}
		}elseif ($type=="bf2S02FXS"){
			for ($i = 0; $i < 2; $i++)
			{
				$port=$i+1;
				$lifport_isdn++;
				$strsql="INSERT INTO isdn ('type','lif','port','gr','ptp','ntteHW','ntteSW',";
				$strsql.="'config','lt','clock','chansel','tones','odt','odt_empty_dad','master','port_type')";
				$strsql.="VALUES('" . $type . "'," . $lifnr ."," .  $lifport_isdn .",'NULL'";
				$strsql.= "," . $ptp[$i] . ",'" . $ntteSW[$i] . "','" . $ntteHW[$i] . "',''," . $termination[$i] . "," . $clock[$i];
				$strsql.= ",'standard','[de]',3,15," . $master .",'BRI')";

				if($DEBUG)echo "isdncfg->DB: " . $lifport . ":" . $strsql . "\n";
				$res=$db->insert_($strsql);

				if (!$res && !$DEBUG ) error("hardware.conf->DB(2s02fxs, isdn part)", __LINE__);

			}
			for ($i = 0; $i < 2; $i++)
			{
				$port=$i+1;
				$lifport_analog++;
				$strsql="INSERT INTO analog ('type','lif','port','gr',";
				$strsql.="'config','master','port_type')";
				$strsql.="VALUES('" . $type . "'," . $lifnr ."," .  $lifport_analog .",'NULL'";
				$strsql.= ", '', " . $master .",'FXS')";

				if($DEBUG)echo "isdncfg->DB: " . $lifport . ":" . $strsql . "\n";
				$res=$db->insert_($strsql);

				if (!$res && !$DEBUG ) error("hardware.conf->DB(2s02fxs,analog part)", __LINE__);

			}
		}elseif ($type=="bf4FXS" || $type=="bf4FXO"){
			for ($i = 0; $i < $ports; $i++)
			{
				$port=$i+1;
				$lifport_analog++;
				$strsql="INSERT INTO analog ('type','lif','port','gr',";
				$strsql.="'config','master','port_type')";
				$strsql.="VALUES('" . $type . "'," . $lifnr ."," .  $lifport_analog .",'NULL'";
				$strsql.= ", '', " . $master .",'" . $porttype . "')";

				if($DEBUG)echo "isdncfg->DB: " . $lifport . ":" . $strsql . "\n";
				$res=$db->insert_($strsql);

				if (!$res && !$DEBUG ) error("hardware.conf->DB(4fxs/4fxo)", __LINE__);



			}
                }else{
			for ($i = 0; $i < $ports; $i++)
			{
				$port=$i+1;
				$lifport_isdn++;
				/*
				$strsql="INSERT INTO isdn VALUES('" . $type . "'," . $lifnr ."," .  $lifport .",'NULL'";
				$strsql.= "," . $ptp[$i] . ",'" . $ntteSW[$i] . "','" . $ntteHW[$i] . "',''," . $termination[$i] . "," . $clock[$i];
				$strsql.= ",'standard','[de]',3,15," . $master .")";
				*/
				$strsql="INSERT INTO isdn ('type','lif','port','gr','ptp','ntteHW','ntteSW',";
				$strsql.="'config','lt','clock','chansel','tones','odt','odt_empty_dad','master','port_type')";
				$strsql.="VALUES('" . $type . "'," . $lifnr ."," .  $lifport_isdn .",'NULL'";
				$strsql.= "," . $ptp[$i] . ",'" . $ntteSW[$i] . "','" . $ntteHW[$i] . "',''," . $termination[$i] . "," . $clock[$i];
				$strsql.= ",'standard','[de]',3,15," . $master .",'" . $porttype . "')";

				if($DEBUG)echo "isdncfg->DB: " . $lifport . ":" . $strsql . "\n";
				$res=$db->insert_($strsql);

				if (!$res && !$DEBUG ) error("hardware.conf->DB(isdn)", __LINE__);

			}
		}

		if ($type=="bf4S0" || $type=="bf4FXS" || $type=="bf4FXO" || $type=="bf2S02FXS")
		{
					$strsql="Delete from key_val where key='portswitch'";
					$res=$db->delete($strsql);
					$strsql="Insert into key_val ('key','value') Values ('portswitch','" .$portswitch . "')";
					if($DEBUG)echo "isdncfg->DB: key_val Portswitch:" . $strsql . "\n";
					$res=$db->insert_($strsql);
					if (!$res && !$DEBUG) error("hardware.conf->DB(portswitc)", __LINE__);


		}
		if ($type=="bf4FXS" || $type=="bf4FXO")
		{
					$strsql="Delete from key_val where key='country'";
					$res=$db->delete($strsql);
					$strsql="Insert into key_val ('key','value') Values ('country','" .$country . "')";
					if($DEBUG)echo "isdncfg->DB: key_val Country:" . $strsql . "\n";
					$res=$db->insert_($strsql);
					if (!$res && !$DEBUG) error("hardware.conf->DB(4fxs,4fxo,country)", __LINE__);


		}
		if ($type=="bf4FXS" || $type=="bf2S02FXS")
		{
					$strsql="Delete from key_val where key='boostringer'";
					$res=$db->delete($strsql);
					$strsql="Insert into key_val ('key','value') Values ('boostringer','" .$boostringer . "')";
					if($DEBUG)echo "isdncfg->DB: key_val boostringer:" . $strsql . "\n";
					$res=$db->insert_($strsql);
					if (!$res && !$DEBUG) error("hardware.conf->DB(4fxs,bf2S02FXS,boostringer)", __LINE__);


		}
}//end first for loop

$res=unlink($strpath ."hardware.conf.tmp");
return 1;

}

function getports($stype){
    $stype=trim(strtolower($stype));
    switch ($stype)
    {
    case bf4s0:

    case bf4fxs:
    case bf4fxo:
        return 4;
        break;
    case bf1e1:
        return 1;
        break;
    case bf2e1:
    case bf2gsm:
    case bf2s02fxs:
        return 2;
        break;
    default:
        return -1;
        break;
    }
}

function getdefaultValue($sname,$val,$iports){

	//unset($tmp);

	if($sname=="PORTSWITCH"){
            if (!strlen($val)) return 0;

		$arr=array('0','1');
		if (!in_array($val,$arr,true))error("hardware.conf: portswitch out of range", __LINE__);
		return $val;
	}

	if ($sname=="CLOCK"){
		$arr=array('0','1','2','3','4','5');
		if (!in_array($val,$arr,true))error("hardware.conf: clock out of range", __LINE__);
		if ($val==5){
					for ($i = 1; $i <= $iports; $i++)
					{
						$tmp[$i-1]="5";
					}

		}else{

					for ($i = 1; $i <= $iports ; $i++)
					{


						if ($val==$i){

							$tmp[$i-1]="1";
						}else{

							$tmp[$i-1]="0";
						}
					}
		}
		return $tmp;
	}

	if ($sname=="PTP" || $sname=="TERMINATION"){
		if (count($val)!=$iports || !is_numeric($val[0])){

			/*for ($i = 1; $i <= $iports ; $i++){
				$tmp[$i-1]="1";

			}*/
			error("hardware.conf: PTP/TERMINATION out of range", __LINE__);
			return $tmp;

		}
	}

	if ($sname=="NTTESW" || $sname=="NTTEHW"){
		if (count($val)!=$iports || empty($val[0])){

			/*for ($i = 1; $i <= $iports ; $i++){
					$tmp[$i-1]="te";
			}*/
			error("hardware.conf: NTTESW/NTTEHW out of range", __LINE__);
			return $tmp;
		}
	}


	return $val;


}
?>
