<?php
include_once("class/mysql.class.php");
function controller() {
$action = $_GET['action'];
$public = array("home", "login", "register", "post");
$private = array("createPost", "editPost", "deletePost", "viewPosts");
if (isset($_SESSION['private']) {
$scope = array_merge($public, $private);
} else {
$scope = $public;
}
if (! empty($action)) {
$action = mysql_real_escape_string($action);
}
if (in_array($action, $scope)) {
return $action();
} else {
return home();
}
function home() {
include_once("tpl/home.inc.php");
}
function menu() {
if (isset($_SESSION['private')) {
include_once("tpl/privateMenu.inc.php");
} else {
include_once("tpl/menu.inc.php");
}
}
function deletePost() {
$pid = $_GET['pid'];
if (is_numeric($pid)) {
$db = new mysql("localhost", "root", "hackme", "blog");
$db->connect();
$db->select();
$db->query("DELETE FROM posts WHERE pid = '$pid'");
echo "<div class='message'>Post $pid Has Been Deleted</div>";
return viewPosts();
}
}
function createPost() {
if (isset($_POST['submit'])) {
$title = htmlentities(mysql_real_escape_string($_POST['title']));
$body = htmlentities(mysql_real_escape_string($_POST['body']));
$created = date("F j, Y, g:i a");
$db = new mysql("localhost", "root", "hackme", "blog");
$db->connect();
$db->select();
$db->query("INSERT INTO posts (title, body, created) VALUES ('$title', '$body', '$created') LIMIT 1");
echo "<div class='message'>Post $pid Has Been Deleted</div>";
return viewPosts();
} else {
include_once("tpl/createPost.inc.php");
}
}
function editPost() {
$pid = $_GET['pid'];
if (is_numeric($pid)) {
if (isset($_POST['submit']) {
$title = $_POST['title'];
$body = $_POST['body'];
$created = date("F j, Y, g:i a");
$db = new mysql("localhost", "root", "hackme", "blog");
$db->connect();
$db->select();
$db->query("UPDATE posts SET (title = '$title', body = '$body', created = '$created') WHERE pid = '$pid' ");
echo "<div class='message'>Your Post Has Been Saved</div>";
return viewPosts();
} else {
$db = new mysql("localhost", "root", "hackme", "blog");
$db->connect();
$db->select();
$db->query("SELECT * FROM posts WHERE uid = '$uid' ");
include_once("tpl/editPost.inc.php");
} else {
return home();
}
}
}
function viewPosts() {
$db = new mysql("localhost", "root", "hackme", "blog");
$db->connect();
$db->select();
$db->query("SELECT * FROM posts ORDER BY created ASC");
include_once("tpl/viewPosts.inc.php");
}
?>
|