Skip to content

Commit d83fcfe

Browse files
committed
- Added database server module.
1 parent 4cb79a5 commit d83fcfe

File tree

8 files changed

+329
-0
lines changed

8 files changed

+329
-0
lines changed

install/install.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@
183183
$inst->conf['apache']['vhost_port'] = $inst->free_query('ISPConfig Port', '8080');
184184

185185
$inst->install_ispconfig();
186+
187+
//* Configure DBServer
188+
swriteln('Configuring DBServer');
189+
$inst->configure_dbserver();
186190

187191
//* Configure ISPConfig
188192
swriteln('Installing Crontab');
@@ -306,6 +310,10 @@
306310
}
307311

308312
$inst->install_ispconfig();
313+
314+
//* Configure DBServer
315+
swriteln('Configuring DBServer');
316+
$inst->configure_dbserver();
309317

310318
//* Configure ISPConfig
311319
swriteln('Installing Crontab');

install/lib/installer_base.lib.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,30 @@ public function install_ispconfig()
755755
exec('chmod 755 '.$install_dir.'/interface/bin/php-fcgi');
756756
}
757757

758+
public function configure_dbserver()
759+
{
760+
global $conf;
761+
762+
//* If this server shall act as database server for client DB's, we configure this here
763+
$install_dir = $this->conf['ispconfig_install_dir'];
764+
765+
// Create a file with the database login details which
766+
// are used to create the client databases.
767+
768+
if(!is_dir("$install_dir/server/lib")) {
769+
$command = "mkdir $install_dir/server/lib";
770+
caselog($command.' &> /dev/null', __FILE__, __LINE__, "EXECUTED: $command", "Failed to execute the command $command");
771+
}
772+
773+
$content = rf("tpl/mysql_clientdb.conf.master");
774+
$content = str_replace('{username}',$conf['mysql']['admin_user'],$content);
775+
$content = str_replace('{password}',$conf['mysql']['admin_password'], $content);
776+
wf("$install_dir/server/lib/mysql_clientdb.conf",$content);
777+
exec('chmod 600 '."$install_dir/server/lib/mysql_clientdb.conf");
778+
exec('chown root:root '."$install_dir/server/lib/mysql_clientdb.conf");
779+
780+
}
781+
758782
public function install_crontab()
759783
{
760784
//* Root Crontab
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
$clientdb_host = 'localhost';
4+
$clientdb_user = '{username}';
5+
$clientdb_password = '{password}';
6+
7+
?>

install/update.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@
196196
//** Configure Apache
197197
swriteln('Configuring Apache');
198198
$inst->configure_apache();
199+
200+
//* Configure DBServer
201+
swriteln('Configuring DBServer');
202+
$inst->configure_dbserver();
199203
}
200204

201205
//** Configure ISPConfig

interface/web/sites/database_edit.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,20 @@ function onSubmit() {
189189
parent::onSubmit();
190190
}
191191

192+
function onUpdate() {
193+
global $app, $conf;
194+
195+
//* Prevent that the database name is changed
196+
$old_record = $app->tform->getDataRecord($this->id);
197+
if($old_record["database_name"] != $this->dataRecord["database_name"]) {
198+
$app->tform->errorMessage .= $app->tform->wordbook["database_name_change_txt"].'<br />';
199+
}
200+
unset($old_record);
201+
202+
parent::onUpdate();
203+
204+
}
205+
192206
function onAfterInsert() {
193207
global $app, $conf;
194208

interface/web/sites/lib/lang/en_database.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ $wb["database_user_error_empty"] = 'Database user is empty.';
1616
$wb["database_user_error_unique"] = 'There is already a database user with this name on the server. To get a unique name, e.g. prepend your domain name to the username.';
1717
$wb["database_user_error_regex"] = 'Invalid database user name. The username may contain these characters: a-z, A-Z, 0-9 and the underscore. Length: 2 - 64 characters.';
1818
$wb["limit_database_txt"] = 'The max. number of databases is reached.';
19+
$wb["database_name_change_txt"] = 'The database name can not be changed';
1920
?>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/*
4+
Copyright (c) 2008, Till Brehm, projektfarm Gmbh
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without modification,
8+
are permitted provided that the following conditions are met:
9+
10+
* Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
* Neither the name of ISPConfig nor the names of its contributors
16+
may be used to endorse or promote products derived from this software without
17+
specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
class database_module {
32+
33+
var $module_name = 'database_module';
34+
var $class_name = 'database_module';
35+
var $actions_available = array( 'database_insert',
36+
'database_update',
37+
'database_delete'
38+
);
39+
40+
/*
41+
This function is called when the module is loaded
42+
*/
43+
44+
function onLoad() {
45+
global $app;
46+
47+
/*
48+
Annonce the actions that where provided by this module, so plugins
49+
can register on them.
50+
*/
51+
52+
$app->plugins->announceEvents($this->module_name,$this->actions_available);
53+
54+
/*
55+
As we want to get notified of any changes on several database tables,
56+
we register for them.
57+
58+
The following function registers the function "functionname"
59+
to be executed when a record for the table "dbtable" is
60+
processed in the sys_datalog. "classname" is the name of the
61+
class that contains the function functionname.
62+
*/
63+
64+
$app->modules->registerTableHook('web_database','server_module','process');
65+
66+
// Register service
67+
//$app->services->registerService('httpd','web_module','restartHttpd');
68+
69+
}
70+
71+
/*
72+
This function is called when a change in one of the registered tables is detected.
73+
The function then raises the events for the plugins.
74+
*/
75+
76+
function process($tablename,$action,$data) {
77+
global $app;
78+
79+
switch ($tablename) {
80+
case 'web_database':
81+
if($action == 'i') $app->plugins->raiseEvent('database_insert',$data);
82+
if($action == 'u') $app->plugins->raiseEvent('database_update',$data);
83+
if($action == 'd') $app->plugins->raiseEvent('database_delete',$data);
84+
break;
85+
} // end switch
86+
} // end function
87+
88+
} // end class
89+
90+
?>
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?php
2+
3+
/*
4+
Copyright (c) 2008, Till Brehm, projektfarm Gmbh
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without modification,
8+
are permitted provided that the following conditions are met:
9+
10+
* Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
* Neither the name of ISPConfig nor the names of its contributors
16+
may be used to endorse or promote products derived from this software without
17+
specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
class mysql_clientdb {
32+
33+
var $plugin_name = 'mysql_clientdb';
34+
var $class_name = 'mysql_clientdb';
35+
36+
37+
/*
38+
This function is called when the plugin is loaded
39+
*/
40+
41+
function onLoad() {
42+
global $app;
43+
44+
/*
45+
Register for the events
46+
*/
47+
48+
//* Mailboxes
49+
$app->plugins->registerEvent('database_insert',$this->plugin_name,'db_insert');
50+
$app->plugins->registerEvent('database_update',$this->plugin_name,'db_update');
51+
$app->plugins->registerEvent('database_delete',$this->plugin_name,'db_delete');
52+
53+
54+
}
55+
56+
57+
function db_insert($event_name,$data) {
58+
global $app, $conf;
59+
60+
if($data["new"]["type"] == 'mysql') {
61+
if(!include_once(ISPC_LIB_PATH.'/mysql_clientdb.conf')) {
62+
$app->log('Unable to open'.ISPC_LIB_PATH.'/mysql_clientdb.conf',LOGLEVEL_ERROR);
63+
}
64+
65+
//* Connect to the database
66+
$link = mysql_connect($clientdb_host, $clientdb_user, $clientdb_password);
67+
if (!$link) {
68+
$app->log('Unable to connect to the database'.mysql_error($link),LOGLEVEL_ERROR);
69+
}
70+
71+
//* Create the new database
72+
if (mysql_create_db($data["new"]["database_name"]),$link) {
73+
$app->log('Created MySQL database: '.$data["new"]["database_name"],LOGLEVEL_DEBUG);
74+
} else {
75+
$app->log('Unable to connect to the database'.mysql_error($link),LOGLEVEL_ERROR);
76+
}
77+
78+
// Create the database user
79+
if($data["new"]["remote_access"] == 'y') {
80+
$db_host = '%';
81+
} else {
82+
$db_host = 'localhost';
83+
}
84+
85+
mysql_query("GRANT ALL ON ".addslashes($data["new"]["database_name"])." TO '".addslashes($data["new"]["database_user"])."'@'$db_host' IDENTIFIED BY '".addslashes($data["new"]["database_password"])."';",$link);
86+
87+
mysql_query("FLUSH PRIVILEGES;",$link);
88+
mysql_close($link);
89+
}
90+
}
91+
92+
function db_update($event_name,$data) {
93+
global $app, $conf;
94+
95+
if($data["new"]["type"] == 'mysql') {
96+
if(!include_once(ISPC_LIB_PATH.'/mysql_clientdb.conf')) {
97+
$app->log('Unable to open'.ISPC_LIB_PATH.'/mysql_clientdb.conf',LOGLEVEL_ERROR);
98+
}
99+
100+
//* Connect to the database
101+
$link = mysql_connect($clientdb_host, $clientdb_user, $clientdb_password);
102+
if (!$link) {
103+
$app->log('Unable to connect to the database'.mysql_error($link),LOGLEVEL_ERROR);
104+
}
105+
106+
//* Rename User
107+
if($data["new"]["database_user"] != $data["old"]["database_user"]) {
108+
mysql_query("RENAME USER '".addslashes($data["old"]["database_user"])."' TO '".addslashes($data["new"]["database_user"])."'",$link);
109+
$app->log('Renaming mysql user: '.$data["old"]["database_user"].' to '.$data["new"]["database_user"],LOGLEVEL_DEBUG);
110+
}
111+
112+
//* Remote access option has changed.
113+
if($data["new"]["remote_access"] != $data["old"]["remote_access"]) {
114+
if($data["new"]["remote_access"] == 'y') {
115+
mysql_query("UPDATE mysql.user SET Host = '%' WHERE User = '".addslashes($data["new"]["database_user"])."' and Host = 'localhost';",$link);
116+
} else {
117+
mysql_query("UPDATE mysql.user SET Host = 'localhost' WHERE User = '".addslashes($data["new"]["database_user"])."' and Host = '%';",$link);
118+
}
119+
$app->log('Changing mysql remote access priveliges for database: '.$data["new"]["database_name"],LOGLEVEL_DEBUG);
120+
}
121+
122+
//* Get the db host setting for the access priveliges
123+
if($data["new"]["remote_access"] == 'y') {
124+
$db_host = '%';
125+
} else {
126+
$db_host = 'localhost';
127+
}
128+
129+
/*
130+
//* Rename database
131+
if($data["new"]["database_name"] != $data["old"]["database_name"]) {
132+
mysql_query("",$link);
133+
}
134+
*/
135+
136+
//* Change password
137+
if($data["new"]["database_password"] != $data["old"]["database_password"]) {
138+
mysql_query("SET PASSWORD FOR '".addslashes($data["new"]["database_user"])."'@'$db_host' = PASSWORD('".addslashes($data["new"]["database_password"])."');",$link);
139+
$app->log('Changing mysql user password for: '.$data["new"]["database_user"],LOGLEVEL_DEBUG);
140+
}
141+
142+
mysql_query("FLUSH PRIVILEGES;",$link);
143+
mysql_close($link);
144+
}
145+
146+
}
147+
148+
function db_delete($event_name,$data) {
149+
global $app, $conf;
150+
151+
if($data["new"]["type"] == 'mysql') {
152+
if(!include_once(ISPC_LIB_PATH.'/mysql_clientdb.conf')) {
153+
$app->log('Unable to open'.ISPC_LIB_PATH.'/mysql_clientdb.conf',LOGLEVEL_ERROR);
154+
}
155+
156+
//* Connect to the database
157+
$link = mysql_connect($clientdb_host, $clientdb_user, $clientdb_password);
158+
if (!$link) {
159+
$app->log('Unable to connect to the database'.mysql_error($link),LOGLEVEL_ERROR);
160+
}
161+
162+
mysql_query("DROP USER '".addslashes($data["old"]["database_user"])."';",$link);
163+
$app->log('Dropping mysql user: '.$data["old"]["database_user"],LOGLEVEL_DEBUG);
164+
165+
mysql_drop_db($data["old"]["database_name"],$link);
166+
$app->log('Dropping mysql database: '.$data["old"]["database_name"],LOGLEVEL_DEBUG);
167+
168+
169+
mysql_query("FLUSH PRIVILEGES;",$link);
170+
mysql_close($link);
171+
}
172+
173+
174+
}
175+
176+
177+
178+
179+
} // end class
180+
181+
?>

0 commit comments

Comments
 (0)