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