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