Repository navigation
Expand file tree
/
Copy pathlayouts.php
More file actions
296 lines (242 loc) · 9.05 KB
/
Copy pathlayouts.php
File metadata and controls
296 lines (242 loc) · 9.05 KB
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
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| 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. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
require('./include/auth.php');
// Saved-layout create/rename/delete/publish requests (posted here or to any
// other filter page) are handled by filter_layouts_handle_request(), which is
// invoked from include/auth.php before this point. Only the management view
// remains here.
$actions = [
1 => __('Delete'),
];
if (filter_layouts_can_manage_global()) {
$actions[2] = __('Make Global');
$actions[3] = __('Make Local');
}
set_default_action();
switch (get_nfilter_request_var('action')) {
case 'actions':
layouts_form_actions();
break;
default:
top_header();
layouts_manage();
bottom_footer();
break;
}
/**
* Apply a bulk action (Delete, Make Global, Make Local) to the layouts selected
* on the management list, showing the standard confirmation first.
*
* @return void
*/
function layouts_form_actions() : void {
global $actions;
// ================= input validation =================
get_filter_request_var('drp_action', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '/^([a-zA-Z0-9_]+)$/']]);
// ====================================================
$user_id = isset($_SESSION['sess_user_id']) ? (int) $_SESSION['sess_user_id'] : 0;
if (isset_request_var('selected_items')) {
$selected_items = sanitize_unserialize_selected_items(get_nfilter_request_var('selected_items'));
if ($selected_items != false) {
$selected_items = array_values(array_map('intval', $selected_items));
switch (get_nfilter_request_var('drp_action')) {
case '1': // delete
foreach ($selected_items as $id) {
filter_layouts_delete($id);
}
break;
case '2': // make global (publish to all users)
foreach ($selected_items as $id) {
filter_layouts_publish($id);
}
break;
case '3': // make local to the current user
foreach ($selected_items as $id) {
filter_layouts_unpublish($id, $user_id);
}
break;
}
}
cacti_redirect('layouts.php');
}
$ilist = '';
$iarray = [];
foreach ($_POST as $var => $val) {
if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) {
// ==== input validation ====
input_validate_input_number($matches[1], 'chk[1]');
// ==========================
$ilist .= '<li>' . htmle(db_fetch_cell_prepared('SELECT name FROM user_layouts WHERE id = ?', [$matches[1]])) . '</li>';
$iarray[] = $matches[1];
}
}
$form_data = [
'general' => [
'page' => 'layouts.php',
'actions' => $actions,
'optvar' => 'drp_action',
'item_array' => $iarray,
'item_list' => $ilist
],
'options' => [
1 => [
'smessage' => __('Click \'Continue\' to Delete the following Layout.'),
'pmessage' => __('Click \'Continue\' to Delete the following Layouts.'),
'scont' => __('Delete Layout'),
'pcont' => __('Delete Layouts')
],
2 => [
'smessage' => __('Click \'Continue\' to make the following Layout Global to all users.'),
'pmessage' => __('Click \'Continue\' to make the following Layouts Global to all users.'),
'scont' => __('Make Layout Global'),
'pcont' => __('Make Layouts Global')
],
3 => [
'smessage' => __('Click \'Continue\' to make the following Layout Local to you.'),
'pmessage' => __('Click \'Continue\' to make the following Layouts Local to you.'),
'scont' => __('Make Layout Local'),
'pcont' => __('Make Layouts Local')
]
]
];
form_continue_confirmation($form_data);
}
/**
* Render the Layouts management list with the standard preset filter bar and a
* checkbox-driven actions dropdown. Administrators holding the Settings/Utilities
* realm manage every user's layouts; other users manage only their own.
*
* @return void
*/
function layouts_manage() : void {
global $actions;
$is_admin = filter_layouts_can_manage_global();
$user_id = isset($_SESSION['sess_user_id']) ? (int) $_SESSION['sess_user_id'] : 0;
$pageFilter = new CactiTableFilter(__('Layouts'), 'layouts.php', 'form_layouts', 'sess_layouts');
$pageFilter->rows_label = __('Layouts');
$pageFilter->render();
$rows = (grv('rows') == '-1') ? read_config_option('num_rows_table') : grv('rows');
$rows = (int) $rows;
$page = (int) grv('page');
$sql_where = '';
$sql_params = [];
if (grv('filter') != '') {
$sql_where = 'WHERE ul.name LIKE ?';
$sql_params[] = '%' . grv('filter') . '%';
}
if (!$is_admin) {
$sql_where .= ($sql_where != '' ? ' AND ' : 'WHERE ') . 'ul.user_id = ?';
$sql_params[] = $user_id;
}
$total_rows = db_fetch_cell_prepared("SELECT COUNT(*)
FROM user_layouts AS ul
$sql_where", $sql_params);
$sql_order = get_order_string();
$sql_limit = ' LIMIT ' . (($page - 1) * $rows) . ', ' . $rows;
$layouts = db_fetch_assoc_prepared("SELECT ul.id, ul.user_id, ul.page, ul.name, ul.data, ua.username
FROM user_layouts AS ul
LEFT JOIN user_auth AS ua
ON ua.id = ul.user_id
$sql_where
$sql_order
$sql_limit", $sql_params);
$nav = html_nav_bar('layouts.php?filter=' . grv('filter'), MAX_DISPLAY_PAGES, grv('page'), $rows, $total_rows, 5, __('Layouts'), 'page', 'main');
form_start('layouts.php', 'chk');
print $nav;
html_start_box('', '100%', false, 3, 'center', '');
$display_text = [
'name' => [
'display' => __('Name'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('The name of this Layout.')
],
'nosort' => [
'display' => __('Page'),
'align' => 'left',
'tip' => __('The page this Layout applies to.')
],
'nosort2' => [
'display' => __('Owner'),
'align' => 'left',
'tip' => __('The owner of this Layout, or Global when shared with all users.')
],
'nosort3' => [
'display' => __('Filter'),
'align' => 'left',
'tip' => __('The stored filter this Layout applies.')
]
];
html_header_sort_checkbox($display_text, grv('sort_column'), grv('sort_direction'), false);
if (cacti_sizeof($layouts)) {
foreach ($layouts as $layout) {
if ($layout['user_id'] == 0) {
$owner = __('Global');
} elseif (!empty($layout['username'])) {
$owner = html_escape($layout['username']);
} else {
$owner = __('User %d', $layout['user_id']);
}
$document = filter_layouts_decode($layout['data']);
$summary = $document !== false ? filter_layouts_document_url($document) : '';
if ($summary != '') {
$sep = (strpos($summary, '?') !== false) ? '&' : '?';
$edit_url = $summary . $sep . 'filter_layout=' . $layout['id'];
} else {
$edit_url = '';
}
form_alternate_row('line' . $layout['id'], true);
form_selectable_cell(filter_value($layout['name'], grv('filter'), $edit_url), $layout['id']);
form_selectable_cell(html_escape(layouts_page_name($layout['page'])), $layout['id']);
form_selectable_cell($owner, $layout['id']);
form_selectable_cell(html_escape($summary), $layout['id']);
form_checkbox_cell($layout['name'], $layout['id']);
form_end_row();
}
} else {
print "<tr class='tableRow odd'><td colspan='" . (cacti_sizeof($display_text) + 1) . "'><em>" . __('No Layouts Found') . '</em></td></tr>';
}
html_end_box(false);
if (cacti_sizeof($layouts)) {
print $nav;
}
draw_actions_dropdown($actions);
form_end();
}
/**
* Resolve a layout's stored page basename to the friendly name the user sees in
* the Console navigation (e.g. 'host.php' becomes 'Devices'), falling back to the
* raw basename when the page is not registered.
*
* @param string $page The stored page basename.
*
* @return string
*/
function layouts_page_name(string $page) : string {
global $navigation;
if (isset($navigation[$page . ':']['title']) && $navigation[$page . ':']['title'] != '') {
return $navigation[$page . ':']['title'];
}
return $page;
}