Commit 12bb0c3e authored by Petr Skoda's avatar Petr Skoda
Browse files

MDL-22515 plugin settings can be forced now too + minor CFG init refactoring + get_config() cleanup

parent c8166478
......@@ -399,6 +399,21 @@ $CFG->admin = 'admin';
// Since 2.0 sql queries are not shown during upgrade by default.
// Please note that this setting may produce very long upgrade page on large sites.
// $CFG->upgradeshowsql = true; // NOT FOR PRODUCTION SERVERS!
//
//=========================================================================
// 9. FORCED SETTINGS
//=========================================================================
// It is possible to specify normal admin settings here, the point is that
// they can not be changed through the standard admin settings pages any more.
//
// Core settings are specified directly via assignment to $CFG variable.
// Example:
// $CFG->somecoresetting = 'value';
//
// Plugin settings have to be put into a special array.
// Example:
// $CFG->forced_plugin_settings = array('pluginname' => array('settingname' => 'value', 'secondsetting' => 'othervalue'),
// 'otherplugin' => array('mysetting' => 'myvalue', 'thesetting' => 'thevalue'));
//=========================================================================
......
......@@ -5718,10 +5718,15 @@ function format_admin_setting($setting, $title='', $form='', $description='', $l
$labelfor = '';
}
if (empty($setting->plugin) and array_key_exists($setting->name, $CFG->config_php_settings)) {
$override = '<div class="form-overridden">'.get_string('configoverride', 'admin').'</div>';
$override = '';
if (empty($setting->plugin)) {
if (array_key_exists($setting->name, $CFG->config_php_settings)) {
$override = '<div class="form-overridden">'.get_string('configoverride', 'admin').'</div>';
}
} else {
$override = '';
if (array_key_exists($setting->plugin, $CFG->forced_plugin_settings) and array_key_exists($setting->name, $CFG->forced_plugin_settings[$setting->plugin])) {
$override = '<div class="form-overridden">'.get_string('configoverride', 'admin').'</div>';
}
}
if ($warning !== '') {
......
......@@ -935,58 +935,71 @@ function set_config($name, $value, $plugin=NULL) {
* Get configuration values from the global config table
* or the config_plugins table.
*
* If called with no parameters it will do the right thing
* generating $CFG safely from the database without overwriting
* existing values.
*
* If called with one parameter, it will load all the config
* variables for one pugin, and return them as an object.
* variables for one plugin, and return them as an object.
*
* If called with 2 parameters it will return a $string single
* value or false of the value is not found.
* If called with 2 parameters it will return a string single
* value or false if the value is not found.
*
* @global object
* @param string $plugin default NULL
* @param string $plugin full component name
* @param string $name default NULL
* @return mixed hash-like object or single value, return false no config found
*/
function get_config($plugin=NULL, $name=NULL) {
function get_config($plugin, $name = NULL) {
global $CFG, $DB;
// normalise component name
if ($plugin === 'moodle' or $plugin === 'core') {
$plugin = NULL;
}
if (!empty($name)) { // the user is asking for a specific value
if (!empty($plugin)) {
return $DB->get_field('config_plugins', 'value', array('plugin'=>$plugin, 'name'=>$name));
if (isset($CFG->forced_plugin_settings[$plugin]) and array_key_exists($name, $CFG->forced_plugin_settings[$plugin])) {
// setting forced in config file
return $CFG->forced_plugin_settings[$plugin][$name];
} else {
return $DB->get_field('config_plugins', 'value', array('plugin'=>$plugin, 'name'=>$name));
}
} else {
return $DB->get_field('config', 'value', array('name'=>$name));
if (array_key_exists($name, $CFG->config_php_settings)) {
// setting force in config file
return $CFG->config_php_settings[$name];
} else {
return $DB->get_field('config', 'value', array('name'=>$name));
}
}
}
// the user is after a recordset
if (!empty($plugin)) {
if ($plugin) {
$localcfg = $DB->get_records_menu('config_plugins', array('plugin'=>$plugin), '', 'name,value');
if (!empty($localcfg)) {
return (object)$localcfg;
} else {
return false;
}
} else {
// this was originally in setup.php
if ($configs = $DB->get_records('config')) {
$localcfg = (array)$CFG;
foreach ($configs as $config) {
if (!isset($localcfg[$config->name])) {
$localcfg[$config->name] = $config->value;
if (isset($CFG->forced_plugin_settings[$plugin])) {
foreach($CFG->forced_plugin_settings[$plugin] as $n=>$v) {
if (is_null($v) or is_array($v) or is_object($v)) {
// we do not want any extra mess here, just real settings that could be saved in db
unset($localcfg[$n]);
} else {
//convert to string as if it went through the DB
$localcfg[$n] = (string)$v;
}
// do not complain anymore if config.php overrides settings from db
}
$localcfg = (object)$localcfg;
return $localcfg;
} else {
// preserve $CFG if DB returns nothing or error
return $CFG;
}
return (object)$localcfg;
} else {
// this part is not really used any more, but anyway...
$localcfg = $DB->get_records_menu('config', array(), '', 'name,value');
foreach($CFG->config_php_settings as $n=>$v) {
if (is_null($v) or is_array($v) or is_object($v)) {
// we do not want any extra mess here, just real settings that could be saved in db
unset($localcfg[$n]);
} else {
//convert to string as if it went through the DB
$localcfg[$n] = (string)$v;
}
}
return (object)$localcfg;
}
}
......
......@@ -224,7 +224,11 @@ if (!isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['argv'][0])) {
// Store settings from config.php in array in $CFG - we can use it later to detect problems and overrides
$CFG->config_php_settings = (array)$CFG;
// Forced plugin settings override values from config_plugins table
unset($CFG->config_php_settings['forced_plugin_settings']);
if (!isset($CFG->forced_plugin_settings)) {
$CFG->forced_plugin_settings = array();
}
// Set httpswwwroot default value (this variable will replace $CFG->wwwroot
// inside some URLs used in HTTPSPAGEREQUIRED pages.
$CFG->httpswwwroot = $CFG->wwwroot;
......@@ -315,11 +319,7 @@ if (isset($CFG->debug)) {
}
// Load up any configuration from the config table
try {
$CFG = get_config();
} catch (dml_read_exception $e) {
// most probably empty db, going to install soon
}
initialise_cfg();
// Verify upgrade is not running unless we are in a script that needs to execute in any case
if (!defined('NO_UPGRADE_CHECK') and isset($CFG->upgraderunning)) {
......@@ -392,7 +392,7 @@ unset($originalconfigdebug);
unset($originaldatabasedebug);
error_reporting($CFG->debug);
// find out if PHP cofigured to display warnings,
// find out if PHP configured to display warnings,
// this is a security problem because some moodle scripts may
// disclose sensitive information
if (ini_get_bool('display_errors')) {
......
......@@ -454,6 +454,29 @@ function setup_validate_php_configuration() {
}
}
/**
* Initialise global $CFG variable
* @return void
*/
function initialise_cfg() {
global $CFG, $DB;
try {
if ($DB) {
$localcfg = $DB->get_records_menu('config', array(), '', 'name,value');
foreach ($localcfg as $name=>$value) {
if (property_exists($CFG, $name)) {
// config.php settings always take precedence
continue;
}
$CFG->{$name} = $value;
}
}
} catch (dml_read_exception $e) {
// most probably empty db, going to install soon
}
}
/**
* Initialises $FULLME and friends. Private function. Should only be called from
* setup.php.
......
Supports Markdown
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