Skip to content

Commit e6faf86

Browse files
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.
1 parent b14f260 commit e6faf86

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

resources/views/layouts/sidebar.blade.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,42 @@ function update_color_scheme() {
125125
<div class="collapse navbar-collapse" id="navbarSupportedContent">
126126
<ul class="nav navbar-nav ml-auto">
127127
<li class="nav-item">
128+
<div class="row">
129+
130+
<! –– #### begin update detection #### ––>
131+
132+
<?php // Checks if URL exists
133+
function URL_exists(string $url): bool
134+
{
135+
return str_contains(get_headers($url)[0], "200 OK");
136+
}
137+
// Sets $ServerExists to true if URL exists
138+
if (URL_exists("https://littlelink-custom.tru.io/version.json")){
139+
$ServerExists = "true";
140+
} else {
141+
$ServerExists = "false";
142+
}
143+
?>
144+
145+
<! –– Checks if file version.json exists AND if version.json exists on server to continue (without this PHP will throw ErrorException ) ––>
146+
@if(file_exists(base_path("version.json")) and $ServerExists == 'true')
147+
148+
<?php // Requests newest version from server and sets it as variable
149+
$Vgit = file_get_contents("https://littlelink-custom.tru.io/version.json");
150+
151+
// Requests current version from the local version file and sets it as variable
152+
$Vlocal = file_get_contents(base_path("version.json"));
153+
?>
154+
155+
<! –– If user has role admin AND newest GitHub release version is higher than the local one an update notice will be displayed ––>
156+
@if(auth()->user()->role == 'admin' and $Vgit > $Vlocal)
157+
<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>
158+
@endif
159+
@endif
160+
<! –– #### end update detection #### ––>
161+
128162
<a class="nav-link" href="{{ config('app.url') }}/@<?= Auth::user()->littlelink_name ?>" target="_blank">Watch Page</a>
163+
</div>
129164
</li>
130165
</ul>
131166
</div>

0 commit comments

Comments
 (0)