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