Marius Cramer
2014-10-10 2f7e607a9d5331c5d5f56dec8bc167907a08ddad
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
7fe908 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             }
T 64         }
65
66         parent::onShowNew();
67     }
68
69     function onShowEnd() {
70         global $app, $conf, $interfaceConf;
71
72         if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
73
74             // Get the limits of the client
604c0c 75             $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
cfa9da 76             $client = $app->db->queryOneRecord("SELECT default_dbserver FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
T 77
78             // Set the webserver to the default server of the client
604c0c 79             $tmp = $app->db->queryOneRecord("SELECT server_name FROM server WHERE server_id = ".$app->functions->intval($client['default_dbserver']));
7fe908 80             $app->tpl->setVar("server_id", "<option value='$client[default_dbserver]'>$tmp[server_name]</option>");
cfa9da 81             unset($tmp);
T 82
83         } elseif ($_SESSION["s"]["user"]["typ"] != 'admin' && $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
84
85             // Get the limits of the client
604c0c 86             $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
11faaa 87             $client = $app->db->queryOneRecord("SELECT client.client_id, limit_web_domain, default_webserver, contact_name FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
7fe908 88
cfa9da 89             // Set the webserver to the default server of the client
604c0c 90             $tmp = $app->db->queryOneRecord("SELECT server_name FROM server WHERE server_id = ".$app->functions->intval($client['default_webserver']));
7fe908 91             $app->tpl->setVar("server_id", "<option value='$client[default_webserver]'>$tmp[server_name]</option>");
cfa9da 92             unset($tmp);
T 93
94         } else {
95
96             // The user is admin
97             if($this->id > 0) {
98                 $server_id = $this->dataRecord["server_id"];
99             } else {
100                 // Get the first server ID
101                 $tmp = $app->db->queryOneRecord("SELECT server_id FROM server WHERE web_server = 1 ORDER BY server_name LIMIT 0,1");
102                 $server_id = $tmp['server_id'];
103             }
104
105         }
106
107         /*
108          * If the names are restricted -> remove the restriction, so that the
109          * data can be edited
110          */
7fe908 111
cfa9da 112         //* Get the database name and database user prefix
31f6ce 113         $app->uses('getconf,tools_sites');
cfa9da 114         $global_config = $app->getconf->get_global_config('sites');
31f6ce 115         $dbname_prefix = $app->tools_sites->replacePrefix($global_config['dbname_prefix'], $this->dataRecord);
7fe908 116
cfa9da 117         if ($this->dataRecord['database_name'] != ""){
T 118             /* REMOVE the restriction */
10b4c8 119             $app->tpl->setVar("database_name", $app->tools_sites->removePrefix($this->dataRecord['database_name'], $this->dataRecord['database_name_prefix'], $dbname_prefix));
cfa9da 120         }
7fe908 121
MC 122         $app->tpl->setVar("database_name_prefix", $app->tools_sites->getPrefix($this->dataRecord['database_name_prefix'], $dbname_prefix, $global_config['dbname_prefix']));
123
32f2de 124         if($this->id > 0) {
T 125             //* we are editing a existing record
6a8d0d 126             $app->tpl->setVar("edit_disabled", 1);
T 127             $app->tpl->setVar("server_id_value", $this->dataRecord["server_id"]);
128             $app->tpl->setVar("database_charset_value", $this->dataRecord["database_charset"]);
32f2de 129         } else {
6a8d0d 130             $app->tpl->setVar("edit_disabled", 0);
32f2de 131         }
cfa9da 132
T 133         parent::onShowEnd();
134     }
135
136     function onSubmit() {
137         global $app, $conf;
138
7fe908 139         $parent_domain = $app->db->queryOneRecord("select * FROM web_domain WHERE domain_id = ".$app->functions->intval(@$this->dataRecord["parent_domain_id"]) . " AND ".$app->tform->getAuthSQL('r'));
MC 140         if(!$parent_domain || $parent_domain['domain_id'] != @$this->dataRecord['parent_domain_id']) $app->tform->errorMessage .= $app->tform->lng("no_domain_perm");
141
cfa9da 142         if($_SESSION["s"]["user"]["typ"] != 'admin') {
T 143             // Get the limits of the client
604c0c 144             $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
cfa9da 145             $client = $app->db->queryOneRecord("SELECT default_dbserver, limit_database FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
T 146
147             // When the record is updated
148             if($this->id > 0) {
149                 // restore the server ID if the user is not admin and record is edited
65ea2e 150                 $tmp = $app->db->queryOneRecord("SELECT server_id FROM web_database WHERE database_id = ".$app->functions->intval($this->id));
cfa9da 151                 $this->dataRecord["server_id"] = $tmp["server_id"];
T 152                 unset($tmp);
153                 // When the record is inserted
154             } else {
155                 // set the server ID to the default dbserver of the client
156                 $this->dataRecord["server_id"] = $client["default_dbserver"];
157
158
159                 // Check if the user may add another database
160                 if($client["limit_database"] >= 0) {
161                     $tmp = $app->db->queryOneRecord("SELECT count(database_id) as number FROM web_database WHERE sys_groupid = $client_group_id");
162                     if($tmp["number"] >= $client["limit_database"]) {
163                         $app->error($app->tform->wordbook["limit_database_txt"]);
164                     }
165                 }
166
167             }
bfcdef 168         } else {
7fe908 169             // check if client of database parent domain is client of db user!
MC 170             $web_group = $app->db->queryOneRecord("SELECT sys_groupid FROM web_domain WHERE domain_id = '".$app->functions->intval($this->dataRecord['parent_domain_id'])."'");
171             if($this->dataRecord['database_user_id']) {
172                 $group = $app->db->queryOneRecord("SELECT sys_groupid FROM web_database_user WHERE database_user_id = '".$app->functions->intval($this->dataRecord['database_user_id'])."'");
173                 if($group['sys_groupid'] != $web_group['sys_groupid']) {
174                     $app->error($app->tform->wordbook['database_client_differs_txt']);
175                 }
176             }
177             if($this->dataRecord['database_ro_user_id']) {
178                 $group = $app->db->queryOneRecord("SELECT sys_groupid FROM web_database_user WHERE database_user_id = '".$app->functions->intval($this->dataRecord['database_ro_user_id'])."'");
179                 if($group['sys_groupid'] != $web_group['sys_groupid']) {
180                     $app->error($app->tform->wordbook['database_client_differs_txt']);
181                 }
182             }
183         }
cfa9da 184
T 185
186         parent::onSubmit();
187     }
188
189     function onBeforeUpdate() {
190         global $app, $conf, $interfaceConf;
191
7b47c0 192         //* Site shall not be empty
5a43e7 193         if($this->dataRecord['parent_domain_id'] == 0) $app->tform->errorMessage .= $app->tform->lng("database_site_error_empty").'<br />';
7fe908 194
cfa9da 195         //* Get the database name and database user prefix
31f6ce 196         $app->uses('getconf,tools_sites');
cfa9da 197         $global_config = $app->getconf->get_global_config('sites');
31f6ce 198         $dbname_prefix = $app->tools_sites->replacePrefix($global_config['dbname_prefix'], $this->dataRecord);
7fe908 199
cfa9da 200         //* Prevent that the database name and charset is changed
T 201         $old_record = $app->tform->getDataRecord($this->id);
7fe908 202         $dbname_prefix = $app->tools_sites->getPrefix($old_record['database_name_prefix'], $dbname_prefix);
MC 203         $this->dataRecord['database_name_prefix'] = $dbname_prefix;
204
cfa9da 205         if($old_record["database_name"] != $dbname_prefix . $this->dataRecord["database_name"]) {
T 206             $app->tform->errorMessage .= $app->tform->wordbook["database_name_change_txt"].'<br />';
207         }
208         if($old_record["database_charset"] != $this->dataRecord["database_charset"]) {
209             $app->tform->errorMessage .= $app->tform->wordbook["database_charset_change_txt"].'<br />';
210         }
7fe908 211
MC 212         if(!$this->dataRecord['database_user_id']) {
213             $app->tform->errorMessage .= $app->tform->wordbook["database_user_missing_txt"].'<br />';
214         }
215
967cd6 216         //* Database username and database name shall not be empty
e1f89d 217         if($this->dataRecord['database_name'] == '') $app->tform->errorMessage .= $app->tform->wordbook["database_name_error_empty"].'<br />';
7fe908 218
cfa9da 219         //* Check if the server has been changed
T 220         // We do this only for the admin or reseller users, as normal clients can not change the server ID anyway
221         if($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
222             if($old_record["server_id"] != $this->dataRecord["server_id"]) {
223                 //* Add a error message and switch back to old server
224                 $app->tform->errorMessage .= $app->lng('The Server can not be changed.');
225                 $this->dataRecord["server_id"] = $rec['server_id'];
226             }
227         }
228         unset($old_record);
7fe908 229
MC 230         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 />';
231
fb3a98 232         //* Check database name and user against blacklist
7fe908 233         $dbname_blacklist = array($conf['db_database'], 'mysql');
MC 234         if(in_array($dbname_prefix . $this->dataRecord['database_name'], $dbname_blacklist)) {
fb3a98 235             $app->tform->errorMessage .= $app->lng('Database name not allowed.').'<br />';
T 236         }
7fe908 237
cfa9da 238         if ($app->tform->errorMessage == ''){
T 239             /* restrict the names if there is no error */
7fe908 240             /* crop user and db names if they are too long -> mysql: user: 16 chars / db: 64 chars */
cfa9da 241             $this->dataRecord['database_name'] = substr($dbname_prefix . $this->dataRecord['database_name'], 0, 64);
T 242         }
7fe908 243
1ca823 244         //* Check for duplicates
604c0c 245         $tmp = $app->db->queryOneRecord("SELECT count(database_id) as dbnum FROM web_database WHERE database_name = '".$app->db->quote($this->dataRecord['database_name'])."' AND server_id = '".$app->functions->intval($this->dataRecord["server_id"])."' AND database_id != '".$this->id."'");
1ca823 246         if($tmp['dbnum'] > 0) $app->tform->errorMessage .= $app->lng('database_name_error_unique').'<br />';
7fe908 247
MC 248         // get the web server ip (parent domain)
604c0c 249         $tmp = $app->db->queryOneRecord("SELECT server_id FROM web_domain WHERE domain_id = '".$app->functions->intval($this->dataRecord['parent_domain_id'])."'");
7fe908 250         if($tmp['server_id'] && $tmp['server_id'] != $this->dataRecord['server_id']) {
MC 251             // we need remote access rights for this server, so get it's ip address
252             $server_config = $app->getconf->get_server_config($tmp['server_id'], 'server');
253             if($server_config['ip_address']!='') {
cc6568 254                 /*
381520 255                 if($this->dataRecord['remote_access'] != 'y') $this->dataRecord['remote_ips'] = '';
23765c 256                 $this->dataRecord['remote_access'] = 'y';
381520 257                 if(preg_match('/(^|,)' . preg_quote($server_config['ip_address'], '/') . '(,|$)/', $this->dataRecord['remote_ips']) == false) {
M 258                     $this->dataRecord['remote_ips'] .= ($this->dataRecord['remote_ips'] != '' ? ',' : '') . $server_config['ip_address'];
259                 }
cc6568 260                 */
7fe908 261
cc6568 262                 if($this->dataRecord['remote_access'] != 'y'){
H 263                     $this->dataRecord['remote_ips'] = $server_config['ip_address'];
264                     $this->dataRecord['remote_access'] = 'y';
265                 } else {
266                     if($this->dataRecord['remote_ips'] != ''){
267                         if(preg_match('/(^|,)' . preg_quote($server_config['ip_address'], '/') . '(,|$)/', $this->dataRecord['remote_ips']) == false) {
268                             $this->dataRecord['remote_ips'] .= ',' . $server_config['ip_address'];
269                         }
7fe908 270                         $tmp = preg_split('/\s*,\s*/', $this->dataRecord['remote_ips']);
MC 271                         $tmp = array_unique($tmp);
272                         $this->dataRecord['remote_ips'] = implode(',', $tmp);
273                         unset($tmp);
cc6568 274                     }
H 275                 }
7fe908 276             }
MC 277         }
4b7584 278         
MC 279         if ($app->tform->errorMessage == '') {
280             // force update of the used database user
281             if($this->dataRecord['database_user_id']) {
282                 $user_old_rec = $app->db->queryOneRecord('SELECT * FROM `web_database_user` WHERE `database_user_id` = ' . $app->functions->intval($this->dataRecord['database_user_id']));
283                 if($user_old_rec) {
284                     $user_new_rec = $user_old_rec;
285                     $user_new_rec['server_id'] = $this->dataRecord['server_id'];
286                     $app->db->datalogSave('web_database_user', 'UPDATE', 'database_user_id', $this->dataRecord['database_user_id'], $user_old_rec, $user_new_rec);
287                 }
288             }
289             if($this->dataRecord['database_ro_user_id']) {
290                 $user_old_rec = $app->db->queryOneRecord('SELECT * FROM `web_database_user` WHERE `database_user_id` = ' . $app->functions->intval($this->dataRecord['database_ro_user_id']));
291                 if($user_old_rec) {
292                     $user_new_rec = $user_old_rec;
293                     $user_new_rec['server_id'] = $this->dataRecord['server_id'];
294                     $app->db->datalogSave('web_database_user', 'UPDATE', 'database_user_id', $this->dataRecord['database_ro_user_id'], $user_old_rec, $user_new_rec);
295                 }
296             }
297         }
7fe908 298
cfa9da 299         parent::onBeforeUpdate();
T 300     }
301
302     function onBeforeInsert() {
303         global $app, $conf, $interfaceConf;
7fe908 304
5a43e7 305         //* Site shell not be empty
T 306         if($this->dataRecord['parent_domain_id'] == 0) $app->tform->errorMessage .= $app->tform->lng("database_site_error_empty").'<br />';
7fe908 307
967cd6 308         //* Database username and database name shall not be empty
e1f89d 309         if($this->dataRecord['database_name'] == '') $app->tform->errorMessage .= $app->tform->wordbook["database_name_error_empty"].'<br />';
cfa9da 310
T 311         //* Get the database name and database user prefix
31f6ce 312         $app->uses('getconf,tools_sites');
cfa9da 313         $global_config = $app->getconf->get_global_config('sites');
31f6ce 314         $dbname_prefix = $app->tools_sites->replacePrefix($global_config['dbname_prefix'], $this->dataRecord);
7fe908 315         $this->dataRecord['database_name_prefix'] = $dbname_prefix;
MC 316
317         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 />';
318
fb3a98 319         //* Check database name and user against blacklist
7fe908 320         $dbname_blacklist = array($conf['db_database'], 'mysql');
MC 321         if(in_array($dbname_prefix . $this->dataRecord['database_name'], $dbname_blacklist)) {
fb3a98 322             $app->tform->errorMessage .= $app->lng('Database name not allowed.').'<br />';
T 323         }
7fe908 324
cfa9da 325         /* restrict the names */
7fe908 326         /* crop user and db names if they are too long -> mysql: user: 16 chars / db: 64 chars */
6ae1c7 327         if ($app->tform->errorMessage == ''){
T 328             $this->dataRecord['database_name'] = substr($dbname_prefix . $this->dataRecord['database_name'], 0, 64);
329         }
7fe908 330
1ca823 331         //* Check for duplicates
604c0c 332         $tmp = $app->db->queryOneRecord("SELECT count(database_id) as dbnum FROM web_database WHERE database_name = '".$app->db->quote($this->dataRecord['database_name'])."' AND server_id = '".$app->functions->intval($this->dataRecord["server_id"])."'");
1ca823 333         if($tmp['dbnum'] > 0) $app->tform->errorMessage .= $app->tform->lng('database_name_error_unique').'<br />';
cfa9da 334
7fe908 335         // get the web server ip (parent domain)
604c0c 336         $tmp = $app->db->queryOneRecord("SELECT server_id FROM web_domain WHERE domain_id = '".$app->functions->intval($this->dataRecord['parent_domain_id'])."'");
7fe908 337         if($tmp['server_id'] && $tmp['server_id'] != $this->dataRecord['server_id']) {
MC 338             // we need remote access rights for this server, so get it's ip address
339             $server_config = $app->getconf->get_server_config($tmp['server_id'], 'server');
340             if($server_config['ip_address']!='') {
cc6568 341                 /*
381520 342                 if($this->dataRecord['remote_access'] != 'y') $this->dataRecord['remote_ips'] = '';
23765c 343                 $this->dataRecord['remote_access'] = 'y';
381520 344                 if(preg_match('/(^|,)' . preg_quote($server_config['ip_address'], '/') . '(,|$)/', $this->dataRecord['remote_ips']) == false) {
M 345                     $this->dataRecord['remote_ips'] .= ($this->dataRecord['remote_ips'] != '' ? ',' : '') . $server_config['ip_address'];
346                 }
cc6568 347                 */
7fe908 348
cc6568 349                 if($this->dataRecord['remote_access'] != 'y'){
H 350                     $this->dataRecord['remote_ips'] = $server_config['ip_address'];
351                     $this->dataRecord['remote_access'] = 'y';
352                 } else {
353                     if($this->dataRecord['remote_ips'] != ''){
354                         if(preg_match('/(^|,)' . preg_quote($server_config['ip_address'], '/') . '(,|$)/', $this->dataRecord['remote_ips']) == false) {
355                             $this->dataRecord['remote_ips'] .= ',' . $server_config['ip_address'];
356                         }
7fe908 357                         $tmp = preg_split('/\s*,\s*/', $this->dataRecord['remote_ips']);
MC 358                         $tmp = array_unique($tmp);
359                         $this->dataRecord['remote_ips'] = implode(',', $tmp);
360                         unset($tmp);
cc6568 361                     }
H 362                 }
7fe908 363             }
MC 364         }
365
4b7584 366         if ($app->tform->errorMessage == '') {
MC 367             // force update of the used database user
368             if($this->dataRecord['database_user_id']) {
369                 $user_old_rec = $app->db->queryOneRecord('SELECT * FROM `web_database_user` WHERE `database_user_id` = ' . $app->functions->intval($this->dataRecord['database_user_id']));
370                 if($user_old_rec) {
371                     $user_new_rec = $user_old_rec;
372                     $user_new_rec['server_id'] = $this->dataRecord['server_id'];
373                     $app->db->datalogSave('web_database_user', 'UPDATE', 'database_user_id', $this->dataRecord['database_user_id'], $user_old_rec, $user_new_rec);
374                 }
375             }
376             if($this->dataRecord['database_ro_user_id']) {
377                 $user_old_rec = $app->db->queryOneRecord('SELECT * FROM `web_database_user` WHERE `database_user_id` = ' . $app->functions->intval($this->dataRecord['database_ro_user_id']));
378                 if($user_old_rec) {
379                     $user_new_rec = $user_old_rec;
380                     $user_new_rec['server_id'] = $this->dataRecord['server_id'];
381                     $app->db->datalogSave('web_database_user', 'UPDATE', 'database_user_id', $this->dataRecord['database_ro_user_id'], $user_old_rec, $user_new_rec);
382                 }
383             }
384         }
385
386
cfa9da 387         parent::onBeforeInsert();
T 388     }
389
7fe908 390     function onInsertSave($sql) {
MC 391         global $app, $conf;
381520 392
7fe908 393         $app->uses('sites_database_plugin');
381520 394
7fe908 395         //$app->sites_database_plugin->processDatabaseInsert($this);
MC 396
397         $app->db->query($sql);
398         if($app->db->errorMessage != '') die($app->db->errorMessage);
399         $new_id = $app->db->insertID();
400
401         return $new_id;
402     }
403
404     function onUpdateSave($sql) {
405         global $app;
406         if(!empty($sql) && !$app->tform->isReadonlyTab($app->tform->getCurrentTab(), $this->id)) {
407
408             $app->db->query($sql);
409             if($app->db->errorMessage != '') die($app->db->errorMessage);
410         }
411     }
412
cfa9da 413     function onAfterInsert() {
T 414         global $app, $conf;
7fe908 415
2f7e60 416         $app->uses('sites_database_plugin');
MC 417         $app->sites_database_plugin->processDatabaseInsert($this);
cfa9da 418     }
T 419
420     function onAfterUpdate() {
421         global $app, $conf;
422
2f7e60 423         $app->uses('sites_database_plugin');
MC 424         $app->sites_database_plugin->processDatabaseUpdate($this);
cfa9da 425     }
T 426
427 }
428
429 $page = new page_action;
430 $page->onLoad();
431
7fe908 432 ?>