Commit e08ec139 authored by alexpott's avatar alexpott

Issue #3034072 by kim.pepper, Berdir, alexpott, jibran, larowlan,...

Issue #3034072 by kim.pepper, Berdir, alexpott, jibran, larowlan, claudiu.cristea: Move file uri/scheme functions from file.inc and FileSystem to StreamWrapperManager
parent d2792e13
......@@ -1599,7 +1599,7 @@ services:
- { name: needs_destruction }
library.discovery.parser:
class: Drupal\Core\Asset\LibraryDiscoveryParser
arguments: ['@app.root', '@module_handler', '@theme.manager']
arguments: ['@app.root', '@module_handler', '@theme.manager', '@stream_wrapper_manager']
library.dependency_resolver:
class: Drupal\Core\Asset\LibraryDependencyResolver
arguments: ['@library.discovery']
......
......@@ -16,6 +16,7 @@
use Drupal\Core\Site\Settings;
use Drupal\Core\StreamWrapper\PrivateStream;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
/**
* Default mode for new directories.
......@@ -98,25 +99,29 @@
/**
* Returns the scheme of a URI (e.g. a stream).
*
* @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
* Use \Drupal\Core\File\FileSystem::uriScheme().
* @deprecated in drupal:8.8.0 and will be removed from drupal:9.0.0. Use
* Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::getScheme()
* instead.
*
* @see https://www.drupal.org/node/2418133
* @see https://www.drupal.org/node/3035273
*/
function file_uri_scheme($uri) {
return \Drupal::service('file_system')->uriScheme($uri);
@trigger_error('file_uri_scheme() is deprecated in drupal:8.0.0 and will be removed before drupal:9.0.0. Use \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::getScheme() instead. See https://www.drupal.org/node/3035273', E_USER_DEPRECATED);
return StreamWrapperManager::getScheme($uri);
}
/**
* Checks that the scheme of a stream URI is valid.
*
* @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
* Use \Drupal\Core\File\FileSystem::validScheme().
* @deprecated in drupal:8.8.0 and will be removed from Drupal 9.0.0. Use
* Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::isValidScheme()
* instead.
*
* @see https://www.drupal.org/node/2418133
* @see https://www.drupal.org/node/3035273
*/
function file_stream_wrapper_valid_scheme($scheme) {
return \Drupal::service('file_system')->validScheme($scheme);
@trigger_error('file_stream_wrapper_valid_scheme() is deprecated in drupal:8.0.0 and will be removed before drupal:9.0.0. Use \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::isValidScheme() instead. See https://www.drupal.org/node/3035273', E_USER_DEPRECATED);
return \Drupal::service('stream_wrapper_manager')->isValidScheme($scheme);
}
/**
......@@ -130,15 +135,15 @@ function file_stream_wrapper_valid_scheme($scheme) {
* For example, the URI "public://sample/test.txt" would return
* "sample/test.txt".
*
* @see file_uri_scheme()
* @deprecated in drupal:8.8.0 and will be removed from drupal:9.0.0. Use
* \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::getTarget()
* instead.
*
* @see https://www.drupal.org/node/3035273
*/
function file_uri_target($uri) {
// Remove the scheme from the URI and remove erroneous leading or trailing,
// forward-slashes and backslashes.
$target = trim(preg_replace('/^[\w\-]+:\/\/|^data:/', '', $uri), '\/');
// If nothing was replaced, the URI doesn't have a valid scheme.
return $target !== $uri ? $target : FALSE;
@trigger_error('file_uri_target() is deprecated in drupal:8.8.0 and will be removed before drupal:9.0.0. Use \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::getTarget() instead. See https://www.drupal.org/node/3035273', E_USER_DEPRECATED);
return StreamWrapperManager::getTarget($uri);
}
/**
......@@ -171,19 +176,16 @@ function file_default_scheme() {
*
* @return string
* The normalized URI.
*
* @deprecated in drupal:8.8.0 and will be removed from drupal:9.0.0. Use
* \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::normalizeUri()
* instead.
*
* @see https://www.drupal.org/node/3035273
*/
function file_stream_wrapper_uri_normalize($uri) {
$scheme = \Drupal::service('file_system')->uriScheme($uri);
if (file_stream_wrapper_valid_scheme($scheme)) {
$target = file_uri_target($uri);
if ($target !== FALSE) {
$uri = $scheme . '://' . $target;
}
}
return $uri;
@trigger_error('file_stream_wrapper_uri_normalize() is deprecated in drupal:8.8.0 and will be removed before drupal:9.0.0. Use \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::normalizeUri() instead. See https://www.drupal.org/node/3035273', E_USER_DEPRECATED);
return \Drupal::service('stream_wrapper_manager')->normalizeUri($uri);
}
/**
......@@ -216,7 +218,7 @@ function file_create_url($uri) {
// file server.
\Drupal::moduleHandler()->alter('file_url', $uri);
$scheme = \Drupal::service('file_system')->uriScheme($uri);
$scheme = StreamWrapperManager::getScheme($uri);
if (!$scheme) {
// Allow for:
......@@ -364,8 +366,12 @@ function file_ensure_htaccess() {
* if one is already present. Defaults to FALSE.
*/
function file_save_htaccess($directory, $private = TRUE, $force_overwrite = FALSE) {
if (\Drupal::service('file_system')->uriScheme($directory)) {
$htaccess_path = file_stream_wrapper_uri_normalize($directory . '/.htaccess');
/** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
if ($stream_wrapper_manager::getScheme($directory)) {
$htaccess_path = $stream_wrapper_manager->normalizeUri($directory . '/.htaccess');
}
else {
$directory = rtrim($directory, '/\\');
......@@ -421,14 +427,16 @@ function file_htaccess_lines($private = TRUE) {
*
* @return
* TRUE if the URI is allowed.
*
* @deprecated in drupal:8.8.0 and will be removed before drupal:9.0.0. Use
* \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::isValidUri()
* instead.
*
* @see https://www.drupal.org/node/3035273
*/
function file_valid_uri($uri) {
// Assert that the URI has an allowed scheme. Bare paths are not allowed.
$uri_scheme = \Drupal::service('file_system')->uriScheme($uri);
if (!file_stream_wrapper_valid_scheme($uri_scheme)) {
return FALSE;
}
return TRUE;
@trigger_error('file_valid_uri() is deprecated in drupal:8.8.0 and will be removed before drupal:9.0.0. Use \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::isValidUri() instead. See https://www.drupal.org/node/3035273', E_USER_DEPRECATED);
return \Drupal::service('stream_wrapper_manager')->isValidUri($uri);
}
/**
......@@ -586,7 +594,9 @@ function file_unmanaged_prepare($source, &$destination = NULL, $replace = FILE_E
*/
function file_build_uri($path) {
$uri = \Drupal::config('system.file')->get('default_scheme') . '://' . $path;
return file_stream_wrapper_uri_normalize($uri);
/** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
return $stream_wrapper_manager->normalizeUri($uri);
}
/**
......@@ -1001,7 +1011,9 @@ function file_scan_directory($dir, $mask, $options = [], $depth = 0) {
];
// Normalize $dir only once.
if ($depth == 0) {
$dir = file_stream_wrapper_uri_normalize($dir);
/** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
$dir = $stream_wrapper_manager->normalizeUri($dir);
$dir_has_slash = (substr($dir, -1) === '/');
}
......
......@@ -3,6 +3,7 @@
namespace Drupal\Core\Asset;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
/**
* Optimizes a CSS asset.
......@@ -103,7 +104,7 @@ public function loadFile($file, $optimize = NULL, $reset_basepath = TRUE) {
// Stylesheets are relative one to each other. Start by adding a base path
// prefix provided by the parent stylesheet (if necessary).
if ($basepath && !file_uri_scheme($file)) {
if ($basepath && !StreamWrapperManager::getScheme($file)) {
$file = $basepath . '/' . $file;
}
// Store the parent base path to restore it later.
......
......@@ -8,6 +8,7 @@
use Drupal\Core\Asset\Exception\LibraryDefinitionMissingLicenseException;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Drupal\Component\Utility\NestedArray;
......@@ -38,6 +39,13 @@ class LibraryDiscoveryParser {
*/
protected $root;
/**
* The stream wrapper manager.
*
* @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
*/
protected $streamWrapperManager;
/**
* Constructs a new LibraryDiscoveryParser instance.
*
......@@ -47,11 +55,18 @@ class LibraryDiscoveryParser {
* The module handler.
* @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
* The theme manager.
* @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
* The stream wrapper manager.
*/
public function __construct($root, ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager) {
public function __construct($root, ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager, StreamWrapperManagerInterface $stream_wrapper_manager = NULL) {
$this->root = $root;
$this->moduleHandler = $module_handler;
$this->themeManager = $theme_manager;
if (!$stream_wrapper_manager) {
@trigger_error('Calling LibraryDiscoveryParser::__construct() without the $stream_wrapper_manager argument is deprecated in drupal:8.8.0. The $stream_wrapper_manager argument will be required in drupal:9.0.0. See https://www.drupal.org/node/3035273', E_USER_DEPRECATED);
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
}
$this->streamWrapperManager = $stream_wrapper_manager;
}
/**
......@@ -183,7 +198,7 @@ public function buildByExtension($extension) {
}
}
// A stream wrapper URI (e.g., public://generated_js/example.js).
elseif ($this->fileValidUri($source)) {
elseif ($this->streamWrapperManager->isValidUri($source)) {
$options['data'] = $source;
}
// A regular URI (e.g., http://example.com/example.js) without
......@@ -393,10 +408,15 @@ protected function drupalGetPath($type, $name) {
}
/**
* Wraps file_valid_uri().
* Wraps \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::isValidUri().
*
* @deprecated in drupal:8.8.0 and will be removed before drupal:9.0.0. Use
* \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::isValidUri()
* instead.
*/
protected function fileValidUri($source) {
return file_valid_uri($source);
@trigger_error('fileValidUri() is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::isValidUri() instead. See https://www.drupal.org/node/3035273', E_USER_DEPRECATED);
return $this->streamWrapperManager->isValidUri($source);
}
/**
......
......@@ -10,6 +10,7 @@
use Drupal\Core\File\Exception\FileWriteException;
use Drupal\Core\File\Exception\NotRegularFileException;
use Drupal\Core\Site\Settings;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Psr\Log\LoggerInterface;
......@@ -110,8 +111,7 @@ public function chmod($uri, $mode = NULL) {
* {@inheritdoc}
*/
public function unlink($uri, $context = NULL) {
$scheme = $this->uriScheme($uri);
if (!$this->validScheme($scheme) && (substr(PHP_OS, 0, 3) == 'WIN')) {
if (!$this->streamWrapperManager->isValidUri($uri) && (substr(PHP_OS, 0, 3) == 'WIN')) {
chmod($uri, 0600);
}
if ($context) {
......@@ -140,9 +140,9 @@ public function realpath($uri) {
* {@inheritdoc}
*/
public function dirname($uri) {
$scheme = $this->uriScheme($uri);
$scheme = StreamWrapperManager::getScheme($uri);
if ($this->validScheme($scheme)) {
if ($this->streamWrapperManager->isValidScheme($scheme)) {
return $this->streamWrapperManager->getViaScheme($scheme)->dirname($uri);
}
else {
......@@ -181,7 +181,7 @@ public function mkdir($uri, $mode = NULL, $recursive = FALSE, $context = NULL) {
// If the URI has a scheme, don't override the umask - schemes can handle
// this issue in their own implementation.
if ($this->uriScheme($uri)) {
if (StreamWrapperManager::getScheme($uri)) {
return $this->mkdirCall($uri, $mode, $recursive, $context);
}
......@@ -252,8 +252,7 @@ protected function mkdirCall($uri, $mode, $recursive, $context) {
* {@inheritdoc}
*/
public function rmdir($uri, $context = NULL) {
$scheme = $this->uriScheme($uri);
if (!$this->validScheme($scheme) && (substr(PHP_OS, 0, 3) == 'WIN')) {
if (!$this->streamWrapperManager->isValidUri($uri) && (substr(PHP_OS, 0, 3) == 'WIN')) {
chmod($uri, 0700);
}
if ($context) {
......@@ -268,9 +267,9 @@ public function rmdir($uri, $context = NULL) {
* {@inheritdoc}
*/
public function tempnam($directory, $prefix) {
$scheme = $this->uriScheme($directory);
$scheme = StreamWrapperManager::getScheme($directory);
if ($this->validScheme($scheme)) {
if ($this->streamWrapperManager->isValidScheme($scheme)) {
$wrapper = $this->streamWrapperManager->getViaScheme($scheme);
if ($filename = tempnam($wrapper->getDirectoryPath(), $prefix)) {
......@@ -290,22 +289,16 @@ public function tempnam($directory, $prefix) {
* {@inheritdoc}
*/
public function uriScheme($uri) {
if (preg_match('/^([\w\-]+):\/\/|^(data):/', $uri, $matches)) {
// The scheme will always be the last element in the matches array.
return array_pop($matches);
}
return FALSE;
@trigger_error('FileSystem::uriScheme() is deprecated in drupal:8.8.0. It will be removed from drupal:9.0.0. Use \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::getScheme() instead. See https://www.drupal.org/node/3035273', E_USER_DEPRECATED);
return StreamWrapperManager::getScheme($uri);
}
/**
* {@inheritdoc}
*/
public function validScheme($scheme) {
if (!$scheme) {
return FALSE;
}
return class_exists($this->streamWrapperManager->getClass($scheme));
@trigger_error('FileSystem::validScheme() is deprecated in drupal:8.8.0 and will be removed before drupal:9.0.0. Use \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::isValidScheme() instead. See https://www.drupal.org/node/3035273', E_USER_DEPRECATED);
return $this->streamWrapperManager->isValidScheme($scheme);
}
/**
......@@ -397,8 +390,7 @@ public function move($source, $destination, $replace = self::EXISTS_RENAME) {
// Ensure compatibility with Windows.
// @see \Drupal\Core\File\FileSystemInterface::unlink().
$scheme = $this->uriScheme($source);
if (!$this->validScheme($scheme) && (substr(PHP_OS, 0, 3) == 'WIN')) {
if (!$this->streamWrapperManager->isValidUri($source) && (substr(PHP_OS, 0, 3) == 'WIN')) {
chmod($source, 0600);
}
// Attempt to resolve the URIs. This is necessary in certain
......@@ -481,7 +473,7 @@ protected function prepareDestination($source, &$destination, $replace) {
// Prepare the destination directory.
if ($this->prepareDirectory($destination)) {
// The destination is already a directory, so append the source basename.
$destination = file_stream_wrapper_uri_normalize($destination . '/' . $this->basename($source));
$destination = $this->streamWrapperManager->normalizeUri($destination . '/' . $this->basename($source));
}
else {
// Perhaps $destination is a dir/file?
......@@ -537,7 +529,7 @@ public function saveData($data, $destination, $replace = self::EXISTS_RENAME) {
* {@inheritdoc}
*/
public function prepareDirectory(&$directory, $options = self::MODIFY_PERMISSIONS) {
if (!$this->validScheme($this->uriScheme($directory))) {
if (!$this->streamWrapperManager->isValidUri($directory)) {
// Only trim if we're not dealing with a stream.
$directory = rtrim($directory, '/\\');
}
......
......@@ -242,7 +242,11 @@ public function tempnam($directory, $prefix);
* A string containing the name of the scheme, or FALSE if none. For
* example, the URI "public://example.txt" would return "public".
*
* @see file_uri_target()
* @deprecated in drupal:8.8.0 and will be removed from drupal:9.0.0. Use
* Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::getScheme()
* instead.
*
* @see https://www.drupal.org/node/3035273
*/
public function uriScheme($uri);
......@@ -259,6 +263,12 @@ public function uriScheme($uri);
* @return bool
* Returns TRUE if the string is the name of a validated stream, or FALSE if
* the scheme does not have a registered handler.
*
* @deprecated in drupal:8.0.0 and will be removed before Drupal 9.0.0. Use
* Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::isValidScheme()
* instead.
*
* @see https://www.drupal.org/node/3035273
*/
public function validScheme($scheme);
......
......@@ -5,6 +5,8 @@
* Hooks related to the File management system.
*/
use Drupal\Core\StreamWrapper\StreamWrapperManager;
/**
* @addtogroup hooks
* @{
......@@ -30,8 +32,8 @@
*/
function hook_file_download($uri) {
// Check to see if this is a config download.
$scheme = file_uri_scheme($uri);
$target = file_uri_target($uri);
$scheme = StreamWrapperManager::getScheme($uri);
$target = StreamWrapperManager::getTarget($uri);
if ($scheme == 'temporary' && $target == 'config.tar.gz') {
return [
'Content-disposition' => 'attachment; filename="config.tar.gz"',
......@@ -70,7 +72,10 @@ function hook_file_url_alter(&$uri) {
// so don't support this in the common case.
$schemes = ['public'];
$scheme = file_uri_scheme($uri);
/** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
$scheme = $stream_wrapper_manager::getScheme($uri);
// Only serve shipped files and public created files from the CDN.
if (!$scheme || in_array($scheme, $schemes)) {
......@@ -80,8 +85,8 @@ function hook_file_url_alter(&$uri) {
}
// Public created files.
else {
$wrapper = \Drupal::service('stream_wrapper_manager')->getViaScheme($scheme);
$path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri);
$wrapper = $stream_wrapper_manager->getViaScheme($scheme);
$path = $wrapper->getDirectoryPath() . '/' . $stream_wrapper_manager::getTarget($uri);
}
// Clean up Windows paths.
......
......@@ -100,7 +100,7 @@ public function getViaScheme($scheme) {
* {@inheritdoc}
*/
public function getViaUri($uri) {
$scheme = file_uri_scheme($uri);
$scheme = static::getScheme($uri);
return $this->getWrapper($scheme, $uri);
}
......@@ -208,4 +208,75 @@ public function registerWrapper($scheme, $class, $type) {
}
}
/**
* {@inheritdoc}
*/
public static function getTarget($uri) {
// Remove the scheme from the URI and remove erroneous leading or trailing,
// forward-slashes and backslashes.
$target = trim(preg_replace('/^[\w\-]+:\/\/|^data:/', '', $uri), '\/');
// If nothing was replaced, the URI doesn't have a valid scheme.
return $target !== $uri ? $target : FALSE;
}
/**
* Normalizes a URI by making it syntactically correct.
*
* A stream is referenced as "scheme://target".
*
* The following actions are taken:
* - Remove trailing slashes from target
* - Trim erroneous leading slashes from target. e.g. ":///" becomes "://".
*
* @param string $uri
* String reference containing the URI to normalize.
*
* @return string
* The normalized URI.
*/
public function normalizeUri($uri) {
$scheme = $this->getScheme($uri);
if ($this->isValidScheme($scheme)) {
$target = $this->getTarget($uri);
if ($target !== FALSE) {
$uri = $scheme . '://' . $target;
}
}
return $uri;
}
/**
* {@inheritdoc}
*/
public static function getScheme($uri) {
if (preg_match('/^([\w\-]+):\/\/|^(data):/', $uri, $matches)) {
// The scheme will always be the last element in the matches array.
return array_pop($matches);
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function isValidScheme($scheme) {
if (!$scheme) {
return FALSE;
}
return class_exists($this->getClass($scheme));
}
/**
* {@inheritdoc}
*/
public function isValidUri($uri) {
// Assert that the URI has an allowed scheme. Bare paths are not allowed.
return $this->isValidScheme($this->getScheme($uri));
}
}
......@@ -169,4 +169,81 @@ public function getClass($scheme);
*/
public function registerWrapper($scheme, $class, $type);
/**
* Returns the part of a URI after the schema.
*
* @param string $uri
* A stream, referenced as "scheme://target" or "data:target".
*
* @return string|bool
* A string containing the target (path), or FALSE if none.
* For example, the URI "public://sample/test.txt" would return
* "sample/test.txt".
*
* @see \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::getScheme()
*/
public static function getTarget($uri);
/**
* Normalizes a URI by making it syntactically correct.
*
* A stream is referenced as "scheme://target".
*
* The following actions are taken:
* - Remove trailing slashes from target
* - Trim erroneous leading slashes from target. e.g. ":///" becomes "://".
*
* @param string $uri
* String reference containing the URI to normalize.
*
* @return string
* The normalized URI.
*/
public function normalizeUri($uri);
/**
* Returns the scheme of a URI (e.g. a stream).
*
* @param string $uri
* A stream, referenced as "scheme://target" or "data:target".
*
* @return string|bool
* A string containing the name of the scheme, or FALSE if none. For
* example, the URI "public://example.txt" would return "public".
*
* @see \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface::getTarget()
*/
public static function getScheme($uri);
/**
* Checks that the scheme of a stream URI is valid.
*
* Confirms that there is a registered stream handler for the provided scheme
* and that it is callable. This is useful if you want to confirm a valid
* scheme without creating a new instance of the registered handler.
*
* @param string $scheme
* A URI scheme, a stream is referenced as "scheme://target".
*
* @return bool
* Returns TRUE if the string is the name of a validated stream, or FALSE if
* the scheme does not have a registered handler.
*/
public function isValidScheme($scheme);
/**
* Determines whether the URI has a valid scheme for file API operations.
*
* There must be a scheme and it must be a Drupal-provided scheme like
* 'public', 'private', 'temporary', or an extension provided with
* hook_stream_wrappers().
*
* @param string $uri
* The URI to be tested.
*
* @return bool
* TRUE if the URI is valid.
*/
public function isValidUri($uri);
}
......@@ -5,8 +5,9 @@
* Allows site administrators to modify configuration.
*/
use Drupal\Core\Url;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Drupal\Core\Url;
/**
* Implements hook_help().
......@@ -63,8 +64,8 @@ function config_help($route_name, RouteMatchInterface $route_match) {
* Implements hook_file_download().
*/
function config_file_download($uri) {
$scheme = file_uri_scheme($uri);
$target = file_uri_target($uri);
$scheme = StreamWrapperManager::getScheme($uri);
$target = StreamWrapperManager::getTarget($uri);
if ($scheme == 'temporary' && $target == 'config.tar.gz') {
if (\Drupal::currentUser()->hasPermission('export configuration')) {
$request = \Drupal::request();
......
......@@ -77,7 +77,7 @@ public static function create(ContainerInterface $container) {
$container->get('config.storage'),
$container->get('config.storage.sync'),
$container->get('config.manager'),
new FileDownloadController(),
FileDownloadController::create($container),
$container->get('diff.formatter'),
$container->get('file_system'),
$container->get('config.storage.export')
......
......@@ -157,7 +157,10 @@ function file_load($fid, $reset = FALSE) {
function file_copy(FileInterface $source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
if (!file_valid_uri($destination)) {
/** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
if (!$stream_wrapper_manager->isValidUri($destination)) {
if (($realpath = $file_system->realpath($source->getFileUri())) !== FALSE) {
\Drupal::logger('file')->notice('File %file (%realpath) could not be copied because the destination %destination is invalid. This is often caused by improper use of file_copy() or a missing stream wrapper.', ['%file' => $source->getFileUri(), '%realpath' => $realpath, '%destination' => $destination]);
}
......@@ -237,7 +240,10 @@ function file_copy(FileInterface $source, $destination = NULL, $replace = FILE_E
function file_move(FileInterface $source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
if (!file_valid_uri($destination)) {
/** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
if (!$stream_wrapper_manager->isValidUri($destination)) {
if (($realpath = $file_system->realpath($source->getFileUri())) !== FALSE) {
\Drupal::logger('file')->notice('File %file (%realpath) could not be moved because the destination %destination is invalid. This may be caused by improper use of file_move() or a missing stream wrapper.', ['%file' => $source->getFileUri(), '%realpath' => $realpath, '%destination' => $destination]);
}
......@@ -561,7 +567,10 @@ function file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAM
if (empty($destination)) {
$destination = \Drupal::config('system.file')->get('default_scheme') . '://';
}
if (!file_valid_uri($destination)) {
/** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
if (!$stream_wrapper_manager->isValidUri($destination)) {
\Drupal::logger('file')->notice('The data could not be saved because the destination %destination is invalid. This may be caused by improper use of file_save_data() or a missing stream wrapper.', ['%destination' => $destination]);
\Drupal::messenger()->addError(t('The data could not be saved because the destination is invalid. More information is available in the system log.'));
return FALSE;
......@@ -716,6 +725,9 @@ function file_cron() {
$age = \Drupal::config('system.file')->get('temporary_maximum_age');
$file_storage = \Drupal::entityTypeManager()->getStorage('file');
/** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
// Only delete temporary files if older than $age. Note that automatic cleanup
// is disabled if $age set to 0.
if ($age) {
......@@ -729,7 +741,7 @@ function file_cron() {
$references = \Drupal::service('file.usage')->listUsage($file);
if (empty($references)) {
if (!file_exists($file->getFileUri())) {
if (!file_valid_uri($file->getFileUri())) {
if (!$stream_wrapper_manager->isValidUri($file->getFileUri())) {
\Drupal::logger('file system')->warning('Temporary file "%path" that was deleted during garbage collection did not exist on the filesystem. This could be caused by a missing stream wrapper.', ['%path' => $file->getFileUri()]);
}
else {
......@@ -1029,9 +1041,12 @@ function _file_save_upload_single(\SplFileInfo $file_info, $form_field_name, $va
$destination = 'temporary://';
}
/** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
// Assert that the destination contains a valid stream.
$destination_scheme = file_uri_scheme($destination);
if (!file_stream_wrapper_valid_scheme($destination_scheme)) {
$destination_scheme = $stream_wrapper_manager::getScheme($destination);
if (!$stream_wrapper_manager->isValidScheme($destination_scheme)) {
\Drupal::messenger()->addError(t('The file could not be uploaded because the destination %destination is invalid.', ['%destination' => $destination]));
return FALSE;
}
......
......@@ -21,7 +21,7 @@ class FileAccessControlHandler extends EntityAccessControlHandler {
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
/** @var \Drupal\file\FileInterface $entity */
if ($operation == 'download' || $operation == 'view') {
if (\Drupal::service('file_system')->uriScheme($entity->getFileUri()) === 'public') {