Skip to content

Commit a07d9a0

Browse files
author
Till Brehm
committed
Merge branch '6766-database-prefix-is-not-working-correctly' into 'develop'
Resolve "Database prefix is not working correctly" Closes #6766 See merge request ispconfig/ispconfig3!1944
2 parents 1169dfe + 153a59f commit a07d9a0

File tree

1 file changed

+118
-26
lines changed

1 file changed

+118
-26
lines changed

interface/web/sites/database_edit.php

Lines changed: 118 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -306,32 +306,19 @@ function onBeforeUpdate() {
306306
$global_config = $app->getconf->get_global_config('sites');
307307
$dbname_prefix = $app->tools_sites->replacePrefix($global_config['dbname_prefix'], $this->dataRecord);
308308

309-
if($this->id > 0) {
310-
//* Prevent that the database name and charset is changed
311-
$old_record = $app->tform->getDataRecord($this->id);
312-
$dbname_prefix = $app->tools_sites->getPrefix($old_record['database_name_prefix'], $dbname_prefix);
313-
$this->dataRecord['database_name_prefix'] = $dbname_prefix;
314-
315-
//* Only admin can change the database name
316-
if ($_SESSION["s"]["user"]["typ"] != 'admin') {
317-
if($old_record["database_name"] != $dbname_prefix . $this->dataRecord["database_name"]) {
318-
$app->tform->errorMessage .= $app->tform->wordbook["database_name_change_txt"].'<br />';
319-
}
309+
//* Prevent that the database name and charset is changed
310+
$old_record = $app->tform->getDataRecord($this->id);
311+
$dbname_prefix = $app->tools_sites->getPrefix($old_record['database_name_prefix'], $dbname_prefix);
312+
$this->dataRecord['database_name_prefix'] = $dbname_prefix;
313+
314+
//* Only admin can change the database name
315+
if ($_SESSION["s"]["user"]["typ"] != 'admin') {
316+
if($old_record["database_name"] != $dbname_prefix . $this->dataRecord["database_name"]) {
317+
$app->tform->errorMessage .= $app->tform->wordbook["database_name_change_txt"].'<br />';
320318
}
321-
if($old_record["database_charset"] != $this->dataRecord["database_charset"]) {
322-
$app->tform->errorMessage .= $app->tform->wordbook["database_charset_change_txt"].'<br />';
323-
}
324-
325-
//* Check if the server has been changed
326-
// We do this only for the admin or reseller users, as normal clients can not change the server ID anyway
327-
if($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
328-
if($old_record["server_id"] != $this->dataRecord["server_id"]) {
329-
//* Add a error message and switch back to old server
330-
$app->tform->errorMessage .= $app->lng('The Server can not be changed.');
331-
$this->dataRecord["server_id"] = $rec['server_id'];
332-
}
333-
}
334-
unset($old_record);
319+
}
320+
if($old_record["database_charset"] != $this->dataRecord["database_charset"]) {
321+
$app->tform->errorMessage .= $app->tform->wordbook["database_charset_change_txt"].'<br />';
335322
}
336323

337324
if(!$this->dataRecord['database_user_id']) {
@@ -341,6 +328,16 @@ function onBeforeUpdate() {
341328
//* Database username and database name shall not be empty
342329
if($this->dataRecord['database_name'] == '') $app->tform->errorMessage .= $app->tform->wordbook["database_name_error_empty"].'<br />';
343330

331+
//* Check if the server has been changed
332+
// We do this only for the admin or reseller users, as normal clients can not change the server ID anyway
333+
if($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
334+
if($old_record["server_id"] != $this->dataRecord["server_id"]) {
335+
//* Add a error message and switch back to old server
336+
$app->tform->errorMessage .= $app->lng('The Server can not be changed.');
337+
$this->dataRecord["server_id"] = $rec['server_id'];
338+
}
339+
}
340+
unset($old_record);
344341

345342
if(strlen($dbname_prefix . $this->dataRecord['database_name']) > 64) $app->tform->errorMessage .= str_replace('{db}', $dbname_prefix . $this->dataRecord['database_name'], $app->tform->wordbook["database_name_error_len"]).'<br />';
346343

@@ -437,7 +434,102 @@ function onBeforeUpdate() {
437434
function onBeforeInsert() {
438435
global $app, $conf, $interfaceConf;
439436

440-
$this->onBeforeUpdate();
437+
//* Site shell not be empty
438+
if($this->dataRecord['parent_domain_id'] == 0) $app->tform->errorMessage .= $app->tform->lng("database_site_error_empty").'<br />';
439+
440+
//* Database username and database name shall not be empty
441+
if($this->dataRecord['database_name'] == '') $app->tform->errorMessage .= $app->tform->wordbook["database_name_error_empty"].'<br />';
442+
443+
//* Get the database name and database user prefix
444+
$app->uses('getconf,tools_sites');
445+
$global_config = $app->getconf->get_global_config('sites');
446+
$dbname_prefix = $app->tools_sites->replacePrefix($global_config['dbname_prefix'], $this->dataRecord);
447+
$this->dataRecord['database_name_prefix'] = $dbname_prefix;
448+
449+
if(strlen($dbname_prefix . $this->dataRecord['database_name']) > 64) $app->tform->errorMessage .= str_replace('{db}', $dbname_prefix . $this->dataRecord['database_name'], $app->tform->wordbook["database_name_error_len"]).'<br />';
450+
451+
//* Check database name and user against blacklist
452+
$dbname_blacklist = array($conf['db_database'], 'mysql');
453+
if(in_array($dbname_prefix . $this->dataRecord['database_name'], $dbname_blacklist)) {
454+
$app->tform->errorMessage .= $app->lng('Database name not allowed.').'<br />';
455+
}
456+
457+
/* restrict the names */
458+
/* crop user and db names if they are too long -> mysql: user: 16 chars / db: 64 chars */
459+
if ($app->tform->errorMessage == ''){
460+
$this->dataRecord['database_name'] = substr($dbname_prefix . $this->dataRecord['database_name'], 0, 64);
461+
}
462+
463+
//* Check for duplicates
464+
$tmp = $app->db->queryOneRecord("SELECT count(database_id) as dbnum FROM web_database WHERE database_name = ? AND server_id = ?", $this->dataRecord['database_name'], $this->dataRecord["server_id"]);
465+
if($tmp['dbnum'] > 0) $app->tform->errorMessage .= $app->tform->lng('database_name_error_unique').'<br />';
466+
467+
// get the web server ip (parent domain)
468+
$tmp = $app->db->queryOneRecord("SELECT server_id FROM web_domain WHERE domain_id = ?", $this->dataRecord['parent_domain_id']);
469+
if($tmp['server_id'] && $tmp['server_id'] != $this->dataRecord['server_id']) {
470+
// we need remote access rights for this server, so get it's ip address
471+
$server_config = $app->getconf->get_server_config($tmp['server_id'], 'server');
472+
473+
// Add default remote_ips from Main Configuration.
474+
if(empty($global_config['default_remote_dbserver'])) {
475+
$remote_ips = array();
476+
} else {
477+
$remote_ips = explode(",", $global_config['default_remote_dbserver']);
478+
}
479+
480+
if (!in_array($server_config['ip_address'], $remote_ips)) { $remote_ips[] = $server_config['ip_address']; }
481+
482+
if($server_config['ip_address']!='') {
483+
if($this->dataRecord['remote_access'] != 'y'){
484+
$this->dataRecord['remote_ips'] = implode(',', $remote_ips);
485+
$this->dataRecord['remote_access'] = 'y';
486+
} else {
487+
if($this->dataRecord['remote_ips'] != ''){
488+
if(preg_match('/(^|,)' . preg_quote($server_config['ip_address'], '/') . '(,|$)/', $this->dataRecord['remote_ips']) == false) {
489+
$this->dataRecord['remote_ips'] .= ',' . $server_config['ip_address'];
490+
}
491+
$tmp = preg_split('/\s*,\s*/', $this->dataRecord['remote_ips']);
492+
$tmp = array_merge($tmp, $remote_ips);
493+
$tmp = array_unique($tmp);
494+
$this->dataRecord['remote_ips'] = implode(',', $tmp);
495+
unset($tmp);
496+
}
497+
}
498+
}
499+
} else {
500+
if(!empty($global_config['default_remote_dbserver'])) {
501+
// Add default remote_ips from Main Configuration.
502+
$remote_ips = explode(",", $global_config['default_remote_dbserver']);
503+
504+
if($this->dataRecord['remote_access'] != 'y'){
505+
$this->dataRecord['remote_ips'] = implode(',', $remote_ips);
506+
$this->dataRecord['remote_access'] = 'y';
507+
}
508+
}
509+
}
510+
511+
if ($app->tform->errorMessage == '') {
512+
// force update of the used database user
513+
if($this->dataRecord['database_user_id']) {
514+
$user_old_rec = $app->db->queryOneRecord('SELECT * FROM `web_database_user` WHERE `database_user_id` = ?', $this->dataRecord['database_user_id']);
515+
if($user_old_rec) {
516+
$user_new_rec = $user_old_rec;
517+
$user_new_rec['server_id'] = $this->dataRecord['server_id'];
518+
$app->db->datalogSave('web_database_user', 'UPDATE', 'database_user_id', $this->dataRecord['database_user_id'], $user_old_rec, $user_new_rec);
519+
}
520+
}
521+
if($this->dataRecord['database_ro_user_id']) {
522+
$user_old_rec = $app->db->queryOneRecord('SELECT * FROM `web_database_user` WHERE `database_user_id` = ?', $this->dataRecord['database_ro_user_id']);
523+
if($user_old_rec) {
524+
$user_new_rec = $user_old_rec;
525+
$user_new_rec['server_id'] = $this->dataRecord['server_id'];
526+
$app->db->datalogSave('web_database_user', 'UPDATE', 'database_user_id', $this->dataRecord['database_ro_user_id'], $user_old_rec, $user_new_rec);
527+
}
528+
}
529+
}
530+
531+
532+
parent::onBeforeInsert();
441533
}
442534

443535
function onInsertSave($sql) {

0 commit comments

Comments
 (0)