yarmo.eu/functions.php
Yarmo Mackenbach ee77a866a6
All checks were successful
continuous-integration/drone/push Build is passing
Support legacy urls for webmentions
2020-06-25 09:40:41 +02:00

236 lines
7.6 KiB
PHP

<?php
include_once __DIR__ . '/vendor/autoload.php';
use Pagerange\Markdown\MetaParsedown;
use Bhaktaraz\RSSGenerator\Item;
use Bhaktaraz\RSSGenerator\Feed;
use Bhaktaraz\RSSGenerator\Channel;
use Symfony\Component\Yaml\Yaml;
function getIcons() {
$icons = array();
$icons["github"] = file_get_contents('static/img/github.svg');
$icons["gitlab"] = file_get_contents('static/img/gitlab.svg');
$icons["gnuprivacyguard"] = file_get_contents('static/img/gnuprivacyguard.svg');
$icons["mail"] = file_get_contents('static/img/mail.svg');
$icons["mastodon"] = file_get_contents('static/img/mastodon.svg');
$icons["rss"] = file_get_contents('static/img/rss.svg');
$icons["xmpp"] = file_get_contents('static/img/xmpp.svg');
return $icons;
}
function getBlogPosts($params) {
$posts = scandirRec('content/blog/', 'blog', $params);
$posts = array_reverse($posts);
return $posts;
}
function getNotes($params) {
$posts = scandirRec('content/notes/', 'notes', $params);
$posts = array_reverse($posts);
return $posts;
}
function getProjects($params) {
$projects = scandirRec('content/projects/', 'projects', $params);
return $projects;
}
function getReplies($params) {
$projects = scandirRec('content/replies/', 'replies', $params);
return $projects;
}
function getWebmentions($target) {
$data = json_decode(file_get_contents("https://webm.yarmo.eu/get?target=$target"), true);
foreach ($data as $id => $entry) {
$time = strtotime($entry["created_at"]);
$data[$id]["date"] = date("Y-m-d",$time);
$data[$id]["time"] = date("H:i:s",$time);
}
return $data;
}
function getFOSS() {
return Yaml::parseFile('content/foss/foss.yaml');
}
function getVinyl() {
return Yaml::parseFile('content/music/vinyl.yaml');
}
function getAOTW() {
return Yaml::parseFile('content/music/aotw.yaml');
}
function getRSS($params) {
header("Content-Type: text/xml");
$feed = new Feed();
$channel = new Channel();
$channel
->title($params['feed']['title'])
->description($params['feed']['description'])
->url($params['feed']['url'])
->atomLinkSelf($params['feed']['linkself'])
->appendTo($feed);
$dates = array_column($params['posts'], 'date');
array_multisort($dates, SORT_DESC, $params['posts']);
foreach($params['posts'] as $post) {
$item = new Item();
$item
->title($post['title'])
->creator($post['author'])
// ->content($post['content'])
->description($post['content'])
->url($post['url'])
->guid($post['url'], true)
->pubDate(strtotime($post['date']))
->appendTo($channel);
}
echo $feed;
}
function scandirRec($path, $category, $params) {
$Parsedown = new Parsedown();
$mp = new MetaParsedown();
$items = array();
// If a directory
if(is_dir($path)) {
$files = glob($path . '*', GLOB_MARK);
foreach($files as $file) {
if(is_dir($file)) {
$items = array_merge($items, scandirRec($file, $category, $params));
} else {
$return = scandirRec($file, $category, $params);
if (!empty($return)) {
$items[] = $return;
}
}
}
return $items;
}
// If a file
if (($path == '.') || ($path == '..')) {
return;
}
$content = file_get_contents($path);
$meta = $mp->meta($content);
switch ($category) {
case 'blog':
$item = array(
"type" => "blog",
"title" => $meta["title"],
"author" => $meta["author"],
"urlrel" => "/post/".$meta["slug"],
"url" => "https://yarmo.eu/post/".$meta["slug"],
"slug" => $meta["slug"],
"date" => $meta["date"],
"published" => $meta["published"],
"discussion" => $meta["discussion"],
"content" => $mp->text($content));
$date = new DateTime($item['date']);
$item['date_formatted'] = $date->format('Y-m-d');
$item['date_rss'] = $date->format('Y-m-d');
if (!$item['published'] && !(isset($params['slug']) && $item['slug'] == $params['slug'])) {
return;
}
break;
case 'notes':
$item = array(
"type" => "note",
"title" => $meta["title"],
"author" => $meta["author"],
"urlrel" => "/post/".$meta["slug"],
"url" => "https://yarmo.eu/post/".$meta["slug"],
"slug" => $meta["slug"],
"date" => $meta["date"],
"published" => $meta["published"],
"discussion" => $meta["discussion"],
"content" => $mp->text($content));
$date = new DateTime($item['date']);
$item['date_formatted'] = $date->format('Y-m-d');
$item['date_rss'] = $date->format('Y-m-d');
if (!$item['published'] && !(isset($params['slug']) && $item['slug'] == $params['slug'])) {
return;
}
break;
case 'projects':
$item = array(
"title" => $meta["title"],
"status" => $meta["status"],
"urlrel" => "/projects/".$meta["slug"],
"url" => "https://yarmo.eu/projects/".$meta["slug"],
"slug" => $meta["slug"],
"date" => $meta["date"],
"listed" => $meta["listed"],
"content" => $mp->text($content));
$date = new DateTime($item['date']);
$item['date_formatted'] = $date->format('Y-m-d');
$item['date_rss'] = $date->format('Y-m-d');
if (!$item['listed'] && !(isset($params['slug']) && $item['slug'] == $params['slug'])) {
return;
}
break;
case 'replies':
$item = array(
"type" => "note",
"title" => $meta["title"],
"reply-url" => $meta["reply-url"],
"reply-title" => $meta["reply-title"],
"author" => $meta["author"],
"urlrel" => "/reply/".$meta["slug"],
"url" => "https://yarmo.eu/reply/".$meta["slug"],
"slug" => $meta["slug"],
"date" => $meta["date"],
"published" => $meta["published"],
"content" => $mp->text($content));
$date = new DateTime($item['date']);
$item['date_formatted'] = $date->format('Y-m-d');
$item['date_rss'] = $date->format('Y-m-d');
if (!$item['published'] && !(isset($params['slug']) && $item['slug'] == $params['slug'])) {
return;
}
break;
default:
return;
break;
}
if (isset($params['slug']) && $item['slug'] != $params['slug']) {
return;
}
if (isset($params['slug'])) {
$item["webmentions"] = array_merge(
getWebmentions($item["url"]),
getWebmentions("https://yarmo.eu/blog/".$meta["slug"]),
getWebmentions("https://yarmo.eu/notes/".$meta["slug"])
);
$item["hasWebmentions"] = count($item["webmentions"]) > 0;
}
return $item;
}