<?php
/* $Id: securelogin.module,v 1.1.2.1 2007/10/12 23:25:37 avf Exp $ */

/**
* Display help and module information
*/
function securelogin_help($section='') {

  $output = '';

  switch($section) {
    case "admin/help#securelogin":
      $output = '<p>' . t("Enables passwords to be sent over a secure connection.") . '</p>';
      break;
    case "admin/settings/securelogin":
      $output = '<p>' . t("Secure Login redirects any forms with passwords to a secure address so that the password is not sent in cleartext.  If you want the user to be redirected back to the insecure site after the password is sent securely, you need to set <code>\$base_url</code> in <code>settings.php</code> to your insecure URL.") . '</p>';
      break;
  }

  return($output);
} // function securelogin_help

/**
* Menus
*/
function securelogin_menu() {

  $items = array();

  $items[] = array(
    'path' => 'admin/settings/securelogin',
    'title' => t("Secure login"),
    'description' => t("Change secure login settings"),
    'callback' => 'drupal_get_form',
    'callback arguments' => 'securelogin_admin',
    'access' => user_access('access administration pages'),
    'type' => MENU_NORMAL_ITEM
  );

  return $items;
} // function securelogin_menu

/**
* Admin form
*/
function securelogin_admin() {

  global $base_url;

  $defurl = preg_replace('@^http://@', 'https://', $base_url);

  $form['securelogin_baseurl'] = array(
    '#type' => 'textfield',
    '#title' => t("Base URL for secure site"),
    '#default_value' => variable_get('securelogin_baseurl', preg_replace('@^http://@', 'https://', $base_url)),
    '#size' => 50,
    '#maxlength' => 300,
    '#description' => t("The base URL at which the site can be accessed securely (no trailing slash), e.g. <em>" . $defurl . "</em>.  The hostname must match the hostname of the insecure URL, and in most cases, you will want the directory to match also (particularly if you have <code>RewriteBase</code> set).")
  );
  $form['securelogin_loginform'] = array(
    '#type' => 'checkbox',
    '#title' => t("Secure login form"),
    '#default_value' => variable_get('securelogin_loginform', TRUE),
    '#description' => t("Whether or not to secure the login forms.")
  );
  $form['securelogin_editform'] = array(
    '#type' => 'checkbox',
    '#title' => t("Secure user edit form"),
    '#default_value' => variable_get('securelogin_editform', TRUE),
    '#description' => t("Whether or not to secure the user edit form.")
  );
  $form['securelogin_registerform'] = array(
    '#type' => 'checkbox',
    '#title' => t("Secure user registration form"),
    '#default_value' => variable_get('securelogin_registerform', TRUE),
    '#description' => t("Whether or not to secure the new user registration form.  You may want to turn this off if new users get their passwords by email, but this will mean that creating users as an administrator will be insecure.")
  );

  return system_settings_form($form);
} // function securelogin_admin

/**
* Alter address in password forms
*/
function securelogin_form_alter($form_id, &$form) {

  if(   ($form_id == 'user_login_block' && variable_get('securelogin_loginform',    TRUE) == TRUE)
     || ($form_id == 'user_login'       && variable_get('securelogin_loginform',    TRUE) == TRUE)
     || ($form_id == 'user_edit'        && variable_get('securelogin_editform',     TRUE) == TRUE)
     || ($form_id == 'user_register'    && variable_get('securelogin_registerform', TRUE) == TRUE)
    ) {
      // Strip trailing slash from base_path
      $base = substr(base_path(), 0, -1);
      $form['#action'] = preg_replace('@^' . $base . '@', variable_get('securelogin_baseurl', $base), $form['#action']);
  }
} // function securelogin_form_alter

?>
