You are here

function require_login_init in Require Login 7

Same name and namespace in other branches
  1. 6 require_login.module \require_login_init()

Implements hook_init().

File

./require_login.module, line 41
require_login.module

Code

function require_login_init() {

  // Check user authentication status. Non-authenticated users will
  // automatically redirect to /user/login (OR configured login path).
  if (!require_login_authcheck()) {
    $query = array(
      'destination' => $_GET['q'],
    );

    // Display access denied message.
    $deny_message = filter_xss_admin(trim(variable_get('require_login_deny_message', REQUIRE_LOGIN_DEFAULT_MESSAGE)));
    drupal_set_message($deny_message, 'warning');

    // Exclude external PHP scripts from destination query.
    if (preg_match('/^.*\\.php$/i', request_uri())) {
      $query = array();
    }

    // Prepare authentication redirect path.
    $redirect = array(
      'path' => '/user/login',
      'query' => $query,
    );
    if ($auth_path = filter_xss_admin(trim(variable_get('require_login_auth_path', '')))) {
      $redirect = drupal_parse_url($auth_path);
      if (empty($redirect['query'])) {
        $redirect['query'] = $query;
      }
    }
    drupal_goto(ltrim($redirect['path'], ''), array(
      'query' => isset($redirect['query']) ? $redirect['query'] : array(),
      'fragment' => isset($redirect['fragment']) ? $redirect['fragment'] : '',
    ));
  }
}