Skip to content

Commit

Permalink
refactor: rewritten session keepalive for Twig (#2791)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Jan 6, 2024
1 parent ce808df commit 0616335
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 58 deletions.
69 changes: 13 additions & 56 deletions phpmyfaq/admin/session.keepalive.php
Expand Up @@ -24,6 +24,7 @@
use phpMyFAQ\Language;
use phpMyFAQ\Strings;
use phpMyFAQ\System;
use phpMyFAQ\Template\TwigWrapper;
use phpMyFAQ\Translation;
use phpMyFAQ\User\CurrentUser;

Expand All @@ -50,7 +51,6 @@
require PMF_ROOT_DIR . '/translations/language_' . $language . '.php';
}


//
// Set translation class
//
Expand All @@ -71,61 +71,18 @@
$user = CurrentUser::getCurrentUser($faqConfig);

$refreshTime = (PMF_AUTH_TIMEOUT - PMF_AUTH_TIMEOUT_WARNING) * 60;
?>
<!DOCTYPE html>
<html lang="<?= Translation::get('metaLanguage'); ?>" class="no-js">
<head>
<meta charset="utf-8">

<title>phpMyFAQ - "Welcome to the real world."</title>

<meta content="Only Chuck Norris can divide by zero." name="description">
<meta content="phpMyFAQ Team" name="author" >
<meta content="width=device-width, initial-scale=1" name="viewport" />
<meta content="phpMyFAQ <?= System::getVersion(); ?>" name="application-name">
<meta content="© 2001-<?= date('Y') ?> phpMyFAQ Team" name="copyright">
<meta content="phpMyFAQ Team" name="publisher">
<?php if ($user->isLoggedIn() && ($refreshTime > 0)) { ?>
<script>
const sessionTimeoutWarning = () => {
if (window.confirm('<?php printf(Translation::get('ad_session_expiring'), PMF_AUTH_TIMEOUT_WARNING); ?>')) {
location.href = location.href;
}
};

const sessionTimeoutClock = (topRef, sessionStart, expire) => {
expire.setSeconds(expire.getSeconds() - 1);
const duration = expire - sessionStart;

if (expire.getFullYear() < 2022) {
parent.location.search = '?action=logout';
return;
}

if (topRef) {
topRef.innerHTML = new Date(duration).toISOString().substring(11, 19);
}
};

window.onload = () => {
const expire = new Date();
const sessionStart = new Date();
expire.setSeconds(<?= PMF_AUTH_TIMEOUT ?> * 60);

const topRef = top.document.getElementById('sessioncounter');
$twig = new TwigWrapper(PMF_ROOT_DIR . '/assets/templates');
$template = $twig->loadTemplate('./admin/session-keepalive.twig');

window.setTimeout(sessionTimeoutWarning, <?= $refreshTime ?> * 1000);
window.setInterval(
() => {
sessionTimeoutClock(topRef, sessionStart, expire);
},
1000,
);
};
</script>
<?php } ?>
</head>
<body>
$templateVars = [
'metaLanguage' => Translation::get('metaLanguage'),
'phpMyFAQVersion' => System::getVersion(),
'currentYear' => date('Y'),
'isUserLoggedIn' => $user->isLoggedIn() && ($refreshTime > 0),
'msgConfirm' => sprintf(Translation::get('ad_session_expiring'), PMF_AUTH_TIMEOUT_WARNING),
'sessionTimeout' => PMF_AUTH_TIMEOUT,
'refreshTime' => $refreshTime,
];

</body>
</html>
echo $template->render($templateVars);
2 changes: 0 additions & 2 deletions phpmyfaq/admin/stat.ratings.php
Expand Up @@ -39,7 +39,6 @@
$csrfToken = Filter::filterInput(INPUT_GET, 'csrf', FILTER_SANITIZE_SPECIAL_CHARS);

$twig = new TwigWrapper(PMF_ROOT_DIR . '/assets/templates');
$twig->addExtension(new DebugExtension());
$template = $twig->loadTemplate('./admin/statistics/ratings.twig');

$category = new Category($faqConfig, [], false);
Expand All @@ -54,7 +53,6 @@
}

if ('clear-statistics' === $action && $clearStatistics) {
echo 'huhu';
if ($ratings->deleteAll()) {
$deletedStatistics = true;
} else {
Expand Down
57 changes: 57 additions & 0 deletions phpmyfaq/assets/templates/admin/session-keepalive.twig
@@ -0,0 +1,57 @@
<!DOCTYPE html>
<html lang="{{ metaLanguage }}" class="no-js">
<head>
<meta charset="utf-8">

<title>phpMyFAQ - "Welcome to the real world."</title>

<meta content="Only Chuck Norris can divide by zero." name="description">
<meta content="phpMyFAQ Team" name="author">
<meta content="width=device-width, initial-scale=1" name="viewport">
<meta content="phpMyFAQ {{ phpMyFAQVersion }}" name="application-name">
<meta content="© 2001-{{ currentYear }} phpMyFAQ Team" name="copyright">
<meta content="phpMyFAQ Team" name="publisher">
{% if isUserLoggedIn %}
<script>
const sessionTimeoutWarning = () => {
if (window.confirm('{{msgConfirm}}')) {
location.href = location.href;
}
};
const sessionTimeoutClock = (topRef, sessionStart, expire) => {
expire.setSeconds(expire.getSeconds() - 1);
const duration = expire - sessionStart;
if (expire.getFullYear() < 2022) {
parent.location.search = '?action=logout';
return;
}
if (topRef) {
topRef.innerHTML = new Date(duration).toISOString().substring(11, 19);
}
};
window.onload = () => {
const expire = new Date();
const sessionStart = new Date();
expire.setSeconds({{ sessionTimeout }} * 60);
const topRef = top.document.getElementById('sessioncounter');
window.setTimeout(sessionTimeoutWarning, {{ refreshTime }} * 1000);
window.setInterval(
() => {
sessionTimeoutClock(topRef, sessionStart, expire);
},
1000,
);
};
</script>
{% endif %}
</head>
<body>

</body>
</html>

0 comments on commit 0616335

Please sign in to comment.