From 433b4249e0853e3d8c3942c7f9da2cdb1ea3734b Mon Sep 17 00:00:00 2001 From: flashwave Date: Mon, 12 Mar 2018 21:48:09 +0100 Subject: [PATCH] Use GeoLite2 instead of the old php extension, fixes IPv6 lookups. --- composer.json | 4 +- composer.lock | 213 +++++++++++++++++++++++++++++++++++++++++++++++++- utility.php | 27 +++++-- 3 files changed, 235 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 730ac8ae..256d9f51 100644 --- a/composer.json +++ b/composer.json @@ -8,6 +8,7 @@ }, "require": { "php": ">=7.2", + "ext-bcmath": "*", "flashwave/aitemu": "dev-master#7997543717d996b56938d5050ea497d62eb71f2e", "twig/twig": "~2.4", "nesbot/carbon": "~1.22", @@ -16,7 +17,8 @@ "illuminate/pagination": "~5.5", "doctrine/dbal": "~2.6", "swiftmailer/swiftmailer": "~6.0", - "erusev/parsedown": "~1.6" + "erusev/parsedown": "~1.6", + "geoip2/geoip2": "~2.0" }, "require-dev": { "phpunit/phpunit": "~6.0" diff --git a/composer.lock b/composer.lock index 146be931..2d45e2e7 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,64 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "cf7183af01ce52a9430cef6cfcbc9213", + "content-hash": "0ea708fe797f9a4fdc6e049500fec2c3", "packages": [ + { + "name": "composer/ca-bundle", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/composer/ca-bundle.git", + "reference": "943b2c4fcad1ef178d16a713c2468bf7e579c288" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/composer/ca-bundle/zipball/943b2c4fcad1ef178d16a713c2468bf7e579c288", + "reference": "943b2c4fcad1ef178d16a713c2468bf7e579c288", + "shasum": "" + }, + "require": { + "ext-openssl": "*", + "ext-pcre": "*", + "php": "^5.3.2 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.35", + "psr/log": "^1.0", + "symfony/process": "^2.5 || ^3.0 || ^4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Composer\\CaBundle\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jordi Boggiano", + "email": "j.boggiano@seld.be", + "homepage": "http://seld.be" + } + ], + "description": "Lets you find a path to the system CA bundle, and includes a fallback to the Mozilla CA bundle.", + "keywords": [ + "cabundle", + "cacert", + "certificate", + "ssl", + "tls" + ], + "time": "2017-11-29T09:37:33+00:00" + }, { "name": "doctrine/annotations", "version": "v1.6.0", @@ -618,6 +674,59 @@ "homepage": "https://github.com/flashwave/aitemu", "time": "2018-01-03T15:36:23+00:00" }, + { + "name": "geoip2/geoip2", + "version": "v2.8.0", + "source": { + "type": "git", + "url": "https://github.com/maxmind/GeoIP2-php.git", + "reference": "63b0d87d47ee8c9431bff70244401db5ced82bd9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/maxmind/GeoIP2-php/zipball/63b0d87d47ee8c9431bff70244401db5ced82bd9", + "reference": "63b0d87d47ee8c9431bff70244401db5ced82bd9", + "shasum": "" + }, + "require": { + "maxmind-db/reader": "~1.0", + "maxmind/web-service-common": "~0.4", + "php": ">=5.4" + }, + "require-dev": { + "apigen/apigen": "*", + "friendsofphp/php-cs-fixer": "2.*", + "phpunit/phpunit": "4.*", + "squizlabs/php_codesniffer": "3.*" + }, + "type": "library", + "autoload": { + "psr-4": { + "GeoIp2\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Gregory J. Oschwald", + "email": "goschwald@maxmind.com", + "homepage": "http://www.maxmind.com/" + } + ], + "description": "MaxMind GeoIP2 PHP API", + "homepage": "https://github.com/maxmind/GeoIP2-php", + "keywords": [ + "IP", + "geoip", + "geoip2", + "geolocation", + "maxmind" + ], + "time": "2018-01-18T21:30:24+00:00" + }, { "name": "illuminate/container", "version": "v5.5.28", @@ -916,6 +1025,108 @@ "homepage": "https://laravel.com", "time": "2017-12-24T20:02:59+00:00" }, + { + "name": "maxmind-db/reader", + "version": "v1.3.0", + "source": { + "type": "git", + "url": "https://github.com/maxmind/MaxMind-DB-Reader-php.git", + "reference": "e042b4f8a2dff41e19019faf16427178b07fbd58" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/maxmind/MaxMind-DB-Reader-php/zipball/e042b4f8a2dff41e19019faf16427178b07fbd58", + "reference": "e042b4f8a2dff41e19019faf16427178b07fbd58", + "shasum": "" + }, + "require": { + "php": ">=5.4" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "2.*", + "phpunit/phpunit": "4.* || 5.*", + "satooshi/php-coveralls": "1.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": "http://www.maxmind.com/" + } + ], + "description": "MaxMind DB Reader API", + "homepage": "https://github.com/maxmind/MaxMind-DB-Reader-php", + "keywords": [ + "database", + "geoip", + "geoip2", + "geolocation", + "maxmind" + ], + "time": "2018-02-21T21:23:33+00:00" + }, + { + "name": "maxmind/web-service-common", + "version": "v0.5.0", + "source": { + "type": "git", + "url": "https://github.com/maxmind/web-service-common-php.git", + "reference": "61a9836fa3bb1743ab89752bae5005d71e78c73b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/maxmind/web-service-common-php/zipball/61a9836fa3bb1743ab89752bae5005d71e78c73b", + "reference": "61a9836fa3bb1743ab89752bae5005d71e78c73b", + "shasum": "" + }, + "require": { + "composer/ca-bundle": "^1.0.3", + "ext-curl": "*", + "ext-json": "*", + "php": ">=5.4" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "2.*", + "phpunit/phpunit": "4.*", + "squizlabs/php_codesniffer": "3.*" + }, + "type": "library", + "autoload": { + "psr-4": { + "MaxMind\\Exception\\": "src/Exception", + "MaxMind\\WebService\\": "src/WebService" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Gregory Oschwald", + "email": "goschwald@maxmind.com" + } + ], + "description": "Internal MaxMind Web Service API", + "homepage": "https://github.com/maxmind/web-service-common-php", + "time": "2018-02-12T22:31:54+00:00" + }, { "name": "nesbot/carbon", "version": "1.22.1", diff --git a/utility.php b/utility.php index 33a3241e..c435808d 100644 --- a/utility.php +++ b/utility.php @@ -62,17 +62,30 @@ function byte_symbol($bytes, $decimal = false) return sprintf("%.2f %s%sB", $bytes, $symbol, $symbol !== '' && !$decimal ? 'i' : ''); } +// this should be rewritten to only load the database once per Application instance. +// for now this will do since the only time this function is called is once during registration. +// also make sure an instance of Application with config exists before calling this! function get_country_code(string $ipAddr, string $fallback = 'XX'): string { - if (function_exists("geoip_country_code_by_name")) { - try { - $code = geoip_country_code_by_name($ipAddr); + try { + $app = \Misuzu\Application::getInstance(); - if ($code) { - return $code; - } - } catch (\Exception $e) { + if (!$app->hasModule('config')) { + return $fallback; } + + $database_path = $app->config->get('GeoIP', 'database_path'); + + if ($database_path === null) { + return $fallback; + } + + $geoip = new \GeoIp2\Database\Reader($database_path); + $record = $geoip->country($ipAddr); + + return $record->country->isoCode; + } catch (\Exception $e) { + // report error? } return $fallback;