1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-29 17:19:52 +02:00

Enhance checking of new version

- Do not verify version on login page
- Do not show error from version checking
- Sync expiration of version cookie with file
- Clean up the code
This commit is contained in:
Peter Knut
2024-11-01 17:34:06 +01:00
parent 47f1f19970
commit 316754af47
4 changed files with 95 additions and 72 deletions

View File

@@ -1038,18 +1038,26 @@ class Adminer {
*/
function navigation($missing) {
global $VERSION, $jush, $drivers, $connection;
?>
?>
<h1>
<?php echo $this->name(); ?>
<?php if ($missing != "auth"): ?>
<span class="version">
<?php echo $VERSION; ?>
<a href="https://github.com/pematon/adminer/releases"<?php echo target_blank(); ?> id="version">
<?php echo (version_compare($VERSION, $_COOKIE["adminer_version"]) < 0 ? h($_COOKIE["adminer_version"]) : ""); ?>
</a>
</span>
<?php echo $this->name(); ?>
<?php if ($missing != "auth"): ?>
<span class="version">
<?php echo $VERSION; ?>
<a href="https://github.com/pematon/adminer/releases"<?php echo target_blank(); ?> id="version">
<?php echo (version_compare($VERSION, $_COOKIE["adminer_version"]) < 0 ? h($_COOKIE["adminer_version"]) : ""); ?>
</a>
</span>
<?php
if (!isset($_COOKIE["adminer_version"])) {
echo script("verifyVersion('" . js_escape(ME) . "', '" . get_token() . "');");
}
?>
<?php endif; ?>
</h1>
<?php
if ($missing == "auth") {
$output = "";

View File

@@ -18,10 +18,11 @@ function page_header($title, $error = "", $breadcrumb = [], $title2 = "") {
// Load Adminer version from file if cookie is missing.
$filename = get_temp_dir() . "/adminer.version";
if (!$_COOKIE["adminer_version"] && file_exists($filename) && filemtime($filename) + 86400 > time()) { // 86400 - 1 day in seconds
if (!$_COOKIE["adminer_version"] && file_exists($filename) && ($lifetime = filemtime($filename) + 86400 - time()) > 0) { // 86400 - 1 day in seconds
$data = unserialize(file_get_contents($filename));
$_COOKIE["adminer_version"] = $data["version"];
cookie("adminer_version", $data["version"], 24 * 3600);
cookie("adminer_version", $data["version"], $lifetime); // Sync expiration with the file.
}
?>
<!DOCTYPE html>
@@ -42,12 +43,12 @@ function page_header($title, $error = "", $breadcrumb = [], $title2 = "") {
<body class="<?php echo lang('ltr'); ?> nojs">
<script<?php echo nonce(); ?>>
document.body.onkeydown = bodyKeydown;
document.body.onclick = bodyClick;
<?php if (!isset($_COOKIE["adminer_version"])): ?>
document.body.onload = function () { verifyVersion('<?php echo $VERSION; ?>', '<?php echo js_escape(ME); ?>', '<?php echo get_token(); ?>') };
<?php endif; ?>
document.body.className = document.body.className.replace(/ nojs/, ' js');
const body = document.body;
body.onkeydown = bodyKeydown;
body.onclick = bodyClick;
body.classList.remove("nojs");
body.classList.add("js");
var offlineMessage = '<?php echo js_escape(lang('You are offline.')); ?>';
var thousandsSeparator = '<?php echo js_escape(lang(',')); ?>';
@@ -188,9 +189,9 @@ function page_messages($error) {
/**
* Prints HTML footer.
*
* @param $missing string "auth", "db", "ns"
* @param ?string $missing "auth", "db", "ns"
*/
function page_footer($missing = "")
function page_footer($missing = null)
{
global $adminer, $token;
@@ -200,7 +201,7 @@ function page_footer($missing = "")
switch_lang();
if ($missing != "auth") {
?>
?>
<div class="logout">
<form action="" method="post">

View File

@@ -99,14 +99,14 @@ function cookie(assign, days) {
/**
* Verifies current Adminer version.
*
* @param currentVersion string
* @param baseUrl string
* @param token string
*/
function verifyVersion(currentVersion, baseUrl, token) {
function verifyVersion(baseUrl, token) {
// Dummy value to prevent repeated verifications after AJAX failure.
cookie('adminer_version=0', 1);
ajax('https://api.github.com/repos/pematon/adminer/releases/latest', function (request) {
ajax('https://api.github.com/repos/pematon/adminer/releases/latest', (request) => {
const response = JSON.parse(request.responseText);
const version = response.tag_name.replace(/^\D*/, '');
@@ -115,12 +115,8 @@ function verifyVersion(currentVersion, baseUrl, token) {
cookie('adminer_version=' + version, 1);
const data = 'version=' + version + '&token=' + token;
ajax(baseUrl + 'script=version', function () {}, data);
if (currentVersion !== version) {
gid('version').innerText = version;
}
});
ajax(baseUrl + 'script=version', null, data);
}, null, null, true);
}
/** Get value of select
@@ -867,41 +863,51 @@ function fieldChange() {
/** Create AJAX request
* @param string
* @param function (XMLHttpRequest)
* @param [string]
* @param [string]
* @return XMLHttpRequest or false in case of an error
* @uses offlineMessage
*/
function ajax(url, callback, data, message) {
var request = (window.XMLHttpRequest ? new XMLHttpRequest() : (window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : false));
if (request) {
var ajaxStatus = gid('ajaxstatus');
if (message) {
ajaxStatus.innerHTML = '<div class="message">' + message + '</div>';
ajaxStatus.className = ajaxStatus.className.replace(/ hidden/g, '');
} else {
ajaxStatus.className += ' hidden';
}
request.open((data ? 'POST' : 'GET'), url);
if (data) {
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
request.onreadystatechange = function () {
if (request.readyState === 4) {
if (/^2/.test(request.status)) {
callback(request);
} else {
ajaxStatus.innerHTML = (request.status ? request.responseText : '<div class="error">' + offlineMessage + '</div>');
ajaxStatus.className = ajaxStatus.className.replace(/ hidden/g, '');
}
}
};
request.send(data);
/**
* Sends AJAX request.
*
* @param {string} url
* @param {function|null} onSuccess (XMLHttpRequest)
* @param {string|null} data POST data.
* @param {string|null} progressMessage
* @param {boolean} failSilently
* @return XMLHttpRequest or false in case of an error
* @uses offlineMessage
*/
function ajax(url, onSuccess = null, data = null, progressMessage = null, failSilently = false) {
const ajaxStatus = gid('ajaxstatus');
if (progressMessage) {
ajaxStatus.innerHTML = '<div class="message">' + progressMessage + '</div>';
ajaxStatus.classList.remove("hidden");
} else {
ajaxStatus.classList.add("hidden");
}
const request = new XMLHttpRequest();
request.open((data ? 'POST' : 'GET'), url);
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
if (data) {
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
request.onreadystatechange = () => {
if (request.readyState === 4) {
if (request.status >= 200 && request.status < 300) {
if (onSuccess) {
onSuccess(request);
}
} else if (failSilently) {
console.error(request.status ? request.responseText : "No internet connection");
} else {
ajaxStatus.innerHTML = (request.status ? request.responseText : '<div class="error">' + offlineMessage + '</div>');
ajaxStatus.classList.remove("hidden");
}
}
};
request.send(data);
return request;
}

View File

@@ -600,18 +600,26 @@ qsl('div').onclick = whisperClick;", "")
function navigation($missing) {
global $VERSION;
?>
?>
<h1>
<?php echo $this->name(); ?>
<?php if ($missing != "auth"): ?>
<span class="version">
<?php echo $VERSION; ?>
<a href="https://github.com/pematon/adminer/releases"<?php echo target_blank(); ?> id="version">
<?php echo (version_compare($VERSION, $_COOKIE["adminer_version"]) < 0 ? h($_COOKIE["adminer_version"]) : ""); ?>
</a>
</span>
<?php echo $this->name(); ?>
<?php if ($missing != "auth"): ?>
<span class="version">
<?php echo $VERSION; ?>
<a href="https://github.com/pematon/adminer/releases"<?php echo target_blank(); ?> id="version">
<?php echo (version_compare($VERSION, $_COOKIE["adminer_version"]) < 0 ? h($_COOKIE["adminer_version"]) : ""); ?>
</a>
</span>
<?php
if (!isset($_COOKIE["adminer_version"])) {
echo script("verifyVersion('" . js_escape(ME) . "', '" . get_token() . "');");
}
?>
<?php endif; ?>
</h1>
<?php
if ($missing == "auth") {
$first = true;