Repository navigation
Expand file tree
/
Copy pathrouter-compare.php
More file actions
433 lines (357 loc) · 13.7 KB
/
Copy pathrouter-compare.php
File metadata and controls
433 lines (357 loc) · 13.7 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
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
<?php
declare(strict_types = 1);
/*
+-------------------------------------------------------------------------+
| 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(__DIR__ . '/includes/functions.php');
if (cacti_version_compare(CACTI_VERSION, '1.2.23', '<')) {
print get_md5_include_css('plugins/routerconfigs/css/diff.css');
}
if (read_config_option('routerconfigs_presentation') == 'console') {
top_header();
} else {
general_header();
}
compare_validate_vars();
$device1 = get_request_var('device1');
$device2 = get_request_var('device2');
$file1 = get_request_var('file1');
$file2 = get_request_var('file2');
$diffmode = get_request_var('diffmode');
$files1 = [];
$files2 = [];
$devices = db_fetch_assoc_prepared('SELECT id, directory, hostname
FROM plugin_routerconfigs_devices
ORDER BY hostname', []);
if (cacti_sizeof($devices)) {
$default = 0;
foreach ($devices as $d) {
$default = $d['id'];
break;
}
if (!is_numeric($device1)) {
$device1 = $default;
}
if (!is_numeric($device2)) {
$device2 = $default;
}
}
if (is_numeric($device1)) {
$files1 = db_fetch_assoc_prepared('SELECT id, directory, filename
FROM plugin_routerconfigs_backups
WHERE device = ?
ORDER BY filename DESC', [$device1]);
} else {
$files1 = [];
}
if (is_numeric($device2)) {
$files2 = db_fetch_assoc_prepared('SELECT id, directory, filename
FROM plugin_routerconfigs_backups
WHERE device = ? ORDER BY filename DESC', [$device2]);
} else {
$files2 = [];
}
display_tabs();
html_start_box(__('Router Backup Comparison', 'routerconfigs'), '100%', false, 4, 'center', '');
?>
<tr class='even noprint'>
<td>
<form id='form_devices' action='router-devices.php'>
<table class='filterTable'>
<tr>
<td>
<?php print __('Diff Mode', 'routerconfigs'); ?>
</td>
<td>
<select id='diffmode'>
<option value='inline'<?php if (get_request_var('diffmode') == 'inline') {?> selected<?php }?>><?php print __('Inline','routerconfigs'); ?></option>
<option value='sdiff'<?php if (get_request_var('diffmode') == 'sdiff') {?> selected<?php }?>><?php print __('Side by Side','routerconfigs'); ?></option>
</select>
</td>
<td>
<?php print __('Context Lines', 'routerconfigs'); ?>
</td>
<td>
<select id='difflines'>
<?php
foreach ($rc_diff_context as $value => $text) {
print "<option value='" . $value . "'" . (get_request_var('difflines') == $value ? ' selected' : '') . '>' . html_escape($text) . '</option>';
}
?>
</select>
</td>
</tr>
</table>
</form>
<script type='text/javascript' <?php print plugin_routerconfigs_csp_nonce(); ?>>
$(function() {
$('#diffmode').change(function() {
var strURL = urlPath + 'plugins/routerconfigs/router-compare.php';
strURL += '?header=false';
strURL += '&diffmode=' + $('#diffmode').val();
strURL += '&difflines=' + $('#difflines').val();
strURL += '&file1=' + $('#file1').val();
strURL += '&file2=' + $('#file2').val();
loadPageNoHeader(strURL);
});
$('#difflines').change(function() {
var strURL = urlPath + 'plugins/routerconfigs/router-compare.php';
strURL += '?header=false';
strURL += '&diffmode=' + $('#diffmode').val();
strURL += '&difflines=' + $('#difflines').val();
strURL += '&file1=' + $('#file1').val();
strURL += '&file2=' + $('#file2').val();
loadPageNoHeader(strURL);
});
$('#device1').change(function() {
changeDeviceA();
});
$('#device2').change(function() {
changeDeviceB();
});
$('#file1, #file2').change(function() {
changeFileForm();
});
});
</script>
</tr>
<?php
html_end_box();
// show a filter form
form_start('router-compare.php', 'chk');
html_start_box('', '100%', false, 1, 'center', '');
html_header(['File', 'File']);
form_alternate_row();
print "<td width='50%'><select id='device1' name='device1'>";
foreach ($devices as $f) {
print '<option value="' . $f['id'] . '"' . ($device1 == $f['id'] ? ' selected' : '') . '>' . html_escape($f['directory'] . '/' . $f['hostname']) . '</option>';
}
print '</select><br>';
print "<select id='file1' name='file1'><option value='0'></option>";
foreach ($files1 as $f) {
print '<option value="' . $f['id'] . '"' . ($file1 == $f['id'] ? ' selected' : '') . '>' . html_escape($f['filename']) . '</option>';
}
print '</select></td>';
print "<td width='50%'><select id='device2' name='device2'>";
foreach ($devices as $f) {
print '<option value="' . $f['id'] . '"' . ($device2 == $f['id'] ? ' selected' : '') . '>' . html_escape($f['directory'] . '/' . $f['hostname']) . '</option>';
}
print '</select><br>';
print "<select id='file2' name='file2'><option value='0'></option>";
foreach ($files2 as $f) {
print '<option value="' . $f['id'] . '"' . ($file2 == $f['id'] ? ' selected' : '') . '>' . html_escape($f['filename']) . '</option>';
}
print '</select></td></tr>';
html_end_box(false);
form_end();
html_start_box(__('Compare Output', 'routerconfigs'), '100%', false, 1, 'center', '');
if (!empty($file1) && !empty($file2)) {
$device1 = db_fetch_row_prepared('SELECT * FROM plugin_routerconfigs_backups WHERE id = ?', [$file1]);
$device2 = db_fetch_row_prepared('SELECT * FROM plugin_routerconfigs_backups WHERE id = ?', [$file2]);
$device1 = is_array($device1) ? $device1 : [];
$device2 = is_array($device2) ? $device2 : [];
if (isset($device1['id'])) {
$filepath1 = plugin_routerconfigs_dir($device1['directory']) . basename($device1['filename']);
$resolved1 = realpath($filepath1);
$basedir1 = realpath(plugin_routerconfigs_dir($device1['directory']));
if ($basedir1 === false
|| ($resolved1 !== false
&& strpos($resolved1, rtrim($basedir1, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR) !== 0)
) {
$lines1 = ['File path validation failed for backup id ' . $file1];
} elseif ($resolved1 !== false && file_exists($resolved1)) {
$lines1 = @file($resolved1, FILE_IGNORE_NEW_LINES);
if ($lines1 === false) {
$lines1 = ['File \'' . $filepath1 . '\' (' . $file1 . ' ) failed to load'];
}
} else {
$lines1 = ['File \'' . $filepath1 . '\' (' . $file1 . ' ) missing'];
}
} else {
$lines1 = ['Unable to find backup id ' . $file1];
}
if (isset($device2['id'])) {
$filepath2 = plugin_routerconfigs_dir($device2['directory']) . basename($device2['filename']);
$resolved2 = realpath($filepath2);
$basedir2 = realpath(plugin_routerconfigs_dir($device2['directory']));
if ($basedir2 === false
|| ($resolved2 !== false
&& strpos($resolved2, rtrim($basedir2, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR) !== 0)
) {
$lines2 = ['File path validation failed for backup id ' . $file2];
} elseif ($resolved2 !== false && file_exists($resolved2)) {
$lines2 = @file($resolved2, FILE_IGNORE_NEW_LINES);
if ($lines2 === false) {
$lines2 = ['File \'' . $filepath2 . '\' (' . $file2 . ' ) failed to load'];
}
} else {
$lines2 = ['File \'' . $filepath2 . '\' (' . $file2 . ' ) missing'];
}
} else {
$lines2 = ['Unable to find backup id ' . $file2];
}
// Prefer the diff library bundled with Cacti core. As of Cacti 1.2.32 that
// is jfcherng/php-diff, loaded through core's Composer autoloader. Older
// supported releases (1.2.29 - 1.2.31) predate it, so fall back to the Horde
// Text_Diff library shipped with this plugin.
$use_cacti_diff = class_exists('\Jfcherng\Diff\Differ');
$diffcontext = (int) get_request_var('difflines');
if ($diffcontext < 1) {
$diffcontext = 3;
}
if ($use_cacti_diff) {
$differOptions = [
'context' => $diffcontext,
'ignoreWhitespace' => true,
'ignoreCase' => false
];
$differ = new \Jfcherng\Diff\Differ($lines1, $lines2, $differOptions);
if (get_request_var('diffmode') == 'sdiff') {
$renderer = \Jfcherng\Diff\Factory\RendererFactory::make('SideBySide');
} else {
$renderer = \Jfcherng\Diff\Factory\RendererFactory::make('Inline');
}
$text = $renderer->render($differ);
} else {
require_once(__DIR__ . '/includes/HordeText.php');
$diff = new Horde_Text_Diff('Native', [$lines1, $lines2]);
if (get_request_var('diffmode') == 'sdiff') {
$renderer = new Horde_Text_Diff_Renderer_table(['auto']);
} else {
$renderer = new Horde_Text_Diff_Renderer_Unified([
'leading_context_lines' => $diffcontext,
'trailing_context_lines' => $diffcontext
]);
}
$text = $renderer->render($diff);
}
html_start_box('', '100%', false, 1, 'center', '');
if (get_request_var('diffmode') == 'sdiff') {
$label1 = isset($device1['directory']) && isset($device1['filename']) ?
$device1['directory'] . '/' . $device1['filename'] :
__('Backup %s not found', $file1, 'routerconfigs');
$label2 = isset($device2['directory']) && isset($device2['filename']) ?
$device2['directory'] . '/' . $device2['filename'] :
__('Backup %s not found', $file2, 'routerconfigs');
html_header([$label1, $label2]);
if (!$use_cacti_diff) {
print "<tr height='1'><td width='50%'></td><td width='50%'></td></tr>";
}
}
if (trim($text) == '') {
print '<tr><td colspan="2"><center>' . __('There are no Changes', 'routerconfigs') . '</center></td></tr>';
} elseif ($use_cacti_diff) {
// jfcherng renderers return a complete, already HTML-escaped <table>.
// Print it verbatim in a single full-width cell; the legacy newline/row
// substitutions below are for the Horde renderers and would corrupt this
// markup, exploding large diffs into thousands of <br> tags.
print '<tr><td colspan="2">' . $text . '</td></tr>';
} else {
$text = str_replace("\n", '<br>', $text);
$text = str_replace('</td></tr>', '</td></tr>' . "\n", $text);
print $text;
}
html_end_box(false);
} else {
print '<tr><td><h3>' . __('Error, you must have backups for each device.', 'routerconfigs') . '</h3></td></tr>';
}
html_end_box();
?>
<script type='text/javascript' <?php print plugin_routerconfigs_csp_nonce(); ?>>
function changeDeviceA () {
strURL = 'router-compare.php?header=false&device1='+$('#device1').val();
strURL += '&device2='+$('#device2').val();
strURL += '&file1=';
strURL += '&file2='+$('#file2').val();
loadPageNoHeader(strURL);
}
function changeDeviceB () {
strURL = 'router-compare.php?header=false&device1='+$('#device1').val();
strURL += '&device2='+$('#device2').val();
strURL += '&file1='+$('#file1').val();
strURL += '&file2=';
loadPageNoHeader(strURL);
}
function changeFileForm () {
strURL = 'router-compare.php?header=false&device1=' + $('#device1').val();
strURL += '&device2='+$('#device2').val();
strURL += '&file1='+$('#file1').val();
strURL += '&file2='+$('#file2').val();
loadPageNoHeader(strURL);
}
</script>
<?php
bottom_footer();
/**
* Validates and stores the backup-comparison page's filter variables
* (diff mode, context lines, the two devices/files being compared) in the
* session. Called from this file's top-level flow before the two backup
* files are looked up and diffed.
*
* @return void
*
* @global array $rc_diff_context The offered context-line choices, used to
* constrain the stored 'difflines' value.
*/
function compare_validate_vars() {
global $rc_diff_context;
// ================= input validation and session storage =================
$filters = [
'diffmode' => [
'filter' => FILTER_CALLBACK,
'default' => 'inline',
'options' => ['options' => 'sanitize_search_string']
],
'difflines' => [
'filter' => FILTER_VALIDATE_INT,
'default' => read_config_option('routerconfigs_diff_context') ?: '3',
],
'device1' => [
'filter' => FILTER_VALIDATE_INT,
'default' => '',
],
'device2' => [
'filter' => FILTER_VALIDATE_INT,
'default' => '',
],
'file1' => [
'filter' => FILTER_CALLBACK,
'default' => '',
'options' => ['options' => 'sanitize_search_string']
],
'file2' => [
'filter' => FILTER_CALLBACK,
'default' => '',
'options' => ['options' => 'sanitize_search_string']
],
];
validate_store_request_vars($filters, 'sess_rc_compare');
// Constrain 'difflines' to one of the offered choices so a crafted URL or a
// stale session value cannot desync the dropdown from the rendered context.
$allowed = array_map('intval', array_keys($rc_diff_context));
if (!in_array((int) get_request_var('difflines'), $allowed, true)) {
set_request_var('difflines', read_config_option('routerconfigs_diff_context') ?: '3');
}
// ================= input validation =================
}