View source
<?php
define('KEY_MENU_PATH', 'admin/config/system/keys');
function key_permission() {
return array(
'administer keys' => array(
'title' => t('Administer keys'),
'description' => 'Create, edit, and delete keys.',
),
);
}
function key_menu() {
$items = array();
$items[KEY_MENU_PATH] = array(
'title' => 'Keys',
'description' => 'Manage keys.',
'page callback' => 'key_configs_list',
'access arguments' => array(
'administer keys',
),
'file' => 'includes/key.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items[KEY_MENU_PATH . '/list'] = array(
'title' => 'List keys',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[KEY_MENU_PATH . '/add'] = array(
'title' => 'Add key',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'key_config_form',
),
'access arguments' => array(
'administer keys',
),
'file' => 'includes/key.admin.inc',
'type' => MENU_LOCAL_ACTION,
);
$items[KEY_MENU_PATH . '/manage/%key_config'] = array(
'title' => 'Edit key',
'title callback' => 'key_config_edit_title',
'title arguments' => array(
5,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'key_config_form',
5,
),
'access arguments' => array(
'administer keys',
),
'file' => 'includes/key.admin.inc',
);
$items[KEY_MENU_PATH . '/manage/%key_config/delete'] = array(
'title' => 'Delete key',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'key_config_delete_confirm',
5,
),
'access arguments' => array(
'administer keys',
),
'file' => 'includes/key.admin.inc',
);
return $items;
}
function key_theme() {
return array(
'key_configs_list_description' => array(
'variables' => array(
'label' => NULL,
'name' => NULL,
'description' => NULL,
),
'file' => 'includes/key.admin.inc',
),
);
}
function key_ctools_plugin_directory($module, $plugin) {
if ($module == 'key' && !empty($plugin)) {
return "plugins/{$plugin}";
}
}
function key_ctools_plugin_type() {
$plugins['key_provider'] = array(
'cache' => TRUE,
'cache table' => 'cache',
'process' => '_key_provider_plugin_process',
'defaults' => array(
'title' => '',
'description' => '',
'key get value' => NULL,
'config form validate' => NULL,
'config form submit' => NULL,
'delete form submit' => NULL,
'dependency callback' => NULL,
'dependency errors' => NULL,
'provider settings form' => NULL,
'key value form' => NULL,
'key value obscure' => NULL,
'allow base64 encoding' => TRUE,
),
);
return $plugins;
}
function key_element_info() {
$type['key'] = array(
'#input' => TRUE,
'#size' => 0,
'#multiple' => FALSE,
'#process' => array(
'_key_element_expand',
'form_process_select',
'ajax_process_form',
),
'#theme' => 'select',
'#theme_wrappers' => array(
'form_element',
),
'#options' => array(),
'#filters' => array(),
'#key_description' => TRUE,
);
return $type;
}
function key_config_edit_title($config) {
return t('Edit @name key', array(
'@name' => $config['label'],
));
}
function key_config_load($name) {
return key_get_config(strtr($name, array(
'-' => '_',
)));
}
function _key_element_expand($element) {
$element['#empty_option'] = t('Select a key');
$element['#options'] = key_get_configs_as_options();
if (!empty($element['#filters'])) {
$element['#options'] = _key_configs_filter($element['#options'], $element['#filters']);
}
if ($element['#key_description']) {
$original_description = isset($element['#description']) ? $element['#description'] : '';
$key_description = t('Choose an available key to use.');
$key_description .= ' ' . t('If your key is not listed, <a href="@url">create a new key</a>.', array(
'@url' => '/' . KEY_MENU_PATH,
));
$element['#description'] = $key_description . ' ' . $original_description;
}
return $element;
}
function _key_configs_filter($configs, $filters) {
$target_configs = array();
foreach (key_get_configs() as $config) {
$include = TRUE;
if (!empty($filters['provider'])) {
$include &= in_array($config['provider'], (array) $filters['provider']);
}
if ($include) {
$target_configs[$config['name']] = TRUE;
}
}
$configs = array_intersect_key($configs, $target_configs);
return $configs;
}
function key_get_providers($all = TRUE, $reset = FALSE) {
if ($reset) {
_key_clear_plugin_cache('key_provider');
}
ctools_include('plugins');
$providers = ctools_get_plugins('key', 'key_provider');
return $all ? $providers : array_filter($providers, '_key_plugin_is_valid');
}
function key_get_providers_as_options($all = TRUE, $reset = FALSE) {
$providers = key_get_providers($all, $reset);
$options = array();
foreach ($providers as $name => $provider) {
$options[$name] = $provider['title'];
}
return $options;
}
function key_get_provider($provider, $reset = FALSE) {
ctools_include('plugins');
return ctools_get_plugins('key', 'key_provider', $provider);
}
function key_get_configs($reset = FALSE) {
$configs =& drupal_static(__FUNCTION__);
if (!isset($configs) || $reset) {
$configs = db_query("SELECT * FROM {key_config} ORDER BY label ASC")
->fetchAllAssoc('name', PDO::FETCH_ASSOC);
foreach ($configs as $name => $config) {
if (!empty($config['provider_settings'])) {
$provider_settings = unserialize($config['provider_settings']);
$configs[$name]['provider_settings'] = $provider_settings;
}
}
}
return $configs;
}
function key_get_configs_as_options($reset = FALSE) {
$options =& drupal_static(__FUNCTION__);
if (!isset($options) || $reset) {
$configs = key_get_configs($reset);
$options = array();
foreach ($configs as $name => $config) {
$options[$name] = $config['label'];
}
}
return $options;
}
function key_get_config($name, $reset = FALSE) {
$configs =& drupal_static(__FUNCTION__);
if (!isset($configs) || $reset) {
$configs = key_get_configs($reset);
}
if (array_key_exists($name, $configs)) {
$config = $configs[$name];
}
else {
$config = NULL;
}
return $config;
}
function key_save_config($fields, $messages = TRUE) {
$provider = key_get_provider($fields['provider']);
foreach ($fields as $index => $field) {
if (is_array($field)) {
$fields[$index] = serialize($field);
}
}
if (empty($fields['created'])) {
$fields['created'] = REQUEST_TIME;
}
if (empty($fields['changed'])) {
$fields['changed'] = REQUEST_TIME;
}
$merge_status = db_merge('key_config')
->key(array(
'name' => $fields['name'],
))
->fields($fields)
->execute();
if ($messages) {
$t_args = array(
'%label' => $fields['label'],
);
switch ($merge_status) {
case MergeQuery::STATUS_INSERT:
drupal_set_message(t('The key %label has been added.', $t_args));
watchdog('key', 'Added key %label.', $t_args, WATCHDOG_NOTICE, l(t('view'), KEY_MENU_PATH . '/list'));
break;
case MergeQuery::STATUS_UPDATE:
drupal_set_message(t('The key %label has been updated.', $t_args));
watchdog('key', 'Updated key %label.', $t_args, WATCHDOG_NOTICE, l(t('view'), KEY_MENU_PATH . '/list'));
break;
}
}
$key_config = key_get_config($fields['name'], TRUE);
if (empty($key_config)) {
return NULL;
}
return $key_config;
}
function key_get_key($config_name) {
$keys =& drupal_static(__FUNCTION__);
if (isset($keys[$config_name])) {
return $keys[$config_name];
}
$config = key_get_config($config_name);
if (!isset($config)) {
return NULL;
}
$provider = key_get_provider($config['provider']);
$key_function = ctools_plugin_get_function($provider, 'key get value');
$key = call_user_func($key_function, $config);
$keys[$config_name] = $key;
return $key;
}
function _key_clear_plugin_cache($type = NULL) {
if ($type) {
cache_clear_all("plugins:key:{$type}", 'cache');
}
else {
cache_clear_all('plugins:key:', 'cache', TRUE);
}
}
function _key_provider_plugin_process(&$plugin, $info) {
if ($dependency_function = ctools_plugin_get_function($plugin, 'dependency callback')) {
$plugin['dependency errors'] = call_user_func($dependency_function);
}
}
function _key_plugin_is_valid($plugin) {
if (empty($plugin['dependency errors'])) {
return TRUE;
}
else {
return FALSE;
}
}
function key_features_api() {
return array(
'key_config' => array(
'name' => 'Keys',
'file' => drupal_get_path('module', 'key') . '/includes/key_config.features.inc',
'default_hook' => 'key_default_configs',
'feature_source' => TRUE,
),
);
}