View source
<?php
define('REQUIRE_LOGIN_DEFAULT_MESSAGE', 'You must login to view this page.');
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;
}
function require_login_permission() {
return array(
'administer require login' => array(
'title' => t('Administer require login'),
'description' => t('Configure required user authentication (login) settings.'),
),
);
}
function require_login_init() {
if (!require_login_authcheck()) {
$query = array(
'destination' => $_GET['q'],
);
$deny_message = filter_xss_admin(trim(variable_get('require_login_deny_message', REQUIRE_LOGIN_DEFAULT_MESSAGE)));
drupal_set_message($deny_message, 'warning');
if (preg_match('/^.*\\.php$/i', request_uri())) {
$query = array();
}
$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'] : '',
));
}
}
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;
}
function require_login_authcheck() {
global $base_path, $base_url;
$path = '/' . drupal_get_path_alias($_GET['q']);
$raw_uri = request_uri();
if ($base_path != '/') {
$current_uri = '/' . str_replace($base_path, '', $raw_uri);
}
else {
$current_uri = $raw_uri;
}
if (preg_match('/^.*\\.php$/i', $raw_uri)) {
$path = $base_path . trim($current_uri, '/');
}
$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;
}
$checks = array(
user_is_logged_in(),
variable_get('maintenance_mode', 0),
preg_match('/^\\/cron.php/i', $current_uri),
preg_match('/\\/update.php/i', request_uri()),
preg_match('/\\/install.php/i', request_uri()),
isset($_SESSION['openid']),
arg(0) == 'user' && (!arg(1) || !is_numeric(arg(1))),
function_exists('drupal_is_cli') && drupal_is_cli(),
);
foreach ($checks as $check) {
if ($check) {
return TRUE;
}
}
return FALSE;
}