View source
<?php
define('ME_PATH_EXCLUDE', 0);
define('ME_PATH_INCLUDE', 1);
define('ME_PATH_PHP', 2);
function me_help($path, $arg) {
switch ($path) {
case 'admin/help#me':
return t("Allows a user to enter user/me, blog/me etc.. using 'me' in place of their user id.");
}
}
function me_theme_registry_alter(&$theme_registry) {
if (isset($theme_registry['menu_item_link']['function'])) {
variable_set('me_theme_menu_item_link', $theme_registry['menu_item_link']['function']);
$theme_registry['menu_item_link']['function'] = 'me_theme_menu_item_link';
}
}
function me_preprocess_menu_item_link(&$vars) {
_me_check_path($vars['link']);
}
function me_theme_menu_item_link($link) {
_me_check_path($link);
$function = me_variable_get('me_theme_menu_item_link');
if (empty($function) || !function_exists($function)) {
if (isset($GLOBALS['custom_theme']) && function_exists($GLOBALS['custom_theme'] . '_menu_item_link')) {
$function = $GLOBALS['custom_theme'] . '_menu_item_link';
}
elseif (isset($GLOBALS['theme_key']) && function_exists($GLOBALS['theme_key'] . '_menu_item_link')) {
$function = $GLOBALS['theme_key'] . '_menu_item_link';
}
elseif (isset($GLOBALS['theme_engine']) && function_exists($GLOBALS['theme_engine'] . '_menu_item_link')) {
$function = $GLOBALS['theme_engine'] . '_menu_item_link';
}
else {
$function = 'theme_menu_item_link';
}
}
return $function($link);
}
function _me_check_path(&$link) {
if (me_variable_get('me_rewrite_link') && !_me_handle_path($link['href'])) {
$path_parts = explode('/', $link['href'], MENU_MAX_PARTS);
$wild_parts = explode('/', isset($link['path']) ? $link['path'] : $link['router_path'], MENU_MAX_PARTS);
while (list($key, $val) = each($path_parts)) {
if (_me_is_alias($val) && $wild_parts[$key] == '%') {
$path_parts[$key] = $GLOBALS['user']->uid;
}
}
$link['href'] = implode('/', $path_parts);
}
}
function me_variable_get($name) {
static $defaults = array(
'me_alias' => 'me',
'me_case_insensitive' => FALSE,
'me_redirect' => FALSE,
'me_path_rule' => ME_PATH_EXCLUDE,
'me_paths' => '',
'me_redirect_anonymous' => '',
'me_theme_menu_item_link' => '',
'me_rewrite_link' => TRUE,
'me_user_override' => FALSE,
);
return variable_get($name, $defaults[$name]);
}
function _me_handle_path($path) {
$paths = me_variable_get('me_paths');
$path_rule = me_variable_get('me_path_rule');
$path_match = TRUE;
if (!empty($paths)) {
if ($path_rule !== ME_PATH_PHP) {
$path = drupal_get_path_alias($_GET['q']);
$path_match = drupal_match_path($path, $paths);
if ($path != $_GET['q']) {
$path_match = $path_match || drupal_match_path($_GET['q'], $paths);
}
$path_match = !($path_rule xor $path_match);
}
else {
$path_match = drupal_eval($paths);
}
}
return $path_match;
}
function me_handler($parts, $callback) {
$args = func_get_args();
array_shift($args);
array_shift($args);
if (me_variable_get('me_redirect') || _me_user_disabled() || !_me_handle_path($_GET['q'])) {
$redirect = FALSE;
$menu_parts = explode('/', $_GET['q'], MENU_MAX_PARTS);
while (list($key, $val) = each($parts)) {
if (0 === strpos($val, '%me') && _me_is_alias($menu_parts[$key])) {
$redirect = TRUE;
$menu_parts[$key] = $GLOBALS['user']->uid;
}
}
if ($redirect) {
$path = implode('/', $menu_parts);
$redirect_path = me_variable_get('me_redirect_anonymous');
if ($GLOBALS['user']->uid == 0 && !empty($redirect_path)) {
$path = $redirect_path;
}
drupal_goto($path);
}
}
$router_item = menu_get_item();
foreach ($router_item['load_functions'] as $index => $function) {
if (0 === strpos($function, 'me')) {
$router_item['load_functions'][$index] = 'user_load';
}
}
menu_set_item($_GET['q'], $router_item);
return call_user_func_array($callback, $args);
}
function _me_user_disabled() {
return me_variable_get('me_user_override') && !empty($GLOBALS['user']->me_disable);
}
function me_menu_alter(&$callbacks) {
$processed = array();
$handlers = array(
'%user' => '%me',
'%user_uid_optional' => '%me_uid_optional',
'%user_category' => '%me_category',
);
foreach ($callbacks as $path => $data) {
$found = FALSE;
$parts = explode('/', $path, MENU_MAX_PARTS);
foreach ($handlers as $user_handler => $me_handler) {
if (in_array($user_handler, $parts)) {
$found = TRUE;
break;
}
}
if ($found) {
if (isset($data['file']) && !isset($data['file path'])) {
$data['file path'] = drupal_get_path('module', $data['module']);
}
$new_parts = array();
while (list($key, $val) = each($parts)) {
if (array_key_exists($val, $handlers)) {
$val = $handlers[$val];
}
$new_parts[] = $val;
}
$new_path = implode('/', $new_parts);
if (is_array($data['load arguments'])) {
if (FALSE !== ($map_index = array_search('%map', $data['load arguments']))) {
unset($data['load arguments'][$map_index]);
array_unshift($data['load arguments'], '%map', '%index', strval($map_index));
}
}
$parent_path = implode('/', array_slice($parts, 0, count($parts) - 1));
if (in_array($parent_path, $processed)) {
$parts = explode('/', $new_path, MENU_MAX_PARTS);
$parent_path = implode('/', array_slice($parts, 0, count($parts) - 1));
}
$parent = $callbacks[$parent_path];
if (!isset($data['page callback']) && isset($parent['page callback'])) {
$data['page callback'] = $parent['page callback'];
if (!isset($data['page arguments']) && isset($parent['page arguments'])) {
$data['page arguments'] = $parent['page arguments'];
}
if (!isset($data['file']) && isset($parent['file'])) {
$data['file'] = $parent['file'];
}
if (!isset($data['file path']) && isset($parent['file path'])) {
$data['file path'] = $parent['file path'];
}
}
if (isset($data['page callback'])) {
if (!is_array($data['page arguments'])) {
$data['page arguments'] = array();
}
$parts = explode('/', $new_path, MENU_MAX_PARTS);
$data['page arguments'] = array_merge(array(
$parts,
$data['page callback'],
), $data['page arguments']);
$data['page callback'] = 'me_handler';
}
$callbacks[$new_path] = $data;
unset($callbacks[$path]);
$processed[] = $path;
}
}
}
function _me_load_arguments($uid, &$map = NULL, $index = NULL, $map_index = FALSE, $args = array(), $function = 'user_load') {
array_splice($args, 0, min(4, count($args)));
if (!is_null($map) && FALSE !== $map_index) {
$insert = array(
&$map,
);
array_splice($args, $map_index, 0, $insert);
$map[$index] = _me_check_arg($uid);
}
array_unshift($args, _me_check_arg($uid));
if (function_exists($function)) {
return call_user_func_array($function, $args);
}
return FALSE;
}
function me_load($uid, &$map = NULL, $index = NULL, $map_index = FALSE) {
$args = func_get_args();
return _me_load_arguments($uid, $map, $index, $map_index, $args);
}
function me_uid_optional_load($uid, &$map = NULL, $index = NULL, $map_index = FALSE) {
$args = func_get_args();
return _me_load_arguments(isset($uid) ? $uid : $GLOBALS['user']->uid, $map, $index, $map_index, $args);
}
function me_category_load($uid, &$map = NULL, $index = NULL, $map_index = FALSE) {
$args = func_get_args();
return _me_load_arguments($uid, $map, $index, $map_index, $args, 'user_category_load');
}
function me_to_arg($arg, $map, $index) {
$uid = user_uid_optional_to_arg($arg, $map, $index);
if (me_variable_get('me_rewrite_link') && !_me_user_disabled()) {
return $uid == $GLOBALS['user']->uid ? _me_get_me_alias() : $uid;
}
else {
return $uid;
}
}
function me_uid_optional_to_arg($arg, $map, $index) {
return me_to_arg($arg, $map, $index);
}
function me_category_to_arg($arg, $map, $index) {
return me_to_arg($arg, $map, $index);
}
function _me_check_arg($arg, $username = FALSE, $redirect = TRUE) {
$return = _me_is_alias($arg) ? $username ? $GLOBALS['user']->name : $GLOBALS['user']->uid : $arg;
$redirect_path = me_variable_get('me_redirect_anonymous');
if ($redirect && $GLOBALS['user']->uid == 0 && !empty($redirect_path)) {
$original_map = arg(NULL, $_GET['q']);
$parts = array_slice($original_map, 0, MENU_MAX_PARTS);
list($ancestors, $placeholders) = menu_get_ancestors($parts);
if (($router_item = db_fetch_array(db_query_range('SELECT * FROM {menu_router} WHERE path IN (' . implode(',', $placeholders) . ') ORDER BY fit DESC', $ancestors, 0, 1))) && $router_item['page_callback'] == 'me_handler') {
unset($_REQUEST['destination'], $_REQUEST['edit']['destination']);
drupal_goto($redirect_path);
}
}
return $return;
}
function _me_get_me_alias($print = FALSE) {
$alias = me_variable_get('me_alias');
if (module_exists('token')) {
$replaced_alias = token_replace($alias, 'global');
if ($print && $replaced_alias != $alias) {
$alias = ucwords(str_replace(array(
'-',
'[',
']',
), array(
' ',
'',
), $alias));
}
else {
$alias = $replaced_alias;
}
}
return $alias;
}
function _me_is_alias($arg) {
$compare_function = me_variable_get('me_case_insensitive') ? 'strcasecmp' : 'strcmp';
return $compare_function($arg, _me_get_me_alias()) === 0;
}
function me_views_api() {
return array(
'api' => views_api_version(),
'path' => drupal_get_path('module', 'me') . '/includes',
);
}
function me_menu() {
$items = array();
$items['admin/settings/me'] = array(
'title' => "'Me' Aliases",
'description' => "Configure the 'me' aliases, and how they're matched.",
'page callback' => 'drupal_get_form',
'page arguments' => array(
'me_admin_settings_form',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_NORMAL_ITEM,
);
if (!me_variable_get('me_redirect')) {
$items['user/' . me_variable_get('me_alias') . '/edit'] = array(
'title' => 'Edit',
'page callback' => 'me_user_edit',
'access callback' => 'me_user_edit_access',
'type' => MENU_LOCAL_TASK,
);
}
return $items;
}
function me_user_edit() {
drupal_goto('user/' . $GLOBALS['user']->uid . '/edit');
}
function me_user_edit_access() {
return user_edit_access($GLOBALS['user']);
}
function me_perm() {
return array(
'use PHP for me alias paths',
);
}
function me_admin_settings_form(&$form_state) {
$form = array();
$form['me_alias'] = array(
'#type' => 'textfield',
'#title' => t("'Me' Alias"),
'#description' => t('The alias to use to represent the current users uid.'),
'#default_value' => me_variable_get('me_alias'),
'#required' => TRUE,
);
if (module_exists('token')) {
$form['me_token_help'] = array(
'#title' => t('Replacement patterns for me alias'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'help' => array(
'#type' => 'markup',
'#value' => theme('token_help', 'global'),
),
);
}
$form['me_case_insensitive'] = array(
'#type' => 'checkbox',
'#title' => t('Case Insensitive Alias Checking'),
'#description' => t('When checked, "Me" will be matched the same as "me", "ME", and "mE".'),
'#default_value' => me_variable_get('me_case_insensitive'),
);
$form['me_rewrite_link'] = array(
'#type' => 'checkbox',
'#title' => t('Rewrite links generated by the drupal menu system'),
'#description' => t('When checked, links output by the drupal menu system will replace uid with the me alias.'),
'#default_value' => me_variable_get('me_rewrite_link'),
);
$form['me_user_override'] = array(
'#type' => 'checkbox',
'#title' => t('Allow users to turn off me for their account'),
'#default_value' => me_variable_get('me_user_override'),
);
$form['me_redirect'] = array(
'#type' => 'checkbox',
'#title' => t('Redirect to uid'),
'#description' => t('When checked, perform a redirect so the users uid is shown in the address bar instead of the me alias.'),
'#default_value' => me_variable_get('me_redirect'),
);
$form['me_redirect_anonymous'] = array(
'#type' => 'textfield',
'#title' => t('Redirect anonymous users'),
'#description' => t('When this is non-empty, anonymous users will be redirected to the specified drupal path.'),
'#default_value' => me_variable_get('me_redirect_anonymous'),
);
$access = user_access('use PHP for me alias paths');
$path_rule = me_variable_get('me_path_rule');
$paths = me_variable_get('me_paths');
if ($path_rule == ME_PATH_PHP && !$access) {
$form['me_paths_settings'] = array();
$form['me_paths_settings']['me_path_rule'] = array(
'#type' => 'value',
'#value' => $path_rule,
);
$form['me_paths_settings']['me_paths'] = array(
'#type' => 'value',
'#value' => $paths,
);
}
else {
$options = array(
ME_PATH_EXCLUDE => t('Use me alias on every path except the listed paths.'),
ME_PATH_INCLUDE => t('Use me alias only on the listed paths.'),
);
$description = t("Enter one path per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
'%blog' => 'blog',
'%blog-wildcard' => 'blog/*',
'%front' => '<front>',
));
if ($access) {
$options[ME_PATH_PHP] = t('Use me alias if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
$description .= ' ' . t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array(
'%php' => '<?php ?>',
));
}
$form['me_paths_settings']['me_path_rule'] = array(
'#type' => 'radios',
'#title' => t('Use me alias on specific paths'),
'#options' => $options,
'#default_value' => $path_rule,
);
$form['me_paths_settings']['me_paths'] = array(
'#type' => 'textarea',
'#title' => t('Paths'),
'#default_value' => $paths,
'#description' => $description . t('<p>NOTE: This option simply ensures that the browser address bar for these paths have the uid and not me. The me alias will still work for these paths. It will have no effect on specific uids in paths, but if the path includes the me alias, then me will be affected for those paths. This will only affect paths that me can already handle. It will not allow me to work for unknown paths.</p>'),
);
}
$form['#validate'] = array(
'me_admin_settings_form_validate',
);
$form = system_settings_form($form);
$form['#submit'][] = 'menu_rebuild';
$form['#submit'][] = 'drupal_rebuild_theme_registry';
return $form;
}
function me_admin_settings_form_validate($form, &$form_state) {
$token_list = array();
if (module_exists('token')) {
$token_list = array_map(create_function('$n', 'return "[$n]";'), array_keys(array_pop(token_get_list('global'))));
}
if (preg_match('/[^a-zA-Z]/', $form_state['values']['me_alias']) && !in_array($form_state['values']['me_alias'], $token_list)) {
if (!empty($token_list)) {
$message = t('The alias can only contain characters from a-z and A-Z, or one of the tokens specified in the "Replacement patterns for me alias" section.');
}
else {
$message = t('The alias can only contain characters from a-z and A-Z.');
}
form_set_error('me_alias', $message);
}
}
function me_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'categories':
if (me_variable_get('me_user_override')) {
return array(
array(
'name' => 'me',
'title' => t("'@me' alias", array(
'@me' => _me_get_me_alias(TRUE),
)),
'weight' => 2,
),
);
}
break;
case 'form' && $category == 'me':
$form = array();
$form['me_uid'] = array(
'#type' => 'item',
'#title' => t('User id'),
'#value' => $account->uid,
);
$form['me_disable'] = array(
'#type' => 'checkbox',
'#title' => t("Disable '%me' alias for this account", array(
'%me' => _me_get_me_alias(TRUE),
)),
'#description' => t('This option stops your user id from being replaced with %me. %me will still work when entered into the address bar, but you will be redirected to a page with your uid in its place.', array(
'%me' => _me_get_me_alias(TRUE),
)),
'#default_value' => !empty($account->me_disable),
);
return $form;
case 'view':
if (me_variable_get('me_user_override')) {
$enabled = 'enabled';
if (!empty($account->me_disable)) {
$enabled = 'disabled';
}
$account->content['me'] = array(
'#type' => 'markup',
'#value' => t("'%me' aliases are {$enabled} for this account. Account user id is '@uid'.", array(
'%me' => _me_get_me_alias(TRUE),
'@uid' => $account->uid,
)),
'#weight' => 10,
);
}
break;
}
}