You are here

function r4032login_redirect in Redirect 403 to User Login 6

Same name and namespace in other branches
  1. 5 r4032login.module \r4032login_redirect()
  2. 7 r4032login.module \r4032login_redirect()

MENU_CALLBACK for /4032login

Redirect anonymous users from 403 Access Denied pages to the /user/login page with a message explaining that they must login to view the requested page and a query string parameter appended to the url to return after login.

1 string reference to 'r4032login_redirect'
r4032login_menu in ./r4032login.module
Implementation of hook_menu().

File

./r4032login.module, line 105
Redirect denied pages to the user login form.

Code

function r4032login_redirect() {
  global $user;
  $redirect = variable_get('r4032login_redirect_authenticated_users_to', '');
  if (user_is_anonymous()) {

    // Only display the message if there is one.
    $message = variable_get('r4032login_display_denied_message', t('Access denied. You must login to view this page.'));
    if (!empty($message) && empty($_POST)) {
      drupal_set_message(filter_xss_admin($message), 'error');
    }
    $page_match = FALSE;
    $pages = variable_get('r4032login_match_redirect_pages', '');
    if ($pages) {

      // When on an access denied page, Drupal stores the original path in
      // $_REQUEST['destination'] in drupal_access_denied().
      // Convert the Drupal path to lowercase
      $path = drupal_strtolower(drupal_get_path_alias($_REQUEST['destination']));

      // Compare the lowercase internal and lowercase path alias (if any).
      $page_match = drupal_match_path($path, $pages);
      if ($path != $_REQUEST['destination']) {
        $page_match = $page_match || drupal_match_path($_REQUEST['destination'], $pages);
      }
    }
    if ($page_match) {

      // Display the default login page.
      return drupal_get_form('user_login');
    }

    // Handle redirection to the login form.
    // using drupal_goto() with destination set causes a recursive redirect loop
    $login_path = variable_get('r4032login_user_login_path', 'user/login');
    $code = variable_get('r4032login_default_redirect_code', 302);
    header('Location: ' . url($login_path, array(
      'query' => _r4032login_destination(),
      'absolute' => TRUE,
    )), TRUE, $code);
    _r4032login_exit();
  }
  elseif (!empty($redirect)) {
    header('Location: ' . url($redirect));
    _r4032login_exit();
  }
  elseif ($_REQUEST['q'] == 'user/register') {
    return theme('r4032login_user_register');
  }
  else {
    return theme('r4032login_denied');
  }
}