diff --git a/inc/config.php b/inc/config.php index 131ac46c..85335e5a 100644 --- a/inc/config.php +++ b/inc/config.php @@ -15,6 +15,7 @@ $config = Array( 'db' => Array(), + 'memcached' => Array(), 'cookies' => Array(), 'error' => Array(), 'dir' => Array(), @@ -37,9 +38,21 @@ $config['db']['password'] = ''; // Tinyboard database $config['db']['database'] = ''; + // Use a persistent connection + $config['db']['persistent'] = false; // Anything more to add to the DSN string (eg. port=xxx;foo=bar) $config['db']['dsn'] = ''; - + // Timeout duration in seconds (not all drivers support this) + $config['db']['timeout'] = 5; + + + // Optional Memcached server for more cache/optimization (currently at debug state) + $config['memcached']['enabled'] = false; + // Memcached servers to use - http://www.php.net/manual/en/memcached.addservers.php + $config['memcached']['servers'] = Array( + Array('localhost', 11211) + ); + // The name of the session cookie (PHP's $_SESSION) $config['cookies']['session']= 'imgboard'; @@ -608,6 +621,8 @@ // Link imageboard to your Google Analytics account to track users and provide marketing insights. // $config['google_analytics'] = 'UA-xxxxxxx-yy'; + // Keep the Google Analytics cookies to one domain -- ga._setDomainName() + // $config['google_analytics_domain'] = 'www.example.org'; if($_SERVER['SCRIPT_FILENAME'] == str_replace('\\', '/', __FILE__)) { // You cannot request this file directly. diff --git a/inc/database.php b/inc/database.php index 88ca1c0d..3dea39cb 100644 --- a/inc/database.php +++ b/inc/database.php @@ -13,6 +13,9 @@ if(!empty($config['db']['dsn'])) $dsn .= ';' . $config['db']['dsn']; try { + $options = Array(PDO::ATTR_TIMEOUT => $config['db']['timeout']); + if($config['db']['persistent']) + $options[PDO::ATTR_PERSISTENT] = true; return $pdo = new PDO($dsn, $config['db']['user'], $config['db']['password']); } catch(PDOException $e) { $message = $e->getMessage(); @@ -27,17 +30,22 @@ } function sql_close() { - global $pdo; - $pdo = NULL; + global $pdo, $config; + if(!$config['db']['persistent']) + $pdo = NULL; } function prepare($query) { global $pdo; + + sql_open(); return $pdo->prepare($query); } function query($query) { global $pdo; + + sql_open(); return $pdo->query($query); } 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;$indexbindValue(':ip', $m[1]); $query->execute() or error(db_error($query)); + + if($config['memcached']['enabled']) { + // Remove cached ban + $memcached->delete("ban_${m[1]}"); + } } } } @@ -1893,6 +1898,11 @@ $query = prepare("DELETE FROM `bans` WHERE `ip` = :ip"); $query->bindValue(':ip', $ip); $query->execute() or error(db_error($query)); + + if($config['memcached']['enabled']) { + // Remove cached ban + $memcached->delete("ban_${ip}"); + } } elseif($mod['type'] >= $config['mod']['create_notes'] && isset($_POST['note'])) { $query = prepare("INSERT INTO `ip_notes` VALUES(NULL, :ip, :mod, :time, :body)"); $query->bindValue(':ip', $ip);