-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.php
89 lines (83 loc) · 3.49 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
require_once('Parsedown.php');
// Default config is now found in 'config-default.php', but it's best not to edit that directly.
// Instead, duplicate it and rename the new version 'config-custom.php' (no quotes).
// If you're using Git for version control, add config-custom.php to your .gitignore.
// Then you can do pull/fetch/rebase in Git and it won't overwrite your config.
if (file_exists('config-custom.php')) {
require_once('config-custom.php');
} else {
require_once('config-default.php');
}
$content = '';
$is_post = !empty($_GET['post']); // Whether we're viewing the main list or a single post
function sortPosts($a, $b) {
$a_value = $a->getFilename();
$b_value = $b->getFilename();
return strcmp($b_value, $a_value); // Reversed to get descending
}
if ( $is_post ) {
// Single post page
$post_name = filter_var($_GET['post'], FILTER_SANITIZE_NUMBER_INT);
$file_path = __DIR__.'/content/'.$post_name.'.'.FILE_EXT;
if ( file_exists($file_path) ) {
$file = fopen($file_path, 'r');
$post_title = trim(fgets($file),'#');
fclose($file);
// Process the Markdown
$parsedown = new Parsedown();
$content = $parsedown->text(file_get_contents($file_path));
} else {
$content = '
<h2>Not Found</h2>
<p>Sorry, couldn\'t find a post with that name. Please try again, or go to the
<a href="'.BASE_URL.'">home page</a> to select a different post.</p>';
}
} else {
// Blog main page - list all posts
$files = new DirectoryIterator(__DIR__.'/content/');
$files_array = [];
foreach ($files as $file) {
if ( $file->isFile() && $file->getExtension() == FILE_EXT ) {
array_push($files_array, $file->getFileInfo());
}
}
usort($files_array, 'sortPosts'); // See sortPosts() function above
foreach ($files_array as $file) {
$filename_no_ext = $file->getBasename('.'.FILE_EXT);
$file_pointer = $file->openFile();
$post_title = trim($file_pointer->fgets(),'#');
$content .= '<h2 class="list-title"><a href="'.BASE_URL.'?post='.$filename_no_ext.'">'.$filename_no_ext.' - '.$post_title.'</a></h2>';
}
}
// Appending file hashes to the <link> hrefs allows us to cache the files indefinitely,
// but immediately serve a new version once the file changes.
$style_hash = hash('md5', file_get_contents(__DIR__.'/src/css/style-'.APPEARANCE.'.css'));
$fonts_hash = hash('md5', file_get_contents(__DIR__.'/src/css/fonts.css'));
$icon_hash = hash('md5', file_get_contents(__DIR__.'/img/favicon.png'));
?>
<!DOCTYPE html>
<html>
<head>
<title><?php if ( !empty($_GET['post']) ) { echo $post_title.' - '; } ?><?php echo BLOG_TITLE; ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="src/css/style-<?php echo APPEARANCE; ?>.css?v=<?php echo $style_hash; ?>" />
<link rel="stylesheet" href="src/css/fonts.css?v=<?php echo $fonts_hash; ?>" />
<link rel="icon" type="image/png" href="img/favicon.png?v=<?php echo $icon_hash; ?>" />
</head>
<body>
<header>
<h1 class="blog-title"><a href="<?php echo BASE_URL; ?>"><?php echo BLOG_TITLE; ?></a></h1>
</header>
<?php
$tag = $is_post ? 'article' : 'main';
echo '<'.$tag.'>'.$content.'</'.$tag.'>';
?>
<footer>
<p class="postscript">This blog does not offer comment functionality. If you'd like to discuss any of the topics
written about here, you can <a href="mailto:<?php echo CONTACT_EMAIL; ?>">send me an email</a>.</p>
<hr />
<p class="powered-by">Powered by text files and <a href="https://github.com/paintedsky/dead-simple-blog" target="_blank">Dead Simple Blog</a>.</p>
</footer>
</body>
</html>