From 35f4817260505a1aac0a4373949859a13ec94213 Mon Sep 17 00:00:00 2001 From: Jay Trees Date: Fri, 21 Jan 2022 15:35:33 +0100 Subject: [PATCH] Add self updater --- includes/pages/update.php | 57 +++++++++++++++++++++++++++++++++---- index.php | 60 ++++----------------------------------- 2 files changed, 58 insertions(+), 59 deletions(-) diff --git a/includes/pages/update.php b/includes/pages/update.php index 65efaeed..6a32bcd8 100644 --- a/includes/pages/update.php +++ b/includes/pages/update.php @@ -16,6 +16,48 @@ $page->navigation(); * Update */ if ('POST' === $_SERVER['REQUEST_METHOD']) { + /** + * Files + */ + $zip_filename = __DIR__ . '/' . $tag . '.zip'; + + /** Download */ + file_put_contents( + $zip_filename, + file_get_contents('https://github.com/grandeljay/wishthis/archive/refs/tags/' . $tag . '.zip') + ); + + /** Decompress */ + $zip = new ZipArchive(); + + if ($zip->open($zip_filename)) { + $zip->extractTo(__DIR__); + $zip->close(); + + $directory_wishthis_github = __DIR__ . '/wishthis-' . $version; + + foreach (scandir($directory_wishthis_github) as $filename) { + if (in_array($filename, array('.', '..', 'config'))) { + continue; + } + + $filepath = __DIR__ . '/' . $filename; + $filepath_github = $directory_wishthis_github . '/' . $filename; + + if (is_dir($filepath) && is_dir($filepath_github)) { + delete_directory($filepath); + } + + rename($filepath_github, $filepath); + } + } + + /** Delete */ + unlink($zip_filename); + + /** + * Database + */ /** Current version is below 0.2.0 */ if (-1 === version_compare($options->version, '0.2.0')) { $database->query('ALTER TABLE `users` @@ -33,7 +75,7 @@ if ('POST' === $_SERVER['REQUEST_METHOD']) { ;'); $database->query('ALTER TABLE `wishlists` ADD INDEX(`hash`);'); - $database->query('INSERT INTO `options` (`key`, `value`) VALUES ("version", "' . VERSION . '");'); + $database->query('INSERT INTO `options` (`key`, `value`) VALUES ("version", "' . $version . '");'); } /** Current version is below 0.3.0 */ @@ -44,7 +86,7 @@ if ('POST' === $_SERVER['REQUEST_METHOD']) { } /** Update version */ - $options->setOption('version', VERSION); + $options->setOption('version', $version); header('Location: /?page=home'); die(); @@ -57,8 +99,7 @@ if ('POST' === $_SERVER['REQUEST_METHOD']) {

New version detected

-

Thank you for updating to v!

-

There have been some changes in the database, please run the updater.

+

An update is available. If you are brave, please click the button to start the self updater.

@@ -71,10 +112,16 @@ if ('POST' === $_SERVER['REQUEST_METHOD']) {
+ +
+

Changes

+ + ', $release['body']) ?> +
diff --git a/index.php b/index.php index 9da50020..5fb9be7b 100644 --- a/index.php +++ b/index.php @@ -79,61 +79,13 @@ if ($options) { */ use Github\Client; -$client = new Client(); -$release = $client->api('repo')->releases()->latest('grandeljay', 'wishthis'); -$tag = $release['tag_name']; -$version = str_replace('v', '', $tag); - -$zip_filename = __DIR__ . '/' . $tag . '.zip'; - -/** Download */ -file_put_contents( - $zip_filename, - file_get_contents('https://github.com/grandeljay/wishthis/archive/refs/tags/' . $tag . '.zip') -); - -/** Decompress */ -$zip = new ZipArchive(); - -if ($zip->open($zip_filename)) { - $zip->extractTo(__DIR__); - $zip->close(); - - $directory_wishthis_github = __DIR__ . '/wishthis-' . $version; - - foreach (scandir($directory_wishthis_github) as $filename) { - if (in_array($filename, array('.', '..', 'config'))) { - continue; - } - - $filepath = __DIR__ . '/' . $filename; - $filepath_github = $directory_wishthis_github . '/' . $filename; - - if (is_dir($filepath) && is_dir($filepath_github)) { - delete_directory($filepath); - } - - rename($filepath_github, $filepath); - } -} - -/** Delete */ -unlink($zip_filename); - -echo '
';
-var_Dump($release);
-echo '
'; -die(); - -$releases = json_decode(file_get_contents('https://api.github.com/repos/grandeljay/wishthis/releases')); -$version = $releases[0]->tag_name; - -die($version); - -define('VERSION', '0.3.0'); - if ($options) { - if (-1 === version_compare($options->version, VERSION)) { + $client = new Client(); + $release = $client->api('repo')->releases()->latest('grandeljay', 'wishthis'); + $tag = $release['tag_name']; + $version = str_replace('v', '', $tag); + + if (-1 === version_compare($options->version, $version)) { $options->updateAvailable = true; } }