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