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'])) {
$theme_registry['menu_item_link']['me_original_function'] = $theme_registry['menu_item_link']['function'];
$theme_registry['menu_item_link']['function'] = 'me_theme_menu_item_link';
}
}
function me_preprocess_menu_link(&$vars) {
_me_check_path($vars['element']);
}
function _me_check_path(&$link) {
if (isset($link['#href']) && me_variable_get('me_rewrite_link') && !_me_handle_path($link['#href'])) {
$path_parts = explode('/', $link['#href'], MENU_MAX_PARTS);
$wild_parts = explode('/', isset($link['#original_link']['link_path']) ? $link['#original_link']['link_path'] : $link['#original_link']['router_path'], MENU_MAX_PARTS);
foreach ($path_parts as $key => $val) {
if (_me_is_alias($val) && $wild_parts[$key] == '%') {
$path_parts[$key] = $GLOBALS['user']->uid;
}
}
$link['#href'] = implode('/', $path_parts);
$link['#original_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 {
if (module_exists('php')) {
$path_match = php_eval($paths);
}
}
}
return $path_match;
}
function me_handler($parts, $callback) {
$args = func_get_args();
$callback = array_shift($args);
$parts = 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);
foreach ($parts as $key => $val) {
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 (isset($user_handler) && 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();
foreach ($parts as $key => $val) {
if (array_key_exists($val, $handlers)) {
$val = $handlers[$val];
}
$new_parts[] = $val;
}
$new_path = implode('/', $new_parts);
if (isset($data['load arguments']) && 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 (!array_key_exists($parent_path, $callbacks)) {
$parent_path = $path;
}
if (in_array($parent_path, $processed)) {
$parts = explode('/', $new_path, MENU_MAX_PARTS);
$parent_path = implode('/', array_slice($parts, 0, count($parts) - 1));
if (!array_key_exists($parent_path, $callbacks)) {
$parent_path = $path;
}
}
if (isset($callbacks[$parent_path])) {
$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 (isset($data['page arguments']) && !is_array($data['page arguments'])) {
$data['page arguments'] = array();
}
$parts = explode('/', $new_path, MENU_MAX_PARTS);
if (isset($data['page arguments'])) {
$data['page arguments'] = array_merge(array(
$data['page callback'],
$parts,
), $data['page arguments']);
}
else {
$data['page arguments'] = array(
$data['page callback'],
$parts,
);
}
$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', $reset = FALSE) {
global $user;
static $cache = array();
if ($reset) {
$cache = array();
}
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));
$result = FALSE;
if (function_exists($function)) {
if ($function == 'user_load' || $function == 'user_category_load') {
if (!isset($cache[$function][$args[0]])) {
$cache[$function][$args[0]] = call_user_func_array($function, $args);
}
$result = $cache[$function][$args[0]];
}
else {
$result = call_user_func_array($function, $args);
}
}
return $result;
}
function me_load($uid, &$map = NULL, $index = NULL, $map_index = FALSE) {
return _me_load_arguments($uid, $map, $index, $map_index);
}
function me_uid_optional_load($uid, &$map = NULL, $index = NULL, $map_index = FALSE) {
if (!isset($uid)) {
if (!$GLOBALS['user']->uid) {
return;
}
$uid = $GLOBALS['user']->uid;
}
$args = func_get_args();
return _me_load_arguments($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()) {
$uid = $uid == $GLOBALS['user']->uid ? _me_get_me_alias() : $uid;
}
return $uid;
}
function me_uid_optional_to_arg($arg, $map, $index) {
return user_uid_optional_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);
$ancestors = menu_get_ancestors($parts);
if (($router_item = db_query_range('SELECT * FROM {menu_router} WHERE path IN (:ancestors) ORDER BY fit DESC', 0, 1, array(
':ancestors' => $ancestors,
))
->fetchAssoc()) && $router_item['page_callback'] == 'me_handler') {
unset($_GET['destination'], $_REQUEST['edit']['destination']);
drupal_goto($redirect_path);
}
}
return $return;
}
function _me_get_me_alias($print = FALSE) {
$alias = me_variable_get('me_alias');
$replaced_alias = token_replace($alias);
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_menu() {
$items = array();
$items['admin/config/people/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,
);
return $items;
}
function me_permission() {
return array(
'use PHP for me alias paths' => array(
'title' => t('use PHP for me alias paths'),
'description' => t('use PHP for me alias paths'),
),
);
}
function me_admin_settings_form($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,
);
$form['me_token_help'] = array(
'#title' => t('Replacement patterns for me alias'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'help' => array(
'#type' => 'markup',
'#markup' => theme('token_tree'),
),
);
$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_theme_rebuild';
return $form;
}
function me_admin_settings_form_validate($form, &$form_state) {
$token_list = array();
$tk_types = token_get_global_token_types();
$options = array(
'flat' => TRUE,
'restricted' => FALSE,
'depth' => 4,
);
foreach ($tk_types as $type) {
$tree = token_build_tree($type, $options);
$token_list = array_merge($token_list, array_keys($tree));
}
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_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,
),
);
}
}
function me_user_view($account, $view_mode) {
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,
);
}
}
function me_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools') {
return 'plugins/' . $plugin;
}
}
function me_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'me') . '/includes/views',
);
}