Commit 39f64eb7 authored by young hahn's avatar young hahn
Browse files

Initial commit of DRUPAL-6--2 branch of strongarm.

parent 0a2892ef
<?php
// $Id$
/**
* Main admin page menu callback.
*/
function strongarm_admin_page() {
$output = '';
$output .= drupal_get_form('strongarm_admin_settings_form');
if (variable_get('strongarm_mode', STRONGARM_MODE_STRONG) == STRONGARM_MODE_WEAK) {
$output .= drupal_get_form('strongarm_admin_form');
}
return $output;
}
/**
* Get a map of variable to culprit (implementers of hook_strongarm()).
*/
......@@ -27,73 +15,39 @@ function strongarm_get_culprits() {
return $culprits;
}
/**
* Settings form.
*/
function strongarm_admin_settings_form() {
$form = array();
$form['strongarm_mode'] = array(
'#title' => t('Strongarm mode'),
'#description' => t('Choose the strength of strongarm\'s restrictive behavior.'),
'#type' => 'radios',
'#options' => array(
STRONGARM_MODE_STRONG => t('Force variables values'),
STRONGARM_MODE_WEAK => t('Allow variable overrides'),
),
'#default_value' => variable_get('strongarm_mode', STRONGARM_MODE_STRONG),
);
$form['strongarm_show_messages'] = array(
'#title' => t('Show messages'),
'#description' => t('Show helper messages to site administrators when variables have been strongarmed or overridden.'),
'#type' => 'checkbox',
'#default_value' => variable_get('strongarm_show_messages', TRUE),
);
return system_settings_form($form);
}
/**
* Variable management strongarm form.
*/
function strongarm_admin_form() {
$form = array();
$var_conf = strongarm_get_conf();
$culprits = strongarm_get_culprits();
$overridden = array();
$result = db_query("SELECT * FROM {variable} ORDER BY name ASC");
while ($row = db_fetch_object($result)) {
$value = unserialize($row->value);
if (isset($var_conf[$row->name]) && $var_conf[$row->name] != $value) {
$overridden[$row->name] = $value;
}
}
if (!empty($overridden)) {
$form = array('#theme' => 'strongarm_admin_form',);
foreach ($overridden as $name => $value) {
$form['revert']['#tree'] = TRUE;
$form['revert'][$name] = array('#type' => 'checkbox');
function strongarm_admin_form($form_state) {
ctools_include('export');
$vars = ctools_export_load_object('variable');
ksort($vars);
$form = array('#theme' => 'strongarm_admin_form',);
foreach ($vars as $name => $variable) {
if ($variable->export_type & EXPORT_IN_CODE) {
$default = ctools_get_default_object('variable', $name);
if (($variable->export_type & EXPORT_IN_DATABASE) && ($variable->value != $default->value)) {
$form['revert']['#tree'] = TRUE;
$form['revert'][$name] = array('#type' => 'checkbox');
}
$form['name'][$name] = array(
'#type' => 'markup',
'#value' => $name,
);
$form['current_value'][$name] = array(
$form['storage'][$name] = array(
'#type' => 'markup',
'#value' => check_plain(_strongarm_readable($overridden[$name])),
'#value' => ($variable->value == $default->value) ? t('Default') : t('Overridden'),
);
$form['default_value'][$name] = array(
$form['value'][$name] = array(
'#type' => 'markup',
'#value' => check_plain(_strongarm_readable($var_conf[$name])),
);
$form['culprit'][$name] = array(
'#type' => 'markup',
'#value' => $culprits[$name],
'#value' => check_plain(_strongarm_readable($variable->value)),
);
}
}
if (!empty($form['revert'])) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Revert'),
'#value' => t('Reset to defaults'),
'#submit' => array('strongarm_admin_revert_submit'),
);
}
......@@ -110,66 +64,16 @@ function strongarm_admin_revert_submit(&$form, &$form_state) {
variable_del($name);
}
}
// Re-set strongarm and variable caches
strongarm_flush_caches();
$var_conf = strongarm_get_conf();
strongarm_set_conf($var_conf);
}
}
/**
* Recursive function that hunts down form elements that are likely to
* be related to strongarm'd variables and locks them down.
*/
function _strongarm_lockdown(&$form, $pattern = '', $position = STRONGARM_PATTERN_PREFIX) {
$var_conf = strongarm_get_conf();
foreach (element_children($form) as $elem) {
$children = element_children($form[$elem]);
$key = $elem;
$match = FALSE;
if (isset($var_conf[$key])) {
$match = TRUE;
}
else if (!empty($pattern)) {
$key = ($position == STRONGARM_PATTERN_PREFIX) ? "{$pattern}_{$key}" : "{$key}_{$pattern}";
if (isset($var_conf[$key])) {
$match = TRUE;
}
}
if ($match) {
// If the default form value matches the strongarm value,
// it is highly likely we have a match. Disable the field.
if (isset($form[$elem]['#default_value']) && $var_conf[$key] == $form[$elem]['#default_value']) {
$form[$elem]['#disabled'] = TRUE;
$form[$elem]['#value'] = $form[$elem]['#default_value'];
}
if (!isset($form[$elem]['#attributes']['class'])) {
$form[$elem]['#attributes']['class'] = "strongarm";
}
else {
$form[$elem]['#attributes']['class'] .= " strongarm";
}
$altered = TRUE;
}
// If this is a tree'd element, recurse
if (!empty($form[$elem]['#tree']) || !empty($children)) {
$altered = _strongarm_lockdown($form[$elem], $pattern, $position) || $altered;
}
}
return $altered;
}
/**
* Display variables in a nicer way.
*/
function _strongarm_readable($var) {
if (is_string($var) || is_bool($var) || is_numeric($var)) {
return $var;
return truncate_utf8($var, 30, TRUE, TRUE);
}
else if (is_array($var)) {
$test = $detected = array();
......@@ -192,7 +96,7 @@ function _strongarm_readable($var) {
// List of things
if (!empty($var) && $detected['keys']['numeric'] && $detected['keys']['sequential']) {
return implode(', ', $var);
return truncate_utf8(implode(', ', $var), 30, TRUE, TRUE);
}
return '-';
}
......@@ -202,32 +106,21 @@ function _strongarm_readable($var) {
* Theme function for the strongarm admin form.
*/
function theme_strongarm_admin_form($form) {
$output = '';
$overridden = element_children($form['revert']);
if (!empty($overridden)) {
drupal_add_js('misc/tableselect.js');
$rows = $headers = array();
$output .= "<h3>". t('Manage overridden variables') ."</h3>";
// Add checkboxes to the header and the rows.
$headers[] = theme('table_select_header_cell');
$headers[] = t('Variable');
$headers[] = t('Current value');
$headers[] = t('Module value');
$headers[] = t('Module');
foreach ($overridden as $name) {
$row = array();
$row[] = drupal_render($form['revert'][$name]);
$row[] = drupal_render($form['name'][$name]);
$row[] = drupal_render($form['current_value'][$name]);
$row[] = drupal_render($form['default_value'][$name]);
$row[] = drupal_render($form['culprit'][$name]);
$rows[] = $row;
}
$output .= theme('table', $headers, $rows);
$output .= drupal_render($form);
drupal_add_js('misc/tableselect.js');
$rows = $headers = array();
$headers[] = theme('table_select_header_cell');
$headers[] = t('Variable');
$headers[] = t('Storage');
$headers[] = t('Value');
foreach (element_children($form['name']) as $name) {
$row = array();
$row[] = isset($form['revert'][$name]) ? drupal_render($form['revert'][$name]) : '';
$row[] = drupal_render($form['name'][$name]);
$row[] = drupal_render($form['storage'][$name]);
$row[] = drupal_render($form['value'][$name]);
$rows[] = $row;
}
$output = theme('table', $headers, $rows);
$output .= drupal_render($form);
return $output;
}
......@@ -2,3 +2,4 @@
name = "Strongarm"
description = "Enforces variable values defined by modules that need settings set to operate properly."
core = "6.x"
dependencies[] = "ctools"
<?php
// $Id$
define('STRONGARM_PATTERN_PREFIX', 0);
define('STRONGARM_PATTERN_SUFFIX', 1);
define('STRONGARM_MODE_STRONG', 0);
define('STRONGARM_MODE_WEAK', 1);
/**
* Implementation of hook_boot().
* This is a very aggressive way of ensuring that these variables are
* set. Necessary for variables that are checked by modules on hook_init().
*/
function strongarm_boot() {
$var_conf = strongarm_get_conf(FALSE);
strongarm_set_conf($var_conf);
}
/**
* Implementation of hook_init().
*/
function strongarm_init() {
$var_conf = strongarm_get_conf();
strongarm_set_conf($var_conf);
// Display a status message if any variables are overridden.
if ($_GET['q'] != 'admin/settings/strongarm' && variable_get('strongarm_mode', STRONGARM_MODE_STRONG) == STRONGARM_MODE_WEAK) {
$overridden = strongarm_overridden_vars();
if (arg(0) == 'admin' && !empty($overridden) && user_access('administer site configuration') && variable_get('strongarm_show_messages', TRUE)) {
drupal_set_message(t('The following variables have overridden values: !vars. Manage these variables on the !strongarm_page.', array('!vars' => implode(', ', $overridden), '!strongarm_page' => l(t('Strongarm admin page'), 'admin/settings/strongarm'))), 'warning', FALSE);
}
}
strongarm_set_conf();
}
/**
* Retrieve variable configuration from the cache.
*/
function strongarm_get_conf($bootstrapped = TRUE, $reset = FALSE) {
static $var_conf;
if (!isset($var_conf) || $reset) {
if (!$reset) {
$cache = cache_get('strongarm', 'cache');
$var_conf = $cache ? $cache->data : NULL;
}
if (!isset($var_conf) && $bootstrapped) {
// We don't use module_invoke_all() here since
// array_merge_recursive() can alter the desired structure of
// some variables.
foreach (module_implements('strongarm') as $module) {
$module_conf = module_invoke($module, 'strongarm');
foreach ($module_conf as $name => $value) {
if (isset($var_conf[$name])) {
if (is_array($value)) {
$var_conf[$name] = array_merge($var_conf[$name], $value);
}
else {
// Blow the earlier one away
$var_conf[$name] = $value;
}
}
else {
$var_conf[$name] = $value;
}
}
}
cache_set('strongarm', $var_conf);
}
function strongarm_set_conf($reset = FALSE) {
$varcache = cache_get('variables', 'cache');
$cache = cache_get('strongarm', 'cache');
// The > comparison here is cautious but ensures that the strongarm cache
// actually was populated after the variable cache. It is possible with
// >= for the var cache to be populated again during the same stale second.
if (!$reset && ($cache && $varcache && $cache->created > $varcache->created)) {
$var_conf = $cache->data;
}
return $var_conf;
}
/**
* Sets the global configuration. When in Weakarm mode, will log any
* values that have been overridden and leave their conf values alone.
*/
function strongarm_set_conf($var_conf = array()) {
if (!empty($var_conf)) {
global $conf;
foreach ($var_conf as $k => $v) {
// The variable is overridden -- let it be, but log the override if we are in Weakarm mode.
if ((variable_get('strongarm_mode', STRONGARM_MODE_STRONG) == STRONGARM_MODE_WEAK) && isset($conf[$k]) && $conf[$k] != $v) {
strongarm_overridden_vars($k);
}
else {
$conf[$k] = $v;
}
else {
ctools_include('export');
$vars = ctools_export_load_object('variable');
foreach ($vars as $var) {
$var_conf[$var->name] = $var->value;
}
cache_set('strongarm', $var_conf);
}
}
/**
* Log overridden values.
*/
function strongarm_overridden_vars($set = '') {
static $overridden = array();
if (!empty($set)) {
$overridden[$set] = $set;
}
return $overridden;
global $conf;
$conf = $var_conf;
}
/**
......@@ -108,7 +40,8 @@ function strongarm_menu() {
$items['admin/settings/strongarm'] = array(
'title' => 'Strongarm',
'description' => 'Manage Drupal variable settings that have been strongarmed.',
'page callback' => 'strongarm_admin_page',
'page callback' => 'drupal_get_form',
'page arguments' => array('strongarm_admin_form'),
'access callback' => 'user_access',
'access arguments' => array('administer site configuration'),
'file' => 'strongarm.admin.inc',
......@@ -118,38 +51,11 @@ function strongarm_menu() {
}
/**
* Implementation of hook_form_alter().
* Implementation of hook_form_alter() for system_module form.
* Clear strongarm & variable caches on modules page.
*/
function strongarm_form_alter(&$form, &$form_state, $form_id) {
$pattern = '';
$position = STRONGARM_PATTERN_PREFIX;
if (!empty($form['#submit']) && is_array($form['#submit']) && in_array('system_settings_form_submit', $form['#submit'])) {
$lockdown = TRUE;
}
else if ($form_id == 'node_type_form') {
$lockdown = TRUE;
$pattern = $form['#node_type']->type;
$position = STRONGARM_PATTERN_SUFFIX;
}
else if (arg(0) === 'admin' && $form_id != 'strongarm_admin_form') {
$lockdown = TRUE;
}
if (!empty($lockdown) && variable_get('strongarm_mode', STRONGARM_MODE_STRONG) == STRONGARM_MODE_STRONG) {
module_load_include('inc', 'strongarm', 'strongarm.admin');
drupal_add_css(drupal_get_path('module', 'strongarm') .'/strongarm.css');
if (_strongarm_lockdown($form, $pattern, $position) && variable_get('strongarm_show_message', TRUE)) {
drupal_set_message(t('The highlighted fields may need to retain their current values for your site to operate properly. Settings may be disabled or reverted automatically if they are not compatible with one or more of your site\'s modules.'), 'warning', FALSE);
if (user_access('administer site configuration')) {
drupal_set_message(t('You can change locked-in settings by switching to "Allow variable overrides" on the !admin_page.', array('!admin_page' => l(t('Strongarm admin page'), 'admin/settings/strongarm'))), 'warning', FALSE);
}
}
}
// Clear strongarm & variable caches on modules page.
if ($form_id == 'system_module') {
strongarm_flush_caches();
}
function strongarm_form_system_module_alter(&$form, &$form_state) {
strongarm_flush_caches();
}
/**
......@@ -157,7 +63,11 @@ function strongarm_form_alter(&$form, &$form_state, $form_id) {
*/
function strongarm_theme() {
return array(
'strongarm_admin_form' => array(),
'strongarm_admin_form' => array(
'arguments' => array(),
'file' => 'strongarm.admin.inc',
'path' => drupal_get_path('module', 'strongarm'),
),
);
}
......@@ -168,3 +78,32 @@ function strongarm_flush_caches() {
cache_clear_all('variables', 'cache');
cache_clear_all('strongarm', 'cache');
}
/**
* Implementation of hook_schema_alter().
* Makes the variables table usable by ctools' export.inc.
*/
function strongarm_schema_alter(&$schema) {
$schema['variable']['export'] = array(
'key' => 'name',
'identifier' => 'strongarm',
'default hook' => 'strongarm',
'api' => array(
'owner' => 'system',
'api' => 'strongarm',
'minimum_version' => 1,
'current_version' => 1,
),
);
$schema['variable']['fields']['value']['serialize'] = TRUE;
}
/**
* Implementation of hook_features_revert().
*/
if (!function_exists('variable_features_revert')) {
function variable_features_revert($module) {
ctools_component_features_revert('variable', $module);
cache_clear_all('variables', 'cache');
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment