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:
@@ -1038,18 +1038,26 @@ class Adminer {
|
|||||||
*/
|
*/
|
||||||
function navigation($missing) {
|
function navigation($missing) {
|
||||||
global $VERSION, $jush, $drivers, $connection;
|
global $VERSION, $jush, $drivers, $connection;
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<h1>
|
<h1>
|
||||||
<?php echo $this->name(); ?>
|
<?php echo $this->name(); ?>
|
||||||
<?php if ($missing != "auth"): ?>
|
|
||||||
<span class="version">
|
<?php if ($missing != "auth"): ?>
|
||||||
<?php echo $VERSION; ?>
|
<span class="version">
|
||||||
<a href="https://github.com/pematon/adminer/releases"<?php echo target_blank(); ?> id="version">
|
<?php echo $VERSION; ?>
|
||||||
<?php echo (version_compare($VERSION, $_COOKIE["adminer_version"]) < 0 ? h($_COOKIE["adminer_version"]) : ""); ?>
|
<a href="https://github.com/pematon/adminer/releases"<?php echo target_blank(); ?> id="version">
|
||||||
</a>
|
<?php echo (version_compare($VERSION, $_COOKIE["adminer_version"]) < 0 ? h($_COOKIE["adminer_version"]) : ""); ?>
|
||||||
</span>
|
</a>
|
||||||
|
</span>
|
||||||
|
<?php
|
||||||
|
if (!isset($_COOKIE["adminer_version"])) {
|
||||||
|
echo script("verifyVersion('" . js_escape(ME) . "', '" . get_token() . "');");
|
||||||
|
}
|
||||||
|
?>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
if ($missing == "auth") {
|
if ($missing == "auth") {
|
||||||
$output = "";
|
$output = "";
|
||||||
|
@@ -18,10 +18,11 @@ function page_header($title, $error = "", $breadcrumb = [], $title2 = "") {
|
|||||||
|
|
||||||
// Load Adminer version from file if cookie is missing.
|
// Load Adminer version from file if cookie is missing.
|
||||||
$filename = get_temp_dir() . "/adminer.version";
|
$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));
|
$data = unserialize(file_get_contents($filename));
|
||||||
|
|
||||||
$_COOKIE["adminer_version"] = $data["version"];
|
$_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>
|
<!DOCTYPE html>
|
||||||
@@ -42,12 +43,12 @@ function page_header($title, $error = "", $breadcrumb = [], $title2 = "") {
|
|||||||
|
|
||||||
<body class="<?php echo lang('ltr'); ?> nojs">
|
<body class="<?php echo lang('ltr'); ?> nojs">
|
||||||
<script<?php echo nonce(); ?>>
|
<script<?php echo nonce(); ?>>
|
||||||
document.body.onkeydown = bodyKeydown;
|
const body = document.body;
|
||||||
document.body.onclick = bodyClick;
|
|
||||||
<?php if (!isset($_COOKIE["adminer_version"])): ?>
|
body.onkeydown = bodyKeydown;
|
||||||
document.body.onload = function () { verifyVersion('<?php echo $VERSION; ?>', '<?php echo js_escape(ME); ?>', '<?php echo get_token(); ?>') };
|
body.onclick = bodyClick;
|
||||||
<?php endif; ?>
|
body.classList.remove("nojs");
|
||||||
document.body.className = document.body.className.replace(/ nojs/, ' js');
|
body.classList.add("js");
|
||||||
|
|
||||||
var offlineMessage = '<?php echo js_escape(lang('You are offline.')); ?>';
|
var offlineMessage = '<?php echo js_escape(lang('You are offline.')); ?>';
|
||||||
var thousandsSeparator = '<?php echo js_escape(lang(',')); ?>';
|
var thousandsSeparator = '<?php echo js_escape(lang(',')); ?>';
|
||||||
@@ -188,9 +189,9 @@ function page_messages($error) {
|
|||||||
/**
|
/**
|
||||||
* Prints HTML footer.
|
* 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;
|
global $adminer, $token;
|
||||||
|
|
||||||
@@ -200,7 +201,7 @@ function page_footer($missing = "")
|
|||||||
switch_lang();
|
switch_lang();
|
||||||
|
|
||||||
if ($missing != "auth") {
|
if ($missing != "auth") {
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<div class="logout">
|
<div class="logout">
|
||||||
<form action="" method="post">
|
<form action="" method="post">
|
||||||
|
@@ -99,14 +99,14 @@ function cookie(assign, days) {
|
|||||||
/**
|
/**
|
||||||
* Verifies current Adminer version.
|
* Verifies current Adminer version.
|
||||||
*
|
*
|
||||||
* @param currentVersion string
|
|
||||||
* @param baseUrl string
|
* @param baseUrl string
|
||||||
* @param token 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);
|
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 response = JSON.parse(request.responseText);
|
||||||
|
|
||||||
const version = response.tag_name.replace(/^\D*/, '');
|
const version = response.tag_name.replace(/^\D*/, '');
|
||||||
@@ -115,12 +115,8 @@ function verifyVersion(currentVersion, baseUrl, token) {
|
|||||||
cookie('adminer_version=' + version, 1);
|
cookie('adminer_version=' + version, 1);
|
||||||
|
|
||||||
const data = 'version=' + version + '&token=' + token;
|
const data = 'version=' + version + '&token=' + token;
|
||||||
ajax(baseUrl + 'script=version', function () {}, data);
|
ajax(baseUrl + 'script=version', null, data);
|
||||||
|
}, null, null, true);
|
||||||
if (currentVersion !== version) {
|
|
||||||
gid('version').innerText = version;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get value of select
|
/** Get value of select
|
||||||
@@ -867,41 +863,51 @@ function fieldChange() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** Create AJAX request
|
/**
|
||||||
* @param string
|
* Sends AJAX request.
|
||||||
* @param function (XMLHttpRequest)
|
*
|
||||||
* @param [string]
|
* @param {string} url
|
||||||
* @param [string]
|
* @param {function|null} onSuccess (XMLHttpRequest)
|
||||||
* @return XMLHttpRequest or false in case of an error
|
* @param {string|null} data POST data.
|
||||||
* @uses offlineMessage
|
* @param {string|null} progressMessage
|
||||||
*/
|
* @param {boolean} failSilently
|
||||||
function ajax(url, callback, data, message) {
|
* @return XMLHttpRequest or false in case of an error
|
||||||
var request = (window.XMLHttpRequest ? new XMLHttpRequest() : (window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : false));
|
* @uses offlineMessage
|
||||||
if (request) {
|
*/
|
||||||
var ajaxStatus = gid('ajaxstatus');
|
function ajax(url, onSuccess = null, data = null, progressMessage = null, failSilently = false) {
|
||||||
if (message) {
|
const ajaxStatus = gid('ajaxstatus');
|
||||||
ajaxStatus.innerHTML = '<div class="message">' + message + '</div>';
|
|
||||||
ajaxStatus.className = ajaxStatus.className.replace(/ hidden/g, '');
|
if (progressMessage) {
|
||||||
} else {
|
ajaxStatus.innerHTML = '<div class="message">' + progressMessage + '</div>';
|
||||||
ajaxStatus.className += ' hidden';
|
ajaxStatus.classList.remove("hidden");
|
||||||
}
|
} else {
|
||||||
request.open((data ? 'POST' : 'GET'), url);
|
ajaxStatus.classList.add("hidden");
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -600,18 +600,26 @@ qsl('div').onclick = whisperClick;", "")
|
|||||||
|
|
||||||
function navigation($missing) {
|
function navigation($missing) {
|
||||||
global $VERSION;
|
global $VERSION;
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<h1>
|
<h1>
|
||||||
<?php echo $this->name(); ?>
|
<?php echo $this->name(); ?>
|
||||||
<?php if ($missing != "auth"): ?>
|
|
||||||
<span class="version">
|
<?php if ($missing != "auth"): ?>
|
||||||
<?php echo $VERSION; ?>
|
<span class="version">
|
||||||
<a href="https://github.com/pematon/adminer/releases"<?php echo target_blank(); ?> id="version">
|
<?php echo $VERSION; ?>
|
||||||
<?php echo (version_compare($VERSION, $_COOKIE["adminer_version"]) < 0 ? h($_COOKIE["adminer_version"]) : ""); ?>
|
<a href="https://github.com/pematon/adminer/releases"<?php echo target_blank(); ?> id="version">
|
||||||
</a>
|
<?php echo (version_compare($VERSION, $_COOKIE["adminer_version"]) < 0 ? h($_COOKIE["adminer_version"]) : ""); ?>
|
||||||
</span>
|
</a>
|
||||||
|
</span>
|
||||||
|
<?php
|
||||||
|
if (!isset($_COOKIE["adminer_version"])) {
|
||||||
|
echo script("verifyVersion('" . js_escape(ME) . "', '" . get_token() . "');");
|
||||||
|
}
|
||||||
|
?>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
if ($missing == "auth") {
|
if ($missing == "auth") {
|
||||||
$first = true;
|
$first = true;
|
||||||
|
Reference in New Issue
Block a user