opr.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. // Read timezone DB
  3. $timezone = [];
  4. $file = fopen("data/timezone.csv","r");
  5. while(! feof($file)){
  6. $tmp = fgetcsv($file);
  7. //print_r($tmp);
  8. array_push($timezone,array($tmp[0],$tmp[1],$tmp[2]));
  9. }
  10. fclose($file);
  11. // END
  12. //check the opration
  13. if($_GET["opr"] == "query"){
  14. // if win
  15. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  16. $result["f_timezone"] = exec("tzutil /g"); //timezone name in English
  17. //search timezone name and convert it to Unix timezone format
  18. foreach ($timezone as $tz) {
  19. if($tz[2] == $result["f_timezone"]){
  20. $result["timezone"] = $tz[1];
  21. }
  22. }
  23. //full time by program
  24. $result["fulltime"] = exec('getTime.exe');
  25. //explode the y-m-d h:m:s out
  26. $result["time"] = explode(" ",exec('getTime.exe'))[1]." ".explode(" ",exec('getTime.exe'))[2];
  27. //since windows had some problem on detecting the daylight, disable it
  28. $result["existdaylight"] = false; //disable detecing the daylight. NOT supported
  29. //it is windows
  30. $result["isWindows"] = true;
  31. }else{
  32. //get the timezone
  33. $result["timezone"] = exec("cat /etc/timezone");
  34. //get the ntp server
  35. $result["ntpserver"] = explode("=",exec("sudo timedatectl show-timesync | grep 'ServerName='"))[1];
  36. //convert it to readable name , aka Windows TZ format
  37. foreach ($timezone as $tz) {
  38. if($tz[1] == $result["timezone"]){
  39. $result["f_timezone"] = $tz[2];
  40. }
  41. }
  42. //get full time
  43. $result["fulltime"] = exec('date +"%a %Y-%m-%d %T %Z %z"');
  44. //get y-m-d h:m:s out
  45. $result["time"] = exec('date +"%Y-%m-%d %T"');
  46. //check the Daylight saving
  47. $nextDayLightSavingTZ = new DateTimeZone($result["timezone"]); //use timezone to fetch the data
  48. //get next transitions
  49. $nextDayLightSavingTZTransitions = $nextDayLightSavingTZ->getTransitions(time());
  50. //if >1 then means had daylight saving in future
  51. if(sizeOf($nextDayLightSavingTZTransitions) != 1){
  52. //slice and get the first one out
  53. $nextDayLightSavingTZTime = array_slice($nextDayLightSavingTZTransitions,1,1)[0];
  54. //create the DateTime var by using $nextDayLightSavingTZTime
  55. $nextDayLightSavingTZTimeDT = new DateTime($nextDayLightSavingTZTime["time"]);
  56. //get Current time
  57. $currentTimeDT = new DateTime($result["fulltime"]);
  58. //Compare the difference
  59. $interval = $currentTimeDT->diff($nextDayLightSavingTZTimeDT);
  60. //format it to Y-m-d h:m:s, since the input is unix timestamp
  61. $result["nextdaylight"] = $nextDayLightSavingTZTimeDT->format('Y-m-d H:m:s');
  62. //calc the different
  63. $result["nextdaylightremains"] = $interval->format('%a');
  64. //dst means Day Light Saving Time
  65. $result["nextdst"] = $nextDayLightSavingTZTime["isdst"];
  66. $result["existdaylight"] = true;
  67. }else{
  68. //since <1, then no dst right now
  69. $result["existdaylight"] = false;
  70. }
  71. //non windows, false
  72. $result["isWindows"] = false;
  73. }
  74. echo json_encode($result);
  75. }else if($_GET["opr"] == "alltimezone"){
  76. echo json_encode($timezone);
  77. }else if($_GET["opr"] == "modify"){
  78. if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'){
  79. //convert it's timezone to Windows compitiable format
  80. $tz_win = "";
  81. foreach ($timezone as $tz) {
  82. if($tz[1] == $_GET["tz"]){
  83. $tz_win = $tz[2];
  84. }
  85. }
  86. //for TZ
  87. //change timezone
  88. exec('tzutil /s "'.$tz_win.'"');
  89. echo "Finish";
  90. }else{
  91. //for timesyncd.conf
  92. //output it back to system
  93. exec("sudo chmod -R 0777 /etc/systemd/timesyncd.conf");
  94. $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;
  95. $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';
  96. file_put_contents("/etc/systemd/timesyncd.conf",$NTPFile);
  97. //restart
  98. exec("sudo timedatectl set-ntp true");
  99. //for TZ
  100. exec("sudo timedatectl set-timezone '".$_GET["tz"]."'");
  101. //for All
  102. exec("sudo systemctl restart systemd-timesyncd");
  103. //exec("sudo timedatectl status");
  104. //exec("sudo timedatectl timesync-status");
  105. //exec("sudo timedatectl show-timesync");
  106. echo "Finish";
  107. }
  108. }
  109. ?>