<?php

/**
 * Klasse Sec (Security)
 * Gibt dem Benutzer die möglichkeit das Kennwort und den Benutzernamen zu ändern
 */


class Sec {

	/*
	 * Funktion action
	 * wird aufgerufen wenn der Benutzer auf dem Knopf Save clickt
	 */
	function action($args) {


		// args

	}

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

		// args
		$m = $_GET['m'];
		require_once ('./includes/isgwtelnet.php');

		include('./includes/header.php');

		$change_pwd_table = $this->dynamic_table();

		// build page content
		$ret .= "
			<form class='beroari_form' name='change_pwd' action='' method='POST'
			onsubmit='return (verify_number(telnet_range.value, \"".SECURITY_TELNET_ACL_RANGE_DIS."\") &&
					verify_range(telnet_range.value, \"".SECURITY_TELNET_ACL_RANGE_DIS."\") &&
					verifyIP(telnet_ip.value,\"Telnet IP\") &&
					verify_number(http_range.value, \"".SECURITY_HTTP_ACL_RANGE_DIS."\") &&
					verify_range(http_range.value, \"".SECURITY_HTTP_ACL_RANGE_DIS."\") &&
					verifyIP(http_ip.value,\"HTTP IP\") &&
					verify_number(sip_range.value, \"".SECURITY_SIP_ACL_RANGE_DIS."\") &&
					verify_range(sip_range.value, \"".SECURITY_SIP_ACL_RANGE_DIS."\") &&
					verifyIP(sip_ip.value,\"SIP IP\") &&
					verify_number(bfdetect_range.value, \"".SECURITY_BFDETECT_ACL_RANGE_DIS."\") &&
					verify_range(bfdetect_range.value, \"".SECURITY_BFDETECT_ACL_RANGE_DIS."\") &&
					verifyIP(bfdetect_ip.value,\"bfdetect IP\") &&
					verify_number(ssh_range.value, \"".SECURITY_SSH_ACL_RANGE_DIS."\") &&
					verify_range(ssh_range.value, \"".SECURITY_SSH_ACL_RANGE_DIS."\") &&
					verifyIP(ssh_ip.value,\"SSH IP\") &&
					specChar(newp.value,\"The new password\") &&
					testLength(newp.value,20,\"The new password\"));'>
			<input type=hidden name=m value=" . $m . ">
			<input type=hidden name=f value='action'>
			<input type=hidden name=a value='save'>
			<br>
			" . $change_pwd_table;

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

	function telnet_acl(){
		$ba = new beroAri();
		$query = $ba->select("select telnet_acl from general");
		$telnet_acl=sqlite_fetch_single($query);
		$data=split("/",$telnet_acl);
		return "<input type='text' name='telnet_ip' value='".$data[0]."' size='15'> /
				<input type='text' name='telnet_range' value='".$data[1]."' size='2'>";
	}

	function get_acl($name){
		$ba = new beroAri();
		$acl = $ba->get($name."_acl");
		$data=split("/",$acl);
		return "<input type='text' name='".$name."_ip' value='".$data[0]."' size='15'> /
				<input type='text' name='".$name."_range' value='".$data[1]."' size='2'>";
	}


	function show_app() {
		$ba = new beroAri();
		$query = $ba->select("select show_app from set_app");
		while ($entry = $ba->fetch_array($query)) {
			if ($entry['show_app'] == "1") {
				return "<input type='checkbox' name='show_app' value='1' onmouseover=\"Tip('" . GENERAL_ENABLE_API . "')\" onmouseout='UnTip()' checked>";
			} else {
				return "<input type='checkbox' name='show_app' value='1' onmouseover=\"Tip('" . GENERAL_ENABLE_API . "')\" onmouseout='UnTip()'>";
			}
		}
	}
	function safeguard() {
		$bc = new beroConf('root');
		$safeguard=$bc->get("root", "safeguard");
		$res="<input type='hidden' name='safeguard' value='0'>";
		if ($safeguard == "1") {
			$res.= "<input <input type='checkbox' name='safeguard' value='1'  checked>";
		} else {
			$res.= "<input type='checkbox' name='safeguard' value='1'>";
		}
		return $res;
	}

	/*
	 * Funktion dynamic_table
	 */
	function dynamic_table()
	{
		include '/usr/local/php/beroConf.php';
		$a = $_POST['a'];
		$ba = new beroAri();
		$bc = new beroConf('root');

		require_once("./includes/lang.php");

		if ($_POST['submit']=="Restore admin password and username"){
			$ba->update("UPDATE users SET password='d73b128b9c86524c362ab4420c987827:f2', user='admin' WHERE id='1'");
		}elseif ($a=='save'){

			$query=$ba->select("SELECT password from users");
			$old_db=$ba->fetch_array($query);
			$old=$_POST['old'];
			$new=$_POST['newp'];
			$confirm=$_POST['confirm_new'];
			if ($old!="" && $new!="" && $confirm!=""){
				//Das alte Kennwort wird mit dem in der Datenbank gespeichertes Kennwort validiert
				if (tep_validate_password($old,$old_db['password'])){
					//Wenn das Neue Kennwort gleich ist mit dem bestätigten Kennwort,
					//wird das neue Kennwort verschlüsselt und in der Datenbank für den
					//aktuellen Benutzer aktualisiert
					if ($new==$confirm && $username!="beroAdmin"){
						if ($_POST['samePwd']==1){
							@unlink("/usr/conf/apiusers.conf");
							$handle = fopen("/usr/conf/apiusers.conf","w");
							fputs($handle, "admin:".$new);
							fclose($handle);
						}
						$pass=tep_encrypt_password($new);
						$query=$ba->update("UPDATE users SET password='".$pass."' where user='".$_SESSION['beroari_username']."'");
						$table.= "<script>alert('".SECURITY_SUCCES_MESSAGE_DIS."');</script>";
					//Wenn die beiden eingegebenen Kennworten nicht übereinstimmen,
					//wird eine Fehlermeldung angezeigt
					}else{
						$table.= "<script>alert('".SECURITY_CONFIRM_ERROR_DIS."');</script>";
					}
				//Wenn das alte Kennwort falsch ist, wird eine Fehlermeldung angezeigt
				}else{
					$table.= "<script>alert('".SECURITY_OLD_PASS_ERROR_DIS."');</script>";
				}
			}
			$bc->set("root", "safeguard", $_POST['safeguard']);
			if ($_POST['show_app'] == "1") {
				$query = $ba->update("update set_app set show_app='" . $_POST['show_app'] . "'");
			} else {
				$query = $ba->update("update set_app set show_app='0'");
			}
			if (($_POST['telnet_ip'] != "") && ($_POST['telnet_range'] != "")) {

				$query = $ba->update("update general set telnet_acl='" . $_POST['telnet_ip'] . "/" . $_POST['telnet_range'] . "'");
			}
			if (($_POST['http_ip'] != "") && ($_POST['http_range'] != "")) $ba->set('http_acl',$_POST['http_ip'] . "/" . $_POST['http_range']);
			if (($_POST['sip_ip'] != "") && ($_POST['sip_range'] != "")) $ba->set('sip_acl',$_POST['sip_ip'] . "/" . $_POST['sip_range']);
			if (($_POST['rtp_ip'] != "") && ($_POST['rtp_range'] != "")) $ba->set('rtp_acl',$_POST['rtp_ip'] . "/" . $_POST['rtp_range']);
			if (($_POST['bfdetect_ip'] != "") && ($_POST['bfdetect_range'] != "")) $ba->set('bfdetect_acl',$_POST['bfdetect_ip'] . "/" . $_POST['bfdetect_range']);
			if (($_POST['ssh_ip'] != "") && ($_POST['ssh_range'] != "")) $ba->set('ssh_acl',$_POST['ssh_ip'] . "/" . $_POST['ssh_range']);
			$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,'sec','2','".ACTIVATE."');</script>";
				}
			}		}

		$table.="<table class='contenttoc' id='table' style='min-width:330px;'>
			<tr>
			<th colspan='2'>".SECURITY_TABLE_HEADER_DIS."</th>
			</tr>
			<tr>
			<td><b>".SECURITY_OLD_PASS_DIS.":</b></td>
			<td><input type='password' name='old' onmouseover=\"Tip('".CHANGE_PWD_OLD_PASS."')\" onmouseout='UnTip()'></td>
			</tr>
			<tr>
			<td><b>".SECURITY_NEW_PASS_DIS.":</b></td>
			<td><input type='password' name='newp' onmouseover=\"Tip('".CHANGE_PWD_NEW_PASS."')\" onmouseout='UnTip()'></td>
			</tr>
			<tr>
			<td nowrap><b>".SECURITY_CONFIRM_PASS_DIS.":</b></td>
			<td><input type='password' name='confirm_new' onmouseover=\"Tip('".CHANGE_PWD_CONFIRM_NEW_PASS."')\" onmouseout='UnTip()'></td>
			</tr>
			<tr>
			<td nowrap><b>".SECURITY_API_PASS_DIS.":</b></td>
			<td><input type='checkbox' name='samePwd' checked='true' value='1'></td>
			</tr>
			<tr>
			<td><b>".SECURITY_DISABLE_SESSIONS_DIS.":</b></td>
			<td>" . $this->show_app() . "</td>
			</tr>
			<tr onmouseover=\"Tip('".SECURITY_SAFE_GUARD."')\" onmouseout='UnTip()'>
			<td><b>".SECURITY_SAFE_GUARD_DIS.":</b></td>
			<td>" . $this->safeguard() . "</td>
			</tr>
			<tr><th colspan='2'>ACL</th></tr>
			<tr>
			<td><b>".SECURITY_TELNET_ACL_DIS.":</b></td>
			<td>" . $this->telnet_acl() . "</td>
			</tr>
			<tr>
			<td><b>".SECURITY_HTTP_ACL_DIS.":</b></td>
			<td>" . $this->get_acl('http') . "</td>
			</tr>
			<tr>
			<td><b>".SECURITY_SIP_ACL_DIS.":</b></td>
			<td>" . $this->get_acl('sip') . "</td>
			</tr>
			<!--
			<tr>
			<td><b>".SECURITY_RTP_ACL_DIS.":</b></td>
			<td>" . $this->get_acl('rtp') . "</td>
			</tr>
			-->
			<tr>
			<td><b>".SECURITY_BFDETECT_ACL_DIS.":</b></td>
			<td>" . $this->get_acl('bfdetect') . "</td>
			</tr>
			<tr>
			<td><b>".SECURITY_SSH_ACL_DIS.":</b></td>
			<td>" . $this->get_acl('ssh') . "</td>
			</tr>";
			if ($_SESSION['beroari_username']=="beroAdmin"){
				$table.="<tr>
				<td colspan='2'>
				<input  type='submit' name='submit' value='Restore admin password and username' onmouseover=\"Tip('".CHANGE_PWD_RESTORE_ADMIN_PASS."')\" onmouseout='UnTip()'>
				</td>
				</tr>";
			}
			$table.="<tr>
			<td colspan='2'><input name='submit' type='submit' value='".BUTTON_SAVE."' onmouseover=\"Tip('".CHANGE_PWD_SAVE."')\" onmouseout='UnTip()'>
			</td>
			</tr>
			</table>
			<br>
			</form>";

		//Rückgabe: Erzeugte Tabelle mit den änderbaren Feldern um das Kennwort zu ändern
		return $table;
	}

}


?>
