opr.php 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. if($_GET["opr"] == "query"){
  13. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  14. $result["f_timezone"] = exec("tzutil /g");
  15. foreach ($timezone as $tz) {
  16. if($tz[2] == $result["f_timezone"]){
  17. $result["timezone"] = $tz[1];
  18. }
  19. }
  20. $result["fulltime"] = exec('getTime.exe');
  21. $result["time"] = explode(" ",exec('getTime.exe'))[1]." ".explode(" ",exec('getTime.exe'))[2];
  22. }else{
  23. $result["timezone"] = exec("cat /etc/timezone");
  24. $result["ntpserver"] = explode("=",exec("sudo timedatectl show-timesync | grep 'ServerName='"))[1];
  25. foreach ($timezone as $tz) {
  26. if($tz[1] == $result["timezone"]){
  27. $result["f_timezone"] = $tz[2];
  28. }
  29. }
  30. $result["fulltime"] = exec('date +"%a %Y-%m-%d %T %Z %z"');
  31. $result["time"] = exec('date +"%Y-%m-%d %T"');
  32. }
  33. $nextDayLightSavingTZ = new DateTimeZone($result["timezone"]);
  34. $nextDayLightSavingTZTransitions = $nextDayLightSavingTZ->getTransitions(time());
  35. if(sizeOf($nextDayLightSavingTZTransitions) != 1){
  36. $nextDayLightSavingTZTime = array_slice($nextDayLightSavingTZTransitions,1,1)[0];
  37. $nextDayLightSavingTZTimeDT = new DateTime($nextDayLightSavingTZTime["time"]);
  38. $currentTimeDT = new DateTime($result["fulltime"]);
  39. $interval = $currentTimeDT->diff($nextDayLightSavingTZTimeDT);
  40. $result["nextdaylight"] = $nextDayLightSavingTZTimeDT->format('Y-m-d H:m:s');
  41. $result["nextdaylightremains"] = $interval->format('%a');
  42. //dst means Day Light Saving Time
  43. $result["nextdst"] = $nextDayLightSavingTZTime["isdst"];
  44. $result["existdaylight"] = true;
  45. }else{
  46. $result["existdaylight"] = false;
  47. }
  48. echo json_encode($result);
  49. }else if($_GET["opr"] == "alltimezone"){
  50. echo json_encode($timezone);
  51. }else if($_GET["opr"] == "modify"){
  52. if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'){
  53. $tz_win = "";
  54. foreach ($timezone as $tz) {
  55. if($tz[1] == $_GET["tz"]){
  56. $tz_win = $tz[2];
  57. }
  58. }
  59. //for NTP
  60. exec("w32TM /config /syncfromflags:manual /manualpeerlist:".$_GET["ntpserver"]);
  61. //for TZ
  62. exec('tzutil /s "'.$tz_win.'"');
  63. echo "Finish";
  64. }else{
  65. //for NTP
  66. exec("sudo chmod -R 0777 /etc/systemd/timesyncd.conf");
  67. $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;
  68. $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';
  69. file_put_contents("/etc/systemd/timesyncd.conf",$NTPFile);
  70. exec("sudo timedatectl set-ntp true");
  71. //for TZ
  72. exec("sudo timedatectl set-timezone '".$_GET["tz"]."'");
  73. //for All
  74. exec("sudo systemctl restart systemd-timesyncd");
  75. //exec("sudo timedatectl status");
  76. //exec("sudo timedatectl timesync-status");
  77. //exec("sudo timedatectl show-timesync");
  78. echo "Finish";
  79. }
  80. }
  81. ?>