Global: Add a date adaptation to the user's timezone

Closes #170
This commit is contained in:
veselcraft 2022-01-22 15:54:57 +03:00
parent dc20ccb0be
commit 7baa4cf20a
No known key found for this signature in database
GPG key ID: AED66BC1AC628A4E
6 changed files with 41 additions and 1 deletions

View file

@ -1,6 +1,7 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace openvk\Web\Presenters; namespace openvk\Web\Presenters;
use MessagePack\MessagePack; use MessagePack\MessagePack;
use Chandler\Session\Session;
final class InternalAPIPresenter extends OpenVKPresenter final class InternalAPIPresenter extends OpenVKPresenter
{ {
@ -68,4 +69,28 @@ final class InternalAPIPresenter extends OpenVKPresenter
$this->fail(-32603, "Uncaught " . get_class($ex)); $this->fail(-32603, "Uncaught " . get_class($ex));
} }
} }
function renderTimezone() {
if($_SERVER["REQUEST_METHOD"] !== "POST")
exit("ты дебил это метод апи");
$sessionOffset = Session::i()->get("_timezoneOffset");
if(is_numeric($this->postParam("timezone"))) {
$postTZ = intval($this->postParam("timezone"));
if ($postTZ != $sessionOffset || $sessionOffset == null) {
Session::i()->set("_timezoneOffset", $postTZ ? $postTZ : 3 * MINUTE );
$this->returnJson([
"success" => 1 // If it's new value
]);
} else {
$this->returnJson([
"success" => 2 // If it's the same value (if for some reason server will call this func)
]);
}
} else {
$this->returnJson([
"success" => 0
]);
}
}
} }

View file

@ -201,6 +201,7 @@ abstract class OpenVKPresenter extends SimplePresenter
$user = Authenticator::i()->getUser(); $user = Authenticator::i()->getUser();
$this->template->isXmas = intval(date('d')) >= 1 && date('m') == 12 || intval(date('d')) <= 15 && date('m') == 1 ? true : false; $this->template->isXmas = intval(date('d')) >= 1 && date('m') == 12 || intval(date('d')) <= 15 && date('m') == 1 ? true : false;
$this->template->isTimezoned = Session::i()->get("_timezoneOffset");
if(!is_null($user)) { if(!is_null($user)) {
$this->user = (object) []; $this->user = (object) [];

View file

@ -17,6 +17,10 @@
{script "js/l10n.js"} {script "js/l10n.js"}
{script "js/openvk.cls.js"} {script "js/openvk.cls.js"}
{if $isTimezoned == null}
{script "js/timezone.js"}
{/if}
{ifset $thisUser} {ifset $thisUser}
{if $thisUser->getNsfwTolerance() < 2} {if $thisUser->getNsfwTolerance() < 2}
{css "css/nsfw-posts.css"} {css "css/nsfw-posts.css"}

View file

@ -9,6 +9,8 @@ routes:
handler: "About->rules" handler: "About->rules"
- url: "/rpc" - url: "/rpc"
handler: "InternalAPI->route" handler: "InternalAPI->route"
- url: "/iapi/timezone"
handler: "InternalAPI->timezone"
- url: "/support" - url: "/support"
handler: "Support->index" handler: "Support->index"
- url: "/support/tickets" - url: "/support/tickets"

7
Web/static/js/timezone.js Executable file
View file

@ -0,0 +1,7 @@
// This file is included only when there is no info about timezone in users's chandler session
xhr = new XMLHttpRequest();
xhr.open("POST", "/iapi/timezone", true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onload = () => {window.location.reload()};
xhr.send('timezone=' + new Date().getTimezoneOffset());

View file

@ -167,7 +167,8 @@ function ovk_proc_strtrim(string $string, int $length = 0): string
function ovk_strftime_safe(string $format, ?int $timestamp = NULL): string function ovk_strftime_safe(string $format, ?int $timestamp = NULL): string
{ {
$str = strftime($format, $timestamp ?? time()); $sessionOffset = intval(Session::i()->get("_timezoneOffset"));
$str = strftime($format, $timestamp + ($sessionOffset * MINUTE) * -1 ?? time() + ($sessionOffset * MINUTE) * -1);
if(PHP_SHLIB_SUFFIX === "dll") { if(PHP_SHLIB_SUFFIX === "dll") {
$enc = tr("__WinEncoding"); $enc = tr("__WinEncoding");
if($enc === "@__WinEncoding") if($enc === "@__WinEncoding")