forked from freuds/nconf
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdetail.php
473 lines (368 loc) · 19.4 KB
/
detail.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
<?php
require_once 'include/head.php';
?>
<!-- jQuery part -->
<script type="text/javascript">
$(document).ready(function(){
// highlight the previouse applied template
$('.previously_applied').hover(function() {
$("td[id='"+$(this).attr("id")+"_first']").toggleClass("ui-state-highlight");
});
});
</script>
<?php
// Set previous (referer) page if that actual item in detail view will be deleted
if ( empty($_SERVER["HTTP_REFERER"]) AND isset($_SESSION["after_delete_page"]) ) {
message($debug, "referer not set, seems to be from a delete operation, go to after_delete_page");
$from_url = $_SESSION["after_delete_page"];
}elseif ( isset($_SERVER["HTTP_REFERER"]) AND preg_match('/detail\.php/', $_SERVER["HTTP_REFERER"]) ){
message($debug, "detail.php matched");
$from_url = $_SERVER["HTTP_REFERER"];
}elseif( isset($_SERVER["HTTP_REFERER"]) AND preg_match('/modify_item\.php/', $_SERVER["HTTP_REFERER"]) ){
# coming from editing, do still a forward to the after_delete_page
$from_url = $_SESSION["after_delete_page"];
}elseif( isset($_SERVER["HTTP_REFERER"]) AND preg_match('/'.preg_quote("add_item_step2.php").'/', $_SERVER["HTTP_REFERER"]) ){
# coming from an add (entry exists), do still a forward to the after_delete_page
$from_url = $_SESSION["after_delete_page"];
}elseif( !empty($_SERVER["HTTP_REFERER"]) ){
message($debug, "not from detail.php or modify, setting referer");
$_SESSION["after_delete_page"] = $_SERVER["HTTP_REFERER"];
$from_url = $_SERVER["HTTP_REFERER"];
}else{
# direct opening of this file, or no referer, go back to overview.php
$from_url = "index.php";
}
set_page();
if ( empty($_GET["id"]) ){
NConf_DEBUG::set("No id", 'ERROR');
}else{
// Be sure ID it is an integer - fixes injecting issues
$item_id = (int) $_GET["id"];
$item_class = db_templates("class_name", $item_id);
$item_name = db_templates("naming_attr", $item_id);
}
// end / exit page if error
if ( NConf_DEBUG::status('ERROR') ) {
echo NConf_HTML::exit_error();
}
# cache template id's to detect repetitive templates
$template_cache = array();
$template_cache_local = array();
# History Tab-View
require_once 'include/tabs/history.php';
# Normal detail page
echo '<div style="width: 500px;" class="relative">';
echo '<div class="ui-nconf-header ui-widget-header ui-corner-tl ui-corner-tr ui-helper-clearfix">';
echo '<div><h2>Details of '.$item_class.': '.$item_name.'</h2></div>';
echo '<div id="ui-nconf-icon-bar">';
// tool bar of details view
$output = '';
// Edit
$output .= ( !isset($_GET["xmode"]) ) ? '<a href="handle_item.php?item='.$item_class.'&id='.$item_id.'">'.ICON_EDIT.'</a>' : '';
// Clone
$output .= ( $item_class == "host" ) ? '<a href="clone_host.php?class='.$item_class.'&id='.$item_id.'">'.ICON_CLONE.'</a>' : '';
// Delete
$output .= ( !isset($_GET["xmode"]) ) ? '<a href="delete_item.php?item='.$item_class.'&ids='.$item_id.'&from='.$from_url.'">'.ICON_DELETE.'</a>' : '';
// Services
$output .= ( $item_class == "host" ) ? '<a href="modify_item_service.php?id='.$item_id.'">'.ICON_SERVICES.'</a>' : '';
// History
$output .= '<a href="history.php?item='.$item_class.'&id='.$item_id.'&from='.$from_url.'">'.ICON_HISTORY.'</a>';
// Parent child
$output .= ( $item_class == "host" ) ? '<a href="dependency.php?id='.$item_id.'">'.ICON_PARENT_CHILD.'</a>' : '';
echo $output;
echo '</div>';
echo '</div>';
echo '<div class="ui-nconf-content ui-widget-content ui-corner-bottom">';
$colgroup = '<colgroup>
<col width="150">
<col>
</colgroup>';
echo '<table class="ui-nconf-table ui-nconf-max-width">';
echo $colgroup;
echo '<tbody>';
# get basic entries
$query = 'SELECT ConfigAttrs.friendly_name,attr_value, ConfigAttrs.datatype
FROM ConfigAttrs,ConfigValues,ConfigItems
WHERE id_attr=fk_id_attr
AND id_item=fk_id_item
AND ConfigAttrs.visible="yes"
AND id_item='.$item_id.'
ORDER BY ConfigAttrs.ordering';
$result = db_handler($query, "array", "get basic entries");
$basic_result = array();
# show or hide password if exists:
foreach ($result AS $attributes){
if ($attributes["datatype"] == "password"){
# check how to display password
$attributes["attr_value"] = show_password($attributes["attr_value"]);
}
array_push($basic_result, $attributes);
}
# print an overview of the icon if available
if ($item_class == "os" and $basic_result[1]["attr_value"] != "" and is_readable(OS_LOGO_PATH."/".$basic_result[1]["attr_value"])) {
array_push($basic_result, array('friendly_name' => 'icon preview', 'attr_value' => "<img src=".OS_LOGO_PATH."/".$basic_result[1]["attr_value"].">"));
}
echo table_output($basic_result, $item_class);
# get linked entries
$query2 = 'SELECT ConfigAttrs.friendly_name,attr_value,fk_item_linked2 AS item_id,
(SELECT config_class FROM ConfigItems,ConfigClasses
WHERE id_class=fk_id_class AND id_item=item_id) AS config_class
FROM ConfigValues,ItemLinks,ConfigAttrs,ConfigClasses
WHERE fk_item_linked2=ConfigValues.fk_id_item
AND id_attr=ItemLinks.fk_id_attr
AND ConfigAttrs.visible="yes"
AND fk_id_class=id_class
AND (SELECT naming_attr FROM ConfigAttrs WHERE id_attr=ConfigValues.fk_id_attr)="yes"
AND ItemLinks.fk_id_item='.$item_id.'
ORDER BY
ConfigAttrs.friendly_name DESC,
ItemLinks.cust_order,
attr_value';
$result = db_handler($query2, "array", "get linked entries");
echo table_output($result, $item_class, "This item is linked to");
# get entries
$result = db_templates("linked_as_child", $item_id, '', '', 'array');
echo table_output($result, $item_class, "Child items linked");
echo '</table>';
echo '</div>';
###
# template inheritance
if ($item_class == "host" OR $item_class == "service" OR $item_class == "advanced-service"){
# do not output content directly, so we can evaluate if there are templates
$output = '';
NConf_DEBUG::open_group("template inheritance");
$output .= 'Templates are applied in the following order:';
$output .= '<table class="ui-nconf-table ui-nconf-max-width">';
$output .= $colgroup;
# create tables
if ($item_class == "host"){
$direct_host_templates = db_templates("template_inheritance_direct", $item_id);
if ( !empty($direct_host_templates) ){
$output .= '<tr><td colspan=2><br><b>directly linked to host</b></td></tr>';
$output .= table_output($direct_host_templates, 'template_inheritance');
}
# notification period inheritance
$p2 = db_templates("template_inheritance", $item_id, "notification_period", "host_template");
$output .= table_output($p2, 'template_inheritance', '"notification period"');
# check period templates
$p3 = db_templates("template_inheritance", $item_id, "check_period", "host_template");
$output .= table_output($p3, 'template_inheritance', '"check period"');
#collector templates
$monitored_by_id = db_templates("get_linked_item", $item_id, "monitored_by");
if ( !empty($monitored_by_id[0]["fk_id_item"]) ){
# only if host is monitored
$p4_col = db_templates("template_inheritance_collector_monitor", $monitored_by_id[0]["fk_id_item"], "host_template");
$output .= table_output($p4_col, 'template_inheritance', '<b>Nagios-collector</b> ("monitored by")');
# monitor templates
$nagios_monitor_query = 'SELECT id_item FROM ConfigItems, ConfigClasses WHERE id_class=fk_id_class AND config_class="nagios-monitor";';
$nagios_monitors = db_handler($nagios_monitor_query, 'array_direct', "get ids of all nagios-monitors");
if ( !empty($nagios_monitors) ){
$output .= '<tr><td colspan=2><br><b>inherited from Nagios-monitor</b></td></tr>';
foreach ($nagios_monitors AS $monitor_id){
$p4_mon = db_templates("template_inheritance_collector_monitor", $monitor_id, "host_template");
$output .= table_output($p4_mon, 'template_inheritance');
}
}
}
}elseif($item_class == "service" OR $item_class == "advanced-service"){
$direct_service_templates = db_templates("template_inheritance_direct", $item_id);
if ( !empty($direct_service_templates) ){
$output .= '<tr><td colspan=2><br><b>directly linked to service</b></td></tr>';
$output .= table_output($direct_service_templates, 'template_inheritance');
}
# notification period inheritance
$p2 = db_templates("template_inheritance", $item_id, "check_command", "service_template");
$output .= table_output($p2, 'template_inheritance', '"check command"');
# notification period inheritance
$p3 = db_templates("template_inheritance", $item_id, "notification_period", "service_template");
$output .= table_output($p3, 'template_inheritance', '"notification period"');
# check period templates
$p4 = db_templates("template_inheritance", $item_id, "check_period", "service_template");
$output .= table_output($p4, 'template_inheritance', '"check period"');
# collector templates
$host_id = db_templates("hostID_of_service", $item_id);
$monitored_by_id = db_templates("get_linked_item", $host_id, "monitored_by");
if ( !empty($monitored_by_id[0]["fk_id_item"]) ){
# only if host is monitored
# get collector templates
$p5_col = db_templates("template_inheritance_collector_monitor", $monitored_by_id[0]["fk_id_item"], "service_template");
$output .= table_output($p5_col, 'template_inheritance', '<b>Nagios-collector</b> ("monitored by")');
# get monitor templates
$nagios_monitor_query = 'SELECT id_item FROM ConfigItems, ConfigClasses WHERE id_class=fk_id_class AND config_class="nagios-monitor";';
$nagios_monitors = db_handler($nagios_monitor_query, 'array_direct', "get ids of all nagios-monitors");
if ( !empty($nagios_monitors) ){
$output .= '<tr><td colspan=2><br><b>inherited from Nagios-monitor</b></td></tr>';
foreach ($nagios_monitors AS $monitor_id){
$p5_mon = db_templates("template_inheritance_collector_monitor", $monitor_id, "service_template");
$output .= table_output($p5_mon, 'template_inheritance');
}
}
}
}
# close template inheritance
$output .= '</table>';
$output .= '</div>';
# template cache debug
NConf_DEBUG::set($template_cache, 'DEBUG', "template cache");
NConf_DEBUG::close_group();
#print the template inheritace box
echo NConf_HTML::ui_box_header('Template inheritance');
if ( count($template_cache) == 0 ){
$output = 'no templates inherited';
}
echo NConf_HTML::ui_box_content($output);
}
echo '</div>';
/*
commented out because we do not want to group the different link types
we want all linked items in one group
if we want to change that, we have to get all normal types, and then group the child or bidirectionals as follows:
# get entries linked as child
$result = db_templates("linked_as_child", $item_id, "link_as_child");
table_output($result, $item_class, "Child items linked");
# get bidirectional entries
$result = db_templates("linked_as_child", $item_id, "link_bidirectional");
table_output($result, $item_class, "Bidirectional items");
*/
function table_output($result, $item_class = '', $title = '', $level = 0){
# template cache for detect previouse loaded template
global $template_cache;
# the local template cache is for detecting endless loops
global $template_cache_local;
# handling the local template cache
#reseting it if level is 0
if ($level == 0){
$template_cache_local = array();
}elseif($level == 20){
# this is a hardcoded loop stopper, should come into action, but prevents that the page will endless load
# normaly there will never be templates on 20 level inherited
return;
}
# output will catch the content until return
$output = '';
if ( !empty($result) ) {
if( ( is_array($result) AND !empty($result) ) AND !empty($title) ){
$output .= '<tr><td colspan=2><br>';
if ($item_class == 'template_inheritance'){
$output .= '<b>inherited from</b> '.$title;
}else{
$output .= '<b>'.$title.'</b>';
}
$output .= '</td></tr>';
}
$last_fname = '';
foreach ($result AS $entry){
if( !empty($entry["config_class"]) AND $entry["config_class"] == "service"){
$host_query = 'SELECT attr_value AS hostname
FROM ConfigValues,ConfigAttrs,ConfigClasses,ItemLinks
WHERE fk_item_linked2=ConfigValues.fk_id_item
AND id_attr=ConfigValues.fk_id_attr
AND naming_attr="yes"
AND fk_id_class = id_class
AND config_class="host"
AND ItemLinks.fk_id_item='.$entry["item_id"];
$hostname = db_handler($host_query, "getOne", "Get linked hostnames (if service)");
}
if ( !empty($entry["friendly_name"]) ){
$group_name = $entry["friendly_name"];
}
// group same attributes
if( !empty($group_name) AND $last_fname != $group_name){
$show_fname = $group_name;
//$bgcolor = 'class="color_list2"';
}else{
$show_fname = '';
}
$output .= '<tr>';
//$output .= '<td '.$bgcolor.'>'.$show_fname.'</td>';
$output .= '<td class="color_list2">'.$show_fname.'</td>';
# print template (and detect repetitive)
if ($item_class == 'template_inheritance'){
$template_status = apply_template($template_cache, $entry["item_id"]);
$local_template_status = apply_template($template_cache_local, $entry["item_id"]);
NConf_DEBUG::set($local_template_status, 'DEBUG', "local repetitive status");
NConf_DEBUG::set($template_cache_local, 'DEBUG', "local template cache");
}else{
$template_status = FALSE;
$local_template_status = FALSE;
}
if( !empty($entry["config_class"]) AND $entry["config_class"] == "service" && $item_class != "host"){
$output .= '<td class="color_list1 highlight">';
$output .= '<a href="detail.php?id='.$entry["item_id"].'">';
$output .= $hostname.': '.$entry["attr_value"];
$output .= '</td>';
}else{
$level_label = '';
for($i=$level; $i>1;$i--){
$level_label .= '<div style="width: 9px; display: inline-block;"></div>';
}
if ($i == 1 AND $level != 0){
## add a mark
$level_label .= '<span class="link_with_tag2"></span>';
}
# mark previously applied templates
if ($template_status === "repetitive"){
# detect previously applied or template loop
if ($local_template_status === "repetitive"){
# endless loop
$repetitive_text = "(circular template chain detected)";
$class = "ui-state-error highlight";
}else{
$repetitive_text = "(previously applied)";
$class = "color_list1 highlight";
}
$output .= '<td class="'.$class.'">';
$output .= $level_label.'<a href="detail.php?id='.$entry["item_id"].'">';
$output .= $entry["attr_value"].'</a>';
$output .= '<span id="'.$entry["item_id"].'" class="previously_applied" style="float: right;"><i>'.$repetitive_text.'</i></span>';
$output .= '</td>';
}else{
# set id for coming repetitive
if ($item_class == 'template_inheritance'){
$output .= '<td id="'.$entry["item_id"].'_first" class="color_list1 highlight">';
}else{
$output .= '<td class="color_list1 highlight">';
}
if ( !empty($entry["item_id"]) ){
$output .= $level_label;
$output .= '<a href="detail.php?id='.$entry["item_id"].'">';
$output .= $entry["attr_value"].'</a>';
}else{
$output .= $entry["attr_value"];
}
$output .= '</td>';
}
}
$output .= '</tr>';
if( !empty($group_name) ){
$last_fname = $group_name;
$show_fname = '';
$bgcolor = '';
}
# lookup template himself
if ($item_class == 'template_inheritance'){
if ($local_template_status === "repetitive"){
NConf_DEBUG::set('', 'DEBUG', 'template is repetitive, stopping inheritance to prevent endless loop');
}else{
$template_on_template = db_templates("template_inheritance_direct", $entry["item_id"]);
if ( !empty($template_on_template) ){
//$output .= '<tr><td colspan=2><br><b>directly linked to service</b></td></tr>';
$output .= table_output($template_on_template, 'template_inheritance', '', ++$level);
}
}
}
}
}
return $output;
}
function apply_template(&$template_cache, $template_id){
if ( in_array($template_id, $template_cache) ){
return "repetitive";
}else{
array_push($template_cache, $template_id);
return TRUE;
}
}
mysqli_close($dbh);
require_once 'include/foot.php';
?>