7a01d9e5aa52a0fa3d3a4cb260232166a1ab9b16
[lhc/web/wiklou.git] / includes / filerepo / backend / FSFileBackend.php
1 <?php
2 /**
3 * @file
4 * @ingroup FileBackend
5 * @author Aaron Schulz
6 */
7
8 /**
9 * Class for a file system based file backend.
10 * Containers are just directories and container sharding is not supported.
11 * Also, for backwards-compatibility, the wiki ID prefix is not used.
12 * Users of this class should set wiki-specific container paths as needed.
13 *
14 * Status messages should avoid mentioning the internal FS paths.
15 * Likewise, error suppression should be used to avoid path disclosure.
16 *
17 * @ingroup FileBackend
18 */
19 class FSFileBackend extends FileBackend {
20 /** @var Array Map of container names to paths */
21 protected $containerPaths = array();
22 protected $fileMode; // file permission mode
23
24 /**
25 * @see FileBackend::__construct()
26 * Additional $config params include:
27 * containerPaths : Map of container names to absolute file system paths
28 * fileMode : Octal UNIX file permissions to use on files stored
29 */
30 public function __construct( array $config ) {
31 parent::__construct( $config );
32 $this->containerPaths = (array)$config['containerPaths'];
33 foreach ( $this->containerPaths as &$path ) {
34 if ( substr( $path, -1 ) === '/' ) {
35 $path = substr( $path, 0, -1 ); // remove trailing slash
36 }
37 }
38 $this->fileMode = isset( $config['fileMode'] )
39 ? $config['fileMode']
40 : 0644;
41 }
42
43 /**
44 * @see FileBackend::resolveContainerPath()
45 */
46 protected function resolveContainerPath( $container, $relStoragePath ) {
47 // Get absolute path given the container base dir
48 if ( isset( $this->containerPaths[$container] ) ) {
49 return $this->containerPaths[$container] . "/{$relStoragePath}";
50 }
51 return null;
52 }
53
54 /**
55 * @see FileBackend::doStoreInternal()
56 */
57 protected function doStoreInternal( array $params ) {
58 $status = Status::newGood();
59
60 list( $c, $dest ) = $this->resolveStoragePathReal( $params['dst'] );
61 if ( $dest === null ) {
62 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
63 return $status;
64 }
65 if ( file_exists( $dest ) ) {
66 if ( !empty( $params['overwriteDest'] ) ) {
67 wfSuppressWarnings();
68 $ok = unlink( $dest );
69 wfRestoreWarnings();
70 if ( !$ok ) {
71 $status->fatal( 'backend-fail-delete', $params['dst'] );
72 return $status;
73 }
74 } else {
75 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
76 return $status;
77 }
78 } else {
79 if ( !wfMkdirParents( dirname( $dest ) ) ) {
80 $status->fatal( 'directorycreateerror', $params['dst'] );
81 return $status;
82 }
83 }
84
85 wfSuppressWarnings();
86 $ok = copy( $params['src'], $dest );
87 wfRestoreWarnings();
88 if ( !$ok ) {
89 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
90 return $status;
91 }
92
93 $this->chmod( $dest );
94
95 return $status;
96 }
97
98 /**
99 * @see FileBackend::doCopyInternal()
100 */
101 protected function doCopyInternal( array $params ) {
102 $status = Status::newGood();
103
104 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
105 if ( $source === null ) {
106 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
107 return $status;
108 }
109
110 list( $c, $dest ) = $this->resolveStoragePathReal( $params['dst'] );
111 if ( $dest === null ) {
112 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
113 return $status;
114 }
115
116 if ( file_exists( $dest ) ) {
117 if ( !empty( $params['overwriteDest'] ) ) {
118 wfSuppressWarnings();
119 $ok = unlink( $dest );
120 wfRestoreWarnings();
121 if ( !$ok ) {
122 $status->fatal( 'backend-fail-delete', $params['dst'] );
123 return $status;
124 }
125 } else {
126 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
127 return $status;
128 }
129 } else {
130 if ( !wfMkdirParents( dirname( $dest ) ) ) {
131 $status->fatal( 'directorycreateerror', $params['dst'] );
132 return $status;
133 }
134 }
135
136 wfSuppressWarnings();
137 $ok = copy( $source, $dest );
138 wfRestoreWarnings();
139 if ( !$ok ) {
140 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
141 return $status;
142 }
143
144 $this->chmod( $dest );
145
146 return $status;
147 }
148
149 /**
150 * @see FileBackend::doMoveInternal()
151 */
152 protected function doMoveInternal( array $params ) {
153 $status = Status::newGood();
154
155 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
156 if ( $source === null ) {
157 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
158 return $status;
159 }
160 list( $c, $dest ) = $this->resolveStoragePathReal( $params['dst'] );
161 if ( $dest === null ) {
162 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
163 return $status;
164 }
165
166 if ( file_exists( $dest ) ) {
167 if ( !empty( $params['overwriteDest'] ) ) {
168 // Windows does not support moving over existing files
169 if ( wfIsWindows() ) {
170 wfSuppressWarnings();
171 $ok = unlink( $dest );
172 wfRestoreWarnings();
173 if ( !$ok ) {
174 $status->fatal( 'backend-fail-delete', $params['dst'] );
175 return $status;
176 }
177 }
178 } else {
179 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
180 return $status;
181 }
182 } else {
183 if ( !wfMkdirParents( dirname( $dest ) ) ) {
184 $status->fatal( 'directorycreateerror', $params['dst'] );
185 return $status;
186 }
187 }
188
189 wfSuppressWarnings();
190 $ok = rename( $source, $dest );
191 clearstatcache(); // file no longer at source
192 wfRestoreWarnings();
193 if ( !$ok ) {
194 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
195 return $status;
196 }
197
198 return $status;
199 }
200
201 /**
202 * @see FileBackend::doDeleteInternal()
203 */
204 protected function doDeleteInternal( array $params ) {
205 $status = Status::newGood();
206
207 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
208 if ( $source === null ) {
209 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
210 return $status;
211 }
212
213 if ( !is_file( $source ) ) {
214 if ( empty( $params['ignoreMissingSource'] ) ) {
215 $status->fatal( 'backend-fail-delete', $params['src'] );
216 }
217 return $status; // do nothing; either OK or bad status
218 }
219
220 wfSuppressWarnings();
221 $ok = unlink( $source );
222 wfRestoreWarnings();
223 if ( !$ok ) {
224 $status->fatal( 'backend-fail-delete', $params['src'] );
225 return $status;
226 }
227
228 return $status;
229 }
230
231 /**
232 * @see FileBackend::doCreateInternal()
233 */
234 protected function doCreateInternal( array $params ) {
235 $status = Status::newGood();
236
237 list( $c, $dest ) = $this->resolveStoragePathReal( $params['dst'] );
238 if ( $dest === null ) {
239 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
240 return $status;
241 }
242
243 if ( file_exists( $dest ) ) {
244 if ( !empty( $params['overwriteDest'] ) ) {
245 wfSuppressWarnings();
246 $ok = unlink( $dest );
247 wfRestoreWarnings();
248 if ( !$ok ) {
249 $status->fatal( 'backend-fail-delete', $params['dst'] );
250 return $status;
251 }
252 } else {
253 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
254 return $status;
255 }
256 } else {
257 if ( !wfMkdirParents( dirname( $dest ) ) ) {
258 $status->fatal( 'directorycreateerror', $params['dst'] );
259 return $status;
260 }
261 }
262
263 wfSuppressWarnings();
264 $ok = file_put_contents( $dest, $params['content'] );
265 wfRestoreWarnings();
266 if ( !$ok ) {
267 $status->fatal( 'backend-fail-create', $params['dst'] );
268 return $status;
269 }
270
271 $this->chmod( $dest );
272
273 return $status;
274 }
275
276 /**
277 * @see FileBackend::doPrepareInternal()
278 */
279 protected function doPrepareInternal( $container, $dir, array $params ) {
280 $status = Status::newGood();
281 if ( !wfMkdirParents( $dir ) ) {
282 $status->fatal( 'directorycreateerror', $params['dir'] );
283 } elseif ( !is_writable( $dir ) ) {
284 $status->fatal( 'directoryreadonlyerror', $params['dir'] );
285 } elseif ( !is_readable( $dir ) ) {
286 $status->fatal( 'directorynotreadableerror', $params['dir'] );
287 }
288 return $status;
289 }
290
291 /**
292 * @see FileBackend::doSecureInternal()
293 */
294 protected function doSecureInternal( $container, $dir, array $params ) {
295 $status = Status::newGood();
296 if ( !wfMkdirParents( $dir ) ) {
297 $status->fatal( 'directorycreateerror', $params['dir'] );
298 return $status;
299 }
300 // Seed new directories with a blank index.html, to prevent crawling...
301 if ( !empty( $params['noListing'] ) && !file_exists( "{$dir}/index.html" ) ) {
302 wfSuppressWarnings();
303 $ok = file_put_contents( "{$dir}/index.html", '' );
304 wfRestoreWarnings();
305 if ( !$ok ) {
306 $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' );
307 return $status;
308 }
309 }
310 // Add a .htaccess file to the root of the container...
311 if ( !empty( $params['noAccess'] ) ) {
312 list( $b, $container, $r ) = FileBackend::splitStoragePath( $params['dir'] );
313 $dirRoot = $this->containerPaths[$container]; // real path
314 if ( !file_exists( "{$dirRoot}/.htaccess" ) ) {
315 wfSuppressWarnings();
316 $ok = file_put_contents( "{$dirRoot}/.htaccess", "Deny from all\n" );
317 wfRestoreWarnings();
318 if ( !$ok ) {
319 $storeDir = "mwstore://{$this->name}/{$container}";
320 $status->fatal( 'backend-fail-create', "$storeDir/.htaccess" );
321 return $status;
322 }
323 }
324 }
325 return $status;
326 }
327
328 /**
329 * @see FileBackend::doCleanInternal()
330 */
331 protected function doCleanInternal( $container, $dir, array $params ) {
332 $status = Status::newGood();
333 wfSuppressWarnings();
334 if ( is_dir( $dir ) ) {
335 rmdir( $dir ); // remove directory if empty
336 }
337 wfRestoreWarnings();
338 return $status;
339 }
340
341 /**
342 * @see FileBackend::doFileExists()
343 */
344 protected function doGetFileStat( array $params ) {
345 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
346 if ( $source === null ) {
347 return false; // invalid storage path
348 }
349
350 wfSuppressWarnings();
351 if ( is_file( $source ) ) { // regular file?
352 $stat = stat( $source );
353 } else {
354 $stat = false;
355 }
356 wfRestoreWarnings();
357
358 if ( $stat ) {
359 return array(
360 'mtime' => wfTimestamp( TS_MW, $stat['mtime'] ),
361 'size' => $stat['size']
362 );
363 } else {
364 return false;
365 }
366 }
367
368 /**
369 * @see FileBackend::getFileListInternal()
370 */
371 public function getFileListInternal( $container, $dir, array $params ) {
372 wfSuppressWarnings();
373 $exists = is_dir( $dir );
374 wfRestoreWarnings();
375 if ( !$exists ) {
376 return array(); // nothing under this dir
377 }
378 wfSuppressWarnings();
379 $readable = is_readable( $dir );
380 wfRestoreWarnings();
381 if ( !$readable ) {
382 return null; // bad permissions?
383 }
384 return new FSFileIterator( $dir );
385 }
386
387 /**
388 * @see FileBackend::getLocalReference()
389 */
390 public function getLocalReference( array $params ) {
391 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
392 if ( $source === null ) {
393 return null;
394 }
395 return new FSFile( $source );
396 }
397
398 /**
399 * @see FileBackend::getLocalCopy()
400 */
401 public function getLocalCopy( array $params ) {
402 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
403 if ( $source === null ) {
404 return null;
405 }
406
407 // Create a new temporary file with the same extension...
408 $ext = FileBackend::extensionFromPath( $params['src'] );
409 $tmpFile = TempFSFile::factory( wfBaseName( $source ) . '_', $ext );
410 if ( !$tmpFile ) {
411 return null;
412 }
413 $tmpPath = $tmpFile->getPath();
414
415 // Copy the source file over the temp file
416 wfSuppressWarnings();
417 $ok = copy( $source, $tmpPath );
418 wfRestoreWarnings();
419 if ( !$ok ) {
420 return null;
421 }
422
423 $this->chmod( $tmpPath );
424
425 return $tmpFile;
426 }
427
428 /**
429 * Chmod a file, suppressing the warnings
430 *
431 * @param $path string Absolute file system path
432 * @return bool Success
433 */
434 protected function chmod( $path ) {
435 wfSuppressWarnings();
436 $ok = chmod( $path, $this->fileMode );
437 wfRestoreWarnings();
438
439 return $ok;
440 }
441 }
442
443 /**
444 * Wrapper around RecursiveDirectoryIterator that catches
445 * exception or does any custom behavoir that we may want.
446 *
447 * @ingroup FileBackend
448 */
449 class FSFileIterator implements Iterator {
450 /** @var RecursiveIteratorIterator */
451 protected $iter;
452 protected $suffixStart; // integer
453
454 /**
455 * Get an FSFileIterator from a file system directory
456 *
457 * @param $dir string
458 */
459 public function __construct( $dir ) {
460 $this->suffixStart = strlen( realpath( $dir ) ) + 1; // size of "path/to/dir/"
461 try {
462 $flags = FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS;
463 $this->iter = new RecursiveIteratorIterator(
464 new RecursiveDirectoryIterator( $dir, $flags ) );
465 } catch ( UnexpectedValueException $e ) {
466 $this->iter = null; // bad permissions? deleted?
467 }
468 }
469
470 public function current() {
471 // Return only the relative path and normalize slashes to FileBackend-style
472 // Make sure to use the realpath since the suffix is based upon that
473 return str_replace( '\\', '/', substr( realpath($this->iter->current()), $this->suffixStart ) );
474 }
475
476 public function key() {
477 return $this->iter->key();
478 }
479
480 public function next() {
481 try {
482 $this->iter->next();
483 } catch ( UnexpectedValueException $e ) {
484 $this->iter = null;
485 }
486 }
487
488 public function rewind() {
489 try {
490 $this->iter->rewind();
491 } catch ( UnexpectedValueException $e ) {
492 $this->iter = null;
493 }
494 }
495
496 public function valid() {
497 return $this->iter && $this->iter->valid();
498 }
499 }