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