Repository navigation
Expand file tree
/
Copy pathrouter-download.php
More file actions
528 lines (453 loc) · 16.3 KB
/
Copy pathrouter-download.php
File metadata and controls
528 lines (453 loc) · 16.3 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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
<?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/ |
+-------------------------------------------------------------------------+
*/
// do NOT run this script through a web browser
if (!isset($_SERVER['argv'][0]) || isset($_SERVER['REQUEST_METHOD']) || isset($_SERVER['REMOTE_ADDR'])) {
die('<br><strong>This script is only meant to run at the command line.</strong>');
}
routerconfigs_define_exit('EXIT_UNKNOWN',-1, "ERROR: Failed due to unknown reason\n");
routerconfigs_define_exit('EXIT_NORMAL', 0, '');
routerconfigs_define_exit('EXIT_ARGERR', 1, "ERROR: Invalid Argument (%s)\n\n");
routerconfigs_define_exit('EXIT_NONNUM', 2, "ERROR: Argument is not numeric (%s)\n\n");
routerconfigs_define_exit('EXIT_ARGMIS', 3, "ERROR: Argument requires value (%s)\n\n");
routerconfigs_define_exit('EXIT_OPTERR', 4, "ERROR: Invalid option specification (%s)\n\n");
// We are not talking to the browser
$no_http_headers = true;
$dir = __DIR__;
chdir($dir);
if (strpos($dir, 'plugins') !== false) {
chdir('../../');
}
$shortOpts = 'VvH:h:DdBbRrFf';
$longOpts = [
'device:',
'devices:',
'debug',
'debug-buffer',
'retry',
'force',
'version',
'help',
'simulate-schedule'
];
$remaining = '';
$options = routerconfigs_getopts($shortOpts, $longOpts, $remaining);
require('./include/global.php');
require_once(__DIR__ . '/includes/functions.php');
error_reporting(E_ALL ^ E_DEPRECATED);
// setup defaults
$retryMode = false;
$debugBuffer = false;
$debug = false;
$force = false;
$devices = [];
$simulate = false;
foreach ($options as $arg => $value) {
switch ($arg) {
case 'h':
case 'host':
case 'hosts':
case 'device':
case 'devices':
if (!is_array($value)) {
$value = [$value];
}
foreach ($value as $deviceId) {
$deviceIds = explode(',',$deviceId);
foreach ($deviceIds as $deviceId) {
if (!is_numeric($deviceId)) {
routerconfigs_fail(EXIT_NONNUM, $deviceId);
}
// echo "Adding device $deviceId\n";
$devices[] = $deviceId;
}
}
break;
case 'd':
case 'debug':
$debug = true;
break;
case 'b':
case 'debug-buffer':
$debug = true;
$debugBuffer = true;
break;
case 'simulate-schedule':
$simulate = true;
break;
case 'f':
case 'force':
$force = true;
break;
case 'q':
case 'quiet':
$debug = false;
$debugBuffer = false;
$quiet = true;
break;
case 'r':
case 'retry':
$retryMode = true;
break;
default:
routerconfigs_fail(EXIT_ARGERR, $arg, true);
}
}
if (strlen($remaining)) {
routerconfigs_fail(EXIT_ARGERR, $remaining, true);
}
$devices = array_unique($devices);
plugin_routerconfigs_download($retryMode, $force, $devices, $debugBuffer, $simulate);
exit(EXIT_NORMAL);
/**
* Prints the message registered for a CLI exit code (via
* routerconfigs_define_exit()), optionally with printf-style arguments
* substituted in, then terminates the script with that exit code.
* Called throughout this script's argument parsing to report invalid
* CLI usage.
*
* @param int $exit_value The exit code to report and terminate
* with.
* @param mixed $args A single value or array of values to
* substitute into the registered message via
* printf(); defaults to an empty array.
* @param bool|int $display_help Currently unused placeholder for
* optionally showing usage help; defaults to
* 0.
*
* @return void This function always terminates script execution via
* exit and therefore never returns.
*
* @global bool $quiet When true, suppresses the printed message
* (the script still exits with $exit_value).
* @global array $fail_msg The exit-code => message map populated by
* routerconfigs_define_exit().
*/
function routerconfigs_fail($exit_value,$args = [],$display_help = 0) {
global $quiet,$fail_msg;
if (!$quiet) {
if (!isset($args)) {
$args = [];
} elseif (!is_array($args)) {
$args = [$args];
}
if (!array_key_exists($exit_value,$fail_msg)) {
$format = $fail_msg[EXIT_UNKNOWN];
} else {
$format = $fail_msg[$exit_value];
}
call_user_func_array('printf', array_merge((array)$format, $args));
if ($display_help) {
// display_help();
}
}
exit($exit_value);
}
/**
* Defines a named exit-code constant and registers its associated
* failure message text (indexed by both name and value) for later use
* by routerconfigs_fail(). Called at the top of this script to register
* each of its EXIT_* constants.
*
* @param string $name The constant name to define (e.g. 'EXIT_ARGERR').
* @param int $value The exit code value to assign.
* @param string $text The printf-style message format associated with
* this exit code.
*
* @return void
*
* @global array $fail_msg The exit-code => message map being built up.
*/
function routerconfigs_define_exit($name, $value, $text) {
global $fail_msg;
$fail_msg ??= [];
define($name,$value);
$fail_msg[$name] = $text;
$fail_msg[$value] = $text;
}
/**
* A hand-rolled command-line argument parser: builds the short/long
* option definitions and walks $_SERVER['argv'], matching each argument
* against them (via routerconfigs_getopts_find()), collecting repeated
* options into arrays, and accumulating any unrecognized arguments into
* $remaining. Called at the top of this script to parse its CLI
* arguments before Cacti's include/global.php is even loaded.
*
* @param string $short The short-option specification string
* (getopt()-style, e.g. 'Vv').
* @param array $long The long-option specification array
* (getopt()-style, e.g. array('device:')).
* @param string $remaining Reference, set to any arguments that
* didn't match a known option; defaults
* to ''.
*
* @return array Map of matched option name to its value (or array of
* values, if repeated).
*/
function routerconfigs_getopts($short, $long, &$remaining = '') {
$remaining = '';
$argv = $_SERVER['argv'];
$argc = $_SERVER['argc'];
$result = [];
$options = [];
routerconfigs_getopts_short($options, $short);
routerconfigs_getopts_long($options, $long);
$ignoreOptions = false;
for ($loop = 1; $loop < $argc; $loop++) {
$name = null;
$value = null;
$arg = $argv[$loop];
if ($arg == '--') {
$ignoreOptions = true;
} else {
$option = $ignoreOptions ? null : routerconfigs_getopts_find($arg, $options);
if ($option != null) {
if ($option['value']) {
if (!isset($option['result']) && $loop < $argc - 1) {
$option2 = routerconfigs_getopts_find($argv[$loop + 1], $options);
if ($option2 == null) {
$option['result'] = $argv[$loop + 1];
$loop++;
}
}
if (!$option['optional'] && !isset($option['result'])) {
routerconfigs_fail(EXIT_ARGMIS,$option['text']);
}
}
$name = $option['text'];
$value = $option['result'] ?? '';
if (array_key_exists($name, $result)) {
$result_val = $result[$name];
if (!is_array($result_val)) {
$result_val = [$result_val];
}
$result_val[] = $value;
} else {
$result_val = $value;
}
$result[$name] = $result_val;
} else {
$remaining .= (strlen($remaining) ? ' ' : '') . $arg;
}
}
}
return $result;
}
/**
* Parses a getopt()-style long-option specification array into this
* parser's internal option definitions, appending them to $options via
* routerconfigs_addopt(). Called from routerconfigs_getopts() to build
* the long-option half of the option table.
*
* @param array $options Reference, the option definitions array being
* built up.
* @param array $long The long-option specification array to parse
* (each entry optionally suffixed with ':' for a
* required value or '::' for an optional one).
*
* @return void
*/
function routerconfigs_getopts_long(array &$options, array &$long) {
if (!is_array($long)) {
$long = [$long];
}
// This parser runs before include/global.php, so the cacti_*() wrappers do not exist yet; use plain PHP here.
if (!empty($long)) {
$index = 0;
foreach ($long as $long_text) {
$long_text = trim($long_text);
if (strlen($long_text) == 0 || !preg_match("~[A-Za-z0-9:\-]~", $long_text)) {
routerconfigs_fail(EXIT_OPTERR,$long_text);
}
$long_val = routerconfigs_checkopt_string('value',$long_text);
$long_opt = routerconfigs_checkopt_string('optional',$long_text);
if ($long_opt) {
$long_text = substr($long_text, 0, -1);
}
routerconfigs_addopt($options, count($options), $long_text, $long_val, $long_opt);
}
}
}
/**
* Parses a getopt()-style short-option specification string into this
* parser's internal option definitions, appending them to $options via
* routerconfigs_addopt(). Called from routerconfigs_getopts() to build
* the short-option half of the option table.
*
* @param array $options Reference, the option definitions array being
* built up.
* @param string $short The short-option specification string to parse
* (getopt()-style, e.g. 'Vv').
*
* @return void
*/
function routerconfigs_getopts_short(array &$options, $short) {
if (!preg_match('~[A-Za-z0-9:]~', $short)) {
routerconfigs_fail(EXIT_OPTERR,$short);
}
$options = [];
for ($loop = 0; $loop < strlen($short); $loop++) {
$short_text = $short[$loop];
$short_val = routerconfigs_checkopt('value', $short, $loop);
$short_opt = routerconfigs_checkopt('value', $short, $loop);
routerconfigs_addopt($options, $loop, $short_text, $short_val, $short_opt);
}
}
/**
* Finds the option definition matching a single command-line argument
* (a '-x' or '--long[=value]' style token), extracting an inline
* '=value' when present. Called from routerconfigs_getopts() for each
* argument encountered.
*
* @param string $arg The raw command-line argument token to match.
* @param array $options The option definitions to match against.
*
* @return array|null The matching option definition (with 'result' set
* if an inline value was found), or null if no
* option matched.
*/
function routerconfigs_getopts_find($arg, array $options) {
$found = null;
if (strlen($arg) && $arg[0] == '-') {
while (strlen($arg) && $arg[0] == '-') {
$arg = substr($arg,1);
}
foreach ($options as $option) {
if ($arg == $option['text']) {
$found = $option;
unset($found['result']);
}
$length_arg = strlen($arg);
$length_txt = strlen($option['text']);
$substr_txt = substr($arg,0,$length_txt);
// echo sprintf("%3d arg, %3d txt, %15s = %s\n", $length_arg, $length_txt, $option['text'], $substr_txt);
if ($length_arg > $length_txt && $substr_txt == $option['text']) {
$separator_pos = strlen($option['text']);
$separator = $arg[$separator_pos];
// echo "$arg $separator found\n";
if ($separator == '=') {
$found = $option;
$separator_pos++;
if ($separator_pos < strlen($arg)) {
$substr_txt = substr($arg, $separator_pos);
// echo "Setting $substr_txt\n";
$found['result'] = $substr_txt;
} else {
// echo "Unsetting result\n";
unset($found['result']);
}
}
}
}
}
// echo sprintf("routerconfigs_getopts_find('%s', options()) return %s%s\n",
// $arg,
// $found == null ? '<null>' : str_replace("\n","",var_export($found, true)),
// $found == null ? '' : ' '.(isset($found['result']) ? str_replace("\n","",var_export($found,true)) : '<null>'));
return $found;
}
/**
* Appends a single option definition to the option definitions array.
* Called from routerconfigs_getopts_long() and
* routerconfigs_getopts_short() for each parsed option.
*
* @param array $options Reference, the option definitions array to
* append to.
* @param mixed $index Unused positional index (kept for parity with
* the short-option parser's loop variable).
* @param string $text The option's name/flag text.
* @param bool $val Whether this option takes a value.
* @param bool $opt Whether this option's value is optional (only
* meaningful when $val is true).
*
* @return void
*/
function routerconfigs_addopt(array &$options, $index, $text, $val, $opt) {
$option = [
'text' => $text,
'value' => $val,
'optional' => $opt
];
// echo sprintf("Adding option %2s (%3d%s%s)\n", $text, $index, ($val?' hasValue':''), ($opt?' hasOptional':''));
$options[] = $option;
}
/**
* Checks whether a short-option specification character at $index is
* followed by a ':' (indicating it takes a value), advancing $index past
* the colon when found. Called from routerconfigs_getopts_short() for
* each character of the short-option specification string.
*
* @param string $label Unused label parameter (kept for parity with
* other checkopt helpers; not used directly here).
* @param string $short The short-option specification string being
* scanned.
* @param int $index Reference, the current position in $short;
* advanced by one if a value-indicator colon is
* found.
*
* @return bool True if this option takes a value, false otherwise.
*/
function routerconfigs_checkopt($label, $short, &$index) {
$result = false;
$colon = '<unset>';
if ($index < strlen($short) - 1) {
$colon = $short[$index + 1];
if ($colon == ':') {
$index++;
$result = true;
}
}
// echo sprintf("%15s routerconfigs_checkopt('%s', %3d) returned %-5s (%s)\n", $label, $short, $index, $result ? 'Yes' : 'No', $colon);
return $result;
}
/**
* Checks whether a long-option specification string ends with a ':'
* (indicating it takes a value), stripping the trailing colon from the
* returned text when found. Called from routerconfigs_getopts_long() for
* each long-option specification entry (once for the value indicator,
* once for the optional indicator).
*
* @param string $label Unused label parameter (kept for parity with
* other checkopt helpers; not used directly here).
* @param string $text Reference, the long-option specification text;
* updated in place with any trailing ':' removed.
*
* @return bool True if the original text ended with ':', false
* otherwise.
*/
function routerconfigs_checkopt_string($label, &$text) {
$result = false;
$output = $text;
$colon = '<unset>';
if (strlen($text) > 1) {
$colon = $text[strlen($text) - 1];
if ($colon == ':') {
$result = true;
$output = substr($text, 0, - 1);
}
}
// echo sprintf("%15s routerconfigs_checkopt_string('%s returned %-5s (%s - %s)\n", $label, $text . '\')', $result ? 'Yes' : 'No', $colon, $output);
$text = $output;
return $result;
}