Extension:CheckEmailAddress

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
CheckEmailAddress

Release status: beta

Implementation User identity
Description Block the creation of accounts if certain email-address criteria are met
Author(s) Mirko Schiefelbein (NaitsirkTalk)
Last version 0.9 (02-04-2012)
MediaWiki 1.17.0+ (probably former versions as well, not tested)
License No license specified
Download Extension:CheckEmailAddress#Code
Parameters

$wgCheckEmailAddressDomainSources, $wgCheckEmailAddressNameSigns

Hooks used
AbortNewAccount

Check usage (experimental)

Contents

[edit] What can this extension do?

This simple 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.

[edit] Download instructions

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.

[edit] Installation

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
...

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

[edit] Configuration parameters

[edit] $wgCheckEmailAddressDomainSources

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.

[edit] $wgCheckEmailAddressNameSigns

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.

[edit] Code

CheckEmailAddress consists of three files.

[edit] CheckEmailAddress.php

<?php
if ( !defined( 'MEDIAWIKI' ) ) {
        exit(1);
}
 
$wgExtensionCredits['other'][] = array(
        'path'           => __FILE__,
        'name'           => 'CheckEmailAddress',
        'author'         => 'Mirko Schiefelbein',
        'url'            => 'http://www.mediawiki.org/wiki/Extension:CheckEmailAddress',
        'descriptionmsg' => 'checkemailaddress-desc',
        'version'                =>     '0.9',
);
 
$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';

[edit] CheckEmailAddress.hooks.php

<?php
class CheckEmailAddressHooks {
 
        /* Check Domain if once- or trash-mail
        */
        public static function abortNewAccountDomain ( $user, &$abortError ) {
                global $wgUser, $wgCheckEmailAddressDomainSources;
 
                $fulladdress = $user->getEmail();
                $domainat = stristr( $fulladdress, '@' );
                $domain = mb_strtolower( $domainat );
 
                if( !is_array( $wgCheckEmailAddressDomainSources ) || count( $wgCheckEmailAddressDomainSources ) <= 0 ) {
                        return '';
                }
 
                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 = "/@".$domline."/";
                                if( preg_match( $entry, $domain ) ) {
                                        $abortError = wfMsg( 'checkemailaddress-domainerror' );
                                        unset( $domline );
                                        return false;
                                }
                        }
                }
                unset( $domline );
                return true;
        }
 
 
        /* Check Name if spammy indicators
        */
        public static function abortNewAccountName ( $user, &$abortError ) {
                global $wgUser, $wgCheckEmailAddressNameSigns;
 
                $fulladdress = $user->getEmail();
                $at = "@";
                $atpos = strpos( $fulladdress, $at );
                $nameat = substr( $fulladdress, 0, $atpos );
                $name = mb_strtolower( $nameat );
 
                if( !is_array( $wgCheckEmailAddressNameSigns ) || count( $wgCheckEmailAddressNameSigns ) <= 0 ) {
                        return '';
                }
 
                foreach( $wgCheckEmailAddressNameSigns as $key => $value ) {
                        $sign = $value[ 'sign' ];
                        $maxcount = $value[ 'maxcount' ];
                        $signcount = substr_count( $name, $sign );
 
                        if( $signcount >= $maxcount ) {
                                $abortError = wfMsg( 'checkemailaddress-nameerror' );
                                unset( $value );
                                return false;
                        }
                }
                unset( $value );
                return true;
        }
}

[edit] CheckEmailAddress.i18n.php

<?php
$messages = array();
 
$messages['de'] = array(
        'checkemailaddress-desc'                => 'Ermöglicht es, bestimmte Email-Adressen von der Registrierung auszuschließen',
        'checkemailaddress'                             => '# Dies ist eine Liste unerlaubter Domain-Namen für Emails, mit denen eine Registrierung daher nicht möglich ist.',
        'checkemailaddress-domainerror' => 'Wegwerf- und Einmal-Email-Adressen sind erlaubt auf {{SITENAME}}. Bitte registriere dich mit einer anderen Email-Adresse.',
        'checkemailaddress-nameerror'   => 'Es handelt sich vermutlich um eine Spam-Email-Adresse. Falls nicht, schreibe bitte eine Email an den Admin.',
);
 
$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.',
);

[edit] Feedback

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.

Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox