diff --git a/src/library/FOSSBilling/Config.php b/src/library/FOSSBilling/Config.php index 3b7049fc7..5423c4c09 100644 --- a/src/library/FOSSBilling/Config.php +++ b/src/library/FOSSBilling/Config.php @@ -22,6 +22,7 @@ class Config public static function getConfig(): array { + // @phpstan-ignore include.fileNotFound (False positive) return include PATH_CONFIG; } diff --git a/src/library/FOSSBilling/GeoIP/Databases/CC0-Country.mmdb b/src/library/FOSSBilling/GeoIP/Databases/CC0-Country.mmdb index bddcba4c6..1d9235b55 100644 Binary files a/src/library/FOSSBilling/GeoIP/Databases/CC0-Country.mmdb and b/src/library/FOSSBilling/GeoIP/Databases/CC0-Country.mmdb differ diff --git a/src/library/FOSSBilling/GeoIP/Databases/PDDL-ASN.mmdb b/src/library/FOSSBilling/GeoIP/Databases/PDDL-ASN.mmdb index 419084e44..b341138f3 100644 Binary files a/src/library/FOSSBilling/GeoIP/Databases/PDDL-ASN.mmdb and b/src/library/FOSSBilling/GeoIP/Databases/PDDL-ASN.mmdb differ diff --git a/src/library/FOSSBilling/GeoIP/Reader.php b/src/library/FOSSBilling/GeoIP/Reader.php index 8d0d134a2..e2ac27c80 100644 --- a/src/library/FOSSBilling/GeoIP/Reader.php +++ b/src/library/FOSSBilling/GeoIP/Reader.php @@ -17,6 +17,13 @@ use FOSSBilling\StandardsHelper; use MaxMind\Db\Reader as MaxMindReader; use MaxMind\Db\Reader\InvalidDatabaseException; use PrinsFrank\Standards\Language\LanguageAlpha2; +use Symfony\Component\Filesystem\Exception\IOException; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\HttpClient\HttpClient; +use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; class Reader { @@ -39,6 +46,34 @@ class Reader return PATH_LIBRARY . '/FOSSBilling/GeoIP/Databases/PDDL-ASN.mmdb'; } + /** + * Handles updating the built-in, default databases. + * The databases will only be updated if the files do not exist, or if they are over 7 days old. + * This will only update 1 database per call as it's intended to be run in the background and have the work spread out VS all at once. + * + * @throws IOException + */ + public static function updateDefaultDatabases(): bool + { + $databases = [ + self::getCountryDatabase() => 'https://github.com/HostByBelle/IP-Geolocation-DB/releases/latest/download/cc0-both-country.mmdb', + self::getAsnDatabase() => 'https://github.com/HostByBelle/IP-Geolocation-DB/releases/latest/download/pddl-asn-both.mmdb', + ]; + + foreach ($databases as $path => $url) { + if (self::shouldUpdate($path)) { + try { + self::downloadDb($path, $url); + return true; + } catch (\Exception $e) { + error_log('There was an error while updating the IP address database: ' . $e->getMessage()); + } + } + } + + return false; + } + /** * Constructs a new GeoIP reader instance. * @@ -111,4 +146,45 @@ class Reader return new ASN($record); } + + /** + * Checks if a database should be updated. + * + * @param string $path The path to the database + * @param int $maxAge The maximum age in seconds. The default is 7 days. + * + * @throws IOException + */ + private static function shouldUpdate(string $path, int $maxAge = 604800): bool + { + $fs = new Filesystem(); + if (!$fs->exists($path)) { + return true; + } + + $dbAge = time() - filemtime($path); + + return $dbAge >= $maxAge; + } + + /** + * Downloads a database file and saves it to the provided location. + * + * @return void + * + * @throws TransportExceptionInterface + * @throws RedirectionExceptionInterface + * @throws ClientExceptionInterface + * @throws ServerExceptionInterface + */ + private static function downloadDb(string $path, string $url) + { + $httpClient = HttpClient::create(); + $response = $httpClient->request('GET', $url); + if ($response->getStatusCode() === 200) { + file_put_contents($path, $response->getContent()); + } else { + throw new \Exception('Got a ' . $response->getStatusCode() . ' status code when attempting to download ' . $url); + } + } } diff --git a/src/modules/System/Service.php b/src/modules/System/Service.php index aca914613..f4bebf694 100644 --- a/src/modules/System/Service.php +++ b/src/modules/System/Service.php @@ -13,6 +13,7 @@ namespace Box\Mod\System; use FOSSBilling\Config; use FOSSBilling\Environment; +use FOSSBilling\GeoIP\Reader; use FOSSBilling\SentryHelper; use FOSSBilling\Version; use Pimple\Container; @@ -1004,6 +1005,7 @@ class Service public static function onBeforeAdminCronRun(\Box_Event $event) { $di = $event->getDi(); + Reader::updateDefaultDatabases(); try { // Prune the classmap to remove classes which are no longer on the disk or that have moved.