function encrypt_get_key in Encrypt 7
Same name and namespace in other branches
- 6 includes/encrypt.crypt.inc \encrypt_get_key()
Get Key
Get key from appropriate place. Ideally the key should be stored outside the webroot, but may be stored in drupal files or in the db. If none are there, 'files_default' will be used
@reset Boolean whether to reset cache or not
Parameters
$key_name: Name of key, use 'default' by default
Return value
FALSE if no key found or Key Array with following keys.
- "name": Name of key to be able to refer later
- "key": Actual key
1 call to encrypt_get_key()
- _encrypt_decrypt in includes/
encrypt.crypt.inc - Private Encrypt and Decrypt
File
- includes/
encrypt.crypt.inc, line 77 - This file holds the ffunctions necessary to encrypt and decrypt
Code
function encrypt_get_key($key_name = NULL, $reset = FALSE) {
static $keys = array();
static $searched = FALSE;
$return_array = array();
// Check if keys has already been retrieved
if (!$searched || $reset) {
// Get secure encrypt file
$encrypt_key_path = rtrim(variable_get('encrypt_secure_key_path', ''), '/\\');
$encrypt_key_file = $encrypt_key_path . '/' . ENCRYPT_SECURE_KEY_FILE;
// Check secure encrypt file
if (file_exists($encrypt_key_file)) {
$file_data = file_get_contents($encrypt_key_file);
// Check if key is in file
if (!empty($file_data)) {
$keys[ENCRYPT_DEFAULT_KEY_FILE] = $file_data;
}
}
// Though, this is insecure, we default to the drupal_private_key
$keys[ENCRYPT_DEFAULT_KEY_DB] = variable_get('drupal_private_key', ENCRYPT_DEFAULT_KEY_NONE);
// Mark as searched
$searched = TRUE;
}
// Check key name
if ($key_name == NULL) {
$key_name = variable_get('encrypt_default_key', ENCRYPT_DEFAULT_KEY);
}
// If there is a vlaue for the key name, set
if (!empty($keys[$key_name])) {
$return_array = array(
'name' => $key_name,
'key' => $keys[$key_name],
);
}
else {
// If not found, and default was specified
if (!empty($keys[ENCRYPT_DEFAULT_KEY_FILE]) && $key_name == ENCRYPT_DEFAULT_KEY) {
$return_array = array(
'name' => ENCRYPT_DEFAULT_KEY_FILE,
'key' => $keys[ENCRYPT_DEFAULT_KEY_FILE],
);
}
elseif (!empty($keys[ENCRYPT_DEFAULT_KEY_DB]) && $key_name == ENCRYPT_DEFAULT_KEY) {
$return_array = array(
'name' => ENCRYPT_DEFAULT_KEY_DB,
'key' => $keys[ENCRYPT_DEFAULT_KEY_DB],
);
}
}
// Check if found
if (empty($return_array)) {
// Key not found
watchdog('encrypt', 'Key name could not be found: %key_name', array(
'%key_name',
$key_name,
), WATCHDOG_ERROR);
return FALSE;
}
else {
return $return_array;
}
}