<?php
/**
 * Klasse Sip_general
 * Zeigt allgemeine sip Einstellungen
 */

class Sip_general {
	/*
	 * Funktion display erzeugt den Inhalt von der HTML-Seite
	 */
	function display($args) {

		// args
		$m = $_GET['m'];


		// build page content
		include('./includes/header.php');
		$sip_general_table = $this->dynamic_table();
		$ret .= "
					<form class='extensions_form' name='sip_general' action='' method='POST' ENCTYPE=\"multipart/form-data\" onsubmit='return (sip_general_val(bindport.value,udpports.value) && verify_number(bindport.value, \"".SIP_GENERAL_BIND_PORT_DIS."\"));'>
					<input type=hidden name=m value=" . $m . ">
					<input type=hidden name=a value='save'>
					<br>
					" . $sip_general_table;

		//Rückgabe: Inhalt der HTML-Seite
		return $ret;
	}



	/*
	 * Der Bind Port, der in der Datenbank gespeichert ist wird zurückgegeben
	 * und nachher angezeigt.
	 */
	function bind_port() {
		$ba = new beroAri();
		$query = $ba->select("select bindport from sip_general");
		while ($entry = $ba->fetch_array($query)) {
			$ret = $entry['bindport'];
			return $ret;
		}
	}

	/*
	 * Die Udpports, die in der Datenbank gespeichert sind werden zurückgegeben
	 * und nachher angezeigt.
	 */

	function udpports() {
		$ba = new beroAri();
		$query = $ba->select("select udpports from sip_general");
		while ($entry = $ba->fetch_array($query)) {
			return $entry['udpports'];
		}
	}

	/*
	 * Die externe IP Adresse, die in die Datenbank gespeichert ist, wird zurückgegeben
	 * und anschließend angezeigt
	 */
	function externip() {
		$ba = new beroAri();
		$query = $ba->select("select externip from sip_general");
		while ($entry = $ba->fetch_array($query)) {
			return $entry['externip'];
		}
	}


	/*
	 * Regrefresh wird zurückgegeben, falls in der Datenbank keinen Wert
	 * vorhanden ist, wird 60 als default zurückgegeben
	 */
	function regrefresh(){
		$ba=new beroAri();
		if ($ba->get('regrefresh')!="")	return $ba->get('regrefresh');
		else return 60;
	}

	/*
	 * tos_sip (IP type of service for SIP traffic) wird zurückgegeben, falls in der Datenbank keinen Wert
	 * vorhanden ist, wird 160 als default zurückgegeben
	 */
	function tos_sip(){
		$ba=new beroAri();
		if ($ba->get('tos_sip')!="")	return $ba->get('tos_sip');
		else return 160;
	}

	/*
	 * tos_rtp (IP type of service for RTP traffic) wird zurückgegeben, falls in der Datenbank keinen Wert
	 * vorhanden ist, wird 160 als default zurückgegeben
	 */
	function tos_rtp(){
		$ba=new beroAri();
		if ($ba->get('tos_rtp')!="")	return $ba->get('tos_rtp');
		else return 160;
	}

	/*
	 * SSL_VERSION select box wird, mit den in der Datenbank gespeicherten Daten, erzeugt
	 * und angezeigt.
	 */
	function ssl_version(){
		$ba = new beroAri();
		$option=$ba->get("ssl_version");
		$sel="<select name='ssl_version'>";
		if ($option=="sslv2") $sel.="<option value='sslv2' selected>sslv2</option>";
		else $sel.="<option value='sslv2'>sslv2</option>";
		if ($option=="tls") $sel.="<option value='tls' selected>tls</option>";
		else $sel.="<option value='tls'>tls</option>";
		$sel.="</select>";
		return $sel;
	}

	/*
	 * preresolve_addresses checkbox wird, mit den in der Datenbank gespeicherten Daten erzeugt
	 */
	function preresolve_addresses(){
		$ba = new beroAri();
		$option=$ba->get("preresolve_addresses");
		if ($option=="1") return "<input type='hidden' name='preresolve_addresses' value='0'><input type='checkbox' name='preresolve_addresses' value='1' checked>";
		else return "<input type='hidden' name='preresolve_addresses' value='0'><input type='checkbox' name='preresolve_addresses' value='1'>";
	}

	/*
	 * reject_calls_under_load checkbox wird, mit den in der Datenbank gespeicherten Daten erzeugt
	 */
	function reject_calls_under_load(){
		$ba = new beroAri();
		$option=$ba->get("reject_calls_under_load");
		if ($option=="1") return "<input type='hidden' name='reject_calls_under_load' value='0'><input type='checkbox' name='reject_calls_under_load' value='1' checked>";
		else return "<input type='hidden' name='reject_calls_under_load' value='0'><input type='checkbox' name='reject_calls_under_load' value='1'>";
	}

	/*
	 * sip_transport select box wird, mit den in der Datenbank gespeicherten Daten, erzeugt
	 * und angezeigt.
	 */
	function sip_transport(){
		$ba = new beroAri();
		$option=$ba->get("sip_transport");
		$sel="<select name='sip_transport' id='sip_transport' onChange='javascript:tls(sip_transport.selectedIndex);'>";
		if ($option=="udp") $sel.="<option value='udp' selected>udp</option>";
		else $sel.="<option value='udp'>udp</option>";
		if ($option=="tcp") $sel.="<option value='tcp' selected>tcp</option>";
		else $sel.="<option value='tcp'>tcp</option>";
		if ($option=="tls") $sel.="<option value='tls' selected>tls</option>";
		else $sel.="<option value='tls'>tls</option>";
		$sel.="</select>";
		return $sel;
	}

	/*
	 *rtp_port_selection select box wird, mit den in der Datenbank gespeicherten Daten, erzeugt
	 * und angezeigt.
	 */
	function rtp_port_selection(){
		$ba = new beroAri();
		$option=$ba->get("rtp_port_selection");
		$options = array ("standard", "roundrobin");
		$ret='<select name="rtp_port_selection" id="rtp_port_selection">';
		for ($i=0;$i<count($options);$i++){
			if ($option==$options[$i]) $ret.='<option value="'.$options[$i].'" selected>'.$options[$i].'</option>';
			else $ret.='<option value="'.$options[$i].'">'.$options[$i].'</option>';
		}
		$ret .='</select>';

		return $ret;
	}


	/*
	 * Funktion dynamic_table
	 */

	function dynamic_table() {
		//Die geänderten Werte werden gespeichert
		$ba = new beroAri();

		require_once("./includes/lang.php");
		if (isset($_POST['upload'])){
			$file_name= $_FILES["uploadfile"]["name"];
			$tmp_file= $_FILES["uploadfile"]["tmp_name"];
			if($_FILES["uploadfile"]["size"]>4000) //4 kB
			{
				$table.="<font color='red'>".SIP_GENERAL_FILE_SIZE_DIS."</font>";
			}elseif($_FILES["uploadfile"]["name"]==""){
				$table.="<font color='red'>".SIP_GENERAL_FILE_BLANK_DIS."</font>";
			}else{
				exec("/bin/cp ".$tmp_file." /usr/conf/cafile.pem");
				$table.="<font color='red'>".SIP_GENERAL_UPLOAD_SUCCESS_DIS."</font>";
				$query=$ba->select("select option from activate");
				while($entry = $ba->fetch_array($query)){
					if ($entry['option'] <= 2){
						$query =  $ba->update("UPDATE activate SET option=2");
						$table.= "<script>aktivieren(1,'sipg','2','".ACTIVATE."');</script>";
					}
				}
			}
		}elseif (isset($_POST['bindport']) && isset($_POST['udpports']) && isset($_POST['externip'])) {

			if (isset($_POST['rtp_traffic'])) $ba->set("rtp_traffic","1");
			else if (!isset($_POST['rtp_traffic'])) $ba->set("rtp_traffic","0");
			if (isset($_POST['sip_transport'])) $ba->set("sip_transport",$_POST['sip_transport']);
			if (isset($_POST['ssl_version'])) $ba->set("ssl_version",$_POST['ssl_version']);
			if (isset($_POST['tos_rtp'])) $ba->set("tos_rtp",$_POST['tos_rtp']);
			if (isset($_POST['tos_sip'])) $ba->set("tos_sip",$_POST['tos_sip']);
			if (isset($_POST['rtp_port_selection'])) $ba->set("rtp_port_selection",$_POST['rtp_port_selection']);
			if (isset($_POST['preresolve_addresses'])) $ba->set("preresolve_addresses",$_POST['preresolve_addresses']);
			if (isset($_POST['reject_calls_under_load'])) $ba->set("reject_calls_under_load",$_POST['reject_calls_under_load']);
			$query = $ba->update("update sip_general set udpports='" . $_POST['udpports'] . "', bindport='" . $_POST['bindport'] . "', externip='" . $_POST['externip'] . "'");
			$query=$ba->select("select option from activate");
			while($entry = $ba->fetch_array($query)){
				if ($entry['option'] <= 2){
					 $query =  $ba->update("UPDATE activate SET option=2");
					 $table.= "<script>aktivieren(1,'sipg','2','".ACTIVATE."');</script>";
				 }
			}


		}
		$table.="<script>
			function tls(sel){
				if (sel!=2){
					document.getElementById('ssl_version').style.display='none';
					document.getElementById('upload').style.display='none';
				}else{
					document.getElementById('ssl_version').style.display='';
					document.getElementById('upload').style.display='';
				}

			}
		</script>";
		$table .= "<table class='contenttoc' id='table'>
					<tr>
					<th colspan='2'>".SIP_GENERAL_TABLE_HEADER_DIS."</th>
					</tr>
					<tr onmouseover=\"Tip('" . SIP_GENERAL_BINDPORT . "')\" onmouseout='UnTip()'>
					<td><b>".SIP_GENERAL_BIND_PORT_DIS.":</b></td>
					<td><input type='text' name='bindport' value='" . $this->bind_port() ."' ></td>
					</tr>
					<tr onmouseover=\"Tip('" . SIP_GENERAL_UDPPORTS . "')\" onmouseout='UnTip()'>
					<td><b>".SIP_GENERAL_RTP_PORT_RANGE_DIS.":</b></td>
					<td><input type='text' name='udpports' value='" . $this->udpports() ."' ></td>
                    </tr>
                    <tr onmouseover=\"Tip('" . SIP_GENERAL_EXTERNIP . "')\" onmouseout='UnTip()'>
                    <td><b>".SIP_GENERAL_EXTERN_IP_DIS.":</b></td>
					<td><input type='text' name='externip' value='" . $this->externip() ."' ></td>
					</tr>
                    <tr onmouseover=\"Tip('" . SIP_GENERAL_TOS_RTP . "')\" onmouseout='UnTip()'>
                    <td><b>".SIP_GENERAL_TOS_RTP_DIS.":</b></td>
					<td><input type='text' name='tos_rtp' value='" . $this->tos_rtp() ."' ></td>
					</tr>
                    <tr onmouseover=\"Tip('" . SIP_GENERAL_TOS_SIP . "')\" onmouseout='UnTip()'>
                    <td><b>".SIP_GENERAL_TOS_SIP_DIS.":</b></td>
					<td><input type='text' name='tos_sip' value='" . $this->tos_sip() ."' ></td>
					</tr>
                    <tr>
                    <td><b>".SIP_GENERAL_PRERESOLVE_ADDRESSES_DIS.":</b></td>
					<td>". $this->preresolve_addresses() ."</td>
					</tr>
                    <tr>
                    <td><b>".SIP_GENERAL_REJECT_CALLS_UNDER_LOAD_DIS.":</b></td>
					<td>". $this->reject_calls_under_load() ."</td>
					</tr>
					<tr>
					<td><b>".SIP_GENERAL_RTP_PORT_SELECTION_DIS.":</b></td>
					<td>".$this->rtp_port_selection()."</td>
					</tr>
					<tr>
					<td><b>".SIP_GENERAL_SIP_TRANSPORT_DIS.":</b></td>
					<td>".$this->sip_transport()."</td>
					</tr>


					<tr id='ssl_version'>
					<td><b>".SIP_GENERAL_SSL_VERSION_DIS.":</b></td>
					<td>".$this->ssl_version()."</td>
					</tr>";
					if (file_exists("/usr/conf/cafile.pem")){
			$table .= "<tr>
					<td><b>".SIP_GENERAL_UPLOAD_CERTIFICATE_DIS.":</b></td>
					<td><input type='button' value='".SIP_GENERAL_DOWNLOAD_CERTIFICATE_DIS."' onclick=\" window.location.href='misc/files_download.php?option=certificate';\">&nbsp;&nbsp;
					<input type='button' value='".SIP_GENERAL_DELETE_CERTIFICATE_DIS."' onclick=\"javascript:if (confirm('".SIP_GENERAL_DELETE_CERTIFICATE_MESS_DIS."?')==true) window.location.href='misc/deletedatei.php?option=certificate';\"></td>
					</tr>";
				}

			$table .= "<tr id='upload' onmouseover=\"Tip('" . UPLOAD_CERTIFICATE . "')\" onmouseout='UnTip()'>
					<td><b>".SIP_GENERAL_UPLOAD_CERTIFICATE_DIS.":</b></td>
					<td nowrap><input type='file' size='28' name='uploadfile'><input type='submit' name='upload' value='".BUTTON_UPLOAD."'></td>
					</tr>";
                                        if ($ba->get("rtp_traffic")=="1")
                                            $table.='<tr><td><b>'.SIP_GENERAL_RTP_TRAFFIC_DIS.'</b></td><td><input type="checkbox" name="rtp_traffic" id="rtp_traffic" value="1" checked></td></tr>';
                                        else
                                            $table.='<tr><td><b>'.SIP_GENERAL_RTP_TRAFFIC_DIS.'</b></td><td><input type="checkbox" name="rtp_traffic" id="rtp_traffic" value="1"></td></tr>';
                                        $table.="</table>
					<br>
					<input name='submit' type='submit' value='".BUTTON_SAVE."' onmouseover=\"Tip('" . SIP_GENERAL_SAVE . "')\" onmouseout='UnTip()'>
					</form>
					<script type='text/javascript'>tls(document.getElementById('sip_transport').selectedIndex);</script>";
		//Rückgabe: Erzeugte Tabelle mit den änderbaren Feldern von den Einstellungen von isgw.conf
		return $table;
	}

}
?>
