Marius Cramer
2015-08-06 37b29231e47a0c4458dc1c15d98588f16f07e1e2
commit | author | age
cfa9da 1 <?php
T 2 /*
3 Copyright (c) 2008, Till Brehm, projektfarm Gmbh
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without modification,
7 are permitted provided that the following conditions are met:
8
9     * Redistributions of source code must retain the above copyright notice,
10       this list of conditions and the following disclaimer.
11     * Redistributions in binary form must reproduce the above copyright notice,
12       this list of conditions and the following disclaimer in the documentation
13       and/or other materials provided with the distribution.
14     * Neither the name of ISPConfig nor the names of its contributors
15       may be used to endorse or promote products derived from this software without
16       specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30
31 /******************************************
32 * Begin Form configuration
33 ******************************************/
34
35 $tform_def_file = "form/database.tform.php";
36
37 /******************************************
38 * End Form configuration
39 ******************************************/
40
b1a6a5 41 require_once '../../lib/config.inc.php';
MC 42 require_once '../../lib/app.inc.php';
cfa9da 43
T 44 //* Check permissions for module
45 $app->auth->check_module_permissions('sites');
46
47 // Loading classes
48 $app->uses('tpl,tform,tform_actions');
49 $app->load('tform_actions');
50
51 class page_action extends tform_actions {
52
53     function onShowNew() {
54         global $app, $conf;
55
56         // we will check only users, not admins
57         if($_SESSION["s"]["user"]["typ"] == 'user') {
3cebc3 58             if(!$app->tform->checkClientLimit('limit_database')) {
T 59                 $app->error($app->tform->wordbook["limit_database_txt"]);
60             }
61             if(!$app->tform->checkResellerLimit('limit_database')) {
62                 $app->error('Reseller: '.$app->tform->wordbook["limit_database_txt"]);
cfa9da 63             }
41d7d1 64         } else {
SC 65             $settings = $app->getconf->get_global_config('sites');
66             $app->tform->formDef['tabs']['database']['fields']['server_id']['default'] = intval($settings['default_dbserver']);
cfa9da 67         }
T 68
69         parent::onShowNew();
70     }
71
72     function onShowEnd() {
73         global $app, $conf, $interfaceConf;
74
75         if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
76
77             // Get the limits of the client
78             $client_group_id = $_SESSION["s"]["user"]["default_group"];
323f1f 79             $client = $app->db->queryOneRecord("SELECT db_servers FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = ?", $client_group_id);
cfa9da 80
T 81             // Set the webserver to the default server of the client
cc7a82 82             $tmp = $app->db->queryAllRecords("SELECT server_id, server_name FROM server WHERE server_id IN ?", explode(',', $client['db_servers']));
bd6659 83
SJ 84             $only_one_server = count($tmp) === 1;
85             $app->tpl->setVar('only_one_server', $only_one_server);
86
87             if ($only_one_server) {
88                 $app->tpl->setVar('server_id_value', $tmp[0]['server_id']);
89             }
90
91             foreach ($tmp as $db_server) {
75b7fc 92                 $options_db_servers .= '<option value="'.$db_server['server_id'].'"'.($this->id > 0 && $this->dataRecord["server_id"] == $db_server['server_id'] ? ' selected="selected"' : '').'>'.$db_server['server_name'].'</option>';
bd6659 93             }
SJ 94
95             $app->tpl->setVar("server_id", $options_db_servers);
cfa9da 96             unset($tmp);
T 97
98         } elseif ($_SESSION["s"]["user"]["typ"] != 'admin' && $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
99
100             // Get the limits of the client
101             $client_group_id = $_SESSION["s"]["user"]["default_group"];
323f1f 102             $client = $app->db->queryOneRecord("SELECT client.client_id, limit_web_domain, db_servers, contact_name FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = ?", $client_group_id);
b1a6a5 103
cfa9da 104             // Set the webserver to the default server of the client
cc7a82 105             $tmp = $app->db->queryAllRecords("SELECT server_id, server_name FROM server WHERE server_id IN ?", explode(',', $client['db_servers']));
bd6659 106
SJ 107             $only_one_server = count($tmp) === 1;
108             $app->tpl->setVar('only_one_server', $only_one_server);
109
110             if ($only_one_server) {
111                 $app->tpl->setVar('server_id_value', $tmp[0]['server_id']);
112             }
113
114             foreach ($tmp as $db_server) {
75b7fc 115                 $options_db_servers .= '<option value="'.$db_server['server_id'].'"'.($this->id > 0 && $this->dataRecord["server_id"] == $db_server['server_id'] ? ' selected="selected"' : '').'>'.$db_server['server_name'].'</option>';
bd6659 116             }
SJ 117
118             $app->tpl->setVar("server_id", $options_db_servers);
cfa9da 119             unset($tmp);
T 120
121         } else {
122
123             // The user is admin
124             if($this->id > 0) {
125                 $server_id = $this->dataRecord["server_id"];
126             } else {
127                 // Get the first server ID
128                 $tmp = $app->db->queryOneRecord("SELECT server_id FROM server WHERE web_server = 1 ORDER BY server_name LIMIT 0,1");
129                 $server_id = $tmp['server_id'];
130             }
131
132         }
133
134         /*
135          * If the names are restricted -> remove the restriction, so that the
136          * data can be edited
137          */
b1a6a5 138
cfa9da 139         //* Get the database name and database user prefix
31f6ce 140         $app->uses('getconf,tools_sites');
cfa9da 141         $global_config = $app->getconf->get_global_config('sites');
31f6ce 142         $dbname_prefix = $app->tools_sites->replacePrefix($global_config['dbname_prefix'], $this->dataRecord);
b1a6a5 143
cfa9da 144         if ($this->dataRecord['database_name'] != ""){
T 145             /* REMOVE the restriction */
10b4c8 146             $app->tpl->setVar("database_name", $app->tools_sites->removePrefix($this->dataRecord['database_name'], $this->dataRecord['database_name_prefix'], $dbname_prefix));
cfa9da 147         }
b1a6a5 148
ba18a8 149         if($this->dataRecord['database_name'] == "") {
07c297 150             $app->tpl->setVar("database_name_prefix", $dbname_prefix);
SC 151         } else {
152             $app->tpl->setVar("database_name_prefix", $app->tools_sites->getPrefix($this->dataRecord['database_name_prefix'], $dbname_prefix, $global_config['dbname_prefix']));
153         }
b1a6a5 154
32f2de 155         if($this->id > 0) {
T 156             //* we are editing a existing record
6a8d0d 157             $app->tpl->setVar("edit_disabled", 1);
T 158             $app->tpl->setVar("server_id_value", $this->dataRecord["server_id"]);
159             $app->tpl->setVar("database_charset_value", $this->dataRecord["database_charset"]);
323f1f 160             $app->tpl->setVar("limit_database_quota", $this->dataRecord["database_quota"]);
32f2de 161         } else {
6a8d0d 162             $app->tpl->setVar("edit_disabled", 0);
32f2de 163         }
cfa9da 164
T 165         parent::onShowEnd();
166     }
167
168     function onSubmit() {
169         global $app, $conf;
170
cc7a82 171         $parent_domain = $app->db->queryOneRecord("select * FROM web_domain WHERE domain_id = ? AND ".$app->tform->getAuthSQL('r'), @$this->dataRecord["parent_domain_id"]);
b1a6a5 172         if(!$parent_domain || $parent_domain['domain_id'] != @$this->dataRecord['parent_domain_id']) $app->tform->errorMessage .= $app->tform->lng("no_domain_perm");
MC 173
cfa9da 174         if($_SESSION["s"]["user"]["typ"] != 'admin') {
T 175             // Get the limits of the client
176             $client_group_id = $_SESSION["s"]["user"]["default_group"];
9d9833 177             $client = $app->db->queryOneRecord("SELECT db_servers, limit_database, limit_database_quota, parent_client_id FROM sys_group, client WHERE sys_group.client_id = client.client_id AND sys_group.groupid = ?", $client_group_id);
cfa9da 178
T 179             // When the record is updated
180             if($this->id > 0) {
181                 // restore the server ID if the user is not admin and record is edited
323f1f 182                 $tmp = $app->db->queryOneRecord("SELECT server_id FROM web_database WHERE database_id = ?", $app->functions->intval($this->id));
cfa9da 183                 $this->dataRecord["server_id"] = $tmp["server_id"];
T 184                 unset($tmp);
323f1f 185                 //* Check client quota
FS 186                 if ($client['limit_database_quota'] >= 0) {
187                     //* get the database prefix
188                     $app->uses('getconf,tools_sites');
189                     $global_config = $app->getconf->get_global_config('sites');
190                     $dbname_prefix = $app->tools_sites->replacePrefix($global_config['dbname_prefix'], $this->dataRecord);
191                     //* get quota from other databases
192                     $tmp = $app->db->queryOneRecord("SELECT sum(database_quota) as db_quota FROM web_database WHERE sys_groupid = ? AND database_name <> ?", $client_group_id, $dbname_prefix.$this->dataRecord['database_name']);
193                     $used_quota = $app->functions->intval($tmp['db_quota']);
194                     $new_db_quota = $app->functions->intval($this->dataRecord["database_quota"]);
195                     if(($used_quota + $new_db_quota > $client['limit_database_quota']) || ($new_db_quota < 0 && $client['limit_database_quota'] >= 0)) {
196                         $max_free_quota = floor($client['limit_database_quota'] - $used_quota);
197                         if($max_free_quota < 0) {
198                             $max_free_quota = 0;
199                         }
200                         $app->tform->errorMessage .= $app->tform->lng("limit_database_quota_free_txt").": ".$max_free_quota." MB<br>";
201                         $this->dataRecord['database_quota'] = $max_free_quota;
202                     }
203                     unset($tmp);
204                     unset($global_config);
205                     unset($dbname_prefix);
206                 }
9d7676 207
SC 208                 if($client['parent_client_id'] > 0) {
209                     // Get the limits of the reseller
cc7a82 210                     $reseller = $app->db->queryOneRecord("SELECT limit_database, limit_database_quota FROM client WHERE client_id = ?", $client['parent_client_id']);
9d7676 211
SC 212                     //* Check the website quota of the client
213                     if ($reseller['limit_database_quota'] >= 0) {
214                         //* get the database prefix
215                         $app->uses('getconf,tools_sites');
216                         $global_config = $app->getconf->get_global_config('sites');
217                         $dbname_prefix = $app->tools_sites->replacePrefix($global_config['dbname_prefix'], $this->dataRecord);
218                         //* get quota from other databases
219                         $tmp = $app->db->queryOneRecord("SELECT sum(database_quota) as db_quota FROM web_database, sys_group, client WHERE web_database.sys_groupid=sys_group.groupid AND sys_group.client_id=client.client_id AND ? IN (client.parent_client_id, client.client_id) AND database_name <> ?", $client['parent_client_id'], $dbname_prefix.$this->dataRecord['database_name']);
220
221                         $used_quota = $app->functions->intval($tmp['db_quota']);
222                         $new_db_quota = $app->functions->intval($this->dataRecord["database_quota"]);
223                         if(($used_quota + $new_db_quota > $reseller["limit_database_quota"]) || ($new_db_quota < 0 && $reseller["limit_database_quota"] >= 0)) {
224                             $max_free_quota = floor($reseller["limit_database_quota"] - $used_quota);
225                             if($max_free_quota < 0) $max_free_quota = 0;
226                             $app->tform->errorMessage .= $app->tform->lng("limit_database_quota_free_txt").": ".$max_free_quota." MB<br>";
227                             $this->dataRecord["database_quota"] = $max_free_quota;
228                         }
229                         unset($tmp);
230                         unset($global_config);
231                         unset($dbname_prefix);
232                     }
233                 }
cfa9da 234                 // When the record is inserted
T 235             } else {
f19308 236                 $client['db_servers_ids'] = explode(',', $client['db_servers']);
cfa9da 237
f19308 238                 // Check if chosen server is in authorized servers for this client
SJ 239                 if (!(is_array($client['db_servers_ids']) && in_array($this->dataRecord["server_id"], $client['db_servers_ids'])) && $_SESSION["s"]["user"]["typ"] != 'admin') {
240                     $app->error($app->tform->wordbook['error_not_allowed_server_id']);
241                 }
cfa9da 242
T 243                 // Check if the user may add another database
244                 if($client["limit_database"] >= 0) {
323f1f 245                     $tmp = $app->db->queryOneRecord("SELECT count(database_id) as number FROM web_database WHERE sys_groupid = ?", $client_group_id);
cfa9da 246                     if($tmp["number"] >= $client["limit_database"]) {
T 247                         $app->error($app->tform->wordbook["limit_database_txt"]);
248                     }
249                 }
250
323f1f 251                 //* Check client quota
FS 252                 if ($client['limit_database_quota'] >= 0) {
253                     $tmp = $app->db->queryOneRecord("SELECT sum(database_quota) as db_quota FROM web_database WHERE sys_groupid = ?", $client_group_id);
0543b2 254                     $db_quota = $tmp['db_quota'];
F 255                     $new_db_quota = $app->functions->intval($this->dataRecord["database_quota"]);
256                     if(($db_quota + $new_db_quota > $client['limit_database_quota']) || ($new_db_quota < 0 && $client['limit_database_quota'] >= 0)) {
257                         $max_free_quota = floor($client['limit_database_quota'] - $db_quota);
258                         if($max_free_quota < 0) $max_free_quota = 0;
259                         $app->tform->errorMessage .= $app->tform->lng("limit_database_quota_free_txt").": ".$max_free_quota." MB<br>";
260                         $this->dataRecord['database_quota'] = $max_free_quota;
261                     }
262                     unset($tmp);
263                 }
264
cfa9da 265             }
bfcdef 266         } else {
b1a6a5 267             // check if client of database parent domain is client of db user!
cc7a82 268             $web_group = $app->db->queryOneRecord("SELECT sys_groupid FROM web_domain WHERE domain_id = ?", $this->dataRecord['parent_domain_id']);
b1a6a5 269             if($this->dataRecord['database_user_id']) {
cc7a82 270                 $group = $app->db->queryOneRecord("SELECT sys_groupid FROM web_database_user WHERE database_user_id = ?", $this->dataRecord['database_user_id']);
b1a6a5 271                 if($group['sys_groupid'] != $web_group['sys_groupid']) {
MC 272                     $app->error($app->tform->wordbook['database_client_differs_txt']);
273                 }
274             }
275             if($this->dataRecord['database_ro_user_id']) {
cc7a82 276                 $group = $app->db->queryOneRecord("SELECT sys_groupid FROM web_database_user WHERE database_user_id = ?", $this->dataRecord['database_ro_user_id']);
b1a6a5 277                 if($group['sys_groupid'] != $web_group['sys_groupid']) {
MC 278                     $app->error($app->tform->wordbook['database_client_differs_txt']);
279                 }
280             }
281         }
cfa9da 282
T 283
284         parent::onSubmit();
285     }
286
287     function onBeforeUpdate() {
288         global $app, $conf, $interfaceConf;
289
7b47c0 290         //* Site shall not be empty
5a43e7 291         if($this->dataRecord['parent_domain_id'] == 0) $app->tform->errorMessage .= $app->tform->lng("database_site_error_empty").'<br />';
b1a6a5 292
cfa9da 293         //* Get the database name and database user prefix
31f6ce 294         $app->uses('getconf,tools_sites');
cfa9da 295         $global_config = $app->getconf->get_global_config('sites');
31f6ce 296         $dbname_prefix = $app->tools_sites->replacePrefix($global_config['dbname_prefix'], $this->dataRecord);
b1a6a5 297
cfa9da 298         //* Prevent that the database name and charset is changed
T 299         $old_record = $app->tform->getDataRecord($this->id);
b1a6a5 300         $dbname_prefix = $app->tools_sites->getPrefix($old_record['database_name_prefix'], $dbname_prefix);
MC 301         $this->dataRecord['database_name_prefix'] = $dbname_prefix;
302
cfa9da 303         if($old_record["database_name"] != $dbname_prefix . $this->dataRecord["database_name"]) {
T 304             $app->tform->errorMessage .= $app->tform->wordbook["database_name_change_txt"].'<br />';
305         }
306         if($old_record["database_charset"] != $this->dataRecord["database_charset"]) {
307             $app->tform->errorMessage .= $app->tform->wordbook["database_charset_change_txt"].'<br />';
308         }
b1a6a5 309
MC 310         if(!$this->dataRecord['database_user_id']) {
311             $app->tform->errorMessage .= $app->tform->wordbook["database_user_missing_txt"].'<br />';
312         }
313
967cd6 314         //* Database username and database name shall not be empty
e1f89d 315         if($this->dataRecord['database_name'] == '') $app->tform->errorMessage .= $app->tform->wordbook["database_name_error_empty"].'<br />';
b1a6a5 316
cfa9da 317         //* Check if the server has been changed
T 318         // We do this only for the admin or reseller users, as normal clients can not change the server ID anyway
319         if($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
320             if($old_record["server_id"] != $this->dataRecord["server_id"]) {
321                 //* Add a error message and switch back to old server
322                 $app->tform->errorMessage .= $app->lng('The Server can not be changed.');
323                 $this->dataRecord["server_id"] = $rec['server_id'];
324             }
325         }
326         unset($old_record);
b1a6a5 327
MC 328         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 />';
329
fb3a98 330         //* Check database name and user against blacklist
b1a6a5 331         $dbname_blacklist = array($conf['db_database'], 'mysql');
MC 332         if(in_array($dbname_prefix . $this->dataRecord['database_name'], $dbname_blacklist)) {
fb3a98 333             $app->tform->errorMessage .= $app->lng('Database name not allowed.').'<br />';
T 334         }
b1a6a5 335
cfa9da 336         if ($app->tform->errorMessage == ''){
T 337             /* restrict the names if there is no error */
b1a6a5 338             /* crop user and db names if they are too long -> mysql: user: 16 chars / db: 64 chars */
cfa9da 339             $this->dataRecord['database_name'] = substr($dbname_prefix . $this->dataRecord['database_name'], 0, 64);
T 340         }
b1a6a5 341
1ca823 342         //* Check for duplicates
cc7a82 343         $tmp = $app->db->queryOneRecord("SELECT count(database_id) as dbnum FROM web_database WHERE database_name = ? AND server_id = ? AND database_id != ?", $this->dataRecord['database_name'], $this->dataRecord["server_id"], $this->id);
1ca823 344         if($tmp['dbnum'] > 0) $app->tform->errorMessage .= $app->lng('database_name_error_unique').'<br />';
b1a6a5 345
MC 346         // get the web server ip (parent domain)
cc7a82 347         $tmp = $app->db->queryOneRecord("SELECT server_id FROM web_domain WHERE domain_id = ?", $this->dataRecord['parent_domain_id']);
b1a6a5 348         if($tmp['server_id'] && $tmp['server_id'] != $this->dataRecord['server_id']) {
MC 349             // we need remote access rights for this server, so get it's ip address
350             $server_config = $app->getconf->get_server_config($tmp['server_id'], 'server');
351             if($server_config['ip_address']!='') {
cc6568 352                 if($this->dataRecord['remote_access'] != 'y'){
H 353                     $this->dataRecord['remote_ips'] = $server_config['ip_address'];
354                     $this->dataRecord['remote_access'] = 'y';
355                 } else {
356                     if($this->dataRecord['remote_ips'] != ''){
357                         if(preg_match('/(^|,)' . preg_quote($server_config['ip_address'], '/') . '(,|$)/', $this->dataRecord['remote_ips']) == false) {
358                             $this->dataRecord['remote_ips'] .= ',' . $server_config['ip_address'];
359                         }
b1a6a5 360                         $tmp = preg_split('/\s*,\s*/', $this->dataRecord['remote_ips']);
MC 361                         $tmp = array_unique($tmp);
362                         $this->dataRecord['remote_ips'] = implode(',', $tmp);
363                         unset($tmp);
cc6568 364                     }
H 365                 }
b1a6a5 366             }
MC 367         }
4b7584 368         
MC 369         if ($app->tform->errorMessage == '') {
370             // force update of the used database user
371             if($this->dataRecord['database_user_id']) {
cc7a82 372                 $user_old_rec = $app->db->queryOneRecord('SELECT * FROM `web_database_user` WHERE `database_user_id` = ?', $this->dataRecord['database_user_id']);
4b7584 373                 if($user_old_rec) {
MC 374                     $user_new_rec = $user_old_rec;
375                     $user_new_rec['server_id'] = $this->dataRecord['server_id'];
376                     $app->db->datalogSave('web_database_user', 'UPDATE', 'database_user_id', $this->dataRecord['database_user_id'], $user_old_rec, $user_new_rec);
377                 }
378             }
379             if($this->dataRecord['database_ro_user_id']) {
cc7a82 380                 $user_old_rec = $app->db->queryOneRecord('SELECT * FROM `web_database_user` WHERE `database_user_id` = ?', $this->dataRecord['database_ro_user_id']);
4b7584 381                 if($user_old_rec) {
MC 382                     $user_new_rec = $user_old_rec;
383                     $user_new_rec['server_id'] = $this->dataRecord['server_id'];
384                     $app->db->datalogSave('web_database_user', 'UPDATE', 'database_user_id', $this->dataRecord['database_ro_user_id'], $user_old_rec, $user_new_rec);
385                 }
386             }
387         }
b1a6a5 388
cfa9da 389         parent::onBeforeUpdate();
T 390     }
391
392     function onBeforeInsert() {
393         global $app, $conf, $interfaceConf;
b1a6a5 394
5a43e7 395         //* Site shell not be empty
T 396         if($this->dataRecord['parent_domain_id'] == 0) $app->tform->errorMessage .= $app->tform->lng("database_site_error_empty").'<br />';
b1a6a5 397
967cd6 398         //* Database username and database name shall not be empty
e1f89d 399         if($this->dataRecord['database_name'] == '') $app->tform->errorMessage .= $app->tform->wordbook["database_name_error_empty"].'<br />';
cfa9da 400
T 401         //* Get the database name and database user prefix
31f6ce 402         $app->uses('getconf,tools_sites');
cfa9da 403         $global_config = $app->getconf->get_global_config('sites');
31f6ce 404         $dbname_prefix = $app->tools_sites->replacePrefix($global_config['dbname_prefix'], $this->dataRecord);
b1a6a5 405         $this->dataRecord['database_name_prefix'] = $dbname_prefix;
MC 406
407         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 />';
408
fb3a98 409         //* Check database name and user against blacklist
b1a6a5 410         $dbname_blacklist = array($conf['db_database'], 'mysql');
MC 411         if(in_array($dbname_prefix . $this->dataRecord['database_name'], $dbname_blacklist)) {
fb3a98 412             $app->tform->errorMessage .= $app->lng('Database name not allowed.').'<br />';
T 413         }
b1a6a5 414
cfa9da 415         /* restrict the names */
b1a6a5 416         /* crop user and db names if they are too long -> mysql: user: 16 chars / db: 64 chars */
6ae1c7 417         if ($app->tform->errorMessage == ''){
T 418             $this->dataRecord['database_name'] = substr($dbname_prefix . $this->dataRecord['database_name'], 0, 64);
419         }
b1a6a5 420
1ca823 421         //* Check for duplicates
cc7a82 422         $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"]);
1ca823 423         if($tmp['dbnum'] > 0) $app->tform->errorMessage .= $app->tform->lng('database_name_error_unique').'<br />';
cfa9da 424
b1a6a5 425         // get the web server ip (parent domain)
cc7a82 426         $tmp = $app->db->queryOneRecord("SELECT server_id FROM web_domain WHERE domain_id = ?", $this->dataRecord['parent_domain_id']);
b1a6a5 427         if($tmp['server_id'] && $tmp['server_id'] != $this->dataRecord['server_id']) {
MC 428             // we need remote access rights for this server, so get it's ip address
429             $server_config = $app->getconf->get_server_config($tmp['server_id'], 'server');
430             if($server_config['ip_address']!='') {
cc6568 431                 if($this->dataRecord['remote_access'] != 'y'){
H 432                     $this->dataRecord['remote_ips'] = $server_config['ip_address'];
433                     $this->dataRecord['remote_access'] = 'y';
434                 } else {
435                     if($this->dataRecord['remote_ips'] != ''){
436                         if(preg_match('/(^|,)' . preg_quote($server_config['ip_address'], '/') . '(,|$)/', $this->dataRecord['remote_ips']) == false) {
437                             $this->dataRecord['remote_ips'] .= ',' . $server_config['ip_address'];
438                         }
b1a6a5 439                         $tmp = preg_split('/\s*,\s*/', $this->dataRecord['remote_ips']);
MC 440                         $tmp = array_unique($tmp);
441                         $this->dataRecord['remote_ips'] = implode(',', $tmp);
442                         unset($tmp);
cc6568 443                     }
H 444                 }
b1a6a5 445             }
MC 446         }
447
4b7584 448         if ($app->tform->errorMessage == '') {
MC 449             // force update of the used database user
450             if($this->dataRecord['database_user_id']) {
cc7a82 451                 $user_old_rec = $app->db->queryOneRecord('SELECT * FROM `web_database_user` WHERE `database_user_id` = ?', $this->dataRecord['database_user_id']);
4b7584 452                 if($user_old_rec) {
MC 453                     $user_new_rec = $user_old_rec;
454                     $user_new_rec['server_id'] = $this->dataRecord['server_id'];
455                     $app->db->datalogSave('web_database_user', 'UPDATE', 'database_user_id', $this->dataRecord['database_user_id'], $user_old_rec, $user_new_rec);
456                 }
457             }
458             if($this->dataRecord['database_ro_user_id']) {
cc7a82 459                 $user_old_rec = $app->db->queryOneRecord('SELECT * FROM `web_database_user` WHERE `database_user_id` = ?', $this->dataRecord['database_ro_user_id']);
4b7584 460                 if($user_old_rec) {
MC 461                     $user_new_rec = $user_old_rec;
462                     $user_new_rec['server_id'] = $this->dataRecord['server_id'];
463                     $app->db->datalogSave('web_database_user', 'UPDATE', 'database_user_id', $this->dataRecord['database_ro_user_id'], $user_old_rec, $user_new_rec);
464                 }
465             }
466         }
467
468
cfa9da 469         parent::onBeforeInsert();
T 470     }
471
b1a6a5 472     function onInsertSave($sql) {
MC 473         global $app, $conf;
381520 474
b1a6a5 475         $app->db->query($sql);
MC 476         if($app->db->errorMessage != '') die($app->db->errorMessage);
477         $new_id = $app->db->insertID();
478
479         return $new_id;
480     }
481
482     function onUpdateSave($sql) {
483         global $app;
484         if(!empty($sql) && !$app->tform->isReadonlyTab($app->tform->getCurrentTab(), $this->id)) {
485
486             $app->db->query($sql);
487             if($app->db->errorMessage != '') die($app->db->errorMessage);
488         }
489     }
490
cfa9da 491     function onAfterInsert() {
T 492         global $app, $conf;
b1a6a5 493
2f7e60 494         $app->uses('sites_database_plugin');
MC 495         $app->sites_database_plugin->processDatabaseInsert($this);
cfa9da 496     }
T 497
498     function onAfterUpdate() {
499         global $app, $conf;
500
2f7e60 501         $app->uses('sites_database_plugin');
MC 502         $app->sites_database_plugin->processDatabaseUpdate($this);
cfa9da 503     }
T 504
505 }
506
507 $page = new page_action;
508 $page->onLoad();
509
b1a6a5 510 ?>