<?php


/**
 * Klasse General
 * Zeigt algemeine Einstellungen von der isgw.conf Datei
 */

class remoteManagement {

	/*
	 * Funktion display erzeugt den Inhalt von der HTML-Seite
	 */
	function display($args) {
		// args
		$m = $_GET['m'];

		include('./includes/header.php');
		$remoteM_table = $this->dynamic_table();
		// build page content
		$ret .= "
					<form class='extensions_form' name='cloud' action='' method='POST'>
					<input type=hidden name=m value=" . $m . ">
					<input type=hidden name=a value='save'>
					<br>
					" . $remoteM_table;

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


	/*
	 * Der boolesche Wert von cloud_enable, der in der Datenbank gespeichert ist wird zurückgegeben
	 * in der Form eines Checkboxs und nachher angezeigt.
	 */
	function cloud_enable() {
		require_once( '/usr/local/php/beroConf.php' );
		$bc = new beroConf('root');
		$cloud_enable = $bc->get("root","cloud_enable");
		if($cloud_enable == "1")
			return "<input type='hidden' name='cloud_enable' value='0'>
					<input type='checkbox' name='cloud_enable' value='1' onmouseover=\"Tip('" . CLOUD_ENABLE . "')\" onmouseout='UnTip()' checked>";
		else
			return "<input type='hidden' name='cloud_enable' value='0'>
					<input type='checkbox' name='cloud_enable' value='1' onmouseover=\"Tip('" . CLOUD_ENABLE . "')\" onmouseout='UnTip()'>";

	}
	/*
	 * Der boolesche Wert von snmp-enabled, der in der Datenbank gespeichert ist wird zurückgegeben
	 * in der Form eines Checkboxs und nachher angezeigt.
	 */
	function snmp_enabled() {
		require_once( '/usr/local/php/beroConf.php' );
		$bc = new beroConf('root');
		$snmp_enable = $bc->get("root","snmp-enabled");
		if($snmp_enable == "1")
			return "<input type='hidden' name='snmp-enabled' value='0'>
					<input type='checkbox' name='snmp-enabled' value='1' onmouseover=\"Tip('" . SNMP_ENABLED . "')\" onmouseout='UnTip()' checked>";
		else
			return "<input type='hidden' name='snmp-enabled' value='0'>
					<input type='checkbox' name='snmp-enabled' value='1' onmouseover=\"Tip('" . SNMP_ENABLED . "')\" onmouseout='UnTip()'>";

	}
	/*
	 * Die Cloud key, der in der Datenbank gespeichert ist wird zurückgegeben
	 * in der Form eines Textfelds und nachher angezeigt.
	 */
	function cloud_key() {
		require_once( '/usr/local/php/beroConf.php' );
		$bc = new beroConf('root');
		return $bc->get("root","cloud_key");
	}



	/*
	 * Funktion dynamic_table
	 */

	function dynamic_table() {
		require_once( '/usr/local/php/beroConf.php' );
		$result ="";
		//Wenn man Änderugen von der Daten macht werden die in der Tabelle der Datenbank
		//aktualisiert
		if (isset($_POST['cloud_enable'])) {

			$bc = new beroConf('root');
			$bc->set('root','cloud_enable',$_POST['cloud_enable']);
			$bc->set('root','cloud_key',$_POST['cloud_key']);



			$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,'remoteM','1','".ACTIVATE."');</script>";
				}
			}
		}
		if (isset($_POST['snmp-enabled'])) {
			$bc = new beroConf('root');
			$bc->set('root','snmp-enabled',$_POST['snmp-enabled']);

			$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,'remoteM','1','".ACTIVATE."');</script>";
				}
			}
		}
		if (isset($_POST['cloud_username']) && isset($_POST['cloud_password'])) {
			$res=file_get_contents('/proc/cmdline');
			preg_match("/[^a-zA-Z0-9=]*?d=(?<serial>.+) /",$res,$serial);
			$serial=$serial['serial'];
			$apiUrl='https://berocloud.beronet.com/index.php?r=api/register';
			$ch = curl_init();

			$data = array('serial'=>$serial, 'cloud_username'=>$_POST['cloud_username'], 'cloud_password'=>$_POST['cloud_password']);

			curl_setopt($ch, CURLOPT_URL, $apiUrl);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

			$result = curl_exec($ch);
			preg_match("/[^a-zA-Z0-9=]*?yaqwsx(?<key>.+)yaqwsx/",$result,$key);
			$key=unserialize($key['key']);
			if (isset($key['key'])){
				$bc = new beroConf('root');
				$bc->set('root','cloud_key',$key['key']);
				$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,'remoteM','1','".ACTIVATE."');</script>";
					}
				}
				$result=preg_replace("/[^a-zA-Z0-9=]*?yaqwsx(?<key>.+)yaqwsx/",". ",$result);
			}
			curl_close($ch);
		}

		require_once("./includes/lang.php");
		if ($result!="")  $table.="<font color='red'>".$result."</font><br/><br/>";
		$table .= "<table class='contenttoc' id='table'>
					<tr>
					<th colspan='2'>".CLOUD_TABLE_HEADER_DIS."</th>
					</tr>
					<tr>
					<td><b>".CLOUD_ENABLE_DIS.":</b></td>
					<td>" . $this->cloud_enable() . "</td>
					</tr>
					<tr>
					<td><b>".CLOUD_KEY_DIS.":</b></td>
					<td><input type='text' name='cloud_key' value='" . $this->cloud_key() . "'></td>
					</tr>
					<tr>
					<td colspan='2'><input name='submit' type='submit' value='".BUTTON_SAVE."'>
					</td>
					</tr>
					</table>
					</form>
					<br/>

					<form class='extensions_form' action='' method='POST'>
					<input type=hidden name=m value=" . $_GET['m'] . ">
					<table class='contenttoc'>
					<tr>
					<td><b>Cloud Username:</b></td>
					<!--<td><input type='text' name='register_key'></td>-->
					<td><input type='text' name='cloud_username'></td>
					</tr>
					<tr>
					<td><b>Cloud password:</b></td>
					<td><input type='password' name='cloud_password'></td>
					</tr>
					<tr>
					<td colspan='2'><input name='submit' type='submit' value='".BUTTON_REGISTER."'>
					</td>
					</tr>
					</table>
					</form>
					<br/>

					<form class='extensions_form' action='' method='POST'>
					<input type=hidden name=m value=" . $_GET['m'] . ">
					<table class='contenttoc'>
					<tr>
					<th colspan='2'>".SNMP_TABLE_HEADER_DIS."</th>
					</tr>
					<tr>
					<td><b>".SNMP_ENABLED_DIS.":</b></td>
					<td>" . $this->snmp_enabled() . "</td>
					</tr>
					<tr>
					<td colspan='2'><input name='submit' type='submit' value='".BUTTON_SAVE."'>
					</td>
					</tr>";
					if (is_file("/usr/local/conf/BEROFIX-MIB.txt"))
					$table .="<tr>
					<td colspan='2'><a href=\"misc/files_download.php?option=BEROFIX-MIB\">Download BEROFIX-MIB</a></td>
					</tr>";
					$table .= "</table>
					</form>";
		//Rückgabe: Erzeugte Tabelle mit den änderbaren Feldern von den Einstellungen von isgw.conf
		return $table;
	}

}
?>
