3 namespace Drupal\Core\File;
5 use Drupal\Core\Site\Settings;
6 use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
7 use Psr\Log\LoggerInterface;
10 * Provides helpers to operate on files and stream wrappers.
12 class FileSystem implements FileSystemInterface {
15 * Default mode for new directories. See self::chmod().
17 const CHMOD_DIRECTORY = 0775;
20 * Default mode for new files. See self::chmod().
22 const CHMOD_FILE = 0664;
27 * @var \Drupal\Core\Site\Settings
32 * The file logger channel.
34 * @var \Psr\Log\LoggerInterface
39 * The stream wrapper manager.
41 * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
43 protected $streamWrapperManager;
46 * Constructs a new FileSystem.
48 * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
49 * The stream wrapper manager.
50 * @param \Drupal\Core\Site\Settings $settings
52 * @param \Psr\Log\LoggerInterface $logger
53 * The file logger channel.
55 public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager, Settings $settings, LoggerInterface $logger) {
56 $this->streamWrapperManager = $stream_wrapper_manager;
57 $this->settings = $settings;
58 $this->logger = $logger;
64 public function moveUploadedFile($filename, $uri) {
65 $result = @move_uploaded_file($filename, $uri);
66 // PHP's move_uploaded_file() does not properly support streams if
67 // open_basedir is enabled so if the move failed, try finding a real path
68 // and retry the move operation.
70 if ($realpath = $this->realpath($uri)) {
71 $result = move_uploaded_file($filename, $realpath);
74 $result = move_uploaded_file($filename, $uri);
84 public function chmod($uri, $mode = NULL) {
87 $mode = $this->settings->get('file_chmod_directory', static::CHMOD_DIRECTORY);
90 $mode = $this->settings->get('file_chmod_file', static::CHMOD_FILE);
94 if (@chmod($uri, $mode)) {
98 $this->logger->error('The file permissions could not be set on %uri.', ['%uri' => $uri]);
105 public function unlink($uri, $context = NULL) {
106 $scheme = $this->uriScheme($uri);
107 if (!$this->validScheme($scheme) && (substr(PHP_OS, 0, 3) == 'WIN')) {
111 return unlink($uri, $context);
121 public function realpath($uri) {
122 // If this URI is a stream, pass it off to the appropriate stream wrapper.
123 // Otherwise, attempt PHP's realpath. This allows use of this method even
124 // for unmanaged files outside of the stream wrapper interface.
125 if ($wrapper = $this->streamWrapperManager->getViaUri($uri)) {
126 return $wrapper->realpath();
129 return realpath($uri);
135 public function dirname($uri) {
136 $scheme = $this->uriScheme($uri);
138 if ($this->validScheme($scheme)) {
139 return $this->streamWrapperManager->getViaScheme($scheme)->dirname($uri);
142 return dirname($uri);
149 public function basename($uri, $suffix = NULL) {
151 if (DIRECTORY_SEPARATOR != '/') {
152 // For Windows OS add special separator.
153 $separators .= DIRECTORY_SEPARATOR;
155 // Remove right-most slashes when $uri points to directory.
156 $uri = rtrim($uri, $separators);
157 // Returns the trailing part of the $uri starting after one of the directory
159 $filename = preg_match('@[^' . preg_quote($separators, '@') . ']+$@', $uri, $matches) ? $matches[0] : '';
160 // Cuts off a suffix from the filename.
162 $filename = preg_replace('@' . preg_quote($suffix, '@') . '$@', '', $filename);
170 public function mkdir($uri, $mode = NULL, $recursive = FALSE, $context = NULL) {
172 $mode = $this->settings->get('file_chmod_directory', static::CHMOD_DIRECTORY);
175 // If the URI has a scheme, don't override the umask - schemes can handle
176 // this issue in their own implementation.
177 if ($this->uriScheme($uri)) {
178 return $this->mkdirCall($uri, $mode, $recursive, $context);
181 // If recursive, create each missing component of the parent directory
182 // individually and set the mode explicitly to override the umask.
184 // Ensure the path is using DIRECTORY_SEPARATOR, and trim off any trailing
185 // slashes because they can throw off the loop when creating the parent
187 $uri = rtrim(str_replace('/', DIRECTORY_SEPARATOR, $uri), DIRECTORY_SEPARATOR);
188 // Determine the components of the path.
189 $components = explode(DIRECTORY_SEPARATOR, $uri);
190 // If the filepath is absolute the first component will be empty as there
191 // will be nothing before the first slash.
192 if ($components[0] == '') {
193 $recursive_path = DIRECTORY_SEPARATOR;
194 // Get rid of the empty first component.
195 array_shift($components);
198 $recursive_path = '';
200 // Don't handle the top-level directory in this loop.
201 array_pop($components);
202 // Create each component if necessary.
203 foreach ($components as $component) {
204 $recursive_path .= $component;
206 if (!file_exists($recursive_path)) {
207 if (!$this->mkdirCall($recursive_path, $mode, FALSE, $context)) {
210 // Not necessary to use self::chmod() as there is no scheme.
211 if (!chmod($recursive_path, $mode)) {
216 $recursive_path .= DIRECTORY_SEPARATOR;
220 // Do not check if the top-level directory already exists, as this condition
221 // must cause this function to fail.
222 if (!$this->mkdirCall($uri, $mode, FALSE, $context)) {
225 // Not necessary to use self::chmod() as there is no scheme.
226 return chmod($uri, $mode);
230 * Helper function. Ensures we don't pass a NULL as a context resource to
235 protected function mkdirCall($uri, $mode, $recursive, $context) {
236 if (is_null($context)) {
237 return mkdir($uri, $mode, $recursive);
240 return mkdir($uri, $mode, $recursive, $context);
247 public function rmdir($uri, $context = NULL) {
248 $scheme = $this->uriScheme($uri);
249 if (!$this->validScheme($scheme) && (substr(PHP_OS, 0, 3) == 'WIN')) {
253 return rmdir($uri, $context);
263 public function tempnam($directory, $prefix) {
264 $scheme = $this->uriScheme($directory);
266 if ($this->validScheme($scheme)) {
267 $wrapper = $this->streamWrapperManager->getViaScheme($scheme);
269 if ($filename = tempnam($wrapper->getDirectoryPath(), $prefix)) {
270 return $scheme . '://' . static::basename($filename);
277 // Handle as a normal tempnam() call.
278 return tempnam($directory, $prefix);
285 public function uriScheme($uri) {
286 if (preg_match('/^([\w\-]+):\/\/|^(data):/', $uri, $matches)) {
287 // The scheme will always be the last element in the matches array.
288 return array_pop($matches);
297 public function validScheme($scheme) {
301 return class_exists($this->streamWrapperManager->getClass($scheme));