krz/ifconfig.php

A private ifconfig service.

clone: git clone https://gitbay.org/krz/ifconfig.php.git

e30b8e027eb92e9257afbc4c28fff466d6b37c38

unsigned

author: Christian Cleberg <hello@cleberg.net> · 2023-05-25T02:18:39Z

add geolocation functionality
 README.md     | 26 ++++++++++++++++--
 composer.json |  5 ++++
 composer.lock | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ifconfig.php  | 56 +++++++++++++++++++++++++++------------
 4 files changed, 152 insertions(+), 19 deletions(-)

diff --git a/README.md b/README.md
index 39f6c48..e1231b3 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,4 @@
-ifconfig.php
-============
+# ifconfig.php
 
 **Simple PHP script to show IP address, UserAgent and other info**
 
@@ -27,3 +26,26 @@ With this code in the .htaccess file there is no need to type out the full URL,
 Instead, you can just open 'ifconfig/xml' to get XML output or 'ifconfig/ua' to determine useragent.
 
 Run `curl -L -X GET https://ip.cleberg.net/\?q\=text` in Linux terminal to get some info.
+
+## Development
+
+First, install PHP and Composer. For Fedora, run:
+
+```bash
+sudo dnf install php-cli composer
+```
+
+If applicable, install or update the maxmind-db dependency:
+
+```bash
+composer require maxmind-db/reader:~1.0
+
+# to update, run:
+composer update
+```
+
+Next, you can launch a dev server to test any of your changes:
+
+```bash
+sudo php --server localhost:8080 --docroot  .
+```
diff --git a/composer.json b/composer.json
new file mode 100644
index 0000000..c387eef
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,5 @@
+{
+    "require": {
+        "maxmind-db/reader": "~1.0"
+    }
+}
diff --git a/composer.lock b/composer.lock
new file mode 100644
index 0000000..3e3efcf
--- /dev/null
+++ b/composer.lock
@@ -0,0 +1,84 @@
+{
+    "_readme": [
+        "This file locks the dependencies of your project to a known state",
+        "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
+        "This file is @generated automatically"
+    ],
+    "content-hash": "ccf38c391dd2003a7c8899a77fd6b1f4",
+    "packages": [
+        {
+            "name": "maxmind-db/reader",
+            "version": "v1.11.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/maxmind/MaxMind-DB-Reader-php.git",
+                "reference": "b1f3c0699525336d09cc5161a2861268d9f2ae5b"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/maxmind/MaxMind-DB-Reader-php/zipball/b1f3c0699525336d09cc5161a2861268d9f2ae5b",
+                "reference": "b1f3c0699525336d09cc5161a2861268d9f2ae5b",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=7.2"
+            },
+            "conflict": {
+                "ext-maxminddb": "<1.10.1,>=2.0.0"
+            },
+            "require-dev": {
+                "friendsofphp/php-cs-fixer": "3.*",
+                "php-coveralls/php-coveralls": "^2.1",
+                "phpstan/phpstan": "*",
+                "phpunit/phpcov": ">=6.0.0",
+                "phpunit/phpunit": ">=8.0.0,<10.0.0",
+                "squizlabs/php_codesniffer": "3.*"
+            },
+            "suggest": {
+                "ext-bcmath": "bcmath or gmp is required for decoding larger integers with the pure PHP decoder",
+                "ext-gmp": "bcmath or gmp is required for decoding larger integers with the pure PHP decoder",
+                "ext-maxminddb": "A C-based database decoder that provides significantly faster lookups"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "MaxMind\\Db\\": "src/MaxMind/Db"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "Apache-2.0"
+            ],
+            "authors": [
+                {
+                    "name": "Gregory J. Oschwald",
+                    "email": "goschwald@maxmind.com",
+                    "homepage": "https://www.maxmind.com/"
+                }
+            ],
+            "description": "MaxMind DB Reader API",
+            "homepage": "https://github.com/maxmind/MaxMind-DB-Reader-php",
+            "keywords": [
+                "database",
+                "geoip",
+                "geoip2",
+                "geolocation",
+                "maxmind"
+            ],
+            "support": {
+                "issues": "https://github.com/maxmind/MaxMind-DB-Reader-php/issues",
+                "source": "https://github.com/maxmind/MaxMind-DB-Reader-php/tree/v1.11.0"
+            },
+            "time": "2021-10-18T15:23:10+00:00"
+        }
+    ],
+    "packages-dev": [],
+    "aliases": [],
+    "minimum-stability": "stable",
+    "stability-flags": [],
+    "prefer-stable": false,
+    "prefer-lowest": false,
+    "platform": [],
+    "platform-dev": [],
+    "plugin-api-version": "2.3.0"
+}
diff --git a/ifconfig.php b/ifconfig.php
index 8ece6ba..fc4dd30 100644
--- a/ifconfig.php
+++ b/ifconfig.php
@@ -1,28 +1,50 @@
 <?php
 
+require 'vendor/autoload.php';
+use MaxMind\Db\Reader;
+
+function loadGeo(string $ipAddress): ?array
+{
+        $databaseFile = 'GeoLite2-City.mmdb';
+        $reader = new Reader($databaseFile);
+        $results = $reader->get($ipAddress);
+        $reader->close();
+
+        return $results;
+}
+
+// Set top-level variables for HTML
 $site_title = 'ip.cleberg.net';
 $site_owner_name = 'Christian Cleberg';
 $site_owner_url = 'https://cleberg.net';
 $source_repository = 'https://git.sr.ht/~cmc/ifconfig.php';
 
+// Extract geolocation
+$geo = loadGeo($_SERVER['REMOTE_ADDR']);
+
 // Create array of values
 $user = array(
-        'ip' 	     => $_SERVER['REMOTE_ADDR'],
-	'host' 	     => (isset($_SERVER['REMOTE_ADDR']) ? gethostbyaddr($_SERVER['REMOTE_ADDR']) : ""),
-	'port' 	     => $_SERVER['REMOTE_PORT'],
-	'ua' 	     => $_SERVER['HTTP_USER_AGENT'],
-	'lang' 	     => $_SERVER['HTTP_ACCEPT_LANGUAGE'],
-	'mime' 	     => $_SERVER['HTTP_ACCEPT'],
-	'encoding'   => $_SERVER['HTTP_ACCEPT_ENCODING'],
-	'charset'    => $_SERVER['HTTP_ACCEPT_CHARSET'],
-	'connection' => $_SERVER['HTTP_CONNECTION'],
-	'cache'      => $_SERVER['HTTP_CACHE_CONTROL'],
-	'cookie'     => $_SERVER['HTTP_COOKIE'],
-	'referer'    => $_SERVER['HTTP_REFERER'],
-	'real_ip'    => $_SERVER['HTTP_X_REAL_IP'],
-	'fwd_ip'     => $_SERVER['HTTP_X_FORWARDED_FOR'],
-	'fwd_host'   => (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? gethostbyaddr($_SERVER['HTTP_X_FORWARDED_FOR']) : ""),
-	'dnt' 	     => $_SERVER['HTTP_DNT']
+        'ip' 	         => $_SERVER['REMOTE_ADDR'],
+	'host' 	         => (isset($_SERVER['REMOTE_ADDR']) ? gethostbyaddr($_SERVER['REMOTE_ADDR']) : ""),
+	'port' 	         => $_SERVER['REMOTE_PORT'],
+	'ua' 	         => $_SERVER['HTTP_USER_AGENT'],
+	'lang' 	         => $_SERVER['HTTP_ACCEPT_LANGUAGE'],
+	'mime' 	         => $_SERVER['HTTP_ACCEPT'],
+	'encoding'       => $_SERVER['HTTP_ACCEPT_ENCODING'],
+	'charset'        => $_SERVER['HTTP_ACCEPT_CHARSET'],
+	'connection'     => $_SERVER['HTTP_CONNECTION'],
+	'cache'          => $_SERVER['HTTP_CACHE_CONTROL'],
+	'cookie'         => $_SERVER['HTTP_COOKIE'],
+	'referer'        => $_SERVER['HTTP_REFERER'],
+	'real_ip'        => $_SERVER['HTTP_X_REAL_IP'],
+	'fwd_ip'         => $_SERVER['HTTP_X_FORWARDED_FOR'],
+	'fwd_host'       => (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? gethostbyaddr($_SERVER['HTTP_X_FORWARDED_FOR']) : ""),
+	'dnt' 	         => $_SERVER['HTTP_DNT'],
+        'continent_code' => $geo['continent']['code'],
+        'continent_name' => $geo['continent']['names']['en'],
+        'country_code'   => $geo['country']['geoname_id'],
+        'country_iso'    => $geo['country']['iso_code'],
+        'country_name'   => $geo['country']['names']['en']
 	);
 
 // Check request (ex. ifconfig.php?q=ip)
@@ -104,7 +126,7 @@ elseif (isset($query) && (($query=="text") || ($query=="all"))) {
 	$date = date("Y");
 	echo <<<EOD
 	<br>
-	<p><small>Copyright &copy; {$date} <a href="{$site_owner_url}">{$site_owner_name}</a>.
+	<p><small>Copyright &copy; {$date} <a href="{$site_owner_url}">{$site_owner_name}</a>
 	<br><a target="_blank" rel="noreferrer" href="{$source_repository}">Source Code</a></small></p>
 	</body>
 	</html>