Commit 94babc39 authored by Jesse Norell's avatar Jesse Norell

update db class use to support client flags (ssl)

parent 04f35dfc
......@@ -62,7 +62,11 @@ class app {
$this->_conf = $conf;
if($this->_conf['start_db'] == true) {
$this->load('db_'.$this->_conf['db_type']);
$this->db = new db;
try {
$this->db = new db;
} catch (Exception $e) {
$this->db = false;
}
}
//* Start the session
......
......@@ -64,8 +64,8 @@ class db
private $record = array(); // last record fetched
private $autoCommit = 1; // Autocommit Transactions
private $currentRow; // current row number
public $errorNumber = 0; // last error number
*/
public $errorNumber = 0; // last error number
public $errorMessage = ''; // last error message
/*
private $errorLocation = '';// last error location
......@@ -94,18 +94,20 @@ class db
if(!is_object($this->_iConnId)) {
$this->_iConnId = mysqli_init();
}
mysqli_real_connect($this->_iConnId, $this->dbHost, $this->dbUser, $this->dbPass, '', (int)$this->dbPort, NULL, $this->dbClientFlags);
if(!mysqli_real_connect($this->_iConnId, $this->dbHost, $this->dbUser, $this->dbPass, '', (int)$this->dbPort, NULL, $this->dbClientFlags)) {
$this->_sqlerror('Database connection failed');
}
}
if(!is_object($this->_iConnId) || mysqli_connect_errno()) {
$this->_iConnId = null;
$this->_sqlerror('Zugriff auf Datenbankserver fehlgeschlagen! / Database server not accessible!', '', true);
return false;
$this->_sqlerror('Zugriff auf Datenbankserver fehlgeschlagen! / Database server not accessible!', '', true); // sets errorMessage
throw new Exception($this->errorMessage);
}
if(!((bool)mysqli_query( $this->_iConnId, 'USE `' . $this->dbName . '`'))) {
$this->close();
$this->_sqlerror('Datenbank nicht gefunden / Database not found', '', true);
return false;
$this->_sqlerror('Datenbank nicht gefunden / Database not found', '', true); // sets errorMessage
throw new Exception($this->errorMessage);
}
$this->_setCharset();
......@@ -261,8 +263,11 @@ class db
$try++;
$ok = (is_object($this->_iConnId)) ? mysqli_ping($this->_iConnId) : false;
if(!$ok) {
if(!mysqli_real_connect(mysqli_init(), $this->dbHost, $this->dbUser, $this->dbPass, $this->dbName, (int)$this->dbPort, NULL, $this->dbClientFlags)) {
if($this->errorNumber == '111') {
if(!is_object($this->_iConnId)) {
$this->_iConnId = mysqli_init();
}
if(!mysqli_real_connect($this->_isConnId, $this->dbHost, $this->dbUser, $this->dbPass, $this->dbName, (int)$this->dbPort, NULL, $this->dbClientFlags)) {
if(mysqli_connect_errno() == '111') {
// server is not available
if($try > 9) {
if(isset($app) && isset($app->forceErrorExit)) {
......@@ -531,8 +536,13 @@ class db
private function _sqlerror($sErrormsg = 'Unbekannter Fehler', $sAddMsg = '', $bNoLog = false) {
global $app, $conf;
$mysql_error = (is_object($this->_iConnId) ? mysqli_error($this->_iConnId) : mysqli_connect_error());
$mysql_errno = (is_object($this->_iConnId) ? mysqli_errno($this->_iConnId) : mysqli_connect_errno());
$mysql_errno = mysqli_connect_errno();
$mysql_error = mysqli_connect_error();
if ($mysql_errno === 0 && is_object($this->_iConnId)) {
$mysql_errno = mysqli_errno($this->_iConnId);
$mysql_error = mysqli_error($this->_iConnId);
}
$this->errorNumber = $mysql_error;
$this->errorMessage = $mysql_error;
//$sAddMsg .= getDebugBacktrace();
......
......@@ -36,7 +36,11 @@ class session {
private $permanent = false;
function __construct($session_timeout = 0) {
$this->db = new db;
try {
$this->db = new db;
} catch (Exception $e) {
$this->db = false;
}
$this->timeout = $session_timeout;
}
......
......@@ -25,6 +25,9 @@ body {
.form-group input[type='checkbox'] {
margin-top: 10px; }
.form-group .checkbox-inline input[type='checkbox'] {
margin-top: 4px; }
.control-label {
font-weight: normal; }
.control-label:after {
......
......@@ -25,6 +25,9 @@ body
.form-group input[type='checkbox']
margin-top: 10px
.form-group .checkbox-inline input[type='checkbox']
margin-top: 4px
.control-label
font-weight: normal
......@@ -311,4 +314,4 @@ thead.dark
.input-group-field:last-child
border-top-left-radius: 0
border-bottom-left-radius: 0
\ No newline at end of file
border-bottom-left-radius: 0
......@@ -49,32 +49,27 @@ if(isset($_POST['start']) && $_POST['start'] == 1) {
//* CSRF Check
$app->auth->csrf_token_check();
//* Set variable sin template
//* Set variables in template
$app->tpl->setVar('dbhost', $_POST['dbhost'], true);
$app->tpl->setVar('dbname', $_POST['dbname'], true);
$app->tpl->setVar('dbuser', $_POST['dbuser'], true);
$app->tpl->setVar('dbpassword', $_POST['dbpassword'], true);
$app->tpl->setVar('dbssl', 'true', true);
//* Establish connection to external database
$msg .= 'Connecting to external database...<br />';
//* Backup DB login details
/*$conf_bak['db_host'] = $conf['db_host'];
$conf_bak['db_database'] = $conf['db_database'];
$conf_bak['db_user'] = $conf['db_user'];
$conf_bak['db_password'] = $conf['db_password'];*/
//* Set external db client flags
$db_client_flags = 0;
if(isset($_POST['dbssl']) && $_POST['dbssl'] == 1) $db_client_flags |= MYSQLI_CLIENT_SSL;
//* Set external Login details
$conf['imp_db_host'] = $_POST['dbhost'];
$conf['imp_db_database'] = $_POST['dbname'];
$conf['imp_db_user'] = $_POST['dbuser'];
$conf['imp_db_password'] = $_POST['dbpassword'];
$conf['imp_db_charset'] = $conf['db_charset'];
$conf['imp_db_new_link'] = $conf['db_new_link'];
$conf['imp_db_client_flags'] = $conf['db_client_flags'];
//* create new db object
$exdb = new db('imp');
//* create new db object with external login details
try {
$exdb = new db($_POST['dbhost'], $_POST['dbuser'], $_POST['dbpassword'], $_POST['dbname'], 3306, $db_client_flags);
} catch (Exception $e) {
$error .= "Error connecting to Tupa database" . ($e->getMessage() ? ": " . $e->getMessage() : '.') . "<br />\n";
$exdb = false;
}
$server_id = 1;
$sys_userid = 1;
......@@ -159,26 +154,13 @@ if(isset($_POST['start']) && $_POST['start'] == 1) {
);
$dns_rr_id = $app->db->datalogInsert('dns_rr', $insert_data, 'id');
//$msg .= $insert_data.'<br />';
}
}
}
}
}
} else {
$error .= $exdb->errorMessage;
}
//* restore db login details
/*$conf['db_host'] = $conf_bak['db_host'];
$conf['db_database'] = $conf_bak['db_database'];
$conf['db_user'] = $conf_bak['db_user'];
$conf['db_password'] = $conf_bak['db_password'];*/
}
$app->tpl->setVar('msg', $msg);
......
......@@ -52,17 +52,17 @@ $app->tpl->setVar($wb);
if(isset($_POST['db_hostname']) && $_POST['db_hostname'] != '') {
//* Set external Login details
$conf['imp_db_host'] = $_POST['db_hostname'];
$conf['imp_db_database'] = $_POST['db_name'];
$conf['imp_db_user'] = $_POST['db_user'];
$conf['imp_db_password'] = $_POST['db_password'];
$conf['imp_db_charset'] = 'utf8';
$conf['imp_db_new_link'] = false;
$conf['imp_db_client_flags'] = 0;
//* create new db object
$exdb = new db('imp');
//* Set external db client flags
$db_client_flags = 0;
if(isset($_POST['db_ssl']) && $_POST['db_ssl'] == 1) $db_client_flags |= MYSQLI_CLIENT_SSL;
//* create new db object with external login details
try {
$exdb = new db($_POST['db_hostname'], $_POST['db_user'], $_POST['db_password'], $_POST['db_name'], 3306, $db_client_flags);
} catch (Exception $e) {
$error .= "Error connecting to database" . ($e->getMessage() ? ": " . $e->getMessage() : '.') . "<br />\n";
$exdb = false;
}
if($exdb !== false) {
$msg .= 'Databse connection succeeded<br />';
......@@ -75,9 +75,6 @@ if(isset($_POST['db_hostname']) && $_POST['db_hostname'] != '') {
} else {
$msg .= 'The server with the ID $local_server_id is not a mail server.<br />';
}
} else {
$msg .= 'Database connection failed<br />';
}
} else {
......@@ -88,6 +85,7 @@ $app->tpl->setVar('db_hostname', $_POST['db_hostname'], true);
$app->tpl->setVar('db_user', $_POST['db_user'], true);
$app->tpl->setVar('db_password', $_POST['db_password'], true);
$app->tpl->setVar('db_name', $_POST['db_name'], true);
$app->tpl->setVar('db_ssl', 'true', true);
$app->tpl->setVar('local_server_id', $_POST['local_server_id'], true);
$app->tpl->setVar('msg', $msg);
$app->tpl->setVar('error', $error);
......
......@@ -4,22 +4,27 @@
<legend>PowerDNS Tupa import</legend>
<div class="form-group">
<label class="col-sm-3 control-label">Tupa database hostname</label>
<label class="col-sm-3 control-label" for="dbhost">Tupa database hostname</label>
<div class="col-sm-9"><input class="form-control" type="text" id="dbhost" value="{tmpl_var name='dbhost'}" name="dbhost" /></div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Tupa database name</label>
<label class="col-sm-3 control-label" for="dbname">Tupa database name</label>
<div class="col-sm-9"><input class="form-control" type="text" id="dbname" value="{tmpl_var name='dbname'}" name="dbname" /></div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Tupa database user</label>
<label class="col-sm-3 control-label" for="dbuser">Tupa database user</label>
<div class="col-sm-9"><input class="form-control" type="text" id="dbuser" value="{tmpl_var name='dbuser'}" name="dbuser" /></div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Tupa database password</label>
<label class="col-sm-3 control-label" for="dbpassword">Tupa database password</label>
<div class="col-sm-9"><input class="form-control" type="text" id="dbpassword" value="{tmpl_var name='dbpassword'}" name="dbpassword" /></div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="dboptions">Tupa database options</label>
<div class="col-sm-9" id="dboptions">
<label class="checkbox-inline" for="dbssl"><input type="checkbox" id="dbssl" value="1" name="dbssl" <tmpl_if name='dbssl' op='==' value='true'>checked</tmpl_if>/> Use SSL</label>
</div>
</div>
<tmpl_if name="msg">
<div id="OKMsg"><p><tmpl_var name="msg"></p></div>
......@@ -34,4 +39,4 @@
<div class="clear"><div class="right">
<button class="btn btn-default formbutton-success" type="button" value="Import" data-submit-form="pageForm" data-form-action="tools/dns_import_tupa.php">Start</button>
<button class="btn btn-default formbutton-default" type="button" value="Cancel" data-load-content="tools/index.php">Cancel</button>
</div></div>
\ No newline at end of file
</div></div>
......@@ -8,26 +8,31 @@
<div class="pnl_formsarea">
<legend>{tmpl_var name="legend_txt"}</legend>
<div class="form-group">
<label class="col-sm-3 control-label">Database Hostname</label>
<label class="col-sm-3 control-label" for="db_hostname">Database Hostname</label>
<div class="col-sm-9"><input class="form-control" type="text" id="db_hostname" value="{tmpl_var name='db_hostname'}" name="db_hostname" /></div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Database Name</label>
<label class="col-sm-3 control-label" for="db_name">Database Name</label>
<div class="col-sm-9"><input class="form-control" type="text" id="db_name" value="{tmpl_var name='db_name'}" name="db_name" /></div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Database User</label>
<label class="col-sm-3 control-label" for="db_user">Database User</label>
<div class="col-sm-9"><input class="form-control" type="text" id="db_user" value="{tmpl_var name='db_user'}" name="db_user" /></div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Database password</label>
<label class="col-sm-3 control-label" for="db_password">Database password</label>
<div class="col-sm-9"><input class="form-control" type="text" id="db_password" value="{tmpl_var name='db_password'}" name="db_password" /></div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" for="db_options">Tupa database options</label>
<div class="col-sm-9" id="db_options">
<label class="checkbox-inline" for="db_ssl"><input type="checkbox" id="db_ssl" value="1" name="db_ssl" <tmpl_if name='db_ssl' op='==' value='true'>checked</tmpl_if>/> Use SSL</label>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Server ID of local mailserver</label>
<label class="col-sm-3 control-label" for="local_server_id">Server ID of local mailserver</label>
<div class="col-sm-9"><input class="form-control" type="text" id="local_server_id" value="{tmpl_var name='local_server_id'}" name="local_server_id" /></div>
</div>
</div>
<tmpl_if name="msg">
......
......@@ -43,7 +43,11 @@ class app {
if($conf['start_db'] == true) {
$this->load('db_'.$conf['db_type']);
$this->db = new db;
try {
$this->db = new db;
} catch (Exception $e) {
$this->db = false;
}
/*
Initialize the connection to the master DB,
......@@ -51,7 +55,11 @@ class app {
*/
if($conf['dbmaster_host'] != '' && ($conf['dbmaster_host'] != $conf['db_host'] || ($conf['dbmaster_host'] == $conf['db_host'] && $conf['dbmaster_database'] != $conf['db_database']))) {
$this->dbmaster = new db($conf['dbmaster_host'], $conf['dbmaster_user'], $conf['dbmaster_password'], $conf['dbmaster_database'], $conf['dbmaster_port'], $conf['dbmaster_client_flags']);
try {
$this->dbmaster = new db($conf['dbmaster_host'], $conf['dbmaster_user'], $conf['dbmaster_password'], $conf['dbmaster_database'], $conf['dbmaster_port'], $conf['dbmaster_client_flags']);
} catch (Exception $e) {
$this->dbmaster = false;
}
} else {
$this->dbmaster = $this->db;
}
......
......@@ -64,8 +64,8 @@ class db
private $record = array(); // last record fetched
private $autoCommit = 1; // Autocommit Transactions
private $currentRow; // current row number
public $errorNumber = 0; // last error number
*/
public $errorNumber = 0; // last error number
public $errorMessage = ''; // last error message
/*
private $errorLocation = '';// last error location
......@@ -94,18 +94,20 @@ class db
if(!is_object($this->_iConnId)) {
$this->_iConnId = mysqli_init();
}
mysqli_real_connect($this->_iConnId, $this->dbHost, $this->dbUser, $this->dbPass, '', (int)$this->dbPort, NULL, $this->dbClientFlags);
if(!mysqli_real_connect($this->_iConnId, $this->dbHost, $this->dbUser, $this->dbPass, '', (int)$this->dbPort, NULL, $this->dbClientFlags)) {
$this->_sqlerror('Database connection failed');
}
}
if(!is_object($this->_iConnId) || mysqli_connect_errno()) {
$this->_iConnId = null;
$this->_sqlerror('Zugriff auf Datenbankserver fehlgeschlagen! / Database server not accessible!', '', true);
return false;
$this->_sqlerror('Zugriff auf Datenbankserver fehlgeschlagen! / Database server not accessible!', '', true); // sets errorMessage
throw new Exception($this->errorMessage);
}
if(!((bool)mysqli_query( $this->_iConnId, 'USE `' . $this->dbName . '`'))) {
$this->close();
$this->_sqlerror('Datenbank nicht gefunden / Database not found', '', true);
return false;
$this->_sqlerror('Datenbank nicht gefunden / Database not found', '', true); // sets errorMessage
throw new Exception($this->errorMessage);
}
$this->_setCharset();
......@@ -261,8 +263,11 @@ class db
$try++;
$ok = (is_object($this->_iConnId)) ? mysqli_ping($this->_iConnId) : false;
if(!$ok) {
if(!mysqli_real_connect(mysqli_init(), $this->dbHost, $this->dbUser, $this->dbPass, $this->dbName, (int)$this->dbPort, NULL, $this->dbClientFlags)) {
if($this->errorNumber == '111') {
if(!is_object($this->_iConnId)) {
$this->_iConnId = mysqli_init();
}
if(!mysqli_real_connect($this->_isConnId, $this->dbHost, $this->dbUser, $this->dbPass, $this->dbName, (int)$this->dbPort, NULL, $this->dbClientFlags)) {
if(mysqli_connect_errno() == '111') {
// server is not available
if($try > 9) {
if(isset($app) && isset($app->forceErrorExit)) {
......@@ -531,8 +536,13 @@ class db
private function _sqlerror($sErrormsg = 'Unbekannter Fehler', $sAddMsg = '', $bNoLog = false) {
global $app, $conf;
$mysql_error = (is_object($this->_iConnId) ? mysqli_error($this->_iConnId) : mysqli_connect_error());
$mysql_errno = (is_object($this->_iConnId) ? mysqli_errno($this->_iConnId) : mysqli_connect_errno());
$mysql_errno = mysqli_connect_errno();
$mysql_error = mysqli_connect_error();
if ($mysql_errno === 0 && is_object($this->_iConnId)) {
$mysql_errno = mysqli_errno($this->_iConnId);
$mysql_error = mysqli_error($this->_iConnId);
}
$this->errorNumber = $mysql_error;
$this->errorMessage = $mysql_error;
//$sAddMsg .= getDebugBacktrace();
......
Markdown is supported
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