Till Brehm
2016-07-23 5501be8a358c44707752aa6d857af09599b4bfa9
commit | author | age
76ebcb 1 <?php
F 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
73813a 35 $tform_def_file = "form/web_vhost_domain.tform.php";
76ebcb 36
F 37 /******************************************
38 * End Form configuration
39 ******************************************/
40
b1a6a5 41 require_once '../../lib/config.inc.php';
MC 42 require_once '../../lib/app.inc.php';
76ebcb 43
F 44 //* Check permissions for module
45 $app->auth->check_module_permissions('sites');
46
47 // Loading classes
10b4c8 48 $app->uses('tpl,tform,tform_actions,tools_sites');
76ebcb 49 $app->load('tform_actions');
F 50
51 class page_action extends tform_actions {
73813a 52     var $_vhostdomain_type = 'domain';
6c60b1 53     var $_letsencrypt_on_insert = false;
76ebcb 54
F 55     //* Returna a "3/2/1" path hash from a numeric id '123'
b1a6a5 56     function id_hash($id, $levels) {
76ebcb 57         $hash = "" . $id % 10 ;
F 58         $id /= 10 ;
59         $levels -- ;
60         while ( $levels > 0 ) {
61             $hash .= "/" . $id % 10 ;
62             $id /= 10 ;
63             $levels-- ;
64         }
65         return $hash;
66     }
b1a6a5 67
73813a 68     function onLoad() {
MC 69         $show_type = 'domain';
70         if(isset($_GET['type']) && $_GET['type'] == 'subdomain') {
71             $show_type = 'subdomain';
72         } elseif(isset($_GET['type']) && $_GET['type'] == 'aliasdomain') {
73             $show_type = 'aliasdomain';
74         } elseif(!isset($_GET['type']) && isset($_SESSION['s']['var']['vhostdomain_type']) && $_SESSION['s']['var']['vhostdomain_type'] == 'subdomain') {
75             $show_type = 'subdomain';
76         } elseif(!isset($_GET['type']) && isset($_SESSION['s']['var']['vhostdomain_type']) && $_SESSION['s']['var']['vhostdomain_type'] == 'aliasdomain') {
77             $show_type = 'aliasdomain';
78         }
79
80         $_SESSION['s']['var']['vhostdomain_type'] = $show_type;
81         $this->_vhostdomain_type = $show_type;
82         
83         parent::onLoad();
84     }
85
76ebcb 86     function onShowNew() {
F 87         global $app, $conf;
88
89         // we will check only users, not admins
90         if($_SESSION["s"]["user"]["typ"] == 'user') {
73813a 91             if($this->_vhostdomain_type == 'domain') {
MC 92                 if(!$app->tform->checkClientLimit('limit_web_domain', "type = 'vhost'")) {
93                     $app->error($app->tform->wordbook["limit_web_domain_txt"]);
94                 }
95                 if(!$app->tform->checkResellerLimit('limit_web_domain', "type = 'vhost'")) {
96                     $app->error('Reseller: '.$app->tform->wordbook["limit_web_domain_txt"]);
97                 }
98             } elseif($this->_vhostdomain_type == 'subdomain') {
99                 if(!$app->tform->checkClientLimit('limit_web_subdomain', "(type = 'subdomain' OR type = 'vhostsubdomain')")) {
100                     $app->error($app->tform->wordbook["limit_web_subdomain_txt"]);
101                 }
102                 if(!$app->tform->checkResellerLimit('limit_web_subdomain', "(type = 'subdomain' OR type = 'vhostsubdomain')")) {
103                     $app->error('Reseller: '.$app->tform->wordbook["limit_web_subdomain_txt"]);
104                 }
105             } elseif($this->_vhostdomain_type == 'aliasdomain') {
106                 if(!$app->tform->checkClientLimit('limit_web_aliasdomain', "(type = 'alias' OR type = 'vhostalias')")) {
107                     $app->error($app->tform->wordbook["limit_web_aliasdomain_txt"]);
108                 }
109                 if(!$app->tform->checkResellerLimit('limit_web_aliasdomain', "(type = 'alias' OR type = 'vhostalias')")) {
110                     $app->error('Reseller: '.$app->tform->wordbook["limit_web_aliasdomain_txt"]);
111                 }
76ebcb 112             }
F 113             // Get the limits of the client
114             $client_group_id = $_SESSION["s"]["user"]["default_group"];
cc7a82 115             $client = $app->db->queryOneRecord("SELECT client.web_servers FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = ?", $client_group_id);
bd6659 116             $web_servers = explode(',', $client['web_servers']);
3245aa 117             $server_id = $web_servers[0];
SC 118             $app->tpl->setVar("server_id_value", $server_id);
bd6659 119             unset($web_servers);
41d7d1 120         } else {
SC 121             $settings = $app->getconf->get_global_config('sites');
3245aa 122             $server_id = intval($settings['default_webserver']);
SC 123             $app->tform->formDef['tabs']['domain']['fields']['server_id']['default'] = $server_id;
76ebcb 124         }
a4a4ea 125         if(!$server_id){
FT 126             $default_web_server = $app->db->queryOneRecord("SELECT server_id FROM server WHERE web_server = ? ORDER BY server_id LIMIT 0,1", 1);
127             $server_id = $default_web_server['server_id'];
128         }
3245aa 129         $web_config = $app->getconf->get_server_config($server_id, 'web');
SC 130         $app->tform->formDef['tabs']['domain']['fields']['php']['default'] = $web_config['php_handler'];
b1a6a5 131         $app->tform->formDef['tabs']['domain']['readonly'] = false;
76ebcb 132
73813a 133         $app->tpl->setVar('vhostdomain_type', $this->_vhostdomain_type);
76ebcb 134         parent::onShowNew();
F 135     }
136
137     function onShowEnd() {
138         global $app, $conf;
b1a6a5 139
76ebcb 140         $app->uses('ini_parser,getconf');
3e94c8 141         $settings = $app->getconf->get_global_config('domains');
76ebcb 142
67b0cb 143         $read_limits = array('limit_cgi', 'limit_ssi', 'limit_perl', 'limit_ruby', 'limit_python', 'force_suexec', 'limit_hterror', 'limit_wildcard', 'limit_ssl', 'limit_ssl_letsencrypt', 'limit_directive_snippets');
02384b 144
cc7a82 145         if($this->_vhostdomain_type != 'domain') $parent_domain = $app->db->queryOneRecord("select * FROM web_domain WHERE domain_id = ?", @$this->dataRecord["parent_domain_id"]);
8173c6 146         
FT 147         $is_admin = false;
73813a 148
76ebcb 149         //* Client: If the logged in user is not admin and has no sub clients (no reseller)
F 150         if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
151
152             // Get the limits of the client
73813a 153             $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
MC 154             if($this->_vhostdomain_type == 'domain') {
cc7a82 155                 $client = $app->db->queryOneRecord("SELECT client.limit_web_domain, client.web_servers, client." . implode(", client.", $read_limits) . " FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = ?", $client_group_id);
73813a 156             } elseif($this->_vhostdomain_type == 'subdomain') {
cc7a82 157                 $client = $app->db->queryOneRecord("SELECT client.limit_web_subdomain, client.web_servers, 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);
73813a 158             } elseif($this->_vhostdomain_type == 'aliasdomain') {
cc7a82 159                 $client = $app->db->queryOneRecord("SELECT client.limit_web_aliasdomain, client.web_servers, 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);
73813a 160             }
015dff 161
bd6659 162             $client['web_servers_ids'] = explode(',', $client['web_servers']);
SJ 163             $only_one_server = count($client['web_servers_ids']) === 1;
164             $app->tpl->setVar('only_one_server', $only_one_server);
b1a6a5 165
02384b 166             //* Get global web config
bd6659 167             foreach ($client['web_servers_ids'] as $web_server_id) {
SJ 168                 $web_config[$web_server_id] = $app->getconf->get_server_config($web_server_id, 'web');
169             }
b1a6a5 170
2af58c 171             $sql = "SELECT server_id, server_name FROM server WHERE server_id IN ?";
MC 172             $web_servers = $app->db->queryAllRecords($sql, $client['web_servers_ids']);
bd6659 173
SJ 174             $options_web_servers = "";
175
176             foreach ($web_servers as $web_server) {
75b7fc 177                 $options_web_servers .= '<option value="'.$web_server['server_id'].'"'.($this->id > 0 && $this->dataRecord["server_id"] == $web_server['server_id'] ? ' selected="selected"' : '').'>'.$web_server['server_name'].'</option>';
bd6659 178             }
SJ 179
180             $app->tpl->setVar("server_id", $options_web_servers);
181             unset($options_web_servers);
182
183             if($this->id > 0) {
184                 if(!isset($this->dataRecord["server_id"])){
2af58c 185                     $tmp = $app->db->queryOneRecord("SELECT server_id FROM web_domain WHERE domain_id = ?", $this->id);
bd6659 186                     $this->dataRecord["server_id"] = $tmp["server_id"];
SJ 187                     unset($tmp);
188                 }
189                 $server_id = intval(@$this->dataRecord["server_id"]);
190             } else {
0d3e95 191                 $server_id = (isset($web_servers[0])) ? intval($web_servers[0]['server_id']) : 0;
bd6659 192             }
cf646e 193             
MC 194             if($app->functions->intval($this->dataRecord["server_id"]) > 0) {
195                 // check if server is in client's servers or add it.
196                 $chk_sid = explode(',', $client['web_servers']);
1fa8f4 197                 if(in_array($this->dataRecord["server_id"], explode(',', $client['web_servers'])) == false) {
cf646e 198                     if($client['web_servers'] != '') $client['web_servers'] .= ',';
MC 199                     $client['web_servers'] .= $app->functions->intval($this->dataRecord["server_id"]);
200                 }
201             }
202             
0d3e95 203             //* Fill the IPv4 select field with the IP addresses that are allowed for this client on the current server
TB 204             $sql = "SELECT ip_address FROM server_ip WHERE server_id = ? AND ip_type = 'IPv4' AND (client_id = 0 OR client_id=".$_SESSION['s']['user']['client_id'].")";
205             $ips = $app->db->queryAllRecords($sql, $server_id);
1aece9 206             $ip_select = ($web_config[$server_id]['enable_ip_wildcard'] == 'y')?"<option value='*'>*</option>":"";
30848e 207             //if(!in_array($this->dataRecord["ip_address"], $ips)) $ip_select .= "<option value='".$this->dataRecord["ip_address"]."' SELECTED>".$this->dataRecord["ip_address"]."</option>\r\n";
76ebcb 208             //$ip_select = "";
F 209             if(is_array($ips)) {
210                 foreach( $ips as $ip) {
211                     $selected = ($ip["ip_address"] == $this->dataRecord["ip_address"])?'SELECTED':'';
212                     $ip_select .= "<option value='$ip[ip_address]' $selected>$ip[ip_address]</option>\r\n";
213                 }
214             }
b1a6a5 215             $app->tpl->setVar("ip_address", $ip_select);
76ebcb 216             unset($tmp);
F 217             unset($ips);
b1a6a5 218
76ebcb 219             //* Fill the IPv6 select field with the IP addresses that are allowed for this client
0d3e95 220             $sql = "SELECT ip_address FROM server_ip WHERE server_id = ? AND ip_type = 'IPv6' AND (client_id = 0 OR client_id=?)";
TB 221             $ips = $app->db->queryAllRecords($sql, $server_id, $_SESSION['s']['user']['client_id']);
aaeba2 222             //$ip_select = ($web_config[$server_id]['enable_ip_wildcard'] == 'y')?"<option value='*'>*</option>":"";
76ebcb 223             //$ip_select = "";
aaeba2 224             $ip_select = "<option value=''></option>";
76ebcb 225             if(is_array($ips)) {
F 226                 foreach( $ips as $ip) {
227                     $selected = ($ip["ip_address"] == $this->dataRecord["ipv6_address"])?'SELECTED':'';
228                     $ip_select .= "<option value='$ip[ip_address]' $selected>$ip[ip_address]</option>\r\n";
229                 }
230             }
b1a6a5 231             $app->tpl->setVar("ipv6_address", $ip_select);
76ebcb 232             unset($tmp);
F 233             unset($ips);
b1a6a5 234
76ebcb 235             //PHP Version Selection (FastCGI)
F 236             $server_type = 'apache';
1aece9 237             if(!empty($web_config[$server_id]['server_type'])) $server_type = $web_config[$server_id]['server_type'];
76ebcb 238             if($server_type == 'nginx' && $this->dataRecord['php'] == 'fast-cgi') $this->dataRecord['php'] = 'php-fpm';
73813a 239
MC 240             if($this->_vhostdomain_type == 'domain') {
d22277 241                 if($this->dataRecord['php'] == 'php-fpm' || ($this->dataRecord['php'] == 'hhvm' && $server_type == 'nginx')){
2af58c 242                     $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 = ? AND (client_id = 0 OR client_id=?)", ($this->id > 0 ? $this->dataRecord['server_id'] : $client['default_webserver']), $_SESSION['s']['user']['client_id']);
73813a 243                 }
MC 244                 if($this->dataRecord['php'] == 'fast-cgi'){
2af58c 245                     $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = ? AND (client_id = 0 OR client_id=?)", ($this->id > 0 ? $this->dataRecord['server_id'] : $client['default_webserver']), $_SESSION['s']['user']['client_id']);
73813a 246                 }
MC 247             } else {
d22277 248                 if($this->dataRecord['php'] == 'php-fpm' || ($this->dataRecord['php'] == 'hhvm' && $server_type == 'nginx')){
2af58c 249                     $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 = ? AND (client_id = 0 OR client_id=?)", $parent_domain['server_id'], $_SESSION['s']['user']['client_id']);
73813a 250                 }
MC 251                 if($this->dataRecord['php'] == 'fast-cgi'){
2af58c 252                     $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = ? AND (client_id = 0 OR client_id=?)", $parent_domain['server_id'], $_SESSION['s']['user']['client_id']);
73813a 253                 }
76ebcb 254             }
F 255             $php_select = "<option value=''>Default</option>";
256             if(is_array($php_records) && !empty($php_records)) {
257                 foreach( $php_records as $php_record) {
d22277 258                     if($this->dataRecord['php'] == 'php-fpm' || ($this->dataRecord['php'] == 'hhvm' && $server_type == 'nginx')){
76ebcb 259                         $php_version = $php_record['name'].':'.$php_record['php_fpm_init_script'].':'.$php_record['php_fpm_ini_dir'].':'.$php_record['php_fpm_pool_dir'];
F 260                     } else {
261                         $php_version = $php_record['name'].':'.$php_record['php_fastcgi_binary'].':'.$php_record['php_fastcgi_ini_dir'];
262                     }
263                     $selected = ($php_version == $this->dataRecord["fastcgi_php_version"])?'SELECTED':'';
264                     $php_select .= "<option value='$php_version' $selected>".$php_record['name']."</option>\r\n";
265                 }
266             }
b1a6a5 267             $app->tpl->setVar("fastcgi_php_version", $php_select);
76ebcb 268             unset($php_records);
F 269
b1a6a5 270             // add limits to template to be able to hide settings
MC 271             foreach($read_limits as $limit) $app->tpl->setVar($limit, $client[$limit]);
272
273
76ebcb 274             //* Reseller: If the logged in user is not admin and has sub clients (is a reseller)
F 275         } elseif ($_SESSION["s"]["user"]["typ"] != 'admin' && $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
276
277             // Get the limits of the client
604c0c 278             $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
73813a 279
MC 280             if($this->_vhostdomain_type == 'domain') {
cc7a82 281                 $client = $app->db->queryOneRecord("SELECT client.client_id, client.limit_web_domain, client.web_servers, 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);
73813a 282                 $app->tpl->setVar('only_one_server', $only_one_server);
MC 283             } elseif($this->_vhostdomain_type == 'subdomain') {
cc7a82 284                 $client = $app->db->queryOneRecord("SELECT client.client_id, client.limit_web_subdomain, client.web_servers, 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);
73813a 285             } elseif($this->_vhostdomain_type == 'aliasdomain') {
cc7a82 286                 $client = $app->db->queryOneRecord("SELECT client.client_id, client.limit_web_aliasdomain, client.web_servers, 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);
73813a 287             }
MC 288
289             $client['web_servers_ids'] = explode(',', $client['web_servers']);
290             $only_one_server = count($client['web_servers_ids']) === 1;
b1a6a5 291
02384b 292             //* Get global web config
bd6659 293             foreach ($client['web_servers_ids'] as $web_server_id) {
SJ 294                 $web_config[$web_server_id] = $app->getconf->get_server_config($web_server_id, 'web');
295             }
b1a6a5 296
2af58c 297             $sql = "SELECT server_id, server_name FROM server WHERE server_id IN ?";
MC 298             $web_servers = $app->db->queryAllRecords($sql, $client['web_servers_ids']);
bd6659 299
SJ 300             $options_web_servers = "";
301
302             foreach ($web_servers as $web_server) {
75b7fc 303                 $options_web_servers .= '<option value="'.$web_server['server_id'].'"'.($this->id > 0 && $this->dataRecord["server_id"] == $web_server['server_id'] ? ' selected="selected"' : '').'>'.$web_server['server_name'].'</option>';
bd6659 304             }
SJ 305
306             $app->tpl->setVar("server_id", $options_web_servers);
307             unset($options_web_servers);
ebbc3f 308             
TB 309             if($this->id > 0) {
310                 if(!isset($this->dataRecord["server_id"])){
311                     $tmp = $app->db->queryOneRecord("SELECT server_id FROM web_domain WHERE domain_id = ?", $this->id);
312                     $this->dataRecord["server_id"] = $tmp["server_id"];
313                     unset($tmp);
314                 }
315                 $server_id = intval(@$this->dataRecord["server_id"]);
316             } else {
0d3e95 317                 $server_id = (isset($web_servers[0])) ? intval($web_servers[0]['server_id']) : 0;
ebbc3f 318             }
76ebcb 319
3e94c8 320             if ($settings['use_domain_module'] != 'y') {
SC 321                 // Fill the client select field
2af58c 322                 $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 = ? ORDER BY client.company_name, client.contact_name, sys_group.name";
MC 323                 $records = $app->db->queryAllRecords($sql, $client['client_id']);
324                 $tmp = $app->db->queryOneRecord("SELECT groupid FROM sys_group WHERE client_id = ?", $client['client_id']);
3e94c8 325                 $client_select = '<option value="'.$tmp['groupid'].'">'.$client['contactname'].'</option>';
SC 326                 //$tmp_data_record = $app->tform->getDataRecord($this->id);
327                 if(is_array($records)) {
328                     $selected_client_group_id = 0; // needed to get list of PHP versions
329                     foreach( $records as $rec) {
330                         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"];
331                         $selected = @(is_array($this->dataRecord) && ($rec["groupid"] == $this->dataRecord['client_group_id'] || $rec["groupid"] == $this->dataRecord['sys_groupid']))?'SELECTED':'';
332                         if($selected == 'SELECTED') $selected_client_group_id = $rec["groupid"];
333                         $client_select .= "<option value='$rec[groupid]' $selected>$rec[contactname]</option>\r\n";
334                     }
76ebcb 335                 }
3e94c8 336                 $app->tpl->setVar("client_group_id", $client_select);
76ebcb 337             }
F 338
cf646e 339             if($app->functions->intval($this->dataRecord["server_id"]) > 0) {
MC 340                 // check if server is in client's servers or add it.
341                 $chk_sid = explode(',', $client['web_servers']);
342                 if(in_array($this->dataRecord["server_id"], $client['web_servers']) == false) {
343                     if($client['web_servers'] != '') $client['web_servers'] .= ',';
344                     $client['web_servers'] .= $app->functions->intval($this->dataRecord["server_id"]);
345                 }
346             }
347             
76ebcb 348             //* Fill the IPv4 select field with the IP addresses that are allowed for this client
0d3e95 349             $sql = "SELECT ip_address FROM server_ip WHERE server_id = ? AND ip_type = 'IPv4' AND (client_id = 0 OR client_id=?)";
TB 350             $ips = $app->db->queryAllRecords($sql, $server_id, $_SESSION['s']['user']['client_id']);
1aece9 351             $ip_select = ($web_config[$server_id]['enable_ip_wildcard'] == 'y')?"<option value='*'>*</option>":"";
30848e 352             //if(!in_array($this->dataRecord["ip_address"], $ips)) $ip_select .= "<option value='".$this->dataRecord["ip_address"]."' SELECTED>".$this->dataRecord["ip_address"]."</option>\r\n";
76ebcb 353             //$ip_select = "";
F 354             if(is_array($ips)) {
355                 foreach( $ips as $ip) {
356                     $selected = ($ip["ip_address"] == $this->dataRecord["ip_address"])?'SELECTED':'';
357                     $ip_select .= "<option value='$ip[ip_address]' $selected>$ip[ip_address]</option>\r\n";
358                 }
359             }
b1a6a5 360             $app->tpl->setVar("ip_address", $ip_select);
76ebcb 361             unset($tmp);
F 362             unset($ips);
b1a6a5 363
76ebcb 364             //* Fill the IPv6 select field with the IP addresses that are allowed for this client
0d3e95 365             $sql = "SELECT ip_address FROM server_ip WHERE server_id = ? AND ip_type = 'IPv6' AND (client_id = 0 OR client_id=?)";
TB 366             $ips = $app->db->queryAllRecords($sql, $server_id, $_SESSION['s']['user']['client_id']);
76ebcb 367             $ip_select = "<option value=''></option>";
F 368             //$ip_select = "";
369             if(is_array($ips)) {
370                 foreach( $ips as $ip) {
371                     $selected = ($ip["ip_address"] == $this->dataRecord["ipv6_address"])?'SELECTED':'';
372                     $ip_select .= "<option value='$ip[ip_address]' $selected>$ip[ip_address]</option>\r\n";
373                 }
374             }
b1a6a5 375             $app->tpl->setVar("ipv6_address", $ip_select);
76ebcb 376             unset($tmp);
F 377             unset($ips);
b1a6a5 378
76ebcb 379             //PHP Version Selection (FastCGI)
F 380             $server_type = 'apache';
1aece9 381             if(!empty($web_config[$server_id]['server_type'])) $server_type = $web_config[$server_id]['server_type'];
76ebcb 382             if($server_type == 'nginx' && $this->dataRecord['php'] == 'fast-cgi') $this->dataRecord['php'] = 'php-fpm';
2af58c 383             $selected_client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE groupid = ?", $selected_client_group_id);
MC 384             $sql_where = " AND (client_id = 0 OR client_id = ?)";
73813a 385             if($this->_vhostdomain_type == 'domain') {
d22277 386                 if($this->dataRecord['php'] == 'php-fpm' || ($this->dataRecord['php'] == 'hhvm' && $server_type == 'nginx')){
2af58c 387                     $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 = ?".$sql_where, ($this->id > 0 ? $this->dataRecord['server_id'] : $client['default_webserver']), $selected_client['client_id']);
73813a 388                 }
MC 389                 if($this->dataRecord['php'] == 'fast-cgi') {
2af58c 390                     $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = ?".$sql_where, ($this->id > 0 ? $this->dataRecord['server_id'] : $client['default_webserver']), $selected_client['client_id']);
73813a 391                 }
MC 392             } else {
d22277 393                 if($this->dataRecord['php'] == 'php-fpm' || ($this->dataRecord['php'] == 'hhvm' && $server_type == 'nginx')){
2af58c 394                     $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 = ? AND (client_id = 0 OR client_id=?)", $parent_domain['server_id'], $_SESSION['s']['user']['client_id']);
73813a 395                 }
MC 396                 if($this->dataRecord['php'] == 'fast-cgi') {
2af58c 397                     $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = ? AND (client_id = 0 OR client_id=?)", $parent_domain['server_id'], $_SESSION['s']['user']['client_id']);
73813a 398                 }
76ebcb 399             }
F 400             $php_select = "<option value=''>Default</option>";
401             if(is_array($php_records) && !empty($php_records)) {
402                 foreach( $php_records as $php_record) {
d22277 403                     if($this->dataRecord['php'] == 'php-fpm' || ($this->dataRecord['php'] == 'hhvm' && $server_type == 'nginx')){
76ebcb 404                         $php_version = $php_record['name'].':'.$php_record['php_fpm_init_script'].':'.$php_record['php_fpm_ini_dir'].':'.$php_record['php_fpm_pool_dir'];
F 405                     } else {
406                         $php_version = $php_record['name'].':'.$php_record['php_fastcgi_binary'].':'.$php_record['php_fastcgi_ini_dir'];
407                     }
408                     $selected = ($php_version == $this->dataRecord["fastcgi_php_version"])?'SELECTED':'';
409                     $php_select .= "<option value='$php_version' $selected>".$php_record['name']."</option>\r\n";
410                 }
411             }
b1a6a5 412             $app->tpl->setVar("fastcgi_php_version", $php_select);
76ebcb 413             unset($php_records);
b1a6a5 414
MC 415             // add limits to template to be able to hide settings
416             foreach($read_limits as $limit) $app->tpl->setVar($limit, $client[$limit]);
417
418             $sites_config = $app->getconf->get_global_config('sites');
419             if($sites_config['reseller_can_use_options']) {
420                 // Directive Snippets
421                 $php_directive_snippets_txt = '';
d22277 422                 $php_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'php' AND active = 'y' AND master_directive_snippets_id > 0 ORDER BY name");
b1a6a5 423                 if(is_array($php_directive_snippets) && !empty($php_directive_snippets)){
d22277 424                     $php_directive_snippets_txt .= $app->tform->wordbook["select_master_directive_snippet_txt"].'<br>';
b1a6a5 425                     foreach($php_directive_snippets as $php_directive_snippet){
9e5a11 426                         $php_directive_snippet['snippet'] = PHP_EOL . PHP_EOL . $php_directive_snippet['snippet'] . PHP_EOL;
d22277 427                         $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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
MB 428                     }
429                     $php_directive_snippets_txt .= '<br><br>';
430                 }
431                 
432                 $php_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'php' AND active = 'y' AND master_directive_snippets_id = 0 ORDER BY name");
433                 if(is_array($php_directive_snippets) && !empty($php_directive_snippets)){
434                     $php_directive_snippets_txt .= $app->tform->wordbook["select_directive_snippet_txt"].'<br>';
435                     foreach($php_directive_snippets as $php_directive_snippet){
436                         $php_directive_snippet['snippet'] = PHP_EOL . PHP_EOL . $php_directive_snippet['snippet'] . PHP_EOL;
437                         $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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
b1a6a5 438                     }
MC 439                 }
440                 if($php_directive_snippets_txt == '') $php_directive_snippets_txt = '------';
441                 $app->tpl->setVar("php_directive_snippets_txt", $php_directive_snippets_txt);
442
443                 if($server_type == 'apache'){
444                     $apache_directive_snippets_txt = '';
d22277 445                     $apache_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'apache' AND active = 'y' AND master_directive_snippets_id > 0 ORDER BY name");
b1a6a5 446                     if(is_array($apache_directive_snippets) && !empty($apache_directive_snippets)){
d22277 447                         $apache_directive_snippets_txt .= $app->tform->wordbook["select_master_directive_snippet_txt"].'<br>';
b1a6a5 448                         foreach($apache_directive_snippets as $apache_directive_snippet){
9e5a11 449                             $apache_directive_snippet['snippet'] = PHP_EOL . PHP_EOL . $apache_directive_snippet['snippet'] . PHP_EOL;
d22277 450                             $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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
MB 451                         }
452                         $apache_directive_snippets_txt .= '<br><br>';
453                     }
454                     
455                     $apache_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'apache' AND active = 'y' AND master_directive_snippets_id = 0 ORDER BY name");
456                     if(is_array($apache_directive_snippets) && !empty($apache_directive_snippets)){
457                         $apache_directive_snippets_txt .= $app->tform->wordbook["select_directive_snippet_txt"].'<br>';
458                         foreach($apache_directive_snippets as $apache_directive_snippet){
459                             $apache_directive_snippet['snippet'] = PHP_EOL . PHP_EOL . $apache_directive_snippet['snippet'] . PHP_EOL;
460                             $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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
b1a6a5 461                         }
MC 462                     }
463                     if($apache_directive_snippets_txt == '') $apache_directive_snippets_txt = '------';
464                     $app->tpl->setVar("apache_directive_snippets_txt", $apache_directive_snippets_txt);
465                 }
466
467                 if($server_type == 'nginx'){
468                     $nginx_directive_snippets_txt = '';
d22277 469                     $nginx_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'nginx' AND active = 'y' AND master_directive_snippets_id > 0 ORDER BY name");
b1a6a5 470                     if(is_array($nginx_directive_snippets) && !empty($nginx_directive_snippets)){
d22277 471                         $nginx_directive_snippets_txt .= $app->tform->wordbook["select_master_directive_snippet_txt"].'<br>';
b1a6a5 472                         foreach($nginx_directive_snippets as $nginx_directive_snippet){
9e5a11 473                             $nginx_directive_snippet['snippet'] = PHP_EOL . PHP_EOL . $nginx_directive_snippet['snippet'] . PHP_EOL;
d22277 474                             $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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
MB 475                         }
476                         $nginx_directive_snippets_txt .= '<br><br>';
477                     }
478                     
479                     $nginx_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'nginx' AND active = 'y' AND master_directive_snippets_id = 0 ORDER BY name");
480                     if(is_array($nginx_directive_snippets) && !empty($nginx_directive_snippets)){
481                         $nginx_directive_snippets_txt .= $app->tform->wordbook["select_directive_snippet_txt"].'<br>';
482                         foreach($nginx_directive_snippets as $nginx_directive_snippet){
483                             $nginx_directive_snippet['snippet'] = PHP_EOL . PHP_EOL . $nginx_directive_snippet['snippet'] . PHP_EOL;
484                             $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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
b1a6a5 485                         }
MC 486                     }
487                     if($nginx_directive_snippets_txt == '') $nginx_directive_snippets_txt = '------';
488                     $app->tpl->setVar("nginx_directive_snippets_txt", $nginx_directive_snippets_txt);
489                 }
490
491                 $proxy_directive_snippets_txt = '';
d22277 492                 $proxy_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'proxy' AND active = 'y' AND master_directive_snippets_id > 0 ORDER BY name");
b1a6a5 493                 if(is_array($proxy_directive_snippets) && !empty($proxy_directive_snippets)){
d22277 494                     $proxy_directive_snippets_txt .= $app->tform->wordbook["select_master_directive_snippet_txt"].'<br>';
b1a6a5 495                     foreach($proxy_directive_snippets as $proxy_directive_snippet){
9e5a11 496                         $proxy_directive_snippet['snippet'] = PHP_EOL . PHP_EOL . $proxy_directive_snippet['snippet'] . PHP_EOL;
d22277 497                         $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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
MB 498                     }
499                     $proxy_directive_snippets_txt .= '<br><br>';
500                 }
501                 
502                 $proxy_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'proxy' AND active = 'y' AND master_directive_snippets_id = 0 ORDER BY name");
503                 if(is_array($proxy_directive_snippets) && !empty($proxy_directive_snippets)){
504                     $proxy_directive_snippets_txt .= $app->tform->wordbook["select_directive_snippet_txt"].'<br>';
505                     foreach($proxy_directive_snippets as $proxy_directive_snippet){
506                         $proxy_directive_snippet['snippet'] = PHP_EOL . PHP_EOL . $proxy_directive_snippet['snippet'] . PHP_EOL;
507                         $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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
b1a6a5 508                     }
MC 509                 }
510                 if($proxy_directive_snippets_txt == '') $proxy_directive_snippets_txt = '------';
511                 $app->tpl->setVar("proxy_directive_snippets_txt", $proxy_directive_snippets_txt);
512             }
513
76ebcb 514             //* Admin: If the logged in user is admin
F 515         } else {
8173c6 516         
FT 517             $is_admin = true;
76ebcb 518
73813a 519             if($this->_vhostdomain_type == 'domain') {
MC 520                 // The user is admin, so we fill in all IP addresses of the server
521                 if($this->id > 0) {
522                     if(!isset($this->dataRecord["server_id"])){
2af58c 523                         $tmp = $app->db->queryOneRecord("SELECT server_id FROM web_domain WHERE domain_id = ?", $this->id);
73813a 524                         $this->dataRecord["server_id"] = $tmp["server_id"];
MC 525                         unset($tmp);
526                     }
527                     $server_id = intval(@$this->dataRecord["server_id"]);
528                 } else {
eed9fb 529                     $settings = $app->getconf->get_global_config('sites');
SC 530                     $server_id = intval($settings['default_webserver']);
531                     if (!$server_id) {
532                         // Get the first server ID
533                         $tmp = $app->db->queryOneRecord("SELECT server_id FROM server WHERE web_server = 1 ORDER BY server_name LIMIT 0,1");
534                         $server_id = intval($tmp['server_id']);
535                     }
76ebcb 536                 }
b1a6a5 537
73813a 538                 //* get global web config
MC 539                 $web_config = $app->getconf->get_server_config($server_id, 'web');
540             } else {
541                 //* get global web config
542                 $web_config = $app->getconf->get_server_config($parent_domain['server_id'], 'web');
543             }
b1a6a5 544
76ebcb 545             //* Fill the IPv4 select field
2af58c 546             $sql = "SELECT ip_address FROM server_ip WHERE ip_type = 'IPv4' AND server_id = ?";
MC 547             $ips = $app->db->queryAllRecords($sql, $server_id);
02384b 548             $ip_select = ($web_config['enable_ip_wildcard'] == 'y')?"<option value='*'>*</option>":"";
76ebcb 549             //$ip_select = "";
F 550             if(is_array($ips)) {
551                 foreach( $ips as $ip) {
552                     $selected = ($ip["ip_address"] == $this->dataRecord["ip_address"])?'SELECTED':'';
553                     $ip_select .= "<option value='$ip[ip_address]' $selected>$ip[ip_address]</option>\r\n";
554                 }
555             }
b1a6a5 556             $app->tpl->setVar("ip_address", $ip_select);
76ebcb 557             unset($tmp);
F 558             unset($ips);
b1a6a5 559
76ebcb 560             //* Fill the IPv6 select field
2af58c 561             $sql = "SELECT ip_address FROM server_ip WHERE ip_type = 'IPv6' AND server_id = ?";
MC 562             $ips = $app->db->queryAllRecords($sql, $server_id);
76ebcb 563             $ip_select = "<option value=''></option>";
F 564             //$ip_select = "";
565             if(is_array($ips)) {
566                 foreach( $ips as $ip) {
567                     $selected = ($ip["ip_address"] == $this->dataRecord["ipv6_address"])?'SELECTED':'';
568                     $ip_select .= "<option value='$ip[ip_address]' $selected>$ip[ip_address]</option>\r\n";
569                 }
570             }
b1a6a5 571             $app->tpl->setVar("ipv6_address", $ip_select);
76ebcb 572             unset($tmp);
F 573             unset($ips);
7b47c0 574
3e94c8 575             if ($settings['use_domain_module'] != 'y') {
d22277 576                 if(!isset($this->dataRecord["sys_groupid"])){
MB 577                     $tmp = $app->db->queryOneRecord("SELECT sys_groupid FROM web_domain WHERE domain_id = ".$app->functions->intval($this->id));
578                     $this->dataRecord["sys_groupid"] = $tmp["sys_groupid"];
579                 }
3e94c8 580                 // Fill the client select field
SC 581                 $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";
582                 $clients = $app->db->queryAllRecords($sql);
583                 $client_select = "<option value='0'></option>";
584                 //$tmp_data_record = $app->tform->getDataRecord($this->id);
585                 if(is_array($clients)) {
586                     $selected_client_group_id = 0; // needed to get list of PHP versions
587                     foreach($clients as $client) {
588                         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"];
589                         //$selected = @($client["groupid"] == $tmp_data_record["sys_groupid"])?'SELECTED':'';
590                         $selected = @(is_array($this->dataRecord) && ($client["groupid"] == $this->dataRecord['client_group_id'] || $client["groupid"] == $this->dataRecord['sys_groupid']))?'SELECTED':'';
591                         if($selected == 'SELECTED') $selected_client_group_id = $client["groupid"];
592                         $client_select .= "<option value='$client[groupid]' $selected>$client[contactname]</option>\r\n";
593                     }
7b47c0 594                 }
3e94c8 595                 $app->tpl->setVar("client_group_id", $client_select);
7b47c0 596             }
b1a6a5 597
76ebcb 598             //PHP Version Selection (FastCGI)
F 599             $server_type = 'apache';
600             if(!empty($web_config['server_type'])) $server_type = $web_config['server_type'];
601             if($server_type == 'nginx' && $this->dataRecord['php'] == 'fast-cgi') $this->dataRecord['php'] = 'php-fpm';
2af58c 602             $selected_client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE groupid = ?", $selected_client_group_id);
MC 603             $sql_where = " AND (client_id = 0 OR client_id = ?)";
73813a 604             if($this->_vhostdomain_type == 'domain') {
d22277 605                 if($this->dataRecord['php'] == 'php-fpm' || ($this->dataRecord['php'] == 'hhvm' && $server_type == 'nginx')){
2af58c 606                     $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 = ?".$sql_where, $server_id, $selected_client['client_id']);
73813a 607                 }
MC 608                 if($this->dataRecord['php'] == 'fast-cgi') {
2af58c 609                     $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = ?".$sql_where, $server_id, $selected_client['client_id']);
73813a 610                 }
MC 611             } else {
d22277 612                 if($this->dataRecord['php'] == 'php-fpm' || ($this->dataRecord['php'] == 'hhvm' && $server_type == 'nginx')){
2af58c 613                     $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 = ?", $parent_domain['server_id']);
73813a 614                 }
MC 615                 if($this->dataRecord['php'] == 'fast-cgi') {
2af58c 616                     $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = ?", $parent_domain['server_id']);
73813a 617                 }
76ebcb 618             }
F 619             $php_select = "<option value=''>Default</option>";
620             if(is_array($php_records) && !empty($php_records)) {
621                 foreach( $php_records as $php_record) {
d22277 622                     if($this->dataRecord['php'] == 'php-fpm' || ($this->dataRecord['php'] == 'hhvm' && $server_type == 'nginx')){
76ebcb 623                         $php_version = $php_record['name'].':'.$php_record['php_fpm_init_script'].':'.$php_record['php_fpm_ini_dir'].':'.$php_record['php_fpm_pool_dir'];
F 624                     } else {
625                         $php_version = $php_record['name'].':'.$php_record['php_fastcgi_binary'].':'.$php_record['php_fastcgi_ini_dir'];
626                     }
627                     $selected = ($php_version == $this->dataRecord["fastcgi_php_version"])?'SELECTED':'';
628                     $php_select .= "<option value='$php_version' $selected>".$php_record['name']."</option>\r\n";
629                 }
630             }
b1a6a5 631             $app->tpl->setVar("fastcgi_php_version", $php_select);
76ebcb 632             unset($php_records);
b1a6a5 633
MC 634             foreach($read_limits as $limit) $app->tpl->setVar($limit, ($limit == 'force_suexec' ? 'n' : 'y'));
635
ef55b5 636             // Directive Snippets
F 637             $php_directive_snippets_txt = '';
d22277 638             $php_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'php' AND active = 'y' AND master_directive_snippets_id > 0 ORDER BY name");
ef55b5 639             if(is_array($php_directive_snippets) && !empty($php_directive_snippets)){
d22277 640                 $php_directive_snippets_txt .= $app->tform->wordbook["select_master_directive_snippet_txt"].'<br>';
b1a6a5 641                 foreach($php_directive_snippets as $php_directive_snippet){
9e5a11 642                     $php_directive_snippet['snippet'] = PHP_EOL . PHP_EOL . $php_directive_snippet['snippet'] . PHP_EOL;
d22277 643                     $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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
MB 644                 }
645                 $php_directive_snippets_txt .= '<br><br>';
646             }
647             
648             $php_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'php' AND active = 'y' AND master_directive_snippets_id = 0 ORDER BY name");
649             if(is_array($php_directive_snippets) && !empty($php_directive_snippets)){
650                 $php_directive_snippets_txt .= $app->tform->wordbook["select_directive_snippet_txt"].'<br>';
651                 foreach($php_directive_snippets as $php_directive_snippet){
652                     $php_directive_snippet['snippet'] = PHP_EOL . PHP_EOL . $php_directive_snippet['snippet'] . PHP_EOL;
653                     $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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
b1a6a5 654                 }
ef55b5 655             }
F 656             if($php_directive_snippets_txt == '') $php_directive_snippets_txt = '------';
b1a6a5 657             $app->tpl->setVar("php_directive_snippets_txt", $php_directive_snippets_txt);
MC 658
ef55b5 659             if($server_type == 'apache'){
F 660                 $apache_directive_snippets_txt = '';
d22277 661                 $apache_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'apache' AND active = 'y' AND master_directive_snippets_id > 0 ORDER BY name");
ef55b5 662                 if(is_array($apache_directive_snippets) && !empty($apache_directive_snippets)){
d22277 663                     $apache_directive_snippets_txt .= $app->tform->wordbook["select_master_directive_snippet_txt"].'<br>';
b1a6a5 664                     foreach($apache_directive_snippets as $apache_directive_snippet){
9e5a11 665                         $apache_directive_snippet['snippet'] = PHP_EOL . PHP_EOL . $apache_directive_snippet['snippet'] . PHP_EOL;
d22277 666                         $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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
MB 667                     }
668                     $apache_directive_snippets_txt .= '<br><br>';
669                 }
670                 
671                 $apache_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'apache' AND active = 'y' AND master_directive_snippets_id = 0 ORDER BY name");
672                 if(is_array($apache_directive_snippets) && !empty($apache_directive_snippets)){
673                     $apache_directive_snippets_txt .= $app->tform->wordbook["select_directive_snippet_txt"].'<br>';
674                     foreach($apache_directive_snippets as $apache_directive_snippet){
675                         $apache_directive_snippet['snippet'] = PHP_EOL . PHP_EOL . $apache_directive_snippet['snippet'] . PHP_EOL;
676                         $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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
b1a6a5 677                     }
ef55b5 678                 }
F 679                 if($apache_directive_snippets_txt == '') $apache_directive_snippets_txt = '------';
b1a6a5 680                 $app->tpl->setVar("apache_directive_snippets_txt", $apache_directive_snippets_txt);
ef55b5 681             }
b1a6a5 682
3a93f2 683             if($server_type == 'nginx'){
ef55b5 684                 $nginx_directive_snippets_txt = '';
d22277 685                 $nginx_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'nginx' AND active = 'y' AND master_directive_snippets_id > 0 ORDER BY name");
ef55b5 686                 if(is_array($nginx_directive_snippets) && !empty($nginx_directive_snippets)){
d22277 687                     $nginx_directive_snippets_txt .= $app->tform->wordbook["select_master_directive_snippet_txt"].'<br>';
b1a6a5 688                     foreach($nginx_directive_snippets as $nginx_directive_snippet){
9e5a11 689                         $nginx_directive_snippet['snippet'] = PHP_EOL . PHP_EOL . $nginx_directive_snippet['snippet'] . PHP_EOL;
d22277 690                         $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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
MB 691                     }
692                     $nginx_directive_snippets_txt .= '<br><br>';
693                 }
694                 
695                 $nginx_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'nginx' AND active = 'y' AND master_directive_snippets_id = 0 ORDER BY name");
696                 if(is_array($nginx_directive_snippets) && !empty($nginx_directive_snippets)){
697                     $nginx_directive_snippets_txt .= $app->tform->wordbook["select_directive_snippet_txt"].'<br>';
698                     foreach($nginx_directive_snippets as $nginx_directive_snippet){
699                         $nginx_directive_snippet['snippet'] = PHP_EOL . PHP_EOL . $nginx_directive_snippet['snippet'] . PHP_EOL;
700                         $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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
b1a6a5 701                     }
ef55b5 702                 }
F 703                 if($nginx_directive_snippets_txt == '') $nginx_directive_snippets_txt = '------';
b1a6a5 704                 $app->tpl->setVar("nginx_directive_snippets_txt", $nginx_directive_snippets_txt);
ef55b5 705             }
b1a6a5 706
bfcdef 707             $proxy_directive_snippets_txt = '';
d22277 708             $proxy_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'proxy' AND active = 'y' AND master_directive_snippets_id > 0 ORDER BY name");
bfcdef 709             if(is_array($proxy_directive_snippets) && !empty($proxy_directive_snippets)){
d22277 710                 $proxy_directive_snippets_txt .= $app->tform->wordbook["select_master_directive_snippet_txt"].'<br>';
b1a6a5 711                 foreach($proxy_directive_snippets as $proxy_directive_snippet){
9e5a11 712                     $proxy_directive_snippet['snippet'] = PHP_EOL . PHP_EOL . $proxy_directive_snippet['snippet'] . PHP_EOL;
d22277 713                     $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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
MB 714                 }
715                 $proxy_directive_snippets_txt .= '<br><br>';
716             }
717             
718             $proxy_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'proxy' AND active = 'y' AND master_directive_snippets_id = 0 ORDER BY name");
719             if(is_array($proxy_directive_snippets) && !empty($proxy_directive_snippets)){
720                 $proxy_directive_snippets_txt .= $app->tform->wordbook["select_directive_snippet_txt"].'<br>';
721                 foreach($proxy_directive_snippets as $proxy_directive_snippet){
722                     $proxy_directive_snippet['snippet'] = PHP_EOL . PHP_EOL . $proxy_directive_snippet['snippet'] . PHP_EOL;
723                     $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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
b1a6a5 724                 }
bfcdef 725             }
T 726             if($proxy_directive_snippets_txt == '') $proxy_directive_snippets_txt = '------';
b1a6a5 727             $app->tpl->setVar("proxy_directive_snippets_txt", $proxy_directive_snippets_txt);
76ebcb 728         }
F 729
730         $ssl_domain_select = '';
797215 731         $ssl_domains = array();
2af58c 732         $tmpd = $app->db->queryAllRecords("SELECT domain, type FROM web_domain WHERE domain_id = ? OR parent_domain_id = ?", $this->id, $this->id);
797215 733         foreach($tmpd as $tmp) {
MC 734             if($tmp['type'] == 'subdomain' || $tmp['type'] == 'vhostsubdomain') {
735                 $ssl_domains[] = $tmp["domain"];
736             } else {
737                 $ssl_domains = array_merge($ssl_domains, array($tmp["domain"],'www.'.$tmp["domain"],'*.'.$tmp["domain"]));
738             }
739         }
76ebcb 740         if(is_array($ssl_domains)) {
F 741             foreach( $ssl_domains as $ssl_domain) {
742                 $selected = ($ssl_domain == $this->dataRecord['ssl_domain'])?'SELECTED':'';
024e13 743                 $ssl_domain_select .= "<option value='$ssl_domain' $selected>".$app->functions->idn_decode($ssl_domain)."</option>\r\n";
76ebcb 744             }
F 745         }
b1a6a5 746         $app->tpl->setVar("ssl_domain", $ssl_domain_select);
76ebcb 747         unset($ssl_domain_select);
F 748         unset($ssl_domains);
749         unset($ssl_domain);
750
751         if($this->id > 0) {
752             //* we are editing a existing record
753             $app->tpl->setVar("edit_disabled", 1);
73813a 754             $app->tpl->setVar('fixed_folder', 'y');
568b4b 755             if($this->_vhostdomain_type == 'domain') {
FS 756                 $app->tpl->setVar("server_id_value", $this->dataRecord["server_id"]);
757                 $app->tpl->setVar("document_root", $this->dataRecord["document_root"]);
758             }
73813a 759             else $app->tpl->setVar('server_id_value', $parent_domain['server_id']);
76ebcb 760         } else {
F 761             $app->tpl->setVar("edit_disabled", 0);
73813a 762             $app->tpl->setVar('fixed_folder', 'n');
MC 763             if($this->_vhostdomain_type != 'domain') $app->tpl->setVar('server_id_value', $parent_domain['server_id']);
76ebcb 764         }
F 765
766         $tmp_txt = ($this->dataRecord['traffic_quota_lock'] == 'y')?'<b>('.$app->tform->lng('traffic_quota_exceeded_txt').')</b>':'';
767         $app->tpl->setVar("traffic_quota_exceeded_txt", $tmp_txt);
768
769         /*
770          * Now we have to check, if we should use the domain-module to select the domain
771          * or not
772          */
f93927 773         $settings = $app->getconf->get_global_config('domains');
76ebcb 774         if ($settings['use_domain_module'] == 'y') {
F 775             /*
776              * The domain-module is in use.
777             */
9ec304 778             $domains = $app->tools_sites->getDomainModuleDomains($this->_vhostdomain_type == 'subdomain' ? null : "web_domain", $this->dataRecord["domain"]);
76ebcb 779             $domain_select = '';
73813a 780             $selected_domain = '';
76ebcb 781             if(is_array($domains) && sizeof($domains) > 0) {
F 782                 /* We have domains in the list, so create the drop-down-list */
783                 foreach( $domains as $domain) {
4e18bd 784                     $domain_select .= "<option value=" . $domain['domain_id'] ;
73813a 785                     if ($this->_vhostdomain_type == 'subdomain' && '.' . $domain['domain'] == substr($this->dataRecord["domain"], -strlen($domain['domain']) - 1)) {
MC 786                         $domain_select .= " selected";
787                         $selected_domain = $domain['domain'];
788                     } elseif($this->_vhostdomain_type == 'aliasdomain' && $domain['domain'] == $this->dataRecord["domain"]) {
789                         $domain_select .= " selected";
790                     } elseif($this->_vhostdomain_type == 'domain' && $domain['domain'] == $this->dataRecord["domain"]) {
76ebcb 791                         $domain_select .= " selected";
F 792                     }
8c1761 793                     $domain_select .= ">" . $app->functions->idn_decode($domain['domain']) . "</option>\r\n";
76ebcb 794                 }
F 795             }
796             else {
797                 /*
798                  * We have no domains in the domain-list. This means, we can not add ANY new domain.
799                  * To avoid, that the variable "domain_option" is empty and so the user can
800                  * free enter a domain, we have to create a empty option!
801                 */
802                 $domain_select .= "<option value=''></option>\r\n";
803             }
b1a6a5 804             $app->tpl->setVar("domain_option", $domain_select);
5501be 805             
TB 806             // remove the parent domain part of the domain name before we show it in the text field.
807             if($this->dataRecord["type"] == 'vhostsubdomain') $this->dataRecord["domain"] = str_replace('.'.$selected_domain, '', $this->dataRecord["domain"]);
0998a3 808
5501be 809         
TB 810         } else {
0998a3 811             // remove the parent domain part of the domain name before we show it in the text field.
MC 812             if($this->dataRecord["type"] == 'vhostsubdomain') $this->dataRecord["domain"] = str_replace('.'.$parent_domain["domain"], '', $this->dataRecord["domain"]);
7b47c0 813         }
5501be 814         
73813a 815         if($this->_vhostdomain_type != 'domain') $app->tpl->setVar("domain", $this->dataRecord["domain"]);
b1a6a5 816
7b47c0 817         // check for configuration errors in sys_datalog
T 818         if($this->id > 0) {
2af58c 819             $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 = ? 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", 'domain_id:' . $this->id);
7b47c0 820             if(is_array($datalog) && !empty($datalog)){
T 821                 if(trim($datalog['error']) != ''){
b1a6a5 822                     $app->tpl->setVar("config_error_msg", nl2br(htmlentities($datalog['error'])));
MC 823                     $app->tpl->setVar("config_error_tstamp", date($app->lng('conf_format_datetime'), $datalog['tstamp']));
7b47c0 824                 }
T 825             }
76ebcb 826         }
73813a 827         
MC 828         $app->tpl->setVar('vhostdomain_type', $this->_vhostdomain_type);
76ebcb 829
dcf94e 830         $app->tpl->setVar('is_spdy_enabled', ($web_config['enable_spdy'] === 'y'));
8173c6 831         $app->tpl->setVar("is_admin", $is_admin);
FT 832         
833         if($this->id > 0) {
834             $tmp_web = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ?", intval($this->id));
835             $tmp_sys_group = $app->db->queryOneRecord("SELECT * FROM sys_group WHERE groupid = ?", intval($tmp_web['sys_groupid']));
836             if(intval($tmp_sys_group['client_id']) > 0) $tmp_client = $app->db->queryOneRecord("SELECT * FROM client WHERE client_id = ?", intval($tmp_sys_group['client_id']));
837             if(is_array($tmp_client) && !empty($tmp_client) && trim($this->dataRecord['ssl_organisation']) == '' && trim($this->dataRecord['ssl_locality']) == '' && trim($this->dataRecord['ssl_state']) == '' && trim($this->dataRecord['ssl_organisation_unit']) == '') $app->tpl->setVar("show_helper_links", true);
838         }
dcf94e 839
d5f2d5 840         $sys_config = $app->getconf->get_global_config('misc');
MC 841         if($sys_config['use_combobox'] == 'y') {
842             $app->tpl->setVar('use_combobox', 'y');
843         }
d22277 844         
MB 845         $directive_snippets_id_select = '<option value="0"'.($this->dataRecord['directive_snippets_id'] == 0? ' selected="selected"' : '').'>-</option>';
846         $server_type = $app->getconf->get_server_config($server_id, 'web');
847         $server_type = $server_type['server_type'];
848         
849         $m_directive_snippets = $app->db->queryAllRecords("SELECT directive_snippets_id, name FROM directive_snippets WHERE customer_viewable = 'y' AND active = 'y' AND master_directive_snippets_id > 0 AND type = ? ORDER BY name ASC", $server_type);
850         if(is_array($m_directive_snippets) && !empty($m_directive_snippets)){
851             $directive_snippets_id_select .= '<optgroup label="'.$app->tform->wordbook["select_master_directive_snippet_txt"].'">';
852             foreach($m_directive_snippets as $m_directive_snippet){
853                 $directive_snippets_id_select .= '<option value="'.$m_directive_snippet['directive_snippets_id'].'"'.($this->dataRecord['directive_snippets_id'] == $m_directive_snippet['directive_snippets_id']? ' selected="selected"' : '').'>'.$m_directive_snippet['name'].'</option>';
854             }
855             $directive_snippets_id_select .= '</optgroup>';
856         }
857         
858         $directive_snippets = $app->db->queryAllRecords("SELECT directive_snippets_id, name FROM directive_snippets WHERE customer_viewable = 'y' AND active = 'y' AND master_directive_snippets_id = 0 AND type = ? ORDER BY name ASC", $server_type);
859         if(is_array($directive_snippets) && !empty($directive_snippets)){
860             $directive_snippets_id_select .= '<optgroup label="'.$app->tform->wordbook["select_directive_snippet_txt"].'">';
861             foreach($directive_snippets as $directive_snippet){
862                 $directive_snippets_id_select .= '<option value="'.$directive_snippet['directive_snippets_id'].'"'.($this->dataRecord['directive_snippets_id'] == $directive_snippet['directive_snippets_id']? ' selected="selected"' : '').'>'.$directive_snippet['name'].'</option>';
863             }
864             $directive_snippets_id_select .= '</optgroup>';
865         }
866         $app->tpl->setVar("directive_snippets_id", $directive_snippets_id_select);
867         
868         // folder_directive_snippets
869         if(isset($_POST['folder_directive_snippets']) && !isset($this->dataRecord['folder_directive_snippets'])){
870             $this->dataRecord['folder_directive_snippets'] = '';
871             if(is_array($_POST['folder_directive_snippets']) && !empty($_POST['folder_directive_snippets'])){
872                 foreach($_POST['folder_directive_snippets'] as $folder_directive_snippet){
873                     if(trim($folder_directive_snippet['folder']) != '' && intval($folder_directive_snippet['snippets_id']) > 0) $this->dataRecord['folder_directive_snippets'] .= trim($folder_directive_snippet['folder']).':'.intval($folder_directive_snippet['snippets_id'])."\n";
874                 }
875             }
876             $this->dataRecord['folder_directive_snippets'] = trim($this->dataRecord['folder_directive_snippets']);
877         }
878         
879         $master_directive_snippets = $app->db->queryAllRecords("SELECT directive_snippets_id, name FROM directive_snippets WHERE customer_viewable = 'y' AND active = 'y' AND snippet LIKE '%{FOLDER}%' AND master_directive_snippets_id > 0 AND type = ? ORDER BY name ASC", $server_type);
880         $c_directive_snippets = $app->db->queryAllRecords("SELECT directive_snippets_id, name FROM directive_snippets WHERE customer_viewable = 'y' AND active = 'y' AND snippet LIKE '%{FOLDER}%' AND master_directive_snippets_id = 0 AND type = ? ORDER BY name ASC", $server_type);
881         
882         $folder_directive_snippets = array();
883         $this->dataRecord['folder_directive_snippets'] = str_replace("\r\n", "\n", $this->dataRecord['folder_directive_snippets']);
884         $this->dataRecord['folder_directive_snippets'] = str_replace("\r", "\n", $this->dataRecord['folder_directive_snippets']);
885         $folder_directive_snippets_lines = explode("\n", trim($this->dataRecord['folder_directive_snippets']));
886         for($i=0;$i<sizeof($folder_directive_snippets_lines)+50;$i++){
887             $folder_directive_snippets[$i]['folder_directive_snippets_index'] = $i;
888             $folder_directive_snippets[$i]['folder_directive_snippets_index_plus_1'] = $i + 1;
889             if($i > sizeof($folder_directive_snippets_lines)){
890                 $folder_directive_snippets[$i]['folder_directive_snippets_css'] = 'hidden';
891             } else {
892                 $folder_directive_snippets[$i]['folder_directive_snippets_css'] = '';
893             }
894             if(trim($folder_directive_snippets_lines[$i]) != ''){
895                 list($folder_directive_snippets[$i]['folder_directive_snippets_folder'], $selected_snippet) = explode(':', trim($folder_directive_snippets_lines[$i]));
896                 $folder_directive_snippets[$i]['folder_directive_snippets_id'] = '<option value="0">-</option>';
897                 if(is_array($master_directive_snippets) && !empty($master_directive_snippets)){
898                     $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '<optgroup label="'.$app->tform->wordbook["select_master_directive_snippet_txt"].'">';
899                     foreach($master_directive_snippets as $master_directive_snippet){
900                         $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '<option value="'.$master_directive_snippet['directive_snippets_id'].'"'.($master_directive_snippet['directive_snippets_id'] == $selected_snippet ? ' selected="selected"' : '').'>'.$master_directive_snippet['name'].'</option>';
901                     }
902                     $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '</optgroup>';
903                 }
904                 
905                 if(is_array($c_directive_snippets) && !empty($c_directive_snippets)){
906                     $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '<optgroup label="'.$app->tform->wordbook["select_directive_snippet_txt"].'">';
907                     foreach($c_directive_snippets as $c_directive_snippet){
908                         $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '<option value="'.$c_directive_snippet['directive_snippets_id'].'"'.($c_directive_snippet['directive_snippets_id'] == $selected_snippet? ' selected="selected"' : '').'>'.$c_directive_snippet['name'].'</option>';
909                     }
910                     $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '</optgroup>';
911                 }
912             } else {
913                 $folder_directive_snippets[$i]['folder_directive_snippets_folder'] = '';
914                 $folder_directive_snippets[$i]['folder_directive_snippets_id'] = '<option value="0">-</option>';
915                 if(is_array($master_directive_snippets) && !empty($master_directive_snippets)){
916                     $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '<optgroup label="'.$app->tform->wordbook["select_master_directive_snippet_txt"].'">';
917                     foreach($master_directive_snippets as $master_directive_snippet){
918                         $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '<option value="'.$master_directive_snippet['directive_snippets_id'].'">'.$master_directive_snippet['name'].'</option>';
919                     }
920                     $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '</optgroup>';
921                 }
922                 
923                 if(is_array($c_directive_snippets) && !empty($c_directive_snippets)){
924                     $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '<optgroup label="'.$app->tform->wordbook["select_directive_snippet_txt"].'">';
925                     foreach($c_directive_snippets as $c_directive_snippet){
926                         $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '<option value="'.$c_directive_snippet['directive_snippets_id'].'">'.$c_directive_snippet['name'].'</option>';
927                     }
928                     $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '</optgroup>';
929                 }
930             }
931         }
932         $app->tpl->setLoop('folder_directive_snippets', $folder_directive_snippets);
933
76ebcb 934         parent::onShowEnd();
F 935     }
b1a6a5 936
MC 937     function onShowEdit() {
938         global $app;
939         if($app->tform->checkPerm($this->id, 'riud')) $app->tform->formDef['tabs']['domain']['readonly'] = false;
940         parent::onShowEdit();
941     }
76ebcb 942
F 943     function onSubmit() {
944         global $app, $conf;
945
73813a 946         // Set a few fixed values
MC 947         $this->dataRecord["vhost_type"] = 'name';
948         if($this->_vhostdomain_type == 'domain') {
949             $this->dataRecord["parent_domain_id"] = 0;
950             $this->dataRecord["type"] = 'vhost';
951         } else {
952             // Get the record of the parent domain
953             if(!@$this->dataRecord["parent_domain_id"] && $this->id) {
2af58c 954                 $tmp = $app->db->queryOneRecord("SELECT parent_domain_id FROM web_domain WHERE domain_id = ?", $this->id);
73813a 955                 if($tmp) $this->dataRecord["parent_domain_id"] = $tmp['parent_domain_id'];
MC 956                 unset($tmp);
957             }
958
2af58c 959             $parent_domain = $app->db->queryOneRecord("select * FROM web_domain WHERE domain_id = ? AND ".$app->tform->getAuthSQL('r'), @$this->dataRecord["parent_domain_id"]);
73813a 960             if(!$parent_domain || $parent_domain['domain_id'] != @$this->dataRecord['parent_domain_id']) $app->tform->errorMessage .= $app->tform->lng("no_domain_perm");
MC 961
962             if($this->_vhostdomain_type == 'subdomain') {
963                 $this->dataRecord["type"] = 'vhostsubdomain';
964             } else {
965                 $this->dataRecord["type"] = 'vhostalias';
966             }
967             $this->dataRecord["server_id"] = $parent_domain["server_id"];
968             $this->dataRecord["ip_address"] = $parent_domain["ip_address"];
969             $this->dataRecord["ipv6_address"] = $parent_domain["ipv6_address"];
970             $this->dataRecord["client_group_id"] = $parent_domain["client_group_id"];
971
972             $this->parent_domain_record = $parent_domain;
973         }
974
67b0cb 975         $read_limits = array('limit_cgi', 'limit_ssi', 'limit_perl', 'limit_ruby', 'limit_python', 'force_suexec', 'limit_hterror', 'limit_wildcard', 'limit_ssl', 'limit_ssl_letsencrypt', 'limit_directive_snippets');
73813a 976
b1a6a5 977         /* check if the domain module is used - and check if the selected domain can be used! */
f601d9 978         if($app->tform->getCurrentTab() == 'domain') {
ebbe63 979             if($this->_vhostdomain_type == 'subdomain') {
MC 980                 // Check that domain (the subdomain part) is not empty
981                 if(!preg_match('/^[a-zA-Z0-9].*/',$this->dataRecord['domain'])) {
982                     $app->tform->errorMessage .= $app->tform->lng("subdomain_error_empty")."<br />";
983                 }
984             }
985             
986             /* check if the domain module is used - and check if the selected domain can be used! */
b1a6a5 987             $app->uses('ini_parser,getconf');
MC 988             $settings = $app->getconf->get_global_config('domains');
989             if ($settings['use_domain_module'] == 'y') {
73813a 990                 if($this->_vhostdomain_type == 'subdomain') $domain_check = $app->tools_sites->checkDomainModuleDomain($this->dataRecord['sel_domain']);
MC 991                 else $domain_check = $app->tools_sites->checkDomainModuleDomain($this->dataRecord['domain']);
b1a6a5 992                 if(!$domain_check) {
MC 993                     // invalid domain selected
994                     $app->tform->errorMessage .= $app->tform->lng("domain_error_empty")."<br />";
995                 } else {
3e94c8 996                     if ($this->_vhostdomain_type == 'domain' &&
SC 997                             ($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid']))) {
998                         $this->dataRecord['client_group_id'] = $app->tools_sites->getClientIdForDomain($this->dataRecord['domain']);
999                     }
73813a 1000                     if($this->_vhostdomain_type == 'subdomain') $this->dataRecord['domain'] = $this->dataRecord['domain'] . '.' . $domain_check;
MC 1001                     else $this->dataRecord['domain'] = $domain_check;
b1a6a5 1002                 }
73813a 1003             } else {
MC 1004                 if($this->_vhostdomain_type == 'subdomain') $this->dataRecord["domain"] = $this->dataRecord["domain"].'.'.$parent_domain["domain"];
1005             }
1006
1007             if($this->_vhostdomain_type != 'domain') {
1008                 $this->dataRecord['web_folder'] = strtolower($this->dataRecord['web_folder']);
1009                 if(substr($this->dataRecord['web_folder'], 0, 1) === '/') $this->dataRecord['web_folder'] = substr($this->dataRecord['web_folder'], 1);
1010                 if(substr($this->dataRecord['web_folder'], -1) === '/') $this->dataRecord['web_folder'] = substr($this->dataRecord['web_folder'], 0, -1);
1011                 $forbidden_folders = array('', 'cgi-bin', 'log', 'private', 'ssl', 'tmp', 'webdav');
1012                 $check_folder = strtolower($this->dataRecord['web_folder']);
1013                 if(substr($check_folder, 0, 1) === '/') $check_folder = substr($check_folder, 1); // strip / at beginning to check against forbidden entries
1014                 if(strpos($check_folder, '/') !== false) $check_folder = substr($check_folder, 0, strpos($check_folder, '/')); // get the first part of the path to check it
1015                 if(in_array($check_folder, $forbidden_folders)) {
1016                     $app->tform->errorMessage .= $app->tform->lng("web_folder_invalid_txt")."<br>";
1017                 }
1018
1019                 // vhostaliasdomains do not have a quota of their own
1020                 $this->dataRecord["hd_quota"] = 0;
b1a6a5 1021             }
MC 1022         }
1023
ac099e 1024
M 1025
76ebcb 1026         if($_SESSION["s"]["user"]["typ"] != 'admin') {
F 1027             // Get the limits of the client
73813a 1028             $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
cc7a82 1029             $client = $app->db->queryOneRecord("SELECT limit_traffic_quota, limit_web_domain, limit_web_aliasdomain, limit_web_subdomain, web_servers, 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);
bd6659 1030
SJ 1031             $client['web_servers_ids'] = explode(',', $client['web_servers']);
b1a6a5 1032
ebbe63 1033             if($client['limit_cgi'] != 'y') $this->dataRecord['cgi'] = 'n';
MC 1034             if($client['limit_ssi'] != 'y') $this->dataRecord['ssi'] = 'n';
1035             if($client['limit_perl'] != 'y') $this->dataRecord['perl'] = 'n';
1036             if($client['limit_ruby'] != 'y') $this->dataRecord['ruby'] = 'n';
1037             if($client['limit_python'] != 'y') $this->dataRecord['python'] = 'n';
b1a6a5 1038             if($client['force_suexec'] == 'y') $this->dataRecord['suexec'] = 'y';
ebbe63 1039             if($client['limit_hterror'] != 'y') $this->dataRecord['errordocs'] = 'n';
MC 1040             if($client['limit_wildcard'] != 'y' && $this->dataRecord['subdomain'] == '*') $this->dataRecord['subdomain'] = 'n';
1041             if($client['limit_ssl'] != 'y') $this->dataRecord['ssl'] = 'n';
67b0cb 1042             if($client['limit_ssl_letsencrypt'] != 'y') $this->dataRecord['ssl_letsencrypt'] = 'n';
af56b4 1043             if($client['limit_directive_snippets'] != 'y') $this->dataRecord['directive_snippets_id'] = 0;
b1a6a5 1044
146783 1045             // only generate quota and traffic warnings if value has changed
MC 1046             if($this->id > 0) {
2af58c 1047                 $old_web_values = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ?", $this->id);
1c3655 1048             } else {
f9c424 1049                 $old_web_values = array();
146783 1050             }
73813a 1051             
MC 1052             if($this->_vhostdomain_type == 'domain') {
1053                 //* Check the website quota of the client
1054                 if(isset($_POST["hd_quota"]) && $client["limit_web_quota"] >= 0 && $_POST["hd_quota"] != $old_web_values["hd_quota"]) {
2af58c 1055                     $tmp = $app->db->queryOneRecord("SELECT sum(hd_quota) as webquota FROM web_domain WHERE domain_id != ? AND type = 'vhost' AND ".$app->tform->getAuthSQL('u'), $this->id);
73813a 1056                     $webquota = $tmp["webquota"];
MC 1057                     $new_web_quota = $app->functions->intval($this->dataRecord["hd_quota"]);
1058                     if(($webquota + $new_web_quota > $client["limit_web_quota"]) || ($new_web_quota < 0 && $client["limit_web_quota"] >= 0)) {
1059                         $max_free_quota = floor($client["limit_web_quota"] - $webquota);
1060                         if($max_free_quota < 0) $max_free_quota = 0;
1061                         $app->tform->errorMessage .= $app->tform->lng("limit_web_quota_free_txt").": ".$max_free_quota." MB<br>";
1062                         // Set the quota field to the max free space
1063                         $this->dataRecord["hd_quota"] = $max_free_quota;
1064                     }
1065                     unset($tmp);
1066                     unset($tmp_quota);
76ebcb 1067                 }
F 1068             }
1069
1070             //* Check the traffic quota of the client
146783 1071             if(isset($_POST["traffic_quota"]) && $client["limit_traffic_quota"] > 0 && $_POST["traffic_quota"] != $old_web_values["traffic_quota"]) {
2af58c 1072                 $tmp = $app->db->queryOneRecord("SELECT sum(traffic_quota) as trafficquota FROM web_domain WHERE domain_id != ? AND ".$app->tform->getAuthSQL('u'), $this->id);
76ebcb 1073                 $trafficquota = $tmp["trafficquota"];
65ea2e 1074                 $new_traffic_quota = $app->functions->intval($this->dataRecord["traffic_quota"]);
76ebcb 1075                 if(($trafficquota + $new_traffic_quota > $client["limit_traffic_quota"]) || ($new_traffic_quota < 0 && $client["limit_traffic_quota"] >= 0)) {
F 1076                     $max_free_quota = floor($client["limit_traffic_quota"] - $trafficquota);
1077                     if($max_free_quota < 0) $max_free_quota = 0;
1078                     $app->tform->errorMessage .= $app->tform->lng("limit_traffic_quota_free_txt").": ".$max_free_quota." MB<br>";
1079                     // Set the quota field to the max free space
1080                     $this->dataRecord["traffic_quota"] = $max_free_quota;
1081                 }
1082                 unset($tmp);
1083                 unset($tmp_quota);
1084             }
b1a6a5 1085
76ebcb 1086             if($client['parent_client_id'] > 0) {
F 1087                 // Get the limits of the reseller
2af58c 1088                 $reseller = $app->db->queryOneRecord("SELECT limit_traffic_quota, limit_web_domain, limit_web_aliasdomain, limit_web_subdomain, web_servers, limit_web_quota FROM client WHERE client_id = ?", $client['parent_client_id']);
76ebcb 1089
73813a 1090                 if($this->_vhostdomain_type == 'domain') {
MC 1091                     //* Check the website quota of the client
1092                     if(isset($_POST["hd_quota"]) && $reseller["limit_web_quota"] >= 0 && $_POST["hd_quota"] != $old_web_values["hd_quota"]) {
2af58c 1093                         $tmp = $app->db->queryOneRecord("SELECT sum(hd_quota) as webquota FROM web_domain, sys_group, client WHERE web_domain.sys_groupid=sys_group.groupid AND sys_group.client_id=client.client_id AND ? IN (client.parent_client_id, client.client_id) AND domain_id != ? AND type = 'vhost'", $client['parent_client_id'], $this->id);
dcd2c0 1094
73813a 1095                         $webquota = $tmp["webquota"];
MC 1096                         $new_web_quota = $app->functions->intval($this->dataRecord["hd_quota"]);
1097                         if(($webquota + $new_web_quota > $reseller["limit_web_quota"]) || ($new_web_quota < 0 && $reseller["limit_web_quota"] >= 0)) {
1098                             $max_free_quota = floor($reseller["limit_web_quota"] - $webquota);
1099                             if($max_free_quota < 0) $max_free_quota = 0;
1100                             $app->tform->errorMessage .= $app->tform->lng("limit_web_quota_free_txt").": ".$max_free_quota." MB<br>";
1101                             // Set the quota field to the max free space
1102                             $this->dataRecord["hd_quota"] = $max_free_quota;
1103                         }
1104                         unset($tmp);
1105                         unset($tmp_quota);
76ebcb 1106                     }
F 1107                 }
1108
1109                 //* Check the traffic quota of the client
146783 1110                 if(isset($_POST["traffic_quota"]) && $reseller["limit_traffic_quota"] > 0 && $_POST["traffic_quota"] != $old_web_values["traffic_quota"]) {
2af58c 1111                     $tmp = $app->db->queryOneRecord("SELECT sum(traffic_quota) as trafficquota FROM web_domain, sys_group, client WHERE web_domain.sys_groupid=sys_group.groupid AND sys_group.client_id=client.client_id AND ? IN (client.parent_client_id, client.client_id) AND domain_id != ? AND type = 'vhost'", $client['parent_client_id'], $this->id);
76ebcb 1112                     $trafficquota = $tmp["trafficquota"];
65ea2e 1113                     $new_traffic_quota = $app->functions->intval($this->dataRecord["traffic_quota"]);
76ebcb 1114                     if(($trafficquota + $new_traffic_quota > $reseller["limit_traffic_quota"]) || ($new_traffic_quota < 0 && $reseller["limit_traffic_quota"] >= 0)) {
F 1115                         $max_free_quota = floor($reseller["limit_traffic_quota"] - $trafficquota);
1116                         if($max_free_quota < 0) $max_free_quota = 0;
1117                         $app->tform->errorMessage .= $app->tform->lng("limit_traffic_quota_free_txt").": ".$max_free_quota." MB<br>";
1118                         // Set the quota field to the max free space
1119                         $this->dataRecord["traffic_quota"] = $max_free_quota;
1120                     }
1121                     unset($tmp);
1122                     unset($tmp_quota);
1123                 }
1124             }
1125
1126             // When the record is updated
1127             if($this->id > 0) {
1128                 // restore the server ID if the user is not admin and record is edited
2af58c 1129                 $tmp = $app->db->queryOneRecord("SELECT server_id, `system_user`, `system_group`, `web_folder`, `cgi`, `ssi`, `perl`, `ruby`, `python`, `suexec`, `errordocs`, `subdomain`, `ssl` FROM web_domain WHERE domain_id = ?", $this->id);
76ebcb 1130                 $this->dataRecord["server_id"] = $tmp["server_id"];
d22277 1131                 if($this->_vhostdomain_type != 'domain') $this->dataRecord['web_folder'] = $tmp['web_folder']; // cannot be changed!
797215 1132                 $this->dataRecord['system_user'] = $tmp['system_user'];
MC 1133                 $this->dataRecord['system_group'] = $tmp['system_group'];
b1a6a5 1134
MC 1135                 // set the settings to current if not provided (or cleared due to limits)
ebbe63 1136                 if($this->dataRecord['cgi'] == 'n') $this->dataRecord['cgi'] = $tmp['cgi'];
MC 1137                 if($this->dataRecord['ssi'] == 'n') $this->dataRecord['ssi'] = $tmp['ssi'];
1138                 if($this->dataRecord['perl'] == 'n') $this->dataRecord['perl'] = $tmp['perl'];
1139                 if($this->dataRecord['ruby'] == 'n') $this->dataRecord['ruby'] = $tmp['ruby'];
1140                 if($this->dataRecord['python'] == 'n') $this->dataRecord['python'] = $tmp['python'];
1141                 if($this->dataRecord['suexec'] == 'n') $this->dataRecord['suexec'] = $tmp['suexec'];
1142                 if($this->dataRecord['errordocs'] == 'n') $this->dataRecord['errordocs'] = $tmp['errordocs'];
1143                 if($this->dataRecord['subdomain'] == 'n') $this->dataRecord['subdomain'] = $tmp['subdomain'];
1144                 if($this->dataRecord['ssl'] == 'n') $this->dataRecord['ssl'] = $tmp['ssl'];
b1a6a5 1145
76ebcb 1146                 unset($tmp);
F 1147                 // When the record is inserted
1148             } else {
73813a 1149                 if($this->_vhostdomain_type == 'domain') {
MC 1150                     //* display an error if chosen server is not allowed for this client
1151                     if (!is_array($client['web_servers_ids']) || !in_array($this->dataRecord['server_id'], $client['web_servers_ids'])) {
1152                         $app->error($app->tform->wordbook['server_chosen_not_ok']);
1153                     }
bd6659 1154                 }
76ebcb 1155
F 1156                 // Check if the user may add another web_domain
73813a 1157                 if($this->_vhostdomain_type == 'domain' && $client["limit_web_domain"] >= 0) {
2af58c 1158                     $tmp = $app->db->queryOneRecord("SELECT count(domain_id) as number FROM web_domain WHERE sys_groupid = ? and type = 'vhost'", $client_group_id);
76ebcb 1159                     if($tmp["number"] >= $client["limit_web_domain"]) {
F 1160                         $app->error($app->tform->wordbook["limit_web_domain_txt"]);
1161                     }
73813a 1162                 } elseif($this->_vhostdomain_type == 'aliasdomain' && $client["limit_web_aliasdomain"] >= 0) {
2af58c 1163                     $tmp = $app->db->queryOneRecord("SELECT count(domain_id) as number FROM web_domain WHERE sys_groupid = ? and (type = 'alias' OR type = 'vhostalias')", $client_group_id);
73813a 1164                     if($tmp["number"] >= $client["limit_web_aliasdomain"]) {
MC 1165                         $app->error($app->tform->wordbook["limit_web_aliasdomain_txt"]);
1166                     }
1167                 } elseif($this->_vhostdomain_type == 'subdomain' && $client["limit_web_subdomain"] >= 0) {
2af58c 1168                     $tmp = $app->db->queryOneRecord("SELECT count(domain_id) as number FROM web_domain WHERE sys_groupid = ? and (type = 'subdomain' OR type = 'vhostsubdomain')", $client_group_id);
73813a 1169                     if($tmp["number"] >= $client["limit_web_subdomain"]) {
MC 1170                         $app->error($app->tform->wordbook["limit_web_subdomain_txt"]);
1171                     }
76ebcb 1172                 }
F 1173             }
1174
1175             // 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
1176             if(!$app->auth->has_clients($_SESSION['s']['user']['userid'])) unset($this->dataRecord["client_group_id"]);
1177         }
b1a6a5 1178
73813a 1179         //* make sure that the domain is lowercase
76ebcb 1180         if(isset($this->dataRecord["domain"])) $this->dataRecord["domain"] = strtolower($this->dataRecord["domain"]);
b1a6a5 1181
76ebcb 1182         //* get the server config for this server
F 1183         $app->uses("getconf");
615a0a 1184         if($this->id > 0){
T 1185             $web_rec = $app->tform->getDataRecord($this->id);
1186             $server_id = $web_rec["server_id"];
1187         } else {
1188             // Get the first server ID
1189             $tmp = $app->db->queryOneRecord("SELECT server_id FROM server WHERE web_server = 1 ORDER BY server_name LIMIT 0,1");
1190             $server_id = intval($tmp['server_id']);
1191         }
b1a6a5 1192         $web_config = $app->getconf->get_server_config($app->functions->intval(isset($this->dataRecord["server_id"]) ? $this->dataRecord["server_id"] : $server_id), 'web');
76ebcb 1193         //* Check for duplicate ssl certs per IP if SNI is disabled
F 1194         if(isset($this->dataRecord['ssl']) && $this->dataRecord['ssl'] == 'y' && $web_config['enable_sni'] != 'y') {
2af58c 1195             $sql = "SELECT count(domain_id) as number FROM web_domain WHERE `ssl` = 'y' AND ip_address = ? and domain_id != ?";
MC 1196             $tmp = $app->db->queryOneRecord($sql, $this->dataRecord['ip_address'], $this->id);
76ebcb 1197             if($tmp['number'] > 0) $app->tform->errorMessage .= $app->tform->lng("error_no_sni_txt");
F 1198         }
b1a6a5 1199
76ebcb 1200         // Check if pm.max_children >= pm.max_spare_servers >= pm.start_servers >= pm.min_spare_servers > 0
dd7ce4 1201         if(isset($this->dataRecord['pm_max_children']) && $this->dataRecord['pm'] == 'dynamic') {
65ea2e 1202             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){
b1a6a5 1203
76ebcb 1204             } else {
F 1205                 $app->tform->errorMessage .= $app->tform->lng("error_php_fpm_pm_settings_txt").'<br>';
1206             }
1207         }
b1a6a5 1208
615a0a 1209         // Check rewrite rules
T 1210         $server_type = $web_config['server_type'];
b1a6a5 1211
615a0a 1212         if($server_type == 'nginx' && isset($this->dataRecord['rewrite_rules']) && trim($this->dataRecord['rewrite_rules']) != '') {
T 1213             $rewrite_rules = trim($this->dataRecord['rewrite_rules']);
1214             $rewrites_are_valid = true;
1215             // use this counter to make sure all curly brackets are properly closed
1216             $if_level = 0;
1217             // Make sure we only have Unix linebreaks
1218             $rewrite_rules = str_replace("\r\n", "\n", $rewrite_rules);
1219             $rewrite_rules = str_replace("\r", "\n", $rewrite_rules);
1220             $rewrite_rule_lines = explode("\n", $rewrite_rules);
1221             if(is_array($rewrite_rule_lines) && !empty($rewrite_rule_lines)){
1222                 foreach($rewrite_rule_lines as $rewrite_rule_line){
7b47c0 1223                     // ignore comments
b1a6a5 1224                     if(substr(ltrim($rewrite_rule_line), 0, 1) == '#') continue;
7b47c0 1225                     // empty lines
T 1226                     if(trim($rewrite_rule_line) == '') continue;
615a0a 1227                     // rewrite
T 1228                     if(preg_match('@^\s*rewrite\s+(^/)?\S+(\$)?\s+\S+(\s+(last|break|redirect|permanent|))?\s*;\s*$@', $rewrite_rule_line)) continue;
7c2179 1229                     if(preg_match('@^\s*rewrite\s+(^/)?(\'[^\']+\'|"[^"]+")+(\$)?\s+(\'[^\']+\'|"[^"]+")+(\s+(last|break|redirect|permanent|))?\s*;\s*$@', $rewrite_rule_line)) continue;
MC 1230                     if(preg_match('@^\s*rewrite\s+(^/)?(\'[^\']+\'|"[^"]+")+(\$)?\s+\S+(\s+(last|break|redirect|permanent|))?\s*;\s*$@', $rewrite_rule_line)) continue;
1231                     if(preg_match('@^\s*rewrite\s+(^/)?\S+(\$)?\s+(\'[^\']+\'|"[^"]+")+(\s+(last|break|redirect|permanent|))?\s*;\s*$@', $rewrite_rule_line)) continue;
615a0a 1232                     // if
T 1233                     if(preg_match('@^\s*if\s+\(\s*\$\S+(\s+(\!?(=|~|~\*))\s+(\S+|\".+\"))?\s*\)\s*\{\s*$@', $rewrite_rule_line)){
1234                         $if_level += 1;
1235                         continue;
1236                     }
1237                     // if - check for files, directories, etc.
1238                     if(preg_match('@^\s*if\s+\(\s*\!?-(f|d|e|x)\s+\S+\s*\)\s*\{\s*$@', $rewrite_rule_line)){
1239                         $if_level += 1;
1240                         continue;
1241                     }
1242                     // break
1243                     if(preg_match('@^\s*break\s*;\s*$@', $rewrite_rule_line)){
1244                         continue;
1245                     }
1246                     // return code [ text ]
1247                     if(preg_match('@^\s*return\s+\d\d\d.*;\s*$@', $rewrite_rule_line)) continue;
1248                     // return code URL
1249                     // return URL
1250                     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;
1251                     // set
1252                     if(preg_match('@^\s*set\s+\$\S+\s+\S+\s*;\s*$@', $rewrite_rule_line)) continue;
1253                     // closing curly bracket
1254                     if(trim($rewrite_rule_line) == '}'){
1255                         $if_level -= 1;
1256                         continue;
1257                     }
1258                     $rewrites_are_valid = false;
1259                     break;
1260                 }
1261             }
b1a6a5 1262
615a0a 1263             if(!$rewrites_are_valid || $if_level != 0){
T 1264                 $app->tform->errorMessage .= $app->tform->lng("invalid_rewrite_rules_txt").'<br>';
1265             }
1266         }
3faab0 1267         
MC 1268         // check custom php.ini settings
1269         if(isset($this->dataRecord['custom_php_ini']) && trim($this->dataRecord['custom_php_ini']) != '') {
1270             $custom_php_ini_settings = trim($this->dataRecord['custom_php_ini']);
1271             $custom_php_ini_settings_are_valid = true;
1272             // Make sure we only have Unix linebreaks
1273             $custom_php_ini_settings = str_replace("\r\n", "\n", $custom_php_ini_settings);
1274             $custom_php_ini_settings = str_replace("\r", "\n", $custom_php_ini_settings);
1275             $custom_php_ini_settings_lines = explode("\n", $custom_php_ini_settings);
1276             if(is_array($custom_php_ini_settings_lines) && !empty($custom_php_ini_settings_lines)){
1277                 foreach($custom_php_ini_settings_lines as $custom_php_ini_settings_line){
1278                     if(trim($custom_php_ini_settings_line) == '') continue;
1279                     if(substr(trim($custom_php_ini_settings_line),0,1) == ';') continue;
1280                     // empty value
1281                     if(preg_match('@^\s*;*\s*[a-zA-Z0-9._]*\s*=\s*;*\s*$@', $custom_php_ini_settings_line)) continue;
1282                     // value inside ""
1283                     if(preg_match('@^\s*;*\s*[a-zA-Z0-9._]*\s*=\s*".*"\s*;*\s*$@', $custom_php_ini_settings_line)) continue;
1284                     // value inside ''
1285                     if(preg_match('@^\s*;*\s*[a-zA-Z0-9._]*\s*=\s*\'.*\'\s*;*\s*$@', $custom_php_ini_settings_line)) continue;
1286                     // everything else
1287                     if(preg_match('@^\s*;*\s*[a-zA-Z0-9._]*\s*=\s*[-a-zA-Z0-9~&=_\@/,.#\s]*\s*;*\s*$@', $custom_php_ini_settings_line)) continue;
1288                     $custom_php_ini_settings_are_valid = false;
1289                     break;
1290                 }
1291             }
1292             if(!$custom_php_ini_settings_are_valid){
1293                 $app->tform->errorMessage .= $app->tform->lng("invalid_custom_php_ini_settings_txt").'<br>';
1294             }
1295         }
76ebcb 1296
dcf94e 1297         if($web_config['enable_spdy'] === 'n') {
PA 1298             unset($app->tform->formDef["tabs"]['ssl']['fields']['enable_spdy']);
1299         }
86bc65 1300         if($this->dataRecord["directive_snippets_id"] < 1) $this->dataRecord["enable_pagespeed"] = 'n';
d22277 1301         
MB 1302         //print_r($_POST['folder_directive_snippets']);
1303         //print_r($_POST['folder_directive_snippets_id']);
1304         if(isset($_POST['folder_directive_snippets'])){
1305             $this->dataRecord['folder_directive_snippets'] = '';
1306             if(is_array($_POST['folder_directive_snippets']) && !empty($_POST['folder_directive_snippets'])){
1307                 $existing_directive_snippets_folders = array();
1308                 foreach($_POST['folder_directive_snippets'] as $folder_directive_snippet){
1309                     $folder_directive_snippet['folder'] = trim($folder_directive_snippet['folder']);
1310                     if($folder_directive_snippet['folder'] != '' && intval($folder_directive_snippet['snippets_id']) > 0){
1311                         if(substr($folder_directive_snippet['folder'], -1) != '/') $folder_directive_snippet['folder'] .= '/';
1312                         if(substr($folder_directive_snippet['folder'], 0, 1) == '/') $folder_directive_snippet['folder'] = substr($folder_directive_snippet['folder'], 1);
1313                         if(in_array($folder_directive_snippet['folder'], $existing_directive_snippets_folders)){
1314                             $app->tform->errorMessage .= $app->tform->lng("config_for_folder_exists_already_txt").'<br>';
1315                         } else {
1316                             $existing_directive_snippets_folders[] = $folder_directive_snippet['folder'];
1317                         }
1318                         $this->dataRecord['folder_directive_snippets'] .= $folder_directive_snippet['folder'].':'.intval($folder_directive_snippet['snippets_id'])."\n";
1319                     }
1320                     if(!preg_match('@^((?!(.*\.\.)|(.*\./)|(.*//))[^/][\w/_\.\-]{1,100})?$@', $folder_directive_snippet['folder'])) $app->tform->errorMessage .= $app->tform->lng("web_folder_error_regex").'<br>';
1321                 }
1322             }
1323             $this->dataRecord['folder_directive_snippets'] = trim($this->dataRecord['folder_directive_snippets']);
1324         }
1325         
b5f6a1 1326         // Check custom PHP version
TB 1327         if(isset($this->dataRecord['fastcgi_php_version']) && $this->dataRecord['fastcgi_php_version'] != '') {
1328             // Check php-fpm mode
1329             if($this->dataRecord['php'] == 'php-fpm'){
1330                 $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'])."'");
1331                 if(is_array($tmp)) {
1332                     $this->dataRecord['fastcgi_php_version'] = $tmp['name'].':'.$tmp['php_fpm_init_script'].':'.$tmp['php_fpm_ini_dir'].':'.$tmp['php_fpm_pool_dir'];
1333                 } else {
1334                     $this->dataRecord['fastcgi_php_version'] = '';
1335                 }
1336                 unset($tmp);
1337             // Check fast-cgi mode
1338             } elseif($this->dataRecord['php'] == 'fast-cgi') {
1339                 $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'])."'");
1340                 if(is_array($tmp)) {
1341                     $this->dataRecord['fastcgi_php_version'] = $tmp['name'].':'.$tmp['php_fastcgi_binary'].':'.$tmp['php_fastcgi_ini_dir'];
1342                 } else {
1343                     $this->dataRecord['fastcgi_php_version'] = '';
1344                 }
1345                 unset($tmp);
1346             } else {
1347                 // Other PHP modes do not have custom versions, so we force the value to be empty
1348                 $this->dataRecord['fastcgi_php_version'] = '';
1349             }
1350         }
1351         
76ebcb 1352         parent::onSubmit();
F 1353     }
6c60b1 1354     
TB 1355     function onBeforeInsert() {
1356         global $app, $conf;
1357         
1358         // Letsencrypt can not be activated before the website has been created
1359         // So we deactivate it here and add a datalog update in onAfterInsert
d228f3 1360         if(isset($this->dataRecord['ssl_letsencrypt']) && $this->dataRecord['ssl_letsencrypt'] == 'y' && isset($this->dataRecord['ssl']) && $this->dataRecord['ssl'] == 'y') {
279724 1361             // Disable letsencrypt and ssl temporarily
6c60b1 1362             $this->dataRecord['ssl_letsencrypt'] = 'n';
d228f3 1363             $this->dataRecord['ssl'] = 'n';
279724 1364             // Prevent that the datalog history gets written
TB 1365             $app->tform->formDef['db_history'] = 'no';
1366             // Set variable that we check in onAfterInsert
6c60b1 1367             $this->_letsencrypt_on_insert = true;
TB 1368         }
1369     }
1370     
76ebcb 1371
F 1372     function onAfterInsert() {
1373         global $app, $conf;
1374
1375         // make sure that the record belongs to the clinet group and not the admin group when admin inserts it
1376         // also make sure that the user can not delete domain created by a admin
1377         if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($this->dataRecord["client_group_id"])) {
65ea2e 1378             $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
2af58c 1379             $app->db->query("UPDATE web_domain SET sys_groupid = ?, sys_perm_group = 'ru' WHERE domain_id = ?", $client_group_id, $this->id);
76ebcb 1380         }
F 1381         if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($this->dataRecord["client_group_id"])) {
65ea2e 1382             $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
2af58c 1383             $app->db->query("UPDATE web_domain SET sys_groupid = ?, sys_perm_group = 'riud' WHERE domain_id = ?", $client_group_id, $this->id);
76ebcb 1384         }
F 1385
1386         // Get configuration for the web system
1387         $app->uses("getconf");
1388         $web_rec = $app->tform->getDataRecord($this->id);
b1a6a5 1389         $web_config = $app->getconf->get_server_config($app->functions->intval($web_rec["server_id"]), 'web');
76ebcb 1390
73813a 1391         if($this->_vhostdomain_type == 'domain') {
MC 1392             $document_root = str_replace("[website_id]", $this->id, $web_config["website_path"]);
1393             $document_root = str_replace("[website_idhash_1]", $this->id_hash($page_form->id, 1), $document_root);
1394             $document_root = str_replace("[website_idhash_2]", $this->id_hash($page_form->id, 1), $document_root);
1395             $document_root = str_replace("[website_idhash_3]", $this->id_hash($page_form->id, 1), $document_root);
1396             $document_root = str_replace("[website_idhash_4]", $this->id_hash($page_form->id, 1), $document_root);
1397
1398             // get the ID of the client
1399             if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
1400                 $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
2af58c 1401                 $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ?", $client_group_id);
73813a 1402                 $client_id = $app->functions->intval($client["client_id"]);
MC 1403             } else {
1404                 //$client_id = $app->functions->intval($this->dataRecord["client_group_id"]);
2af58c 1405                 $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ?", $this->dataRecord["client_group_id"]);
73813a 1406                 $client_id = $app->functions->intval($client["client_id"]);
MC 1407             }
1408
1409             // Set the values for document_root, system_user and system_group
2af58c 1410             $system_user = 'web'.$this->id;
MC 1411             $system_group = 'client'.$client_id;
73813a 1412             $document_root = str_replace("[client_id]", $client_id, $document_root);
MC 1413             $document_root = str_replace("[client_idhash_1]", $this->id_hash($client_id, 1), $document_root);
1414             $document_root = str_replace("[client_idhash_2]", $this->id_hash($client_id, 2), $document_root);
1415             $document_root = str_replace("[client_idhash_3]", $this->id_hash($client_id, 3), $document_root);
1416             $document_root = str_replace("[client_idhash_4]", $this->id_hash($client_id, 4), $document_root);
2af58c 1417             $document_root = $document_root;
73813a 1418             $php_open_basedir = str_replace("[website_path]", $document_root, $web_config["php_open_basedir"]);
2af58c 1419             $php_open_basedir = str_replace("[website_domain]", $web_rec['domain'], $php_open_basedir);
MC 1420             $htaccess_allow_override = $web_config["htaccess_allow_override"];
1421             $added_by = $_SESSION['s']['user']['username'];
73813a 1422
c1e0c6 1423             $sql = "UPDATE web_domain SET system_user = ?, system_group = ?, document_root = ?, allow_override = ?, php_open_basedir = ?, added_date = CURDATE(), added_by = ?  WHERE domain_id = ?";
MB 1424             $app->db->query($sql, $system_user, $system_group, $document_root, $htaccess_allow_override, $php_open_basedir, $added_by, $this->id);
73813a 1425         } else  {
MC 1426             // Set the values for document_root, system_user and system_group
2af58c 1427             $system_user = $this->parent_domain_record['system_user'];
MC 1428             $system_group = $this->parent_domain_record['system_group'];
1429             $document_root = $this->parent_domain_record['document_root'];
73813a 1430             $php_open_basedir = str_replace("[website_path]/web", $document_root.'/'.$web_rec['web_folder'], $web_config["php_open_basedir"]);
MC 1431             $php_open_basedir = str_replace("[website_domain]/web", $web_rec['domain'].'/'.$web_rec['web_folder'], $php_open_basedir);
1432             $php_open_basedir = str_replace("[website_path]", $document_root, $php_open_basedir);
2af58c 1433             $php_open_basedir = str_replace("[website_domain]", $web_rec['domain'], $php_open_basedir);
MC 1434             $htaccess_allow_override = $this->parent_domain_record['allow_override'];
1435             $added_by = $_SESSION['s']['user']['username'];
1436             
c1e0c6 1437             $sql = "UPDATE web_domain SET sys_groupid = ?, system_user = ?, system_group = ?, document_root = ?, allow_override = ?, php_open_basedir = ?, added_date = CURDATE(), added_by = ?  WHERE domain_id = ?";
MB 1438             $app->db->query($sql, $this->parent_domain_record['sys_groupid'], $system_user, $system_group, $document_root, $htaccess_allow_override, $php_open_basedir, $added_by, $this->id);
76ebcb 1439         }
d22277 1440         if(isset($this->dataRecord['folder_directive_snippets'])) $app->db->query("UPDATE web_domain SET folder_directive_snippets = ? WHERE domain_id = ?", $this->dataRecord['folder_directive_snippets'], $this->id);
6c60b1 1441         
279724 1442         // Add a datalog insert without letsencrypt and then an update with letsencrypt enabled (see also onBeforeInsert)
6c60b1 1443         if($this->_letsencrypt_on_insert == true) {
279724 1444             $new_data_record = $app->tform->getDataRecord($this->id);
TB 1445             $app->tform->datalogSave('INSERT', $this->id, array(), $new_data_record);
1446             $new_data_record['ssl_letsencrypt'] = 'y';
1447             $new_data_record['ssl'] = 'y';
1448             $app->db->datalogUpdate('web_domain', $new_data_record, 'domain_id', $this->id);
6c60b1 1449         }
TB 1450     
76ebcb 1451     }
F 1452
1453     function onBeforeUpdate () {
1454         global $app, $conf;
1455
73813a 1456         if($this->_vhostdomain_type == 'domain') {
MC 1457             //* Check if the server has been changed
1458             // We do this only for the admin or reseller users, as normal clients can not change the server ID anyway
1459             if($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
1460                 if (isset($this->dataRecord["server_id"])) {
2af58c 1461                     $rec = $app->db->queryOneRecord("SELECT server_id from web_domain WHERE domain_id = ?", $this->id);
73813a 1462                     if($rec['server_id'] != $this->dataRecord["server_id"]) {
MC 1463                         //* Add a error message and switch back to old server
1464                         $app->tform->errorMessage .= $app->lng('The Server can not be changed.');
1465                         $this->dataRecord["server_id"] = $rec['server_id'];
1466                     }
1467                     unset($rec);
1468                 }
1469                 //* If the user is neither admin nor reseller
1470             } else {
1471                 //* We do not allow users to change a domain which has been created by the admin
2af58c 1472                 $rec = $app->db->queryOneRecord("SELECT sys_perm_group, domain, ip_address, ipv6_address from web_domain WHERE domain_id = ?", $this->id);
f7bdb7 1473                 if(isset($this->dataRecord["domain"]) && $rec['domain'] != $this->dataRecord["domain"] && !$app->tform->checkPerm($this->id, 'u')) {
76ebcb 1474                     //* Add a error message and switch back to old server
73813a 1475                     $app->tform->errorMessage .= $app->lng('The Domain can not be changed. Please ask your Administrator if you want to change the domain name.');
MC 1476                     $this->dataRecord["domain"] = $rec['domain'];
1477                 }
1478                 if(isset($this->dataRecord["ip_address"]) && $rec['ip_address'] != $this->dataRecord["ip_address"] && $rec['sys_perm_group'] != 'riud') {
1af0f8 1479                     //* Add a error message and switch back to old server
FS 1480                     $app->tform->errorMessage .= $app->lng('The IP can not be changed. Please ask your Administrator if you want to change the IPv4-Address.');
73813a 1481                     $this->dataRecord["ip_address"] = $rec['ip_address'];
MC 1482                 }
1483                 if(isset($this->dataRecord["ipv6_address"]) && $rec['ipv6_address'] != $this->dataRecord["ipv6_address"] && $rec['sys_perm_group'] != 'riud') {
1af0f8 1484                     //* Add a error message and switch back to old server
FS 1485                     $app->tform->errorMessage .= $app->lng('The IP can not be changed. Please ask your Administrator if you want to change the IPv6-Address.');
73813a 1486                     $this->dataRecord["ipv6_address"] = $rec['ipv6_address'];
76ebcb 1487                 }
F 1488                 unset($rec);
1489             }
1490         }
1491
1492         //* Check that all fields for the SSL cert creation are filled
1493         if(isset($this->dataRecord['ssl_action']) && $this->dataRecord['ssl_action'] == 'create') {
1494             if($this->dataRecord['ssl_country'] == '') $app->tform->errorMessage .= $app->tform->lng('error_ssl_country_empty').'<br />';
1495         }
b1a6a5 1496
76ebcb 1497         if(isset($this->dataRecord['ssl_action']) && $this->dataRecord['ssl_action'] == 'save') {
F 1498             if(trim($this->dataRecord['ssl_cert']) == '') $app->tform->errorMessage .= $app->tform->lng('error_ssl_cert_empty').'<br />';
1499         }
1500
1501     }
d22277 1502     
MB 1503     function onAfterUpdate() {
1504         global $app, $conf;
1505
1506         if(isset($this->dataRecord['folder_directive_snippets'])) $app->db->query("UPDATE web_domain SET folder_directive_snippets = ? WHERE domain_id = ?", $this->dataRecord['folder_directive_snippets'], $this->id);
1507     }
76ebcb 1508 }
F 1509
1510 $page = new page_action;
1511 $page->onLoad();
1512
b1a6a5 1513 ?>