]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/filerepo/FSRepo.php
MediaWiki 1.11.0
[autoinstalls/mediawiki.git] / includes / filerepo / FSRepo.php
1 <?php
2
3 /**
4  * A repository for files accessible via the local filesystem. Does not support
5  * database access or registration.
6  */
7
8 class FSRepo extends FileRepo {
9         var $directory, $deletedDir, $url, $hashLevels, $deletedHashLevels;
10         var $fileFactory = array( 'UnregisteredLocalFile', 'newFromTitle' );
11         var $oldFileFactory = false;
12         var $pathDisclosureProtection = 'simple';
13
14         function __construct( $info ) {
15                 parent::__construct( $info );
16
17                 // Required settings
18                 $this->directory = $info['directory'];
19                 $this->url = $info['url'];
20
21                 // Optional settings
22                 $this->hashLevels = isset( $info['hashLevels'] ) ? $info['hashLevels'] : 2;
23                 $this->deletedHashLevels = isset( $info['deletedHashLevels'] ) ? 
24                         $info['deletedHashLevels'] : $this->hashLevels;
25                 $this->deletedDir = isset( $info['deletedDir'] ) ? $info['deletedDir'] : false;
26         }
27
28         /**
29          * Get the public root directory of the repository.
30          */
31         function getRootDirectory() {
32                 return $this->directory;
33         }
34
35         /**
36          * Get the public root URL of the repository
37          */
38         function getRootUrl() {
39                 return $this->url;
40         }
41
42         /**
43          * Returns true if the repository uses a multi-level directory structure
44          */
45         function isHashed() {
46                 return (bool)$this->hashLevels;
47         }
48
49         /**
50          * Get the local directory corresponding to one of the three basic zones
51          */
52         function getZonePath( $zone ) {
53                 switch ( $zone ) {
54                         case 'public':
55                                 return $this->directory;
56                         case 'temp':
57                                 return "{$this->directory}/temp";
58                         case 'deleted':
59                                 return $this->deletedDir;
60                         default:
61                                 return false;
62                 }
63         }
64
65         /**
66          * Get the URL corresponding to one of the three basic zones
67          */
68         function getZoneUrl( $zone ) {
69                 switch ( $zone ) {
70                         case 'public':
71                                 return $this->url;
72                         case 'temp':
73                                 return "{$this->url}/temp";
74                         case 'deleted':
75                                 return false; // no public URL
76                         default:
77                                 return false;
78                 }
79         }
80
81         /**
82          * Get a URL referring to this repository, with the private mwrepo protocol.
83          * The suffix, if supplied, is considered to be unencoded, and will be 
84          * URL-encoded before being returned.
85          */
86         function getVirtualUrl( $suffix = false ) {
87                 $path = 'mwrepo://' . $this->name;
88                 if ( $suffix !== false ) {
89                         $path .= '/' . rawurlencode( $suffix );
90                 }
91                 return $path;
92         }
93
94         /**
95          * Get the local path corresponding to a virtual URL
96          */
97         function resolveVirtualUrl( $url ) {
98                 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
99                         throw new MWException( __METHOD__.': unknown protoocl' );
100                 }
101
102                 $bits = explode( '/', substr( $url, 9 ), 3 );
103                 if ( count( $bits ) != 3 ) {
104                         throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
105                 }
106                 list( $repo, $zone, $rel ) = $bits;
107                 if ( $repo !== $this->name ) {
108                         throw new MWException( __METHOD__.": fetching from a foreign repo is not supported" );
109                 }
110                 $base = $this->getZonePath( $zone );
111                 if ( !$base ) {
112                         throw new MWException( __METHOD__.": invalid zone: $zone" );
113                 }
114                 return $base . '/' . rawurldecode( $rel );
115         }
116
117         /**
118          * Store a batch of files
119          *
120          * @param array $triplets (src,zone,dest) triplets as per store()
121          * @param integer $flags Bitwise combination of the following flags:
122          *     self::DELETE_SOURCE     Delete the source file after upload
123          *     self::OVERWRITE         Overwrite an existing destination file instead of failing
124          *     self::OVERWRITE_SAME    Overwrite the file if the destination exists and has the 
125          *                             same contents as the source
126          */
127         function storeBatch( $triplets, $flags = 0 ) {
128                 if ( !is_writable( $this->directory ) ) {
129                         return $this->newFatal( 'upload_directory_read_only', $this->directory );
130                 }
131                 $status = $this->newGood();
132                 foreach ( $triplets as $i => $triplet ) {
133                         list( $srcPath, $dstZone, $dstRel ) = $triplet;
134
135                         $root = $this->getZonePath( $dstZone );
136                         if ( !$root ) {
137                                 throw new MWException( "Invalid zone: $dstZone" );
138                         }
139                         if ( !$this->validateFilename( $dstRel ) ) {
140                                 throw new MWException( 'Validation error in $dstRel' );
141                         }
142                         $dstPath = "$root/$dstRel";
143                         $dstDir = dirname( $dstPath );
144
145                         if ( !is_dir( $dstDir ) ) {
146                                 if ( !wfMkdirParents( $dstDir ) ) {
147                                         return $this->newFatal( 'directorycreateerror', $dstDir );
148                                 }
149                                 // In the deleted zone, seed new directories with a blank 
150                                 // index.html, to prevent crawling
151                                 if ( $dstZone == 'deleted' ) {
152                                         file_put_contents( "$dstDir/index.html", '' );
153                                 }
154                         }
155                         
156                         if ( self::isVirtualUrl( $srcPath ) ) {
157                                 $srcPath = $triplets[$i][0] = $this->resolveVirtualUrl( $srcPath );
158                         }
159                         if ( !is_file( $srcPath ) ) {
160                                 // Make a list of files that don't exist for return to the caller
161                                 $status->fatal( 'filenotfound', $srcPath );
162                                 continue;
163                         }
164                         if ( !( $flags & self::OVERWRITE ) && file_exists( $dstPath ) ) {
165                                 if ( $flags & self::OVERWRITE_SAME ) {
166                                         $hashSource = sha1_file( $srcPath );
167                                         $hashDest = sha1_file( $dstPath );
168                                         if ( $hashSource != $hashDest ) {
169                                                 $status->fatal( 'fileexistserror', $dstPath );
170                                         }
171                                 } else {
172                                         $status->fatal( 'fileexistserror', $dstPath );
173                                 }
174                         }
175                 }
176
177                 $deleteDest = wfIsWindows() && ( $flags & self::OVERWRITE );
178
179                 // Abort now on failure
180                 if ( !$status->ok ) {
181                         return $status;
182                 }
183
184                 foreach ( $triplets as $triplet ) {
185                         list( $srcPath, $dstZone, $dstRel ) = $triplet;
186                         $root = $this->getZonePath( $dstZone );
187                         $dstPath = "$root/$dstRel";
188                         $good = true;
189
190                         if ( $flags & self::DELETE_SOURCE ) {
191                                 if ( $deleteDest ) {
192                                         unlink( $dstPath );
193                                 }
194                                 if ( !rename( $srcPath, $dstPath ) ) {
195                                         $status->error( 'filerenameerror', $srcPath, $dstPath );
196                                         $good = false;
197                                 }
198                         } else {
199                                 if ( !copy( $srcPath, $dstPath ) ) {
200                                         $status->error( 'filecopyerror', $srcPath, $dstPath );
201                                         $good = false;
202                                 }
203                         }
204                         if ( $good ) {
205                                 chmod( $dstPath, 0644 );
206                                 $status->successCount++;
207                         } else {
208                                 $status->failCount++;
209                         }
210                 }
211                 return $status;
212         }
213
214         /**
215          * Pick a random name in the temp zone and store a file to it.
216          * @param string $originalName The base name of the file as specified 
217          *     by the user. The file extension will be maintained.
218          * @param string $srcPath The current location of the file.
219          * @return FileRepoStatus object with the URL in the value.
220          */
221         function storeTemp( $originalName, $srcPath ) {
222                 $date = gmdate( "YmdHis" );
223                 $hashPath = $this->getHashPath( $originalName );
224                 $dstRel = "$hashPath$date!$originalName";
225                 $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
226
227                 $result = $this->store( $srcPath, 'temp', $dstRel );
228                 $result->value = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
229                 return $result;
230         }
231
232         /**
233          * Remove a temporary file or mark it for garbage collection
234          * @param string $virtualUrl The virtual URL returned by storeTemp
235          * @return boolean True on success, false on failure
236          */
237         function freeTemp( $virtualUrl ) {
238                 $temp = "mwrepo://{$this->name}/temp";
239                 if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
240                         wfDebug( __METHOD__.": Invalid virtual URL\n" );
241                         return false;
242                 }
243                 $path = $this->resolveVirtualUrl( $virtualUrl );
244                 wfSuppressWarnings();
245                 $success = unlink( $path );
246                 wfRestoreWarnings();
247                 return $success;
248         }
249
250         /**
251          * Publish a batch of files
252          * @param array $triplets (source,dest,archive) triplets as per publish()
253          * @param integer $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
254          *        that the source files should be deleted if possible
255          */
256         function publishBatch( $triplets, $flags = 0 ) {
257                 // Perform initial checks
258                 if ( !is_writable( $this->directory ) ) {
259                         return $this->newFatal( 'upload_directory_read_only', $this->directory );
260                 }
261                 $status = $this->newGood( array() );
262                 foreach ( $triplets as $i => $triplet ) {
263                         list( $srcPath, $dstRel, $archiveRel ) = $triplet;
264
265                         if ( substr( $srcPath, 0, 9 ) == 'mwrepo://' ) {
266                                 $triplets[$i][0] = $srcPath = $this->resolveVirtualUrl( $srcPath );
267                         }
268                         if ( !$this->validateFilename( $dstRel ) ) {
269                                 throw new MWException( 'Validation error in $dstRel' );
270                         }
271                         if ( !$this->validateFilename( $archiveRel ) ) {
272                                 throw new MWException( 'Validation error in $archiveRel' );
273                         }
274                         $dstPath = "{$this->directory}/$dstRel";
275                         $archivePath = "{$this->directory}/$archiveRel";
276                         
277                         $dstDir = dirname( $dstPath );
278                         $archiveDir = dirname( $archivePath );
279                         // Abort immediately on directory creation errors since they're likely to be repetitive
280                         if ( !is_dir( $dstDir ) && !wfMkdirParents( $dstDir ) ) {
281                                 return $this->newFatal( 'directorycreateerror', $dstDir );
282                         }
283                         if ( !is_dir( $archiveDir ) && !wfMkdirParents( $archiveDir ) ) {
284                                 return $this->newFatal( 'directorycreateerror', $archiveDir );
285                         }
286                         if ( !is_file( $srcPath ) ) {
287                                 // Make a list of files that don't exist for return to the caller
288                                 $status->fatal( 'filenotfound', $srcPath );
289                         }
290                 }
291
292                 if ( !$status->ok ) {
293                         return $status;
294                 }
295                 
296                 foreach ( $triplets as $i => $triplet ) {
297                         list( $srcPath, $dstRel, $archiveRel ) = $triplet;
298                         $dstPath = "{$this->directory}/$dstRel";
299                         $archivePath = "{$this->directory}/$archiveRel";
300
301                         // Archive destination file if it exists
302                         if( is_file( $dstPath ) ) {
303                                 // Check if the archive file exists
304                                 // This is a sanity check to avoid data loss. In UNIX, the rename primitive
305                                 // unlinks the destination file if it exists. DB-based synchronisation in 
306                                 // publishBatch's caller should prevent races. In Windows there's no 
307                                 // problem because the rename primitive fails if the destination exists.
308                                 if ( is_file( $archivePath ) ) {
309                                         $success = false;
310                                 } else {
311                                         wfSuppressWarnings();
312                                         $success = rename( $dstPath, $archivePath );
313                                         wfRestoreWarnings();
314                                 }
315
316                                 if( !$success ) {
317                                         $status->error( 'filerenameerror',$dstPath, $archivePath );
318                                         $status->failCount++;
319                                         continue;
320                                 } else {
321                                         wfDebug(__METHOD__.": moved file $dstPath to $archivePath\n");
322                                 }
323                                 $status->value[$i] = 'archived';
324                         } else {
325                                 $status->value[$i] = 'new';
326                         }
327
328                         $good = true;
329                         wfSuppressWarnings();
330                         if ( $flags & self::DELETE_SOURCE ) {
331                                 if ( !rename( $srcPath, $dstPath ) ) {
332                                         $status->error( 'filerenameerror', $srcPath, $dstPath );
333                                         $good = false;
334                                 }
335                         } else {
336                                 if ( !copy( $srcPath, $dstPath ) ) {
337                                         $status->error( 'filecopyerror', $srcPath, $dstPath );
338                                         $good = false;
339                                 }
340                         }
341                         wfRestoreWarnings();
342
343                         if ( $good ) {
344                                 $status->successCount++;
345                                 wfDebug(__METHOD__.": wrote tempfile $srcPath to $dstPath\n");
346                                 // Thread-safe override for umask
347                                 chmod( $dstPath, 0644 );
348                         } else {
349                                 $status->failCount++;
350                         }
351                 }
352                 return $status;
353         }
354
355         /**
356          * Move a group of files to the deletion archive.
357          * If no valid deletion archive is configured, this may either delete the 
358          * file or throw an exception, depending on the preference of the repository.
359          *
360          * @param array $sourceDestPairs Array of source/destination pairs. Each element 
361          *        is a two-element array containing the source file path relative to the
362          *        public root in the first element, and the archive file path relative 
363          *        to the deleted zone root in the second element.
364          * @return FileRepoStatus
365          */
366         function deleteBatch( $sourceDestPairs ) {
367                 $status = $this->newGood();
368                 if ( !$this->deletedDir ) {
369                         throw new MWException( __METHOD__.': no valid deletion archive directory' );
370                 }
371
372                 /**
373                  * Validate filenames and create archive directories
374                  */
375                 foreach ( $sourceDestPairs as $pair ) {
376                         list( $srcRel, $archiveRel ) = $pair;
377                         if ( !$this->validateFilename( $srcRel ) ) {
378                                 throw new MWException( __METHOD__.':Validation error in $srcRel' );
379                         }
380                         if ( !$this->validateFilename( $archiveRel ) ) {
381                                 throw new MWException( __METHOD__.':Validation error in $archiveRel' );
382                         }
383                         $archivePath = "{$this->deletedDir}/$archiveRel";
384                         $archiveDir = dirname( $archivePath );
385                         if ( !is_dir( $archiveDir ) ) {
386                                 if ( !wfMkdirParents( $archiveDir ) ) {
387                                         $status->fatal( 'directorycreateerror', $archiveDir );
388                                         continue;
389                                 }
390                                 // Seed new directories with a blank index.html, to prevent crawling
391                                 file_put_contents( "$archiveDir/index.html", '' );
392                         }
393                         // Check if the archive directory is writable
394                         // This doesn't appear to work on NTFS
395                         if ( !is_writable( $archiveDir ) ) {
396                                 $status->fatal( 'filedelete-archive-read-only', $archiveDir );
397                         }
398                 }
399                 if ( !$status->ok ) {
400                         // Abort early
401                         return $status;
402                 }
403
404                 /**
405                  * Move the files
406                  * We're now committed to returning an OK result, which will lead to 
407                  * the files being moved in the DB also.
408                  */
409                 foreach ( $sourceDestPairs as $pair ) {
410                         list( $srcRel, $archiveRel ) = $pair;
411                         $srcPath = "{$this->directory}/$srcRel";
412                         $archivePath = "{$this->deletedDir}/$archiveRel";
413                         $good = true;
414                         if ( file_exists( $archivePath ) ) {
415                                 # A file with this content hash is already archived
416                                 if ( !@unlink( $srcPath ) ) {
417                                         $status->error( 'filedeleteerror', $srcPath );
418                                         $good = false;
419                                 }
420                         } else{
421                                 if ( !@rename( $srcPath, $archivePath ) ) {
422                                         $status->error( 'filerenameerror', $srcPath, $archivePath );
423                                         $good = false;
424                                 } else {
425                                         chmod( $archivePath, 0644 );
426                                 }
427                         }
428                         if ( $good ) {
429                                 $status->successCount++;
430                         } else {
431                                 $status->failCount++;
432                         }
433                 }
434                 return $status;
435         }
436         
437         /**
438          * Get a relative path including trailing slash, e.g. f/fa/
439          * If the repo is not hashed, returns an empty string
440          */
441         function getHashPath( $name ) {
442                 return FileRepo::getHashPathForLevel( $name, $this->hashLevels );
443         }
444
445         /**
446          * Get a relative path for a deletion archive key, 
447          * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
448          */
449         function getDeletedHashPath( $key ) {
450                 $path = '';
451                 for ( $i = 0; $i < $this->deletedHashLevels; $i++ ) {
452                         $path .= $key[$i] . '/';
453                 }
454                 return $path;
455         }
456         
457         /**
458          * Call a callback function for every file in the repository.
459          * Uses the filesystem even in child classes.
460          */
461         function enumFilesInFS( $callback ) {
462                 $numDirs = 1 << ( $this->hashLevels * 4 );
463                 for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++ ) {
464                         $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
465                         $path = $this->directory;
466                         for ( $hexPos = 0; $hexPos < $this->hashLevels; $hexPos++ ) {
467                                 $path .= '/' . substr( $hexString, 0, $hexPos + 1 );
468                         }
469                         if ( !file_exists( $path ) || !is_dir( $path ) ) {
470                                 continue;
471                         }
472                         $dir = opendir( $path );
473                         while ( false !== ( $name = readdir( $dir ) ) ) {
474                                 call_user_func( $callback, $path . '/' . $name );
475                         }
476                 }
477         }
478
479         /**
480          * Call a callback function for every file in the repository
481          * May use either the database or the filesystem
482          */
483         function enumFiles( $callback ) {
484                 $this->enumFilesInFS( $callback );
485         }
486
487         /**
488          * Get properties of a file with a given virtual URL
489          * The virtual URL must refer to this repo
490          */
491         function getFileProps( $virtualUrl ) {
492                 $path = $this->resolveVirtualUrl( $virtualUrl );
493                 return File::getPropsFromPath( $path );
494         }
495
496         /**
497          * Path disclosure protection functions
498          *
499          * Get a callback function to use for cleaning error message parameters
500          */
501         function getErrorCleanupFunction() {
502                 switch ( $this->pathDisclosureProtection ) {
503                         case 'simple':
504                                 $callback = array( $this, 'simpleClean' );
505                                 break;
506                         default:
507                                 $callback = parent::getErrorCleanupFunction();
508                 }
509                 return $callback;
510         }
511
512         function simpleClean( $param ) {
513                 if ( !isset( $this->simpleCleanPairs ) ) {
514                         global $IP;
515                         $this->simpleCleanPairs = array(
516                                 $this->directory => 'public',
517                                 "{$this->directory}/temp" => 'temp',
518                                 $IP => '$IP',
519                                 dirname( __FILE__ ) => '$IP/extensions/WebStore',
520                         );
521                         if ( $this->deletedDir ) {
522                                 $this->simpleCleanPairs[$this->deletedDir] = 'deleted';
523                         }
524                 }
525                 return strtr( $param, $this->simpleCleanPairs );
526         }
527
528 }
529
530