From 7d7479b9774d05cc9f2df158d85057d9061a3f24 Mon Sep 17 00:00:00 2001 From: laf Date: Mon, 16 Nov 2015 18:50:52 -0800 Subject: [PATCH] Added parsing support for lat/lng coords from device location --- html/includes/common/worldmap.inc.php | 13 +++++++++---- html/includes/dev-overview-data.inc.php | 5 ++++- html/includes/functions.inc.php | 1 + includes/common.php | 12 ++++++++++++ includes/polling/functions.inc.php | 5 ++++- 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/html/includes/common/worldmap.inc.php b/html/includes/common/worldmap.inc.php index bf9e6f36e..ebeebc412 100644 --- a/html/includes/common/worldmap.inc.php +++ b/html/includes/common/worldmap.inc.php @@ -122,17 +122,17 @@ var greenMarker = L.AwesomeMarkers.icon({ // Checking user permissions if (is_admin() || is_read()) { // Admin or global read-only - show all devices - $sql = "SELECT DISTINCT(`device_id`),`hostname`,`os`,`status`,`lat`,`lng` FROM `devices` + $sql = "SELECT DISTINCT(`device_id`),`devices`.`location`,`hostname`,`os`,`status`,`lat`,`lng` FROM `devices` LEFT JOIN `locations` ON `devices`.`location`=`locations`.`location` - WHERE `disabled`=0 AND `ignore`=0 AND `lat` != '' AND `lng` != '' + WHERE `disabled`=0 AND `ignore`=0 AND ((`lat` != '' AND `lng` != '') OR (`devices`.`location` REGEXP '\[[0-9\.\, ]+\]')) ORDER BY `status` ASC, `hostname`"; } else { // Normal user - grab devices that user has permissions to - $sql = "SELECT DISTINCT(`devices`.`device_id`) as `device_id`,`hostname`,`os`,`status`,`lat`,`lng` + $sql = "SELECT DISTINCT(`devices`.`device_id`) as `device_id`,`devices`.`location`,`hostname`,`os`,`status`,`lat`,`lng` FROM `devices_perms`, `devices` LEFT JOIN `locations` ON `devices`.`location`=`locations`.`location` - WHERE `disabled`=0 AND `ignore`=0 AND `lat` != '' AND `lng` != '' + WHERE `disabled`=0 AND `ignore`=0 AND ((`lat` != '' AND `lng` != '') OR (`devices`.`location` REGEXP '\[[0-9\.\, ]+\]')) AND `devices`.`device_id` = `devices_perms`.`device_id` AND `devices_perms`.`user_id` = ? ORDER BY `status` ASC, `hostname`"; @@ -140,6 +140,11 @@ var greenMarker = L.AwesomeMarkers.icon({ foreach (dbFetchRows($sql, array($_SESSION['user_id'])) as $map_devices) { $icon = 'greenMarker'; $z_offset = 0; + $tmp_loc = parse_location($map_devices['location']); + if (!empty($tmp_loc['lat']) && !empty($tmp_loc['lng'])) { + $map_devices['lat'] = $tmp_loc['lat']; + $map_devices['lng'] = $tmp_loc['lng']; + } if ($map_devices['status'] == 0) { $icon = 'redMarker'; $z_offset = 10000; // move marker to foreground diff --git a/html/includes/dev-overview-data.inc.php b/html/includes/dev-overview-data.inc.php index 4adae6a31..de602c18f 100644 --- a/html/includes/dev-overview-data.inc.php +++ b/html/includes/dev-overview-data.inc.php @@ -78,7 +78,10 @@ if ($device['location']) { } } -$loc = dbFetchRow("SELECT `lat`,`lng` FROM `locations` WHERE `location`=? LIMIT 1", array($device['location'])); +$loc = parse_location($device['location']); +if (!is_array($loc)) { + $loc = dbFetchRow("SELECT `lat`,`lng` FROM `locations` WHERE `location`=? LIMIT 1", array($device['location'])); +} if (is_array($loc)) { echo ' Lat / Lng diff --git a/html/includes/functions.inc.php b/html/includes/functions.inc.php index 21121d0ce..3246cdc9a 100644 --- a/html/includes/functions.inc.php +++ b/html/includes/functions.inc.php @@ -1249,3 +1249,4 @@ function generate_dynamic_config_panel($title,$end_panel=true,$config_groups,$it } return $output; }//end generate_dynamic_config_panel() + diff --git a/includes/common.php b/includes/common.php index 2f7a7aedc..167468d44 100644 --- a/includes/common.php +++ b/includes/common.php @@ -795,3 +795,15 @@ function ceph_rrd($gtype) { $rrd = join('-', array('app', 'ceph', $vars['id'], $gtype, $var)).'.rrd'; return join('/', array($config['rrd_dir'], $device['hostname'], $rrd)); } + +/** + * Parse location field for coordinates + * @param string location The location field to look for coords in. + * @return array Containing the lat and lng coords +**/ +function parse_location($location) { + preg_match('/(\[)([0-9\. ]+), ([0-9\. ]+)(\])/', $location, $tmp_loc); + if (!empty($tmp_loc[2]) && !empty($tmp_loc[3])) { + return array('lat' => $tmp_loc[2], 'lng' => $tmp_loc[3]); + } +}//end parse_location() diff --git a/includes/polling/functions.inc.php b/includes/polling/functions.inc.php index fff7bbe45..72332250e 100644 --- a/includes/polling/functions.inc.php +++ b/includes/polling/functions.inc.php @@ -445,7 +445,10 @@ function location_to_latlng($device) { if (!empty($device_location)) { $new_device_location = preg_replace("/ /","+",$device_location); // We have a location string for the device. - $loc = dbFetchRow("SELECT `lat`,`lng` FROM `locations` WHERE `location`=? LIMIT 1", array($device_location)); + $loc = parse_location($device_location); + if (!is_array($loc)) { + $loc = dbFetchRow("SELECT `lat`,`lng` FROM `locations` WHERE `location`=? LIMIT 1", array($device_location)); + } if (is_array($loc) === false) { // Grab data from which ever Geocode service we use. switch ($config['geoloc']['engine']) {