You are here

function securelogin_form_alter in Secure Login 6

Same name and namespace in other branches
  1. 8 securelogin.module \securelogin_form_alter()
  2. 5 securelogin.module \securelogin_form_alter()
  3. 7 securelogin.module \securelogin_form_alter()

Alter address in password forms

File

./securelogin.module, line 96

Code

function securelogin_form_alter(&$form, $form_state, $form_id) {
  global $base_url;
  if ($form_id == 'user_login_block' && variable_get('securelogin_loginform', TRUE) || $form_id == 'user_login' && variable_get('securelogin_loginform', TRUE) || $form_id == 'user_profile_form' && variable_get('securelogin_profileform', TRUE) || $form_id == 'user_register' && variable_get('securelogin_registerform', TRUE)) {

    // Get original base URL
    $orig_url = isset($_REQUEST['securelogin_original_baseurl']) ? $_REQUEST['securelogin_original_baseurl'] : $base_url;

    // Get secure URL
    $sec_url = variable_get('securelogin_baseurl', preg_replace('@^http://@', 'https://', $base_url));

    // Check if we already have HTTPS.
    if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on') {

      // Redirect form to secure page, if necessary.
      if (variable_get('securelogin_secure_forms', FALSE)) {

        // Build destination URL.
        $sec_form_url = variable_get('clean_url', '0') ? $sec_url . '/' . $_GET['q'] : $sec_url . '/';
        if (empty($_SERVER['SCRIPT_URI']) || $_SERVER['SCRIPT_URI'] != $sec_form_url) {

          // Apply query string to destination URL, excluding $_GET['q'] if
          // clean URLs are enabled.
          $exclude = variable_get('clean_url', '0') ? array(
            'q',
          ) : array();
          $query = drupal_query_string_encode($_GET, $exclude);

          // If redirect to insecure site is enabled, provide the base URL.
          if (variable_get('securelogin_redirect', TRUE)) {
            if ($query) {
              $query .= '&';
            }
            $query .= 'securelogin_original_baseurl=' . urlencode($orig_url);
          }

          // Ignore destination on this redirect. It has been preserved in the
          // destination URL query string.
          unset($_REQUEST['destination']);
          drupal_goto($sec_form_url, $query, NULL, 301);
        }
      }
    }

    // Strip trailing slash from base_path
    $base = rtrim(base_path(), '/');

    // Set form action
    $form['#action'] = preg_replace('@^' . $base . '@', $sec_url, $form['#action']);

    // If redirect to insecure site is enabled, add field to remember original
    // base URL.
    if (variable_get('securelogin_redirect', TRUE)) {
      $form['securelogin_original_baseurl'] = array(
        '#type' => 'hidden',
        '#value' => $orig_url,
      );
    }
  }
}