diff --git a/inc/functions.php b/inc/functions.php index 36ee9807..70520334 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -27,7 +27,6 @@ } function openBoard($uri) { - global $sql; sql_open(); $query = prepare("SELECT * FROM `boards` WHERE `uri` = :uri LIMIT 1"); @@ -46,6 +45,71 @@ return $boards; } + function until($timestamp) { + $difference = $timestamp - time(); + if($difference < 60) { + return $difference . ' second' . ($difference != 1 ? 's' : ''); + } elseif($difference < 60*60) { + return ($num = round($difference/(60))) . ' minute' . ($num != 1 ? 's' : ''); + } elseif($difference < 60*60*24) { + return ($num = round($difference/(60*60))) . ' hour' . ($num != 1 ? 's' : ''); + } elseif($difference < 60*60*24*7) { + return ($num = round($difference/(60*60*24))) . ' day' . ($num != 1 ? 's' : ''); + } elseif($difference < 60*60*24*7*52) { + return ($num = round($difference/(60*60*24*7))) . ' week' . ($num != 1 ? 's' : ''); + } else { + return ($num = round($difference/(60*60*24*7*52))) . ' year' . ($num != 1 ? 's' : ''); + } + } + + function formatDate($timestamp) { + return date('jS F, Y', $timestamp); + } + + function checkBan() { + if(!isset($_SERVER['REMOTE_ADDR'])) { + // Server misconfiguration + return; + } + + $query = prepare("SELECT * FROM `bans` WHERE `ip` = :ip LIMIT 1"); + $query->bindValue(':ip', $_SERVER['REMOTE_ADDR']); + $query->execute() or error(db_error($query)); + + if($ban = $query->fetch()) { + $body = '
+

You are banned! ;_;

+

You have been banned ' . + ($ban['reason'] ? 'for the following reason:' : 'for an unspecified reason.') . + '

' . + ($ban['reason'] ? + '

' . + $ban['reason'] . + '

' + : '') . + '

Your ban was filed on ' . + formatDate($ban['set']) . + ', and ' . + ($ban['expires'] ? + 'expires on ' . + formatDate($ban['expires']) . + ', which is ' . until($ban['expires']) . ' from now' + : 'does not expire' ) . + '.

+

Your IP address is ' . $_SERVER['REMOTE_ADDR'] . '.

+
'; + + // Show banned page and exit + die(Element('page.html', Array( + 'index' => ROOT, + 'title' => 'Banned', + 'subtitle' => 'You are banned!', + 'body' => $body + ) + )); + } + } + function threadExists($id) { global $board; @@ -117,7 +181,7 @@ } function index($page, $mod=false) { - global $sql, $board; + global $board; $body = ''; $offset = round($page*THREADS_PER_PAGE-THREADS_PER_PAGE); @@ -162,7 +226,7 @@ } function getPages($mod=false) { - global $sql, $board; + global $board; // Count threads $query = query(sprintf("SELECT COUNT(`id`) as `num` FROM `posts_%s` WHERE `thread` IS NULL", $board['uri'])) or error(db_error()); @@ -179,7 +243,7 @@ } function buildIndex() { - global $sql, $board; + global $board; sql_open(); $pages = getPages(); @@ -206,7 +270,7 @@ } function markup(&$body) { - global $sql, $board; + global $board; if(AUTO_UNICODE) { $body = str_replace('...', '…', $body); diff --git a/post.php b/post.php index fccf3f16..6233f807 100644 --- a/post.php +++ b/post.php @@ -59,6 +59,9 @@ // Open database connection sql_open(); + // Check if banned + checkBan(); + // Check if board exists if(!openBoard($post['board'])) error(ERROR_NOBOARD);