View source
<?php
define('KEY_STATUS_VALID', 1);
define('KEY_STATUS_NOT_VALID', 0);
function key_permission() {
return array(
'administer keys' => array(
'title' => t('Administer keys'),
'description' => 'Create, edit, and delete keys.',
),
);
}
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 callback' => NULL,
'key set callback' => NULL,
'dependency callback' => NULL,
'dependency errors' => NULL,
'settings form' => NULL,
'key form' => NULL,
'instructions' => NULL,
'status callback' => NULL,
),
);
$plugins['key_integration'] = array(
'cache' => TRUE,
'cache table' => 'cache',
'process' => '_key_integration_plugin_process',
'defaults' => array(
'title' => '',
'description' => '',
'type' => NULL,
'enabled' => FALSE,
'locked' => FALSE,
'settings' => array(),
'enable callback' => NULL,
'disable callback' => NULL,
),
);
$plugins['key_type'] = array(
'cache' => TRUE,
'cache table' => 'cache',
'defaults' => array(
'title' => '',
'description' => '',
),
);
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_element_expand($element) {
$element['#options'] = key_get_configs_as_options();
if (!empty($element['#filters'])) {
$element['#options'] = _key_configs_filter($element['#options'], $element['#filters']);
}
if ($element['#key_description']) {
$key_description = t('Choose an available key to use.');
if (module_exists('key_ui')) {
$key_description .= ' ' . t('If your key is not listed, <a href="@url">create a new key</a>.', array(
'@url' => '/' . KEY_MENU_PATH,
));
}
else {
$key_description .= ' ' . t('If your key is not listed, enable the Keys UI module and create a new key.');
}
$element['#description'] = $key_description . ' ' . $element['#description'];
}
return $element;
}
function _key_configs_filter($configs, $filters) {
$target_configs = array();
foreach (key_get_configs() as $config) {
$include = TRUE;
if (!empty($filters['type'])) {
$include &= in_array($config['type'], (array) $filters['type']);
}
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_types($all = TRUE, $reset = FALSE) {
if ($reset) {
_key_clear_plugin_cache('key_type');
}
ctools_include('plugins');
$types = ctools_get_plugins('key', 'key_type');
return $all ? $types : array_filter($types, '_key_plugin_is_valid');
}
function key_get_types_as_options($all = TRUE, $reset = FALSE) {
$providers = key_get_types($all, $reset);
$options = array();
foreach ($providers as $name => $provider) {
$options[$name] = $provider['title'];
}
return $options;
}
function key_get_type($type, $reset = FALSE) {
ctools_include('plugins');
return ctools_get_plugins('key', 'key_type', $type);
}
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, $key = FALSE, $overwrite = TRUE, $messages = TRUE) {
$provider = key_get_provider($fields['provider']);
if (!$overwrite) {
$configs = key_get_configs();
$counter = 2;
$base_name = $fields['name'];
while (key_get_config($fields['name'])) {
$fields['name'] = "{$base_name}_{$counter}";
$counter++;
}
$counter = 2;
$base_label = $fields['label'];
$config_labels = array();
foreach ($configs as $index => $config) {
$config_labels[] = $config['label'];
}
while (in_array($fields['label'], $config_labels)) {
$fields['label'] = "{$base_label} {$counter}";
$counter++;
}
if ($fields['provider'] == 'variable' && isset($key)) {
$counter = 2;
$base_variable_name = $fields['provider_settings']['variable_name'];
$config_variable_names = array();
foreach ($configs as $index => $config) {
if ($config['provider'] == 'variable') {
$config_variable_names[] = $config['provider_settings']['variable_name'];
}
}
while (in_array($fields['provider_settings']['variable_name'], $config_variable_names)) {
$fields['provider_settings']['variable_name'] = "{$base_variable_name}_{$counter}";
$counter++;
}
}
foreach ($configs as $index => $config) {
while (key_get_config($fields['name'])) {
$fields['name'] = "{$base_name}_{$counter}";
$counter++;
}
}
}
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;
}
if (isset($key) && ($key_set_callback = ctools_plugin_get_function($provider, 'key set callback'))) {
call_user_func($key_set_callback, $key_config['provider_settings'], $key);
}
return $key_config;
}
function key_get_integrations($status = NULL, $type = NULL, $reset = FALSE) {
$integrations =& drupal_static(__FUNCTION__);
if ($reset) {
_key_clear_plugin_cache('key_integration');
}
if (!isset($integrations) || $reset) {
ctools_include('plugins');
$integrations = ctools_get_plugins('key', 'key_integration');
}
if (!$status && !$type) {
return $integrations;
}
$filtered_integrations = $integrations;
foreach ($integrations as $name => $integration) {
if ($status == 'enabled' && !$integration['enabled']) {
unset($filtered_integrations[$name]);
continue;
}
if ($status == 'disabled' && $integration['enabled']) {
unset($filtered_integrations[$name]);
continue;
}
if (isset($type) && $integration['type'] != $type) {
unset($filtered_integrations[$name]);
continue;
}
}
return $filtered_integrations;
}
function key_get_integrations_as_options($status = NULL, $type = NULL, $reset = FALSE) {
$integrations = key_get_integrations($status, $type, $reset);
$options = array();
foreach ($integrations as $name => $integration) {
$options[$name] = $integration['title'];
}
return $options;
}
function key_get_integration($name, $reset = FALSE) {
$integrations = key_get_integrations(NULL, NULL, $reset);
if (array_key_exists($name, $integrations)) {
$integration = $integrations[$name];
}
else {
$integration = NULL;
}
return $integration;
}
function key_integration_is_enabled($name, $reset = FALSE) {
$integration = key_get_integration($name, $reset);
return $integration['enabled'];
}
function key_get_integration_settings($name = NULL, $reset = FALSE) {
$rows =& drupal_static(__FUNCTION__);
if (!isset($rows) || $reset) {
$rows = db_query("SELECT * FROM {key_integration} ORDER BY name ASC")
->fetchAllAssoc('name', PDO::FETCH_ASSOC);
foreach ($rows as $index => $row) {
if (!empty($row['settings'])) {
$settings = unserialize($row['settings']);
$rows[$index]['settings'] = $settings;
}
}
}
if ($name) {
return isset($rows[$name]) ? $rows[$name] : NULL;
}
else {
return $rows;
}
}
function key_save_integration_settings($fields) {
$integration = key_get_integration($fields['name']);
if ($integration['locked']) {
$num_deleted = db_delete('key_integration')
->condition('name', $fields['name'])
->execute();
return;
}
foreach ($fields as $index => $field) {
if (is_array($field)) {
$fields[$index] = serialize($field);
}
}
$merge_status = db_merge('key_integration')
->key(array(
'name' => $fields['name'],
))
->fields($fields)
->execute();
}
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);
$provider = key_get_provider($config['provider']);
$key_function = ctools_plugin_get_function($provider, 'key get callback');
$provider_settings = isset($config['provider_settings']) ? $config['provider_settings'] : array();
$key = call_user_func($key_function, $provider_settings);
$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_integration_plugin_process(&$plugin, $info) {
$integrations = db_query("SELECT * FROM {key_integration} ORDER BY name ASC")
->fetchAllAssoc('name', PDO::FETCH_ASSOC);
$name = $plugin['name'];
if (array_key_exists($name, $integrations)) {
if (isset($integrations[$name]['enabled'])) {
$plugin['enabled'] = $integrations[$name]['enabled'];
}
}
}
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,
),
'key_integration' => array(
'name' => 'Key Integration',
'file' => drupal_get_path('module', 'key') . '/includes/key_integration.features.inc',
'default_hook' => 'key_default_integrations',
'feature_source' => TRUE,
),
);
}