forked from nconf/nconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_item.php
312 lines (245 loc) · 10.6 KB
/
delete_item.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
<?php
require_once 'include/head.php';
if (DB_NO_WRITES == 1) {
message($info, TXT_DB_NO_WRITES);
}
if ( !empty($_REQUEST["class"]) ){
$class = $_REQUEST["class"];
}elseif( !empty($_REQUEST["item"]) ){
$class = $_REQUEST["item"];
}
# delete item count
$deleted_items = 0;
echo NConf_HTML::title('Delete Items');
if( ( ( isset($_POST["delete"]) ) AND ($_POST["delete"] == "yes") ) AND
( ( isset($_POST["ids"]) ) AND ($_POST["ids"] != "") )
){
# make ids as array
if ( !empty($_POST["ids"]) ){
$ids = explode(",", $_POST["ids"]);
}
foreach ($ids as $id){
$item_name = db_templates("naming_attr", $id);
$item_class = db_templates("class_name", $id);
# Delete Services if item = host
if ($item_class == "host"){
# when deleting a host, go back to host overview (the session var could link to the service list)
$_SESSION["after_delete_page"] = 'overview.php?class=host';
# Select all linked services
$result = db_templates("get_services_from_host_id", $id);
if ( !empty($result) ){
if ( count($result) > 0 ){
foreach ($result AS $service_ID => $service_name){
$query = 'DELETE FROM ConfigItems
WHERE id_item='.$service_ID
;
if (DB_NO_WRITES != 1) {
$result_delete = db_handler($query, "delete", "Delete entry");
if ( $result_delete ){
message ($debug, '', "ok");
history_add("removed", "service", $service_name, $id);
}else{
message ($debug, '', "failed");
}
}
}
}
}else{
message ($debug, '', "failed");
}
} //END $item_class == "host"
# Services: Bevore deleting the entry, check the host ID, for later history entry
if ($class == "service"){
$Host_ID = db_templates("hostID_of_service", $id);
}
// Delete entry
$query = 'DELETE FROM ConfigItems
WHERE id_item='.$id;
$result = db_handler($query, "result", "Delete entry");
if ( $result ){
# increase deleted items
if (mysql_affected_rows() > 0){
$deleted_items++;
}
message ($debug, '', "ok");
# Special service handling
if ( ($class == "service") AND !empty($Host_ID) ){
# Enter also the Host_ID of the deleted service into the History table
history_add("removed", $class, $item_name, $Host_ID);
}else{
# Enter normal deletion, which object is deleted, without a "parent / linked" id
history_add("removed", $class, $item_name );
}
// Go to next page without pressing the button (also have a look if class is not host, otherwise go back to overview )
if ( !empty($_POST["from"]) AND $class != "host" ){
$url = $_POST["from"];
}else{
$url = $_SESSION["after_delete_page"];
}
}elseif (DB_NO_WRITES != 1){
message($error, 'Error deleting '.$id.':'.$query);
}
} // foreach
# Feedback of delete action
if ($deleted_items > 0){
//echo TXT_DELETED.' '.$deleted_items.' item(s)<br>';
NConf_DEBUG::set(TXT_DELETED.' '.$deleted_items.' item(s)', 'INFO');
}else{
NConf_DEBUG::set('No item(s) deleted', 'INFO');
}
echo NConf_DEBUG::show_debug('INFO', TRUE);
if ( !NConf_DEBUG::status('ERROR') ){
echo '<meta http-equiv="refresh" content="'.(REDIRECTING_DELAY+1).'; url='.$url.'">';
NConf_DEBUG::set('<a href="'.$url.'"> [ this page ] </a>', 'INFO', "<br>redirecting to");
}
}elseif( !empty($_GET["ids"])
OR (
( isset($_GET["type"]) AND $_GET["type"] == "multidelete" )
AND !empty($_POST["advanced_items"])
)
){
// Go to next page without pressing the button (also have a look if delete comes from detailview
if ( isset($_GET["from"]) AND ($_GET["from"] != "") ){
$url = $_GET["from"];
}elseif ( !empty($_SESSION["after_delete_page"]) ){
$url = $_SESSION["after_delete_page"];
}
# make ids as array
if ( !empty($_GET["ids"]) ){
$ids = explode(",", $_GET["ids"]);
}elseif (!empty($_POST["advanced_items"]) ){
$ids = $_POST["advanced_items"];
}
# list of ids to send with confirmation of deletion
$id_list = implode(",", $ids);
foreach ($ids as $id ){
// CHECK IF A USER TRIES TO DELETE AN ADMIN
if ( !empty($id) ) $nc_id = $id;
if ($_SESSION["group"] != "admin" AND $class == "contact"){
$nc_permission_query = 'SELECT attr_value FROM ConfigValues, ConfigAttrs WHERE fk_id_attr=id_attr AND fk_id_item="'.$nc_id.'" AND attr_name = "nc_permission"';
$nc_permission = db_handler($nc_permission_query, "getOne", "Look for nc_permissions of user");
if ($nc_permission == "admin"){
// Disable the submit button and add message
include('include/stop_user_modifying_admin_account.php');
}
}
}
# Modify question when deleting services
if ($class == "service"){
$content = "Do you really want to delete these services? The hosts will not be deleted.";
}else{
$content = "Do you really want to delete these item(s) ?";
}
// Buttons
$content_button = '
<form name="delete_item" action="delete_item.php?item='.$class.'" method="post">
<input type="hidden" name="ids" value="'.$id_list.'">
<input type="hidden" name="from" value="'.$url.'">
<input type="hidden" name="delete" value="yes">
<div id=buttons><br>';
$content_button .= '<input type="Submit" value="Delete" name="submit" align="middle" ';
if(DB_NO_WRITES == 1) $content_button .= 'disabled';
$content_button .= '> ';
$content_button .= '<input type=button onClick="window.location.href=\''.$_SESSION["go_back_page"].'\'" value="Back">';
$content_button .= '</form>';
echo NConf_HTML::limit_space(
NConf_HTML::show_highlight('WARNING', $content.$content_button)
);
if(DB_NO_WRITES == 1) echo NConf_DEBUG::show_debug('INFO', TRUE);
foreach ($ids as $id) {
# Delete Services if item = host
$item_class = db_templates("class_name", $id);
if ($item_class == "host"){
# WARN services linked to host
$get_srv_query = 'SELECT attr_value, ConfigValues.fk_id_item AS item_id,"service" AS config_class,
"service name" AS friendly_name
FROM ConfigValues, ConfigAttrs, ConfigClasses, ItemLinks
WHERE id_attr = ConfigValues.fk_id_attr
AND naming_attr = "yes"
AND id_class = fk_id_class
AND config_class = "service"
AND ConfigValues.fk_id_item = ItemLinks.fk_id_item
AND fk_item_linked2 = '.$id.'
ORDER BY attr_value';
$result = db_handler($get_srv_query, "result", "get services linked to host");
# prepare services
$services = array();
while($entry = mysql_fetch_assoc($result)){
$services[] = array(
"id" => $entry["item_id"],
"name" => $entry["attr_value"],
"type" => "service" ) ;
}
}
# Lookup class and name of item
$item_class = db_templates("class_name", $id);
$item_name = db_templates("naming_attr", $id);
# on service items we want to group it by their associated hostname
if ($item_class == "service"){
# service deletions
# get host name of service
$hostID = db_templates("hostID_of_service", $id);
$hostname = db_templates("naming_attr", $hostID);
# create hostname entrie
if ( !isset($entries[$hostname]) ){
$entries[$hostname] = array(
"id" => $hostID,
"name" => $hostname,
"title" => "",
"status" => "open" );
}
# add the service to the host branch
$services = array(
"id" => $id,
"name" => $item_name,
"type" => "service" ) ;
$entries[$hostname]["childs"][] = $services;
}else{
# any other deletion:
if (!empty($services) ){
# for host trees with services (class = host)
$entries[] = array(
"id" => $id,
"name" => $item_name,
"title" => $item_class.": ",
"status" => "open",
"childs" => $services );
}else{
# for single (any other) classes
$entries[] = array(
"id" => $id,
"name" => $item_name,
"title" => $item_class.": ",
"type" => $item_class);
}
}
}//foreach
# service deletion: sort the array on hostnames
ksort($entries);
# Modify text on top tree element (root) when deleting services
if ($item_class == "service"){
$items = "services";
}else{
$items = "items";
}
$tree_view = array( "root" => array(
"id" => "root",
"status" => 'open',
"name" => "The following $items will be deleted",
"type" => "parent",
"childs" => $entries) );
# display tree
echo '<br><div>';
displayTree_list($tree_view);
echo '</div>';
}else{
NConf_DEBUG::set("No item to delete", "ERROR");
}
if ( NConf_DEBUG::status('ERROR') ) {
echo NConf_HTML::limit_space( NConf_HTML::show_error() );
echo "<br><br>";
echo NConf_HTML::back_button($_SESSION["go_back_page"]);
}
mysql_close($dbh);
require_once 'include/foot.php';
?>