Skip to content

Commit 57fdd61

Browse files
author
Till Brehm
committed
Merge branch '6625-dedupe-database_edit' into 'develop'
De-duplicate onBeforeUpdate and onBeforeInsert Closes #6625 See merge request ispconfig/ispconfig3!1836
2 parents 7e8a5bf + ba12d61 commit 57fdd61

File tree

1 file changed

+26
-118
lines changed

1 file changed

+26
-118
lines changed

interface/web/sites/database_edit.php

Lines changed: 26 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -306,19 +306,32 @@ 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-
//* 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 />';
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+
}
318320
}
319-
}
320-
if($old_record["database_charset"] != $this->dataRecord["database_charset"]) {
321-
$app->tform->errorMessage .= $app->tform->wordbook["database_charset_change_txt"].'<br />';
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);
322335
}
323336

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

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);
341344

342345
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 />';
343346

@@ -426,102 +429,7 @@ function onBeforeUpdate() {
426429
function onBeforeInsert() {
427430
global $app, $conf, $interfaceConf;
428431

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

527435
function onInsertSave($sql) {

0 commit comments

Comments
 (0)