You are here

function aes_config_submit in AES encryption 7

Same name and namespace in other branches
  1. 5 aes.module \aes_config_submit()
  2. 6 aes.module \aes_config_submit()

Submits aes_config form.

File

./aes.admin.inc, line 196
AES encryption module administration settings.

Code

function aes_config_submit($form, &$form_state) {
  variable_set("aes_viewing_method", $form_state['values']['view_method']);
  if ($form_state['values']['aes_convert']) {
    if (!variable_get("aes_convert", FALSE)) {
      variable_set("aes_convert", TRUE);
      drupal_set_message(t("Creation of encrypted passwords enabled."));
    }
  }
  else {
    if (variable_get("aes_convert", FALSE)) {
      variable_set("aes_convert", FALSE);
      drupal_set_message(t("Creation of encrypted passwords disabled."));
    }
  }

  // Fulfill our promise if needed: PHPSecLib is locked to 128bits.
  if ($form_state['values']['aes_cipher'] == 'phpseclib') {
    $form_state['values']['aes_cipher'] = 'rijndael-128';
  }

  // Check if the storage method or key path has changed.
  if ($form_state['values']['aes_key_storage_method'] != variable_get("aes_key_storage_method", "Database") || $form_state['values']['aes_key_path'] != variable_get("aes_key_path", "")) {

    // If it has changed, we need to move the key.
    if ($form_state['values']['aes_key_storage_method'] != variable_get("aes_key_storage_method", "Database")) {
      drupal_set_message(t("Switching key storage method to %method.", array(
        '%method' => $form_state['values']['aes_key_storage_method'],
      )));
    }
    else {
      drupal_set_message(t("Changed key file path."));
    }

    // get the key
    $key = aes_get_key();

    // delete the key from the old storage
    aes_delete_key(variable_get("aes_key_storage_method"));

    // set key path
    variable_set("aes_key_path", $form_state['values']['aes_key_path']);

    // set the new storage
    variable_set("aes_key_storage_method", $form_state['values']['aes_key_storage_method']);

    // store the key in its new location
    aes_store_key($key);
  }

  // To use if re-encryption is required.
  $decrypt_params = array(
    'string' => '',
    'base64encoded' => TRUE,
    'custom_key' => aes_get_key(),
    'custom_cipher' => variable_get("aes_cipher", "rijndael-128"),
    'custom_iv' => variable_get("aes_encryption_iv", ""),
    'custom_implementation' => variable_get("aes_implementation", "mcrypt"),
  );
  $encrypt_params = array(
    'string' => '',
    'base64encode' => TRUE,
    'custom_key' => NULL,
    'custom_cipher' => NULL,
    'custom_iv' => NULL,
    'custom_implementation' => NULL,
  );
  $do_reencypt = FALSE;

  // If the cipher has changed...
  if ($form_state['values']['aes_cipher'] != variable_get("aes_cipher", "rijndael-128")) {
    variable_set("aes_cipher", $form_state['values']['aes_cipher']);
    $new_cipher = $form_state['values']['aes_cipher'];

    // update the cipher the system uses
    variable_set("aes_cipher", $form_state['values']['aes_cipher']);

    // Create a new iv to match the new cipher.
    aes_make_iv();
    $encrypt_params['custom_cipher'] = $new_cipher;
    $encrypt_params['custom_iv'] = variable_get("aes_encryption_iv");
    $do_reencypt = TRUE;
    drupal_set_message(t("Cipher changed."));
  }

  // If the key has changed...
  if (!empty($form_state['values']['aes_key'])) {
    if (!aes_store_key($form_state['values']['aes_key'])) {
      drupal_set_message(t("Failed to write new encryption key! Aborting."));
      return;
    }
    $encrypt_params['custom_key'] = $form_state['values']['aes_key'];
    $do_reencypt = TRUE;
    drupal_set_message(t("Key changed."));
  }

  // If the implementation has changed...
  if ($form_state['values']['aes_implementation'] != variable_get("aes_implementation", "mcrypt")) {
    variable_set("aes_implementation", $form_state['values']['aes_implementation']);
    $encrypt_params['custom_implementation'] = $form_state['values']['aes_implementation'];
    $do_reencypt = TRUE;
    if ($form_state['values']['aes_implementation'] == "phpseclib") {

      // If we have switched to phpseclib implementation, set the cipher to 128,
      // since it's the only one phpseclib supports.
      variable_set("aes_cipher", "rijndael-128");
      $encrypt_params['custom_cipher'] = "rijndael-128";
    }
    $iv = base64_decode(variable_get("aes_encryption_iv", ""));
    if (empty($iv)) {

      // Create a new IV, this IV won't actually be used by phpseclib, but it's
      // needed if the implementation is switched to mcrypt.
      aes_make_iv(TRUE);
      $encrypt_params['custom_iv'] = variable_get("aes_encryption_iv");
    }
    drupal_set_message(t("Implementation changed."));
  }

  // So far think of PHPSecLib as something not using IV by default.
  if ($decrypt_params['custom_implementation'] == 'phpseclib') {
    $decrypt_params['custom_iv'] = NULL;
  }
  if ($encrypt_params['custom_implementation'] == 'phpseclib') {
    $encrypt_params['custom_iv'] = NULL;
  }
  if ($do_reencypt) {

    // Calling custom hook_aes_config_change from each module which
    // implements this hook.
    foreach (module_implements('aes_config_change') as $module) {
      $function = $module . '_aes_config_change';
      $function($decrypt_params, $encrypt_params);
    }
  }
}