config = $config; $this->isEuropeanUnionCountry = $isEuropeanUnionCountry; $this->geoIpServices = $geoIpServices; } /** * @return bool */ public function canUse(): bool { return $this->config->isConfiguredForUse(); } /** * @param int $ip * @param array $priorityOfServices * @return bool */ public function isInEuropeanUnion($ip = 0, $priorityOfServices = []): bool { return $this->isEuropeanUnionCountry->validate( $this->getCountryCode($ip, $priorityOfServices) ); } /** * @param int $ip * @return array|string */ public function getGeoLocation($ip = 0, $priorityOfServices = []) { $result = null; $geoIpModels = $this->getGeoIpModels($priorityOfServices); if ($geoIpModels) { foreach ($geoIpModels as $geoIpModel) { $result = $geoIpModel->getGeoLocation($ip); if ($result) { break; } } } if (!empty($result) && !isset($result['is_in_european_union'])) { $result['is_in_european_union'] = $this->isInEuropeanUnion($ip); } return (!empty($result)) ? $result : __('No records found')->getText(); } /** * @param int $ip * @return null */ public function getCountryCode($ip = 0, $priorityOfServices = []) { $result = null; $geoIpModels = $this->getGeoIpModels($priorityOfServices); foreach ($geoIpModels as $geoIpModel) { $result = $geoIpModel->getCountryCode($ip); if ($result) { break; } } return (!empty($result)) ? $result : null; } /** * @return array|null */ public function getGeoIpModels($priorityOfServices = []) { $serviceItems = []; if (! $this->config->isModuleEnabled()) { return null; } if (!empty($this->geoIpModels)) { return $this->geoIpModels; } $services = [ self::MAXMIND => [ 'model' => $this->geoIpServices['MaxMind'], 'enabled' => $this->config->enabledMaxmindGeoIp() ], self::IPTOCOUNTRY => [ 'model' => $this->geoIpServices['IpToCountry'], 'enabled' => $this->config->enabledIpToCountryGeoIp() ], self::IPAPI => [ 'model' => $this->geoIpServices['IpApi'], 'enabled' => $this->config->enabledIpApiGeoIp() ] ]; if (!empty($priorityOfServices)) { foreach ($priorityOfServices as $sId) { if (!empty($services[$sId])) { $serviceItems[$sId] = $services[$sId]; } } } else { $serviceItems = $services; } foreach ($serviceItems as $service) { if ($service['enabled']) { $this->geoIpModels[] = $service['model']; } } return $this->geoIpModels; } /** * @param $ip * @return array */ public function geoIpLookupTest($ip) { $fields = [ 'continent_code' => __('Continent'), 'continent_name' => __('Continent Code'), 'country_name' => __('Country'), 'country_code' => __('Country Code'), 'region_name' => __('Region Name'), 'city_name' => __('City'), 'postal_code' => __('Postal Code'), 'time_zone' => __('Time Zone'), 'is_in_european_union' => __('Is In European Union'), 'latitude' => __('Latitude'), 'longitude' => __('Longitude'), 'metro_code' => __('Metro Code'), 'subdivision_1_iso_code' => __('Subdivision 1 Iso Code'), 'subdivision_1_name' => __('Subdivision 1 Name'), 'subdivision_2_iso_code' => __('Subdivision 2 Iso Code'), 'subdivision_2_name' => __('Subdivision 2 Name'), 'database_name' => __('Database Name') ]; $result = []; $text = ''; $geoData = $this->getGeoLocation($ip); if (is_array($geoData)) { $geoData['is_in_european_union'] = ($geoData['is_in_european_union'] === true) ? 'true' : 'false'; foreach ($fields as $key => $value) { if (array_key_exists($key, $geoData) && !empty($geoData[$key])) { $text .= $value . ": " . $geoData[$key] . "\n"; } } if (!empty($geoData['latitude']) && !empty($geoData['longitude'])) { $result['latitude'] = $geoData['latitude']; $result['longitude'] = $geoData['longitude']; } elseif (!empty($geoData['country_name'])) { $result['country_name'] = $geoData['country_name']; } } else { $text = $geoData; } $result['text'] = $text; return $result; } /** * @param int $ip * @param array $priorityOfServices * @return null */ public function getCurrentCountryState($ip = 0, $priorityOfServices = []) { $result = null; $geoIpModels = $this->getGeoIpModels($priorityOfServices); foreach ($geoIpModels as $geoIpModel) { $result = $geoIpModel->getCurrentCountryState($ip); if ($result) { break; } } return (!empty($result)) ? $result : null; } }