You are here

private function LinkRenderer::isExternalUrl in Markdown 3.0.x

Determines if a URL is external to current host.

Parameters

string $url: The URL to verify.

Return value

bool TRUE or FALSE

1 call to LinkRenderer::isExternalUrl()
LinkRenderer::render in src/Plugin/Markdown/Extension/LinkRenderer.php

File

src/Plugin/Markdown/Extension/LinkRenderer.php, line 151

Class

LinkRenderer
Plugin annotation @MarkdownExtension( id = "enhanced_links", label = @Translation("Enhanced Links"), installed = TRUE, description = @Translation("Extends CommonMark to provide additional enhancements when rendering links."), parsers =…

Namespace

Drupal\markdown\Plugin\Markdown\Extension

Code

private function isExternalUrl($url) {
  $url_host = parse_url($url, PHP_URL_HOST);

  // Only process URLs that actually have a host (e.g. not fragments).
  if (!isset($url_host) || empty($url_host)) {
    return FALSE;
  }

  // The environment can be reset, this too would be reset and would re-parse
  // the hosts again. Save some time during the same environment instance.
  static $hosts;

  // Parse the whitelist of internal hosts.
  if (!isset($hosts)) {
    $hosts = preg_split("/\r\n|\n/", $this
      ->getSetting('internal_host_whitelist'), -1, PREG_SPLIT_NO_EMPTY);

    // Ensure that the site's base url host name is always in this whitelist.
    $base_host = parse_url($GLOBALS['base_url'], PHP_URL_HOST);
    $key = array_search($base_host, $hosts);
    if ($key === FALSE) {
      $hosts[] = $base_host;
    }
  }

  // Iterate through the internal host whitelist.
  $internal = FALSE;
  foreach ($hosts as $host) {
    if ($host === $url_host) {
      $internal = TRUE;
      break;
    }
  }
  return !$internal;
}