1<?php declare(strict_types = 1);
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
22/**
23 * Class containing methods for operations with housekeeping parameters.
24 */
25class CHousekeeping extends CApiService {
26
27	public const ACCESS_RULES = [
28		'get' => ['min_user_type' => USER_TYPE_ZABBIX_USER],
29		'update' => ['min_user_type' => USER_TYPE_SUPER_ADMIN]
30	];
31
32	/**
33	 * @var string
34	 */
35	protected $tableName = 'config';
36
37	/**
38	 * @var string
39	 */
40	protected $tableAlias = 'c';
41
42	/**
43	 * @var array
44	 */
45	private $output_fields = ['hk_events_mode', 'hk_events_trigger', 'hk_events_internal', 'hk_events_discovery',
46		'hk_events_autoreg', 'hk_services_mode', 'hk_services', 'hk_audit_mode', 'hk_audit', 'hk_sessions_mode',
47		'hk_sessions', 'hk_history_mode', 'hk_history_global', 'hk_history', 'hk_trends_mode', 'hk_trends_global',
48		'hk_trends', 'db_extension', 'compression_status', 'compress_older'];
49
50	/**
51	 * Get housekeeping parameters.
52	 *
53	 * @param array $options
54	 *
55	 * @throws APIException if the input is invalid.
56	 *
57	 * @return array
58	 */
59	public function get(array $options): array {
60		$api_input_rules = ['type' => API_OBJECT, 'fields' => [
61			'output' =>	['type' => API_OUTPUT, 'in' => implode(',', $this->output_fields), 'default' => API_OUTPUT_EXTEND]
62		]];
63		if (!CApiInputValidator::validate($api_input_rules, $options, '/', $error)) {
64			self::exception(ZBX_API_ERROR_PARAMETERS, $error);
65		}
66
67		if ($options['output'] === API_OUTPUT_EXTEND) {
68			$options['output'] = $this->output_fields;
69		}
70
71		$db_hk = [];
72
73		$result = DBselect($this->createSelectQuery($this->tableName(), $options));
74		while ($row = DBfetch($result)) {
75			$db_hk[] = $row;
76		}
77		$db_hk = $this->unsetExtraFields($db_hk, ['configid'], []);
78
79		return $db_hk[0];
80	}
81
82	/**
83	 * Update housekeeping parameters.
84	 *
85	 * @param array  $hk
86	 *
87	 * @return array
88	 */
89	public function update(array $hk): array {
90		$db_hk = $this->validateUpdate($hk);
91
92		if (self::$userData['type'] != USER_TYPE_SUPER_ADMIN) {
93			return [];
94		}
95
96		$upd_config = [];
97
98		// strings
99		$field_names = ['hk_events_trigger', 'hk_events_internal', 'hk_events_discovery', 'hk_events_autoreg',
100			'hk_services', 'hk_audit', 'hk_sessions', 'hk_history', 'hk_trends', 'compress_older'
101		];
102		foreach ($field_names as $field_name) {
103			if (array_key_exists($field_name, $hk) && $hk[$field_name] !== $db_hk[$field_name]) {
104				$upd_config[$field_name] = $hk[$field_name];
105			}
106		}
107
108		// integers
109		$field_names = ['hk_events_mode', 'hk_services_mode', 'hk_audit_mode', 'hk_sessions_mode', 'hk_history_mode',
110			'hk_history_global', 'hk_trends_mode', 'hk_trends_global', 'compression_status'
111		];
112		foreach ($field_names as $field_name) {
113			if (array_key_exists($field_name, $hk) && $hk[$field_name] != $db_hk[$field_name]) {
114				$upd_config[$field_name] = $hk[$field_name];
115			}
116		}
117
118		if ($upd_config) {
119			DB::update('config', [
120				'values' => $upd_config,
121				'where' => ['configid' => $db_hk['configid']]
122			]);
123		}
124
125		$this->addAuditBulk(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_HOUSEKEEPING,
126			[['configid' => $db_hk['configid']] + $hk], [$db_hk['configid'] => $db_hk]
127		);
128
129		return array_keys($hk);
130	}
131
132	/**
133	 * Validate updated housekeeping parameters.
134	 *
135	 * @param array  $hk
136	 *
137	 * @throws APIException if the input is invalid.
138	 *
139	 * @return array
140	 */
141	protected function validateUpdate(array $hk): array {
142		$api_input_rules = ['type' => API_OBJECT, 'flags' => API_NOT_EMPTY, 'fields' => [
143			'hk_events_mode' =>			['type' => API_INT32, 'in' => '0,1'],
144			'hk_events_trigger' =>		['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR])],
145			'hk_events_internal' =>		['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR])],
146			'hk_events_discovery' =>	['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR])],
147			'hk_events_autoreg' =>		['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR])],
148			'hk_services_mode' =>		['type' => API_INT32, 'in' => '0,1'],
149			'hk_services' =>			['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR])],
150			'hk_audit_mode' =>			['type' => API_INT32, 'in' => '0,1'],
151			'hk_audit' =>				['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR])],
152			'hk_sessions_mode' =>		['type' => API_INT32, 'in' => '0,1'],
153			'hk_sessions' =>			['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR])],
154			'hk_history_mode' =>		['type' => API_INT32, 'in' => '0,1'],
155			'hk_history_global' =>		['type' => API_INT32, 'in' => '0,1'],
156			'hk_history' =>				['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => '0,'.implode(':', [SEC_PER_HOUR, 25 * SEC_PER_YEAR])],
157			'hk_trends_mode' =>			['type' => API_INT32, 'in' => '0,1'],
158			'hk_trends_global' =>		['type' => API_INT32, 'in' => '0,1'],
159			'hk_trends' =>				['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => '0,'.implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR])],
160			'compression_status' =>		['type' => API_INT32, 'in' => '0,1'],
161			'compress_older' =>			['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => implode(':', [7 * SEC_PER_DAY, 25 * SEC_PER_YEAR])]
162		]];
163		if (!CApiInputValidator::validate($api_input_rules, $hk, '/', $error)) {
164			self::exception(ZBX_API_ERROR_PARAMETERS, $error);
165		}
166
167		// Check permissions.
168		if (self::$userData['type'] != USER_TYPE_SUPER_ADMIN) {
169			self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!'));
170		}
171
172		$output_fields = array_diff($this->output_fields, ['db_extension']);
173		$output_fields[] = 'configid';
174
175		return DB::select('config', ['output' => $output_fields])[0];
176	}
177}
178