Skip to content

Commit

Permalink
added pages, moved components
Browse files Browse the repository at this point in the history
  • Loading branch information
arajnoha committed Feb 10, 2021
1 parent f5d2561 commit 893ee14
Show file tree
Hide file tree
Showing 19 changed files with 398 additions and 16 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# pureblog
#### PHP flatfile self-hostable platform for simple markdown blogging based on writefreely design and principles.
___
<img width="300" src="pretty/showcase0.png"><img width="300" src="pretty/showcase1.png"><img width="300" src="pretty/showcase2.png">
___

Simple CMS for blogging with nothing in between. Just writing.
Flat-file system doesn't require any database and no dependencies.

### Install
1. Simply copy all content to root or subfolder of your web server / hosting.
2. Make sure all content has the rights of 755
2. Make sure that the directories has rights of 775 and files 664.
3. Open the folder in browser and log in with password "pencil" (change it in Settings afterwards)

### Use
Just write. No rules but one, every blog __post must begin with title__ (# heading)
Just write. No rules but one, every blog __post must begin with title__ (# heading).
You can now enable custom Pages from the _Extra_ section of the Settings page (visible from blog admin menu).

### Purpose
I wanted to have a simple environment for me to write my posts without any distractions from the UI, without multiple dependencies and that could run on a web hosting without a server.
Expand All @@ -26,7 +25,6 @@ This project is free software licenced with GPL. The markdown-to-html backend co
- beggining with the title will not be required in future
- langauges support
- drafts
- pinning posts to pages
- securing admin changes from CSRF
- blog-type switch between posts with perex or just a title
- minor design customizations
Expand Down
92 changes: 92 additions & 0 deletions admin/addpage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
session_start();

function getTitle($string) {
$pattern = "/<h1>(.*?)<\/h1>/";
preg_match_all($pattern, $string, $matches);
return ($matches[1]);
}

function stringifyTitle($str, $delimiter = '-') {
$slug = strtolower(trim(preg_replace('/[\s-]+/', $delimiter, preg_replace('/[^A-Za-z0-9-]+/', $delimiter, preg_replace('/[&]/', 'and', preg_replace('/[\']/', '', iconv('UTF-8', 'ASCII//TRANSLIT', $str))))), $delimiter));
return $slug;
}

if (isset($_SESSION["in"]) && $_SESSION["in"] === 1) {

include("data.php");
include("bones/function.php");


if (isset($_POST["writearea"]) && $_POST["writearea"] !== "") {

include("parsedown/Parsedown.php");
$Parsedown = new Parsedown();

// parse all content to html
$content = $Parsedown->text($_POST["writearea"]);

// get title and slug before cutting it from the content
$title = getTitle($content);
$slug = stringifyTitle(implode($title));

// crop the title and make perex
$content = substr($content, strpos($content, '</h1>') + 5);
$perex = mb_strimwidth(strip_tags($content), 0, 180, "...");


mkdir("../".$slug);

// create markdown backup for future edits
$file = fopen("../".$slug."/article.md","w");
fwrite($file, $_POST["writearea"]);
fclose($file);

// create title file for navigation
$file = fopen("../".$slug."/name","w");
fwrite($file, implode($title));
fclose($file);

// create actual permalink file
$file = fopen("../".$slug."/index.php","w");

// pre-created html filled with new content
$fileString = makePageDOM(implode($title),$perex,$siteDescription,$siteName,$content,$slug);

fwrite($file, $fileString);
fclose($file);

header("Location: index.php");

}

?>

<!doctype html>
<html lang="cs">
<head>
<meta charset="utf-8">
<title><?=$siteName;?></title>
<link rel="stylesheet" type="text/css" href="pretty/neon.css">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="<?=$siteDescription;?>">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Lora:ital,wght@0,400;0,700;1,400&display=swap" rel="stylesheet">
<link rel="icon" type="image/png" href="pretty/i/favicon.png">
</head>
<body>
<main>
<form action="addpage.php" method="post" class="write">
<nav class="clean">
<a class="graylink" href="../admin/">Discard</a>
<input type="submit" value="Publish">
</nav>
<textarea name="writearea" spellcheck="false" placeholder="# Start with a title" autofocus></textarea>
</form>
</main>
</body>
</html>
<?php } else {
header("Location: ../admin/");
}
?>
69 changes: 69 additions & 0 deletions admin/bones/function.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
session_start();
include("../admin/data.php");

function makeArticleDOM($title,$perex,$siteName,$content) {

$date = date("Y-m-d");
$DOM = <<<LOL
<?php include("../../admin/data.php"); ?>
<!doctype html>
<html lang="cs">
<head>
<meta charset="utf-8">
<title>$title</title>
<link rel="stylesheet" type="text/css" href="../../admin/pretty/neon.css">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="$perex">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Lora:ital,wght@0,400;0,700;1,400&display=swap" rel="stylesheet">
<link rel="icon" type="image/png" href="../../admin/pretty/i/favicon.png">
</head>
<body>
<main>
<header class="blogpost">
<h2><a href="../../">$siteName</a></h2>
</header>
<section><h1>$title</h1><span>$date</span><div>$content</div></section>
</main>
</body>
</html>
LOL;
return $DOM;
}

function makePageDOM($title,$perex,$siteDescription,$siteName,$content,$slug) {


$DOM = <<<LOL
<!doctype html>
<html lang="cs">
<head>
<meta charset="utf-8">
<title>$title</title>
<link rel="stylesheet" type="text/css" href="../admin/pretty/neon.css">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="$siteDescription">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Lora:ital,wght@0,400;0,700;1,400&display=swap" rel="stylesheet">
<link rel="icon" type="image/png" href="../admin/pretty/i/favicon.png">
</head>
<body>
<main>
<header>
<h2><a href="../">$siteName</a></h2>
<p>$siteDescription</p>
</header>
<?php include("../admin/bones/nav.php"); ?>
<?php include("../admin/bones/pageadminnav.php"); ?>
<section><div>$content</div></section>
</main>
<script src="../admin/pretty/do.js"></script>
</body>
</html>
LOL;
return $DOM;

}

?>
22 changes: 22 additions & 0 deletions admin/bones/nav.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

include("../admin/data.php");

if ($siteExtraEnabledPages === "1") {
$pagePath = "../*";
$pageArray = glob($pagePath, GLOB_ONLYDIR);

$nav = "<nav class='pages'>";
$nav.= "<a href='../'>Blog</a>";

foreach ($pageArray as $page) {
if ($page !== "../admin" && $page !== "../".$siteBlogPageSlug) {
$name = file_get_contents($page."/name",true);
$nav .= "<a href='".$page."'>".$name."</a>";
}
}

$nav .= "</nav>";
echo $nav;
}
?>
17 changes: 17 additions & 0 deletions admin/bones/pageadminnav.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
session_start();
include("../admin/data.php");

if ($siteExtraEnabledPages === "1" && isset($_SESSION["in"]) && $_SESSION["in"] === 1) {


$nav = <<<LOL
<nav class="adminnav">
<a href="../admin/" class="graylink">Return back</a>
<a href="../admin/editpage.php" id="editpage" class="graylink">Edit this page</a>
<a href="#" id="deletepage" class="graylink">Delete this page</a>
</nav>
LOL;
echo $nav;
}
?>
2 changes: 1 addition & 1 deletion admin/data.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php $siteName = "Blog title";$siteDescription = "Lengthy blog description"; $sitePassword = "pencil"; $siteBlogPageSlug = "blog"; ?>
<?php $siteName = "Blog title";$siteDescription = "Lengthy blog description"; $sitePassword = "pencil"; $siteBlogPageSlug = "blog"; $siteExtraEnabledPages = "0"; ?>
2 changes: 1 addition & 1 deletion admin/delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
rmdir("../".$siteBlogPageSlug."/".$dirname);
header("Location: ../admin/");
} else {
header("Location: ../login/");
header("Location: ../admin/");
}
?>
13 changes: 13 additions & 0 deletions admin/deletepage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
session_start();

if (isset($_SESSION["in"]) && $_SESSION["in"] === 1 && isset($_GET["id"])) {
include("data.php");
$dirname = $_GET["id"];
array_map('unlink', glob("../".$dirname."/*"));
rmdir("../".$dirname);
header("Location: ../admin/");
} else {
header("Location: ../admin/");
}
?>
5 changes: 3 additions & 2 deletions admin/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function stringifyTitle($str, $delimiter = '-') {
if (isset($_SESSION["in"]) && $_SESSION["in"] === 1) {

include("data.php");
include("bones/function.php");
$id = $_GET["id"];

if (isset($_POST["writearea"]) && $_POST["writearea"] !== "") {
Expand Down Expand Up @@ -54,7 +55,7 @@ function stringifyTitle($str, $delimiter = '-') {
$file = fopen("../".$siteBlogPageSlug."/".$slug."/index.php","w");

// pre-created html filled with new content
$fileString = '<?php include("../../admin/data.php"); ?><!doctype html><html lang="cs"><head><meta charset="utf-8"><title>'.implode($title).'</title><link rel="stylesheet" type="text/css" href="../../admin/pretty/neon.css"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="description" content="'.$perex.'"><link rel="preconnect" href="https://fonts.gstatic.com"><link href="https://fonts.googleapis.com/css2?family=Lora:ital,wght@0,400;0,700;1,400&display=swap" rel="stylesheet"> <link rel="icon" type="image/png" href="../../admin/pretty/i/favicon.png"></head><body><main><header class="blogpost"><h2><a href="../../">'.$siteName.'</a></h2></header><section><h1>'.implode($title).'</h1><span>'.date("Y-m-d").'</span><div>'.$content.'</div></section></main></body></html>';
$fileString = makeArticleDOM(implode($title),$perex,$siteName,$content);

fwrite($file, $fileString);
fclose($file);
Expand Down Expand Up @@ -94,6 +95,6 @@ function stringifyTitle($str, $delimiter = '-') {
</body>
</html>
<?php } else {
header("Location: ../login/");
header("Location: ../admin/");
}
?>
101 changes: 101 additions & 0 deletions admin/editpage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
session_start();

function getTitle($string) {
$pattern = "/<h1>(.*?)<\/h1>/";
preg_match_all($pattern, $string, $matches);
return ($matches[1]);
}

function stringifyTitle($str, $delimiter = '-') {
$slug = strtolower(trim(preg_replace('/[\s-]+/', $delimiter, preg_replace('/[^A-Za-z0-9-]+/', $delimiter, preg_replace('/[&]/', 'and', preg_replace('/[\']/', '', iconv('UTF-8', 'ASCII//TRANSLIT', $str))))), $delimiter));
return $slug;
}

if (isset($_SESSION["in"]) && $_SESSION["in"] === 1) {

include("data.php");
include("bones/function.php");
$id = $_GET["id"];


if (isset($_POST["writearea"]) && $_POST["writearea"] !== "") {

include("parsedown/Parsedown.php");
$Parsedown = new Parsedown();

// parse all content to html
$content = $Parsedown->text($_POST["writearea"]);

// get title and slug before cutting it from the content
$title = getTitle($content);
$slug = stringifyTitle(implode($title));

// crop the title and make perex
$content = substr($content, strpos($content, '</h1>') + 5);
$perex = mb_strimwidth(strip_tags($content), 0, 180, "...");

// delete original page if slug differs
if ($slug !== $id) {
array_map('unlink', glob("../".$id."/*"));
rmdir("../".$id);
mkdir("../".$slug);
}

// create markdown backup for future edits
$file = fopen("../".$slug."/article.md","w");
fwrite($file, $_POST["writearea"]);
fclose($file);

// create title file for navigation
$file = fopen("../".$slug."/name","w");
fwrite($file, implode($title));
fclose($file);

// create actual permalink file
$file = fopen("../".$slug."/index.php","w");

// pre-created html filled with new content
$fileString = makePageDOM(implode($title),$perex,$siteDescription,$siteName,$content,$slug);

fwrite($file, $fileString);
fclose($file);

header("Location: ../".$id);

}

?>

<!doctype html>
<html lang="cs">
<head>
<meta charset="utf-8">
<title><?=$siteName;?></title>
<link rel="stylesheet" type="text/css" href="pretty/neon.css">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="<?=$siteDescription;?>">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Lora:ital,wght@0,400;0,700;1,400&display=swap" rel="stylesheet">
<link rel="icon" type="image/png" href="pretty/i/favicon.png">
</head>
<body>
<main>
<form action="addpage.php" method="post" class="write">
<nav class="clean">
<a class="graylink" href="../admin/">Discard</a>
<input type="submit" value="Publish">
</nav>
<textarea name="writearea" spellcheck="false" placeholder="# Start with a title" autofocus><?php
$originalFile = fopen("../".$id."/article.md", "r") or die("php file reading error.");
echo fread($originalFile,filesize("../".$id."/article.md"));
fclose($originalFile);
?></textarea>
</form>
</main>
</body>
</html>
<?php } else {
header("Location: ../admin/");
}
?>
Loading

0 comments on commit 893ee14

Please sign in to comment.