1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-11-27 17:00:52 +01:00
vichan/inc/filters.php

145 lines
3.3 KiB
PHP
Raw Normal View History

<?php
2012-04-11 18:49:22 +02:00
/*
* Copyright (c) 2010-2012 Tinyboard Development Group
*/
2012-04-12 16:18:19 +02:00
if (realpath($_SERVER['SCRIPT_FILENAME']) == str_replace('\\', '/', __FILE__)) {
2012-04-11 18:49:22 +02:00
// You cannot request this file directly.
exit;
}
class Filter {
private $condition;
public function __construct(array $arr) {
2012-04-12 16:18:19 +02:00
foreach ($arr as $key => $value)
$this->$key = $value;
}
public function match(array $post, $condition, $match) {
$condition = strtolower($condition);
switch($condition) {
case 'custom':
2012-04-12 16:18:19 +02:00
if (!is_callable($match))
error('Custom condition for filter is not callable!');
return $match($post);
case 'name':
return preg_match($match, $post['name']);
case 'trip':
return preg_match($match, $post['trip']);
case 'email':
return preg_match($match, $post['email']);
case 'subject':
return preg_match($match, $post['subject']);
case 'body':
return preg_match($match, $post['body']);
case 'filename':
2012-04-12 16:18:19 +02:00
if (!$post['has_file'])
return false;
return preg_match($match, $post['filename']);
case 'extension':
2012-04-12 16:18:19 +02:00
if (!$post['has_file'])
return false;
return preg_match($match, $post['body']);
case 'ip':
return preg_match($match, $_SERVER['REMOTE_ADDR']);
case 'op':
return $post['op'] == $match;
case 'has_file':
return $post['has_file'] == $match;
default:
error('Unknown filter condition: ' . $condition);
}
}
public function action() {
global $board;
switch($this->action) {
case 'reject':
error(isset($this->message) ? $this->message : 'Posting throttled by flood filter.');
case 'ban':
2012-04-12 16:18:19 +02:00
if (!isset($this->reason))
error('The ban action requires a reason.');
$reason = $this->reason;
2012-04-12 16:18:19 +02:00
if (isset($this->expires))
$expires = time() + $this->expires;
else
$expires = 0; // Ban indefinitely
2012-04-12 16:18:19 +02:00
if (isset($this->reject))
$reject = $this->reject;
else
$reject = true;
2012-04-12 16:18:19 +02:00
if (isset($this->all_boards))
$all_boards = $this->all_boards;
else
$all_boards = false;
$query = prepare("INSERT INTO `bans` VALUES (NULL, :ip, :mod, :set, :expires, :reason, :board)");
$query->bindValue(':ip', $_SERVER['REMOTE_ADDR']);
$query->bindValue(':mod', -1);
$query->bindValue(':set', time());
2012-04-12 16:18:19 +02:00
if ($expires)
$query->bindValue(':expires', $expires);
else
$query->bindValue(':expires', null, PDO::PARAM_NULL);
2012-04-12 16:18:19 +02:00
if ($reason)
$query->bindValue(':reason', $reason);
else
$query->bindValue(':reason', null, PDO::PARAM_NULL);
2012-04-12 16:18:19 +02:00
if ($all_boards)
$query->bindValue(':board', null, PDO::PARAM_NULL);
else
$query->bindValue(':board', $board['uri']);
$query->execute() or error(db_error($query));
2012-04-12 16:18:19 +02:00
if ($reject) {
if (isset($this->message))
error($message);
checkBan($board['uri']);
exit;
}
break;
default:
error('Unknown filter action: ' . $this->action);
}
}
public function check(array $post) {
2012-04-12 16:18:19 +02:00
foreach ($this->condition as $condition => $value) {
if (!$this->match($post, $condition, $value))
return false;
}
/* match */
return true;
}
}
function do_filters(array $post) {
global $config;
2012-04-12 16:18:19 +02:00
if (!isset($config['flood_filters']))
return;
2012-04-12 16:18:19 +02:00
foreach ($config['flood_filters'] as $arr) {
$filter = new Filter($arr);
2012-04-12 16:18:19 +02:00
if ($filter->check($post))
$filter->action();
}
}