| 
									
										
										
										
											2017-06-26 07:58:17 -05:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2018-04-07 15:55:28 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | use LibreNMS\Authentication\Auth; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-26 07:58:17 -05:00
										 |  |  | $param = array(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 21:14:28 +02:00
										 |  |  | $select = "SELECT `F`.`port_id` AS `port_id`, `F`.`device_id`, `ifInErrors`, `ifOutErrors`, `ifOperStatus`,"; | 
					
						
							| 
									
										
										
										
											2017-07-04 16:27:43 -05:00
										 |  |  | $select .= " `ifAdminStatus`, `ifAlias` AS `interface`, `ifDescr`, `mac_address`, `V`.`vlan_vlan` AS `vlan`,"; | 
					
						
							|  |  |  | $select .= " `hostname`, `hostname` AS `device` , group_concat(`M`.`ipv4_address` SEPARATOR ', ') AS `ipv4_address`"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $sql  = " FROM `ports_fdb` AS `F`"; | 
					
						
							|  |  |  | $sql .= " LEFT JOIN `devices` AS `D` USING(`device_id`)"; | 
					
						
							|  |  |  | $sql .= " LEFT JOIN `ports` AS `P` USING(`port_id`, `device_id`)"; | 
					
						
							|  |  |  | $sql .= " LEFT JOIN `vlans` AS `V` USING(`vlan_id`, `device_id`)"; | 
					
						
							| 
									
										
										
										
											2018-04-07 21:14:28 +02:00
										 |  |  | // Add counter so we can ORDER BY the port_id with least amount of macs attached
 | 
					
						
							|  |  |  | $sql .= " LEFT JOIN ( SELECT `port_id`, COUNT(*) `portCount` FROM `ports_fdb` GROUP BY `port_id` ) AS `C` ON `C`.`port_id` = `F`.`port_id`"; | 
					
						
							| 
									
										
										
										
											2017-07-04 16:27:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | $where = " WHERE 1"; | 
					
						
							| 
									
										
										
										
											2017-06-26 07:58:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 15:55:28 -05:00
										 |  |  | if (!Auth::user()->hasGlobalRead()) { | 
					
						
							| 
									
										
										
										
											2017-07-04 16:27:43 -05:00
										 |  |  |     $sql    .= ' LEFT JOIN `devices_perms` AS `DP` USING (`device_id`)'; | 
					
						
							| 
									
										
										
										
											2017-06-26 07:58:17 -05:00
										 |  |  |     $where  .= ' AND `DP`.`user_id`=?'; | 
					
						
							| 
									
										
										
										
											2018-04-07 15:55:28 -05:00
										 |  |  |     $param[] = Auth::id(); | 
					
						
							| 
									
										
										
										
											2017-06-26 07:58:17 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 22:50:09 +02:00
										 |  |  | if (is_numeric($vars['device_id'])) { | 
					
						
							| 
									
										
										
										
											2017-07-04 16:27:43 -05:00
										 |  |  |     $where    .= ' AND `F`.`device_id`=?'; | 
					
						
							| 
									
										
										
										
											2018-03-25 22:50:09 +02:00
										 |  |  |     $param[] = $vars['device_id']; | 
					
						
							| 
									
										
										
										
											2017-06-26 07:58:17 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 22:50:09 +02:00
										 |  |  | if (is_numeric($vars['port_id'])) { | 
					
						
							| 
									
										
										
										
											2017-07-04 16:27:43 -05:00
										 |  |  |     $where    .= ' AND `F`.`port_id`=?'; | 
					
						
							| 
									
										
										
										
											2018-03-25 22:50:09 +02:00
										 |  |  |     $param[] = $vars['port_id']; | 
					
						
							| 
									
										
										
										
											2017-06-26 07:58:17 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 22:50:09 +02:00
										 |  |  | if (isset($vars['searchPhrase']) && !empty($vars['searchPhrase'])) { | 
					
						
							|  |  |  |     $search = mres(trim($vars['searchPhrase'])); | 
					
						
							| 
									
										
										
										
											2018-04-07 21:14:28 +02:00
										 |  |  |     $ip_search = '%'.mres(trim($vars['searchPhrase'])).'%'; | 
					
						
							| 
									
										
										
										
											2017-07-04 16:27:43 -05:00
										 |  |  |     $mac_search = '%'.str_replace(array(':', ' ', '-', '.', '0x'), '', $search).'%'; | 
					
						
							| 
									
										
										
										
											2017-06-26 07:58:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 22:50:09 +02:00
										 |  |  |     if (isset($vars['searchby']) && $vars['searchby'] == 'vlan') { | 
					
						
							| 
									
										
										
										
											2017-07-04 16:27:43 -05:00
										 |  |  |         $where  .= ' AND `V`.`vlan_vlan` = ?'; | 
					
						
							|  |  |  |         $param[] = (int)$search; | 
					
						
							| 
									
										
										
										
											2018-04-07 21:14:28 +02:00
										 |  |  |     } elseif (isset($vars['searchby']) && $vars['searchby'] == 'ip') { | 
					
						
							|  |  |  |         $sql .= " LEFT JOIN `ipv4_mac` AS `M` USING (`mac_address`)"; | 
					
						
							|  |  |  |         $where  .= ' AND `M`.`ipv4_address` LIKE ?'; | 
					
						
							|  |  |  |         $param[] = $ip_search; | 
					
						
							| 
									
										
										
										
											2018-03-25 22:50:09 +02:00
										 |  |  |     } elseif ((isset($vars['searchby']) && $vars['searchby'] == 'mac') || | 
					
						
							| 
									
										
										
										
											2017-07-04 16:27:43 -05:00
										 |  |  |         (!is_numeric($search) || $search > 4096) | 
					
						
							|  |  |  |     ) { | 
					
						
							|  |  |  |         $where  .= ' AND `F`.`mac_address` LIKE ?'; | 
					
						
							| 
									
										
										
										
											2017-06-26 07:58:17 -05:00
										 |  |  |         $param[] = $mac_search; | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2017-07-04 16:27:43 -05:00
										 |  |  |         $where  .= ' AND (`V`.`vlan_vlan` = ? OR `F`.`mac_address` LIKE ?)'; | 
					
						
							|  |  |  |         $param[] = (int)$search; | 
					
						
							| 
									
										
										
										
											2017-06-26 07:58:17 -05:00
										 |  |  |         $param[] = $mac_search; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-04 16:27:43 -05:00
										 |  |  | $total = (int)dbFetchCell("SELECT COUNT(*) $sql $where", $param); | 
					
						
							| 
									
										
										
										
											2017-06-26 07:58:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 21:14:28 +02:00
										 |  |  | // Don't use ipv4_mac in count it will inflate the rows unless we aggregate it
 | 
					
						
							|  |  |  | // Except for 'ip' search.
 | 
					
						
							|  |  |  | if ($vars['searchby'] != 'ip') { | 
					
						
							|  |  |  |     $sql .= " LEFT JOIN `ipv4_mac` AS `M` USING (`mac_address`)"; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-07-04 16:27:43 -05:00
										 |  |  | $sql .= $where; | 
					
						
							|  |  |  | $sql .= " GROUP BY `device_id`, `port_id`, `mac_address`, `vlan`, `hostname`, `ifAlias`,"; | 
					
						
							|  |  |  | $sql .= " `ifAdminStatus`, `ifDescr`, `ifOperStatus`, `ifInErrors`, `ifOutErrors`"; | 
					
						
							| 
									
										
										
										
											2017-06-26 07:58:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 21:14:28 +02:00
										 |  |  | // Get most likely endpoint port_id, used to add a visual marker for this element
 | 
					
						
							|  |  |  | // in the list 
 | 
					
						
							|  |  |  | if (isset($vars['searchby']) && !empty($vars['searchPhrase']) && $vars['searchby'] != 'vlan') { | 
					
						
							|  |  |  |     $countsql .= " ORDER BY `C`.`portCount` ASC LIMIT 1"; | 
					
						
							|  |  |  |     foreach (dbFetchRows($select . $sql . $countsql, $param) as $entry) { | 
					
						
							|  |  |  |         $endpoint_portid = $entry['port_id']; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-06-26 07:58:17 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 21:14:28 +02:00
										 |  |  | if (!isset($sort) || empty($sort)) { | 
					
						
							|  |  |  |     $sort = '`C`.`portCount` ASC'; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-06-26 07:58:17 -05:00
										 |  |  | $sql .= " ORDER BY $sort"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if (isset($current)) { | 
					
						
							|  |  |  |     $limit_low  = (($current * $rowCount) - ($rowCount)); | 
					
						
							|  |  |  |     $limit_high = $rowCount; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if ($rowCount != -1) { | 
					
						
							|  |  |  |     $sql .= " LIMIT $limit_low,$limit_high"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-04 16:27:43 -05:00
										 |  |  | $response = array(); | 
					
						
							|  |  |  | foreach (dbFetchRows($select . $sql, $param) as $entry) { | 
					
						
							| 
									
										
										
										
											2017-06-26 07:58:17 -05:00
										 |  |  |     $entry = cleanPort($entry); | 
					
						
							|  |  |  |     if (!$ignore) { | 
					
						
							|  |  |  |         if ($entry['ifInErrors'] > 0 || $entry['ifOutErrors'] > 0) { | 
					
						
							| 
									
										
										
										
											2017-07-04 16:27:43 -05:00
										 |  |  |             $error_img = generate_port_link( | 
					
						
							|  |  |  |                 $entry, | 
					
						
							|  |  |  |                 "<i class='fa fa-flag fa-lg' style='color:red' aria-hidden='true'></i>", | 
					
						
							|  |  |  |                 'port_errors' | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2017-06-26 07:58:17 -05:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             $error_img = ''; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-07 21:14:28 +02:00
										 |  |  |         if ($entry['port_id'] == $endpoint_portid) { | 
					
						
							|  |  |  |             $endpoint_img = "<i class='fa fa-star fa-lg' style='color:green' aria-hidden='true' title='This indicates the most likely endpoint switchport'></i>"; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $endpoint_img = ''; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-06-26 07:58:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $response[] = array( | 
					
						
							| 
									
										
										
										
											2017-07-04 16:27:43 -05:00
										 |  |  |             'device'       => generate_device_link(device_by_id_cache($entry['device_id'])), | 
					
						
							|  |  |  |             'mac_address'  => formatMac($entry['mac_address']), | 
					
						
							|  |  |  |             'ipv4_address' => $entry['ipv4_address'], | 
					
						
							| 
									
										
										
										
											2018-04-07 21:14:28 +02:00
										 |  |  |             'interface'    => generate_port_link($entry, makeshortif(fixifname($entry['label']))).' '.$error_img.' '.$endpoint_img, | 
					
						
							| 
									
										
										
										
											2017-07-04 16:27:43 -05:00
										 |  |  |             'vlan'         => $entry['vlan'], | 
					
						
							| 
									
										
										
										
											2017-06-26 07:58:17 -05:00
										 |  |  |         ); | 
					
						
							|  |  |  |     }//end if
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     unset($ignore); | 
					
						
							|  |  |  | }//end foreach
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $output = array( | 
					
						
							|  |  |  |     'current'  => $current, | 
					
						
							|  |  |  |     'rowCount' => $rowCount, | 
					
						
							|  |  |  |     'rows'     => $response, | 
					
						
							|  |  |  |     'total'    => $total, | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | echo _json_encode($output); |