summaryrefslogtreecommitdiffstats
path: root/ldap-site-mngmt/webinterface/lib/syntax_check.php
blob: 35924e3cf54d34a4d7471889eebb8b737cddc763 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
/*
	Syntaxcheck 1.2  2006/08/08

	A class for checking syntax of forms data
	Copyright (c) Tarik Gasmi, All Rights Reserved
*/


class Syntaxcheck
{

	var $ERROR	=	"";
	var $CLEAR	=	false;

	function Syntaxcheck ()
	{
		return;
	}

	function clear_error ()
	{
		$this->ERROR = "";
	}
	




# Ist "dotted quad IPAddress" in gueltigem Bereich? true or false
# Ueberprueft Format, fuehrende Nullen, und Werte > 255
# 
# Ueberprueft nicht nach reservierten oder nicht-route-baren IPs.
# 
function check_ip_syntax($IP)
{
	if($this->CLEAR) { $this->clear_error();}
	
	$len = strlen($IP);
	if( $len > 15 ){
		$this->ERROR = "check_ip_syntax: too long [$IP][$len]";
		return false;
		}
		
	$badcharacter = eregi_replace("([0-9\.]+)","",$IP);
	if(!empty($badcharacter)){
		$this->ERROR = "check_ip_syntax: Bad data in IP address [$badcharacter]";
		return false;
	}
	
	$chunks = explode(".",$IP);
	$count = count($chunks);
	if ($count != 4){
		$this->ERROR = "check_ip_syntax: not a dotted quad [$IP]";
		return false;
	}
	
	while ( list ($key,$val) = each ($chunks) ){
		if(ereg("^0",$val)){
			$this->ERROR = "check_ip_syntax: Invalid IP segment [$val]";
			return false;
		}
		$Num = $val;
		settype($Num,"integer");
		if($Num > 255){
			$this->ERROR = "check_ip_syntax: Segment out of range [$Num]";
			return false;
		}
	}
	
	return true;
	
}	

# Netzwerkaddresse
function check_netip_syntax($IP)
{
	if($this->CLEAR) { $this->clear_error();}
	
	if ( !($this->check_ip_syntax($IP)) ){
	   return false;
	}
	$chunks = explode(".",$IP);
	if ( $chunks[3] != "0" ){
	   return false;
	}
	return true;
}	

# MAC Adresse

# Domainname

# Hostname
function is_hostname ($hostname = ""){

	if($this->CLEAR) { $this->clear_error(); }

	$web = false;

	if(empty($hostname))
	{
		$this->ERROR = "is_hostname: No hostname submitted";
		return false;
	}

	// Only a-z, 0-9, and "-" or "." are permitted in a hostname

	// Patch for POSIX regex lib by Sascha Schumann sas@schell.de
	$Bad = eregi_replace("[-A-Z0-9\.]","",$hostname);

	if(!empty($Bad))
	{
		$this->ERROR = "is_hostname: invalid chars [$Bad]";
		return false;
	}

	// See if we're doing www.hostname.tld or hostname.tld
	if(eregi("^www\.",$hostname))
	{
		$web = true;
	}

	// double "." is a not permitted
	if(ereg("\.\.",$hostname))
	{
		$this->ERROR = "is_hostname: Double dot in [$hostname]";
		return false;
	}
	if(ereg("^\.",$hostname))
	{
		$this->ERROR = "is_hostname: leading dot in [$hostname]";
		return false;
	}

	$chunks = explode(".",$hostname);

	if( (gettype($chunks)) != "array")
	{
		$this->ERROR = "is_hostname: Invalid hostname, no dot seperator [$hostname]";
		return false;
	}

	$count = ( (count($chunks)) - 1);

	if($count < 1)
	{
		$this->ERROR = "is_hostname: Invalid hostname [$count] [$hostname]\n";
		return false;
	}

	// Bug that can't be killed without doing an is_host,
	// something.something will return TRUE, even if it's something
	// stupid like NS.SOMETHING (with no tld), because SOMETHING is
	// construed to BE the tld.  The is_bigfour and is_country
	// checks should help eliminate this inconsistancy. To really
	// be sure you've got a valid hostname, do an is_host() on it.

	if( ($web) and ($count < 2) )
	{
		$this->ERROR = "is_hostname: Invalid hostname [$count] [$hostname]\n";
		return false;
	}

	$tld = $chunks[$count];

	if(empty($tld))
	{
		$this->ERROR = "is_hostname: No TLD found in [$hostname]";
		return false;
	}

	if(!$this->is_bigfour($tld))
	{
		if(!$this->is_country($tld))
		{
			$this->ERROR = "is_hostname: Unrecognized TLD [$tld]";
			return false;
		}
	}
	

	return true;
}


# Syntax Check für die Eingaben: Uhrzeit, Wochentag, Monatstag, Monatstag.Monat
function check_timerange_syntax($mcday,$mcbeg,$mcend){
	
	if($this->CLEAR) { $this->clear_error();}

	$badcharacter = eregi_replace("([a-z0-9\.]+)","",$mcday);
	if(!empty($badcharacter)){
		$this->ERROR = "check_ip_syntax: Bad data in MC Day [$badcharacter]";
		return false;
	}
	$badcharacter = eregi_replace("([x0-9]+)","",$mcbeg);
	if(!empty($badcharacter)){
		$this->ERROR = "check_ip_syntax: Bad data in MC Begin [$badcharacter]";
		return false;
	}
	$badcharacter = eregi_replace("([x0-9]+)","",$mcend);
	if(!empty($badcharacter)){
		$this->ERROR = "check_ip_syntax: Bad data in MC End [$badcharacter]";
		return false;
	}
	
	$lenmcday = strlen($mcday);
	if (eregi("([a-z]+)",$mcday)){
		if ($lenmcday > 2){$this->ERROR = "WOTAG > 2"; return false;}
		if (eregi("([0-9\.]+)",$mcday)){$this->ERROR = "WOTAG enthaelt (0-9.)"; return false;}
		#if (!(eregi("[mdsfx][aiorx]",$mcday))){return false;}
		if (!(eregi("(m[io]|d[io]|s[ao]|fr|x)",$mcday))){$this->ERROR = "WOTAG falscher String"; return false;}
	}
	if (eregi("([0-9]+)",$mcday)){
		if (eregi("[\.]",$mcday)){
			preg_match("/[\.]/",$mcday,$treffer);
			if (count($treffer) > 1){$this->ERROR = "mehr als 2 Punkte"; return false;};
			$exp = explode('.',$mcday);
			$day = $exp[0]; 
			$lenday = strlen($day);
			if ($lenday > 2){$this->ERROR = "TAG > 2"; return false;}
			if (!(eregi("(0[1-9]|[0-2][0-9]|3[01])",$day))){$this->ERROR = "TAG nicht korrekt"; return false;}
			$month = $exp[1];
			$lenmonth = strlen($month);
			if ($lenmonth > 2){$this->ERROR = "MONAT > 2"; return false;}
			if (!(eregi("(0[0-9]|0[0-9]|1[0-2])",$month))){$this->ERROR = "Monat nicht korrekt"; return false;}
			
		}
		else{
			if ($lenmcday > 2){$this->ERROR = "TAG > 2"; return false;}
			if (!(eregi("(0[0-9]|[0-2][0-9]|3[01])",$mcday))){$this->ERROR = "Tag nicht korrekt"; return false;}
		}
	}
	
	$lenmcbeg = strlen($mcbeg);
	if ($lenmcbeg == 2){
		if (!(eregi("(0[0-9]|1[0-9]|2[0-3]|x)",$mcbeg))){$this->ERROR = "Uhrzeit nicht korrekt"; return false;}
	}
	if ($lenmcbeg == 1){
		if (!(eregi("([0-9]|x)",$mcbeg))){$this->ERROR = "Uhrzeit nicht korrekt"; return false;}
	}
	$lenmcend = strlen($mcend);
	if ($lenmcend == 2){
		if (!(eregi("(0[0-9]|1[0-9]|2[0-3]|x)",$mcend))){$this->ERROR = "Uhrzeit nicht korrekt"; return false;}
	}
	if ($lenmcend == 1){
		if (!(eregi("([0-9]|x)",$mcend))){$this->ERROR = "Uhrzeit nicht korrekt"; return false;}
	}
	
	return true;
}


# Überprüft ob Menuposition ein Zahl ist
function check_menuposition($menpos){
	
	if($this->CLEAR) { $this->clear_error();}

	$badcharacter = eregi_replace("([0-9]+)","",$menpos);
	if(!empty($badcharacter)){
		$this->ERROR = "check_menupostion: Bad data in Menu Position [$badcharacter]";
		return false;
	}
}

}
?>