-
Notifications
You must be signed in to change notification settings - Fork 51
/
history.php
222 lines (185 loc) · 6.93 KB
/
history.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
<?php
// JSON response, get history entries
###
# Handle different histories (basic or detail)
if ( !empty($_GET["id"]) ){
# Get History of a Item
$basic_query = ' fk_id_item='.$_GET["id"].'
AND action <> "edited"';
$show_item_links = FALSE;
}else{
$show_item_links = TRUE;
# Show all entries but still with sql filters (basic history entries, no details about services)
$basic_query =
' (action="created"'
. ' OR action="removed"'
. ' OR action="edited"'
. ' OR action="general" )'
. ' OR action ="module"'
. ' OR (action ="modified"'
. ' AND (attr_name = "Class" OR attr_name = "Attribute"))';
}
/* Paging */
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) AND ( !empty($_GET['iDisplayLength']) AND $_GET['iDisplayLength'] != "-1" ) )
{
$sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
mysql_real_escape_string( $_GET['iDisplayLength'] );
}
/* Ordering */
if ( isset( $_GET['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $i<mysql_real_escape_string( $_GET['iSortingCols'] ) ; $i++ )
{
$sOrder .= fnColumnToField(mysql_real_escape_string( $_GET['iSortCol_'.$i] ))."
".mysql_real_escape_string( $_GET['sSortDir_'.$i] ) .", ";
}
$sOrder = substr_replace( $sOrder, "", -2 );
}
/* Filtering */
$sWhere = "";
if ( $_GET['sSearch'] != "" )
{
# split each word on space
$search_words = explode(" ", $_GET['sSearch']);
$sWhere = "AND ( ";
$i = 0;
foreach ($search_words AS $search_word) {
# after the first search, add a AND
if ($i > 0) $sWhere .= ' AND ';
# concatenate all history fields with separator and search word
$sWhere .= "CONCAT_WS(',', LOWER(user_str), action, attr_name, LOWER(attr_value), timestamp ) LIKE '%".mysql_real_escape_string( strtolower($search_word) )."%'";
$i++;
}
$sWhere .= " ) ";
}
$sQuery = 'SELECT SQL_CALC_FOUND_ROWS *
FROM History
WHERE ('
. $basic_query
. ')';
$sQuery .= "
$sWhere
$sOrder
$sLimit
";
# for debugging query, but JSON load will then fail
if ($debug == "yes") echo $sQuery;
$rResult = mysql_query( $sQuery, $dbh ) or die('Query: '.$sQuery.'<br><br>'.mysql_error());
$sQuery = "
SELECT FOUND_ROWS()
";
$rResultFilterTotal = mysql_query( $sQuery, $dbh ) or die(mysql_error());
$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];
$sQuery = "
SELECT COUNT(id_hist)
FROM History
WHERE "
. $basic_query;
$rResultTotal = mysql_query( $sQuery, $dbh ) or die(mysql_error());
$aResultTotal = mysql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];
if (function_exists("json_encoded")){
// If json_ecode we use the php function to encode the array
// as example code from DataTables
$aColumns = array( 'timestamp', 'user_str', 'action', 'attr_name', 'attr_value', 'id_hist' );
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] == "version" )
{
$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
}
else if ( $aColumns[$i] != ' ' )
{
$row[] = $aRow[ $aColumns[$i] ];
}
}
# do some stuff bevore generating output
# if object name contains password, and PASSWD_DISPLAY is 0 (made in called function), do not show password
if ( stristr($aRow["attr_name"], "password") ){
$aRow["attr_value"] = show_password($aRow["attr_value"]);
}
# item linking
if ( !empty($aRow["fk_id_item"]) AND $show_item_links ){
if ($aRow["action"] == "removed" AND $aRow["attr_name"] == "service"){
# removed services will link to hosts service list view
$aRow['attr_value'] = '<a href="modify_item_service.php?id='.$aRow["fk_id_item"].'">'.$aRow["attr_value"].'</a>';
}else{
# all other entries will link to its detail view
$aRow['attr_value'] = '<a href="detail.php?id='.$aRow["fk_id_item"].'">'.$aRow["attr_value"].'</a>';
}
}else{
# entries which are removed, do not have any link
}
$output['aaData'][] = $row;
}
echo json_encode( $output );
}else{
// else we have a manual process to create JSON data
$sOutput = '{';
$sOutput .= '"sEcho": '.intval($_GET['sEcho']).', ';
$sOutput .= '"iTotalRecords": '.$iTotal.', ';
$sOutput .= '"iTotalDisplayRecords": '.$iFilteredTotal.', ';
$sOutput .= '"aaData": [ ';
while ( $aRow = mysql_fetch_array( $rResult ) )
{
# do some stuff bevore generating output
# if object name contains password, and PASSWD_DISPLAY is 0 (made in called function), do not show password
if ( stristr($aRow["attr_name"], "password") ){
$aRow["attr_value"] = show_password($aRow["attr_value"]);
}
# item linking
if ( !empty($aRow["fk_id_item"]) AND $show_item_links ){
if ($aRow["action"] == "removed" AND $aRow["attr_name"] == "service"){
# removed services will link to hosts service list view
$aRow['attr_value'] = '<a href="modify_item_service.php?id='.$aRow["fk_id_item"].'">'.$aRow["attr_value"].'</a>';
}else{
# all other entries will link to its detail view
$aRow['attr_value'] = '<a href="detail.php?id='.$aRow["fk_id_item"].'">'.$aRow["attr_value"].'</a>';
}
}else{
# entries which are removed, do not have any link
}
# create output (JSON)
$sOutput .= "[";
$sOutput .= '"'.addslashes($aRow['timestamp']).'",';
$sOutput .= '"'.addslashes($aRow['user_str']).'",';
$sOutput .= '"'.addslashes($aRow['action']).'",';
$sOutput .= '"'.addslashes($aRow['attr_name']).'",';
$sOutput .= '"'.addslashes($aRow['attr_value']).'",';
$sOutput .= '"'.addslashes($aRow['id_hist']).'"';
$sOutput .= "],";
}
$sOutput = substr_replace( $sOutput, "", -1 );
$sOutput .= '] }';
// This should fix a json problem
$sOutput = str_replace("\'", "'", $sOutput);
echo $sOutput;
}
function fnColumnToField( $i )
{
if ( $i == 0 )
return "timestamp";
else if ( $i == 1 )
return "user_str";
else if ( $i == 2 )
return "action";
else if ( $i == 3 )
return "attr_name";
else if ( $i == 4 )
return "attr_value";
else if ( $i == 5 )
return "id_hist";
}
?>