You are here

require_login.module in Require Login 7

Same filename and directory in other branches
  1. 8.2 require_login.module
  2. 8 require_login.module
  3. 6 require_login.module

File

require_login.module
View source
<?php

/**
 * @file
 * require_login.module
 */
define('REQUIRE_LOGIN_DEFAULT_MESSAGE', 'You must login to view this page.');

/**
 * Implements hook_menu().
 */
function require_login_menu() {
  $items['admin/config/people/require-login'] = array(
    'title' => 'Require login',
    'description' => 'Settings for required user authentication (login).',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'require_login_admin_settings',
    ),
    'file' => 'require_login.admin.inc',
    'access arguments' => array(
      'administer require login',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function require_login_permission() {
  return array(
    'administer require login' => array(
      'title' => t('Administer require login'),
      'description' => t('Configure required user authentication (login) settings.'),
    ),
  );
}

/**
 * Implements hook_init().
 */
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'] : '',
    ));
  }
}

/**
 * Implements hook_variable_info().
 */
function require_login_variable_info($options) {
  $variables = array();
  $variables['require_login_deny_message'] = array(
    'type' => 'text',
    'title' => t('Access Denied Message', array(), $options),
    'default' => REQUIRE_LOGIN_DEFAULT_MESSAGE,
    'description' => t('Shown to anonymous users prior to user authentication (login).', array(), $options),
    'localize' => TRUE,
  );
  return $variables;
}

/**
 * Checks current user authentication.
 *
 * @return bool
 *   Returns TRUE if authenticated and FALSE otherwise.
 */
function require_login_authcheck() {
  global $base_path, $base_url;
  $path = '/' . drupal_get_path_alias($_GET['q']);

  // Isolate request path from subdirectory when present.
  $raw_uri = request_uri();
  if ($base_path != '/') {
    $current_uri = '/' . str_replace($base_path, '', $raw_uri);
  }
  else {
    $current_uri = $raw_uri;
  }

  // Detect PHP script running Drupal bootstrap.
  if (preg_match('/^.*\\.php$/i', $raw_uri)) {
    $path = $base_path . trim($current_uri, '/');
  }

  // Compare current path with configured exclude paths. Returns TRUE when
  // at least one excluded path matches the current path.
  $exclude_paths = explode(PHP_EOL, variable_get('require_login_excluded_paths', ''));
  foreach ($exclude_paths as $key => $exclude_path) {
    $exclude_paths[$key] = trim($exclude_path);
    if ($exclude_paths[$key] == '<front>') {
      $exclude_paths[$key] = '/' . drupal_get_path_alias(variable_get('site_frontpage', 'node'));
    }
  }
  if ($auth_path = filter_xss_admin(trim(variable_get('require_login_auth_path', '')))) {
    $auth_path = drupal_parse_url($auth_path);
    $exclude_paths[] = trim($auth_path['path']);
  }
  if (drupal_match_path($path, implode(PHP_EOL, $exclude_paths))) {
    return TRUE;
  }

  // Various checks to determine exceptions for current page. Returns TRUE
  // when at least one check has evaluated as TRUE.
  $checks = array(
    // Check user authentication.
    user_is_logged_in(),
    // Check maintenance mode.
    variable_get('maintenance_mode', 0),
    // Check cron.php file.
    preg_match('/^\\/cron.php/i', $current_uri),
    // Check update.php file.
    preg_match('/\\/update.php/i', request_uri()),
    // Check install.php file.
    preg_match('/\\/install.php/i', request_uri()),
    // Check OpenID integration.
    isset($_SESSION['openid']),
    // Check user pages.
    arg(0) == 'user' && (!arg(1) || !is_numeric(arg(1))),
    // Check drush command.
    function_exists('drupal_is_cli') && drupal_is_cli(),
  );
  foreach ($checks as $check) {
    if ($check) {
      return TRUE;
    }
  }
  return FALSE;
}

Functions

Namesort descending Description
require_login_authcheck Checks current user authentication.
require_login_init Implements hook_init().
require_login_menu Implements hook_menu().
require_login_permission Implements hook_permission().
require_login_variable_info Implements hook_variable_info().

Constants