Skip to content

Commit

Permalink
Fully update blog to use filesystem-based data storage
Browse files Browse the repository at this point in the history
  • Loading branch information
yiays committed Jan 15, 2024
1 parent 078b1fa commit acb945d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 149 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
98
blog/articles/*.views
98
19 changes: 13 additions & 6 deletions blog/article.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
<?php
$article = fetch_article($params[2]);
$article?->fetch_content();

$edit = false;
if(count($params) > 3) {
if($params[3] == 'edit') {
if(key_exists('passportToken', $_COOKIE)) {
// Bypass authorization when needed
/*class User {
public int $id = 0;
public string $username = 'Yiays';
public bool $admin = true;
}
$user = new User();*/
//if(key_exists('passportToken', $_COOKIE)) {
if(!$user || !$user->admin) {
http_response_code(403);
die('Failed to verify you as an administrator.');
}else{
$edit = true;
}
}else{
http_response_code(301);
/*}else{
http_response_code(302);
header('location: https://passport.yiays.com/account/login/?redirect='.urlencode('https://yiays.com'.$_SERVER['REQUEST_URI']));
}
}*/
}elseif($params[3] != '') {
http_response_code(404);
die();
Expand All @@ -35,8 +43,7 @@
}

$title = $article->title;
$content = file_get_contents($_SERVER['DOCUMENT_ROOT']."/blog/articles/$article->urlid.md");
$content = $ParseDown->text($content);
$content = $ParseDown->text($article->content);
# Add IDs to all post headings
preg_match_all('/<(h[123456])>(.*)<\/h[123456]>/U', $content, $headings);
$norepeatid = [];
Expand Down
50 changes: 19 additions & 31 deletions blog/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,19 @@
}

if(is_null($article)) {
assert($user instanceof passport\User);
$article = new Article();
$article->id = -1;
$article->author = new Author();
$article->author->id = $user->id;
$article->author->username = $user->username;
$article->title = str_replace('-', ' ', ucfirst($params[2]));
$article->urlid = $params[2];
$article->tags = [];
$article->img = 'default.svg';
$article->col = '004461';
$article->date = new DateTime();
$article->editdate = null;
//assert($user instanceof passport\User);
$article = new Article(
author: $user->username,
title: str_replace('-', ' ', ucfirst($params[2])),
urlid: $params[2],
tags: [],
img: 'default.svg',
col: '004461',
date: new DateTime(),
editdate: null,
hidden: true
);
$article->content = 'Click here to edit the article content.';
$article->hidden = true;
}else{
if($article->hidden && (!$user || !$user->admin)) {
http_response_code(404);
Expand All @@ -51,20 +49,10 @@
}
$article->col = substr($_POST['color'], 1);
$article->hidden = array_key_exists('hidden', $_POST);
$article->content = $ParseDown->parse($_POST['contenthtml']);
# Add IDs to all post headings
preg_match_all('/<(h[123456])>(.*)<\/h[123456]>/U', $article->content, $headings);
$norepeatid = [];
for($i=0; $i<count($headings[0]); $i++) {
$headingid = preg_replace('/-+/', '-', preg_replace('/[^a-z0-9]/', '-', strtolower($headings[2][$i])));
if(array_search($headingid, $norepeatid)) continue;
$article->content = str_replace($headings[0][$i], "<{$headings[1][$i]} id=\"{$headingid}\">{$headings[2][$i]}</{$headings[1][$i]}>", $article->content);
$norepeatid[]=$headingid;
}
$article->content = $_POST['contenthtml'];
$article->save();
http_response_code(403);
header("location: /blog/$article->urlid");
die();
}

$title = "Editing ".$article->title;
Expand All @@ -75,27 +63,27 @@

?>
<form method="POST" enctype="multipart/form-data">
<article class="post post-editable" id="<?php echo $article->id; ?>">
<article class="post post-editable">
<div class="post-header hero" id="postcolor" style="background:#<?php echo $article->col; ?>;">
<div class="flex-row" style="flex-wrap:nowrap;">
<h2 style="max-width:min(30rem,60vw);">
<input type="text" name="title" placeholder="Title" required value="<?php echo htmlspecialchars($article->title); ?>">
</h2>
<?php echo userpreview($user); ?>
</div>
<span class="dim"><?php echo "By ".$article->author->handle().", written ".$article->date->format('Y-m-d').($article->editdate?', <i>last edited '.$article->editdate->format('Y-m-d').'</i>':''); ?></span>
<span class="dim"><?php echo "By ".$article->author.", written ".$article->date->format('Y-m-d').($article->editdate?', <i>last edited '.$article->editdate->format('Y-m-d').'</i>':''); ?></span>
<br><input type="text" name="tags" placeholder="Tags" value="<?php echo htmlspecialchars(implode(', ', $article->tags)); ?>">
<br>Colour:
<br><label for="color">Colour:</label>
<input type="color" id="color" name="color" required value="#<?php echo $article->col; ?>">
<br>Cover image:
<input type="file" name="cover" id="cover" accept=".jpg,.jpeg,.png,.webp,.svg">
<br><label for="cover">Cover image:</label>
<input type="file" name="cover" id="cover" accept=".jpg,.jpeg,.png,.webp,.svg" value="<?php echo $article->img; ?>">
<br>
<div class="flex-row">
<label for="hidden">Hidden:</label>&nbsp;
<input type="checkbox" id="hidden" name="hidden" <?php echo ($article->hidden?'checked':''); ?>>
<input type="submit" id="save" class="btn" style="margin-left:auto;" value="<?php echo ($article->hidden?'Save':'Publish'); ?>">
</div>
<img alt="<?php echo htmlspecialchars($article->title); ?> cover art" id="postcover" src="https://cdn.yiays.com/blog/<? echo $article->img; ?>">
<img alt="<?php echo htmlspecialchars($article->title); ?> cover art" id="postcover" src="https://cdn.yiays.com/blog/<?php echo $article->img; ?>">
</div>
<input type="hidden" name="contenthtml" id="submitcontent">
<div class="post-content" id="postcontent"><?php echo $article->content; ?></div>
Expand Down
4 changes: 1 addition & 3 deletions blog/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
require_once($_SERVER['DOCUMENT_ROOT'].'/router.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/includes/blogdata.php');

$user = null;

$params = explode('/', $url);
if(strlen($params[2])) {
require('article.php');
Expand Down Expand Up @@ -49,7 +47,7 @@ function userpreview($user, $edit=null) {
$result .= "Logged in as <b>$user->username</b>
<br><sub>Not you? <a href=\"https://passport.yiays.com/account/logout/?redirect=".urlencode('https://yiays.com'.$_SERVER['REQUEST_URI'])."\">Logout</a>";
}
if(!is_null($edit)) {
if(!is_null($edit) && ($user && $user->admin)) {
$result .= "<br><a class=\"btn\" href=\"edit/\">Edit</a>";
}
return $result.'</span>';
Expand Down
130 changes: 23 additions & 107 deletions includes/blogdata.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Article {
public string $urlid;
public string $url;
public array $tags;
public ?string $content = null;
public int $views;
public string $img;
public string $col;
Expand All @@ -21,23 +22,34 @@ function __construct($author, $title, $urlid, $tags, $img, $col, $date, $editdat
$this->urlid = $urlid;
$this->url = "https://yiays.com/blog/$urlid/";
$this->tags = $tags;
if(file_exists($_SERVER['DOCUMENT_ROOT']."/blog/articles/$urlid.views"))
$this->views = intval(file_get_contents($_SERVER['DOCUMENT_ROOT']."/blog/articles/$urlid.views"));
else $this->views = 0;
$this->views = 0;
$this->img = $img;
$this->col = $col;
$this->date = $date;
$this->editdate = $editdate;
$this->hidden = $hidden;
}

function save($content) {
file_put_contents($_SERVER['DOCUMENT_ROOT']."/blog/articles/$this->urlid.md", $content);
function save() {
// Save blog content, if loaded, then remove from object to reduce metadata size
$tempcontent = $this->content;
if($this->content) {
file_put_contents($_SERVER['DOCUMENT_ROOT']."/blog/articles/$this->urlid.md", $this->content);
$this->content = null;
}
// Save metadata
file_put_contents($_SERVER['DOCUMENT_ROOT']."/blog/articles/$this->urlid.meta", serialize($this));
// Restore blog content
if($tempcontent) $this->content = $tempcontent;
}

function view() {
$this->views += 1;
file_put_contents($_SERVER['DOCUMENT_ROOT']."/blog/articles/$this->urlid.views", $this->views);
$this->save();
}

function fetch_content() {
$this->content = file_get_contents($_SERVER['DOCUMENT_ROOT']."/blog/articles/$this->urlid.md");
}

function preview_wide($edit = false) : string {
Expand Down Expand Up @@ -99,107 +111,11 @@ function carousel_images() : string {
}
}

$articles = [
new Article(
"Yiays",
"GitHub Activity Summary 2022",
'github-activity-summary-2022',
['GitHub', 'Development', 'Web Development'],
'github-summary-2022.webp',
'0e1017',
new DateTime('2023-01-13'),
new DateTime('2023-01-14'),
false
),
new Article(
'Yiays',
"Bringing developers, translators, and users together with Discord",
'bringing-developers-translators-and-users-together-with-discord',
['Case Study', 'Development', 'Discord'],
'discord.webp',
'5662f6',
new DateTime('2022-01-25'),
new DateTime('2022-03-25'),
false
),
new Article(
'Yiays',
"GitHub Activity Summary 2021",
'github-activity-summary-2021',
['GutHub', 'Development', 'Web Development'],
'github-summary-2021.webp',
'0d1017',
new DateTime('2022-01-10'),
new DateTime('2023-01-12'),
false
),
new Article(
'Yiays',
"The curious case of my Cookie Clicker clone",
'the-curious-case-of-my-cookie-clicker-clone',
['GitHub', 'Web Development'],
'cookie-clicker-win11.webp',
'653319',
new DateTime('2021-11-30'),
null,
false
),
new Article(
'Yiays',
"GitHub Activity Summary 2020",
'github-activity-summary-2020',
['GitHub', 'Development', 'Web Development'],
'github-summary-2020.webp',
'0d1017',
new DateTime('2021-11-15'),
new DateTime('2023-01-12'),
false
),
new Article(
'Yiays',
"Who is Yiays?",
'who-is-yiays',
['Personal', 'Development'],
'who-is-yiays.webp',
'000000',
new DateTime('2021-10-08'),
new DateTime('2021-11-17'),
false
),
new Article(
'Yiays',
"GitHub Activity Summary 2019",
'github-activity-summary-2019',
['GitHub', 'Development', 'Web Development'],
'github-summary-2019.webp',
'0d1017',
new DateTime('2021-09-30'),
new DateTime('2023-01-12'),
false
),
new Article(
'Yiays',
"A history of Yiays.com",
'a-history-of-my-websites',
['Web Development', 'Personal'],
'yiayscom.webp',
'7295cd',
new DateTime('2020-03-18'),
new DateTime('2021-11-17'),
false
),
new Article(
'Yiays',
"Introducing ConfessionBot",
'confession-bot-a-side-project',
['Discord', 'Development'],
'confessionbot.webp',
'000000',
new DateTime('2019-06-22'),
new DateTime('2022-05-15'),
false
)
];
$articles = [];
foreach(glob($_SERVER['DOCUMENT_ROOT']."/blog/articles/*.meta") as $article) {
$articles []= unserialize(file_get_contents($article));
usort($articles, fn($a, $b) => $a->date < $b->date);
}

function fetch_article($urlid) {
global $articles;
Expand Down
3 changes: 3 additions & 0 deletions router.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
$url = explode('?', $_SERVER['REQUEST_URI'])[0];

// As passport is disabled, provide default state for user
$user = null;

if($url == '/'){
require('index.php');
}
Expand Down

0 comments on commit acb945d

Please sign in to comment.