You are here

function r4032login_redirect in Redirect 403 to User Login 7

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

MENU_CALLBACK for /r4032login

Redirect anonymous users from 403 Access Denied pages to the /user/login page with a message explaining that they must log in 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
Implements hook_menu().

File

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

Code

function r4032login_redirect() {
  global $user, $language;
  if (user_is_anonymous()) {

    // Show the access denied message.
    if (variable_get('r4032login_display_denied_message', TRUE) && empty($_POST)) {
      $message = variable_get('r4032login_access_denied_message', t('Access denied. You must log in to view this page.'));
      $message_type = variable_get('r4032login_access_denied_message_type', 'error');
      drupal_set_message(filter_xss_admin($message), $message_type);
    }
    $page_match = FALSE;
    $pages = variable_get('r4032login_match_noredirect_pages', '');
    if ($pages) {

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

      // Compare the lowercase internal and lowercase path alias (if any).
      $page_match = drupal_match_path($path, $pages);
      if ($path != $_GET['destination']) {
        $page_match = $page_match || drupal_match_path($_GET['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);
    drupal_alter('r4032login_code', $code);

    // Check whether we should redirect to desired page after login
    $options = array(
      'absolute' => TRUE,
    );
    if (variable_get('r4032login_redirect_to_destination', TRUE)) {

      // The code in drupal_get_destination() doesn't preserve any query string
      // on 403 pages, so reproduce the part we want here.
      $path = $_GET['destination'];
      $query = drupal_http_build_query(drupal_get_query_parameters(NULL, array(
        'q',
        'destination',
      )));
      if ($query != '') {
        $path .= '?' . $query;
      }
      if (url_is_external($login_path)) {

        // If we are redirecting to another Drupal site for auth, add this site's
        // base path to the destination URL.
        $path = $GLOBALS['base_url'] . $GLOBALS['base_path'] . $path;
      }
      $destination = array(
        'destination' => $path,
      );
      $options['query'] = $destination;
    }
    $url = url($login_path, $options);
    drupal_alter('r4032login_url', $url);
    header('Location: ' . $url, TRUE, $code);
    drupal_exit();
  }
  else {

    // Check to see if we are to redirect the user.
    $redirect = variable_get('r4032login_redirect_authenticated_users_to', '');
    if (empty($redirect)) {

      // Display the default access denied page.
      return theme('r4032login_denied');
    }
    else {

      // Custom access denied page for logged in users.
      header('Location: ' . url($redirect, array(
        'absolute' => TRUE,
      )));
      drupal_exit();
    }
  }
}