Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Component/Utility/UrlHelper.php \Drupal\Component\Utility\UrlHelper::isExternal()
  2. 9 core/lib/Drupal/Component/Utility/UrlHelper.php \Drupal\Component\Utility\UrlHelper::isExternal()

Determines whether a path is external to Drupal.

An example of an external path is http://example.com. If a path cannot be assessed by Drupal's menu handler, then we must treat it as potentially insecure.

Parameters

string $path: The internal path or external URL being linked to, such as "node/34" or "http://example.com/foo".

Return value

bool TRUE or FALSE, where TRUE indicates an external path.

13 calls to UrlHelper::isExternal()
Endpoint::__construct in core/modules/media/src/OEmbed/Endpoint.php
Endpoint constructor.
FormBuilder::doBuildForm in core/lib/Drupal/Core/Form/FormBuilder.php
LocalAwareRedirectResponseTrait::isLocal in core/lib/Drupal/Core/Routing/LocalAwareRedirectResponseTrait.php
Determines whether a path is local.
MenuLinkParent::transform in core/modules/migrate/src/Plugin/migrate/process/MenuLinkParent.php
Find the parent link GUID.
PathValidator::getUrl in core/lib/Drupal/Core/Path/PathValidator.php
Helper for getUrlIfValid() and getUrlIfValidWithoutAccessCheck().

... See full list

File

core/lib/Drupal/Component/Utility/UrlHelper.php, line 269

Class

UrlHelper
Helper class URL based methods.

Namespace

Drupal\Component\Utility

Code

public static function isExternal($path) {
  $colon_position = strpos($path, ':');

  // Some browsers treat \ as / so normalize to forward slashes.
  $path = str_replace('\\', '/', $path);

  // If the path starts with 2 slashes then it is always considered an
  // external URL without an explicit protocol part.
  return str_starts_with($path, '//') || preg_match('/^\\p{C}/u', $path) !== 0 || $colon_position !== FALSE && !preg_match('![/?#]!', substr($path, 0, $colon_position)) && static::stripDangerousProtocols($path) == $path;
}