add methods to page API, cleaned up api returns

This commit is contained in:
Ro 2021-06-15 12:38:33 -07:00
commit 321903643a
2 changed files with 102 additions and 5 deletions

View file

@ -1,5 +1,6 @@
<?php <?php
use function _\filter; use function _\filter;
use Mni\FrontYAML\Parser;
class PagesAPI class PagesAPI
{ {
@ -10,20 +11,78 @@ class PagesAPI
public static function getPageContent($request, $args) public static function getPageContent($request, $args)
{ {
$task = $args["fourth"]; $task = $args["fourth"];
$content = (new Book("../content/pages"))->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) { switch ($task) {
case "published": case "published":
//$pageNum = $args["fifth"]; not not needed but gonna keep remnant in case it becomes useful/needed
$published = filter($content, function ($item) { $published = filter($content, function ($item) {
return $item["published"] == true && $item["deleted"] == false; return $item["published"] == true && $item["deleted"] == false;
}); });
$result = ["pages" => $published, "totalPages" => count($published)]; $result = ["pages" => $published, "totalItems" => count($published)];
//$result = (new Book("../content/pages"))->getPages($pageNum, 4, $task); 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; break;
case "single": case "single":
$uuid = $args["fifth"]; $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; break;
case "tags": case "tags":
$result = Settings::getTags(); $result = Settings::getTags();

View file

@ -1,6 +1,7 @@
<?php <?php
use ReallySimpleJWT\Token; use ReallySimpleJWT\Token;
use ReallySimpleJWT\Exception\BuildException; use ReallySimpleJWT\Exception\BuildException;
use Mni\FrontYAML\Parser;
//include "brain/data/Auth.inc.php"; //include "brain/data/Auth.inc.php";
class StringTools class StringTools
@ -24,6 +25,43 @@ class StringTools
); );
} }
public static function sanitizeContent($entry)
{
$parser = new Parser();
$rendered = $parser->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) public static function safeString($string)
{ {
return strtolower( return strtolower(