File: CItemDelayFlexValidator.php

package info (click to toggle)
zabbix 1%3A3.0.7%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 60,008 kB
  • ctags: 38,245
  • sloc: php: 125,527; ansic: 120,253; sql: 40,319; sh: 5,620; makefile: 1,138; java: 957; cpp: 211; perl: 41; xml: 29
file content (474 lines) | stat: -rw-r--r-- 14,239 bytes parent folder | download | duplicates (2)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<?php
/*
** Zabbix
** Copyright (C) 2001-2016 Zabbix SIA
**
** 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/


class CItemDelayFlexValidator extends CValidator {

	private $time_period_validator;
	/**
	 * Returns true if the all of the given values and conditions match the criteria:
	 *
	 *		Flexible intervals must have valid delay and time period. Delay must not exceed 86400 seconds and
	 *		period should correspond to time period syntax %d-%d,%d:%d-%d:%d or %d,%d:%d-%d:%d
	 *		which is validated by CTimePeriodValidator.
	 *
	 *		Scheduling intervals must have valid month days 1-31, week days 1-7, hours 0-23, minutes 0-59
	 *		and seconds 0-59. Second month day, week day, hour, minute or second should not be greater than
	 *		first.
	 *
	 * @param array $intervals						An array of intervals to validate.
	 * @param string $intervals[]['type']			Interval type: flexible or scheduling.
	 *
	 * @return bool
	 */
	public function validate($intervals) {
		if (!$intervals) {
			return true;
		}

		$this->time_period_validator = new CTimePeriodValidator();

		$result = true;

		foreach ($intervals as $interval) {
			if ($interval['type'] == ITEM_DELAY_FLEX_TYPE_FLEXIBLE) {
				$result = $result && $this->validateFlexibleInterval($interval);
			}
			elseif ($interval['type'] == ITEM_DELAY_FLEX_TYPE_SCHEDULING) {
				$result = $result && $this->validateSchedulingInterval($interval);
			}
		}

		return $result;
	}

	/**
	 * Validate flexible interval delay and time period.
	 *
	 * @param array $interval						Data of flexible interval array.
	 * @param string $interval[]['delay']			Flexible interval delay.
	 * @param string $interval[]['period']			Flexible interval period.
	 *
	 * return bool
	 */
	private function validateFlexibleInterval($interval) {
		if ($interval['delay'] > SEC_PER_DAY) {
			$this->setError(_s('Invalid flexible interval delay: "%1$s" exceeds maximum delay of "%2$s".',
				$interval['delay'],
				SEC_PER_DAY
			));

			return false;
		}

		if ($this->time_period_validator->validate($interval['period'])) {
			return true;
		}
		else {
			$this->setError($this->time_period_validator->getError());

			return false;
		}
	}

	/**
	 * Validate scheduling interval month days, week days, hours, minutes and seconds.
	 *
	 * @param array $interval						Data of scheduling interval array.
	 * @param string $interval[]['interval']		An array containing month days, week days, hours, minutes, seconds.
	 * @param array $interval[]['md']				An array of month days.
	 * @param string $interval[]['md'][]['from']	Month day "from".
	 * @param string $interval[]['md'][]['till']	Month day "till".
	 * @param string $interval[]['md'][]['step']	Month day "step".
	 * @param array $interval[]['wd']				An array of week days.
	 * @param string $interval[]['wd'][]['from']	Week day "from".
	 * @param string $interval[]['wd'][]['till']	Week day "till".
	 * @param string $interval[]['wd'][]['step']	Week day "step".
	 * @param array $interval[]['h']				An array of hours.
	 * @param string $interval[]['h'][]['from']		Hours "from".
	 * @param string $interval[]['h'][]['till']		Hours "till".
	 * @param string $interval[]['h'][]['step']		Hour "step".
	 * @param array $interval[]['m']				An array of minutes.
	 * @param string $interval[]['m'][]['from']		Minutes "from".
	 * @param string $interval[]['m'][]['till']		Minutes "till".
	 * @param string $interval[]['m'][]['step']		Minute "step".
	 * @param array $interval[]['s']				An array of seconds.
	 * @param string $interval[]['s'][]['from']		Seconds "from".
	 * @param string $interval[]['s'][]['till']		Seconds "till".
	 * @param string $interval[]['s'][]['step']		Second "step".
	 *
	 * return bool
	 */
	private function validateSchedulingInterval($interval) {
		// Check month day boundaries.
		if (array_key_exists('md', $interval)) {
			foreach ($interval['md'] as $month_day) {
				if ($month_day['from'] !== '') {
					$month_day_from = (int) $month_day['from'];

					if ($month_day_from < 1 || $month_day_from > 31) {
						$this->setError(_s('Invalid interval "%1$s": invalid month day "%2$s".', $interval['interval'],
							$month_day['from']
						));

						return false;
					}

					// Ending month day is optional.
					if ($month_day['till'] !== '') {
						$month_day_till = (int) $month_day['till'];

						// Should be a valid month day.
						if ($month_day_till < 1 || $month_day_till > 31) {
							$this->setError(_s('Invalid interval "%1$s": invalid month day "%2$s".',
								$interval['interval'],
								$month_day['till']
							));

							return false;
						}

						// If entered, it cannot be greater than starting month day.
						if ($month_day_from > $month_day_till) {
							$this->setError(_s(
								'Invalid interval "%1$s": starting month day must be less or equal to ending month day.',
								$interval['interval']
							));

							return false;
						}

						// Month day step is optional.
						if ($month_day['step'] !== '') {
							$month_day_step = (int) $month_day['step'];

							if ($month_day_step < 1 || $month_day_step > 30
									|| ($month_day_step > ($month_day_till - $month_day_from))
									|| ($month_day_from == $month_day_till && $month_day_step != 1)) {
								$this->setError(_s('Invalid interval "%1$s": invalid month day step "%2$s".',
									$interval['interval'],
									$month_day['step']
								));

								return false;
							}
						}
					}
				}
				elseif ($month_day['step'] !== '') {
					$month_day_step = (int) $month_day['step'];

					// If month day is ommited, month day step is mandatory.
					if ($month_day_step < 1 || $month_day_step > 30) {
						$this->setError(_s('Invalid interval "%1$s": invalid month day step "%2$s".',
							$interval['interval'],
							$month_day['step']
						));

						return false;
					}
				}
			}
		}

		// Check week day boundaries.
		if (array_key_exists('wd', $interval)) {
			foreach ($interval['wd'] as $week_day) {
				if ($week_day['from'] !== '') {
					$week_day_from = (int) $week_day['from'];

					if ($week_day_from < 1 || $week_day_from > 7) {
						$this->setError(_s('Invalid interval "%1$s": invalid week day "%2$s".', $interval['interval'],
							$week_day['from']
						));

						return false;
					}

					// Ending week day is optional.
					if ($week_day['till'] !== '') {
						$week_day_till = (int) $week_day['till'];

						// Should be a valid week day.
						if ($week_day_till < 1 || $week_day_till > 7) {
							$this->setError(_s('Invalid interval "%1$s": invalid week day "%2$s".',
								$interval['interval'],
								$week_day['till']
							));

							return false;
						}

						// If entered, it cannot be greater than starting week day.
						if ($week_day_from > $week_day_till) {
							$this->setError(_s(
								'Invalid interval "%1$s": starting week day must be less or equal to ending week day.',
								$interval['interval']
							));

							return false;
						}

						// Week day step is optional.
						if ($week_day['step'] !== '') {
							$week_day_step = (int) $week_day['step'];

							if ($week_day_step < 1 || $week_day_step > 6
									|| ($week_day_step > ($week_day_till - $week_day_from))
									|| ($week_day_from == $week_day_till && $week_day_step != 1)) {
								$this->setError(_s('Invalid interval "%1$s": invalid week day step "%2$s".',
									$interval['interval'],
									$week_day['step']
								));

								return false;
							}
						}
					}
				}
				elseif ($week_day['step'] !== '') {
					$week_day_step = (int) $week_day['step'];

					// If week day is ommited, week day step is mandatory.
					if ($week_day_step < 1 || $week_day_step > 6) {
						$this->setError(_s('Invalid interval "%1$s": invalid week day step "%2$s".',
							$interval['interval'],
							$week_day['step']
						));

						return false;
					}
				}
			}
		}

		// Check hour boundaries.
		if (array_key_exists('h', $interval)) {
			foreach ($interval['h'] as $hours) {
				if ($hours['from'] !== '') {
					$hours_from = (int) $hours['from'];

					if ($hours_from > 23) {
						$this->setError(_s('Invalid interval "%1$s": invalid hours "%2$s".', $interval['interval'],
							$hours['from']
						));

						return false;
					}

					// Ending hour is optional.
					if ($hours['till'] !== '') {
						$hours_till = (int) $hours['till'];

						// Should be a valid hour.
						if ($hours_till > 23) {
							$this->setError(_s('Invalid interval "%1$s": invalid hours "%2$s".', $interval['interval'],
								$hours['till']
							));

							return false;
						}

						// If entered, it cannot be greater than starting hour.
						if ($hours_from > $hours_till) {
							$this->setError(_s(
								'Invalid interval "%1$s": starting hour must be less or equal to ending hour.',
								$interval['interval']
							));

							return false;
						}

						// Hour step is optional.
						if ($hours['step'] !== '') {
							$hour_step = (int) $hours['step'];

							if ($hour_step < 1 || $hour_step > 23
									|| ($hour_step > ($hours_till - $hours_from))
									|| ($hours_from == $hours_till && $hour_step != 1)) {
								$this->setError(_s('Invalid interval "%1$s": invalid hour step "%2$s".',
									$interval['interval'],
									$hours['step']
								));

								return false;
							}
						}
					}
				}
				elseif ($hours['step'] !== '') {
					$hour_step = (int) $hours['step'];

					// If hour is ommited, hour step is mandatory.
					if ($hour_step < 1 || $hour_step > 23) {
						$this->setError(_s('Invalid interval "%1$s": invalid hour step "%2$s".', $interval['interval'],
							$hours['step']
						));

						return false;
					}
				}
			}
		}

		// Check minute boundaries.
		if (array_key_exists('m', $interval)) {
			foreach ($interval['m'] as $minutes) {
				if ($minutes['from'] !== '') {
					$minutes_from = (int) $minutes['from'];

					if ($minutes_from > 59) {
						$this->setError(_s('Invalid interval "%1$s": invalid minutes "%2$s".', $interval['interval'],
							$minutes['from']
						));

						return false;
					}

					// Ending minute is optional.
					if ($minutes['till'] !== '') {
						$minutes_till = (int) $minutes['till'];

						// Should be a valid minute.
						if ($minutes_till > 59) {
							$this->setError(_s('Invalid interval "%1$s": invalid minutes "%2$s".',
								$interval['interval'], $minutes['till']
							));

							return false;
						}

						// If entered, it cannot be greater than starting minute.
						if ($minutes_from > $minutes_till) {
							$this->setError(_s(
								'Invalid interval "%1$s": starting minute must be less or equal to ending minute.',
								$interval['interval']
							));

							return false;
						}

						// Minute step is optional.
						if ($minutes['step'] !== '') {
							$minute_step = (int) $minutes['step'];

							if ($minute_step < 1 || $minute_step > 59
									|| ($minute_step > ($minutes_till - $minutes_from))
									|| ($minutes_from == $minutes_till && $minute_step != 1)) {
								$this->setError(_s('Invalid interval "%1$s": invalid minute step "%2$s".',
									$interval['interval'],
									$minutes['step']
								));

								return false;
							}
						}
					}
				}
				elseif ($minutes['step'] !== '') {
					$minute_step = (int) $minutes['step'];

					// If minute is ommited, minute step is mandatory.
					if ($minute_step < 1 || $minute_step > 59) {
						$this->setError(_s('Invalid interval "%1$s": invalid minute step "%2$s".',
							$interval['interval'],
							$minutes['step']
						));

						return false;
					}
				}
			}
		}

		// Check minute boundaries.
		if (array_key_exists('s', $interval)) {
			foreach ($interval['s'] as $seconds) {
				if ($seconds['from'] !== '') {
					$seconds_from = (int) $seconds['from'];

					if ($seconds_from > 59) {
						$this->setError(_s('Invalid interval "%1$s": invalid seconds "%2$s".', $interval['interval'],
							$seconds['from']
						));

						return false;
					}

					// Ending second is optional.
					if ($seconds['till'] !== '') {
						$seconds_till = (int) $seconds['till'];

						// Should be a valid second.
						if ($seconds_till > 59) {
							$this->setError(_s('Invalid interval "%1$s": invalid seconds "%2$s".',
								$interval['interval'],
								$seconds['till']
							));

							return false;
						}

						// If entered, it cannot be greater than starting second.
						if ($seconds_from > $seconds_till) {
							$this->setError(_s(
								'Invalid interval "%1$s": starting second must be less or equal to ending second.',
								$interval['interval']
							));

							return false;
						}

						// Second step is optional.
						if ($seconds['step'] !== '') {
							$second_step = (int) $seconds['step'];

							if ($second_step < 1 || $second_step > 59
									|| ($second_step > ($seconds_till - $seconds_from))
									|| ($seconds_from == $seconds_till && $second_step != 1)) {
								$this->setError(_s('Invalid interval "%1$s": invalid second step "%2$s".',
									$interval['interval'],
									$seconds['step']
								));

								return false;
							}
						}
					}
				}
				elseif ($seconds['step'] !== '') {
					$second_step = (int) $seconds['step'];

					// If second is ommited, second step is mandatory.
					if ($second_step < 1 || $second_step > 59) {
						$this->setError(_s('Invalid interval "%1$s": invalid second step "%2$s".',
							$interval['interval'],
							$seconds['step']
						));

						return false;
					}
				}
			}
		}

		return true;
	}
}