Till Brehm
2016-03-29 e1b4cacb8d66891ce11dd4203bd92aa265fffcde
commit | author | age
146783 1 <?php
MC 2 /*
3 Copyright (c) 2007 - 2009, 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/web_domain.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';
146783 43
MC 44 //* Check permissions for module
45 $app->auth->check_module_permissions('sites');
46
47 // Loading classes
48 $app->uses('tpl,tform,tform_actions,tools_sites');
49 $app->load('tform_actions');
50
51 class page_action extends tform_actions {
52
53     //* Returna a "3/2/1" path hash from a numeric id '123'
7fe908 54     function id_hash($id, $levels) {
146783 55         $hash = "" . $id % 10 ;
MC 56         $id /= 10 ;
57         $levels -- ;
58         while ( $levels > 0 ) {
59             $hash .= "/" . $id % 10 ;
60             $id /= 10 ;
61             $levels-- ;
62         }
63         return $hash;
64     }
7fe908 65
146783 66     function onShowNew() {
MC 67         global $app, $conf;
68
69         // we will check only users, not admins
70         if($_SESSION["s"]["user"]["typ"] == 'user') {
7fe908 71             if(!$app->tform->checkClientLimit('limit_web_domain', "type = 'vhost'")) {
146783 72                 $app->error($app->tform->wordbook["limit_web_domain_txt"]);
MC 73             }
7fe908 74             if(!$app->tform->checkResellerLimit('limit_web_domain', "type = 'vhost'")) {
146783 75                 $app->error('Reseller: '.$app->tform->wordbook["limit_web_domain_txt"]);
MC 76             }
7fe908 77
146783 78             // Get the limits of the client
604c0c 79             $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
146783 80             $client = $app->db->queryOneRecord("SELECT client.default_webserver FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
MC 81             $app->tpl->setVar("server_id_value", $client['default_webserver']);
82         }
7fe908 83         $app->tform->formDef['tabs']['domain']['readonly'] = false;
146783 84
MC 85         parent::onShowNew();
86     }
87
88     function onShowEnd() {
89         global $app, $conf;
7fe908 90
146783 91         $app->uses('ini_parser,getconf');
MC 92
7fe908 93         $read_limits = array('limit_cgi', 'limit_ssi', 'limit_perl', 'limit_ruby', 'limit_python', 'force_suexec', 'limit_hterror', 'limit_wildcard', 'limit_ssl');
146783 94
MC 95         //* Client: If the logged in user is not admin and has no sub clients (no reseller)
96         if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
97
98             // Get the limits of the client
604c0c 99             $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
146783 100             $client = $app->db->queryOneRecord("SELECT client.limit_web_domain, client.default_webserver, client." . implode(", client.", $read_limits) . " FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
7fe908 101
146783 102             //* Get global web config
MC 103             $web_config = $app->getconf->get_server_config($client['default_webserver'], 'web');
7fe908 104
146783 105             // Set the webserver to the default server of the client
MC 106             $tmp = $app->db->queryOneRecord("SELECT server_name FROM server WHERE server_id = ".intval($client['default_webserver']));
7fe908 107             $app->tpl->setVar("server_id", "<option value='$client[default_webserver]'>$tmp[server_name]</option>");
146783 108             unset($tmp);
MC 109
30848e 110             //* Fill the IPv4 select field with the IP addresses that are allowed for this client; if website was created on a different server (e.g. by admin), display IP addresses from that server - otherwise IP will be changed to * if user changes the tab
FT 111             $sql = "SELECT ip_address FROM server_ip WHERE server_id = ".($app->functions->intval($this->dataRecord["server_id"]) > 0 && $app->functions->intval($this->dataRecord["server_id"]) != $app->functions->intval($client['default_webserver'])? $app->functions->intval($this->dataRecord["server_id"]) : $app->functions->intval($client['default_webserver']))." AND ip_type = 'IPv4' AND (client_id = 0 OR client_id=".$app->functions->intval($_SESSION['s']['user']['client_id']).")";
146783 112             $ips = $app->db->queryAllRecords($sql);
MC 113             $ip_select = ($web_config['enable_ip_wildcard'] == 'y')?"<option value='*'>*</option>":"";
30848e 114             //if(!in_array($this->dataRecord["ip_address"], $ips)) $ip_select .= "<option value='".$this->dataRecord["ip_address"]."' SELECTED>".$this->dataRecord["ip_address"]."</option>\r\n";
146783 115             //$ip_select = "";
MC 116             if(is_array($ips)) {
117                 foreach( $ips as $ip) {
118                     $selected = ($ip["ip_address"] == $this->dataRecord["ip_address"])?'SELECTED':'';
119                     $ip_select .= "<option value='$ip[ip_address]' $selected>$ip[ip_address]</option>\r\n";
120                 }
121             }
7fe908 122             $app->tpl->setVar("ip_address", $ip_select);
146783 123             unset($tmp);
MC 124             unset($ips);
7fe908 125
30848e 126             //* Fill the IPv6 select field with the IP addresses that are allowed for this client; if website was created on a different server (e.g. by admin), display IP addresses from that server
FT 127             $sql = "SELECT ip_address FROM server_ip WHERE server_id = ".($app->functions->intval($this->dataRecord["server_id"]) > 0 && $app->functions->intval($this->dataRecord["server_id"]) != $app->functions->intval($client['default_webserver'])? $app->functions->intval($this->dataRecord["server_id"]) : $app->functions->intval($client['default_webserver']))." AND ip_type = 'IPv6' AND (client_id = 0 OR client_id=".$app->functions->intval($_SESSION['s']['user']['client_id']).")";
146783 128             $ips = $app->db->queryAllRecords($sql);
MC 129             $ip_select = "<option value=''></option>";
130             //$ip_select = "";
131             if(is_array($ips)) {
132                 foreach( $ips as $ip) {
133                     $selected = ($ip["ip_address"] == $this->dataRecord["ipv6_address"])?'SELECTED':'';
134                     $ip_select .= "<option value='$ip[ip_address]' $selected>$ip[ip_address]</option>\r\n";
135                 }
136             }
7fe908 137             $app->tpl->setVar("ipv6_address", $ip_select);
146783 138             unset($tmp);
MC 139             unset($ips);
7fe908 140
146783 141             //PHP Version Selection (FastCGI)
MC 142             $server_type = 'apache';
143             if(!empty($web_config['server_type'])) $server_type = $web_config['server_type'];
144             if($server_type == 'nginx' && $this->dataRecord['php'] == 'fast-cgi') $this->dataRecord['php'] = 'php-fpm';
145             if($this->dataRecord['php'] == 'php-fpm'){
604c0c 146                 $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fpm_init_script != '' AND php_fpm_ini_dir != '' AND php_fpm_pool_dir != '' AND server_id = ".($this->id > 0 ? $app->functions->intval($this->dataRecord['server_id']) : $app->functions->intval($client['default_webserver']))." AND (client_id = 0 OR client_id=".$app->functions->intval($_SESSION['s']['user']['client_id']).")");
146783 147             }
MC 148             if($this->dataRecord['php'] == 'fast-cgi'){
604c0c 149                 $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = ".($this->id > 0 ? $app->functions->intval($this->dataRecord['server_id']) : $app->functions->intval($client['default_webserver']))." AND (client_id = 0 OR client_id=".$app->functions->intval($_SESSION['s']['user']['client_id']).")");
146783 150             }
MC 151             $php_select = "<option value=''>Default</option>";
152             if(is_array($php_records) && !empty($php_records)) {
153                 foreach( $php_records as $php_record) {
154                     if($this->dataRecord['php'] == 'php-fpm'){
155                         $php_version = $php_record['name'].':'.$php_record['php_fpm_init_script'].':'.$php_record['php_fpm_ini_dir'].':'.$php_record['php_fpm_pool_dir'];
156                     } else {
157                         $php_version = $php_record['name'].':'.$php_record['php_fastcgi_binary'].':'.$php_record['php_fastcgi_ini_dir'];
158                     }
159                     $selected = ($php_version == $this->dataRecord["fastcgi_php_version"])?'SELECTED':'';
160                     $php_select .= "<option value='$php_version' $selected>".$php_record['name']."</option>\r\n";
161                 }
162             }
7fe908 163             $app->tpl->setVar("fastcgi_php_version", $php_select);
146783 164             unset($php_records);
MC 165
7fe908 166             // add limits to template to be able to hide settings
MC 167             foreach($read_limits as $limit) $app->tpl->setVar($limit, $client[$limit]);
168
169
146783 170             //* Reseller: If the logged in user is not admin and has sub clients (is a reseller)
MC 171         } elseif ($_SESSION["s"]["user"]["typ"] != 'admin' && $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
172
173             // Get the limits of the client
604c0c 174             $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
146783 175             $client = $app->db->queryOneRecord("SELECT client.client_id, client.limit_web_domain, client.default_webserver, client.contact_name, CONCAT(IF(client.company_name != '', CONCAT(client.company_name, ' :: '), ''), client.contact_name, ' (', client.username, IF(client.customer_no != '', CONCAT(', ', client.customer_no), ''), ')') as contactname, sys_group.name, client." . implode(", client.", $read_limits) . " FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
7fe908 176
146783 177             //* Get global web config
MC 178             $web_config = $app->getconf->get_server_config($client['default_webserver'], 'web');
7fe908 179
146783 180             // Set the webserver to the default server of the client
604c0c 181             $tmp = $app->db->queryOneRecord("SELECT server_name FROM server WHERE server_id = ".$app->functions->intval($client['default_webserver']));
7fe908 182             $app->tpl->setVar("server_id", "<option value='$client[default_webserver]'>$tmp[server_name]</option>");
146783 183             unset($tmp);
MC 184
185             // Fill the client select field
ca4b71 186             $sql = "SELECT sys_group.groupid, sys_group.name, CONCAT(IF(client.company_name != '', CONCAT(client.company_name, ' :: '), ''), client.contact_name, ' (', client.username, IF(client.customer_no != '', CONCAT(', ', client.customer_no), ''), ')') as contactname FROM sys_group, client WHERE sys_group.client_id = client.client_id AND client.parent_client_id = ".$client['client_id']." ORDER BY client.company_name, client.contact_name, sys_group.name";
146783 187             $records = $app->db->queryAllRecords($sql);
604c0c 188             $tmp = $app->db->queryOneRecord("SELECT groupid FROM sys_group WHERE client_id = ".$app->functions->intval($client['client_id']));
146783 189             $client_select = '<option value="'.$tmp['groupid'].'">'.$client['contactname'].'</option>';
MC 190             //$tmp_data_record = $app->tform->getDataRecord($this->id);
191             if(is_array($records)) {
192                 $selected_client_group_id = 0; // needed to get list of PHP versions
193                 foreach( $records as $rec) {
194                     if(is_array($this->dataRecord) && ($rec["groupid"] == $this->dataRecord['client_group_id'] || $rec["groupid"] == $this->dataRecord['sys_groupid']) && !$selected_client_group_id) $selected_client_group_id = $rec["groupid"];
195                     $selected = @(is_array($this->dataRecord) && ($rec["groupid"] == $this->dataRecord['client_group_id'] || $rec["groupid"] == $this->dataRecord['sys_groupid']))?'SELECTED':'';
196                     if($selected == 'SELECTED') $selected_client_group_id = $rec["groupid"];
197                     $client_select .= "<option value='$rec[groupid]' $selected>$rec[contactname]</option>\r\n";
198                 }
199             }
7fe908 200             $app->tpl->setVar("client_group_id", $client_select);
146783 201
30848e 202             //* Fill the IPv4 select field with the IP addresses that are allowed for this client; if website was created on a different server (e.g. by admin), display IP addresses from that server - otherwise IP will be changed to * if user changes the tab
FT 203             $sql = "SELECT ip_address FROM server_ip WHERE server_id = ".($app->functions->intval($this->dataRecord["server_id"]) > 0 && $app->functions->intval($this->dataRecord["server_id"]) != $app->functions->intval($client['default_webserver'])? $app->functions->intval($this->dataRecord["server_id"]) : $app->functions->intval($client['default_webserver']))." AND ip_type = 'IPv4' AND (client_id = 0 OR client_id=".$app->functions->intval($_SESSION['s']['user']['client_id']).")";
146783 204             $ips = $app->db->queryAllRecords($sql);
MC 205             $ip_select = ($web_config['enable_ip_wildcard'] == 'y')?"<option value='*'>*</option>":"";
30848e 206             //if(!in_array($this->dataRecord["ip_address"], $ips)) $ip_select .= "<option value='".$this->dataRecord["ip_address"]."' SELECTED>".$this->dataRecord["ip_address"]."</option>\r\n";
146783 207             //$ip_select = "";
MC 208             if(is_array($ips)) {
209                 foreach( $ips as $ip) {
210                     $selected = ($ip["ip_address"] == $this->dataRecord["ip_address"])?'SELECTED':'';
211                     $ip_select .= "<option value='$ip[ip_address]' $selected>$ip[ip_address]</option>\r\n";
212                 }
213             }
7fe908 214             $app->tpl->setVar("ip_address", $ip_select);
146783 215             unset($tmp);
MC 216             unset($ips);
7fe908 217
30848e 218             //* Fill the IPv6 select field with the IP addresses that are allowed for this client; if website was created on a different server (e.g. by admin), display IP addresses from that server
FT 219             $sql = "SELECT ip_address FROM server_ip WHERE server_id = ".($app->functions->intval($this->dataRecord["server_id"]) > 0 && $app->functions->intval($this->dataRecord["server_id"]) != $app->functions->intval($client['default_webserver'])? $app->functions->intval($this->dataRecord["server_id"]) : $app->functions->intval($client['default_webserver']))." AND ip_type = 'IPv6' AND (client_id = 0 OR client_id=".$app->functions->intval($_SESSION['s']['user']['client_id']).")";
146783 220             $ips = $app->db->queryAllRecords($sql);
MC 221             $ip_select = "<option value=''></option>";
222             //$ip_select = "";
223             if(is_array($ips)) {
224                 foreach( $ips as $ip) {
225                     $selected = ($ip["ip_address"] == $this->dataRecord["ipv6_address"])?'SELECTED':'';
226                     $ip_select .= "<option value='$ip[ip_address]' $selected>$ip[ip_address]</option>\r\n";
227                 }
228             }
7fe908 229             $app->tpl->setVar("ipv6_address", $ip_select);
146783 230             unset($tmp);
MC 231             unset($ips);
7fe908 232
146783 233             //PHP Version Selection (FastCGI)
MC 234             $server_type = 'apache';
235             if(!empty($web_config['server_type'])) $server_type = $web_config['server_type'];
236             if($server_type == 'nginx' && $this->dataRecord['php'] == 'fast-cgi') $this->dataRecord['php'] = 'php-fpm';
604c0c 237             $selected_client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE groupid = ".$app->functions->intval($selected_client_group_id));
146783 238             //$sql_where = " AND (client_id = 0 OR client_id=".$_SESSION['s']['user']['client_id']." OR client_id = ".intval($selected_client['client_id']).")";
MC 239             $sql_where = " AND (client_id = 0 OR client_id = ".intval($selected_client['client_id']).")";
240             if($this->dataRecord['php'] == 'php-fpm'){
604c0c 241                 $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fpm_init_script != '' AND php_fpm_ini_dir != '' AND php_fpm_pool_dir != '' AND server_id = ".($this->id > 0 ? $app->functions->intval($this->dataRecord['server_id']) : $app->functions->intval($client['default_webserver'])).$sql_where);
146783 242             }
MC 243             if($this->dataRecord['php'] == 'fast-cgi') {
604c0c 244                 $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = ".($this->id > 0 ? $app->functions->intval($this->dataRecord['server_id']) : $app->functions->intval($client['default_webserver'])).$sql_where);
146783 245             }
MC 246             $php_select = "<option value=''>Default</option>";
247             if(is_array($php_records) && !empty($php_records)) {
248                 foreach( $php_records as $php_record) {
249                     if($this->dataRecord['php'] == 'php-fpm'){
250                         $php_version = $php_record['name'].':'.$php_record['php_fpm_init_script'].':'.$php_record['php_fpm_ini_dir'].':'.$php_record['php_fpm_pool_dir'];
251                     } else {
252                         $php_version = $php_record['name'].':'.$php_record['php_fastcgi_binary'].':'.$php_record['php_fastcgi_ini_dir'];
253                     }
254                     $selected = ($php_version == $this->dataRecord["fastcgi_php_version"])?'SELECTED':'';
255                     $php_select .= "<option value='$php_version' $selected>".$php_record['name']."</option>\r\n";
256                 }
257             }
7fe908 258             $app->tpl->setVar("fastcgi_php_version", $php_select);
146783 259             unset($php_records);
7fe908 260
MC 261             // add limits to template to be able to hide settings
262             foreach($read_limits as $limit) $app->tpl->setVar($limit, $client[$limit]);
263
264
146783 265             //* Admin: If the logged in user is admin
MC 266         } else {
267
268             // The user is admin, so we fill in all IP addresses of the server
269             if($this->id > 0) {
270                 if(!isset($this->dataRecord["server_id"])){
271                     $tmp = $app->db->queryOneRecord("SELECT server_id FROM web_domain WHERE domain_id = ".$app->functions->intval($this->id));
272                     $this->dataRecord["server_id"] = $tmp["server_id"];
273                     unset($tmp);
274                 }
275                 $server_id = intval(@$this->dataRecord["server_id"]);
276             } else {
277                 // Get the first server ID
278                 $tmp = $app->db->queryOneRecord("SELECT server_id FROM server WHERE web_server = 1 ORDER BY server_name LIMIT 0,1");
279                 $server_id = intval($tmp['server_id']);
280             }
7fe908 281
146783 282             //* get global web config
MC 283             $web_config = $app->getconf->get_server_config($server_id, 'web');
7fe908 284
146783 285             //* Fill the IPv4 select field
604c0c 286             $sql = "SELECT ip_address FROM server_ip WHERE ip_type = 'IPv4' AND server_id = ".$app->functions->intval($server_id);
146783 287             $ips = $app->db->queryAllRecords($sql);
MC 288             $ip_select = ($web_config['enable_ip_wildcard'] == 'y')?"<option value='*'>*</option>":"";
289             //$ip_select = "";
290             if(is_array($ips)) {
291                 foreach( $ips as $ip) {
292                     $selected = ($ip["ip_address"] == $this->dataRecord["ip_address"])?'SELECTED':'';
293                     $ip_select .= "<option value='$ip[ip_address]' $selected>$ip[ip_address]</option>\r\n";
294                 }
295             }
7fe908 296             $app->tpl->setVar("ip_address", $ip_select);
146783 297             unset($tmp);
MC 298             unset($ips);
7fe908 299
146783 300             //* Fill the IPv6 select field
604c0c 301             $sql = "SELECT ip_address FROM server_ip WHERE ip_type = 'IPv6' AND server_id = ".$app->functions->intval($server_id);
146783 302             $ips = $app->db->queryAllRecords($sql);
MC 303             $ip_select = "<option value=''></option>";
304             //$ip_select = "";
305             if(is_array($ips)) {
306                 foreach( $ips as $ip) {
307                     $selected = ($ip["ip_address"] == $this->dataRecord["ipv6_address"])?'SELECTED':'';
308                     $ip_select .= "<option value='$ip[ip_address]' $selected>$ip[ip_address]</option>\r\n";
309                 }
310             }
7fe908 311             $app->tpl->setVar("ipv6_address", $ip_select);
146783 312             unset($tmp);
MC 313             unset($ips);
314
315             // Fill the client select field
ca4b71 316             $sql = "SELECT sys_group.groupid, sys_group.name, CONCAT(IF(client.company_name != '', CONCAT(client.company_name, ' :: '), ''), client.contact_name, ' (', client.username, IF(client.customer_no != '', CONCAT(', ', client.customer_no), ''), ')') as contactname FROM sys_group, client WHERE sys_group.client_id = client.client_id AND sys_group.client_id > 0 ORDER BY client.company_name, client.contact_name, sys_group.name";
146783 317             $clients = $app->db->queryAllRecords($sql);
MC 318             $client_select = "<option value='0'></option>";
319             //$tmp_data_record = $app->tform->getDataRecord($this->id);
320             if(is_array($clients)) {
321                 $selected_client_group_id = 0; // needed to get list of PHP versions
322                 foreach($clients as $client) {
323                     if(is_array($this->dataRecord) && ($client["groupid"] == $this->dataRecord['client_group_id'] || $client["groupid"] == $this->dataRecord['sys_groupid']) && !$selected_client_group_id) $selected_client_group_id = $client["groupid"];
324                     //$selected = @($client["groupid"] == $tmp_data_record["sys_groupid"])?'SELECTED':'';
325                     $selected = @(is_array($this->dataRecord) && ($client["groupid"] == $this->dataRecord['client_group_id'] || $client["groupid"] == $this->dataRecord['sys_groupid']))?'SELECTED':'';
326                     if($selected == 'SELECTED') $selected_client_group_id = $client["groupid"];
327                     $client_select .= "<option value='$client[groupid]' $selected>$client[contactname]</option>\r\n";
328                 }
329             }
7fe908 330             $app->tpl->setVar("client_group_id", $client_select);
MC 331
146783 332             //PHP Version Selection (FastCGI)
MC 333             $server_type = 'apache';
334             if(!empty($web_config['server_type'])) $server_type = $web_config['server_type'];
335             if($server_type == 'nginx' && $this->dataRecord['php'] == 'fast-cgi') $this->dataRecord['php'] = 'php-fpm';
604c0c 336             $selected_client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE groupid = ".$app->functions->intval($selected_client_group_id));
146783 337             //$sql_where = " AND (client_id = 0 OR client_id=".$_SESSION['s']['user']['client_id']." OR client_id = ".intval($selected_client['client_id']).")";
604c0c 338             $sql_where = " AND (client_id = 0 OR client_id = ".$app->functions->intval($selected_client['client_id']).")";
146783 339             if($this->dataRecord['php'] == 'php-fpm'){
MC 340                 $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fpm_init_script != '' AND php_fpm_ini_dir != '' AND php_fpm_pool_dir != '' AND server_id = $server_id".$sql_where);
341             }
342             if($this->dataRecord['php'] == 'fast-cgi') {
604c0c 343                 $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = ".$app->functions->intval($server_id).$sql_where);
146783 344             }
MC 345             $php_select = "<option value=''>Default</option>";
346             if(is_array($php_records) && !empty($php_records)) {
347                 foreach( $php_records as $php_record) {
348                     if($this->dataRecord['php'] == 'php-fpm'){
349                         $php_version = $php_record['name'].':'.$php_record['php_fpm_init_script'].':'.$php_record['php_fpm_ini_dir'].':'.$php_record['php_fpm_pool_dir'];
350                     } else {
351                         $php_version = $php_record['name'].':'.$php_record['php_fastcgi_binary'].':'.$php_record['php_fastcgi_ini_dir'];
352                     }
353                     $selected = ($php_version == $this->dataRecord["fastcgi_php_version"])?'SELECTED':'';
354                     $php_select .= "<option value='$php_version' $selected>".$php_record['name']."</option>\r\n";
355                 }
356             }
7fe908 357             $app->tpl->setVar("fastcgi_php_version", $php_select);
146783 358             unset($php_records);
7fe908 359
MC 360             foreach($read_limits as $limit) $app->tpl->setVar($limit, ($limit == 'force_suexec' ? 'n' : 'y'));
361
146783 362             // Directive Snippets
MC 363             $php_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'php' AND active = 'y'");
364             $php_directive_snippets_txt = '';
365             if(is_array($php_directive_snippets) && !empty($php_directive_snippets)){
7fe908 366                 foreach($php_directive_snippets as $php_directive_snippet){
MC 367                     $php_directive_snippets_txt .= '<a href="javascript:void(0);" class="addPlaceholderContent">['.$php_directive_snippet['name'].']<pre class="addPlaceholderContent" style="display:none;">'.htmlentities($php_directive_snippet['snippet']).'</pre></a> ';
368                 }
146783 369             }
MC 370             if($php_directive_snippets_txt == '') $php_directive_snippets_txt = '------';
7fe908 371             $app->tpl->setVar("php_directive_snippets_txt", $php_directive_snippets_txt);
MC 372
146783 373             if($server_type == 'apache'){
MC 374                 $apache_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'apache' AND active = 'y'");
375                 $apache_directive_snippets_txt = '';
376                 if(is_array($apache_directive_snippets) && !empty($apache_directive_snippets)){
7fe908 377                     foreach($apache_directive_snippets as $apache_directive_snippet){
MC 378                         $apache_directive_snippets_txt .= '<a href="javascript:void(0);" class="addPlaceholderContent">['.$apache_directive_snippet['name'].']<pre class="addPlaceholderContent" style="display:none;">'.htmlentities($apache_directive_snippet['snippet']).'</pre></a> ';
379                     }
146783 380                 }
MC 381                 if($apache_directive_snippets_txt == '') $apache_directive_snippets_txt = '------';
7fe908 382                 $app->tpl->setVar("apache_directive_snippets_txt", $apache_directive_snippets_txt);
146783 383             }
7fe908 384
146783 385             if($server_type == 'nginx'){
MC 386                 $nginx_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'nginx' AND active = 'y'");
387                 $nginx_directive_snippets_txt = '';
388                 if(is_array($nginx_directive_snippets) && !empty($nginx_directive_snippets)){
7fe908 389                     foreach($nginx_directive_snippets as $nginx_directive_snippet){
MC 390                         $nginx_directive_snippets_txt .= '<a href="javascript:void(0);" class="addPlaceholderContent">['.$nginx_directive_snippet['name'].']<pre class="addPlaceholderContent" style="display:none;">'.htmlentities($nginx_directive_snippet['snippet']).'</pre></a> ';
391                     }
146783 392                 }
MC 393                 if($nginx_directive_snippets_txt == '') $nginx_directive_snippets_txt = '------';
7fe908 394                 $app->tpl->setVar("nginx_directive_snippets_txt", $nginx_directive_snippets_txt);
146783 395             }
7fe908 396
146783 397             $proxy_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'proxy' AND active = 'y'");
MC 398             $proxy_directive_snippets_txt = '';
399             if(is_array($proxy_directive_snippets) && !empty($proxy_directive_snippets)){
7fe908 400                 foreach($proxy_directive_snippets as $proxy_directive_snippet){
MC 401                     $proxy_directive_snippets_txt .= '<a href="javascript:void(0);" class="addPlaceholderContent">['.$proxy_directive_snippet['name'].']<pre class="addPlaceholderContent" style="display:none;">'.htmlentities($proxy_directive_snippet['snippet']).'</pre></a> ';
402                 }
146783 403             }
MC 404             if($proxy_directive_snippets_txt == '') $proxy_directive_snippets_txt = '------';
7fe908 405             $app->tpl->setVar("proxy_directive_snippets_txt", $proxy_directive_snippets_txt);
146783 406         }
MC 407
408         $ssl_domain_select = '';
d96e29 409         $ssl_domains = array();
MC 410         $tmpd = $app->db->queryAllRecords("SELECT domain, type FROM web_domain WHERE domain_id = ".$this->id." OR parent_domain_id = ".$this->id);
411         foreach($tmpd as $tmp) {
90923d 412             if($tmp['type'] == 'subdomain' || $tmp['type'] == 'vhostsubdomain') {
d96e29 413                 $ssl_domains[] = $tmp["domain"];
MC 414             } else {
415                 $ssl_domains = array_merge($ssl_domains, array($tmp["domain"],'www.'.$tmp["domain"],'*.'.$tmp["domain"]));
416             }
417         }
146783 418         if(is_array($ssl_domains)) {
MC 419             foreach( $ssl_domains as $ssl_domain) {
420                 $selected = ($ssl_domain == $this->dataRecord['ssl_domain'])?'SELECTED':'';
421                 $ssl_domain_select .= "<option value='$ssl_domain' $selected>$ssl_domain</option>\r\n";
422             }
423         }
7fe908 424         $app->tpl->setVar("ssl_domain", $ssl_domain_select);
146783 425         unset($ssl_domain_select);
MC 426         unset($ssl_domains);
427         unset($ssl_domain);
428
429         if($this->id > 0) {
430             //* we are editing a existing record
431             $app->tpl->setVar("edit_disabled", 1);
432             $app->tpl->setVar("server_id_value", $this->dataRecord["server_id"]);
433         } else {
434             $app->tpl->setVar("edit_disabled", 0);
435         }
436
437         $tmp_txt = ($this->dataRecord['traffic_quota_lock'] == 'y')?'<b>('.$app->tform->lng('traffic_quota_exceeded_txt').')</b>':'';
438         $app->tpl->setVar("traffic_quota_exceeded_txt", $tmp_txt);
439
440         /*
441          * Now we have to check, if we should use the domain-module to select the domain
442          * or not
443          */
444         $settings = $app->getconf->get_global_config('domains');
445         if ($settings['use_domain_module'] == 'y') {
446             /*
447              * The domain-module is in use.
448             */
449             $domains = $app->tools_sites->getDomainModuleDomains();
450             $domain_select = '';
451             if(is_array($domains) && sizeof($domains) > 0) {
452                 /* We have domains in the list, so create the drop-down-list */
453                 foreach( $domains as $domain) {
454                     $domain_select .= "<option value=" . $domain['domain_id'] ;
455                     if ($domain['domain'] == $this->dataRecord["domain"]) {
456                         $domain_select .= " selected";
457                     }
458                     $domain_select .= ">" . $app->functions->idn_decode($domain['domain']) . "</option>\r\n";
459                 }
460             }
461             else {
462                 /*
463                  * We have no domains in the domain-list. This means, we can not add ANY new domain.
464                  * To avoid, that the variable "domain_option" is empty and so the user can
465                  * free enter a domain, we have to create a empty option!
466                 */
467                 $domain_select .= "<option value=''></option>\r\n";
468             }
7fe908 469             $app->tpl->setVar("domain_option", $domain_select);
146783 470         }
7fe908 471
146783 472         // check for configuration errors in sys_datalog
MC 473         if($this->id > 0) {
604c0c 474             $datalog = $app->db->queryOneRecord("SELECT sys_datalog.error, sys_log.tstamp FROM sys_datalog, sys_log WHERE sys_datalog.dbtable = 'web_domain' AND sys_datalog.dbidx = 'domain_id:".$app->functions->intval($this->id)."' AND sys_datalog.datalog_id = sys_log.datalog_id AND sys_log.message = CONCAT('Processed datalog_id ',sys_log.datalog_id) ORDER BY sys_datalog.tstamp DESC");
146783 475             if(is_array($datalog) && !empty($datalog)){
MC 476                 if(trim($datalog['error']) != ''){
7fe908 477                     $app->tpl->setVar("config_error_msg", nl2br(htmlentities($datalog['error'])));
MC 478                     $app->tpl->setVar("config_error_tstamp", date($app->lng('conf_format_datetime'), $datalog['tstamp']));
146783 479                 }
MC 480             }
481         }
482
483         parent::onShowEnd();
484     }
7fe908 485
MC 486     function onShowEdit() {
487         global $app;
488         if($app->tform->checkPerm($this->id, 'riud')) $app->tform->formDef['tabs']['domain']['readonly'] = false;
489         parent::onShowEdit();
490     }
146783 491
MC 492     function onSubmit() {
493         global $app, $conf;
494
7fe908 495         /* check if the domain module is used - and check if the selected domain can be used! */
146783 496         if($app->tform->getCurrentTab() == 'domain') {
7fe908 497             $app->uses('ini_parser,getconf');
MC 498             $settings = $app->getconf->get_global_config('domains');
499             if ($settings['use_domain_module'] == 'y') {
500                 $domain_check = $app->tools_sites->checkDomainModuleDomain($this->dataRecord['domain']);
501                 if(!$domain_check) {
502                     // invalid domain selected
503                     $app->tform->errorMessage .= $app->tform->lng("domain_error_empty")."<br />";
504                 } else {
505                     $this->dataRecord['domain'] = $domain_check;
506                 }
507             }
508         }
509
146783 510         // nginx: if redirect type is proxy and redirect path is no URL, display error
MC 511         //if($this->dataRecord["redirect_type"] == 'proxy' && substr($this->dataRecord['redirect_path'],0,1) == '/'){
7fe908 512         // $app->tform->errorMessage .= $app->tform->lng("error_proxy_requires_url")."<br />";
146783 513         //}
MC 514
515         // Set a few fixed values
516         $this->dataRecord["parent_domain_id"] = 0;
517         $this->dataRecord["type"] = 'vhost';
518         $this->dataRecord["vhost_type"] = 'name';
519
7fe908 520         $read_limits = array('limit_cgi', 'limit_ssi', 'limit_perl', 'limit_ruby', 'limit_python', 'force_suexec', 'limit_hterror', 'limit_wildcard', 'limit_ssl');
146783 521
MC 522
523         if($_SESSION["s"]["user"]["typ"] != 'admin') {
524             // Get the limits of the client
604c0c 525             $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
146783 526             $client = $app->db->queryOneRecord("SELECT limit_traffic_quota, limit_web_domain, default_webserver, parent_client_id, limit_web_quota, client." . implode(", client.", $read_limits) . " FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
7fe908 527
31143b 528             if($client['limit_cgi'] != 'y') $this->dataRecord['cgi'] = 'n';
TB 529             if($client['limit_ssi'] != 'y') $this->dataRecord['ssi'] = 'n';
530             if($client['limit_perl'] != 'y') $this->dataRecord['perl'] = 'n';
531             if($client['limit_ruby'] != 'y') $this->dataRecord['ruby'] = 'n';
532             if($client['limit_python'] != 'y') $this->dataRecord['python'] = 'n';
7fe908 533             if($client['force_suexec'] == 'y') $this->dataRecord['suexec'] = 'y';
31143b 534             if($client['limit_hterror'] != 'y') $this->dataRecord['errordocs'] = 'n';
TB 535             if($client['limit_wildcard'] != 'y' && $this->dataRecord['subdomain'] == '*') $this->dataRecord['subdomain'] = 'n';
536             if($client['limit_ssl'] != 'y') $this->dataRecord['ssl'] = 'n';
7fe908 537
146783 538             // only generate quota and traffic warnings if value has changed
MC 539             if($this->id > 0) {
540                 $old_web_values = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ".$app->functions->intval($this->id));
1c3655 541             } else {
f9c424 542                 $old_web_values = array();
146783 543             }
7fe908 544
146783 545             //* Check the website quota of the client
MC 546             if(isset($_POST["hd_quota"]) && $client["limit_web_quota"] >= 0 && $_POST["hd_quota"] != $old_web_values["hd_quota"]) {
547                 $tmp = $app->db->queryOneRecord("SELECT sum(hd_quota) as webquota FROM web_domain WHERE domain_id != ".$app->functions->intval($this->id)." AND type = 'vhost' AND ".$app->tform->getAuthSQL('u'));
548                 $webquota = $tmp["webquota"];
549                 $new_web_quota = $app->functions->intval($this->dataRecord["hd_quota"]);
550                 if(($webquota + $new_web_quota > $client["limit_web_quota"]) || ($new_web_quota < 0 && $client["limit_web_quota"] >= 0)) {
551                     $max_free_quota = floor($client["limit_web_quota"] - $webquota);
552                     if($max_free_quota < 0) $max_free_quota = 0;
553                     $app->tform->errorMessage .= $app->tform->lng("limit_web_quota_free_txt").": ".$max_free_quota." MB<br>";
554                     // Set the quota field to the max free space
555                     $this->dataRecord["hd_quota"] = $max_free_quota;
556                 }
557                 unset($tmp);
558                 unset($tmp_quota);
559             }
560
561             //* Check the traffic quota of the client
562             if(isset($_POST["traffic_quota"]) && $client["limit_traffic_quota"] > 0 && $_POST["traffic_quota"] != $old_web_values["traffic_quota"]) {
563                 $tmp = $app->db->queryOneRecord("SELECT sum(traffic_quota) as trafficquota FROM web_domain WHERE domain_id != ".$app->functions->intval($this->id)." AND ".$app->tform->getAuthSQL('u'));
564                 $trafficquota = $tmp["trafficquota"];
565                 $new_traffic_quota = $app->functions->intval($this->dataRecord["traffic_quota"]);
566                 if(($trafficquota + $new_traffic_quota > $client["limit_traffic_quota"]) || ($new_traffic_quota < 0 && $client["limit_traffic_quota"] >= 0)) {
567                     $max_free_quota = floor($client["limit_traffic_quota"] - $trafficquota);
568                     if($max_free_quota < 0) $max_free_quota = 0;
569                     $app->tform->errorMessage .= $app->tform->lng("limit_traffic_quota_free_txt").": ".$max_free_quota." MB<br>";
570                     // Set the quota field to the max free space
571                     $this->dataRecord["traffic_quota"] = $max_free_quota;
572                 }
573                 unset($tmp);
574                 unset($tmp_quota);
575             }
7fe908 576
146783 577             if($client['parent_client_id'] > 0) {
MC 578                 // Get the limits of the reseller
604c0c 579                 $reseller = $app->db->queryOneRecord("SELECT limit_traffic_quota, limit_web_domain, default_webserver, limit_web_quota FROM client WHERE client_id = ".$app->functions->intval($client['parent_client_id']));
146783 580
MC 581                 //* Check the website quota of the client
582                 if(isset($_POST["hd_quota"]) && $reseller["limit_web_quota"] >= 0 && $_POST["hd_quota"] != $old_web_values["hd_quota"]) {
583                     $tmp = $app->db->queryOneRecord("SELECT sum(hd_quota) as webquota FROM web_domain WHERE domain_id != ".$app->functions->intval($this->id)." AND type = 'vhost' AND ".$app->tform->getAuthSQL('u'));
584                     $webquota = $tmp["webquota"];
585                     $new_web_quota = $app->functions->intval($this->dataRecord["hd_quota"]);
586                     if(($webquota + $new_web_quota > $reseller["limit_web_quota"]) || ($new_web_quota < 0 && $reseller["limit_web_quota"] >= 0)) {
587                         $max_free_quota = floor($reseller["limit_web_quota"] - $webquota);
588                         if($max_free_quota < 0) $max_free_quota = 0;
589                         $app->tform->errorMessage .= $app->tform->lng("limit_web_quota_free_txt").": ".$max_free_quota." MB<br>";
590                         // Set the quota field to the max free space
591                         $this->dataRecord["hd_quota"] = $max_free_quota;
592                     }
593                     unset($tmp);
594                     unset($tmp_quota);
595                 }
596
597                 //* Check the traffic quota of the client
598                 if(isset($_POST["traffic_quota"]) && $reseller["limit_traffic_quota"] > 0 && $_POST["traffic_quota"] != $old_web_values["traffic_quota"]) {
599                     $tmp = $app->db->queryOneRecord("SELECT sum(traffic_quota) as trafficquota FROM web_domain WHERE domain_id != ".$app->functions->intval($this->id)." AND ".$app->tform->getAuthSQL('u'));
600                     $trafficquota = $tmp["trafficquota"];
601                     $new_traffic_quota = $app->functions->intval($this->dataRecord["traffic_quota"]);
602                     if(($trafficquota + $new_traffic_quota > $reseller["limit_traffic_quota"]) || ($new_traffic_quota < 0 && $reseller["limit_traffic_quota"] >= 0)) {
603                         $max_free_quota = floor($reseller["limit_traffic_quota"] - $trafficquota);
604                         if($max_free_quota < 0) $max_free_quota = 0;
605                         $app->tform->errorMessage .= $app->tform->lng("limit_traffic_quota_free_txt").": ".$max_free_quota." MB<br>";
606                         // Set the quota field to the max free space
607                         $this->dataRecord["traffic_quota"] = $max_free_quota;
608                     }
609                     unset($tmp);
610                     unset($tmp_quota);
611                 }
612             }
613
614             // When the record is updated
615             if($this->id > 0) {
616                 // restore the server ID if the user is not admin and record is edited
64ea56 617                 $tmp = $app->db->queryOneRecord("SELECT server_id, `system_user`, `system_group`, `cgi`, `ssi`, `perl`, `ruby`, `python`, `suexec`, `errordocs`, `subdomain`, `ssl` FROM web_domain WHERE domain_id = ".$app->functions->intval($this->id));
146783 618                 $this->dataRecord["server_id"] = $tmp["server_id"];
7fe908 619
64ea56 620                 $this->dataRecord['system_user'] = $tmp['system_user'];
MC 621                 $this->dataRecord['system_group'] = $tmp['system_group'];
7fe908 622                 // set the settings to current if not provided (or cleared due to limits)
31143b 623                 if($this->dataRecord['cgi'] == 'n') $this->dataRecord['cgi'] = $tmp['cgi'];
TB 624                 if($this->dataRecord['ssi'] == 'n') $this->dataRecord['ssi'] = $tmp['ssi'];
625                 if($this->dataRecord['perl'] == 'n') $this->dataRecord['perl'] = $tmp['perl'];
626                 if($this->dataRecord['ruby'] == 'n') $this->dataRecord['ruby'] = $tmp['ruby'];
627                 if($this->dataRecord['python'] == 'n') $this->dataRecord['python'] = $tmp['python'];
628                 if($this->dataRecord['suexec'] == 'n') $this->dataRecord['suexec'] = $tmp['suexec'];
629                 if($this->dataRecord['errordocs'] == 'n') $this->dataRecord['errordocs'] = $tmp['errordocs'];
630                 if($this->dataRecord['subdomain'] == 'n') $this->dataRecord['subdomain'] = $tmp['subdomain'];
631                 if($this->dataRecord['ssl'] == 'n') $this->dataRecord['ssl'] = $tmp['ssl'];
7fe908 632
146783 633                 unset($tmp);
MC 634                 // When the record is inserted
635             } else {
636                 //* set the server ID to the default webserver of the client
637                 $this->dataRecord["server_id"] = $client["default_webserver"];
638
639                 // Check if the user may add another web_domain
640                 if($client["limit_web_domain"] >= 0) {
641                     $tmp = $app->db->queryOneRecord("SELECT count(domain_id) as number FROM web_domain WHERE sys_groupid = $client_group_id and type = 'vhost'");
642                     if($tmp["number"] >= $client["limit_web_domain"]) {
643                         $app->error($app->tform->wordbook["limit_web_domain_txt"]);
644                     }
645                 }
646
647             }
648
649             // Clients may not set the client_group_id, so we unset them if user is not a admin and the client is not a reseller
650             if(!$app->auth->has_clients($_SESSION['s']['user']['userid'])) unset($this->dataRecord["client_group_id"]);
651         }
7fe908 652
146783 653         //* make sure that the email domain is lowercase
MC 654         if(isset($this->dataRecord["domain"])) $this->dataRecord["domain"] = strtolower($this->dataRecord["domain"]);
7fe908 655
146783 656         //* get the server config for this server
MC 657         $app->uses("getconf");
658         if($this->id > 0){
659             $web_rec = $app->tform->getDataRecord($this->id);
660             $server_id = $web_rec["server_id"];
661         } else {
662             // Get the first server ID
663             $tmp = $app->db->queryOneRecord("SELECT server_id FROM server WHERE web_server = 1 ORDER BY server_name LIMIT 0,1");
664             $server_id = intval($tmp['server_id']);
665         }
7fe908 666         $web_config = $app->getconf->get_server_config($app->functions->intval(isset($this->dataRecord["server_id"]) ? $this->dataRecord["server_id"] : $server_id), 'web');
146783 667         //* Check for duplicate ssl certs per IP if SNI is disabled
MC 668         if(isset($this->dataRecord['ssl']) && $this->dataRecord['ssl'] == 'y' && $web_config['enable_sni'] != 'y') {
669             $sql = "SELECT count(domain_id) as number FROM web_domain WHERE `ssl` = 'y' AND ip_address = '".$app->db->quote($this->dataRecord['ip_address'])."' and domain_id != ".$this->id;
670             $tmp = $app->db->queryOneRecord($sql);
671             if($tmp['number'] > 0) $app->tform->errorMessage .= $app->tform->lng("error_no_sni_txt");
672         }
7fe908 673
146783 674         // Check if pm.max_children >= pm.max_spare_servers >= pm.start_servers >= pm.min_spare_servers > 0
MC 675         if(isset($this->dataRecord['pm_max_children']) && $this->dataRecord['pm'] == 'dynamic') {
676             if($app->functions->intval($this->dataRecord['pm_max_children'], true) >= $app->functions->intval($this->dataRecord['pm_max_spare_servers'], true) && $app->functions->intval($this->dataRecord['pm_max_spare_servers'], true) >= $app->functions->intval($this->dataRecord['pm_start_servers'], true) && $app->functions->intval($this->dataRecord['pm_start_servers'], true) >= $app->functions->intval($this->dataRecord['pm_min_spare_servers'], true) && $app->functions->intval($this->dataRecord['pm_min_spare_servers'], true) > 0){
7fe908 677
146783 678             } else {
MC 679                 $app->tform->errorMessage .= $app->tform->lng("error_php_fpm_pm_settings_txt").'<br>';
680             }
681         }
7fe908 682
146783 683         // Check rewrite rules
MC 684         $server_type = $web_config['server_type'];
7fe908 685
146783 686         if($server_type == 'nginx' && isset($this->dataRecord['rewrite_rules']) && trim($this->dataRecord['rewrite_rules']) != '') {
MC 687             $rewrite_rules = trim($this->dataRecord['rewrite_rules']);
688             $rewrites_are_valid = true;
689             // use this counter to make sure all curly brackets are properly closed
690             $if_level = 0;
691             // Make sure we only have Unix linebreaks
692             $rewrite_rules = str_replace("\r\n", "\n", $rewrite_rules);
693             $rewrite_rules = str_replace("\r", "\n", $rewrite_rules);
694             $rewrite_rule_lines = explode("\n", $rewrite_rules);
695             if(is_array($rewrite_rule_lines) && !empty($rewrite_rule_lines)){
696                 foreach($rewrite_rule_lines as $rewrite_rule_line){
697                     // ignore comments
7fe908 698                     if(substr(ltrim($rewrite_rule_line), 0, 1) == '#') continue;
146783 699                     // empty lines
MC 700                     if(trim($rewrite_rule_line) == '') continue;
701                     // rewrite
702                     if(preg_match('@^\s*rewrite\s+(^/)?\S+(\$)?\s+\S+(\s+(last|break|redirect|permanent|))?\s*;\s*$@', $rewrite_rule_line)) continue;
b5b613 703                     if(preg_match('@^\s*rewrite\s+(^/)?(\'[^\']+\'|"[^"]+")+(\$)?\s+(\'[^\']+\'|"[^"]+")+(\s+(last|break|redirect|permanent|))?\s*;\s*$@', $rewrite_rule_line)) continue;
FT 704                     if(preg_match('@^\s*rewrite\s+(^/)?(\'[^\']+\'|"[^"]+")+(\$)?\s+\S+(\s+(last|break|redirect|permanent|))?\s*;\s*$@', $rewrite_rule_line)) continue;
705                     if(preg_match('@^\s*rewrite\s+(^/)?\S+(\$)?\s+(\'[^\']+\'|"[^"]+")+(\s+(last|break|redirect|permanent|))?\s*;\s*$@', $rewrite_rule_line)) continue;
146783 706                     // if
MC 707                     if(preg_match('@^\s*if\s+\(\s*\$\S+(\s+(\!?(=|~|~\*))\s+(\S+|\".+\"))?\s*\)\s*\{\s*$@', $rewrite_rule_line)){
708                         $if_level += 1;
709                         continue;
710                     }
711                     // if - check for files, directories, etc.
712                     if(preg_match('@^\s*if\s+\(\s*\!?-(f|d|e|x)\s+\S+\s*\)\s*\{\s*$@', $rewrite_rule_line)){
713                         $if_level += 1;
714                         continue;
715                     }
716                     // break
717                     if(preg_match('@^\s*break\s*;\s*$@', $rewrite_rule_line)){
718                         continue;
719                     }
720                     // return code [ text ]
721                     if(preg_match('@^\s*return\s+\d\d\d.*;\s*$@', $rewrite_rule_line)) continue;
722                     // return code URL
723                     // return URL
724                     if(preg_match('@^\s*return(\s+\d\d\d)?\s+(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*\@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*\s*;\s*$@', $rewrite_rule_line)) continue;
725                     // set
726                     if(preg_match('@^\s*set\s+\$\S+\s+\S+\s*;\s*$@', $rewrite_rule_line)) continue;
727                     // closing curly bracket
728                     if(trim($rewrite_rule_line) == '}'){
729                         $if_level -= 1;
730                         continue;
731                     }
732                     $rewrites_are_valid = false;
733                     break;
734                 }
735             }
7fe908 736
146783 737             if(!$rewrites_are_valid || $if_level != 0){
MC 738                 $app->tform->errorMessage .= $app->tform->lng("invalid_rewrite_rules_txt").'<br>';
739             }
740         }
03cc01 741         
FT 742         // check custom php.ini settings
743         if(isset($this->dataRecord['custom_php_ini']) && trim($this->dataRecord['custom_php_ini']) != '') {
744             $custom_php_ini_settings = trim($this->dataRecord['custom_php_ini']);
745             $custom_php_ini_settings_are_valid = true;
746             // Make sure we only have Unix linebreaks
747             $custom_php_ini_settings = str_replace("\r\n", "\n", $custom_php_ini_settings);
748             $custom_php_ini_settings = str_replace("\r", "\n", $custom_php_ini_settings);
749             $custom_php_ini_settings_lines = explode("\n", $custom_php_ini_settings);
750             if(is_array($custom_php_ini_settings_lines) && !empty($custom_php_ini_settings_lines)){
751                 foreach($custom_php_ini_settings_lines as $custom_php_ini_settings_line){
752                     if(trim($custom_php_ini_settings_line) == '') continue;
753                     if(substr(trim($custom_php_ini_settings_line),0,1) == ';') continue;
754                     // empty value
755                     if(preg_match('@^\s*;*\s*[a-zA-Z0-9._]*\s*=\s*;*\s*$@', $custom_php_ini_settings_line)) continue;
756                     // value inside ""
757                     if(preg_match('@^\s*;*\s*[a-zA-Z0-9._]*\s*=\s*".*"\s*;*\s*$@', $custom_php_ini_settings_line)) continue;
758                     // value inside ''
759                     if(preg_match('@^\s*;*\s*[a-zA-Z0-9._]*\s*=\s*\'.*\'\s*;*\s*$@', $custom_php_ini_settings_line)) continue;
760                     // everything else
b00e47 761                     if(preg_match('@^\s*;*\s*[a-zA-Z0-9._]*\s*=\s*[-a-zA-Z0-9~&=_\@/,.#\s|()]*\s*;*\s*$@', $custom_php_ini_settings_line)) continue;
03cc01 762                     $custom_php_ini_settings_are_valid = false;
FT 763                     break;
764                 }
765             }
766             if(!$custom_php_ini_settings_are_valid){
767                 $app->tform->errorMessage .= $app->tform->lng("invalid_custom_php_ini_settings_txt").'<br>';
768             }
769         }
e1b4ca 770         
TB 771         // Check custom PHP version
772         if(isset($this->dataRecord['fastcgi_php_version']) && $this->dataRecord['fastcgi_php_version'] != '') {
773             // Check php-fpm mode
774             if($this->dataRecord['php'] == 'php-fpm'){
775                 $tmp = $app->db->queryOneRecord("SELECT * FROM server_php WHERE CONCAT(name,':',php_fpm_init_script,':',php_fpm_ini_dir,':',php_fpm_pool_dir) = '".$app->db->quote($this->dataRecord['fastcgi_php_version'])."'");
776                 if(is_array($tmp)) {
777                     $this->dataRecord['fastcgi_php_version'] = $tmp['name'].':'.$tmp['php_fpm_init_script'].':'.$tmp['php_fpm_ini_dir'].':'.$tmp['php_fpm_pool_dir'];
778                 } else {
779                     $this->dataRecord['fastcgi_php_version'] = '';
780                 }
781                 unset($tmp);
782             // Check fast-cgi mode
783             } elseif($this->dataRecord['php'] == 'fast-cgi') {
784                 $tmp = $app->db->queryOneRecord("SELECT * FROM server_php WHERE CONCAT(name,':',php_fastcgi_binary,':',php_fastcgi_ini_dir) = '".$app->db->quote($this->dataRecord['fastcgi_php_version'])."'");
785                 if(is_array($tmp)) {
786                     $this->dataRecord['fastcgi_php_version'] = $tmp['name'].':'.$tmp['php_fastcgi_binary'].':'.$tmp['php_fastcgi_ini_dir'];
787                 } else {
788                     $this->dataRecord['fastcgi_php_version'] = '';
789                 }
790                 unset($tmp);
791             } else {
792                 // Other PHP modes do not have custom versions, so we force the value to be empty
793                 $this->dataRecord['fastcgi_php_version'] = '';
794             }
795         }
796         
146783 797
MC 798         parent::onSubmit();
799     }
800
801     function onAfterInsert() {
802         global $app, $conf;
803
804         // make sure that the record belongs to the clinet group and not the admin group when admin inserts it
805         // also make sure that the user can not delete domain created by a admin
806         if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($this->dataRecord["client_group_id"])) {
807             $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
808             $app->db->query("UPDATE web_domain SET sys_groupid = $client_group_id, sys_perm_group = 'ru' WHERE domain_id = ".$this->id);
809         }
810         if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($this->dataRecord["client_group_id"])) {
811             $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
812             $app->db->query("UPDATE web_domain SET sys_groupid = $client_group_id, sys_perm_group = 'riud' WHERE domain_id = ".$this->id);
813         }
814
815         // Get configuration for the web system
816         $app->uses("getconf");
817         $web_rec = $app->tform->getDataRecord($this->id);
7fe908 818         $web_config = $app->getconf->get_server_config($app->functions->intval($web_rec["server_id"]), 'web');
MC 819         $document_root = str_replace("[website_id]", $this->id, $web_config["website_path"]);
820         $document_root = str_replace("[website_idhash_1]", $this->id_hash($page_form->id, 1), $document_root);
821         $document_root = str_replace("[website_idhash_2]", $this->id_hash($page_form->id, 1), $document_root);
822         $document_root = str_replace("[website_idhash_3]", $this->id_hash($page_form->id, 1), $document_root);
823         $document_root = str_replace("[website_idhash_4]", $this->id_hash($page_form->id, 1), $document_root);
146783 824
MC 825         // get the ID of the client
826         if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
604c0c 827             $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
146783 828             $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = $client_group_id");
MC 829             $client_id = $app->functions->intval($client["client_id"]);
830         } else {
831             //$client_id = $app->functions->intval($this->dataRecord["client_group_id"]);
832             $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ".$app->functions->intval($this->dataRecord["client_group_id"]));
833             $client_id = $app->functions->intval($client["client_id"]);
834         }
835
836         // Set the values for document_root, system_user and system_group
837         $system_user = $app->db->quote('web'.$this->id);
838         $system_group = $app->db->quote('client'.$client_id);
7fe908 839         $document_root = str_replace("[client_id]", $client_id, $document_root);
MC 840         $document_root = str_replace("[client_idhash_1]", $this->id_hash($client_id, 1), $document_root);
841         $document_root = str_replace("[client_idhash_2]", $this->id_hash($client_id, 2), $document_root);
842         $document_root = str_replace("[client_idhash_3]", $this->id_hash($client_id, 3), $document_root);
843         $document_root = str_replace("[client_idhash_4]", $this->id_hash($client_id, 4), $document_root);
146783 844         $document_root = $app->db->quote($document_root);
7fe908 845         $php_open_basedir = str_replace("[website_path]", $document_root, $web_config["php_open_basedir"]);
MC 846         $php_open_basedir = $app->db->quote(str_replace("[website_domain]", $web_rec['domain'], $php_open_basedir));
146783 847         $htaccess_allow_override = $app->db->quote($web_config["htaccess_allow_override"]);
3d96c5 848         $added_date = date($app->lng('conf_format_dateshort'));
TB 849         $added_by = $app->db->quote($_SESSION['s']['user']['username']);
146783 850
3d96c5 851         $sql = "UPDATE web_domain SET system_user = '$system_user', system_group = '$system_group', document_root = '$document_root', allow_override = '$htaccess_allow_override', php_open_basedir = '$php_open_basedir', added_date = '$added_date', added_by = '$added_by'  WHERE domain_id = ".$this->id;
146783 852         $app->db->query($sql);
MC 853     }
854
855     function onBeforeUpdate () {
856         global $app, $conf;
857
858         //* Check if the server has been changed
859         // We do this only for the admin or reseller users, as normal clients can not change the server ID anyway
860         if($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
861             if (isset($this->dataRecord["server_id"])) {
862                 $rec = $app->db->queryOneRecord("SELECT server_id from web_domain WHERE domain_id = ".$this->id);
863                 if($rec['server_id'] != $this->dataRecord["server_id"]) {
864                     //* Add a error message and switch back to old server
865                     $app->tform->errorMessage .= $app->lng('The Server can not be changed.');
866                     $this->dataRecord["server_id"] = $rec['server_id'];
867                 }
868                 unset($rec);
869             }
870             //* If the user is neither admin nor reseller
871         } else {
872             //* We do not allow users to change a domain which has been created by the admin
873             $rec = $app->db->queryOneRecord("SELECT sys_perm_group, domain, ip_address, ipv6_address from web_domain WHERE domain_id = ".$this->id);
7fe908 874             if(isset($this->dataRecord["domain"]) && $rec['domain'] != $this->dataRecord["domain"] && $app->tform->checkPerm($this->id, 'u')) {
146783 875                 //* Add a error message and switch back to old server
MC 876                 $app->tform->errorMessage .= $app->lng('The Domain can not be changed. Please ask your Administrator if you want to change the domain name.');
877                 $this->dataRecord["domain"] = $rec['domain'];
878             }
879             if(isset($this->dataRecord["ip_address"]) && $rec['ip_address'] != $this->dataRecord["ip_address"] && $rec['sys_perm_group'] != 'riud') {
7fe908 880                 $this->dataRecord["ip_address"] = $rec['ip_address'];
MC 881             }
146783 882             if(isset($this->dataRecord["ipv6_address"]) && $rec['ipv6_address'] != $this->dataRecord["ipv6_address"] && $rec['sys_perm_group'] != 'riud') {
7fe908 883                 $this->dataRecord["ipv6_address"] = $rec['ipv6_address'];
MC 884             }
146783 885             unset($rec);
MC 886         }
887
888         //* Check that all fields for the SSL cert creation are filled
889         if(isset($this->dataRecord['ssl_action']) && $this->dataRecord['ssl_action'] == 'create') {
890             if($this->dataRecord['ssl_state'] == '') $app->tform->errorMessage .= $app->tform->lng('error_ssl_state_empty').'<br />';
891             if($this->dataRecord['ssl_locality'] == '') $app->tform->errorMessage .= $app->tform->lng('error_ssl_locality_empty').'<br />';
892             if($this->dataRecord['ssl_organisation'] == '') $app->tform->errorMessage .= $app->tform->lng('error_ssl_organisation_empty').'<br />';
893             if($this->dataRecord['ssl_organisation_unit'] == '') $app->tform->errorMessage .= $app->tform->lng('error_ssl_organisation_unit_empty').'<br />';
894             if($this->dataRecord['ssl_country'] == '') $app->tform->errorMessage .= $app->tform->lng('error_ssl_country_empty').'<br />';
895         }
7fe908 896
146783 897         if(isset($this->dataRecord['ssl_action']) && $this->dataRecord['ssl_action'] == 'save') {
MC 898             if(trim($this->dataRecord['ssl_cert']) == '') $app->tform->errorMessage .= $app->tform->lng('error_ssl_cert_empty').'<br />';
899         }
900
901     }
902
903     function onAfterUpdate() {
904         global $app, $conf;
905
906         // make sure that the record belongs to the client group and not the admin group when a admin inserts it
907         // also make sure that the user can not delete domain created by a admin
908         if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($this->dataRecord["client_group_id"])) {
909             $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
910             $app->db->query("UPDATE web_domain SET sys_groupid = $client_group_id, sys_perm_group = 'ru' WHERE domain_id = ".$this->id);
911         }
912         if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($this->dataRecord["client_group_id"])) {
913             $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
914             $app->db->query("UPDATE web_domain SET sys_groupid = $client_group_id, sys_perm_group = 'riud' WHERE domain_id = ".$this->id);
915         }
916
917         // Get configuration for the web system
918         $app->uses("getconf");
919         $web_rec = $app->tform->getDataRecord($this->id);
7fe908 920         $web_config = $app->getconf->get_server_config($app->functions->intval($web_rec["server_id"]), 'web');
MC 921         $document_root = str_replace("[website_id]", $this->id, $web_config["website_path"]);
146783 922         $page_formid = isset($page_form->id) ? $page_form->id : '';
7fe908 923         $document_root = str_replace("[website_idhash_1]", $this->id_hash($page_formid, 1), $document_root);
MC 924         $document_root = str_replace("[website_idhash_2]", $this->id_hash($page_formid, 1), $document_root);
925         $document_root = str_replace("[website_idhash_3]", $this->id_hash($page_formid, 1), $document_root);
926         $document_root = str_replace("[website_idhash_4]", $this->id_hash($page_formid, 1), $document_root);
146783 927
MC 928         // get the ID of the client
929         if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
604c0c 930             $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
146783 931             $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = $client_group_id");
MC 932             $client_id = $app->functions->intval($client["client_id"]);
933         } elseif (isset($this->dataRecord["client_group_id"])) {
934             $client_group_id = $this->dataRecord["client_group_id"];
935             $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ".$app->functions->intval(@$this->dataRecord["client_group_id"]));
936             $client_id = $app->functions->intval($client["client_id"]);
937         } else {
938             $client_group_id = $web_rec['sys_groupid'];
939             $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ".$app->functions->intval($client_group_id));
940             $client_id = $app->functions->intval($client["client_id"]);
941         }
87f67a 942         
TB 943         $tmp = $app->db->queryOneRecord("SELECT userid FROM sys_user WHERE default_group = $client_group_id");
944         $client_user_id = $app->functions->intval(($tmp['userid'] > 0)?$tmp['userid']:1);
146783 945
MC 946         if(($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) &&  isset($this->dataRecord["client_group_id"]) && $this->dataRecord["client_group_id"] != $this->oldDataRecord["sys_groupid"]) {
947             // Set the values for document_root, system_user and system_group
948             $system_user = $app->db->quote('web'.$this->id);
949             $system_group = $app->db->quote('client'.$client_id);
7fe908 950             $document_root = str_replace("[client_id]", $client_id, $document_root);
MC 951             $document_root = str_replace("[client_idhash_1]", $this->id_hash($client_id, 1), $document_root);
952             $document_root = str_replace("[client_idhash_2]", $this->id_hash($client_id, 2), $document_root);
953             $document_root = str_replace("[client_idhash_3]", $this->id_hash($client_id, 3), $document_root);
954             $document_root = str_replace("[client_idhash_4]", $this->id_hash($client_id, 4), $document_root);
146783 955             $document_root = $app->db->quote($document_root);
MC 956
87f67a 957             $sql = "UPDATE web_domain SET sys_userid = '$client_user_id' ,system_user = '$system_user', system_group = '$system_group', document_root = '$document_root' WHERE domain_id = ".$this->id;
146783 958             //$sql = "UPDATE web_domain SET system_user = '$system_user', system_group = '$system_group' WHERE domain_id = ".$this->id;
MC 959             $app->db->query($sql);
960
961             // Update the FTP user(s) too
962             $records = $app->db->queryAllRecords("SELECT ftp_user_id FROM ftp_user WHERE parent_domain_id = ".$this->id);
963             foreach($records as $rec) {
87f67a 964                 $app->db->datalogUpdate('ftp_user', "sys_userid = '".$app->functions->intval($client_user_id)."', sys_groupid = '".$app->functions->intval($web_rec['sys_groupid'])."', uid = '$system_user', gid = '$system_group', dir = '$document_root'", 'ftp_user_id', $app->functions->intval($rec['ftp_user_id']));
146783 965             }
MC 966             unset($records);
967             unset($rec);
968
969             // Update the Shell user(s) too
970             $records = $app->db->queryAllRecords("SELECT shell_user_id FROM shell_user WHERE parent_domain_id = ".$this->id);
971             foreach($records as $rec) {
87f67a 972                 $app->db->datalogUpdate('shell_user', "sys_userid = '".$client_user_id."', sys_groupid = '".$web_rec['sys_groupid']."', puser = '$system_user', pgroup = '$system_group', dir = '$document_root'", 'shell_user_id', $app->functions->intval($rec['shell_user_id']));
146783 973             }
MC 974             unset($records);
975             unset($rec);
7fe908 976
146783 977             //* Update all subdomains and alias domains
MC 978             $records = $app->db->queryAllRecords("SELECT domain_id, `domain`, `type`, `web_folder` FROM web_domain WHERE parent_domain_id = ".$this->id);
979             foreach($records as $rec) {
87f67a 980                 $update_columns = "sys_userid = '".$client_user_id."', sys_groupid = '".$web_rec['sys_groupid']."'";
7fe908 981                 if($rec['type'] == 'vhostsubdomain') {
MC 982                     $php_open_basedir = str_replace("[website_path]/web", $document_root.'/'.$rec['web_folder'], $web_config["php_open_basedir"]);
983                     $php_open_basedir = str_replace("[website_domain]/web", $rec['domain'].'/'.$rec['web_folder'], $php_open_basedir);
984                     $php_open_basedir = str_replace("[website_path]", $document_root, $php_open_basedir);
985                     $php_open_basedir = $app->db->quote(str_replace("[website_domain]", $rec['domain'], $php_open_basedir));
146783 986
7fe908 987                     $update_columns .= ", document_root = '".$document_root."', `php_open_basedir` = '".$php_open_basedir."'";
MC 988                 }
146783 989                 $app->db->datalogUpdate('web_domain', $update_columns, 'domain_id', $rec['domain_id']);
MC 990             }
991             unset($records);
992             unset($rec);
7fe908 993
146783 994             //* Update all databases
MC 995             $records = $app->db->queryAllRecords("SELECT database_id FROM web_database WHERE parent_domain_id = ".$this->id);
996             foreach($records as $rec) {
87f67a 997                 $app->db->datalogUpdate('web_database', "sys_userid = '".$app->functions->intval($client_user_id)."', sys_groupid = '".$app->functions->intval($web_rec['sys_groupid'])."'", 'database_id', $app->functions->intval($rec['database_id']));
146783 998             }
MC 999             unset($records);
1000             unset($rec);
1001
1002         }
1003
1004         //* If the domain name has been changed, we will have to change all subdomains + APS instances
1005         if(!empty($this->dataRecord["domain"]) && !empty($this->oldDataRecord["domain"]) && $this->dataRecord["domain"] != $this->oldDataRecord["domain"]) {
1006             $records = $app->db->queryAllRecords("SELECT domain_id,domain FROM web_domain WHERE (type = 'subdomain' OR type = 'vhostsubdomain') AND domain LIKE '%.".$app->db->quote($this->oldDataRecord["domain"])."'");
1007             foreach($records as $rec) {
7fe908 1008                 $subdomain = $app->db->quote(str_replace($this->oldDataRecord["domain"], $this->dataRecord["domain"], $rec['domain']));
146783 1009                 $app->db->datalogUpdate('web_domain', "domain = '".$subdomain."'", 'domain_id', $rec['domain_id']);
MC 1010             }
1011             unset($records);
1012             unset($rec);
1013             unset($subdomain);
7fe908 1014
146783 1015             // Update APS instances
604c0c 1016             $records = $app->db->queryAllRecords("SELECT id, instance_id FROM aps_instances_settings WHERE name = 'main_domain' AND value = '".$app->db->quote($this->oldDataRecord["domain"])."'");
146783 1017             if(is_array($records) && !empty($records)){
MC 1018                 foreach($records as $rec){
604c0c 1019                     $app->db->datalogUpdate('aps_instances_settings', "value = '".$app->db->quote($this->dataRecord["domain"])."'", 'id', $rec['id']);
146783 1020                     // Reinstall of package needed?
MC 1021                     //$app->db->datalogUpdate('aps_instances', "instance_status = '1'", 'id', $rec['instance_id']);
1022                 }
1023             }
1024             unset($records);
1025             unset($rec);
1026         }
1027
1028         //* Set allow_override if empty
1029         if($web_rec['allow_override'] == '') {
1030             $sql = "UPDATE web_domain SET allow_override = '".$app->db->quote($web_config["htaccess_allow_override"])."' WHERE domain_id = ".$this->id;
1031             $app->db->query($sql);
1032         }
7fe908 1033
146783 1034         //* Set php_open_basedir if empty or domain or client has been changed
MC 1035         if(empty($web_rec['php_open_basedir']) ||
7fe908 1036             (!empty($this->dataRecord["domain"]) && !empty($this->oldDataRecord["domain"]) && $this->dataRecord["domain"] != $this->oldDataRecord["domain"])) {
146783 1037             $php_open_basedir = $web_rec['php_open_basedir'];
604c0c 1038             $php_open_basedir = $app->db->quote(str_replace($this->oldDataRecord['domain'], $web_rec['domain'], $php_open_basedir));
146783 1039             $sql = "UPDATE web_domain SET php_open_basedir = '$php_open_basedir' WHERE domain_id = ".$this->id;
MC 1040             $app->db->query($sql);
1041         }
1042         if(empty($web_rec['php_open_basedir']) ||
7fe908 1043             (isset($this->dataRecord["client_group_id"]) && $this->dataRecord["client_group_id"] != $this->oldDataRecord["sys_groupid"])) {
MC 1044             $document_root = $app->db->quote(str_replace("[client_id]", $client_id, $document_root));
1045             $php_open_basedir = str_replace("[website_path]", $document_root, $web_config["php_open_basedir"]);
1046             $php_open_basedir = $app->db->quote(str_replace("[website_domain]", $web_rec['domain'], $php_open_basedir));
146783 1047             $sql = "UPDATE web_domain SET php_open_basedir = '$php_open_basedir' WHERE domain_id = ".$this->id;
MC 1048             $app->db->query($sql);
1049         }
7fe908 1050
146783 1051         //* Change database backup options when web backup options have been changed
MC 1052         if(isset($this->dataRecord['backup_interval']) && ($this->dataRecord['backup_interval'] != $this->oldDataRecord['backup_interval'] || $this->dataRecord['backup_copies'] != $this->oldDataRecord['backup_copies'])) {
1053             //* Update all databases
7c19f2 1054             $backup_interval = $app->db->quote($this->dataRecord['backup_interval']);
604c0c 1055             $backup_copies = $app->functions->intval($this->dataRecord['backup_copies']);
146783 1056             $records = $app->db->queryAllRecords("SELECT database_id FROM web_database WHERE parent_domain_id = ".$this->id);
MC 1057             foreach($records as $rec) {
1058                 $app->db->datalogUpdate('web_database', "backup_interval = '$backup_interval', backup_copies = '$backup_copies'", 'database_id', $rec['database_id']);
1059             }
1060             unset($records);
1061             unset($rec);
1062             unset($backup_copies);
1063             unset($backup_interval);
1064         }
7fe908 1065
MC 1066         //* Change vhost subdomain ip/ipv6 if domain ip/ipv6 has changed
1067         if(isset($this->dataRecord['ip_address']) && ($this->dataRecord['ip_address'] != $this->oldDataRecord['ip_address'] || $this->dataRecord['ipv6_address'] != $this->oldDataRecord['ipv6_address'])) {
146783 1068             $records = $app->db->queryAllRecords("SELECT domain_id FROM web_domain WHERE type = 'vhostsubdomain' AND parent_domain_id = ".$this->id);
MC 1069             foreach($records as $rec) {
604c0c 1070                 $app->db->datalogUpdate('web_domain', "ip_address = '".$app->db->quote($web_rec['ip_address'])."', ipv6_address = '".$app->db->quote($web_rec['ipv6_address'])."'", 'domain_id', $rec['domain_id']);
146783 1071             }
MC 1072             unset($records);
1073             unset($rec);
7fe908 1074         }
146783 1075     }
MC 1076
1077     function onAfterDelete() {
1078         global $app, $conf;
1079
1080         // Delete the sub and alias domains
1081         $child_domains = $app->db->queryAllRecords("SELECT * FROM web_domain WHERE parent_domain_id = ".$this->id);
1082         foreach($child_domains as $d) {
1083             // Saving record to datalog when db_history enabled
1084             if($app->tform->formDef["db_history"] == 'yes') {
7fe908 1085                 $app->tform->datalogSave('DELETE', $d["domain_id"], $d, array());
146783 1086             }
MC 1087
604c0c 1088             $app->db->query("DELETE FROM web_domain WHERE domain_id = ".$app->functions->intval($d["domain_id"])." LIMIT 0,1");
146783 1089         }
MC 1090         unset($child_domains);
1091         unset($d);
1092
1093     }
1094
1095 }
1096
1097 $page = new page_action;
1098 $page->onLoad();
1099
7fe908 1100 ?>