tbrehm
2013-02-22 526b997c9891a796b152cdbab8e329b356b1f596
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
35 $tform_def_file = "form/web_domain.tform.php";
36
37 /******************************************
38 * End Form configuration
39 ******************************************/
40
41 require_once('../../lib/config.inc.php');
42 require_once('../../lib/app.inc.php');
43
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 {
52
53     //* Returna a "3/2/1" path hash from a numeric id '123'
54     function id_hash($id,$levels) {
55         $hash = "" . $id % 10 ;
56         $id /= 10 ;
57         $levels -- ;
58         while ( $levels > 0 ) {
59             $hash .= "/" . $id % 10 ;
60             $id /= 10 ;
61             $levels-- ;
62         }
63         return $hash;
64     }
65     
66     function onShowNew() {
67         global $app, $conf;
68
69         // we will check only users, not admins
70         if($_SESSION["s"]["user"]["typ"] == 'user') {
71             if(!$app->tform->checkClientLimit('limit_web_domain',"type = 'vhost'")) {
72                 $app->error($app->tform->wordbook["limit_web_domain_txt"]);
73             }
74             if(!$app->tform->checkResellerLimit('limit_web_domain',"type = 'vhost'")) {
75                 $app->error('Reseller: '.$app->tform->wordbook["limit_web_domain_txt"]);
76             }
77             
78             // Get the limits of the client
79             $client_group_id = $_SESSION["s"]["user"]["default_group"];
80             $client = $app->db->queryOneRecord("SELECT client.default_webserver FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
81             $app->tpl->setVar("server_id_value", $client['default_webserver']);
82         }
4bd960 83         $app->tform->formDef['tabs']['domain']['readonly'] = false;
76ebcb 84
F 85         parent::onShowNew();
86     }
87
88     function onShowEnd() {
89         global $app, $conf;
90         
91         $app->uses('ini_parser,getconf');
92
ac099e 93         $read_limits = array('limit_cgi', 'limit_ssi', 'limit_perl', 'limit_ruby', 'limit_python', 'force_suexec', 'limit_hterror', 'limit_wildcard', 'limit_ssl');
02384b 94
76ebcb 95         //* Client: If the logged in user is not admin and has no sub clients (no reseller)
F 96         if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
97
98             // Get the limits of the client
99             $client_group_id = $_SESSION["s"]["user"]["default_group"];
ac099e 100             $client = $app->db->queryOneRecord("SELECT client.limit_web_domain, client.default_webserver, client." . implode(", client.", $read_limits) . " FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
02384b 101             
T 102             //* Get global web config
103             $web_config = $app->getconf->get_server_config($client['default_webserver'], 'web');
104             
76ebcb 105             // Set the webserver to the default server of the client
F 106             $tmp = $app->db->queryOneRecord("SELECT server_name FROM server WHERE server_id = $client[default_webserver]");
107             $app->tpl->setVar("server_id","<option value='$client[default_webserver]'>$tmp[server_name]</option>");
108             unset($tmp);
109
110             //* Fill the IPv4 select field with the IP addresses that are allowed for this client
111             $sql = "SELECT ip_address FROM server_ip WHERE server_id = ".$client['default_webserver']." AND ip_type = 'IPv4' AND (client_id = 0 OR client_id=".$_SESSION['s']['user']['client_id'].")";
112             $ips = $app->db->queryAllRecords($sql);
02384b 113             $ip_select = ($web_config['enable_ip_wildcard'] == 'y')?"<option value='*'>*</option>":"";
76ebcb 114             //$ip_select = "";
F 115             if(is_array($ips)) {
116                 foreach( $ips as $ip) {
117                     $selected = ($ip["ip_address"] == $this->dataRecord["ip_address"])?'SELECTED':'';
118                     $ip_select .= "<option value='$ip[ip_address]' $selected>$ip[ip_address]</option>\r\n";
119                 }
120             }
121             $app->tpl->setVar("ip_address",$ip_select);
122             unset($tmp);
123             unset($ips);
124             
125             //* Fill the IPv6 select field with the IP addresses that are allowed for this client
126             $sql = "SELECT ip_address FROM server_ip WHERE server_id = ".$client['default_webserver']." AND ip_type = 'IPv6' AND (client_id = 0 OR client_id=".$_SESSION['s']['user']['client_id'].")";
127             $ips = $app->db->queryAllRecords($sql);
128             $ip_select = "<option value=''></option>";
129             //$ip_select = "";
130             if(is_array($ips)) {
131                 foreach( $ips as $ip) {
132                     $selected = ($ip["ip_address"] == $this->dataRecord["ipv6_address"])?'SELECTED':'';
133                     $ip_select .= "<option value='$ip[ip_address]' $selected>$ip[ip_address]</option>\r\n";
134                 }
135             }
136             $app->tpl->setVar("ipv6_address",$ip_select);
137             unset($tmp);
138             unset($ips);
139             
140             //PHP Version Selection (FastCGI)
141             $server_type = 'apache';
142             if(!empty($web_config['server_type'])) $server_type = $web_config['server_type'];
143             if($server_type == 'nginx' && $this->dataRecord['php'] == 'fast-cgi') $this->dataRecord['php'] = 'php-fpm';
144             if($this->dataRecord['php'] == 'php-fpm'){
c146a2 145                 $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fpm_init_script != '' AND php_fpm_ini_dir != '' AND php_fpm_pool_dir != '' AND server_id = ".($this->id > 0 ? $this->dataRecord['server_id'] : $client['default_webserver'])." AND (client_id = 0 OR client_id=".$_SESSION['s']['user']['client_id'].")");
76ebcb 146             }
F 147             if($this->dataRecord['php'] == 'fast-cgi'){
c146a2 148                 $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = ".($this->id > 0 ? $this->dataRecord['server_id'] : $client['default_webserver'])." AND (client_id = 0 OR client_id=".$_SESSION['s']['user']['client_id'].")");
76ebcb 149             }
F 150             $php_select = "<option value=''>Default</option>";
151             if(is_array($php_records) && !empty($php_records)) {
152                 foreach( $php_records as $php_record) {
153                     if($this->dataRecord['php'] == 'php-fpm'){
154                         $php_version = $php_record['name'].':'.$php_record['php_fpm_init_script'].':'.$php_record['php_fpm_ini_dir'].':'.$php_record['php_fpm_pool_dir'];
155                     } else {
156                         $php_version = $php_record['name'].':'.$php_record['php_fastcgi_binary'].':'.$php_record['php_fastcgi_ini_dir'];
157                     }
158                     $selected = ($php_version == $this->dataRecord["fastcgi_php_version"])?'SELECTED':'';
159                     $php_select .= "<option value='$php_version' $selected>".$php_record['name']."</option>\r\n";
160                 }
161             }
162             $app->tpl->setVar("fastcgi_php_version",$php_select);
163             unset($php_records);
164
ac099e 165             // add limits to template to be able to hide settings
M 166             foreach($read_limits as $limit) $app->tpl->setVar($limit, $client[$limit]);
167             
168             
76ebcb 169             //* Reseller: If the logged in user is not admin and has sub clients (is a reseller)
F 170         } elseif ($_SESSION["s"]["user"]["typ"] != 'admin' && $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
171
172             // Get the limits of the client
173             $client_group_id = $_SESSION["s"]["user"]["default_group"];
ac099e 174             $client = $app->db->queryOneRecord("SELECT client.client_id, client.limit_web_domain, client.default_webserver, client.contact_name, CONCAT(client.company_name,' :: ',client.contact_name) 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");
02384b 175             
T 176             //* Get global web config
177             $web_config = $app->getconf->get_server_config($client['default_webserver'], 'web');
178             
76ebcb 179             // Set the webserver to the default server of the client
F 180             $tmp = $app->db->queryOneRecord("SELECT server_name FROM server WHERE server_id = $client[default_webserver]");
181             $app->tpl->setVar("server_id","<option value='$client[default_webserver]'>$tmp[server_name]</option>");
182             unset($tmp);
183
184             // Fill the client select field
185             $sql = "SELECT sys_group.groupid, sys_group.name, CONCAT(client.company_name,' :: ',client.contact_name) as contactname FROM sys_group, client WHERE sys_group.client_id = client.client_id AND client.parent_client_id = ".$client['client_id']." ORDER BY sys_group.name";
186             $records = $app->db->queryAllRecords($sql);
187             $tmp = $app->db->queryOneRecord("SELECT groupid FROM sys_group WHERE client_id = ".$client['client_id']);
188             $client_select = '<option value="'.$tmp['groupid'].'">'.$client['name'].' :: '.$client['contactname'].'</option>';
189             //$tmp_data_record = $app->tform->getDataRecord($this->id);
190             if(is_array($records)) {
191                 foreach( $records as $rec) {
192                     $selected = @(is_array($this->dataRecord) && ($rec["groupid"] == $this->dataRecord['client_group_id'] || $rec["groupid"] == $this->dataRecord['sys_groupid']))?'SELECTED':'';
193                     $client_select .= "<option value='$rec[groupid]' $selected>$rec[name] :: $rec[contactname]</option>\r\n";
194                 }
195             }
196             $app->tpl->setVar("client_group_id",$client_select);
197
198             //* Fill the IPv4 select field with the IP addresses that are allowed for this client
199             $sql = "SELECT ip_address FROM server_ip WHERE server_id = ".$client['default_webserver']." AND ip_type = 'IPv4' AND (client_id = 0 OR client_id=".$_SESSION['s']['user']['client_id'].")";
200             $ips = $app->db->queryAllRecords($sql);
02384b 201             $ip_select = ($web_config['enable_ip_wildcard'] == 'y')?"<option value='*'>*</option>":"";
76ebcb 202             //$ip_select = "";
F 203             if(is_array($ips)) {
204                 foreach( $ips as $ip) {
205                     $selected = ($ip["ip_address"] == $this->dataRecord["ip_address"])?'SELECTED':'';
206                     $ip_select .= "<option value='$ip[ip_address]' $selected>$ip[ip_address]</option>\r\n";
207                 }
208             }
209             $app->tpl->setVar("ip_address",$ip_select);
210             unset($tmp);
211             unset($ips);
212             
213             //* Fill the IPv6 select field with the IP addresses that are allowed for this client
214             $sql = "SELECT ip_address FROM server_ip WHERE server_id = ".$client['default_webserver']." AND ip_type = 'IPv6' AND (client_id = 0 OR client_id=".$_SESSION['s']['user']['client_id'].")";
215             $ips = $app->db->queryAllRecords($sql);
216             $ip_select = "<option value=''></option>";
217             //$ip_select = "";
218             if(is_array($ips)) {
219                 foreach( $ips as $ip) {
220                     $selected = ($ip["ip_address"] == $this->dataRecord["ipv6_address"])?'SELECTED':'';
221                     $ip_select .= "<option value='$ip[ip_address]' $selected>$ip[ip_address]</option>\r\n";
222                 }
223             }
224             $app->tpl->setVar("ipv6_address",$ip_select);
225             unset($tmp);
226             unset($ips);
227             
228             //PHP Version Selection (FastCGI)
229             $server_type = 'apache';
230             if(!empty($web_config['server_type'])) $server_type = $web_config['server_type'];
231             if($server_type == 'nginx' && $this->dataRecord['php'] == 'fast-cgi') $this->dataRecord['php'] = 'php-fpm';
232             if($this->dataRecord['php'] == 'php-fpm'){
c146a2 233                 $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fpm_init_script != '' AND php_fpm_ini_dir != '' AND php_fpm_pool_dir != '' AND server_id = ".($this->id > 0 ? $this->dataRecord['server_id'] : $client['default_webserver'])." AND (client_id = 0 OR client_id=".$_SESSION['s']['user']['client_id'].")");
76ebcb 234             }
F 235             if($this->dataRecord['php'] == 'fast-cgi') {
c146a2 236                 $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = ".($this->id > 0 ? $this->dataRecord['server_id'] : $client['default_webserver'])." AND (client_id = 0 OR client_id=".$_SESSION['s']['user']['client_id'].")");
76ebcb 237             }
F 238             $php_select = "<option value=''>Default</option>";
239             if(is_array($php_records) && !empty($php_records)) {
240                 foreach( $php_records as $php_record) {
241                     if($this->dataRecord['php'] == 'php-fpm'){
242                         $php_version = $php_record['name'].':'.$php_record['php_fpm_init_script'].':'.$php_record['php_fpm_ini_dir'].':'.$php_record['php_fpm_pool_dir'];
243                     } else {
244                         $php_version = $php_record['name'].':'.$php_record['php_fastcgi_binary'].':'.$php_record['php_fastcgi_ini_dir'];
245                     }
246                     $selected = ($php_version == $this->dataRecord["fastcgi_php_version"])?'SELECTED':'';
247                     $php_select .= "<option value='$php_version' $selected>".$php_record['name']."</option>\r\n";
248                 }
249             }
250             $app->tpl->setVar("fastcgi_php_version",$php_select);
251             unset($php_records);
ac099e 252             
M 253             // add limits to template to be able to hide settings
254             foreach($read_limits as $limit) $app->tpl->setVar($limit, $client[$limit]);
255             
256             
76ebcb 257             //* Admin: If the logged in user is admin
F 258         } else {
259
260             // The user is admin, so we fill in all IP addresses of the server
261             if($this->id > 0) {
262                 if(!isset($this->dataRecord["server_id"])){
65ea2e 263                     $tmp = $app->db->queryOneRecord("SELECT server_id FROM web_domain WHERE domain_id = ".$app->functions->intval($this->id));
76ebcb 264                     $this->dataRecord["server_id"] = $tmp["server_id"];
F 265                     unset($tmp);
266                 }
267                 $server_id = @$this->dataRecord["server_id"];
268             } else {
269                 // Get the first server ID
270                 $tmp = $app->db->queryOneRecord("SELECT server_id FROM server WHERE web_server = 1 ORDER BY server_name LIMIT 0,1");
271                 $server_id = $tmp['server_id'];
272             }
02384b 273             
T 274             //* get global web config
275             $web_config = $app->getconf->get_server_config($server_id, 'web');
76ebcb 276         
F 277             //* Fill the IPv4 select field
278             $sql = "SELECT ip_address FROM server_ip WHERE ip_type = 'IPv4' AND server_id = $server_id";
279             $ips = $app->db->queryAllRecords($sql);
02384b 280             $ip_select = ($web_config['enable_ip_wildcard'] == 'y')?"<option value='*'>*</option>":"";
76ebcb 281             //$ip_select = "";
F 282             if(is_array($ips)) {
283                 foreach( $ips as $ip) {
284                     $selected = ($ip["ip_address"] == $this->dataRecord["ip_address"])?'SELECTED':'';
285                     $ip_select .= "<option value='$ip[ip_address]' $selected>$ip[ip_address]</option>\r\n";
286                 }
287             }
288             $app->tpl->setVar("ip_address",$ip_select);
289             unset($tmp);
290             unset($ips);
291             
292             //* Fill the IPv6 select field
293             $sql = "SELECT ip_address FROM server_ip WHERE ip_type = 'IPv6' AND server_id = $server_id";
294             $ips = $app->db->queryAllRecords($sql);
295             $ip_select = "<option value=''></option>";
296             //$ip_select = "";
297             if(is_array($ips)) {
298                 foreach( $ips as $ip) {
299                     $selected = ($ip["ip_address"] == $this->dataRecord["ipv6_address"])?'SELECTED':'';
300                     $ip_select .= "<option value='$ip[ip_address]' $selected>$ip[ip_address]</option>\r\n";
301                 }
302             }
303             $app->tpl->setVar("ipv6_address",$ip_select);
304             unset($tmp);
305             unset($ips);
306             
307             //PHP Version Selection (FastCGI)
308             $server_type = 'apache';
309             if(!empty($web_config['server_type'])) $server_type = $web_config['server_type'];
310             if($server_type == 'nginx' && $this->dataRecord['php'] == 'fast-cgi') $this->dataRecord['php'] = 'php-fpm';
311             if($this->dataRecord['php'] == 'php-fpm'){
312                 $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fpm_init_script != '' AND php_fpm_ini_dir != '' AND php_fpm_pool_dir != '' AND server_id = $server_id");
313             }
314             if($this->dataRecord['php'] == 'fast-cgi') {
315                 $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = $server_id");
316             }
317             $php_select = "<option value=''>Default</option>";
318             if(is_array($php_records) && !empty($php_records)) {
319                 foreach( $php_records as $php_record) {
320                     if($this->dataRecord['php'] == 'php-fpm'){
321                         $php_version = $php_record['name'].':'.$php_record['php_fpm_init_script'].':'.$php_record['php_fpm_ini_dir'].':'.$php_record['php_fpm_pool_dir'];
322                     } else {
323                         $php_version = $php_record['name'].':'.$php_record['php_fastcgi_binary'].':'.$php_record['php_fastcgi_ini_dir'];
324                     }
325                     $selected = ($php_version == $this->dataRecord["fastcgi_php_version"])?'SELECTED':'';
326                     $php_select .= "<option value='$php_version' $selected>".$php_record['name']."</option>\r\n";
327                 }
328             }
329             $app->tpl->setVar("fastcgi_php_version",$php_select);
330             unset($php_records);
331
332             // Fill the client select field
333             $sql = "SELECT sys_group.groupid, sys_group.name, CONCAT(client.company_name,' :: ',client.contact_name) as contactname FROM sys_group, client WHERE sys_group.client_id = client.client_id AND sys_group.client_id > 0 ORDER BY sys_group.name";
334             $clients = $app->db->queryAllRecords($sql);
335             $client_select = "<option value='0'></option>";
336             //$tmp_data_record = $app->tform->getDataRecord($this->id);
337             if(is_array($clients)) {
338                 foreach( $clients as $client) {
339                     //$selected = @($client["groupid"] == $tmp_data_record["sys_groupid"])?'SELECTED':'';
340                     $selected = @(is_array($this->dataRecord) && ($client["groupid"] == $this->dataRecord['client_group_id'] || $client["groupid"] == $this->dataRecord['sys_groupid']))?'SELECTED':'';
341                     $client_select .= "<option value='$client[groupid]' $selected>$client[name] :: $client[contactname]</option>\r\n";
342                 }
343             }
344             $app->tpl->setVar("client_group_id",$client_select);
ac099e 345             
c3bf39 346             foreach($read_limits as $limit) $app->tpl->setVar($limit, ($limit == 'force_suexec' ? 'n' : 'y'));
ef55b5 347             
F 348             // Directive Snippets
349             $php_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'php' AND active = 'y'");
350             $php_directive_snippets_txt = '';
351             if(is_array($php_directive_snippets) && !empty($php_directive_snippets)){
352                     foreach($php_directive_snippets as $php_directive_snippet){
72695f 353                         $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> ';
ef55b5 354                     }
F 355             }
356             if($php_directive_snippets_txt == '') $php_directive_snippets_txt = '------';
357             $app->tpl->setVar("php_directive_snippets_txt",$php_directive_snippets_txt);
358             
359             if($server_type == 'apache'){
360                 $apache_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'apache' AND active = 'y'");
361                 $apache_directive_snippets_txt = '';
362                 if(is_array($apache_directive_snippets) && !empty($apache_directive_snippets)){
363                         foreach($apache_directive_snippets as $apache_directive_snippet){
72695f 364                             $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> ';
ef55b5 365                         }
F 366                 }
367                 if($apache_directive_snippets_txt == '') $apache_directive_snippets_txt = '------';
368                 $app->tpl->setVar("apache_directive_snippets_txt",$apache_directive_snippets_txt);
369             }
370             
3a93f2 371             if($server_type == 'nginx'){
ef55b5 372                 $nginx_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'nginx' AND active = 'y'");
F 373                 $nginx_directive_snippets_txt = '';
374                 if(is_array($nginx_directive_snippets) && !empty($nginx_directive_snippets)){
375                         foreach($nginx_directive_snippets as $nginx_directive_snippet){
72695f 376                             $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> ';
ef55b5 377                         }
F 378                 }
379                 if($nginx_directive_snippets_txt == '') $nginx_directive_snippets_txt = '------';
380                 $app->tpl->setVar("nginx_directive_snippets_txt",$nginx_directive_snippets_txt);
381             }
bfcdef 382             
T 383             $proxy_directive_snippets = $app->db->queryAllRecords("SELECT * FROM directive_snippets WHERE type = 'proxy' AND active = 'y'");
384             $proxy_directive_snippets_txt = '';
385             if(is_array($proxy_directive_snippets) && !empty($proxy_directive_snippets)){
386                     foreach($proxy_directive_snippets as $proxy_directive_snippet){
72695f 387                         $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> ';
bfcdef 388                     }
T 389             }
390             if($proxy_directive_snippets_txt == '') $proxy_directive_snippets_txt = '------';
391             $app->tpl->setVar("proxy_directive_snippets_txt",$proxy_directive_snippets_txt);
76ebcb 392         }
F 393
394         $ssl_domain_select = '';
395         $tmp = $app->db->queryOneRecord("SELECT domain FROM web_domain WHERE domain_id = ".$this->id);
10b4c8 396         $ssl_domains = array($tmp["domain"],'www.'.$tmp["domain"],'*.'.$tmp["domain"]);
76ebcb 397         if(is_array($ssl_domains)) {
F 398             foreach( $ssl_domains as $ssl_domain) {
399                 $selected = ($ssl_domain == $this->dataRecord['ssl_domain'])?'SELECTED':'';
400                 $ssl_domain_select .= "<option value='$ssl_domain' $selected>$ssl_domain</option>\r\n";
401             }
402         }
403         $app->tpl->setVar("ssl_domain",$ssl_domain_select);
404         unset($ssl_domain_select);
405         unset($ssl_domains);
406         unset($ssl_domain);
407
408         if($this->id > 0) {
409             //* we are editing a existing record
410             $app->tpl->setVar("edit_disabled", 1);
411             $app->tpl->setVar("server_id_value", $this->dataRecord["server_id"]);
412         } else {
413             $app->tpl->setVar("edit_disabled", 0);
414         }
415
416         $tmp_txt = ($this->dataRecord['traffic_quota_lock'] == 'y')?'<b>('.$app->tform->lng('traffic_quota_exceeded_txt').')</b>':'';
417         $app->tpl->setVar("traffic_quota_exceeded_txt", $tmp_txt);
418
419         /*
420          * Now we have to check, if we should use the domain-module to select the domain
421          * or not
422          */
423         $settings = $app->getconf->get_global_config('domains');
424         if ($settings['use_domain_module'] == 'y') {
425             /*
426              * The domain-module is in use.
427             */
10b4c8 428             $domains = $app->tools_sites->getDomainModuleDomains();
76ebcb 429             $domain_select = '';
F 430             if(is_array($domains) && sizeof($domains) > 0) {
431                 /* We have domains in the list, so create the drop-down-list */
432                 foreach( $domains as $domain) {
4e18bd 433                     $domain_select .= "<option value=" . $domain['domain_id'] ;
76ebcb 434                     if ($domain['domain'] == $this->dataRecord["domain"]) {
F 435                         $domain_select .= " selected";
436                     }
8c1761 437                     $domain_select .= ">" . $app->functions->idn_decode($domain['domain']) . "</option>\r\n";
76ebcb 438                 }
F 439             }
440             else {
441                 /*
442                  * We have no domains in the domain-list. This means, we can not add ANY new domain.
443                  * To avoid, that the variable "domain_option" is empty and so the user can
444                  * free enter a domain, we have to create a empty option!
445                 */
446                 $domain_select .= "<option value=''></option>\r\n";
447             }
448             $app->tpl->setVar("domain_option",$domain_select);
449         }
450
451         parent::onShowEnd();
452     }
ac099e 453     
M 454     function onShowEdit() {
455         global $app;
456         if($app->tform->checkPerm($this->id, 'riud')) $app->tform->formDef['tabs']['domain']['readonly'] = false;
457         parent::onShowEdit();
458     }
76ebcb 459
F 460     function onSubmit() {
461         global $app, $conf;
462
4e18bd 463         /* check if the domain module is used - and check if the selected domain can be used! */
f601d9 464         if($app->tform->getCurrentTab() == 'domain') {
M 465             $app->uses('ini_parser,getconf');
466             $settings = $app->getconf->get_global_config('domains');
467             if ($settings['use_domain_module'] == 'y') {
10b4c8 468                 $domain_check = $app->tools_sites->checkDomainModuleDomain($this->dataRecord['domain']);
f601d9 469                 if(!$domain_check) {
M 470                     // invalid domain selected
471                     $app->tform->errorMessage .= $app->tform->lng("domain_error_empty")."<br />";
472                 } else {
10b4c8 473                     $this->dataRecord['domain'] = $domain_check;
f601d9 474                 }
4e18bd 475             }
M 476         }
43b345 477         
T 478         // nginx: if redirect type is proxy and redirect path is no URL, display error
479         //if($this->dataRecord["redirect_type"] == 'proxy' && substr($this->dataRecord['redirect_path'],0,1) == '/'){
480         //    $app->tform->errorMessage .= $app->tform->lng("error_proxy_requires_url")."<br />";
481         //}
4e18bd 482
76ebcb 483         // Set a few fixed values
F 484         $this->dataRecord["parent_domain_id"] = 0;
485         $this->dataRecord["type"] = 'vhost';
486         $this->dataRecord["vhost_type"] = 'name';
487
ac099e 488         $read_limits = array('limit_cgi', 'limit_ssi', 'limit_perl', 'limit_ruby', 'limit_python', 'force_suexec', 'limit_hterror', 'limit_wildcard', 'limit_ssl');
M 489
490
76ebcb 491         if($_SESSION["s"]["user"]["typ"] != 'admin') {
F 492             // Get the limits of the client
493             $client_group_id = $_SESSION["s"]["user"]["default_group"];
ac099e 494             $client = $app->db->queryOneRecord("SELECT limit_traffic_quota, limit_web_domain, default_webserver, parent_client_id, limit_web_quota, client." . implode(", client.", $read_limits) . " FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
M 495             
496             if($client['limit_cgi'] != 'y') $this->dataRecord['cgi'] = '-';
497             if($client['limit_ssi'] != 'y') $this->dataRecord['ssi'] = '-';
498             if($client['limit_perl'] != 'y') $this->dataRecord['perl'] = '-';
499             if($client['limit_ruby'] != 'y') $this->dataRecord['ruby'] = '-';
500             if($client['limit_python'] != 'y') $this->dataRecord['python'] = '-';
10b4c8 501             if($client['force_suexec'] == 'y') $this->dataRecord['suexec'] = 'y';
ac099e 502             if($client['limit_hterror'] != 'y') $this->dataRecord['errordocs'] = '-';
M 503             if($client['limit_wildcard'] != 'y' && $this->dataRecord['subdomain'] == '*') $this->dataRecord['subdomain'] = '-';
504             if($client['limit_ssl'] != 'y') $this->dataRecord['ssl'] = '-';
505             
76ebcb 506             //* Check the website quota of the client
F 507             if(isset($_POST["hd_quota"]) && $client["limit_web_quota"] >= 0) {
65ea2e 508                 $tmp = $app->db->queryOneRecord("SELECT sum(hd_quota) as webquota FROM web_domain WHERE domain_id != ".$app->functions->intval($this->id)." AND ".$app->tform->getAuthSQL('u'));
76ebcb 509                 $webquota = $tmp["webquota"];
65ea2e 510                 $new_web_quota = $app->functions->intval($this->dataRecord["hd_quota"]);
76ebcb 511                 if(($webquota + $new_web_quota > $client["limit_web_quota"]) || ($new_web_quota < 0 && $client["limit_web_quota"] >= 0)) {
F 512                     $max_free_quota = floor($client["limit_web_quota"] - $webquota);
513                     if($max_free_quota < 0) $max_free_quota = 0;
514                     $app->tform->errorMessage .= $app->tform->lng("limit_web_quota_free_txt").": ".$max_free_quota." MB<br>";
515                     // Set the quota field to the max free space
516                     $this->dataRecord["hd_quota"] = $max_free_quota;
517                 }
518                 unset($tmp);
519                 unset($tmp_quota);
520             }
521
522             //* Check the traffic quota of the client
523             if(isset($_POST["traffic_quota"]) && $client["limit_traffic_quota"] > 0) {
65ea2e 524                 $tmp = $app->db->queryOneRecord("SELECT sum(traffic_quota) as trafficquota FROM web_domain WHERE domain_id != ".$app->functions->intval($this->id)." AND ".$app->tform->getAuthSQL('u'));
76ebcb 525                 $trafficquota = $tmp["trafficquota"];
65ea2e 526                 $new_traffic_quota = $app->functions->intval($this->dataRecord["traffic_quota"]);
76ebcb 527                 if(($trafficquota + $new_traffic_quota > $client["limit_traffic_quota"]) || ($new_traffic_quota < 0 && $client["limit_traffic_quota"] >= 0)) {
F 528                     $max_free_quota = floor($client["limit_traffic_quota"] - $trafficquota);
529                     if($max_free_quota < 0) $max_free_quota = 0;
530                     $app->tform->errorMessage .= $app->tform->lng("limit_traffic_quota_free_txt").": ".$max_free_quota." MB<br>";
531                     // Set the quota field to the max free space
532                     $this->dataRecord["traffic_quota"] = $max_free_quota;
533                 }
534                 unset($tmp);
535                 unset($tmp_quota);
536             }
537             
538             if($client['parent_client_id'] > 0) {
539                 // Get the limits of the reseller
540                 $reseller = $app->db->queryOneRecord("SELECT limit_traffic_quota, limit_web_domain, default_webserver, limit_web_quota FROM client WHERE client_id = ".$client['parent_client_id']);
541
542                 //* Check the website quota of the client
543                 if(isset($_POST["hd_quota"]) && $reseller["limit_web_quota"] >= 0) {
65ea2e 544                     $tmp = $app->db->queryOneRecord("SELECT sum(hd_quota) as webquota FROM web_domain WHERE domain_id != ".$app->functions->intval($this->id)." AND ".$app->tform->getAuthSQL('u'));
76ebcb 545                     $webquota = $tmp["webquota"];
65ea2e 546                     $new_web_quota = $app->functions->intval($this->dataRecord["hd_quota"]);
76ebcb 547                     if(($webquota + $new_web_quota > $reseller["limit_web_quota"]) || ($new_web_quota < 0 && $reseller["limit_web_quota"] >= 0)) {
F 548                         $max_free_quota = floor($reseller["limit_web_quota"] - $webquota);
549                         if($max_free_quota < 0) $max_free_quota = 0;
550                         $app->tform->errorMessage .= $app->tform->lng("limit_web_quota_free_txt").": ".$max_free_quota." MB<br>";
551                         // Set the quota field to the max free space
552                         $this->dataRecord["hd_quota"] = $max_free_quota;
553                     }
554                     unset($tmp);
555                     unset($tmp_quota);
556                 }
557
558                 //* Check the traffic quota of the client
559                 if(isset($_POST["traffic_quota"]) && $reseller["limit_traffic_quota"] > 0) {
65ea2e 560                     $tmp = $app->db->queryOneRecord("SELECT sum(traffic_quota) as trafficquota FROM web_domain WHERE domain_id != ".$app->functions->intval($this->id)." AND ".$app->tform->getAuthSQL('u'));
76ebcb 561                     $trafficquota = $tmp["trafficquota"];
65ea2e 562                     $new_traffic_quota = $app->functions->intval($this->dataRecord["traffic_quota"]);
76ebcb 563                     if(($trafficquota + $new_traffic_quota > $reseller["limit_traffic_quota"]) || ($new_traffic_quota < 0 && $reseller["limit_traffic_quota"] >= 0)) {
F 564                         $max_free_quota = floor($reseller["limit_traffic_quota"] - $trafficquota);
565                         if($max_free_quota < 0) $max_free_quota = 0;
566                         $app->tform->errorMessage .= $app->tform->lng("limit_traffic_quota_free_txt").": ".$max_free_quota." MB<br>";
567                         // Set the quota field to the max free space
568                         $this->dataRecord["traffic_quota"] = $max_free_quota;
569                     }
570                     unset($tmp);
571                     unset($tmp_quota);
572                 }
573             }
574
575             // When the record is updated
576             if($this->id > 0) {
577                 // restore the server ID if the user is not admin and record is edited
65ea2e 578                 $tmp = $app->db->queryOneRecord("SELECT server_id, `cgi`, `ssi`, `perl`, `ruby`, `python`, `suexec`, `errordocs`, `subdomain`, `ssl` FROM web_domain WHERE domain_id = ".$app->functions->intval($this->id));
76ebcb 579                 $this->dataRecord["server_id"] = $tmp["server_id"];
ac099e 580                 
M 581                 // set the settings to current if not provided (or cleared due to limits)
582                 if($this->dataRecord['cgi'] == '-') $this->dataRecord['cgi'] = $tmp['cgi'];
583                 if($this->dataRecord['ssi'] == '-') $this->dataRecord['ssi'] = $tmp['ssi'];
584                 if($this->dataRecord['perl'] == '-') $this->dataRecord['perl'] = $tmp['perl'];
585                 if($this->dataRecord['ruby'] == '-') $this->dataRecord['ruby'] = $tmp['ruby'];
586                 if($this->dataRecord['python'] == '-') $this->dataRecord['python'] = $tmp['python'];
587                 if($this->dataRecord['suexec'] == '-') $this->dataRecord['suexec'] = $tmp['suexec'];
588                 if($this->dataRecord['errordocs'] == '-') $this->dataRecord['errordocs'] = $tmp['errordocs'];
589                 if($this->dataRecord['subdomain'] == '-') $this->dataRecord['subdomain'] = $tmp['subdomain'];
590                 if($this->dataRecord['ssl'] == '-') $this->dataRecord['ssl'] = $tmp['ssl'];
591                 
76ebcb 592                 unset($tmp);
F 593                 // When the record is inserted
594             } else {
595                 //* set the server ID to the default webserver of the client
596                 $this->dataRecord["server_id"] = $client["default_webserver"];
597
598                 // Check if the user may add another web_domain
599                 if($client["limit_web_domain"] >= 0) {
600                     $tmp = $app->db->queryOneRecord("SELECT count(domain_id) as number FROM web_domain WHERE sys_groupid = $client_group_id and type = 'vhost'");
601                     if($tmp["number"] >= $client["limit_web_domain"]) {
602                         $app->error($app->tform->wordbook["limit_web_domain_txt"]);
603                     }
604                 }
605
606             }
607
608             // 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
609             if(!$app->auth->has_clients($_SESSION['s']['user']['userid'])) unset($this->dataRecord["client_group_id"]);
610         }
611         
612         //* make sure that the email domain is lowercase
613         if(isset($this->dataRecord["domain"])) $this->dataRecord["domain"] = strtolower($this->dataRecord["domain"]);
614         
615         //* get the server config for this server
616         $app->uses("getconf");
65ea2e 617         $web_config = $app->getconf->get_server_config($app->functions->intval(isset($this->dataRecord["server_id"]) ? $this->dataRecord["server_id"] : 0),'web');
76ebcb 618         //* Check for duplicate ssl certs per IP if SNI is disabled
F 619         if(isset($this->dataRecord['ssl']) && $this->dataRecord['ssl'] == 'y' && $web_config['enable_sni'] != 'y') {
620             $sql = "SELECT count(domain_id) as number FROM web_domain WHERE `ssl` = 'y' AND ip_address = '".$app->db->quote($this->dataRecord['ip_address'])."' and domain_id != ".$this->id;
621             $tmp = $app->db->queryOneRecord($sql);
622             if($tmp['number'] > 0) $app->tform->errorMessage .= $app->tform->lng("error_no_sni_txt");
623         }
624         
625         // Check if pm.max_children >= pm.max_spare_servers >= pm.start_servers >= pm.min_spare_servers > 0
dd7ce4 626         if(isset($this->dataRecord['pm_max_children']) && $this->dataRecord['pm'] == 'dynamic') {
65ea2e 627             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){
76ebcb 628         
F 629             } else {
630                 $app->tform->errorMessage .= $app->tform->lng("error_php_fpm_pm_settings_txt").'<br>';
631             }
632         }
633
634         parent::onSubmit();
635     }
636
637     function onAfterInsert() {
638         global $app, $conf;
639
640         // make sure that the record belongs to the clinet group and not the admin group when admin inserts it
641         // also make sure that the user can not delete domain created by a admin
642         if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($this->dataRecord["client_group_id"])) {
65ea2e 643             $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
76ebcb 644             $app->db->query("UPDATE web_domain SET sys_groupid = $client_group_id, sys_perm_group = 'ru' WHERE domain_id = ".$this->id);
F 645         }
646         if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($this->dataRecord["client_group_id"])) {
65ea2e 647             $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
76ebcb 648             $app->db->query("UPDATE web_domain SET sys_groupid = $client_group_id, sys_perm_group = 'riud' WHERE domain_id = ".$this->id);
F 649         }
650
651         // Get configuration for the web system
652         $app->uses("getconf");
653         $web_rec = $app->tform->getDataRecord($this->id);
65ea2e 654         $web_config = $app->getconf->get_server_config($app->functions->intval($web_rec["server_id"]),'web');
76ebcb 655         $document_root = str_replace("[website_id]",$this->id,$web_config["website_path"]);
F 656         $document_root = str_replace("[website_idhash_1]",$this->id_hash($page_form->id,1),$document_root);
657         $document_root = str_replace("[website_idhash_2]",$this->id_hash($page_form->id,1),$document_root);
658         $document_root = str_replace("[website_idhash_3]",$this->id_hash($page_form->id,1),$document_root);
659         $document_root = str_replace("[website_idhash_4]",$this->id_hash($page_form->id,1),$document_root);
660
661         // get the ID of the client
662         if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
663             $client_group_id = $_SESSION["s"]["user"]["default_group"];
664             $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = $client_group_id");
65ea2e 665             $client_id = $app->functions->intval($client["client_id"]);
76ebcb 666         } else {
65ea2e 667             //$client_id = $app->functions->intval($this->dataRecord["client_group_id"]);
M 668             $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ".$app->functions->intval($this->dataRecord["client_group_id"]));
669             $client_id = $app->functions->intval($client["client_id"]);
76ebcb 670         }
F 671
672         // Set the values for document_root, system_user and system_group
673         $system_user = $app->db->quote('web'.$this->id);
674         $system_group = $app->db->quote('client'.$client_id);
675         $document_root = str_replace("[client_id]",$client_id,$document_root);
676         $document_root = str_replace("[client_idhash_1]",$this->id_hash($client_id,1),$document_root);
677         $document_root = str_replace("[client_idhash_2]",$this->id_hash($client_id,2),$document_root);
678         $document_root = str_replace("[client_idhash_3]",$this->id_hash($client_id,3),$document_root);
679         $document_root = str_replace("[client_idhash_4]",$this->id_hash($client_id,4),$document_root);
680         $document_root = $app->db->quote($document_root);
681         $php_open_basedir = str_replace("[website_path]",$document_root,$web_config["php_open_basedir"]);
682         $php_open_basedir = $app->db->quote(str_replace("[website_domain]",$web_rec['domain'],$php_open_basedir));
683         $htaccess_allow_override = $app->db->quote($web_config["htaccess_allow_override"]);
684
685         $sql = "UPDATE web_domain SET system_user = '$system_user', system_group = '$system_group', document_root = '$document_root', allow_override = '$htaccess_allow_override', php_open_basedir = '$php_open_basedir'  WHERE domain_id = ".$this->id;
686         $app->db->query($sql);
687     }
688
689     function onBeforeUpdate () {
690         global $app, $conf;
691
692         //* Check if the server has been changed
693         // We do this only for the admin or reseller users, as normal clients can not change the server ID anyway
694         if($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
695             if (isset($this->dataRecord["server_id"])) {
696                 $rec = $app->db->queryOneRecord("SELECT server_id from web_domain WHERE domain_id = ".$this->id);
697                 if($rec['server_id'] != $this->dataRecord["server_id"]) {
698                     //* Add a error message and switch back to old server
699                     $app->tform->errorMessage .= $app->lng('The Server can not be changed.');
700                     $this->dataRecord["server_id"] = $rec['server_id'];
701                 }
702                 unset($rec);
703             }
704             //* If the user is neither admin nor reseller
705         } else {
706             //* We do not allow users to change a domain which has been created by the admin
ac099e 707             $rec = $app->db->queryOneRecord("SELECT sys_perm_group, domain, ip_address, ipv6_address from web_domain WHERE domain_id = ".$this->id);
76ebcb 708             if(isset($this->dataRecord["domain"]) && $rec['domain'] != $this->dataRecord["domain"] && $app->tform->checkPerm($this->id,'u')) {
F 709                 //* Add a error message and switch back to old server
710                 $app->tform->errorMessage .= $app->lng('The Domain can not be changed. Please ask your Administrator if you want to change the domain name.');
711                 $this->dataRecord["domain"] = $rec['domain'];
712             }
ac099e 713             if(isset($this->dataRecord["ip_address"]) && $rec['ip_address'] != $this->dataRecord["ip_address"] && $rec['sys_perm_group'] != 'riud') {
M 714                 $this->dataRecord["ip_address"] = $rec['ip_address'];
715             }
716             if(isset($this->dataRecord["ipv6_address"]) && $rec['ipv6_address'] != $this->dataRecord["ipv6_address"] && $rec['sys_perm_group'] != 'riud') {
717                 $this->dataRecord["ipv6_address"] = $rec['ipv6_address'];
718             }
76ebcb 719             unset($rec);
F 720         }
721
722         //* Check that all fields for the SSL cert creation are filled
723         if(isset($this->dataRecord['ssl_action']) && $this->dataRecord['ssl_action'] == 'create') {
724             if($this->dataRecord['ssl_state'] == '') $app->tform->errorMessage .= $app->tform->lng('error_ssl_state_empty').'<br />';
725             if($this->dataRecord['ssl_locality'] == '') $app->tform->errorMessage .= $app->tform->lng('error_ssl_locality_empty').'<br />';
726             if($this->dataRecord['ssl_organisation'] == '') $app->tform->errorMessage .= $app->tform->lng('error_ssl_organisation_empty').'<br />';
727             if($this->dataRecord['ssl_organisation_unit'] == '') $app->tform->errorMessage .= $app->tform->lng('error_ssl_organisation_unit_empty').'<br />';
728             if($this->dataRecord['ssl_country'] == '') $app->tform->errorMessage .= $app->tform->lng('error_ssl_country_empty').'<br />';
729         }
730         
731         if(isset($this->dataRecord['ssl_action']) && $this->dataRecord['ssl_action'] == 'save') {
732             if(trim($this->dataRecord['ssl_cert']) == '') $app->tform->errorMessage .= $app->tform->lng('error_ssl_cert_empty').'<br />';
733         }
734
735     }
736
737     function onAfterUpdate() {
738         global $app, $conf;
739
740         // make sure that the record belongs to the client group and not the admin group when a admin inserts it
741         // also make sure that the user can not delete domain created by a admin
742         if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($this->dataRecord["client_group_id"])) {
65ea2e 743             $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
76ebcb 744             $app->db->query("UPDATE web_domain SET sys_groupid = $client_group_id, sys_perm_group = 'ru' WHERE domain_id = ".$this->id);
F 745         }
746         if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($this->dataRecord["client_group_id"])) {
65ea2e 747             $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
76ebcb 748             $app->db->query("UPDATE web_domain SET sys_groupid = $client_group_id, sys_perm_group = 'riud' WHERE domain_id = ".$this->id);
F 749         }
750
751         // Get configuration for the web system
752         $app->uses("getconf");
753         $web_rec = $app->tform->getDataRecord($this->id);
65ea2e 754         $web_config = $app->getconf->get_server_config($app->functions->intval($web_rec["server_id"]),'web');
76ebcb 755         $document_root = str_replace("[website_id]",$this->id,$web_config["website_path"]);
F 756         $page_formid = isset($page_form->id) ? $page_form->id : '';
757         $document_root = str_replace("[website_idhash_1]",$this->id_hash($page_formid,1),$document_root);
758         $document_root = str_replace("[website_idhash_2]",$this->id_hash($page_formid,1),$document_root);
759         $document_root = str_replace("[website_idhash_3]",$this->id_hash($page_formid,1),$document_root);
760         $document_root = str_replace("[website_idhash_4]",$this->id_hash($page_formid,1),$document_root);
761
762         // get the ID of the client
763         if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
764             $client_group_id = $_SESSION["s"]["user"]["default_group"];
765             $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = $client_group_id");
65ea2e 766             $client_id = $app->functions->intval($client["client_id"]);
76ebcb 767         } elseif (isset($this->dataRecord["client_group_id"])) {
F 768             $client_group_id = $this->dataRecord["client_group_id"];
65ea2e 769             $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ".$app->functions->intval(@$this->dataRecord["client_group_id"]));
M 770             $client_id = $app->functions->intval($client["client_id"]);
76ebcb 771         } else {
F 772             $client_group_id = $web_rec['sys_groupid'];
65ea2e 773             $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ".$app->functions->intval($client_group_id));
M 774             $client_id = $app->functions->intval($client["client_id"]);
76ebcb 775         }
F 776
777         if(($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) &&  isset($this->dataRecord["client_group_id"]) && $this->dataRecord["client_group_id"] != $this->oldDataRecord["sys_groupid"]) {
778             // Set the values for document_root, system_user and system_group
779             $system_user = $app->db->quote('web'.$this->id);
780             $system_group = $app->db->quote('client'.$client_id);
781             $document_root = str_replace("[client_id]",$client_id,$document_root);
782             $document_root = str_replace("[client_idhash_1]",$this->id_hash($client_id,1),$document_root);
783             $document_root = str_replace("[client_idhash_2]",$this->id_hash($client_id,2),$document_root);
784             $document_root = str_replace("[client_idhash_3]",$this->id_hash($client_id,3),$document_root);
785             $document_root = str_replace("[client_idhash_4]",$this->id_hash($client_id,4),$document_root);
786             $document_root = $app->db->quote($document_root);
787
788             $sql = "UPDATE web_domain SET system_user = '$system_user', system_group = '$system_group', document_root = '$document_root' WHERE domain_id = ".$this->id;
789             //$sql = "UPDATE web_domain SET system_user = '$system_user', system_group = '$system_group' WHERE domain_id = ".$this->id;
790             $app->db->query($sql);
791
792             // Update the FTP user(s) too
793             $records = $app->db->queryAllRecords("SELECT ftp_user_id FROM ftp_user WHERE parent_domain_id = ".$this->id);
794             foreach($records as $rec) {
795                 $app->db->datalogUpdate('ftp_user', "sys_userid = '".$web_rec['sys_userid']."', sys_groupid = '".$web_rec['sys_groupid']."', uid = '$system_user', gid = '$system_group', dir = '$document_root'", 'ftp_user_id', $rec['ftp_user_id']);
796             }
797             unset($records);
798             unset($rec);
799
800             // Update the Shell user(s) too
801             $records = $app->db->queryAllRecords("SELECT shell_user_id FROM shell_user WHERE parent_domain_id = ".$this->id);
802             foreach($records as $rec) {
803                 $app->db->datalogUpdate('shell_user', "sys_userid = '".$web_rec['sys_userid']."', sys_groupid = '".$web_rec['sys_groupid']."', puser = '$system_user', pgroup = '$system_group', dir = '$document_root'", 'shell_user_id', $rec['shell_user_id']);
804             }
805             unset($records);
806             unset($rec);
807             
808             //* Update all subdomains and alias domains
72695f 809             $records = $app->db->queryAllRecords("SELECT domain_id, `domain`, `type`, `web_folder` FROM web_domain WHERE parent_domain_id = ".$this->id);
76ebcb 810             foreach($records as $rec) {
72695f 811                 $update_columns = "sys_userid = '".$web_rec['sys_userid']."', sys_groupid = '".$web_rec['sys_groupid']."'";
T 812                 if($rec['type'] == 'vhostsubdomain') {
813                     $php_open_basedir = str_replace("[website_path]/web",$document_root.'/'.$rec['web_folder'],$web_config["php_open_basedir"]);
814                     $php_open_basedir = str_replace("[website_domain]/web",$rec['domain'].'/'.$rec['web_folder'],$php_open_basedir);
815                     $php_open_basedir = str_replace("[website_path]",$document_root,$php_open_basedir);
816                     $php_open_basedir = $app->db->quote(str_replace("[website_domain]",$rec['domain'],$php_open_basedir));
817
818                     $update_columns .= ", document_root = '".$document_root."', `php_open_basedir` = '".$php_open_basedir."'";
819                 }
820                 $app->db->datalogUpdate('web_domain', $update_columns, 'domain_id', $rec['domain_id']);
76ebcb 821             }
F 822             unset($records);
823             unset($rec);
824             
825             //* Update all databases
826             $records = $app->db->queryAllRecords("SELECT database_id FROM web_database WHERE parent_domain_id = ".$this->id);
827             foreach($records as $rec) {
828                 $app->db->datalogUpdate('web_database', "sys_userid = '".$web_rec['sys_userid']."', sys_groupid = '".$web_rec['sys_groupid']."'", 'database_id', $rec['database_id']);
829             }
830             unset($records);
831             unset($rec);
832
833         }
834
526b99 835         //* If the domain name has been changed, we will have to change all subdomains + APS instances
76ebcb 836         if(!empty($this->dataRecord["domain"]) && !empty($this->oldDataRecord["domain"]) && $this->dataRecord["domain"] != $this->oldDataRecord["domain"]) {
6fb93d 837             $records = $app->db->queryAllRecords("SELECT domain_id,domain FROM web_domain WHERE (type = 'subdomain' OR type = 'vhostsubdomain') AND domain LIKE '%.".$app->db->quote($this->oldDataRecord["domain"])."'");
76ebcb 838             foreach($records as $rec) {
F 839                 $subdomain = $app->db->quote(str_replace($this->oldDataRecord["domain"],$this->dataRecord["domain"],$rec['domain']));
840                 $app->db->datalogUpdate('web_domain', "domain = '".$subdomain."'", 'domain_id', $rec['domain_id']);
841             }
842             unset($records);
843             unset($rec);
844             unset($subdomain);
526b99 845             
T 846             // Update APS instances
847             $records = $app->db->queryAllRecords("SELECT id, instance_id FROM aps_instances_settings WHERE name = 'main_domain' AND value = '".$this->oldDataRecord["domain"]."'");
848             if(is_array($records) && !empty($records)){
849                 foreach($records as $rec){
850                     $app->db->datalogUpdate('aps_instances_settings', "value = '".$this->dataRecord["domain"]."'", 'id', $rec['id']);
851                     // Reinstall of package needed?
852                     //$app->db->datalogUpdate('aps_instances', "instance_status = '1'", 'id', $rec['instance_id']);
853                 }
854             }
855             unset($records);
856             unset($rec);
76ebcb 857         }
F 858
859         //* Set allow_override if empty
860         if($web_rec['allow_override'] == '') {
861             $sql = "UPDATE web_domain SET allow_override = '".$app->db->quote($web_config["htaccess_allow_override"])."' WHERE domain_id = ".$this->id;
862             $app->db->query($sql);
863         }
864         
865         //* Set php_open_basedir if empty or domain or client has been changed
866         if(empty($web_rec['php_open_basedir']) ||
10b4c8 867         (!empty($this->dataRecord["domain"]) && !empty($this->oldDataRecord["domain"]) && $this->dataRecord["domain"] != $this->oldDataRecord["domain"])) {
T 868             $php_open_basedir = $web_rec['php_open_basedir'];
869             $php_open_basedir = str_replace($this->oldDataRecord['domain'],$web_rec['domain'],$php_open_basedir);
870             $sql = "UPDATE web_domain SET php_open_basedir = '$php_open_basedir' WHERE domain_id = ".$this->id;
871             $app->db->query($sql);
872         }
873         if(empty($web_rec['php_open_basedir']) ||
76ebcb 874         (isset($this->dataRecord["client_group_id"]) && $this->dataRecord["client_group_id"] != $this->oldDataRecord["sys_groupid"])) {
F 875             $document_root = $app->db->quote(str_replace("[client_id]",$client_id,$document_root));
876             $php_open_basedir = str_replace("[website_path]",$document_root,$web_config["php_open_basedir"]);
877             $php_open_basedir = $app->db->quote(str_replace("[website_domain]",$web_rec['domain'],$php_open_basedir));
878             $sql = "UPDATE web_domain SET php_open_basedir = '$php_open_basedir' WHERE domain_id = ".$this->id;
879             $app->db->query($sql);
880         }
881         
882         //* Change database backup options when web backup options have been changed
883         if(isset($this->dataRecord['backup_interval']) && ($this->dataRecord['backup_interval'] != $this->oldDataRecord['backup_interval'] || $this->dataRecord['backup_copies'] != $this->oldDataRecord['backup_copies'])) {
884             //* Update all databases
885             $backup_interval = $this->dataRecord['backup_interval'];
886             $backup_copies = $this->dataRecord['backup_copies'];
887             $records = $app->db->queryAllRecords("SELECT database_id FROM web_database WHERE parent_domain_id = ".$this->id);
888             foreach($records as $rec) {
889                 $app->db->datalogUpdate('web_database', "backup_interval = '$backup_interval', backup_copies = '$backup_copies'", 'database_id', $rec['database_id']);
890             }
891             unset($records);
892             unset($rec);
893             unset($backup_copies);
894             unset($backup_interval);
895         }
896
897     }
898
899     function onAfterDelete() {
900         global $app, $conf;
901
902         // Delete the sub and alias domains
903         $child_domains = $app->db->queryAllRecords("SELECT * FROM web_domain WHERE parent_domain_id = ".$this->id);
904         foreach($child_domains as $d) {
905             // Saving record to datalog when db_history enabled
906             if($app->tform->formDef["db_history"] == 'yes') {
907                 $app->tform->datalogSave('DELETE',$d["domain_id"],$d,array());
908             }
909
910             $app->db->query("DELETE FROM web_domain WHERE domain_id = ".$d["domain_id"]." LIMIT 0,1");
911         }
912         unset($child_domains);
913         unset($d);
914
915     }
916
917 }
918
919 $page = new page_action;
920 $page->onLoad();
921
f70fd0 922 ?>