Added automatic update detection

This displays an update notice if a new version of LittleLink Custom has been released.


This one is definitely a messy one. Everything I added is commented, but I'm going to try explaining it here one more time. 

General outcome:
 • This feature checks the local version against the newest version stored on a separate server.

Error prevention: 
 • I used 'file_get_contents' to get the version number from a JSON file. If  'file_get_contents' cant find the file, PHP will throw an Error Exception.
 • To prevent this, I put the update section in an if-statement that only applies if the following conditions are met.

Check if files exist:
 • To test whether the file on the server exists (remember, if the server can't be reached, you will get an Error Exception) I used a function that checks if the URL to the file on the server successfully returns a '200 OK' response (see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/200).
 • If this condition is met, it sets the variable '$ServerExists' to true, if not to false.

 • Now, the previous mentioned if-statement simply checks if the local file exists and the '$ServerExists' variable equals 'true' to proceed.

Get version number:
 • If all previous conditions are met, two variables get defined via 'file_get_contents' '$Vgit' and '$Vlocal'.
 • Both the file on the server and the local file simply contain the version number.
 • The variable '$Vgit' is the newest version number retrieved from the server.
 • The variable '$Vlocal' is the local  version number.

Display update notice:
 • To finally display the  version number (added with HTML) one last check via an if-statement is performed. This simply checks if the current user is an admin AND if the version retrieved from the server is higher than the local one.
 • If both conditions are met, the notice is displayed.



In addition, I should mention that I put the existing 'Watch Page' link as well as the update notice in a div with the class 'row' to display both links next to another instead of them being stacked.
This commit is contained in:
JulianPrieber 2022-02-23 13:38:15 +01:00 committed by GitHub
parent b14f26080d
commit e6faf86386
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 0 deletions

View File

@ -125,7 +125,42 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="nav navbar-nav ml-auto">
<li class="nav-item">
<div class="row">
<! #### begin update detection #### >
<?php // Checks if URL exists
function URL_exists(string $url): bool
{
return str_contains(get_headers($url)[0], "200 OK");
}
// Sets $ServerExists to true if URL exists
if (URL_exists("https://littlelink-custom.tru.io/version.json")){
$ServerExists = "true";
} else {
$ServerExists = "false";
}
?>
<! Checks if file version.json exists AND if version.json exists on server to continue (without this PHP will throw ErrorException ) >
@if(file_exists(base_path("version.json")) and $ServerExists == 'true')
<?php // Requests newest version from server and sets it as variable
$Vgit = file_get_contents("https://littlelink-custom.tru.io/version.json");
// Requests current version from the local version file and sets it as variable
$Vlocal = file_get_contents(base_path("version.json"));
?>
<! If user has role admin AND newest GitHub release version is higher than the local one an update notice will be displayed >
@if(auth()->user()->role == 'admin' and $Vgit > $Vlocal)
<a style="color:#007bff" class="nav-link" href="https://littlelink-custom.tru.io/how-to-update.html" target="_blank" title="Click here to learn more about how to update">An update is available</a>
@endif
@endif
<! #### end update detection #### >
<a class="nav-link" href="{{ config('app.url') }}/@<?= Auth::user()->littlelink_name ?>" target="_blank">Watch Page</a>
</div>
</li>
</ul>
</div>