mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Ports API: Workaround for ifNames with slashes (#10502)
* Workaround for ifNames with slashes Basically, /api/v0/devices/3/ports/0%2f4 -> /api/v0/devices/3/ports/ifName?ifName=0/4 /api/v0/devices/3/ports/0%2f4/port_bits -> /api/v0/devices/3/ports/ifName/port_bits?ifName=0/4 Or you can just use port_id. * Fix API ports using ifName with slashes hand parse the path for the ports graph endpoints this way we can respect the original way of handling slashes (%2F)
This commit is contained in:
@@ -74,6 +74,12 @@ function api_get_graph(array $vars)
|
||||
$auth = '1';
|
||||
$base64_output = '';
|
||||
|
||||
// prevent ugly error for undefined graphs from being passed to the user
|
||||
list($type, $subtype) = extract_graph_type($vars['type']);
|
||||
if (!is_file(base_path("includes/html/graphs/$type/auth.inc.php"))) {
|
||||
return api_error(400, 'Invalid graph type');
|
||||
}
|
||||
|
||||
ob_start();
|
||||
|
||||
rrdtool_initialize(false);
|
||||
@@ -117,14 +123,14 @@ function check_port_permission($port_id, $device_id, $callback)
|
||||
return $callback($port_id);
|
||||
}
|
||||
|
||||
function get_graph_by_port_hostname(\Illuminate\Http\Request $request)
|
||||
function get_graph_by_port_hostname(\Illuminate\Http\Request $request, $ifname = null, $type = 'port_bits')
|
||||
{
|
||||
// This will return a graph for a given port by the ifName
|
||||
$hostname = $request->route('hostname');
|
||||
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
|
||||
$vars = [
|
||||
'port' => $request->route('ifname'),
|
||||
'type' => $request->route('type', 'port_bits'),
|
||||
'port' => $ifname ?: $request->route('ifname'),
|
||||
'type' => $request->route('type', $type),
|
||||
'output' => $request->get('output', 'display'),
|
||||
'width' => $request->get('width', 1075),
|
||||
'height' => $request->get('height', 300),
|
||||
@@ -149,10 +155,23 @@ function get_graph_by_port_hostname(\Illuminate\Http\Request $request)
|
||||
|
||||
function get_port_stats_by_port_hostname(\Illuminate\Http\Request $request)
|
||||
{
|
||||
$ifName = $request->route('ifname');
|
||||
|
||||
// handle %2f in paths and pass to get_graph_by_port_hostname if needed
|
||||
if (str_contains($ifName, '/')) {
|
||||
$parts = explode('/', $request->path());
|
||||
|
||||
if (isset($parts[5])) {
|
||||
$ifName = urldecode($parts[5]);
|
||||
if (isset($parts[6])) {
|
||||
return get_graph_by_port_hostname($request, $ifName, $parts[6]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This will return port stats based on a devices hostname and ifName
|
||||
$hostname = $request->route('hostname');
|
||||
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
|
||||
$ifName = $request->route('ifname');
|
||||
$port = dbFetchRow('SELECT * FROM `ports` WHERE `device_id`=? AND `ifName`=? AND `deleted` = 0', [$device_id, $ifName]);
|
||||
|
||||
return check_port_permission($port['port_id'], $device_id, function () use ($request, $port) {
|
||||
|
||||
Reference in New Issue
Block a user