Created
April 12, 2012 01:56
-
-
Save jtdroste/2364240 to your computer and use it in GitHub Desktop.
One File Blog - Well, read the name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* One File Blog - OFB | |
* Author: James "debug" Droste <me-@-jdroste.me> | |
* ------ | |
* Untested, but it should be good to go. | |
* Config items should be self-explanatory | |
* ------ | |
* Use header.php and footer.php for your themeing needs | |
* The post_(title|body|date) require a "%s" for where the content will be | |
* post_date_format uses PHP's date function (php.net/date). See that page | |
* for the format | |
* ------ | |
* Final notes: | |
* One day (read: soon(TM)), I'll make a fully featured one, aiming to be one file | |
* that is under maybe 10-15kbs. Should be fun :). (Not including website theme, only code) | |
*/ | |
// --------------------- start config | |
$config['posts_dir'] = './posts'; | |
$config['posts_per_page'] = 10; | |
$config['no_posts'] = 'Sorry, no posts yet!'; | |
$config['post_title'] = '<h1>%s</h1>'; | |
$config['post_body'] = '<p>%s</p>'; | |
$config['post_date'] = 'Posted on %s'; | |
$config['post_date_format'] = 'n/j/Y'; | |
// --------------------- end config | |
include 'header.php'; | |
$posts = glob($config['posts_dir'].'/*.txt'); | |
if ( empty($posts) ) { | |
echo $config['no_posts']; | |
} else { | |
$lastpage = floor(count($posts)/$config['posts_per_page']); | |
// No page... | |
if ( isset($_GET['page']) || !is_numberic($_GET['page']) ) { | |
$page = 1; | |
// We have a higher page given than what we have | |
} else if ( $_GET['page'] > $lastpage ) { | |
$page = 1; | |
// Typecasting FTW! | |
} else { | |
$page = (int) $_GET['page']; | |
} | |
$start = $page*$config['posts_per_page']-$config['posts_per_page']; | |
for ( $i=0; $i < $config['posts_per_page']; $i++ ) { | |
$id = $start+$i; | |
echo sprintf($config['post_title'], substr($posts[$id], 0, -3)); // Remove the ".txt" | |
echo sprintf($config['post_date'], date($config['post_date_format'], filemtime($posts[$id]))); | |
echo sprintf($config['post_body'], file_get_contents($posts[$id])); // Body | |
} | |
if ( count($posts) > $config['posts_per_page'] ) { | |
echo 'Pages: <a href="?page=1">1</a> '; | |
for ( $i=$page; $i > $page+5; $i++ ) { | |
if ( $i == $lastpage ) break; | |
echo '<a href="?page='.$i.'">'.$i.'</a>'; | |
} | |
echo '<a href="?page='.$lastpage.'">'.$lastpage.'</a>'; | |
} | |
} | |
include 'footer.php'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
403 error in use ...