Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\File;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Site\Settings;
|
Chris@0
|
6 use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
|
Chris@0
|
7 use Psr\Log\LoggerInterface;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Provides helpers to operate on files and stream wrappers.
|
Chris@0
|
11 */
|
Chris@0
|
12 class FileSystem implements FileSystemInterface {
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Default mode for new directories. See self::chmod().
|
Chris@0
|
16 */
|
Chris@0
|
17 const CHMOD_DIRECTORY = 0775;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Default mode for new files. See self::chmod().
|
Chris@0
|
21 */
|
Chris@0
|
22 const CHMOD_FILE = 0664;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * The site settings.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var \Drupal\Core\Site\Settings
|
Chris@0
|
28 */
|
Chris@0
|
29 protected $settings;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * The file logger channel.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @var \Psr\Log\LoggerInterface
|
Chris@0
|
35 */
|
Chris@0
|
36 protected $logger;
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * The stream wrapper manager.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
|
Chris@0
|
42 */
|
Chris@0
|
43 protected $streamWrapperManager;
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * Constructs a new FileSystem.
|
Chris@0
|
47 *
|
Chris@0
|
48 * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
|
Chris@0
|
49 * The stream wrapper manager.
|
Chris@0
|
50 * @param \Drupal\Core\Site\Settings $settings
|
Chris@0
|
51 * The site settings.
|
Chris@0
|
52 * @param \Psr\Log\LoggerInterface $logger
|
Chris@0
|
53 * The file logger channel.
|
Chris@0
|
54 */
|
Chris@0
|
55 public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager, Settings $settings, LoggerInterface $logger) {
|
Chris@0
|
56 $this->streamWrapperManager = $stream_wrapper_manager;
|
Chris@0
|
57 $this->settings = $settings;
|
Chris@0
|
58 $this->logger = $logger;
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * {@inheritdoc}
|
Chris@0
|
63 */
|
Chris@0
|
64 public function moveUploadedFile($filename, $uri) {
|
Chris@0
|
65 $result = @move_uploaded_file($filename, $uri);
|
Chris@0
|
66 // PHP's move_uploaded_file() does not properly support streams if
|
Chris@0
|
67 // open_basedir is enabled so if the move failed, try finding a real path
|
Chris@0
|
68 // and retry the move operation.
|
Chris@0
|
69 if (!$result) {
|
Chris@0
|
70 if ($realpath = $this->realpath($uri)) {
|
Chris@0
|
71 $result = move_uploaded_file($filename, $realpath);
|
Chris@0
|
72 }
|
Chris@0
|
73 else {
|
Chris@0
|
74 $result = move_uploaded_file($filename, $uri);
|
Chris@0
|
75 }
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 return $result;
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * {@inheritdoc}
|
Chris@0
|
83 */
|
Chris@0
|
84 public function chmod($uri, $mode = NULL) {
|
Chris@0
|
85 if (!isset($mode)) {
|
Chris@0
|
86 if (is_dir($uri)) {
|
Chris@0
|
87 $mode = $this->settings->get('file_chmod_directory', static::CHMOD_DIRECTORY);
|
Chris@0
|
88 }
|
Chris@0
|
89 else {
|
Chris@0
|
90 $mode = $this->settings->get('file_chmod_file', static::CHMOD_FILE);
|
Chris@0
|
91 }
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 if (@chmod($uri, $mode)) {
|
Chris@0
|
95 return TRUE;
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 $this->logger->error('The file permissions could not be set on %uri.', ['%uri' => $uri]);
|
Chris@0
|
99 return FALSE;
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 /**
|
Chris@0
|
103 * {@inheritdoc}
|
Chris@0
|
104 */
|
Chris@0
|
105 public function unlink($uri, $context = NULL) {
|
Chris@0
|
106 $scheme = $this->uriScheme($uri);
|
Chris@0
|
107 if (!$this->validScheme($scheme) && (substr(PHP_OS, 0, 3) == 'WIN')) {
|
Chris@0
|
108 chmod($uri, 0600);
|
Chris@0
|
109 }
|
Chris@0
|
110 if ($context) {
|
Chris@0
|
111 return unlink($uri, $context);
|
Chris@0
|
112 }
|
Chris@0
|
113 else {
|
Chris@0
|
114 return unlink($uri);
|
Chris@0
|
115 }
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * {@inheritdoc}
|
Chris@0
|
120 */
|
Chris@0
|
121 public function realpath($uri) {
|
Chris@0
|
122 // If this URI is a stream, pass it off to the appropriate stream wrapper.
|
Chris@0
|
123 // Otherwise, attempt PHP's realpath. This allows use of this method even
|
Chris@0
|
124 // for unmanaged files outside of the stream wrapper interface.
|
Chris@0
|
125 if ($wrapper = $this->streamWrapperManager->getViaUri($uri)) {
|
Chris@0
|
126 return $wrapper->realpath();
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 return realpath($uri);
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 /**
|
Chris@0
|
133 * {@inheritdoc}
|
Chris@0
|
134 */
|
Chris@0
|
135 public function dirname($uri) {
|
Chris@0
|
136 $scheme = $this->uriScheme($uri);
|
Chris@0
|
137
|
Chris@0
|
138 if ($this->validScheme($scheme)) {
|
Chris@0
|
139 return $this->streamWrapperManager->getViaScheme($scheme)->dirname($uri);
|
Chris@0
|
140 }
|
Chris@0
|
141 else {
|
Chris@0
|
142 return dirname($uri);
|
Chris@0
|
143 }
|
Chris@0
|
144 }
|
Chris@0
|
145
|
Chris@0
|
146 /**
|
Chris@0
|
147 * {@inheritdoc}
|
Chris@0
|
148 */
|
Chris@0
|
149 public function basename($uri, $suffix = NULL) {
|
Chris@0
|
150 $separators = '/';
|
Chris@0
|
151 if (DIRECTORY_SEPARATOR != '/') {
|
Chris@0
|
152 // For Windows OS add special separator.
|
Chris@0
|
153 $separators .= DIRECTORY_SEPARATOR;
|
Chris@0
|
154 }
|
Chris@0
|
155 // Remove right-most slashes when $uri points to directory.
|
Chris@0
|
156 $uri = rtrim($uri, $separators);
|
Chris@0
|
157 // Returns the trailing part of the $uri starting after one of the directory
|
Chris@0
|
158 // separators.
|
Chris@0
|
159 $filename = preg_match('@[^' . preg_quote($separators, '@') . ']+$@', $uri, $matches) ? $matches[0] : '';
|
Chris@0
|
160 // Cuts off a suffix from the filename.
|
Chris@0
|
161 if ($suffix) {
|
Chris@0
|
162 $filename = preg_replace('@' . preg_quote($suffix, '@') . '$@', '', $filename);
|
Chris@0
|
163 }
|
Chris@0
|
164 return $filename;
|
Chris@0
|
165 }
|
Chris@0
|
166
|
Chris@0
|
167 /**
|
Chris@0
|
168 * {@inheritdoc}
|
Chris@0
|
169 */
|
Chris@0
|
170 public function mkdir($uri, $mode = NULL, $recursive = FALSE, $context = NULL) {
|
Chris@0
|
171 if (!isset($mode)) {
|
Chris@0
|
172 $mode = $this->settings->get('file_chmod_directory', static::CHMOD_DIRECTORY);
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 // If the URI has a scheme, don't override the umask - schemes can handle
|
Chris@0
|
176 // this issue in their own implementation.
|
Chris@0
|
177 if ($this->uriScheme($uri)) {
|
Chris@0
|
178 return $this->mkdirCall($uri, $mode, $recursive, $context);
|
Chris@0
|
179 }
|
Chris@0
|
180
|
Chris@0
|
181 // If recursive, create each missing component of the parent directory
|
Chris@0
|
182 // individually and set the mode explicitly to override the umask.
|
Chris@0
|
183 if ($recursive) {
|
Chris@0
|
184 // Ensure the path is using DIRECTORY_SEPARATOR, and trim off any trailing
|
Chris@0
|
185 // slashes because they can throw off the loop when creating the parent
|
Chris@0
|
186 // directories.
|
Chris@0
|
187 $uri = rtrim(str_replace('/', DIRECTORY_SEPARATOR, $uri), DIRECTORY_SEPARATOR);
|
Chris@0
|
188 // Determine the components of the path.
|
Chris@0
|
189 $components = explode(DIRECTORY_SEPARATOR, $uri);
|
Chris@0
|
190 // If the filepath is absolute the first component will be empty as there
|
Chris@0
|
191 // will be nothing before the first slash.
|
Chris@0
|
192 if ($components[0] == '') {
|
Chris@0
|
193 $recursive_path = DIRECTORY_SEPARATOR;
|
Chris@0
|
194 // Get rid of the empty first component.
|
Chris@0
|
195 array_shift($components);
|
Chris@0
|
196 }
|
Chris@0
|
197 else {
|
Chris@0
|
198 $recursive_path = '';
|
Chris@0
|
199 }
|
Chris@0
|
200 // Don't handle the top-level directory in this loop.
|
Chris@0
|
201 array_pop($components);
|
Chris@0
|
202 // Create each component if necessary.
|
Chris@0
|
203 foreach ($components as $component) {
|
Chris@0
|
204 $recursive_path .= $component;
|
Chris@0
|
205
|
Chris@0
|
206 if (!file_exists($recursive_path)) {
|
Chris@0
|
207 if (!$this->mkdirCall($recursive_path, $mode, FALSE, $context)) {
|
Chris@0
|
208 return FALSE;
|
Chris@0
|
209 }
|
Chris@0
|
210 // Not necessary to use self::chmod() as there is no scheme.
|
Chris@0
|
211 if (!chmod($recursive_path, $mode)) {
|
Chris@0
|
212 return FALSE;
|
Chris@0
|
213 }
|
Chris@0
|
214 }
|
Chris@0
|
215
|
Chris@0
|
216 $recursive_path .= DIRECTORY_SEPARATOR;
|
Chris@0
|
217 }
|
Chris@0
|
218 }
|
Chris@0
|
219
|
Chris@0
|
220 // Do not check if the top-level directory already exists, as this condition
|
Chris@0
|
221 // must cause this function to fail.
|
Chris@0
|
222 if (!$this->mkdirCall($uri, $mode, FALSE, $context)) {
|
Chris@0
|
223 return FALSE;
|
Chris@0
|
224 }
|
Chris@0
|
225 // Not necessary to use self::chmod() as there is no scheme.
|
Chris@0
|
226 return chmod($uri, $mode);
|
Chris@0
|
227 }
|
Chris@0
|
228
|
Chris@0
|
229 /**
|
Chris@0
|
230 * Helper function. Ensures we don't pass a NULL as a context resource to
|
Chris@0
|
231 * mkdir().
|
Chris@0
|
232 *
|
Chris@0
|
233 * @see self::mkdir()
|
Chris@0
|
234 */
|
Chris@0
|
235 protected function mkdirCall($uri, $mode, $recursive, $context) {
|
Chris@0
|
236 if (is_null($context)) {
|
Chris@0
|
237 return mkdir($uri, $mode, $recursive);
|
Chris@0
|
238 }
|
Chris@0
|
239 else {
|
Chris@0
|
240 return mkdir($uri, $mode, $recursive, $context);
|
Chris@0
|
241 }
|
Chris@0
|
242 }
|
Chris@0
|
243
|
Chris@0
|
244 /**
|
Chris@0
|
245 * {@inheritdoc}
|
Chris@0
|
246 */
|
Chris@0
|
247 public function rmdir($uri, $context = NULL) {
|
Chris@0
|
248 $scheme = $this->uriScheme($uri);
|
Chris@0
|
249 if (!$this->validScheme($scheme) && (substr(PHP_OS, 0, 3) == 'WIN')) {
|
Chris@0
|
250 chmod($uri, 0700);
|
Chris@0
|
251 }
|
Chris@0
|
252 if ($context) {
|
Chris@0
|
253 return rmdir($uri, $context);
|
Chris@0
|
254 }
|
Chris@0
|
255 else {
|
Chris@0
|
256 return rmdir($uri);
|
Chris@0
|
257 }
|
Chris@0
|
258 }
|
Chris@0
|
259
|
Chris@0
|
260 /**
|
Chris@0
|
261 * {@inheritdoc}
|
Chris@0
|
262 */
|
Chris@0
|
263 public function tempnam($directory, $prefix) {
|
Chris@0
|
264 $scheme = $this->uriScheme($directory);
|
Chris@0
|
265
|
Chris@0
|
266 if ($this->validScheme($scheme)) {
|
Chris@0
|
267 $wrapper = $this->streamWrapperManager->getViaScheme($scheme);
|
Chris@0
|
268
|
Chris@0
|
269 if ($filename = tempnam($wrapper->getDirectoryPath(), $prefix)) {
|
Chris@0
|
270 return $scheme . '://' . static::basename($filename);
|
Chris@0
|
271 }
|
Chris@0
|
272 else {
|
Chris@0
|
273 return FALSE;
|
Chris@0
|
274 }
|
Chris@0
|
275 }
|
Chris@0
|
276 else {
|
Chris@0
|
277 // Handle as a normal tempnam() call.
|
Chris@0
|
278 return tempnam($directory, $prefix);
|
Chris@0
|
279 }
|
Chris@0
|
280 }
|
Chris@0
|
281
|
Chris@0
|
282 /**
|
Chris@0
|
283 * {@inheritdoc}
|
Chris@0
|
284 */
|
Chris@0
|
285 public function uriScheme($uri) {
|
Chris@0
|
286 if (preg_match('/^([\w\-]+):\/\/|^(data):/', $uri, $matches)) {
|
Chris@0
|
287 // The scheme will always be the last element in the matches array.
|
Chris@0
|
288 return array_pop($matches);
|
Chris@0
|
289 }
|
Chris@0
|
290
|
Chris@0
|
291 return FALSE;
|
Chris@0
|
292 }
|
Chris@0
|
293
|
Chris@0
|
294 /**
|
Chris@0
|
295 * {@inheritdoc}
|
Chris@0
|
296 */
|
Chris@0
|
297 public function validScheme($scheme) {
|
Chris@0
|
298 if (!$scheme) {
|
Chris@0
|
299 return FALSE;
|
Chris@0
|
300 }
|
Chris@0
|
301 return class_exists($this->streamWrapperManager->getClass($scheme));
|
Chris@0
|
302 }
|
Chris@0
|
303
|
Chris@0
|
304 }
|