MDL-86495 core: Better errors handling for IP Lookup
- Since `Geoplugin` is not free anymore, a new admin setting called `GeoPlugin API key` has been added for user to input their API key if they still want to use `Geoplugin` service. - The `iplookup_find_location()` method has been improved to handle error responses more effectively.
This commit is contained in:
@@ -51,6 +51,14 @@ if ($hassiteconfig) {
|
||||
|
||||
$temp->add(new admin_setting_countrycodes('allcountrycodes', new lang_string('allcountrycodes', 'core_admin'),
|
||||
new lang_string('configallcountrycodes', 'core_admin')));
|
||||
|
||||
$temp->add(new admin_setting_configtext(
|
||||
'geopluginapikey',
|
||||
new lang_string('geopluginapikey', 'core_admin'),
|
||||
new lang_string('geopluginapikey_desc', 'core_admin'),
|
||||
'',
|
||||
PARAM_TEXT,
|
||||
));
|
||||
}
|
||||
|
||||
$ADMIN->add('location', $temp);
|
||||
|
||||
+10
-5
@@ -64,7 +64,7 @@ function iplookup_find_location($ip) {
|
||||
|
||||
return $info;
|
||||
|
||||
} else {
|
||||
} else if (!empty($CFG->geopluginapikey)) {
|
||||
require_once($CFG->libdir.'/filelib.php');
|
||||
|
||||
if (strpos($ip, ':') !== false) {
|
||||
@@ -73,11 +73,13 @@ function iplookup_find_location($ip) {
|
||||
return $info;
|
||||
}
|
||||
|
||||
$ipdata = download_file_content('http://www.geoplugin.net/json.gp?ip='.$ip);
|
||||
if ($ipdata) {
|
||||
$ipdata = preg_replace('/^geoPlugin\((.*)\)\s*$/s', '$1', $ipdata);
|
||||
$ipdata = json_decode($ipdata, true);
|
||||
$requesturl = new moodle_url('https://api.geoplugin.com', ['ip' => $ip, 'auth' => $CFG->geopluginapikey]);
|
||||
$response = download_file_content($requesturl->out(false), null, null, true);
|
||||
if ($response->response_code != 200) {
|
||||
$info['error'] = get_string('cannotgeoplugin', 'error');
|
||||
return $info;
|
||||
}
|
||||
$ipdata = json_decode($response->results, true);
|
||||
if (!is_array($ipdata)) {
|
||||
$info['error'] = get_string('cannotgeoplugin', 'error');
|
||||
return $info;
|
||||
@@ -104,4 +106,7 @@ function iplookup_find_location($ip) {
|
||||
return $info;
|
||||
}
|
||||
|
||||
$info['error'] = get_string('iplookupfailed', 'error', $ip);
|
||||
return $info;
|
||||
|
||||
}
|
||||
|
||||
@@ -50,8 +50,14 @@ final class geoip_test extends \advanced_testcase {
|
||||
* @param string $ip The IP to test
|
||||
*/
|
||||
public function test_ip($ip): void {
|
||||
global $CFG;
|
||||
if (!defined('TEST_GEOIP_APIKEY') || empty(TEST_GEOIP_APIKEY)) {
|
||||
$this->markTestSkipped('External geo tests are disabled.');
|
||||
}
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Store the old value to restore later.
|
||||
$oldvalue = $CFG->geopluginapikey;
|
||||
$CFG->geopluginapikey = TEST_GEOIP_APIKEY;
|
||||
$this->setup_geoip2file();
|
||||
|
||||
// Note: The results we get from the iplookup tests are beyond our control.
|
||||
@@ -69,6 +75,9 @@ final class geoip_test extends \advanced_testcase {
|
||||
$this->assertIsString($result['title'][0]);
|
||||
$this->assertIsString($result['title'][1]);
|
||||
$this->assertNull($result['error']);
|
||||
|
||||
// Restore the old value.
|
||||
$CFG->geopluginapikey = $oldvalue;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,6 +25,8 @@ namespace core;
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
final class geoplugin_test extends \advanced_testcase {
|
||||
/** @var string Current Geoplugin API key. */
|
||||
private string $currentgeopluginapikey = '';
|
||||
|
||||
/**
|
||||
* Load required test libraries
|
||||
@@ -35,12 +37,27 @@ final class geoplugin_test extends \advanced_testcase {
|
||||
}
|
||||
|
||||
/**
|
||||
* In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
|
||||
* In order to execute this test:
|
||||
* - PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
|
||||
* - GeoPlugin API key should be defined in config.php as TEST_GEOIP_APIKEY
|
||||
*/
|
||||
public function setUp(): void {
|
||||
global $CFG;
|
||||
if (!PHPUNIT_LONGTEST) {
|
||||
$this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
|
||||
}
|
||||
|
||||
if (!defined('TEST_GEOIP_APIKEY') || empty(TEST_GEOIP_APIKEY)) {
|
||||
$this->markTestSkipped('External geo tests are disabled.');
|
||||
}
|
||||
// Store the old value to restore later.
|
||||
$this->currentgeopluginapikey = $CFG->geopluginapikey;
|
||||
$CFG->geopluginapikey = TEST_GEOIP_APIKEY;
|
||||
}
|
||||
|
||||
protected function tearDown(): void {
|
||||
global $CFG;
|
||||
$CFG->geopluginapikey = $this->currentgeopluginapikey;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -690,6 +690,8 @@ $string['fullnamedisplayprivate'] = 'Full name format - private';
|
||||
$string['gdrequired'] = 'The GD extension is now required by Moodle for image conversion.';
|
||||
$string['generalsettings'] = 'General settings';
|
||||
$string['geoipfile'] = 'GeoLite2 City MaxMind DB';
|
||||
$string['geopluginapikey'] = 'GeoPlugin API key';
|
||||
$string['geopluginapikey_desc'] = 'The API key used to access the GeoPlugin service. Get your own key at <a href="https://www.geoplugin.com/" target="_blank">GeoPlugin page</a>.';
|
||||
$string['getremoteaddrconf'] = 'Logged IP address source';
|
||||
$string['globalsearch'] = 'Global search';
|
||||
$string['globalsearchmanage'] = 'Manage global search';
|
||||
|
||||
Reference in New Issue
Block a user