3 namespace Drupal\Component\Utility;
6 * Helper class URL based methods.
13 * The list of allowed protocols.
17 protected static $allowedProtocols = ['http', 'https'];
20 * Parses an array into a valid, rawurlencoded query string.
22 * rawurlencode() is RFC3986 compliant, and as a consequence RFC3987
23 * compliant. The latter defines the required format of "URLs" in HTML5.
24 * urlencode() is almost the same as rawurlencode(), except that it encodes
25 * spaces as "+" instead of "%20". This makes its result non compliant to
26 * RFC3986 and as a consequence non compliant to RFC3987 and as a consequence
27 * not valid as a "URL" in HTML5.
29 * @todo Remove this function once PHP 5.4 is required as we can use just
30 * http_build_query() directly.
33 * The query parameter array to be processed; for instance,
34 * \Drupal::request()->query->all().
35 * @param string $parent
36 * (optional) Internal use only. Used to build the $query array key for
37 * nested items. Defaults to an empty string.
40 * A rawurlencoded string which can be used as or appended to the URL query
43 * @ingroup php_wrappers
45 public static function buildQuery(array $query, $parent = '') {
48 foreach ($query as $key => $value) {
49 $key = ($parent ? $parent . rawurlencode('[' . $key . ']') : rawurlencode($key));
51 // Recurse into children.
52 if (is_array($value)) {
53 $params[] = static::buildQuery($value, $key);
55 // If a query parameter value is NULL, only append its key.
56 elseif (!isset($value)) {
60 // For better readability of paths in query strings, we decode slashes.
61 $params[] = $key . '=' . str_replace('%2F', '/', rawurlencode($value));
65 return implode('&', $params);
69 * Filters a URL query parameter array to remove unwanted elements.
72 * An array to be processed.
73 * @param array $exclude
74 * (optional) A list of $query array keys to remove. Use "parent[child]" to
75 * exclude nested items.
76 * @param string $parent
77 * Internal use only. Used to build the $query array key for nested items.
80 * An array containing query parameters.
82 public static function filterQueryParameters(array $query, array $exclude = [], $parent = '') {
83 // If $exclude is empty, there is nothing to filter.
84 if (empty($exclude)) {
88 $exclude = array_flip($exclude);
92 foreach ($query as $key => $value) {
93 $string_key = ($parent ? $parent . '[' . $key . ']' : $key);
94 if (isset($exclude[$string_key])) {
98 if (is_array($value)) {
99 $params[$key] = static::filterQueryParameters($value, $exclude, $string_key);
102 $params[$key] = $value;
110 * Parses a URL string into its path, query, and fragment components.
112 * This function splits both internal paths like @code node?b=c#d @endcode and
113 * external URLs like @code https://example.com/a?b=c#d @endcode into their
114 * component parts. See
115 * @link http://tools.ietf.org/html/rfc3986#section-3 RFC 3986 @endlink for an
116 * explanation of what the component parts are.
118 * Note that, unlike the RFC, when passed an external URL, this function
119 * groups the scheme, authority, and path together into the path component.
122 * The internal path or external URL string to parse.
125 * An associative array containing:
126 * - path: The path component of $url. If $url is an external URL, this
127 * includes the scheme, authority, and path.
128 * - query: An array of query parameters from $url, if they exist.
129 * - fragment: The fragment component from $url, if it exists.
131 * @see \Drupal\Core\Utility\LinkGenerator
132 * @see http://tools.ietf.org/html/rfc3986
134 * @ingroup php_wrappers
136 public static function parse($url) {
143 // External URLs: not using parse_url() here, so we do not have to rebuild
144 // the scheme, host, and path without having any use for it.
145 // The URL is considered external if it contains the '://' delimiter. Since
146 // a URL can also be passed as a query argument, we check if this delimiter
147 // appears in front of the '?' query argument delimiter.
148 $scheme_delimiter_position = strpos($url, '://');
149 $query_delimiter_position = strpos($url, '?');
150 if ($scheme_delimiter_position !== FALSE && ($query_delimiter_position === FALSE || $scheme_delimiter_position < $query_delimiter_position)) {
151 // Split off the fragment, if any.
152 if (strpos($url, '#') !== FALSE) {
153 list($url, $options['fragment']) = explode('#', $url, 2);
156 // Split off everything before the query string into 'path'.
157 $parts = explode('?', $url);
159 // Don't support URLs without a path, like 'http://'.
160 list(, $path) = explode('://', $parts[0], 2);
162 $options['path'] = $parts[0];
164 // If there is a query string, transform it into keyed query parameters.
165 if (isset($parts[1])) {
166 parse_str($parts[1], $options['query']);
171 // parse_url() does not support relative URLs, so make it absolute. For
172 // instance, the relative URL "foo/bar:1" isn't properly parsed.
173 $parts = parse_url('http://example.com/' . $url);
174 // Strip the leading slash that was just added.
175 $options['path'] = substr($parts['path'], 1);
176 if (isset($parts['query'])) {
177 parse_str($parts['query'], $options['query']);
179 if (isset($parts['fragment'])) {
180 $options['fragment'] = $parts['fragment'];
188 * Encodes a Drupal path for use in a URL.
190 * For aesthetic reasons slashes are not escaped.
192 * @param string $path
193 * The Drupal path to encode.
198 public static function encodePath($path) {
199 return str_replace('%2F', '/', rawurlencode($path));
203 * Determines whether a path is external to Drupal.
205 * An example of an external path is http://example.com. If a path cannot be
206 * assessed by Drupal's menu handler, then we must treat it as potentially
209 * @param string $path
210 * The internal path or external URL being linked to, such as "node/34" or
211 * "http://example.com/foo".
214 * TRUE or FALSE, where TRUE indicates an external path.
216 public static function isExternal($path) {
217 $colonpos = strpos($path, ':');
218 // Some browsers treat \ as / so normalize to forward slashes.
219 $path = str_replace('\\', '/', $path);
220 // If the path starts with 2 slashes then it is always considered an
221 // external URL without an explicit protocol part.
222 return (strpos($path, '//') === 0)
223 // Leading control characters may be ignored or mishandled by browsers,
224 // so assume such a path may lead to an external location. The \p{C}
225 // character class matches all UTF-8 control, unassigned, and private
227 || (preg_match('/^\p{C}/u', $path) !== 0)
228 // Avoid calling static::stripDangerousProtocols() if there is any slash
229 // (/), hash (#) or question_mark (?) before the colon (:) occurrence -
230 // if any - as this would clearly mean it is not a URL.
231 || ($colonpos !== FALSE
232 && !preg_match('![/?#]!', substr($path, 0, $colonpos))
233 && static::stripDangerousProtocols($path) == $path);
237 * Determines if an external URL points to this installation.
240 * A string containing an external URL, such as "http://example.com/foo".
241 * @param string $base_url
242 * The base URL string to check against, such as "http://example.com/"
245 * TRUE if the URL has the same domain and base path.
247 * @throws \InvalidArgumentException
248 * Exception thrown when a either $url or $bath_url are not fully qualified.
250 public static function externalIsLocal($url, $base_url) {
251 $url_parts = parse_url($url);
252 $base_parts = parse_url($base_url);
254 if (empty($base_parts['host']) || empty($url_parts['host'])) {
255 throw new \InvalidArgumentException('A path was passed when a fully qualified domain was expected.');
258 if (!isset($url_parts['path']) || !isset($base_parts['path'])) {
259 return (!isset($base_parts['path']) || $base_parts['path'] == '/')
260 && ($url_parts['host'] == $base_parts['host']);
263 // When comparing base paths, we need a trailing slash to make sure a
264 // partial URL match isn't occurring. Since base_path() always returns
265 // with a trailing slash, we don't need to add the trailing slash here.
266 return ($url_parts['host'] == $base_parts['host'] && stripos($url_parts['path'], $base_parts['path']) === 0);
271 * Processes an HTML attribute value and strips dangerous protocols from URLs.
273 * @param string $string
274 * The string with the attribute value.
277 * Cleaned up and HTML-escaped version of $string.
279 public static function filterBadProtocol($string) {
280 // Get the plain text representation of the attribute value (i.e. its
282 $string = Html::decodeEntities($string);
283 return Html::escape(static::stripDangerousProtocols($string));
287 * Gets the allowed protocols.
290 * An array of protocols, for example http, https and irc.
292 public static function getAllowedProtocols() {
293 return static::$allowedProtocols;
297 * Sets the allowed protocols.
299 * @param array $protocols
300 * An array of protocols, for example http, https and irc.
302 public static function setAllowedProtocols(array $protocols = []) {
303 static::$allowedProtocols = $protocols;
307 * Strips dangerous protocols (for example, 'javascript:') from a URI.
309 * This function must be called for all URIs within user-entered input prior
310 * to being output to an HTML attribute value. It is often called as part of
311 * \Drupal\Component\Utility\UrlHelper::filterBadProtocol() or
312 * \Drupal\Component\Utility\Xss::filter(), but those functions return an
313 * HTML-encoded string, so this function can be called independently when the
314 * output needs to be a plain-text string for passing to functions that will
315 * call Html::escape() separately. The exact behavior depends on the value:
316 * - If the value is a well-formed (per RFC 3986) relative URL or
317 * absolute URL that does not use a dangerous protocol (like
318 * "javascript:"), then the URL remains unchanged. This includes all
319 * URLs generated via Url::toString() and UrlGeneratorTrait::url().
320 * - If the value is a well-formed absolute URL with a dangerous protocol,
321 * the protocol is stripped. This process is repeated on the remaining URL
322 * until it is stripped down to a safe protocol.
323 * - If the value is not a well-formed URL, the same sanitization behavior as
324 * for well-formed URLs will be invoked, which strips most substrings that
325 * precede a ":". The result can be used in URL attributes such as "href"
326 * or "src" (only after calling Html::escape() separately), but this may not
327 * produce valid HTML (for example, malformed URLs within "href" attributes
328 * fail HTML validation). This can be avoided by using
329 * Url::fromUri($possibly_not_a_url)->toString(), which either throws an
330 * exception or returns a well-formed URL.
333 * A plain-text URI that might contain dangerous protocols.
336 * A plain-text URI stripped of dangerous protocols. As with all plain-text
337 * strings, this return value must not be output to an HTML page without
338 * being sanitized first. However, it can be passed to functions
339 * expecting plain-text strings.
341 * @see \Drupal\Component\Utility\Html::escape()
342 * @see \Drupal\Core\Url::toString()
343 * @see \Drupal\Core\Routing\UrlGeneratorTrait::url()
344 * @see \Drupal\Core\Url::fromUri()
346 public static function stripDangerousProtocols($uri) {
347 $allowed_protocols = array_flip(static::$allowedProtocols);
349 // Iteratively remove any invalid protocol found.
352 $colonpos = strpos($uri, ':');
354 // We found a colon, possibly a protocol. Verify.
355 $protocol = substr($uri, 0, $colonpos);
356 // If a colon is preceded by a slash, question mark or hash, it cannot
357 // possibly be part of the URL scheme. This must be a relative URL, which
358 // inherits the (safe) protocol of the base document.
359 if (preg_match('![/?#]!', $protocol)) {
362 // Check if this is a disallowed protocol. Per RFC2616, section 3.2.3
363 // (URI Comparison) scheme comparison must be case-insensitive.
364 if (!isset($allowed_protocols[strtolower($protocol)])) {
365 $uri = substr($uri, $colonpos + 1);
368 } while ($before != $uri);
374 * Verifies the syntax of the given URL.
376 * This function should only be used on actual URLs. It should not be used for
377 * Drupal menu paths, which can contain arbitrary characters.
378 * Valid values per RFC 3986.
382 * @param bool $absolute
383 * Whether the URL is absolute (beginning with a scheme such as "http:").
386 * TRUE if the URL is in a valid format, FALSE otherwise.
388 public static function isValid($url, $absolute = FALSE) {
390 return (bool) preg_match("
391 /^ # Start at the beginning of the text
392 (?:ftp|https?|feed):\/\/ # Look for ftp, http, https or feed schemes
393 (?: # Userinfo (optional) which is typically
394 (?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)* # a username or a username and password
395 (?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@ # combination
398 (?:[a-z0-9\-\.]|%[0-9a-f]{2})+ # A domain name or a IPv4 address
399 |(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\]) # or a well formed IPv6 address
401 (?::[0-9]+)? # Server port number (optional)
403 (?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2}) # The path and query (optional)
408 return (bool) preg_match("/^(?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})+$/i", $url);