Skip to content

Commit f3ba894

Browse files
authored
Use Config helper (librenms#10339)
remove usage of global variable
1 parent 342acf5 commit f3ba894

File tree

367 files changed

+1589
-1857
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

367 files changed

+1589
-1857
lines changed

LibreNMS/Alert/Transport/Kayako.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace LibreNMS\Alert\Transport;
1313

1414
use LibreNMS\Alert\Transport;
15+
use LibreNMS\Config;
1516

1617
class Kayako extends Transport
1718
{
@@ -28,11 +29,10 @@ public function deliverAlert($host, $kayako)
2829

2930
public function contactKayako($host, $kayako)
3031
{
31-
global $config;
3232
$url = $kayako['url']."/Tickets/Ticket";
3333
$key = $kayako['key'];
3434
$secret = $kayako['secret'];
35-
$user = $config['email_from'];
35+
$user = Config::get('email_from');
3636
$department = $kayako['department'];
3737
$ticket_type= 1;
3838
$ticket_status = 1;

LibreNMS/Alert/Transport/Osticket.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace LibreNMS\Alert\Transport;
1313

1414
use LibreNMS\Alert\Transport;
15+
use LibreNMS\Config;
1516

1617
class Osticket extends Transport
1718
{
@@ -26,12 +27,10 @@ public function deliverAlert($obj, $opts)
2627

2728
public function contactOsticket($obj, $opts)
2829
{
29-
global $config;
30-
3130
$url = $opts['url'];
3231
$token = $opts['token'];
3332

34-
foreach (parse_email($config['email_from']) as $from => $from_name) {
33+
foreach (parse_email(Config::get('email_from')) as $from => $from_name) {
3534
$email = $from_name . ' <' . $from . '>';
3635
break;
3736
}

LibreNMS/Authentication/LdapAuthorizationAuthorizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*
3535
* To save lots of redundant queries to the LDAP server and speed up the
3636
* libreNMS WebUI, all information is cached within the PHP $_SESSION as
37-
* long as specified in $config['auth_ldap_cache_ttl'] (Default: 300s).
37+
* long as specified in the 'auth_ldap_cache_ttl' setting (Default: 300s).
3838
*/
3939

4040
namespace LibreNMS\Authentication;

LibreNMS/Authentication/SSOAuthorizer.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class SSOAuthorizer extends MysqlAuthorizer
4545
public function authenticate($credentials)
4646
{
4747
if (empty($credentials['username'])) {
48-
throw new AuthenticationException('$config[\'sso\'][\'user_attr\'] was not found or was empty');
48+
throw new AuthenticationException('\'sso.user_attr\' config setting was not found or was empty');
4949
}
5050

5151
// Build the user's details from attributes
@@ -97,7 +97,7 @@ public function authSSOGetAttr($attr, $prefix = 'HTTP_')
9797
return null;
9898
}
9999
} else {
100-
throw new AuthenticationException('$config[\'sso\'][\'trusted_proxies\'] is set, but this connection did not originate from trusted source: ' . $_SERVER['REMOTE_ADDR']);
100+
throw new AuthenticationException('\'sso.trusted_proxies\'] is set in your config, but this connection did not originate from trusted source: ' . $_SERVER['REMOTE_ADDR']);
101101
}
102102
}
103103

@@ -155,23 +155,23 @@ public function authSSOCalculateLevel()
155155
throw new AuthenticationException('group assignment by attribute requested, but httpd is not setting the attribute to a number');
156156
}
157157
} else {
158-
throw new AuthenticationException('group assignment by attribute requested, but $config[\'sso\'][\'level_attr\'] not set');
158+
throw new AuthenticationException('group assignment by attribute requested, but \'sso.level_attr\' not set in your config');
159159
}
160160
} elseif (Config::get('sso.group_strategy') === 'map') {
161161
if (Config::get('sso.group_level_map') && is_array(Config::get('sso.group_level_map')) && Config::get('sso.group_delimiter') && Config::get('sso.group_attr')) {
162162
return (int) $this->authSSOParseGroups();
163163
} else {
164-
throw new AuthenticationException('group assignment by level map requested, but $config[\'sso\'][\'group_level_map\'], $config[\'sso\'][\'group_attr\'], or $config[\'sso\'][\'group_delimiter\'] are not set');
164+
throw new AuthenticationException('group assignment by level map requested, but \'sso.group_level_map\', \'sso.group_attr\', or \'sso.group_delimiter\' are not set in your config');
165165
}
166166
} elseif (Config::get('sso.group_strategy') === 'static') {
167167
if (Config::get('sso.static_level')) {
168168
return (int) Config::get('sso.static_level');
169169
} else {
170-
throw new AuthenticationException('group assignment by static level was requested, but $config[\'sso\'][\'group_level_map\'] was not set');
170+
throw new AuthenticationException('group assignment by static level was requested, but \'sso.group_level_map\' was not set in your config');
171171
}
172172
}
173173

174-
throw new AuthenticationException('$config[\'sso\'][\'group_strategy\'] is not set to one of attribute, map or static - configuration is unsafe');
174+
throw new AuthenticationException('\'sso.group_strategy\' is not set to one of attribute in your config, map or static - configuration is unsafe');
175175
}
176176

177177
/**

LibreNMS/Config.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,16 @@ public static function json_encode()
336336
return json_encode($config);
337337
}
338338

339+
/**
340+
* Get the full configuration array
341+
* @return array
342+
*/
343+
public static function getAll()
344+
{
345+
global $config;
346+
return $config;
347+
}
348+
339349
/**
340350
* merge the database config with the global config
341351
* Global config overrides db

LibreNMS/IRCBot.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ class IRCBot
6868

6969
public function __construct()
7070
{
71-
global $config;
7271
$this->log('Setting up IRC-Bot..');
7372

74-
$this->config = $config;
73+
$this->config = Config::getAll();
7574
$this->debug = $this->config['irc_debug'];
7675
$this->config['irc_authtime'] = $this->config['irc_authtime'] ? $this->config['irc_authtime'] : 3;
7776
$this->max_retry = $this->config['irc_maxretry'];
@@ -623,15 +622,9 @@ private function _auth($params)
623622
private function _reload()
624623
{
625624
if ($this->user['level'] == 10) {
626-
global $config;
627-
$config = array();
628-
$config['install_dir'] = $this->config['install_dir'];
629-
chdir($config['install_dir']);
630-
include 'includes/defaults.inc.php';
631-
include 'config.php';
632-
include 'includes/definitions.inc.php';
625+
$new_config = Config::load();
633626
$this->respond('Reloading configuration & defaults');
634-
if ($config != $this->config) {
627+
if ($new_config != $this->config) {
635628
return $this->__construct();
636629
}
637630
} else {

LibreNMS/ObjectCache.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class ObjectCache implements ArrayAccess
4242
*/
4343
public function __construct($obj)
4444
{
45-
global $config;
4645
$this->obj = $obj;
4746
if (isset($GLOBALS['_ObjCache'][$obj])) {
4847
$this->data = $GLOBALS['_ObjCacheSkell'][$obj];
@@ -55,9 +54,9 @@ public function __construct($obj)
5554
$GLOBALS['_ObjCache'] = array();
5655
}
5756

58-
if (file_exists($config['install_dir'].'/includes/caches/'.$obj.'.inc.php')) {
57+
if (file_exists(\LibreNMS\Config::get('install_dir') . '/includes/caches/' . $obj . '.inc.php')) {
5958
$data = array();
60-
include $config['install_dir'].'/includes/caches/'.$obj.'.inc.php';
59+
include \LibreNMS\Config::get('install_dir') . '/includes/caches/' . $obj . '.inc.php';
6160
$this->data = $data;
6261
$GLOBALS['_ObjCacheSkell'][$obj] = $this->data;
6362
if (!(isset($GLOBALS['_ObjCache'][$obj]) && is_array($GLOBALS['_ObjCache'][$obj]))) {

LibreNMS/RRD/RrdDefinition.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
namespace LibreNMS\RRD;
2727

28+
use LibreNMS\Config;
2829
use LibreNMS\Exceptions\InvalidRrdTypeException;
2930

3031
class RrdDefinition
@@ -53,16 +54,14 @@ public static function make()
5354
*/
5455
public function addDataset($name, $type, $min = null, $max = null, $heartbeat = null)
5556
{
56-
global $config;
57-
5857
if (empty($name)) {
5958
d_echo("DS must be set to a non-empty string.");
6059
}
6160

6261
$ds = array();
6362
$ds[] = $this->escapeName($name);
6463
$ds[] = $this->checkType($type);
65-
$ds[] = is_null($heartbeat) ? $config['rrd']['heartbeat'] : $heartbeat;
64+
$ds[] = is_null($heartbeat) ? Config::get('rrd.heartbeat') : $heartbeat;
6665
$ds[] = is_null($min) ? 'U' : $min;
6766
$ds[] = is_null($max) ? 'U' : $max;
6867

addhost.php

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* @copyright (C) 2006 - 2012 Adam Armstrong
1212
*/
1313

14+
use LibreNMS\Config;
1415
use LibreNMS\Exceptions\HostUnreachableException;
1516

1617
$init_modules = array();
@@ -24,8 +25,8 @@
2425
array_shift($argv);
2526
array_unshift($argv, $cmd);
2627
$poller_group = $options['g'];
27-
} elseif ($config['distributed_poller'] === true) {
28-
$poller_group = $config['distributed_poller_group'];
28+
} elseif (Config::get('distributed_poller') === true) {
29+
$poller_group = Config::get('distributed_poller_group');
2930
} else {
3031
$poller_group = 0;
3132
}
@@ -39,7 +40,7 @@
3940
$force_add = false;
4041
}
4142

42-
$port_assoc_mode = $config['default_port_association_mode'];
43+
$port_assoc_mode = Config::get('default_port_association_mode');
4344
$valid_assoc_modes = get_port_assoc_modes();
4445
if (isset($options['p'])) {
4546
$port_assoc_mode = $options['p'];
@@ -67,6 +68,7 @@
6768
array_unshift($argv, $cmd);
6869
}
6970

71+
$transports_regex = implode('|', Config::get('snmp.transports'));
7072
if (!empty($argv[1])) {
7173
$host = strtolower($argv[1]);
7274
$community = $argv[2];
@@ -111,7 +113,7 @@
111113
// parse all remaining args
112114
if (is_numeric($arg)) {
113115
$port = $arg;
114-
} elseif (preg_match('/^('.implode('|', $config['snmp']['transports']).')$/', $arg)) {
116+
} elseif (preg_match('/^(' . $transports_regex . ')$/', $arg)) {
115117
$transport = $arg;
116118
} else {
117119
// should add a sanity check of chars allowed in user
@@ -120,7 +122,9 @@
120122
}
121123

122124
if ($seclevel === 'nanp') {
123-
array_unshift($config['snmp']['v3'], $v3);
125+
$v3_config = Config::get('snmp.v3');
126+
array_unshift($v3_config, $v3);
127+
Config::set('snmp.v3', $v3_config);
124128
}
125129
} elseif ($seclevel === 'anp' or $seclevel === 'authNoPriv') {
126130
$v3['authlevel'] = 'authNoPriv';
@@ -132,7 +136,7 @@
132136
// parse all remaining args
133137
if (is_numeric($arg)) {
134138
$port = $arg;
135-
} elseif (preg_match('/^('.implode('|', $config['snmp']['transports']).')$/i', $arg)) {
139+
} elseif (preg_match('/^(' . $transports_regex . ')$/i', $arg)) {
136140
$transport = $arg;
137141
} elseif (preg_match('/^(sha|md5)$/i', $arg)) {
138142
$v3['authalgo'] = $arg;
@@ -142,7 +146,9 @@
142146
}
143147
}
144148

145-
array_unshift($config['snmp']['v3'], $v3);
149+
$v3_config = Config::get('snmp.v3');
150+
array_unshift($v3_config, $v3);
151+
Config::set('snmp.v3', $v3_config);
146152
} elseif ($seclevel === 'ap' or $seclevel === 'authPriv') {
147153
$v3['authlevel'] = 'authPriv';
148154
$v3args = array_slice($argv, 4);
@@ -154,7 +160,7 @@
154160
// parse all remaining args
155161
if (is_numeric($arg)) {
156162
$port = $arg;
157-
} elseif (preg_match('/^('.implode('|', $config['snmp']['transports']).')$/i', $arg)) {
163+
} elseif (preg_match('/^(' . $transports_regex . ')$/i', $arg)) {
158164
$transport = $arg;
159165
} elseif (preg_match('/^(sha|md5)$/i', $arg)) {
160166
$v3['authalgo'] = $arg;
@@ -166,7 +172,9 @@
166172
}
167173
}//end while
168174

169-
array_unshift($config['snmp']['v3'], $v3);
175+
$v3_config = Config::get('snmp.v3');
176+
array_unshift($v3_config, $v3);
177+
Config::set('snmp.v3', $v3_config);
170178
}
171179
} else {
172180
// v2c or v1
@@ -176,15 +184,17 @@
176184
// parse all remaining args
177185
if (is_numeric($arg)) {
178186
$port = $arg;
179-
} elseif (preg_match('/('.implode('|', $config['snmp']['transports']).')/i', $arg)) {
187+
} elseif (preg_match('/(' . $transports_regex . ')/i', $arg)) {
180188
$transport = $arg;
181189
} elseif (preg_match('/^(v1|v2c)$/i', $arg)) {
182190
$snmpver = $arg;
183191
}
184192
}
185193

186194
if ($community) {
187-
array_unshift($config['snmp']['community'], $community);
195+
$comm_config = Config::get('snmp.community');
196+
array_unshift($comm_config, $community);
197+
Config::set('snmp.community', $comm_config);
188198
}
189199
}//end if
190200

@@ -205,21 +215,21 @@
205215
}
206216
} else {
207217
c_echo(
208-
"\n".$config['project_name_version'].' Add Host Tool
218+
"\n" . Config::get('project_name_version') . ' Add Host Tool
209219
210-
Usage (SNMPv1/2c) : ./addhost.php [-g <poller group>] [-f] [-b] [-p <port assoc mode>] <%Whostname%n> [community] [v1|v2c] [port] ['.implode('|', $config['snmp']['transports']).']
220+
Usage (SNMPv1/2c) : ./addhost.php [-g <poller group>] [-f] [-b] [-p <port assoc mode>] <%Whostname%n> [community] [v1|v2c] [port] [' . $transports_regex . ']
211221
Usage (SNMPv3) :
212-
Config Defaults : ./addhost.php [-g <poller group>] [-f] [-b] [-p <port assoc mode>] <%Whostname%n> any v3 [user] [port] ['.implode('|', $config['snmp']['transports']).']
213-
No Auth, No Priv : ./addhost.php [-g <poller group>] [-f] [-b] [-p <port assoc mode>] <%Whostname%n> nanp v3 [user] [port] ['.implode('|', $config['snmp']['transports']).']
214-
Auth, No Priv : ./addhost.php [-g <poller group>] [-f] [-b] [-p <port assoc mode>] <%Whostname%n> anp v3 <user> <password> [md5|sha] [port] ['.implode('|', $config['snmp']['transports']).']
215-
Auth, Priv : ./addhost.php [-g <poller group>] [-f] [-b] [-p <port assoc mode>] <%Whostname%n> ap v3 <user> <password> <enckey> [md5|sha] [aes|dsa] [port] ['.implode('|', $config['snmp']['transports']).']
222+
Config Defaults : ./addhost.php [-g <poller group>] [-f] [-b] [-p <port assoc mode>] <%Whostname%n> any v3 [user] [port] [' . $transports_regex . ']
223+
No Auth, No Priv : ./addhost.php [-g <poller group>] [-f] [-b] [-p <port assoc mode>] <%Whostname%n> nanp v3 [user] [port] [' . $transports_regex . ']
224+
Auth, No Priv : ./addhost.php [-g <poller group>] [-f] [-b] [-p <port assoc mode>] <%Whostname%n> anp v3 <user> <password> [md5|sha] [port] [' . $transports_regex . ']
225+
Auth, Priv : ./addhost.php [-g <poller group>] [-f] [-b] [-p <port assoc mode>] <%Whostname%n> ap v3 <user> <password> <enckey> [md5|sha] [aes|dsa] [port] [' . $transports_regex . ']
216226
Usage (ICMP only) : ./addhost.php [-g <poller group>] [-f] -P <%Whostname%n> [os] [hardware]
217227
218228
-g <poller group> allows you to add a device to be pinned to a specific poller when using distributed polling. X can be any number associated with a poller group
219229
-f forces the device to be added by skipping the icmp and snmp check against the host.
220230
-p <port assoc mode> allow you to set a port association mode for this device. By default ports are associated by \'ifIndex\'.
221231
For Linux/Unix based devices \'ifName\' or \'ifDescr\' might be useful for a stable iface mapping.
222-
The default for this installation is \'' . $config['default_port_association_mode'] . '\'
232+
The default for this installation is \'' . Config::get('default_port_association_mode') . '\'
223233
Valid port assoc modes are: ' . join(', ', $valid_assoc_modes) . '
224234
-b Add the host with SNMP if it replies to it, otherwise only ICMP.
225235
-P Add the host with only ICMP, no SNMP or OS discovery.

alerts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
echo "DEBUG!\n";
3939
}
4040

41-
if (!defined('TEST') && $config['alert']['disable'] != 'true') {
41+
if (!defined('TEST') && \LibreNMS\Config::get('alert.disable') != 'true') {
4242
echo 'Start: '.date('r')."\r\n";
4343
echo "ClearStaleAlerts():" . PHP_EOL;
4444
ClearStaleAlerts();

0 commit comments

Comments
 (0)