1<?php
2/*
3** Zabbix
4** Copyright (C) 2001-2021 Zabbix SIA
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19**/
20
21
22class CEventCorrCondValidator extends CValidator {
23
24	/**
25	 * Returns true if the given $value is valid, or set's an error and returns false otherwise.
26	 *
27	 * @param array $condition
28	 *
29	 * @return bool
30	 */
31	public function validate($condition) {
32		$operator = array_key_exists('operator', $condition) ? $condition['operator'] : null;
33		$tag = array_key_exists('tag', $condition) ? $condition['tag'] : null;
34		$oldtag = array_key_exists('oldtag', $condition) ? $condition['oldtag'] : null;
35		$newtag = array_key_exists('newtag', $condition) ? $condition['newtag'] : null;
36		$value = array_key_exists('value', $condition) ? $condition['value'] : null;
37		$groupids = array_key_exists('groupids', $condition) ? $condition['groupids'] : null;
38
39		$condition_type_validator = new CLimitedSetValidator([
40			'values' => [ZBX_CORR_CONDITION_OLD_EVENT_TAG, ZBX_CORR_CONDITION_NEW_EVENT_TAG,
41				ZBX_CORR_CONDITION_NEW_EVENT_HOSTGROUP,	ZBX_CORR_CONDITION_EVENT_TAG_PAIR,
42				ZBX_CORR_CONDITION_OLD_EVENT_TAG_VALUE,	ZBX_CORR_CONDITION_NEW_EVENT_TAG_VALUE
43			]
44		]);
45
46		if (!$condition_type_validator->validate($condition['type'])) {
47			$this->setError(_s('Incorrect value for field "%1$s": %2$s.', 'type', _('incorrect condition type')));
48		}
49
50		// Validate condition values depending on condition type.
51		switch ($condition['type']) {
52			case ZBX_CORR_CONDITION_OLD_EVENT_TAG:
53			case ZBX_CORR_CONDITION_NEW_EVENT_TAG:
54				if (zbx_empty($tag)) {
55					$this->setError(_s('Incorrect value for field "%1$s": %2$s.', 'tag', _('cannot be empty')));
56				}
57				break;
58
59			case ZBX_CORR_CONDITION_NEW_EVENT_HOSTGROUP:
60				if (!is_array($groupids) || zbx_empty($groupids)) {
61					$this->setError(_s('Incorrect value for field "%1$s": %2$s.', 'groupid', _('cannot be empty')));
62				}
63				else {
64					foreach ($groupids as $groupid) {
65						if ($groupid == 0) {
66							$this->setError(
67								_s('Incorrect value for field "%1$s": %2$s.', 'groupid', _('cannot be empty'))
68							);
69							break;
70						}
71					}
72				}
73				break;
74
75			case ZBX_CORR_CONDITION_EVENT_TAG_PAIR:
76				if (zbx_empty($oldtag)) {
77					$this->setError(_s('Incorrect value for field "%1$s": %2$s.', 'oldtag', _('cannot be empty')));
78				}
79				elseif (zbx_empty($newtag)) {
80					$this->setError(_s('Incorrect value for field "%1$s": %2$s.', 'newtag', _('cannot be empty')));
81				}
82				break;
83
84			case ZBX_CORR_CONDITION_OLD_EVENT_TAG_VALUE:
85			case ZBX_CORR_CONDITION_NEW_EVENT_TAG_VALUE:
86				if (zbx_empty($tag)) {
87					$this->setError(_s('Incorrect value for field "%1$s": %2$s.', 'tag', _('cannot be empty')));
88				}
89				elseif (!is_string($value)) {
90					$this->setError(
91						_s('Incorrect value for field "%1$s": %2$s.', 'value', _('a character string is expected'))
92					);
93				}
94				elseif (($operator == CONDITION_OPERATOR_LIKE || $operator == CONDITION_OPERATOR_NOT_LIKE)
95						&& $value === '') {
96					$this->setError(_s('Incorrect value for field "%1$s": %2$s.', 'value', _('cannot be empty')));
97				}
98				break;
99		}
100
101		// If no error is not set, return true.
102		return !(bool) $this->getError();
103	}
104}
105