Skip to content

Commit 94babc3

Browse files
committed
update db class use to support client flags (ssl)
1 parent 04f35df commit 94babc3

File tree

11 files changed

+113
-81
lines changed

11 files changed

+113
-81
lines changed

interface/lib/app.inc.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ public function __construct() {
6262
$this->_conf = $conf;
6363
if($this->_conf['start_db'] == true) {
6464
$this->load('db_'.$this->_conf['db_type']);
65-
$this->db = new db;
65+
try {
66+
$this->db = new db;
67+
} catch (Exception $e) {
68+
$this->db = false;
69+
}
6670
}
6771

6872
//* Start the session

interface/lib/classes/db_mysql.inc.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ class db
6464
private $record = array(); // last record fetched
6565
private $autoCommit = 1; // Autocommit Transactions
6666
private $currentRow; // current row number
67-
public $errorNumber = 0; // last error number
6867
*/
68+
public $errorNumber = 0; // last error number
6969
public $errorMessage = ''; // last error message
7070
/*
7171
private $errorLocation = '';// last error location
@@ -94,18 +94,20 @@ public function __construct($host = NULL , $user = NULL, $pass = NULL, $database
9494
if(!is_object($this->_iConnId)) {
9595
$this->_iConnId = mysqli_init();
9696
}
97-
mysqli_real_connect($this->_iConnId, $this->dbHost, $this->dbUser, $this->dbPass, '', (int)$this->dbPort, NULL, $this->dbClientFlags);
97+
if(!mysqli_real_connect($this->_iConnId, $this->dbHost, $this->dbUser, $this->dbPass, '', (int)$this->dbPort, NULL, $this->dbClientFlags)) {
98+
$this->_sqlerror('Database connection failed');
99+
}
98100
}
99101

100102
if(!is_object($this->_iConnId) || mysqli_connect_errno()) {
101103
$this->_iConnId = null;
102-
$this->_sqlerror('Zugriff auf Datenbankserver fehlgeschlagen! / Database server not accessible!', '', true);
103-
return false;
104+
$this->_sqlerror('Zugriff auf Datenbankserver fehlgeschlagen! / Database server not accessible!', '', true); // sets errorMessage
105+
throw new Exception($this->errorMessage);
104106
}
105107
if(!((bool)mysqli_query( $this->_iConnId, 'USE `' . $this->dbName . '`'))) {
106108
$this->close();
107-
$this->_sqlerror('Datenbank nicht gefunden / Database not found', '', true);
108-
return false;
109+
$this->_sqlerror('Datenbank nicht gefunden / Database not found', '', true); // sets errorMessage
110+
throw new Exception($this->errorMessage);
109111
}
110112

111113
$this->_setCharset();
@@ -261,8 +263,11 @@ private function _query($sQuery = '') {
261263
$try++;
262264
$ok = (is_object($this->_iConnId)) ? mysqli_ping($this->_iConnId) : false;
263265
if(!$ok) {
264-
if(!mysqli_real_connect(mysqli_init(), $this->dbHost, $this->dbUser, $this->dbPass, $this->dbName, (int)$this->dbPort, NULL, $this->dbClientFlags)) {
265-
if($this->errorNumber == '111') {
266+
if(!is_object($this->_iConnId)) {
267+
$this->_iConnId = mysqli_init();
268+
}
269+
if(!mysqli_real_connect($this->_isConnId, $this->dbHost, $this->dbUser, $this->dbPass, $this->dbName, (int)$this->dbPort, NULL, $this->dbClientFlags)) {
270+
if(mysqli_connect_errno() == '111') {
266271
// server is not available
267272
if($try > 9) {
268273
if(isset($app) && isset($app->forceErrorExit)) {
@@ -531,8 +536,13 @@ public function escape($sString) {
531536
private function _sqlerror($sErrormsg = 'Unbekannter Fehler', $sAddMsg = '', $bNoLog = false) {
532537
global $app, $conf;
533538

534-
$mysql_error = (is_object($this->_iConnId) ? mysqli_error($this->_iConnId) : mysqli_connect_error());
535-
$mysql_errno = (is_object($this->_iConnId) ? mysqli_errno($this->_iConnId) : mysqli_connect_errno());
539+
$mysql_errno = mysqli_connect_errno();
540+
$mysql_error = mysqli_connect_error();
541+
if ($mysql_errno === 0 && is_object($this->_iConnId)) {
542+
$mysql_errno = mysqli_errno($this->_iConnId);
543+
$mysql_error = mysqli_error($this->_iConnId);
544+
}
545+
$this->errorNumber = $mysql_error;
536546
$this->errorMessage = $mysql_error;
537547

538548
//$sAddMsg .= getDebugBacktrace();

interface/lib/classes/session.inc.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ class session {
3636
private $permanent = false;
3737

3838
function __construct($session_timeout = 0) {
39-
$this->db = new db;
39+
try {
40+
$this->db = new db;
41+
} catch (Exception $e) {
42+
$this->db = false;
43+
}
4044
$this->timeout = $session_timeout;
4145
}
4246

interface/web/themes/default/assets/stylesheets/ispconfig.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ body {
2525
.form-group input[type='checkbox'] {
2626
margin-top: 10px; }
2727

28+
.form-group .checkbox-inline input[type='checkbox'] {
29+
margin-top: 4px; }
30+
2831
.control-label {
2932
font-weight: normal; }
3033
.control-label:after {

interface/web/themes/default/assets/stylesheets/ispconfig.sass

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ body
2525
.form-group input[type='checkbox']
2626
margin-top: 10px
2727

28+
.form-group .checkbox-inline input[type='checkbox']
29+
margin-top: 4px
30+
2831
.control-label
2932
font-weight: normal
3033

@@ -311,4 +314,4 @@ thead.dark
311314

312315
.input-group-field:last-child
313316
border-top-left-radius: 0
314-
border-bottom-left-radius: 0
317+
border-bottom-left-radius: 0

interface/web/tools/dns_import_tupa.php

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,27 @@
4949
//* CSRF Check
5050
$app->auth->csrf_token_check();
5151

52-
//* Set variable sin template
52+
//* Set variables in template
5353
$app->tpl->setVar('dbhost', $_POST['dbhost'], true);
5454
$app->tpl->setVar('dbname', $_POST['dbname'], true);
5555
$app->tpl->setVar('dbuser', $_POST['dbuser'], true);
5656
$app->tpl->setVar('dbpassword', $_POST['dbpassword'], true);
57+
$app->tpl->setVar('dbssl', 'true', true);
5758

5859
//* Establish connection to external database
5960
$msg .= 'Connecting to external database...<br />';
6061

61-
//* Backup DB login details
62-
/*$conf_bak['db_host'] = $conf['db_host'];
63-
$conf_bak['db_database'] = $conf['db_database'];
64-
$conf_bak['db_user'] = $conf['db_user'];
65-
$conf_bak['db_password'] = $conf['db_password'];*/
62+
//* Set external db client flags
63+
$db_client_flags = 0;
64+
if(isset($_POST['dbssl']) && $_POST['dbssl'] == 1) $db_client_flags |= MYSQLI_CLIENT_SSL;
6665

67-
//* Set external Login details
68-
$conf['imp_db_host'] = $_POST['dbhost'];
69-
$conf['imp_db_database'] = $_POST['dbname'];
70-
$conf['imp_db_user'] = $_POST['dbuser'];
71-
$conf['imp_db_password'] = $_POST['dbpassword'];
72-
$conf['imp_db_charset'] = $conf['db_charset'];
73-
$conf['imp_db_new_link'] = $conf['db_new_link'];
74-
$conf['imp_db_client_flags'] = $conf['db_client_flags'];
75-
76-
//* create new db object
77-
$exdb = new db('imp');
66+
//* create new db object with external login details
67+
try {
68+
$exdb = new db($_POST['dbhost'], $_POST['dbuser'], $_POST['dbpassword'], $_POST['dbname'], 3306, $db_client_flags);
69+
} catch (Exception $e) {
70+
$error .= "Error connecting to Tupa database" . ($e->getMessage() ? ": " . $e->getMessage() : '.') . "<br />\n";
71+
$exdb = false;
72+
}
7873

7974
$server_id = 1;
8075
$sys_userid = 1;
@@ -159,26 +154,13 @@ function addot($text) {
159154
);
160155
$dns_rr_id = $app->db->datalogInsert('dns_rr', $insert_data, 'id');
161156
//$msg .= $insert_data.'<br />';
162-
163157
}
164158
}
165159
}
166-
167160
}
168161
}
169-
170-
171-
172-
} else {
173-
$error .= $exdb->errorMessage;
174162
}
175163

176-
//* restore db login details
177-
/*$conf['db_host'] = $conf_bak['db_host'];
178-
$conf['db_database'] = $conf_bak['db_database'];
179-
$conf['db_user'] = $conf_bak['db_user'];
180-
$conf['db_password'] = $conf_bak['db_password'];*/
181-
182164
}
183165

184166
$app->tpl->setVar('msg', $msg);

interface/web/tools/import_vpopmail.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@
5252

5353
if(isset($_POST['db_hostname']) && $_POST['db_hostname'] != '') {
5454

55-
//* Set external Login details
56-
$conf['imp_db_host'] = $_POST['db_hostname'];
57-
$conf['imp_db_database'] = $_POST['db_name'];
58-
$conf['imp_db_user'] = $_POST['db_user'];
59-
$conf['imp_db_password'] = $_POST['db_password'];
60-
$conf['imp_db_charset'] = 'utf8';
61-
$conf['imp_db_new_link'] = false;
62-
$conf['imp_db_client_flags'] = 0;
63-
64-
//* create new db object
65-
$exdb = new db('imp');
55+
//* Set external db client flags
56+
$db_client_flags = 0;
57+
if(isset($_POST['db_ssl']) && $_POST['db_ssl'] == 1) $db_client_flags |= MYSQLI_CLIENT_SSL;
58+
59+
//* create new db object with external login details
60+
try {
61+
$exdb = new db($_POST['db_hostname'], $_POST['db_user'], $_POST['db_password'], $_POST['db_name'], 3306, $db_client_flags);
62+
} catch (Exception $e) {
63+
$error .= "Error connecting to database" . ($e->getMessage() ? ": " . $e->getMessage() : '.') . "<br />\n";
64+
$exdb = false;
65+
}
6666

6767
if($exdb !== false) {
6868
$msg .= 'Databse connection succeeded<br />';
@@ -75,9 +75,6 @@
7575
} else {
7676
$msg .= 'The server with the ID $local_server_id is not a mail server.<br />';
7777
}
78-
79-
} else {
80-
$msg .= 'Database connection failed<br />';
8178
}
8279

8380
} else {
@@ -88,6 +85,7 @@
8885
$app->tpl->setVar('db_user', $_POST['db_user'], true);
8986
$app->tpl->setVar('db_password', $_POST['db_password'], true);
9087
$app->tpl->setVar('db_name', $_POST['db_name'], true);
88+
$app->tpl->setVar('db_ssl', 'true', true);
9189
$app->tpl->setVar('local_server_id', $_POST['local_server_id'], true);
9290
$app->tpl->setVar('msg', $msg);
9391
$app->tpl->setVar('error', $error);

interface/web/tools/templates/dns_import_tupa.htm

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,27 @@ <h1><tmpl_var name="list_head_txt">Import DNS records from Tupa PowerDNS control
44

55
<legend>PowerDNS Tupa import</legend>
66
<div class="form-group">
7-
<label class="col-sm-3 control-label">Tupa database hostname</label>
7+
<label class="col-sm-3 control-label" for="dbhost">Tupa database hostname</label>
88
<div class="col-sm-9"><input class="form-control" type="text" id="dbhost" value="{tmpl_var name='dbhost'}" name="dbhost" /></div>
99
</div>
1010
<div class="form-group">
11-
<label class="col-sm-3 control-label">Tupa database name</label>
11+
<label class="col-sm-3 control-label" for="dbname">Tupa database name</label>
1212
<div class="col-sm-9"><input class="form-control" type="text" id="dbname" value="{tmpl_var name='dbname'}" name="dbname" /></div>
1313
</div>
1414
<div class="form-group">
15-
<label class="col-sm-3 control-label">Tupa database user</label>
15+
<label class="col-sm-3 control-label" for="dbuser">Tupa database user</label>
1616
<div class="col-sm-9"><input class="form-control" type="text" id="dbuser" value="{tmpl_var name='dbuser'}" name="dbuser" /></div>
1717
</div>
1818
<div class="form-group">
19-
<label class="col-sm-3 control-label">Tupa database password</label>
19+
<label class="col-sm-3 control-label" for="dbpassword">Tupa database password</label>
2020
<div class="col-sm-9"><input class="form-control" type="text" id="dbpassword" value="{tmpl_var name='dbpassword'}" name="dbpassword" /></div>
2121
</div>
22-
22+
<div class="form-group">
23+
<label class="col-sm-3 control-label" for="dboptions">Tupa database options</label>
24+
<div class="col-sm-9" id="dboptions">
25+
<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>
26+
</div>
27+
</div>
2328

2429
<tmpl_if name="msg">
2530
<div id="OKMsg"><p><tmpl_var name="msg"></p></div>
@@ -34,4 +39,4 @@ <h1><tmpl_var name="list_head_txt">Import DNS records from Tupa PowerDNS control
3439
<div class="clear"><div class="right">
3540
<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>
3641
<button class="btn btn-default formbutton-default" type="button" value="Cancel" data-load-content="tools/index.php">Cancel</button>
37-
</div></div>
42+
</div></div>

interface/web/tools/templates/import_vpopmail.htm

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,31 @@ <h1><tmpl_var name="head_txt"></h1>
88
<div class="pnl_formsarea">
99
<legend>{tmpl_var name="legend_txt"}</legend>
1010
<div class="form-group">
11-
<label class="col-sm-3 control-label">Database Hostname</label>
11+
<label class="col-sm-3 control-label" for="db_hostname">Database Hostname</label>
1212
<div class="col-sm-9"><input class="form-control" type="text" id="db_hostname" value="{tmpl_var name='db_hostname'}" name="db_hostname" /></div>
1313
</div>
1414
<div class="form-group">
15-
<label class="col-sm-3 control-label">Database Name</label>
15+
<label class="col-sm-3 control-label" for="db_name">Database Name</label>
1616
<div class="col-sm-9"><input class="form-control" type="text" id="db_name" value="{tmpl_var name='db_name'}" name="db_name" /></div>
1717
</div>
1818
<div class="form-group">
19-
<label class="col-sm-3 control-label">Database User</label>
19+
<label class="col-sm-3 control-label" for="db_user">Database User</label>
2020
<div class="col-sm-9"><input class="form-control" type="text" id="db_user" value="{tmpl_var name='db_user'}" name="db_user" /></div>
2121
</div>
2222
<div class="form-group">
23-
<label class="col-sm-3 control-label">Database password</label>
23+
<label class="col-sm-3 control-label" for="db_password">Database password</label>
2424
<div class="col-sm-9"><input class="form-control" type="text" id="db_password" value="{tmpl_var name='db_password'}" name="db_password" /></div>
25+
</div>
26+
<div class="form-group">
27+
<label class="col-sm-3 control-label" for="db_options">Tupa database options</label>
28+
<div class="col-sm-9" id="db_options">
29+
<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>
30+
</div>
2531
</div>
2632
<div class="form-group">
27-
<label class="col-sm-3 control-label">Server ID of local mailserver</label>
33+
<label class="col-sm-3 control-label" for="local_server_id">Server ID of local mailserver</label>
2834
<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>
2935
</div>
30-
3136
</div>
3237

3338
<tmpl_if name="msg">

server/lib/app.inc.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,23 @@ function __construct() {
4343

4444
if($conf['start_db'] == true) {
4545
$this->load('db_'.$conf['db_type']);
46-
$this->db = new db;
46+
try {
47+
$this->db = new db;
48+
} catch (Exception $e) {
49+
$this->db = false;
50+
}
4751

4852
/*
4953
Initialize the connection to the master DB,
5054
if we are in a multiserver setup
5155
*/
5256

5357
if($conf['dbmaster_host'] != '' && ($conf['dbmaster_host'] != $conf['db_host'] || ($conf['dbmaster_host'] == $conf['db_host'] && $conf['dbmaster_database'] != $conf['db_database']))) {
54-
$this->dbmaster = new db($conf['dbmaster_host'], $conf['dbmaster_user'], $conf['dbmaster_password'], $conf['dbmaster_database'], $conf['dbmaster_port'], $conf['dbmaster_client_flags']);
58+
try {
59+
$this->dbmaster = new db($conf['dbmaster_host'], $conf['dbmaster_user'], $conf['dbmaster_password'], $conf['dbmaster_database'], $conf['dbmaster_port'], $conf['dbmaster_client_flags']);
60+
} catch (Exception $e) {
61+
$this->dbmaster = false;
62+
}
5563
} else {
5664
$this->dbmaster = $this->db;
5765
}

0 commit comments

Comments
 (0)