You are here

strongarm.module in Strongarm 6

Same filename and directory in other branches
  1. 6.2 strongarm.module
  2. 7.2 strongarm.module

File

strongarm.module
View source
<?php

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);
    }
  }
}

/**
 * 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);
    }
  }
  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;
      }
    }
  }
}

/**
 * Log overridden values.
 */
function strongarm_overridden_vars($set = '') {
  static $overridden = array();
  if (!empty($set)) {
    $overridden[$set] = $set;
  }
  return $overridden;
}

/**
 * Implementation of hook_menu().
 */
function strongarm_menu() {
  $items = array();
  $items['admin/settings/strongarm'] = array(
    'title' => 'Strongarm',
    'description' => 'Manage Drupal variable settings that have been strongarmed.',
    'page callback' => 'strongarm_admin_page',
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'strongarm.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implementation of hook_form_alter().
 */
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_messages', 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();
  }
}

/**
 * Implementation of hook_theme().
 */
function strongarm_theme() {
  return array(
    'strongarm_admin_form' => array(),
  );
}

/**
 * Implementation of hook_flush_caches().
 */
function strongarm_flush_caches() {
  cache_clear_all('variables', 'cache');
  cache_clear_all('strongarm', 'cache');
}

Functions

Namesort descending Description
strongarm_boot 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().
strongarm_flush_caches Implementation of hook_flush_caches().
strongarm_form_alter Implementation of hook_form_alter().
strongarm_get_conf Retrieve variable configuration from the cache.
strongarm_init Implementation of hook_init().
strongarm_menu Implementation of hook_menu().
strongarm_overridden_vars Log overridden values.
strongarm_set_conf Sets the global configuration. When in Weakarm mode, will log any values that have been overridden and leave their conf values alone.
strongarm_theme Implementation of hook_theme().

Constants