Skip to content

Commit fff267f

Browse files
Merge pull request #419 from nextcloud/backport/418/stable20
[stable20] Trying to access offset on null on maxmind reader
2 parents 3882f31 + 1d3c707 commit fff267f

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

lib/CountryDetector.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,18 @@ public function getCountry(): string {
4646
$reader = new Reader(__DIR__ . '/../vendor/GeoLite2-Country.mmdb');
4747
$record = $reader->get($this->request->getRemoteAddress());
4848
} catch (\Exception $e) {
49-
return '--';
49+
return CountryMapper::GLOBAL;
5050
}
5151

52-
if($this->countryMapper->isValidCountry($record['country']['iso_code'])) {
52+
if ($record === null) {
53+
// No match found, e.g. for local address like 127.0.0.1
54+
return CountryMapper::GLOBAL;
55+
}
56+
57+
if ($this->countryMapper->isValidCountry($record['country']['iso_code'])) {
5358
return $record['country']['iso_code'];
5459
}
5560

56-
return '--';
61+
return CountryMapper::GLOBAL;
5762
}
5863
}

lib/Db/Mapper/CountryMapper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
use OCP\IL10N;
2525

2626
class CountryMapper {
27+
28+
public const GLOBAL = '--';
29+
2730
/** @var IL10N */
2831
private $l;
2932

@@ -48,7 +51,7 @@ public function isValidCountry($countryCode): bool {
4851
*/
4952
public function getCountries(): array {
5053
$countries = [
51-
'--' => $this->l->t('Global'),
54+
self::GLOBAL => $this->l->t('Global'),
5255
'AF' => $this->l->t('Afghanistan'),
5356
'AX' => $this->l->t('Åland Islands'),
5457
'AL' => $this->l->t('Albania'),

lib/Db/Mapper/TermsMapper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public function getTermsForCountryCode(string $countryCode): array {
5555
}
5656
$result->closeCursor();
5757

58-
if (empty($entities) && $countryCode !== '--') {
59-
return $this->getTermsForCountryCode('--');
58+
if (empty($entities) && $countryCode !== CountryMapper::GLOBAL) {
59+
return $this->getTermsForCountryCode(CountryMapper::GLOBAL);
6060
}
6161

6262
return $entities;

0 commit comments

Comments
 (0)