Open Journal Systems  3.3.0
GeoLocationTool.inc.php
1 <?php
2 
17 include('lib' . DIRECTORY_SEPARATOR . 'geoIp' . DIRECTORY_SEPARATOR . 'geoipcity.inc');
18 
20 
22 
24 
26 
32  function __construct() {
33  $geoLocationDbFile = dirname(__FILE__) . DIRECTORY_SEPARATOR . "GeoLiteCity.dat";
34  if (file_exists($geoLocationDbFile)) {
35  $isDbFilePresent = true;
36  $this->_geoLocationTool = geoip_open($geoLocationDbFile, GEOIP_STANDARD);
37  include('lib' . DIRECTORY_SEPARATOR . 'geoIp' . DIRECTORY_SEPARATOR . 'geoipregionvars.php');
38  $this->_regionName = $GEOIP_REGION_NAME;
39  } else {
40  $isDbFilePresent = false;
41  }
42 
43  $this->_isDbFilePresent = $isDbFilePresent;
44  }
45 
46  //
47  // Public methods.
48  //
53  function isPresent() {
55  }
56 
63  function getGeoLocation($ip) {
64  // If no geolocation tool, the geo database file is missing.
65  if (!$this->_geoLocationTool) return array(null, null, null);
66 
67  $record = geoip_record_by_addr($this->_geoLocationTool, $ip);
68 
69  if (!$record) {
70  return array(null, null, null);
71  }
72 
73  $regionName = null;
74  if(isset($this->_regionName[$record->country_code][$record->region])) {
75  $regionName = $this->_regionName[$record->country_code][$record->region];
76  }
77 
78  return array(
79  $record->country_code,
80  utf8_encode($record->city),
81  $record->region
82  );
83  }
84 
89  function getAllCountryCodes() {
90  if (!$this->_geoLocationTool) return null;
91 
93  $countryCodes = $tool->GEOIP_COUNTRY_CODES;
94 
95  // Overwrite the first empty record with the code to
96  // unknow country.
97  $countryCodes[0] = STATISTICS_UNKNOWN_COUNTRY_ID;
98  return $countryCodes;
99  }
100 
107  function get3LettersCountryCode($countryCode) {
108  return $this->_getCountryCodeOnList($countryCode, 'GEOIP_COUNTRY_CODES3');
109  }
110 
117  function get2LettersCountryCode($countryCode3) {
118  return $this->_getCountryCodeOnList($countryCode3, 'GEOIP_COUNTRY_CODES');
119  }
120 
126  function getRegions($countryId) {
127  $regions = array();
128  $database = $this->_regionName;
129  if (isset($database[$countryId])) {
130  $regions = $database[$countryId];
131  }
132 
133  return $regions;
134  }
135 
144  function _getCountryCodeOnList($countryCode, $countryCodeListName) {
145  $returner = null;
146 
147  if (!$this->_geoLocationTool) return $returner;
149 
150  if (isset($tool->$countryCodeListName)) {
151  $countryCodeList = $tool->$countryCodeListName;
152  } else {
153  return $returner;
154  }
155 
156  $countryCodesIndex = $tool->GEOIP_COUNTRY_CODE_TO_NUMBER;
157  $countryCodeIndex = null;
158 
159  if (isset($countryCodesIndex[$countryCode])) {
160  $countryCodeIndex = $countryCodesIndex[$countryCode];
161  }
162 
163  if ($countryCodeIndex) {
164  if (isset($countryCodeList[$countryCodeIndex])) {
165  $returner = $countryCodeList[$countryCodeIndex];
166  }
167  }
168 
169  return $returner;
170  }
171 }
172 
GeoLocationTool\_getCountryCodeOnList
_getCountryCodeOnList($countryCode, $countryCodeListName)
Definition: GeoLocationTool.inc.php:144
GeoLocationTool\isPresent
isPresent()
Definition: GeoLocationTool.inc.php:53
GeoLocationTool\__construct
__construct()
Definition: GeoLocationTool.inc.php:32
GeoLocationTool
Geo location by ip wrapper class.
Definition: GeoLocationTool.inc.php:19
$tool
$tool
Definition: mergeCoverageReportTool.php:120
GeoLocationTool\getAllCountryCodes
getAllCountryCodes()
Definition: GeoLocationTool.inc.php:89
GeoLocationTool\$_regionName
$_regionName
Definition: GeoLocationTool.inc.php:23
GeoLocationTool\$_isDbFilePresent
$_isDbFilePresent
Definition: GeoLocationTool.inc.php:25
GeoLocationTool\getGeoLocation
getGeoLocation($ip)
Definition: GeoLocationTool.inc.php:63
GeoLocationTool\get2LettersCountryCode
get2LettersCountryCode($countryCode3)
Definition: GeoLocationTool.inc.php:117
GeoLocationTool\get3LettersCountryCode
get3LettersCountryCode($countryCode)
Definition: GeoLocationTool.inc.php:107
GeoLocationTool\getRegions
getRegions($countryId)
Definition: GeoLocationTool.inc.php:126
GeoLocationTool\$_geoLocationTool
$_geoLocationTool
Definition: GeoLocationTool.inc.php:21