You are here

rename_admin_paths.module in Rename Admin Paths 7.2

Allows users to rename admin paths

File

rename_admin_paths.module
View source
<?php

/**
 * @file
 * Allows users to rename admin paths
 */

/**
 * Implements hook_help().
 */
function rename_admin_paths_help($path, $arg) {
  switch ($path) {
    case 'admin/help#rename_admin_paths':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The path admin module allows users to rename admin paths (/admin/..) to /something/... and (/user/..) to /something_else/...') . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl><dt>' . t('Rename admin paths') . '</dt>';
      $output .= '<dd>' . t('Choose how to rename admin paths by using') . ' ' . l(t('Path admin configuration'), 'admin/config/user-interface/rename-admin-paths') . '</dd>';
      $output .= '</dl>';
      return $output;
    case 'admin/config/user-interface/rename-admin-paths':
      $output = '<p>' . t('Configure how to rename admin paths.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_permission().
 */
function rename_admin_paths_permission() {
  return array(
    'administer path admin' => array(
      'title' => t('Administer path admin'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function rename_admin_paths_menu() {
  $items['admin/config/user-interface/rename-admin-paths'] = array(
    'title' => 'Rename Admin Paths',
    'description' => 'Configure how to rename admin paths.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'rename_admin_paths_settings',
    ),
    'access arguments' => array(
      'administer path admin',
    ),
  );
  return $items;
}

/**
 * Implements hook_outbound_alter().
 */
function rename_admin_paths_url_outbound_alter(&$path, &$options, $original_path) {

  // Admin path
  if (variable_get('rename_admin_path', NULL)) {
    $rename_admin_path_value = variable_get('rename_admin_path_value', 'backend');

    // Replace admin in path
    if (preg_match('|^admin(?![^/])(.*)|', $path, $matches)) {
      $path = urlencode($rename_admin_path_value) . $matches[1];
    }
    if (!empty($options['query']['destination'])) {

      // Replace admin in the destination. Take care of the possible presence of %2F,
      // which represents the forward slash (/).
      if (preg_match('/^admin(?:(?![^\\/])|(?!(?!\\%2F)))(.*)/', $options['query']['destination'], $matches)) {
        $options['query']['destination'] = urlencode($rename_admin_path_value) . $matches[1];
      }
    }
  }

  // user path
  if (variable_get('rename_user_path', NULL)) {
    $rename_user_path_value = variable_get('rename_user_path_value', 'member');

    // Replace user in path
    if (preg_match('|^user(?![^/])(.*)|', $path, $matches)) {
      $path = urlencode($rename_user_path_value) . $matches[1];
    }
    if (!empty($options['query']['destination'])) {

      // Replace user in the destination.
      if (preg_match('/^user(?:(?![^\\/])|(?!(?!\\%2F)))(.*)/', $options['query']['destination'], $matches)) {
        $options['query']['destination'] = urlencode($rename_user_path_value) . $matches[1];
      }
    }
  }
}

/**
 * Implements hook_inbound_alter().
 */
function rename_admin_paths_url_inbound_alter(&$path, $original_path, $path_language) {
  static $once = FALSE;

  // Prevent the second call by module that invokes drupal_path_initialize(). (@see http://drupal.org/node/1703242)
  if ($once) {
    return;
  }
  $once = TRUE;

  // Admin path
  if (variable_get('rename_admin_path', NULL)) {
    $rename_admin_path_value = variable_get('rename_admin_path_value', 'backend');

    // Get back default admin path
    if (preg_match('|^' . urlencode($rename_admin_path_value) . '(?![^/])(.*)|', $path, $matches)) {
      $path = 'admin' . $matches[1];
    }

    // Get 404 for default admin path
    if (preg_match('|^admin(?![^/])|i', $original_path)) {
      $path = '404';
    }
  }

  // User path
  if (variable_get('rename_user_path', NULL)) {
    $rename_user_path_value = variable_get('rename_user_path_value', 'member');

    /**
     * Fix module path bug
     * If there is a url alias for user/*, make sure that this url alias stay accessible
     * Issue #1331770 (http://drupal.org/node/1331770)
     */
    if ($path != $original_path && drupal_get_path_alias($path) == $original_path) {
      $original_path = str_ireplace('user', 'people', $path);
    }

    // Get back default user path
    if (preg_match('|^' . urlencode($rename_user_path_value) . '(?![^/])(.*)|', $path, $matches)) {
      $path = 'user' . $matches[1];
    }

    // Get 404 for default user path
    if (preg_match('|^user(?![^/])|i', $original_path)) {
      $path = '404';
    }
  }
}

/**
 * Implements hook_admin_paths_alter().
 */
function rename_admin_paths_admin_paths_alter(&$paths) {

  // Check if enable
  if (variable_get('rename_admin_path', NULL) || variable_get('rename_admin_path', NULL)) {
    foreach ($paths as $path => $enable) {

      // Add renamed admin paths
      if (variable_get('rename_admin_path', NULL)) {
        $rename_admin_path_value = variable_get('rename_admin_path_value', 'backend');
        $renamed_path = preg_replace('|^admin(?![^/])|', $rename_admin_path_value, $path);
        $paths[$renamed_path] = $enable;
      }

      // Replace "user" in administrative paths
      if (variable_get('rename_user_path', NULL)) {
        $rename_user_path_value = variable_get('rename_user_path_value', 'member');
        $renamed_path = preg_replace('|^user(?![^/])|', $rename_user_path_value, $path);
        $paths[$renamed_path] = $enable;
      }
    }
  }
}

/**
 * Implements hook_overlay_parent_initialize().
 */
function rename_admin_paths_overlay_parent_initialize() {

  // Check if enable
  if (variable_get('rename_admin_path', NULL) || variable_get('rename_user_path', NULL)) {

    // Override overlay paths
    $paths = path_get_admin_paths();
    drupal_add_js(array(
      'overlay' => array(
        'paths' => $paths,
      ),
    ), 'setting');
  }
}

/**
 * Settings form().
 */
function rename_admin_paths_settings() {
  $form = array();
  $form['rename_admin_path'] = array(
    '#type' => 'fieldset',
    '#title' => t('Rename admin path'),
  );
  $form['rename_admin_path']['rename_admin_path'] = array(
    '#type' => 'checkbox',
    '#title' => t('Rename admin path'),
    '#default_value' => variable_get('rename_admin_path', NULL),
    '#description' => t('If checked, "admin" will be replaced by the following term in admin path.'),
  );
  $form['rename_admin_path']['rename_admin_path_value'] = array(
    '#type' => 'textfield',
    '#title' => t('Replace "admin" in admin path by'),
    '#default_value' => variable_get('rename_admin_path_value', 'backend'),
    '#description' => t('This value will replace "admin" in admin path.'),
    '#element_validate' => array(
      'rename_admin_paths_form_validate_path_field',
    ),
  );
  $form['rename_user_path'] = array(
    '#type' => 'fieldset',
    '#title' => t('Rename user path'),
  );
  $form['rename_user_path']['rename_user_path'] = array(
    '#type' => 'checkbox',
    '#title' => t('Rename user path'),
    '#default_value' => variable_get('rename_user_path', NULL),
    '#description' => t('If checked, "user" will be replaced by the following term in user path.'),
    '#element_validate' => array(
      'rename_admin_paths_form_validate_path_field',
    ),
  );
  $form['rename_user_path']['rename_user_path_value'] = array(
    '#type' => 'textfield',
    '#title' => t('Replace "user" in user path by'),
    '#default_value' => variable_get('rename_user_path_value', 'member'),
    '#description' => t('This value will replace "user" in user path.'),
    '#element_validate' => array(
      'rename_admin_paths_form_validate_path_field',
    ),
  );
  $form['#submit'][] = 'rename_admin_paths_settings_submit';
  return system_settings_form($form);
}

/**
 * Validation for paths values.
 */
function rename_admin_paths_form_validate_path_field($element, &$form_state) {

  // Force path replacement values to contain only lowercase letters, numbers, and underscores.
  if (!preg_match('!^[a-z0-9_]+$!i', $element['#value'])) {
    form_error($element, t('Path replacement value must contain only lowercase letters, numbers, and underscores.'));
  }
}

/**
 * Settings form submit.
 */
function rename_admin_paths_settings_submit() {

  // Flush admin menu
  if (function_exists('admin_menu_flush_caches')) {
    admin_menu_flush_caches();
  }
}