From 819b36d9d47988411f39545fdc49e7e061f43c48 Mon Sep 17 00:00:00 2001 From: Thorsten Rinne Date: Sat, 14 Oct 2023 11:05:26 +0200 Subject: [PATCH] feat: added route to install package (WIP) (#2492) --- .gitignore | 2 ++ phpmyfaq/.htaccess | 1 + .../admin/assets/src/configuration/upgrade.js | 5 +++-- phpmyfaq/src/admin-routes.php | 13 ++++++++++- .../Administration/UpdateController.php | 22 ++++++++++++++++++- phpmyfaq/src/phpMyFAQ/Setup/Upgrade.php | 5 +++-- 6 files changed, 42 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index de7f8a439e..3a39517ec9 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,8 @@ phpmyfaq/content/core/data/* !phpmyfaq/content/core/data/.gitkeep phpmyfaq/content/core/logs/* !phpmyfaq/content/core/logs/.gitkeep +phpmyfaq/content/upgrades/* +!phpmyfaq/content/upgrades/.gitkeep phpmyfaq/content/user/images/* !phpmyfaq/content/user/images/.gitkeep phpmyfaq/content/user/attachments/* diff --git a/phpmyfaq/.htaccess b/phpmyfaq/.htaccess index 9e6b75dfff..a2914009ea 100644 --- a/phpmyfaq/.htaccess +++ b/phpmyfaq/.htaccess @@ -148,6 +148,7 @@ RewriteRule admin/api/versions admin/api/index.php RewriteRule admin/api/update-check admin/api/index.php RewriteRule admin/api/download-package admin/api/index.php RewriteRule admin/api/extract-package admin/api/index.php +RewriteRule admin/api/install-package admin/api/index.php # REST API v2.0 # * http://[...]/api/v2.0/ diff --git a/phpmyfaq/admin/assets/src/configuration/upgrade.js b/phpmyfaq/admin/assets/src/configuration/upgrade.js index 3973accb42..188e7f5f06 100644 --- a/phpmyfaq/admin/assets/src/configuration/upgrade.js +++ b/phpmyfaq/admin/assets/src/configuration/upgrade.js @@ -50,8 +50,9 @@ export const handleCheckForUpdates = () => { } } }) - .catch((error) => { - console.error(error); + .catch(async (error) => { + const errorMessage = await error.cause.response.json(); + console.error(errorMessage); }); }); } diff --git a/phpmyfaq/src/admin-routes.php b/phpmyfaq/src/admin-routes.php index c4df07428b..50a0c0dab0 100644 --- a/phpmyfaq/src/admin-routes.php +++ b/phpmyfaq/src/admin-routes.php @@ -28,7 +28,7 @@ $routes->add( 'admin.api.versions', - new Route('/versions', ['_controller' => [UpdateController::class, 'versions']]) + new Route('/versions', ['_controller' => [UpdateController::class, 'versions', '_methods' => 'GET']]) ); $routes->add( @@ -58,4 +58,15 @@ ) ); +$routes->add( + 'admin.api.install-package', + new Route( + '/install-package', + [ + '_controller' => [UpdateController::class, 'installPackage'], + '_methods' => 'POST' + ] + ) +); + return $routes; diff --git a/phpmyfaq/src/phpMyFAQ/Controller/Administration/UpdateController.php b/phpmyfaq/src/phpMyFAQ/Controller/Administration/UpdateController.php index 10ed6d2790..e829bf5123 100644 --- a/phpmyfaq/src/phpMyFAQ/Controller/Administration/UpdateController.php +++ b/phpmyfaq/src/phpMyFAQ/Controller/Administration/UpdateController.php @@ -48,6 +48,7 @@ public function healthCheck(): JsonResponse $dateLastChecked = $dateTime->format(DateTimeInterface::ATOM); $upgrade = new Upgrade(new System(), $configuration); + try { $upgrade->checkFilesystem(); $response->setStatusCode(Response::HTTP_OK); @@ -178,7 +179,7 @@ public function downloadPackage(Request $request): JsonResponse return $response; } - + #[Route('admin/api/extract-package')] public function extractPackage(): JsonResponse { $response = new JsonResponse(); @@ -194,4 +195,23 @@ public function extractPackage(): JsonResponse return $response; } + + #[Route('admin/api/install-package')] + public function installPackage(): JsonResponse + { + $response = new JsonResponse(); + $configuration = Configuration::getConfigurationInstance(); + + $backupHash = md5(uniqid()); + + $upgrade = new Upgrade(new System(), $configuration); + + $result = $upgrade->createTemporaryBackup($backupHash . '.zip'); + + //$result = $upgrade->installPackage(); + + $response->setData(['result' => $result]); + + return $response; + } } diff --git a/phpmyfaq/src/phpMyFAQ/Setup/Upgrade.php b/phpmyfaq/src/phpMyFAQ/Setup/Upgrade.php index cb3ccf6e6f..88a736a50c 100644 --- a/phpmyfaq/src/phpMyFAQ/Setup/Upgrade.php +++ b/phpmyfaq/src/phpMyFAQ/Setup/Upgrade.php @@ -212,7 +212,8 @@ public function extractPackage(string $path): bool public function createTemporaryBackup(string $backupName): bool { $outputZipFile = PMF_CONTENT_DIR . '/upgrades/' . $backupName; - if (!file_exists($outputZipFile)) { + + if (file_exists($outputZipFile)) { return false; } @@ -221,7 +222,7 @@ public function createTemporaryBackup(string $backupName): bool return false; } - $sourceDir = rtrim(PMF_ROOT_DIR, '/\\'); + $sourceDir = PMF_ROOT_DIR; $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($sourceDir), RecursiveIteratorIterator::SELF_FIRST