]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/filerepo/FSRepo.php
MediaWiki 1.17.4
[autoinstalls/mediawiki.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                 if ( !wfMkdirParents( $this->directory ) ) {
150                         return $this->newFatal( 'upload_directory_missing', $this->directory );
151                 }
152                 if ( !is_writable( $this->directory ) ) {
153                         return $this->newFatal( 'upload_directory_read_only', $this->directory );
154                 }
155                 $status = $this->newGood();
156                 foreach ( $triplets as $i => $triplet ) {
157                         list( $srcPath, $dstZone, $dstRel ) = $triplet;
158
159                         $root = $this->getZonePath( $dstZone );
160                         if ( !$root ) {
161                                 throw new MWException( "Invalid zone: $dstZone" );
162                         }
163                         if ( !$this->validateFilename( $dstRel ) ) {
164                                 throw new MWException( 'Validation error in $dstRel' );
165                         }
166                         $dstPath = "$root/$dstRel";
167                         $dstDir = dirname( $dstPath );
168
169                         if ( !is_dir( $dstDir ) ) {
170                                 if ( !wfMkdirParents( $dstDir ) ) {
171                                         return $this->newFatal( 'directorycreateerror', $dstDir );
172                                 }
173                                 if ( $dstZone == 'deleted' ) {
174                                         $this->initDeletedDir( $dstDir );
175                                 }
176                         }
177
178                         if ( self::isVirtualUrl( $srcPath ) ) {
179                                 $srcPath = $triplets[$i][0] = $this->resolveVirtualUrl( $srcPath );
180                         }
181                         if ( !is_file( $srcPath ) ) {
182                                 // Make a list of files that don't exist for return to the caller
183                                 $status->fatal( 'filenotfound', $srcPath );
184                                 continue;
185                         }
186                         if ( !( $flags & self::OVERWRITE ) && file_exists( $dstPath ) ) {
187                                 if ( $flags & self::OVERWRITE_SAME ) {
188                                         $hashSource = sha1_file( $srcPath );
189                                         $hashDest = sha1_file( $dstPath );
190                                         if ( $hashSource != $hashDest ) {
191                                                 $status->fatal( 'fileexistserror', $dstPath );
192                                         }
193                                 } else {
194                                         $status->fatal( 'fileexistserror', $dstPath );
195                                 }
196                         }
197                 }
198
199                 $deleteDest = wfIsWindows() && ( $flags & self::OVERWRITE );
200
201                 // Abort now on failure
202                 if ( !$status->ok ) {
203                         return $status;
204                 }
205
206                 foreach ( $triplets as $triplet ) {
207                         list( $srcPath, $dstZone, $dstRel ) = $triplet;
208                         $root = $this->getZonePath( $dstZone );
209                         $dstPath = "$root/$dstRel";
210                         $good = true;
211
212                         if ( $flags & self::DELETE_SOURCE ) {
213                                 if ( $deleteDest ) {
214                                         unlink( $dstPath );
215                                 }
216                                 if ( !rename( $srcPath, $dstPath ) ) {
217                                         $status->error( 'filerenameerror', $srcPath, $dstPath );
218                                         $good = false;
219                                 }
220                         } else {
221                                 if ( !copy( $srcPath, $dstPath ) ) {
222                                         $status->error( 'filecopyerror', $srcPath, $dstPath );
223                                         $good = false;
224                                 }
225                         }
226                         if ( $good ) {
227                                 $this->chmod( $dstPath );
228                                 $status->successCount++;
229                         } else {
230                                 $status->failCount++;
231                         }
232                 }
233                 return $status;
234         }
235
236         function append( $srcPath, $toAppendPath, $flags = 0 ) {
237                 $status = $this->newGood();
238
239                 // Resolve the virtual URL
240                 if ( self::isVirtualUrl( $srcPath ) ) {
241                         $srcPath = $this->resolveVirtualUrl( $srcPath );
242                 }
243                 // Make sure the files are there
244                 if ( !is_file( $srcPath ) )
245                         $status->fatal( 'filenotfound', $srcPath );
246
247                 if ( !is_file( $toAppendPath ) )
248                         $status->fatal( 'filenotfound', $toAppendPath );
249
250                 if ( !$status->isOk() ) return $status;
251
252                 // Do the append
253                 $chunk = file_get_contents( $toAppendPath );
254                 if( $chunk === false ) {
255                         $status->fatal( 'fileappenderrorread', $toAppendPath );
256                 }
257
258                 if( $status->isOk() ) {
259                         if ( file_put_contents( $srcPath, $chunk, FILE_APPEND ) ) {
260                                 $status->value = $srcPath;
261                         } else {
262                                 $status->fatal( 'fileappenderror', $toAppendPath,  $srcPath);
263                         }
264                 }
265
266                 if ( $flags & self::DELETE_SOURCE ) {
267                         unlink( $toAppendPath );
268                 }
269
270                 return $status;
271         }
272
273         /**
274          * Checks existence of specified array of files.
275          *
276          * @param $files Array: URLs of files to check
277          * @param $flags Integer: bitwise combination of the following flags:
278          *     self::FILES_ONLY     Mark file as existing only if it is a file (not directory)
279          * @return Either array of files and existence flags, or false
280          */
281         function fileExistsBatch( $files, $flags = 0 ) {
282                 if ( !file_exists( $this->directory ) || !is_readable( $this->directory ) ) {
283                         return false;
284                 }
285                 $result = array();
286                 foreach ( $files as $key => $file ) {
287                         if ( self::isVirtualUrl( $file ) ) {
288                                 $file = $this->resolveVirtualUrl( $file );
289                         }
290                         if( $flags & self::FILES_ONLY ) {
291                                 $result[$key] = is_file( $file );
292                         } else {
293                                 $result[$key] = file_exists( $file );
294                         }
295                 }
296
297                 return $result;
298         }
299
300         /**
301          * Take all available measures to prevent web accessibility of new deleted
302          * directories, in case the user has not configured offline storage
303          */
304         protected function initDeletedDir( $dir ) {
305                 // Add a .htaccess file to the root of the deleted zone
306                 $root = $this->getZonePath( 'deleted' );
307                 if ( !file_exists( "$root/.htaccess" ) ) {
308                         file_put_contents( "$root/.htaccess", "Deny from all\n" );
309                 }
310                 // Seed new directories with a blank index.html, to prevent crawling
311                 file_put_contents( "$dir/index.html", '' );
312         }
313
314         /**
315          * Pick a random name in the temp zone and store a file to it.
316          * @param $originalName String: the base name of the file as specified
317          *     by the user. The file extension will be maintained.
318          * @param $srcPath String: the current location of the file.
319          * @return FileRepoStatus object with the URL in the value.
320          */
321         function storeTemp( $originalName, $srcPath ) {
322                 $date = gmdate( "YmdHis" );
323                 $hashPath = $this->getHashPath( $originalName );
324                 $dstRel = "$hashPath$date!$originalName";
325                 $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
326
327                 $result = $this->store( $srcPath, 'temp', $dstRel );
328                 $result->value = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
329                 return $result;
330         }
331
332         /**
333          * Remove a temporary file or mark it for garbage collection
334          * @param $virtualUrl String: the virtual URL returned by storeTemp
335          * @return Boolean: true on success, false on failure
336          */
337         function freeTemp( $virtualUrl ) {
338                 $temp = "mwrepo://{$this->name}/temp";
339                 if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
340                         wfDebug( __METHOD__.": Invalid virtual URL\n" );
341                         return false;
342                 }
343                 $path = $this->resolveVirtualUrl( $virtualUrl );
344                 wfSuppressWarnings();
345                 $success = unlink( $path );
346                 wfRestoreWarnings();
347                 return $success;
348         }
349
350         /**
351          * Publish a batch of files
352          * @param $triplets Array: (source,dest,archive) triplets as per publish()
353          * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
354          *        that the source files should be deleted if possible
355          */
356         function publishBatch( $triplets, $flags = 0 ) {
357                 // Perform initial checks
358                 if ( !wfMkdirParents( $this->directory ) ) {
359                         return $this->newFatal( 'upload_directory_missing', $this->directory );
360                 }
361                 if ( !is_writable( $this->directory ) ) {
362                         return $this->newFatal( 'upload_directory_read_only', $this->directory );
363                 }
364                 $status = $this->newGood( array() );
365                 foreach ( $triplets as $i => $triplet ) {
366                         list( $srcPath, $dstRel, $archiveRel ) = $triplet;
367
368                         if ( substr( $srcPath, 0, 9 ) == 'mwrepo://' ) {
369                                 $triplets[$i][0] = $srcPath = $this->resolveVirtualUrl( $srcPath );
370                         }
371                         if ( !$this->validateFilename( $dstRel ) ) {
372                                 throw new MWException( 'Validation error in $dstRel' );
373                         }
374                         if ( !$this->validateFilename( $archiveRel ) ) {
375                                 throw new MWException( 'Validation error in $archiveRel' );
376                         }
377                         $dstPath = "{$this->directory}/$dstRel";
378                         $archivePath = "{$this->directory}/$archiveRel";
379
380                         $dstDir = dirname( $dstPath );
381                         $archiveDir = dirname( $archivePath );
382                         // Abort immediately on directory creation errors since they're likely to be repetitive
383                         if ( !is_dir( $dstDir ) && !wfMkdirParents( $dstDir ) ) {
384                                 return $this->newFatal( 'directorycreateerror', $dstDir );
385                         }
386                         if ( !is_dir( $archiveDir ) && !wfMkdirParents( $archiveDir ) ) {
387                                 return $this->newFatal( 'directorycreateerror', $archiveDir );
388                         }
389                         if ( !is_file( $srcPath ) ) {
390                                 // Make a list of files that don't exist for return to the caller
391                                 $status->fatal( 'filenotfound', $srcPath );
392                         }
393                 }
394
395                 if ( !$status->ok ) {
396                         return $status;
397                 }
398
399                 foreach ( $triplets as $i => $triplet ) {
400                         list( $srcPath, $dstRel, $archiveRel ) = $triplet;
401                         $dstPath = "{$this->directory}/$dstRel";
402                         $archivePath = "{$this->directory}/$archiveRel";
403
404                         // Archive destination file if it exists
405                         if( is_file( $dstPath ) ) {
406                                 // Check if the archive file exists
407                                 // This is a sanity check to avoid data loss. In UNIX, the rename primitive
408                                 // unlinks the destination file if it exists. DB-based synchronisation in
409                                 // publishBatch's caller should prevent races. In Windows there's no
410                                 // problem because the rename primitive fails if the destination exists.
411                                 if ( is_file( $archivePath ) ) {
412                                         $success = false;
413                                 } else {
414                                         wfSuppressWarnings();
415                                         $success = rename( $dstPath, $archivePath );
416                                         wfRestoreWarnings();
417                                 }
418
419                                 if( !$success ) {
420                                         $status->error( 'filerenameerror',$dstPath, $archivePath );
421                                         $status->failCount++;
422                                         continue;
423                                 } else {
424                                         wfDebug(__METHOD__.": moved file $dstPath to $archivePath\n");
425                                 }
426                                 $status->value[$i] = 'archived';
427                         } else {
428                                 $status->value[$i] = 'new';
429                         }
430
431                         $good = true;
432                         wfSuppressWarnings();
433                         if ( $flags & self::DELETE_SOURCE ) {
434                                 if ( !rename( $srcPath, $dstPath ) ) {
435                                         $status->error( 'filerenameerror', $srcPath, $dstPath );
436                                         $good = false;
437                                 }
438                         } else {
439                                 if ( !copy( $srcPath, $dstPath ) ) {
440                                         $status->error( 'filecopyerror', $srcPath, $dstPath );
441                                         $good = false;
442                                 }
443                         }
444                         wfRestoreWarnings();
445
446                         if ( $good ) {
447                                 $status->successCount++;
448                                 wfDebug(__METHOD__.": wrote tempfile $srcPath to $dstPath\n");
449                                 // Thread-safe override for umask
450                                 $this->chmod( $dstPath );
451                         } else {
452                                 $status->failCount++;
453                         }
454                 }
455                 return $status;
456         }
457
458         /**
459          * Move a group of files to the deletion archive.
460          * If no valid deletion archive is configured, this may either delete the
461          * file or throw an exception, depending on the preference of the repository.
462          *
463          * @param $sourceDestPairs Array of source/destination pairs. Each element
464          *        is a two-element array containing the source file path relative to the
465          *        public root in the first element, and the archive file path relative
466          *        to the deleted zone root in the second element.
467          * @return FileRepoStatus
468          */
469         function deleteBatch( $sourceDestPairs ) {
470                 $status = $this->newGood();
471                 if ( !$this->deletedDir ) {
472                         throw new MWException( __METHOD__.': no valid deletion archive directory' );
473                 }
474
475                 /**
476                  * Validate filenames and create archive directories
477                  */
478                 foreach ( $sourceDestPairs as $pair ) {
479                         list( $srcRel, $archiveRel ) = $pair;
480                         if ( !$this->validateFilename( $srcRel ) ) {
481                                 throw new MWException( __METHOD__.':Validation error in $srcRel' );
482                         }
483                         if ( !$this->validateFilename( $archiveRel ) ) {
484                                 throw new MWException( __METHOD__.':Validation error in $archiveRel' );
485                         }
486                         $archivePath = "{$this->deletedDir}/$archiveRel";
487                         $archiveDir = dirname( $archivePath );
488                         if ( !is_dir( $archiveDir ) ) {
489                                 if ( !wfMkdirParents( $archiveDir ) ) {
490                                         $status->fatal( 'directorycreateerror', $archiveDir );
491                                         continue;
492                                 }
493                                 $this->initDeletedDir( $archiveDir );
494                         }
495                         // Check if the archive directory is writable
496                         // This doesn't appear to work on NTFS
497                         if ( !is_writable( $archiveDir ) ) {
498                                 $status->fatal( 'filedelete-archive-read-only', $archiveDir );
499                         }
500                 }
501                 if ( !$status->ok ) {
502                         // Abort early
503                         return $status;
504                 }
505
506                 /**
507                  * Move the files
508                  * We're now committed to returning an OK result, which will lead to
509                  * the files being moved in the DB also.
510                  */
511                 foreach ( $sourceDestPairs as $pair ) {
512                         list( $srcRel, $archiveRel ) = $pair;
513                         $srcPath = "{$this->directory}/$srcRel";
514                         $archivePath = "{$this->deletedDir}/$archiveRel";
515                         $good = true;
516                         if ( file_exists( $archivePath ) ) {
517                                 # A file with this content hash is already archived
518                                 if ( !@unlink( $srcPath ) ) {
519                                         $status->error( 'filedeleteerror', $srcPath );
520                                         $good = false;
521                                 }
522                         } else{
523                                 if ( !@rename( $srcPath, $archivePath ) ) {
524                                         $status->error( 'filerenameerror', $srcPath, $archivePath );
525                                         $good = false;
526                                 } else {
527                                         $this->chmod( $archivePath );
528                                 }
529                         }
530                         if ( $good ) {
531                                 $status->successCount++;
532                         } else {
533                                 $status->failCount++;
534                         }
535                 }
536                 return $status;
537         }
538
539         /**
540          * Get a relative path for a deletion archive key,
541          * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
542          */
543         function getDeletedHashPath( $key ) {
544                 $path = '';
545                 for ( $i = 0; $i < $this->deletedHashLevels; $i++ ) {
546                         $path .= $key[$i] . '/';
547                 }
548                 return $path;
549         }
550
551         /**
552          * Call a callback function for every file in the repository.
553          * Uses the filesystem even in child classes.
554          */
555         function enumFilesInFS( $callback ) {
556                 $numDirs = 1 << ( $this->hashLevels * 4 );
557                 for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++ ) {
558                         $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
559                         $path = $this->directory;
560                         for ( $hexPos = 0; $hexPos < $this->hashLevels; $hexPos++ ) {
561                                 $path .= '/' . substr( $hexString, 0, $hexPos + 1 );
562                         }
563                         if ( !file_exists( $path ) || !is_dir( $path ) ) {
564                                 continue;
565                         }
566                         $dir = opendir( $path );
567                         while ( false !== ( $name = readdir( $dir ) ) ) {
568                                 call_user_func( $callback, $path . '/' . $name );
569                         }
570                 }
571         }
572
573         /**
574          * Call a callback function for every file in the repository
575          * May use either the database or the filesystem
576          */
577         function enumFiles( $callback ) {
578                 $this->enumFilesInFS( $callback );
579         }
580
581         /**
582          * Get properties of a file with a given virtual URL
583          * The virtual URL must refer to this repo
584          */
585         function getFileProps( $virtualUrl ) {
586                 $path = $this->resolveVirtualUrl( $virtualUrl );
587                 return File::getPropsFromPath( $path );
588         }
589
590         /**
591          * Path disclosure protection functions
592          *
593          * Get a callback function to use for cleaning error message parameters
594          */
595         function getErrorCleanupFunction() {
596                 switch ( $this->pathDisclosureProtection ) {
597                         case 'simple':
598                                 $callback = array( $this, 'simpleClean' );
599                                 break;
600                         default:
601                                 $callback = parent::getErrorCleanupFunction();
602                 }
603                 return $callback;
604         }
605
606         function simpleClean( $param ) {
607                 if ( !isset( $this->simpleCleanPairs ) ) {
608                         global $IP;
609                         $this->simpleCleanPairs = array(
610                                 $this->directory => 'public',
611                                 "{$this->directory}/temp" => 'temp',
612                                 $IP => '$IP',
613                                 dirname( __FILE__ ) => '$IP/extensions/WebStore',
614                         );
615                         if ( $this->deletedDir ) {
616                                 $this->simpleCleanPairs[$this->deletedDir] = 'deleted';
617                         }
618                 }
619                 return strtr( $param, $this->simpleCleanPairs );
620         }
621
622         /**
623          * Chmod a file, supressing the warnings.
624          * @param $path String: the path to change
625          */
626         protected function chmod( $path ) {
627                 wfSuppressWarnings();
628                 chmod( $path, $this->fileMode );
629                 wfRestoreWarnings();
630         }
631
632 }