yarmo.eu/functions.php
2020-06-18 18:41:23 +02:00

229 lines
6.8 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) {
$Parsedown = new Parsedown();
$mp = new MetaParsedown();
$posts = array();
$files = scandir('content/blog/');
foreach($files as $file) {
// Skip . and ..
if (($file == '.') || ($file == '..')) {
continue;
}
$content = file_get_contents('content/blog/' . $file);
$meta = $mp->meta($content);
$id = intval(substr($file, 0, -3));
$post = array(
"title" => $meta["title"],
"author" => $meta["author"],
"urlrel" => "/blog/".$meta["slug"],
"url" => "https://yarmo.eu/blog/".$meta["slug"],
"slug" => $meta["slug"],
"date" => $meta["date"],
"published" => $meta["published"],
"discussion" => $meta["discussion"],
"content" => $mp->text($content));
$date = new DateTime($post['date']);
$post['date_formatted'] = $date->format('Y-m-d');
$post['date_rss'] = $date->format('Y-m-d');
if (!$post['published'] && !(isset($params['slug']) && $post['slug'] == $params['slug'])) {
continue;
}
if (isset($params['slug']) && $post['slug'] != $params['slug']) {
continue;
}
if (isset($params['slug'])) {
$post["webmentions"] = getWebmentions($post["url"]);
$post["hasWebmentions"] = count($post["webmentions"]) > 0;
}
$posts[] = $post;
}
$posts = array_reverse($posts);
return $posts;
}
function getNotes($params) {
$Parsedown = new Parsedown();
$mp = new MetaParsedown();
$posts = array();
$files = scandir('content/notes/');
foreach($files as $file) {
// Skip . and ..
if (($file == '.') || ($file == '..')) {
continue;
}
$content = file_get_contents('content/notes/' . $file);
$meta = $mp->meta($content);
$id = intval(substr($file, 0, -3));
$post = array(
"title" => $meta["title"],
"author" => $meta["author"],
"urlrel" => "/notes/".$meta["slug"],
"url" => "https://yarmo.eu/notes/".$meta["slug"],
"slug" => $meta["slug"],
"date" => $meta["date"],
"published" => $meta["published"],
"content" => $mp->text($content));
$date = new DateTime($post['date']);
$post['date_formatted'] = $date->format('Y-m-d');
$post['date_rss'] = $date->format('Y-m-d');
if (!$post['published'] && !(isset($params['slug']) && $post['slug'] == $params['slug'])) {
continue;
}
if (isset($params['slug']) && $post['slug'] != $params['slug']) {
continue;
}
if (isset($params['slug'])) {
$post["webmentions"] = getWebmentions($post["url"]);
$post["hasWebmentions"] = count($post["webmentions"]) > 0;
}
$posts[] = $post;
}
$posts = array_reverse($posts);
return $posts;
}
function getProjects($params) {
$Parsedown = new Parsedown();
$mp = new MetaParsedown();
$posts = array();
$files = scandir('content/projects/');
foreach($files as $file) {
// Skip . and ..
if (($file == '.') || ($file == '..')) {
continue;
}
$content = file_get_contents('content/projects/' . $file);
$meta = $mp->meta($content);
$id = intval(substr($file, 0, -3));
$project = 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($project['date']);
$project['date_formatted'] = $date->format('Y-m-d');
$project['date_rss'] = $date->format('Y-m-d');
if (!$project['listed'] && !(isset($params['slug']) && $project['slug'] == $params['slug'])) {
continue;
}
if (isset($params['slug']) && $project['slug'] != $params['slug']) {
continue;
}
if (isset($params['slug'])) {
$project["webmentions"] = getWebmentions($post["url"]);
$project["hasWebmentions"] = count($project["webmentions"]) > 0;
}
$projects[] = $project;
}
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;
}