View source
<?php
$plugin = array(
'label' => t('File'),
'description' => t('The File key provider allows a key to be stored in a file, preferably outside of the web root.'),
'storage method' => 'file',
'key value' => array(
'accepted' => FALSE,
'required' => FALSE,
),
'default configuration' => 'key_provider_file_default_configuration',
'build configuration form' => 'key_provider_file_build_configuration_form',
'validate configuration form' => 'key_provider_file_validate_configuration_form',
'get key value' => 'key_provider_file_get_key_value',
);
function key_provider_file_default_configuration() {
return array(
'file_location' => '',
'base64_encoded' => FALSE,
);
}
function key_provider_file_build_configuration_form($form, &$form_state) {
$config = $form_state['storage']['key_config'];
$plugin_config = $form_state['storage']['key_config']['key_provider_settings'] + key_provider_file_default_configuration();
$form['file_location'] = array(
'#type' => 'textfield',
'#title' => t('File location'),
'#description' => t('The location of the file in which the key will be stored. The path may be absolute (e.g., %abs), relative to the Drupal directory (e.g., %rel), or defined using a stream wrapper (e.g., %str).', array(
'%abs' => '/etc/keys/foobar.key',
'%rel' => '../keys/foobar.key',
'%str' => 'private://keys/foobar.key',
)),
'#default_value' => $plugin_config['file_location'],
'#required' => TRUE,
);
$key_type = key_get_plugin('key_type', $config['key_type']);
if ($key_type['group'] == 'encryption') {
$form['base64_encoded'] = array(
'#type' => 'checkbox',
'#title' => t('Base64-encoded'),
'#description' => t('Checking this will store the key with Base64 encoding.'),
'#default_value' => $plugin_config['base64_encoded'],
);
}
return $form;
}
function key_provider_file_validate_configuration_form($form, &$form_state) {
$key_provider_settings = $form_state['values'];
$file = $key_provider_settings['file_location'];
if (!is_file($file)) {
form_set_error('file_location', t('There is no file at the specified location.'));
return;
}
if (!is_readable($file)) {
form_set_error('file_location', t('The file at the specified location is not readable.'));
return;
}
}
function key_provider_file_get_key_value($config) {
if (empty($config['key_provider_settings']['file_location'])) {
return NULL;
}
$file = $config['key_provider_settings']['file_location'];
if (!is_file($file) || !is_readable($file)) {
return NULL;
}
$key_value = file_get_contents($file);
if (isset($config['key_provider_settings']['base64_encoded']) && $config['key_provider_settings']['base64_encoded']) {
$key_value = base64_decode($key_value);
}
return $key_value;
}