You are here

me.module in me aliases 5

Same filename and directory in other branches
  1. 8 me.module
  2. 6.2 me.module
  3. 6 me.module
  4. 7 me.module

File

me.module
View source
<?php

/**
 * Implementation of hook_menu().
 */
function me_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/me',
      'title' => t('\'me\' Aliases'),
      'description' => t('Define URL paths for Me aliasing.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'me_settings',
      ),
      'access' => user_access('administer site configuration'),
      'type' => MENU_NORMAL_ITEM,
    );
  }

  // If users have static paths for 'me' in the menu, then we need to be able to handle those correctly.
  // This is handled when $may_cache is TRUE.
  // When $may_cache is FALSE, then we need to check the current path to see

  //i f it matches any of our aliases. We only need to check that it matches

  // the beginning of the alias. For this to work properly, 'me' also needs
  // to be the last module to run it's hook_menu().
  return array_merge($items, _me_create_menu_items($may_cache));
}

/**
 * Helper function to create our menu items.
 */
function _me_create_menu_items($may_cache) {
  $items = array();
  $aliases = explode("\n", variable_get('me_aliases', 'user/me'));
  foreach ($aliases as $alias) {

    // check to see if there's a 'me' to replace;
    // either '.../me/...', 'me/...' or '.../me' but eg 'readme/...' does not count
    $alias = preg_split('/[\\?\\#]/', $alias);
    $alias = trim(check_url($alias[0]), "/ \t\n\r\0");
    $path = drupal_get_normal_path($_GET['q']);
    if (preg_match('/(\\/m|^m)(e$|e\\/)/', $alias, $matches) > 0 && ($may_cache || 0 === strpos($path, $alias))) {
      $items[] = array(
        'path' => $may_cache ? $alias : $path,
        'type' => MENU_CALLBACK,
        'callback' => 'me_relay',
        'access' => true,
      );
      if (!$may_cache) {
        break;
      }
    }
  }
  return $items;
}

/**
 * Implementation of hook_settings().
 *
 * Provide a single textbox that allows admins to enter any number of paths containing 'me'
 */
function me_settings() {
  $form['me_aliases'] = array(
    '#type' => 'textarea',
    '#title' => t('Aliases to create'),
    '#default_value' => variable_get('me_aliases', "user/me"),
    '#cols' => 50,
    '#rows' => 6,
    '#description' => t('The per-user aliases to create. Each alias must contain the \'me\' fragment or it will be ignored. Enter one alias per line, and do not include trailing or leading slashes.'),
  );
  return system_settings_form($form);
}

/**
 * Forward to same url with proper uid this time.
 *
 * The paths have already been checked to contain 'me' in the _menu hook.
 * We don't have to check node access, drupal_goto will take care of that.
 */
function me_relay() {
  global $user;
  if ($user->uid != 0) {
    $index = 0;
    $destination = '';
    $fragment = arg(0);
    while ($fragment) {
      $destination .= ($destination == '' ? '' : '/') . ($fragment == 'me' ? $user->uid : $fragment);
      $index++;
      $fragment = arg($index);
    }
    drupal_goto($destination);
  }
  else {

    // user is not logged in
    drupal_set_message(t('Please login to access this personalised page.'));
    $destination = "destination=" . drupal_urlencode($_GET['q']);
    drupal_goto('user/login', $destination);
  }
}

Functions

Namesort descending Description
me_menu Implementation of hook_menu().
me_relay Forward to same url with proper uid this time.
me_settings Implementation of hook_settings().
_me_create_menu_items Helper function to create our menu items.