fix: do not rely on /etc/timezone to get the time zone. timedatectl does not update it immediately (anymore), it only gets updated after a reboot

This commit is contained in:
frederik
2024-02-21 17:09:30 +01:00
parent 4ba418e836
commit 421575423c
3 changed files with 15 additions and 40 deletions
+3 -18
View File
@@ -4,25 +4,10 @@
$_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING); $_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING); $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$sys_timezone = ""; if(!isset($_SESSION['my_timezone'])) {
// If we can get the timezome from the systems timezone file ust that $_SESSION['my_timezone'] = trim(shell_exec('timedatectl show --value --property=Timezone'));
if (file_exists('/etc/timezone')) {
$tz_data = file_get_contents('/etc/timezone');
if ($tz_data !== false) {
$sys_timezone = trim($tz_data);
}
} else {
// Else get timezone from the timedatectl command
$tz_data = shell_exec('timedatectl show');
$tz_data_array = parse_ini_string($tz_data);
if (is_array($tz_data_array) && array_key_exists('Timezone', $tz_data_array)) {
$sys_timezone = $tz_data_array['Timezone'];
}
}
//Finally if we have a valod timezone, set it as the one PHP uses
if ($sys_timezone !== "") {
date_default_timezone_set($sys_timezone);
} }
date_default_timezone_set($_SESSION['my_timezone']);
if (file_exists('./scripts/thisrun.txt')) { if (file_exists('./scripts/thisrun.txt')) {
$config = parse_ini_file('./scripts/thisrun.txt'); $config = parse_ini_file('./scripts/thisrun.txt');
+6 -20
View File
@@ -4,27 +4,13 @@
$_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING); $_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING); $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$sys_timezone = "";
// If we can get the timezome from the systems timezone file ust that
if (file_exists('/etc/timezone')) {
$tz_data = file_get_contents('/etc/timezone');
if ($tz_data !== false) {
$sys_timezone = trim($tz_data);
}
} else {
// else get timezone from the timedatectl command
$tz_data = shell_exec('timedatectl show');
$tz_data_array = parse_ini_string($tz_data);
if (is_array($tz_data_array) && array_key_exists('Timezone', $tz_data_array)) {
$sys_timezone = $tz_data_array['Timezone'];
}
}
// finally if we have a valod timezone, set it as the one PHP uses
if ($sys_timezone !== "") {
date_default_timezone_set($sys_timezone);
}
session_start(); session_start();
if(!isset($_SESSION['my_timezone'])) {
$_SESSION['my_timezone'] = trim(shell_exec('timedatectl show --value --property=Timezone'));
}
date_default_timezone_set($_SESSION['my_timezone']);
$user = shell_exec("awk -F: '/1000/{print $1}' /etc/passwd"); $user = shell_exec("awk -F: '/1000/{print $1}' /etc/passwd");
$user = trim($user); $user = trim($user);
$home = shell_exec("awk -F: '/1000/{print $6}' /etc/passwd"); $home = shell_exec("awk -F: '/1000/{print $6}' /etc/passwd");
+6 -2
View File
@@ -1,7 +1,10 @@
<?php <?php
error_reporting(E_ERROR); error_reporting(E_ERROR);
ini_set('display_errors',1); ini_set('display_errors',1);
if(!isset($_SESSION['my_timezone'])) {
$_SESSION['my_timezone'] = trim(shell_exec('timedatectl show --value --property=Timezone'));
}
date_default_timezone_set($_SESSION['my_timezone']);
$user = shell_exec("awk -F: '/1000/{print $1}' /etc/passwd"); $user = shell_exec("awk -F: '/1000/{print $1}' /etc/passwd");
$home = shell_exec("awk -F: '/1000/{print $6}' /etc/passwd"); $home = shell_exec("awk -F: '/1000/{print $6}' /etc/passwd");
$home = trim($home); $home = trim($home);
@@ -114,6 +117,7 @@ if(isset($_GET["latitude"])){
if(isset($timezone) && in_array($timezone, DateTimeZone::listIdentifiers())) { if(isset($timezone) && in_array($timezone, DateTimeZone::listIdentifiers())) {
shell_exec("sudo timedatectl set-timezone ".$timezone); shell_exec("sudo timedatectl set-timezone ".$timezone);
$_SESSION['my_timezone'] = $timezone;
date_default_timezone_set($timezone); date_default_timezone_set($timezone);
echo "<script>setTimeout( echo "<script>setTimeout(
function() { function() {
@@ -688,7 +692,7 @@ https://discordapp.com/api/webhooks/{WebhookID}/{WebhookToken}
Select a timezone Select a timezone
</option> </option>
<?php <?php
$current_timezone = trim(shell_exec("cat /etc/timezone")); $current_timezone = trim(shell_exec("timedatectl show --value --property=Timezone"));
$timezone_identifiers = DateTimeZone::listIdentifiers(DateTimeZone::ALL); $timezone_identifiers = DateTimeZone::listIdentifiers(DateTimeZone::ALL);
$n = 425; $n = 425;