Skip to content

Commit

Permalink
Fixed problem with country codes not being normalized to lowercase in…
Browse files Browse the repository at this point in the history
… config

Touches #25
  • Loading branch information
sjelfull committed Jun 16, 2020
1 parent 5187b6f commit ecff0fb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 37 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## [2.1.1] - 2020-06-16

### Fixed

- Fixed problem with country codes not being normalized to lowercase in config

## [2.1.0] - 2020-05-30

> {warning} You now have to register for a MaxMind account and obtain a license key in order to download the GeoLite2 databases. This is due to a change that was introduced December 30th 2019, [to comply with GDPR and CCPA](https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-geolite2-databases/). After upgrading to 2.1.0, you need to add the license key in the Country Redirect plugin settings to continue downloading the databases. Check the readme for detailed instructions on this.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "superbig/craft-countryredirect",
"description": "Easily redirect visitors to a locale based on their country of origin",
"type": "craft-plugin",
"version": "2.1.0",
"version": "2.1.1",
"keywords": [
"craft",
"cms",
Expand Down
75 changes: 39 additions & 36 deletions src/services/CountryRedirectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class CountryRedirectService extends Component
/** @var Settings $config */
protected $config;
protected $countryMap;
private $_matchedElement;
private $_matchedElementRoute;
private $_matchedElement;
private $_matchedElementRoute;

public function init()
{
Expand All @@ -60,7 +60,7 @@ public function init()
public function maybeRedirect()
{
// Get the site URL config setting
$enabled = $this->config->enabled;
$enabled = $this->config->enabled;
$ignoreSegments = $this->config->ignoreSegments;

if ($this->config->ignoreBots) {
Expand Down Expand Up @@ -114,7 +114,7 @@ public function maybeRedirect()
}
}

$countryCode = $this->getCountryCode();
$countryCode = $this->getCountryCode();
$countryLocale = $this->getSiteHandle($countryCode);

if ($url = $this->getRedirectUrl($countryLocale)) {
Expand All @@ -137,19 +137,19 @@ public function maybeRedirect()
public function getLinks()
{
// Get locales
$sites = Craft::$app->getSites()->getAllSites();
$links = [];
$sites = Craft::$app->getSites()->getAllSites();
$links = [];
$overrideLocaleParam = $this->getOverrideLocaleParam();

foreach ($sites as $site) {
/** @var Site $site */
$link = new Link();
$link = new Link();
$siteUrl = $site->baseUrl;

$link->siteName = $site->name;
$link->siteName = $site->name;
$link->siteHandle = $site->handle;
$link->url = rtrim($siteUrl, '?') . '?' . http_build_query([$overrideLocaleParam => '']);
$links[] = $link;
$link->url = rtrim($siteUrl, '?') . '?' . http_build_query([$overrideLocaleParam => '']);
$links[] = $link;
}

return $links;
Expand Down Expand Up @@ -178,14 +178,14 @@ public function wasOverridden()

public function getBanner($currentUrl = null, $currentSiteHandle = null)
{
$banner = null;
$banners = $this->config->banners;
$banner = null;
$banners = $this->config->banners;
$countryCode = $this->getCountryCode();
$siteHandle = $this->getSiteHandle($countryCode);
$info = $this->getInfo();
$siteHandle = $this->getSiteHandle($countryCode);
$info = $this->getInfo();
$redirectUrl = $this->getRedirectUrl($siteHandle, $currentUrl, $currentSiteHandle);
$site = Craft::$app->getSites()->getSiteByHandle($siteHandle);
$bannersLc = array_change_key_case($banners);
$site = Craft::$app->getSites()->getSiteByHandle($siteHandle);
$bannersLc = array_change_key_case($banners);

if ($matchesCountry = array_key_exists(strtolower($countryCode), $bannersLc)) {
$banner = $bannersLc[ strtolower($countryCode) ] ?? null;
Expand All @@ -196,26 +196,26 @@ public function getBanner($currentUrl = null, $currentSiteHandle = null)

if ($redirectUrl && $banner && !$this->getBannerCookie()) {
return new Banner([
'text' => $banner,
'url' => $this->appendBannerParamToUrl($redirectUrl),
'text' => $banner,
'url' => $this->appendBannerParamToUrl($redirectUrl),
'countryName' => $info ? $info->name : null,
'siteHandle' => $siteHandle,
'siteName' => $site->name ?? null,
'siteHandle' => $siteHandle,
'siteName' => $site->name ?? null,
]);
}
}

public function getInfo()
{
$ip = $this->getIpAddress();
$ip = $this->getIpAddress();
$info = $this->getInfoFromIp($ip);

return $info ? $info->country : null;
}

public function getCountryFromIpAddress()
{
$ip = $this->getIpAddress();
$ip = $this->getIpAddress();
$cacheKey = 'countryRedirect-ip-' . $ip;

if ($ip == '::1' || $ip == '127.0.0.1' || !$ip) {
Expand Down Expand Up @@ -291,14 +291,14 @@ public function getIpAddress()
*/
private function appendRedirectedParamToUrl($url = null)
{
$url = Craft::parseEnv($url);
$url = Craft::parseEnv($url);
$param = $this->config->redirectedParam;

if (!$param) {
return $url;
}

$query = $param . '=✓';
$query = $param . '=✓';
$parsedUrl = parse_url($url);

if (empty($parsedUrl['path'])) {
Expand All @@ -322,14 +322,14 @@ private function appendRedirectedParamToUrl($url = null)
*/
private function appendBannerParamToUrl($url = null)
{
$url = Craft::parseEnv($url);
$url = Craft::parseEnv($url);
$param = $this->config->bannerParam;

if (!$param) {
return $url;
}

$query = $param . '=✓';
$query = $param . '=✓';
$parsedUrl = parse_url($url);

if (empty($parsedUrl['path'])) {
Expand Down Expand Up @@ -456,7 +456,7 @@ private function _getMatchedElementRoute(string $path)
return $this->_matchedElementRoute;
}

$this->_matchedElement = false;
$this->_matchedElement = false;
$this->_matchedElementRoute = false;


Expand All @@ -475,16 +475,16 @@ private function _getMatchedElementRoute(string $path)
$route = [$route, []];
}

$this->_matchedElement = $element;
$this->_matchedElement = $element;
$this->_matchedElementRoute = $route;
}
}
}

if (YII_DEBUG) {
Craft::debug([
'rule' => 'Element URI: ' . $path,
'match' => isset($element, $route),
'rule' => 'Element URI: ' . $path,
'match' => isset($element, $route),
'parent' => null,
], __METHOD__);
}
Expand Down Expand Up @@ -536,7 +536,7 @@ private function getCountryMap()
return false;
}

return $this->countryMap;
return array_change_key_case($this->countryMap);
}

/**
Expand All @@ -545,14 +545,14 @@ private function getCountryMap()
private function getCountryCode()
{
$currentLocale = Craft::$app->getSites()->currentSite->handle;
$countryCode = null;
$countryCode = null;

// Get country map
$countryMap = $this->getCountryMap();

// Get selected country from GET
$overrideLocaleParam = $this->getOverrideLocaleParam();
$overrideLocale = Craft::$app->getRequest()->getParam($overrideLocaleParam);
$overrideLocale = Craft::$app->getRequest()->getParam($overrideLocaleParam);

if ($overrideLocale) {
// The selected country could be both key and value, so check for both
Expand Down Expand Up @@ -583,7 +583,7 @@ private function getCountryCode()
private function getSiteHandle($countryCode)
{
// Get country map
$countryMap = $this->getCountryMap();
$countryMap = $this->getCountryMap();
$countryCode = $this->_normalizeCountryCode($countryCode);

// Get country locale
Expand All @@ -592,11 +592,14 @@ private function getSiteHandle($countryCode)

// If country locale is array, it's a list of regional languages
if (is_array($countryLocale)) {
$countryLocaleLc = array_change_key_case($countryLocale);
$browseLanguageCodes = $this->getBrowserLanguages();

foreach ($browseLanguageCodes as $blCode) {
if (isset($countryLocale[ $blCode ])) {
return $countryLocale[ $blCode ];
$blCode = $this->_normalizeCountryCode($blCode);

if (isset($countryLocaleLc[ $blCode ])) {
return $countryLocaleLc[ $blCode ];
}
}
}
Expand Down

0 comments on commit ecff0fb

Please sign in to comment.