1 file.inc file_uri_normalize_dot_segments($uri)

Normalize dot segments in a URI.

Parameters

$uri: A stream, referenced as "scheme://target".

Return value

string: The URI with dot segments removed and slashes as directory separator.

Related topics

File

core/includes/file.inc, line 2818
API for handling file uploads and server file management.

Code

function file_uri_normalize_dot_segments($uri) {
  $scheme = file_uri_scheme($uri);

  if (file_stream_wrapper_valid_scheme($scheme)) {
    $target = file_uri_target($uri);

    if ($target !== FALSE) {
      $skip_schemes = config_get('system.core', 'file_not_normalized_schemes');
      if (empty($skip_schemes) || !in_array($scheme, $skip_schemes)) {
        // URIs should always use '/' for separating directories in paths.
        $class = file_stream_wrapper_get_class($scheme);
        if (is_subclass_of($class, BackdropLocalStreamWrapper::)) {
          $target = str_replace(DIRECTORY_SEPARATOR, '/', $target);

          // Remove '.' and '..' from the path.
          $parts = explode('/', $target);
          $normalized = array();
          foreach ($parts as $part) {
            if ($part === '' || $part === '.') {
              continue;
            }
            elseif ($part === '..') {
              array_pop($normalized);
            }
            else {
              $normalized[] = $part;
            }
          }

          $target = implode('/', $normalized);
        }

        $uri = $scheme . '://' . $target;
      }
    }
  }

  return $uri;
}