Skip to content

Commit

Permalink
Check maj 200110 (#371)
Browse files Browse the repository at this point in the history
Test Maj: Utilisation de Javascript si curl pas disponible
  • Loading branch information
kazimentou authored and haruka-7 committed Jan 13, 2020
1 parent 8ea412f commit a03b38a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
55 changes: 53 additions & 2 deletions core/admin/parametres_infos.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<div class="inline-form action-bar">
<h2><?php echo L_CONFIG_INFOS_TITLE ?></h2>
<p><?php echo L_PLUXML_CHECK_VERSION ?></p>
<?php echo $plxAdmin->checkMaj(); ?>
<?php $maj = $plxAdmin->checkMaj(); echo $maj; ?>
</div>

<p><?php echo L_CONFIG_INFOS_DESCRIPTION ?></p>
Expand Down Expand Up @@ -114,7 +114,58 @@
header('Location: ' . basename(__FILE__));
exit;
}

if(preg_match('%class="[^"]*\bred\b[^"]*"%', $maj)) {
# checkMaj() has failed with curl or allow_url_fopen is off
?>
<script type="text/javascript">
(function() {
'use strict';
const currentVersion = '<?= PLX_VERSION ?>';
const id = 'latest-version';
const el = document.getElementById(id);
if(el == null) {
console.error('Element with id="' + id + '" not found');
return;
}

function compareVersion(v1, v2) {
if(typeof v1 != 'string' || typeof v2 != 'string') { return; }

const t1 = v1.split('.');
const t2 = v2.split('.');
for(let i=0, iMax=(t1.length < t2.length) ? t1.length : t2.length; i<iMax; i++) {
const n1 = parseInt(t1[i]);
const n2 = parseInt(t2[i]);
if(n1 == n2) { continue; }
return (n1 < n2) ? -1 : 1;
}
return (t1.length == t2.length) ? 0 : (t1.length < t2.length) ? -1 : 1;
}

const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState === XMLHttpRequest.DONE) {
if(this.status === 200) {
console.log('Available version :', this.responseText);
el.classList.remove('red');
if(compareVersion(currentVersion, this.responseText) < 0) {
el.innerHTML = '<?= $plxAdmin->update_link ?>';
el.classList.add('orange');
} else {
el.innerHTML = "<?= L_PLUXML_UPTODATE.' ('.PLX_VERSION.')' ?>";
el.classList.add('green');
}
return;
}
console.error('[check update]', this.status, this.statusText);
}
};
xhr.open('GET', '<?= PLX_URL_VERSION ?>');
xhr.send();
})();
</script>
<?php
}
# On inclut le footer
include __DIR__ .'/foot.php';
?>
6 changes: 3 additions & 3 deletions core/lib/class.plx.admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class plxAdmin extends plxMotor {

private static $instance = null;
public $update_link = PLX_URL_REPO; // overwritten by self::checmMaj()

/**
* Méthode qui se charger de créer le Singleton plxAdmin
Expand Down Expand Up @@ -1216,10 +1217,9 @@ public function editTags() {
**/
public function checkMaj() {

$caption='PluXml.org';

$latest_version = 'L_PLUXML_UPDATE_ERR';
$className = '';
$this->update_link = sprintf('%s : <a href="%s">%s</a>', L_PLUXML_UPDATE_AVAILABLE, PLX_URL_REPO, PLX_URL_REPO);

$http_response_header = '';
# test avec allow_url_open ou file_get_contents ?
Expand Down Expand Up @@ -1260,7 +1260,7 @@ public function checkMaj() {
$className = 'green';
}
else {
$msg = sprintf('%s <a href="%s">%s</a>', L_PLUXML_UPDATE_AVAILABLE, PLX_URL_REPO, $caption);
$msg = $this->update_link;
$className = 'orange';
}

Expand Down

0 comments on commit a03b38a

Please sign in to comment.