You are here

shield.module in Shield 6

Same filename and directory in other branches
  1. 8 shield.module
  2. 7 shield.module

Functions for shield module

File

shield.module
View source
<?php

/**
 * @file
 * Functions for shield module
 */

/**
 * Implementation of hook_perm().
 */
function shield_perm() {
  return array(
    'administer shield',
  );
}

/**
 * Implementation of hook_menu().
 */
function shield_menu() {
  $items['admin/settings/shield'] = array(
    'title' => 'Shield',
    'description' => 'Manage the settings of PHP Authentication shield.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'shield_admin_settings',
    ),
    'weight' => 10,
    'access arguments' => array(
      'administer shield',
    ),
  );
  return $items;
}

/**
 * Administration settings form
 */
function shield_admin_settings() {
  $form['description'] = array(
    '#type' => 'item',
    '#title' => t('Shield settings'),
    '#description' => t('Set up credentials for an authenticated user. You can also decide whether you want to print out the credentials or not.'),
  );
  $form['general'] = array(
    '#type' => 'fieldset',
    '#title' => t('General settings'),
  );
  $form['general']['shield_allow_cli'] = array(
    '#type' => 'checkbox',
    '#title' => t("Allow command line access"),
    '#description' => t('When the site is accessed from command line (e.g. from Drush, cron), the shield should not work.'),
    '#default_value' => variable_get('shield_allow_cli', 1),
  );
  $form['credentials'] = array(
    '#type' => 'fieldset',
    '#title' => t('Credentials'),
  );
  $form['credentials']['shield_user'] = array(
    '#type' => 'textfield',
    '#title' => t("User"),
    '#default_value' => variable_get('shield_user', ''),
  );
  $form['credentials']['shield_pass'] = array(
    '#type' => 'textfield',
    '#title' => t("Password"),
    '#default_value' => variable_get('shield_pass', ''),
  );
  $form['shield_print'] = array(
    '#type' => 'textfield',
    '#title' => t('Authentication message'),
    '#description' => t("The message to print in the authentication request popup. You can use [user] and [pass] to print the user and the password respectively. You can leave it empty, if you don't want to print out any special message to the users."),
    '#default_value' => variable_get('shield_print', 'Hello, user: [user], pass: [pass]!'),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_boot().
 */
function shield_boot() {
  $user = variable_get('shield_user', '');
  if (!$user) {
    return;
  }

  // allow Drush to bypass Shield
  if (php_sapi_name() == 'cli' && variable_get('shield_allow_cli', 1) == 1) {
    return;
  }
  $pass = variable_get('shield_pass', '');

  // handling CGI PHP calls, where there is no PHP_AUTH_USER and PHP_AUTH_PW
  // http://www.besthostratings.com/articles/http-auth-php-cgi.html
  if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
    list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
  }
  if (!empty($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW']) && $_SERVER['PHP_AUTH_USER'] == $user && $_SERVER['PHP_AUTH_PW'] == $pass) {
    return;
  }
  $print = variable_get('shield_print', 'Hello, user: [user], pass: [pass]!');
  header(sprintf('WWW-Authenticate: Basic realm="%s"', strtr($print, array(
    '[user]' => $user,
    '[pass]' => $pass,
  ))));
  header('HTTP/1.0 401 Unauthorized');
  exit;
}

Functions

Namesort descending Description
shield_admin_settings Administration settings form
shield_boot Implementation of hook_boot().
shield_menu Implementation of hook_menu().
shield_perm Implementation of hook_perm().