You are here

public static function DomainRedirectResponse::externalIsRegistered in Domain Access 8

Determines if an external URL points to this domain-aware installation.

This method replaces the logic in Drupal\Component\Utility\UrlHelper::externalIsLocal(). Since that class is not directly extendable, we have to replace it.

Parameters

string $url: A string containing an external URL, such as "http://example.com/foo".

string $base_url: The base URL string to check against, such as "http://example.com/".

Return value

bool TRUE if the URL has the same domain and base path.

Throws

\InvalidArgumentException Exception thrown when $url is not fully qualified.

1 call to DomainRedirectResponse::externalIsRegistered()
DomainRedirectResponse::isLocal in domain/src/DomainRedirectResponse.php

File

domain/src/DomainRedirectResponse.php, line 103

Class

DomainRedirectResponse
A redirect response which understands domain URLs are local to the install.

Namespace

Drupal\domain

Code

public static function externalIsRegistered($url, $base_url) {
  $url_parts = parse_url($url);
  $base_parts = parse_url($base_url);
  if (empty($url_parts['host'])) {
    throw new \InvalidArgumentException('A path was passed when a fully qualified domain was expected.');
  }

  // Check that the host name is registered with trusted hosts.
  $trusted = self::checkTrustedHost($url_parts['host']);
  if (!$trusted) {
    return FALSE;
  }

  // Check that the requested $url is registered.
  $negotiator = \Drupal::service('domain.negotiator');
  $registered_domain = $negotiator
    ->isRegisteredDomain($url_parts['host']);
  if (!isset($url_parts['path']) || !isset($base_parts['path'])) {
    return $registered_domain;
  }
  else {

    // When comparing base paths, we need a trailing slash to make sure a
    // partial URL match isn't occurring. Since base_path() always returns
    // with a trailing slash, we don't need to add the trailing slash here.
    return $registered_domain && stripos($url_parts['path'], $base_parts['path']) === 0;
  }
}