Files
ltsecurityinc/app/code/Plumrocket/GeoIPLookup/Helper/Config.php
T

240 lines
5.9 KiB
PHP
Executable File

<?php
/**
* Plumrocket Inc.
*
* NOTICE OF LICENSE
*
* This source file is subject to the End-user License Agreement
* that is available through the world-wide-web at this URL:
* http://wiki.plumrocket.net/wiki/EULA
* If you are unable to obtain it through the world-wide-web, please
* send an email to support@plumrocket.com so we can send you a copy immediately.
*
* @package Plumrocket_GeoIPLookup
* @copyright Copyright (c) 2018 Plumrocket Inc. (http://www.plumrocket.com)
* @license http://wiki.plumrocket.net/wiki/EULA End-user License Agreement
*/
namespace Plumrocket\GeoIPLookup\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Plumrocket\Base\Model\ConfigUtils;
/**
* Class Config use for retrieve module configuration
*/
class Config extends AbstractHelper
{
/**
* Name of Maxmind Group
*/
public const MAXMIND_GROUP = 'maxmindgeoip';
/**
* Name of IpToCountry Group
*/
public const IPTOCOUNTRY_GROUP = 'iptocountry';
/**
* Name of Ipapi Group
*/
public const IPAPI_GROUP = 'ipapigeoip';
/**
* Local File Path
*/
public const LOCAL_PATH = 'prgeoiplookup/data/';
/**
* Name of GeoIp Test Group
*/
public const IPAPI_CONNECTION_URL = 'api.ipapi.com';
/**
* Default Ip for testing
*/
public const DEFAULT_IP = '192.168.1.1';
public const IPTOCOUNTRY_SOURCE_FILE_NAME = 'IpToCountry.mmdb';
public const IPTOCOUNTRY_SOURCE_FILE_URL = 'https://plumrocket.com/docs/wp-content/uploads/geoip/v2/' . self::IPTOCOUNTRY_SOURCE_FILE_NAME;
public const IPTOCOUNTRY_DATABASE_NAME = 'Iptocountry';
public const MAXMIND_SOURCE_FILE_NAME = 'MaxMind.mmdb';
public const MAXMIND_SOURCE_FILE_URL = 'https://plumrocket.com/docs/wp-content/uploads/geoip/v2/' . self::MAXMIND_SOURCE_FILE_NAME;
public const MAXMIND_DATABASE_NAME = 'Maxmind';
/**
* Version source file on Wiki
*/
public const PATH_VERSION_FILE = 'https://plumrocket.com/docs/wp-content/uploads/geoip/v2/versions.csv';
/**
* @var \Plumrocket\Base\Model\ConfigUtils
*/
private $configUtils;
/**
* @param \Magento\Framework\App\Helper\Context $context
* @param \Plumrocket\Base\Model\ConfigUtils $configUtils
*/
public function __construct(
Context $context,
ConfigUtils $configUtils
) {
parent::__construct($context);
$this->configUtils = $configUtils;
}
/**
* @param $path
* @param null $store
* @param null $scope
* @return mixed
*/
private function getConfigForCurrentSection($path, $store = null, $scope = null)
{
return $this->configUtils->getConfig(
'prgeoiplookup/'. $path,
$store,
$scope
);
}
/**
* @inheritDoc
*/
public function isModuleEnabled($store = null, $scope = null): bool
{
return $this->configUtils->isSetFlag('prgeoiplookup/general/enabled', $store, $scope);
}
/**
* @return bool
*/
public function isConfiguredForUse(): bool
{
return $this->isModuleEnabled() && $this->getEnableMethodsNumber();
}
/**
* @param int|string $store
* @return bool
*/
public function enabledMaxmindGeoIp($store = null)
{
return (bool)$this->getConfigForCurrentSection(
'methods/' . self::MAXMIND_GROUP . '/enabled',
$store
);
}
/**
* @param int|string $store
* @return string
*/
public function getMaxmindInstallMethod($store = null)
{
return $this->getConfigForCurrentSection(
'methods/' . self::MAXMIND_GROUP . '/install_method',
$store
);
}
/**
* @param int|string $store
* @return bool
*/
public function enabledIpToCountryGeoIp($store = null)
{
return (bool)$this->getConfigForCurrentSection(
'methods/' . self::IPTOCOUNTRY_GROUP . '/enabled',
$store
);
}
/**
* @param int|string $store
* @return string
*/
public function getIpToCountryInstallMethod($store = null)
{
return $this->getConfigForCurrentSection(
'methods/' . self::IPTOCOUNTRY_GROUP . '/install_method',
$store
);
}
/**
* @param int|string $store
* @return bool
*/
public function enabledIpApiGeoIp($store = null)
{
return (bool)$this->getConfigForCurrentSection(
'methods/' . self::IPAPI_GROUP . '/enabled',
$store
);
}
/**
* @param null $store
* @return bool
*/
public function useIpApiHttps($store = null)
{
return (bool) $this->getConfigForCurrentSection(
'methods/' . self::IPAPI_GROUP . '/use_https',
$store
);
}
/**
* @param null $store
* @return bool
*/
private function getIpApiAccessKey($store = null)
{
return (string)$this->getConfigForCurrentSection(
'methods/' . self::IPAPI_GROUP . '/access_key',
$store
);
}
/**
* @param $ip
* @return string
*/
public function getIpApiUrl($ip = self::DEFAULT_IP, $withAccessKey = true)
{
$apiUrl = self::IPAPI_CONNECTION_URL . '/' . $ip;
if ($withAccessKey) {
$apiUrl .= '?access_key=' . $this->getIpApiAccessKey();
}
return $apiUrl;
}
/**
* @param null $store
* @return int
*/
public function getEnableMethodsNumber($store = null)
{
$methodsArray = [self::MAXMIND_GROUP, self::IPTOCOUNTRY_GROUP, self::IPAPI_GROUP];
$count = 0;
foreach ($methodsArray as $method) {
if ($this->getConfigForCurrentSection(
'methods/' . $method . '/enabled',
$store
)) {
$count++;
}
}
return $count;
}
}