function SystemHooks::fileDownload

Implements hook_file_download().

File

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

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',
            ];
        }
    }
}

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