'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('

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 .= '

'.$post['post_subject'].'

'.$post['post_body'].'
'; if($config['enableComments'] == TRUE) { $comments = dbQuery('SELECT * FROM comments WHERE post_id = \''.$id.'\';', 'rows'); foreach($comments as $comment) { $content .= '
'.$comment['comment_body'].' - '.$comment['comment_author'].'

'; } $content .= '

add a comment

name:
email:
comment:

'; } 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 .= '
'; setContent($content); } // API FUNCTIONS function formatPost($post) { //print_r($post); return "

".$post['post_subject']."

".betterEllipsis($post['post_body'], 255)."
\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 .= ''.$item.' | '; } 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 ?> <?php echo $config['blogTitle'].' : '.$title ?>