Don't assume newFileFromKey always returns a File object (some repos many not support...
[lhc/web/wiklou.git] / includes / filerepo / FSRepo.php
1 <?php
2 /**
3 * A repository for files accessible via the local filesystem.
4 *
5 * @file
6 * @ingroup FileRepo
7 */
8
9 /**
10 * A repository for files accessible via the local filesystem. Does not support
11 * database access or registration.
12 * @ingroup FileRepo
13 */
14 class FSRepo extends FileRepo {
15 var $directory, $deletedDir, $deletedHashLevels, $fileMode;
16 var $fileFactory = array( 'UnregisteredLocalFile', 'newFromTitle' );
17 var $oldFileFactory = false;
18 var $pathDisclosureProtection = 'simple';
19
20 function __construct( $info ) {
21 parent::__construct( $info );
22
23 // Required settings
24 $this->directory = $info['directory'];
25 $this->url = $info['url'];
26
27 // Optional settings
28 $this->hashLevels = isset( $info['hashLevels'] ) ? $info['hashLevels'] : 2;
29 $this->deletedHashLevels = isset( $info['deletedHashLevels'] ) ?
30 $info['deletedHashLevels'] : $this->hashLevels;
31 $this->deletedDir = isset( $info['deletedDir'] ) ? $info['deletedDir'] : false;
32 $this->fileMode = isset( $info['fileMode'] ) ? $info['fileMode'] : 0644;
33 if ( isset( $info['thumbDir'] ) ) {
34 $this->thumbDir = $info['thumbDir'];
35 } else {
36 $this->thumbDir = "{$this->directory}/thumb";
37 }
38 if ( isset( $info['thumbUrl'] ) ) {
39 $this->thumbUrl = $info['thumbUrl'];
40 } else {
41 $this->thumbUrl = "{$this->url}/thumb";
42 }
43 }
44
45 /**
46 * Get the public root directory of the repository.
47 */
48 function getRootDirectory() {
49 return $this->directory;
50 }
51
52 /**
53 * Get the public root URL of the repository
54 */
55 function getRootUrl() {
56 return $this->url;
57 }
58
59 /**
60 * Returns true if the repository uses a multi-level directory structure
61 */
62 function isHashed() {
63 return (bool)$this->hashLevels;
64 }
65
66 /**
67 * Get the local directory corresponding to one of the three basic zones
68 */
69 function getZonePath( $zone ) {
70 switch ( $zone ) {
71 case 'public':
72 return $this->directory;
73 case 'temp':
74 return "{$this->directory}/temp";
75 case 'deleted':
76 return $this->deletedDir;
77 case 'thumb':
78 return $this->thumbDir;
79 default:
80 return false;
81 }
82 }
83
84 /**
85 * @see FileRepo::getZoneUrl()
86 */
87 function getZoneUrl( $zone ) {
88 switch ( $zone ) {
89 case 'public':
90 return $this->url;
91 case 'temp':
92 return "{$this->url}/temp";
93 case 'deleted':
94 return parent::getZoneUrl( $zone ); // no public URL
95 case 'thumb':
96 return $this->thumbUrl;
97 default:
98 return parent::getZoneUrl( $zone );
99 }
100 }
101
102 /**
103 * Get a URL referring to this repository, with the private mwrepo protocol.
104 * The suffix, if supplied, is considered to be unencoded, and will be
105 * URL-encoded before being returned.
106 */
107 function getVirtualUrl( $suffix = false ) {
108 $path = 'mwrepo://' . $this->name;
109 if ( $suffix !== false ) {
110 $path .= '/' . rawurlencode( $suffix );
111 }
112 return $path;
113 }
114
115 /**
116 * Get the local path corresponding to a virtual URL
117 */
118 function resolveVirtualUrl( $url ) {
119 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
120 throw new MWException( __METHOD__.': unknown protoocl' );
121 }
122
123 $bits = explode( '/', substr( $url, 9 ), 3 );
124 if ( count( $bits ) != 3 ) {
125 throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
126 }
127 list( $repo, $zone, $rel ) = $bits;
128 if ( $repo !== $this->name ) {
129 throw new MWException( __METHOD__.": fetching from a foreign repo is not supported" );
130 }
131 $base = $this->getZonePath( $zone );
132 if ( !$base ) {
133 throw new MWException( __METHOD__.": invalid zone: $zone" );
134 }
135 return $base . '/' . rawurldecode( $rel );
136 }
137
138 /**
139 * Store a batch of files
140 *
141 * @param $triplets Array: (src,zone,dest) triplets as per store()
142 * @param $flags Integer: bitwise combination of the following flags:
143 * self::DELETE_SOURCE Delete the source file after upload
144 * self::OVERWRITE Overwrite an existing destination file instead of failing
145 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
146 * same contents as the source
147 */
148 function storeBatch( $triplets, $flags = 0 ) {
149 wfDebug( __METHOD__ . ': Storing ' . count( $triplets ) .
150 " triplets; flags: {$flags}\n" );
151
152 // Try creating directories
153 if ( !wfMkdirParents( $this->directory ) ) {
154 return $this->newFatal( 'upload_directory_missing', $this->directory );
155 }
156 if ( !is_writable( $this->directory ) ) {
157 return $this->newFatal( 'upload_directory_read_only', $this->directory );
158 }
159
160 // Validate each triplet
161 $status = $this->newGood();
162 foreach ( $triplets as $i => $triplet ) {
163 list( $srcPath, $dstZone, $dstRel ) = $triplet;
164
165 // Resolve destination path
166 $root = $this->getZonePath( $dstZone );
167 if ( !$root ) {
168 throw new MWException( "Invalid zone: $dstZone" );
169 }
170 if ( !$this->validateFilename( $dstRel ) ) {
171 throw new MWException( 'Validation error in $dstRel' );
172 }
173 $dstPath = "$root/$dstRel";
174 $dstDir = dirname( $dstPath );
175
176 // Create destination directories for this triplet
177 if ( !is_dir( $dstDir ) ) {
178 if ( !wfMkdirParents( $dstDir ) ) {
179 return $this->newFatal( 'directorycreateerror', $dstDir );
180 }
181 if ( $dstZone == 'deleted' ) {
182 $this->initDeletedDir( $dstDir );
183 }
184 }
185
186 // Resolve source
187 if ( self::isVirtualUrl( $srcPath ) ) {
188 $srcPath = $triplets[$i][0] = $this->resolveVirtualUrl( $srcPath );
189 }
190 if ( !is_file( $srcPath ) ) {
191 // Make a list of files that don't exist for return to the caller
192 $status->fatal( 'filenotfound', $srcPath );
193 continue;
194 }
195
196 // Check overwriting
197 if ( !( $flags & self::OVERWRITE ) && file_exists( $dstPath ) ) {
198 if ( $flags & self::OVERWRITE_SAME ) {
199 $hashSource = sha1_file( $srcPath );
200 $hashDest = sha1_file( $dstPath );
201 if ( $hashSource != $hashDest ) {
202 $status->fatal( 'fileexistserror', $dstPath );
203 }
204 } else {
205 $status->fatal( 'fileexistserror', $dstPath );
206 }
207 }
208 }
209
210 // Windows does not support moving over existing files, so explicitly delete them
211 $deleteDest = wfIsWindows() && ( $flags & self::OVERWRITE );
212
213 // Abort now on failure
214 if ( !$status->ok ) {
215 return $status;
216 }
217
218 // Execute the store operation for each triplet
219 foreach ( $triplets as $i => $triplet ) {
220 list( $srcPath, $dstZone, $dstRel ) = $triplet;
221 $root = $this->getZonePath( $dstZone );
222 $dstPath = "$root/$dstRel";
223 $good = true;
224
225 if ( $flags & self::DELETE_SOURCE ) {
226 if ( $deleteDest ) {
227 unlink( $dstPath );
228 }
229 if ( !rename( $srcPath, $dstPath ) ) {
230 $status->error( 'filerenameerror', $srcPath, $dstPath );
231 $good = false;
232 }
233 } else {
234 if ( !copy( $srcPath, $dstPath ) ) {
235 $status->error( 'filecopyerror', $srcPath, $dstPath );
236 $good = false;
237 }
238 if ( !( $flags & self::SKIP_VALIDATION ) ) {
239 wfSuppressWarnings();
240 $hashSource = sha1_file( $srcPath );
241 $hashDest = sha1_file( $dstPath );
242 wfRestoreWarnings();
243
244 if ( $hashDest === false || $hashSource !== $hashDest ) {
245 wfDebug( __METHOD__ . ': File copy validation failed: ' .
246 "$srcPath ($hashSource) to $dstPath ($hashDest)\n" );
247
248 $status->error( 'filecopyerror', $srcPath, $dstPath );
249 $good = false;
250 }
251 }
252 }
253 if ( $good ) {
254 $this->chmod( $dstPath );
255 $status->successCount++;
256 } else {
257 $status->failCount++;
258 }
259 $status->success[$i] = $good;
260 }
261 return $status;
262 }
263
264 /**
265 * Deletes a batch of files. Each file can be a (zone, rel) pairs, a
266 * virtual url or a real path. It will try to delete each file, but
267 * ignores any errors that may occur
268 *
269 * @param $pairs array List of files to delete
270 */
271 function cleanupBatch( $files ) {
272 foreach ( $files as $file ) {
273 if ( is_array( $file ) ) {
274 // This is a pair, extract it
275 list( $zone, $rel ) = $file;
276 $root = $this->getZonePath( $zone );
277 $path = "$root/$rel";
278 } else {
279 if ( self::isVirtualUrl( $file ) ) {
280 // This is a virtual url, resolve it
281 $path = $this->resolveVirtualUrl( $file );
282 } else {
283 // This is a full file name
284 $path = $file;
285 }
286 }
287
288 wfSuppressWarnings();
289 unlink( $path );
290 wfRestoreWarnings();
291 }
292 }
293
294 function append( $srcPath, $toAppendPath, $flags = 0 ) {
295 $status = $this->newGood();
296
297 // Resolve the virtual URL
298 if ( self::isVirtualUrl( $srcPath ) ) {
299 $srcPath = $this->resolveVirtualUrl( $srcPath );
300 }
301 // Make sure the files are there
302 if ( !is_file( $srcPath ) )
303 $status->fatal( 'filenotfound', $srcPath );
304
305 if ( !is_file( $toAppendPath ) )
306 $status->fatal( 'filenotfound', $toAppendPath );
307
308 if ( !$status->isOk() ) return $status;
309
310 // Do the append
311 $chunk = file_get_contents( $toAppendPath );
312 if( $chunk === false ) {
313 $status->fatal( 'fileappenderrorread', $toAppendPath );
314 }
315
316 if( $status->isOk() ) {
317 if ( file_put_contents( $srcPath, $chunk, FILE_APPEND ) ) {
318 $status->value = $srcPath;
319 } else {
320 $status->fatal( 'fileappenderror', $toAppendPath, $srcPath);
321 }
322 }
323
324 if ( $flags & self::DELETE_SOURCE ) {
325 unlink( $toAppendPath );
326 }
327
328 return $status;
329 }
330
331 /**
332 * Checks existence of specified array of files.
333 *
334 * @param $files Array: URLs of files to check
335 * @param $flags Integer: bitwise combination of the following flags:
336 * self::FILES_ONLY Mark file as existing only if it is a file (not directory)
337 * @return Either array of files and existence flags, or false
338 */
339 function fileExistsBatch( $files, $flags = 0 ) {
340 if ( !file_exists( $this->directory ) || !is_readable( $this->directory ) ) {
341 return false;
342 }
343 $result = array();
344 foreach ( $files as $key => $file ) {
345 if ( self::isVirtualUrl( $file ) ) {
346 $file = $this->resolveVirtualUrl( $file );
347 }
348 if( $flags & self::FILES_ONLY ) {
349 $result[$key] = is_file( $file );
350 } else {
351 $result[$key] = file_exists( $file );
352 }
353 }
354
355 return $result;
356 }
357
358 /**
359 * Take all available measures to prevent web accessibility of new deleted
360 * directories, in case the user has not configured offline storage
361 */
362 protected function initDeletedDir( $dir ) {
363 // Add a .htaccess file to the root of the deleted zone
364 $root = $this->getZonePath( 'deleted' );
365 if ( !file_exists( "$root/.htaccess" ) ) {
366 file_put_contents( "$root/.htaccess", "Deny from all\n" );
367 }
368 // Seed new directories with a blank index.html, to prevent crawling
369 file_put_contents( "$dir/index.html", '' );
370 }
371
372 /**
373 * Pick a random name in the temp zone and store a file to it.
374 * @param $originalName String: the base name of the file as specified
375 * by the user. The file extension will be maintained.
376 * @param $srcPath String: the current location of the file.
377 * @return FileRepoStatus object with the URL in the value.
378 */
379 function storeTemp( $originalName, $srcPath ) {
380 $date = gmdate( "YmdHis" );
381 $hashPath = $this->getHashPath( $originalName );
382 $dstRel = "$hashPath$date!$originalName";
383 $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
384
385 $result = $this->store( $srcPath, 'temp', $dstRel );
386 $result->value = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
387 return $result;
388 }
389
390 /**
391 * Remove a temporary file or mark it for garbage collection
392 * @param $virtualUrl String: the virtual URL returned by storeTemp
393 * @return Boolean: true on success, false on failure
394 */
395 function freeTemp( $virtualUrl ) {
396 $temp = "mwrepo://{$this->name}/temp";
397 if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
398 wfDebug( __METHOD__.": Invalid virtual URL\n" );
399 return false;
400 }
401 $path = $this->resolveVirtualUrl( $virtualUrl );
402 wfSuppressWarnings();
403 $success = unlink( $path );
404 wfRestoreWarnings();
405 return $success;
406 }
407
408 /**
409 * Publish a batch of files
410 * @param $triplets Array: (source,dest,archive) triplets as per publish()
411 * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
412 * that the source files should be deleted if possible
413 */
414 function publishBatch( $triplets, $flags = 0 ) {
415 // Perform initial checks
416 if ( !wfMkdirParents( $this->directory ) ) {
417 return $this->newFatal( 'upload_directory_missing', $this->directory );
418 }
419 if ( !is_writable( $this->directory ) ) {
420 return $this->newFatal( 'upload_directory_read_only', $this->directory );
421 }
422 $status = $this->newGood( array() );
423 foreach ( $triplets as $i => $triplet ) {
424 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
425
426 if ( substr( $srcPath, 0, 9 ) == 'mwrepo://' ) {
427 $triplets[$i][0] = $srcPath = $this->resolveVirtualUrl( $srcPath );
428 }
429 if ( !$this->validateFilename( $dstRel ) ) {
430 throw new MWException( 'Validation error in $dstRel' );
431 }
432 if ( !$this->validateFilename( $archiveRel ) ) {
433 throw new MWException( 'Validation error in $archiveRel' );
434 }
435 $dstPath = "{$this->directory}/$dstRel";
436 $archivePath = "{$this->directory}/$archiveRel";
437
438 $dstDir = dirname( $dstPath );
439 $archiveDir = dirname( $archivePath );
440 // Abort immediately on directory creation errors since they're likely to be repetitive
441 if ( !is_dir( $dstDir ) && !wfMkdirParents( $dstDir ) ) {
442 return $this->newFatal( 'directorycreateerror', $dstDir );
443 }
444 if ( !is_dir( $archiveDir ) && !wfMkdirParents( $archiveDir ) ) {
445 return $this->newFatal( 'directorycreateerror', $archiveDir );
446 }
447 if ( !is_file( $srcPath ) ) {
448 // Make a list of files that don't exist for return to the caller
449 $status->fatal( 'filenotfound', $srcPath );
450 }
451 }
452
453 if ( !$status->ok ) {
454 return $status;
455 }
456
457 foreach ( $triplets as $i => $triplet ) {
458 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
459 $dstPath = "{$this->directory}/$dstRel";
460 $archivePath = "{$this->directory}/$archiveRel";
461
462 // Archive destination file if it exists
463 if( is_file( $dstPath ) ) {
464 // Check if the archive file exists
465 // This is a sanity check to avoid data loss. In UNIX, the rename primitive
466 // unlinks the destination file if it exists. DB-based synchronisation in
467 // publishBatch's caller should prevent races. In Windows there's no
468 // problem because the rename primitive fails if the destination exists.
469 if ( is_file( $archivePath ) ) {
470 $success = false;
471 } else {
472 wfSuppressWarnings();
473 $success = rename( $dstPath, $archivePath );
474 wfRestoreWarnings();
475 }
476
477 if( !$success ) {
478 $status->error( 'filerenameerror',$dstPath, $archivePath );
479 $status->failCount++;
480 continue;
481 } else {
482 wfDebug(__METHOD__.": moved file $dstPath to $archivePath\n");
483 }
484 $status->value[$i] = 'archived';
485 } else {
486 $status->value[$i] = 'new';
487 }
488
489 $good = true;
490 wfSuppressWarnings();
491 if ( $flags & self::DELETE_SOURCE ) {
492 if ( !rename( $srcPath, $dstPath ) ) {
493 $status->error( 'filerenameerror', $srcPath, $dstPath );
494 $good = false;
495 }
496 } else {
497 if ( !copy( $srcPath, $dstPath ) ) {
498 $status->error( 'filecopyerror', $srcPath, $dstPath );
499 $good = false;
500 }
501 }
502 wfRestoreWarnings();
503
504 if ( $good ) {
505 $status->successCount++;
506 wfDebug(__METHOD__.": wrote tempfile $srcPath to $dstPath\n");
507 // Thread-safe override for umask
508 $this->chmod( $dstPath );
509 } else {
510 $status->failCount++;
511 }
512 }
513 return $status;
514 }
515
516 /**
517 * Move a group of files to the deletion archive.
518 * If no valid deletion archive is configured, this may either delete the
519 * file or throw an exception, depending on the preference of the repository.
520 *
521 * @param $sourceDestPairs Array of source/destination pairs. Each element
522 * is a two-element array containing the source file path relative to the
523 * public root in the first element, and the archive file path relative
524 * to the deleted zone root in the second element.
525 * @return FileRepoStatus
526 */
527 function deleteBatch( $sourceDestPairs ) {
528 $status = $this->newGood();
529 if ( !$this->deletedDir ) {
530 throw new MWException( __METHOD__.': no valid deletion archive directory' );
531 }
532
533 /**
534 * Validate filenames and create archive directories
535 */
536 foreach ( $sourceDestPairs as $pair ) {
537 list( $srcRel, $archiveRel ) = $pair;
538 if ( !$this->validateFilename( $srcRel ) ) {
539 throw new MWException( __METHOD__.':Validation error in $srcRel' );
540 }
541 if ( !$this->validateFilename( $archiveRel ) ) {
542 throw new MWException( __METHOD__.':Validation error in $archiveRel' );
543 }
544 $archivePath = "{$this->deletedDir}/$archiveRel";
545 $archiveDir = dirname( $archivePath );
546 if ( !is_dir( $archiveDir ) ) {
547 if ( !wfMkdirParents( $archiveDir ) ) {
548 $status->fatal( 'directorycreateerror', $archiveDir );
549 continue;
550 }
551 $this->initDeletedDir( $archiveDir );
552 }
553 // Check if the archive directory is writable
554 // This doesn't appear to work on NTFS
555 if ( !is_writable( $archiveDir ) ) {
556 $status->fatal( 'filedelete-archive-read-only', $archiveDir );
557 }
558 }
559 if ( !$status->ok ) {
560 // Abort early
561 return $status;
562 }
563
564 /**
565 * Move the files
566 * We're now committed to returning an OK result, which will lead to
567 * the files being moved in the DB also.
568 */
569 foreach ( $sourceDestPairs as $pair ) {
570 list( $srcRel, $archiveRel ) = $pair;
571 $srcPath = "{$this->directory}/$srcRel";
572 $archivePath = "{$this->deletedDir}/$archiveRel";
573 $good = true;
574 if ( file_exists( $archivePath ) ) {
575 # A file with this content hash is already archived
576 if ( !@unlink( $srcPath ) ) {
577 $status->error( 'filedeleteerror', $srcPath );
578 $good = false;
579 }
580 } else{
581 if ( !@rename( $srcPath, $archivePath ) ) {
582 $status->error( 'filerenameerror', $srcPath, $archivePath );
583 $good = false;
584 } else {
585 $this->chmod( $archivePath );
586 }
587 }
588 if ( $good ) {
589 $status->successCount++;
590 } else {
591 $status->failCount++;
592 }
593 }
594 return $status;
595 }
596
597 /**
598 * Get a relative path for a deletion archive key,
599 * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
600 */
601 function getDeletedHashPath( $key ) {
602 $path = '';
603 for ( $i = 0; $i < $this->deletedHashLevels; $i++ ) {
604 $path .= $key[$i] . '/';
605 }
606 return $path;
607 }
608
609 /**
610 * Call a callback function for every file in the repository.
611 * Uses the filesystem even in child classes.
612 */
613 function enumFilesInFS( $callback ) {
614 $numDirs = 1 << ( $this->hashLevels * 4 );
615 for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++ ) {
616 $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
617 $path = $this->directory;
618 for ( $hexPos = 0; $hexPos < $this->hashLevels; $hexPos++ ) {
619 $path .= '/' . substr( $hexString, 0, $hexPos + 1 );
620 }
621 if ( !file_exists( $path ) || !is_dir( $path ) ) {
622 continue;
623 }
624 $dir = opendir( $path );
625 while ( false !== ( $name = readdir( $dir ) ) ) {
626 call_user_func( $callback, $path . '/' . $name );
627 }
628 }
629 }
630
631 /**
632 * Call a callback function for every file in the repository
633 * May use either the database or the filesystem
634 */
635 function enumFiles( $callback ) {
636 $this->enumFilesInFS( $callback );
637 }
638
639 /**
640 * Get properties of a file with a given virtual URL
641 * The virtual URL must refer to this repo
642 */
643 function getFileProps( $virtualUrl ) {
644 $path = $this->resolveVirtualUrl( $virtualUrl );
645 return File::getPropsFromPath( $path );
646 }
647
648 /**
649 * Path disclosure protection functions
650 *
651 * Get a callback function to use for cleaning error message parameters
652 */
653 function getErrorCleanupFunction() {
654 switch ( $this->pathDisclosureProtection ) {
655 case 'simple':
656 $callback = array( $this, 'simpleClean' );
657 break;
658 default:
659 $callback = parent::getErrorCleanupFunction();
660 }
661 return $callback;
662 }
663
664 function simpleClean( $param ) {
665 if ( !isset( $this->simpleCleanPairs ) ) {
666 global $IP;
667 $this->simpleCleanPairs = array(
668 $this->directory => 'public',
669 "{$this->directory}/temp" => 'temp',
670 $IP => '$IP',
671 dirname( __FILE__ ) => '$IP/extensions/WebStore',
672 );
673 if ( $this->deletedDir ) {
674 $this->simpleCleanPairs[$this->deletedDir] = 'deleted';
675 }
676 }
677 return strtr( $param, $this->simpleCleanPairs );
678 }
679
680 /**
681 * Chmod a file, supressing the warnings.
682 * @param $path String: the path to change
683 */
684 protected function chmod( $path ) {
685 wfSuppressWarnings();
686 chmod( $path, $this->fileMode );
687 wfRestoreWarnings();
688 }
689
690 }