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 CControllerHousekeepingUpdate extends CController {
23
24	protected function checkInput() {
25		$fields = [
26			'hk_trends'				=> 'db config.hk_trends|time_unit 0,'.implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR]),
27			'hk_trends_global'		=> 'db config.hk_trends_global|in 1',
28			'hk_trends_mode'		=> 'db config.hk_trends_mode',
29			'hk_history'			=> 'db config.hk_history|time_unit 0,'.implode(':', [SEC_PER_HOUR, 25 * SEC_PER_YEAR]),
30			'hk_history_global'		=> 'db config.hk_history_global|in 1',
31			'hk_history_mode'		=> 'db config.hk_history_mode',
32			'hk_sessions'			=> 'db config.hk_sessions|time_unit '.implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR]),
33			'hk_sessions_mode'		=> 'db config.hk_sessions_mode|in 1',
34			'hk_audit'				=> 'db config.hk_audit|time_unit '.implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR]),
35			'hk_audit_mode'			=> 'db config.hk_audit_mode|in 1',
36			'hk_services'			=> 'db config.hk_services|time_unit '.implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR]),
37			'hk_services_mode'		=> 'db config.hk_services_mode|in 1',
38			'hk_events_autoreg'		=> 'db config.hk_events_autoreg|time_unit '.implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR]),
39			'hk_events_discovery'	=> 'db config.hk_events_discovery|time_unit '.implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR]),
40			'hk_events_internal'	=> 'db config.hk_events_internal|time_unit '.implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR]),
41			'hk_events_trigger'		=> 'db config.hk_events_trigger|time_unit '.implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR]),
42			'hk_events_mode'		=> 'db config.hk_events_mode|in 1',
43			'compression_status'	=> 'db config.compression_status|in 1',
44			'compress_older'		=> 'db config.compress_older|time_unit '.implode(':', [7 * SEC_PER_DAY, 25 * SEC_PER_YEAR])
45		];
46
47		$ret = $this->validateInput($fields);
48
49		if (!$ret) {
50			switch ($this->GetValidationError()) {
51				case self::VALIDATION_ERROR:
52					$response = new CControllerResponseRedirect((new CUrl('zabbix.php'))
53						->setArgument('action', 'housekeeping.edit')
54						->getUrl()
55					);
56					$response->setFormData($this->getInputAll() + [
57						'hk_events_mode' => '0',
58						'hk_services_mode' => '0',
59						'hk_audit_mode' => '0',
60						'hk_sessions_mode' => '0',
61						'hk_history_mode' => '0',
62						'hk_history_global' => '0',
63						'hk_trends_mode' => '0',
64						'hk_trends_global' => '0',
65						'compression_status' => '0'
66					]);
67					CMessageHelper::setErrorTitle(_('Cannot update configuration'));
68					$this->setResponse($response);
69					break;
70				case self::VALIDATION_FATAL_ERROR:
71					$this->setResponse(new CControllerResponseFatal());
72					break;
73			}
74		}
75
76		return $ret;
77	}
78
79	protected function checkPermissions() {
80		return $this->checkAccess(CRoleHelper::UI_ADMINISTRATION_GENERAL);
81	}
82
83	protected function doAction() {
84		$hk = [
85			CHousekeepingHelper::HK_EVENTS_MODE => $this->getInput('hk_events_mode', 0),
86			CHousekeepingHelper::HK_SERVICES_MODE => $this->getInput('hk_services_mode', 0),
87			CHousekeepingHelper::HK_AUDIT_MODE => $this->getInput('hk_audit_mode', 0),
88			CHousekeepingHelper::HK_SESSIONS_MODE => $this->getInput('hk_sessions_mode', 0),
89			CHousekeepingHelper::HK_HISTORY_MODE => $this->getInput('hk_history_mode', 0),
90			CHousekeepingHelper::HK_HISTORY_GLOBAL => $this->getInput('hk_history_global', 0),
91			CHousekeepingHelper::HK_TRENDS_MODE => $this->getInput('hk_trends_mode', 0),
92			CHousekeepingHelper::HK_TRENDS_GLOBAL => $this->getInput('hk_trends_global', 0),
93			CHousekeepingHelper::COMPRESSION_STATUS => $this->getInput('compression_status', 0),
94			CHousekeepingHelper::COMPRESS_OLDER => $this->getInput('compress_older', DB::getDefault('config',
95				'compress_older'
96			))
97		];
98
99		if ($hk[CHousekeepingHelper::COMPRESSION_STATUS] === 0) {
100			unset($hk[CHousekeepingHelper::COMPRESS_OLDER]);
101		}
102
103		if ($hk[CHousekeepingHelper::HK_EVENTS_MODE] == 1) {
104			$this->getInputs($hk, [CHousekeepingHelper::HK_EVENTS_TRIGGER, CHousekeepingHelper::HK_EVENTS_INTERNAL,
105				CHousekeepingHelper::HK_EVENTS_DISCOVERY, CHousekeepingHelper::HK_EVENTS_AUTOREG
106			]);
107		}
108
109		if ($hk[CHousekeepingHelper::HK_SERVICES_MODE] == 1) {
110			$hk[CHousekeepingHelper::HK_SERVICES] = $this->getInput('hk_services');
111		}
112
113		if ($hk[CHousekeepingHelper::HK_AUDIT_MODE] == 1) {
114			$hk[CHousekeepingHelper::HK_AUDIT] = $this->getInput('hk_audit');
115		}
116
117		if ($hk[CHousekeepingHelper::HK_SESSIONS_MODE] == 1) {
118			$hk[CHousekeepingHelper::HK_SESSIONS] = $this->getInput('hk_sessions');
119		}
120
121		if ($hk[CHousekeepingHelper::HK_HISTORY_GLOBAL] == 1) {
122			$hk[CHousekeepingHelper::HK_HISTORY] = $this->getInput('hk_history');
123		}
124
125		if ($hk[CHousekeepingHelper::HK_TRENDS_GLOBAL] == 1) {
126			$hk[CHousekeepingHelper::HK_TRENDS] = $this->getInput('hk_trends');
127		}
128
129		$result = API::Housekeeping()->update($hk);
130
131		$response = new CControllerResponseRedirect((new CUrl('zabbix.php'))
132			->setArgument('action', 'housekeeping.edit')
133		);
134
135		if ($result) {
136			CMessageHelper::setSuccessTitle(_('Configuration updated'));
137		}
138		else {
139			$response->setFormData($this->getInputAll());
140			CMessageHelper::setErrorTitle(_('Cannot update configuration'));
141		}
142
143		$this->setResponse($response);
144	}
145}
146