Automatically Block Spam Users

From WinWolf3D/WDC

For MediaWiki 1.5.2.

If you block guest users from editing pages, spammers will start making junk user accounts. Luckily their usernames follow some patterns. This code will cause user names that match the patterns to be automatically blocked upon creation.

includes/User.php

  • Open includes/User.php
  • Find:
function isAllowedToCreateAccount() {
  • Add BEFORE:
// 20080907 - Detect spam-like names. - ANB
function isSpamName($userName) {
	$patterns = array(
		"/^([A-Z0-9][a-z0-9]{4}){2}$/", // AcdarZelsi, C4tboCtroc
		"/^([A-Z0-9][a-z0-9]{2}){2}$/", // RozNar
	);
	foreach( $patterns as $pattern ) {
		if (preg_match($pattern, $userName)) {
			return true;
		}
	}
	return false;
}
  • Close file

includes/SpecialUserlogin.php

  • Open includes/SpecialUserlogin.php
  • Find:
function &initUser( &$u ) {
  • Find in function &initUser:
$u->setOption( 'rememberpassword', $r );
  • Add AFTER (you have two choices).

This choice causes the sysop user to block the spam account and does not log the action.

// 20080907 - Automatically block spam-like names. - ANB
if ($u->isSpamName($u->mName)) {
	$blockinguserid = $u->mId;
	// Try to find the sysop's user id and use that as the blocking user.
	$dbr =& wfGetDB( DB_SLAVE );
	$s = $dbr->selectRow( 'user_groups', array( 'ug_user' ),
  		array( 'ug_group' => 'sysop' ), 'SpecialUserlogin::initUser' );
	if ( $s !== false ) {
		$blockinguserid = $s->ug_user;
	}
	// Block the new user.
	$block = new Block( $u->mName, $u->mId, $blockinguserid, 'Suspected spammer', 
		wfTimestampNow() );
	$block->insert();
}

This choice makes it look like the user blocked itself and logs the block. Between the two, I'd use the first.

// 20080907 - Automatically block spam-like names. - ANB
if ($u->isSpamName($u->mName)) {
	// The spammer will appear to block itself in the block list and log.
	// Block the new user.
	$block = new Block( $u->mName, $u->mId, $u->mId, 'Suspected spammer', 
		wfTimestampNow() );
	$block->insert();
	// Log the blocking.
	global $wgUser;
	$wgUser = $u;
	$log = new LogPage( 'block' );
	$log->addEntry( 'block', Title::makeTitle( NS_USER, $u->mName ), 
		'Suspected spammer', 'infinite' );
}
  • Close file