From 98c38351acf58f2ea9987cd42e267e2e80168f99 Mon Sep 17 00:00:00 2001
From: Simone Mainardi <simonemainardi@gmail.com>
Date: Wed, 6 Jul 2016 11:46:37 +0200
Subject: [PATCH] Adds the ability to set a custom Google Maps API key

Users can now generate a custom Google Maps API key and
set it via ntopng Preferences page.

Addresses #646
---
 httpdocs/inc/geolocation_disclaimer.inc |  5 ++++-
 scripts/lua/admin/prefs.lua             | 12 ++++++++++--
 scripts/lua/host_details.lua            |  7 ++++++-
 scripts/lua/hosts_geomap.lua            | 10 ++++++++--
 scripts/lua/modules/lua_utils.lua       | 10 ++++++++++
 5 files changed, 38 insertions(+), 6 deletions(-)

diff --git a/httpdocs/inc/geolocation_disclaimer.inc b/httpdocs/inc/geolocation_disclaimer.inc
index f7a9fbc..85fbeff 100644
--- a/usr/local/share/ntopng/httpdocs/inc/geolocation_disclaimer.inc
+++ b/usr/local/share/ntopng/httpdocs/inc/geolocation_disclaimer.inc
@@ -8,5 +8,8 @@
 <li> <small>Have active flows between peers with public IP addresses.</small>
 </ol>
 <li> <small>HTML <A HREF=http://diveintohtml5.info/geolocation.html>browser geolocation</A> is used to place on map hosts based on unknown locations.</small>
-</ul>
+<li> <small>A Google Maps Browser API Key may be required. Detailed information on key generation <a href="https://googlegeodevelopers.blogspot.it/2016/06/building-for-scale-updates-to-google.html">can be found here</a>.<br>
+   Once generated, the key can be submitted via ntopng <a href="@HTTP_PREFIX@/lua/admin/prefs.lua?subpage_active=users"><i class="fa fa-flask"></i> Preferences</a>.
+   </small>
+</ol>
 </small>
diff --git a/scripts/lua/admin/prefs.lua b/scripts/lua/admin/prefs.lua
index 4b52392..fa9fa1e 100644
--- a/usr/local/share/ntopng/scripts/lua/admin/prefs.lua
+++ b/usr/local/share/ntopng/scripts/lua/admin/prefs.lua
@@ -202,13 +202,21 @@ function printUsers()
   print('<input type=hidden name="subpage_active" value="users"/>\n')
   print('<table class="table">')
 
+  print('<tr><th colspan=2 class="info">Web User Interface</th></tr>')
   if prefs.is_autologout_enabled == true then
-     print('<tr><th colspan=2 class="info">Web User Interface</th></tr>')
-
      toggleTableButtonPrefs("Auto Logout",
 			    "Toggle the automatic logout of web interface users with expired sessions.",
 			    "On", "1", "success", "Off", "0", "danger", "toggle_autologout", "ntopng.prefs.is_autologon_enabled", "1")
   end
+  prefsInputFieldPrefs("Google APIs Browser Key",
+		       "Graphical hosts geomaps are based on Google Maps APIs. Google recently changed Maps API access policies "..
+		       "and now requires a browser API key to be sumbitted for every request. Detailed information on how to obtain an API key "..
+		       "<a href=\"https://googlegeodevelopers.blogspot.it/2016/06/building-for-scale-updates-to-google.html\">can be found here</a>. "..
+                       "Once obtained, the API key can be placed in this field."
+		       ,
+		       "ntopng.prefs.",
+		       "google_apis_browser_key",
+		       "")
 
   if ntop.isPro() then
 
diff --git a/scripts/lua/host_details.lua b/scripts/lua/host_details.lua
index acbdd80..780b913 100644
--- a/usr/local/share/ntopng/scripts/lua/host_details.lua
+++ b/usr/local/share/ntopng/scripts/lua/host_details.lua
@@ -1445,7 +1445,12 @@ print [[
    </style>
 
 </center>
-    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
+]]
+
+addGoogleMapsScript()
+
+print[[
+
     <script src="]] print(ntop.getHttpPrefix()) print [[/js/markerclusterer.js"></script>
 <div class="container-fluid">
   <div class="row-fluid">
diff --git a/scripts/lua/hosts_geomap.lua b/scripts/lua/hosts_geomap.lua
index d170c94..c9581e1 100644
--- a/usr/local/share/ntopng/scripts/lua/hosts_geomap.lua
+++ b/usr/local/share/ntopng/scripts/lua/hosts_geomap.lua
@@ -22,6 +22,7 @@ for key, value in pairs(hosts_stats) do
 end
 
 
+
 if(num > 0) then
 print [[
 
@@ -32,7 +33,12 @@ print [[
 <hr>
 <h2>Hosts GeoMap</H2>
 
-    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
+]]
+
+addGoogleMapsScript()
+
+print[[
+
     <script src="]] print(ntop.getHttpPrefix()) print [[/js/markerclusterer.js"></script>
 <div class="container-fluid">
   <div class="row-fluid">
@@ -61,4 +67,4 @@ else
    print("<div class=\"alert alert-danger\"><img src=".. ntop.getHttpPrefix() .. "/img/warning.png> No results found</div>")
 end
 
-dofile(dirs.installdir .. "/scripts/lua/inc/footer.lua")
\ No newline at end of file
+dofile(dirs.installdir .. "/scripts/lua/inc/footer.lua")
diff --git a/scripts/lua/modules/lua_utils.lua b/scripts/lua/modules/lua_utils.lua
index 3f218d8..3e5e36b 100644
--- a/usr/local/share/ntopng/scripts/lua/modules/lua_utils.lua
+++ b/usr/local/share/ntopng/scripts/lua/modules/lua_utils.lua
@@ -947,6 +947,16 @@ function isIPv6(ip)
   return true
 end
 
+function addGoogleMapsScript()
+   local g_maps_key = ntop.getCache('ntopng.prefs.google_apis_browser_key')
+   if g_maps_key ~= nil and g_maps_key~= "" then
+      g_maps_key = "&key="..g_maps_key
+   else
+   g_maps_key = ""
+   end
+   print("<script src=\"https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"..g_maps_key.."\"></script>\n")
+end
+
 
 
 function addGauge(name, url, maxValue, width, height)
