Marius Cramer
2014-01-24 b50d36a340f2cb3a54f7fa29699ec5d0f064f31c
interface/web/sites/web_vhost_domain_edit.php
File was renamed from interface/web/sites/web_domain_edit.php
@@ -32,7 +32,7 @@
* Begin Form configuration
******************************************/
$tform_def_file = "form/web_domain.tform.php";
$tform_def_file = "form/web_vhost_domain.tform.php";
/******************************************
* End Form configuration
@@ -49,6 +49,7 @@
$app->load('tform_actions');
class page_action extends tform_actions {
   var $_vhostdomain_type = 'domain';
   //* Returna a "3/2/1" path hash from a numeric id '123'
   function id_hash($id, $levels) {
@@ -63,18 +64,51 @@
      return $hash;
   }
   function onLoad() {
      $show_type = 'domain';
      if(isset($_GET['type']) && $_GET['type'] == 'subdomain') {
         $show_type = 'subdomain';
      } elseif(isset($_GET['type']) && $_GET['type'] == 'aliasdomain') {
         $show_type = 'aliasdomain';
      } elseif(!isset($_GET['type']) && isset($_SESSION['s']['var']['vhostdomain_type']) && $_SESSION['s']['var']['vhostdomain_type'] == 'subdomain') {
         $show_type = 'subdomain';
      } elseif(!isset($_GET['type']) && isset($_SESSION['s']['var']['vhostdomain_type']) && $_SESSION['s']['var']['vhostdomain_type'] == 'aliasdomain') {
         $show_type = 'aliasdomain';
      }
      $_SESSION['s']['var']['vhostdomain_type'] = $show_type;
      $this->_vhostdomain_type = $show_type;
      parent::onLoad();
   }
   function onShowNew() {
      global $app, $conf;
      // we will check only users, not admins
      if($_SESSION["s"]["user"]["typ"] == 'user') {
         if(!$app->tform->checkClientLimit('limit_web_domain', "type = 'vhost'")) {
            $app->error($app->tform->wordbook["limit_web_domain_txt"]);
         if($this->_vhostdomain_type == 'domain') {
            if(!$app->tform->checkClientLimit('limit_web_domain', "type = 'vhost'")) {
               $app->error($app->tform->wordbook["limit_web_domain_txt"]);
            }
            if(!$app->tform->checkResellerLimit('limit_web_domain', "type = 'vhost'")) {
               $app->error('Reseller: '.$app->tform->wordbook["limit_web_domain_txt"]);
            }
         } elseif($this->_vhostdomain_type == 'subdomain') {
            if(!$app->tform->checkClientLimit('limit_web_subdomain', "(type = 'subdomain' OR type = 'vhostsubdomain')")) {
               $app->error($app->tform->wordbook["limit_web_subdomain_txt"]);
            }
            if(!$app->tform->checkResellerLimit('limit_web_subdomain', "(type = 'subdomain' OR type = 'vhostsubdomain')")) {
               $app->error('Reseller: '.$app->tform->wordbook["limit_web_subdomain_txt"]);
            }
         } elseif($this->_vhostdomain_type == 'aliasdomain') {
            if(!$app->tform->checkClientLimit('limit_web_aliasdomain', "(type = 'alias' OR type = 'vhostalias')")) {
               $app->error($app->tform->wordbook["limit_web_aliasdomain_txt"]);
            }
            if(!$app->tform->checkResellerLimit('limit_web_aliasdomain', "(type = 'alias' OR type = 'vhostalias')")) {
               $app->error('Reseller: '.$app->tform->wordbook["limit_web_aliasdomain_txt"]);
            }
         }
         if(!$app->tform->checkResellerLimit('limit_web_domain', "type = 'vhost'")) {
            $app->error('Reseller: '.$app->tform->wordbook["limit_web_domain_txt"]);
         }
         // Get the limits of the client
         $client_group_id = $_SESSION["s"]["user"]["default_group"];
         $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");
@@ -84,6 +118,7 @@
      }
      $app->tform->formDef['tabs']['domain']['readonly'] = false;
      $app->tpl->setVar('vhostdomain_type', $this->_vhostdomain_type);
      parent::onShowNew();
   }
@@ -94,15 +129,22 @@
      $read_limits = array('limit_cgi', 'limit_ssi', 'limit_perl', 'limit_ruby', 'limit_python', 'force_suexec', 'limit_hterror', 'limit_wildcard', 'limit_ssl');
      if($this->_vhostdomain_type != 'domain') $parent_domain = $app->db->queryOneRecord("select * FROM web_domain WHERE domain_id = ".$app->functions->intval(@$this->dataRecord["parent_domain_id"]));
      //* Client: If the logged in user is not admin and has no sub clients (no reseller)
      if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
         // Get the limits of the client
         $client_group_id = $_SESSION["s"]["user"]["default_group"];
         $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");
         $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
         if($this->_vhostdomain_type == 'domain') {
            $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");
         } elseif($this->_vhostdomain_type == 'subdomain') {
            $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");
         } elseif($this->_vhostdomain_type == 'aliasdomain') {
            $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");
         }
         $client['web_servers_ids'] = explode(',', $client['web_servers']);
         $only_one_server = count($client['web_servers_ids']) === 1;
         $app->tpl->setVar('only_one_server', $only_one_server);
@@ -178,11 +220,21 @@
         $server_type = 'apache';
         if(!empty($web_config['server_type'])) $server_type = $web_config['server_type'];
         if($server_type == 'nginx' && $this->dataRecord['php'] == 'fast-cgi') $this->dataRecord['php'] = 'php-fpm';
         if($this->dataRecord['php'] == 'php-fpm'){
            $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fpm_init_script != '' AND php_fpm_ini_dir != '' AND php_fpm_pool_dir != '' AND server_id = ".($this->id > 0 ? $app->functions->intval($this->dataRecord['server_id']) : $app->functions->intval($client['default_webserver']))." AND (client_id = 0 OR client_id=".$app->functions->intval($_SESSION['s']['user']['client_id']).")");
         }
         if($this->dataRecord['php'] == 'fast-cgi'){
            $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = ".($this->id > 0 ? $app->functions->intval($this->dataRecord['server_id']) : $app->functions->intval($client['default_webserver']))." AND (client_id = 0 OR client_id=".$app->functions->intval($_SESSION['s']['user']['client_id']).")");
         if($this->_vhostdomain_type == 'domain') {
            if($this->dataRecord['php'] == 'php-fpm'){
               $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fpm_init_script != '' AND php_fpm_ini_dir != '' AND php_fpm_pool_dir != '' AND server_id = ".($this->id > 0 ? $app->functions->intval($this->dataRecord['server_id']) : $app->functions->intval($client['default_webserver']))." AND (client_id = 0 OR client_id=".$app->functions->intval($_SESSION['s']['user']['client_id']).")");
            }
            if($this->dataRecord['php'] == 'fast-cgi'){
               $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = ".($this->id > 0 ? $app->functions->intval($this->dataRecord['server_id']) : $app->functions->intval($client['default_webserver']))." AND (client_id = 0 OR client_id=".$app->functions->intval($_SESSION['s']['user']['client_id']).")");
            }
         } else {
            if($this->dataRecord['php'] == 'php-fpm'){
               $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 = ".$app->functions->intval($parent_domain['server_id'])." AND (client_id = 0 OR client_id=".$app->functions->intval($_SESSION['s']['user']['client_id']).")");
            }
            if($this->dataRecord['php'] == 'fast-cgi'){
               $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = ".$app->functions->intval($parent_domain['server_id'])." AND (client_id = 0 OR client_id=".$app->functions->intval($_SESSION['s']['user']['client_id']).")");
            }
         }
         $php_select = "<option value=''>Default</option>";
         if(is_array($php_records) && !empty($php_records)) {
@@ -208,7 +260,18 @@
         // Get the limits of the client
         $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
         $client = $app->db->queryOneRecord("SELECT client.client_id, client.limit_web_domain, client.default_webserver, client.contact_name, CONCAT(IF(client.company_name != '', CONCAT(client.company_name, ' :: '), ''), client.contact_name, ' (', client.username, IF(client.customer_no != '', CONCAT(', ', client.customer_no), ''), ')') as contactname, sys_group.name, client." . implode(", client.", $read_limits) . " FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
         if($this->_vhostdomain_type == 'domain') {
            $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");
            $app->tpl->setVar('only_one_server', $only_one_server);
         } elseif($this->_vhostdomain_type == 'subdomain') {
            $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");
         } elseif($this->_vhostdomain_type == 'aliasdomain') {
            $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");
         }
         $client['web_servers_ids'] = explode(',', $client['web_servers']);
         $only_one_server = count($client['web_servers_ids']) === 1;
         //* Get global web config
         foreach ($client['web_servers_ids'] as $web_server_id) {
@@ -291,11 +354,20 @@
         $selected_client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE groupid = ".$app->functions->intval($selected_client_group_id));
         //$sql_where = " AND (client_id = 0 OR client_id=".$_SESSION['s']['user']['client_id']." OR client_id = ".intval($selected_client['client_id']).")";
         $sql_where = " AND (client_id = 0 OR client_id = ".intval($selected_client['client_id']).")";
         if($this->dataRecord['php'] == 'php-fpm'){
            $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fpm_init_script != '' AND php_fpm_ini_dir != '' AND php_fpm_pool_dir != '' AND server_id = ".($this->id > 0 ? $app->functions->intval($this->dataRecord['server_id']) : $app->functions->intval($client['default_webserver'])).$sql_where);
         }
         if($this->dataRecord['php'] == 'fast-cgi') {
            $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = ".($this->id > 0 ? $app->functions->intval($this->dataRecord['server_id']) : $app->functions->intval($client['default_webserver'])).$sql_where);
         if($this->_vhostdomain_type == 'domain') {
            if($this->dataRecord['php'] == 'php-fpm'){
               $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fpm_init_script != '' AND php_fpm_ini_dir != '' AND php_fpm_pool_dir != '' AND server_id = ".($this->id > 0 ? $app->functions->intval($this->dataRecord['server_id']) : $app->functions->intval($client['default_webserver'])).$sql_where);
            }
            if($this->dataRecord['php'] == 'fast-cgi') {
               $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = ".($this->id > 0 ? $app->functions->intval($this->dataRecord['server_id']) : $app->functions->intval($client['default_webserver'])).$sql_where);
            }
         } else {
            if($this->dataRecord['php'] == 'php-fpm'){
               $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 = ".$app->functions->intval($parent_domain['server_id'])." AND (client_id = 0 OR client_id=".$app->functions->intval($_SESSION['s']['user']['client_id']).")");
            }
            if($this->dataRecord['php'] == 'fast-cgi') {
               $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = ".$app->functions->intval($parent_domain['server_id'])." AND (client_id = 0 OR client_id=".$app->functions->intval($_SESSION['s']['user']['client_id']).")");
            }
         }
         $php_select = "<option value=''>Default</option>";
         if(is_array($php_records) && !empty($php_records)) {
@@ -366,22 +438,27 @@
         //* Admin: If the logged in user is admin
      } else {
         // The user is admin, so we fill in all IP addresses of the server
         if($this->id > 0) {
            if(!isset($this->dataRecord["server_id"])){
               $tmp = $app->db->queryOneRecord("SELECT server_id FROM web_domain WHERE domain_id = ".$app->functions->intval($this->id));
               $this->dataRecord["server_id"] = $tmp["server_id"];
               unset($tmp);
         if($this->_vhostdomain_type == 'domain') {
            // The user is admin, so we fill in all IP addresses of the server
            if($this->id > 0) {
               if(!isset($this->dataRecord["server_id"])){
                  $tmp = $app->db->queryOneRecord("SELECT server_id FROM web_domain WHERE domain_id = ".$app->functions->intval($this->id));
                  $this->dataRecord["server_id"] = $tmp["server_id"];
                  unset($tmp);
               }
               $server_id = intval(@$this->dataRecord["server_id"]);
            } else {
               // Get the first server ID
               $tmp = $app->db->queryOneRecord("SELECT server_id FROM server WHERE web_server = 1 ORDER BY server_name LIMIT 0,1");
               $server_id = intval($tmp['server_id']);
            }
            $server_id = intval(@$this->dataRecord["server_id"]);
         } else {
            // Get the first server ID
            $tmp = $app->db->queryOneRecord("SELECT server_id FROM server WHERE web_server = 1 ORDER BY server_name LIMIT 0,1");
            $server_id = intval($tmp['server_id']);
         }
         //* get global web config
         $web_config = $app->getconf->get_server_config($server_id, 'web');
            //* get global web config
            $web_config = $app->getconf->get_server_config($server_id, 'web');
         } else {
            //* get global web config
            $web_config = $app->getconf->get_server_config($parent_domain['server_id'], 'web');
         }
         //* Fill the IPv4 select field
         $sql = "SELECT ip_address FROM server_ip WHERE ip_type = 'IPv4' AND server_id = ".$app->functions->intval($server_id);
@@ -437,11 +514,20 @@
         $selected_client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE groupid = ".$app->functions->intval($selected_client_group_id));
         //$sql_where = " AND (client_id = 0 OR client_id=".$_SESSION['s']['user']['client_id']." OR client_id = ".intval($selected_client['client_id']).")";
         $sql_where = " AND (client_id = 0 OR client_id = ".$app->functions->intval($selected_client['client_id']).")";
         if($this->dataRecord['php'] == 'php-fpm'){
            $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fpm_init_script != '' AND php_fpm_ini_dir != '' AND php_fpm_pool_dir != '' AND server_id = $server_id".$sql_where);
         }
         if($this->dataRecord['php'] == 'fast-cgi') {
            $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = ".$app->functions->intval($server_id).$sql_where);
         if($this->_vhostdomain_type == 'domain') {
            if($this->dataRecord['php'] == 'php-fpm'){
               $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fpm_init_script != '' AND php_fpm_ini_dir != '' AND php_fpm_pool_dir != '' AND server_id = $server_id".$sql_where);
            }
            if($this->dataRecord['php'] == 'fast-cgi') {
               $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = ".$app->functions->intval($server_id).$sql_where);
            }
         } else {
            if($this->dataRecord['php'] == 'php-fpm'){
               $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 = " . $app->functions->intval($parent_domain['server_id']));
            }
            if($this->dataRecord['php'] == 'fast-cgi') {
               $php_records = $app->db->queryAllRecords("SELECT * FROM server_php WHERE php_fastcgi_binary != '' AND php_fastcgi_ini_dir != '' AND server_id = " . $app->functions->intval($parent_domain['server_id']));
            }
         }
         $php_select = "<option value=''>Default</option>";
         if(is_array($php_records) && !empty($php_records)) {
@@ -523,9 +609,13 @@
      if($this->id > 0) {
         //* we are editing a existing record
         $app->tpl->setVar("edit_disabled", 1);
         $app->tpl->setVar("server_id_value", $this->dataRecord["server_id"]);
         $app->tpl->setVar('fixed_folder', 'y');
         if($this->_vhostdomain_type == 'domain') $app->tpl->setVar("server_id_value", $this->dataRecord["server_id"]);
         else $app->tpl->setVar('server_id_value', $parent_domain['server_id']);
      } else {
         $app->tpl->setVar("edit_disabled", 0);
         $app->tpl->setVar('fixed_folder', 'n');
         if($this->_vhostdomain_type != 'domain') $app->tpl->setVar('server_id_value', $parent_domain['server_id']);
      }
      $tmp_txt = ($this->dataRecord['traffic_quota_lock'] == 'y')?'<b>('.$app->tform->lng('traffic_quota_exceeded_txt').')</b>':'';
@@ -535,6 +625,7 @@
       * Now we have to check, if we should use the domain-module to select the domain
       * or not
       */
      $app->uses('ini_parser,getconf');
      $settings = $app->getconf->get_global_config('domains');
      if ($settings['use_domain_module'] == 'y') {
         /*
@@ -542,11 +633,17 @@
         */
         $domains = $app->tools_sites->getDomainModuleDomains();
         $domain_select = '';
         $selected_domain = '';
         if(is_array($domains) && sizeof($domains) > 0) {
            /* We have domains in the list, so create the drop-down-list */
            foreach( $domains as $domain) {
               $domain_select .= "<option value=" . $domain['domain_id'] ;
               if ($domain['domain'] == $this->dataRecord["domain"]) {
               if ($this->_vhostdomain_type == 'subdomain' && '.' . $domain['domain'] == substr($this->dataRecord["domain"], -strlen($domain['domain']) - 1)) {
                  $domain_select .= " selected";
                  $selected_domain = $domain['domain'];
               } elseif($this->_vhostdomain_type == 'aliasdomain' && $domain['domain'] == $this->dataRecord["domain"]) {
                  $domain_select .= " selected";
               } elseif($this->_vhostdomain_type == 'domain' && $domain['domain'] == $this->dataRecord["domain"]) {
                  $domain_select .= " selected";
               }
               $domain_select .= ">" . $app->functions->idn_decode($domain['domain']) . "</option>\r\n";
@@ -562,6 +659,7 @@
         }
         $app->tpl->setVar("domain_option", $domain_select);
      }
      if($this->_vhostdomain_type != 'domain') $app->tpl->setVar("domain", $this->dataRecord["domain"]);
      // check for configuration errors in sys_datalog
      if($this->id > 0) {
@@ -573,6 +671,8 @@
            }
         }
      }
      $app->tpl->setVar('vhostdomain_type', $this->_vhostdomain_type);
      parent::onShowEnd();
   }
@@ -586,38 +686,86 @@
   function onSubmit() {
      global $app, $conf;
      // Set a few fixed values
      $this->dataRecord["vhost_type"] = 'name';
      if($this->_vhostdomain_type == 'domain') {
         $this->dataRecord["parent_domain_id"] = 0;
         $this->dataRecord["type"] = 'vhost';
      } else {
         // Get the record of the parent domain
         if(!@$this->dataRecord["parent_domain_id"] && $this->id) {
            $tmp = $app->db->queryOneRecord("SELECT parent_domain_id FROM web_domain WHERE domain_id = ".$app->functions->intval($this->id));
            if($tmp) $this->dataRecord["parent_domain_id"] = $tmp['parent_domain_id'];
            unset($tmp);
         }
         $parent_domain = $app->db->queryOneRecord("select * FROM web_domain WHERE domain_id = ".$app->functions->intval(@$this->dataRecord["parent_domain_id"]) . " AND ".$app->tform->getAuthSQL('r'));
         if(!$parent_domain || $parent_domain['domain_id'] != @$this->dataRecord['parent_domain_id']) $app->tform->errorMessage .= $app->tform->lng("no_domain_perm");
         if($this->_vhostdomain_type == 'subdomain') {
            $this->dataRecord["type"] = 'vhostsubdomain';
         } else {
            $this->dataRecord["type"] = 'vhostalias';
         }
         $this->dataRecord["server_id"] = $parent_domain["server_id"];
         $this->dataRecord["ip_address"] = $parent_domain["ip_address"];
         $this->dataRecord["ipv6_address"] = $parent_domain["ipv6_address"];
         $this->dataRecord["client_group_id"] = $parent_domain["client_group_id"];
         $this->parent_domain_record = $parent_domain;
      }
      $read_limits = array('limit_cgi', 'limit_ssi', 'limit_perl', 'limit_ruby', 'limit_python', 'force_suexec', 'limit_hterror', 'limit_wildcard', 'limit_ssl');
      /* check if the domain module is used - and check if the selected domain can be used! */
      if($app->tform->getCurrentTab() == 'domain') {
         $app->uses('ini_parser,getconf');
         $settings = $app->getconf->get_global_config('domains');
         if ($settings['use_domain_module'] == 'y') {
            $domain_check = $app->tools_sites->checkDomainModuleDomain($this->dataRecord['domain']);
            if($this->_vhostdomain_type == 'subdomain') $domain_check = $app->tools_sites->checkDomainModuleDomain($this->dataRecord['sel_domain']);
            else $domain_check = $app->tools_sites->checkDomainModuleDomain($this->dataRecord['domain']);
            if(!$domain_check) {
               // invalid domain selected
               $app->tform->errorMessage .= $app->tform->lng("domain_error_empty")."<br />";
            } else {
               $this->dataRecord['domain'] = $domain_check;
               if($this->_vhostdomain_type == 'subdomain') $this->dataRecord['domain'] = $this->dataRecord['domain'] . '.' . $domain_check;
               else $this->dataRecord['domain'] = $domain_check;
            }
         } else {
            if($this->_vhostdomain_type == 'subdomain') $this->dataRecord["domain"] = $this->dataRecord["domain"].'.'.$parent_domain["domain"];
         }
         if($this->_vhostdomain_type != 'domain') {
            $this->dataRecord['web_folder'] = strtolower($this->dataRecord['web_folder']);
            if(substr($this->dataRecord['web_folder'], 0, 1) === '/') $this->dataRecord['web_folder'] = substr($this->dataRecord['web_folder'], 1);
            if(substr($this->dataRecord['web_folder'], -1) === '/') $this->dataRecord['web_folder'] = substr($this->dataRecord['web_folder'], 0, -1);
            $forbidden_folders = array('', 'cgi-bin', 'log', 'private', 'ssl', 'tmp', 'webdav');
            $check_folder = strtolower($this->dataRecord['web_folder']);
            if(substr($check_folder, 0, 1) === '/') $check_folder = substr($check_folder, 1); // strip / at beginning to check against forbidden entries
            if(strpos($check_folder, '/') !== false) $check_folder = substr($check_folder, 0, strpos($check_folder, '/')); // get the first part of the path to check it
            if(in_array($check_folder, $forbidden_folders)) {
               $app->tform->errorMessage .= $app->tform->lng("web_folder_invalid_txt")."<br>";
            }
            // vhostaliasdomains do not have a quota of their own
            $this->dataRecord["hd_quota"] = 0;
            // check for duplicate folder usage
            /*
              $check = $app->db->queryOneRecord("SELECT COUNT(*) as `cnt` FROM `web_domain` WHERE `type` = 'vhostalias' AND `parent_domain_id` = '" . $app->functions->intval($this->dataRecord['parent_domain_id']) . "' AND `web_folder` = '" . $app->db->quote($this->dataRecord['web_folder']) . "' AND `domain_id` != '" . $app->functions->intval($this->id) . "'");
              if($check && $check['cnt'] > 0) {
                  $app->tform->errorMessage .= $app->tform->lng("web_folder_unique_txt")."<br>";
              }
            */
         }
      }
      // nginx: if redirect type is proxy and redirect path is no URL, display error
      //if($this->dataRecord["redirect_type"] == 'proxy' && substr($this->dataRecord['redirect_path'],0,1) == '/'){
      // $app->tform->errorMessage .= $app->tform->lng("error_proxy_requires_url")."<br />";
      //}
      // Set a few fixed values
      $this->dataRecord["parent_domain_id"] = 0;
      $this->dataRecord["type"] = 'vhost';
      $this->dataRecord["vhost_type"] = 'name';
      $read_limits = array('limit_cgi', 'limit_ssi', 'limit_perl', 'limit_ruby', 'limit_python', 'force_suexec', 'limit_hterror', 'limit_wildcard', 'limit_ssl');
      if($_SESSION["s"]["user"]["typ"] != 'admin') {
         // Get the limits of the client
         $client_group_id = $_SESSION["s"]["user"]["default_group"];
         $client = $app->db->queryOneRecord("SELECT limit_traffic_quota, limit_web_domain, 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");
         $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
         $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");
         $client['web_servers_ids'] = explode(',', $client['web_servers']);
@@ -637,21 +785,23 @@
         } else {
            $old_web_values = array();
         }
         //* Check the website quota of the client
         if(isset($_POST["hd_quota"]) && $client["limit_web_quota"] >= 0 && $_POST["hd_quota"] != $old_web_values["hd_quota"]) {
            $tmp = $app->db->queryOneRecord("SELECT sum(hd_quota) as webquota FROM web_domain WHERE domain_id != ".$app->functions->intval($this->id)." AND type = 'vhost' AND ".$app->tform->getAuthSQL('u'));
            $webquota = $tmp["webquota"];
            $new_web_quota = $app->functions->intval($this->dataRecord["hd_quota"]);
            if(($webquota + $new_web_quota > $client["limit_web_quota"]) || ($new_web_quota < 0 && $client["limit_web_quota"] >= 0)) {
               $max_free_quota = floor($client["limit_web_quota"] - $webquota);
               if($max_free_quota < 0) $max_free_quota = 0;
               $app->tform->errorMessage .= $app->tform->lng("limit_web_quota_free_txt").": ".$max_free_quota." MB<br>";
               // Set the quota field to the max free space
               $this->dataRecord["hd_quota"] = $max_free_quota;
         if($this->_vhostdomain_type == 'domain') {
            //* Check the website quota of the client
            if(isset($_POST["hd_quota"]) && $client["limit_web_quota"] >= 0 && $_POST["hd_quota"] != $old_web_values["hd_quota"]) {
               $tmp = $app->db->queryOneRecord("SELECT sum(hd_quota) as webquota FROM web_domain WHERE domain_id != ".$app->functions->intval($this->id)." AND type = 'vhost' AND ".$app->tform->getAuthSQL('u'));
               $webquota = $tmp["webquota"];
               $new_web_quota = $app->functions->intval($this->dataRecord["hd_quota"]);
               if(($webquota + $new_web_quota > $client["limit_web_quota"]) || ($new_web_quota < 0 && $client["limit_web_quota"] >= 0)) {
                  $max_free_quota = floor($client["limit_web_quota"] - $webquota);
                  if($max_free_quota < 0) $max_free_quota = 0;
                  $app->tform->errorMessage .= $app->tform->lng("limit_web_quota_free_txt").": ".$max_free_quota." MB<br>";
                  // Set the quota field to the max free space
                  $this->dataRecord["hd_quota"] = $max_free_quota;
               }
               unset($tmp);
               unset($tmp_quota);
            }
            unset($tmp);
            unset($tmp_quota);
         }
         //* Check the traffic quota of the client
@@ -672,22 +822,24 @@
         if($client['parent_client_id'] > 0) {
            // Get the limits of the reseller
            $reseller = $app->db->queryOneRecord("SELECT limit_traffic_quota, limit_web_domain, web_servers, limit_web_quota FROM client WHERE client_id = ".$client['parent_client_id']);
            $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']);
            //* Check the website quota of the client
            if(isset($_POST["hd_quota"]) && $reseller["limit_web_quota"] >= 0 && $_POST["hd_quota"] != $old_web_values["hd_quota"]) {
               $tmp = $app->db->queryOneRecord("SELECT sum(hd_quota) as webquota FROM web_domain WHERE domain_id != ".$app->functions->intval($this->id)." AND type = 'vhost' AND ".$app->tform->getAuthSQL('u'));
               $webquota = $tmp["webquota"];
               $new_web_quota = $app->functions->intval($this->dataRecord["hd_quota"]);
               if(($webquota + $new_web_quota > $reseller["limit_web_quota"]) || ($new_web_quota < 0 && $reseller["limit_web_quota"] >= 0)) {
                  $max_free_quota = floor($reseller["limit_web_quota"] - $webquota);
                  if($max_free_quota < 0) $max_free_quota = 0;
                  $app->tform->errorMessage .= $app->tform->lng("limit_web_quota_free_txt").": ".$max_free_quota." MB<br>";
                  // Set the quota field to the max free space
                  $this->dataRecord["hd_quota"] = $max_free_quota;
            if($this->_vhostdomain_type == 'domain') {
               //* Check the website quota of the client
               if(isset($_POST["hd_quota"]) && $reseller["limit_web_quota"] >= 0 && $_POST["hd_quota"] != $old_web_values["hd_quota"]) {
                  $tmp = $app->db->queryOneRecord("SELECT sum(hd_quota) as webquota FROM web_domain WHERE domain_id != ".$app->functions->intval($this->id)." AND type = 'vhost' AND ".$app->tform->getAuthSQL('u'));
                  $webquota = $tmp["webquota"];
                  $new_web_quota = $app->functions->intval($this->dataRecord["hd_quota"]);
                  if(($webquota + $new_web_quota > $reseller["limit_web_quota"]) || ($new_web_quota < 0 && $reseller["limit_web_quota"] >= 0)) {
                     $max_free_quota = floor($reseller["limit_web_quota"] - $webquota);
                     if($max_free_quota < 0) $max_free_quota = 0;
                     $app->tform->errorMessage .= $app->tform->lng("limit_web_quota_free_txt").": ".$max_free_quota." MB<br>";
                     // Set the quota field to the max free space
                     $this->dataRecord["hd_quota"] = $max_free_quota;
                  }
                  unset($tmp);
                  unset($tmp_quota);
               }
               unset($tmp);
               unset($tmp_quota);
            }
            //* Check the traffic quota of the client
@@ -710,8 +862,9 @@
         // When the record is updated
         if($this->id > 0) {
            // restore the server ID if the user is not admin and record is edited
            $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));
            $tmp = $app->db->queryOneRecord("SELECT server_id, `web_folder`, `cgi`, `ssi`, `perl`, `ruby`, `python`, `suexec`, `errordocs`, `subdomain`, `ssl` FROM web_domain WHERE domain_id = ".$app->functions->intval($this->id));
            $this->dataRecord["server_id"] = $tmp["server_id"];
            $this->dataRecord['web_folder'] = $tmp['web_folder']; // cannot be changed!
            // set the settings to current if not provided (or cleared due to limits)
            if($this->dataRecord['cgi'] == '-') $this->dataRecord['cgi'] = $tmp['cgi'];
@@ -727,26 +880,37 @@
            unset($tmp);
            // When the record is inserted
         } else {
            //* display an error if chosen server is not allowed for this client
            if (!is_array($client['web_servers_ids']) || !in_array($this->dataRecord['server_id'], $client['web_servers_ids'])) {
               $app->error($app->tform->wordbook['server_chosen_not_ok']);
            if($this->_vhostdomain_type == 'domain') {
               //* display an error if chosen server is not allowed for this client
               if (!is_array($client['web_servers_ids']) || !in_array($this->dataRecord['server_id'], $client['web_servers_ids'])) {
                  $app->error($app->tform->wordbook['server_chosen_not_ok']);
               }
            }
            // Check if the user may add another web_domain
            if($client["limit_web_domain"] >= 0) {
            if($this->_vhostdomain_type == 'domain' && $client["limit_web_domain"] >= 0) {
               $tmp = $app->db->queryOneRecord("SELECT count(domain_id) as number FROM web_domain WHERE sys_groupid = $client_group_id and type = 'vhost'");
               if($tmp["number"] >= $client["limit_web_domain"]) {
                  $app->error($app->tform->wordbook["limit_web_domain_txt"]);
               }
            } elseif($this->_vhostdomain_type == 'aliasdomain' && $client["limit_web_aliasdomain"] >= 0) {
               $tmp = $app->db->queryOneRecord("SELECT count(domain_id) as number FROM web_domain WHERE sys_groupid = $client_group_id and (type = 'alias' OR type = 'vhostalias')");
               if($tmp["number"] >= $client["limit_web_aliasdomain"]) {
                  $app->error($app->tform->wordbook["limit_web_aliasdomain_txt"]);
               }
            } elseif($this->_vhostdomain_type == 'subdomain' && $client["limit_web_subdomain"] >= 0) {
               $tmp = $app->db->queryOneRecord("SELECT count(domain_id) as number FROM web_domain WHERE sys_groupid = $client_group_id and (type = 'subdomain' OR type = 'vhostsubdomain')");
               if($tmp["number"] >= $client["limit_web_subdomain"]) {
                  $app->error($app->tform->wordbook["limit_web_subdomain_txt"]);
               }
            }
         }
         // 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
         if(!$app->auth->has_clients($_SESSION['s']['user']['userid'])) unset($this->dataRecord["client_group_id"]);
      }
      //* make sure that the email domain is lowercase
      //* make sure that the domain is lowercase
      if(isset($this->dataRecord["domain"])) $this->dataRecord["domain"] = strtolower($this->dataRecord["domain"]);
      //* get the server config for this server
@@ -853,73 +1017,93 @@
      $app->uses("getconf");
      $web_rec = $app->tform->getDataRecord($this->id);
      $web_config = $app->getconf->get_server_config($app->functions->intval($web_rec["server_id"]), 'web');
      $document_root = str_replace("[website_id]", $this->id, $web_config["website_path"]);
      $document_root = str_replace("[website_idhash_1]", $this->id_hash($page_form->id, 1), $document_root);
      $document_root = str_replace("[website_idhash_2]", $this->id_hash($page_form->id, 1), $document_root);
      $document_root = str_replace("[website_idhash_3]", $this->id_hash($page_form->id, 1), $document_root);
      $document_root = str_replace("[website_idhash_4]", $this->id_hash($page_form->id, 1), $document_root);
      // get the ID of the client
      if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
         $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
         $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = $client_group_id");
         $client_id = $app->functions->intval($client["client_id"]);
      } else {
         //$client_id = $app->functions->intval($this->dataRecord["client_group_id"]);
         $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ".$app->functions->intval($this->dataRecord["client_group_id"]));
         $client_id = $app->functions->intval($client["client_id"]);
      if($this->_vhostdomain_type == 'domain') {
         $document_root = str_replace("[website_id]", $this->id, $web_config["website_path"]);
         $document_root = str_replace("[website_idhash_1]", $this->id_hash($page_form->id, 1), $document_root);
         $document_root = str_replace("[website_idhash_2]", $this->id_hash($page_form->id, 1), $document_root);
         $document_root = str_replace("[website_idhash_3]", $this->id_hash($page_form->id, 1), $document_root);
         $document_root = str_replace("[website_idhash_4]", $this->id_hash($page_form->id, 1), $document_root);
         // get the ID of the client
         if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
            $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
            $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = $client_group_id");
            $client_id = $app->functions->intval($client["client_id"]);
         } else {
            //$client_id = $app->functions->intval($this->dataRecord["client_group_id"]);
            $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ".$app->functions->intval($this->dataRecord["client_group_id"]));
            $client_id = $app->functions->intval($client["client_id"]);
         }
         // Set the values for document_root, system_user and system_group
         $system_user = $app->db->quote('web'.$this->id);
         $system_group = $app->db->quote('client'.$client_id);
         $document_root = str_replace("[client_id]", $client_id, $document_root);
         $document_root = str_replace("[client_idhash_1]", $this->id_hash($client_id, 1), $document_root);
         $document_root = str_replace("[client_idhash_2]", $this->id_hash($client_id, 2), $document_root);
         $document_root = str_replace("[client_idhash_3]", $this->id_hash($client_id, 3), $document_root);
         $document_root = str_replace("[client_idhash_4]", $this->id_hash($client_id, 4), $document_root);
         $document_root = $app->db->quote($document_root);
         $php_open_basedir = str_replace("[website_path]", $document_root, $web_config["php_open_basedir"]);
         $php_open_basedir = $app->db->quote(str_replace("[website_domain]", $web_rec['domain'], $php_open_basedir));
         $htaccess_allow_override = $app->db->quote($web_config["htaccess_allow_override"]);
         $added_date = date($app->lng('conf_format_dateshort'));
         $added_by = $app->db->quote($_SESSION['s']['user']['username']);
         $sql = "UPDATE web_domain SET system_user = '$system_user', system_group = '$system_group', document_root = '$document_root', allow_override = '$htaccess_allow_override', php_open_basedir = '$php_open_basedir', added_date = '$added_date', added_by = '$added_by'  WHERE domain_id = ".$this->id;
      } else  {
         // Set the values for document_root, system_user and system_group
         $system_user = $app->db->quote($this->parent_domain_record['system_user']);
         $system_group = $app->db->quote($this->parent_domain_record['system_group']);
         $document_root = $app->db->quote($this->parent_domain_record['document_root']);
         $php_open_basedir = str_replace("[website_path]/web", $document_root.'/'.$web_rec['web_folder'], $web_config["php_open_basedir"]);
         $php_open_basedir = str_replace("[website_domain]/web", $web_rec['domain'].'/'.$web_rec['web_folder'], $php_open_basedir);
         $php_open_basedir = str_replace("[website_path]", $document_root, $php_open_basedir);
         $php_open_basedir = $app->db->quote(str_replace("[website_domain]", $web_rec['domain'], $php_open_basedir));
         $htaccess_allow_override = $app->db->quote($this->parent_domain_record['allow_override']);
         $added_date = date($app->lng('conf_format_dateshort'));
         $added_by = $app->db->quote($_SESSION['s']['user']['username']);
         $sql = "UPDATE web_domain SET sys_groupid = ".$app->functions->intval($this->parent_domain_record['sys_groupid']).",system_user = '$system_user', system_group = '$system_group', document_root = '$document_root', allow_override = '$htaccess_allow_override', php_open_basedir = '$php_open_basedir', added_date = '$added_date', added_by = '$added_by' WHERE domain_id = ".$this->id;
      }
      // Set the values for document_root, system_user and system_group
      $system_user = $app->db->quote('web'.$this->id);
      $system_group = $app->db->quote('client'.$client_id);
      $document_root = str_replace("[client_id]", $client_id, $document_root);
      $document_root = str_replace("[client_idhash_1]", $this->id_hash($client_id, 1), $document_root);
      $document_root = str_replace("[client_idhash_2]", $this->id_hash($client_id, 2), $document_root);
      $document_root = str_replace("[client_idhash_3]", $this->id_hash($client_id, 3), $document_root);
      $document_root = str_replace("[client_idhash_4]", $this->id_hash($client_id, 4), $document_root);
      $document_root = $app->db->quote($document_root);
      $php_open_basedir = str_replace("[website_path]", $document_root, $web_config["php_open_basedir"]);
      $php_open_basedir = $app->db->quote(str_replace("[website_domain]", $web_rec['domain'], $php_open_basedir));
      $htaccess_allow_override = $app->db->quote($web_config["htaccess_allow_override"]);
      $added_date = date($app->lng('conf_format_dateshort'));
      $added_by = $app->db->quote($_SESSION['s']['user']['username']);
      $sql = "UPDATE web_domain SET system_user = '$system_user', system_group = '$system_group', document_root = '$document_root', allow_override = '$htaccess_allow_override', php_open_basedir = '$php_open_basedir', added_date = '$added_date', added_by = '$added_by'  WHERE domain_id = ".$this->id;
      $app->db->query($sql);
   }
   function onBeforeUpdate () {
      global $app, $conf;
      //* Check if the server has been changed
      // We do this only for the admin or reseller users, as normal clients can not change the server ID anyway
      if($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
         if (isset($this->dataRecord["server_id"])) {
            $rec = $app->db->queryOneRecord("SELECT server_id from web_domain WHERE domain_id = ".$this->id);
            if($rec['server_id'] != $this->dataRecord["server_id"]) {
      if($this->_vhostdomain_type == 'domain') {
         //* Check if the server has been changed
         // We do this only for the admin or reseller users, as normal clients can not change the server ID anyway
         if($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
            if (isset($this->dataRecord["server_id"])) {
               $rec = $app->db->queryOneRecord("SELECT server_id from web_domain WHERE domain_id = ".$this->id);
               if($rec['server_id'] != $this->dataRecord["server_id"]) {
                  //* Add a error message and switch back to old server
                  $app->tform->errorMessage .= $app->lng('The Server can not be changed.');
                  $this->dataRecord["server_id"] = $rec['server_id'];
               }
               unset($rec);
            }
            //* If the user is neither admin nor reseller
         } else {
            //* We do not allow users to change a domain which has been created by the admin
            $rec = $app->db->queryOneRecord("SELECT sys_perm_group, domain, ip_address, ipv6_address from web_domain WHERE domain_id = ".$this->id);
            if(isset($this->dataRecord["domain"]) && $rec['domain'] != $this->dataRecord["domain"] && $app->tform->checkPerm($this->id, 'u')) {
               //* Add a error message and switch back to old server
               $app->tform->errorMessage .= $app->lng('The Server can not be changed.');
               $this->dataRecord["server_id"] = $rec['server_id'];
               $app->tform->errorMessage .= $app->lng('The Domain can not be changed. Please ask your Administrator if you want to change the domain name.');
               $this->dataRecord["domain"] = $rec['domain'];
            }
            if(isset($this->dataRecord["ip_address"]) && $rec['ip_address'] != $this->dataRecord["ip_address"] && $rec['sys_perm_group'] != 'riud') {
               $this->dataRecord["ip_address"] = $rec['ip_address'];
            }
            if(isset($this->dataRecord["ipv6_address"]) && $rec['ipv6_address'] != $this->dataRecord["ipv6_address"] && $rec['sys_perm_group'] != 'riud') {
               $this->dataRecord["ipv6_address"] = $rec['ipv6_address'];
            }
            unset($rec);
         }
         //* If the user is neither admin nor reseller
      } else {
         //* We do not allow users to change a domain which has been created by the admin
         $rec = $app->db->queryOneRecord("SELECT sys_perm_group, domain, ip_address, ipv6_address from web_domain WHERE domain_id = ".$this->id);
         if(isset($this->dataRecord["domain"]) && $rec['domain'] != $this->dataRecord["domain"] && $app->tform->checkPerm($this->id, 'u')) {
            //* Add a error message and switch back to old server
            $app->tform->errorMessage .= $app->lng('The Domain can not be changed. Please ask your Administrator if you want to change the domain name.');
            $this->dataRecord["domain"] = $rec['domain'];
         }
         if(isset($this->dataRecord["ip_address"]) && $rec['ip_address'] != $this->dataRecord["ip_address"] && $rec['sys_perm_group'] != 'riud') {
            $this->dataRecord["ip_address"] = $rec['ip_address'];
         }
         if(isset($this->dataRecord["ipv6_address"]) && $rec['ipv6_address'] != $this->dataRecord["ipv6_address"] && $rec['sys_perm_group'] != 'riud') {
            $this->dataRecord["ipv6_address"] = $rec['ipv6_address'];
         }
         unset($rec);
      }
      //* Check that all fields for the SSL cert creation are filled
@@ -934,195 +1118,6 @@
      if(isset($this->dataRecord['ssl_action']) && $this->dataRecord['ssl_action'] == 'save') {
         if(trim($this->dataRecord['ssl_cert']) == '') $app->tform->errorMessage .= $app->tform->lng('error_ssl_cert_empty').'<br />';
      }
   }
   function onAfterUpdate() {
      global $app, $conf;
      // make sure that the record belongs to the client group and not the admin group when a admin inserts it
      // also make sure that the user can not delete domain created by a admin
      if($_SESSION["s"]["user"]["typ"] == 'admin' && isset($this->dataRecord["client_group_id"])) {
         $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
         $app->db->query("UPDATE web_domain SET sys_groupid = $client_group_id, sys_perm_group = 'ru' WHERE domain_id = ".$this->id);
      }
      if($app->auth->has_clients($_SESSION['s']['user']['userid']) && isset($this->dataRecord["client_group_id"])) {
         $client_group_id = $app->functions->intval($this->dataRecord["client_group_id"]);
         $app->db->query("UPDATE web_domain SET sys_groupid = $client_group_id, sys_perm_group = 'riud' WHERE domain_id = ".$this->id);
      }
      // Get configuration for the web system
      $app->uses("getconf");
      $web_rec = $app->tform->getDataRecord($this->id);
      $web_config = $app->getconf->get_server_config($app->functions->intval($web_rec["server_id"]), 'web');
      $document_root = str_replace("[website_id]", $this->id, $web_config["website_path"]);
      $page_formid = isset($page_form->id) ? $page_form->id : '';
      $document_root = str_replace("[website_idhash_1]", $this->id_hash($page_formid, 1), $document_root);
      $document_root = str_replace("[website_idhash_2]", $this->id_hash($page_formid, 1), $document_root);
      $document_root = str_replace("[website_idhash_3]", $this->id_hash($page_formid, 1), $document_root);
      $document_root = str_replace("[website_idhash_4]", $this->id_hash($page_formid, 1), $document_root);
      // get the ID of the client
      if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
         $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
         $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = $client_group_id");
         $client_id = $app->functions->intval($client["client_id"]);
      } elseif (isset($this->dataRecord["client_group_id"])) {
         $client_group_id = $this->dataRecord["client_group_id"];
         $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ".$app->functions->intval(@$this->dataRecord["client_group_id"]));
         $client_id = $app->functions->intval($client["client_id"]);
      } else {
         $client_group_id = $web_rec['sys_groupid'];
         $client = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE sys_group.groupid = ".$app->functions->intval($client_group_id));
         $client_id = $app->functions->intval($client["client_id"]);
      }
      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"]) {
         // Set the values for document_root, system_user and system_group
         $system_user = $app->db->quote('web'.$this->id);
         $system_group = $app->db->quote('client'.$client_id);
         $document_root = str_replace("[client_id]", $client_id, $document_root);
         $document_root = str_replace("[client_idhash_1]", $this->id_hash($client_id, 1), $document_root);
         $document_root = str_replace("[client_idhash_2]", $this->id_hash($client_id, 2), $document_root);
         $document_root = str_replace("[client_idhash_3]", $this->id_hash($client_id, 3), $document_root);
         $document_root = str_replace("[client_idhash_4]", $this->id_hash($client_id, 4), $document_root);
         $document_root = $app->db->quote($document_root);
         $sql = "UPDATE web_domain SET system_user = '$system_user', system_group = '$system_group', document_root = '$document_root' WHERE domain_id = ".$this->id;
         //$sql = "UPDATE web_domain SET system_user = '$system_user', system_group = '$system_group' WHERE domain_id = ".$this->id;
         $app->db->query($sql);
         // Update the FTP user(s) too
         $records = $app->db->queryAllRecords("SELECT ftp_user_id FROM ftp_user WHERE parent_domain_id = ".$this->id);
         foreach($records as $rec) {
            $app->db->datalogUpdate('ftp_user', "sys_userid = '".$app->functions->intval($web_rec['sys_userid'])."', sys_groupid = '".$app->functions->intval($web_rec['sys_groupid'])."', uid = '$system_user', gid = '$system_group', dir = '$document_root'", 'ftp_user_id', $app->functions->intval($rec['ftp_user_id']));
         }
         unset($records);
         unset($rec);
         // Update the Shell user(s) too
         $records = $app->db->queryAllRecords("SELECT shell_user_id FROM shell_user WHERE parent_domain_id = ".$this->id);
         foreach($records as $rec) {
            $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', $app->functions->intval($rec['shell_user_id']));
         }
         unset($records);
         unset($rec);
         //* Update all subdomains and alias domains
         $records = $app->db->queryAllRecords("SELECT domain_id, `domain`, `type`, `web_folder` FROM web_domain WHERE parent_domain_id = ".$this->id);
         foreach($records as $rec) {
            $update_columns = "sys_userid = '".$web_rec['sys_userid']."', sys_groupid = '".$web_rec['sys_groupid']."'";
            if($rec['type'] == 'vhostsubdomain' || $rec['type'] == 'vhostalias') {
               $php_open_basedir = str_replace("[website_path]/web", $document_root.'/'.$rec['web_folder'], $web_config["php_open_basedir"]);
               $php_open_basedir = str_replace("[website_domain]/web", $rec['domain'].'/'.$rec['web_folder'], $php_open_basedir);
               $php_open_basedir = str_replace("[website_path]", $document_root, $php_open_basedir);
               $php_open_basedir = $app->db->quote(str_replace("[website_domain]", $rec['domain'], $php_open_basedir));
               $update_columns .= ", document_root = '".$document_root."', `php_open_basedir` = '".$php_open_basedir."'";
            }
            $app->db->datalogUpdate('web_domain', $update_columns, 'domain_id', $rec['domain_id']);
         }
         unset($records);
         unset($rec);
         //* Update all databases
         $records = $app->db->queryAllRecords("SELECT database_id FROM web_database WHERE parent_domain_id = ".$this->id);
         foreach($records as $rec) {
            $app->db->datalogUpdate('web_database', "sys_userid = '".$app->functions->intval($web_rec['sys_userid'])."', sys_groupid = '".$app->functions->intval($web_rec['sys_groupid'])."'", 'database_id', $app->functions->intval($rec['database_id']));
         }
         unset($records);
         unset($rec);
      }
      //* If the domain name has been changed, we will have to change all subdomains + APS instances
      if(!empty($this->dataRecord["domain"]) && !empty($this->oldDataRecord["domain"]) && $this->dataRecord["domain"] != $this->oldDataRecord["domain"]) {
         $records = $app->db->queryAllRecords("SELECT domain_id,domain FROM web_domain WHERE (type = 'subdomain' OR type = 'vhostsubdomain' OR type = 'vhostalias') AND domain LIKE '%.".$app->db->quote($this->oldDataRecord["domain"])."'");
         foreach($records as $rec) {
            $subdomain = $app->db->quote(str_replace($this->oldDataRecord["domain"], $this->dataRecord["domain"], $rec['domain']));
            $app->db->datalogUpdate('web_domain', "domain = '".$subdomain."'", 'domain_id', $rec['domain_id']);
         }
         unset($records);
         unset($rec);
         unset($subdomain);
         // Update APS instances
         $records = $app->db->queryAllRecords("SELECT id, instance_id FROM aps_instances_settings WHERE name = 'main_domain' AND value = '".$app->db->quote($this->oldDataRecord["domain"])."'");
         if(is_array($records) && !empty($records)){
            foreach($records as $rec){
               $app->db->datalogUpdate('aps_instances_settings', "value = '".$app->db->quote($this->dataRecord["domain"])."'", 'id', $rec['id']);
               // Reinstall of package needed?
               //$app->db->datalogUpdate('aps_instances', "instance_status = '1'", 'id', $rec['instance_id']);
            }
         }
         unset($records);
         unset($rec);
      }
      //* Set allow_override if empty
      if($web_rec['allow_override'] == '') {
         $sql = "UPDATE web_domain SET allow_override = '".$app->db->quote($web_config["htaccess_allow_override"])."' WHERE domain_id = ".$this->id;
         $app->db->query($sql);
      }
      //* Set php_open_basedir if empty or domain or client has been changed
      if(empty($web_rec['php_open_basedir']) ||
         (!empty($this->dataRecord["domain"]) && !empty($this->oldDataRecord["domain"]) && $this->dataRecord["domain"] != $this->oldDataRecord["domain"])) {
         $php_open_basedir = $web_rec['php_open_basedir'];
         $php_open_basedir = $app->db->quote(str_replace($this->oldDataRecord['domain'], $web_rec['domain'], $php_open_basedir));
         $sql = "UPDATE web_domain SET php_open_basedir = '$php_open_basedir' WHERE domain_id = ".$this->id;
         $app->db->query($sql);
      }
      if(empty($web_rec['php_open_basedir']) ||
         (isset($this->dataRecord["client_group_id"]) && $this->dataRecord["client_group_id"] != $this->oldDataRecord["sys_groupid"])) {
         $document_root = $app->db->quote(str_replace("[client_id]", $client_id, $document_root));
         $php_open_basedir = str_replace("[website_path]", $document_root, $web_config["php_open_basedir"]);
         $php_open_basedir = $app->db->quote(str_replace("[website_domain]", $web_rec['domain'], $php_open_basedir));
         $sql = "UPDATE web_domain SET php_open_basedir = '$php_open_basedir' WHERE domain_id = ".$this->id;
         $app->db->query($sql);
      }
      //* Change database backup options when web backup options have been changed
      if(isset($this->dataRecord['backup_interval']) && ($this->dataRecord['backup_interval'] != $this->oldDataRecord['backup_interval'] || $this->dataRecord['backup_copies'] != $this->oldDataRecord['backup_copies'])) {
         //* Update all databases
         $backup_interval = $app->functions->intval($this->dataRecord['backup_interval']);
         $backup_copies = $app->functions->intval($this->dataRecord['backup_copies']);
         $records = $app->db->queryAllRecords("SELECT database_id FROM web_database WHERE parent_domain_id = ".$this->id);
         foreach($records as $rec) {
            $app->db->datalogUpdate('web_database', "backup_interval = '$backup_interval', backup_copies = '$backup_copies'", 'database_id', $rec['database_id']);
         }
         unset($records);
         unset($rec);
         unset($backup_copies);
         unset($backup_interval);
      }
      //* Change vhost subdomain and alias ip/ipv6 if domain ip/ipv6 has changed
      if(isset($this->dataRecord['ip_address']) && ($this->dataRecord['ip_address'] != $this->oldDataRecord['ip_address'] || $this->dataRecord['ipv6_address'] != $this->oldDataRecord['ipv6_address'])) {
         $records = $app->db->queryAllRecords("SELECT domain_id FROM web_domain WHERE (type = 'vhostsubdomain' OR type = 'vhostalias') AND parent_domain_id = ".$this->id);
         foreach($records as $rec) {
            $app->db->datalogUpdate('web_domain', "ip_address = '".$app->db->quote($web_rec['ip_address'])."', ipv6_address = '".$app->db->quote($web_rec['ipv6_address'])."'", 'domain_id', $rec['domain_id']);
         }
         unset($records);
         unset($rec);
      }
   }
   function onAfterDelete() {
      global $app, $conf;
      // Delete the sub and alias domains
      $child_domains = $app->db->queryAllRecords("SELECT * FROM web_domain WHERE parent_domain_id = ".$this->id);
      foreach($child_domains as $d) {
         // Saving record to datalog when db_history enabled
         if($app->tform->formDef["db_history"] == 'yes') {
            $app->tform->datalogSave('DELETE', $d["domain_id"], $d, array());
         }
         $app->db->query("DELETE FROM web_domain WHERE domain_id = ".$app->functions->intval($d["domain_id"])." LIMIT 0,1");
      }
      unset($child_domains);
      unset($d);
   }