From ff3e2bad8c335b0bc25b70b913cac8aaa8151545 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stephan=20B=C3=B6sch-Plepelits?= <skunk@xover.mud.at>
Date: Sat, 9 Sep 2017 21:48:36 +0200
Subject: [PATCH] Instead of FreeGeoIP use local GeoIP database

---
 .gitignore                |  3 +++
 bin/download_dependencies |  5 +++++
 composer.json             |  5 +++++
 index.php                 |  1 +
 modulekit.php             |  1 +
 src/index.js              |  1 -
 src/ip-location.js        | 19 -------------------
 src/ip-location.php       | 24 ++++++++++++++++++++++++
 src/location.js           | 17 -----------------
 9 files changed, 39 insertions(+), 37 deletions(-)
 create mode 100755 bin/download_dependencies
 create mode 100644 composer.json
 delete mode 100644 src/ip-location.js
 create mode 100644 src/ip-location.php
 delete mode 100644 src/location.js

diff --git a/.gitignore b/.gitignore
index 6a32cee2..f1407379 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,4 @@
 /conf.php
+/vendor/
+/data/
+/composer.lock
diff --git a/bin/download_dependencies b/bin/download_dependencies
new file mode 100755
index 00000000..3b18f8c2
--- /dev/null
+++ b/bin/download_dependencies
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+mkdir -p data/GeoIP
+cd data/GeoIP
+wget -O- http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz | tar --strip-components=1 -xvzf -
diff --git a/composer.json b/composer.json
new file mode 100644
index 00000000..bdcb887e
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,5 @@
+{
+    "require": {
+        "geoip2/geoip2": "~2.0"
+    }
+}
diff --git a/index.php b/index.php
index 2dcc0abb..2b17183f 100644
--- a/index.php
+++ b/index.php
@@ -1,5 +1,6 @@
 <?php include "conf.php"; /* load a local configuration */ ?>
 <?php session_start(); ?>
+<?php require 'vendor/autoload.php'; /* composer includes */ ?>
 <?php include "modulekit/loader.php"; /* loads all php-includes */ ?>
 <?php call_hooks("init"); /* initialize submodules */ ?>
 <?php
diff --git a/modulekit.php b/modulekit.php
index 6f2b1f63..f22fca9e 100644
--- a/modulekit.php
+++ b/modulekit.php
@@ -13,6 +13,7 @@ $include = array(
   'php' => array(
     'src/options.php',
     'src/language.php',
+    'src/ip-location.php',
   ),
   'css' => array(
     'style.css',
diff --git a/src/index.js b/src/index.js
index 85d934cc..f104df58 100644
--- a/src/index.js
+++ b/src/index.js
@@ -21,7 +21,6 @@ var lastPopupClose = 0
 // Optional modules
 require('./options')
 require('./language')
-require('./location')
 require('./overpassChooser')
 require('./fullscreen')
 require('./mapLayers')
diff --git a/src/ip-location.js b/src/ip-location.js
deleted file mode 100644
index 6d098e5f..00000000
--- a/src/ip-location.js
+++ /dev/null
@@ -1,19 +0,0 @@
-var ipLocation = require('ip-location')
-
-ipLocation.httpGet = function (url, callback) {
-  var xhr = new XMLHttpRequest()
-  xhr.open('get', url, true)
-  xhr.responseType = 'text'
-  xhr.onreadystatechange = function () {
-    if (xhr.readyState === 4) {
-      if (xhr.status === 200) {
-        callback(null, { body: xhr.responseText })
-      } else {
-        callback(xhr.responseText)
-      }
-    }
-  }
-  xhr.send()
-}
-
-module.exports = ipLocation
diff --git a/src/ip-location.php b/src/ip-location.php
new file mode 100644
index 00000000..e3b3656f
--- /dev/null
+++ b/src/ip-location.php
@@ -0,0 +1,24 @@
+<?php
+use GeoIp2\Database\Reader;
+
+register_hook('init', function () {
+  global $config;
+
+  if (isset($config['checkIpLocation']) && !$config['checkIpLocation']) {
+    return;
+  }
+
+  $reader = new Reader('data/GeoIP/GeoLite2-City.mmdb');
+
+  try {
+    $record = $reader->city($_SERVER['REMOTE_ADDR']);
+
+    $config['defaultView']['lat'] = $record->location->latitude;
+    $config['defaultView']['lon'] = $record->location->longitude;
+    $config['defaultView']['zoom'] = 10;
+  }
+  catch (Exception $e) {
+    // ignore error
+    trigger_error("Can't resolve IP address: " . $e->getMessage(), E_USER_WARNING);
+  }
+});
diff --git a/src/location.js b/src/location.js
deleted file mode 100644
index f160b199..00000000
--- a/src/location.js
+++ /dev/null
@@ -1,17 +0,0 @@
-var ipLocation = require('./ip-location')
-
-register_hook('init_callback', function (initState, callback) {
-  if ('checkIpLocation' in config && !config.checkIpLocation) {
-    return callback()
-  }
-
-  ipLocation('', function (err, ipLoc) {
-    if (typeof ipLoc === 'object' && 'latitude' in ipLoc) {
-      initState.zoom = 14
-      initState.lat = ipLoc.latitude
-      initState.lon = ipLoc.longitude
-    }
-
-    callback(err)
-  })
-})