00001 <?php
00002
00008 class FSRepo extends FileRepo {
00009 var $directory, $deletedDir, $url, $deletedHashLevels;
00010 var $fileFactory = array( 'UnregisteredLocalFile', 'newFromTitle' );
00011 var $oldFileFactory = false;
00012 var $pathDisclosureProtection = 'simple';
00013
00014 function __construct( $info ) {
00015 parent::__construct( $info );
00016
00017
00018 $this->directory = $info['directory'];
00019 $this->url = $info['url'];
00020
00021
00022 $this->hashLevels = isset( $info['hashLevels'] ) ? $info['hashLevels'] : 2;
00023 $this->deletedHashLevels = isset( $info['deletedHashLevels'] ) ?
00024 $info['deletedHashLevels'] : $this->hashLevels;
00025 $this->deletedDir = isset( $info['deletedDir'] ) ? $info['deletedDir'] : false;
00026 }
00027
00031 function getRootDirectory() {
00032 return $this->directory;
00033 }
00034
00038 function getRootUrl() {
00039 return $this->url;
00040 }
00041
00045 function isHashed() {
00046 return (bool)$this->hashLevels;
00047 }
00048
00052 function getZonePath( $zone ) {
00053 switch ( $zone ) {
00054 case 'public':
00055 return $this->directory;
00056 case 'temp':
00057 return "{$this->directory}/temp";
00058 case 'deleted':
00059 return $this->deletedDir;
00060 default:
00061 return false;
00062 }
00063 }
00064
00068 function getZoneUrl( $zone ) {
00069 switch ( $zone ) {
00070 case 'public':
00071 return $this->url;
00072 case 'temp':
00073 return "{$this->url}/temp";
00074 case 'deleted':
00075 return false;
00076 default:
00077 return false;
00078 }
00079 }
00080
00086 function getVirtualUrl( $suffix = false ) {
00087 $path = 'mwrepo://' . $this->name;
00088 if ( $suffix !== false ) {
00089 $path .= '/' . rawurlencode( $suffix );
00090 }
00091 return $path;
00092 }
00093
00097 function resolveVirtualUrl( $url ) {
00098 if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
00099 throw new MWException( __METHOD__.': unknown protoocl' );
00100 }
00101
00102 $bits = explode( '/', substr( $url, 9 ), 3 );
00103 if ( count( $bits ) != 3 ) {
00104 throw new MWException( __METHOD__.": invalid mwrepo URL: $url" );
00105 }
00106 list( $repo, $zone, $rel ) = $bits;
00107 if ( $repo !== $this->name ) {
00108 throw new MWException( __METHOD__.": fetching from a foreign repo is not supported" );
00109 }
00110 $base = $this->getZonePath( $zone );
00111 if ( !$base ) {
00112 throw new MWException( __METHOD__.": invalid zone: $zone" );
00113 }
00114 return $base . '/' . rawurldecode( $rel );
00115 }
00116
00127 function storeBatch( $triplets, $flags = 0 ) {
00128 if ( !wfMkdirParents( $this->directory ) ) {
00129 return $this->newFatal( 'upload_directory_missing', $this->directory );
00130 }
00131 if ( !is_writable( $this->directory ) ) {
00132 return $this->newFatal( 'upload_directory_read_only', $this->directory );
00133 }
00134 $status = $this->newGood();
00135 foreach ( $triplets as $i => $triplet ) {
00136 list( $srcPath, $dstZone, $dstRel ) = $triplet;
00137
00138 $root = $this->getZonePath( $dstZone );
00139 if ( !$root ) {
00140 throw new MWException( "Invalid zone: $dstZone" );
00141 }
00142 if ( !$this->validateFilename( $dstRel ) ) {
00143 throw new MWException( 'Validation error in $dstRel' );
00144 }
00145 $dstPath = "$root/$dstRel";
00146 $dstDir = dirname( $dstPath );
00147
00148 if ( !is_dir( $dstDir ) ) {
00149 if ( !wfMkdirParents( $dstDir ) ) {
00150 return $this->newFatal( 'directorycreateerror', $dstDir );
00151 }
00152 if ( $dstZone == 'deleted' ) {
00153 $this->initDeletedDir( $dstDir );
00154 }
00155 }
00156
00157 if ( self::isVirtualUrl( $srcPath ) ) {
00158 $srcPath = $triplets[$i][0] = $this->resolveVirtualUrl( $srcPath );
00159 }
00160 if ( !is_file( $srcPath ) ) {
00161
00162 $status->fatal( 'filenotfound', $srcPath );
00163 continue;
00164 }
00165 if ( !( $flags & self::OVERWRITE ) && file_exists( $dstPath ) ) {
00166 if ( $flags & self::OVERWRITE_SAME ) {
00167 $hashSource = sha1_file( $srcPath );
00168 $hashDest = sha1_file( $dstPath );
00169 if ( $hashSource != $hashDest ) {
00170 $status->fatal( 'fileexistserror', $dstPath );
00171 }
00172 } else {
00173 $status->fatal( 'fileexistserror', $dstPath );
00174 }
00175 }
00176 }
00177
00178 $deleteDest = wfIsWindows() && ( $flags & self::OVERWRITE );
00179
00180
00181 if ( !$status->ok ) {
00182 return $status;
00183 }
00184
00185 foreach ( $triplets as $triplet ) {
00186 list( $srcPath, $dstZone, $dstRel ) = $triplet;
00187 $root = $this->getZonePath( $dstZone );
00188 $dstPath = "$root/$dstRel";
00189 $good = true;
00190
00191 if ( $flags & self::DELETE_SOURCE ) {
00192 if ( $deleteDest ) {
00193 unlink( $dstPath );
00194 }
00195 if ( !rename( $srcPath, $dstPath ) ) {
00196 $status->error( 'filerenameerror', $srcPath, $dstPath );
00197 $good = false;
00198 }
00199 } else {
00200 if ( !copy( $srcPath, $dstPath ) ) {
00201 $status->error( 'filecopyerror', $srcPath, $dstPath );
00202 $good = false;
00203 }
00204 }
00205 if ( $good ) {
00206 chmod( $dstPath, 0644 );
00207 $status->successCount++;
00208 } else {
00209 $status->failCount++;
00210 }
00211 }
00212 return $status;
00213 }
00214
00219 protected function initDeletedDir( $dir ) {
00220
00221 $root = $this->getZonePath( 'deleted' );
00222 if ( !file_exists( "$root/.htaccess" ) ) {
00223 file_put_contents( "$root/.htaccess", "Deny from all\n" );
00224 }
00225
00226 file_put_contents( "$dir/index.html", '' );
00227 }
00228
00236 function storeTemp( $originalName, $srcPath ) {
00237 $date = gmdate( "YmdHis" );
00238 $hashPath = $this->getHashPath( $originalName );
00239 $dstRel = "$hashPath$date!$originalName";
00240 $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
00241
00242 $result = $this->store( $srcPath, 'temp', $dstRel );
00243 $result->value = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
00244 return $result;
00245 }
00246
00252 function freeTemp( $virtualUrl ) {
00253 $temp = "mwrepo://{$this->name}/temp";
00254 if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
00255 wfDebug( __METHOD__.": Invalid virtual URL\n" );
00256 return false;
00257 }
00258 $path = $this->resolveVirtualUrl( $virtualUrl );
00259 wfSuppressWarnings();
00260 $success = unlink( $path );
00261 wfRestoreWarnings();
00262 return $success;
00263 }
00264
00271 function publishBatch( $triplets, $flags = 0 ) {
00272
00273 if ( !wfMkdirParents( $this->directory ) ) {
00274 return $this->newFatal( 'upload_directory_missing', $this->directory );
00275 }
00276 if ( !is_writable( $this->directory ) ) {
00277 return $this->newFatal( 'upload_directory_read_only', $this->directory );
00278 }
00279 $status = $this->newGood( array() );
00280 foreach ( $triplets as $i => $triplet ) {
00281 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
00282
00283 if ( substr( $srcPath, 0, 9 ) == 'mwrepo://' ) {
00284 $triplets[$i][0] = $srcPath = $this->resolveVirtualUrl( $srcPath );
00285 }
00286 if ( !$this->validateFilename( $dstRel ) ) {
00287 throw new MWException( 'Validation error in $dstRel' );
00288 }
00289 if ( !$this->validateFilename( $archiveRel ) ) {
00290 throw new MWException( 'Validation error in $archiveRel' );
00291 }
00292 $dstPath = "{$this->directory}/$dstRel";
00293 $archivePath = "{$this->directory}/$archiveRel";
00294
00295 $dstDir = dirname( $dstPath );
00296 $archiveDir = dirname( $archivePath );
00297
00298 if ( !is_dir( $dstDir ) && !wfMkdirParents( $dstDir ) ) {
00299 return $this->newFatal( 'directorycreateerror', $dstDir );
00300 }
00301 if ( !is_dir( $archiveDir ) && !wfMkdirParents( $archiveDir ) ) {
00302 return $this->newFatal( 'directorycreateerror', $archiveDir );
00303 }
00304 if ( !is_file( $srcPath ) ) {
00305
00306 $status->fatal( 'filenotfound', $srcPath );
00307 }
00308 }
00309
00310 if ( !$status->ok ) {
00311 return $status;
00312 }
00313
00314 foreach ( $triplets as $i => $triplet ) {
00315 list( $srcPath, $dstRel, $archiveRel ) = $triplet;
00316 $dstPath = "{$this->directory}/$dstRel";
00317 $archivePath = "{$this->directory}/$archiveRel";
00318
00319
00320 if( is_file( $dstPath ) ) {
00321
00322
00323
00324
00325
00326 if ( is_file( $archivePath ) ) {
00327 $success = false;
00328 } else {
00329 wfSuppressWarnings();
00330 $success = rename( $dstPath, $archivePath );
00331 wfRestoreWarnings();
00332 }
00333
00334 if( !$success ) {
00335 $status->error( 'filerenameerror',$dstPath, $archivePath );
00336 $status->failCount++;
00337 continue;
00338 } else {
00339 wfDebug(__METHOD__.": moved file $dstPath to $archivePath\n");
00340 }
00341 $status->value[$i] = 'archived';
00342 } else {
00343 $status->value[$i] = 'new';
00344 }
00345
00346 $good = true;
00347 wfSuppressWarnings();
00348 if ( $flags & self::DELETE_SOURCE ) {
00349 if ( !rename( $srcPath, $dstPath ) ) {
00350 $status->error( 'filerenameerror', $srcPath, $dstPath );
00351 $good = false;
00352 }
00353 } else {
00354 if ( !copy( $srcPath, $dstPath ) ) {
00355 $status->error( 'filecopyerror', $srcPath, $dstPath );
00356 $good = false;
00357 }
00358 }
00359 wfRestoreWarnings();
00360
00361 if ( $good ) {
00362 $status->successCount++;
00363 wfDebug(__METHOD__.": wrote tempfile $srcPath to $dstPath\n");
00364
00365 chmod( $dstPath, 0644 );
00366 } else {
00367 $status->failCount++;
00368 }
00369 }
00370 return $status;
00371 }
00372
00384 function deleteBatch( $sourceDestPairs ) {
00385 $status = $this->newGood();
00386 if ( !$this->deletedDir ) {
00387 throw new MWException( __METHOD__.': no valid deletion archive directory' );
00388 }
00389
00393 foreach ( $sourceDestPairs as $pair ) {
00394 list( $srcRel, $archiveRel ) = $pair;
00395 if ( !$this->validateFilename( $srcRel ) ) {
00396 throw new MWException( __METHOD__.':Validation error in $srcRel' );
00397 }
00398 if ( !$this->validateFilename( $archiveRel ) ) {
00399 throw new MWException( __METHOD__.':Validation error in $archiveRel' );
00400 }
00401 $archivePath = "{$this->deletedDir}/$archiveRel";
00402 $archiveDir = dirname( $archivePath );
00403 if ( !is_dir( $archiveDir ) ) {
00404 if ( !wfMkdirParents( $archiveDir ) ) {
00405 $status->fatal( 'directorycreateerror', $archiveDir );
00406 continue;
00407 }
00408 $this->initDeletedDir( $archiveDir );
00409 }
00410
00411
00412 if ( !is_writable( $archiveDir ) ) {
00413 $status->fatal( 'filedelete-archive-read-only', $archiveDir );
00414 }
00415 }
00416 if ( !$status->ok ) {
00417
00418 return $status;
00419 }
00420
00426 foreach ( $sourceDestPairs as $pair ) {
00427 list( $srcRel, $archiveRel ) = $pair;
00428 $srcPath = "{$this->directory}/$srcRel";
00429 $archivePath = "{$this->deletedDir}/$archiveRel";
00430 $good = true;
00431 if ( file_exists( $archivePath ) ) {
00432 # A file with this content hash is already archived
00433 if ( !@unlink( $srcPath ) ) {
00434 $status->error( 'filedeleteerror', $srcPath );
00435 $good = false;
00436 }
00437 } else{
00438 if ( !@rename( $srcPath, $archivePath ) ) {
00439 $status->error( 'filerenameerror', $srcPath, $archivePath );
00440 $good = false;
00441 } else {
00442 @chmod( $archivePath, 0644 );
00443 }
00444 }
00445 if ( $good ) {
00446 $status->successCount++;
00447 } else {
00448 $status->failCount++;
00449 }
00450 }
00451 return $status;
00452 }
00453
00458 function getDeletedHashPath( $key ) {
00459 $path = '';
00460 for ( $i = 0; $i < $this->deletedHashLevels; $i++ ) {
00461 $path .= $key[$i] . '/';
00462 }
00463 return $path;
00464 }
00465
00470 function enumFilesInFS( $callback ) {
00471 $numDirs = 1 << ( $this->hashLevels * 4 );
00472 for ( $flatIndex = 0; $flatIndex < $numDirs; $flatIndex++ ) {
00473 $hexString = sprintf( "%0{$this->hashLevels}x", $flatIndex );
00474 $path = $this->directory;
00475 for ( $hexPos = 0; $hexPos < $this->hashLevels; $hexPos++ ) {
00476 $path .= '/' . substr( $hexString, 0, $hexPos + 1 );
00477 }
00478 if ( !file_exists( $path ) || !is_dir( $path ) ) {
00479 continue;
00480 }
00481 $dir = opendir( $path );
00482 while ( false !== ( $name = readdir( $dir ) ) ) {
00483 call_user_func( $callback, $path . '/' . $name );
00484 }
00485 }
00486 }
00487
00492 function enumFiles( $callback ) {
00493 $this->enumFilesInFS( $callback );
00494 }
00495
00500 function getFileProps( $virtualUrl ) {
00501 $path = $this->resolveVirtualUrl( $virtualUrl );
00502 return File::getPropsFromPath( $path );
00503 }
00504
00510 function getErrorCleanupFunction() {
00511 switch ( $this->pathDisclosureProtection ) {
00512 case 'simple':
00513 $callback = array( $this, 'simpleClean' );
00514 break;
00515 default:
00516 $callback = parent::getErrorCleanupFunction();
00517 }
00518 return $callback;
00519 }
00520
00521 function simpleClean( $param ) {
00522 if ( !isset( $this->simpleCleanPairs ) ) {
00523 global $IP;
00524 $this->simpleCleanPairs = array(
00525 $this->directory => 'public',
00526 "{$this->directory}/temp" => 'temp',
00527 $IP => '$IP',
00528 dirname( __FILE__ ) => '$IP/extensions/WebStore',
00529 );
00530 if ( $this->deletedDir ) {
00531 $this->simpleCleanPairs[$this->deletedDir] = 'deleted';
00532 }
00533 }
00534 return strtr( $param, $this->simpleCleanPairs );
00535 }
00536
00537 }