Extension:CheckEmailAddress

From mediawiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced-invert.svg
CheckEmailAddress
Release status: unmaintained
Implementation User identity
Description Block the creation of accounts if certain email-address criteria are met
Author(s) Mirko Schiefelbein (Naitsirktalk)
Latest version 1.0 (2017-02-27)
MediaWiki 1.17+
License GNU General Public License 2.0 or later
Download See here
  • $wgCheckEmailAddressDomainSources
  • $wgCheckEmailAddressNameSigns

Check usage and version matrix.

The CheckEmailAddress extension checks the email-address during the registration process. It aims at preventing spammers from creating an account on wikis which require email confirmation. CheckEmailAddress basically provides two features:

  • check the domain of the email-address:
If it matches a certain provider-address, e.g. a known trash-email-address, account creation fails, and the user is asked to register with a different email-address.
  • check the name of the email-address:
If the name meets certain criteria, account creation fails, and the user is asked to provide a different email-address.

Download instructions[edit]

Please copy and paste the code found below and place it in

  • $IP/extensions/CheckEmailAddress/CheckEmailAddress.php
  • $IP/extensions/CheckEmailAddress/CheckEmailAddress.hooks.php
  • $IP/extensions/CheckEmailAddress/CheckEmailAddress.i18n.php.

Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

Installation[edit]

To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/CheckEmailAddress/CheckEmailAddress.php");
$wgCheckEmailAddressDomainSources = array(
	'type'	=> "CEASRC_FILE",
	'src'	=> "$IP/Blacklistfile.txt"
);
$wgCheckEmailAddressNameSigns = array(
	array(
		'sign'		=> ".",
		'maxcount'	=> 5,
	),
);

Create a textfile "Blacklistfile.txt". Fill in domains of email-addresses that are known as trash- or once-only-mails, e.g. mailinator.com. Just write the domains you want to disable one below the other, without "@". It should look something like this:

mailinator.com
spoofmail.de
...

For example, you can get a list of such domains in a burner-email-providers repo.

Save Blacklistfile.txt in your root. Note: the root directory of your MediaWiki installation is the same directory that holds LocalSettings.php.

After that users with email like abc@mailinator.com or abc.subdomain.mailinator.com cannot be registered.

Configuration parameters[edit]

$wgCheckEmailAddressDomainSources[edit]

This is a simple array that informs what type the source is and which sourcefile is used. At the moment only "CEASRC_FILE" is possible as "type". Point "src" to the file containing the email-addresses.

$wgCheckEmailAddressNameSigns[edit]

This array defines the signs and their limit in the name-part of the email-address, i.e. in the part before "@". Since this variable is an array of arrays, you can name several signs with different limits. If for example known spammers keep registring with characteristic email-names, you can now exclude them by patterns. In the configuration given above, email-names are searched for dots ("."). If they count equal or more than five, account creation fails.

Code[edit]

CheckEmailAddress consists of three files.

CheckEmailAddress.php[edit]

<?php
if ( !defined( 'MEDIAWIKI' ) ) {
	exit(1);
}

$wgExtensionCredits['other'][] = array(
	'path'           => __FILE__,
	'name'           => 'CheckEmailAddress',
	'author'         => 'Mirko Schiefelbein',
	'url'            => 'https://www.mediawiki.org/wiki/Extension:CheckEmailAddress',
	'descriptionmsg' => 'checkemailaddress-desc',
	'version'        => '1.0',
);

$wgExtensionMessagesFiles[ 'CheckEmailAddress' ] = dirname( __FILE__ ) . '/CheckEmailAddress.i18n.php';
$wgAutoloadClasses[ 'CheckEmailAddressHooks' ] = dirname( __FILE__ ) . '/CheckEmailAddress.hooks.php';

/** for future integration: CheckEmailAddress source types */
define( 'CEASRC_MSG',       0 );	///< For internal usage
define( 'CEASRC_LOCALPAGE', 1 );	///< Local wiki page
define( 'CEASRC_URL',	    2 );	///< Load blacklist from URL
define( 'CEASRC_FILE',      3 );	///< Load from file

/** Array of CheckEmailAddress sources */
$wgCheckEmailAddressDomainSources = array();

/** Array of CheckEmailAddress names */
$wgCheckEmailAddressNameSigns = array();

/** Hooks */
$wgHooks['AbortNewAccount'][] = 'CheckEmailAddressHooks::abortNewAccountDomain';
$wgHooks['AbortNewAccount'][] = 'CheckEmailAddressHooks::abortNewAccountName';

CheckEmailAddress.hooks.php[edit]

<?php
class CheckEmailAddressHooks {

	/* Check Domain if once- or trash-mail
	 */
	public static function abortNewAccountDomain ( $user, &$abortError ) {
		global $wgCheckEmailAddressDomainSources;

		$fulladdress = $user->getEmail();
		$domainat = stristr( $fulladdress, '@' );
		$domain = mb_strtolower( $domainat );

		if( empty( $wgCheckEmailAddressDomainSources ) ) {
			return true;
		}

		if( $wgCheckEmailAddressDomainSources[ 'type' ] == CEASRC_FILE ) {
			$srcfile = $wgCheckEmailAddressDomainSources[ 'src' ];

			if( file_exists( $srcfile ) ) {
				$domlines = file( $srcfile );
			} else {
				return true;
			}

			foreach( $domlines as $domline ) {
				$domline = rtrim( $domline, "\r\n");
				$entry = "/@([-a-z0-9]+\.)*". str_replace('.', '\.', $domline) ."$/";
				if( preg_match( $entry, $domain ) ) {
					$abortError = wfMessage( 'checkemailaddress-domainerror' )->text();
					return false;
				}
			}
		}
		return true;
	}


	/* Check Name if spammy indicators
	 */
	public static function abortNewAccountName ( $user, &$abortError ) {
		global $wgCheckEmailAddressNameSigns;

		$fulladdress = $user->getEmail();
		$atpos = strpos( $fulladdress, "@" );
		$nameat = substr( $fulladdress, 0, $atpos );
		$name = mb_strtolower( $nameat );

		if( empty( $wgCheckEmailAddressNameSigns ) ) {
			return true;
		}

		foreach( $wgCheckEmailAddressNameSigns as $key => $value ) {
			$sign = $value[ 'sign' ];
			$maxcount = $value[ 'maxcount' ];
			$signcount = substr_count( $name, $sign );

			if( $signcount >= $maxcount ) {
				$abortError = wfMessage( 'checkemailaddress-nameerror' )->text();
				return false;
			}
		}
		return true;
	}
}

CheckEmailAddress.i18n.php[edit]

<?php
$messages = array();

/** English
* @author Naitsirk
*/
$messages['en'] = array(
	'checkemailaddress-desc' => 'Allows to exclude certain email addresses from registration',
	'checkemailaddress' => '# This is a list of forbidden domains for emails which therefore are disabled to register with',
	'checkemailaddress-domainerror' => 'Once-only and trash email addresses are not allowed on {{SITENAME}}. Please register with a different email address',
	'checkemailaddress-nameerror' => 'The email address you submitted is supposed to be spam. If it is not, please email the admin',
);

/** German (Deutsch)
* @author Naitsirk
* @author Kghbln
*/
$messages['de'] = array(
	'checkemailaddress-desc' => 'Ermöglicht es, bestimmte E-Mail-Adressen von der Registrierung auszuschliessen',
	'checkemailaddress' => '# Dies ist eine Liste unzulässiger Domainnamen für E-Mails, mit denen eine Registrierung nicht möglich ist',
	'checkemailaddress-domainerror' => 'Wegwerf- und Einmal-E-Mail-Adressen sind auf {{SITENAME}} nicht zulässig. Bitte registriere dich mit einer anderen E-Mail-Adresse',
	'checkemailaddress-nameerror' => 'Es handelt sich vermutlich um eine Spam-E-Mail-Adresse. Sofen dies nicht der Fall ist, kontaktiere bitte einen Administrator per E-Mail',
);

/** German (formal address) (\202aDeutsch (Sie-Form)\202c)
* @author Kghbln
*/
$messages['de-formal'] = array(
	'checkemailaddress-domainerror' => 'Wegwerf- und Einmal-E-Mail-Adressen sind auf {{SITENAME}} nicht zulässig. Bitte registrieren Sie sich mit einer anderen E-Mail-Adresse',
	'checkemailaddress-nameerror' => 'Es handelt sich vermutlich um eine Spam-E-Mail-Adresse. Sofen dies nicht der Fall ist, kontaktieren Sie bitte einen Administrator per E-Mail',
);

/** Spanish
* @author gaboflowers
*/
$messages['es'] = array(
	'checkemailaddress-desc' => 'Permite excluir algunas direcciones de correo electrónico de ser registradas',
	'checkemailaddress' => '# Ésta es una lista de dominios prohibidos, con cuyas direcciones de correo electrónico no se permite registrarse',
	'checkemailaddress-domainerror' => 'No se permiten direcciones correo electrónico de un solo uso en {{SITENAME}}. Por favor, utiliza una dirección de correo distinta',
	'checkemailaddress-nameerror' => 'La dirección de correo que enviaste está catalogada como spam. Si no lo es, por favor contacta al administrador',
);

/**  Dutch (Nederlands)
* @author Arent
*/
$messages['nl'] = array(
	'checkemailaddress-desc' => 'Maakt het mogelijk om bepaalde emailadressen uit te sluiten voor accountregistratie',
	'checkemailaddress' => '# Dit is een lijst met email-domeinen waarmee geen accountregistratie kan plaatsvinden',
	'checkemailaddress-domainerror' => 'Eenmalige en wegwerpadressen zijn niet toegestaan op {{SITENAME}}. Gebruik a.u.b. een ander emailadres',
	'checkemailaddress-nameerror' => 'Het gebruikte emailadres is als spam-adres geclassificeerd. Mocht dit onterecht gebeurd zijn neem dan contact op met de beheerder',
);

/** Polish
 * @author jakubsam
 */
$messages['pl'] = array(
	'checkemailaddress-desc' => 'Pozwala na zabronienie rejestracji z niektórych adresów e-mail',
	'checkemailaddress' => '# To jest lista domen e-mail z których rejestracja jest zabroniona.',
	'checkemailaddress-domainerror' => 'Tymczasowe i fałszywe adresy e-mail nie są dozwolone na {{SITENAME}}. Proszę użyć prawdziwego adresu e-mail',
	'checkemailaddress-nameerror' => 'Wybrany e-mail jest adresem oznaczonym jako spam. Proszę podać innego maila',
);

/** Russian
* @author Martynov Maxim
*/
$messages['ru'] = array(
	'checkemailaddress-desc' => 'Позволяет запрещать регистрацию с определенных e-mail адресов',
	'checkemailaddress' => '# Список доменов, с которых запрещена регистрация пользователей',
	'checkemailaddress-domainerror' => 'На {{SITENAME}} запрещена регистрация с использованием одноразовых e-mail или с доменов, заподозренных в спаме. Пожалуйста, используйте другой e-mail для регистрации',
	'checkemailaddress-nameerror' => 'Ваш e-mail использовался для рассылки спама. Если это не так, свяжитесь с администратором',
);

Feedback[edit]

Feedback is greatly appreciated. Give me a call when you have improvements in mind, have identified certain spam-name-patterns, or have done some internationalization.