From 9afec7554c422030e273a29332970261d4c652b7 Mon Sep 17 00:00:00 2001 From: Ro Date: Sat, 24 Apr 2021 16:37:25 -0700 Subject: [PATCH] activated site restore from zip, fixed imgs to rendering src --- brain/api/v1/InitAPI.inc.php | 1 + brain/controller/APIControl.inc.php | 8 ++- brain/data/Render.inc.php | 25 ++++++- brain/data/Settings.inc.php | 4 +- brain/utility/DocTools.inc.php | 27 ++++++++ brain/utility/Setup.inc.php | 101 +++++++++++++++++++++++++++- src/com/Base.js | 4 +- src/libraries/FipamoAPI.js | 20 ++++++ src/libraries/FipamoAdminAPI.js | 19 ------ 9 files changed, 181 insertions(+), 28 deletions(-) diff --git a/brain/api/v1/InitAPI.inc.php b/brain/api/v1/InitAPI.inc.php index 778f6e9..dfa9b0e 100644 --- a/brain/api/v1/InitAPI.inc.php +++ b/brain/api/v1/InitAPI.inc.php @@ -13,6 +13,7 @@ class InitAPI $result = Setup::init($request); break; case "restore": + $result = Setup::restore($request); break; } diff --git a/brain/controller/APIControl.inc.php b/brain/controller/APIControl.inc.php index cd09474..92a0ff7 100644 --- a/brain/controller/APIControl.inc.php +++ b/brain/controller/APIControl.inc.php @@ -40,9 +40,15 @@ class APIControl } switch (isset($args["third"]) ? $args["third"] : "none") { + case "restore": case "init": - $result = InitApi::handleInitTasks($args["third"], $body); + $task = $args["third"]; + $result = InitApi::handleInitTasks( + $task, + $task == "init" ? $body : $request + ); break; + case "login": $result = AuthAPI::login($body); break; diff --git a/brain/data/Render.inc.php b/brain/data/Render.inc.php index 3bdcb62..a67475c 100644 --- a/brain/data/Render.inc.php +++ b/brain/data/Render.inc.php @@ -16,7 +16,9 @@ class Render $settings = $config->getSettings(); $this->menu = $settings["menu"]; $this->pageInfo = [ - "keywords" => $settings["global"]["keywords"], + "keywords" => isset($settings["global"]["keywords"]) + ? $settings["global"]["keywords"] + : "fipamo, blog, jamstack, php, markdown, js", "description" => $settings["global"]["descriptions"], "image" => $settings["global"]["background"], ]; @@ -66,11 +68,21 @@ class Render //render markdown content and clean it $parser = new Parser(); $rendered = $parser->parse($page["content"]); - $sanitizer = \HtmlSanitizer\Sanitizer::create([ + $sanitizer = HtmlSanitizer\Sanitizer::create([ "extensions" => ["basic", "image", "list", "code"], + "tags" => [ + "img" => [ + "allowed_attributes" => ["src", "alt", "title", "class"], + "allowed_hosts" => null, + ], + ], ]); + $preclean = $sanitizer->sanitize($rendered->getContent()); - $cleaned = strip_tags($preclean, [ + + //just clean renderd string for now, Sanitize doesn't like relative img urls + //so another option is needed + $cleaned = strip_tags($rendered->getContent(), [ "a", "br", "p", @@ -159,6 +171,13 @@ class Render $html = $this->twig->render($template, $pageOptions); $location = "../public/tags/" . $item["slug"] . ".html"; + + //if tags folder doesn't exist, make it + if (!is_dir("../public/tags")) { + mkdir("../public/tags", 0755, true); + } else { + } + if (!is_file($location)) { file_put_contents($location, $html); } else { diff --git a/brain/data/Settings.inc.php b/brain/data/Settings.inc.php index 9ae7226..4917c1e 100644 --- a/brain/data/Settings.inc.php +++ b/brain/data/Settings.inc.php @@ -109,7 +109,9 @@ class Settings if (isset($key)) { $member = Session::get("member"); $found = find($this->folks, ["handle" => $member["handle"]]); - return $found[$key]; + if ($found) { + return $found[$key]; + } } else { return $this->folks; } diff --git a/brain/utility/DocTools.inc.php b/brain/utility/DocTools.inc.php index c64da0d..9650864 100644 --- a/brain/utility/DocTools.inc.php +++ b/brain/utility/DocTools.inc.php @@ -55,6 +55,33 @@ class DocTools } } + public static function deleteFolder($path) + { + if (!empty($path) && is_dir($path)) { + $dir = new RecursiveDirectoryIterator( + $path, + RecursiveDirectoryIterator::SKIP_DOTS + ); //upper dirs are not included,otherwise DISASTER HAPPENS :) + $files = new RecursiveIteratorIterator( + $dir, + RecursiveIteratorIterator::CHILD_FIRST + ); + foreach ($files as $f) { + if (is_file($f)) { + unlink($f); + } else { + $empty_dirs[] = $f; + } + } + if (!empty($empty_dirs)) { + foreach ($empty_dirs as $eachDir) { + rmdir($eachDir); + } + } + rmdir($path); + } + } + public static function objectToMD($object) { $markdown = diff --git a/brain/utility/Setup.inc.php b/brain/utility/Setup.inc.php index 1ce2593..a31c2f2 100644 --- a/brain/utility/Setup.inc.php +++ b/brain/utility/Setup.inc.php @@ -1,5 +1,5 @@ "requestLame", + "message" => "Still working on it.", + ]; + $body = $request->getParsedBody(); + + $backup = $request->getUploadedFiles(); + $file = $backup["backup-upload"]; + $name = $file->getClientFileName(); + + //park it so it can be read + $file->moveTo("../content" . "/" . $name); + + //open it and get files to verify user + $zip = new ZipArchive(); + if ($zip->open("../content" . "/" . $name) === true) { + $folks = json_decode($zip->getFromName("settings/folks.json"), true); + $found = find($folks, ["handle" => $body["restore_member_handle"]]); + + //if member is found in back up, check pass + if ($found) { + if (password_verify($body["restore_member_pass"], $found["password"])) { + //backup verified, restore site + + //set new secret key for older folks configs + $newFolks = []; + if (!isset($found["secret"])) { + $found["secret"] = StringTools::randomString(12); + } + array_push($newFolks, $found); + //dump files in folder + $zip->extractTo("../content"); + + //move to appropriate spots + rename( + "../content/settings/settings.json", + "../config/settings.json" + ); + + //rename("../content/settings/folks.json", "../config/folks.json"); + DocTools::writeSettings("../config/folks.json", $newFolks); + + rename("../content/settings/tags.json", "../config/tags.json"); + + rename( + "../content/public/assets/images/blog", + "../public/assets/images/blog" + ); + + rename("../content/content/pages/", "../content/pages"); + + //legacy check for old file structure + if (is_file("../content/pages/index.md")) { + if (!is_dir("../content/pages/start")) { + //Directory does not exist, so lets create it. + mkdir("../content/pages/start", 0755, true); + //move start page to appropriate spot + rename( + "../content/pages/index.md", + "../content/pages/start/index.md" + ); + } + } else { + //chill + } + + //clean up + + DocTools::deleteFolder("../content/settings"); + DocTools::deleteFolder("../content/public"); + DocTools::deleteFolder("../content/content"); + + echo "AUTH VERIFIED"; + } else { + $result = [ + "type" => "requestLame", + "message" => "Check that password, champ.", + ]; + } + } else { + $result = [ + "type" => "requestLame", + "message" => "No member found by that name, hoss", + ]; + } + + $zip->close(); + $zipPath = "../content/" . $name; + //trash zip when done + unlink($zipPath); + } else { + $result = [ + "type" => "requestLame", + "message" => "Could not open backup. RATS!", + ]; + } + return $result; } } diff --git a/src/com/Base.js b/src/com/Base.js index cb7ec9a..7ca7585 100644 --- a/src/com/Base.js +++ b/src/com/Base.js @@ -95,7 +95,7 @@ export default class Base { handleRestore(e) { e.stopPropagation(); e.preventDefault(); - let admin = new FipamoAdminAPI(); + let api = new FipamoApi(); var form = document.getElementById("init-restore"); admin .handleInitRestore(form) @@ -105,7 +105,7 @@ export default class Base { } else { notify.alert(response.message, true); setTimeout(() => { - //window.location = '/@/dashboard'; + window.location = "/dashboard"; }, 700); } }) diff --git a/src/libraries/FipamoAPI.js b/src/libraries/FipamoAPI.js index 8e092bb..014a619 100644 --- a/src/libraries/FipamoAPI.js +++ b/src/libraries/FipamoAPI.js @@ -6,6 +6,7 @@ export const CONTENT_TYPE_JSON = "json"; 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_LOGIN = "/api/v1/login"; export const API_GET_PAGES = "/api/v1/page/published"; export const API_GET_PAGE = "/api/v1/page/single"; @@ -52,6 +53,25 @@ export default class FipamoAPI { }); }); } + + handleInitRestore(form) { + return new Promise((resolve, reject) => { + var url, event, method, type, data; + + url = API_RESTORE; + event = DataEvent.API_BACKUP_RESTORE; + method = REQUEST_TYPE_POST; + type = CONTENT_TYPE_FORM; + data = new FormData(form); + this._request(url, event, method, type, data) + .then((result) => { + resolve(result); + }) + .catch((err) => { + reject(err); + }); + }); + } getPages(num) { let pageNum = num; if (pageNum === null || pageNum === "" || !pageNum) pageNum = 1; diff --git a/src/libraries/FipamoAdminAPI.js b/src/libraries/FipamoAdminAPI.js index f0c2c25..55b05d7 100644 --- a/src/libraries/FipamoAdminAPI.js +++ b/src/libraries/FipamoAdminAPI.js @@ -22,7 +22,6 @@ export const API_REINDEX_PAGES = "/api/v1/settings/reindex"; export const API_CREATE_BACKUP = "/api/v1/backup/create"; export const API_DOWNLOAD_BACKUP = "/api/v1/backup/download"; export const API_RESTORE_BACKUP = "/api/v1/backup/restore"; -export const API_INIT_RESTORE_BACKUP = "/api/v1/backup/init-restore"; export const API_SEND_MAIL = "/api/v1/mailer"; import * as DataEvent from "../com/events/DataEvent"; export default class APIUtils { @@ -227,24 +226,6 @@ export default class APIUtils { }); }); } - handleInitRestore(form) { - return new Promise((resolve, reject) => { - var url, event, method, type, data; - - url = API_INIT_RESTORE_BACKUP; - event = DataEvent.API_BACKUP_RESTORE; - method = REQUEST_TYPE_POST; - type = CONTENT_TYPE_FORM; - data = new FormData(form); - this._request(url, event, method, type, data) - .then((result) => { - resolve(result); - }) - .catch((err) => { - reject(err); - }); - }); - } handleReindex(data) { return new Promise((resolve, reject) => {