-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgexport.php
More file actions
1091 lines (936 loc) · 38.8 KB
/
Copy pathgexport.php
File metadata and controls
1091 lines (936 loc) · 38.8 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('./include/auth.php');
require_once('./plugins/gexport/includes/functions.php');
require_once('./plugins/gexport/includes/gexport_security.php');
$export_actions = [
'1' => __('Delete', 'gexport'),
'2' => __('Enable', 'gexport'),
'3' => __('Disable', 'gexport'),
'4' => __('Export Now', 'gexport')
];
$export_timing = [
__('Periodic', 'gexport'),
__('Daily', 'gexport'),
__('Hourly', 'gexport')
];
set_default_action();
switch (get_request_var('action')) {
case 'save':
export_form_save();
break;
case 'actions':
export_form_actions();
break;
case 'edit':
top_header();
export_edit();
bottom_footer();
break;
default:
top_header();
gexport();
bottom_footer();
break;
}
/* --------------------------
The Save Function
-------------------------- */
/**
* Validates and saves a single Graph Export configuration (type, enabled
* flag, presentation, theme, tree/site selection, graph sizing/paging,
* timing, and remote delivery/SCP credentials) from the submitted edit
* form, computing its next scheduled run time. Invoked from this file's
* dispatcher when the request's 'action' is 'save'.
*
* @return void Redirects back to the edit form for this export
* definition; does not return a value.
*/
function export_form_save() {
if (isset_request_var('save_component_export')) {
$save['id'] = get_filter_request_var('id');
$save['name'] = form_input_validate(get_nfilter_request_var('name'), 'name', '', false, 3);
$save['export_type'] = form_input_validate(get_nfilter_request_var('export_type'), 'export_type', '^local|ftp|scp|sftp|rsync$', false, 3);
$save['enabled'] = isset_request_var('enabled') ? 'on' : '';
$save['export_presentation'] = form_input_validate(get_nfilter_request_var('export_presentation'), 'export_presentation', '^preview|tree$', false, 3);
$save['export_effective_user'] = form_input_validate(get_nfilter_request_var('export_effective_user'), 'export_effective_user', '', true, 3);
$save['export_expand_hosts'] = form_input_validate(get_nfilter_request_var('export_expand_hosts'), 'export_expand_hosts', '^on|off$', false, 3);
$save['export_theme'] = form_input_validate(get_nfilter_request_var('export_theme'), 'export_theme', '', false, 3);
if (isset_request_var('graph_tree')) {
$save['graph_tree'] = form_input_validate(implode(',', get_nfilter_request_var('graph_tree')), 'graph_tree', '', true, 3);
} else {
$save['graph_tree'] = '';
}
if (isset_request_var('graph_site')) {
$save['graph_site'] = form_input_validate(implode(',', get_nfilter_request_var('graph_site')), 'graph_site', '', true, 3);
} else {
$save['graph_site'] = '';
}
$save['graph_width'] = form_input_validate(get_nfilter_request_var('graph_width'), 'graph_width', '^[0-9]+$', false, 3);
$save['graph_height'] = form_input_validate(get_nfilter_request_var('graph_height'), 'graph_height', '^[0-9]+$', false, 3);
$save['graph_thumbnails'] = isset_request_var('graph_thumbnails') ? 'on' : '';
$save['graph_perpage'] = form_input_validate(get_nfilter_request_var('graph_perpage'), 'graph_perpage', '^[0-9]+$', false, 3);
$save['graph_columns'] = form_input_validate(get_nfilter_request_var('graph_columns'), 'graph_columns', '^[0-9]+$', false, 3);
$save['graph_max'] = form_input_validate(get_nfilter_request_var('graph_max'), 'graph_max', '^[0-9]+$', false, 3);
$save['export_args'] = form_input_validate(get_nfilter_request_var('export_args'), 'export_args', '', false, 3);
$save['export_clear'] = isset_request_var('export_clear') ? 'on' : '';
$save['export_thumbs'] = isset_request_var('export_thumbs') ? 'on' : '';
$save['export_directory'] = form_input_validate(get_nfilter_request_var('export_directory'), 'export_directory', '', false, 3);
$save['export_temp_directory'] = form_input_validate(get_nfilter_request_var('export_temp_directory'), 'export_temp_directory', '', false, 3);
$save['export_timing'] = form_input_validate(get_nfilter_request_var('export_timing'), 'export_timing', '^periodic|hourly|daily$', false, 3);
$save['export_threads'] = form_input_validate(get_nfilter_request_var('export_threads'), 'export_threads', '^[0-9]+$', false, 3);
$save['export_skip'] = form_input_validate(get_nfilter_request_var('export_skip'), 'export_skip', '^[0-9]+$', false, 3);
$save['export_hourly'] = form_input_validate(get_nfilter_request_var('export_hourly'), 'export_hourly', '^[0-9]+$', false, 3);
$save['export_daily'] = form_input_validate(get_nfilter_request_var('export_daily'), 'export_daily', '^[0-9]+:[0-9]+$', false, 3);
$save['export_sanitize_remote'] = isset_request_var('export_sanitize_remote') ? 'on' : '';
$save['export_host'] = form_input_validate(get_nfilter_request_var('export_host'), 'export_host', '', true, 3);
$save['export_port'] = form_input_validate(get_nfilter_request_var('export_port'), 'export_port', '^[0-9]+$', true, 3);
$save['export_passive'] = isset_request_var('export_passive') ? 'on' : '';
$save['export_user'] = form_input_validate(get_nfilter_request_var('export_user'), 'export_user', '', true, 3);
$save['export_password'] = form_input_validate(get_nfilter_request_var('export_password'), 'export_password', '', true, 3);
$save['export_private_key_path'] = form_input_validate(get_nfilter_request_var('export_private_key_path'), 'export_private_key_path', '', true, 3);
// determine the start time
$next_start = gexport_calc_next_start($save);
$save['next_start'] = $next_start;
if (!is_error_message()) {
$export_id = sql_save($save, 'graph_exports');
if ($export_id) {
raise_message(1);
} else {
raise_message(2);
}
}
header('Location: gexport.php?action=edit&header=false&id=' . (empty($export_id) ? get_request_var('id') : $export_id));
}
}
/**
* Creates a copy of an existing Graph Export configuration under a new
* name (substituting the '<export_title>' placeholder with the original
* name), copying every non-hidden field. Currently unused/dead code: not
* called from anywhere else in this file (no 'duplicate' bulk action is
* wired up), and its final sql_save() call also targets a non-existent
* 'export' table rather than 'graph_exports'.
*
* @param int $_export_id The graph_exports.id to duplicate.
* @param string $export_title The new name pattern, with
* '<export_title>' replaced by the original
* export's name.
*
* @return void
*
* @global array $fields_export_edit The Export edit form's field
* definitions, used to determine which
* fields are copyable (non-hidden).
*/
function duplicate_export($_export_id, $export_title) {
global $fields_export_edit;
$export = db_fetch_row_prepared('SELECT * FROM graph_exports WHERE id = ?', [$_export_id]);
$export = is_array($export) ? $export : [];
if (empty($export)) {
return;
}
// substitute the title variable
$export['name'] = str_replace('<export_title>', $export['name'], $export_title);
// create new entry: device_template
$save['id'] = 0;
reset($fields_export_edit);
foreach ($fields_export_edit as $field => $array) {
if (!preg_match('/^hidden/', $array['method'])) {
$save[$field] = $export[$field];
}
}
$export_id = sql_save($save, 'export');
}
/* ------------------------
The 'actions' function
------------------------ */
/**
* Handles the bulk-actions form for the Graph Exports list (delete/
* enable/disable/export-now). On first display, renders the
* confirmation dialog listing the selected export definitions; once
* confirmed, applies the chosen action to each selected row. 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 $export_actions Map of bulk-action ids to their display
* labels, used for the confirmation
* dialog title.
*/
function export_form_actions() {
global $export_actions;
$bulk_action = gexport_normalize_bulk_action(get_nfilter_request_var('drp_action'));
// 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 ($bulk_action === '1') { // delete
// do a referential integrity check
if (sizeof($selected_items)) {
foreach ($selected_items as $export_id) {
// ================= input validation =================
input_validate_input_number($export_id);
// ====================================================
$export_ids[] = $export_id;
}
}
if (isset($export_ids)) {
db_execute('DELETE FROM graph_exports WHERE ' . array_to_sql_or($export_ids, 'id'));
}
} elseif ($bulk_action === '2') { // enable
for ($i = 0; ($i < count($selected_items)); $i++) {
// ================= input validation =================
input_validate_input_number($selected_items[$i]);
// ====================================================
export_enable($selected_items[$i]);
}
} elseif ($bulk_action === '3') { // disable
for ($i = 0; ($i < count($selected_items)); $i++) {
// ================= input validation =================
input_validate_input_number($selected_items[$i]);
// ====================================================
export_disable($selected_items[$i]);
}
} elseif ($bulk_action === '4') { // run now
for ($i = 0; ($i < count($selected_items)); $i++) {
// ================= input validation =================
input_validate_input_number($selected_items[$i]);
// ====================================================
export_runnow($selected_items[$i]);
}
header('Location: gexport.php?header=false&refresh=20');
exit;
}
}
header('Location: gexport.php?header=false');
exit;
}
// setup some variables
$export_list = '';
// 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]);
// ====================================================
$export_list .= '<li>' . db_fetch_cell_prepared('SELECT name FROM graph_exports WHERE id = ?', [$matches[1]]) . '</li>';
$export_array[] = $matches[1];
}
}
top_header();
form_start('gexport.php', 'export_actions');
html_start_box(isset($export_actions[$bulk_action]) ? $export_actions[$bulk_action] : '', '60%', false, 3, 'center', '');
if (isset($export_array)) {
if ($bulk_action === '1') { // delete
print " <tr>
<td class='topBoxAlt'>
<p>" . __n('Click \'Continue\' to delete the following Graph Export Definition.', 'Click \'Continue\' to delete following Graph Export Definitions.', sizeof($export_array), 'gexport') . "</p>
<div class='itemlist'><ul>$export_list</ul></div>
</td>
</tr>";
$save_html = "<button type='button' class='ui-button ui-corner-all ui-widget' onClick='cactiReturnTo()'>" . __esc('Cancel', 'gexport') . "</button>
<button type='submit' class='ui-button ui-corner-all ui-widget ui-state-active' title='" . __esc('Delete Graph Export Definition(s)', 'gexport') . "'>" . __esc('Continue', 'gexport') . '</button>';
} elseif ($bulk_action === '2') { // disable
print " <tr>
<td class='topBoxAlt'>
<p>" . __n('Click \'Continue\' to disable the following Graph Export Definition.', 'Click \'Continue\' to disable following Graph Export Definitions.', sizeof($export_array), 'gexport') . "</p>
<div class='itemlist'><ul>$export_list</ul></div>
</td>
</tr>";
$save_html = "<button type='button' class='ui-button ui-corner-all ui-widget' onClick='cactiReturnTo()'>" . __esc('Cancel', 'gexport') . "</button>
<button type='submit' class='ui-button ui-corner-all ui-widget ui-state-active' title='" . __esc('Disable Graph Export Definition(s)', 'gexport') . "'>" . __esc('Continue', 'gexport') . '</button>';
} elseif ($bulk_action === '3') { // enable
print " <tr>
<td class='topBoxAlt'>
<p>" . __n('Click \'Continue\' to enable the following Graph Export Definition.', 'Click \'Continue\' to enable following Graph Export Definitions.', sizeof($export_array), 'gexport') . "</p>
<div class='itemlist'><ul>$export_list</ul></div>
</td>
</tr>";
$save_html = "<button type='button' class='ui-button ui-corner-all ui-widget' onClick='cactiReturnTo()'>" . __esc('Cancel', 'gexport') . "</button>
<input type='submit' class='ui-button ui-corner-all ui-widget ui-state-active' title='" . __esc('Enable Graph Export Definition(s)', 'gexport') . "'>" . __esc('Continue', 'gexport') . '</button>';
} elseif ($bulk_action === '4') { // export now
print "<tr>
<td class='topBoxAlt'>
<p>" . __n('Click \'Continue\' to run the following Graph Export Definition now.', 'Click \'Continue\' to run following Graph Export Definitions now.', sizeof($export_array)) . "</p>
<div class='itemlist'><ul>$export_list</ul></div>
</td>
</tr>";
$save_html = "<button type='button' class='ui-button ui-corner-all ui-widget' onClick='cactiReturnTo()'>" . __esc('Cancel', 'gexport') . "</button>
<button type='submit' class='ui-button ui-corner-all ui-widget ui-state-active' title='" . __esc('Run Graph Export Definition(s) Now', 'gexport') . "'>" . __esc('Continue', 'gexport') . '</button>';
} else {
print "<tr><td class='odd'><span class='textError'>" . __('You must select a valid action.', 'gexport') . '</span></td></tr>';
$save_html = "<button type='button' class='ui-button ui-corner-all ui-widget' onClick='cactiReturnTo()'>" . __esc('Return', 'gexport') . '</button>';
}
} else {
print "<tr><td class='odd'><span class='textError'>" . __('You must select at least one Graph Export Definition.', 'gexport') . '</span></td></tr>';
$save_html = "<button type='button' class='ui-button ui-corner-all ui-widget' onClick='cactiReturnTo()'>" . __esc('Return', 'gexport') . '</button>';
}
print "<tr>
<td class='saveRow'>
<input type='hidden' name='action' value='actions'>
<input type='hidden' name='selected_items' value='" . (isset($export_array) ? serialize($export_array) : '') . "'>
<input type='hidden' name='drp_action' value='" . html_escape($bulk_action) . "'>
$save_html
</td>
</tr>";
html_end_box();
form_end();
bottom_footer();
}
/* ---------------------
Graph Export Functions
--------------------- */
/**
* Marks a single Graph Export configuration as enabled. Called from
* export_form_actions() for each export selected for the 'Enable' bulk
* action.
*
* @param int $export_id The graph_exports.id to enable.
*
* @return void
*/
function export_enable($export_id) {
db_execute_prepared('UPDATE graph_exports SET enabled="on" WHERE id = ?', [$export_id]);
}
/**
* Marks a single Graph Export configuration as disabled. Called from
* export_form_actions() for each export selected for the 'Disable' bulk
* action.
*
* @param int $export_id The graph_exports.id to disable.
*
* @return void
*/
function export_disable($export_id) {
db_execute_prepared('UPDATE graph_exports SET enabled="" WHERE id = ?', [$export_id]);
}
/**
* Immediately launches a background poller_export.php process to run a
* single enabled, non-currently-running export configuration, bypassing
* its normal schedule. Called from export_form_actions() for each export
* selected for the 'Export Now' bulk action.
*
* @param int $export_id The graph_exports.id to run immediately.
*
* @return void
*
* @global array $config Cacti global configuration array; used to locate
* the PHP binary and this plugin's poller script.
*/
function export_runnow($export_id) {
global $config;
require_once('./lib/poller.php');
$status = db_fetch_row_prepared('SELECT status, enabled FROM graph_exports WHERE id = ?', [$export_id]);
$status = is_array($status) ? $status : [];
if (empty($status)) {
return;
}
if (($status['status'] == 0 || $status['status'] == 2) && $status['enabled'] == 'on') {
$command_string = read_config_option('path_php_binary');
$extra_args = '-q "' . $config['base_path'] . '/plugins/gexport/poller_export.php" --id=' . $export_id . ' --force';
exec_background($command_string, $extra_args);
sleep(2);
}
}
/**
* Renders the add/edit form for a single Graph Export configuration,
* pre-populating its fields when editing an existing export. Invoked
* from this file's dispatcher when the request's 'action' is 'edit'.
*
* @return void Outputs the edit form HTML directly.
*
* @global array $fields_export_edit The edit form's field definitions
* (populated by gexport_config_arrays()),
* filled in here with the export's
* current values.
*/
function export_edit() {
global $fields_export_edit;
// ================= input validation =================
get_filter_request_var('id');
// ====================================================
if (!isempty_request_var('id')) {
$export = db_fetch_row_prepared('SELECT * FROM graph_exports WHERE id = ?', [get_request_var('id')]);
$export = is_array($export) ? $export : [];
if (empty($export)) {
$header_label = __('Graph Export Definition [new]', 'gexport');
} else {
$header_label = __('Graph Export Definition [edit: %s]', $export['name'], 'gexport');
}
} else {
$header_label = __('Graph Export Definition [new]', 'gexport');
}
form_start('gexport.php', 'export_edit');
html_start_box($header_label, '100%', false, 3, 'center', '');
draw_edit_form(
[
'config' => ['no_form_tag' => true],
'fields' => inject_form_variables($fields_export_edit, (isset($export) ? $export : []))
]
);
html_end_box();
form_hidden_box('id', (isset($export['id']) ? $export['id'] : '0'), '');
form_hidden_box('save_component_export', '1', '');
form_save_button('gexport.php', 'return');
?>
<script type='text/javascript' <?php print plugin_gexport_csp_nonce(); ?>>
$(function() {
$('#graph_tree').multiselect({
noneSelectedText: '<?php print __('Select Tree(s)', 'gexport'); ?>',
selectedText: function(numChecked, numTotal, checkedItems) {
myReturn = numChecked + ' <?php print __('Trees Selected', 'gexport'); ?>';
$.each(checkedItems, function(index, value) {
if (value.value == '0') {
myReturn='<?php print __('All Trees Selected', 'gexport'); ?>';
return false;
}
});
return myReturn;
},
checkAllText: '<?php print __('All', 'gexport'); ?>',
uncheckAllText: '<?php print __('None', 'gexport'); ?>',
uncheckall: function() {
$(this).multiselect('widget').find(':checkbox:first').each(function() {
$(this).prop('checked', true);
});
},
click: function(event, ui) {
checked=$(this).multiselect('widget').find('input:checked').length;
if (ui.value == '0') {
if (ui.checked == true) {
$('#graph_tree').multiselect('uncheckAll');
$(this).multiselect('widget').find(':checkbox:first').each(function() {
$(this).prop('checked', true);
});
}
}else if (checked == 0) {
$(this).multiselect('widget').find(':checkbox:first').each(function() {
$(this).click();
});
}else if ($(this).multiselect('widget').find('input:checked:first').val() == '0') {
if (checked > 0) {
$(this).multiselect('widget').find(':checkbox:first').each(function() {
$(this).click();
$(this).prop('disable', true);
});
}
}
}
}).multiselectfilter( {
label: '<?php print __('Search', 'gexport'); ?>', width: '150'
});
$('#graph_site').multiselect({
noneSelectedText: '<?php print __('Select Site(s)', 'gexport'); ?>',
selectedText: function(numChecked, numTotal, checkedItems) {
myReturn = numChecked + ' <?php print __('Sites Selected', 'gexport'); ?>';
$.each(checkedItems, function(index, value) {
if (value.value == '0') {
myReturn='<?php print __('All Sites Selected', 'gexport'); ?>';
return false;
}
});
return myReturn;
},
checkAllText: '<?php print __('All', 'gexport'); ?>',
uncheckAllText: '<?php print __('None', 'gexport'); ?>',
uncheckall: function() {
$(this).multiselect('widget').find(':checkbox:first').each(function() {
$(this).prop('checked', true);
});
},
click: function(event, ui) {
checked=$(this).multiselect('widget').find('input:checked').length;
if (ui.value == '0') {
if (ui.checked == true) {
$('#graph_site').multiselect('uncheckAll');
$(this).multiselect('widget').find(':checkbox:first').each(function() {
$(this).prop('checked', true);
});
}
}else if (checked == 0) {
$(this).multiselect('widget').find(':checkbox:first').each(function() {
$(this).click();
});
}else if ($(this).multiselect('widget').find('input:checked:first').val() == '0') {
if (checked > 0) {
$(this).multiselect('widget').find(':checkbox:first').each(function() {
$(this).click();
$(this).prop('disable', true);
});
}
}
}
}).multiselectfilter( {
label: '<?php print __('Search', 'gexport'); ?>', width: '150'
});
setRemoteVisibility();
setTimingVisibility();
setTreeVisibility();
$('#export_type').change(function() {
setRemoteVisibility();
});
$('#export_timing').change(function() {
setTimingVisibility();
});
$('#export_presentation').change(function() {
setTreeVisibility();
});
});
function setTimingVisibility() {
if ($('#export_timing').val() == 'periodic') {
$('#row_export_skip').show();
$('#row_export_hourly').hide();
$('#row_export_daily').hide();
}else if ($('#export_timing').val() == 'hourly') {
$('#row_export_skip').hide();
$('#row_export_hourly').show();
$('#row_export_daily').hide();
}else if ($('#export_timing').val() == 'daily') {
$('#row_export_skip').hide();
$('#row_export_hourly').hide();
$('#row_export_daily').show();
} else {
$('#row_export_skip').hide();
$('#row_export_hourly').hide();
$('#row_export_daily').hide();
}
}
function setRemoteVisibility() {
if ($('#export_type').val() != 'local') {
$('#row_export_temp_directory').show();
$('#row_export_hdr_remote').show();
$('#row_export_sanitize_remote').show();
$('#row_export_host').show();
$('#row_export_port').show();
$('#row_export_user').show();
if ($('#export_type').val() == 'sftp' || $('#export_type').val() == 'ftp') {
$('#row_export_password').show();
$('#row_export_passive').show();
} else {
$('#row_export_password').hide();
$('#row_export_passive').hide();
}
if ($('#export_type').val() == 'rsync' || $('#export_type').val() == 'scp') {
$('#row_export_private_key_path').show();
} else {
$('#row_export_private_key_path').hide();
}
} else {
$('#row_export_temp_directory').hide();
$('#row_export_hdr_remote').hide();
$('#row_export_sanitize_remote').hide();
$('#row_export_host').hide();
$('#row_export_port').hide();
$('#row_export_user').hide();
$('#row_export_password').hide();
$('#row_export_passive').hide();
$('#row_export_private_key_path').hide();
}
}
function setTreeVisibility() {
if ($('#export_presentation').val() == 'tree') {
$('#row_graph_tree').show();
$('#row_export_expand_hosts').show();
$('#row_graph_site').hide();
} else {
$('#row_graph_tree').hide();
$('#row_export_expand_hosts').hide();
$('#row_graph_site').show();
}
}
</script>
<?php
}
/**
* Renders the Graph Exports list's search/filter toolbar (name search
* box, rows-per-page selector) and its client-side JavaScript. Called
* from gexport() before the exports 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.
*/
function export_filter() {
global $item_rows;
html_start_box(__('Graph Export Definitions', 'gexport'), '100%', false, 3, 'center', 'gexport.php?action=edit');
?>
<tr class='even'>
<td>
<form id='form_export' action='gexport.php'>
<table class='filterTable'>
<tr>
<td>
<?php print __('Search', 'gexport'); ?>
</td>
<td>
<input type='text' id='filter' size='25' value='<?php print html_escape_request_var('filter'); ?>'>
</td>
<td>
<?php print __('Exports', 'gexport'); ?>
</td>
<td>
<select id='rows' onChange='applyFilter()'>
<option value='-1'<?php print (get_request_var('rows') == '-1' ? ' selected>' : '>') . __('Default', 'gexport'); ?></option>
<?php
if (sizeof($item_rows)) {
foreach ($item_rows as $key => $value) {
print "<option value='" . $key . "'";
if (get_request_var('rows') == $key) {
print ' selected';
} print '>' . $value . "</option>\n";
}
}
?>
</select>
</td>
<td>
<?php print __('Refresh', 'gexport'); ?>
</td>
<td>
<select id='refresh' onChange='applyFilter()'>
<?php
$frequency = [
99999999 => __('Never', 'gexport'),
10 => __('%d Seconds', 10, 'gexport'),
20 => __('%d Seconds', 20, 'gexport'),
30 => __('%d Seconds', 30, 'gexport'),
45 => __('%d Seconds', 45, 'gexport'),
60 => __('%d Minute', 1, 'gexport'),
120 => __('%d Minutes', 2, 'gexport'),
300 => __('%d Minutes', 5, 'gexport')
];
foreach ($frequency as $r => $row) {
print "<option value='" . $r . "'" . (isset_request_var('refresh') && $r == get_request_var('refresh') ? ' selected' : '') . '>' . $row . '</option>';
}
?>
</select>
<td>
<span>
<button type='submit' id='go' class='ui-button ui-corner-all ui-widget ui-state-active'><?php print __x('filter: use', 'Go', 'gexport'); ?></button>
<button type='button' id='clear' class='ui-button ui-corner-all ui-widget'><?php print __x('filter: reset', 'Clear', 'gexport'); ?></button>
</span>
</td>
</tr>
</table>
</form>
<script type='text/javascript' <?php print plugin_gexport_csp_nonce(); ?>>
function applyFilter() {
strURL = 'gexport.php?header=false';
strURL += '&filter='+$('#filter').val();
strURL += '&refresh='+$('#refresh').val();
strURL += '&rows='+$('#rows').val();
loadPageNoHeader(strURL);
}
function clearFilter() {
strURL = 'gexport.php?clear=1&header=false';
loadPageNoHeader(strURL);
}
$(function() {
$('#refresh').click(function() {
applyFilter();
});
$('#has_graphs').click(function() {
applyFilter();
});
$('#clear').click(function() {
clearFilter();
});
$('#form_export').submit(function(event) {
event.preventDefault();
applyFilter();
});
});
</script>
</td>
</tr>
<?php
html_end_box();
}
/**
* Queries graph_exports for the current filter/sort/pagination settings.
* Called from gexport() to fetch the rows to display on the current
* list page.
*
* @param int $total_rows Reference, set to the total number of
* matching rows (ignoring pagination).
* @param int $rows The number of rows to return per page.
*
* @return array The matching graph_exports rows for the current page.
*/
function get_export_records(&$total_rows, &$rows) {
// form the 'where' clause for our main sql query
if (get_request_var('filter') != '') {
$sql_where = 'WHERE (name LIKE ' . db_qstr('%' . get_request_var('filter') . '%') . ')';
} else {
$sql_where = '';
}
$total_rows = db_fetch_cell("SELECT COUNT(*) FROM graph_exports $sql_where");
$sql_order = get_order_string();
$sql_limit = ' LIMIT ' . ($rows * (get_request_var('page') - 1)) . ',' . $rows;
return db_fetch_assoc("SELECT *
FROM graph_exports
$sql_where
$sql_order
$sql_limit");
}
/**
* Renders the main Graph Exports list page: disables page auto-refresh
* when no export is currently running, draws the search filter toolbar,
* and prints the paginated, sortable table of configured exports with
* their status. Invoked from this file's dispatcher for the default (no
* 'action') request.
*
* @return void Outputs the list page HTML directly.
*
* @global array $export_actions Map of bulk-action ids to their display
* labels, used to populate the actions
* dropdown.
*/
function gexport() {
global $export_actions;
$running = db_fetch_cell('SELECT COUNT(*)
FROM graph_exports
WHERE export_pid > 0
AND status > 0');
if ($running == 0) {
set_request_var('refresh', 99999999);
}
// ================= input validation and session storage =================
$filters = [
'rows' => [
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
],
'page' => [
'filter' => FILTER_VALIDATE_INT,
'default' => '1'
],
'refresh' => [
'filter' => FILTER_VALIDATE_INT,
'default' => '20'
],
'filter' => [
'filter' => FILTER_DEFAULT,
'pageset' => true,
'default' => ''
],
'sort_column' => [
'filter' => FILTER_CALLBACK,
'default' => 'name',
'options' => ['options' => 'sanitize_search_string']
],
'sort_direction' => [
'filter' => FILTER_CALLBACK,
'default' => 'ASC',
'options' => ['options' => 'sanitize_search_string']
]
];
validate_store_request_vars($filters, 'sess_gexport');
// ================= input validation =================
$refresh = [];
$refresh['page'] = 'gexport.php?header=false';
$refresh['seconds'] = get_request_var('refresh');
$refresh['logout'] = 'false';
set_page_refresh($refresh);
export_filter();
$total_rows = 0;
$exports = [];
if (get_request_var('rows') == '-1') {
$rows = read_config_option('num_rows_table');
} else {
$rows = get_request_var('rows');
}
$exports = get_export_records($total_rows, $rows);
$display_text = [
'name' => [
'display' => __('Export Name', 'gexport'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('The name of this Graph Export Definition.', 'gexport')
],
'id' => [
'display' => __('ID', 'gexport'),
'align' => 'right',
'sort' => 'ASC',
'tip' => __('The internal ID of the Graph Export Definition.', 'gexport')
],
'export_timing' => [
'display' => __('Schedule', 'gexport'),
'align' => 'right',
'sort' => 'DESC',
'tip' => __('The frequency that Graphs will be exported.', 'gexport')
],
'next_start' => [
'display' => __('Next Start', 'gexport'),
'align' => 'right',
'sort' => 'ASC',
'tip' => __('The next time the Graph Export should run.', 'gexport')
],
'nosort' => [
'display' => __('Enabled', 'gexport'),
'align' => 'right',
'tip' => __('If enabled, this Graph Export definition will run as required.', 'gexport')
],
'status' => [
'display' => __('Status', 'gexport'),
'align' => 'right',
'tip' => __('The current Graph Export Status.', 'gexport')
],
'nosort1' => [
'display' => __('Exporting (Sites/Trees)', 'gexport'),
'align' => 'right',
'sort' => 'ASC',
'tip' => __('What is being Exported.', 'gexport')
],
'export_effective_user' => [
'display' => __('Effective User', 'gexport'),
'align' => 'right',
'sort' => 'ASC',
'tip' => __('The user that this export will impersonate.', 'gexport')
],
'last_runtime' => [
'display' => __('Last Runtime', 'gexport'),
'align' => 'right',
'sort' => 'ASC',
'tip' => __('The last runtime for the Graph Export.', 'gexport')
],
'total_graphs' => [
'display' => __('Graphs', 'gexport'),
'align' => 'right',
'sort' => 'ASC',
'tip' => __('The number of Graphs Exported on the last run.', 'gexport')
],
'last_started' => [
'display' => __('Last Started', 'gexport'),
'align' => 'right',
'sort' => 'ASC',
'tip' => __('The last time that this Graph Export was started.', 'gexport')
],
'last_errored' => [
'display' => __('Last Errored', 'gexport'),
'align' => 'right',
'sort' => 'ASC',
'tip' => __('The last time that this Graph Export experienced an error.', 'gexport')
]
];
$nav = html_nav_bar(gexport_build_nav_filter_url(get_request_var('filter')), MAX_DISPLAY_PAGES, get_request_var('page'), $rows, $total_rows, sizeof($display_text) + 1, __('Export Definitions', 'gexport'), 'page', 'main');
form_start('gexport.php', '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);
$i = 0;
if (sizeof($exports)) {
foreach ($exports as $export) {
$user = db_fetch_cell_prepared('SELECT username
FROM user_auth
WHERE id = ?',
[$export['export_effective_user']]);
if ($export['export_pid'] > 0 && $export['status'] > 0) {
if (function_exists('posix_getpgid')) {
$running = posix_getpgid($export['export_pid']);
} elseif (function_exists('posix_kill')) {
$running = posix_kill($export['export_pid'], 0);
}
if (!$running) {
db_execute_prepared('UPDATE graph_exports
SET status=0, export_pid=0, last_error="Killed Outside Cacti", last_errored=NOW()