MDL-31832 CLI upgrade will return 0 exit status if no upgrade is needed
[moodle.git] / iplookup / lib.php
blob920f55551f0a0bcfdd60f89f1571302c17da7d62
1 <?php
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.
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/>.
18 /**
19 * IP Lookup utility functions
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
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Returns location information
31 * @param string $ip
32 * @return array
34 function iplookup_find_location($ip) {
35 global $CFG;
37 $info = array('city'=>null, 'country'=>null, 'longitude'=>null, 'latitude'=>null, 'error'=>null, 'note'=>'', 'title'=>array());
39 if (!empty($CFG->geoipfile) and file_exists($CFG->geoipfile)) {
40 require_once('Net/GeoIP.php');
42 $textlib = textlib_get_instance();
44 $geoip = Net_GeoIP::getInstance($CFG->geoipfile, Net_GeoIP::STANDARD);
45 $location = $geoip->lookupLocation($ip);
46 $geoip->close();
48 if (empty($location)) {
49 $info['error'] = get_string('iplookupfailed', 'error', $ip);
50 return $info;
52 if (!empty($location->city)) {
53 $info['city'] = $textlib->convert($location->city, 'iso-8859-1', 'utf-8');
54 $info['title'][] = $info['city'];
57 if (!empty($location->country_code)) {
58 $countries = get_string_manager()->get_list_of_countries(true);
59 if (isset($countries[$location->country_code])) {
60 // prefer our localized country names
61 $info['country'] = $countries[$location->country_code];
62 } else {
63 $info['country'] = $location->country_name;
65 $info['title'][] = $info['country'];
67 $info['longitude'] = $location->longitude;
68 $info['latitude'] = $location->latitude;
69 $info['note'] = get_string('iplookupmaxmindnote', 'admin');
71 return $info;
73 } else {
74 require_once($CFG->libdir.'/filelib.php');
76 $ipdata = download_file_content('http://netgeo.caida.org/perl/netgeo.cgi?target='.$ip);
77 if ($ipdata === false) {
78 $info['error'] = get_string('cannotnetgeo', 'error');
79 return $info;
81 $matches = null;
82 if (!preg_match('/LAT:\s*(-?\d+\.\d+)/s', $ipdata, $matches)) {
83 $info['error'] = get_string('iplookupfailed', 'error', $ip);
84 return $info;
86 $info['latitude'] = (float)$matches[1];
87 if (!preg_match('/LONG:\s*(-?\d+\.\d+)/s', $ipdata, $matches)) {
88 $info['error'] = get_string('iplookupfailed', 'error', $ip);
89 return $info;
91 $info['longitude'] = (float)$matches[1];
93 if (preg_match('/CITY:\s*([^<]*)/', $ipdata, $matches)) {
94 if (!empty($matches[1])) {
95 $info['city'] = s($matches[1]);
96 $info['title'][] = $info['city'];
100 if (preg_match('/COUNTRY:\s*([^<]*)/', $ipdata, $matches)) {
101 if (!empty($matches[1])) {
102 $countrycode = $matches[1];
103 $countries = get_string_manager()->get_list_of_countries(true);
104 if (isset($countries[$countrycode])) {
105 // prefer our localized country names
106 $info['country'] = $countries[$countrycode];
107 } else {
108 $info['country'] = $countrycode;
110 $info['title'][] = $info['country'];
113 $info['note'] = get_string('iplookupnetgeonote', 'admin');
115 return $info;