All pastes #72752 Raw Edit

Something

public text v1 · immutable
#72752 ·published 2006-06-27 03:23 UTC
rendered paste body
<?php
/***
litelog - A minimal, SQLite-based PHP weblog system.

DOCUMENTATION

SQL queries you need to run:

http://localhost/blog.php?a=debugquery&q=CREATE TABLE posts (post_id INTEGER PRIMARY KEY, post_subject TEXT, post_body TEXT, post_pubdate TEXT);INSERT INTO posts (post_subject, post_body, post_pubdate) VALUES ("Test", "This is a test of SQLite..", "2006-6-26");INSERT INTO posts (post_subject, post_body, post_pubdate) VALUES ("Second Test", "Return of the SQLite tests!", "2006-6-27");CREATE TABLE comments (comment_id INTEGER PRIMARY KEY, post_id INTEGER, comment_author TEXT, comment_email, comment_ip TEXT, comment_body TEXT);INSERT INTO comments (post_id, comment_author, comment_email, comment_ip, comment_body) VALUES (1, "foo", "foo@gmail.com", "127.0.0.1", "This is a test comment...");
***/

// CONFIG
define('ROOT', 'http://localhost/blog.php');
$config = array('blogTitle' => 'agentmd\'s blog',
                'database' => 'blog.db',
                'enableComments' => FALSE);
// defaults
$nav = array('home' => '?a=home', 'archives' => '?a=archives');
$content = 'Nothing to see here..';

// CODE STARTS HERE
if(!$dbHandle = sqlite_open($config['database'], 0666, &$errStr))
{
    die($errStr);
}

$func = 'init_'.$_GET['a'];
if(function_exists($func))
{
    $func();
}
else
{
    init_home();
}


// REQUEST FUNCTIONS

function init_home()
{
    $posts = dbQuery('SELECT * FROM posts WHERE 1 ORDER BY post_pubdate DESC LIMIT 10', 'rows');
    foreach($posts as $post)
    {
        $output .= formatPost($post);
    }
    setPageTitle("Home");
    setContent($output);
}

function init_setup()
{
    global $dbHandle;
    dbQuery('CREATE TABLE posts (post_id INTEGER PRIMARY KEY, post_subject TEXT, post_body TEXT, post_pubdate TEXT);');
    dbQuery('INSERT INTO posts (post_subject, post_body, post_pubdate) VALUES ("Test", "This is a test of SQLite..", "2006-6-26");');
    dbQuery('INSERT INTO posts (post_subject, post_body, post_pubdate) VALUES ("Second Test", "Return of the SQLite tests!", "2006-6-27");');
    dbQuery('CREATE TABLE comments (comment_id INTEGER PRIMARY KEY, post_id INTEGER, comment_author TEXT, comment_email, comment_ip TEXT, comment_body TEXT);');
    dbQuery('INSERT INTO comments (post_id, comment_author, comment_email, comment_ip, comment_body) VALUES (1, "foo", "foo@gmail.com", "127.0.0.1", "This is a test comment...");');
    if($err)
    {
        die($err);
    }
    setContent('<h1>Looks like we\'re setup successfully!');
}
function init_debugquery()
{
    global $dbHandle;
    $q = $_GET['q'];
    sqlite_exec($q, $dbHandle, &$errorStr);
    if($errorString)
    {
        die($errString);
    }
}

function init_view()
{
    global $config;
    $id = (int)$_GET['id'];
    $post = dbQuery('SELECT * FROM posts WHERE post_id = \''.$id.'\';', 'row');
    $content .= '<div id="post"><h1>'.$post['post_subject'].'</h1>'.$post['post_body'].'</div>';
    if($config['enableComments'] == TRUE)
    {
        $comments = dbQuery('SELECT * FROM comments WHERE post_id = \''.$id.'\';', 'rows');
        foreach($comments as $comment)
        {
            $content .= '<br /><div id="comment">'.$comment['comment_body'].' - '.$comment['comment_author'].'</div><hr />';
        }
        $content .= '<div id="reply"><h3>add a comment</h3><form action="'.ROOT.'?a=comment" method="post"><input type="hidden" name="post_id" value="'.$post['post_id'].'" />name: <input type="text" name="comment_author" /><br />email: <input type="text" name="comment_email" /><br />comment:<br /><textarea name="comment_body" rows="5" cols="25"></textarea><br /><input type="submit" value="add comment" /></form>';
    }
    setPageTitle($post['post_subject']);
    setContent($content);
}

function init_comment()
{
    if(isset($_POST['post_id'], $_POST['comment_body'], $_POST['comment_author'], $_POST['comment_email']))
    {
        dbQuery("INSERT INTO comments (post_id, comment_body, comment_author, comment_email, comment_ip) VALUES (".(int)$_POST['post_id'].", \"".addslashes($_POST['comment_body'])."\", \"".addslashes($_POST['comment_author'])."\", \"".addslashes($_POST['comment_email'])."\", \"".addslashes($_SERVER['REMOTE_ADDR'])."\");");
        header('Location: '.ROOT.'?a=view&id='.(int)$_POST['post_id'].'&thanks');
    } else {
        throwError('Please enter in all the information');
    }
}

function init_archives()
{
    $posts = dbQuery('SELECT * FROM posts WHERE 1 ORDER BY post_pubdate DESC', 'rows');
    $content .= '<div id="archives">
                <ul>';
    foreach($posts as $post)
    {
        $content .= '<li><a href="'.ROOT.'?a=view&id='.$post['post_id'].'">'.$post['post_subject'].'</a></li>';
    }
    $content .= '</ul></div>';
    setContent($content);
}

// API FUNCTIONS
function formatPost($post)
{
    //print_r($post);
    return "<div id=\"post_".$post['post_id']."\"><a href=\"".ROOT."?a=view&id=".$post['post_id']."\"><h3>".$post['post_subject']."</h3></a>".betterEllipsis($post['post_body'], 255)."</div>\n";
}

function debug($str) {
    file_put_contents("litelog.log", $str);
    return;
}
function throwError($errMsg) {
    die($errMsg);
    exit();
}
function setContent($newContent) { 
    global $content;
    $content = $newContent;
}
function setPageTitle($theTitle) {
    global $title;
    $title = $theTitle;
}
function getNavbar() {
    global $nav;
    foreach($nav as $item => $dest) { 
        $output .= '<a href="'.ROOT.$dest.'">'.$item.'</a> | ';
    }
    return $output;
}
function dbQuery($q, $r = NULL) {
    global $dbHandle;
    $res = sqlite_query($q, $dbHandle, &$errStr);
    if($errStr) { throwError(sqlite_error_string($errStr)); }
    switch($r)
    {
        case 'object':
            return (object) sqlite_fetch_object($res);
        case 'row':
            return sqlite_fetch_array($res, SQLITE_BOTH);
            break;
        case 'rows':
        default:
            return sqlite_fetch_all($res, SQLITE_ASSOC);
            break;
    }
}
function betterEllipsis($string, $length, $end='...')
{
   return (strLen($string)>$length)?subStr($string,0,$length-strLen($end)).$end:$string;
}
// BELOW IS TEMPLATE
?>

<html>
<head>
    <title><?php echo $config['blogTitle'].' : '.$title ?></title>
    <style type="text/css">
    body { background: #CCCCCC; }
    #container { background: #FFFFFF; width: 742px; margin: auto; font-family: Lucida Grande; }
    #nav { margin: 15px; }
    #content { margin: 5px; }
    #footer { text-align: center; font-size: small; }
    </style>
</head>
<body>
<div id="container">
    <div id="header">
    <h1><?php echo $config['blogTitle']; ?></h1>
    </div>
    
    <div id="nav">
    <?php echo getNavbar(); ?>
    </div>
    
    <div id="content">
    <?php echo $content; ?>
    </div>
    
    <div id="footer">
    powered by litelog
    </div>
</div>
</body>
</html>