|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 defined('MOODLE_INTERNAL') || die(); 00028 00034 function iplookup_find_location($ip) { 00035 global $CFG; 00036 00037 $info = array('city'=>null, 'country'=>null, 'longitude'=>null, 'latitude'=>null, 'error'=>null, 'note'=>'', 'title'=>array()); 00038 00039 if (!empty($CFG->geoipfile) and file_exists($CFG->geoipfile)) { 00040 require_once('Net/GeoIP.php'); 00041 00042 $textlib = textlib_get_instance(); 00043 00044 $geoip = Net_GeoIP::getInstance($CFG->geoipfile, Net_GeoIP::STANDARD); 00045 $location = $geoip->lookupLocation($ip); 00046 $geoip->close(); 00047 00048 if (empty($location)) { 00049 $info['error'] = get_string('iplookupfailed', 'error', $ip); 00050 return $info; 00051 } 00052 if (!empty($location->city)) { 00053 $info['city'] = $textlib->convert($location->city, 'iso-8859-1', 'utf-8'); 00054 $info['title'][] = $info['city']; 00055 } 00056 00057 if (!empty($location->country_code)) { 00058 $countries = get_string_manager()->get_list_of_countries(true); 00059 if (isset($countries[$location->country_code])) { 00060 // prefer our localized country names 00061 $info['country'] = $countries[$location->country_code]; 00062 } else { 00063 $info['country'] = $location->country_name; 00064 } 00065 $info['title'][] = $info['country']; 00066 } 00067 $info['longitude'] = $location->longitude; 00068 $info['latitude'] = $location->latitude; 00069 $info['note'] = get_string('iplookupmaxmindnote', 'admin'); 00070 00071 return $info; 00072 00073 } else { 00074 require_once($CFG->libdir.'/filelib.php'); 00075 00076 $ipdata = download_file_content('http://netgeo.caida.org/perl/netgeo.cgi?target='.$ip); 00077 if ($ipdata === false) { 00078 $info['error'] = get_string('cannotnetgeo', 'error'); 00079 return $info; 00080 } 00081 $matches = null; 00082 if (!preg_match('/LAT:\s*(-?\d+\.\d+)/s', $ipdata, $matches)) { 00083 $info['error'] = get_string('iplookupfailed', 'error', $ip); 00084 return $info; 00085 } 00086 $info['latitude'] = (float)$matches[1]; 00087 if (!preg_match('/LONG:\s*(-?\d+\.\d+)/s', $ipdata, $matches)) { 00088 $info['error'] = get_string('iplookupfailed', 'error', $ip); 00089 return $info; 00090 } 00091 $info['longitude'] = (float)$matches[1]; 00092 00093 if (preg_match('/CITY:\s*([^<]*)/', $ipdata, $matches)) { 00094 if (!empty($matches[1])) { 00095 $info['city'] = s($matches[1]); 00096 $info['title'][] = $info['city']; 00097 } 00098 } 00099 00100 if (preg_match('/COUNTRY:\s*([^<]*)/', $ipdata, $matches)) { 00101 if (!empty($matches[1])) { 00102 $countrycode = $matches[1]; 00103 $countries = get_string_manager()->get_list_of_countries(true); 00104 if (isset($countries[$countrycode])) { 00105 // prefer our localized country names 00106 $info['country'] = $countries[$countrycode]; 00107 } else { 00108 $info['country'] = $countrycode; 00109 } 00110 $info['title'][] = $info['country']; 00111 } 00112 } 00113 $info['note'] = get_string('iplookupnetgeonote', 'admin'); 00114 00115 return $info; 00116 } 00117 00118 }