Permalink
Browse files

Integrate stopforumspam.com

  • Loading branch information...
r04r committed Oct 2, 2015
1 parent 9693a57 commit c03092a7c88020e45711d706fbfcfa67d476eb37
Showing with 214 additions and 11 deletions.
  1. +2 −1 composer.json
  2. +61 −8 composer.lock
  3. +69 −0 includes/async_functions.php
  4. +7 −0 includes/config.example.php
  5. +3 −1 includes/functions.php
  6. +8 −1 includes/header.php
  7. +4 −0 rabbitmq/.htaccess
  8. +60 −0 rabbitmq/worker.php
View
@@ -1,5 +1,6 @@
{
"require": {
"geoip2/geoip2": "~2.0"
"geoip2/geoip2": "~2.0",
"videlalvaro/php-amqplib": "2.5.*"
}
}
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,69 @@
<?php
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
class AsyncToRabbitMQ {
private $channel;
function __call($name, $arguments) {
$msg = json_encode(array(
'name' => $name,
'arguments' => $arguments
));
$this->getChannel()->basic_publish(new AMQPMessage($msg), 'tinybbs', 'async.'.$name);
}
function getChannel() {
if($this->channel) return $this->channel;
$connection = new AMQPStreamConnection(RABBITMQ_SERVER, RABBITMQ_PORT, RABBITMQ_USER, RABBITMQ_PASS, RABBITMQ_VHOST);
$this->channel = $connection->channel();
$this->channel->exchange_declare('tinybbs', 'topic', false, true, false);
return $this->channel;
}
}
class AsyncImplementation {
function __construct() {
global $link;
$this->link = $link;
}
function log($msg) {
if(!defined("IS_ASYNC_WORKER")) return;
echo $msg."\n";
}
function checkUID($uid, $ip) {
$result = json_decode(file_get_contents("http://api.stopforumspam.org/api?ip=" . urlencode($ip) . "&f=json"));
if($result == null)
throw new Exception("Could not retrieve data from stopforumspam.com");
if($result->success && $result->ip->appears) {
$reason = "The IP address ($ip) used to create your UID has been listed in stopforumspam.com, so your account has been banned.";
$this->link->insertorupdate("ip_bans", array(
"ip_address" => $ip,
"expiry" => 0,
"filed" => "UNIX_TIMESTAMP()",
"who" => "",
"stealth" => 0,
"reason" => $reason
));
log_mod("ban_ip", $ip);
$this->link->insertorupdate("uid_bans", array(
"uid" => $uid,
"expiry" => 0,
"filed" => "UNIX_TIMESTAMP()",
"who" => "",
"stealth" => 0,
"reason" => $reason
));
log_mod("ban_uid", $uid);
$this->log("Banned $uid / $ip");
}
}
}
@@ -18,6 +18,13 @@
define('GOOGLE_ANALYTICS_ID', '');
define('GOOGLE_ANALYTICS_DOMAIN', '');
// RabbitMQ settings can be left blank but performance will suffer
define('RABBITMQ_SERVER', '');
define('RABBITMQ_PORT', 5672);
define('RABBITMQ_VHOST', '');
define('RABBITMQ_USER', '');
define('RABBITMQ_PASS', '');
//define('GOOGLE_TOKEN', ''); // Used for android notifications. You likely can't use it without a custom app
define('ENABLE_PROFILER', false);
define('ADMIN_PROFILER', true); // require administrator for profiler to activate
View
@@ -844,7 +844,7 @@ function nameAndTripcode($name) {
}
function create_id() {
global $link, $DEFCON, $proxy;
global $link, $DEFCON, $proxy, $async;
if(DEFCON<5 || $proxy) return false; // DEFCON 4.
/*if(ENABLE_RECAPTCHA_ON_BOT){
@@ -871,6 +871,8 @@ function create_id() {
setcookie('password', $password, $_SERVER['REQUEST_TIME'] + 315569260, '/', COOKIE_DOMAIN);
$_COOKIE['password'] = $password;
$_SESSION['UID'] = $user_id;
$async->checkUID($user_id, $_SERVER['REMOTE_ADDR']);
}
function generate_password() {
View
@@ -38,9 +38,11 @@ function ipCIDRCheck ($IP, $CIDR) {
}
$_start_time = microtime(); //Prepare our neat xx seconds to load thingy at the bottom of the page.
require_once('vendor/autoload.php');
require_once('includes/config.php');
require_once('includes/database.class.php');
require_once('includes/functions.php');
require_once('includes/async_functions.php');
require_once('includes/unicode.php');
/*
@@ -101,11 +103,16 @@ function abortForMaintenance($error) {
require("includes/private.php");
}
if($link->getVersion() != DB_VERSION) {
abortForMaintenance("Database version mismatch! The Board has likely been upgraded lately, and the administrator has not yet executed includes/upgrade.php");
}
if(RABBITMQ_SERVER) {
$async = new AsyncToRabbitMQ();
}else{
$async = new AsyncImplementation();
}
date_default_timezone_set('UTC');
header('Content-Type: text/html; charset=UTF-8');
View
@@ -0,0 +1,4 @@
Options -Indexes -ExecCGI
Order Allow,Deny
Deny from all
View
@@ -0,0 +1,60 @@
<?php
if(php_sapi_name() != 'cli') die("Must be ran through CLI!");
chdir(realpath(dirname(__FILE__)).'/..');
define("IS_ASYNC_WORKER", true);
require_once('vendor/autoload.php');
require_once('includes/config.php');
require_once('includes/database.class.php');
require_once('includes/functions.php');
require_once('includes/async_functions.php');
$link = new db($db_info['server'], $db_info['username'], $db_info['password'], $db_info['database']);
if(file_exists("includes/private.php")){
require("includes/private.php");
}
$async = new AsyncImplementation();
use PhpAmqpLib\Connection\AMQPStreamConnection;
$connection = new AMQPStreamConnection(RABBITMQ_SERVER, RABBITMQ_PORT, RABBITMQ_USER, RABBITMQ_PASS, RABBITMQ_VHOST);
$channel = $connection->channel();
$channel->exchange_declare('tinybbs', 'topic', false, true, false);
list($queue_name, ,) = $channel->queue_declare("phpworker", false, true, false, false);
$binding_keys = array("async.*");
foreach($binding_keys as $binding_key) {
$channel->queue_bind($queue_name, 'tinybbs', $binding_key);
}
echo '[*] Waiting for commands. To exit press CTRL+C', "\n";
$callback = function($msg){
global $async;
echo "\n[x] ",$msg->delivery_info['routing_key'], ': ', $msg->body, "\n";
$data = json_decode($msg->body, true);
try {
call_user_func_array(array($async, $data["name"]), $data["arguments"]);
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
}catch(Exception $e) {
$msg->delivery_info['channel']->basic_nack($msg->delivery_info['delivery_tag'], false, true);
echo "Caught exception: ", $e->getMessage(), "\n";
sleep(5);
}
};
$channel->basic_consume($queue_name, '', false, false, false, false, $callback);
while(count($channel->callbacks)) {
$channel->wait();
}
$channel->close();
$connection->close();
?>

1 comment on commit c03092a

@parhamr

This comment has been minimized.

Show comment
Hide comment
@parhamr

parhamr commented on c03092a Oct 3, 2015

😻

Please sign in to comment.