You are here

public function AesAdminForm::submitForm in AES encryption 8.2

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ConfigFormBase::submitForm

File

src/Form/AesAdminForm.php, line 138

Class

AesAdminForm
Provides a fields form controller.

Namespace

Drupal\aes\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  \Drupal::logger('aes')
    ->notice('Saving config...');

  // $config = $this->config('aes.settings')->getRawData();
  $fileStorage = FileStorageFactory::getActive();
  $config = $fileStorage
    ->read('aes.settings');

  // If the cipher has changed...
  $old_cipher = $config['cipher'];
  $new_cipher = $form_state
    ->getValue('cipher');
  if ($form_state
    ->getValue('cipher') != $config['cipher']) {
    $config['cipher'] = $form_state
      ->getValue('cipher');
    FileStorageFactory::getActive()
      ->write('aes.settings', $config);

    // Get the old iv.
    $old_iv = $config['mcrypt_iv'];

    // create a new iv to match the new cipher
    AES::make_iv();

    // get the new iv
    $config = $fileStorage
      ->read('aes.settings');
    $new_iv = $config['mcrypt_iv'];
  }

  // If the key has changed...
  if ($form_state
    ->getValue('key') != $config['key']) {
    $config['key'] = $form_state
      ->getValue('key');
    FileStorageFactory::getActive()
      ->write('aes.settings', $config);
    drupal_set_message(t('Key changed.'));

    // @todo: invoke hook?
  }

  // If the implementation has changed...
  if ($form_state
    ->getValue('implementation') != $config['implementation']) {
    $config['implementation'] = $form_state
      ->getValue('implementation');
    FileStorageFactory::getActive()
      ->write('aes.settings', $config);
    if ($form_state
      ->getValue('implementation') == 'phpseclib') {

      // If we have switched to phpseclib implementation, set the cipher to 128, since it's the only one phpseclib supports.
      $config['cipher'] = 'rijndael-128';
      FileStorageFactory::getActive()
        ->write('aes.settings', $config);

      // Create a new IV, this IV won't actually be used by phpseclib, but it's needed if the implementation is switched back to mcrypt.
      AES::make_iv(TRUE);
    }
  }
}