fix for open graph image render, minor tweaks to class importing

This commit is contained in:
Are0h 2022-04-14 14:46:17 -07:00
parent 7393e4572c
commit 2e30d6eb26
5 changed files with 147 additions and 138 deletions

View file

@ -14,13 +14,14 @@ class MailerAPI
public static function handleMail($request, $body, $response) public static function handleMail($request, $body, $response)
{ {
// if testing, verify session is active // if testing, verify session is active
if ($body["mail_task"] == "TESTING") { // add clean method for sending programmtic emails
if ($body['mail_task'] == 'TESTING') {
if (Session::active()) { if (Session::active()) {
$result = Mailer::sendmail($body); $result = Mailer::sendmail($body);
} else { } else {
$result = [ $result = [
"message" => "You need to be logged in for this, champ.", 'message' => 'You need to be logged in for this, champ.',
"type" => "MAILER_ERROR", 'type' => 'MAILER_ERROR',
]; ];
} }
} else { } else {

View file

@ -2,13 +2,11 @@
namespace brain\init; namespace brain\init;
use Psr\Http\Message\ResponseInterface as Response; use brain\utility\HandleCors;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory; use Slim\Factory\AppFactory;
use Slim\Views\Twig; use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
// Fipamo Core Classes // Fipamo Core Classes
use brain\utility\HandleCors; use Slim\Views\TwigMiddleware;
class App class App
{ {
@ -18,15 +16,15 @@ class App
// set up cors // set up cors
new HandleCors(); new HandleCors();
$app = AppFactory::create(); $app = AppFactory::create();
$twig = Twig::create("../brain/views/"); $twig = Twig::create('../brain/views/');
$app->add(TwigMiddleware::create($app, $twig)); $app->add(TwigMiddleware::create($app, $twig));
// set up routing // set up routing
$app->get( $app->get(
"/[{first}[/{second}[/{third}[/{fourth}[/{fifth}]]]]]", '/[{first}[/{second}[/{third}[/{fourth}[/{fifth}]]]]]',
"brain\controller\RouteControl:get" "brain\controller\RouteControl:get"
); );
$app->post( $app->post(
"/[{first}[/{second}[/{third}[/{fourth}]]]]", '/[{first}[/{second}[/{third}[/{fourth}]]]]',
"brain\controller\RouteControl:post" "brain\controller\RouteControl:post"
); );
// start the app // start the app

View file

@ -2,34 +2,34 @@
namespace brain\utility; namespace brain\utility;
use Mni\FrontYAML\Parser; use function _\filter;
use function _\find;
use brain\data\Book; use brain\data\Book;
use brain\data\Settings; use brain\data\Settings;
use Mni\FrontYAML\Parser;
use function _\find;
use function _\filter;
class Sorting class Sorting
{ {
private static $_tags = []; private static $_tags = [];
private static $_archive = []; private static $_archive = [];
public function __construct() public function __construct()
{ {
} }
public static function tags() public static function tags()
{ {
$pages = (new Book("../content/pages"))->getContents(); $pages = (new Book('../content/pages'))->getContents();
foreach ($pages as $page) { foreach ($pages as $page) {
$temp = []; $temp = [];
$temp = explode(",", $page["tags"]); $temp = explode(',', $page['tags']);
foreach ($temp as $tag) { foreach ($temp as $tag) {
$label = trim($tag); $label = trim($tag);
if (!find(self::$_tags, ["tag_name" => $label])) { if (!find(self::$_tags, ['tag_name' => $label])) {
array_push(self::$_tags, [ array_push(self::$_tags, [
"tag_name" => $label, 'tag_name' => $label,
"slug" => StringTools::safeString($label), 'slug' => StringTools::safeString($label),
"pages" => self::tagPages($label, $pages), 'pages' => self::tagPages($label, $pages),
]); ]);
} }
} }
@ -42,12 +42,12 @@ class Sorting
{ {
$tagged = []; $tagged = [];
foreach ($pages as $page) { foreach ($pages as $page) {
if (strpos($page["tags"], $tag) !== false) { if (strpos($page['tags'], $tag) !== false) {
array_push($tagged, [ array_push($tagged, [
"title" => $page["title"], 'title' => $page['title'],
"slug" => $page["slug"], 'slug' => $page['slug'],
"path" => $page["path"], 'path' => $page['path'],
"feature" => $page["feature"], 'feature' => $page['feature'],
]); ]);
} }
} }
@ -57,48 +57,49 @@ class Sorting
public static function archive() public static function archive()
{ {
$pages = (new Book("../content/pages"))->getContents(); $pages = (new Book('../content/pages'))->getContents();
$years = []; $years = [];
$archive = []; $archive = [];
foreach ($pages as $page) { foreach ($pages as $page) {
// $year = date("Y", date($page["rawCreated"])); // $year = date("Y", date($page["rawCreated"]));
$date = explode("/", $page["path"]); $date = explode('/', $page['path']);
// echo $page["title"] . " : " . $year . "\n"; // echo $page["title"] . " : " . $year . "\n";
if (!find($years, ["year" => trim($date[0])])) { if (!find($years, ['year' => trim($date[0])])) {
$findPages = filter($pages, ["createdYear" => trim($date[0])]); $findPages = filter($pages, ['createdYear' => trim($date[0])]);
// var_dump($findPages); // var_dump($findPages);
array_push($years, [ array_push($years, [
"year" => trim($date[0]), 'year' => trim($date[0]),
"count" => count($findPages), 'count' => count($findPages),
]); ]);
} }
} }
foreach ($years as $year) { foreach ($years as $year) {
$sorted = []; $sorted = [];
$filtered = filter($pages, ["createdYear" => $year["year"]]); $filtered = filter($pages, ['createdYear' => $year['year']]);
foreach ($filtered as $obj) { foreach ($filtered as $obj) {
$month = date("m", date($obj["rawCreated"])); $month = date('m', date($obj['rawCreated']));
if (!find($sorted, ["month" => $month])) { if (!find($sorted, ['month' => $month])) {
$perMonth = filter($pages, [ $perMonth = filter($pages, [
"path" => $year["year"] . "/" . $month, 'path' => $year['year'].'/'.$month,
"deleted" => false, 'deleted' => false,
"published" => true, 'published' => true,
"layout" => "page", 'layout' => 'page',
]); ]);
array_push($sorted, [ array_push($sorted, [
"month" => $month, 'month' => $month,
"full_month" => date("F", date($obj["rawCreated"])), 'full_month' => date('F', date($obj['rawCreated'])),
"count" => count($perMonth), 'count' => count($perMonth),
"pages" => $perMonth, 'pages' => $perMonth,
]); ]);
} }
} }
array_push(self::$_archive, [ array_push(self::$_archive, [
"year" => $year["year"], 'year' => $year['year'],
"year_data" => $sorted, 'year_data' => $sorted,
]); ]);
} }
return self::$_archive; return self::$_archive;
} }
@ -109,40 +110,39 @@ class Sorting
$pageOption = []; $pageOption = [];
$pageInfo = [ $pageInfo = [
"keywords" => isset($settings["global"]["keywords"]) 'keywords' => isset($settings['global']['keywords'])
? $settings["global"]["keywords"] ? $settings['global']['keywords']
: "fipamo, blog, jamstack, php, markdown, js", : 'fipamo, blog, jamstack, php, markdown, js',
"description" => $settings["global"]["descriptions"], 'description' => $settings['global']['descriptions'],
"image" => 'image' => $settings['global']['base_url'].$settings['global']['background'],
$settings["global"]["base_url"] . $settings["global"]["background"], 'baseURL' => $settings['global']['base_url'],
"baseURL" => $settings["global"]["base_url"],
]; ];
$taglist = explode(",", $page["tags"]); $taglist = explode(',', $page['tags']);
$tags = []; $tags = [];
foreach ($taglist as $tag) { foreach ($taglist as $tag) {
$label = trim($tag); $label = trim($tag);
array_push($tags, [ array_push($tags, [
"label" => $label . " ", 'label' => $label.' ',
"slug" => StringTools::safeString($label), 'slug' => StringTools::safeString($label),
]); ]);
} }
$meta = [ $meta = [
"who" => $page["author"], 'who' => $page['author'],
"when" => $page["created"], 'when' => $page['created'],
"tags" => $tags, 'tags' => $tags,
]; ];
// render markdown content and clean it // render markdown content and clean it
$parser = new Parser(); $parser = new Parser();
$rendered = $parser->parse($page["content"]); $rendered = $parser->parse($page['content']);
$sanitizer = \HtmlSanitizer\Sanitizer::create([ $sanitizer = \HtmlSanitizer\Sanitizer::create([
"extensions" => ["basic", "image", "list", "code"], 'extensions' => ['basic', 'image', 'list', 'code'],
"tags" => [ 'tags' => [
"img" => [ 'img' => [
"allowed_attributes" => ["src", "alt", "title", "class"], 'allowed_attributes' => ['src', 'alt', 'title', 'class'],
"allowed_hosts" => null, 'allowed_hosts' => null,
], ],
], ],
]); ]);
@ -152,32 +152,41 @@ class Sorting
// just clean renderd string for now, Sanitize doesn't like relative img urls // just clean renderd string for now, Sanitize doesn't like relative img urls
// so another option is needed // so another option is needed
$cleaned = strip_tags($rendered->getContent(), [ $cleaned = strip_tags($rendered->getContent(), [
"a", 'a',
"br", 'br',
"p", 'p',
"strong", 'strong',
"br", 'br',
"img", 'img',
"iframe", 'iframe',
"ul", 'ul',
"li", 'li',
"i", 'i',
"em", 'em',
"h1", 'h1',
"h2", 'h2',
"h3", 'h3',
"pre", 'pre',
"code", 'code',
]); ]);
//$cleaned = preg_replace('/(?:\r\n|[\r\n]){2,}/', "\n\n", $cleaned);
//$cleaned = html_entity_decode($cleaned, ENT_QUOTES, "UTF-8");
//if page feature isn't empty, replace page info meta image // if page feature isn't empty, find image from list and set it as background image
if ($page["feature"] != "" || $page["feature"] != null) { // if it is empty, just use global background
$pageInfo["image"] = $pageInfo["baseURL"] . $page["feature"]; if ($page['feature'] != '' || $page['feature'] != null) {
$media = explode(',', $page['feature']);
$set = false;
foreach ($media as $file) {
$item = trim($file);
$ext = pathinfo($item, PATHINFO_EXTENSION);
if ($ext != 'mp4' && !$set) {
$pageInfo['image'] = $pageInfo['baseURL'].$item;
$set = true;
}
}
} }
if ($page["layout"] == "index") { if ($page['layout'] == 'index') {
// $template = $this->theme . "/index.twig"; // $template = $this->theme . "/index.twig";
// $location = "../public/index.html"; // $location = "../public/index.html";
// $dir = null; // $dir = null;
@ -188,26 +197,26 @@ class Sorting
$pages = (new Book())->getContents(); $pages = (new Book())->getContents();
foreach ($pages as $item) { foreach ($pages as $item) {
if ( if (
!$item["deleted"] && !$item['deleted'] &&
$item["published"] && $item['published'] &&
$item["menu"] != "true" $item['menu'] != 'true'
) { ) {
if (count($recent) < $limit) { if (count($recent) < $limit) {
array_push($recent, [ array_push($recent, [
"path" => $item["path"], 'path' => $item['path'],
"slug" => $item["slug"], 'slug' => $item['slug'],
"title" => $item["title"], 'title' => $item['title'],
"feature" => $item["feature"], 'feature' => $item['feature'],
]); ]);
} }
if ($item["featured"] == true) { if ($item['featured'] == true) {
if (count($featured) < $limit) { if (count($featured) < $limit) {
array_push($featured, [ array_push($featured, [
"path" => $item["path"], 'path' => $item['path'],
"slug" => $item["slug"], 'slug' => $item['slug'],
"title" => $item["title"], 'title' => $item['title'],
"feature" => $item["feature"], 'feature' => $item['feature'],
]); ]);
} }
} }
@ -215,34 +224,35 @@ class Sorting
} }
$pageOptions = [ $pageOptions = [
"title" => $page["title"], 'title' => $page['title'],
"background" => $page["feature"], 'background' => $page['feature'],
"content" => $page["html"], //$cleaned, 'content' => $page['html'], // $cleaned,
"meta" => $meta, 'meta' => $meta,
"recent" => $recent, 'recent' => $recent,
"featured" => $featured, 'featured' => $featured,
"info" => $pageInfo, 'info' => $pageInfo,
"menu" => $settings["menu"], 'menu' => $settings['menu'],
"dynamicRender" => $settings["global"]["dynamicRender"], 'dynamicRender' => $settings['global']['dynamicRender'],
"media" => $page["media"], 'media' => $page['media'],
"files" => $page["docs"], 'files' => $page['docs'],
]; ];
} else { } else {
// $template = $this->theme . "/page.twig"; // $template = $this->theme . "/page.twig";
// $location = "../public/" . $page["path"] . "/" . $page["slug"] . ".html"; // $location = "../public/" . $page["path"] . "/" . $page["slug"] . ".html";
// $dir = "../public/" . $page["path"]; // $dir = "../public/" . $page["path"];
$pageOptions = [ $pageOptions = [
"title" => $page["title"], 'title' => $page['title'],
"background" => $page["feature"], 'background' => $page['feature'],
"content" => $page["html"], //$cleaned, 'content' => $page['html'], // $cleaned,
"meta" => $meta, 'meta' => $meta,
"info" => $pageInfo, 'info' => $pageInfo,
"menu" => $settings["menu"], 'menu' => $settings['menu'],
"dynamicRender" => $settings["global"]["dynamicRender"], 'dynamicRender' => $settings['global']['dynamicRender'],
"media" => $page["media"], 'media' => $page['media'],
"files" => $page["docs"], 'files' => $page['docs'],
]; ];
} }
return $pageOptions; return $pageOptions;
} }
} }

View file

@ -1,7 +1,7 @@
<?php <?php
require "../vendor/autoload.php"; require '../vendor/autoload.php';
use brain\init\App as App; use brain\init\App;
new App(); new App();