From af92f3d70aa272bcc76fdb4a1918d5ddc428b14f Mon Sep 17 00:00:00 2001 From: Sergio Brighenti Date: Thu, 24 Jan 2019 21:48:22 +0100 Subject: [PATCH] Working on self update feature --- app/Controllers/UpgradeController.php | 121 ++++++++++++++++++++++ app/helpers.php | 4 +- app/routes.php | 6 ++ composer.json | 5 +- index.php | 1 + resources/templates/dashboard/system.twig | 27 ++++- src/js/app.js | 15 +++ 7 files changed, 170 insertions(+), 9 deletions(-) create mode 100644 app/Controllers/UpgradeController.php diff --git a/app/Controllers/UpgradeController.php b/app/Controllers/UpgradeController.php new file mode 100644 index 0000000..9bd05e4 --- /dev/null +++ b/app/Controllers/UpgradeController.php @@ -0,0 +1,121 @@ +getApiJson(); + } catch (\RuntimeException $e) { + $jsonResponse['message'] = $e->getMessage(); + return $response->withJson($jsonResponse, 503); + } + + if (version_compare($json[0]->tag_name, PLATFORM_VERSION, '<=')) { + $this->session->alert(lang('already_latest_version'), 'warning'); + return redirect($response, 'system'); + } + + $tmpFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'xbackbone_update.zip'; + + if (file_put_contents($tmpFile, file_get_contents($json[0]->assets[0]->browser_download_url)) === false) { + $this->session->alert(lang('cannot_retrieve_file'), 'danger'); + return redirect($response, 'system'); + }; + + if (filesize($tmpFile) !== $json[0]->assets[0]->size) { + $this->session->alert(lang('file_size_no_match'), 'danger'); + return redirect($response, 'system'); + } + + $updateZip = new ZipArchive(); + $updateZip->open($tmpFile); + + + for ($i = 0; $i < $updateZip->numFiles; $i++) { + $nameIndex = $updateZip->getNameIndex($i); + if (is_dir(BASE_DIR . $nameIndex)) { + continue; + } + if (file_exists(BASE_DIR . $nameIndex) && md5($updateZip->getFromIndex($i)) !== md5_file(BASE_DIR . $nameIndex)) { + $updateZip->extractTo(BASE_DIR, $nameIndex); + } elseif (!file_exists(BASE_DIR . $nameIndex)) { + $updateZip->extractTo(BASE_DIR, $nameIndex); + } + print_r($updateZip->getNameIndex($i) . '
'); + } + + $updateZip->close(); + unlink($tmpFile); + + return redirect($response, '/install'); + } + + /** + * @param Request $request + * @param Response $response + * @return Response + */ + public function checkForUpdates(Request $request, Response $response): Response + { + $jsonResponse = [ + 'status' => null, + 'message' => null, + 'upgrade' => false, + ]; + + try { + $json = $this->getApiJson(); + + $jsonResponse['status'] = 'OK'; + if (version_compare($json[0]->tag_name, PLATFORM_VERSION, '>')) { + $jsonResponse['message'] = lang('new_version_available', $json[0]->tag_name); //"New version {$json[0]->tag_name} available!"; + $jsonResponse['upgrade'] = true; + } else { + $jsonResponse['message'] = lang('already_latest_version');//'You have already the latest version.'; + $jsonResponse['upgrade'] = false; + } + return $response->withJson($jsonResponse, 200); + } catch (\RuntimeException $e) { + $jsonResponse['status'] = 'ERROR'; + $jsonResponse['message'] = $e->getMessage(); + return $response->withJson($jsonResponse, 503); + } + } + + protected function getApiJson() + { + $opts = [ + 'http' => [ + 'method' => 'GET', + 'header' => [ + 'User-Agent: XBackBone-App', + 'Accept: application/vnd.github.v3+json', + ], + ], + ]; + + $data = file_get_contents(self::GITHUB_SOURCE_API, false, stream_context_create($opts)); + + if ($data === false) { + throw new \RuntimeException('Cannot contact the Github API. Try again.'); + } + + return json_decode($data); + } + +} \ No newline at end of file diff --git a/app/helpers.php b/app/helpers.php index f421db5..a67a717 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -233,11 +233,11 @@ if (!function_exists('dd')) { */ function dd() { - echo '
';
 		array_map(function ($x) {
+			echo '
';
 			print_r($x);
+			echo '
'; }, func_get_args()); - echo '
'; die(); } } diff --git a/app/routes.php b/app/routes.php index aa9c004..c833f08 100644 --- a/app/routes.php +++ b/app/routes.php @@ -5,9 +5,15 @@ $app->group('', function () { $this->group('', function () { $this->get('/home/switchView', \App\Controllers\DashboardController::class . ':switchView')->setName('switchView'); + $this->get('/system/deleteOrphanFiles', \App\Controllers\AdminController::class . ':deleteOrphanFiles')->setName('system.deleteOrphanFiles'); + $this->get('/system/themes', \App\Controllers\ThemeController::class . ':getThemes')->setName('theme'); $this->post('/system/theme/apply', \App\Controllers\ThemeController::class . ':applyTheme')->setName('theme.apply'); + + $this->post('/system/upgrade', \App\Controllers\UpgradeController::class . ':upgrade')->setName('system.upgrade'); + $this->get('/system/checkForUpdates', \App\Controllers\UpgradeController::class . ':checkForUpdates')->setName('system.checkForUpdates'); + $this->get('/system', \App\Controllers\AdminController::class . ':system')->setName('system'); $this->get('/users[/page/{page}]', \App\Controllers\UserController::class . ':index')->setName('user.index'); diff --git a/composer.json b/composer.json index adb76b9..7b19f58 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "sergix44/xbackbone", - "version": "2.4.1", + "version": "2.5", "description": "A lightweight ShareX PHP backend", "type": "project", "require": { @@ -12,7 +12,8 @@ "intervention/image": "^2.4", "ext-json": "*", "ext-gd": "*", - "ext-pdo": "*" + "ext-pdo": "*", + "ext-zip": "*" }, "autoload": { "files": [ diff --git a/index.php b/index.php index b382b22..e1d192e 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,7 @@ version); require 'bootstrap/app.php'; diff --git a/resources/templates/dashboard/system.twig b/resources/templates/dashboard/system.twig index 4214041..cdfc079 100644 --- a/resources/templates/dashboard/system.twig +++ b/resources/templates/dashboard/system.twig @@ -53,8 +53,8 @@
-
-
+
+
{{ lang('theme') }}
@@ -75,9 +75,26 @@
-
-
+
{{ lang('updates') }}
+
+
+
+ +
+
+ +
+
+ +
+
+
+
+
+
+
+
{{ lang('system_info') }}
Max upload size: @@ -87,7 +104,7 @@
-
+
{{ lang('maintenance') }}
{{ lang('clean_orphaned_uploads') }} diff --git a/src/js/app.js b/src/js/app.js index 6779a4d..12f738b 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -8,6 +8,7 @@ var app = { $('.publish-toggle').click(app.publishToggle); $('.refresh-token').click(app.refreshToken); $('#themes').mousedown(app.loadThemes); + $('#checkForUpdatesButton').click(app.checkForUpdates); $('.alert').fadeTo(4000, 500).slideUp(500, function () { $('.alert').slideUp(500); @@ -89,6 +90,20 @@ var app = { }, telegramShare: function () { window.open($('#telegram-share-button').data('url') + $('#telegram-share-text').val(), '_blank'); + }, + checkForUpdates: function () { + $('#checkForUpdatesMessage').empty().text('...'); + $('#doUpgradeButton').prop('disabled', true); + $.get(window.AppConfig.base_url + '/system/checkForUpdates', function (data) { + if (data.status === 'OK') { + $('#checkForUpdatesMessage').empty().text(data.message); + if (data.upgrade) { + $('#doUpgradeButton').prop('disabled', false); + } else { + $('#doUpgradeButton').prop('disabled', true); + } + } + }); } };