forked from emoncms/emoncms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_settings.php
125 lines (108 loc) · 4.48 KB
/
process_settings.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/*
All Emoncms code is released under the GNU Affero General Public License.
See COPYRIGHT.txt and LICENSE.txt.
---------------------------------------------------------------------
Emoncms - open source energy visualisation
Part of the OpenEnergyMonitor project:
http://openenergymonitor.org
*/
// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');
require_once('Lib/enum.php');
// Load settings.php
$settings_error = false;
if(file_exists(dirname(__FILE__)."/settings.php")) {
require_once('default-settings.php');
require_once('settings.php');
if (!isset($settings)) {
require_once('Lib/process_old_settings.php');
//$settings_error = true;
//$settings_error_title = "settings.php file error";
//$settings_error_message = "It looks like you are using an old version of settings.php try re-creating your settings.php file from default-settings.php";
} else {
$settings = array_replace_recursive($_settings,$settings);
}
} else if(file_exists(dirname(__FILE__)."/settings.ini")) {
$CONFIG_INI = parse_ini_file("default-settings.ini", true);
$CUSTOM_INI = parse_ini_file("settings.ini", true);
$settings = ini_merge($CONFIG_INI, $CUSTOM_INI);
// $settings = ini_check_envvars($settings);
} else {
$settings_error = true;
$settings_error_title = "missing settings file";
$settings_error_message = "Create a settings.ini file from a example.settings.ini template";
}
if ($settings_error) {
if (PHP_SAPI === 'cli') {
echo "$settings_error_title\n";
echo "$settings_error_message\n";
} else {
echo "<div style='width:600px; background-color:#eee; padding:20px; font-family:arial;'>";
echo "<h3>$settings_error_title</h3>";
echo "<p>$settings_error_message</p>";
echo "</div>";
}
die;
}
// ---------------------------------------------------------------------------------------
if (is_dir($settings["emoncms_dir"]."/modules")) {
$linked_modules_dir = $settings["emoncms_dir"]."/modules";
} else {
$linked_modules_dir = $settings["emoncms_dir"];
}
// Set display errors
if (isset($settings["display_errors"]) && ($settings["display_errors"])) {
error_reporting(E_ALL);
ini_set('display_errors', 'on');
}
// ---------------------------------------------------------------------------------------
// FUNCTIONS
// ---------------------------------------------------------------------------------------
// This function takes two arrays of settings and merges them, using
// the value from $overrides where it differs from the one in $defaults.
function ini_merge ($defaults, $overrides) {
foreach ($overrides as $k => $v) {
if (is_array($v)) {
$defaults[$k] = ini_merge($defaults[$k], $overrides[$k]);
} else {
$defaults[$k] = $v;
}
}
return $defaults;
};
// This function iterates over all the config file entries, replacing values
// of the format {{VAR_NAME}} with the environment variable 'VAR_NAME'.
//
// This can be useful in containerised setups, or testing environments.
function ini_check_envvars($config) {
global $error_out;
foreach ($config as $section => $options) {
foreach ($options as $key => $value) {
// Find {{ }} vars and replace what's within them with the
// named environment var
if (strpos($value, '{{') !== false && strpos($value, '}}') !== false) {
preg_match_all( '/{{([^}]*)}}/', $value, $matches);
foreach ($matches[1] as $match) {
if (!isset($_ENV[$match])) {
$error_out .= "<p>Error: environment var '${match}' not defined in config section [${section}], setting '${key}'</p>";
} else {
$newval = str_replace('{{'.$match.'}}', $_ENV[$match], $value);
// Convert booleans from strings
if ($newval === 'true') {
$newval = true;
} else if ($newval === 'false') {
$newval = false;
// Convert numbers from strings
} else if (is_numeric($newval)) {
$newval = $newval + 0;
}
// Set the new value
$config[$section][$key] = $newval;
}
}
}
}
}
return $config;
}