Commit e30b8e027e

e30b8e027eb92e9257afbc4c28fff466d6b37c38

parent: 864f1349e4

Unsigned

cmc <hello@cleberg.net> · 2023-05-25 02:18 UTC

add geolocation functionality

Layout: unified · split

README.md +24 −2
@@ -1,5 +1,4 @@
1ifconfig.php 1# ifconfig.php
2============
3 2
4**Simple PHP script to show IP address, UserAgent and other info** 3**Simple PHP script to show IP address, UserAgent and other info**
5 4
@@ -27,3 +26,26 @@ With this code in the .htaccess file there is no need to type out the full URL,
27Instead, you can just open 'ifconfig/xml' to get XML output or 'ifconfig/ua' to determine useragent. 26Instead, you can just open 'ifconfig/xml' to get XML output or 'ifconfig/ua' to determine useragent.
28 27
29Run `curl -L -X GET https://ip.cleberg.net/\?q\=text` in Linux terminal to get some info. 28Run `curl -L -X GET https://ip.cleberg.net/\?q\=text` in Linux terminal to get some info.
29
30## Development
31
32First, install PHP and Composer. For Fedora, run:
33
34```bash
35sudo dnf install php-cli composer
36```
37
38If applicable, install or update the maxmind-db dependency:
39
40```bash
41composer require maxmind-db/reader:~1.0
42
43# to update, run:
44composer update
45```
46
47Next, you can launch a dev server to test any of your changes:
48
49```bash
50sudo php --server localhost:8080 --docroot .
51```
composer.json added +5
@@ -0,0 +1,5 @@
1{
2 "require": {
3 "maxmind-db/reader": "~1.0"
4 }
5}
composer.lock added +84
@@ -0,0 +1,84 @@
1{
2 "_readme": [
3 "This file locks the dependencies of your project to a known state",
4 "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
5 "This file is @generated automatically"
6 ],
7 "content-hash": "ccf38c391dd2003a7c8899a77fd6b1f4",
8 "packages": [
9 {
10 "name": "maxmind-db/reader",
11 "version": "v1.11.0",
12 "source": {
13 "type": "git",
14 "url": "https://github.com/maxmind/MaxMind-DB-Reader-php.git",
15 "reference": "b1f3c0699525336d09cc5161a2861268d9f2ae5b"
16 },
17 "dist": {
18 "type": "zip",
19 "url": "https://api.github.com/repos/maxmind/MaxMind-DB-Reader-php/zipball/b1f3c0699525336d09cc5161a2861268d9f2ae5b",
20 "reference": "b1f3c0699525336d09cc5161a2861268d9f2ae5b",
21 "shasum": ""
22 },
23 "require": {
24 "php": ">=7.2"
25 },
26 "conflict": {
27 "ext-maxminddb": "<1.10.1,>=2.0.0"
28 },
29 "require-dev": {
30 "friendsofphp/php-cs-fixer": "3.*",
31 "php-coveralls/php-coveralls": "^2.1",
32 "phpstan/phpstan": "*",
33 "phpunit/phpcov": ">=6.0.0",
34 "phpunit/phpunit": ">=8.0.0,<10.0.0",
35 "squizlabs/php_codesniffer": "3.*"
36 },
37 "suggest": {
38 "ext-bcmath": "bcmath or gmp is required for decoding larger integers with the pure PHP decoder",
39 "ext-gmp": "bcmath or gmp is required for decoding larger integers with the pure PHP decoder",
40 "ext-maxminddb": "A C-based database decoder that provides significantly faster lookups"
41 },
42 "type": "library",
43 "autoload": {
44 "psr-4": {
45 "MaxMind\\Db\\": "src/MaxMind/Db"
46 }
47 },
48 "notification-url": "https://packagist.org/downloads/",
49 "license": [
50 "Apache-2.0"
51 ],
52 "authors": [
53 {
54 "name": "Gregory J. Oschwald",
55 "email": "goschwald@maxmind.com",
56 "homepage": "https://www.maxmind.com/"
57 }
58 ],
59 "description": "MaxMind DB Reader API",
60 "homepage": "https://github.com/maxmind/MaxMind-DB-Reader-php",
61 "keywords": [
62 "database",
63 "geoip",
64 "geoip2",
65 "geolocation",
66 "maxmind"
67 ],
68 "support": {
69 "issues": "https://github.com/maxmind/MaxMind-DB-Reader-php/issues",
70 "source": "https://github.com/maxmind/MaxMind-DB-Reader-php/tree/v1.11.0"
71 },
72 "time": "2021-10-18T15:23:10+00:00"
73 }
74 ],
75 "packages-dev": [],
76 "aliases": [],
77 "minimum-stability": "stable",
78 "stability-flags": [],
79 "prefer-stable": false,
80 "prefer-lowest": false,
81 "platform": [],
82 "platform-dev": [],
83 "plugin-api-version": "2.3.0"
84}
ifconfig.php +39 −17
@@ -1,28 +1,50 @@
1<?php 1<?php
2 2
3require 'vendor/autoload.php';
4use MaxMind\Db\Reader;
5
6function loadGeo(string $ipAddress): ?array
7{
8 $databaseFile = 'GeoLite2-City.mmdb';
9 $reader = new Reader($databaseFile);
10 $results = $reader->get($ipAddress);
11 $reader->close();
12
13 return $results;
14}
15
16// Set top-level variables for HTML
3$site_title = 'ip.cleberg.net'; 17$site_title = 'ip.cleberg.net';
4$site_owner_name = 'Christian Cleberg'; 18$site_owner_name = 'Christian Cleberg';
5$site_owner_url = 'https://cleberg.net'; 19$site_owner_url = 'https://cleberg.net';
6$source_repository = 'https://git.sr.ht/~cmc/ifconfig.php'; 20$source_repository = 'https://git.sr.ht/~cmc/ifconfig.php';
7 21
22// Extract geolocation
23$geo = loadGeo($_SERVER['REMOTE_ADDR']);
24
8// Create array of values 25// Create array of values
9$user = array( 26$user = array(
10 'ip' => $_SERVER['REMOTE_ADDR'], 27 'ip' => $_SERVER['REMOTE_ADDR'],
11 'host' => (isset($_SERVER['REMOTE_ADDR']) ? gethostbyaddr($_SERVER['REMOTE_ADDR']) : ""), 28 'host' => (isset($_SERVER['REMOTE_ADDR']) ? gethostbyaddr($_SERVER['REMOTE_ADDR']) : ""),
12 'port' => $_SERVER['REMOTE_PORT'], 29 'port' => $_SERVER['REMOTE_PORT'],
13 'ua' => $_SERVER['HTTP_USER_AGENT'], 30 'ua' => $_SERVER['HTTP_USER_AGENT'],
14 'lang' => $_SERVER['HTTP_ACCEPT_LANGUAGE'], 31 'lang' => $_SERVER['HTTP_ACCEPT_LANGUAGE'],
15 'mime' => $_SERVER['HTTP_ACCEPT'], 32 'mime' => $_SERVER['HTTP_ACCEPT'],
16 'encoding' => $_SERVER['HTTP_ACCEPT_ENCODING'], 33 'encoding' => $_SERVER['HTTP_ACCEPT_ENCODING'],
17 'charset' => $_SERVER['HTTP_ACCEPT_CHARSET'], 34 'charset' => $_SERVER['HTTP_ACCEPT_CHARSET'],
18 'connection' => $_SERVER['HTTP_CONNECTION'], 35 'connection' => $_SERVER['HTTP_CONNECTION'],
19 'cache' => $_SERVER['HTTP_CACHE_CONTROL'], 36 'cache' => $_SERVER['HTTP_CACHE_CONTROL'],
20 'cookie' => $_SERVER['HTTP_COOKIE'], 37 'cookie' => $_SERVER['HTTP_COOKIE'],
21 'referer' => $_SERVER['HTTP_REFERER'], 38 'referer' => $_SERVER['HTTP_REFERER'],
22 'real_ip' => $_SERVER['HTTP_X_REAL_IP'], 39 'real_ip' => $_SERVER['HTTP_X_REAL_IP'],
23 'fwd_ip' => $_SERVER['HTTP_X_FORWARDED_FOR'], 40 'fwd_ip' => $_SERVER['HTTP_X_FORWARDED_FOR'],
24 'fwd_host' => (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? gethostbyaddr($_SERVER['HTTP_X_FORWARDED_FOR']) : ""), 41 'fwd_host' => (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? gethostbyaddr($_SERVER['HTTP_X_FORWARDED_FOR']) : ""),
25 'dnt' => $_SERVER['HTTP_DNT'] 42 'dnt' => $_SERVER['HTTP_DNT'],
43 'continent_code' => $geo['continent']['code'],
44 'continent_name' => $geo['continent']['names']['en'],
45 'country_code' => $geo['country']['geoname_id'],
46 'country_iso' => $geo['country']['iso_code'],
47 'country_name' => $geo['country']['names']['en']
26 ); 48 );
27 49
28// Check request (ex. ifconfig.php?q=ip) 50// Check request (ex. ifconfig.php?q=ip)
@@ -104,7 +126,7 @@ elseif (isset($query) && (($query=="text") || ($query=="all"))) {
104 $date = date("Y"); 126 $date = date("Y");
105 echo <<<EOD 127 echo <<<EOD
106 <br> 128 <br>
107 <p><small>Copyright &copy; {$date} <a href="{$site_owner_url}">{$site_owner_name}</a>. 129 <p><small>Copyright &copy; {$date} <a href="{$site_owner_url}">{$site_owner_name}</a>
108 <br><a target="_blank" rel="noreferrer" href="{$source_repository}">Source Code</a></small></p> 130 <br><a target="_blank" rel="noreferrer" href="{$source_repository}">Source Code</a></small></p>
109 </body> 131 </body>
110 </html> 132 </html>