You are here

function anonymous_login_redirect in Anonymous login 7

Perform the anonymous user redirection, if needed.

1 call to anonymous_login_redirect()
anonymous_login_init in ./anonymous_login.module
Implements hook_init().

File

./anonymous_login.module, line 30

Code

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,
          ));
        }
      }
    }
  }
}