Skip to content

Commit

Permalink
Update db conf
Browse files Browse the repository at this point in the history
  • Loading branch information
VaMaster committed Feb 2, 2019
1 parent 3da63a4 commit 7f3c9f8
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/logs/*.log.old.php
/rw/config.ini.php
/rw/db.conf.php
/rw/db.json.php
/rw/config.php
/rw/commands.php
/rw/cacert.pem
Expand Down
12 changes: 5 additions & 7 deletions php/config/DbConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ public function isValid()


/**
* @param $configFile
* @param $config
* @return bool|mysqli
* @throws ConfigException
*/
protected function connect($configFile) {
$config = parse_ini_file($configFile);
$host = $config['dburi'] ?? 'localhost';
$mysqli = new mysqli($host,$config['username'],$config['password'],$config['dbname']);
protected function connect($config) {
$host = $config['host'] ?? 'localhost';
$mysqli = new mysqli($host,$config['username'],$config['password'],$config['database']);
if ($mysqli->connect_errno) {
throw new ConfigException("ERROR CONNECTING: ".$mysqli->connect_errno);
write_log("ERROR CONNECTING: ".$mysqli->connect_errno, "ERROR");
}

/* check if server is alive */
Expand Down
14 changes: 9 additions & 5 deletions php/config/appConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@ class appConfig
/**
* Config constructor.
* @param string $configFile
* @param $type
* @throws ConfigException
*/
public function __construct($configFile)
{
public function __construct($configFile, $type) {
$configObject = false;
if (!file_exists($configFile) || !is_readable($configFile)) {
throw new ConfigException("Invalid config file specified.");
}
$configFile = realpath($configFile);
$config = @parse_ini_file($configFile);
if ($type === 'db') {
$config = str_replace("'; <?php die('Access denied'); ?>", "", file_get_contents($configFile));
$config = json_decode($config, true);
if ($config) {
$user = $config['username'] ?? false;
$pass = $config['password'] ?? false;
$dbName = $config['dbname'] ?? false;
$dbName = $config['database'] ?? false;
if (!$user || !$pass || !$dbName) {
$words = [];
if (!$user) array_push($words, "'username'");
Expand All @@ -35,8 +38,9 @@ public function __construct($configFile)
$strings = join(", ",$words);
throw new ConfigException("Missing config param(s) $strings");
} else {
$configObject = new DbConfig($configFile);
$configObject = new DbConfig($config);
}
}
} else {
$configObject = new JsonConfig($configFile);
}
Expand Down
44 changes: 27 additions & 17 deletions php/webApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,25 @@ function initConfig() {
$config = isWebApp() ? false : ($_SESSION['configObject'] ?? false);
$configObject = false;
$error = false;
$dbFile = dirname(__FILE__) . "/../rw/db.conf.php";
$dbConfig = dirname(__FILE__) . "/../rw/db.json.php";
$jsonFile = dirname(__FILE__). "/../rw/config.php";
$configFile = file_exists($dbFile) ? $dbFile : $jsonFile;
if (!$config) {
//write_log("Creating session config object.");
if (file_exists($dbFile)) checkDefaultsDb($dbFile);
try {
$config = new digitalhigh\appConfig($configFile);
} catch (\digitalhigh\ConfigException $e) {
write_log("An exception occurred creating the configuration. '$e'", "ERROR",false,false,true);
$error = true;
}
$_SESSION['configObject'] = $config;
$type = file_exists($dbConfig) ? 'db' : 'file';
$configFile = file_exists($dbConfig) ? $dbConfig : $jsonFile;
if ($type === 'db') {
$configData = str_replace("'; <?php die('Access denied'); ?>", "", file_get_contents($config));
$configData = json_decode($configData, true);
checkDefaultsDb($configData);
}

try {
$config = new digitalhigh\appConfig($configFile, $type);
} catch (\digitalhigh\ConfigException $e) {
write_log("An exception occurred creating the configuration. '$e'", "ERROR",false,false,true);
$error = true;
}

$_SESSION['configObject'] = $config;

if (!$error) {
$configObject = $config->ConfigObject;
}
Expand Down Expand Up @@ -182,9 +187,11 @@ function scriptDefaults() {
}

function checkDefaults() {
$config = dirname(__FILE__) . "/../rw/db.conf.php";
$useDb = file_exists($config);
$configFile = "/../rw/db.json.php";
$useDb = file_exists($configFile);
if ($useDb) {
$config = str_replace("'; <?php die('Access denied'); ?>", "", file_get_contents($configFile));
$config = json_decode($config, true);
checkDefaultsDb($config);
}
// Loading from General
Expand Down Expand Up @@ -230,8 +237,7 @@ function checkDefaults() {
}

function checkDefaultsDb($config) {
$config = parse_ini_file($config);
$db = $config['dbname'];

$head = '<!DOCTYPE html>
<html lang="en">
<head>
Expand All @@ -245,7 +251,11 @@ function checkDefaultsDb($config) {
</body>
</html>';

$mysqli = new mysqli('localhost',$config['username'],$config['password']);
$db = $config['database'];
$host = $config['host'] ?? "localhost";
$username = $config['username'];
$pass = $config['password'];
$mysqli = new mysqli($host, $username, $pass);
$noDb = false;
if (! $mysqli->select_db($db)) {
$noDb = true;
Expand Down
7 changes: 0 additions & 7 deletions rw/db.conf.sample.php

This file was deleted.

8 changes: 8 additions & 0 deletions rw/db.sample.json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'; <?php die('Access denied'); ?>

{
"username": "dbuser",
"database": "flextvdb",
"host": "myflexdb.com",
"password": "dbpassworD"
}

0 comments on commit 7f3c9f8

Please sign in to comment.