<?php

/**
 * @file
 * Install, update and uninstall functions for the encrypt module.
 */

/**
 * Implements hook_uninstall().
 *
 * Delete all of our variables from the variables table.
 */
function encrypt_uninstall() {
  $vars = db_select('variable', 'v')
    ->fields('v', array('name'))
    ->condition('name', 'encrypt_%', 'LIKE')
    ->execute()
    ->fetchCol();

  foreach ($vars as $var) {
    variable_del($var);
  }
}

/**
 * Implements hook_requirements().
 */
function encrypt_requirements($phase) {
  $requirements = array();

  switch ($phase) {
    case 'runtime':
      $method = encrypt_get_encryption_method();
      $key_provider = encrypt_get_key_provider();

      // Check the plugins for dependency errors.
      if (isset($method['dependency errors']) && !empty($method['dependency errors'])) {
        $dependency_errors .= theme('item_list', array('items' => $method['dependency errors']));
      }
      if (isset($key_provider['dependency errors']) && !empty($key_provider['dependency errors'])) {
        $dependency_errors .= theme('item_list', array('items' => $key_provider['dependency errors']));
      }

      if (isset($dependency_errors) && !empty($dependency_errors)) {
        $requirements['encrypt_dependencies'] = array(
          'title' => t('Encrypt Dependencies'),
          'value' => t('Unmet dependencies'),
          'description' => t('There are unmet dependencies in your active encryption plugins. Either !change_link or resolve the following dependencies: !deps', array(
            '!change_link' => l('change your encryption settings', 'admin/config/system/encrypt'),
            '!deps' => $dependency_errors,
          )),
          'severity' => REQUIREMENT_ERROR,
        );
      }

      // Set a warning about the Drupal Private Key method.
      if ($key_provider['name'] == 'drupal_private_key') {
        $requirements['encrypt_secure'] = array(
          'title' => t('Encryption Security'),
          'description' => t(
            'The site is using the private key stored in the database to encrypt
            data. While this provides some level of obfuscation, it is highly
            recommended to store the encryption key outside of the database.
            See the !link for more information.', array(
              '!link' => l('Encrypt module page', 'http://drupal.org/project/encrypt'),
          )),
          'severity' => REQUIREMENT_WARNING,
          'value' => t('Security concerns'),
        );
      }

      break;
  }

  return $requirements;
}

/**
 * Our default encryption method variable has changed.
 * So get the old one and save it as the new one.
 */
function encrypt_update_7200() {
  if ($default_method = variable_get('encrypt_default_method', NULL)) {
    variable_set('encrypt_encryption_method', $default_method);
    variable_del('encrypt_default_method');
  }
}

/**
 * Provide a nice upgrade for 1.x users by copying their key path.
 */
function encrypt_update_7201() {
  if ($key_path = variable_get('encrypt_secure_key_path', NULL)) {
    variable_set('encrypt_key_provider', 'file');
    variable_set('encrypt_key_providers_file_settings', array(
      'path' => $key_path,
      'method' => 'file_contents',
    ));
    variable_del('encrypt_secure_key_path');
  }
}
