Skip to content

Commit

Permalink
refactor: Refactor database and config init (librenms#8527)
Browse files Browse the repository at this point in the history
* Refactor database and config init
Connect to the database without loading full config
Load config completely so post-processing is always done consistently.
Erase existing $config when loading, fixes issues in case we load the config twice.
If the database is not connected, don't try to load database settings. (Fixes some db errors on install)
Attempt to remove $config access/modification before init.php
Remove usage of db_name, that might not match the connected database.
Centralize db config loading, so we consistently apply db_test database settings.
Many of these changes are influenced by Laravel port.

* Some safety so we don't assign strings to numeric port field
Smooth out phpunit bootstrap

* Fix a couple of scrutinizer warnings.
  • Loading branch information
murrant authored and TheMysteriousX committed May 20, 2018
1 parent 9850152 commit 28c01b9
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 160 deletions.
103 changes: 69 additions & 34 deletions LibreNMS/Config.php
Expand Up @@ -28,62 +28,59 @@
class Config
{
/**
* Load the user config from config.php
* Load the config, if the database connected, pull in database settings.
*
* @param string $install_dir
* @return array
* return &array
*/
public static function &load($install_dir = null)
public static function &load()
{
global $config;

if (empty($install_dir)) {
$install_dir = __DIR__ . '/../';
}
$install_dir = realpath($install_dir);
$config['install_dir'] = $install_dir;

// load defaults
require $install_dir . '/includes/defaults.inc.php';
require $install_dir . '/includes/definitions.inc.php';
self::loadFiles();

// import standard settings
$macros = json_decode(file_get_contents($install_dir . '/misc/macros.json'), true);
self::set('alert.macros.rule', $macros);
// Make sure the database is connected
if (dbIsConnected()) {
// pull in the database config settings
self::mergeDb();

// variable definitions (remove me)
require $install_dir . '/includes/vmware_guestid.inc.php';
// load graph types from the database
self::loadGraphsFromDb();
}

// Load user config
include $install_dir . '/config.php';
// Process $config to tidy up
self::processConfig();

return $config;
}

/**
* Load Config from the database
* Load the user config from config.php, defaults.inc.php and definitions.inc.php, etc.
* Erases existing config.
*
* @throws Exceptions\DatabaseConnectException
* @return array
*/
public static function &loadFromDatabase()
private static function &loadFiles()
{
global $config;

if (empty($config)) {
self::load();
}
$config = []; // start fresh

// Make sure the database is connected
dbConnect();
$install_dir = realpath(__DIR__ . '/../');
$config['install_dir'] = $install_dir;

// pull in the database config settings
self::mergeDb();
// load defaults
require $install_dir . '/includes/defaults.inc.php';
require $install_dir . '/includes/definitions.inc.php';

// load graph types from the database
self::loadGraphsFromDb();
// import standard settings
$macros = json_decode(file_get_contents($install_dir . '/misc/macros.json'), true);
self::set('alert.macros.rule', $macros);

// Process $config to tidy up
self::processConfig();
// variable definitions (remove me)
require $install_dir . '/includes/vmware_guestid.inc.php';

// Load user config
include $install_dir . '/config.php';

return $config;
}
Expand Down Expand Up @@ -440,4 +437,42 @@ private static function deprecatedVariable($old, $new)
self::set($new, self::get($old));
}
}

/**
* Get just the database connection settings from config.php
*
* @return array (keys: db_host, db_port, db_name, db_user, db_pass, db_socket)
*/
public static function getDatabaseSettings()
{
// Do not access global $config in this function!

$keys = $config = [
'db_host' => '',
'db_port' => '',
'db_name' => '',
'db_user' => '',
'db_pass' => '',
'db_socket' => '',
];

if (is_file(__DIR__ . '/../config.php')) {
include __DIR__ . '/../config.php';
}

// Check for testing database
if (getenv('DBTEST')) {
if (isset($config['test_db_name'])) {
$config['db_name'] = $config['test_db_name'];
}
if (isset($config['test_db_user'])) {
$config['db_user'] = $config['test_db_user'];
}
if (isset($config['test_db_pass'])) {
$config['db_pass'] = $config['test_db_pass'];
}
}

return array_intersect_key($config, $keys); // return only the db settings
}
}
9 changes: 1 addition & 8 deletions LibreNMS/IRCBot.php
Expand Up @@ -509,14 +509,7 @@ private function chkdb()
{
if (!is_resource($this->sql)) {
try {
$this->sql = dbConnect(
$this->config['db_host'],
$this->config['db_user'],
$this->config['db_pass'],
$this->config['db_name'],
$this->config['db_port'],
$this->config['db_socket']
);
$this->sql = dbConnect();
} catch (DatabaseConnectException $e) {
$this->log('Cannot connect to MySQL: ' . $e->getMessage());
return die();
Expand Down
14 changes: 8 additions & 6 deletions LibreNMS/Validations/Database.php
Expand Up @@ -89,11 +89,13 @@ private function checkMode(Validator $validator)

private function checkCollation(Validator $validator)
{
$db_name = dbFetchCell('SELECT DATABASE()');

// Test for correct character set and collation
$db_collation_sql = "SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME
FROM information_schema.SCHEMATA S
WHERE schema_name = '" . Config::get('db_name') .
"' AND ( DEFAULT_CHARACTER_SET_NAME != 'utf8' OR DEFAULT_COLLATION_NAME != 'utf8_unicode_ci')";
WHERE schema_name = '$db_name' AND
( DEFAULT_CHARACTER_SET_NAME != 'utf8' OR DEFAULT_COLLATION_NAME != 'utf8_unicode_ci')";
$collation = dbFetchRows($db_collation_sql);
if (empty($collation) !== true) {
$validator->fail(
Expand All @@ -104,8 +106,8 @@ private function checkCollation(Validator $validator)

$table_collation_sql = "SELECT T.TABLE_NAME, C.CHARACTER_SET_NAME, C.COLLATION_NAME
FROM information_schema.TABLES AS T, information_schema.COLLATION_CHARACTER_SET_APPLICABILITY AS C
WHERE C.collation_name = T.table_collation AND T.table_schema = '" . Config::get('db_name') .
"' AND ( C.CHARACTER_SET_NAME != 'utf8' OR C.COLLATION_NAME != 'utf8_unicode_ci' );";
WHERE C.collation_name = T.table_collation AND T.table_schema = '$db_name' AND
( C.CHARACTER_SET_NAME != 'utf8' OR C.COLLATION_NAME != 'utf8_unicode_ci' );";
$collation_tables = dbFetchRows($table_collation_sql);
if (empty($collation_tables) !== true) {
$result = ValidationResult::fail('MySQL tables collation is wrong: ')
Expand All @@ -115,8 +117,8 @@ private function checkCollation(Validator $validator)
}

$column_collation_sql = "SELECT TABLE_NAME, COLUMN_NAME, CHARACTER_SET_NAME, COLLATION_NAME
FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '" . Config::get('db_name') .
"' AND ( CHARACTER_SET_NAME != 'utf8' OR COLLATION_NAME != 'utf8_unicode_ci' );";
FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '$db_name' AND
( CHARACTER_SET_NAME != 'utf8' OR COLLATION_NAME != 'utf8_unicode_ci' );";
$collation_columns = dbFetchRows($column_collation_sql);
if (empty($collation_columns) !== true) {
$result = ValidationResult::fail('MySQL column collation is wrong: ')
Expand Down
39 changes: 22 additions & 17 deletions build-base.php
Expand Up @@ -24,32 +24,37 @@
* @author Tony Murray <murraytony@gmail.com>
*/

use LibreNMS\Exceptions\DatabaseConnectException;

if (!isset($init_modules)) {
$opts = getopt('ldh:u:p:n:t:s:');
$init_modules = array('nodb');
require __DIR__ . '/includes/init.php';

$db_vars = array(
'db_host' => 'h',
'db_user' => 'u',
'db_pass' => 'p',
'db_name' => 'n',
'db_port' => 't',
'db_socket' => 's',
);
$opts = getopt('ldh:u:p:n:t:s:');

$config = array();
foreach ($db_vars as $setting => $opt) {
if (isset($opts[$opt])) {
$config[$setting] = $opts[$opt];
try {
if (isset($opts['h'])) {
dbConnect(
isset($opts['h']) ? $opts['h'] : null,
isset($opts['u']) ? $opts['u'] : '',
isset($opts['p']) ? $opts['p'] : '',
isset($opts['n']) ? $opts['n'] : '',
isset($opts['t']) ? $opts['t'] : null,
isset($opts['s']) ? $opts['s'] : null
);
} else {
// use configured database credentials
dbConnect();
}
} catch (DatabaseConnectException $e) {
echo $e->getMessage() . PHP_EOL;
exit;
}

$init_modules = array();
require __DIR__ . '/includes/init.php';

$debug = isset($opts['d']);
$skip_schema_lock = isset($opts['l']);
}

require 'includes/sql-schema/update.php';
require __DIR__ . '/includes/sql-schema/update.php';

exit($return);
2 changes: 1 addition & 1 deletion html/index.php
Expand Up @@ -53,7 +53,7 @@
// Check for install.inc.php
if (!file_exists('../config.php') && $_SERVER['PATH_INFO'] != '/install.php') {
// no config.php does so let's redirect to the install
header("Location: {$config['base_url']}/install.php");
header("Location: /install.php");
exit;
}

Expand Down
19 changes: 6 additions & 13 deletions html/install.php
Expand Up @@ -37,19 +37,12 @@
$dbpass = @$_POST['dbpass'] ?: '';
$dbname = @$_POST['dbname'] ?: 'librenms';
$dbport = @$_POST['dbport'] ?: 3306;
$dbsocket = @$_POST['dbsocket'] ?: '';
$config['db_host']=$dbhost;
$config['db_user']=$dbuser;
$config['db_pass']=$dbpass;
$config['db_name']=$dbname;
$config['db_port']=$dbport;
$config['db_socket']=$dbsocket;

if (!empty($config['db_socket'])) {
$config['db_host'] = 'localhost';
$config['db_port'] = null;
if (empty($_POST['dbsocket'])) {
$dbsocket = null;
} else {
$config['db_socket'] = null;
$dbhost = 'localhost';
$dbsocket = $_POST['dbsocket'];
$dbport = null;
}

$add_user = @$_POST['add_user'] ?: '';
Expand All @@ -61,7 +54,7 @@
if ($stage > 1) {
try {
if ($stage != 6) {
dbConnect();
dbConnect($dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbsocket);
}
if ($stage == 2 && $_SESSION['build-ok'] == true) {
$stage = 3;
Expand Down
52 changes: 32 additions & 20 deletions includes/dbFacile.php
Expand Up @@ -17,6 +17,7 @@
* 3. Oh, and dbFetchAll() is now dbFetchRows()
*/

use LibreNMS\Config;
use LibreNMS\Exceptions\DatabaseConnectException;

function dbIsConnected()
Expand All @@ -33,18 +34,18 @@ function dbIsConnected()
* Connect to the database.
* Will use global $config variables if they are not sent: db_host, db_user, db_pass, db_name, db_port, db_socket
*
* @param string $host
* @param string $user
* @param string $password
* @param string $database
* @param string $port
* @param string $socket
* @param string $db_host
* @param string $db_user
* @param string $db_pass
* @param string $db_name
* @param string $db_port
* @param string $db_socket
* @return mysqli
* @throws DatabaseConnectException
*/
function dbConnect($host = null, $user = '', $password = '', $database = '', $port = null, $socket = null)
function dbConnect($db_host = null, $db_user = '', $db_pass = '', $db_name = '', $db_port = null, $db_socket = null)
{
global $config, $database_link;
global $database_link;

if (dbIsConnected()) {
return $database_link;
Expand All @@ -54,32 +55,43 @@ function dbConnect($host = null, $user = '', $password = '', $database = '', $po
throw new DatabaseConnectException("mysqli extension not loaded!");
}

$host = empty($host) ? $config['db_host'] : $host;
$user = empty($user) ? $config['db_user'] : $user;
$password = empty($password) ? $config['db_pass'] : $password;
$database = empty($database) ? $config['db_name'] : $database;
$port = empty($port) ? $config['db_port'] : $port;
$socket = empty($socket) ? $config['db_socket'] : $socket;
if (is_null($db_host)) {
$db_config = Config::getDatabaseSettings();
extract($db_config);
/** @var string $db_host */
/** @var string $db_port */
/** @var string $db_socket */
/** @var string $db_name */
/** @var string $db_user */
/** @var string $db_pass */
}

if (empty($db_socket)) {
$db_socket = null;
}
if (!is_numeric($db_port)) {
$db_port = null;
}

$database_link = mysqli_connect('p:' . $host, $user, $password, null, $port, $socket);
$database_link = mysqli_connect('p:' . $db_host, $db_user, $db_pass, null, $db_port, $db_socket);
mysqli_options($database_link, MYSQLI_OPT_LOCAL_INFILE, false);
if ($database_link === false) {
$error = mysqli_connect_error();
if ($error == 'No such file or directory') {
$error = 'Could not connect to ' . $host;
$error = 'Could not connect to ' . $db_host;
}
throw new DatabaseConnectException($error);
}

$database_db = mysqli_select_db($database_link, $config['db_name']);
$database_db = mysqli_select_db($database_link, $db_name);
if (!$database_db) {
$db_create_sql = "CREATE DATABASE " . $config['db_name'] . " CHARACTER SET utf8 COLLATE utf8_unicode_ci";
$db_create_sql = "CREATE DATABASE $db_name CHARACTER SET utf8 COLLATE utf8_unicode_ci";
mysqli_query($database_link, $db_create_sql);
$database_db = mysqli_select_db($database_link, $database);
$database_db = mysqli_select_db($database_link, $db_name);
}

if (!$database_db) {
throw new DatabaseConnectException("Could not select database: $database. " . mysqli_error($database_link));
throw new DatabaseConnectException("Could not select database: $db_name. " . mysqli_error($database_link));
}

dbQuery("SET NAMES 'utf8'");
Expand Down

0 comments on commit 28c01b9

Please sign in to comment.