From 35d5ab65b0bee60706ddd49e356364de0b27d61e Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Fri, 31 May 2024 21:15:07 +0200 Subject: [PATCH 1/3] Try curl when allow_url_fopen is disabled --- src/Services/MaxMindDatabase.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Services/MaxMindDatabase.php b/src/Services/MaxMindDatabase.php index 5d7e2a4..e324c15 100644 --- a/src/Services/MaxMindDatabase.php +++ b/src/Services/MaxMindDatabase.php @@ -73,7 +73,7 @@ public function update() $this->withTemporaryDirectory(function ($directory) { $tarFile = sprintf('%s/maxmind.tar.gz', $directory); - file_put_contents($tarFile, fopen($this->config('update_url'), 'r')); + $this->downloadFileByUrl($tarFile, $this->config('update_url')); $archive = new PharData($tarFile); @@ -166,4 +166,26 @@ protected function deleteDirectory(string $directory) return rmdir($directory); } + + protected function downloadFileByUrl(string $filename, string $url): void + { + $canUseFopenForUrl = in_array(strtolower((string) ini_get('allow_url_fopen')), ['1', 'on'], true); + if ($canUseFopenForUrl) { + file_put_contents($filename, fopen($url, 'rb')); + } elseif (extension_loaded('curl')) { + $fp = fopen($filename, 'wb+'); + if ($fp === false) { + throw new \RuntimeException("Cannot open $filename file for writing."); + } + $ch = curl_init(); + curl_setopt($ch, \CURLOPT_URL, $url); + curl_setopt($ch, \CURLOPT_FILE, $fp); + curl_setopt($ch, \CURLOPT_FOLLOWLOCATION, true); + curl_exec($ch); + curl_close($ch); + fclose($fp); + } else { + throw new \RuntimeException('Cannot download the file. Please enable allow_url_fopen or install curl extension.'); + } + } } From de4dec1915fbc79cdcc598e8468d97064ed25c72 Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Fri, 31 May 2024 21:19:58 +0200 Subject: [PATCH 2/3] Update Psalm baseline --- psalm-baseline.xml | 191 ++++++++++++++++++++++----------------------- 1 file changed, 94 insertions(+), 97 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 3681407..ebd5b0f 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,11 +1,11 @@ - + - $value + - bool + cache->flush()]]> @@ -18,22 +18,19 @@ output->error('Default cache system does not support tags')]]> - - void - - config - flush - getCache + + + - $description + - Clear - Clear - Clear - Clear + + + + @@ -41,24 +38,24 @@ - $result - $service - $service + + + - $result + - getService + - $description + - Update - Update - Update - Update + + + + @@ -78,29 +75,29 @@ cache]]> - \InteractionDesignFoundation\GeoIP\Cache + service]]> - $config + config('cache_expires', 30)]]> config('cache_tags')]]> config('default_location', [])]]> - $class - $config - $log + + + - string + - error - new $class($config) - pushHandler + + + config('service')]]> @@ -109,40 +106,40 @@ currencies, $iso)]]> - \InteractionDesignFoundation\GeoIP\Contracts\ServiceInterface + - $ip - $ip - $ip + + + - $ip + iso_code]]> - null - null - null + + + - $service + - new $class($config) + - Logger - Logger - StreamHandler + + + - $app + config->get('geoip', [])]]> @@ -152,7 +149,7 @@ - get + config]]> @@ -163,43 +160,43 @@ - $key - $value + + - ArrayAccess + - $key + - $value + - bool + - is_null($value) ? false : $value - is_null($value) ? false : $value + + - $data[0] - $data[0] + + countryCode]]> - $path + config('continent_path')]]> config('continent_path')]]> - $json - $path + + continents]]> - string + message]]> @@ -221,10 +218,10 @@ continents, $code, 'Unknown')]]> - $output + - $continents + client->getErrors() !== null]]> @@ -233,10 +230,10 @@ - $data[0] - $json - $json - $json + + + + @@ -249,7 +246,7 @@ - $json + client->getErrors() !== null]]> @@ -257,11 +254,11 @@ - $data[0] - $json + + - $json + client->getErrors() !== null]]> @@ -269,11 +266,11 @@ - $data[0] - $json + + - $json + config('key')]]> @@ -284,32 +281,32 @@ - $file + - $directory + - $directory - $directory - $path - $path - $path - $path + + + + + + config('database_path')]]> config('locales', ['en'])]]> config('update_url')]]> - $file - $path + + - getFilename + - getPathName - isDir + + @@ -324,34 +321,34 @@ errors)]]> - $response - $response + + - $header_size - $header_size + + config, 'headers', [])]]> config, 'query', [])]]> config, 'query', [])]]> - $header_size + http_code]]> config, 'base_uri')]]> - null - null + + - \InteractionDesignFoundation\GeoIP\GeoIP|\InteractionDesignFoundation\GeoIP\Location + - getLocation + From f6534177e43f118db816fb5edb9c16744de201a0 Mon Sep 17 00:00:00 2001 From: alies-dev Date: Fri, 31 May 2024 19:21:25 +0000 Subject: [PATCH 3/3] Fix coding style --- src/Services/MaxMindDatabase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Services/MaxMindDatabase.php b/src/Services/MaxMindDatabase.php index f5f00f0..40f65c0 100644 --- a/src/Services/MaxMindDatabase.php +++ b/src/Services/MaxMindDatabase.php @@ -182,7 +182,7 @@ protected function downloadFileByUrl(string $filename, string $url): void } elseif (extension_loaded('curl')) { $fp = fopen($filename, 'wb+'); if ($fp === false) { - throw new \RuntimeException("Cannot open $filename file for writing."); + throw new \RuntimeException("Cannot open {$filename} file for writing."); } $ch = curl_init(); curl_setopt($ch, \CURLOPT_URL, $url);