From 2d84ec9e331eb7697f0700a2bef853483c79216e Mon Sep 17 00:00:00 2001 From: Ro Date: Tue, 15 Jun 2021 12:37:27 -0700 Subject: [PATCH] added featured and menu options for page API, cleaned up API json return --- brain/api/v1/PagesAPI.inc.php | 69 ++++++++++++++++++++++++++++--- brain/utility/StringTools.inc.php | 38 +++++++++++++++++ 2 files changed, 102 insertions(+), 5 deletions(-) diff --git a/brain/api/v1/PagesAPI.inc.php b/brain/api/v1/PagesAPI.inc.php index 2786efc..76f3d63 100644 --- a/brain/api/v1/PagesAPI.inc.php +++ b/brain/api/v1/PagesAPI.inc.php @@ -1,5 +1,6 @@ getContents(); + $pages = (new Book("../content/pages"))->getContents(); + $content = []; + foreach ($pages as $page) { + $entry = [ + "id" => $page["id"], + "uuid" => $page["uuid"], + "title" => $page["title"], + "feature" => $page["feature"], + "path" => $page["path"], + "layout" => $page["layout"], + "tags" => $page["tags"], + "author" => $page["author"], + "created" => $page["created"], + "updated" => $page["updated"], + "deleted" => $page["deleted"], + "menu" => $page["menu"], + "featured" => $page["featured"], + "published" => $page["published"], + "slug" => $page["slug"], + "content" => StringTools::sanitizeContent($page["content"]), + ]; + + array_push($content, $entry); + } switch ($task) { case "published": - //$pageNum = $args["fifth"]; not not needed but gonna keep remnant in case it becomes useful/needed $published = filter($content, function ($item) { return $item["published"] == true && $item["deleted"] == false; }); - $result = ["pages" => $published, "totalPages" => count($published)]; - //$result = (new Book("../content/pages"))->getPages($pageNum, 4, $task); + $result = ["pages" => $published, "totalItems" => count($published)]; + break; + case "featured": + $featured = filter($content, function ($item) { + return $item["featured"] == true && $item["deleted"] == false; + }); + + $result = [ + "pages" => $featured, + "totalItems" => count($featured), + ]; + break; + case "menu": + $menu = filter($content, function ($item) { + return $item["menu"] == true && $item["deleted"] == false; + }); + + $result = ["pages" => $menu, "totalItems" => count($menu)]; break; case "single": $uuid = $args["fifth"]; - $result = (new Book("../content/pages"))->findPageById($uuid); + $page = (new Book("../content/pages"))->findPageById($uuid); + + $entry = [ + "id" => $page["id"], + "uuid" => $page["uuid"], + "title" => $page["title"], + "feature" => $page["feature"], + "path" => $page["path"], + "layout" => $page["layout"], + "tags" => $page["tags"], + "author" => $page["author"], + "created" => $page["created"], + "updated" => $page["updated"], + "deleted" => $page["deleted"], + "menu" => $page["menu"], + "featured" => $page["featured"], + "published" => $page["published"], + "slug" => $page["slug"], + "content" => StringTools::sanitizeContent($page["content"]), + ]; + $result = $entry; break; case "tags": $result = Settings::getTags(); diff --git a/brain/utility/StringTools.inc.php b/brain/utility/StringTools.inc.php index 68d81cd..2c16ae7 100644 --- a/brain/utility/StringTools.inc.php +++ b/brain/utility/StringTools.inc.php @@ -1,6 +1,7 @@ parse($entry); + $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($rendered->getContent(), [ + "a", + "br", + "p", + "strong", + "br", + "img", + "iframe", + "ul", + "li", + "i", + "h1", + "h2", + "h3", + "pre", + "code", + ]); + + return $cleaned; + } + public static function safeString($string) { return strtolower(