Repository navigation
Expand file tree
/
Copy pathmactrack_vendormacs.php
More file actions
335 lines (288 loc) · 10.5 KB
/
Copy pathmactrack_vendormacs.php
File metadata and controls
335 lines (288 loc) · 10.5 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
<?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/mactrack/lib/mactrack_functions.php');
set_default_action();
if (isset_request_var('export')) {
mactrack_vmacs_export();
} else {
top_header();
mactrack_vmacs();
bottom_footer();
}
/**
* Validates and stores this view's filter request variables (rows,
* page, filter text, sort column/direction) into the session for the
* vendor MACs list view.
*
* @return void
*/
function mactrack_vmacs_validate_request_vars(): void {
// ================= input validation and session storage =================
$filters = [
'rows' => [
'filter' => FILTER_VALIDATE_INT,
'pageset' => true,
'default' => '-1'
],
'page' => [
'filter' => FILTER_VALIDATE_INT,
'default' => '1'
],
'filter' => [
'filter' => FILTER_CALLBACK,
'pageset' => true,
'default' => '',
'options' => ['options' => 'sanitize_search_string']
],
'sort_column' => [
'filter' => FILTER_CALLBACK,
'default' => 'vendor_mac',
'options' => ['options' => 'sanitize_search_string']
],
'sort_direction' => [
'filter' => FILTER_CALLBACK,
'default' => 'ASC',
'options' => ['options' => 'sanitize_search_string']
]
];
validate_store_request_vars($filters, 'sess_mt_vmacs');
// ================= input validation =================
}
/**
* Exports the current filtered OUI vendor MAC database entries as a
* downloaded CSV file. Called from this script's main
* request-dispatch switch when action=export.
*
* @return void
*
* @global array $site_actions Reserved/declared for parity with other
* functions in this file; not used directly
* here.
* @global array $config Cacti global configuration array (declared
* but not used directly here).
*/
function mactrack_vmacs_export(): void {
global $site_actions, $config;
mactrack_vmacs_validate_request_vars();
$sql_where = '';
$vmacs = mactrack_vmacs_get_vmac_records($sql_where, 0, false);
$xport_array = [];
array_push($xport_array, '"vendor_mac","vendor_name","vendor_address"');
if (cacti_sizeof($vmacs)) {
foreach ($vmacs as $vmac) {
array_push($xport_array,'"' . mactrack_format_mac($vmac['vendor_mac']) . '","' .
$vmac['vendor_name'] . '","' .
$vmac['vendor_address'] . '"');
}
}
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename=cacti_site_xport.csv');
foreach ($xport_array as $xport_line) {
print $xport_line . "\n";
}
}
/**
* Builds and executes the SQL query for the OUI vendor MAC database
* list view, applying the current filter request variable, sort
* order, and optional row limits.
*
* @param string &$sql_where Receives the generated SQL WHERE clause
* for reuse by the caller (e.g. for a
* matching COUNT query).
* @param int $rows Number of rows per page, used to compute
* the SQL LIMIT clause when $apply_limits
* is true.
* @param bool $apply_limits Whether to apply a SQL LIMIT clause
* (default true).
*
* @return array The matching vendor MAC (OUI) database records.
*/
function mactrack_vmacs_get_vmac_records(&$sql_where, $rows, $apply_limits = true): array {
$sql_where = '';
// form the 'where' clause for our main sql query
if (get_request_var('filter') != '') {
$filter = str_replace(['.','-',':'], ['','',''], get_request_var('filter'));
$sql_where = 'WHERE (mac_track_oui_database.vendor_name LIKE ' . db_qstr('%' . $filter . '%') . ' OR ' .
'mac_track_oui_database.vendor_mac LIKE ' . db_qstr('%' . $filter . '%') . ' OR ' .
'mac_track_oui_database.vendor_address LIKE ' . db_qstr('%' . $filter . '%') . ')';
}
$sql_order = get_order_string();
if ($apply_limits) {
$sql_limit = ' LIMIT ' . ($rows * (get_request_var('page') - 1)) . ', ' . $rows;
} else {
$sql_limit = '';
}
$query_string = "SELECT *
FROM mac_track_oui_database
$sql_where
$sql_order
$sql_limit";
return db_fetch_assoc($query_string);
}
/**
* Renders the main OUI vendor MAC database list page: displays the
* vendor MAC filter form, validates/stores this view's filter request
* variables, then displays a filtered, sorted, paginated table of
* vendor MAC entries. Called from this script's main request-dispatch
* switch as the default view.
*
* @return void
*
* @global array $site_actions Reserved/declared for parity with other
* functions in this file; not used directly
* here.
* @global array $config Cacti global configuration array (declared
* but not used directly here).
* @global int $item_rows Default number of rows per page from
* Cacti settings.
*/
function mactrack_vmacs(): void {
global $site_actions, $config, $item_rows;
mactrack_vmacs_validate_request_vars();
$rows = plugin_get_rows_per_page();
html_start_box(__('Mactrack Vendor Mac Filter', 'mactrack'), '100%', '', '3', 'center', '');
mactrack_vmac_filter();
html_end_box();
$sql_where = '';
$vmacs = mactrack_vmacs_get_vmac_records($sql_where, $rows);
$total_rows = db_fetch_cell("SELECT
COUNT(*)
FROM mac_track_oui_database
$sql_where");
$display_text = [
'vendor_mac' => [__('Vendor MAC', 'mactrack'), 'ASC'],
'vendor_name' => [__('Corporation', 'mactrack'), 'ASC'],
'vendor_address' => [__('Address', 'mactrack'), 'ASC']
];
$columns = cacti_sizeof($display_text);
$nav = html_nav_bar('mactrack_vendormacs.php', MAX_DISPLAY_PAGES, get_request_var('page'), $rows, $total_rows, $columns, __('Vendor Macs', 'mactrack'), 'page', 'main');
print $nav;
html_start_box('', '100%', '', '3', 'center', '');
html_header_sort($display_text, get_request_var('sort_column'), get_request_var('sort_direction'));
if (cacti_sizeof($vmacs)) {
foreach ($vmacs as $vmac) {
form_alternate_row();
?>
<td class='linkEditMain'><?php print mactrack_format_mac($vmac['vendor_mac']); ?></td>
<td><?php print filter_value($vmac['vendor_name'], get_request_var('filter')); ?></td>
<td><?php print filter_value($vmac['vendor_address'], get_request_var('filter')); ?></td>
</tr>
<?php
}
} else {
print '<tr><td colspen="' . $columns . '"><em>' . __('No Mactrack Vendor MACS Found', 'mactrack') . '</em></td></tr>';
}
html_end_box(false);
if (cacti_sizeof($vmacs)) {
print $nav;
}
}
/**
* Renders the search/rows filter form controls for the vendor MAC
* database list view.
*
* @return void
*
* @global array $item_rows Rows-per-page option list used to populate
* the rows dropdown.
*/
function mactrack_vmac_filter(): void {
global $item_rows;
?>
<tr class='even'>
<td>
<form id='mactrack'>
<table class='filterTable'>
<tr>
<td>
<?php print __('Search', 'mactrack'); ?>
</td>
<td>
<input type='text' id='filter' size='25' value='<?php print html_escape_request_var('filter'); ?>'>
</td>
<td>
<?php print __('MAC\'s', 'mactrack'); ?>
</td>
<td>
<select id='rows'>
<option value='-1'<?php if (get_request_var('rows') == '-1') {?> selected<?php }?>><?php print __('Default', 'mactrack'); ?></option>
<?php
if (cacti_sizeof($item_rows) > 0) {
foreach ($item_rows as $key => $value) {
print "<option value='" . $key . "'";
if (get_request_var('rows') == $key) {
print ' selected';
} print '>' . $value . '</option>';
}
}
?>
</select>
</td>
<td>
<span class='nowrap'>
<button type='submit' id='go' class='ui-button ui-corner-all ui-widget ui-state-active'><?php print __esc('Go', 'mactrack'); ?></button>
<button type='button' id='clear' class='ui-button ui-corner-all ui-widget'><?php print __esc('Clear', 'mactrack'); ?></button>
<button type='button' id='export' class='ui-button ui-corner-all ui-widget'><?php print __esc('Export', 'mactrack'); ?></button>
</span>
</td>
</tr>
</table>
</form>
<script type='text/javascript' <?php print plugin_mactrack_csp_nonce(); ?>>
function applyFilter() {
strURL = urlPath+'plugins/mactrack/mactrack_vendormacs.php?header=false';
strURL += '&filter=' + $('#filter').val();
strURL += '&rows=' + $('#rows').val();
loadPageNoHeader(strURL);
}
function clearFilter() {
strURL = urlPath+'plugins/mactrack/mactrack_vendormacs.php?header=false&clear=true';
loadPageNoHeader(strURL);
}
function exportRows() {
strURL = urlPath+'plugins/mactrack/mactrack_vendormacs.php?export=true';
document.location = strURL;
}
$(function() {
$('#rows').on('change', function() {
applyFilter();
});
$('#mactrack').on('submit', function(event) {
event.preventDefault();
applyFilter();
});
$('#clear').on('click', function() {
clearFilter();
});
$('#export').on('click', function() {
exportRows();
});
});
</script>
</td>
</tr>
<?php
}