Mercurial > hg > isophonics-drupal-site
comparison core/lib/Drupal/Core/File/FileSystem.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Core\File; | |
4 | |
5 use Drupal\Core\Site\Settings; | |
6 use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface; | |
7 use Psr\Log\LoggerInterface; | |
8 | |
9 /** | |
10 * Provides helpers to operate on files and stream wrappers. | |
11 */ | |
12 class FileSystem implements FileSystemInterface { | |
13 | |
14 /** | |
15 * Default mode for new directories. See self::chmod(). | |
16 */ | |
17 const CHMOD_DIRECTORY = 0775; | |
18 | |
19 /** | |
20 * Default mode for new files. See self::chmod(). | |
21 */ | |
22 const CHMOD_FILE = 0664; | |
23 | |
24 /** | |
25 * The site settings. | |
26 * | |
27 * @var \Drupal\Core\Site\Settings | |
28 */ | |
29 protected $settings; | |
30 | |
31 /** | |
32 * The file logger channel. | |
33 * | |
34 * @var \Psr\Log\LoggerInterface | |
35 */ | |
36 protected $logger; | |
37 | |
38 /** | |
39 * The stream wrapper manager. | |
40 * | |
41 * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface | |
42 */ | |
43 protected $streamWrapperManager; | |
44 | |
45 /** | |
46 * Constructs a new FileSystem. | |
47 * | |
48 * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager | |
49 * The stream wrapper manager. | |
50 * @param \Drupal\Core\Site\Settings $settings | |
51 * The site settings. | |
52 * @param \Psr\Log\LoggerInterface $logger | |
53 * The file logger channel. | |
54 */ | |
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; | |
59 } | |
60 | |
61 /** | |
62 * {@inheritdoc} | |
63 */ | |
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. | |
69 if (!$result) { | |
70 if ($realpath = $this->realpath($uri)) { | |
71 $result = move_uploaded_file($filename, $realpath); | |
72 } | |
73 else { | |
74 $result = move_uploaded_file($filename, $uri); | |
75 } | |
76 } | |
77 | |
78 return $result; | |
79 } | |
80 | |
81 /** | |
82 * {@inheritdoc} | |
83 */ | |
84 public function chmod($uri, $mode = NULL) { | |
85 if (!isset($mode)) { | |
86 if (is_dir($uri)) { | |
87 $mode = $this->settings->get('file_chmod_directory', static::CHMOD_DIRECTORY); | |
88 } | |
89 else { | |
90 $mode = $this->settings->get('file_chmod_file', static::CHMOD_FILE); | |
91 } | |
92 } | |
93 | |
94 if (@chmod($uri, $mode)) { | |
95 return TRUE; | |
96 } | |
97 | |
98 $this->logger->error('The file permissions could not be set on %uri.', ['%uri' => $uri]); | |
99 return FALSE; | |
100 } | |
101 | |
102 /** | |
103 * {@inheritdoc} | |
104 */ | |
105 public function unlink($uri, $context = NULL) { | |
106 $scheme = $this->uriScheme($uri); | |
107 if (!$this->validScheme($scheme) && (substr(PHP_OS, 0, 3) == 'WIN')) { | |
108 chmod($uri, 0600); | |
109 } | |
110 if ($context) { | |
111 return unlink($uri, $context); | |
112 } | |
113 else { | |
114 return unlink($uri); | |
115 } | |
116 } | |
117 | |
118 /** | |
119 * {@inheritdoc} | |
120 */ | |
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(); | |
127 } | |
128 | |
129 return realpath($uri); | |
130 } | |
131 | |
132 /** | |
133 * {@inheritdoc} | |
134 */ | |
135 public function dirname($uri) { | |
136 $scheme = $this->uriScheme($uri); | |
137 | |
138 if ($this->validScheme($scheme)) { | |
139 return $this->streamWrapperManager->getViaScheme($scheme)->dirname($uri); | |
140 } | |
141 else { | |
142 return dirname($uri); | |
143 } | |
144 } | |
145 | |
146 /** | |
147 * {@inheritdoc} | |
148 */ | |
149 public function basename($uri, $suffix = NULL) { | |
150 $separators = '/'; | |
151 if (DIRECTORY_SEPARATOR != '/') { | |
152 // For Windows OS add special separator. | |
153 $separators .= DIRECTORY_SEPARATOR; | |
154 } | |
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 | |
158 // separators. | |
159 $filename = preg_match('@[^' . preg_quote($separators, '@') . ']+$@', $uri, $matches) ? $matches[0] : ''; | |
160 // Cuts off a suffix from the filename. | |
161 if ($suffix) { | |
162 $filename = preg_replace('@' . preg_quote($suffix, '@') . '$@', '', $filename); | |
163 } | |
164 return $filename; | |
165 } | |
166 | |
167 /** | |
168 * {@inheritdoc} | |
169 */ | |
170 public function mkdir($uri, $mode = NULL, $recursive = FALSE, $context = NULL) { | |
171 if (!isset($mode)) { | |
172 $mode = $this->settings->get('file_chmod_directory', static::CHMOD_DIRECTORY); | |
173 } | |
174 | |
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); | |
179 } | |
180 | |
181 // If recursive, create each missing component of the parent directory | |
182 // individually and set the mode explicitly to override the umask. | |
183 if ($recursive) { | |
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 | |
186 // directories. | |
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); | |
196 } | |
197 else { | |
198 $recursive_path = ''; | |
199 } | |
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; | |
205 | |
206 if (!file_exists($recursive_path)) { | |
207 if (!$this->mkdirCall($recursive_path, $mode, FALSE, $context)) { | |
208 return FALSE; | |
209 } | |
210 // Not necessary to use self::chmod() as there is no scheme. | |
211 if (!chmod($recursive_path, $mode)) { | |
212 return FALSE; | |
213 } | |
214 } | |
215 | |
216 $recursive_path .= DIRECTORY_SEPARATOR; | |
217 } | |
218 } | |
219 | |
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)) { | |
223 return FALSE; | |
224 } | |
225 // Not necessary to use self::chmod() as there is no scheme. | |
226 return chmod($uri, $mode); | |
227 } | |
228 | |
229 /** | |
230 * Helper function. Ensures we don't pass a NULL as a context resource to | |
231 * mkdir(). | |
232 * | |
233 * @see self::mkdir() | |
234 */ | |
235 protected function mkdirCall($uri, $mode, $recursive, $context) { | |
236 if (is_null($context)) { | |
237 return mkdir($uri, $mode, $recursive); | |
238 } | |
239 else { | |
240 return mkdir($uri, $mode, $recursive, $context); | |
241 } | |
242 } | |
243 | |
244 /** | |
245 * {@inheritdoc} | |
246 */ | |
247 public function rmdir($uri, $context = NULL) { | |
248 $scheme = $this->uriScheme($uri); | |
249 if (!$this->validScheme($scheme) && (substr(PHP_OS, 0, 3) == 'WIN')) { | |
250 chmod($uri, 0700); | |
251 } | |
252 if ($context) { | |
253 return rmdir($uri, $context); | |
254 } | |
255 else { | |
256 return rmdir($uri); | |
257 } | |
258 } | |
259 | |
260 /** | |
261 * {@inheritdoc} | |
262 */ | |
263 public function tempnam($directory, $prefix) { | |
264 $scheme = $this->uriScheme($directory); | |
265 | |
266 if ($this->validScheme($scheme)) { | |
267 $wrapper = $this->streamWrapperManager->getViaScheme($scheme); | |
268 | |
269 if ($filename = tempnam($wrapper->getDirectoryPath(), $prefix)) { | |
270 return $scheme . '://' . static::basename($filename); | |
271 } | |
272 else { | |
273 return FALSE; | |
274 } | |
275 } | |
276 else { | |
277 // Handle as a normal tempnam() call. | |
278 return tempnam($directory, $prefix); | |
279 } | |
280 } | |
281 | |
282 /** | |
283 * {@inheritdoc} | |
284 */ | |
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); | |
289 } | |
290 | |
291 return FALSE; | |
292 } | |
293 | |
294 /** | |
295 * {@inheritdoc} | |
296 */ | |
297 public function validScheme($scheme) { | |
298 if (!$scheme) { | |
299 return FALSE; | |
300 } | |
301 return class_exists($this->streamWrapperManager->getClass($scheme)); | |
302 } | |
303 | |
304 } |