_geoLocationTool = geoip_open($geoLocationDbFile, GEOIP_STANDARD); include('lib' . DIRECTORY_SEPARATOR . 'geoIp' . DIRECTORY_SEPARATOR . 'geoipregionvars.php'); $this->_regionName = $GEOIP_REGION_NAME; } else { $isDbFilePresent = false; } $this->_isDbFilePresent = $isDbFilePresent; } // // Public methods. // /** * Return country code and city name for the passed * ip address. * @param $ip string * @return array */ function getGeoLocation($ip) { // If no geolocation tool, the geo database file is missing. if (!$this->_geoLocationTool) return array(null, null, null); $record = geoip_record_by_addr($this->_geoLocationTool, $ip); if (!$record) { return array(null, null, null); } $regionName = null; if(isset($this->_regionName[$record->country_code][$record->region])) { $regionName = $this->_regionName[$record->country_code][$record->region]; } return array( $record->country_code, utf8_encode($record->city), $record->region ); } /** * Identify if the geolocation database tool is available for use. * @return boolean */ function isPresent() { return $this->_isDbFilePresent; } /** * Get all country codes. * @return mixed array or null */ function getAllCountryCodes() { if (!$this->_geoLocationTool) return null; $tool =& $this->_geoLocationTool; $countryCodes = $tool->GEOIP_COUNTRY_CODES; // Overwrite the first empty record with the code to // unknow country. $countryCodes[0] = STATISTICS_UNKNOWN_COUNTRY_ID; return $countryCodes; } /** * Return the 3 letters version of country codes * based on the passed 2 letters version. * @param $countryCode string * @return mixed string or null */ function get3LettersCountryCode($countryCode) { return $this->_getCountryCodeOnList($countryCode, 'GEOIP_COUNTRY_CODES3'); } /** * Return the 2 letter version of country codes * based on the passed 3 letters version. * @param $countryCode3 string * @return mixed string or null */ function get2LettersCountryCode($countryCode3) { return $this->_getCountryCodeOnList($countryCode3, 'GEOIP_COUNTRY_CODES'); } /** * Get regions by country. * @param $countryId int * @return array */ function getRegions($countryId) { $regions = array(); $database = $this->_regionName; if (isset($database[$countryId])) { $regions = $database[$countryId]; } return $regions; } /** * Get the passed country code inside the passed * list. * @param $countryCode The 2 letters country code. * @param $countryCodeList array Any geoip country * code list. * @return mixed String or null. */ function _getCountryCodeOnList($countryCode, $countryCodeListName) { $returner = null; if (!$this->_geoLocationTool) return $returner; $tool =& $this->_geoLocationTool; if (isset($tool->$countryCodeListName)) { $countryCodeList = $tool->$countryCodeListName; } else { return $returner; } $countryCodesIndex = $tool->GEOIP_COUNTRY_CODE_TO_NUMBER; $countryCodeIndex = null; if (isset($countryCodesIndex[$countryCode])) { $countryCodeIndex = $countryCodesIndex[$countryCode]; } if ($countryCodeIndex) { if (isset($countryCodeList[$countryCodeIndex])) { $returner = $countryCodeList[$countryCodeIndex]; } } return $returner; } } ?>