create common method for generating type_wheres

This commit is contained in:
Brandon Boudrias 2016-05-31 15:17:11 -07:00
parent dcffc8381a
commit 3b4964e13c
4 changed files with 65 additions and 59 deletions

View File

@ -495,19 +495,10 @@ function get_graph_by_portgroup() {
$vars['width'] = $_GET['width'] ?: 1075;
$vars['height'] = $_GET['height'] ?: 300;
$auth = '1';
$type_where = ' (';
$or = '';
$type_param = array();
foreach (explode(',', $group) as $type) {
$type_where .= " $or `port_descr_type` = ?";
$or = 'OR';
$type_param[] = $type;
}
$type_where .= ') ';
$ports = get_ports_from_type(explode(',', $group));
$if_list = '';
$seperator = '';
$ports = dbFetchRows("SELECT * FROM `ports` as I, `devices` AS D WHERE $type_where AND I.device_id = D.device_id ORDER BY I.ifAlias", $type_param);
foreach ($ports as $port) {
$if_list .= $seperator.$port['port_id'];
$seperator = ',';

View File

@ -393,39 +393,19 @@ else {
elseif ($type == 'munin') {
$param = 'device='.$widget_settings['graph_'.$type]['device_id'].'&plugin='.$widget_settings['graph_'.$type]['name'];
}
elseif ($type == 'transit' || $type == 'peering' || $type == 'core') {
$type_where = ' (';
if (is_array($config[$type.'_descr']) === false) {
$config[$type.'_descr'] = array($config[$type.'_descr']);
elseif ($type == 'transit' || $type == 'peering' || $type == 'core' || $type == 'custom') {
if ( $type == 'custom' ) {
$type = $widget_settings['graph_custom'];
}
foreach ($config[$type.'_descr'] as $additional_type) {
if (!empty($additional_type)) {
$type_where .= " $or `port_descr_type` = ?";
$or = 'OR';
$type_param[] = $additional_type;
}
}
$type_where .= " $or `port_descr_type` = ?";
$or = 'OR';
$type_param[] = $type;
$type_where .= ') ';
foreach (dbFetchRows("SELECT port_id FROM `ports` WHERE $type_where ORDER BY ifAlias", $type_param) as $port) {
$ports = get_ports_from_type($type);
foreach ($ports as $port) {
$tmp[] = $port['port_id'];
}
$param = 'id='.implode(',',$tmp);
$widget_settings['graph_type'] = 'multiport_bits_separate';
if (empty($widget_settings['title'])) {
$widget_settings['title'] = 'Overall '.ucfirst($type).' Bits ('.$widget_settings['graph_range'].')';
}
}
elseif ($type == 'custom') {
foreach (dbFetchRows("SELECT port_id FROM `ports` WHERE `port_descr_type` = ? ORDER BY ifAlias", array($widget_settings['graph_custom'])) as $port) {
$tmp[] = $port['port_id'];
}
$param = 'id='.implode(',',$tmp);
$widget_settings['graph_type'] = 'multiport_bits_separate';
if (empty($widget_settings['title'])) {
$widget_settings['title'] = 'Overall '.ucfirst(htmlspecialchars($widget_settings['graph_custom'])).' Bits ('.$widget_settings['graph_range'].')';
$widget_settings['title'] = 'Overall '.ucfirst(htmlspecialchars($type)).' Bits ('.$widget_settings['graph_range'].')';
}
}
else {

View File

@ -1283,3 +1283,58 @@ function get_ripe_api_whois_data_json($ripe_data_param, $ripe_query_param) {
return json_decode(file_get_contents($ripe_whois_url) , true);
}//end get_ripe_api_whois_data_json()
/**
* Return the rows from 'ports' for all ports of a certain type as parsed by port_descr_parser.
* One or an array of strings can be provided as an argument; if an array is passed, all ports matching
* any of the types in the array are returned.
* @param $types mixed String or strings matching 'port_descr_type's.
* @return array Rows from the ports table for matching ports.
*/
function get_ports_from_type($given_types) {
global $config;
# Make the arg an array if it isn't, so subsequent steps only have to handle arrays.
if(!is_array($given_types)) {
$given_types = array($given_types);
}
# Check the config for a '_descr' entry for each argument. This is how a 'custom_descr' entry can
# be key/valued to some other string that's actually searched for in the DB. Merge or append the
# configured value if it's an array or a string. Or append the argument itself if there's no matching
# entry in config.
$search_types = array();
foreach($given_types as $type) {
if(isset($config[$type.'_descr']) === true) {
if (is_array($config[$type.'_descr']) === true) {
$search_types = array_merge($search_types, $config[$type.'_descr']);
}
else {
$search_types[] = $config[$type.'_descr'];
}
}
else {
$search_types[] = $type;
}
}
# Using the full list of strings to search the DB for, build the 'where' portion of a query that
# compares 'port_descr_type' with entry in the list. Also, since '@' is the convential wildcard,
# replace it with '%' so it functions as a wildcard in the SQL query.
$type_where = ' (';
$or = '';
$type_param = array();
foreach($search_types as $type) {
if (!empty($type)) {
$type = strtr($type, '@', '%');
$type_where .= " $or `port_descr_type` LIKE ?";
$or = 'OR';
$type_param[] = $type;
}
}
$type_where .= ') ';
# Run the query with the generated 'where' and necessary parameters, and send it back.
$ports = dbFetchRows("SELECT * FROM `ports` as I, `devices` AS D WHERE $type_where AND I.device_id = D.device_id ORDER BY I.ifAlias", $type_param);
return $ports;
}

View File

@ -16,27 +16,8 @@ else {
$bg = '#ffffff';
}
$type_where = ' (';
foreach (explode(',', $vars['type']) as $type) {
if (is_array($config[$type.'_descr']) === false) {
$config[$type.'_descr'] = array($config[$type.'_descr']);
}
foreach ($config[$type.'_descr'] as $additional_type) {
if (!empty($additional_type)) {
$type_where .= " $or `port_descr_type` = ?";
$or = 'OR';
$type_param[] = $additional_type;
}
}
$type_where .= " $or `port_descr_type` = ?";
$or = 'OR';
$type_param[] = $type;
}
$type_where .= ') ';
$ports = dbFetchRows("SELECT * FROM `ports` as I, `devices` AS D WHERE $type_where AND I.device_id = D.device_id ORDER BY I.ifAlias", $type_param);
$types_array = explode(',', $vars['type']);
$ports = get_ports_from_type($types_array);
foreach ($ports as $port) {
$if_list .= $seperator.$port['port_id'];
@ -45,7 +26,6 @@ foreach ($ports as $port) {
unset($seperator);
$types_array = explode(',', $vars['type']);
for ($i = 0; $i < count($types_array);
$i++) {
$types_array[$i] = ucfirst($types_array[$i]);