View source
<?php
define('AUN_PUNCTUATION_REMOVE', 0);
define('AUN_PUNCTUATION_REPLACE', 1);
define('AUN_PUNCTUATION_DO_NOTHING', 2);
function auto_username_perm() {
return array(
'configure username patterns',
);
}
function auto_username_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/user/auto-username',
'title' => t('Username rules'),
'description' => t('Configure the way that usernames are automatically generated.'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'auto_username_configuration',
),
'access' => user_access('configure username patterns'),
);
}
return $items;
}
function auto_username_configuration() {
$form = array();
$form['aun_pattern'] = array(
'#type' => 'textarea',
'#title' => t('Pattern for username'),
'#description' => t('Enter the pattern for usernames. You may use any of the tokens listed below.'),
'#default_value' => variable_get('aun_pattern', ''),
);
$form['aun_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Other options'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['aun_settings']['aun_php'] = array(
'#type' => 'checkbox',
'#title' => t('Evaluate PHP in pattern.'),
'#description' => t('If this box is checked, the pattern will be executed as PHP code after token substitution has taken place. You must surround the PHP code in <?php and ?> tags. Token replacement will take place before PHP code execution. That means you can place tokens into the PHP code where you want that value to appear literally.'),
'#default_value' => variable_get('aun_php', 0),
);
$form['aun_settings']['aun_update_on_edit'] = array(
'#type' => 'checkbox',
'#title' => t('Update on user edit'),
'#description' => t('If this box is checked, the username will be reset any time the user\'s profile is updated. That can help to enforce a username format, but may result in a user\'s login name changing unexpectedly. It is best used in conjunction with an alternative login mechanism, such as OpenID or an e-mail address.'),
'#default_value' => variable_get('aun_update_on_edit', 1),
);
$form['aun_settings']['aun_reduce_ascii'] = array(
'#type' => 'checkbox',
'#title' => t('Reduce strings to letters and numbers from ASCII-96'),
'#default_value' => variable_get('aun_reduce_ascii', 0),
'#description' => t('Filters the new username to only letters and numbers found in the ASCII-96 set.'),
);
$form['aun_settings']['aun_replace_whitespace'] = array(
'#type' => 'checkbox',
'#title' => t('Replace whitespace with separator.'),
'#default_value' => variable_get('aun_replace_whitespace', 0),
'#description' => t('Replace all whitespace in tokens with the separator character specified below. Note that this will affect the tokens themselves, not the pattern specified above. To avoid spaces entirely, ensure that the pattern above contains no spaces.'),
);
$form['aun_settings']['aun_separator'] = array(
'#type' => 'textfield',
'#title' => t('Separator'),
'#description' => t('This value will be used in place of selected punctuation characters (see below).'),
'#default_value' => variable_get('aun_separator', '-'),
);
$options = array(
AUN_PUNCTUATION_REMOVE => t('Remove'),
AUN_PUNCTUATION_REPLACE => t('Replace by separator'),
AUN_PUNCTUATION_DO_NOTHING => t('No action (do not replace)'),
);
$form['punctuation'] = array(
'#type' => 'fieldset',
'#title' => t('Punctuation settings'),
'#description' => t('The following replacement rules will be applied to each token.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
foreach (auto_username_punctuation_chars() as $name => $details) {
$form['punctuation']['aun_punctuation_' . $name] = array(
'#type' => 'select',
'#title' => $details['name'],
'#default_value' => variable_get('aun_punctuation_' . $name, AUN_PUNCTUATION_REMOVE),
'#options' => $options,
);
}
$form['token_help'] = array(
'#title' => t('Replacement patterns'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('Note that profile fields that are not present in the user registration form will get replaced with an empty string when the account is created. That is rarely desirable. Also, values such as the user id are not available yet on user creation, so using them is not advisable.'),
);
$form['token_help']['help'] = array(
'#value' => theme('token_help', 'user'),
);
return system_settings_form($form);
}
function auto_username_user($op, &$edit, &$account, $category = NULL) {
static $new_name;
switch ($op) {
case 'validate':
$new_name = _auto_username_patternprocessor($account);
if ($error = user_validate_name($new_name)) {
form_set_error('name', $error);
}
$counter = 1;
$base_name = $new_name;
while (db_result(db_query("SELECT COUNT(uid) FROM {users} WHERE uid != %d AND LOWER(name) = LOWER('%s')", $account->uid, $new_name)) > 0) {
$new_name = $base_name . $counter++;
}
$_SESSION['auto_username']['new_name'] = $new_name;
break;
case 'insert':
db_query("UPDATE {users} SET name='%s' WHERE uid=%d", array(
$new_name,
$account->uid,
));
break;
case 'after_update':
if (variable_get('aun_update_on_edit', 1)) {
db_query("UPDATE {users} SET name='%s' WHERE uid=%d", array(
$new_name,
$account->uid,
));
}
break;
}
}
function _auto_username_patternprocessor($account) {
$output = '';
$placeholders = auto_username_get_placeholders('user', $account);
$pattern = variable_get('aun_pattern', '');
if (trim($pattern)) {
$output = str_replace($placeholders['tokens'], $placeholders['values'], $pattern);
if (variable_get('aun_php', 0)) {
$output = drupal_eval($output);
}
}
return trim($output);
}
function auto_username_get_placeholders($type, $object) {
$full = token_get_values($type, $object, TRUE);
$tokens = token_prepare_tokens($full->tokens);
$values = auto_username_clean_token_values($full);
return array(
'tokens' => $tokens,
'values' => $values,
);
}
function auto_username_clean_token_values($full) {
foreach ($full->values as $key => $value) {
$full->values[$key] = auto_username_cleanstring($value, FALSE);
}
return $full->values;
}
function auto_username_cleanstring($string, $clean_slash = TRUE) {
$ignore_words = array(
"a",
"an",
"as",
"at",
"before",
"but",
"by",
"for",
"from",
"is",
"in",
"into",
"like",
"of",
"off",
"on",
"onto",
"per",
"since",
"than",
"the",
"this",
"that",
"to",
"up",
"via",
"with",
);
$separator = variable_get('aun_separator', '-');
$output = $string;
$punctuation = auto_username_punctuation_chars();
foreach ($punctuation as $name => $details) {
$action = variable_get('aun_punctuation_' . $name, 0);
if ($action != AUN_PUNCTUATION_DO_NOTHING) {
$output = str_replace($details['value'], $action ? $separator : '', $output);
}
}
if ($clean_slash) {
$output = str_replace('/', '', $output);
}
if (variable_get('aun_reduce_ascii', FALSE)) {
$pattern = '/[^a-zA-Z0-9\\/]+/ ';
$output = preg_replace($pattern, $separator, $output);
}
if (variable_get('aun_replace_whitespace', 0)) {
$output = preg_replace("/\\s+/", $separator, $output);
}
if (isset($separator)) {
if (preg_match('/^[^' . PREG_CLASS_ALNUM . ']+$/uD', $separator)) {
$seppattern = $separator;
}
else {
$seppattern = '\\' . $separator;
}
$output = preg_replace("/^{$seppattern}+|{$seppattern}+\$/", "", $output);
$output = preg_replace("/{$seppattern}+/", "{$separator}", $output);
}
$maxlength = min(variable_get('pathauto_max_component_length', 100), 128);
$output = drupal_substr($output, 0, $maxlength);
return $output;
}
function auto_username_form_alter($form_id, &$form) {
if ('user_register' == $form_id) {
$form['account']['name'] = array(
'#type' => 'value',
'#value' => user_password(10),
);
unset($form['#submit']['user_register_submit']);
$form['#submit']['auto_username_user_register_submit'] = array();
}
if ('user_edit' == $form_id && variable_get('aun_update_on_edit', 1)) {
if (isset($form['account']['name'])) {
$form['account']['name'] = array(
'#type' => 'value',
'#value' => $form['account']['name']['#default_value'] ? $form['account']['name']['#default_value'] : user_password(),
);
}
}
}
function auto_username_user_register_submit($form_id, $form_values) {
$form_values['name'] = $_SESSION['auto_username']['new_name'];
unset($_SESSION['auto_username']['new_name']);
return user_register_submit($form_id, $form_values);
}
function auto_username_punctuation_chars() {
$punctuation = array();
$punctuation['double_quotes'] = array(
'value' => '"',
'name' => t('Double quotes "'),
);
$punctuation['quotes'] = array(
'value' => "'",
'name' => t("Single quotes (apostrophe) '"),
);
$punctuation['backtick'] = array(
'value' => "`",
'name' => t("Back tick `"),
);
$punctuation['comma'] = array(
'value' => ",",
'name' => t('Comma ,'),
);
$punctuation['period'] = array(
'value' => ".",
'name' => t('Period .'),
);
$punctuation['hyphen'] = array(
'value' => "-",
'name' => t('Hyphen -'),
);
$punctuation['underscore'] = array(
'value' => "_",
'name' => t('Underscore _'),
);
$punctuation['colon'] = array(
'value' => ":",
'name' => t('Colon :'),
);
$punctuation['semicolon'] = array(
'value' => ";",
'name' => t('Semicolon ;'),
);
$punctuation['pipe'] = array(
'value' => "|",
'name' => t('Pipe |'),
);
$punctuation['left_curly'] = array(
'value' => "{",
'name' => t('Left curly bracket {'),
);
$punctuation['left_square'] = array(
'value' => "[",
'name' => t('Left square bracket ['),
);
$punctuation['right_curly'] = array(
'value' => "}",
'name' => t('Right curly bracket }'),
);
$punctuation['right_square'] = array(
'value' => "]",
'name' => t('Right square bracket ]'),
);
$punctuation['plus'] = array(
'value' => "+",
'name' => t('Plus +'),
);
$punctuation['equal'] = array(
'value' => "=",
'name' => t('Equal ='),
);
$punctuation['asterisk'] = array(
'value' => "*",
'name' => t('Asterisk *'),
);
$punctuation['ampersand'] = array(
'value' => "&",
'name' => t('Ampersand &'),
);
$punctuation['percent'] = array(
'value' => "%",
'name' => t('Percent %'),
);
$punctuation['caret'] = array(
'value' => "^",
'name' => t('Caret ^'),
);
$punctuation['dollar'] = array(
'value' => "\$",
'name' => t('Dollar $'),
);
$punctuation['hash'] = array(
'value' => "#",
'name' => t('Hash #'),
);
$punctuation['at'] = array(
'value' => "@",
'name' => t('At @'),
);
$punctuation['exclamation'] = array(
'value' => "!",
'name' => t('Exclamation !'),
);
$punctuation['tilde'] = array(
'value' => "~",
'name' => t('Tilde ~'),
);
$punctuation['left_parenthesis'] = array(
'value' => "(",
'name' => t('Left parenthesis ('),
);
$punctuation['right_parenthesis'] = array(
'value' => ")",
'name' => t('right parenthesis )'),
);
$punctuation['question_mark'] = array(
'value' => "?",
'name' => t('Question mark ?'),
);
$punctuation['less_than'] = array(
'value' => "<",
'name' => t('Less than <'),
);
$punctuation['greater_than'] = array(
'value' => ">",
'name' => t('Greater than >'),
);
$punctuation['back_slash'] = array(
'value' => '\\',
'name' => t('Back slash \\'),
);
return $punctuation;
}
if (!function_exists('profile_token_values')) {
function profile_token_values($type, $object = NULL, $options = array()) {
$values = array();
switch ($type) {
case 'user':
if (isset($object)) {
$account = $object;
$account = (object) $account;
profile_load_profile($account);
}
else {
global $user;
$account = user_load(array(
'uid' => $user->uid,
));
}
$result = db_query("SELECT * FROM {profile_fields} ORDER BY category, weight");
while ($record = db_fetch_object($result)) {
$name = isset($account->{$record->name}) ? $account->{$record->name} : '';
$values[$record->name . '-raw'] = $name;
$values[$record->name . '-filtered'] = check_plain($name);
}
break;
}
return $values;
}
function profile_token_list($type = 'all') {
if ($type == 'user' || $type == 'all') {
$result = db_query("SELECT * FROM {profile_fields} ORDER BY category, weight");
while ($record = db_fetch_object($result)) {
$tokens['user'][$record->name . '-raw'] = $record->title;
$tokens['user'][$record->name . '-filtered'] = t('@field (filtered)', array(
'@field' => $record->title,
));
}
return $tokens;
}
}
}