From 8ec1d88214d82a625af71a50155895d3b202deaf Mon Sep 17 00:00:00 2001
From: mcramer <m.cramer@pixcept.de>
Date: Thu, 13 Sep 2012 04:13:56 -0400
Subject: [PATCH] Fixed: FS#2426 - Auto subdomains are ignored when checking if domain is unique  Implemented: FS#2427 - Allow wildcard subdomain creation on limit_wildcard = y 

---
 interface/web/sites/lib/lang/de_web_subdomain.lng       |    1 
 interface/web/sites/form/web_vhost_subdomain.tform.php  |   13 +--
 interface/web/sites/form/web_aliasdomain.tform.php      |   13 +--
 interface/web/sites/lib/lang/en_web_vhost_subdomain.lng |    1 
 interface/web/sites/form/web_subdomain.tform.php        |   13 +--
 interface/web/sites/lib/lang/de_web_vhost_subdomain.lng |    1 
 interface/web/sites/lib/lang/en_web_subdomain.lng       |    1 
 interface/lib/classes/validate_domain.inc.php           |  115 ++++++++++++++++++++++++++++++++++++++
 interface/web/sites/form/web_domain.tform.php           |   13 +--
 9 files changed, 139 insertions(+), 32 deletions(-)

diff --git a/interface/lib/classes/validate_domain.inc.php b/interface/lib/classes/validate_domain.inc.php
new file mode 100644
index 0000000..25f16af
--- /dev/null
+++ b/interface/lib/classes/validate_domain.inc.php
@@ -0,0 +1,115 @@
+<?php
+
+/*
+Copyright (c) 2007, Till Brehm, projektfarm Gmbh
+Copyright (c) 2012, Marius Cramer, pixcept KG
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright notice,
+      this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright notice,
+      this list of conditions and the following disclaimer in the documentation
+      and/or other materials provided with the distribution.
+    * Neither the name of ISPConfig nor the names of its contributors
+      may be used to endorse or promote products derived from this software without
+      specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+class validate_domain {
+	
+    function get_error($errmsg) {
+        global $app;
+        
+        if(isset($app->tform->wordbook[$errmsg])) {
+            return $app->tform->wordbook[$errmsg]."<br>\r\n";
+        } else {
+            return $errmsg."<br>\r\n";
+        }
+    }
+    
+    /* Validator function for domain (website) */
+    function web_domain($field_name, $field_value, $validator) {
+        if(empty($field_value)) return $this->get_error('domain_error_empty');
+        
+        // do not allow wildcards on website domains
+        $result = $this->_regex_validate($field_value);
+        if(!$result) return $this->get_error('domain_error_regex');
+        
+        $result = $this->_check_unique($field_value);
+        if(!$result) return $this->get_error('domain_error_unique');
+    }
+    
+    /* Validator function for sub domain */
+    function sub_domain($field_name, $field_value, $validator) {
+        if(empty($field_value)) return $this->get_error('domain_error_empty');
+        
+        $allow_wildcard = $this->_wildcard_limit();
+        if($allow_wildcard == false && substr($field_value, 0, 2) === '*.') return $this->get_error('domain_error_wildcard');
+        
+        $result = $this->_regex_validate($field_value, $allow_wildcard);
+        if(!$result) return $this->get_error('domain_error_regex');
+        
+        $result = $this->_check_unique($field_value);
+        if(!$result) return $this->get_error('domain_error_unique');
+    }
+    
+    /* Validator function for alias domain */
+    function alias_domain($field_name, $field_value, $validator) {
+        if(empty($field_value)) return $this->get_error('domain_error_empty');
+        
+        // do not allow wildcards on alias domains
+        $result = $this->_regex_validate($field_value);
+        if(!$result) return $this->get_error('domain_error_regex');
+        
+        $result = $this->_check_unique($field_value);
+        if(!$result) return $this->get_error('domain_error_unique');
+    }
+    
+    /* internal validator function to match regexp */
+    function _regex_validate($domain_name, $allow_wildcard = false) {
+        $pattern = '/^' . ($allow_wildcard == true ? '(\*\.)?' : '') . '[\w\.\-]{2,255}\.[a-zA-Z0-9\-]{2,30}$/';
+        return preg_match($pattern, $domain_name);
+    }
+    
+    /* check if the domain hostname is unique (keep in mind the auto subdomains!) */
+    function _check_unique($domain_name) {
+        global $app;
+        
+        $check = $app->db->queryOneRecord("SELECT COUNT(*) as `cnt` FROM `web_domain` WHERE `domain` = '" . $app->db->quote($domain_name) . "' AND `domain_id` != " . intval($app->tform->primary_id));
+        if($check['cnt'] > 0) return false;
+        
+        $check = $app->db->queryOneRecord("SELECT COUNT(*) as `cnt` FROM `web_domain` WHERE CONCAT(`subdomain`, '.', `domain`) = '" . $app->db->quote($domain_name) . "' AND `domain_id` != " . intval($app->tform->primary_id));
+        if($check['cnt'] > 0) return false;
+        
+        return true;
+    }
+    
+    /* check if the client may add wildcard domains */
+    function _wildcard_limit() {
+        global $app;
+        
+        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_wildcard FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
+        
+            if($client["limit_wildcard"] == 'y') return true;
+            else return false;
+        }
+        return true; // admin may always add wildcard domain
+    }
+}
\ No newline at end of file
diff --git a/interface/web/sites/form/web_aliasdomain.tform.php b/interface/web/sites/form/web_aliasdomain.tform.php
index 9507aa0..69cf831 100644
--- a/interface/web/sites/form/web_aliasdomain.tform.php
+++ b/interface/web/sites/form/web_aliasdomain.tform.php
@@ -72,14 +72,11 @@
 		'domain' => array (
 			'datatype'	=> 'VARCHAR',
 			'formtype'	=> 'TEXT',
-			'validators'	=> array ( 	0 => array (	'type'	=> 'NOTEMPTY',
-														'errmsg'=> 'domain_error_empty'),
-										1 => array (	'type'	=> 'UNIQUE',
-														'errmsg'=> 'domain_error_unique'),
-										2 => array (	'type'	=> 'REGEX',
-														'regex' => '/^[\w\.\-]{2,255}\.[a-zA-Z0-9\-]{2,30}$/',
-														'errmsg'=> 'domain_error_regex'),
-									),
+            'validators'    => array (  0 => array (    'type'  => 'CUSTOM',
+                                                        'class' => 'validate_domain',
+                                                        'function' => 'alias_domain',
+                                                        'errmsg'=> 'domain_error_regex'),
+                                    ),
 			'default'	=> '',
 			'value'		=> '',
 			'width'		=> '30',
diff --git a/interface/web/sites/form/web_domain.tform.php b/interface/web/sites/form/web_domain.tform.php
index 467376c..1deec48 100644
--- a/interface/web/sites/form/web_domain.tform.php
+++ b/interface/web/sites/form/web_domain.tform.php
@@ -119,14 +119,11 @@
 		'domain' => array (
 			'datatype'	=> 'VARCHAR',
 			'formtype'	=> 'TEXT',
-			'validators'	=> array ( 	0 => array (	'type'	=> 'NOTEMPTY',
-														'errmsg'=> 'domain_error_empty'),
-										1 => array (	'type'	=> 'UNIQUE',
-														'errmsg'=> 'domain_error_unique'),
-										2 => array (	'type'	=> 'REGEX',
-														'regex' => '/^[\w\.\-]{2,255}\.[a-zA-Z0-9\-]{2,30}$/',
-														'errmsg'=> 'domain_error_regex'),
-									),
+            'validators'    => array (  0 => array (    'type'  => 'CUSTOM',
+                                                        'class' => 'validate_domain',
+                                                        'function' => 'web_domain',
+                                                        'errmsg'=> 'domain_error_regex'),
+                                    ),
 			'filters'	=> array ( 	0 => array (	'event'	=> 'SAVE',
 													'type'	=> 'TOLOWER'),
 									),
diff --git a/interface/web/sites/form/web_subdomain.tform.php b/interface/web/sites/form/web_subdomain.tform.php
index 9b2744b..d8d6c2c 100644
--- a/interface/web/sites/form/web_subdomain.tform.php
+++ b/interface/web/sites/form/web_subdomain.tform.php
@@ -72,14 +72,11 @@
 		'domain' => array (
 			'datatype'	=> 'VARCHAR',
 			'formtype'	=> 'TEXT',
-			'validators'	=> array ( 	0 => array (	'type'	=> 'NOTEMPTY',
-														'errmsg'=> 'domain_error_empty'),
-										1 => array (	'type'	=> 'UNIQUE',
-														'errmsg'=> 'domain_error_unique'),
-										2 => array (	'type'	=> 'REGEX',
-														'regex' => '/^[\w\.\-]{2,255}\.[a-zA-Z0-9\-]{2,30}$/',
-														'errmsg'=> 'domain_error_regex'),
-									),
+            'validators'    => array (  0 => array (    'type'  => 'CUSTOM',
+                                                        'class' => 'validate_domain',
+                                                        'function' => 'sub_domain',
+                                                        'errmsg'=> 'domain_error_regex'),
+                                    ),
 			'default'	=> '',
 			'value'		=> '',
 			'width'		=> '30',
diff --git a/interface/web/sites/form/web_vhost_subdomain.tform.php b/interface/web/sites/form/web_vhost_subdomain.tform.php
index 1b25767..3440eba 100644
--- a/interface/web/sites/form/web_vhost_subdomain.tform.php
+++ b/interface/web/sites/form/web_vhost_subdomain.tform.php
@@ -111,14 +111,11 @@
 		'domain' => array (
 			'datatype'	=> 'VARCHAR',
 			'formtype'	=> 'TEXT',
-			'validators'	=> array ( 	0 => array (	'type'	=> 'NOTEMPTY',
-														'errmsg'=> 'domain_error_empty'),
-										1 => array (	'type'	=> 'UNIQUE',
-														'errmsg'=> 'domain_error_unique'),
-										2 => array (	'type'	=> 'REGEX',
-														'regex' => '/^[\w\.\-]{2,255}\.[a-zA-Z0-9\-]{2,30}$/',
-														'errmsg'=> 'domain_error_regex'),
-									),
+            'validators'    => array (  0 => array (    'type'  => 'CUSTOM',
+                                                        'class' => 'validate_domain',
+                                                        'function' => 'sub_domain',
+                                                        'errmsg'=> 'domain_error_regex'),
+                                    ),
 			'default'	=> '',
 			'value'		=> '',
 			'width'		=> '30',
diff --git a/interface/web/sites/lib/lang/de_web_subdomain.lng b/interface/web/sites/lib/lang/de_web_subdomain.lng
index 9621a1f..20565fd 100644
--- a/interface/web/sites/lib/lang/de_web_subdomain.lng
+++ b/interface/web/sites/lib/lang/de_web_subdomain.lng
@@ -35,6 +35,7 @@
 $wb['domain_error_empty'] = 'Domain ist leer.';
 $wb['domain_error_unique'] = 'Domain muss eindeutig sein.';
 $wb['domain_error_regex'] = 'Domainname ist ungültig.';
+$wb['domain_error_wildcard'] = 'Wildcard Subdomains sind nicht erlaubt.';
 $wb['host_txt'] = 'Host';
 $wb['redirect_error_regex'] = 'Ungültiger redirect Pfad. Gültige Pfade sind beispielsweise: /test/ oder http://www.domain.tld/test/';
 $wb['no_redirect_txt'] = 'Kein Redirect';
diff --git a/interface/web/sites/lib/lang/de_web_vhost_subdomain.lng b/interface/web/sites/lib/lang/de_web_vhost_subdomain.lng
index d4d9660..0b3315a 100644
--- a/interface/web/sites/lib/lang/de_web_vhost_subdomain.lng
+++ b/interface/web/sites/lib/lang/de_web_vhost_subdomain.lng
@@ -42,6 +42,7 @@
 $wb['domain_error_empty'] = 'Domain ist leer.';
 $wb['domain_error_unique'] = 'Domain muss eindeutig sein';
 $wb['domain_error_regex'] = 'Domainname ungültig.';
+$wb['domain_error_wildcard'] = 'Wildcard Subdomains sind nicht erlaubt.';
 $wb['hd_quota_error_empty'] = 'Harddisk Quota ist leer.';
 $wb['traffic_quota_error_empty'] = 'Traffic Quota ist leer.';
 $wb['errordocs_txt'] = 'Eigene Fehlerseiten';
diff --git a/interface/web/sites/lib/lang/en_web_subdomain.lng b/interface/web/sites/lib/lang/en_web_subdomain.lng
index a26e03e..a6c3103 100644
--- a/interface/web/sites/lib/lang/en_web_subdomain.lng
+++ b/interface/web/sites/lib/lang/en_web_subdomain.lng
@@ -35,6 +35,7 @@
 $wb["domain_error_empty"] = 'Domain is empty.';
 $wb["domain_error_unique"] = 'There is already a website or sub / aliasdomain with this domain name.';
 $wb["domain_error_regex"] = 'Domain name invalid.';
+$wb['domain_error_wildcard'] = 'Wildcard subdomains are not allowed.';
 $wb["host_txt"] = 'Host';
 $wb["redirect_error_regex"] = 'Invalid redirect path. Valid redirects are for example: /test/ or http://www.domain.tld/test/';
 $wb['no_redirect_txt'] = 'No redirect';
diff --git a/interface/web/sites/lib/lang/en_web_vhost_subdomain.lng b/interface/web/sites/lib/lang/en_web_vhost_subdomain.lng
index 42c6e4f..d993af8 100644
--- a/interface/web/sites/lib/lang/en_web_vhost_subdomain.lng
+++ b/interface/web/sites/lib/lang/en_web_vhost_subdomain.lng
@@ -48,6 +48,7 @@
 $wb["domain_error_empty"] = 'Domain is empty.';
 $wb["domain_error_unique"] = 'There is already a website or sub / aliasdomain with this domain name.';
 $wb["domain_error_regex"] = 'Domain name invalid.';
+$wb['domain_error_wildcard'] = 'Wildcard subdomains are not allowed.';
 $wb["hd_quota_error_empty"] = 'Harddisk quota is 0 or empty.';
 $wb["traffic_quota_error_empty"] = 'Traffic quota is empty.';
 $wb["error_ssl_state_empty"] = 'SSL State is empty.';

--
Gitblit v1.9.1