summaryrefslogtreecommitdiffstats
path: root/frontends/php/include/validate.inc.php
blob: 89090b518a1c137036dd2529668a5e686469cc5d (plain)
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
<?php
/*
** ZABBIX
** Copyright (C) 2000-2006 SIA Zabbix
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/
?>
<?php
	function	BETWEEN($min,$max)
	{
		return "({}>=$min&&{}<=$max)&&";
	}

	function	GT($value)
	{
		return "({}>=$value)&&";
	}

	function	IN($array)
	{
		return "in_array({},array($array))&&";
	}

	define("NOT_EMPTY","({}!='')&&");

//		VAR			TYPE	OPTIONAL FLAGS	VALIDATION	EXCEPTION
	function	calc_exp($fields,$field,$expression)
	{
		global $_REQUEST;

		if(strstr($expression,"{}"))
		{
			if(!isset($_REQUEST[$field]))	return FALSE;
		}
		$expression = str_replace("{}",'$_REQUEST["'.$field.'"]',$expression);
		foreach($fields as $f => $checks)
		{
			// If an unset variable used in expression, return FALSE
			if(strstr($expression,'{'.$f.'}')&&!isset($_REQUEST[$f]))
			{
//				info("Variable is not set. $expression is FALSE");
				return FALSE;
			}
//			echo $f,":",$expression,"<br>";
			$expression = str_replace('{'.$f.'}','$_REQUEST["'.$f.'"]',$expression);
		}
		$expression=rtrim($expression,"&");
		if($expression[strlen($expression)-1]=='&')	$expression[strlen($expression)-1]=0;
		if($expression[strlen($expression)-1]=='&')	$expression[strlen($expression)-1]=0;
		$exec = "return ".$expression.";";
//		info($exec);
		return eval($exec);
	}

	function	unset_not_in_list(&$fields)
	{
		foreach($_REQUEST as $key => $val)
		{
			if(!isset($fields[$key]))
			{
//				info("Unset:".$key);
				unset($_REQUEST[$key]);
			}
		}
	}

	function	unset_action_vars($fields)
	{
		foreach($fields as $field => $checks)
		{
			list($type,$opt,$flags,$validation,$exception)=$checks;
			
			if(($flags&P_ACT)&&(isset($_REQUEST[$field])))
			{
//				info("Unset:".$field);
				unset($_REQUEST[$field]);
			}
		}
	}

	function	unset_all()
	{
		foreach($_REQUEST as $key => $val)
		{
			unset($_REQUEST[$key]);
		}
	}

	function	check_fields(&$fields)
	{
		global	$_REQUEST;

		$ret = TRUE;

		$critical = FALSE;

		foreach($fields as $field => $checks)
		{
			list($type,$opt,$flags,$validation,$exception)=$checks;

//			info("Field: $field");

			if($exception==NULL)	$except=FALSE;
			else			$except=calc_exp($fields,$field,$exception);

			if($opt == O_MAND &&	$except)	$opt = O_NO;
			else if($opt == O_OPT && $except)	$opt = O_MAND;
			else if($opt == O_NO && $except)	$opt = O_MAND;


			if($opt == O_MAND)
			{
				if(!isset($_REQUEST[$field]))
				{
					$ret = FALSE;
					info("Warning. Field [".$field."] is mandatory");
					if($flags&P_SYS)
					{
						info("Critical error. Field [".$field."] is mandatory");
						unset_all();
						$critical = TRUE;
						break;
					}
					else	continue;
				}
			}

			if($opt == O_NO)
			{
				if(isset($_REQUEST[$field]))
				{
					$ret = FALSE;
					info("Warning. Field [".$field."] must be missing");
					if($flags&P_SYS)
					{
						info("Critical error. Field [".$field."] must be missing");
						unset_all();
						$critical = TRUE;
						break;
					}
					else	continue;
				}
				else continue;
			}

			if($opt == O_OPT)
			{
				if(!isset($_REQUEST[$field]))	continue;
			}


			if( ($type == T_ZBX_INT) && !is_numeric($_REQUEST[$field])) {
				$ret = FALSE;
				info("Warning. Field [".$field."] is not integer");
				if($flags&P_SYS)
				{
					info("Critical error. Field [".$field."] is not integer");
					unset_all();
					$critical = TRUE;
					break;
				}
				else	continue;
			}

			if( ($type == T_ZBX_DBL) && !is_numeric($_REQUEST[$field])) {
				$ret = FALSE;
				info("Warning. Field [".$field."] is not double");
				if($flags&P_SYS)
				{
					info("Critical error. Field [".$field."] is not double");
					unset_all();
					$critical = TRUE;
					break;
				}
				else	continue;
			}

			if(($exception==NULL)||($except==TRUE))
			{
				if(!$validation)	$valid=TRUE;
				else			$valid=calc_exp($fields,$field,$validation);

				if(!$valid)
				{
					$ret = FALSE;
					info("Warning. Incorrect value for [".$field."]");
					if($flags&P_SYS)
					{
						info("Critical error. Incorrect value for [".$field."]");
						unset_all();
						$critical = TRUE;
						break;
					}
					else	continue;
				}
			}

			if(isset($_REQUEST[$field])&&!get_magic_quotes_gpc()) {
				$_REQUEST[$field]=addslashes($_REQUEST[$field]);
			}
		}
		unset_not_in_list($fields);
		if($critical)
		{
			show_messages(FALSE, "", "Invalid URL");
			show_page_footer();
			exit;
		}
		if(!$ret)
		{
			unset_action_vars($fields);
		}
		show_messages();
		return $ret;
	}
?>