You are here

function aes_install in AES encryption 8.2

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

Implements hook_install().

Please note - we moving our settings to YAML from default DB storage.

File

./aes.install, line 69
Install/uninstall handling for AES.

Code

function aes_install() {
  $aesImplementations = AES::get_available_implementations();
  $configDbImmutable = \Drupal::config('aes.settings');
  $configDbMutable = \Drupal::configFactory()
    ->getEditable('aes.settings');
  if ($aesImplementations['mcrypt']) {
    $configDbMutable
      ->set('implementation', 'mcrypt')
      ->save();
    $installMsg = t('AES installed using the Mcrypt implementation.');
  }
  else {
    if ($aesImplementations['phpseclib']) {
      $configDbMutable
        ->set('implementation', 'phpseclib')
        ->save();
      $installMsg = t('AES installed using the phpseclib implementation.');
    }
    else {

      // This case shouldn't actually be possible since hook_requirements should stop the installation if there's no implementation.
      $configDbMutable
        ->set('implementation', 'missing')
        ->save();
      $installMsg = t('AES installed without any implementation!');
    }
  }

  // Copy settings to YAML file storage.
  $fileStorage = FileStorageFactory::getActive();
  $fileStorage
    ->write('aes.settings', $configDbImmutable
    ->getRawData());

  // And remove settings from DB.
  $configDbMutable
    ->initWithData(array(
    'msg' => 'Configuration is stored in setting YAML file.',
  ))
    ->save();
  $configFiles = $fileStorage
    ->read('aes.settings');
  drupal_set_message($installMsg);
  if ($configFiles['implementation'] == 'mcrypt') {
    $iv = $configFiles['mcrypt_iv'];
    if (empty($iv)) {
      AES::make_iv();
      $configFiles = $fileStorage
        ->read('aes.settings');
    }
  }

  // This will create a new key if one doesn't exist.
  if (!AES::get_key()) {
    $key = AES::make_key();
    $configFiles['key'] = $key;
    $fileStorage
      ->write('aes.settings', $configFiles);
    \Drupal::logger('aes')
      ->notice("AES module made a new key since one couldn't be found.");
  }
}