1+ <?php
2+
3+ /*
4+ Copyright (c) 2007, Till Brehm, projektfarm Gmbh
5+ Copyright (c) 2012, Marius Cramer, pixcept KG
6+ All rights reserved.
7+
8+ Redistribution and use in source and binary forms, with or without modification,
9+ are permitted provided that the following conditions are met:
10+
11+ * Redistributions of source code must retain the above copyright notice,
12+ this list of conditions and the following disclaimer.
13+ * Redistributions in binary form must reproduce the above copyright notice,
14+ this list of conditions and the following disclaimer in the documentation
15+ and/or other materials provided with the distribution.
16+ * Neither the name of ISPConfig nor the names of its contributors
17+ may be used to endorse or promote products derived from this software without
18+ specific prior written permission.
19+
20+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23+ IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27+ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+ */
31+
32+ class validate_domain {
33+
34+ function get_error ($ errmsg ) {
35+ global $ app ;
36+
37+ if (isset ($ app ->tform ->wordbook [$ errmsg ])) {
38+ return $ app ->tform ->wordbook [$ errmsg ]."<br> \r\n" ;
39+ } else {
40+ return $ errmsg ."<br> \r\n" ;
41+ }
42+ }
43+
44+ /* Validator function for domain (website) */
45+ function web_domain ($ field_name , $ field_value , $ validator ) {
46+ if (empty ($ field_value )) return $ this ->get_error ('domain_error_empty ' );
47+
48+ // do not allow wildcards on website domains
49+ $ result = $ this ->_regex_validate ($ field_value );
50+ if (!$ result ) return $ this ->get_error ('domain_error_regex ' );
51+
52+ $ result = $ this ->_check_unique ($ field_value );
53+ if (!$ result ) return $ this ->get_error ('domain_error_unique ' );
54+ }
55+
56+ /* Validator function for sub domain */
57+ function sub_domain ($ field_name , $ field_value , $ validator ) {
58+ if (empty ($ field_value )) return $ this ->get_error ('domain_error_empty ' );
59+
60+ $ allow_wildcard = $ this ->_wildcard_limit ();
61+ if ($ allow_wildcard == false && substr ($ field_value , 0 , 2 ) === '*. ' ) return $ this ->get_error ('domain_error_wildcard ' );
62+
63+ $ result = $ this ->_regex_validate ($ field_value , $ allow_wildcard );
64+ if (!$ result ) return $ this ->get_error ('domain_error_regex ' );
65+
66+ $ result = $ this ->_check_unique ($ field_value );
67+ if (!$ result ) return $ this ->get_error ('domain_error_unique ' );
68+ }
69+
70+ /* Validator function for alias domain */
71+ function alias_domain ($ field_name , $ field_value , $ validator ) {
72+ if (empty ($ field_value )) return $ this ->get_error ('domain_error_empty ' );
73+
74+ // do not allow wildcards on alias domains
75+ $ result = $ this ->_regex_validate ($ field_value );
76+ if (!$ result ) return $ this ->get_error ('domain_error_regex ' );
77+
78+ $ result = $ this ->_check_unique ($ field_value );
79+ if (!$ result ) return $ this ->get_error ('domain_error_unique ' );
80+ }
81+
82+ /* internal validator function to match regexp */
83+ function _regex_validate ($ domain_name , $ allow_wildcard = false ) {
84+ $ pattern = '/^ ' . ($ allow_wildcard == true ? '(\*\.)? ' : '' ) . '[\w\.\-]{2,255}\.[a-zA-Z0-9\-]{2,30}$/ ' ;
85+ return preg_match ($ pattern , $ domain_name );
86+ }
87+
88+ /* check if the domain hostname is unique (keep in mind the auto subdomains!) */
89+ function _check_unique ($ domain_name ) {
90+ global $ app ;
91+
92+ $ 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 ));
93+ if ($ check ['cnt ' ] > 0 ) return false ;
94+
95+ $ 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 ));
96+ if ($ check ['cnt ' ] > 0 ) return false ;
97+
98+ return true ;
99+ }
100+
101+ /* check if the client may add wildcard domains */
102+ function _wildcard_limit () {
103+ global $ app ;
104+
105+ if ($ _SESSION ["s " ]["user " ]["typ " ] != 'admin ' ) {
106+ // Get the limits of the client
107+ $ client_group_id = $ _SESSION ["s " ]["user " ]["default_group " ];
108+ $ 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 " );
109+
110+ if ($ client ["limit_wildcard " ] == 'y ' ) return true ;
111+ else return false ;
112+ }
113+ return true ; // admin may always add wildcard domain
114+ }
115+ }
0 commit comments