function system_file_download

Same name and namespace in other branches
  1. 7.x modules/system/system.module \system_file_download()
  2. 9 core/modules/system/system.module \system_file_download()

Implements hook_file_download().

File

core/modules/system/system.module, line 1322

Code

function system_file_download($uri) {
  $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
  $scheme = $stream_wrapper_manager->getScheme($uri);
  if ($stream_wrapper_manager->isValidScheme($scheme)) {
    $target = $stream_wrapper_manager->getTarget($uri);
    if ($target !== FALSE) {
      if (!in_array($scheme, Settings::get('file_sa_core_2023_005_schemes', []))) {
        if (DIRECTORY_SEPARATOR !== '/') {
          $class = $stream_wrapper_manager->getClass($scheme);
          if (is_subclass_of($class, LocalStream::class)) {
            $target = str_replace(DIRECTORY_SEPARATOR, '/', $target);
          }
        }
        $parts = explode('/', $target);
        if (array_intersect($parts, [
          '.',
          '..',
        ])) {
          return -1;
        }
      }
    }
  }
  $core_schemes = [
    'public',
    'private',
    'temporary',
  ];
  $additional_public_schemes = array_diff(Settings::get('file_additional_public_schemes', []), $core_schemes);
  if ($additional_public_schemes) {
    $scheme = StreamWrapperManager::getScheme($uri);
    if (in_array($scheme, $additional_public_schemes, TRUE)) {
      return [
        // Returning any header grants access, and setting the 'Cache-Control'
        // header is appropriate for public files.
'Cache-Control' => 'public',
      ];
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.