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
22require_once dirname(__FILE__).'/include/config.inc.php';
23require_once dirname(__FILE__).'/include/audit.inc.php';
24require_once dirname(__FILE__).'/include/actions.inc.php';
25require_once dirname(__FILE__).'/include/users.inc.php';
26
27$page['title'] = _('Action log');
28$page['file'] = 'auditacts.php';
29$page['scripts'] = ['class.calendar.js', 'gtlc.js', 'flickerfreescreen.js', 'multiselect.js'];
30$page['type'] = detect_page_type(PAGE_TYPE_HTML);
31
32require_once dirname(__FILE__).'/include/page_header.php';
33
34// VAR	TYPE	OPTIONAL	FLAGS	VALIDATION	EXCEPTION
35$fields = [
36	// filter
37	'filter_rst' =>		[T_ZBX_STR,			O_OPT, P_SYS,	null,	null],
38	'filter_set' =>		[T_ZBX_STR,			O_OPT, P_SYS,	null,	null],
39	'filter_userids' =>	[T_ZBX_STR,			O_OPT, P_SYS,	null,	null],
40	'from' =>			[T_ZBX_RANGE_TIME,	O_OPT, P_SYS,	null,	null],
41	'to' =>				[T_ZBX_RANGE_TIME,	O_OPT, P_SYS,	null,	null]
42];
43check_fields($fields);
44validateTimeSelectorPeriod(getRequest('from'), getRequest('to'));
45
46if ($page['type'] == PAGE_TYPE_JS || $page['type'] == PAGE_TYPE_HTML_BLOCK) {
47	require_once dirname(__FILE__).'/include/page_footer.php';
48	exit;
49}
50
51/*
52 * Filter
53 */
54if (hasRequest('filter_set')) {
55	CProfile::updateArray('web.auditacts.filter.userids', getRequest('filter_userids', []), PROFILE_TYPE_ID);
56}
57elseif (hasRequest('filter_rst')) {
58	DBStart();
59	CProfile::deleteIdx('web.auditacts.filter.userids');
60	DBend();
61}
62
63/*
64 * Display
65 */
66$timeselector_options = [
67	'profileIdx' => 'web.auditacts.filter',
68	'profileIdx2' => 0,
69	'from' => getRequest('from'),
70	'to' => getRequest('to')
71];
72updateTimeSelectorPeriod($timeselector_options);
73
74$data = [
75	'filter_userids' => CProfile::getArray('web.auditacts.filter.userids', []),
76	'users' => [],
77	'alerts' => [],
78	'paging' => null,
79	'timeline' => getTimeSelectorPeriod($timeselector_options),
80	'active_tab' => CProfile::get('web.auditacts.filter.active', 1)
81];
82
83$userids = [];
84
85if ($data['filter_userids']) {
86	$data['users'] = API::User()->get([
87		'output' => ['userid', 'username', 'name', 'surname'],
88		'userids' => $data['filter_userids'],
89		'preservekeys' => true
90	]);
91
92	$userids = array_column($data['users'], 'userid');
93
94	// Sanitize userids for multiselect.
95	$data['filter_userids'] = array_map(function (array $value): array {
96		return ['id' => $value['userid'], 'name' => getUserFullname($value)];
97	}, $data['users']);
98
99	CArrayHelper::sort($data['filter_userids'], ['name']);
100}
101
102if (!$data['filter_userids'] || $data['users']) {
103	// Fetch alerts for different objects and sources and combine them in a single stream.
104	$limit = CSettingsHelper::get(CSettingsHelper::SEARCH_LIMIT) + 1;
105	foreach (eventSourceObjects() as $eventSource) {
106		$data['alerts'] = array_merge($data['alerts'], API::Alert()->get([
107			'output' => ['alertid', 'actionid', 'userid', 'clock', 'sendto', 'subject', 'message', 'status',
108				'retries', 'error', 'alerttype'
109			],
110			'selectMediatypes' => ['mediatypeid', 'name', 'maxattempts'],
111			'userids' => $userids ? $userids : null,
112			// API::Alert operates with 'open' time interval therefore before call have to alter 'from' and 'to' values.
113			'time_from' => $data['timeline']['from_ts'] - 1,
114			'time_till' => $data['timeline']['to_ts'] + 1,
115			'eventsource' => $eventSource['source'],
116			'eventobject' => $eventSource['object'],
117			'sortfield' => 'alertid',
118			'sortorder' => ZBX_SORT_DOWN,
119			'limit' => $limit
120		]));
121	}
122
123	CArrayHelper::sort($data['alerts'], [
124		['field' => 'alertid', 'order' => ZBX_SORT_DOWN]
125	]);
126
127	$data['alerts'] = array_slice($data['alerts'], 0, CSettingsHelper::get(CSettingsHelper::SEARCH_LIMIT) + 1);
128
129	$data['paging'] = CPagerHelper::paginate(getRequest('page', 1), $data['alerts'], ZBX_SORT_DOWN,
130		new CUrl('auditacts.php')
131	);
132
133	// Get users.
134	if (!$data['filter_userids']) {
135		$data['users'] = API::User()->get([
136			'output' => ['userid', 'username', 'name', 'surname'],
137			'userids' => array_column($data['alerts'], 'userid'),
138			'preservekeys' => true
139		]);
140	}
141}
142
143// Get actions names.
144if ($data['alerts']) {
145	$data['actions'] = API::Action()->get([
146		'output' => ['actionid', 'name'],
147		'actionids' => array_unique(array_column($data['alerts'], 'actionid')),
148		'preservekeys' => true
149	]);
150}
151
152echo (new CView('administration.auditacts.list', $data))->getOutput();
153
154require_once dirname(__FILE__).'/include/page_footer.php';
155