Last active
June 1, 2017 07:24
-
-
Save hachpai/f7d1a7dd968715cb42383ec7c9ceb1f6 to your computer and use it in GitHub Desktop.
making generic search usable with API calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//html/ajax_search.php | |
<?php | |
$init_modules = array('web', 'auth'); | |
require realpath(__DIR__ . '/..') . '/includes/init.php'; | |
require '/includes/generic_search.inc.php'; //requiring html/includes/generic_search.inc.php | |
set_debug($_REQUEST['debug']); | |
//check auth based on session | |
if (!$_SESSION['authenticated']) { | |
echo "Unauthenticated\n"; | |
exit; | |
} | |
$output = generic_search($_REQUEST['search'],$_REQUEST['type'],$_REQUEST['map']); | |
//die($output) is a bit nasty no? | |
echo($output); | |
exit(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//html/includes/api_functions.inc.php | |
<?php | |
/* | |
* LibreNMS | |
* | |
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa> | |
* | |
* 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 3 of the License, or (at your | |
* option) any later version. Please see LICENSE.txt at the top level of | |
* the source code distribution for details. | |
*/ | |
require 'generic_search.inc.php'; //requiring html/includes/generic_search.inc.php; | |
function authToken(\Slim\Route $route) | |
{ | |
$app = \Slim\Slim::getInstance(); | |
$token = $app->request->headers->get('X-Auth-Token'); | |
if (isset($token) && !empty($token)) { | |
if (!function_exists('get_user')) { | |
$username = dbFetchCell('SELECT `U`.`username` FROM `api_tokens` AS AT JOIN `users` AS U ON `AT`.`user_id`=`U`.`user_id` WHERE `AT`.`token_hash`=?', array($token)); | |
} else { | |
$username = get_user(dbFetchCell('SELECT `AT`.`user_id` FROM `api_tokens` AS AT WHERE `AT`.`token_hash`=?', array($token))); | |
} | |
if (!empty($username)) { | |
$authenticated = true; | |
} else { | |
$authenticated = false; | |
} | |
} else { | |
$authenticated = false; | |
} | |
if ($authenticated === false) { | |
$app->response->setStatus(401); | |
$output = array( | |
'status' => 'error', | |
'message' => 'API Token is missing or invalid; please supply a valid token', | |
); | |
echo _json_encode($output); | |
$app->stop(); | |
} | |
} | |
function perform_generic_search() | |
{ | |
global $config; | |
$app = \Slim\Slim::getInstance(); | |
$search = $_GET['search']; | |
$type = $_GET['type']; | |
$map = $_GET['map']; | |
$output=generic_search($search$type,$map); | |
echo $output; | |
} | |
//...all API functions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//librenms/html/includes/generic_search.inc.php | |
<?php | |
function generic_search($search_query,$type,$map) | |
{ | |
$device = array(); | |
$ports = array(); | |
$bgp = array(); | |
$limit = $config['webui']['global_search_result_limit']; | |
if (isset($search_query)) { | |
$search = mres($search_query); | |
header('Content-type: application/json'); | |
if (strlen($search) > 0) { | |
$found = 0; | |
if ($type == 'group') { | |
include_once '../includes/device-groups.inc.php'; | |
foreach (dbFetchRows("SELECT id,name FROM device_groups WHERE name LIKE '%".$search."%'") as $group) { | |
if ($map) { | |
$results[] = array( | |
'name' => 'g:'.$group['name'], | |
'group_id' => $group['id'], | |
); | |
} else { | |
$results[] = array('name' => $group['name']); | |
} | |
} | |
return(json_encode($results)); | |
} elseif ($type == 'alert-rules') { | |
foreach (dbFetchRows("SELECT name FROM alert_rules WHERE name LIKE '%".$search."%'") as $rules) { | |
$results[] = array('name' => $rules['name']); | |
} | |
return(json_encode($results)); | |
} elseif ($type == 'device') { | |
// Device search | |
if (is_admin() === true || is_read() === true) { | |
$results = dbFetchRows("SELECT * FROM `devices` WHERE `hostname` LIKE '%".$search."%' OR `location` LIKE '%".$search."%' OR `sysName` LIKE '%".$search."%' OR `purpose` LIKE '%".$search."%' OR `notes` LIKE '%".$search."%' ORDER BY hostname LIMIT ".$limit); | |
} else { | |
$results = dbFetchRows("SELECT * FROM `devices` AS `D`, `devices_perms` AS `P` WHERE `P`.`user_id` = ? AND `P`.`device_id` = `D`.`device_id` AND (`hostname` LIKE '%".$search."%' OR `location` LIKE '%".$search."%') ORDER BY hostname LIMIT ".$limit, array($_SESSION['user_id'])); | |
} | |
if (count($results)) { | |
$found = 1; | |
$devices = count($results); | |
foreach ($results as $result) { | |
$name = $result['hostname']; | |
if ($map != 1 && $result['sysName'] != $name && !empty($result['sysName'])) { | |
$name .= ' ('.$result['sysName'].') '; | |
} | |
if ($result['disabled'] == 1) { | |
$highlight_colour = '#808080'; | |
} elseif ($result['ignored'] == 1 && $result['disabled'] == 0) { | |
$highlight_colour = '#000000'; | |
} elseif ($result['status'] == 0 && $result['ignore'] == 0 && $result['disabled'] == 0) { | |
$highlight_colour = '#ff0000'; | |
} elseif ($result['status'] == 1 && $result['ignore'] == 0 && $result['disabled'] == 0) { | |
$highlight_colour = '#008000'; | |
} | |
if (is_admin() === true || is_read() === true) { | |
$num_ports = dbFetchCell('SELECT COUNT(*) FROM `ports` WHERE device_id = ?', array($result['device_id'])); | |
} else { | |
$num_ports = dbFetchCell('SELECT COUNT(*) FROM `ports` AS `I`, `devices` AS `D`, `devices_perms` AS `P` WHERE `P`.`user_id` = ? AND `P`.`device_id` = `D`.`device_id` AND `I`.`device_id` = `D`.`device_id` AND device_id = ?', array($_SESSION['user_id'], $result['device_id'])); | |
} | |
$device[] = array( | |
'name' => $name, | |
'device_id' => $result['device_id'], | |
'url' => generate_device_url($result), | |
'colours' => $highlight_colour, | |
'device_ports' => $num_ports, | |
'device_image' => getIcon($result), | |
'device_hardware' => $result['hardware'], | |
'device_os' => $config['os'][$result['os']]['text'], | |
'version' => $result['version'], | |
'location' => $result['location'], | |
); | |
}//end foreach | |
}//end if | |
$json = json_encode($device); | |
return($json); | |
} elseif ($type == 'ports') { | |
// Search ports | |
if (is_admin() === true || is_read() === true) { | |
$results = dbFetchRows("SELECT `ports`.*,`devices`.* FROM `ports` LEFT JOIN `devices` ON `ports`.`device_id` = `devices`.`device_id` WHERE `ifAlias` LIKE '%".$search."%' OR `ifDescr` LIKE '%".$search."%' OR `ifName` LIKE '%".$search."%' ORDER BY ifDescr LIMIT ".$limit); | |
} else { | |
$results = dbFetchRows("SELECT DISTINCT(`I`.`port_id`), `I`.*, `D`.`hostname` FROM `ports` AS `I`, `devices` AS `D`, `devices_perms` AS `P`, `ports_perms` AS `PP` WHERE ((`P`.`user_id` = ? AND `P`.`device_id` = `D`.`device_id`) OR (`PP`.`user_id` = ? AND `PP`.`port_id` = `I`.`port_id` AND `I`.`device_id` = `D`.`device_id`)) AND `D`.`device_id` = `I`.`device_id` AND (`ifAlias` LIKE '%".$search."%' OR `ifDescr` LIKE '%".$search."%' OR `ifName` LIKE '%".$search."%') ORDER BY ifDescr LIMIT ".$limit, array($_SESSION['user_id'], $_SESSION['user_id'])); | |
} | |
if (count($results)) { | |
$found = 1; | |
foreach ($results as $result) { | |
$name = $result['ifDescr'] == $result['ifAlias'] ? $result['ifName'] : $result['ifDescr']; | |
$description = display($result['ifAlias']); | |
if ($result['deleted'] == 0 && ($result['ignore'] == 0 || $result['ignore'] == 0) && ($result['ifInErrors_delta'] > 0 || $result['ifOutErrors_delta'] > 0)) { | |
// Errored ports | |
$port_colour = '#ffa500'; | |
} elseif ($result['deleted'] == 0 && ($result['ignore'] == 1 || $result['ignore'] == 1)) { | |
// Ignored ports | |
$port_colour = '#000000'; | |
} elseif ($result['deleted'] == 0 && $result['ifAdminStatus'] == 'down' && $result['ignore'] == 0 && $result['ignore'] == 0) { | |
// Shutdown ports | |
$port_colour = '#808080'; | |
} elseif ($result['deleted'] == 0 && $result['ifOperStatus'] == 'down' && $result['ifAdminStatus'] == 'up' && $result['ignore'] == 0 && $result['ignore'] == 0) { | |
// Down ports | |
$port_colour = '#ff0000'; | |
} elseif ($result['deleted'] == 0 && $result['ifOperStatus'] == 'up' && $result['ignore'] == 0 && $result['ignore'] == 0) { | |
// Up ports | |
$port_colour = '#008000'; | |
}//end if | |
$ports[] = array( | |
'count' => count($results), | |
'url' => generate_port_url($result), | |
'name' => $name, | |
'description' => $description, | |
'colours' => $highlight_colour, | |
'hostname' => $result['hostname'], | |
'port_id' => $result['port_id'], | |
); | |
}//end foreach | |
}//end if | |
$json = json_encode($ports); | |
return($json); | |
} elseif ($type == 'bgp') { | |
// Search bgp peers | |
if (is_admin() === true || is_read() === true) { | |
$results = dbFetchRows("SELECT `bgpPeers`.*,`devices`.* FROM `bgpPeers` LEFT JOIN `devices` ON `bgpPeers`.`device_id` = `devices`.`device_id` WHERE `astext` LIKE '%".$search."%' OR `bgpPeerIdentifier` LIKE '%".$search."%' OR `bgpPeerRemoteAs` LIKE '%".$search."%' ORDER BY `astext` LIMIT ".$limit); | |
} else { | |
$results = dbFetchRows("SELECT `bgpPeers`.*,`D`.* FROM `bgpPeers`, `devices` AS `D`, `devices_perms` AS `P` WHERE `P`.`user_id` = ? AND `P`.`device_id` = `D`.`device_id` AND `bgpPeers`.`device_id`=`D`.`device_id` AND (`astext` LIKE '%".$search."%' OR `bgpPeerIdentifier` LIKE '%".$search."%' OR `bgpPeerRemoteAs` LIKE '%".$search."%') ORDER BY `astext` LIMIT ".$limit, array($_SESSION['user_id'])); | |
} | |
if (count($results)) { | |
$found = 1; | |
foreach ($results as $result) { | |
$name = $result['bgpPeerIdentifier']; | |
$description = $result['astext']; | |
$remoteas = $result['bgpPeerRemoteAs']; | |
$localas = $result['bgpLocalAs']; | |
if ($result['bgpPeerAdminStatus'] == 'start' && $result['bgpPeerState'] != 'established') { | |
// Session active but errored | |
$port_colour = '#ffa500'; | |
} elseif ($result['bgpPeerAdminStatus'] != 'start') { | |
// Session inactive | |
$port_colour = '#000000'; | |
} elseif ($result['bgpPeerAdminStatus'] == 'start' && $result['bgpPeerState'] == 'established') { | |
// Session Up | |
$port_colour = '#008000'; | |
} | |
if ($result['bgpPeerRemoteAs'] == $result['bgpLocalAs']) { | |
$bgp_image = '<i class="fa fa-square fa-lg icon-theme" aria-hidden="true"></i>'; | |
} else { | |
$bgp_image = '<i class="fa fa-external-link-square fa-lg icon-theme" aria-hidden="true"></i>'; | |
} | |
$bgp[] = array( | |
'count' => count($results), | |
'url' => generate_peer_url($result), | |
'name' => $name, | |
'description' => $description, | |
'localas' => $localas, | |
'bgp_image' => $bgp_image, | |
'remoteas' => $remoteas, | |
'colours' => $port_colour, | |
'hostname' => $result['hostname'], | |
); | |
}//end foreach | |
}//end if | |
$json = json_encode($bgp); | |
return($json); | |
} elseif ($type == 'applications') { | |
// Device search | |
if (is_admin() === true || is_read() === true) { | |
$results = dbFetchRows("SELECT * FROM `applications` INNER JOIN `devices` ON devices.device_id = applications.device_id WHERE `app_type` LIKE '%".$search."%' OR `hostname` LIKE '%".$search."%' ORDER BY hostname LIMIT ".$limit); | |
} else { | |
$results = dbFetchRows("SELECT * FROM `applications` INNER JOIN `devices` AS `D` ON `D`.`device_id` = `applications`.`device_id` INNER JOIN `devices_perms` AS `P` ON `P`.`device_id` = `D`.`device_id` WHERE `P`.`user_id` = ? AND (`app_type` LIKE '%".$search."%' OR `hostname` LIKE '%".$search."%') ORDER BY hostname LIMIT ".$limit, array($_SESSION['user_id'])); | |
} | |
if (count($results)) { | |
$found = 1; | |
$devices = count($results); | |
foreach ($results as $result) { | |
$name = $result['app_type']; | |
if ($result['disabled'] == 1) { | |
$highlight_colour = '#808080'; | |
} elseif ($result['ignored'] == 1 && $result['disabled'] == 0) { | |
$highlight_colour = '#000000'; | |
} elseif ($result['status'] == 0 && $result['ignore'] == 0 && $result['disabled'] == 0) { | |
$highlight_colour = '#ff0000'; | |
} elseif ($result['status'] == 1 && $result['ignore'] == 0 && $result['disabled'] == 0) { | |
$highlight_colour = '#008000'; | |
} | |
$device[] = array( | |
'name' => $name, | |
'hostname' => $result['hostname'], | |
'app_id' => $result['app_id'], | |
'device_id' => $result['device_id'], | |
'colours' => $highlight_colour, | |
'device_image' => getIcon($result), | |
'device_hardware' => $result['hardware'], | |
'device_os' => $config['os'][$result['os']]['text'], | |
'version' => $result['version'], | |
'location' => $result['location'], | |
); | |
}//end foreach | |
}//end if | |
$json = json_encode($device); | |
return($json); | |
} elseif ($type == 'munin') { | |
// Device search | |
if (is_admin() === true || is_read() === true) { | |
$results = dbFetchRows("SELECT * FROM `munin_plugins` INNER JOIN `devices` ON devices.device_id = munin_plugins.device_id WHERE `mplug_type` LIKE '%".$search."%' OR `mplug_title` LIKE '%".$search."%' OR `hostname` LIKE '%".$search."%' ORDER BY hostname LIMIT ".$limit); | |
} else { | |
$results = dbFetchRows("SELECT * FROM `munin_plugins` INNER JOIN `devices` AS `D` ON `D`.`device_id` = `munin_plugins`.`device_id` INNER JOIN `devices_perms` AS `P` ON `P`.`device_id` = `D`.`device_id` WHERE `P`.`user_id` = ? AND (`mplug_type` LIKE '%".$search."%' OR `mplug_title` LIKE '%".$search."%' OR `hostname` LIKE '%".$search."%') ORDER BY hostname LIMIT ".$limit, array($_SESSION['user_id'])); | |
} | |
if (count($results)) { | |
$found = 1; | |
$devices = count($results); | |
foreach ($results as $result) { | |
$name = $result['mplug_title']; | |
if ($result['disabled'] == 1) { | |
$highlight_colour = '#808080'; | |
} elseif ($result['ignored'] == 1 && $result['disabled'] == 0) { | |
$highlight_colour = '#000000'; | |
} elseif ($result['status'] == 0 && $result['ignore'] == 0 && $result['disabled'] == 0) { | |
$highlight_colour = '#ff0000'; | |
} elseif ($result['status'] == 1 && $result['ignore'] == 0 && $result['disabled'] == 0) { | |
$highlight_colour = '#008000'; | |
} | |
$device[] = array( | |
'name' => $name, | |
'hostname' => $result['hostname'], | |
'device_id' => $result['device_id'], | |
'colours' => $highlight_colour, | |
'device_image' => getIcon($result), | |
'device_hardware' => $result['hardware'], | |
'device_os' => $config['os'][$result['os']]['text'], | |
'version' => $result['version'], | |
'location' => $result['location'], | |
'plugin' => $result['mplug_type'], | |
); | |
}//end foreach | |
}//end if | |
$json = json_encode($device); | |
return($json); | |
} elseif ($type == 'iftype') { | |
// Device search | |
if (is_admin() === true || is_read() === true) { | |
$results = dbFetchRows("SELECT `ports`.ifType FROM `ports` WHERE `ifType` LIKE '%".$search."%' GROUP BY ifType ORDER BY ifType LIMIT ".$limit); | |
} else { | |
$results = dbFetchRows("SELECT `I`.ifType FROM `ports` AS `I`, `devices` AS `D`, `devices_perms` AS `P`, `ports_perms` AS `PP` WHERE ((`P`.`user_id` = ? AND `P`.`device_id` = `D`.`device_id`) OR (`PP`.`user_id` = ? AND `PP`.`port_id` = `I`.`port_id` AND `I`.`device_id` = `D`.`device_id`)) AND `D`.`device_id` = `I`.`device_id` AND (`ifType` LIKE '%".$search."%') GROUP BY ifType ORDER BY ifType LIMIT ".$limit, array($_SESSION['user_id'], $_SESSION['user_id'])); | |
} | |
if (count($results)) { | |
$found = 1; | |
$devices = count($results); | |
foreach ($results as $result) { | |
$device[] = array( | |
'filter' => $result['ifType'], | |
); | |
}//end foreach | |
}//end if | |
$json = json_encode($device); | |
return($json); | |
} elseif ($type == 'bill') { | |
// Device search | |
if (is_admin() === true || is_read() === true) { | |
$results = dbFetchRows("SELECT `bills`.bill_id, `bills`.bill_name FROM `bills` WHERE `bill_name` LIKE '%".$search."%' OR `bill_notes` LIKE '%".$search."%' LIMIT ".$limit); | |
} else { | |
$results = dbFetchRows("SELECT `bills`.bill_id, `bills`.bill_name FROM `bills` INNER JOIN `bill_perms` ON `bills`.bill_id = `bill_perms`.bill_id WHERE `bill_perms`.user_id = ? AND (`bill_name` LIKE '%".$search."%' OR `bill_notes` LIKE '%".$search."%') LIMIT ".$limit, array($_SESSION['user_id'])); | |
} | |
$json = json_encode($results); | |
return($json); | |
}//end if | |
}//end if | |
}//end if | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/html/ajax_search.php b/html/ajax_search.php | |
index 5dc3393..c038ab9 100644 | |
--- a/html/ajax_search.php | |
+++ b/html/ajax_search.php | |
@@ -2,6 +2,7 @@ | |
$init_modules = array('web', 'auth'); | |
require realpath(__DIR__ . '/..') . '/includes/init.php'; | |
+require '/includes/generic_search.inc.php'; //requiring html/includes/generic_search.inc.php | |
set_debug($_REQUEST['debug']); | |
@@ -10,294 +11,7 @@ if (!$_SESSION['authenticated']) { | |
exit; | |
} | |
-$device = array(); | |
-$ports = array(); | |
-$bgp = array(); | |
-$limit = $config['webui']['global_search_result_limit']; | |
- | |
-if (isset($_REQUEST['search'])) { | |
- $search = mres($_REQUEST['search']); | |
- header('Content-type: application/json'); | |
- if (strlen($search) > 0) { | |
- $found = 0; | |
- | |
- if ($_REQUEST['type'] == 'group') { | |
- include_once '../includes/device-groups.inc.php'; | |
- foreach (dbFetchRows("SELECT id,name FROM device_groups WHERE name LIKE '%".$search."%'") as $group) { | |
- if ($_REQUEST['map']) { | |
- $results[] = array( | |
- 'name' => 'g:'.$group['name'], | |
- 'group_id' => $group['id'], | |
- ); | |
- } else { | |
- $results[] = array('name' => $group['name']); | |
- } | |
- } | |
- | |
- die(json_encode($results)); | |
- } elseif ($_REQUEST['type'] == 'alert-rules') { | |
- foreach (dbFetchRows("SELECT name FROM alert_rules WHERE name LIKE '%".$search."%'") as $rules) { | |
- $results[] = array('name' => $rules['name']); | |
- } | |
- | |
- die(json_encode($results)); | |
- } elseif ($_REQUEST['type'] == 'device') { | |
- // Device search | |
- if (is_admin() === true || is_read() === true) { | |
- $results = dbFetchRows("SELECT * FROM `devices` WHERE `hostname` LIKE '%".$search."%' OR `location` LIKE '%".$search."%' OR `sysName` LIKE '%".$search."%' OR `purpose` LIKE '%".$search."%' OR `notes` LIKE '%".$search."%' ORDER BY hostname LIMIT ".$limit); | |
- } else { | |
- $results = dbFetchRows("SELECT * FROM `devices` AS `D`, `devices_perms` AS `P` WHERE `P`.`user_id` = ? AND `P`.`device_id` = `D`.`device_id` AND (`hostname` LIKE '%".$search."%' OR `location` LIKE '%".$search."%') ORDER BY hostname LIMIT ".$limit, array($_SESSION['user_id'])); | |
- } | |
- | |
- if (count($results)) { | |
- $found = 1; | |
- $devices = count($results); | |
- | |
- foreach ($results as $result) { | |
- $name = $result['hostname']; | |
- if ($_REQUEST['map'] != 1 && $result['sysName'] != $name && !empty($result['sysName'])) { | |
- $name .= ' ('.$result['sysName'].') '; | |
- } | |
- if ($result['disabled'] == 1) { | |
- $highlight_colour = '#808080'; | |
- } elseif ($result['ignored'] == 1 && $result['disabled'] == 0) { | |
- $highlight_colour = '#000000'; | |
- } elseif ($result['status'] == 0 && $result['ignore'] == 0 && $result['disabled'] == 0) { | |
- $highlight_colour = '#ff0000'; | |
- } elseif ($result['status'] == 1 && $result['ignore'] == 0 && $result['disabled'] == 0) { | |
- $highlight_colour = '#008000'; | |
- } | |
- | |
- if (is_admin() === true || is_read() === true) { | |
- $num_ports = dbFetchCell('SELECT COUNT(*) FROM `ports` WHERE device_id = ?', array($result['device_id'])); | |
- } else { | |
- $num_ports = dbFetchCell('SELECT COUNT(*) FROM `ports` AS `I`, `devices` AS `D`, `devices_perms` AS `P` WHERE `P`.`user_id` = ? AND `P`.`device_id` = `D`.`device_id` AND `I`.`device_id` = `D`.`device_id` AND device_id = ?', array($_SESSION['user_id'], $result['device_id'])); | |
- } | |
- | |
- $device[] = array( | |
- 'name' => $name, | |
- 'device_id' => $result['device_id'], | |
- 'url' => generate_device_url($result), | |
- 'colours' => $highlight_colour, | |
- 'device_ports' => $num_ports, | |
- 'device_image' => getIcon($result), | |
- 'device_hardware' => $result['hardware'], | |
- 'device_os' => $config['os'][$result['os']]['text'], | |
- 'version' => $result['version'], | |
- 'location' => $result['location'], | |
- ); | |
- }//end foreach | |
- }//end if | |
- | |
- $json = json_encode($device); | |
- die($json); | |
- } elseif ($_REQUEST['type'] == 'ports') { | |
- // Search ports | |
- if (is_admin() === true || is_read() === true) { | |
- $results = dbFetchRows("SELECT `ports`.*,`devices`.* FROM `ports` LEFT JOIN `devices` ON `ports`.`device_id` = `devices`.`device_id` WHERE `ifAlias` LIKE '%".$search."%' OR `ifDescr` LIKE '%".$search."%' OR `ifName` LIKE '%".$search."%' ORDER BY ifDescr LIMIT ".$limit); | |
- } else { | |
- $results = dbFetchRows("SELECT DISTINCT(`I`.`port_id`), `I`.*, `D`.`hostname` FROM `ports` AS `I`, `devices` AS `D`, `devices_perms` AS `P`, `ports_perms` AS `PP` WHERE ((`P`.`user_id` = ? AND `P`.`device_id` = `D`.`device_id`) OR (`PP`.`user_id` = ? AND `PP`.`port_id` = `I`.`port_id` AND `I`.`device_id` = `D`.`device_id`)) AND `D`.`device_id` = `I`.`device_id` AND (`ifAlias` LIKE '%".$search."%' OR `ifDescr` LIKE '%".$search."%' OR `ifName` LIKE '%".$search."%') ORDER BY ifDescr LIMIT ".$limit, array($_SESSION['user_id'], $_SESSION['user_id'])); | |
- } | |
- | |
- if (count($results)) { | |
- $found = 1; | |
- | |
- foreach ($results as $result) { | |
- $name = $result['ifDescr'] == $result['ifAlias'] ? $result['ifName'] : $result['ifDescr']; | |
- $description = display($result['ifAlias']); | |
- | |
- if ($result['deleted'] == 0 && ($result['ignore'] == 0 || $result['ignore'] == 0) && ($result['ifInErrors_delta'] > 0 || $result['ifOutErrors_delta'] > 0)) { | |
- // Errored ports | |
- $port_colour = '#ffa500'; | |
- } elseif ($result['deleted'] == 0 && ($result['ignore'] == 1 || $result['ignore'] == 1)) { | |
- // Ignored ports | |
- $port_colour = '#000000'; | |
- } elseif ($result['deleted'] == 0 && $result['ifAdminStatus'] == 'down' && $result['ignore'] == 0 && $result['ignore'] == 0) { | |
- // Shutdown ports | |
- $port_colour = '#808080'; | |
- } elseif ($result['deleted'] == 0 && $result['ifOperStatus'] == 'down' && $result['ifAdminStatus'] == 'up' && $result['ignore'] == 0 && $result['ignore'] == 0) { | |
- // Down ports | |
- $port_colour = '#ff0000'; | |
- } elseif ($result['deleted'] == 0 && $result['ifOperStatus'] == 'up' && $result['ignore'] == 0 && $result['ignore'] == 0) { | |
- // Up ports | |
- $port_colour = '#008000'; | |
- }//end if | |
- | |
- $ports[] = array( | |
- 'count' => count($results), | |
- 'url' => generate_port_url($result), | |
- 'name' => $name, | |
- 'description' => $description, | |
- 'colours' => $highlight_colour, | |
- 'hostname' => $result['hostname'], | |
- 'port_id' => $result['port_id'], | |
- ); | |
- }//end foreach | |
- }//end if | |
- | |
- $json = json_encode($ports); | |
- die($json); | |
- } elseif ($_REQUEST['type'] == 'bgp') { | |
- // Search bgp peers | |
- if (is_admin() === true || is_read() === true) { | |
- $results = dbFetchRows("SELECT `bgpPeers`.*,`devices`.* FROM `bgpPeers` LEFT JOIN `devices` ON `bgpPeers`.`device_id` = `devices`.`device_id` WHERE `astext` LIKE '%".$search."%' OR `bgpPeerIdentifier` LIKE '%".$search."%' OR `bgpPeerRemoteAs` LIKE '%".$search."%' ORDER BY `astext` LIMIT ".$limit); | |
- } else { | |
- $results = dbFetchRows("SELECT `bgpPeers`.*,`D`.* FROM `bgpPeers`, `devices` AS `D`, `devices_perms` AS `P` WHERE `P`.`user_id` = ? AND `P`.`device_id` = `D`.`device_id` AND `bgpPeers`.`device_id`=`D`.`device_id` AND (`astext` LIKE '%".$search."%' OR `bgpPeerIdentifier` LIKE '%".$search."%' OR `bgpPeerRemoteAs` LIKE '%".$search."%') ORDER BY `astext` LIMIT ".$limit, array($_SESSION['user_id'])); | |
- } | |
- | |
- if (count($results)) { | |
- $found = 1; | |
- | |
- foreach ($results as $result) { | |
- $name = $result['bgpPeerIdentifier']; | |
- $description = $result['astext']; | |
- $remoteas = $result['bgpPeerRemoteAs']; | |
- $localas = $result['bgpLocalAs']; | |
- | |
- if ($result['bgpPeerAdminStatus'] == 'start' && $result['bgpPeerState'] != 'established') { | |
- // Session active but errored | |
- $port_colour = '#ffa500'; | |
- } elseif ($result['bgpPeerAdminStatus'] != 'start') { | |
- // Session inactive | |
- $port_colour = '#000000'; | |
- } elseif ($result['bgpPeerAdminStatus'] == 'start' && $result['bgpPeerState'] == 'established') { | |
- // Session Up | |
- $port_colour = '#008000'; | |
- } | |
- | |
- if ($result['bgpPeerRemoteAs'] == $result['bgpLocalAs']) { | |
- $bgp_image = '<i class="fa fa-square fa-lg icon-theme" aria-hidden="true"></i>'; | |
- } else { | |
- $bgp_image = '<i class="fa fa-external-link-square fa-lg icon-theme" aria-hidden="true"></i>'; | |
- } | |
- | |
- $bgp[] = array( | |
- 'count' => count($results), | |
- 'url' => generate_peer_url($result), | |
- 'name' => $name, | |
- 'description' => $description, | |
- 'localas' => $localas, | |
- 'bgp_image' => $bgp_image, | |
- 'remoteas' => $remoteas, | |
- 'colours' => $port_colour, | |
- 'hostname' => $result['hostname'], | |
- ); | |
- }//end foreach | |
- }//end if | |
- | |
- $json = json_encode($bgp); | |
- die($json); | |
- } elseif ($_REQUEST['type'] == 'applications') { | |
- // Device search | |
- if (is_admin() === true || is_read() === true) { | |
- $results = dbFetchRows("SELECT * FROM `applications` INNER JOIN `devices` ON devices.device_id = applications.device_id WHERE `app_type` LIKE '%".$search."%' OR `hostname` LIKE '%".$search."%' ORDER BY hostname LIMIT ".$limit); | |
- } else { | |
- $results = dbFetchRows("SELECT * FROM `applications` INNER JOIN `devices` AS `D` ON `D`.`device_id` = `applications`.`device_id` INNER JOIN `devices_perms` AS `P` ON `P`.`device_id` = `D`.`device_id` WHERE `P`.`user_id` = ? AND (`app_type` LIKE '%".$search."%' OR `hostname` LIKE '%".$search."%') ORDER BY hostname LIMIT ".$limit, array($_SESSION['user_id'])); | |
- } | |
- | |
- if (count($results)) { | |
- $found = 1; | |
- $devices = count($results); | |
- | |
- foreach ($results as $result) { | |
- $name = $result['app_type']; | |
- if ($result['disabled'] == 1) { | |
- $highlight_colour = '#808080'; | |
- } elseif ($result['ignored'] == 1 && $result['disabled'] == 0) { | |
- $highlight_colour = '#000000'; | |
- } elseif ($result['status'] == 0 && $result['ignore'] == 0 && $result['disabled'] == 0) { | |
- $highlight_colour = '#ff0000'; | |
- } elseif ($result['status'] == 1 && $result['ignore'] == 0 && $result['disabled'] == 0) { | |
- $highlight_colour = '#008000'; | |
- } | |
- | |
- $device[] = array( | |
- 'name' => $name, | |
- 'hostname' => $result['hostname'], | |
- 'app_id' => $result['app_id'], | |
- 'device_id' => $result['device_id'], | |
- 'colours' => $highlight_colour, | |
- 'device_image' => getIcon($result), | |
- 'device_hardware' => $result['hardware'], | |
- 'device_os' => $config['os'][$result['os']]['text'], | |
- 'version' => $result['version'], | |
- 'location' => $result['location'], | |
- ); | |
- }//end foreach | |
- }//end if | |
- | |
- $json = json_encode($device); | |
- die($json); | |
- } elseif ($_REQUEST['type'] == 'munin') { | |
- // Device search | |
- if (is_admin() === true || is_read() === true) { | |
- $results = dbFetchRows("SELECT * FROM `munin_plugins` INNER JOIN `devices` ON devices.device_id = munin_plugins.device_id WHERE `mplug_type` LIKE '%".$search."%' OR `mplug_title` LIKE '%".$search."%' OR `hostname` LIKE '%".$search."%' ORDER BY hostname LIMIT ".$limit); | |
- } else { | |
- $results = dbFetchRows("SELECT * FROM `munin_plugins` INNER JOIN `devices` AS `D` ON `D`.`device_id` = `munin_plugins`.`device_id` INNER JOIN `devices_perms` AS `P` ON `P`.`device_id` = `D`.`device_id` WHERE `P`.`user_id` = ? AND (`mplug_type` LIKE '%".$search."%' OR `mplug_title` LIKE '%".$search."%' OR `hostname` LIKE '%".$search."%') ORDER BY hostname LIMIT ".$limit, array($_SESSION['user_id'])); | |
- } | |
- | |
- if (count($results)) { | |
- $found = 1; | |
- $devices = count($results); | |
- | |
- foreach ($results as $result) { | |
- $name = $result['mplug_title']; | |
- if ($result['disabled'] == 1) { | |
- $highlight_colour = '#808080'; | |
- } elseif ($result['ignored'] == 1 && $result['disabled'] == 0) { | |
- $highlight_colour = '#000000'; | |
- } elseif ($result['status'] == 0 && $result['ignore'] == 0 && $result['disabled'] == 0) { | |
- $highlight_colour = '#ff0000'; | |
- } elseif ($result['status'] == 1 && $result['ignore'] == 0 && $result['disabled'] == 0) { | |
- $highlight_colour = '#008000'; | |
- } | |
- | |
- $device[] = array( | |
- 'name' => $name, | |
- 'hostname' => $result['hostname'], | |
- 'device_id' => $result['device_id'], | |
- 'colours' => $highlight_colour, | |
- 'device_image' => getIcon($result), | |
- 'device_hardware' => $result['hardware'], | |
- 'device_os' => $config['os'][$result['os']]['text'], | |
- 'version' => $result['version'], | |
- 'location' => $result['location'], | |
- 'plugin' => $result['mplug_type'], | |
- ); | |
- }//end foreach | |
- }//end if | |
- | |
- $json = json_encode($device); | |
- die($json); | |
- } elseif ($_REQUEST['type'] == 'iftype') { | |
- // Device search | |
- if (is_admin() === true || is_read() === true) { | |
- $results = dbFetchRows("SELECT `ports`.ifType FROM `ports` WHERE `ifType` LIKE '%".$search."%' GROUP BY ifType ORDER BY ifType LIMIT ".$limit); | |
- } else { | |
- $results = dbFetchRows("SELECT `I`.ifType FROM `ports` AS `I`, `devices` AS `D`, `devices_perms` AS `P`, `ports_perms` AS `PP` WHERE ((`P`.`user_id` = ? AND `P`.`device_id` = `D`.`device_id`) OR (`PP`.`user_id` = ? AND `PP`.`port_id` = `I`.`port_id` AND `I`.`device_id` = `D`.`device_id`)) AND `D`.`device_id` = `I`.`device_id` AND (`ifType` LIKE '%".$search."%') GROUP BY ifType ORDER BY ifType LIMIT ".$limit, array($_SESSION['user_id'], $_SESSION['user_id'])); | |
- } | |
- if (count($results)) { | |
- $found = 1; | |
- $devices = count($results); | |
- | |
- foreach ($results as $result) { | |
- $device[] = array( | |
- 'filter' => $result['ifType'], | |
- ); | |
- }//end foreach | |
- }//end if | |
- | |
- $json = json_encode($device); | |
- die($json); | |
- } elseif ($_REQUEST['type'] == 'bill') { | |
- // Device search | |
- if (is_admin() === true || is_read() === true) { | |
- $results = dbFetchRows("SELECT `bills`.bill_id, `bills`.bill_name FROM `bills` WHERE `bill_name` LIKE '%".$search."%' OR `bill_notes` LIKE '%".$search."%' LIMIT ".$limit); | |
- } else { | |
- $results = dbFetchRows("SELECT `bills`.bill_id, `bills`.bill_name FROM `bills` INNER JOIN `bill_perms` ON `bills`.bill_id = `bill_perms`.bill_id WHERE `bill_perms`.user_id = ? AND (`bill_name` LIKE '%".$search."%' OR `bill_notes` LIKE '%".$search."%') LIMIT ".$limit, array($_SESSION['user_id'])); | |
- } | |
- $json = json_encode($results); | |
- die($json); | |
- }//end if | |
- }//end if | |
-}//end if | |
+$output = generic_search($_REQUEST['search'],$_REQUEST['type'],$_REQUEST['map']); | |
+//die($output) is a bit nasty no? | |
+echo($output); | |
+exit(); | |
diff --git a/html/api_v0.php b/html/api_v0.php | |
index 4d423e1..82430a2 100644 | |
--- a/html/api_v0.php | |
+++ b/html/api_v0.php | |
@@ -25,9 +25,12 @@ $app->group( | |
$app->group( | |
'/v0', | |
function () use ($app) { | |
+ $app->get('/generic_search', 'authToken', 'perform_generic_search')->name('perform_generic_search'); | |
+ // api/v0/generic_search | |
$app->get('/bgp', 'authToken', 'list_bgp')->name('list_bgp'); | |
// api/v0/bgp | |
$app->get('/oxidized', 'authToken', 'list_oxidized')->name('list_oxidized'); | |
+ | |
$app->group( | |
'/devices', | |
function () use ($app) { | |
diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php | |
index 532a34d..bd66846 100644 | |
--- a/html/includes/api_functions.inc.php | |
+++ b/html/includes/api_functions.inc.php | |
@@ -11,6 +11,7 @@ | |
* option) any later version. Please see LICENSE.txt at the top level of | |
* the source code distribution for details. | |
*/ | |
+require 'generic_search.inc.php'; //requiring html/includes/generic_search.inc.php; | |
function authToken(\Slim\Route $route) | |
{ | |
@@ -41,7 +42,16 @@ function authToken(\Slim\Route $route) | |
$app->stop(); | |
} | |
} | |
- | |
+function perform_generic_search() | |
+{ | |
+ global $config; | |
+ $app = \Slim\Slim::getInstance(); | |
+ $search = $_GET['search']; | |
+ $type = $_GET['type']; | |
+ $map = $_GET['map']; | |
+ $output=generic_search($search$type,$map); | |
+ echo $output; | |
+} | |
function get_graph_by_port_hostname() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment