Skip to content

Instantly share code, notes, and snippets.

@binyaminyblatt
Created May 15, 2022 11:24
Show Gist options
  • Save binyaminyblatt/f80601c964579f953620de32d579eb04 to your computer and use it in GitHub Desktop.
Save binyaminyblatt/f80601c964579f953620de32d579eb04 to your computer and use it in GitHub Desktop.
Tachidesk-Sorayomi web auto update
<?php
function CallAPI($url) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$useragent = 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0';
curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
$response = curl_exec($curl);
/* Check for 404 (file not found). */
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Check the HTTP Status code
switch ($httpCode) {
case 200:
$error_status = "200: Success";
return ($response);
break;
case 404:
$error_status = "404: API Not found";
break;
case 410:
$error_status = "410: Gone";
return false;
break;
case 500:
$error_status = "500: servers replied with an error.";
break;
case 502:
$error_status = "502: servers may be down or being upgraded. Hopefully they'll be OK soon!";
break;
case 503:
$error_status = "503: service unavailable. Hopefully they'll be OK soon!";
break;
default:
$error_status = "Undocumented error: " . $httpCode . " : " . curl_error($curl);
return ($response);
break;
}
curl_close($curl);
echo $error_status;
die;
}
function get_this_v(){
$myfile = fopen('version.json', "r") or die("Unable to open file!");
$output = fread($myfile,filesize('version.json'));
fclose($myfile);
return json_decode($output, true);
}
function update_check(){
$git = json_decode(CallAPI('https://api.github.com/repos/Suwayomi/Tachidesk-Sorayomi/releases/latest'), true);
$this_v = get_this_v();
if ($this_v["version"] != $git["tag_name"]){
$assets = $git['assets'];
$data;
foreach($assets as $i=>$v) {
if($v['name'] != "tachidesk-sorayomi-".$git["tag_name"]."-web.zip" ){
continue;
}
$data=$v;
file_put_contents('./'.$v['name'], fopen($v['browser_download_url'], 'r'));
break;
}
$zip = new ZipArchive;
$res = $zip->open($data['name']);
if ($res === TRUE) {
$zip->extractTo('./');
$zip->close();
}
unlink($data['name']);
}
return true;
}
update_check();
$myfile = fopen('index.html', "r") or die("Unable to open file!");
$html = fread($myfile,filesize('index.html'));
fclose($myfile);
$host = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if ($host != $actual_link){
$replace = 'base href="/"';
$with_me = 'base href="./"';
$output = str_replace($replace, $with_me, $html);
}
if (file_exists('./config.js')){
?>
<script src="config.js"></script>
<?php
}
echo $output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment