mirror of
https://github.com/vichan-devel/vichan.git
synced 2024-12-18 10:25:55 +01:00
cached bans with memcached (added Memcached option)
This commit is contained in:
parent
a3cb21ba5b
commit
e7a0eb14f4
@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
$config = Array(
|
$config = Array(
|
||||||
'db' => Array(),
|
'db' => Array(),
|
||||||
|
'memcached' => Array(),
|
||||||
'cookies' => Array(),
|
'cookies' => Array(),
|
||||||
'error' => Array(),
|
'error' => Array(),
|
||||||
'dir' => Array(),
|
'dir' => Array(),
|
||||||
@ -37,8 +38,20 @@
|
|||||||
$config['db']['password'] = '';
|
$config['db']['password'] = '';
|
||||||
// Tinyboard database
|
// Tinyboard database
|
||||||
$config['db']['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)
|
// Anything more to add to the DSN string (eg. port=xxx;foo=bar)
|
||||||
$config['db']['dsn'] = '';
|
$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)
|
// The name of the session cookie (PHP's $_SESSION)
|
||||||
$config['cookies']['session']= 'imgboard';
|
$config['cookies']['session']= 'imgboard';
|
||||||
@ -608,6 +621,8 @@
|
|||||||
|
|
||||||
// Link imageboard to your Google Analytics account to track users and provide marketing insights.
|
// Link imageboard to your Google Analytics account to track users and provide marketing insights.
|
||||||
// $config['google_analytics'] = 'UA-xxxxxxx-yy';
|
// $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__)) {
|
if($_SERVER['SCRIPT_FILENAME'] == str_replace('\\', '/', __FILE__)) {
|
||||||
// You cannot request this file directly.
|
// You cannot request this file directly.
|
||||||
|
@ -13,6 +13,9 @@
|
|||||||
if(!empty($config['db']['dsn']))
|
if(!empty($config['db']['dsn']))
|
||||||
$dsn .= ';' . $config['db']['dsn'];
|
$dsn .= ';' . $config['db']['dsn'];
|
||||||
try {
|
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']);
|
return $pdo = new PDO($dsn, $config['db']['user'], $config['db']['password']);
|
||||||
} catch(PDOException $e) {
|
} catch(PDOException $e) {
|
||||||
$message = $e->getMessage();
|
$message = $e->getMessage();
|
||||||
@ -27,17 +30,22 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function sql_close() {
|
function sql_close() {
|
||||||
global $pdo;
|
global $pdo, $config;
|
||||||
$pdo = NULL;
|
if(!$config['db']['persistent'])
|
||||||
|
$pdo = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
function prepare($query) {
|
function prepare($query) {
|
||||||
global $pdo;
|
global $pdo;
|
||||||
|
|
||||||
|
sql_open();
|
||||||
return $pdo->prepare($query);
|
return $pdo->prepare($query);
|
||||||
}
|
}
|
||||||
|
|
||||||
function query($query) {
|
function query($query) {
|
||||||
global $pdo;
|
global $pdo;
|
||||||
|
|
||||||
|
sql_open();
|
||||||
return $pdo->query($query);
|
return $pdo->query($query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,6 +91,18 @@
|
|||||||
if(preg_match('/^\:\:(ffff\:)?(\d+\.\d+\.\d+\.\d+)$/', $__ip, $m))
|
if(preg_match('/^\:\:(ffff\:)?(\d+\.\d+\.\d+\.\d+)$/', $__ip, $m))
|
||||||
$_SERVER['REMOTE_ADDR'] = $m[2];
|
$_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) {
|
function loadThemeConfig($_theme) {
|
||||||
@ -162,7 +174,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function openBoard($uri) {
|
function openBoard($uri) {
|
||||||
sql_open();
|
|
||||||
|
|
||||||
$query = prepare("SELECT * FROM `boards` WHERE `uri` = :uri LIMIT 1");
|
$query = prepare("SELECT * FROM `boards` WHERE `uri` = :uri LIMIT 1");
|
||||||
$query->bindValue(':uri', $uri);
|
$query->bindValue(':uri', $uri);
|
||||||
@ -232,40 +244,10 @@
|
|||||||
return date('jS F, Y', $timestamp);
|
return date('jS F, Y', $timestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkBan() {
|
function displayBan($ban) {
|
||||||
global $config;
|
global $config;
|
||||||
|
|
||||||
if(!isset($_SERVER['REMOTE_ADDR'])) {
|
$body = '<div class="ban">
|
||||||
// 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 = '<div class="ban">
|
|
||||||
<h2>You are banned! ;_;</h2>
|
<h2>You are banned! ;_;</h2>
|
||||||
<p>You have been banned ' .
|
<p>You have been banned ' .
|
||||||
($ban['reason'] ? 'for the following reason:' : 'for an unspecified reason.') .
|
($ban['reason'] ? 'for the following reason:' : 'for an unspecified reason.') .
|
||||||
@ -319,16 +301,62 @@
|
|||||||
: '<em>will not expire</em>.' ) .
|
: '<em>will not expire</em>.' ) .
|
||||||
'</span></p>
|
'</span></p>
|
||||||
<p>Your IP address is <strong>' . $_SERVER['REMOTE_ADDR'] . '</strong>.</p>
|
<p>Your IP address is <strong>' . $_SERVER['REMOTE_ADDR'] . '</strong>.</p>
|
||||||
</div>';
|
</div>';
|
||||||
|
|
||||||
// Show banned page and exit
|
// Show banned page and exit
|
||||||
die(Element('page.html', Array(
|
die(Element('page.html', Array(
|
||||||
'config' => $config,
|
'config' => $config,
|
||||||
'title' => 'Banned',
|
'title' => 'Banned',
|
||||||
'subtitle' => 'You are banned!',
|
'subtitle' => 'You are banned!',
|
||||||
'body' => $body
|
'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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if($config['memcached']['enabled'])
|
||||||
|
$memcached->set("ban_${_SERVER['REMOTE_ADDR']}", $ban, $ban['expires']);
|
||||||
|
displayBan($ban);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -535,7 +563,7 @@
|
|||||||
$body = '';
|
$body = '';
|
||||||
$offset = round($page*$config['threads_per_page']-$config['threads_per_page']);
|
$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 = 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);
|
$query->bindValue(1, $offset, PDO::PARAM_INT);
|
||||||
@ -887,7 +915,7 @@
|
|||||||
|
|
||||||
function buildIndex() {
|
function buildIndex() {
|
||||||
global $board, $config;
|
global $board, $config;
|
||||||
sql_open();
|
|
||||||
|
|
||||||
$pages = getPages();
|
$pages = getPages();
|
||||||
|
|
||||||
@ -1021,7 +1049,7 @@
|
|||||||
if(count($cites[0]) > $config['max_cites']) {
|
if(count($cites[0]) > $config['max_cites']) {
|
||||||
error($config['error']['toomanycites']);
|
error($config['error']['toomanycites']);
|
||||||
}
|
}
|
||||||
sql_open();
|
|
||||||
for($index=0;$index<count($cites[0]);$index++) {
|
for($index=0;$index<count($cites[0]);$index++) {
|
||||||
$cite = $cites[2][$index];
|
$cite = $cites[2][$index];
|
||||||
$query = prepare(sprintf("SELECT `thread`,`id` FROM `posts_%s` WHERE `id` = :id LIMIT 1", $board['uri']));
|
$query = prepare(sprintf("SELECT `thread`,`id` FROM `posts_%s` WHERE `id` = :id LIMIT 1", $board['uri']));
|
||||||
@ -1043,7 +1071,7 @@
|
|||||||
if(count($cites[0]) > $config['max_cites']) {
|
if(count($cites[0]) > $config['max_cites']) {
|
||||||
error($config['error']['toomanycross']);
|
error($config['error']['toomanycross']);
|
||||||
}
|
}
|
||||||
sql_open();
|
|
||||||
for($index=0;$index<count($cites[0]);$index++) {
|
for($index=0;$index<count($cites[0]);$index++) {
|
||||||
$_board = $cites[2][$index];
|
$_board = $cites[2][$index];
|
||||||
$cite = @$cites[3][$index];
|
$cite = @$cites[3][$index];
|
||||||
@ -1390,7 +1418,7 @@
|
|||||||
switch($type) {
|
switch($type) {
|
||||||
case 'jpg':
|
case 'jpg':
|
||||||
case 'jpeg':
|
case 'jpeg':
|
||||||
if(!$image = @imagecreatefromjpeg($source_pic)) {
|
if(!$image = imagecreatefromjpeg($source_pic)) {
|
||||||
unlink($source_pic);
|
unlink($source_pic);
|
||||||
error($config['error']['invalidimg']);
|
error($config['error']['invalidimg']);
|
||||||
}
|
}
|
||||||
|
10
mod.php
10
mod.php
@ -1276,6 +1276,11 @@
|
|||||||
$query = prepare("DELETE FROM `bans` WHERE `ip` = :ip");
|
$query = prepare("DELETE FROM `bans` WHERE `ip` = :ip");
|
||||||
$query->bindValue(':ip', $m[1]);
|
$query->bindValue(':ip', $m[1]);
|
||||||
$query->execute() or error(db_error($query));
|
$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 = prepare("DELETE FROM `bans` WHERE `ip` = :ip");
|
||||||
$query->bindValue(':ip', $ip);
|
$query->bindValue(':ip', $ip);
|
||||||
$query->execute() or error(db_error($query));
|
$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'])) {
|
} elseif($mod['type'] >= $config['mod']['create_notes'] && isset($_POST['note'])) {
|
||||||
$query = prepare("INSERT INTO `ip_notes` VALUES(NULL, :ip, :mod, :time, :body)");
|
$query = prepare("INSERT INTO `ip_notes` VALUES(NULL, :ip, :mod, :time, :body)");
|
||||||
$query->bindValue(':ip', $ip);
|
$query->bindValue(':ip', $ip);
|
||||||
|
Loading…
Reference in New Issue
Block a user