You are here

anonymous_login.module in Anonymous login 7

Same filename and directory in other branches
  1. 8.2 anonymous_login.module
  2. 8 anonymous_login.module

File

anonymous_login.module
View source
<?php

/**
 * Implements hook_menu().
 */
function anonymous_login_menu() {
  $items = array();
  $items['admin/config/system/anonymous-login'] = array(
    'title' => 'Anonymous login',
    'description' => 'Specify pages that force anonyous users to login',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'anonymous_login_admin_settings',
    ),
    'access arguments' => array(
      'administer anonymous login settings',
    ),
    'file' => 'anonymous_login.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_init().
 */
function anonymous_login_init() {
  anonymous_login_redirect();
}

/**
 * Perform the anonymous user redirection, if needed.
 */
function anonymous_login_redirect() {

  // Skip if maintenance mode is enabled.
  if (variable_get('maintenance_mode', FALSE)) {
    return;
  }

  // Check if user is anonymous.
  if (user_is_anonymous()) {
    $redirect = FALSE;

    // Determine the login page path.
    $login_path = variable_get('anonymous_login_page_path', 'user/login');

    // Detect whether this request is for cron or site installation or xmlrpc
    // request.
    foreach (array(
      'cron',
      'install',
      'xmlrpc',
    ) as $task) {

      // Generate a path for the task.
      $path = base_path() . "{$task}.php";

      // See if we have a match.
      if (substr(request_uri(), 0, strlen($path)) == $path) {

        // Stop here.
        return;
      }
    }

    // Proceed if the request is not coming from the command-line.
    if (!drupal_is_cli()) {

      // Fetch paths to force login.
      $paths = anonymous_login_paths();

      // Continue only if there are paths set to redirect on.
      if (!empty($paths['include'])) {

        // Determine the current path.
        $path = drupal_strtolower(filter_xss($_GET['q']));

        // Convert the path if this is the home page.
        $path = $path ? $path : '<front>';

        // Determine the current path's alias.
        $alias = strtolower(drupal_get_path_alias($path));

        // See if we have a match for the included paths against the
        // current path and it's alias.
        foreach (array(
          $path,
          $alias,
        ) as $check) {

          // Redirect if the path is a match for included paths.
          if (drupal_match_path($check, implode("\n", $paths['include']))) {
            $redirect = TRUE;
          }

          // Do not redirect if the path is a match for excluded paths.
          if (drupal_match_path($check, implode("\n", $paths['exclude']))) {
            $redirect = FALSE;

            // Matching an excluded path is a hard-stop.
            break;
          }
        }

        // See if we're going to redirect.
        if ($redirect) {

          // See if we have a message to display.
          if ($message = variable_get('anonymous_login_message', NULL)) {
            drupal_set_message(t($message));
          }
          $destination = array(
            'destination' => $alias,
          );

          // Redirect to the login, keeping the requested path.
          drupal_goto($login_path, array(
            'query' => $destination,
          ));
        }
      }
    }
  }
}

/**
 * Fetch the paths that should be used when determining when to force
 * anonymous users to login.
 *
 * @return
 *   An array of paths, keyed by "include", paths that should force a
 *   login, and "exclude", paths that should be ignored.
 */
function anonymous_login_paths() {
  $paths = array(
    'include' => array(),
    'exclude' => array(),
  );

  // Fetch the stored paths set in the admin settings.
  if ($setting = variable_get('anonymous_login_paths', NULL)) {

    // Split by each newline.
    $setting = explode("\n", $setting);

    // Iterate each path and determine if the path should be included
    // or excluded.
    foreach ($setting as $path) {
      if (substr($path, 0, 1) == '~') {
        $paths['exclude'][] = substr($path, 1);
      }
      else {
        $paths['include'][] = $path;
      }
    }
  }

  // Always exclude certain paths.
  $paths['exclude'][] = variable_get('anonymous_login_page_path', 'user/login');
  $paths['exclude'][] = 'user/reset/*';

  // Allow other modules to alter the paths.
  drupal_alter('anonymous_login_paths', $paths);
  return $paths;
}

/**
 * Implements hook_permission().
 */
function anonymous_login_permission() {
  return array(
    'administer anonymous login settings' => array(
      'title' => t('Administer anonymous login settings'),
    ),
  );
}

Functions

Namesort descending Description
anonymous_login_init Implements hook_init().
anonymous_login_menu Implements hook_menu().
anonymous_login_paths Fetch the paths that should be used when determining when to force anonymous users to login.
anonymous_login_permission Implements hook_permission().
anonymous_login_redirect Perform the anonymous user redirection, if needed.