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

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

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

		require_once('/usr/local/php/beroConf.php');
		// build page content
		include('./includes/header.php');
		$gsm_sms_table = $this->dynamic_table();
		$ret .= "
					<form class='extensions_form' name='gsm_gen' id='gsm_gen' action='' method='POST' ENCTYPE=\"multipart/form-data\" onsubmit='return (verify_number(smtp_port.value, \"".GSM_GENERAL_SMTP_PORT_DIS."\") && verify_number(pop3_port.value, \"".GSM_GENERAL_POP3_PORT_DIS."\"));'>
					<input type=hidden name=m value=" . $m . ">
					<input type=hidden name=a value='save'>
					<br>
					" . $gsm_sms_table;

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



	/*
	 * Smtp port wird zurückgegeben, falls in der Datenbank keinen Wert
	 * vorhanden ist, wird 60 als default zurückgegeben
	 */
	function smtp_port(){
		$bc=new beroConf('root');
		if ($bc->get("root", 'smtp_port')!="")	return $bc->get("root", 'smtp_port');
		else return 25;
	}

	/*
	 * POP3 port wird zurückgegeben, falls in der Datenbank keinen Wert
	 * vorhanden ist, wird 60 als default zurückgegeben
	 */
	function pop3_port(){
		$bc=new beroConf('root');
		if ($bc->get("root", 'pop3_port')!="")	return $bc->get("root", 'pop3_port');
		else return 110;
	}

	/*
	 * smgw-enabled wird zurückgegeben, falls in der Datenbank keinen Wert
	 * vorhanden ist, wird 60 als default zurückgegeben
	 */
	function smgw_enabled(){
		$bc=new beroConf('root');
		if ($bc->get("root", 'smgw-enabled')==1)	return "checked";
		else return "";
	}
	/*
	 * smgw_use_ssl wird zurückgegeben
	 */
	function smgw_use_ssl(){
		$bc=new beroConf('root');
		if ($bc->get("root", 'smgw_use_ssl')==1)	return "checked";
		else return "";
	}


	/*
	 * Funktion dynamic_table
	 */

	function dynamic_table() {
		//Die geänderten Werte werden gespeichert
		$bc = new beroConf('root');

		require_once("./includes/lang.php");
		if (isset($_POST['smgw-enabled'])) {

			$bc->set("root", "smgw-enabled",$_POST['smgw-enabled']);
			if ($_POST['smgw-enabled']=="1"){
				$bc->set("root", "smtp_host",$_POST['smtp_host']);
				$bc->set("root", "smtp_port",$_POST['smtp_port']);
				$bc->set("root", "smtp_user",$_POST['smtp_user']);
				$bc->set("root", "smtp_pass",$_POST['smtp_pass']);
				$bc->set("root", "smtp_from",$_POST['smtp_from']);
				$bc->set("root", "smtp_rcpt",$_POST['smtp_rcpt']);

				$bc->set("root", "pop3_host",$_POST['pop3_host']);
				$bc->set("root", "pop3_port",$_POST['pop3_port']);
				$bc->set("root", "pop3_user",$_POST['pop3_user']);
				$bc->set("root", "pop3_pass",$_POST['pop3_pass']);

				$bc->set("root", "smgw_use_ssl",$_POST['smgw_use_ssl']);
			}else{
				$bc->set("root", "smtp_host","");
				$bc->set("root", "smtp_port","25");
				$bc->set("root", "smtp_user","");
				$bc->set("root", "smtp_pass","");
				$bc->set("root", "smtp_from","");
				$bc->set("root", "smtp_rcpt","");

				$bc->set("root", "pop3_host","");
				$bc->set("root", "pop3_port","110");
				$bc->set("root", "pop3_user","");
				$bc->set("root", "pop3_pass","");

				$bc->set("root", "smgw_use_ssl","0");
			}

			$ba=new beroAri();
			$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,'gsmGen','2','".ACTIVATE."');</script>";
				 }
			}


		}
		if (isset($_POST['id_upd'])){
			$id=$_POST['id_upd'];
			for ($i=0;$i<count($id);$i++){
				$id_upd=$id[$i];
				$pin="pin".$id_upd;
				$pin_id=$_POST[$pin];
				$smsc="smsc".$id_upd;
				$smsc_id=$_POST[$smsc];
				$ba=new beroAri();
				$query_upd = $ba->update("UPDATE gsm SET pin='".$pin_id."', smsc='".$smsc_id."' WHERE port='".$id_upd."'");
			}
			$ba=new beroAri();
			$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,'gsmGen','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>";
		$ba=new beroAri();
		$q=$ba->select("select type, lif, master from gsm group by type,lif, master order by lif;");
		while ($ent=$ba->fetch_array($q)){
				$table.="<br/><table class='contenttoc' width='60%'>
					<tr><th colspan='3'><center>".HARDWARE_LINE_INTERFACE_DIS.":&nbsp;".$ent['lif']."
					</center></th></tr>";
		//GSM
			$query=$ba->select("select * from gsm where lif='".$ent['lif']."'");
			while ($entry=$ba->fetch_array($query)){
				$table.="<tr>
				<td><b>".HARDWARE_PORT_DIS.":</b>&nbsp;".$entry['port']."&nbsp;</td>
				<td nowrap style='padding-left:5px; padding-right:5px;'><b>".HARDWARE_PIN_DIS.":</b>&nbsp;<input type='text' name='pin".$entry['port']."' id='pin".$entry['port']."' value='".$entry['pin']."'>
				<td nowrap><b>".HARDWARE_SMSC_DIS.":</b>&nbsp;<input type='text' name='smsc".$entry['port']."' id='smsc".$entry['port']."' value='".$entry['smsc']."'>
				<input type=hidden name=id_upd[] value=".$entry['port'].">
				<script>
				$('#gsm_gen').submit(function() {
					return (check_pin($('#pin".$entry['port']."').val()) && check_smsc($('#smsc".$entry['port']."').val()));
				});
				</script>
				</td>";
			}
			$table.="</table>";
		}

		$table .= "<br><table class='contenttoc' id='table' width='60%'>
					<tr>
					<th colspan='2'>".GSM_GENERAL_TABLE_HEADER_DIS."</th>
					</tr>
					<tr>
					<td><b>".GSM_GENERAL_ENABLE_SMS_DIS.":</b></td>
					<td><input type='hidden' name='smgw-enabled' value='0'><input type='checkbox' name='smgw-enabled' id='smgw-enabled' value='1' ".$this->smgw_enabled()."></td>
					</tr>
					</table>
					<br>
					<table class='contenttoc' id='smtp' width='40%'>
					<tr>
					<th colspan='2'>".GSM_GENERAL_RECEIPTION_DIS."</th>
					</tr>
					<tr>
					<td><b>".GSM_GENERAL_SMTP_HOST_DIS.":</b></td>
					<td><input type='text' name='smtp_host' value='" . $bc->get("root", 'smtp_host') ."' ></td>
                    </tr>
					<tr>
					<td><b>".GSM_GENERAL_SMTP_PORT_DIS.":</b></td>
					<td><input type='text' name='smtp_port' value='" . $this->smtp_port() ."' ></td>
                    </tr>
                    <tr>
					<td><b>".GSM_GENERAL_SMTP_USER_DIS.":</b></td>
					<td><input type='text' name='smtp_user' value='" . $bc->get("root", 'smtp_user') ."' ></td>
                    </tr>
                    <tr>
					<td><b>".GSM_GENERAL_SMTP_SECRET_DIS.":</b></td>
					<td><input type='text' name='smtp_pass' value='" . $bc->get("root", 'smtp_pass') ."' ></td>
                    </tr>
                    <tr>
					<td><b>".GSM_GENERAL_SMTP_FROM_DIS.":</b></td>
					<td><input type='text' name='smtp_from' value='" . $bc->get("root", 'smtp_from') ."' ></td>
                    </tr>
                    <tr>
					<td><b>".GSM_GENERAL_SMTP_RCPT_DIS.":</b></td>
					<td><input type='text' name='smtp_rcpt' value='" . $bc->get("root", 'smtp_rcpt') ."' ></td>
                    </tr>
                    </table>
                    <br>
					<table class='contenttoc' id='pop3' width='40%'>
					<tr>
					<th colspan='2'>".GSM_GENERAL_TRANSMISSION_DIS."</th>
					</tr>
					<tr>
					<td><b>".GSM_GENERAL_POP3_HOST_DIS.":</b></td>
					<td><input type='text' name='pop3_host' value='" . $bc->get("root", 'pop3_host') ."' ></td>
                    </tr>
					<tr>
					<td><b>".GSM_GENERAL_POP3_PORT_DIS.":</b></td>
					<td><input type='text' name='pop3_port' value='" . $this->pop3_port() ."' ></td>
                    </tr>
                    <tr>
					<td><b>".GSM_GENERAL_POP3_USER_DIS.":</b></td>
					<td><input type='text' name='pop3_user' value='" . $bc->get("root", 'pop3_user') ."' ></td>
                    </tr>
                    <tr>
					<td><b>".GSM_GENERAL_POP3_SECRET_DIS.":</b></td>
					<td><input type='text' name='pop3_pass' value='" . $bc->get("root", 'pop3_pass') ."' ></td>
                    </tr>
                    </table>

                    <br>
					<table class='contenttoc' id='ssl_sec' width='40%'>
					<tr>
					<th colspan='2'>".GSM_GENERAL_SECURITY_DIS."</th>
					</tr>
					<tr>
					<td><b>".GSM_GENERAL_USE_SSL_DIS.":</b></td>
					<td><input type='hidden' name='smgw_use_ssl' value='0'><input type='checkbox' name='smgw_use_ssl' id='smgw_use_ssl' value='1' ".$this->smgw_use_ssl()."></td>
                    </tr>
                    </table>

					<input name='submit' type='submit' value='".BUTTON_SAVE."' >
					</form>
					<script type='text/javascript'>
					handler=function() {
						if ($('#smgw-enabled').attr('checked')) {
							$('#smtp').css('display','');
							$('#pop3').css('display','');
							$('#ssl_sec').css('display','');
						}else{
							$('#smtp').css('display','none');
							$('#pop3').css('display','none');
							$('#ssl_sec').css('display','none');
						}
					};
					$('#smgw-enabled').change(handler);
					$(document).ready(handler);
					</script>";
		//Rückgabe: Erzeugte Tabelle mit den änderbaren Feldern von den Einstellungen von isgw.conf
		return $table;
	}

}
?>
