<?php
// Read timezone DB
	$timezone = [];
	$file = fopen("data/timezone.csv","r");
	while(! feof($file)){
		$tmp = fgetcsv($file);
		//print_r($tmp);
		array_push($timezone,array($tmp[0],$tmp[1],$tmp[2]));
	}
	fclose($file);
// END

if($_GET["opr"] == "query"){
	if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
		$result["f_timezone"] = exec("tzutil /g");
		foreach ($timezone as $tz) {
			if($tz[2] == $result["f_timezone"]){
				$result["timezone"] = $tz[1];
			}
		}	
		$result["fulltime"] = exec('getTime.exe');
		$result["time"] =  explode(" ",exec('getTime.exe'))[1]." ".explode(" ",exec('getTime.exe'))[2];
	}else{
		$result["timezone"] = exec("cat /etc/timezone");
		$result["ntpserver"] = explode("=",exec("sudo timedatectl show-timesync | grep 'ServerName='"))[1];
		foreach ($timezone as $tz) {
			if($tz[1] == $result["timezone"]){
				$result["f_timezone"] = $tz[2];
			}
		}
		$result["fulltime"] = exec('date +"%a %Y-%m-%d %T %Z %z"');
		$result["time"] = exec('date +"%Y-%m-%d %T"');
	}
	$nextDayLightSavingTZ = new DateTimeZone($result["timezone"]);
    $nextDayLightSavingTZTransitions = $nextDayLightSavingTZ->getTransitions(time());
    if(sizeOf($nextDayLightSavingTZTransitions) != 1){
        $nextDayLightSavingTZTime = array_slice($nextDayLightSavingTZTransitions,1,1)[0];
        $nextDayLightSavingTZTimeDT = new DateTime($nextDayLightSavingTZTime["time"]);
        $currentTimeDT = new DateTime($result["fulltime"]);
        $interval = $currentTimeDT->diff($nextDayLightSavingTZTimeDT);
        $result["nextdaylight"] = $nextDayLightSavingTZTimeDT->format('Y-m-d H:m:s');
        $result["nextdaylightremains"] = $interval->format('%a');
        //dst means Day Light Saving Time
        $result["nextdst"] = $nextDayLightSavingTZTime["isdst"];
        $result["existdaylight"] = true;
    }else{
        $result["existdaylight"] = false;
    }
	echo json_encode($result);
}else if($_GET["opr"] == "alltimezone"){
	echo json_encode($timezone);
}else if($_GET["opr"] == "modify"){
	if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'){
		$tz_win = "";
		foreach ($timezone as $tz) {
			if($tz[1] == $_GET["tz"]){
				$tz_win = $tz[2];
			}
		}
		//for NTP
		exec("w32TM /config /syncfromflags:manual /manualpeerlist:".$_GET["ntpserver"]);
		
		//for TZ
		exec('tzutil /s "'.$tz_win.'"');
		echo "Finish";
	}else{
	    //for NTP
		exec("sudo chmod -R 0777 /etc/systemd/timesyncd.conf");
		$NTPFile = '#  This file is part of systemd.'.PHP_EOL.'#'.PHP_EOL.'#  systemd is free software; you can redistribute it and/or modify it'.PHP_EOL.'#  under the terms of the GNU Lesser General Public License as published by'.PHP_EOL.'#  the Free Software Foundation; either version 2.1 of the License, or'.PHP_EOL.'#  (at your option) any later version.'.PHP_EOL.'#'.PHP_EOL.'# Entries in this file show the compile time defaults.'.PHP_EOL.'# You can change settings by editing this file.'.PHP_EOL.'# Defaults can be restored by simply deleting this file.'.PHP_EOL.'#'.PHP_EOL.'# See timesyncd.conf(5) for details.'.PHP_EOL.'# Generated by ArOZ Online at '.date("Y-m-d H:i:s O T").PHP_EOL.PHP_EOL.'[Time]'.PHP_EOL;
		$NTPFile = $NTPFile.'NTP='.$_GET["ntpserver"].PHP_EOL.'#FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org'.PHP_EOL.'#RootDistanceMaxSec=5'.PHP_EOL.'#PollIntervalMinSec=32'.PHP_EOL.'#PollIntervalMaxSec=2048';
		file_put_contents("/etc/systemd/timesyncd.conf",$NTPFile);
		exec("sudo timedatectl set-ntp true");
        //for TZ
		exec("sudo timedatectl set-timezone '".$_GET["tz"]."'");
		
		//for All
		exec("sudo systemctl restart systemd-timesyncd");
		//exec("sudo timedatectl status");
		//exec("sudo timedatectl timesync-status");
		//exec("sudo timedatectl show-timesync");
		
		echo "Finish";
	}
}
?>