Marius Burkard
2016-07-01 49441bdd0f3ff75d5092d5b832b97ea722a66363
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);
0998a3 805         } else {
MC 806
807             // remove the parent domain part of the domain name before we show it in the text field.
808             if($this->dataRecord["type"] == 'vhostsubdomain') $this->dataRecord["domain"] = str_replace('.'.$parent_domain["domain"], '', $this->dataRecord["domain"]);
7b47c0 809         }
73813a 810         if($this->_vhostdomain_type != 'domain') $app->tpl->setVar("domain", $this->dataRecord["domain"]);
b1a6a5 811
7b47c0 812         // check for configuration errors in sys_datalog
T 813         if($this->id > 0) {
2af58c 814             $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 815             if(is_array($datalog) && !empty($datalog)){
T 816                 if(trim($datalog['error']) != ''){
b1a6a5 817                     $app->tpl->setVar("config_error_msg", nl2br(htmlentities($datalog['error'])));
MC 818                     $app->tpl->setVar("config_error_tstamp", date($app->lng('conf_format_datetime'), $datalog['tstamp']));
7b47c0 819                 }
T 820             }
76ebcb 821         }
73813a 822         
MC 823         $app->tpl->setVar('vhostdomain_type', $this->_vhostdomain_type);
76ebcb 824
dcf94e 825         $app->tpl->setVar('is_spdy_enabled', ($web_config['enable_spdy'] === 'y'));
8173c6 826         $app->tpl->setVar("is_admin", $is_admin);
FT 827         
828         if($this->id > 0) {
829             $tmp_web = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ?", intval($this->id));
830             $tmp_sys_group = $app->db->queryOneRecord("SELECT * FROM sys_group WHERE groupid = ?", intval($tmp_web['sys_groupid']));
831             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']));
832             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);
833         }
dcf94e 834
d5f2d5 835         $sys_config = $app->getconf->get_global_config('misc');
MC 836         if($sys_config['use_combobox'] == 'y') {
837             $app->tpl->setVar('use_combobox', 'y');
838         }
d22277 839         
MB 840         $directive_snippets_id_select = '<option value="0"'.($this->dataRecord['directive_snippets_id'] == 0? ' selected="selected"' : '').'>-</option>';
841         $server_type = $app->getconf->get_server_config($server_id, 'web');
842         $server_type = $server_type['server_type'];
843         
844         $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);
845         if(is_array($m_directive_snippets) && !empty($m_directive_snippets)){
846             $directive_snippets_id_select .= '<optgroup label="'.$app->tform->wordbook["select_master_directive_snippet_txt"].'">';
847             foreach($m_directive_snippets as $m_directive_snippet){
848                 $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>';
849             }
850             $directive_snippets_id_select .= '</optgroup>';
851         }
852         
853         $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);
854         if(is_array($directive_snippets) && !empty($directive_snippets)){
855             $directive_snippets_id_select .= '<optgroup label="'.$app->tform->wordbook["select_directive_snippet_txt"].'">';
856             foreach($directive_snippets as $directive_snippet){
857                 $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>';
858             }
859             $directive_snippets_id_select .= '</optgroup>';
860         }
861         $app->tpl->setVar("directive_snippets_id", $directive_snippets_id_select);
862         
863         // folder_directive_snippets
864         if(isset($_POST['folder_directive_snippets']) && !isset($this->dataRecord['folder_directive_snippets'])){
865             $this->dataRecord['folder_directive_snippets'] = '';
866             if(is_array($_POST['folder_directive_snippets']) && !empty($_POST['folder_directive_snippets'])){
867                 foreach($_POST['folder_directive_snippets'] as $folder_directive_snippet){
868                     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";
869                 }
870             }
871             $this->dataRecord['folder_directive_snippets'] = trim($this->dataRecord['folder_directive_snippets']);
872         }
873         
874         $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);
875         $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);
876         
877         $folder_directive_snippets = array();
878         $this->dataRecord['folder_directive_snippets'] = str_replace("\r\n", "\n", $this->dataRecord['folder_directive_snippets']);
879         $this->dataRecord['folder_directive_snippets'] = str_replace("\r", "\n", $this->dataRecord['folder_directive_snippets']);
880         $folder_directive_snippets_lines = explode("\n", trim($this->dataRecord['folder_directive_snippets']));
881         for($i=0;$i<sizeof($folder_directive_snippets_lines)+50;$i++){
882             $folder_directive_snippets[$i]['folder_directive_snippets_index'] = $i;
883             $folder_directive_snippets[$i]['folder_directive_snippets_index_plus_1'] = $i + 1;
884             if($i > sizeof($folder_directive_snippets_lines)){
885                 $folder_directive_snippets[$i]['folder_directive_snippets_css'] = 'hidden';
886             } else {
887                 $folder_directive_snippets[$i]['folder_directive_snippets_css'] = '';
888             }
889             if(trim($folder_directive_snippets_lines[$i]) != ''){
890                 list($folder_directive_snippets[$i]['folder_directive_snippets_folder'], $selected_snippet) = explode(':', trim($folder_directive_snippets_lines[$i]));
891                 $folder_directive_snippets[$i]['folder_directive_snippets_id'] = '<option value="0">-</option>';
892                 if(is_array($master_directive_snippets) && !empty($master_directive_snippets)){
893                     $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '<optgroup label="'.$app->tform->wordbook["select_master_directive_snippet_txt"].'">';
894                     foreach($master_directive_snippets as $master_directive_snippet){
895                         $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>';
896                     }
897                     $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '</optgroup>';
898                 }
899                 
900                 if(is_array($c_directive_snippets) && !empty($c_directive_snippets)){
901                     $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '<optgroup label="'.$app->tform->wordbook["select_directive_snippet_txt"].'">';
902                     foreach($c_directive_snippets as $c_directive_snippet){
903                         $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>';
904                     }
905                     $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '</optgroup>';
906                 }
907             } else {
908                 $folder_directive_snippets[$i]['folder_directive_snippets_folder'] = '';
909                 $folder_directive_snippets[$i]['folder_directive_snippets_id'] = '<option value="0">-</option>';
910                 if(is_array($master_directive_snippets) && !empty($master_directive_snippets)){
911                     $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '<optgroup label="'.$app->tform->wordbook["select_master_directive_snippet_txt"].'">';
912                     foreach($master_directive_snippets as $master_directive_snippet){
913                         $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '<option value="'.$master_directive_snippet['directive_snippets_id'].'">'.$master_directive_snippet['name'].'</option>';
914                     }
915                     $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '</optgroup>';
916                 }
917                 
918                 if(is_array($c_directive_snippets) && !empty($c_directive_snippets)){
919                     $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '<optgroup label="'.$app->tform->wordbook["select_directive_snippet_txt"].'">';
920                     foreach($c_directive_snippets as $c_directive_snippet){
921                         $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '<option value="'.$c_directive_snippet['directive_snippets_id'].'">'.$c_directive_snippet['name'].'</option>';
922                     }
923                     $folder_directive_snippets[$i]['folder_directive_snippets_id'] .= '</optgroup>';
924                 }
925             }
926         }
927         $app->tpl->setLoop('folder_directive_snippets', $folder_directive_snippets);
928
76ebcb 929         parent::onShowEnd();
F 930     }
b1a6a5 931
MC 932     function onShowEdit() {
933         global $app;
934         if($app->tform->checkPerm($this->id, 'riud')) $app->tform->formDef['tabs']['domain']['readonly'] = false;
935         parent::onShowEdit();
936     }
76ebcb 937
F 938     function onSubmit() {
939         global $app, $conf;
940
73813a 941         // Set a few fixed values
MC 942         $this->dataRecord["vhost_type"] = 'name';
943         if($this->_vhostdomain_type == 'domain') {
944             $this->dataRecord["parent_domain_id"] = 0;
945             $this->dataRecord["type"] = 'vhost';
946         } else {
947             // Get the record of the parent domain
948             if(!@$this->dataRecord["parent_domain_id"] && $this->id) {
2af58c 949                 $tmp = $app->db->queryOneRecord("SELECT parent_domain_id FROM web_domain WHERE domain_id = ?", $this->id);
73813a 950                 if($tmp) $this->dataRecord["parent_domain_id"] = $tmp['parent_domain_id'];
MC 951                 unset($tmp);
952             }
953
2af58c 954             $parent_domain = $app->db->queryOneRecord("select * FROM web_domain WHERE domain_id = ? AND ".$app->tform->getAuthSQL('r'), @$this->dataRecord["parent_domain_id"]);
73813a 955             if(!$parent_domain || $parent_domain['domain_id'] != @$this->dataRecord['parent_domain_id']) $app->tform->errorMessage .= $app->tform->lng("no_domain_perm");
MC 956
957             if($this->_vhostdomain_type == 'subdomain') {
958                 $this->dataRecord["type"] = 'vhostsubdomain';
959             } else {
960                 $this->dataRecord["type"] = 'vhostalias';
961             }
962             $this->dataRecord["server_id"] = $parent_domain["server_id"];
963             $this->dataRecord["ip_address"] = $parent_domain["ip_address"];
964             $this->dataRecord["ipv6_address"] = $parent_domain["ipv6_address"];
965             $this->dataRecord["client_group_id"] = $parent_domain["client_group_id"];
966
967             $this->parent_domain_record = $parent_domain;
968         }
969
67b0cb 970         $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 971
b1a6a5 972         /* check if the domain module is used - and check if the selected domain can be used! */
f601d9 973         if($app->tform->getCurrentTab() == 'domain') {
ebbe63 974             if($this->_vhostdomain_type == 'subdomain') {
MC 975                 // Check that domain (the subdomain part) is not empty
976                 if(!preg_match('/^[a-zA-Z0-9].*/',$this->dataRecord['domain'])) {
977                     $app->tform->errorMessage .= $app->tform->lng("subdomain_error_empty")."<br />";
978                 }
979             }
980             
981             /* check if the domain module is used - and check if the selected domain can be used! */
b1a6a5 982             $app->uses('ini_parser,getconf');
MC 983             $settings = $app->getconf->get_global_config('domains');
984             if ($settings['use_domain_module'] == 'y') {
73813a 985                 if($this->_vhostdomain_type == 'subdomain') $domain_check = $app->tools_sites->checkDomainModuleDomain($this->dataRecord['sel_domain']);
MC 986                 else $domain_check = $app->tools_sites->checkDomainModuleDomain($this->dataRecord['domain']);
b1a6a5 987                 if(!$domain_check) {
MC 988                     // invalid domain selected
989                     $app->tform->errorMessage .= $app->tform->lng("domain_error_empty")."<br />";
990                 } else {
3e94c8 991                     if ($this->_vhostdomain_type == 'domain' &&
SC 992                             ($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid']))) {
993                         $this->dataRecord['client_group_id'] = $app->tools_sites->getClientIdForDomain($this->dataRecord['domain']);
994                     }
73813a 995                     if($this->_vhostdomain_type == 'subdomain') $this->dataRecord['domain'] = $this->dataRecord['domain'] . '.' . $domain_check;
MC 996                     else $this->dataRecord['domain'] = $domain_check;
b1a6a5 997                 }
73813a 998             } else {
MC 999                 if($this->_vhostdomain_type == 'subdomain') $this->dataRecord["domain"] = $this->dataRecord["domain"].'.'.$parent_domain["domain"];
1000             }
1001
1002             if($this->_vhostdomain_type != 'domain') {
1003                 $this->dataRecord['web_folder'] = strtolower($this->dataRecord['web_folder']);
1004                 if(substr($this->dataRecord['web_folder'], 0, 1) === '/') $this->dataRecord['web_folder'] = substr($this->dataRecord['web_folder'], 1);
1005                 if(substr($this->dataRecord['web_folder'], -1) === '/') $this->dataRecord['web_folder'] = substr($this->dataRecord['web_folder'], 0, -1);
1006                 $forbidden_folders = array('', 'cgi-bin', 'log', 'private', 'ssl', 'tmp', 'webdav');
1007                 $check_folder = strtolower($this->dataRecord['web_folder']);
1008                 if(substr($check_folder, 0, 1) === '/') $check_folder = substr($check_folder, 1); // strip / at beginning to check against forbidden entries
1009                 if(strpos($check_folder, '/') !== false) $check_folder = substr($check_folder, 0, strpos($check_folder, '/')); // get the first part of the path to check it
1010                 if(in_array($check_folder, $forbidden_folders)) {
1011                     $app->tform->errorMessage .= $app->tform->lng("web_folder_invalid_txt")."<br>";
1012                 }
1013
1014                 // vhostaliasdomains do not have a quota of their own
1015                 $this->dataRecord["hd_quota"] = 0;
b1a6a5 1016             }
MC 1017         }
1018
ac099e 1019
M 1020
76ebcb 1021         if($_SESSION["s"]["user"]["typ"] != 'admin') {
F 1022             // Get the limits of the client
73813a 1023             $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
cc7a82 1024             $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 1025
SJ 1026             $client['web_servers_ids'] = explode(',', $client['web_servers']);
b1a6a5 1027
ebbe63 1028             if($client['limit_cgi'] != 'y') $this->dataRecord['cgi'] = 'n';
MC 1029             if($client['limit_ssi'] != 'y') $this->dataRecord['ssi'] = 'n';
1030             if($client['limit_perl'] != 'y') $this->dataRecord['perl'] = 'n';
1031             if($client['limit_ruby'] != 'y') $this->dataRecord['ruby'] = 'n';
1032             if($client['limit_python'] != 'y') $this->dataRecord['python'] = 'n';
b1a6a5 1033             if($client['force_suexec'] == 'y') $this->dataRecord['suexec'] = 'y';
ebbe63 1034             if($client['limit_hterror'] != 'y') $this->dataRecord['errordocs'] = 'n';
MC 1035             if($client['limit_wildcard'] != 'y' && $this->dataRecord['subdomain'] == '*') $this->dataRecord['subdomain'] = 'n';
1036             if($client['limit_ssl'] != 'y') $this->dataRecord['ssl'] = 'n';
67b0cb 1037             if($client['limit_ssl_letsencrypt'] != 'y') $this->dataRecord['ssl_letsencrypt'] = 'n';
af56b4 1038             if($client['limit_directive_snippets'] != 'y') $this->dataRecord['directive_snippets_id'] = 0;
b1a6a5 1039
146783 1040             // only generate quota and traffic warnings if value has changed
MC 1041             if($this->id > 0) {
2af58c 1042                 $old_web_values = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ?", $this->id);
1c3655 1043             } else {
f9c424 1044                 $old_web_values = array();
146783 1045             }
73813a 1046             
MC 1047             if($this->_vhostdomain_type == 'domain') {
1048                 //* Check the website quota of the client
1049                 if(isset($_POST["hd_quota"]) && $client["limit_web_quota"] >= 0 && $_POST["hd_quota"] != $old_web_values["hd_quota"]) {
2af58c 1050                     $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 1051                     $webquota = $tmp["webquota"];
MC 1052                     $new_web_quota = $app->functions->intval($this->dataRecord["hd_quota"]);
1053                     if(($webquota + $new_web_quota > $client["limit_web_quota"]) || ($new_web_quota < 0 && $client["limit_web_quota"] >= 0)) {
1054                         $max_free_quota = floor($client["limit_web_quota"] - $webquota);
1055                         if($max_free_quota < 0) $max_free_quota = 0;
1056                         $app->tform->errorMessage .= $app->tform->lng("limit_web_quota_free_txt").": ".$max_free_quota." MB<br>";
1057                         // Set the quota field to the max free space
1058                         $this->dataRecord["hd_quota"] = $max_free_quota;
1059                     }
1060                     unset($tmp);
1061                     unset($tmp_quota);
76ebcb 1062                 }
F 1063             }
1064
1065             //* Check the traffic quota of the client
146783 1066             if(isset($_POST["traffic_quota"]) && $client["limit_traffic_quota"] > 0 && $_POST["traffic_quota"] != $old_web_values["traffic_quota"]) {
2af58c 1067                 $tmp = $app->db->queryOneRecord("SELECT sum(traffic_quota) as trafficquota FROM web_domain WHERE domain_id != ? AND ".$app->tform->getAuthSQL('u'), $this->id);
76ebcb 1068                 $trafficquota = $tmp["trafficquota"];
65ea2e 1069                 $new_traffic_quota = $app->functions->intval($this->dataRecord["traffic_quota"]);
76ebcb 1070                 if(($trafficquota + $new_traffic_quota > $client["limit_traffic_quota"]) || ($new_traffic_quota < 0 && $client["limit_traffic_quota"] >= 0)) {
F 1071                     $max_free_quota = floor($client["limit_traffic_quota"] - $trafficquota);
1072                     if($max_free_quota < 0) $max_free_quota = 0;
1073                     $app->tform->errorMessage .= $app->tform->lng("limit_traffic_quota_free_txt").": ".$max_free_quota." MB<br>";
1074                     // Set the quota field to the max free space
1075                     $this->dataRecord["traffic_quota"] = $max_free_quota;
1076                 }
1077                 unset($tmp);
1078                 unset($tmp_quota);
1079             }
b1a6a5 1080
76ebcb 1081             if($client['parent_client_id'] > 0) {
F 1082                 // Get the limits of the reseller
2af58c 1083                 $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 1084
73813a 1085                 if($this->_vhostdomain_type == 'domain') {
MC 1086                     //* Check the website quota of the client
1087                     if(isset($_POST["hd_quota"]) && $reseller["limit_web_quota"] >= 0 && $_POST["hd_quota"] != $old_web_values["hd_quota"]) {
2af58c 1088                         $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 1089
73813a 1090                         $webquota = $tmp["webquota"];
MC 1091                         $new_web_quota = $app->functions->intval($this->dataRecord["hd_quota"]);
1092                         if(($webquota + $new_web_quota > $reseller["limit_web_quota"]) || ($new_web_quota < 0 && $reseller["limit_web_quota"] >= 0)) {
1093                             $max_free_quota = floor($reseller["limit_web_quota"] - $webquota);
1094                             if($max_free_quota < 0) $max_free_quota = 0;
1095                             $app->tform->errorMessage .= $app->tform->lng("limit_web_quota_free_txt").": ".$max_free_quota." MB<br>";
1096                             // Set the quota field to the max free space
1097                             $this->dataRecord["hd_quota"] = $max_free_quota;
1098                         }
1099                         unset($tmp);
1100                         unset($tmp_quota);
76ebcb 1101                     }
F 1102                 }
1103
1104                 //* Check the traffic quota of the client
146783 1105                 if(isset($_POST["traffic_quota"]) && $reseller["limit_traffic_quota"] > 0 && $_POST["traffic_quota"] != $old_web_values["traffic_quota"]) {
2af58c 1106                     $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 1107                     $trafficquota = $tmp["trafficquota"];
65ea2e 1108                     $new_traffic_quota = $app->functions->intval($this->dataRecord["traffic_quota"]);
76ebcb 1109                     if(($trafficquota + $new_traffic_quota > $reseller["limit_traffic_quota"]) || ($new_traffic_quota < 0 && $reseller["limit_traffic_quota"] >= 0)) {
F 1110                         $max_free_quota = floor($reseller["limit_traffic_quota"] - $trafficquota);
1111                         if($max_free_quota < 0) $max_free_quota = 0;
1112                         $app->tform->errorMessage .= $app->tform->lng("limit_traffic_quota_free_txt").": ".$max_free_quota." MB<br>";
1113                         // Set the quota field to the max free space
1114                         $this->dataRecord["traffic_quota"] = $max_free_quota;
1115                     }
1116                     unset($tmp);
1117                     unset($tmp_quota);
1118                 }
1119             }
1120
1121             // When the record is updated
1122             if($this->id > 0) {
1123                 // restore the server ID if the user is not admin and record is edited
2af58c 1124                 $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 1125                 $this->dataRecord["server_id"] = $tmp["server_id"];
d22277 1126                 if($this->_vhostdomain_type != 'domain') $this->dataRecord['web_folder'] = $tmp['web_folder']; // cannot be changed!
797215 1127                 $this->dataRecord['system_user'] = $tmp['system_user'];
MC 1128                 $this->dataRecord['system_group'] = $tmp['system_group'];
b1a6a5 1129
MC 1130                 // set the settings to current if not provided (or cleared due to limits)
ebbe63 1131                 if($this->dataRecord['cgi'] == 'n') $this->dataRecord['cgi'] = $tmp['cgi'];
MC 1132                 if($this->dataRecord['ssi'] == 'n') $this->dataRecord['ssi'] = $tmp['ssi'];
1133                 if($this->dataRecord['perl'] == 'n') $this->dataRecord['perl'] = $tmp['perl'];
1134                 if($this->dataRecord['ruby'] == 'n') $this->dataRecord['ruby'] = $tmp['ruby'];
1135                 if($this->dataRecord['python'] == 'n') $this->dataRecord['python'] = $tmp['python'];
1136                 if($this->dataRecord['suexec'] == 'n') $this->dataRecord['suexec'] = $tmp['suexec'];
1137                 if($this->dataRecord['errordocs'] == 'n') $this->dataRecord['errordocs'] = $tmp['errordocs'];
1138                 if($this->dataRecord['subdomain'] == 'n') $this->dataRecord['subdomain'] = $tmp['subdomain'];
1139                 if($this->dataRecord['ssl'] == 'n') $this->dataRecord['ssl'] = $tmp['ssl'];
b1a6a5 1140
76ebcb 1141                 unset($tmp);
F 1142                 // When the record is inserted
1143             } else {
73813a 1144                 if($this->_vhostdomain_type == 'domain') {
MC 1145                     //* display an error if chosen server is not allowed for this client
1146                     if (!is_array($client['web_servers_ids']) || !in_array($this->dataRecord['server_id'], $client['web_servers_ids'])) {
1147                         $app->error($app->tform->wordbook['server_chosen_not_ok']);
1148                     }
bd6659 1149                 }
76ebcb 1150
F 1151                 // Check if the user may add another web_domain
73813a 1152                 if($this->_vhostdomain_type == 'domain' && $client["limit_web_domain"] >= 0) {
2af58c 1153                     $tmp = $app->db->queryOneRecord("SELECT count(domain_id) as number FROM web_domain WHERE sys_groupid = ? and type = 'vhost'", $client_group_id);
76ebcb 1154                     if($tmp["number"] >= $client["limit_web_domain"]) {
F 1155                         $app->error($app->tform->wordbook["limit_web_domain_txt"]);
1156                     }
73813a 1157                 } elseif($this->_vhostdomain_type == 'aliasdomain' && $client["limit_web_aliasdomain"] >= 0) {
2af58c 1158                     $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 1159                     if($tmp["number"] >= $client["limit_web_aliasdomain"]) {
MC 1160                         $app->error($app->tform->wordbook["limit_web_aliasdomain_txt"]);
1161                     }
1162                 } elseif($this->_vhostdomain_type == 'subdomain' && $client["limit_web_subdomain"] >= 0) {
2af58c 1163                     $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 1164                     if($tmp["number"] >= $client["limit_web_subdomain"]) {
MC 1165                         $app->error($app->tform->wordbook["limit_web_subdomain_txt"]);
1166                     }
76ebcb 1167                 }
F 1168             }
1169
1170             // 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
1171             if(!$app->auth->has_clients($_SESSION['s']['user']['userid'])) unset($this->dataRecord["client_group_id"]);
1172         }
b1a6a5 1173
73813a 1174         //* make sure that the domain is lowercase
76ebcb 1175         if(isset($this->dataRecord["domain"])) $this->dataRecord["domain"] = strtolower($this->dataRecord["domain"]);
b1a6a5 1176
76ebcb 1177         //* get the server config for this server
F 1178         $app->uses("getconf");
615a0a 1179         if($this->id > 0){
T 1180             $web_rec = $app->tform->getDataRecord($this->id);
1181             $server_id = $web_rec["server_id"];
1182         } else {
1183             // Get the first server ID
1184             $tmp = $app->db->queryOneRecord("SELECT server_id FROM server WHERE web_server = 1 ORDER BY server_name LIMIT 0,1");
1185             $server_id = intval($tmp['server_id']);
1186         }
b1a6a5 1187         $web_config = $app->getconf->get_server_config($app->functions->intval(isset($this->dataRecord["server_id"]) ? $this->dataRecord["server_id"] : $server_id), 'web');
76ebcb 1188         //* Check for duplicate ssl certs per IP if SNI is disabled
F 1189         if(isset($this->dataRecord['ssl']) && $this->dataRecord['ssl'] == 'y' && $web_config['enable_sni'] != 'y') {
2af58c 1190             $sql = "SELECT count(domain_id) as number FROM web_domain WHERE `ssl` = 'y' AND ip_address = ? and domain_id != ?";
MC 1191             $tmp = $app->db->queryOneRecord($sql, $this->dataRecord['ip_address'], $this->id);
76ebcb 1192             if($tmp['number'] > 0) $app->tform->errorMessage .= $app->tform->lng("error_no_sni_txt");
F 1193         }
b1a6a5 1194
76ebcb 1195         // Check if pm.max_children >= pm.max_spare_servers >= pm.start_servers >= pm.min_spare_servers > 0
dd7ce4 1196         if(isset($this->dataRecord['pm_max_children']) && $this->dataRecord['pm'] == 'dynamic') {
65ea2e 1197             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 1198
76ebcb 1199             } else {
F 1200                 $app->tform->errorMessage .= $app->tform->lng("error_php_fpm_pm_settings_txt").'<br>';
1201             }
1202         }
b1a6a5 1203
615a0a 1204         // Check rewrite rules
T 1205         $server_type = $web_config['server_type'];
b1a6a5 1206
615a0a 1207         if($server_type == 'nginx' && isset($this->dataRecord['rewrite_rules']) && trim($this->dataRecord['rewrite_rules']) != '') {
T 1208             $rewrite_rules = trim($this->dataRecord['rewrite_rules']);
1209             $rewrites_are_valid = true;
1210             // use this counter to make sure all curly brackets are properly closed
1211             $if_level = 0;
1212             // Make sure we only have Unix linebreaks
1213             $rewrite_rules = str_replace("\r\n", "\n", $rewrite_rules);
1214             $rewrite_rules = str_replace("\r", "\n", $rewrite_rules);
1215             $rewrite_rule_lines = explode("\n", $rewrite_rules);
1216             if(is_array($rewrite_rule_lines) && !empty($rewrite_rule_lines)){
1217                 foreach($rewrite_rule_lines as $rewrite_rule_line){
7b47c0 1218                     // ignore comments
b1a6a5 1219                     if(substr(ltrim($rewrite_rule_line), 0, 1) == '#') continue;
7b47c0 1220                     // empty lines
T 1221                     if(trim($rewrite_rule_line) == '') continue;
615a0a 1222                     // rewrite
T 1223                     if(preg_match('@^\s*rewrite\s+(^/)?\S+(\$)?\s+\S+(\s+(last|break|redirect|permanent|))?\s*;\s*$@', $rewrite_rule_line)) continue;
7c2179 1224                     if(preg_match('@^\s*rewrite\s+(^/)?(\'[^\']+\'|"[^"]+")+(\$)?\s+(\'[^\']+\'|"[^"]+")+(\s+(last|break|redirect|permanent|))?\s*;\s*$@', $rewrite_rule_line)) continue;
MC 1225                     if(preg_match('@^\s*rewrite\s+(^/)?(\'[^\']+\'|"[^"]+")+(\$)?\s+\S+(\s+(last|break|redirect|permanent|))?\s*;\s*$@', $rewrite_rule_line)) continue;
1226                     if(preg_match('@^\s*rewrite\s+(^/)?\S+(\$)?\s+(\'[^\']+\'|"[^"]+")+(\s+(last|break|redirect|permanent|))?\s*;\s*$@', $rewrite_rule_line)) continue;
615a0a 1227                     // if
T 1228                     if(preg_match('@^\s*if\s+\(\s*\$\S+(\s+(\!?(=|~|~\*))\s+(\S+|\".+\"))?\s*\)\s*\{\s*$@', $rewrite_rule_line)){
1229                         $if_level += 1;
1230                         continue;
1231                     }
1232                     // if - check for files, directories, etc.
1233                     if(preg_match('@^\s*if\s+\(\s*\!?-(f|d|e|x)\s+\S+\s*\)\s*\{\s*$@', $rewrite_rule_line)){
1234                         $if_level += 1;
1235                         continue;
1236                     }
1237                     // break
1238                     if(preg_match('@^\s*break\s*;\s*$@', $rewrite_rule_line)){
1239                         continue;
1240                     }
1241                     // return code [ text ]
1242                     if(preg_match('@^\s*return\s+\d\d\d.*;\s*$@', $rewrite_rule_line)) continue;
1243                     // return code URL
1244                     // return URL
1245                     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;
1246                     // set
1247                     if(preg_match('@^\s*set\s+\$\S+\s+\S+\s*;\s*$@', $rewrite_rule_line)) continue;
1248                     // closing curly bracket
1249                     if(trim($rewrite_rule_line) == '}'){
1250                         $if_level -= 1;
1251                         continue;
1252                     }
1253                     $rewrites_are_valid = false;
1254                     break;
1255                 }
1256             }
b1a6a5 1257
615a0a 1258             if(!$rewrites_are_valid || $if_level != 0){
T 1259                 $app->tform->errorMessage .= $app->tform->lng("invalid_rewrite_rules_txt").'<br>';
1260             }
1261         }
3faab0 1262         
MC 1263         // check custom php.ini settings
1264         if(isset($this->dataRecord['custom_php_ini']) && trim($this->dataRecord['custom_php_ini']) != '') {
1265             $custom_php_ini_settings = trim($this->dataRecord['custom_php_ini']);
1266             $custom_php_ini_settings_are_valid = true;
1267             // Make sure we only have Unix linebreaks
1268             $custom_php_ini_settings = str_replace("\r\n", "\n", $custom_php_ini_settings);
1269             $custom_php_ini_settings = str_replace("\r", "\n", $custom_php_ini_settings);
1270             $custom_php_ini_settings_lines = explode("\n", $custom_php_ini_settings);
1271             if(is_array($custom_php_ini_settings_lines) && !empty($custom_php_ini_settings_lines)){
1272                 foreach($custom_php_ini_settings_lines as $custom_php_ini_settings_line){
1273                     if(trim($custom_php_ini_settings_line) == '') continue;
1274                     if(substr(trim($custom_php_ini_settings_line),0,1) == ';') continue;
1275                     // empty value
1276                     if(preg_match('@^\s*;*\s*[a-zA-Z0-9._]*\s*=\s*;*\s*$@', $custom_php_ini_settings_line)) continue;
1277                     // value inside ""
1278                     if(preg_match('@^\s*;*\s*[a-zA-Z0-9._]*\s*=\s*".*"\s*;*\s*$@', $custom_php_ini_settings_line)) continue;
1279                     // value inside ''
1280                     if(preg_match('@^\s*;*\s*[a-zA-Z0-9._]*\s*=\s*\'.*\'\s*;*\s*$@', $custom_php_ini_settings_line)) continue;
1281                     // everything else
1282                     if(preg_match('@^\s*;*\s*[a-zA-Z0-9._]*\s*=\s*[-a-zA-Z0-9~&=_\@/,.#\s]*\s*;*\s*$@', $custom_php_ini_settings_line)) continue;
1283                     $custom_php_ini_settings_are_valid = false;
1284                     break;
1285                 }
1286             }
1287             if(!$custom_php_ini_settings_are_valid){
1288                 $app->tform->errorMessage .= $app->tform->lng("invalid_custom_php_ini_settings_txt").'<br>';
1289             }
1290         }
76ebcb 1291
dcf94e 1292         if($web_config['enable_spdy'] === 'n') {
PA 1293             unset($app->tform->formDef["tabs"]['ssl']['fields']['enable_spdy']);
1294         }
86bc65 1295         if($this->dataRecord["directive_snippets_id"] < 1) $this->dataRecord["enable_pagespeed"] = 'n';
d22277 1296         
MB 1297         //print_r($_POST['folder_directive_snippets']);
1298         //print_r($_POST['folder_directive_snippets_id']);
1299         if(isset($_POST['folder_directive_snippets'])){
1300             $this->dataRecord['folder_directive_snippets'] = '';
1301             if(is_array($_POST['folder_directive_snippets']) && !empty($_POST['folder_directive_snippets'])){
1302                 $existing_directive_snippets_folders = array();
1303                 foreach($_POST['folder_directive_snippets'] as $folder_directive_snippet){
1304                     $folder_directive_snippet['folder'] = trim($folder_directive_snippet['folder']);
1305                     if($folder_directive_snippet['folder'] != '' && intval($folder_directive_snippet['snippets_id']) > 0){
1306                         if(substr($folder_directive_snippet['folder'], -1) != '/') $folder_directive_snippet['folder'] .= '/';
1307                         if(substr($folder_directive_snippet['folder'], 0, 1) == '/') $folder_directive_snippet['folder'] = substr($folder_directive_snippet['folder'], 1);
1308                         if(in_array($folder_directive_snippet['folder'], $existing_directive_snippets_folders)){
1309                             $app->tform->errorMessage .= $app->tform->lng("config_for_folder_exists_already_txt").'<br>';
1310                         } else {
1311                             $existing_directive_snippets_folders[] = $folder_directive_snippet['folder'];
1312                         }
1313                         $this->dataRecord['folder_directive_snippets'] .= $folder_directive_snippet['folder'].':'.intval($folder_directive_snippet['snippets_id'])."\n";
1314                     }
1315                     if(!preg_match('@^((?!(.*\.\.)|(.*\./)|(.*//))[^/][\w/_\.\-]{1,100})?$@', $folder_directive_snippet['folder'])) $app->tform->errorMessage .= $app->tform->lng("web_folder_error_regex").'<br>';
1316                 }
1317             }
1318             $this->dataRecord['folder_directive_snippets'] = trim($this->dataRecord['folder_directive_snippets']);
1319         }
1320         
b5f6a1 1321         // Check custom PHP version
TB 1322         if(isset($this->dataRecord['fastcgi_php_version']) && $this->dataRecord['fastcgi_php_version'] != '') {
1323             // Check php-fpm mode
1324             if($this->dataRecord['php'] == 'php-fpm'){
1325                 $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'])."'");
1326                 if(is_array($tmp)) {
1327                     $this->dataRecord['fastcgi_php_version'] = $tmp['name'].':'.$tmp['php_fpm_init_script'].':'.$tmp['php_fpm_ini_dir'].':'.$tmp['php_fpm_pool_dir'];
1328                 } else {
1329                     $this->dataRecord['fastcgi_php_version'] = '';
1330                 }
1331                 unset($tmp);
1332             // Check fast-cgi mode
1333             } elseif($this->dataRecord['php'] == 'fast-cgi') {
1334                 $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'])."'");
1335                 if(is_array($tmp)) {
1336                     $this->dataRecord['fastcgi_php_version'] = $tmp['name'].':'.$tmp['php_fastcgi_binary'].':'.$tmp['php_fastcgi_ini_dir'];
1337                 } else {
1338                     $this->dataRecord['fastcgi_php_version'] = '';
1339                 }
1340                 unset($tmp);
1341             } else {
1342                 // Other PHP modes do not have custom versions, so we force the value to be empty
1343                 $this->dataRecord['fastcgi_php_version'] = '';
1344             }
1345         }
1346         
76ebcb 1347         parent::onSubmit();
F 1348     }
6c60b1 1349     
TB 1350     function onBeforeInsert() {
1351         global $app, $conf;
1352         
1353         // Letsencrypt can not be activated before the website has been created
1354         // So we deactivate it here and add a datalog update in onAfterInsert
d228f3 1355         if(isset($this->dataRecord['ssl_letsencrypt']) && $this->dataRecord['ssl_letsencrypt'] == 'y' && isset($this->dataRecord['ssl']) && $this->dataRecord['ssl'] == 'y') {
279724 1356             // Disable letsencrypt and ssl temporarily
6c60b1 1357             $this->dataRecord['ssl_letsencrypt'] = 'n';
d228f3 1358             $this->dataRecord['ssl'] = 'n';
279724 1359             // Prevent that the datalog history gets written
TB 1360             $app->tform->formDef['db_history'] = 'no';
1361             // Set variable that we check in onAfterInsert
6c60b1 1362             $this->_letsencrypt_on_insert = true;
TB 1363         }
1364     }
1365     
76ebcb 1366
F 1367     function onAfterInsert() {
1368         global $app, $conf;
1369
1370         // make sure that the record belongs to the clinet group and not the admin group when admin inserts it
1371         // also make sure that the user can not delete domain created by a admin
1372         if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($this->dataRecord["client_group_id"])) {
65ea2e 1373             $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
2af58c 1374             $app->db->query("UPDATE web_domain SET sys_groupid = ?, sys_perm_group = 'ru' WHERE domain_id = ?", $client_group_id, $this->id);
76ebcb 1375         }
F 1376         if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($this->dataRecord["client_group_id"])) {
65ea2e 1377             $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
2af58c 1378             $app->db->query("UPDATE web_domain SET sys_groupid = ?, sys_perm_group = 'riud' WHERE domain_id = ?", $client_group_id, $this->id);
76ebcb 1379         }
F 1380
1381         // Get configuration for the web system
1382         $app->uses("getconf");
1383         $web_rec = $app->tform->getDataRecord($this->id);
b1a6a5 1384         $web_config = $app->getconf->get_server_config($app->functions->intval($web_rec["server_id"]), 'web');
76ebcb 1385
73813a 1386         if($this->_vhostdomain_type == 'domain') {
MC 1387             $document_root = str_replace("[website_id]", $this->id, $web_config["website_path"]);
1388             $document_root = str_replace("[website_idhash_1]", $this->id_hash($page_form->id, 1), $document_root);
1389             $document_root = str_replace("[website_idhash_2]", $this->id_hash($page_form->id, 1), $document_root);
1390             $document_root = str_replace("[website_idhash_3]", $this->id_hash($page_form->id, 1), $document_root);
1391             $document_root = str_replace("[website_idhash_4]", $this->id_hash($page_form->id, 1), $document_root);
1392
1393             // get the ID of the client
1394             if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
1395                 $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
2af58c 1396                 $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ?", $client_group_id);
73813a 1397                 $client_id = $app->functions->intval($client["client_id"]);
MC 1398             } else {
1399                 //$client_id = $app->functions->intval($this->dataRecord["client_group_id"]);
2af58c 1400                 $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ?", $this->dataRecord["client_group_id"]);
73813a 1401                 $client_id = $app->functions->intval($client["client_id"]);
MC 1402             }
1403
1404             // Set the values for document_root, system_user and system_group
2af58c 1405             $system_user = 'web'.$this->id;
MC 1406             $system_group = 'client'.$client_id;
73813a 1407             $document_root = str_replace("[client_id]", $client_id, $document_root);
MC 1408             $document_root = str_replace("[client_idhash_1]", $this->id_hash($client_id, 1), $document_root);
1409             $document_root = str_replace("[client_idhash_2]", $this->id_hash($client_id, 2), $document_root);
1410             $document_root = str_replace("[client_idhash_3]", $this->id_hash($client_id, 3), $document_root);
1411             $document_root = str_replace("[client_idhash_4]", $this->id_hash($client_id, 4), $document_root);
2af58c 1412             $document_root = $document_root;
73813a 1413             $php_open_basedir = str_replace("[website_path]", $document_root, $web_config["php_open_basedir"]);
2af58c 1414             $php_open_basedir = str_replace("[website_domain]", $web_rec['domain'], $php_open_basedir);
MC 1415             $htaccess_allow_override = $web_config["htaccess_allow_override"];
1416             $added_by = $_SESSION['s']['user']['username'];
73813a 1417
c1e0c6 1418             $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 1419             $app->db->query($sql, $system_user, $system_group, $document_root, $htaccess_allow_override, $php_open_basedir, $added_by, $this->id);
73813a 1420         } else  {
MC 1421             // Set the values for document_root, system_user and system_group
2af58c 1422             $system_user = $this->parent_domain_record['system_user'];
MC 1423             $system_group = $this->parent_domain_record['system_group'];
1424             $document_root = $this->parent_domain_record['document_root'];
73813a 1425             $php_open_basedir = str_replace("[website_path]/web", $document_root.'/'.$web_rec['web_folder'], $web_config["php_open_basedir"]);
MC 1426             $php_open_basedir = str_replace("[website_domain]/web", $web_rec['domain'].'/'.$web_rec['web_folder'], $php_open_basedir);
1427             $php_open_basedir = str_replace("[website_path]", $document_root, $php_open_basedir);
2af58c 1428             $php_open_basedir = str_replace("[website_domain]", $web_rec['domain'], $php_open_basedir);
MC 1429             $htaccess_allow_override = $this->parent_domain_record['allow_override'];
1430             $added_by = $_SESSION['s']['user']['username'];
1431             
c1e0c6 1432             $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 1433             $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 1434         }
d22277 1435         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 1436         
279724 1437         // Add a datalog insert without letsencrypt and then an update with letsencrypt enabled (see also onBeforeInsert)
6c60b1 1438         if($this->_letsencrypt_on_insert == true) {
279724 1439             $new_data_record = $app->tform->getDataRecord($this->id);
TB 1440             $app->tform->datalogSave('INSERT', $this->id, array(), $new_data_record);
1441             $new_data_record['ssl_letsencrypt'] = 'y';
1442             $new_data_record['ssl'] = 'y';
1443             $app->db->datalogUpdate('web_domain', $new_data_record, 'domain_id', $this->id);
6c60b1 1444         }
TB 1445     
76ebcb 1446     }
F 1447
1448     function onBeforeUpdate () {
1449         global $app, $conf;
1450
73813a 1451         if($this->_vhostdomain_type == 'domain') {
MC 1452             //* Check if the server has been changed
1453             // We do this only for the admin or reseller users, as normal clients can not change the server ID anyway
1454             if($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
1455                 if (isset($this->dataRecord["server_id"])) {
2af58c 1456                     $rec = $app->db->queryOneRecord("SELECT server_id from web_domain WHERE domain_id = ?", $this->id);
73813a 1457                     if($rec['server_id'] != $this->dataRecord["server_id"]) {
MC 1458                         //* Add a error message and switch back to old server
1459                         $app->tform->errorMessage .= $app->lng('The Server can not be changed.');
1460                         $this->dataRecord["server_id"] = $rec['server_id'];
1461                     }
1462                     unset($rec);
1463                 }
1464                 //* If the user is neither admin nor reseller
1465             } else {
1466                 //* We do not allow users to change a domain which has been created by the admin
2af58c 1467                 $rec = $app->db->queryOneRecord("SELECT sys_perm_group, domain, ip_address, ipv6_address from web_domain WHERE domain_id = ?", $this->id);
f7bdb7 1468                 if(isset($this->dataRecord["domain"]) && $rec['domain'] != $this->dataRecord["domain"] && !$app->tform->checkPerm($this->id, 'u')) {
76ebcb 1469                     //* Add a error message and switch back to old server
73813a 1470                     $app->tform->errorMessage .= $app->lng('The Domain can not be changed. Please ask your Administrator if you want to change the domain name.');
MC 1471                     $this->dataRecord["domain"] = $rec['domain'];
1472                 }
1473                 if(isset($this->dataRecord["ip_address"]) && $rec['ip_address'] != $this->dataRecord["ip_address"] && $rec['sys_perm_group'] != 'riud') {
1af0f8 1474                     //* Add a error message and switch back to old server
FS 1475                     $app->tform->errorMessage .= $app->lng('The IP can not be changed. Please ask your Administrator if you want to change the IPv4-Address.');
73813a 1476                     $this->dataRecord["ip_address"] = $rec['ip_address'];
MC 1477                 }
1478                 if(isset($this->dataRecord["ipv6_address"]) && $rec['ipv6_address'] != $this->dataRecord["ipv6_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 IPv6-Address.');
73813a 1481                     $this->dataRecord["ipv6_address"] = $rec['ipv6_address'];
76ebcb 1482                 }
F 1483                 unset($rec);
1484             }
1485         }
1486
1487         //* Check that all fields for the SSL cert creation are filled
1488         if(isset($this->dataRecord['ssl_action']) && $this->dataRecord['ssl_action'] == 'create') {
1489             if($this->dataRecord['ssl_country'] == '') $app->tform->errorMessage .= $app->tform->lng('error_ssl_country_empty').'<br />';
1490         }
b1a6a5 1491
76ebcb 1492         if(isset($this->dataRecord['ssl_action']) && $this->dataRecord['ssl_action'] == 'save') {
F 1493             if(trim($this->dataRecord['ssl_cert']) == '') $app->tform->errorMessage .= $app->tform->lng('error_ssl_cert_empty').'<br />';
1494         }
1495
1496     }
d22277 1497     
MB 1498     function onAfterUpdate() {
1499         global $app, $conf;
1500
1501         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);
1502     }
76ebcb 1503 }
F 1504
1505 $page = new page_action;
1506 $page->onLoad();
1507
b1a6a5 1508 ?>