View source
<?php
use Drush\Log\LogLevel;
require_once drupal_get_path('module', 'key') . '/includes/key.admin.inc';
function _drush_key_save($id, $key_value = NULL) {
$values = array();
$values['id'] = $id;
$existing_key = key_get_key($id, TRUE);
$overwrite = drush_get_option('overwrite');
if ($existing_key && !$overwrite) {
return drush_set_error('DRUSH_KEY_EXISTS', dt('Key !id exists; specify --overwrite to overwrite.', array(
'!id' => $values['id'],
)));
}
if ($existing_key) {
drush_log(dt('Be extremely careful when overwriting a key! It may result in losing access to a service or making encrypted data unreadable.'), LogLevel::WARNING);
}
drush_print(dt('The following key will be saved: !id', array(
'!id' => $values['id'],
)));
if (!drush_confirm(dt('Do you really want to continue?'))) {
return drush_user_abort();
}
$key_config_fields = array(
'label' => $id,
'description' => '',
'key-type' => 'authentication',
'key-type-settings' => array(),
'key-provider' => 'config',
'key-provider-settings' => array(),
'key-input' => 'none',
'key-input-settings' => array(),
);
foreach (array_keys(key_drush_save_options()) as $option) {
$value = drush_get_option($option, NULL);
if (isset($value) && in_array($option, array_keys($key_config_fields))) {
if (in_array($option, array(
'key-type-settings',
'key-provider-settings',
'key-input-settings',
))) {
$values[str_replace('-', '_', $option)] = drupal_json_decode($value);
}
else {
$values[str_replace('-', '_', $option)] = $value;
}
}
}
foreach ($key_config_fields as $key_config_field => $key_config_default) {
$key_config_field = str_replace('-', '_', $key_config_field);
if (!isset($values[$key_config_field])) {
$values[str_replace('-', '_', $key_config_field)] = $key_config_default;
}
}
if (!$existing_key) {
$existing_key = array();
}
if (isset($key_value)) {
$key_provider = key_get_plugin('key_provider', $values['key_provider']);
if ($key_provider['key value']['accepted'] && ($set_key_value_function = ctools_plugin_get_function($key_provider, 'set key value'))) {
$form_state = array(
'values' => $values,
);
$plugin_form_state = _key_create_plugin_form_state('key_provider', $form_state);
$set_key_value_function($values, $plugin_form_state, $key_value);
$form_state['values']['key_provider_settings'] = $plugin_form_state['values'];
$values = $form_state['values'];
}
}
key_save_key($values, $existing_key, FALSE);
$key_check = key_get_key($values['id'], TRUE);
if (!$key_check) {
return drush_set_error('DRUSH_KEY_NOT_SAVED', dt('Key !id was not saved.', array(
'!id' => $values['id'],
)));
}
drush_log(dt('Key !id was saved successfully.', array(
'!id' => $values['id'],
)), LogLevel::OK);
}