You are here

function _encrypt_decrypt in Encrypt 7.2

Same name and namespace in other branches
  1. 6 includes/encrypt.crypt.inc \_encrypt_decrypt()
  2. 7.3 includes/encrypt.encrypt.inc \_encrypt_decrypt()
  3. 7 includes/encrypt.crypt.inc \_encrypt_decrypt()

Private internal function to Encrypt and Decrypt text.

Parameters

string $op: Whether to encrypt or decrypt.

  • "encrypt": Encrypt text.
  • "decrypt": Decrypt text.

string $text: Text to encrypt.

array $options: Array of options for encryption.

mixed $method: String name of method to use. Uses setting default if NULL.

mixed $key_provider: The key provider used to fetch the encryption key. Uses default if NULL.

string $config: A configuration to use. Uses the default configuration if NULL.

Return value

string Depends on the value of $op

  • "encrypt": A serialized array representing the encrypted text, method, and key provider.
  • "decrypt": The decrypted text.
2 calls to _encrypt_decrypt()
decrypt in ./encrypt.module
Decrypt text.
encrypt in ./encrypt.module
Encrypt text.

File

includes/encrypt.encrypt.inc, line 34
This file holds the functions necessary to encrypt and decrypt.

Code

function _encrypt_decrypt($op = 'encrypt', $text = '', array $options = array(), $method = NULL, $key_provider = NULL, $config = NULL) {
  $encryption_array = array();
  $processed = '';

  // Check text.
  if ($text === '') {
    return $processed;
  }

  // Check op.
  if ($op !== 'encrypt') {
    $op = 'decrypt';
  }
  if ($op == 'decrypt') {

    // The text may not actually be serialized, so suppress the warning.
    $encryption_array = @unserialize($text);
    if (!function_exists('mcrypt_module_open') && function_exists('openssl_encrypt') && 'mcrypt_aes_cbc' == $encryption_array['method']) {

      // Fallback if MCrypt is depreciated and OpenSSL exists.
      $encryption_array['method'] = 'openssl';
    }
    $method = isset($encryption_array['method']) ? $encryption_array['method'] : $method;
    $key_provider = isset($encryption_array['key_provider']) ? $encryption_array['key_provider'] : $key_provider;
    $text = isset($encryption_array['text']) ? $encryption_array['text'] : $text;
    $options = isset($encryption_array['options']) ? $encryption_array['options'] + $options : $options;
    $method_settings = isset($encryption_array['method_settings']) ? $encryption_array['method_settings'] : '';
    $provider_settings = isset($encryption_array['provider_settings']) ? $encryption_array['provider_settings'] : '';
  }

  // If the configuration was specified, load it. Otherwise, load the
  // default configuration.
  if ($config) {
    $config = encrypt_get_config($config);
  }
  else {
    $config = encrypt_get_default_config($config);
  }

  // If the method was not specified, use the method
  // from the configuration.
  if (empty($method)) {
    $method = $config['method'];
  }

  // If the method settings were not specified, use the method settings
  // from the configuration.
  if (empty($method_settings)) {
    $method_settings = $config['method_settings'];
  }

  // If the provider was not specified, use the provider
  // from the configuration.
  if (empty($key_provider)) {
    $key_provider = $config['provider'];
  }

  // If the provider settings were not specified, use the provider settings
  // from the configuration.
  if (empty($provider_settings)) {
    $provider_settings = $config['provider_settings'];
  }

  // Get the encryption key from the default key provider.
  $key = encrypt_get_key_from_key_provider($key_provider, $provider_settings, $config);
  if (!$key) {
    drupal_set_message(t('The key from %key_provider could not be retrieved.', array(
      '%key_provider' => $key_provider,
    )), 'error');
    throw new Exception('Key not found');
  }

  // Call callback function for encryption and decryption.
  $encryption_method = encrypt_get_encryption_method($method);
  $function = ctools_plugin_get_function($encryption_method, 'encrypt callback');
  $processed = call_user_func($function, $op, $text, $key, $options, $config);

  // Check for returned value.
  if (!empty($processed) && $op == 'encrypt') {
    $encryption_array = array(
      'text' => $processed,
      'method' => $method,
      'key_provider' => $key_provider,
      'options' => $options,
      'method_settings' => $method_settings,
      'provider_settings' => $provider_settings,
    );

    // Serialize array.
    $processed = serialize($encryption_array);
  }
  return $processed;
}