W3cubDocs

/Drupal 8

public static function UrlHelper::isExternal

public static UrlHelper::isExternal($path)

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.

File

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

Class

UrlHelper
Helper class URL based methods.

Namespace

Drupal\Component\Utility

Code

public static function isExternal($path) {
  $colonpos = 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 (strpos($path, '//') === 0)
    // Leading control characters may be ignored or mishandled by browsers,
    // so assume such a path may lead to an external location. The \p{C}
    // character class matches all UTF-8 control, unassigned, and private
    // characters.
    || (preg_match('/^\p{C}/u', $path) !== 0)
    // Avoid calling static::stripDangerousProtocols() if there is any slash
    // (/), hash (#) or question_mark (?) before the colon (:) occurrence -
    // if any - as this would clearly mean it is not a URL.
    || ($colonpos !== FALSE
    && !preg_match('![/?#]!', substr($path, 0, $colonpos))
    && static::stripDangerousProtocols($path) == $path);
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Component!Utility!UrlHelper.php/function/UrlHelper::isExternal/8.1.x