Skip to content

Commit

Permalink
Merge pull request #48 from v17development/dev
Browse files Browse the repository at this point in the history
Better domain host determination
  • Loading branch information
jaspervriends authored Feb 17, 2021
2 parents 4ad3999 + 9409c5b commit cc95ed3
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/Formatter/FormatLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public function __construct(Application $app, SettingsRepositoryInterface $setti
$this->settings = $settings;

// Current forum domain
$this->internalDomain = parse_url($this->app->url());
$this->internalDomain = $this->urlToDomain($this->app->url());

// Define list of domain URLs that are allowed to follow
$this->doFollowList = array_merge($this->getDoFollowList(), [$this->app->url()]);
$this->doFollowList = array_merge($this->getDoFollowList(), [$this->internalDomain]);
}

/**
Expand All @@ -56,13 +56,13 @@ public function __construct(Application $app, SettingsRepositoryInterface $setti
public function __invoke(Renderer $renderer, $context, $xml, Request $request = null)
{
return Utils::replaceAttributes($xml, 'URL', function ($attributes) {
$url = parse_url($attributes['url']);
$domain = $this->urlToDomain($attributes['url']);

// Do we add a nofollow?
$attributes['rel'] = "ugc noopener" . ($this->addNofollow($url) ? " nofollow" : "");
$attributes['rel'] = "ugc noopener" . ($this->addNofollow($domain) ? " nofollow" : "");

// Open link in new tab
$attributes['target'] = $this->openInNewTab($url) ? "_blank" : "_self";
$attributes['target'] = $this->openInNewTab($domain) ? "_blank" : "_self";

return $attributes;
});
Expand All @@ -71,19 +71,19 @@ public function __invoke(Renderer $renderer, $context, $xml, Request $request =
/**
* Do we need to add a nofollow to this link?
*
* @param array $url
* @param string $domain
*/
private function addNofollow(array $url) {
return !isset($url['host']) || !in_array($url['host'], $this->doFollowList);
private function addNofollow(string $domain) {
return !isset($domain) || !in_array($domain, $this->doFollowList);
}

/**
* Is the link an internal link?
*
* @param array $url
* @param string $domain
*/
private function openInNewTab(array $url) {
return !isset($url['host']) || $this->internalDomain['host'] != $url['host'];
private function openInNewTab(string $domain) {
return !isset($domain) || $this->internalDomain != $domain;
}

/**
Expand All @@ -93,4 +93,20 @@ public function getDoFollowList()
{
return json_decode($this->settings->get("seo_dofollow_domains", []), true) ?? [];
}

/**
* Get domain (and strip subdomains, if any)
*/
private function urlToDomain($url) {
// Parse URL
$url = parse_url($url);

// Invalid URL
if(!isset($url['host'])) {
return '';
}

// Strip subdomains
return implode('.', array_slice(explode(".", $url['host']), -2, 2, true));
}
}

0 comments on commit cc95ed3

Please sign in to comment.