MDL-48766 lib: Import MaxMind GeoIP2 PHP API
[moodle.git] / iplookup / lib.php
CommitLineData
b9ee1fc7
PS
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
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Returns location information
31 * @param string $ip
32 * @return array
33 */
34function 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->geoipfile) and file_exists($CFG->geoipfile)) {
40 require_once('Net/GeoIP.php');
41
b9ee1fc7
PS
42 $geoip = Net_GeoIP::getInstance($CFG->geoipfile, Net_GeoIP::STANDARD);
43 $location = $geoip->lookupLocation($ip);
44 $geoip->close();
45
46 if (empty($location)) {
47 $info['error'] = get_string('iplookupfailed', 'error', $ip);
48 return $info;
49 }
50 if (!empty($location->city)) {
2f1e464a 51 $info['city'] = core_text::convert($location->city, 'iso-8859-1', 'utf-8');
b9ee1fc7
PS
52 $info['title'][] = $info['city'];
53 }
54
64b0f3ab 55 if (!empty($location->countryCode)) {
b9ee1fc7 56 $countries = get_string_manager()->get_list_of_countries(true);
64b0f3ab 57 if (isset($countries[$location->countryCode])) {
b9ee1fc7 58 // prefer our localized country names
64b0f3ab 59 $info['country'] = $countries[$location->countryCode];
b9ee1fc7 60 } else {
64b0f3ab 61 $info['country'] = $location->countryName;
b9ee1fc7
PS
62 }
63 $info['title'][] = $info['country'];
64b0f3ab
PS
64
65 } else if (!empty($location->countryName)) {
66 $info['country'] = $location->countryName;
67 $info['title'][] = $info['country'];
b9ee1fc7 68 }
64b0f3ab 69
b9ee1fc7
PS
70 $info['longitude'] = $location->longitude;
71 $info['latitude'] = $location->latitude;
72 $info['note'] = get_string('iplookupmaxmindnote', 'admin');
73
74 return $info;
75
76 } else {
77 require_once($CFG->libdir.'/filelib.php');
78
8c6136a8
PS
79 $ipdata = download_file_content('http://www.geoplugin.net/json.gp?ip='.$ip);
80 if ($ipdata) {
81 $ipdata = preg_replace('/^geoPlugin\((.*)\)\s*$/s', '$1', $ipdata);
82 $ipdata = json_decode($ipdata, true);
b9ee1fc7 83 }
8c6136a8
PS
84 if (!is_array($ipdata)) {
85 $info['error'] = get_string('cannotgeoplugin', 'error');
b9ee1fc7
PS
86 return $info;
87 }
8c6136a8
PS
88 $info['latitude'] = (float)$ipdata['geoplugin_latitude'];
89 $info['longitude'] = (float)$ipdata['geoplugin_longitude'];
90 $info['city'] = s($ipdata['geoplugin_city']);
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']);
b9ee1fc7 99 }
b9ee1fc7 100
8c6136a8 101 $info['note'] = get_string('iplookupgeoplugin', 'admin');
b9ee1fc7 102
8c6136a8 103 $info['title'][] = $info['city'];
64b0f3ab 104 $info['title'][] = $info['country'];
b9ee1fc7
PS
105
106 return $info;
107 }
108
8c6136a8 109}