From 06787aac30616df3aac823a04414c8ef74e51f18 Mon Sep 17 00:00:00 2001 From: Ro Date: Sun, 25 Apr 2021 21:18:01 -0700 Subject: [PATCH] moved site restore to utility class, updated set up script, fixed minor error in front end api restore request --- brain/App.inc.php | 1 + brain/api/v1/SettingsAPI.inc.php | 2 +- brain/data/Settings.inc.php | 71 ---------------------- brain/utility/Maintenance.inc.php | 99 +++++++++++++++++++++++++++++++ brain/utility/Setup.inc.php | 11 +++- src/com/Base.js | 2 +- 6 files changed, 111 insertions(+), 75 deletions(-) create mode 100644 brain/utility/Maintenance.inc.php diff --git a/brain/App.inc.php b/brain/App.inc.php index ec8dca7..33dd18e 100644 --- a/brain/App.inc.php +++ b/brain/App.inc.php @@ -17,6 +17,7 @@ include "../brain/utility/FileUploader.inc.php"; include "../brain/utility/DocTools.inc.php"; include "../brain/utility/Sorting.inc.php"; include "../brain/utility/Setup.inc.php"; +include "../brain/utility/Maintenance.inc.php"; class App { diff --git a/brain/api/v1/SettingsAPI.inc.php b/brain/api/v1/SettingsAPI.inc.php index f8fa99b..da6394e 100644 --- a/brain/api/v1/SettingsAPI.inc.php +++ b/brain/api/v1/SettingsAPI.inc.php @@ -57,7 +57,7 @@ class SettingsAPI public static function createBackup() { - $result = Settings::makeBackup(); + $result = Maintenance::makeBackup(); return $result; } } diff --git a/brain/data/Settings.inc.php b/brain/data/Settings.inc.php index 535a5a9..4917c1e 100644 --- a/brain/data/Settings.inc.php +++ b/brain/data/Settings.inc.php @@ -168,77 +168,6 @@ class Settings DocTools::writeSettings("../config/settings.json", $settings); } - public static function makeBackup() - { - //make sure back directory is there - if (!is_dir("../config/backups")) { - mkdir("../config/backups", 0755, true); - } - //creat backup zip - $zip = new ZipArchive(); - $zip->open( - "../config/backups/latest_back.zip", - ZipArchive::CREATE | ZipArchive::OVERWRITE - ); - //gather data and path info for md pages - $pagePath = "../content/pages"; - $yearPaths = glob($pagePath . "/*", GLOB_ONLYDIR); - foreach ($yearPaths as $years) { - $year = explode("/", $years); - //grap the index and save it - if (trim($year[3]) == "start") { - $options = [ - "add_path" => "content/pages/" . $year[3] . "/", - "remove_all_path" => true, - ]; - $zip->addGlob($years . "/*.md", GLOB_BRACE, $options); - } - $monthsPath = glob($pagePath . "/" . $year[3] . "/*", GLOB_ONLYDIR); - foreach ($monthsPath as $months) { - $month = explode("/", $months); - //once info is collected, add md pages to zip - $options = [ - "add_path" => "content/pages/" . $year[3] . "/" . $month[4] . "/", - "remove_all_path" => true, - ]; - $zip->addGlob($months . "/*.md", GLOB_BRACE, $options); - } - } - - //tather data and path info for blog images - $blogImagesPath = "../public/assets/images/blog"; - $yearPaths = glob($blogImagesPath . "/*", GLOB_ONLYDIR); - foreach ($yearPaths as $years) { - $year = explode("/", $years); - $monthsPath = glob($blogImagesPath . "/" . $year[5] . "/*", GLOB_ONLYDIR); - foreach ($monthsPath as $months) { - $month = explode("/", $months); - //once info is collected, add images pages to zip - $options = [ - "add_path" => - "public/assets/images/blog/" . $year[5] . "/" . $month[6] . "/", - "remove_all_path" => true, - ]; - $zip->addGlob($months . "/*.*", GLOB_BRACE, $options); - } - } - - //add directory for settings and save them - $zip->addEmptyDir("settings"); - $zip->addFile("../config/settings.json", "settings/settings.json"); - $zip->addFile("../config/folks.json", "settings/folks.json"); - $zip->addFile("../config/tags.json", "settings/tags.json"); - //save zip file - $zip->close(); - - //update settings file with latest back up date - $updated = new \Moment\Moment(); - self::updateGlobalData("last_backup", $updated->format("Y-m-d\TH:i:sP")); - - $result = ["message" => "Backup created. THIS IS A SAFE SPACE!"]; - return $result; - } - public static function updateTags() { $tags = Sorting::tags(); diff --git a/brain/utility/Maintenance.inc.php b/brain/utility/Maintenance.inc.php new file mode 100644 index 0000000..2697e3e --- /dev/null +++ b/brain/utility/Maintenance.inc.php @@ -0,0 +1,99 @@ +open( + "../config/backups/latest_back.zip", + ZipArchive::CREATE | ZipArchive::OVERWRITE + ); + //gather data and path info for md pages + $pagePath = "../content/pages"; + $yearPaths = glob($pagePath . "/*", GLOB_ONLYDIR); + foreach ($yearPaths as $years) { + $year = explode("/", $years); + //grap the index and save it + if (trim($year[3]) == "start") { + $options = [ + "add_path" => "content/pages/" . $year[3] . "/", + "remove_all_path" => true, + ]; + $zip->addGlob($years . "/*.md", GLOB_BRACE, $options); + } + $monthsPath = glob($pagePath . "/" . $year[3] . "/*", GLOB_ONLYDIR); + foreach ($monthsPath as $months) { + $month = explode("/", $months); + //once info is collected, add md pages to zip + $options = [ + "add_path" => "content/pages/" . $year[3] . "/" . $month[4] . "/", + "remove_all_path" => true, + ]; + $zip->addGlob($months . "/*.md", GLOB_BRACE, $options); + } + } + + //gather data and path info for blog images + $blogImagesPath = "../public/assets/images/blog"; + $yearPaths = glob($blogImagesPath . "/*", GLOB_ONLYDIR); + foreach ($yearPaths as $years) { + $year = explode("/", $years); + $monthsPath = glob($blogImagesPath . "/" . $year[5] . "/*", GLOB_ONLYDIR); + foreach ($monthsPath as $months) { + $month = explode("/", $months); + //once info is collected, add images pages to zip + $options = [ + "add_path" => + "public/assets/images/blog/" . $year[5] . "/" . $month[6] . "/", + "remove_all_path" => true, + ]; + $zip->addGlob($months . "/*.*", GLOB_BRACE, $options); + } + } + + //gather data and path info for user images + $userImagesPath = "../public/assets/images/user"; + $yearPaths = glob($userImagesPath . "/*", GLOB_ONLYDIR); + foreach ($yearPaths as $years) { + $year = explode("/", $years); + $monthsPath = glob($userImagesPath . "/" . $year[5] . "/*", GLOB_ONLYDIR); + foreach ($monthsPath as $months) { + $month = explode("/", $months); + //once info is collected, add images pages to zip + $options = [ + "add_path" => + "public/assets/images/user/" . $year[5] . "/" . $month[6] . "/", + "remove_all_path" => true, + ]; + $zip->addGlob($months . "/*.*", GLOB_BRACE, $options); + } + } + + //add directory for settings and save them + $zip->addEmptyDir("settings"); + $zip->addFile("../config/settings.json", "settings/settings.json"); + $zip->addFile("../config/folks.json", "settings/folks.json"); + $zip->addFile("../config/tags.json", "settings/tags.json"); + //save zip file + $zip->close(); + + //update settings file with latest back up date + $updated = new \Moment\Moment(); + Settings::updateGlobalData( + "last_backup", + $updated->format("Y-m-d\TH:i:sP") + ); + + $result = ["message" => "Backup created. THIS IS A SAFE SPACE!"]; + return $result; + } +} diff --git a/brain/utility/Setup.inc.php b/brain/utility/Setup.inc.php index a31c2f2..05d2cfa 100644 --- a/brain/utility/Setup.inc.php +++ b/brain/utility/Setup.inc.php @@ -142,6 +142,11 @@ class SetUp "../public/assets/images/blog" ); + rename( + "../content/public/assets/images/user", + "../public/assets/images/user" + ); + rename("../content/content/pages/", "../content/pages"); //legacy check for old file structure @@ -164,8 +169,10 @@ class SetUp DocTools::deleteFolder("../content/settings"); DocTools::deleteFolder("../content/public"); DocTools::deleteFolder("../content/content"); - - echo "AUTH VERIFIED"; + $result = [ + "type" => "requestGood", + "message" => "Site Restored! Redirecting", + ]; } else { $result = [ "type" => "requestLame", diff --git a/src/com/Base.js b/src/com/Base.js index 7ca7585..6e1328c 100644 --- a/src/com/Base.js +++ b/src/com/Base.js @@ -97,7 +97,7 @@ export default class Base { e.preventDefault(); let api = new FipamoApi(); var form = document.getElementById("init-restore"); - admin + api .handleInitRestore(form) .then((response) => { if (response.type === DataEvent.REQUEST_LAME) {