Repository navigation
Expand file tree
/
Copy pathtags.php
More file actions
1054 lines (890 loc) · 33.5 KB
/
Copy pathtags.php
File metadata and controls
1054 lines (890 loc) · 33.5 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/ |
+-------------------------------------------------------------------------+
*/
chdir('../../');
require_once('./include/auth.php');
/** @var array<string,mixed> $config */
require_once($config['base_path'] . '/plugins/tags/include/functions.php');
require($config['base_path'] . '/plugins/tags/include/arrays.php');
$tags_actions_menu = [
1 => __('Delete', 'tags'),
2 => __('Disable', 'tags'),
3 => __('Enable', 'tags'),
4 => __('Archive', 'tags'),
];
set_default_action();
switch (get_request_var('action')) {
case 'ajax_hosts':
get_allowed_ajax_hosts(false, false);
break;
case 'ajax_graphs':
if (!isempty_request_var('host_id')) {
$sql_where = 'WHERE host_id = ' . get_filter_request_var('host_id');
} else {
$sql_where = '';
}
get_allowed_ajax_graphs($sql_where);
break;
case 'ajax_sites':
if (!isempty_request_var('site_id')) {
$sql_where = 'WHERE id = ' . get_filter_request_var('site_id');
} else {
$sql_where = '';
}
tags_get_ajax_sites($sql_where);
break;
case 'save':
form_save();
break;
case 'actions':
form_actions();
break;
case 'edit':
top_header();
tags_edit();
bottom_footer();
break;
default:
top_header();
tags_list();
bottom_footer();
break;
}
/**
* Handles the bulk-actions form for the Tags list (delete/disable/enable/
* archive). On first display, renders the confirmation dialog listing the
* selected tags; once confirmed, applies the chosen action to each
* selected row in either the active or archive table. Invoked from this
* file's dispatcher when the request's 'action' is 'actions'.
*
* @return void Either redirects back to this page after applying the
* action, or prints the confirmation dialog and returns
* nothing.
*
* @global array $tags_actions_menu Map of bulk-action ids to their display
* labels, used to populate the actions
* dropdown/confirmation title.
*/
function form_actions(): void {
global $tags_actions_menu;
if (!in_array(get_filter_request_var('drp_action'), [1, 2, 3, 4], true)) {
header('Location: ' . htmlspecialchars(basename($_SERVER['PHP_SELF'])) . '?header=false');
exit;
}
$sql_table = 'plugin_tags_event';
if (get_filter_request_var('is_archive')) {
$sql_table = 'plugin_tags_event_archive';
}
// if we are to save this form, instead of display it
if (isset_request_var('selected_items')) {
$selected_items = sanitize_unserialize_selected_items(get_nfilter_request_var('selected_items'));
if ($selected_items != false) {
if (get_filter_request_var('drp_action') == 1) {
if (cacti_sizeof($selected_items)) {
foreach ($selected_items as $item) {
db_execute_prepared("DELETE FROM $sql_table WHERE id = ?", [$item]);
}
}
} elseif (get_filter_request_var('drp_action') == 2) { // disable
foreach ($selected_items as $item) {
db_execute_prepared("UPDATE $sql_table SET enabled = '' WHERE id = ?", [$item]);
}
} elseif (get_filter_request_var('drp_action') == 3) { // enable
foreach ($selected_items as $item) {
db_execute_prepared("UPDATE $sql_table SET enabled = 'on' WHERE id = ?", [$item]);
}
} elseif (get_filter_request_var('drp_action') == 4) { // archive
plugin_tags_move_old_events($selected_items);
}
}
header('Location: ' . htmlspecialchars(basename($_SERVER['PHP_SELF'])) . '?header=false');
exit;
}
// setup some variables
$item_list = '';
$items_array = [];
// loop through each of the graphs selected on the previous page and get more info about them
foreach ($_POST as $var => $val) {
if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) {
// ================= input validation =================
input_validate_input_number($matches[1]);
// ====================================================
$item_list .= '<li>' . db_fetch_cell_prepared("SELECT description FROM $sql_table WHERE id = ?", [$matches[1]]) . '</li>';
$items_array[] = $matches[1];
}
}
top_header();
form_start(htmlspecialchars(basename($_SERVER['PHP_SELF'])));
html_start_box($tags_actions_menu[get_filter_request_var('drp_action')], '60%', false, 3, 'center', '');
$save_html = '';
if (cacti_sizeof($items_array) > 0) {
if (get_filter_request_var('drp_action') == 1) {
print " <tr>
<td class='topBoxAlt'>
<p>" . __n('Click \'Continue\' to delete the following item.', 'Click \'Continue\' to delete following items.', cacti_sizeof($items_array)) . "</p>
<div class='itemlist'><ul>$item_list</ul></div>
</td>
</tr>";
$save_html = "<input class='cactiReturnTo' type='button' value='" . __esc('Cancel') . "'> <input type='submit' value='" . __esc('Continue') . "' title='" . __esc_n('Delete item', 'Delete items', cacti_sizeof($items_array)) . "'>";
} elseif (get_filter_request_var('drp_action') == 2) { // disable
print "><tr>
<td class='topBoxAlt'>
<p>" . __n('Click \'Continue\' to disable the following item.', 'Click \'Continue\' to delete following items.', cacti_sizeof($items_array)) . "</p>
<div class='itemlist'><ul>$item_list</ul></div>
</td>
</tr>";
$save_html = "<input class='cactiReturnTo' type='button' value='" . __esc('Cancel') . "'> <input type='submit' value='" . __esc('Continue') . "' title='" . __esc_n('Disable item', 'Disable items', cacti_sizeof($items_array)) . "'>";
} elseif (get_filter_request_var('drp_action') == 3) { // enable
print "><tr>
<td class='topBoxAlt'>
<p>" . __n('Click \'Continue\' to enable the following item.', 'Click \'Continue\' to delete following items.', cacti_sizeof($items_array)) . "</p>
<div class='itemlist'><ul>$item_list</ul></div>
</td>
</tr>";
$save_html = "<input class='cactiReturnTo' type='button' value='" . __esc('Cancel') . "'> <input type='submit' value='" . __esc('Continue') . "' title='" . __esc_n('Enable item', 'Enable items', cacti_sizeof($items_array)) . "'>";
} elseif (get_filter_request_var('drp_action') == 4) { // archive
print "><tr>
<td class='topBoxAlt'>
<p>" . __n('Click \'Continue\' to archive the following item.', 'Click \'Continue\' to archive following items.', cacti_sizeof($items_array)) . "</p>
<div class='itemlist'><ul>$item_list</ul></div>
</td>
</tr>";
$save_html = "<input class='cactiReturnTo' type='button' value='" . __esc('Cancel') . "'> <input type='submit' value='" . __esc('Continue') . "' title='" . __esc_n('Archive item', 'Archive items', cacti_sizeof($items_array)) . "'>";
}
} else {
raise_message(40);
header('Location: ' . htmlspecialchars(basename($_SERVER['PHP_SELF'])) . '?header=false');
exit;
}
print "<tr>
<td class='saveRow'>
<input type='hidden' name='action' value='actions'>
<input type='hidden' name='selected_items' value='" . serialize($items_array) . "'>
<input type='hidden' name='drp_action' value='" . get_filter_request_var('drp_action') . "'>
<input type='hidden' name='is_archive' value='" . (get_filter_request_var('is_archive') ? 1 : 0) . "'>
$save_html
</td>
</tr>";
html_end_box();
form_end();
bottom_footer();
}
/**
* Validates and saves a single tag (description, color, target scope, and
* associated device/graph/site) from the submitted edit form. Invoked from
* this file's dispatcher when the request's 'action' is 'save'.
*
* @return void Redirects back to this page; does not return a value.
*
* @global array $tags_colors The plugin's valid tag color palette, used to
* validate/default the submitted color.
* @global array $tags_target The plugin's valid target-scope values, used
* to validate the submitted target.
*/
function form_save(): void {
global $tags_colors, $tags_target;
if (isset_request_var('save_component')) {
$save['id'] = get_filter_request_var('id');
$save['description'] = form_input_validate(get_nfilter_request_var('description'), 'description', '', false, 3);
$available_colors = $tags_colors;
if (array_key_exists(get_nfilter_request_var('color'), $available_colors)) {
$save['color'] = get_nfilter_request_var('color');
} else {
$save['color'] = array_key_first($available_colors);
}
if (!array_key_exists(get_nfilter_request_var('target'), $tags_target)) {
$_SESSION['sess_error_fields']['target'] = 'target';
} else {
$save['target'] = get_nfilter_request_var('target');
if (get_nfilter_request_var('target') == 'all') {
$save['host_id'] = 0;
$save['graph_id'] = 0;
} elseif (get_nfilter_request_var('target') == 'device') {
$save['host_id'] = get_filter_request_var('host_id');
} elseif (get_nfilter_request_var('target') == 'graph') {
$host_id = db_fetch_cell_prepared('SELECT host_id FROM graph_local WHERE id = ?', [get_filter_request_var('graph_id')]);
if ($host_id > 0) {
$save['host_id'] = $host_id;
$save['graph_id'] = get_filter_request_var('graph_id');
} else {
$_SESSION['sess_error_fields']['graph_id'] = 'graph_id';
}
} elseif (get_nfilter_request_var('target') == 'site') {
$site_id = db_fetch_cell_prepared('SELECT id FROM sites WHERE id = ?', [get_filter_request_var('site_id')]);
if ($site_id > 0) {
$save['site_id'] = $site_id;
} else {
$_SESSION['sess_error_fields']['site_id'] = 'site_id';
}
}
}
$save['tag_time'] = strtotime(get_nfilter_request_var('tag_time'));
if (isset_request_var('enabled')) {
$save['enabled'] = 'on';
} else {
$save['enabled'] = '';
}
if ($save['id'] > 0 && isset($_SESSION['sess_error_fields']) && cacti_sizeof($_SESSION['sess_error_fields'])) {
foreach ($_SESSION['sess_error_fields'] as $item) {
unset($save[$item], $_SESSION['sess_error_fields'][$item]);
}
clear_messages();
}
if (!is_error_message()) {
$saved_id = sql_save($save, 'plugin_tags_event');
if ($saved_id) {
raise_message(1);
} else {
raise_message(2);
}
}
if (is_error_message()) {
header('Location: ' . htmlspecialchars(basename($_SERVER['PHP_SELF'])) . '?header=false&action=edit&id=' . get_nfilter_request_var('id'));
} else {
header('Location: ' . htmlspecialchars(basename($_SERVER['PHP_SELF'])) . '?header=false');
}
}
exit;
}
/**
* Renders the add/edit form for a single tag, pre-populating its fields
* (and resolving the display name of its associated device/graph/site)
* when editing an existing tag. Invoked from this file's dispatcher when
* the request's 'action' is 'edit'.
*
* @return void Outputs the edit form HTML directly.
*
* @global array $tags_fields The edit form's field definitions, populated
* here with the tag's current values.
* @global array $tags_colors The plugin's valid tag color palette, used to
* populate the color field's options.
*/
function tags_edit(): void {
global $tags_fields, $tags_colors;
// ================= input validation =================
get_filter_request_var('id');
// ====================================================
if (isset_request_var('id')) {
$data = db_fetch_row_prepared('SELECT *
FROM plugin_tags_event
WHERE id = ?',
[get_filter_request_var('id')]);
$data = is_array($data) ? $data : [];
$header_label = __('Tag [edit: %s]', $data['description'] ?? __('Not Found', 'tags'));
$tags_fields['color']['array'] = $tags_colors;
} else {
$data = [];
$header_label = __('Tag [new]');
$tags_fields['color']['array'] = $tags_colors;
}
$tags_fields['host_id']['id'] = isset($data['host_id']) ? $data['host_id'] : '';
if (isset($data['host_id']) && $data['host_id'] > 0) {
$tags_fields['host_id']['value'] = db_fetch_cell_prepared('SELECT description FROM host WHERE id = ?', [$data['host_id']]);
}
$tags_fields['graph_id']['id'] = isset($data['graph_id']) ? $data['graph_id'] : '';
if (isset($data['graph_id']) && $data['graph_id'] > 0) {
$tags_fields['graph_id']['value'] = db_fetch_cell_prepared('SELECT gt.name FROM graph_local AS gl
INNER JOIN graph_templates_graph AS gtg ON gl.id=gtg.local_graph_id
LEFT JOIN graph_templates AS gt ON gl.graph_template_id=gt.id WHERE gl.id = ?', [$data['graph_id']]);
}
$tags_fields['site_id']['id'] = isset($data['site_id']) ? $data['site_id'] : '';
if (isset($data['site_id']) && $data['site_id'] > 0) {
$tags_fields['site_id']['value'] = db_fetch_cell_prepared('SELECT name FROM sites WHERE id = ?', [$data['site_id']]);
}
// I don't know why it doesn't work in arrays.php
if (isset($data['tag_time']) && $data['tag_time'] > 0) {
$tags_fields['tag_time']['value'] = date('Y-m-d H:i',$data['tag_time']);
}
form_start(htmlspecialchars(basename($_SERVER['PHP_SELF'])));
html_start_box($header_label, '100%', true, 3, 'center', '');
draw_edit_form(
[
'config' => ['no_form_tag' => true],
'fields' => inject_form_variables($tags_fields, $data)
]
);
form_hidden_box('save_component', '1', '');
html_end_box(true, true);
form_save_button(htmlspecialchars(basename($_SERVER['PHP_SELF'])));
?>
<script type='text/javascript' <?php print plugin_tags_csp_nonce(); ?>>
var dateOpen = false;
$(function() {
setTag();
$('#tag_time').after('<i id="tagTime" class="calendar fa fa-calendar" title="<?php print __esc('Start Date/Time Selector', 'tags'); ?>"></i>');
$('#tagTime').click(function() {
if (dateOpen) {
dateOpen = false;
$('#tag_time').datetimepicker('hide');
} else {
dateOpen = true;
$('#tag_time').datetimepicker('show');
}
});
$('#tag_time').datetimepicker({
minuteGrid: 10,
stepMinute: 1,
showAnim: 'slideDown',
numberOfMonths: 1,
timeFormat: 'HH:mm',
dateFormat: 'yy-mm-dd',
showButtonPanel: false
});
});
function setTag() {
var target_type = $('#target').val();
$('#row_host_id').hide();
$('#row_graph_id').hide();
$('#row_site_id').hide();
if (target_type == 'device') {
$('#row_host_id').show();
}
if (target_type == 'graph') {
$('#row_graph_id').show();
}
if (target_type == 'site') {
$('#row_site_id').show();
}
}
</script>
<?php
}
/**
* Validates and persists (in session) the Tags list's filter request
* variables (timespan, rows, page, filter, sort, target, type). Called
* from tags_list() before the list is queried.
*
* @return void
*/
function request_validation(): void {
$filters = [
'tag_timespan' => [
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
],
'rows' => [
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
],
'page' => [
'filter' => FILTER_VALIDATE_INT,
'default' => '1'
],
'filter' => [
'filter' => FILTER_DEFAULT,
'pageset' => true,
'default' => ''
],
'sort_column' => [
'filter' => FILTER_CALLBACK,
'default' => 'tag_time',
'options' => ['options' => 'sanitize_search_string']
],
'sort_direction' => [
'filter' => FILTER_CALLBACK,
'default' => 'ASC',
'options' => ['options' => 'sanitize_search_string']
],
'target' => [
'filter' => FILTER_CALLBACK,
'options' => ['options' => 'sanitize_search_string'],
'default' => '-1'
],
'type' => [
'filter' => FILTER_CALLBACK,
'options' => ['options' => 'sanitize_search_string'],
'default' => '-1'
],
];
validate_store_request_vars($filters, 'sess_tags_event');
}
/**
* Renders the main Tags list page: validates the stored filters, draws the
* filter toolbar, queries the active (or archived) tag events matching the
* selected device/target/type/time-range filters, and prints the
* paginated, sortable results table. Invoked from this file's dispatcher
* for the default (no 'action') request.
*
* @return void|bool Outputs the list page HTML directly; returns true
* early (printing a configuration notice instead) when
* filtering by the 'primary' target with no primary
* device configured.
*
* @global array $config Cacti global configuration array;
* used to load timespan_settings.php.
* @global array $tags_actions_menu Map of bulk-action ids to their
* display labels, used to populate the
* actions dropdown.
* @global array $tags_colors The plugin's tag color palette, used
* to render each row's color swatch.
* @global array $tags_target Map of target-scope values to display
* labels, used to render each row's
* target column.
* @global array $tags_type Reserved/declared for parity with
* other list-page functions; not used
* directly here.
*/
function tags_list() {
global $config, $tags_actions_menu, $tags_colors, $tags_target, $tags_type;
$sql_table = 'plugin_tags_event';
if (get_filter_request_var('archive')) {
$sql_table = 'plugin_tags_event_archive';
}
include_once($config['base_path'] . '/lib/timespan_settings.php');
request_validation();
$sql_where = '';
if (get_filter_request_var('rows') == '-1') {
$rows = read_config_option('num_rows_table');
} else {
$rows = get_filter_request_var('rows');
}
if (get_filter_request_var('host_id') && get_filter_request_var('host_id') > 0) {
$sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . 'host_id = ' . get_filter_request_var('host_id');
}
if (get_nfilter_request_var('target') && in_array(get_nfilter_request_var('target'), array_keys($tags_target), true)) {
$sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . 'target = "' . get_nfilter_request_var('target') . '"';
}
if (get_nfilter_request_var('type') === 'automatic') {
$sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . "type LIKE 'auto\\_%'";
} elseif (get_nfilter_request_var('type') === 'manual') {
$sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . "type = 'manual'";
}
if (get_filter_request_var('tag_timespan') >= GT_CUSTOM) {
$graph_start = (int) get_current_graph_start();
$graph_end = (int) get_current_graph_end();
if ($graph_start > 0 && $graph_end >= $graph_start) {
$sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') .
"tag_time BETWEEN $graph_start AND $graph_end";
}
}
tags_filter();
$primary = (int) read_config_option('tags_primary_device');
if (get_nfilter_request_var('target') == 'primary' && $primary == 0) {
print __('You have to set primary device in Console - Configuration - Settings - Tags');
return true;
}
if (get_request_var('filter') != '') {
$sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . ' plugin_tags_event.description LIKE ' .
db_qstr('%' . get_nfilter_request_var('filter') . '%');
}
$sql_order = get_order_string();
$sql_limit = ' LIMIT ' . ($rows * (get_request_var('page') - 1)) . ',' . $rows;
$total_rows = db_fetch_cell("SELECT COUNT(id)
FROM $sql_table
$sql_where");
// outer is used for target=all
$result = db_fetch_assoc("SELECT $sql_table.*, host.description AS host_description
FROM $sql_table
LEFT OUTER JOIN host
ON host.id = $sql_table.host_id
$sql_where
$sql_order
$sql_limit");
$display_text = [
'description' => [
'display' => __('Description', 'tags'),
'sort' => 'ASC'
],
'tag_time' => [
'display' => __('Tag time', 'tags'),
'sort' => 'ASC'
],
'target' => [
'display' => __('Target', 'tags'),
'sort' => 'ASC'
],
'host_id' => [
'display' => __('Device', 'tags'),
'sort' => 'ASC'
],
'graph_id' => [
'display' => __('Graph', 'tags'),
'sort' => 'ASC'
],
'site_id' => [
'display' => __('Site', 'tags'),
'sort' => 'ASC'
],
'automatic' => [
'display' => __('Automatic', 'tags'),
'sort' => 'ASC'
],
];
$columns = cacti_sizeof($display_text);
$nav = html_nav_bar(htmlspecialchars(basename($_SERVER['PHP_SELF'])) . '?filter=' . get_request_var('filter') . '&archive=' . get_filter_request_var('archive'), MAX_DISPLAY_PAGES, get_request_var('page'), $rows, $total_rows, $columns, __('Tags', 'tags'), 'page', 'main');
form_start(htmlspecialchars(basename($_SERVER['PHP_SELF'])), 'chk');
print $nav;
html_start_box('', '100%', false, 3, 'center', '');
html_header_sort_checkbox($display_text, get_request_var('sort_column'), get_request_var('sort_direction'), false);
if (cacti_sizeof($result)) {
foreach ($result as $row) {
if (array_key_exists($row['color'], $tags_colors)) {
$color = $tags_colors[$row['color']];
} else {
$color = '999999';
}
$row['host_id'] = $row['host_id'] > 0 ? $row['host_id'] : '-';
$row['graph_id'] = $row['graph_id'] > 0 ? $row['graph_id'] : '-';
$row['site_id'] = $row['site_id'] > 0 ? $row['site_id'] : '-';
$row['type'] = tags_is_automatic_type($row['type']) ? __('Yes', 'tags') : __('No', 'tags');
form_alternate_row('line' . $row['id'], false, $row['enabled']);
form_selectable_cell("<span class='color-box' style='--tagcolor: " . htmlspecialchars($color) . "'></span><a class='linkEditMain' href='" . html_escape(htmlspecialchars(basename($_SERVER['PHP_SELF'])) . '?header=false&action=edit&id=' . $row['id']) . "'>" . $row['description'] . '</a>', $row['id']);
form_selectable_cell(date('Y-m-d H:i', $row['tag_time']), $row['id']);
form_selectable_cell($tags_target[$row['target']], $row['id']);
form_selectable_cell($row['host_description'], $row['id']);
form_selectable_cell($row['graph_id'], $row['id']);
form_selectable_cell($row['site_id'], $row['id']);
form_selectable_cell($row['type'], $row['id']);
form_checkbox_cell($row['description'], $row['id']);
form_end_row();
}
} else {
print "<tr class='tableRow'><td colspan='" . $columns . "'><em>" . __('Empty', 'tags') . "</em></td></tr>\n";
}
html_end_box(false);
if (cacti_sizeof($result)) {
print $nav;
}
if (get_filter_request_var('archive')) {
$a = array_slice($tags_actions_menu, 0, 1, true);
draw_actions_dropdown($a, 1);
} else {
draw_actions_dropdown($tags_actions_menu, 1);
}
print "<input type='hidden' name='is_archive' value='" . (get_nfilter_request_var('archive') ? 1 : 0) . "'>";
form_end();
}
/**
* Draws the Tags list's filter toolbar (search box, target/type/timespan
* selectors, rows-per-page selector) and the client-side JS/date pickers
* that drive it. Called from tags_list() before the list table itself is
* rendered.
*
* @return void Outputs HTML and JavaScript directly.
*
* @global array $item_rows Rows-per-page options offered by Cacti
* core, used to populate the 'rows'
* select list.
* @global array $tags_target Map of target-scope values to display
* labels, used to populate the target
* filter.
* @global array $tags_type Map of type values to display labels,
* used to populate the type filter.
* @global array $graph_timespans Cacti's predefined graph timespan
* options, used to populate the timespan
* selector.
*/
function tags_filter(): void {
global $item_rows, $tags_target, $tags_type, $graph_timespans;
if (get_filter_request_var('host_id')) {
$host_id = get_filter_request_var('host_id');
} elseif (isset($_SESSION['plugin_uptime_host_id'])) {
$host_id = $_SESSION['plugin_uptime_host_id'];
} else {
$host_id = -1;
}
html_start_box(__('Tags Management', 'tags') , '100%', false, 3, 'center', htmlspecialchars(basename($_SERVER['PHP_SELF'])) . '?action=edit');
?>
<tr class='even'>
<td>
<form id='form_tags_item' action='<?php print htmlspecialchars(basename($_SERVER['PHP_SELF'])); ?>'>
<table class='filterTable'>
<tr>
<?php
html_host_filter($host_id, 'applyFilter', '', false, true);
?>
<td>
<?php print __('Search', 'tags'); ?>
</td>
<td>
<input type='text' class='ui-state-default ui-corner-all' id='filter' name='filter' size='20' value='<?php print html_escape_request_var('filter'); ?>'>
</td>
<td>
<?php print __('Target', 'tags'); ?>
</td>
<td>
<select id='target'>
<?php
print "<option value='-1'" . (get_request_var('rows') == -1 ? ' selected' : '') . '>' . __('Any', 'tags') . '</option>';
if (cacti_sizeof($tags_target)) {
foreach ($tags_target as $key => $value) {
print "<option value='" . $key . "'";
if (get_request_var('target') == $key) {
print ' selected';
} print '>' . htmlspecialchars($value) . '</option>';
}
}
?>
</select>
</td>
<td>
<?php print __('Type', 'tags'); ?>
</td>
<td>
<select id='type'>
<?php
print "<option value='-1'" . (get_request_var('rows') == -1 ? ' selected' : '') . '>' . __('Any', 'tags') . '</option>';
if (cacti_sizeof($tags_type)) {
foreach ($tags_type as $key => $value) {
print "<option value='" . $key . "'";
if (get_request_var('type') == $key) {
print ' selected';
} print '>' . htmlspecialchars($value) . '</option>';
}
}
?>
</select>
</td>
<td>
<?php print __('Tags', 'tags'); ?>
</td>
<td>
<select id='rows'>
<?php
print "<option value='-1'" . (get_request_var('rows') == -1 ? ' selected' : '') . '>' . __('Default', 'tags') . '</option>';
if (cacti_sizeof($item_rows)) {
foreach ($item_rows as $key => $value) {
print "<option value='" . $key . "'";
if (get_request_var('rows') == $key) {
print ' selected';
} print '>' . htmlspecialchars($value) . '</option>';
}
}
?>
</select>
</td>
<td>
<input id="archive" type="checkbox" name="archive" value="1" class="ui-state-default ui-corner-all"
<?php print(get_filter_request_var('archive') ? 'checked="checked"' : ''); ?>> Archive
</td>
<td>
<span class='nowrap'>
<input type='button' class='ui-button ui-corner-all ui-widget' id='refresh' value='<?php print __esc('Go'); ?>' title='<?php print __esc('Set/Refresh Filters', 'tags'); ?>'>
<input type='button' class='ui-button ui-corner-all ui-widget' id='clear' value='<?php print __esc('Clear'); ?>' title='<?php print __esc('Clear Filters', 'tags'); ?>'>
</span>
</td>
</tr>
</table>
</td>
</tr>
<tr class='even noprint'>
<td class='noprint'>
<table class='filterTable'>
<tr id='timespan'>
<td>
<?php print __('Presets'); ?>
</td>
<td>
<select id='predefined_timespan'>
<?php
$display_timespans = [
-1 => __('Any'),
GT_CUSTOM => __('Custom'),
] + $graph_timespans;
$start_val = 0;
$end_val = cacti_sizeof($display_timespans);
if (cacti_sizeof($display_timespans)) {
foreach ($display_timespans as $value => $text) {
print "<option value='$value'";
if (get_filter_request_var('tag_timespan') == $value) {
print ' selected';
} print '>' . html_escape($text) . '</option>';
}
}
?>
</select>
</td>
<td>
<?php print __('From'); ?>
</td>
<td>
<span>
<input type='text' class='ui-state-default ui-corner-all' id='date1' size='18' value='<?php print(isset($_SESSION['sess_current_date1']) ? $_SESSION['sess_current_date1'] : ''); ?>'>
<i id='startDate' class='calendar fa fa-calendar' title='<?php print __esc('Start Date Selector'); ?>'></i>
</span>
</td>
<td>
<?php print __('To'); ?>
</td>
<td>
<span>
<input type='text' class='ui-state-default ui-corner-all' id='date2' size='18' value='<?php print(isset($_SESSION['sess_current_date2']) ? $_SESSION['sess_current_date2'] : ''); ?>'>
<i id='endDate' class='calendar fa fa-calendar' title='<?php print __esc('End Date Selector'); ?>'></i>
</span>
</td>
</tr>
</table>
</td>
</tr>
<?php
html_end_box();
?>
</form>
<script type='text/javascript' <?php print plugin_tags_csp_nonce(); ?>>
var graph_start = <?php print get_current_graph_start(); ?>;
var graph_end = <?php print get_current_graph_end(); ?>;
var graphPage = 'tags.php';
var date1Open = false;
var date2Open = false;
function setCustomTimespan() {
var preset = $('#predefined_timespan');
preset.val(<?php print GT_CUSTOM; ?>);
if (preset.data('ui-selectmenu')) {
preset.selectmenu('refresh');
}
}
function applyFilter() {
if ($('#archive').is(':checked')) {
var archive = 1;
} else {
var archive = 0;
}
strURL = '<?php print htmlspecialchars(basename($_SERVER['PHP_SELF'])); ?>?header=false';
strURL += '&host_id=' + $('#host_id').val();
strURL += '&filter=' + $('#filter').val();
strURL += '&rows=' + $('#rows').val();
strURL += '&target=' + $('#target').val();
strURL += '&archive=' + archive;
strURL += '&type=' + $('#type').val();
strURL += '&tag_timespan=' + $('#predefined_timespan').val();
if ($('#predefined_timespan').val() >= 0) {
strURL += '&predefined_timespan=' + $('#predefined_timespan').val();
if ($('#predefined_timespan').val() == <?php print GT_CUSTOM; ?>) {
strURL += '&date1=' + encodeURIComponent($('#date1').val());
strURL += '&date2=' + encodeURIComponent($('#date2').val());
}
}
loadPageNoHeader(strURL);
}
function clearFilter() {
strURL = '<?php print htmlspecialchars(basename($_SERVER['PHP_SELF'])); ?>?clear=1&header=false';
loadPageNoHeader(strURL);
}
$(function() {
$('#rows').change(function() {
applyFilter();
});
$('#target').change(function() {
applyFilter();
});
$('#type').change(function() {
applyFilter();
});
$('#archive').click(function() {
applyFilter();
});
$('#refresh').click(function() {
applyFilter();
});
$('#clear').click(function() {
clearFilter();
});
$('#form_tags_item').submit(function(event) {
event.preventDefault();
applyFilter();
});
$('#startDate').on('click', function() {
if (date1Open) {
date1Open = false;
$('#date1').datetimepicker('hide');
} else {
date1Open = true;
$('#date1').datetimepicker('show');
}
});
$('#endDate').on('click', function() {
if (date2Open) {
date2Open = false;
$('#date2').datetimepicker('hide');
} else {
date2Open = true;
$('#date2').datetimepicker('show');
}
});
$('#date1').datetimepicker({
minuteGrid: 10,
stepMinute: 1,
showAnim: 'slideDown',
numberOfMonths: 1,
timeFormat: 'HH:mm',
dateFormat: 'yy-mm-dd',
showButtonPanel: false
});
$('#date2').datetimepicker({
minuteGrid: 10,
stepMinute: 1,
showAnim: 'slideDown',
numberOfMonths: 1,
timeFormat: 'HH:mm',
dateFormat: 'yy-mm-dd',
showButtonPanel: false
});
$('#date1, #date2').on('change', function() {
setCustomTimespan();
});