From 316754af47e1b6c6f7393a1aeea0b33ba13f1b03 Mon Sep 17 00:00:00 2001 From: Peter Knut Date: Fri, 1 Nov 2024 17:34:06 +0100 Subject: [PATCH] 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 --- adminer/include/adminer.inc.php | 26 ++++++---- adminer/include/design.inc.php | 23 +++++---- adminer/static/functions.js | 92 ++++++++++++++++++--------------- editor/include/adminer.inc.php | 26 ++++++---- 4 files changed, 95 insertions(+), 72 deletions(-) diff --git a/adminer/include/adminer.inc.php b/adminer/include/adminer.inc.php index 011ed46a..59674b34 100644 --- a/adminer/include/adminer.inc.php +++ b/adminer/include/adminer.inc.php @@ -1038,18 +1038,26 @@ class Adminer { */ function navigation($missing) { global $VERSION, $jush, $drivers, $connection; - ?> +?> +

- name(); ?> - - - - id="version"> - - - + name(); ?> + + + + + id="version"> + + + +

+ 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. } ?> @@ -42,12 +43,12 @@ function page_header($title, $error = "", $breadcrumb = [], $title2 = "") { > - document.body.onkeydown = bodyKeydown; - document.body.onclick = bodyClick; - - document.body.onload = function () { verifyVersion('', '', '') }; - - 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 = ''; var thousandsSeparator = ''; @@ -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") { - ?> +?>
diff --git a/adminer/static/functions.js b/adminer/static/functions.js index b07835af..0a1ea0ae 100644 --- a/adminer/static/functions.js +++ b/adminer/static/functions.js @@ -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 = '
' + message + '
'; - 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 : '
' + offlineMessage + '
'); - 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 = '
' + progressMessage + '
'; + 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 : '
' + offlineMessage + '
'); + ajaxStatus.classList.remove("hidden"); + } + } + }; + + request.send(data); + return request; } diff --git a/editor/include/adminer.inc.php b/editor/include/adminer.inc.php index 8cf08838..38562a3e 100644 --- a/editor/include/adminer.inc.php +++ b/editor/include/adminer.inc.php @@ -600,18 +600,26 @@ qsl('div').onclick = whisperClick;", "") function navigation($missing) { global $VERSION; - ?> +?> +

- name(); ?> - - - - id="version"> - - - + name(); ?> + + + + + id="version"> + + + +

+