* We no longer just give up on a missing upload base directory; it's now
[lhc/web/wiklou.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 * @ingroup FileRepo
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 ( !wfMkdirParents( $this->directory ) ) {
129 return $this->newFatal( 'upload_directory_missing', $this->directory );
130 }
131 if ( !is_writable( $this->directory ) ) {
132 return $this->newFatal( 'upload_directory_read_only', $this->directory );
133 }
134 $status = $this->newGood();
135 foreach ( $triplets as $i => $triplet ) {
136 list( $srcPath, $dstZone, $dstRel ) = $triplet;
137
138 $root = $this->getZonePath( $dstZone );
139 if ( !$root ) {
140 throw new MWException( "Invalid zone: $dstZone" );
141 }
142 if ( !$this->validateFilename( $dstRel ) ) {
143 throw new MWException( 'Validation error in $dstRel' );
144 }
145 $dstPath = "$root/$dstRel";
146 $dstDir = dirname( $dstPath );
147
148 if ( !is_dir( $dstDir ) ) {
149 if ( !wfMkdirParents( $dstDir ) ) {
150 return $this->newFatal( 'directorycreateerror', $dstDir );
151 }
152 // In the deleted zone, seed new directories with a blank
153 // index.html, to prevent crawling
154 if ( $dstZone == 'deleted' ) {
155 file_put_contents( "$dstDir/index.html", '' );
156 }
157 }
158
159 if ( self::isVirtualUrl( $srcPath ) ) {
160 $srcPath = $triplets[$i][0] = $this->resolveVirtualUrl( $srcPath );
161 }
162 if ( !is_file( $srcPath ) ) {
163 // Make a list of files that don't exist for return to the caller
164 $status->fatal( 'filenotfound', $srcPath );
165 continue;
166 }
167 if ( !( $flags & self::OVERWRITE ) && file_exists( $dstPath ) ) {
168 if ( $flags & self::OVERWRITE_SAME ) {
169 $hashSource = sha1_file( $srcPath );
170 $hashDest = sha1_file( $dstPath );
171 if ( $hashSource != $hashDest ) {
172 $status->fatal( 'fileexistserror', $dstPath );
173 }
174 } else {
175 $status->fatal( 'fileexistserror', $dstPath );
176 }
177 }
178 }
179
180 $deleteDest = wfIsWindows() && ( $flags & self::OVERWRITE );
181
182 // Abort now on failure
183 if ( !$status->ok ) {
184 return $status;
185 }
186
187 foreach ( $triplets as $triplet ) {
188 list( $srcPath, $dstZone, $dstRel ) = $triplet;
189 $root = $this->getZonePath( $dstZone );
190 $dstPath = "$root/$dstRel";
191 $good = true;
192
193 if ( $flags & self::DELETE_SOURCE ) {
194 if ( $deleteDest ) {
195 unlink( $dstPath );
196 }
197 if ( !rename( $srcPath, $dstPath ) ) {
198 $status->error( 'filerenameerror', $srcPath, $dstPath );
199 $good = false;
200 }
201 } else {
202 if ( !copy( $srcPath, $dstPath ) ) {
203 $status->error( 'filecopyerror', $srcPath, $dstPath );
204 $good = false;
205 }
206 }
207 if ( $good ) {
208 chmod( $dstPath, 0644 );
209 $status->successCount++;
210 } else {
211 $status->failCount++;
212 }
213 }
214 return $status;
215 }
216
217 /**
218 * Pick a random name in the temp zone and store a file to it.
219 * @param string $originalName The base name of the file as specified
220 * by the user. The file extension will be maintained.
221 * @param string $srcPath The current location of the file.
222 * @return FileRepoStatus object with the URL in the value.
223 */
224 function storeTemp( $originalName, $srcPath ) {
225 $date = gmdate( "YmdHis" );
226 $hashPath = $this->getHashPath( $originalName );
227 $dstRel = "$hashPath$date!$originalName";
228 $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
229
230 $result = $this->store( $srcPath, 'temp', $dstRel );
231 $result->value = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
232 return $result;
233 }
234
235 /**
236 * Remove a temporary file or mark it for garbage collection
237 * @param string $virtualUrl The virtual URL returned by storeTemp
238 * @return boolean True on success, false on failure
239 */
240 function freeTemp( $virtualUrl ) {
241 $temp = "mwrepo://{$this->name}/temp";
242 if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
243 wfDebug( __METHOD__.": Invalid virtual URL\n" );
244 return false;
245 }
246 $path = $this->resolveVirtualUrl( $virtualUrl );
247 wfSuppressWarnings();
248 $success = unlink( $path );
249 wfRestoreWarnings();
250 return $success;
251 }
252
253 /**
254 * Publish a batch of files
255 * @param array $triplets (source,dest,archive) triplets as per publish()
256 * @param integer $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
257 * that the source files should be deleted if possible
258 */
259 function publishBatch( $triplets, $flags = 0 ) {
260 // Perform initial checks
261 if ( !wfMkdirParents( $this->directory ) ) {
262 return $this->newFatal( 'upload_directory_missing', $this->directory );
263 }
264 if ( !is_writable( $this->directory ) ) {
265 return $this->newFatal( 'upload_directory_read_only', $this->directory );
266 }
267 $status = $this->newGood( array() );
268 foreach ( $triplets as $i => $triplet ) {
269 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
270
271 if ( substr( $srcPath, 0, 9 ) == 'mwrepo://' ) {
272 $triplets[$i][0] = $srcPath = $this->resolveVirtualUrl( $srcPath );
273 }
274 if ( !$this->validateFilename( $dstRel ) ) {
275 throw new MWException( 'Validation error in $dstRel' );
276 }
277 if ( !$this->validateFilename( $archiveRel ) ) {
278 throw new MWException( 'Validation error in $archiveRel' );
279 }
280 $dstPath = "{$this->directory}/$dstRel";
281 $archivePath = "{$this->directory}/$archiveRel";
282
283 $dstDir = dirname( $dstPath );
284 $archiveDir = dirname( $archivePath );
285 // Abort immediately on directory creation errors since they're likely to be repetitive
286 if ( !is_dir( $dstDir ) && !wfMkdirParents( $dstDir ) ) {
287 return $this->newFatal( 'directorycreateerror', $dstDir );
288 }
289 if ( !is_dir( $archiveDir ) && !wfMkdirParents( $archiveDir ) ) {
290 return $this->newFatal( 'directorycreateerror', $archiveDir );
291 }
292 if ( !is_file( $srcPath ) ) {
293 // Make a list of files that don't exist for return to the caller
294 $status->fatal( 'filenotfound', $srcPath );
295 }
296 }
297
298 if ( !$status->ok ) {
299 return $status;
300 }
301
302 foreach ( $triplets as $i => $triplet ) {
303 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
304 $dstPath = "{$this->directory}/$dstRel";
305 $archivePath = "{$this->directory}/$archiveRel";
306
307 // Archive destination file if it exists
308 if( is_file( $dstPath ) ) {
309 // Check if the archive file exists
310 // This is a sanity check to avoid data loss. In UNIX, the rename primitive
311 // unlinks the destination file if it exists. DB-based synchronisation in
312 // publishBatch's caller should prevent races. In Windows there's no
313 // problem because the rename primitive fails if the destination exists.
314 if ( is_file( $archivePath ) ) {
315 $success = false;
316 } else {
317 wfSuppressWarnings();
318 $success = rename( $dstPath, $archivePath );
319 wfRestoreWarnings();
320 }
321
322 if( !$success ) {
323 $status->error( 'filerenameerror',$dstPath, $archivePath );
324 $status->failCount++;
325 continue;
326 } else {
327 wfDebug(__METHOD__.": moved file $dstPath to $archivePath\n");
328 }
329 $status->value[$i] = 'archived';
330 } else {
331 $status->value[$i] = 'new';
332 }
333
334 $good = true;
335 wfSuppressWarnings();
336 if ( $flags & self::DELETE_SOURCE ) {
337 if ( !rename( $srcPath, $dstPath ) ) {
338 $status->error( 'filerenameerror', $srcPath, $dstPath );
339 $good = false;
340 }
341 } else {
342 if ( !copy( $srcPath, $dstPath ) ) {
343 $status->error( 'filecopyerror', $srcPath, $dstPath );
344 $good = false;
345 }
346 }
347 wfRestoreWarnings();
348
349 if ( $good ) {
350 $status->successCount++;
351 wfDebug(__METHOD__.": wrote tempfile $srcPath to $dstPath\n");
352 // Thread-safe override for umask
353 chmod( $dstPath, 0644 );
354 } else {
355 $status->failCount++;
356 }
357 }
358 return $status;
359 }
360
361 /**
362 * Move a group of files to the deletion archive.
363 * If no valid deletion archive is configured, this may either delete the
364 * file or throw an exception, depending on the preference of the repository.
365 *
366 * @param array $sourceDestPairs Array of source/destination pairs. Each element
367 * is a two-element array containing the source file path relative to the
368 * public root in the first element, and the archive file path relative
369 * to the deleted zone root in the second element.
370 * @return FileRepoStatus
371 */
372 function deleteBatch( $sourceDestPairs ) {
373 $status = $this->newGood();
374 if ( !$this->deletedDir ) {
375 throw new MWException( __METHOD__.': no valid deletion archive directory' );
376 }
377
378 /**
379 * Validate filenames and create archive directories
380 */
381 foreach ( $sourceDestPairs as $pair ) {
382 list( $srcRel, $archiveRel ) = $pair;
383 if ( !$this->validateFilename( $srcRel ) ) {
384 throw new MWException( __METHOD__.':Validation error in $srcRel' );
385 }
386 if ( !$this->validateFilename( $archiveRel ) ) {
387 throw new MWException( __METHOD__.':Validation error in $archiveRel' );
388 }
389 $archivePath = "{$this->deletedDir}/$archiveRel";
390 $archiveDir = dirname( $archivePath );
391 if ( !is_dir( $archiveDir ) ) {
392 if ( !wfMkdirParents( $archiveDir ) ) {
393 $status->fatal( 'directorycreateerror', $archiveDir );
394 continue;
395 }
396 // Seed new directories with a blank index.html, to prevent crawling
397 file_put_contents( "$archiveDir/index.html", '' );
398 }
399 // Check if the archive directory is writable
400 // This doesn't appear to work on NTFS
401 if ( !is_writable( $archiveDir ) ) {
402 $status->fatal( 'filedelete-archive-read-only', $archiveDir );
403 }
404 }
405 if ( !$status->ok ) {
406 // Abort early
407 return $status;
408 }
409
410 /**
411 * Move the files
412 * We're now committed to returning an OK result, which will lead to
413 * the files being moved in the DB also.
414 */
415 foreach ( $sourceDestPairs as $pair ) {
416 list( $srcRel, $archiveRel ) = $pair;
417 $srcPath = "{$this->directory}/$srcRel";
418 $archivePath = "{$this->deletedDir}/$archiveRel";
419 $good = true;
420 if ( file_exists( $archivePath ) ) {
421 # A file with this content hash is already archived
422 if ( !@unlink( $srcPath ) ) {
423 $status->error( 'filedeleteerror', $srcPath );
424 $good = false;
425 }
426 } else{
427 if ( !@rename( $srcPath, $archivePath ) ) {
428 $status->error( 'filerenameerror', $srcPath, $archivePath );
429 $good = false;
430 } else {
431 @chmod( $archivePath, 0644 );
432 }
433 }
434 if ( $good ) {
435 $status->successCount++;
436 } else {
437 $status->failCount++;
438 }
439 }
440 return $status;
441 }
442
443 /**
444 * Get a relative path including trailing slash, e.g. f/fa/
445 * If the repo is not hashed, returns an empty string
446 */
447 function getHashPath( $name ) {
448 return FileRepo::getHashPathForLevel( $name, $this->hashLevels );
449 }
450
451 /**
452 * Get a relative path for a deletion archive key,
453 * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
454 */
455 function getDeletedHashPath( $key ) {
456 $path = '';
457 for ( $i = 0; $i < $this->deletedHashLevels; $i++ ) {
458 $path .= $key[$i] . '/';
459 }
460 return $path;
461 }
462
463 /**
464 * Call a callback function for every file in the repository.
465 * Uses the filesystem even in child classes.
466 */
467 function enumFilesInFS( $callback ) {
468 $numDirs = 1 << ( $this->hashLevels * 4 );
469 for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++ ) {
470 $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
471 $path = $this->directory;
472 for ( $hexPos = 0; $hexPos < $this->hashLevels; $hexPos++ ) {
473 $path .= '/' . substr( $hexString, 0, $hexPos + 1 );
474 }
475 if ( !file_exists( $path ) || !is_dir( $path ) ) {
476 continue;
477 }
478 $dir = opendir( $path );
479 while ( false !== ( $name = readdir( $dir ) ) ) {
480 call_user_func( $callback, $path . '/' . $name );
481 }
482 }
483 }
484
485 /**
486 * Call a callback function for every file in the repository
487 * May use either the database or the filesystem
488 */
489 function enumFiles( $callback ) {
490 $this->enumFilesInFS( $callback );
491 }
492
493 /**
494 * Get properties of a file with a given virtual URL
495 * The virtual URL must refer to this repo
496 */
497 function getFileProps( $virtualUrl ) {
498 $path = $this->resolveVirtualUrl( $virtualUrl );
499 return File::getPropsFromPath( $path );
500 }
501
502 /**
503 * Path disclosure protection functions
504 *
505 * Get a callback function to use for cleaning error message parameters
506 */
507 function getErrorCleanupFunction() {
508 switch ( $this->pathDisclosureProtection ) {
509 case 'simple':
510 $callback = array( $this, 'simpleClean' );
511 break;
512 default:
513 $callback = parent::getErrorCleanupFunction();
514 }
515 return $callback;
516 }
517
518 function simpleClean( $param ) {
519 if ( !isset( $this->simpleCleanPairs ) ) {
520 global $IP;
521 $this->simpleCleanPairs = array(
522 $this->directory => 'public',
523 "{$this->directory}/temp" => 'temp',
524 $IP => '$IP',
525 dirname( __FILE__ ) => '$IP/extensions/WebStore',
526 );
527 if ( $this->deletedDir ) {
528 $this->simpleCleanPairs[$this->deletedDir] = 'deleted';
529 }
530 }
531 return strtr( $param, $this->simpleCleanPairs );
532 }
533
534 }