diff --git a/includes/base.inc b/includes/base.inc index 8b3a99c..017a815 100644 --- a/includes/base.inc +++ b/includes/base.inc @@ -160,6 +160,13 @@ abstract class MigrationBase { public static function setDisplayFunction($display_function) { self::$displayFunction = $display_function; } + + /** + * Track whether or not we've already displayed an encryption warning + * + * @var bool + */ + protected static $showEncryptionWarning = TRUE; /** * The fraction of the memory limit at which an operation will be interrupted. @@ -1036,6 +1043,72 @@ abstract class MigrationBase { return FALSE; } } + + + /** + * Encrypt an incoming value. Detects for existence of the Drupal 'Encrypt' + * module or the mcrypt PHP extension. + * + * @param string $value + * @return string The encrypted value. + */ + protected function encrypt($value) { + if (module_exists('encrypt')) { + $value = encrypt($value); + } + else if (extension_loaded('mcrypt')) { + // Mimic encrypt module to ensure compatibility + $key = drupal_substr(variable_get('drupal_private_key', 'no_key'), 0, 32); + $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); + $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); + $value = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $value, MCRYPT_MODE_ECB, $iv); + + $encryption_array['text'] = $value; + $encryption_array['method'] = 'mcrypt_rij_256'; // For forward compatibility with encrypt module + $encryption_array['key_name'] = 'drupal_private_key'; // For forward compatibility with encrypt module + $value = serialize($encryption_array); + } + else { + if (self::$showEncryptionWarning) { + MigrationBase::displayMessage(t('Your database credentials are not encrypted. Ensure the Encrypt Drupal module or mcrypt PHP extension is installed for this functionality.'), 'warning'); + self::$showEncryptionWarning = FALSE; + } + } + return $value; + } + + + /** + * Decrypt an incoming value. + * + * @param string $value + * @return string The encrypted value + */ + protected function decrypt($value) { + if (module_exists('encrypt')) { + $value = decrypt($value); + } + else if (extension_loaded('mcrypt')) { + // Mimic encrypt module to ensure compatibility + $encryption_array = unserialize($value); + $method = $encryption_array['method']; // Not used right now + $text = $encryption_array['text']; + $key_name = $encryption_array['key_name']; // Not used right now + + $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); + $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); + $key = drupal_substr(variable_get('drupal_private_key', 'no_key'), 0, 32); + $value = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv); + } + else { + if (self::$showEncryptionWarning) { + MigrationBase::displayMessage(t('Your database credentials are not encrypted. Ensure the Encrypt Drupal module or mcrypt PHP extension is installed for this functionality.'), 'warning'); + self::$showEncryptionWarning = FALSE; + } + } + return $value; + } + /** * Convert an incoming string (which may be a UNIX timestamp, or an arbitrarily-formatted