Skip to content

Commit a1df8ca

Browse files
authored
Merge pull request #35 from InteractionDesignFoundation/curl-instead-allow_url_fopen
Use CURL when `allow_url_fopen` is disabled
2 parents c4b73d3 + f653417 commit a1df8ca

File tree

2 files changed

+95
-82
lines changed

2 files changed

+95
-82
lines changed

psalm-baseline.xml

Lines changed: 72 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="5.23.1@8471a896ccea3526b26d082f4461eeea467f10a4">
2+
<files psalm-version="5.24.0@462c80e31c34e58cc4f750c656be3927e80e550e">
33
<file src="src/Cache.php">
4+
<MixedAssignment>
5+
<code><![CDATA[$value]]></code>
6+
</MixedAssignment>
47
<MixedInferredReturnType>
58
<code><![CDATA[bool]]></code>
69
</MixedInferredReturnType>
710
<MixedReturnStatement>
811
<code><![CDATA[$this->cache->flush()]]></code>
912
</MixedReturnStatement>
1013
<PossiblyInvalidPropertyAssignmentValue>
11-
<code><![CDATA[($tags === [] || !$cache->supportsTags()) ? $cache : $cache->tags($tags)]]></code>
14+
<code><![CDATA[$tags ? $cache->tags($tags) : $cache]]></code>
1215
</PossiblyInvalidPropertyAssignmentValue>
1316
</file>
1417
<file src="src/Console/Clear.php">
15-
<DeprecatedMethod>
16-
<code><![CDATA[fire]]></code>
17-
</DeprecatedMethod>
1818
<InvalidReturnStatement>
1919
<code><![CDATA[$this->output->error('Default cache system does not support tags')]]></code>
2020
</InvalidReturnStatement>
@@ -26,21 +26,17 @@
2626
<NonInvariantDocblockPropertyType>
2727
<code><![CDATA[$description]]></code>
2828
</NonInvariantDocblockPropertyType>
29-
<PossiblyUndefinedMethod>
30-
<code><![CDATA[config]]></code>
31-
<code><![CDATA[getCache]]></code>
32-
</PossiblyUndefinedMethod>
3329
<PropertyNotSetInConstructor>
3430
<code><![CDATA[Clear]]></code>
3531
<code><![CDATA[Clear]]></code>
3632
<code><![CDATA[Clear]]></code>
3733
<code><![CDATA[Clear]]></code>
3834
</PropertyNotSetInConstructor>
35+
<UndefinedFunction>
36+
<code><![CDATA[config('cache.default')]]></code>
37+
</UndefinedFunction>
3938
</file>
4039
<file src="src/Console/Update.php">
41-
<DeprecatedMethod>
42-
<code><![CDATA[fire]]></code>
43-
</DeprecatedMethod>
4440
<MixedArgument>
4541
<code><![CDATA[$result]]></code>
4642
<code><![CDATA[$service]]></code>
@@ -55,38 +51,20 @@
5551
<NonInvariantDocblockPropertyType>
5652
<code><![CDATA[$description]]></code>
5753
</NonInvariantDocblockPropertyType>
58-
<PossiblyUndefinedMethod>
59-
<code><![CDATA[getService]]></code>
60-
</PossiblyUndefinedMethod>
6154
<PropertyNotSetInConstructor>
6255
<code><![CDATA[Update]]></code>
6356
<code><![CDATA[Update]]></code>
6457
<code><![CDATA[Update]]></code>
6558
<code><![CDATA[Update]]></code>
6659
</PropertyNotSetInConstructor>
6760
</file>
68-
<file src="src/Contracts/ServiceInterface.php">
69-
<InvalidParamDefault>
70-
<code><![CDATA[LocationArray]]></code>
71-
<code><![CDATA[LocationArray]]></code>
72-
</InvalidParamDefault>
73-
</file>
7461
<file src="src/GeoIP.php">
75-
<DeprecatedConstant>
76-
<code><![CDATA[Logger::ERROR]]></code>
77-
</DeprecatedConstant>
78-
<DeprecatedMethod>
79-
<code><![CDATA[setPrefix]]></code>
80-
</DeprecatedMethod>
81-
<DeprecatedProperty>
82-
<code><![CDATA[$this->remote_ip]]></code>
83-
<code><![CDATA[$this->remote_ip]]></code>
84-
</DeprecatedProperty>
8562
<DocblockTypeContradiction>
8663
<code><![CDATA[$this->currencies === null]]></code>
8764
<code><![CDATA[$this->service === null]]></code>
8865
</DocblockTypeContradiction>
8966
<InvalidPropertyAssignmentValue>
67+
<code><![CDATA[$this->default_location['ip'] = $this->getClientIP()]]></code>
9068
<code><![CDATA[new Cache(
9169
$cache,
9270
$this->config('cache_tags'),
@@ -108,18 +86,18 @@
10886
<code><![CDATA[$this->config('cache_tags')]]></code>
10987
<code><![CDATA[$this->config('default_location', [])]]></code>
11088
</MixedArgument>
111-
<MixedArgumentTypeCoercion>
112-
<code><![CDATA[$this->default_location]]></code>
113-
</MixedArgumentTypeCoercion>
11489
<MixedAssignment>
11590
<code><![CDATA[$class]]></code>
11691
<code><![CDATA[$config]]></code>
92+
<code><![CDATA[$log]]></code>
11793
</MixedAssignment>
11894
<MixedInferredReturnType>
11995
<code><![CDATA[string]]></code>
12096
</MixedInferredReturnType>
12197
<MixedMethodCall>
98+
<code><![CDATA[error]]></code>
12299
<code><![CDATA[new $class($config)]]></code>
100+
<code><![CDATA[pushHandler]]></code>
123101
</MixedMethodCall>
124102
<MixedOperand>
125103
<code><![CDATA[$this->config('service')]]></code>
@@ -130,25 +108,34 @@
130108
<MoreSpecificReturnType>
131109
<code><![CDATA[\InteractionDesignFoundation\GeoIP\Contracts\ServiceInterface]]></code>
132110
</MoreSpecificReturnType>
111+
<PossiblyInvalidArgument>
112+
<code><![CDATA[$ip]]></code>
113+
<code><![CDATA[$ip]]></code>
114+
<code><![CDATA[$ip]]></code>
115+
</PossiblyInvalidArgument>
133116
<PossiblyNullArgument>
134117
<code><![CDATA[$ip]]></code>
135118
<code><![CDATA[$location->iso_code]]></code>
136119
</PossiblyNullArgument>
137120
<PossiblyNullPropertyAssignmentValue>
138121
<code><![CDATA[null]]></code>
139122
<code><![CDATA[null]]></code>
123+
<code><![CDATA[null]]></code>
140124
</PossiblyNullPropertyAssignmentValue>
141125
<PropertyNotSetInConstructor>
142126
<code><![CDATA[$service]]></code>
143127
</PropertyNotSetInConstructor>
144128
<PropertyTypeCoercion>
145129
<code><![CDATA[new $class($config)]]></code>
146130
</PropertyTypeCoercion>
147-
<RiskyTruthyFalsyComparison>
148-
<code><![CDATA[! $location->currency]]></code>
149-
<code><![CDATA[$address = getenv($key)]]></code>
150-
<code><![CDATA[$ip]]></code>
151-
</RiskyTruthyFalsyComparison>
131+
<RedundantCondition>
132+
<code><![CDATA['some']]></code>
133+
</RedundantCondition>
134+
<UndefinedClass>
135+
<code><![CDATA[Logger]]></code>
136+
<code><![CDATA[Logger]]></code>
137+
<code><![CDATA[StreamHandler]]></code>
138+
</UndefinedClass>
152139
</file>
153140
<file src="src/GeoIPServiceProvider.php">
154141
<MissingClosureParamType>
@@ -167,11 +154,11 @@
167154
<MixedPropertyFetch>
168155
<code><![CDATA[$app->config]]></code>
169156
</MixedPropertyFetch>
157+
<UndefinedFunction>
158+
<code><![CDATA[config_path('geoip.php')]]></code>
159+
</UndefinedFunction>
170160
</file>
171161
<file src="src/Location.php">
172-
<InvalidParamDefault>
173-
<code><![CDATA[LocationArray]]></code>
174-
</InvalidParamDefault>
175162
<MissingParamType>
176163
<code><![CDATA[$key]]></code>
177164
<code><![CDATA[$value]]></code>
@@ -202,31 +189,30 @@
202189
<code><![CDATA[$this->config('continent_path')]]></code>
203190
<code><![CDATA[$this->config('continent_path')]]></code>
204191
</MixedArgument>
205-
<MixedArgumentTypeCoercion>
206-
<code><![CDATA[[
207-
'ip' => $ip,
208-
'iso_code' => $json->countryCode,
209-
'country' => $json->country,
210-
'city' => $json->city,
211-
'state' => $json->region,
212-
'state_name' => $json->regionName,
213-
'postal_code' => $json->zip,
214-
'lat' => $json->lat,
215-
'lon' => $json->lon,
216-
'timezone' => $json->timezone,
217-
'continent' => $this->getContinent($json->countryCode),
218-
]]]></code>
219-
</MixedArgumentTypeCoercion>
220192
<MixedAssignment>
221193
<code><![CDATA[$base['query']['key']]]></code>
194+
<code><![CDATA[$json]]></code>
222195
<code><![CDATA[$path]]></code>
223196
<code><![CDATA[$this->continents]]></code>
224197
</MixedAssignment>
225198
<MixedInferredReturnType>
226199
<code><![CDATA[string]]></code>
227200
</MixedInferredReturnType>
201+
<MixedOperand>
202+
<code><![CDATA[$json->message]]></code>
203+
</MixedOperand>
228204
<MixedPropertyFetch>
205+
<code><![CDATA[$json->city]]></code>
206+
<code><![CDATA[$json->country]]></code>
207+
<code><![CDATA[$json->countryCode]]></code>
208+
<code><![CDATA[$json->lat]]></code>
209+
<code><![CDATA[$json->lon]]></code>
229210
<code><![CDATA[$json->message]]></code>
211+
<code><![CDATA[$json->region]]></code>
212+
<code><![CDATA[$json->regionName]]></code>
213+
<code><![CDATA[$json->status]]></code>
214+
<code><![CDATA[$json->timezone]]></code>
215+
<code><![CDATA[$json->zip]]></code>
230216
</MixedPropertyFetch>
231217
<MixedReturnStatement>
232218
<code><![CDATA[Arr::get($this->continents, $code, 'Unknown')]]></code>
@@ -249,22 +235,6 @@
249235
<code><![CDATA[$json]]></code>
250236
<code><![CDATA[$json]]></code>
251237
</MixedArgument>
252-
<MixedArgumentTypeCoercion>
253-
<code><![CDATA[[
254-
'ip' => $ip,
255-
'iso_code' => $json['country_code'],
256-
'country' => $json['country_name'],
257-
'city' => $json['city'],
258-
'state' => $json['region_code'],
259-
'state_name' => $json['region'],
260-
'postal_code' => $json['postal'],
261-
'lat' => $json['latitude'],
262-
'lon' => $json['longitude'],
263-
'timezone' => Arr::get($json, 'time_zone.name'),
264-
'continent' => Arr::get($json, 'continent_code'),
265-
'currency' => Arr::get($json, 'currency.code'),
266-
]]]></code>
267-
</MixedArgumentTypeCoercion>
268238
<MixedArrayAccess>
269239
<code><![CDATA[$json['city']]]></code>
270240
<code><![CDATA[$json['country_code']]]></code>
@@ -310,11 +280,34 @@
310280
</RedundantConditionGivenDocblockType>
311281
</file>
312282
<file src="src/Services/MaxMindDatabase.php">
283+
<ImplicitToStringCast>
284+
<code><![CDATA[$file]]></code>
285+
</ImplicitToStringCast>
286+
<MissingClosureParamType>
287+
<code><![CDATA[$directory]]></code>
288+
</MissingClosureParamType>
313289
<MixedArgument>
290+
<code><![CDATA[$directory]]></code>
291+
<code><![CDATA[$directory]]></code>
292+
<code><![CDATA[$path]]></code>
293+
<code><![CDATA[$path]]></code>
294+
<code><![CDATA[$path]]></code>
295+
<code><![CDATA[$path]]></code>
314296
<code><![CDATA[$this->config('database_path')]]></code>
315297
<code><![CDATA[$this->config('locales', ['en'])]]></code>
316298
<code><![CDATA[$this->config('update_url')]]></code>
317299
</MixedArgument>
300+
<MixedAssignment>
301+
<code><![CDATA[$file]]></code>
302+
<code><![CDATA[$path]]></code>
303+
</MixedAssignment>
304+
<MixedMethodCall>
305+
<code><![CDATA[getFilename]]></code>
306+
</MixedMethodCall>
307+
<PossiblyInvalidMethodCall>
308+
<code><![CDATA[getPathName]]></code>
309+
<code><![CDATA[isDir]]></code>
310+
</PossiblyInvalidMethodCall>
318311
</file>
319312
<file src="src/Services/MaxMindWebService.php">
320313
<MixedArgument>
@@ -327,7 +320,12 @@
327320
<DocblockTypeContradiction>
328321
<code><![CDATA[is_null($this->errors)]]></code>
329322
</DocblockTypeContradiction>
323+
<InvalidScalarArgument>
324+
<code><![CDATA[$response]]></code>
325+
<code><![CDATA[$response]]></code>
326+
</InvalidScalarArgument>
330327
<MixedArgument>
328+
<code><![CDATA[$header_size]]></code>
331329
<code><![CDATA[$header_size]]></code>
332330
<code><![CDATA[Arr::get($this->config, 'headers', [])]]></code>
333331
<code><![CDATA[Arr::get($this->config, 'query', [])]]></code>
@@ -344,24 +342,17 @@
344342
<code><![CDATA[null]]></code>
345343
<code><![CDATA[null]]></code>
346344
</PossiblyNullPropertyAssignmentValue>
347-
<RiskyTruthyFalsyComparison>
348-
<code><![CDATA[strpos($url, '?')]]></code>
349-
</RiskyTruthyFalsyComparison>
350345
</file>
351346
<file src="src/helpers.php">
352347
<MixedInferredReturnType>
353-
<code><![CDATA[($ip is null ? \InteractionDesignFoundation\GeoIP\GeoIP : \InteractionDesignFoundation\GeoIP\Location)]]></code>
348+
<code><![CDATA[\InteractionDesignFoundation\GeoIP\GeoIP|\InteractionDesignFoundation\GeoIP\Location]]></code>
354349
</MixedInferredReturnType>
355350
<MixedMethodCall>
356351
<code><![CDATA[getLocation]]></code>
357352
</MixedMethodCall>
358353
<MixedReturnStatement>
359-
<code><![CDATA[app('geoip')]]></code>
360354
<code><![CDATA[app('geoip')]]></code>
361355
<code><![CDATA[app('geoip')->getLocation($ip)]]></code>
362356
</MixedReturnStatement>
363-
<PossiblyUndefinedMethod>
364-
<code><![CDATA[getLocation]]></code>
365-
</PossiblyUndefinedMethod>
366357
</file>
367358
</files>

src/Services/MaxMindDatabase.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function update()
7777
$this->withTemporaryDirectory(function ($directory): void {
7878
$tarFile = sprintf('%s/maxmind.tar.gz', $directory);
7979

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

8282
$archive = new \PharData($tarFile);
8383

@@ -173,4 +173,26 @@ protected function deleteDirectory(string $directory)
173173

174174
return rmdir($directory);
175175
}
176+
177+
protected function downloadFileByUrl(string $filename, string $url): void
178+
{
179+
$canUseFopenForUrl = in_array(strtolower((string) ini_get('allow_url_fopen')), ['1', 'on'], true);
180+
if ($canUseFopenForUrl) {
181+
file_put_contents($filename, fopen($url, 'rb'));
182+
} elseif (extension_loaded('curl')) {
183+
$fp = fopen($filename, 'wb+');
184+
if ($fp === false) {
185+
throw new \RuntimeException("Cannot open {$filename} file for writing.");
186+
}
187+
$ch = curl_init();
188+
curl_setopt($ch, \CURLOPT_URL, $url);
189+
curl_setopt($ch, \CURLOPT_FILE, $fp);
190+
curl_setopt($ch, \CURLOPT_FOLLOWLOCATION, true);
191+
curl_exec($ch);
192+
curl_close($ch);
193+
fclose($fp);
194+
} else {
195+
throw new \RuntimeException('Cannot download the file. Please enable allow_url_fopen or install curl extension.');
196+
}
197+
}
176198
}

0 commit comments

Comments
 (0)