-
-
Notifications
You must be signed in to change notification settings - Fork 413
/
Copy pathfunctions.php
5623 lines (4776 loc) · 173 KB
/
functions.php
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-2019 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/ |
+-------------------------------------------------------------------------+
*/
/* title_trim - takes a string of text, truncates it to $max_length and appends
three periods onto the end
@arg $text - the string to evaluate
@arg $max_length - the maximum number of characters the string can contain
before it is truncated
@returns - the truncated string if len($text) is greater than $max_length, else
the original string */
function title_trim($text, $max_length) {
if (strlen($text) > $max_length) {
return mb_substr($text, 0, $max_length) . '...';
} else {
return $text;
}
}
/* filter_value - a quick way to highlight text in a table from general filtering
@arg $text - the string to filter
@arg $filter - the search term to filter for
@arg $href - the href if you wish to have an anchor returned
@returns - the filtered string */
function filter_value($value, $filter, $href = '') {
static $charset;
if ($charset == '') {
$charset = ini_get('default_charset');
}
if ($charset == '') {
$charset = 'UTF-8';
}
$value = htmlspecialchars($value, ENT_QUOTES, $charset, false);
if ($filter != '') {
$value = preg_replace('#(' . $filter . ')#i', "<span class='filteredValue'>\\1</span>", $value);
}
if ($href != '') {
$value = '<a class="linkEditMain" href="' . htmlspecialchars($href, ENT_QUOTES, $charset, false) . '">' . $value . '</a>';
}
return $value;
}
/* set_graph_config_option - deprecated - wrapper to set_user_setting().
@arg $config_name - the name of the configuration setting as specified $settings array
@arg $value - the values to be saved
@arg $user - the user id, otherwise the session user
@returns - void */
function set_graph_config_option($config_name, $value, $user = -1) {
set_user_setting($config_name, $value, $user);
}
/* graph_config_value_exists - deprecated - wrapper to user_setting_exists
@arg $config_name - the name of the configuration setting as specified $settings_user array
in 'include/global_settings.php'
@arg $user_id - the id of the user to check the configuration value for
@returns (bool) - true if a value exists, false if a value does not exist */
function graph_config_value_exists($config_name, $user_id) {
return user_setting_exists($config_name, $user_id);
}
/* read_default_graph_config_option - deprecated - wrapper to read_default_user_setting
@arg $config_name - the name of the configuration setting as specified $settings array
in 'include/global_settings.php'
@returns - the default value of the configuration option */
function read_default_graph_config_option($config_name) {
return read_default_user_setting($config_name);
}
/* read_graph_config_option - deprecated - finds the current value of a graph configuration setting
@arg $config_name - the name of the configuration setting as specified $settings_user array
in 'include/global_settings.php'
@returns - the current value of the graph configuration option */
function read_graph_config_option($config_name, $force = false) {
return read_user_setting($config_name, false, $force);
}
/* save_user_setting - sets/updates aLL user settings
@arg $config_name - the name of the configuration setting as specified $settings array
@arg $value - the values to be saved
@arg $user - the user id, otherwise the session user
@returns - void */
function save_user_settings($user = -1) {
global $settings_user;
if ($user == -1 || empty($user)) {
$user = $_SESSION['sess_user_id'];
}
foreach ($settings_user as $tab_short_name => $tab_fields) {
foreach ($tab_fields as $field_name => $field_array) {
/* Check every field with a numeric default value and reset it to default if the inputted value is not numeric */
if (isset($field_array['default']) && is_numeric($field_array['default']) && !is_numeric(get_nfilter_request_var($field_name))) {
set_request_var($field_name, $field_array['default']);
}
if (isset($field_array['method'])) {
if ($field_array['method'] == 'checkbox') {
set_user_setting($field_name, (isset_request_var($field_name) ? 'on' : ''), $user);
} elseif ($field_array['method'] == 'checkbox_group') {
foreach ($field_array['items'] as $sub_field_name => $sub_field_array) {
set_user_setting($sub_field_name, (isset_request_var($sub_field_name) ? 'on' : ''), $user);
}
} elseif ($field_array['method'] == 'textbox_password') {
if (get_nfilter_request_var($field_name) != get_nfilter_request_var($field_name.'_confirm')) {
$_SESSION['sess_error_fields'][$field_name] = $field_name;
$_SESSION['sess_field_values'][$field_name] = get_nfilter_request_var($field_name);
$errors[4] = 4;
} elseif (isset_request_var($field_name)) {
set_user_setting($field_name, get_nfilter_request_var($field_name), $user);
}
} elseif ((isset($field_array['items'])) && (is_array($field_array['items']))) {
foreach ($field_array['items'] as $sub_field_name => $sub_field_array) {
if (isset_request_var($sub_field_name)) {
set_user_setting($sub_field_name, get_nfilter_request_var($sub_field_name), $user);
}
}
} elseif (isset_request_var($field_name)) {
set_user_setting($field_name, get_nfilter_request_var($field_name), $user);
}
}
}
}
}
/* set_user_setting - sets/updates a user setting with the given value.
@arg $config_name - the name of the configuration setting as specified $settings array
@arg $value - the values to be saved
@arg $user - the user id, otherwise the session user
@returns - void */
function set_user_setting($config_name, $value, $user = -1) {
global $settings_user;
if ($user == -1 && isset($_SESSION['sess_user_id'])) {
$user = $_SESSION['sess_user_id'];
}
if ($user == -1) {
cacti_log('Attempt to set user setting \'' . $config_name . '\', with no user id: ' . cacti_debug_backtrace('', false, false, 0, 1), false, 'WARNING:');
} elseif (db_table_exists('settings_user')) {
db_execute_prepared('REPLACE INTO settings_user
SET user_id = ?,
name = ?,
value = ?',
array($user, $config_name, $value));
unset($_SESSION['sess_user_config_array']);
$settings_user[$config_name]['value'] = $value;
}
}
/* user_setting_exists - determines if a value exists for the current user/setting specified
@arg $config_name - the name of the configuration setting as specified $settings_user array
in 'include/global_settings.php'
@arg $user_id - the id of the user to check the configuration value for
@returns (bool) - true if a value exists, false if a value does not exist */
function user_setting_exists($config_name, $user_id) {
static $user_setting_values = array();
if (!isset($user_setting_values[$config_name])) {
$value = 0;
if (db_table_exists('settings_user')) {
$value = db_fetch_cell_prepared('SELECT COUNT(*)
FROM settings_user
WHERE name = ?
AND user_id = ?',
array($config_name, $user_id));
}
if ($value !== false && $value > 0) {
$user_setting_values[$config_name] = true;
} else {
$user_setting_values[$config_name] = false;
}
}
return $user_setting_values[$config_name];
}
/* clear_user_setting - if a value exists for the current user/setting specified, removes it
@arg $config_name - the name of the configuration setting as specified $settings_user array
in 'include/global_settings.php'
@arg $user_id - the id of the user to remove the configuration value for */
function clear_user_setting($config_name, $user = -1) {
global $settings_user;
if ($user == -1) {
$user = $_SESSION['sess_user_id'];
}
if (db_table_exists('settings_user')) {
db_execute_prepared('DELETE FROM settings_user
WHERE name = ?
AND user_id = ?',
array($config_name, $user));
}
unset($_SESSION['sess_user_config_array']);
}
/* read_default_user_setting - finds the default value of a user configuration setting
@arg $config_name - the name of the configuration setting as specified $settings array
in 'include/global_settings.php'
@returns - the default value of the configuration option */
function read_default_user_setting($config_name) {
global $config, $settings_user;
foreach ($settings_user as $tab_array) {
if (isset($tab_array[$config_name]) && isset($tab_array[$config_name]['default'])) {
return $tab_array[$config_name]['default'];
} else {
foreach ($tab_array as $field_array) {
if (isset($field_array['items']) && isset($field_array['items'][$config_name]) && isset($field_array['items'][$config_name]['default'])) {
return $field_array['items'][$config_name]['default'];
}
}
}
}
}
/* read_user_setting - finds the current value of a graph configuration setting
@arg $config_name - the name of the configuration setting as specified $settings_user array
in 'include/global_settings.php'
@arg $default - the default value is none is set
@arg $force - pull the data from the database if true ignoring session
@arg $user - assume this user's identity
@returns - the current value of the user setting */
function read_user_setting($config_name, $default = false, $force = false, $user = 0) {
global $config;
/* users must have cacti user auth turned on to use this, or the guest account must be active */
if ($user == 0 && isset($_SESSION['sess_user_id'])) {
$effective_uid = $_SESSION['sess_user_id'];
} elseif (read_config_option('auth_method') == 0 || $user > 0) {
/* first attempt to get the db setting for guest */
if ($user == 0) {
$effective_uid = db_fetch_cell("SELECT user_auth.id
FROM settings
INNER JOIN user_auth
ON user_auth.username = settings.value
WHERE settings.name = 'guest_user'");
if ($effective_uid == '') {
$effective_uid = 0;
}
} else {
$effective_uid = $user;
}
$db_setting = false;
if (db_table_exists('settings_user')) {
$db_setting = db_fetch_row_prepared('SELECT value
FROM settings_user
WHERE name = ?
AND user_id = ?',
array($config_name, $effective_uid));
}
if (cacti_sizeof($db_setting)) {
return $db_setting['value'];
} elseif ($default !== false) {
return $default;
} else {
return read_default_user_setting($config_name);
}
} else {
$effective_uid = 0;
}
if (!$force) {
if (isset($_SESSION['sess_user_config_array'])) {
$user_config_array = $_SESSION['sess_user_config_array'];
}
}
if (!isset($user_config_array[$config_name])) {
$db_setting = false;
if (db_table_exists('settings_user')) {
$db_setting = db_fetch_row_prepared('SELECT value
FROM settings_user
WHERE name = ?
AND user_id = ?',
array($config_name, $effective_uid));
}
if (cacti_sizeof($db_setting)) {
$user_config_array[$config_name] = $db_setting['value'];
} elseif ($default !== false) {
$user_config_array[$config_name] = $default;
} else {
$user_config_array[$config_name] = read_default_user_setting($config_name);
}
if (isset($_SESSION)) {
$_SESSION['sess_user_config_array'] = $user_config_array;
} else {
$config['config_user_settings_array'] = $user_config_array;
}
}
return $user_config_array[$config_name];
}
/* set_config_option - sets/updates a cacti config option with the given value.
@arg $config_name - the name of the configuration setting as specified $settings array
@arg $value - the values to be saved
@returns - void */
function set_config_option($config_name, $value) {
global $config;
db_execute_prepared('REPLACE INTO settings
SET name = ?, value = ?',
array($config_name, $value));
$config_array = array();
if (isset($_SESSION['sess_config_array'])) {
$config_array = $_SESSION['sess_config_array'];
} elseif (isset($config['config_options_array'])) {
$config_array = $config['config_options_array'];
}
$config_array[$config_name] = $value;
// Store the array back for later retrieval
if (isset($_SESSION)) {
$_SESSION['sess_config_array'] = $config_array;
} else {
$config['config_options_array'] = $config_array;
}
if (!empty($config['DEBUG_SET_CONFIG_OPTION'])) {
file_put_contents(sys_get_temp_dir() . '/cacti-option.log', get_debug_prefix() . cacti_debug_backtrace($config_name, false, false, 0, 1) . "\n", FILE_APPEND);
}
}
/* config_value_exists - determines if a value exists for the current user/setting specified
@arg $config_name - the name of the configuration setting as specified $settings array
in 'include/global_settings.php'
@returns (bool) - true if a value exists, false if a value does not exist */
function config_value_exists($config_name) {
static $config_values = array();
if (!isset($config_values[$config_name])) {
$value = db_fetch_cell_prepared('SELECT COUNT(*) FROM settings WHERE name = ?', array($config_name));
if ($value > 0) {
$config_values[$config_name] = true;
} else {
$config_values[$config_name] = false;
}
}
return $config_values[$config_name];
}
/* read_default_config_option - finds the default value of a Cacti configuration setting
@arg $config_name - the name of the configuration setting as specified $settings array
in 'include/global_settings.php'
@returns - the default value of the configuration option */
function read_default_config_option($config_name) {
global $config, $settings;
if (is_array($settings)) {
foreach ($settings as $tab_array) {
if (isset($tab_array[$config_name]) && isset($tab_array[$config_name]['default'])) {
return $tab_array[$config_name]['default'];
} else {
foreach ($tab_array as $field_array) {
if (isset($field_array['items']) && isset($field_array['items'][$config_name]) && isset($field_array['items'][$config_name]['default'])) {
return $field_array['items'][$config_name]['default'];
}
}
}
}
}
}
/* read_config_option - finds the current value of a Cacti configuration setting
@arg $config_name - the name of the configuration setting as specified $settings array
in 'include/global_settings.php'
@returns - the current value of the configuration option */
function read_config_option($config_name, $force = false) {
global $config, $database_hostname, $database_default, $database_port, $database_sessions;
$config_array = array();
if (isset($_SESSION['sess_config_array'])) {
$config_array = $_SESSION['sess_config_array'];
} elseif (isset($config['config_options_array'])) {
$config_array = $config['config_options_array'];
}
if (!empty($config['DEBUG_READ_CONFIG_OPTION'])) {
file_put_contents(sys_get_temp_dir() . '/cacti-option.log', get_debug_prefix() . cacti_debug_backtrace($config_name, false, false, 0, 1) . "\n", FILE_APPEND);
}
// Do we have a value already stored in the array, or
// do we want to make sure we have the latest value
// from the database?
if (!array_key_exists($config_name, $config_array) || ($force)) {
// We need to check against the DB, but lets assume default value
// unless we can actually read the DB
$value = read_default_config_option($config_name);
if (!empty($config['DEBUG_READ_CONFIG_OPTION'])) {
file_put_contents(sys_get_temp_dir() . '/cacti-option.log', get_debug_prefix() .
" $config_name: " .
' dh: ' . isset($database_hostname) .
' dp: ' . isset($database_port) .
' dd: ' . isset($database_default) .
' ds: ' . isset($database_sessions["$database_hostname:$database_port:$database_default"]) .
"\n", FILE_APPEND);
if (isset($database_hostname) && isset($database_port) && isset($database_default)) {
file_put_contents(sys_get_temp_dir() . '/cacti-option.log', get_debug_prefix() .
" $config_name: [$database_hostname:$database_port:$database_default]\n", FILE_APPEND);
}
}
// Are the database variables set, and do we have a connection??
// If we don't, we'll only use the default value without storing
// so that we can read the database version later.
if (isset($database_hostname) && isset($database_port) && isset($database_default) &&
isset($database_sessions["$database_hostname:$database_port:$database_default"])) {
// Get the database setting
$db_setting = db_fetch_row_prepared('SELECT value FROM settings WHERE name = ?', array($config_name), false);
// Does the settings exist in the database?
if (isset($db_setting['value'])) {
// It does? lets use it
$value = $db_setting['value'];
}
// Store whatever value we have in the array
$config_array[$config_name] = $value;
// Store the array back for later retrieval
if (isset($_SESSION)) {
$_SESSION['sess_config_array'] = $config_array;
} else {
$config['config_options_array'] = $config_array;
}
}
} else {
// We already have the value stored in the array and
// we don't want to force a db read, so use the cached
// version
$value = $config_array[$config_name];
}
return $value;
}
/*
* get_selected_theme - checks the user settings and if the user selected
* theme is set, returns it otherwise returns the system default.
*
* @return - the theme name
*/
function get_selected_theme($theme = 'cacti') {
global $config, $themes;
// shortcut if theme is set in session
if (isset($_SESSION['selected_theme'])) {
$theme = $_SESSION['selected_theme'];
} else {
// default to system selected theme
$theme = read_config_option('selected_theme');
// check for a pre-1.x cacti being upgraded
if ($theme == '') {
$theme = 'cacti';
}
// figure out user defined theme
if (isset($_SESSION['sess_user_id']) && db_table_exists('settings_user')) {
// fetch user defined theme
$user_theme = db_fetch_cell_prepared("SELECT value
FROM settings_user
WHERE name='selected_theme'
AND user_id = ?",
array($_SESSION['sess_user_id']));
// user has a theme
if (! empty($user_theme)) {
$theme = $user_theme;
}
}
}
if (is_valid_theme($theme, true)) {
$_SESSION['selected_theme'] = $theme;
}
return $theme;
}
function is_valid_theme(&$theme, $set_user = false) {
global $themes, $config;
$valid = true;
if (!file_exists($config['base_path'] . '/include/themes/' . $theme . '/main.css')) {
$valid = false;
$user_table = db_table_exists('settings_user');
foreach($themes as $t => $name) {
if (file_exists($config['base_path'] . '/include/themes/' . $t . '/main.css')) {
$theme = $t;
$valid = true;
if ($user_table && $set_user && isset($_SESSION['sess_user_id'])) {
db_execute_prepared('UPDATE settings_user
SET value = ?
WHERE user_id = ?
AND name="selected_theme"',
array($theme, $_SESSION['sess_user_id']));
}
break;
}
}
}
// update session
return $valid;
}
/* form_input_validate - validates the value of a form field and Takes the appropriate action if the input
is not valid
@arg $field_value - the value of the form field
@arg $field_name - the name of the $_POST field as specified in the HTML
@arg $regexp_match - (optionally) enter a regular expression to match the value against
@arg $allow_nulls - (bool) whether to allow an empty string as a value or not
@arg $custom_message - (int) the ID of the message to raise upon an error which is defined in the
$messages array in 'include/global_arrays.php'
@returns - the original $field_value */
function form_input_validate($field_value, $field_name, $regexp_match, $allow_nulls, $custom_message = 3) {
global $messages;
/* write current values to the "field_values" array so we can retain them */
$_SESSION['sess_field_values'][$field_name] = $field_value;
if (($allow_nulls == true) && ($field_value == '')) {
return $field_value;
}
if ($allow_nulls == false && $field_value == '') {
if (read_config_option('log_validation') == 'on') {
cacti_log("Form Validation Failed: Variable '$field_name' does not allow nulls and variable is null", false);
}
raise_message($custom_message);
$_SESSION['sess_error_fields'][$field_name] = $field_name;
} elseif ($regexp_match != '' && !preg_match('/' . $regexp_match . '/', $field_value)) {
if (read_config_option('log_validation') == 'on') {
cacti_log("Form Validation Failed: Variable '$field_name' with Value '$field_value' Failed REGEX '$regexp_match'", false);
}
raise_message($custom_message);
$_SESSION['sess_error_fields'][$field_name] = $field_name;
}
return $field_value;
}
/* check_changed - determines if a request variable has changed between page loads
@returns - (bool) true if the value changed between loads */
function check_changed($request, $session) {
if ((isset_request_var($request)) && (isset($_SESSION[$session]))) {
if (get_nfilter_request_var($request) != $_SESSION[$session]) {
return 1;
}
}
}
/* is_error_message - finds whether an error message has been raised and has not been outputted to the
user
@returns - (bool) whether the messages array contains an error or not */
function is_error_message() {
global $config, $messages;
if (isset($_SESSION['sess_error_fields']) && sizeof($_SESSION['sess_error_fields'])) {
return true;
} else {
return false;
}
}
function get_message_level($current_message) {
$current_level = MESSAGE_LEVEL_NONE;
if (isset($current_message['level'])) {
$current_level = $current_message['level'];
} elseif (isset($current_message['type'])) {
switch ($current_message['type']) {
case 'error':
$current_level = MESSAGE_LEVEL_ERROR;
break;
case 'info':
$current_level = MESSAGE_LEVEL_INFO;
break;
}
}
return $current_level;
}
/* get_format_message_instance - finds the level of the current message instance
* @arg message array the message instance
* @returns - (string) a formatted message
*/
function get_format_message_instance($current_message) {
if (is_array($current_message)) {
$fmessage = isset($current_message['message']) ? $current_message['message'] : __esc('Message Not Found.');
} else {
$fmessage = $current_message;
}
$level = get_message_level($current_message);
switch ($level) {
case MESSAGE_LEVEL_NONE:
$message = '<span>' . $fmessage . '</span>';
break;
case MESSAGE_LEVEL_INFO:
$message = '<span class="deviceUp">' . $fmessage . '</span>';
break;
case MESSAGE_LEVEL_WARN:
$message = '<span class="deviceWarning">' . $fmessage . '</span>';
break;
case MESSAGE_LEVEL_ERROR:
$message = '<span class="deviceDown">' . $fmessage . '</span>';
break;
case MESSAGE_LEVEL_CSRF:
$message = '<span class="deviceDown">' . $fmessage . '</span>';
break;
default;
$message = '<span class="deviceUnknown">' . $fmessage . '</span>';
break;
}
return $message;
}
/* get_message_max_type - finds the message and returns its type
@returns - (string) the message type 'info', 'warn', 'error' or 'csrf' */
function get_message_max_type() {
global $messages;
$level = MESSAGE_LEVEL_NONE;
if (isset($_SESSION['sess_messages'])) {
if (is_array($_SESSION['sess_messages'])) {
foreach ($_SESSION['sess_messages'] as $current_message_id => $current_message) {
$current_level = get_message_level($current_message);
if ($current_level == MESSAGE_LEVEL_NONE && isset($messages[$current_message_id])) {
$current_level = get_message_level($messages[$current_message_id]);
}
if ($current_level != $level && $level != MESSAGE_LEVEL_NONE) {
$level = MESSAGE_LEVEL_MIXED;
} else {
$level = $current_level;
}
}
}
}
return $level;
}
/* raise_message - mark a message to be displayed to the user once display_output_messages() is called
@arg $message_id - the ID of the message to raise as defined in $messages in 'include/global_arrays.php' */
function raise_message($message_id, $message = '', $message_level = MESSAGE_LEVEL_NONE) {
global $messages, $no_http_headers;
// This function should always exist, if not its an invalid install
if (function_exists('session_status')) {
$need_session = (session_status() == PHP_SESSION_NONE) && (!isset($no_http_headers));
} else {
return false;
}
if (empty($message)) {
if (array_key_exists($message_id, $messages)) {
$predefined = $messages[$message_id];
if (isset($predefined['message'])) {
$message = $predefined['message'];
} else {
$message = $predefined;
}
if ($message_level == MESSAGE_LEVEL_NONE) {
$message_level = get_message_level($predefined);
}
} elseif (isset($_SESSION[$message_id])) {
$message = $_SESSION[$message_id];
$message_level = MESSAGE_LEVEL_ERROR;
} else {
$message = __('Message Not Found.');
$message_level = MESSAGE_LEVEL_ERROR;
}
}
if ($need_session) {
session_start();
}
if (!isset($_SESSION['sess_messages'])) {
$_SESSION['sess_messages'] = array();
}
$_SESSION['sess_messages'][$message_id] = array('message' => $message, 'level' => $message_level);
if ($need_session) {
session_write_close();
}
}
/* display_output_messages - displays all of the cached messages from the raise_message() function and clears
the message cache */
function display_output_messages() {
global $messages;
$omessage = array();
$debug_message = debug_log_return('new_graphs');
if ($debug_message != '') {
$omessage['level'] = MESSAGE_LEVEL_NONE;
$omessage['message'] = $debug_message;
debug_log_clear('new_graphs');
} elseif (isset($_SESSION['sess_messages'])) {
if (!is_array($_SESSION['sess_messages'])) {
$_SESSION['sess_messages'] = array('custom_error' => array('level' => 3, 'message' => $_SESSION['sess_messages']));
}
$omessage['level'] = get_message_max_type();
foreach ($_SESSION['sess_messages'] as $current_message_id => $current_message) {
$message = get_format_message_instance($current_message);
if (!empty($message)) {
$omessage['message'] = (isset($omessage['message']) && $omessage['message'] != '' ? $omessage['message'] . '<br>':'') . $message;
} else {
cacti_log("ERROR: Cacti Error Message Id '$current_message_id' Not Defined", false, 'WEBUI');
}
}
}
clear_messages();
return json_encode($omessage);
}
function display_custom_error_message($message) {
raise_message('custom_error', $message);
}
/* clear_messages - clears the message cache */
function clear_messages() {
// This function should always exist, if not its an invalid install
if (function_exists('session_status')) {
$need_session = (session_status() == PHP_SESSION_NONE) && (!isset($no_http_headers));
} else {
return false;
}
if ($need_session) {
session_start();
}
kill_session_var('sess_messages');
if ($need_session) {
session_write_close();
}
}
/* kill_session_var - kills a session variable using unset() */
function kill_session_var($var_name) {
/* register_global = on: reset local settings cache so the user sees the new settings */
unset($_SESSION[$var_name]);
/* register_global = off: reset local settings cache so the user sees the new settings */
/* session_unregister is deprecated in PHP 5.3.0, unset is sufficient */
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
session_unregister($var_name);
} else {
unset($var_name);
}
}
/* force_session_data - forces session data into the session if the session was closed for some reason */
function force_session_data() {
// This function should always exist, if not its an invalid install
if (!function_exists('session_status')) {
return false;
} elseif (session_status() == PHP_SESSION_NONE) {
$data = $_SESSION;
session_start();
$_SESSION = $data;
session_write_close();
}
}
/* array_rekey - changes an array in the form:
'$arr[0] = array('id' => 23, 'name' => 'blah')'
to the form
'$arr = array(23 => 'blah')'
@arg $array - (array) the original array to manipulate
@arg $key - the name of the key
@arg $key_value - the name of the key value
@returns - the modified array */
function array_rekey($array, $key, $key_value) {
$ret_array = array();
if (is_array($array)) {
foreach ($array as $item) {
$item_key = $item[$key];
if (is_array($key_value)) {
foreach ($key_value as $value) {
$ret_array[$item_key][$value] = $item[$value];
}
} else {
$ret_array[$item_key] = $item[$key_value];
}
}
}
return $ret_array;
}
/* cacti_log_file - returns the log filename */
function cacti_log_file() {
global $config;
$logfile = read_config_option('path_cactilog');
if ($logfile == '') {
$logfile = $config['base_path'] . '/log/cacti.log';
}
$config['log_path'] = $logfile;
return $logfile;
}
/* cacti_log - logs a string to Cacti's log file or optionally to the browser
@arg $string - the string to append to the log file
@arg $output - (bool) whether to output the log line to the browser using print() or not
@arg $environ - (string) tells from where the script was called from
@arg $level - (int) only log if above the specified log level */
function cacti_log($string, $output = false, $environ = 'CMDPHP', $level = '') {
global $config, $database_log;
if (!isset($database_log)) {
$database_log = false;
}
$last_log = $database_log;
$database_log = false;
if (isset($_SERVER['PHP_SELF'])) {
$current_file = basename($_SERVER['PHP_SELF']);
$dir_name = dirname($_SERVER['PHP_SELF']);
} elseif (isset($_SERVER['SCRIPT_NAME'])) {
$current_file = basename($_SERVER['SCRIPT_NAME']);
$dir_name = dirname($_SERVER['SCRIPT_NAME']);
} elseif (isset($_SERVER['SCRIPT_FILENAME'])) {
$current_file = basename($_SERVER['SCRIPT_FILENAME']);
$dir_name = dirname($_SERVER['SCRIPT_FILENAME']);
} else {
$current_file = basename(__FILE__);
$dir_name = dirname(__FILE__);
}
$force_level = '';
$debug_files = read_config_option('selective_debug');
if ($debug_files != '') {
$files = explode(',', $debug_files);
if (array_search($current_file, $files) !== false) {
$force_level = POLLER_VERBOSITY_DEBUG;
}
}
if (strpos($dir_name, 'plugins') !== false) {
$debug_plugins = read_config_option('selective_plugin_debug');
if ($debug_plugins != '') {
$debug_plugins = explode(',', $debug_plugins);
foreach($debug_plugins as $myplugin) {
if (strpos($dir_name, DIRECTORY_SEPARATOR . $myplugin) !== false) {
$force_level = POLLER_VERBOSITY_DEBUG;
break;
}
}
}
}
/* only log if the specific level is reached, developer debug is special low + specific devdbg calls */
if ($force_level == '') {
if ($level != '') {
$logVerbosity = read_config_option('log_verbosity');
if ($logVerbosity == POLLER_VERBOSITY_DEVDBG) {
if ($level != POLLER_VERBOSITY_DEVDBG) {
if ($level > POLLER_VERBOSITY_LOW) {
$database_log = $last_log;
return;
}
}
} elseif ($level > $logVerbosity) {
$database_log = $last_log;
return;
}
}
}
/* fill in the current date for printing in the log */
if (defined('CACTI_DATE_TIME_FORMAT')) {
$date = date(CACTI_DATE_TIME_FORMAT);
} else {
$date = date('Y-m-d H:i:s');
}
/* determine how to log data */
$logdestination = read_config_option('log_destination');
$logfile = cacti_log_file();
/* format the message */
if ($environ == 'POLLER') {
$prefix = "$date - " . $environ . ': Poller[' . $config['poller_id'] . '] ';
} else {
$prefix = "$date - " . $environ . ' ';
}
/* Log to Logfile */
$message = clean_up_lines($string) . PHP_EOL;
if (($logdestination == 1 || $logdestination == 2) && read_config_option('log_verbosity') != POLLER_VERBOSITY_NONE) {
/* echo the data to the log (append) */
$fp = @fopen($logfile, 'a');
if ($fp) {
$message = $prefix . $message;
@fwrite($fp, $message);
fclose($fp);
}
}
/* Log to Syslog/Eventlog */
/* Syslog is currently Unstable in Win32 */
if ($logdestination == 2 || $logdestination == 3) {
$log_type = '';
if (strpos($string, 'ERROR:') !== false) {
$log_type = 'err';
} elseif (strpos($string, 'WARNING:') !== false) {
$log_type = 'warn';
} elseif (strpos($string, 'STATS:') !== false) {
$log_type = 'stat';
} elseif (strpos($string, 'NOTICE:') !== false) {
$log_type = 'note';
}
if ($log_type != '') {
if ($config['cacti_server_os'] == 'win32') {
openlog('Cacti', LOG_NDELAY | LOG_PID, LOG_USER);
} else {
openlog('Cacti', LOG_NDELAY | LOG_PID, LOG_SYSLOG);
}
if ($log_type == 'err' && read_config_option('log_perror')) {
syslog(LOG_CRIT, $environ . ': ' . $string);
} elseif ($log_type == 'warn' && read_config_option('log_pwarn')) {
syslog(LOG_WARNING, $environ . ': ' . $string);
} elseif (($log_type == 'stat' || $log_type == 'note') && read_config_option('log_pstats')) {