/** * Get GeopIP Record */ public function getGeoipRecord() { if (is_null($this->_geoip_record)) { if (extension_loaded('mbstring')) { $datafile = Mage::getBaseDir('media') . '/anattadesign/geoip/default/GeoLiteCity.dat'; if (is_readable($datafile) && is_file($datafile)) { try { $this->_geoip_record = AnattaDesign_AwesomeCheckout_Model_GeoIP_Core::getInstance($datafile, AnattaDesign_AwesomeCheckout_Model_GeoIP_Core::GEOIP_STANDARD)->geoip_record_by_addr($_SERVER['REMOTE_ADDR']); } catch (Exception $e) { $this->_geoip_record = false; } } else { $this->_geoip_record = false; } } else { $this->_geoip_record = false; } } return $this->_geoip_record; }
static function _get_record($gi, $ipnum, AnattaDesign_AwesomeCheckout_Model_GeoIP_Core $geoip) { $seek_country = $geoip->_geoip_seek_country($gi, $ipnum); if ($seek_country == $gi->databaseSegments) { return NULL; } // workaround php's broken substr, strpos, etc handling with // mbstring.func_overload and mbstring.internal_encoding $enc = mb_internal_encoding(); mb_internal_encoding('ISO-8859-1'); $record_pointer = $seek_country + (2 * $gi->record_length - 1) * $gi->databaseSegments; if ($gi->flags & AnattaDesign_AwesomeCheckout_Model_GeoIP_Core::GEOIP_MEMORY_CACHE) { $record_buf = substr($gi->memory_buffer, $record_pointer, self::FULL_RECORD_LENGTH); } elseif ($gi->flags & AnattaDesign_AwesomeCheckout_Model_GeoIP_Core::GEOIP_SHARED_MEMORY) { $record_buf = @shmop_read($gi->shmid, $record_pointer, self::FULL_RECORD_LENGTH); } else { fseek($gi->filehandle, $record_pointer, SEEK_SET); $record_buf = fread($gi->filehandle, self::FULL_RECORD_LENGTH); } $record = new GeoIp_Record(); $record_buf_pos = 0; $char = ord(substr($record_buf, $record_buf_pos, 1)); $record->country_code = $gi->GEOIP_COUNTRY_CODES[$char]; $record->country_code3 = $gi->GEOIP_COUNTRY_CODES3[$char]; $record->country_name = $gi->GEOIP_COUNTRY_NAMES[$char]; $record->continent_code = $gi->GEOIP_CONTINENT_CODES[$char]; $record_buf_pos++; $str_length = 0; // Get region $char = ord(substr($record_buf, $record_buf_pos + $str_length, 1)); while ($char != 0) { $str_length++; $char = ord(substr($record_buf, $record_buf_pos + $str_length, 1)); } if ($str_length > 0) { $record->region = substr($record_buf, $record_buf_pos, $str_length); } $record_buf_pos += $str_length + 1; $str_length = 0; // Get city $char = ord(substr($record_buf, $record_buf_pos + $str_length, 1)); while ($char != 0) { $str_length++; $char = ord(substr($record_buf, $record_buf_pos + $str_length, 1)); } if ($str_length > 0) { $record->city = substr($record_buf, $record_buf_pos, $str_length); } $record_buf_pos += $str_length + 1; $str_length = 0; // Get postal code $char = ord(substr($record_buf, $record_buf_pos + $str_length, 1)); while ($char != 0) { $str_length++; $char = ord(substr($record_buf, $record_buf_pos + $str_length, 1)); } if ($str_length > 0) { $record->postal_code = substr($record_buf, $record_buf_pos, $str_length); } $record_buf_pos += $str_length + 1; $str_length = 0; // Get latitude and longitude $latitude = 0; $longitude = 0; for ($j = 0; $j < 3; ++$j) { $char = ord(substr($record_buf, $record_buf_pos++, 1)); $latitude += $char << $j * 8; } $record->latitude = $latitude / 10000 - 180; for ($j = 0; $j < 3; ++$j) { $char = ord(substr($record_buf, $record_buf_pos++, 1)); $longitude += $char << $j * 8; } $record->longitude = $longitude / 10000 - 180; if (AnattaDesign_AwesomeCheckout_Model_GeoIP_Core::GEOIP_CITY_EDITION_REV1 == $gi->databaseType) { $metroarea_combo = 0; if ($record->country_code == "US") { for ($j = 0; $j < 3; ++$j) { $char = ord(substr($record_buf, $record_buf_pos++, 1)); $metroarea_combo += $char << $j * 8; } $record->metro_code = $record->dma_code = floor($metroarea_combo / 1000); $record->area_code = $metroarea_combo % 1000; } } mb_internal_encoding($enc); return $record; }
static function getInstance($filename, $flags) { if (is_null(self::$gi)) { $gi = new AnattaDesign_AwesomeCheckout_Model_GeoIP_Core(); $gi->flags = $flags; if ($gi->flags & self::GEOIP_SHARED_MEMORY) { $gi->shmid = @shmop_open(self::GEOIP_SHM_KEY, "a", 0, 0); } else { $gi->filehandle = fopen($filename, "rb"); if (!$gi->filehandle) { throw new Exception("Can not open {$filename}\n"); } if ($gi->flags & self::GEOIP_MEMORY_CACHE) { $s_array = fstat($gi->filehandle); $gi->memory_buffer = fread($gi->filehandle, $s_array['size']); } } $gi = self::_setup_segments($gi); self::$gi = $gi; } return self::$gi; }