<?php
/**
 * Klasse Prosivioning
 * TFTP oder HTTP Einstellungen
 */

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

		// args
		$m = $_GET['m'];
		include('./includes/header.php');
		$prov_table = $this->dynamic_table();
		// build page content

		//Javascript zur Anzeige des richtigen Formulars (entweder tftp oder http)
		$ret.="<script>
		function tftp_http(index){
				if (index==0){
					document.getElementById('use_boot_http').disabled=true;
					document.getElementById('http_host').style.display='none';
					document.getElementById('http_url').style.display='none';
					document.getElementById('http_boot').style.display='none';
					document.getElementById('tftp_host').style.display='';
					document.getElementById('tftp_url').style.display='';
					document.getElementById('tftp_boot').style.display='';
					document.getElementById('use_boot_tftp').disabled=false;

				}else if (index==1){
					document.getElementById('http_host').style.display='';
					document.getElementById('http_url').style.display='';
					document.getElementById('http_boot').style.display='';
					document.getElementById('tftp_host').style.display='none';
					document.getElementById('tftp_url').style.display='none';
					document.getElementById('tftp_boot').style.display='none';
					document.getElementById('use_boot_tftp').disabled=true;
					document.getElementById('use_boot_http').disabled=false;
				}

			}
			</script>";
		$ret .= "
					<form class='extensions_form' name='provisioning' action='' method='POST'>
					<input type=hidden name=m value=" . $m . ">
					<input type=hidden name=a value='save'>
					<br>
					" . $prov_table;

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




	/*
	 * Funktion dynamic_table
	 */

	function dynamic_table() {
		$table="";
		//Wenn man Änderugen von der Daten macht werden die in der Tabelle der Datenbank
		//aktualisiert
		require_once("./includes/lang.php");
		include '/usr/local/php/beroConf.php';
		$bc = new beroConf('root');
		if (isset($_POST['tftp_host']) && isset($_POST['tftp_url']) && $_POST['prov_opt']=="tftp") {
			if (($_POST['tftp_host']=="" && $_POST['tftp_url']=="") ||
				($_POST['tftp_host']!="" && $_POST['tftp_url']=="") ||
				($_POST['tftp_host']!="" && $_POST['tftp_url']!="")) {
				$bc->set("root", "tftp-host", $_POST['tftp_host']);
				$bc->set("root", "tftp-url", $_POST['tftp_url']);
				$bc->set("root", "provisioning-option", $_POST['prov_opt']);
			}else{
				echo "<script>alert('".PROVISIONING_TFTP_HOST_ERROR_DIS."');</script>";
			}
		}elseif(isset($_POST['http_host']) && isset($_POST['http_url']) && $_POST['prov_opt']=="http"){
			if (($_POST['http_host']=="" && $_POST['http_url']=="") ||
				($_POST['http_host']!="" && $_POST['http_url']=="") ||
				($_POST['http_host']!="" && $_POST['http_url']!="")) {
				$bc->set("root", "http-host", $_POST['http_host']);
				$bc->set("root", "http-url", $_POST['http_url']);
				$bc->set("root", "provisioning-option", $_POST['prov_opt']);
			}else{
				echo "<script>alert('".PROVISIONING_HTTP_HOST_ERROR_DIS."');</script>";
			}
		}
		if ($_POST['action']=='save'){
			if (isset($_POST['reset-admin-pass'])) exec("/usr/local/sbin/reset_admin_passwd.sh");
                        if (isset($_POST['userapp-enabled'])) {
                            $bc->set("root", "userapp-enabled", "1");
                            $bc->set("root", "userapp-user-enabled", "1");
                        } else {
                            $bc->set("root", "userapp-enabled", "0");
                            $bc->set("root", "userapp-user-enabled", "0");
                        }
			if (isset($_POST['beroAvahi-enabled'])) $bc->set("root", "beroAvahi-enabled", "1");
			else $bc->set("root", "beroAvahi-enabled", "0");
			if (isset($_POST['use_boot_prov'])) $bc->set("root", "use-boot-prov", "yes");
			else $bc->set("root", "use-boot-prov", "no");
			if ($_POST['password']!=""){
				@unlink("/usr/conf/apiusers.conf");
				$handle = fopen("/usr/conf/apiusers.conf","w");
   				fputs($handle, "admin:".$_POST['password']);
    	 		fclose($handle);
			}
			$ba = new beroAri();
			$query=$ba->select("select option from activate");
			while($entry = $ba->fetch_array($query)){

				if ($entry['option'] <= 1){
					$query =  $ba->update("UPDATE activate SET option=1");
					$table.= "<script>aktivieren(1,'prov','1','".ACTIVATE."');</script>";
				}
			}
		}
		$zeile = @file("/usr/conf/apiusers.conf");

   		$tmp=split(":",$zeile[0]);
   		$password=$tmp[1];
   		$select_opt="<select name='prov_opt' id='prov_opt' onchange='tftp_http(prov_opt.selectedIndex);'>";
   		if ($bc->get('root','provisioning-option')=="http"){
   			$select_opt.="<option value='tftp'>tftp</option>
   						<option value='http' selected>http</option>";
		}else{
			$select_opt.="<option value='tftp'>tftp</option>
   						<option value='http'>http</option>";
		}
		$select_opt.="</select>";

		$table .= "<table class='contenttoc' id='table'>
					<tr>
					<th colspan='2'>".PROVISIONING_TABLE_HEADER_DIS."</th>
					</tr>
					<tr>
					<td><b>".PROVISIONING_OPTIONS_DIS.":</b></td>
					<td>".$select_opt."</td>
					</tr>
					<tr id='tftp_host'>
					<td><b>".PROVISIONING_TFTP_HOST_DIS.":</b></td>
					<td><input type='text' name='tftp_host' value='" . $bc->get('root','tftp-host') . "'></td>
					</tr>
					<tr id='tftp_url'>
					<td><b>".PROVISIONING_TFTP_URL_DIS.":</b></td>
					<td><input type='text' name='tftp_url' value='" . $bc->get('root','tftp-url')  . "'></td>
					</tr>
					<tr id='tftp_boot'>
					<td><b>".PROVISIONING_USE_BOOT_TFTP_DIS.":</b></td>
					<td>";
					if ($bc->get('root','use-boot-prov')=="yes") $table.="<input type='checkbox' id='use_boot_tftp' name='use_boot_prov' value='yes' checked>";
					else $table.="<input type='checkbox' id='use_boot_tftp' name='use_boot_prov' value='yes'>";
		$table.="</td>
					<tr id='http_host'>
					<td><b>".PROVISIONING_HTTP_HOST_DIS.":</b></td>
					<td><input type='text' name='http_host' value='" . $bc->get('root','http-host') . "'></td>
					</tr>
					<tr id='http_url'>
					<td><b>".PROVISIONING_HTTP_URL_DIS.":</b></td>
					<td><input type='text' name='http_url' value='" . $bc->get('root','http-url')  . "'></td>
					</tr>
					<tr id='http_boot'>
					<td><b>".PROVISIONING_USE_BOOT_HTTP_DIS.":</b></td>
					<td>";
					if ($bc->get('root','use-boot-prov')=="yes") $table.="<input type='checkbox' id='use_boot_http' name='use_boot_prov' value='yes' checked>";
					else $table.="<input type='checkbox' id='use_boot_http' name='use_boot_prov' value='yes'>";
		$table.="</td>
					</tr>
					<tr>
					<td><b>".PROVISIONING_API_PASSWORD_DIS.":</b></td>
					<td><input type='text' name='password' value='".$password."' ></td>
					</tr>
					<tr>
					<td><b>".PROVISIONING_USERAPP_ENABLE_DIS.":</b></td>
					<td>";
					if ($bc->get('root','userapp-enabled')=="1") $table.="<input type='checkbox' id='userapp-enabled' name='userapp-enabled' value='1' checked>";
					else $table.="<input type='checkbox' id='userapp-enabled' name='userapp-enabled' value='1'>";
					$table.="</td>
					</tr>
					<tr id='admin-pass' onmouseover=\"Tip('" . RESET_ADMIN_PWD . "')\" onmouseout='UnTip()'>
					<td><b>".PROVISIONING_ADMIN_PASS_RESET_DIS.":</b></td>
					<td>
					<input type='checkbox' id='reset-admin-pass' name='reset-admin-pass' value='1'>
					</td>
					</tr>
					<tr>
					<td><b>".PROVISIONING_AVAHI_DIS.":</b></td>
					<td>";
					if ($bc->get('root','beroAvahi-enabled')=="1") $table.="<input type='checkbox' name='beroAvahi-enabled' value='1' checked>";
					else $table.="<input type='checkbox' name='beroAvahi-enabled' value='1'>";
					$table.="</td>
					</tr>
					<tr>
					<td colspan='2'><input type='hidden' name='action' value='save'>
					<input name='submit' type='submit' value='Save' >
					</td>
					</tr>
					</table>
					<br>
					</form>
					<script type='text/javascript'>tftp_http(document.getElementById('prov_opt').selectedIndex);
					function show_tr_adminPass() {
						if ($('#userapp-enabled').is(':checked'))
							$('#admin-pass').css('display','');
						else
							$('#admin-pass').css('display','none');
					}
					$('#userapp-enabled').change( function(){ show_tr_adminPass(); });
					$(document).ready(function(){ show_tr_adminPass(); });
					</script>";
		//Rückgabe: Erzeugte Tabelle mit den änderbaren Feldern von den Einstellungen von isgw.conf
		return $table;
	}

}
?>
