You are here

aes.install in AES encryption 8.2

Same filename and directory in other branches
  1. 5 aes.install
  2. 6 aes.install
  3. 7 aes.install

Install/uninstall handling for AES.

File

aes.install
View source
<?php

/**
 * @file
 * Install/uninstall handling for AES.
 */
use Drupal\aes\AES;

// @todo move this to service.
require_once 'src/AES.php';
use Drupal\Core\Config\FileStorageFactory;

/**
 * Implements hook_requirements().
 */
function aes_requirements($phase) {
  $aesImplementations = AES::get_available_implementations();
  if ($aesImplementations['mcrypt'] === false && $aesImplementations['phpseclib'] === false) {
    $requirementSeverity = REQUIREMENT_ERROR;
    $value = t('The AES encryption module requires at least one of two things to function: Either the PHP Mcrypt extension has to be installed on the web server, or the PHP Secure Communications Library (phpseclib) needs to be installed in the AES directory. Check the README.txt for more information.');
  }
  else {
    $requirementSeverity = REQUIREMENT_OK;
    if ($aesImplementations['mcrypt'] && $aesImplementations['phpseclib']) {
      $value = t('Both MCrypt and PHP Secure Communications Library are available.');
    }
    else {
      if ($aesImplementations['mcrypt']) {
        $value = t('MCrypt is available.');
      }
      else {
        $value = t('PHP Secure Communications Library is available.');
      }
    }
  }
  $requirements['aes_lib'] = array(
    'title' => t('[AES encryption implementation]'),
    'value' => $value,
    'severity' => $requirementSeverity,
  );
  $requirementSeverity = REQUIREMENT_OK;
  $value = t("The 'active' config directory found.");
  try {
    config_get_config_directory(CONFIG_ACTIVE_DIRECTORY);
  } catch (\Exception $e) {
    $requirementSeverity = REQUIREMENT_ERROR;
    $value = t("Requirements error - cannot install AES: No 'active' config directory found. Please define it in your settings.php file.");
  }
  $requirements['aes_config_dir'] = array(
    'title' => t('[AES config directory]'),
    'value' => $value,
    'severity' => $requirementSeverity,
  );
  return $requirements;
}

/**
 * Implements hook_install().
 *
 * Please note - we moving our settings to YAML from default DB storage.
 */
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.");
  }
}

/**
 * Implements hook_uninstall().
 */
function aes_uninstall() {
  FileStorageFactory::getActive()
    ->delete('aes.settings');
  drupal_set_message(t('AES uninstalled.'));
}

Functions

Namesort descending Description
aes_install Implements hook_install().
aes_requirements Implements hook_requirements().
aes_uninstall Implements hook_uninstall().