Skip to content

Use CURL when allow_url_fopen is disabled #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 72 additions & 81 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.23.1@8471a896ccea3526b26d082f4461eeea467f10a4">
<files psalm-version="5.24.0@462c80e31c34e58cc4f750c656be3927e80e550e">
<file src="src/Cache.php">
<MixedAssignment>
<code><![CDATA[$value]]></code>
</MixedAssignment>
<MixedInferredReturnType>
<code><![CDATA[bool]]></code>
</MixedInferredReturnType>
<MixedReturnStatement>
<code><![CDATA[$this->cache->flush()]]></code>
</MixedReturnStatement>
<PossiblyInvalidPropertyAssignmentValue>
<code><![CDATA[($tags === [] || !$cache->supportsTags()) ? $cache : $cache->tags($tags)]]></code>
<code><![CDATA[$tags ? $cache->tags($tags) : $cache]]></code>
</PossiblyInvalidPropertyAssignmentValue>
</file>
<file src="src/Console/Clear.php">
<DeprecatedMethod>
<code><![CDATA[fire]]></code>
</DeprecatedMethod>
<InvalidReturnStatement>
<code><![CDATA[$this->output->error('Default cache system does not support tags')]]></code>
</InvalidReturnStatement>
Expand All @@ -26,21 +26,17 @@
<NonInvariantDocblockPropertyType>
<code><![CDATA[$description]]></code>
</NonInvariantDocblockPropertyType>
<PossiblyUndefinedMethod>
<code><![CDATA[config]]></code>
<code><![CDATA[getCache]]></code>
</PossiblyUndefinedMethod>
<PropertyNotSetInConstructor>
<code><![CDATA[Clear]]></code>
<code><![CDATA[Clear]]></code>
<code><![CDATA[Clear]]></code>
<code><![CDATA[Clear]]></code>
</PropertyNotSetInConstructor>
<UndefinedFunction>
<code><![CDATA[config('cache.default')]]></code>
</UndefinedFunction>
</file>
<file src="src/Console/Update.php">
<DeprecatedMethod>
<code><![CDATA[fire]]></code>
</DeprecatedMethod>
<MixedArgument>
<code><![CDATA[$result]]></code>
<code><![CDATA[$service]]></code>
Expand All @@ -55,38 +51,20 @@
<NonInvariantDocblockPropertyType>
<code><![CDATA[$description]]></code>
</NonInvariantDocblockPropertyType>
<PossiblyUndefinedMethod>
<code><![CDATA[getService]]></code>
</PossiblyUndefinedMethod>
<PropertyNotSetInConstructor>
<code><![CDATA[Update]]></code>
<code><![CDATA[Update]]></code>
<code><![CDATA[Update]]></code>
<code><![CDATA[Update]]></code>
</PropertyNotSetInConstructor>
</file>
<file src="src/Contracts/ServiceInterface.php">
<InvalidParamDefault>
<code><![CDATA[LocationArray]]></code>
<code><![CDATA[LocationArray]]></code>
</InvalidParamDefault>
</file>
<file src="src/GeoIP.php">
<DeprecatedConstant>
<code><![CDATA[Logger::ERROR]]></code>
</DeprecatedConstant>
<DeprecatedMethod>
<code><![CDATA[setPrefix]]></code>
</DeprecatedMethod>
<DeprecatedProperty>
<code><![CDATA[$this->remote_ip]]></code>
<code><![CDATA[$this->remote_ip]]></code>
</DeprecatedProperty>
<DocblockTypeContradiction>
<code><![CDATA[$this->currencies === null]]></code>
<code><![CDATA[$this->service === null]]></code>
</DocblockTypeContradiction>
<InvalidPropertyAssignmentValue>
<code><![CDATA[$this->default_location['ip'] = $this->getClientIP()]]></code>
<code><![CDATA[new Cache(
$cache,
$this->config('cache_tags'),
Expand All @@ -108,18 +86,18 @@
<code><![CDATA[$this->config('cache_tags')]]></code>
<code><![CDATA[$this->config('default_location', [])]]></code>
</MixedArgument>
<MixedArgumentTypeCoercion>
<code><![CDATA[$this->default_location]]></code>
</MixedArgumentTypeCoercion>
<MixedAssignment>
<code><![CDATA[$class]]></code>
<code><![CDATA[$config]]></code>
<code><![CDATA[$log]]></code>
</MixedAssignment>
<MixedInferredReturnType>
<code><![CDATA[string]]></code>
</MixedInferredReturnType>
<MixedMethodCall>
<code><![CDATA[error]]></code>
<code><![CDATA[new $class($config)]]></code>
<code><![CDATA[pushHandler]]></code>
</MixedMethodCall>
<MixedOperand>
<code><![CDATA[$this->config('service')]]></code>
Expand All @@ -130,25 +108,34 @@
<MoreSpecificReturnType>
<code><![CDATA[\InteractionDesignFoundation\GeoIP\Contracts\ServiceInterface]]></code>
</MoreSpecificReturnType>
<PossiblyInvalidArgument>
<code><![CDATA[$ip]]></code>
<code><![CDATA[$ip]]></code>
<code><![CDATA[$ip]]></code>
</PossiblyInvalidArgument>
<PossiblyNullArgument>
<code><![CDATA[$ip]]></code>
<code><![CDATA[$location->iso_code]]></code>
</PossiblyNullArgument>
<PossiblyNullPropertyAssignmentValue>
<code><![CDATA[null]]></code>
<code><![CDATA[null]]></code>
<code><![CDATA[null]]></code>
</PossiblyNullPropertyAssignmentValue>
<PropertyNotSetInConstructor>
<code><![CDATA[$service]]></code>
</PropertyNotSetInConstructor>
<PropertyTypeCoercion>
<code><![CDATA[new $class($config)]]></code>
</PropertyTypeCoercion>
<RiskyTruthyFalsyComparison>
<code><![CDATA[! $location->currency]]></code>
<code><![CDATA[$address = getenv($key)]]></code>
<code><![CDATA[$ip]]></code>
</RiskyTruthyFalsyComparison>
<RedundantCondition>
<code><![CDATA['some']]></code>
</RedundantCondition>
<UndefinedClass>
<code><![CDATA[Logger]]></code>
<code><![CDATA[Logger]]></code>
<code><![CDATA[StreamHandler]]></code>
</UndefinedClass>
</file>
<file src="src/GeoIPServiceProvider.php">
<MissingClosureParamType>
Expand All @@ -167,11 +154,11 @@
<MixedPropertyFetch>
<code><![CDATA[$app->config]]></code>
</MixedPropertyFetch>
<UndefinedFunction>
<code><![CDATA[config_path('geoip.php')]]></code>
</UndefinedFunction>
</file>
<file src="src/Location.php">
<InvalidParamDefault>
<code><![CDATA[LocationArray]]></code>
</InvalidParamDefault>
<MissingParamType>
<code><![CDATA[$key]]></code>
<code><![CDATA[$value]]></code>
Expand Down Expand Up @@ -202,31 +189,30 @@
<code><![CDATA[$this->config('continent_path')]]></code>
<code><![CDATA[$this->config('continent_path')]]></code>
</MixedArgument>
<MixedArgumentTypeCoercion>
<code><![CDATA[[
'ip' => $ip,
'iso_code' => $json->countryCode,
'country' => $json->country,
'city' => $json->city,
'state' => $json->region,
'state_name' => $json->regionName,
'postal_code' => $json->zip,
'lat' => $json->lat,
'lon' => $json->lon,
'timezone' => $json->timezone,
'continent' => $this->getContinent($json->countryCode),
]]]></code>
</MixedArgumentTypeCoercion>
<MixedAssignment>
<code><![CDATA[$base['query']['key']]]></code>
<code><![CDATA[$json]]></code>
<code><![CDATA[$path]]></code>
<code><![CDATA[$this->continents]]></code>
</MixedAssignment>
<MixedInferredReturnType>
<code><![CDATA[string]]></code>
</MixedInferredReturnType>
<MixedOperand>
<code><![CDATA[$json->message]]></code>
</MixedOperand>
<MixedPropertyFetch>
<code><![CDATA[$json->city]]></code>
<code><![CDATA[$json->country]]></code>
<code><![CDATA[$json->countryCode]]></code>
<code><![CDATA[$json->lat]]></code>
<code><![CDATA[$json->lon]]></code>
<code><![CDATA[$json->message]]></code>
<code><![CDATA[$json->region]]></code>
<code><![CDATA[$json->regionName]]></code>
<code><![CDATA[$json->status]]></code>
<code><![CDATA[$json->timezone]]></code>
<code><![CDATA[$json->zip]]></code>
</MixedPropertyFetch>
<MixedReturnStatement>
<code><![CDATA[Arr::get($this->continents, $code, 'Unknown')]]></code>
Expand All @@ -249,22 +235,6 @@
<code><![CDATA[$json]]></code>
<code><![CDATA[$json]]></code>
</MixedArgument>
<MixedArgumentTypeCoercion>
<code><![CDATA[[
'ip' => $ip,
'iso_code' => $json['country_code'],
'country' => $json['country_name'],
'city' => $json['city'],
'state' => $json['region_code'],
'state_name' => $json['region'],
'postal_code' => $json['postal'],
'lat' => $json['latitude'],
'lon' => $json['longitude'],
'timezone' => Arr::get($json, 'time_zone.name'),
'continent' => Arr::get($json, 'continent_code'),
'currency' => Arr::get($json, 'currency.code'),
]]]></code>
</MixedArgumentTypeCoercion>
<MixedArrayAccess>
<code><![CDATA[$json['city']]]></code>
<code><![CDATA[$json['country_code']]]></code>
Expand Down Expand Up @@ -310,11 +280,34 @@
</RedundantConditionGivenDocblockType>
</file>
<file src="src/Services/MaxMindDatabase.php">
<ImplicitToStringCast>
<code><![CDATA[$file]]></code>
</ImplicitToStringCast>
<MissingClosureParamType>
<code><![CDATA[$directory]]></code>
</MissingClosureParamType>
<MixedArgument>
<code><![CDATA[$directory]]></code>
<code><![CDATA[$directory]]></code>
<code><![CDATA[$path]]></code>
<code><![CDATA[$path]]></code>
<code><![CDATA[$path]]></code>
<code><![CDATA[$path]]></code>
<code><![CDATA[$this->config('database_path')]]></code>
<code><![CDATA[$this->config('locales', ['en'])]]></code>
<code><![CDATA[$this->config('update_url')]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$file]]></code>
<code><![CDATA[$path]]></code>
</MixedAssignment>
<MixedMethodCall>
<code><![CDATA[getFilename]]></code>
</MixedMethodCall>
<PossiblyInvalidMethodCall>
<code><![CDATA[getPathName]]></code>
<code><![CDATA[isDir]]></code>
</PossiblyInvalidMethodCall>
</file>
<file src="src/Services/MaxMindWebService.php">
<MixedArgument>
Expand All @@ -327,7 +320,12 @@
<DocblockTypeContradiction>
<code><![CDATA[is_null($this->errors)]]></code>
</DocblockTypeContradiction>
<InvalidScalarArgument>
<code><![CDATA[$response]]></code>
<code><![CDATA[$response]]></code>
</InvalidScalarArgument>
<MixedArgument>
<code><![CDATA[$header_size]]></code>
<code><![CDATA[$header_size]]></code>
<code><![CDATA[Arr::get($this->config, 'headers', [])]]></code>
<code><![CDATA[Arr::get($this->config, 'query', [])]]></code>
Expand All @@ -344,24 +342,17 @@
<code><![CDATA[null]]></code>
<code><![CDATA[null]]></code>
</PossiblyNullPropertyAssignmentValue>
<RiskyTruthyFalsyComparison>
<code><![CDATA[strpos($url, '?')]]></code>
</RiskyTruthyFalsyComparison>
</file>
<file src="src/helpers.php">
<MixedInferredReturnType>
<code><![CDATA[($ip is null ? \InteractionDesignFoundation\GeoIP\GeoIP : \InteractionDesignFoundation\GeoIP\Location)]]></code>
<code><![CDATA[\InteractionDesignFoundation\GeoIP\GeoIP|\InteractionDesignFoundation\GeoIP\Location]]></code>
</MixedInferredReturnType>
<MixedMethodCall>
<code><![CDATA[getLocation]]></code>
</MixedMethodCall>
<MixedReturnStatement>
<code><![CDATA[app('geoip')]]></code>
<code><![CDATA[app('geoip')]]></code>
<code><![CDATA[app('geoip')->getLocation($ip)]]></code>
</MixedReturnStatement>
<PossiblyUndefinedMethod>
<code><![CDATA[getLocation]]></code>
</PossiblyUndefinedMethod>
</file>
</files>
24 changes: 23 additions & 1 deletion src/Services/MaxMindDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function update()
$this->withTemporaryDirectory(function ($directory): void {
$tarFile = sprintf('%s/maxmind.tar.gz', $directory);

file_put_contents($tarFile, fopen($this->config('update_url'), 'rb'));
$this->downloadFileByUrl($tarFile, $this->config('update_url'));

$archive = new \PharData($tarFile);

Expand Down Expand Up @@ -173,4 +173,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.');
}
}
}