View source
<?php
if (module_exists('token')) {
include_once drupal_get_path('module', 'realname') . '/realname_token.inc';
}
function realname_views_api() {
return array(
'api' => 2.0,
);
}
function realname_help($path, $args = NULL) {
switch ($path) {
case 'admin/user/realname':
case 'admin/user/realname/module':
case 'admin/user/realname/fields':
case 'admin/user/realname/bypass':
if (variable_get('realname_recalculate', FALSE)) {
drupal_set_message(t('In order to apply new settings you need to <a href="!rebuild">rebuild</a> your realnames.', array(
'!rebuild' => url('admin/user/realname/recalc'),
)), 'warning');
}
return t('This page displays the status of and settings for the RealName module.');
case 'admin/help#realname':
return t('This simple module adds a "realname" element (method) to a user object when that object is loaded.');
}
}
function realname_theme() {
return array(
'realname_admin_levels' => array(
'arguments' => array(
'form' => NULL,
),
'file' => 'realname.admin.inc',
),
'realname_new_role' => array(
'arguments' => array(
'form' => NULL,
),
'file' => 'realname_theme.inc',
),
);
}
function realname_perm() {
return array(
'use realname',
);
}
function realname_menu() {
$items['admin/user/realname'] = array(
'title' => 'RealName',
'description' => "Configure which fields are used to create a user's RealName.",
'access arguments' => array(
'administer users',
),
'page callback' => 'realname_admin_settings',
'file' => 'realname.admin.inc',
);
$items['admin/user/realname/general'] = array(
'title' => 'General',
'access arguments' => array(
'administer users',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'realname_admin_general',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'file' => 'realname.admin.inc',
'weight' => 0,
);
$items['admin/user/realname/fields'] = array(
'title' => 'Fields',
'access arguments' => array(
'administer users',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'realname_admin_fields',
),
'type' => MENU_LOCAL_TASK,
'file' => 'realname.admin.inc',
'weight' => 2,
);
$items['admin/user/realname/module'] = array(
'title' => 'Module',
'access arguments' => array(
'administer users',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'realname_admin_module',
),
'type' => MENU_LOCAL_TASK,
'file' => 'realname.admin.inc',
'weight' => 4,
);
$items['admin/user/realname/bypass'] = array(
'title' => 'Bypass Forms',
'access arguments' => array(
'administer users',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'realname_admin_bypass',
),
'type' => MENU_LOCAL_TASK,
'file' => 'realname.admin.inc',
'weight' => 6,
);
$items['admin/user/realname/recalc'] = array(
'title' => 'Recalculate names',
'access arguments' => array(
'administer users',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'realname_rebuild_confirm',
),
'type' => MENU_LOCAL_TASK,
'file' => 'realname.admin.inc',
'weight' => 8,
);
$items['admin/user/realname/ajax/autocomplete/user'] = array(
'page callback' => 'realname_ajax_autocomplete_user',
'access callback' => 'user_access',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
if (module_exists('privatemsg')) {
$items['realname/privatemsg/autocomplete'] = array(
'title' => 'Realname Privatemsg autocomplete',
'access callback' => user_access('access private messages'),
'page callback' => 'realname_privatemsg_autocomplete',
'type' => MENU_CALLBACK,
);
}
return $items;
}
function realname_menu_alter(&$callbacks) {
$callbacks['user/%user_uid_optional']['title callback'] = 'realname_user_page_title';
foreach ($callbacks as $path => $callback) {
if (isset($callback['page callback'])) {
switch ($callback['page callback']) {
case 'user_view':
if ($path == 'user/%user_uid_optional' && module_exists('page_manager') && !variable_get('page_manager_user_view_disabled', TRUE)) {
continue;
}
case 'user_edit':
case 'tracker_page':
case 'statistics_user_tracker':
case 'content_profile_page_edit':
$callbacks[$path]['page callback'] = 'realname_' . $callback['page callback'];
break;
case 'contact_user_page':
$callbacks[$path]['page callback'] = 'realname_contact_user';
break;
case 'blog_page_user':
$callbacks[$path]['page callback'] = 'realname_blog_page';
break;
}
}
}
if (variable_get('realname_user_disable', FALSE) && isset($callbacks['search/user/%menu_tail'])) {
unset($callbacks['search/user/%menu_tail']);
}
}
function realname_delegator_override($type) {
return realname_page_manager_override($type);
}
function realname_page_manager_override($type) {
if ($type == 'user_view') {
return 'realname_user_view';
}
}
function realname_ctools_context_converter_alter($context, $converter, &$value) {
if ($context->type == 'user' && $converter == 'name') {
$value = theme('username', $context->data, array(
'plain' => TRUE,
));
}
}
function realname_check_breadcrumbs($account) {
$trail = array(
l(t('Home'), '<front>'),
);
if ($account->uid != $GLOBALS['user']->uid) {
$trail[] = decode_entities(l($account->realname, 'user/' . $account->uid));
}
$trail = drupal_set_breadcrumb($trail);
}
function realname_user_view($account) {
realname_check_breadcrumbs($account);
drupal_set_title(check_plain(realname_user_page_title($account)));
user_build_content($account);
return theme('user_profile', $account);
}
function realname_user_edit($account, $category = 'account') {
realname_check_breadcrumbs($account);
drupal_set_title(check_plain(realname_user_page_title($account)));
return drupal_get_form('user_profile_form', $account, $category);
}
function realname_blog_page($account) {
if (module_exists('blogtitle')) {
$output = blogtitle_blog_page_user($account);
}
else {
$output = blog_page_user($account);
drupal_set_title(t("@name's blog", array(
'@name' => $account->realname,
)));
}
return $output;
}
function realname_tracker_page($account = NULL, $set_title = FALSE) {
if (function_exists('tracker2_page')) {
$output = tracker2_page($account, $set_title);
}
else {
$output = tracker_page($account, $set_title);
}
if ($account) {
realname_check_breadcrumbs($account);
if ($set_title) {
drupal_set_title(check_plain($account->realname));
}
}
return $output;
}
function realname_statistics_user_tracker() {
$output = statistics_user_tracker();
if ($account = user_load(array(
'uid' => arg(1),
))) {
drupal_set_title(check_plain($account->realname));
}
return $output;
}
function realname_content_profile_page_edit($type, $account) {
$output = content_profile_page_edit($type, $account);
drupal_set_title(check_plain(realname_user_page_title($account)));
return $output;
}
function realname_contact_user($account) {
global $user;
if (!valid_email_address($user->mail)) {
$output = t('You need to provide a valid e-mail address to contact other users. Please update your <a href="@url">user information</a> and try again.', array(
'@url' => url("user/{$user->uid}/edit"),
));
}
else {
if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3))) {
$output = t('You cannot contact more than %number users per hour. Please try again later.', array(
'%number' => variable_get('contact_hourly_threshold', 3),
));
}
else {
realname_check_breadcrumbs($account);
$output = drupal_get_form('contact_mail_user', $account);
}
}
return $output;
}
function realname_user_page_title($account) {
global $user;
$myacct = variable_get('realname_myacct', t('My account'));
if ($user->uid == $account->uid && $myacct) {
return $myacct;
}
$name = isset($account->realname) ? $account->realname : realname_make_name($account);
return $name;
}
function realname_theme_registry_alter(&$theme_registry) {
if (!empty($theme_registry['form_element'])) {
$path = drupal_get_path('module', 'realname');
if (variable_get('realname_theme', TRUE)) {
$theme_registry['username']['function'] = 'realname_username';
$theme_registry['username']['file'] = $path . '/realname_theme.inc';
$theme_registry['username']['theme path'] = $path;
$theme_registry['username']['theme paths'][0] = $path;
}
$theme_registry['user_admin_new_role']['function'] = 'theme_realname_new_role';
$theme_registry['user_admin_new_role']['file'] = $path . '/realname_theme.inc';
$theme_registry['user_admin_new_role']['theme path'] = $path;
$theme_registry['user_admin_new_role']['theme paths'][0] = $path;
}
}
function realname_preprocess_user_picture(&$variables) {
if (!empty($variables['picture'])) {
$account = $variables['account'];
$not_real_name = $account->name ? $account->name : variable_get('anonymous', t('Anonymous'));
$real_name = isset($account->realname) ? $account->realname : realname_make_name($account);
if ($real_name && $real_name != $not_real_name) {
$not_real_name_alt = check_plain(t("@user's picture", array(
'@user' => $not_real_name,
)));
$real_name_alt = check_plain(t("@user's picture", array(
'@user' => $real_name,
)));
$variables['picture'] = str_replace($not_real_name_alt, $real_name_alt, $variables['picture']);
$variables['account'] = drupal_clone($account);
$variables['account']->name = $real_name;
}
}
}
function realname_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'load':
$account->realname = realname_make_name($account);
if (variable_get('realname_theme', TRUE) && $account->realname) {
if (!isset($account->realname_save) && is_object($account)) {
$account->realname_save = $account->name;
}
}
break;
case 'delete':
db_query("DELETE FROM {realname} WHERE uid = %d", $account->uid);
break;
case 'insert':
case 'after_update':
if ($module = variable_get('realname_profile_module', NULL)) {
$module_info = realname_supported_modules($module);
if (isset($module_info['cache']) && !$module_info['cache']) {
return;
}
}
$account->realname = _realname_make_name($account);
db_query("DELETE FROM {realname} WHERE uid = %d", $account->uid);
drupal_write_record('realname', $account);
}
}
function realname_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
static $type;
static $accounts = array();
if (!user_access('use realname')) {
return;
}
if (variable_get('realname_profile_module', NULL) == 'content_profile' && module_exists('content_profile') && is_content_profile($node->type)) {
switch ($op) {
case 'update':
case 'insert':
case 'delete':
db_query('DELETE FROM {realname} WHERE uid = %d', $node->uid);
}
}
if (!variable_get('realname_nodeapi', FALSE)) {
return;
}
switch ($op) {
case 'view':
if (!isset($type)) {
$type = variable_get('realname_profile_type', NULL);
}
if ($node->type == $type) {
break;
}
$node->realname_save = $node->name;
if (!isset($accounts[$node->uid])) {
$accounts[$node->uid] = realname_get_user($node->uid);
}
$account = $accounts[$node->uid];
$node->realname = $node->name = $account->name;
$node->homepage = isset($account->homepage) ? $account->homepage : NULL;
break;
case 'prepare':
if (isset($node->realname_save)) {
$node->name = $node->realname_save;
}
break;
}
}
function realname_get_user($uid) {
$result = db_query("SELECT uid, name, mail FROM {users} WHERE uid = %d", $uid);
$account = db_fetch_object($result);
$account->name = realname_make_name($account);
return $account;
}
function realname_comment(&$comment, $op) {
static $users = array();
switch ($op) {
case 'view':
if (!$comment->homepage) {
if (isset($users[$comment->uid])) {
$account = $users[$comment->uid];
}
else {
$users[$comment->uid] = $account = realname_get_user($comment->uid);
}
$comment->homepage = isset($account->homepage) ? $account->homepage : NULL;
}
break;
}
return;
}
function realname_mail_alter(&$message) {
switch ($message['id']) {
case 'user_status_activated':
if (isset($message['params']['account']->realname_save)) {
$message['body'][0] = str_replace(t('username: ') . $message['params']['account']->name, t('username: ') . $message['params']['account']->realname_save, $message['body'][0]);
}
}
}
function realname_form_alter(&$form, $form_state, $form_id) {
global $user;
if (!user_access('use realname')) {
return;
}
$bypass_forms = variable_get('realname_bypass_forms', array(
array(
'name' => 'comment_form',
'fields' => array(
'name',
),
),
));
foreach ($bypass_forms as $bypass) {
if ($form_id == $bypass['name']) {
if (!isset($user->realname)) {
$user->realname = realname_make_name($user);
}
$value = $user->uid ? $user->realname_save : variable_get('anonymous', 'Anonymous');
realname_array_set_nested_value($form, array_merge($bypass['fields'], array(
'#default_value',
)), $value);
}
}
switch ($form_id) {
case 'user_profile_form':
if (variable_get('realname_theme', TRUE)) {
$form['account']['name']['#default_value'] = $form['_account']['#value']->realname_save;
}
break;
case 'contact_mail_user':
if (!isset($user->realname)) {
$user->realname = realname_make_name($user);
}
$form['from']['#value'] = check_plain($user->realname) . ' <' . check_plain($user->mail) . '>';
if (!isset($form['recipient']['#value']->realname)) {
$form['recipient']['#value']->realname = realname_make_name($form['recipient']['#value']);
}
$form['to']['#value'] = check_plain($form['recipient']['#value']->realname);
break;
case 'contact_mail_page':
if (!isset($user->realname)) {
$user->realname = realname_make_name($user);
}
$form['name']['#default_value'] = $user->uid ? $user->realname : '';
break;
case 'user_admin_role':
$rid = $form['rid']['#value'];
$pic = variable_get('realname_user_level_' . $rid, NULL);
$form['level'] = array(
'#type' => 'textfield',
'#default_value' => $pic,
'#title' => t('User level image'),
'#description' => t('This image will be shown when the user name is themed.
It can be used to show special attributes or user participation level.
The path is relative to the files directory "%files".', array(
'%files' => file_directory_path(),
)),
'#field_suffix' => empty($pic) ? '' : ' <img src="' . file_create_url($pic) . '" />',
);
$form['submit']['#weight'] = 98;
$form['delete']['#weight'] = 99;
$form['#submit'][] = '_realname_role_submit';
break;
case 'privatemsg_new':
$form['privatemsg']['recipient']['#autocomplete_path'] = 'realname/privatemsg/autocomplete';
break;
}
}
function _realname_role_submit($form, &$form_state) {
$rid = $form_state['values']['rid'];
$pic = $form_state['values']['level'];
variable_set('realname_user_level_' . $rid, $pic);
}
function realname_search($op = 'search', $keys = NULL, $skip_access_check = FALSE) {
if (!variable_get('realname_search_enable', FALSE)) {
return;
}
switch ($op) {
case 'name':
if ($skip_access_check || user_access('access user profiles')) {
return variable_get('realname_user_disable', FALSE) ? t('Users') : t('User names');
}
break;
case 'reset':
variable_set('realname_last_index', -1);
break;
case 'status':
$start = variable_get('realname_last_index', -1) + 1;
$total = db_result(db_query('SELECT COUNT(*) FROM {users} WHERE status = 1'));
$remaining = db_result(db_query("SELECT COUNT(*) FROM {users} WHERE status = 1 AND uid >= %d", $start));
return array(
'remaining' => $remaining,
'total' => $total,
);
case 'search':
if ($skip_access_check || user_access('access user profiles')) {
$results = array();
$find = do_search($keys, 'realname');
foreach ($find as $item) {
if ($item->type != 'realname') {
continue;
}
$account = user_load(array(
'uid' => $item->sid,
));
$realname = realname_make_name($account);
$results[] = array(
'link' => url('user/' . $account->uid, array(
'absolute' => TRUE,
)),
'type' => t('User'),
'title' => $realname,
'user' => theme('username', $account) . '<div class="clear-block"></div>',
'score' => $item->score,
'snippet' => theme('user_picture', $account) . (user_access('administer users') ? t('Login name: @name', array(
'@name' => $account->name,
)) : NULL),
);
}
return $results;
}
break;
}
}
function realname_update_index() {
if (variable_get('realname_search_enable', FALSE)) {
$start = variable_get('realname_last_index', -1) + 1;
$limit = (int) variable_get('search_cron_limit', 100);
$how_many = db_result(db_query("SELECT COUNT(*) FROM {users} WHERE status = 1 AND uid >= %d", $start));
$limit = min($limit, $how_many);
if ($limit < 1) {
return;
}
watchdog('RealName', 'Indexing up to !limit users, starting at !start', array(
'!start' => $start,
'!limit' => $limit,
), WATCHDOG_NOTICE);
$result = db_query_range("SELECT uid FROM {users} WHERE status = 1 AND uid >= %d ORDER BY uid", $start, 0, $limit);
while ($uid = db_result($result)) {
$account = user_load(array(
'uid' => $uid,
));
$index_text = realname_make_name($account);
if (variable_get('realname_search_login', FALSE)) {
$index_text .= ' ' . $account->name;
}
search_index($account->uid, 'realname', $index_text);
variable_set('realname_last_index', $account->uid);
}
}
}
function realname_privatemsg_autocomplete($string) {
$names = explode(',', $string);
$names = array_map('trim', $names);
$search = array_pop($names);
if ($search != '') {
$sql = 'SELECT u.uid, u.name, r.realname FROM {users} u INNER JOIN {realname} r USING(uid) ';
$sql .= ' WHERE u.status <> 0 AND LOWER(r.realname) LIKE LOWER(\'%%%s%%\') AND ';
$sql .= variable_get('privatemsg_default_allow', 1) ? '(u.data NOT LIKE \'%%:16:"privatemsg_allow";i:0%%\' OR u.data IS NULL)' : 'u.data LIKE \'%%:16:"privatemsg_allow";i:1%%\'';
$sql .= ' ORDER BY r.realname ASC';
$result = db_query_range($sql, $search, 0, 10);
$prefix = count($names) ? implode(', ', $names) . ', ' : '';
$matches = array();
while ($user = db_fetch_object($result)) {
$account = user_load(array(
'uid' => $user->uid,
));
$matches[$prefix . $user->name] = check_plain($user->realname);
if ($user->realname != $user->name) {
$matches[$prefix . $user->name] .= ' (' . check_plain($user->name) . ')';
}
}
drupal_json($matches);
}
else {
drupal_json(array());
}
}
function _realname_include() {
static $not_run = TRUE;
if ($not_run) {
$path = drupal_get_path('module', 'realname');
if (module_exists('content_profile')) {
require_once "{$path}/realname_content_profile.inc";
}
if (module_exists('profile')) {
require_once "{$path}/realname_profile.inc";
}
$not_run = TRUE;
}
}
function realname_supported_modules($module = NULL) {
static $modules;
_realname_include();
if (!isset($modules)) {
foreach (module_implements('realname') as $value) {
$options = call_user_func($value . '_realname');
if (isset($options) && is_array($options)) {
$modules[$value] = $options;
}
}
}
if ($module) {
if (array_key_exists($module, $modules)) {
return $modules[$module];
}
else {
return FALSE;
}
}
return $modules;
}
function realname_make_name(&$account) {
$users =& realname_static('users', array());
$links =& realname_static('links', array());
if ($account->uid == 0) {
return isset($account->name) ? $account->name : variable_get('anonymous', t('Anonymous'));
}
if (isset($users[$account->uid])) {
$account->homepage = isset($links[$account->uid]) ? $links[$account->uid] : NULL;
return $users[$account->uid];
}
if ($module = variable_get('realname_profile_module', NULL)) {
$module_info = realname_supported_modules($module);
}
$result = _realname_make_name($account);
if (isset($module_info['cache']) && !$module_info['cache']) {
return $result;
}
if (empty($result)) {
return $account->name;
}
if (!db_result(db_query("SELECT uid FROM {realname} WHERE uid = %d", $account->uid))) {
db_query("INSERT INTO {realname} (uid, realname) VALUES (%d, '%s')", $account->uid, $result);
}
else {
db_query("UPDATE {realname} SET realname = '%s' WHERE uid = %d", $result, $account->uid);
}
return $result;
}
function _realname_make_name(&$account) {
static $fields, $pattern_saved, $homepage, $use_theme, $type, $module, $module_info, $profile_privacy;
$links =& realname_static('links', array());
$edit =& realname_static('edit', array());
if (!isset($module)) {
$module = variable_get('realname_profile_module', NULL);
if ($module) {
$module_info = realname_supported_modules($module);
if (!$module_info || !module_exists($module)) {
unset($module_info);
$module = FALSE;
}
else {
if (isset($module_info['file'])) {
$path = !empty($module_info['path']) ? $module_info['path'] : drupal_get_path('module', $module);
require_once $path . '/' . $module_info['file'];
}
if (!isset($module_info['fields']) || $module_info['fields']) {
$fields = variable_get('realname_fields', array());
if (!$fields) {
return $account->name;
}
$pattern_saved = variable_get('realname_pattern', ' ');
}
else {
$fields = FALSE;
}
$homepage = variable_get('realname_homepage', '');
$use_theme = variable_get('realname_theme', TRUE);
if (isset($module_info['type']) && $module_info['type']) {
$type = variable_get('realname_profile_type', NULL);
}
else {
$type = FALSE;
}
$profile_privacy = module_exists('profile_privacy');
}
}
}
if (!$module) {
drupal_set_message(t('No module is available for RealName.'), 'error');
$string = $account->name;
}
else {
if (is_array($fields) && empty($fields)) {
$string = $account->name;
}
else {
if ($fields === FALSE) {
$string = module_invoke($module, 'realname_make', $account);
}
else {
if ($fields) {
$pattern = $pattern_saved;
if (!isset($account->{key($fields)})) {
$load_func = $module . '_load_profile';
if (!function_exists($load_func)) {
drupal_set_message(t('The profile load function (!module) was not found.', array(
'!module' => $load_func,
)), 'error');
return $account->name;
}
$load_func($account, $type);
if ($profile_privacy) {
profile_privacy_user('load', $edit, $account);
}
}
$stuff = array();
$i = 0;
foreach ($fields as $name => $weight) {
++$i;
$private = 'private_' . $name;
$private_field = isset($account->{$private}) ? $account->{$private} : FALSE;
if (isset($account->{$name}) && !empty($account->{$name}) && !$private_field) {
if (is_array($account->{$name})) {
$stuff['%' . $i] = $account->{$name}[0]['view'];
}
else {
$stuff['%' . $i] = $account->{$name};
}
}
else {
$pattern = str_replace('%' . $i, NULL, $pattern);
}
}
if (count($stuff) == 0) {
$stuff['%1'] = $account->name;
$pattern = '%1';
}
if (!empty($homepage) && !empty($account->{$homepage})) {
$links[$account->uid] = $account->{$homepage};
}
$string = trim(strtr($pattern, $stuff));
}
}
}
}
if (empty($string)) {
$string = $account->name;
}
$string = preg_replace('/ {2,}/', ' ', $string);
return html_entity_decode(filter_xss_admin($string));
}
function _realname_sort($a, $b) {
$ret = $a['weight'] - $b['weight'];
if ($ret == 0) {
$ret = strcmp($a['title'], $b['title']);
}
return $ret;
}
function realname_ajax_autocomplete_user($string = '') {
$array = drupal_explode_tags($string);
$last_string = trim(array_pop($array));
$matches = array();
if ($last_string != '') {
$prefix = count($array) ? implode(', ', $array) . ', ' : '';
if (strpos('anonymous', strtolower($last_string)) !== FALSE) {
$matches[$prefix . 'Anonymous'] = 'Anonymous';
}
$result = db_query_range("SELECT realname FROM {realname} WHERE LOWER(realname) LIKE LOWER('%s%%')", $last_string, 0, 10);
while ($account = db_fetch_object($result)) {
$n = $account->realname;
if (strpos($account->realname, ',') !== FALSE || strpos($account->realname, '"') !== FALSE) {
$n = '"' . str_replace('"', '""', $account->realname) . '"';
}
$matches[$prefix . $n] = check_plain($account->realname);
}
}
drupal_json($matches);
}
function realname_array_set_nested_value(&$array, $parents, $value, $force = FALSE) {
$ref =& $array;
foreach ($parents as $parent) {
if ($force && isset($ref) && !is_array($ref)) {
$ref = array();
}
$ref =& $ref[$parent];
}
$ref = $value;
}
function &realname_static($name, $default_value = NULL, $reset = FALSE) {
static $data = array(), $default = array();
if (isset($data[$name]) || array_key_exists($name, $data)) {
if ($reset) {
$data[$name] = $default[$name];
}
return $data[$name];
}
if (isset($name)) {
if ($reset) {
return $data;
}
$default[$name] = $data[$name] = $default_value;
return $data[$name];
}
foreach ($default as $name => $value) {
$data[$name] = $value;
}
return $data;
}