Skip to content

Commit 4bbc9a4

Browse files
committed
Update GeoIP to support authentication by account id+license key
Fix #1301 Replace #1302
1 parent 4b58985 commit 4bbc9a4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+4476
-3667
lines changed

.github/workflows/phpstan.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ jobs:
4444
uses: "ramsey/composer-install@v2"
4545

4646
- name: Run PHPStan
47-
run: phpstan analyse --configuration=phpstan.neon.dist --error-format=github
47+
run: phpstan analyse --configuration=phpstan.dist.neon --error-format=github

.php-cs-fixer.dist.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
$finder = (new PhpCsFixer\Finder())
44
->in('mailscanner')->exclude('lib')
55
->in('tools')
6-
76
;
87

98
return (new PhpCsFixer\Config())
9+
->setFinder($finder)
1010
->setRiskyAllowed(true)
1111
->setRules([
1212
'@PSR2' => true,
@@ -16,7 +16,12 @@
1616
'concat_space' => ['spacing' => 'one'],
1717
'cast_spaces' => ['space' => 'none'],
1818
'native_function_invocation' => false,
19-
'no_superfluous_phpdoc_tags' => true,
19+
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true, 'remove_inheritdoc' => true],
20+
'fopen_flags' => ['b_mode' => true],
21+
'function_declaration' => ['closure_function_spacing' => 'none', 'closure_fn_spacing' => 'none'],
22+
'phpdoc_summary' => false,
23+
'phpdoc_no_package' => false,
24+
'phpdoc_separation' => ['groups' => [['ORM\\*'], ['Assert\\*'], ['Serializer\\*']]],
25+
'fully_qualified_strict_types' => false,
2026
])
21-
->setFinder($finder)
2227
;

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"php-parallel-lint/php-parallel-lint": "^1.4",
1414
"php-parallel-lint/php-console-highlighter": "^1.0.0",
1515
"rector/rector": "^1.2",
16-
"phpunit/phpunit": "^9.6"
16+
"phpunit/phpunit": "^9.6",
17+
"symfony/var-dumper": "^7.1"
1718
},
1819
"autoload-dev": {
1920
"psr-4": {

composer.lock

Lines changed: 95 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mailscanner/conf.php.example

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ define('LANG', 'en');
3939
// in the gui to change the language of his browser. The selectable languages are defined as a comma separated list.
4040
define('USER_SELECTABLE_LANG', 'de,en,es-419,fr,it,ja,nl,pt_br');
4141

42-
// Session Handling - conflicts can exist when the your environment makes use of multiple php sessions on the same server
43-
// to resolve this, uncomment the following option. See https://github.com/mailwatch/MailWatch/issues/730 for more info
44-
// A valid session name may consists of digits, letters A to Z (both upper and lower case), comma and dash
42+
// Session Handling - conflicts can exist when your environment makes use of multiple php sessions on the same server
43+
// to resolve this, uncomment the following option. See https://github.com/mailwatch/MailWatch/issues/730 for more info
44+
// A valid session name may consist of digits, letters A to Z (both upper and lower case), comma and dash
4545
//define('SESSION_NAME', 'MailWatch');
4646

4747
// Session Timeout - Sets the global session timeout value, default is 600 sec (10 minutes) if not defined
@@ -53,6 +53,7 @@ define('SESSION_TIMEOUT', 600);
5353
// A free license key from MaxMind is required to download GeoLite2 data
5454
// https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-geolite2-databases/
5555
// define('MAXMIND_LICENSE_KEY', 'mylicensekey');
56+
// define('MAXMIND_ACCOUNT_ID', 'myaccountid');
5657

5758
// Database settings
5859
//

mailscanner/functions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4297,6 +4297,7 @@ function checkConfVariables()
42974297
'IMAP_HOST' => ['description' => 'IMAP host to be used for user authentication'],
42984298
'IMAP_AUTOCREATE_VALID_USER' => ['description' => 'enable to autorcreate user from valid imap login'],
42994299
'MAXMIND_LICENSE_KEY' => ['description' => 'needed to download MaxMind GeoLite2 data'],
4300+
'MAXMIND_ACCOUNT_ID' => ['description' => 'needed to download MaxMind GeoLite2 data'],
43004301
'QUARANTINE_DAYS_TO_KEEP_NONSPAM' => ['description' => 'to have quarantine keeping days independently configured for nonspam mails'],
43014302
];
43024303

mailscanner/geoip_update.php

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
/*
3+
/**
44
* MailWatch for MailScanner
55
* Copyright (C) 2003-2011 Steve Freegard ([email protected])
66
* Copyright (C) 2011 Garrod Alwood ([email protected])
@@ -46,7 +46,7 @@
4646
<tr>
4747
<td>
4848
<br>
49-
' . __('message115') . ' <a href="https://dev.maxmind.com/geoip/geoip2/geolite2/" target="_maxmind">MaxMind</a> ' . __('message215') . '<br><br>
49+
' . __('message115') . ' <a href="https://dev.maxmind.com/geoip/geolite2-free-geolocation-data" target="_maxmind">MaxMind</a> ' . __('message215') . '<br><br>
5050
</td>
5151
</tr>
5252
<tr>
@@ -63,7 +63,8 @@
6363

6464
$files_base_url = 'https://download.maxmind.com';
6565
$file['description'] = __('geoip15');
66-
$file['path'] = '/app/geoip_download?edition_id=GeoLite2-Country&suffix=tar.gz&license_key=' . MAXMIND_LICENSE_KEY;
66+
$file['path'] = '/geoip/databases/GeoLite2-Country/download?suffix=tar.gz';
67+
$file['legacy_path'] = '/app/geoip_download?edition_id=GeoLite2-Country&suffix=tar.gz&license_key=' . MAXMIND_LICENSE_KEY;
6768
$file['destination'] = __DIR__ . '/temp/GeoLite2-Country.tar.gz';
6869
$file['destinationFileName'] = 'GeoLite2-Country.mmdb';
6970

@@ -112,12 +113,36 @@
112113

113114
try {
114115
$requestSession->options['filename'] = $file['destination'];
115-
$result = $requestSession->get($file['path']);
116+
if (defined('MAXMIND_ACCOUNT_ID')) {
117+
// use basic auth
118+
$requestSession->options['auth'] = new Requests_Auth_Basic([MAXMIND_ACCOUNT_ID, MAXMIND_LICENSE_KEY]);
119+
120+
// remove auth and hooks from redirect request
121+
$hooks = new Requests_Hooks();
122+
$hooks->register('requests.before_redirect', function (
123+
&$location,
124+
&$req_headers,
125+
&$req_data,
126+
&$options
127+
) {
128+
$options['auth'] = false;
129+
$options['hooks'] = new Requests_Hooks();
130+
});
131+
$requestSession->options['hooks'] = $hooks;
132+
$result = $requestSession->get($file['path']);
133+
} else {
134+
$result = $requestSession->get($file['legacy_path']);
135+
}
136+
$result->throw_for_status();
116137
if (true === $result->success) {
117138
echo $file['description'] . ' ' . __('downok15') . '<br>' . "\n";
118139
}
119140
} catch (Requests_Exception $e) {
120-
echo __('downbad15') . ' ' . $file['description'] . __('colon99') . ' ' . $e->getMessage() . "<br>\n";
141+
echo sprintf('%s %s%s %s', __('downbad15'), $file['description'], __('colon99'), $e->getMessage());
142+
if (file_exists($file['destination'])) {
143+
echo sprintf(' (%s)', strip_tags(file_get_contents($file['destination'])));
144+
}
145+
exit;
121146
}
122147

123148
ob_flush();
@@ -137,7 +162,13 @@
137162
}
138163
}
139164

140-
$command = escapeshellcmd('wget ' . $proxyString . ' -N ' . $files_base_url . $file['path'] . ' -O ' . $file['destination']);
165+
if (defined('MAXMIND_ACCOUNT_ID')) {
166+
$wget_basic_auth = sprintf('--user=%s --password=%s', MAXMIND_ACCOUNT_ID, MAXMIND_LICENSE_KEY);
167+
$command = escapeshellcmd('wget ' . $wget_basic_auth . $proxyString . ' -N ' . $files_base_url . $file['path'] . ' -O ' . $file['destination']);
168+
} else {
169+
$command = escapeshellcmd('wget ' . $proxyString . ' -N ' . $files_base_url . $file['path'] . ' -O ' . $file['destination']);
170+
}
171+
141172
$result = exec(
142173
$command,
143174
$output_wget,
@@ -146,6 +177,7 @@
146177

147178
if ($retval_wget > 0) {
148179
echo __('downbad15') . ' ' . $file['description'] . "<br>\n";
180+
exit;
149181
} else {
150182
echo $file['description'] . ' ' . __('downok15') . '<br>' . "\n";
151183
}

0 commit comments

Comments
 (0)