<?php


/**
 * Class for time settings
 */
class Time_conf {
	function options(){
			$bc = new beroConf('root');
			$arr_text=array("(GMT -12:00) Eniwetok, Kwajalein","(GMT -11:00) Midway Island, Samoa",
			"(GMT -10:00) Hawaii","(GMT -9:00) Alaska","(GMT -8:00) Pacific Time (US &amp; Canada)",
			"(GMT -7:00) Mountain Time (US &amp; Canada)","(GMT -6:00) Central Time (US &amp; Canada), Mexico City",
			"(GMT -5:00) Eastern Time (US &amp; Canada), Bogota, Lima","(GMT -4:00) Atlantic Time (Canada), Caracas, La Paz",
			"(GMT -3:30) Newfoundland","(GMT -3:00) Brazil, Buenos Aires, Georgetown",
			"(GMT -2:00) Mid-Atlantic","(GMT -1:00 hour) Azores, Cape Verde Islands",
			"(GMT) Western Europe Time, London, Lisbon, Casablanca","(GMT +1:00) Berlin, Brussels, Copenhagen, Madrid, Paris",
			"(GMT +2:00) Kaliningrad, South Africa","(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersburg",
			"(GMT +3:30) Tehran","(GMT +4:00) Abu Dhabi, Muscat, Baku, Tbilisi",
			"(GMT +4:30) Kabul","(GMT +5:00) Ekaterinburg, Islamabad, Karachi, Tashkent",
			"(GMT +5:30) Bombay, Calcutta, Madras, New Delhi","(GMT +5:45) Kathmandu",
			"(GMT +6:00) Almaty, Dhaka, Colombo","(GMT +7:00) Bangkok, Hanoi, Jakarta",
			"(GMT +8:00) Beijing, Perth, Singapore, Hong Kong","(GMT +9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk",
			"(GMT +9:30) Adelaide, Darwin","(GMT +10:00) Eastern Australia, Guam, Vladivostok",
			"(GMT +11:00) Magadan, Solomon Islands, New Caledonia","(GMT +12:00) Auckland, Wellington, Fiji, Kamchatka");
	
			$arr_val=array("+12","+11","+10","+9","+8","+7","+6","+5","+4","+3.5","+3","+2","+1","+0",
						"-1","-2","-3","-3.5","-4","-4.5","-5","-5.5","-5.75","-6","-7","-8","-9","-9.5","-10","-11","-12");
			$sp=$bc->get("root", "timezone");
			$options="";
			for ($i=0;$i<count($arr_val);$i++){
				if ($arr_val[$i]==$sp){
					$options.="<option value='".$arr_val[$i]."' selected>".$arr_text[$i]."</option>";
				}else{
					$options.="<option value='".$arr_val[$i]."'>".$arr_text[$i]."</option>";
				}
			}
			return $options;
	}
	
	//Anzeige der pstn ports für time from isdn
	function ports($def=0){
		$ba= new beroAri();
		$options="<option value='no'>Disabled</option>";
		$query= $ba->select("select port from isdn");
		while ($ports=$ba->fetch_array($query)){
			if ($def==$ports['port']){
				$options.= "<option value='".$ports['port']."' selected>Port ".$ports['port']."</option>";
			}else{
				$options.= "<option value='".$ports['port']."'>Port ".$ports['port']."</option>";
			}
		}
		return $options;
	}
	
	
	
	function display($args) {
				
		include '/usr/local/php/beroConf.php';
		require_once ('./includes/isgwtelnet.php');
		$bc = new beroConf('root');
		$ba= new beroAri();

		include('./includes/header.php');
		require_once("./includes/lang.php");
		
		// args
		$m = $_GET['m'];
		$q = $_GET['q'];
		if (!$bc->valid()) {
			$ret.= '<h3>Error:</h3><b>Failed to connect to config database!</b><br>';
			exit(0);
		}
		
		//Einstellungen speichern
		if ($_POST['action']=="save"){
			$bc->set("root", "ntphost", $_POST['ntphost']);
			$bc->set("root","timezone",$_POST['timezone']);
			
			if ($_POST['time_from_isdn']!="no") 
			{
				$bc->set("root","time_from_isdn",$_POST['time_from_isdn']);	
				$isgw = new isgwtelnet();	
  				$isgw->isgw_login();
  				$ret.="<br>". $isgw->itpX($_POST['time_from_isdn'])."<br>";
				$bc->set("root","timezone","0");	
				$bc->set("root", "ntphost","");
				$ba->set("summer_winter_time","0");
				system("/bin/mount -o remount,rw /;
			 			/bin/echo \"GMT0\" > /etc/TZ;
 			 			/bin/mount -o remount,ro /;
 			 			/bin/sync;");	
 			 	$bc->set("root", "ntphost", "");		
 			 	$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");
						$ret.= "<script>aktivieren(1,'time','1','".ACTIVATE."');</script>";
					}
				}
				
			}else{
				if (isset($_POST['summer_winter_time'])){
					$ba->set("summer_winter_time","1");
					//Wenn davor das time from ISDN gesetzt war, und jetzt ausgeschaltet wurde, soll ein gelbes activate erscheinen
					if ($bc->get("root","time_from_isdn")!="no"){
						$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");
								$ret.= "<script>aktivieren(1,'time','1','".ACTIVATE."');</script>";
							}
						}
					}	
					$bc->set("root","time_from_isdn","no");
					$bc->set("root","timezone",$_POST['timezone']);
					system("/bin/mount -o remount,rw /;
			 				/bin/echo \"CET".$_POST['timezone']."CEST,M3.5.0/02:00:00,M10.5.0/03:00:00\" > /etc/TZ;
 			 				/bin/mount -o remount,ro /;
 			 				/bin/sync;");
 			 		
 			 		$bc->set("root", "ntphost", $_POST['ntphost']);
 			 		exec("/usr/sbin/update-time");
				}else{
					$ba->set("summer_winter_time","0");
					$bc->set("root","time_from_isdn","no");
					$bc->set("root","timezone",$_POST['timezone']);
					system("/bin/mount -o remount,rw /;
			 				/bin/echo \"GMT".($_POST['timezone'])."\" > /etc/TZ;
 			 				/bin/mount -o remount,ro /;
 			 				/bin/sync;");
 			 		
 			 		$bc->set("root", "ntphost", $_POST['ntphost']);
 			 		exec("/usr/sbin/update-time");
				}
				$isgw = new isgwtelnet();	
  				$isgw->isgw_login();
  				$isgw->itpX("no");
  				$ret.="<br/>".exec("date  +%d-%B-%Y-%H:%M:%S")."<br/>";
			}
			
		}
		
		
		
		$ret.="<script>
			function tfi(ch){
				if (ch!=0){
					document.getElementById('ntphost').style.display='none';
					document.getElementById('tz').style.display='none';
					document.getElementById('summer_winter').style.display='none';
				}else{
					document.getElementById('ntphost').style.display='';
					document.getElementById('tz').style.display='';
					document.getElementById('summer_winter').style.display='';
				}
				
			}
		</script>";
		
		$ret.='<br/><form class="extensions_form" name="networkform" method="POST" action="">
			<input type="hidden" name="action" value="save">
			<table id="table" class="contenttoc">
			<tr>
			<th colspan="2">'.TIME_TABLE_HEADER_DIS.'</th>
			</tr>
			<tr id="ntphost">
					<td style="text-align:center">'.TIME_NTP_HOST_DIS.': </td>
					<td>
						<input name="ntphost" type="text" size="15" id="ntphost" onmouseover=\'Tip("Ex. 131.188.3.222")\' onmouseout="UnTip()" value="' . $bc->get("root", "ntphost") . '">
					</td>
				</tr>
				<tr id="tz">
				<td style="text-align:center">'.TIME_TIMEZONE_DIS.': </td>
					<td>
						<select name="timezone" id="timezone" onmouseover=\'Tip("Timezone")\' onmouseout="UnTip()">'.$this->options().'</select>
					</td>
				</tr>
				<tr id="summer_winter">
					<td style="text-align:center" nowrap> '.TIME_SUMMER_WINTER_DIS.': </td>
					<td onmouseover=\'Tip("'.TIME_SUMMER_WINTER_DIS.'")\' onmouseout="UnTip()">';
					if ($ba->get("summer_winter_time")=="1" || $ba->get("summer_winter_time")=="yes") 
					$ret.='<input type="checkbox" name="summer_winter_time" id="summer_winter_time" value="1" checked>';
					else
					$ret.='<input type="checkbox" name="summer_winter_time" id="summer_winter_time" value="1">';
					$ret.='</td>
				</tr>				
				<tr>
				<td style="text-align:center">'.TIME_TIME_FROM_ISDN_DIS.': </td>
					<td>';
					if ($bc->get("root", "time_from_isdn")=="no")
						$ret.= '<select name="time_from_isdn" id="time_from_isdn" onclick="tfi(time_from_isdn.selectedIndex);" onmouseover=\'Tip("Time from ISDN")\' onmouseout="UnTip()">'.$this->ports($bc->get("root", "time_from_isdn")).'</select>';
					else
						$ret.= '<select name="time_from_isdn" id="time_from_isdn" onclick="tfi(time_from_isdn.selectedIndex);" onmouseover=\'Tip("Time from ISDN")\' onmouseout="UnTip()">'.$this->ports($bc->get("root", "time_from_isdn")).'</select>';
				$ret.= '</td>
				</tr>
				<tr>
					<td colspan="2" align="center">
						<input type="reset" name="reset" value="'.BUTTON_RESET.'" onmouseover=\'Tip("' . NET_CONF_RESET . '")\' onmouseout="UnTip()">&nbsp;
						<input type="submit" name="Apply" value="'.BUTTON_OK.'" onmouseover=\'Tip("' . NET_CONF_APPLY . '")\' onmouseout="UnTip()">
					</td>
				</tr>
			</table>
			</form>
			<script type="text/javascript">tfi(document.getElementById("time_from_isdn").selectedIndex);</script>';
			
			
			
			
		//Nigthly Restart(nur für beroAdmin)		
		if (isset($_POST['disable'])){
			if (is_file("/usr/conf/cron/root")){
				$i=0;
				$handle = fopen("/usr/conf/cron/root", "r");
				while (!feof($handle)) {
					$linet = fgets($handle, 1024);
					if (!preg_match('/\/usr\/local\/sbin\/graceful-reboot/', $linet)){
						 $lines[$i]=$linet;
						 $i++;
					 }
				} 
				fclose($handle);
				
				$handle = fopen("/usr/conf/cron/root", "w");
				for ($i=0;$i<count($lines);$i++){
					fwrite($handle,$lines[$i]);
				} 
				fclose($handle);
				system("/etc/init.d/cron stop");
				system("/etc/init.d/cron start");
			}
		}elseif (isset($_POST['hour'])){
			if (is_file("/usr/conf/cron/root")){
				$i=0;
				$handle = fopen("/usr/conf/cron/root", "r");
				while (!feof($handle)) {
					$linet = fgets($handle, 1024);
					if (!preg_match('/\/usr\/local\/sbin\/graceful-reboot/', $linet)){
						 $lines[$i]=$linet;
						 $i++;
					 }
				} 
				fclose($handle);	
				$handle = fopen("/usr/conf/cron/root", "w");
				for ($i=0;$i<count($lines);$i++){
					fwrite($handle,$lines[$i]);
				} 
				fwrite($handle,"1 ".$_POST['hour']." * * * /usr/local/sbin/graceful-reboot\n");
				fclose($handle);
				system("/etc/init.d/cron stop");
				system("/etc/init.d/cron start");

			}
		}

		if(is_file("/usr/local/sbin/graceful-reboot") && date("Y")>=2009 && $bc->get("root", "ntphost")!="" && $_SESSION['beroari_username']=="beroAdmin"){
			$value=false;
			$line="";
			if (is_file("/usr/conf/cron/root")){
				$handle = fopen ("/usr/conf/cron/root", "r");
				while (!feof($handle)) {
					$linet = fgets($handle, 1024);
					if (preg_match('/\/usr\/local\/sbin\/graceful-reboot/', $linet)){
						 $value=true;
						 $line=$linet;
					 }
				} 
				fclose($handle);
			}
			if ($value){
				$v=split(" ",$line);
				$ret.="<br/><br/><form name='nightly_restart' method='POST' class='extensions_form'>
				<table class='contenttoc'>
				<tr><th>".TIME_NIGHTLY_RESTART_DIS."</th></tr>
				<tr><td>".TIME_DISABLE_DIS." <input type='checkbox' value='true' name='disable'></td></tr>
				<tr><td>".TIME_HOUR_DIS.":  <select name='hour'>";
				for ($i=0;$i<=23;$i++) {
					if ($i==$v[1]) $ret.="<option value='$i' selected>$i</option>";
					else $ret.="<option value='$i'>$i</option>";
				}
				
			}else{
				$ret.="<br/><br/>
				<form name='nightly_restart' method='POST' class='extensions_form'>
				<table class='contenttoc'>
				<tr><th>".TIME_NIGHTLY_RESTART_DIS."</th></tr>
				<tr><td>".TIME_DISABLE_DIS." <input type='checkbox' value='true' name='disable' checked></td></tr>
				<tr><td>".TIME_HOUR_DIS.":  <select name='hour'>";
				for ($i=0;$i<=23;$i++) $ret.="<option value='$i'>$i</option>";
			}
			$ret.="</select></td></tr>
			<tr><td><input type='submit' name='restart' value='".BUTTON_SAVE."'></td></tr></table></form>";
		}

			
			
		return $ret;
	}
	
}
?>
