Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. SystemHooks.php

function SystemHooks::fileDownload

Implements hook_file_download().

File

core/modules/system/src/Hook/SystemHooks.php, line 487

Class

SystemHooks
Hook implementations for system.

Namespace

Drupal\system\Hook

Code

public function fileDownload($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 [
                'Cache-Control' => 'public',
            ];
        }
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal