From e7a0eb14f4171943d4ab034d751bd74cfb5dd788 Mon Sep 17 00:00:00 2001 From: Savetheinternet Date: Sat, 23 Apr 2011 00:24:15 +1000 Subject: cached bans with memcached (added Memcached option) --- inc/functions.php | 124 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 76 insertions(+), 48 deletions(-) (limited to 'inc/functions.php') diff --git a/inc/functions.php b/inc/functions.php index 318cfa1c..6ecb34ce 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -91,6 +91,18 @@ if(preg_match('/^\:\:(ffff\:)?(\d+\.\d+\.\d+\.\d+)$/', $__ip, $m)) $_SERVER['REMOTE_ADDR'] = $m[2]; } + + if($config['memcached']['enabled']) + memcached_open(); + } + + // Memcached + function memcached_open() { + global $memcached, $config; + if($memcached) return; + + $memcached = new Memcached(); + $memcached->addServers($config['memcached']['servers']); } function loadThemeConfig($_theme) { @@ -162,7 +174,7 @@ } function openBoard($uri) { - sql_open(); + $query = prepare("SELECT * FROM `boards` WHERE `uri` = :uri LIMIT 1"); $query->bindValue(':uri', $uri); @@ -232,40 +244,10 @@ return date('jS F, Y', $timestamp); } - function checkBan() { + function displayBan($ban) { global $config; - if(!isset($_SERVER['REMOTE_ADDR'])) { - // Server misconfiguration - return; - } - - $query = prepare("SELECT * FROM `bans` WHERE `ip` = :ip ORDER BY `expires` IS NULL DESC, `expires` DESC, `expires` DESC LIMIT 1"); - $query->bindValue(':ip', $_SERVER['REMOTE_ADDR']); - $query->execute() or error(db_error($query)); - if($query->rowCount() < 1 && $config['ban_range']) { - $query = prepare("SELECT * FROM `bans` WHERE :ip REGEXP CONCAT('^', REPLACE(REPLACE(`ip`, '.', '\\.'), '*', '[0-9]*'), '$') ORDER BY `expires` IS NULL DESC, `expires` DESC LIMIT 1"); - $query->bindValue(':ip', $_SERVER['REMOTE_ADDR']); - $query->execute() or error(db_error($query)); - } - - if($ban = $query->fetch()) { - if($ban['expires'] && $ban['expires'] < time()) { - // Ban expired - $query = prepare("DELETE FROM `bans` WHERE `ip` = :ip AND `expires` = :expires LIMIT 1"); - $query->bindValue(':ip', $_SERVER['REMOTE_ADDR']); - $query->bindValue(':expires', $ban['expires'], PDO::PARAM_INT); - $query->execute() or error(db_error($query)); - - if($config['ban_range']) { - $query = prepare("DELETE FROM `bans` WHERE :ip REGEXP CONCAT('^', REPLACE(REPLACE(`ip`, '.', '\\.'), '*', '[0-9a-f]*'), '$') AND `expires` = :expires LIMIT 1"); - $query->bindValue(':ip', $_SERVER['REMOTE_ADDR']); - $query->bindValue(':expires', $ban['expires'], PDO::PARAM_INT); - $query->execute() or error(db_error($query)); - } - return; - } - $body = '
+ $body = '

You are banned! ;_;

You have been banned ' . ($ban['reason'] ? 'for the following reason:' : 'for an unspecified reason.') . @@ -319,16 +301,62 @@ : 'will not expire.' ) . '

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

-
'; +
'; + + // Show banned page and exit + die(Element('page.html', Array( + 'config' => $config, + 'title' => 'Banned', + 'subtitle' => 'You are banned!', + 'body' => $body + ) + )); + } + + function checkBan() { + global $config, $memcached; + + if(!isset($_SERVER['REMOTE_ADDR'])) { + // Server misconfiguration + return; + } + + if($config['memcached']['enabled']) { + // Cached ban? + if($ban = $memcached->get("ban_${_SERVER['REMOTE_ADDR']}")) { + displayBan($ban); + } + } + + $query = prepare("SELECT * FROM `bans` WHERE `ip` = :ip ORDER BY `expires` IS NULL DESC, `expires` DESC, `expires` DESC LIMIT 1"); + $query->bindValue(':ip', $_SERVER['REMOTE_ADDR']); + $query->execute() or error(db_error($query)); + if($query->rowCount() < 1 && $config['ban_range']) { + $query = prepare("SELECT * FROM `bans` WHERE :ip REGEXP CONCAT('^', REPLACE(REPLACE(`ip`, '.', '\\.'), '*', '[0-9]*'), '$') ORDER BY `expires` IS NULL DESC, `expires` DESC LIMIT 1"); + $query->bindValue(':ip', $_SERVER['REMOTE_ADDR']); + $query->execute() or error(db_error($query)); + } + + if($ban = $query->fetch()) { + if($ban['expires'] && $ban['expires'] < time()) { + // Ban expired + $query = prepare("DELETE FROM `bans` WHERE `ip` = :ip AND `expires` = :expires LIMIT 1"); + $query->bindValue(':ip', $_SERVER['REMOTE_ADDR']); + $query->bindValue(':expires', $ban['expires'], PDO::PARAM_INT); + $query->execute() or error(db_error($query)); + + if($config['ban_range']) { + $query = prepare("DELETE FROM `bans` WHERE :ip REGEXP CONCAT('^', REPLACE(REPLACE(`ip`, '.', '\\.'), '*', '[0-9a-f]*'), '$') AND `expires` = :expires LIMIT 1"); + $query->bindValue(':ip', $_SERVER['REMOTE_ADDR']); + $query->bindValue(':expires', $ban['expires'], PDO::PARAM_INT); + $query->execute() or error(db_error($query)); + } + return; + } - // Show banned page and exit - die(Element('page.html', Array( - 'config' => $config, - 'title' => 'Banned', - 'subtitle' => 'You are banned!', - 'body' => $body - ) - )); + if($config['memcached']['enabled']) + $memcached->set("ban_${_SERVER['REMOTE_ADDR']}", $ban, $ban['expires']); + displayBan($ban); } } @@ -535,7 +563,7 @@ $body = ''; $offset = round($page*$config['threads_per_page']-$config['threads_per_page']); - sql_open(); + $query = prepare(sprintf("SELECT * FROM `posts_%s` WHERE `thread` IS NULL ORDER BY `sticky` DESC, `bump` DESC LIMIT ?,?", $board['uri'])); $query->bindValue(1, $offset, PDO::PARAM_INT); @@ -887,7 +915,7 @@ function buildIndex() { global $board, $config; - sql_open(); + $pages = getPages(); @@ -1021,7 +1049,7 @@ if(count($cites[0]) > $config['max_cites']) { error($config['error']['toomanycites']); } - sql_open(); + for($index=0;$index $config['max_cites']) { error($config['error']['toomanycross']); } - sql_open(); + for($index=0;$index