| 1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
// This file is part of Moodle - http://moodle.org/
|
|
|
4 |
//
|
|
|
5 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
6 |
// it under the terms of the GNU General Public License as published by
|
|
|
7 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
8 |
// (at your option) any later version.
|
|
|
9 |
//
|
|
|
10 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
13 |
// GNU General Public License for more details.
|
|
|
14 |
//
|
|
|
15 |
// You should have received a copy of the GNU General Public License
|
|
|
16 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* IP Lookup utility functions
|
|
|
20 |
*
|
|
|
21 |
* @package core
|
|
|
22 |
* @subpackage iplookup
|
|
|
23 |
* @copyright 2010 Petr Skoda {@link http://skodak.org}
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Returns location information
|
|
|
31 |
* @param string $ip
|
|
|
32 |
* @return array
|
|
|
33 |
*/
|
|
|
34 |
function iplookup_find_location($ip) {
|
|
|
35 |
global $CFG;
|
|
|
36 |
|
|
|
37 |
$info = array('city'=>null, 'country'=>null, 'longitude'=>null, 'latitude'=>null, 'error'=>null, 'note'=>'', 'title'=>array());
|
|
|
38 |
|
|
|
39 |
if (!empty($CFG->geoip2file) and file_exists($CFG->geoip2file)) {
|
|
|
40 |
$reader = new GeoIp2\Database\Reader($CFG->geoip2file);
|
|
|
41 |
$record = $reader->city($ip);
|
|
|
42 |
|
|
|
43 |
if (empty($record)) {
|
|
|
44 |
$info['error'] = get_string('iplookupfailed', 'error', $ip);
|
|
|
45 |
return $info;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
$info['city'] = core_text::convert($record->city->name, 'iso-8859-1', 'utf-8');
|
|
|
49 |
$info['title'][] = $info['city'];
|
|
|
50 |
|
|
|
51 |
$countrycode = $record->country->isoCode;
|
|
|
52 |
$countries = get_string_manager()->get_list_of_countries(true);
|
|
|
53 |
if (isset($countries[$countrycode])) {
|
|
|
54 |
// Prefer our localized country names.
|
|
|
55 |
$info['country'] = $countries[$countrycode];
|
|
|
56 |
} else {
|
|
|
57 |
$info['country'] = $record->country->names['en'];
|
|
|
58 |
}
|
|
|
59 |
$info['title'][] = $info['country'];
|
|
|
60 |
|
|
|
61 |
$info['longitude'] = $record->location->longitude;
|
|
|
62 |
$info['latitude'] = $record->location->latitude;
|
|
|
63 |
$info['note'] = get_string('iplookupmaxmindnote', 'admin');
|
|
|
64 |
|
|
|
65 |
return $info;
|
|
|
66 |
|
| 1441 |
ariadna |
67 |
} else if (!empty($CFG->geopluginapikey)) {
|
| 1 |
efrain |
68 |
require_once($CFG->libdir.'/filelib.php');
|
|
|
69 |
|
|
|
70 |
if (strpos($ip, ':') !== false) {
|
|
|
71 |
// IPv6 is not supported by geoplugin.net.
|
|
|
72 |
$info['error'] = get_string('invalidipformat', 'error');
|
|
|
73 |
return $info;
|
|
|
74 |
}
|
|
|
75 |
|
| 1441 |
ariadna |
76 |
$requesturl = new moodle_url('https://api.geoplugin.com', ['ip' => $ip, 'auth' => $CFG->geopluginapikey]);
|
|
|
77 |
$response = download_file_content($requesturl->out(false), null, null, true);
|
|
|
78 |
if ($response->response_code != 200) {
|
|
|
79 |
$info['error'] = get_string('cannotgeoplugin', 'error');
|
|
|
80 |
return $info;
|
| 1 |
efrain |
81 |
}
|
| 1441 |
ariadna |
82 |
$ipdata = json_decode($response->results, true);
|
| 1 |
efrain |
83 |
if (!is_array($ipdata)) {
|
|
|
84 |
$info['error'] = get_string('cannotgeoplugin', 'error');
|
|
|
85 |
return $info;
|
|
|
86 |
}
|
|
|
87 |
$info['latitude'] = (float)$ipdata['geoplugin_latitude'];
|
|
|
88 |
$info['longitude'] = (float)$ipdata['geoplugin_longitude'];
|
|
|
89 |
$info['city'] = s($ipdata['geoplugin_city']);
|
|
|
90 |
$info['accuracyRadius'] = (int)$ipdata['geoplugin_locationAccuracyRadius']; // Unit is in Miles.
|
|
|
91 |
|
|
|
92 |
$countrycode = $ipdata['geoplugin_countryCode'];
|
|
|
93 |
$countries = get_string_manager()->get_list_of_countries(true);
|
|
|
94 |
if (isset($countries[$countrycode])) {
|
|
|
95 |
// prefer our localized country names
|
|
|
96 |
$info['country'] = $countries[$countrycode];
|
|
|
97 |
} else {
|
|
|
98 |
$info['country'] = s($ipdata['geoplugin_countryName']);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
$info['note'] = get_string('iplookupgeoplugin', 'admin');
|
|
|
102 |
|
|
|
103 |
$info['title'][] = $info['city'];
|
|
|
104 |
$info['title'][] = $info['country'];
|
|
|
105 |
|
|
|
106 |
return $info;
|
|
|
107 |
}
|
|
|
108 |
|
| 1441 |
ariadna |
109 |
$info['error'] = get_string('iplookupfailed', 'error', $ip);
|
|
|
110 |
return $info;
|
|
|
111 |
|
| 1 |
efrain |
112 |
}
|