From bc7b1fe7ec9f01e72ddf990bb6fbfd77b8fcde2f Mon Sep 17 00:00:00 2001 From: ro Date: Tue, 2 Jul 2024 17:09:27 -0600 Subject: [PATCH] FEATURE: Restore to default plugged in a new feature that will allow the site to be reset to its default state, clearing out all content and configurations to start fresh --- app/Helpers/DirectoryHelpers.php | 8 ++- .../Controllers/API/InitAPIController.php | 13 +++- app/Providers/FipamoServiceProvider.php | 5 ++ app/Services/Upkeep/ResetService.php | 70 +++++++++++++++++++ .../app/controllers/MaintenanceManager.js | 24 +++++++ .../dash/app/controllers/SettingsIndex.js | 25 +++++++ resources/views/back/settings.blade.php | 4 +- routes/api.php | 2 + 8 files changed, 144 insertions(+), 7 deletions(-) create mode 100644 app/Services/Upkeep/ResetService.php diff --git a/app/Helpers/DirectoryHelpers.php b/app/Helpers/DirectoryHelpers.php index b9bcb24..d8c5384 100644 --- a/app/Helpers/DirectoryHelpers.php +++ b/app/Helpers/DirectoryHelpers.php @@ -1,6 +1,6 @@ init = $initService; + $this->init = $initService; + $this->reset = $resetService; } //init stuff @@ -27,4 +30,10 @@ class InitAPIController extends Controller $result = $this->init->restore($request); return response()->json($result)->header('Content-Type', 'application/json'); } + + public function setupReset(Request $request) + { + $result = $this->reset->site($request); + return response()->json($result)->header('Content-Type', 'application/json'); + } } diff --git a/app/Providers/FipamoServiceProvider.php b/app/Providers/FipamoServiceProvider.php index b1c170b..dd4433c 100644 --- a/app/Providers/FipamoServiceProvider.php +++ b/app/Providers/FipamoServiceProvider.php @@ -22,6 +22,7 @@ use App\Services\Data\SortingService; //Upkeep Services use App\Services\Upkeep\MaintenanceService; use App\Services\Upkeep\InitService; +use App\Services\Upkeep\ResetService; class FipamoServiceProvider extends ServiceProvider { @@ -92,6 +93,10 @@ class FipamoServiceProvider extends ServiceProvider $this->app->bind(InitService::class, function ($app) { return new InitService(new DocService()); }); + + $this->app->bind(ResetService::class, function ($app) { + return new ResetService(); + }); } /** diff --git a/app/Services/Upkeep/ResetService.php b/app/Services/Upkeep/ResetService.php new file mode 100644 index 0000000..4926bcb --- /dev/null +++ b/app/Services/Upkeep/ResetService.php @@ -0,0 +1,70 @@ +clearPublicAssets(); + $this->clearPublicRoot(); + $this->clearContent(); + session()->flush(); + $response = [ + 'message' => "PUBLIC CLEARED", + 'type' => "COOL", + ]; + } catch (\Throwable $e) { + $response = [ + 'message' => "RESET NOT COMPLETED", + 'error' => $e, + 'type' => "ERROR", + ]; + } + + return $response; + } + + private function clearContent() + { + $contentDir = env('FIPAMO_DIR'); + delete_directory($contentDir . '/pages', false); + delete_directory($contentDir . '/config', false); + } + + private function clearPublicAssets() + { + delete_directory('../public/assets/docs', false); + delete_directory('../public/assets/video', false); + delete_directory('../public/assets/css/theme', false); + delete_directory('../public/assets/scripts/theme', false); + delete_directory('../public/assets/images/blog', false); + delete_directory('../public/assets/images/user', false); + } + + private function clearPublicRoot() + { + $publicItems = glob('../public/*'); + $response = []; + foreach ($publicItems as $path) { + $item = explode('/', $path); + if (in_array($item[2], $this->protectedItems)) { + //protected item do nothing + } else { + if (is_file($path)) { + unlink($path); + } else { + delete_directory($path); + } + } + } + } +} diff --git a/public/assets/scripts/dash/app/controllers/MaintenanceManager.js b/public/assets/scripts/dash/app/controllers/MaintenanceManager.js index c8e648e..bc4ba28 100644 --- a/public/assets/scripts/dash/app/controllers/MaintenanceManager.js +++ b/public/assets/scripts/dash/app/controllers/MaintenanceManager.js @@ -10,6 +10,7 @@ export const CONTENT_TYPE_FORM = 'x-www-form-urlencoded'; export const API_STATUS = '/api/v1/status'; export const API_INIT = '/api/v1/init'; export const API_RESTORE = '/api/v1/restore'; +export const API_RESET = '/api/v1/reset'; export const API_GET_SECRET = '/api/v1/get-secret'; export const API_RESET_PASS = '/api/v1/reset-password'; export const API_CREATE_BACKUP = '/api/v1/backup/create'; @@ -123,6 +124,29 @@ class MaintenanceManager { }); }); } + + /** + * Promise method for restoring site to default state + */ + reset(data) { + return new Promise((resolve, reject) => { + this._request( + API_RESET, + null, + TASK_SITE_INIT, + REQUEST_TYPE_POST, + CONTENT_TYPE_JSON, + data + ) + .then(result => { + resolve(result); + }) + .catch(err => { + reject(err); + }); + }); + } + /** * Promise method for creating a zip back up of current site. For local use only. */ diff --git a/public/assets/scripts/dash/app/controllers/SettingsIndex.js b/public/assets/scripts/dash/app/controllers/SettingsIndex.js index 9db189e..1bdb82e 100644 --- a/public/assets/scripts/dash/app/controllers/SettingsIndex.js +++ b/public/assets/scripts/dash/app/controllers/SettingsIndex.js @@ -90,6 +90,31 @@ export default class SettingsIndex { } }); + //handle site reset + document.getElementById('reset-to-default').addEventListener('click', e => { + if ( + confirm( + 'You are about to restore the site to its default state!\n' + + 'This cannot be undone, so please confirm.' + ) + ) { + this.mm + .reset() + .then(r => { + if (r.type == 'COOL') { + window.location = '/'; + } else { + notify.alert(r.message, true); + } + }) + .catch(() => { + //console.log(err) + }); + } else { + // Do nothing! + } + }); + //handle tabs let tabBtn = document.querySelectorAll('.tab-button'); let tabs = document.querySelectorAll('.section-tab'); diff --git a/resources/views/back/settings.blade.php b/resources/views/back/settings.blade.php index 1248266..83e4df2 100644 --- a/resources/views/back/settings.blade.php +++ b/resources/views/back/settings.blade.php @@ -117,7 +117,6 @@ COMING SOON --> - s
diff --git a/routes/api.php b/routes/api.php index 6b9e2f8..f6532d1 100644 --- a/routes/api.php +++ b/routes/api.php @@ -35,5 +35,7 @@ Route::get("/v1/backup/download", [SettingsAPIController::class, 'downloadBackup //init Route::post("/v1/init", [InitAPIController::class, 'setupFresh']); Route::post("/v1/restore", [InitAPIController::class, 'setupRestore']); +Route::post("/v1/reset", [InitAPIController::class, 'setupReset']); + //mail Route::post("/v1/mailer", [MailAPIController::class, 'sendNotify']);