View source
<?php
define('DOMAIN_REGISTRATION_ALLOW', 0);
define('DOMAIN_REGISTRATION_DENY', 1);
function domain_registration_menu() {
$items['admin/config/system/domain_register'] = array(
'title' => 'Domain registration',
'description' => 'Set domains for registration',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'domain_registration_admin_form',
),
'access arguments' => array(
'administer domain registration',
),
);
return $items;
}
function domain_registration_permission() {
return array(
'administer domain registration' => array(
'title' => t('Administer domain registration'),
'description' => t('Allows a user to administer the domain registration settings.'),
),
);
}
function domain_registration_variable_info($options) {
$variables = array();
$variables['domain_registration_message'] = array(
'type' => 'string',
'title' => t('Domain Registration Error message'),
'description' => t('This error message will be displayed when the email the user is trying to register with does not validate'),
'localize' => TRUE,
);
return $variables;
}
function domain_registration_admin_form($form, &$form_state) {
$options = array(
DOMAIN_REGISTRATION_ALLOW => t('Allow only domains listed below to register'),
DOMAIN_REGISTRATION_DENY => t('Prevent domains listed below from registering'),
);
$form['domain_registration_method'] = array(
'#type' => 'radios',
'#required' => TRUE,
'#options' => $options,
'#title' => t('Restriction Type'),
'#default_value' => variable_get('domain_registration_method', DOMAIN_REGISTRATION_ALLOW),
'#description' => t('Choose which method you would like the domains list to operate. Only allow domains listed to register, or prevent domains listed from registering.'),
);
$form['domain_registration'] = array(
'#type' => 'textarea',
'#required' => TRUE,
'#title' => t('Email domains'),
'#default_value' => variable_get('domain_registration'),
'#description' => t('Enter the domains you wish to restrict registration. One entry per line in the format (e.g. something.com). Wildcards are also supported (e.g. *.something.com) to match any subdomain.'),
);
$form['domain_registration_message'] = array(
'#type' => 'textfield',
'#required' => TRUE,
'#title' => t('Error message'),
'#maxlength' => 512,
'#size' => 120,
'#default_value' => variable_get('domain_registration_message', t('You are not allowed to register for this site.')),
'#description' => t('Enter the error message you want the user to see if the email address does not validate.'),
);
return system_settings_form($form);
}
function domain_registration_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$form['#validate'][] = 'domain_registration_user_register_validate';
}
function domain_registration_user_register_validate(&$form, &$form_state) {
$errors = form_get_errors();
if (!empty($errors['mail'])) {
return;
}
$default_message = t('You are not allowed to register for this site.');
$mail = explode('@', $form_state['values']['mail']);
$domains = variable_get('domain_registration', array());
if (!empty($domains)) {
$domains = explode("\r\n", $domains);
$match = count(array_filter($domains, function ($domain) use (&$mail) {
return domain_registration_wildcard_match($domain, $mail[1]);
}));
switch (variable_get('domain_registration_method', DOMAIN_REGISTRATION_ALLOW)) {
case 0:
if (!$match) {
form_set_error('account', variable_get('domain_registration_message', $default_message));
}
break;
case 1:
if ($match) {
form_set_error('account', variable_get('domain_registration_message', $default_message));
}
break;
}
}
}
function domain_registration_wildcard_match($pattern, $string) {
return preg_match("#^" . strtr(preg_quote($pattern, '#'), array(
'\\*' => '.*',
'\\?' => '.',
)) . "\$#i", $string);
}