2012-01-27 15:03:50 +00:00
|
|
|
<?php
|
2011-12-20 03:52:06 +00:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup FileBackend
|
|
|
|
|
* @author Aaron Schulz
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-12 18:44:00 +00:00
|
|
|
* Class for a file system (FS) based file backend.
|
|
|
|
|
*
|
|
|
|
|
* All "containers" each map to a directory under the backend's base directory.
|
|
|
|
|
* For backwards-compatibility, some container paths can be set to custom paths.
|
|
|
|
|
* The wiki ID will not be used in any custom paths, so this should be avoided.
|
2012-01-12 19:20:58 +00:00
|
|
|
*
|
|
|
|
|
* Having directories with thousands of files will diminish performance.
|
2012-01-12 18:44:00 +00:00
|
|
|
* Sharding can be accomplished by using FileRepo-style hash paths.
|
2012-01-04 01:08:33 +00:00
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* Status messages should avoid mentioning the internal FS paths.
|
|
|
|
|
* Likewise, error suppression should be used to avoid path disclosure.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup FileBackend
|
2012-01-13 23:30:46 +00:00
|
|
|
* @since 1.19
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2012-01-29 22:22:28 +00:00
|
|
|
class FSFileBackend extends FileBackendStore {
|
2012-01-12 18:44:00 +00:00
|
|
|
protected $basePath; // string; directory holding the container directories
|
|
|
|
|
/** @var Array Map of container names to root paths */
|
|
|
|
|
protected $containerPaths = array(); // for custom container paths
|
|
|
|
|
protected $fileMode; // integer; file permission mode
|
2011-12-20 03:52:06 +00:00
|
|
|
|
2012-01-28 17:05:20 +00:00
|
|
|
protected $hadWarningErrors = array();
|
|
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackendStore::__construct()
|
2011-12-20 03:52:06 +00:00
|
|
|
* Additional $config params include:
|
2012-01-12 18:44:00 +00:00
|
|
|
* basePath : File system directory that holds containers.
|
|
|
|
|
* containerPaths : Map of container names to custom file system directories.
|
|
|
|
|
* This should only be used for backwards-compatibility.
|
|
|
|
|
* fileMode : Octal UNIX file permissions to use on files stored.
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2012-01-04 01:08:33 +00:00
|
|
|
public function __construct( array $config ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
parent::__construct( $config );
|
2012-02-08 09:16:19 +00:00
|
|
|
|
|
|
|
|
// Remove any possible trailing slash from directories
|
|
|
|
|
|
2012-01-12 18:44:00 +00:00
|
|
|
if ( isset( $config['basePath'] ) ) {
|
2012-02-08 09:54:44 +00:00
|
|
|
$this->basePath = rtrim( $config['basePath'], '/' ); // remove trailing slash
|
2012-01-12 18:44:00 +00:00
|
|
|
} else {
|
|
|
|
|
$this->basePath = null; // none; containers must have explicit paths
|
|
|
|
|
}
|
2012-02-08 09:16:19 +00:00
|
|
|
|
2012-02-08 09:21:19 +00:00
|
|
|
if( isset( $config['containerPaths'] ) ) {
|
|
|
|
|
$this->containerPaths = (array)$config['containerPaths'];
|
|
|
|
|
foreach ( $this->containerPaths as &$path ) {
|
|
|
|
|
rtrim( $path, '/' ); // remove trailing slash
|
|
|
|
|
}
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
2012-02-08 09:21:19 +00:00
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
$this->fileMode = isset( $config['fileMode'] )
|
|
|
|
|
? $config['fileMode']
|
|
|
|
|
: 0644;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackendStore::resolveContainerPath()
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
|
|
|
|
protected function resolveContainerPath( $container, $relStoragePath ) {
|
2012-02-08 09:00:31 +00:00
|
|
|
// Check that container has a root directory
|
2012-01-12 18:44:00 +00:00
|
|
|
if ( isset( $this->containerPaths[$container] ) || isset( $this->basePath ) ) {
|
2012-02-08 09:00:31 +00:00
|
|
|
// Check for sane relative paths (assume the base paths are OK)
|
|
|
|
|
if ( $this->isLegalRelPath( $relStoragePath ) ) {
|
|
|
|
|
return $relStoragePath;
|
|
|
|
|
}
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-08 09:00:31 +00:00
|
|
|
/**
|
|
|
|
|
* Sanity check a relative file system path for validity
|
|
|
|
|
*
|
|
|
|
|
* @param $path string Normalized relative path
|
|
|
|
|
*/
|
|
|
|
|
protected function isLegalRelPath( $path ) {
|
|
|
|
|
// Check for file names longer than 255 chars
|
|
|
|
|
if ( preg_match( '![^/]{256}!', $path ) ) { // ext3/NTFS
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if ( wfIsWindows() ) { // NTFS
|
|
|
|
|
return !preg_match( '![:*?"<>]!', $path );
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-12 18:44:00 +00:00
|
|
|
/**
|
|
|
|
|
* Given the short (unresolved) and full (resolved) name of
|
|
|
|
|
* a container, return the file system path of the container.
|
|
|
|
|
*
|
|
|
|
|
* @param $shortCont string
|
|
|
|
|
* @param $fullCont string
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
protected function containerFSRoot( $shortCont, $fullCont ) {
|
|
|
|
|
if ( isset( $this->containerPaths[$shortCont] ) ) {
|
|
|
|
|
return $this->containerPaths[$shortCont];
|
|
|
|
|
} elseif ( isset( $this->basePath ) ) {
|
|
|
|
|
return "{$this->basePath}/{$fullCont}";
|
|
|
|
|
}
|
|
|
|
|
return null; // no container base path defined
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the absolute file system path for a storage path
|
|
|
|
|
*
|
|
|
|
|
* @param $storagePath string Storage path
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
protected function resolveToFSPath( $storagePath ) {
|
|
|
|
|
list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
|
|
|
|
|
if ( $relPath === null ) {
|
|
|
|
|
return null; // invalid
|
|
|
|
|
}
|
|
|
|
|
list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $storagePath );
|
|
|
|
|
$fsPath = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
|
|
|
|
|
if ( $relPath != '' ) {
|
|
|
|
|
$fsPath .= "/{$relPath}";
|
|
|
|
|
}
|
|
|
|
|
return $fsPath;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-19 23:18:03 +00:00
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackendStore::isPathUsableInternal()
|
2012-01-19 23:18:03 +00:00
|
|
|
*/
|
|
|
|
|
public function isPathUsableInternal( $storagePath ) {
|
|
|
|
|
$fsPath = $this->resolveToFSPath( $storagePath );
|
|
|
|
|
if ( $fsPath === null ) {
|
|
|
|
|
return false; // invalid
|
|
|
|
|
}
|
|
|
|
|
$parentDir = dirname( $fsPath );
|
|
|
|
|
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
if ( file_exists( $fsPath ) ) {
|
|
|
|
|
$ok = is_file( $fsPath ) && is_writable( $fsPath );
|
|
|
|
|
} else {
|
|
|
|
|
$ok = is_dir( $parentDir ) && is_writable( $parentDir );
|
|
|
|
|
}
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
|
|
|
|
|
return $ok;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackendStore::doStoreInternal()
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2011-12-21 09:16:28 +00:00
|
|
|
protected function doStoreInternal( array $params ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
$status = Status::newGood();
|
|
|
|
|
|
2012-01-12 18:44:00 +00:00
|
|
|
$dest = $this->resolveToFSPath( $params['dst'] );
|
2011-12-20 03:52:06 +00:00
|
|
|
if ( $dest === null ) {
|
|
|
|
|
$status->fatal( 'backend-fail-invalidpath', $params['dst'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
2012-01-12 18:44:00 +00:00
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
if ( file_exists( $dest ) ) {
|
2012-01-19 02:24:49 +00:00
|
|
|
if ( !empty( $params['overwrite'] ) ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$ok = unlink( $dest );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
if ( !$ok ) {
|
|
|
|
|
$status->fatal( 'backend-fail-delete', $params['dst'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$ok = copy( $params['src'], $dest );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
if ( !$ok ) {
|
2012-01-13 23:30:46 +00:00
|
|
|
$status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
|
2011-12-20 03:52:06 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->chmod( $dest );
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackendStore::doCopyInternal()
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2011-12-21 09:16:28 +00:00
|
|
|
protected function doCopyInternal( array $params ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
$status = Status::newGood();
|
|
|
|
|
|
2012-01-12 18:44:00 +00:00
|
|
|
$source = $this->resolveToFSPath( $params['src'] );
|
2011-12-20 03:52:06 +00:00
|
|
|
if ( $source === null ) {
|
|
|
|
|
$status->fatal( 'backend-fail-invalidpath', $params['src'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-12 18:44:00 +00:00
|
|
|
$dest = $this->resolveToFSPath( $params['dst'] );
|
2011-12-20 03:52:06 +00:00
|
|
|
if ( $dest === null ) {
|
|
|
|
|
$status->fatal( 'backend-fail-invalidpath', $params['dst'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( file_exists( $dest ) ) {
|
2012-01-19 02:24:49 +00:00
|
|
|
if ( !empty( $params['overwrite'] ) ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$ok = unlink( $dest );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
if ( !$ok ) {
|
|
|
|
|
$status->fatal( 'backend-fail-delete', $params['dst'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$ok = copy( $source, $dest );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
if ( !$ok ) {
|
|
|
|
|
$status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->chmod( $dest );
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackendStore::doMoveInternal()
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2011-12-21 09:16:28 +00:00
|
|
|
protected function doMoveInternal( array $params ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
$status = Status::newGood();
|
|
|
|
|
|
2012-01-12 18:44:00 +00:00
|
|
|
$source = $this->resolveToFSPath( $params['src'] );
|
2011-12-20 03:52:06 +00:00
|
|
|
if ( $source === null ) {
|
|
|
|
|
$status->fatal( 'backend-fail-invalidpath', $params['src'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
2012-01-12 18:44:00 +00:00
|
|
|
|
|
|
|
|
$dest = $this->resolveToFSPath( $params['dst'] );
|
2011-12-20 03:52:06 +00:00
|
|
|
if ( $dest === null ) {
|
|
|
|
|
$status->fatal( 'backend-fail-invalidpath', $params['dst'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( file_exists( $dest ) ) {
|
2012-01-19 02:24:49 +00:00
|
|
|
if ( !empty( $params['overwrite'] ) ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
// Windows does not support moving over existing files
|
|
|
|
|
if ( wfIsWindows() ) {
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$ok = unlink( $dest );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
if ( !$ok ) {
|
|
|
|
|
$status->fatal( 'backend-fail-delete', $params['dst'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$ok = rename( $source, $dest );
|
|
|
|
|
clearstatcache(); // file no longer at source
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
if ( !$ok ) {
|
|
|
|
|
$status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackendStore::doDeleteInternal()
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2011-12-21 09:16:28 +00:00
|
|
|
protected function doDeleteInternal( array $params ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
$status = Status::newGood();
|
|
|
|
|
|
2012-01-12 18:44:00 +00:00
|
|
|
$source = $this->resolveToFSPath( $params['src'] );
|
2011-12-20 03:52:06 +00:00
|
|
|
if ( $source === null ) {
|
|
|
|
|
$status->fatal( 'backend-fail-invalidpath', $params['src'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !is_file( $source ) ) {
|
|
|
|
|
if ( empty( $params['ignoreMissingSource'] ) ) {
|
|
|
|
|
$status->fatal( 'backend-fail-delete', $params['src'] );
|
|
|
|
|
}
|
|
|
|
|
return $status; // do nothing; either OK or bad status
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$ok = unlink( $source );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
if ( !$ok ) {
|
|
|
|
|
$status->fatal( 'backend-fail-delete', $params['src'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackendStore::doCreateInternal()
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2011-12-21 09:16:28 +00:00
|
|
|
protected function doCreateInternal( array $params ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
$status = Status::newGood();
|
|
|
|
|
|
2012-01-12 18:44:00 +00:00
|
|
|
$dest = $this->resolveToFSPath( $params['dst'] );
|
2011-12-20 03:52:06 +00:00
|
|
|
if ( $dest === null ) {
|
|
|
|
|
$status->fatal( 'backend-fail-invalidpath', $params['dst'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( file_exists( $dest ) ) {
|
2012-01-19 02:24:49 +00:00
|
|
|
if ( !empty( $params['overwrite'] ) ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$ok = unlink( $dest );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
if ( !$ok ) {
|
|
|
|
|
$status->fatal( 'backend-fail-delete', $params['dst'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wfSuppressWarnings();
|
2012-02-06 05:25:26 +00:00
|
|
|
$bytes = file_put_contents( $dest, $params['content'] );
|
2011-12-20 03:52:06 +00:00
|
|
|
wfRestoreWarnings();
|
2012-02-06 05:25:26 +00:00
|
|
|
if ( $bytes === false ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
$status->fatal( 'backend-fail-create', $params['dst'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->chmod( $dest );
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackendStore::doPrepareInternal()
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2012-01-12 18:44:00 +00:00
|
|
|
protected function doPrepareInternal( $fullCont, $dirRel, array $params ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
$status = Status::newGood();
|
2012-01-12 18:44:00 +00:00
|
|
|
list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
|
|
|
|
|
$contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
|
|
|
|
|
$dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
|
2012-01-13 23:30:46 +00:00
|
|
|
if ( !wfMkdirParents( $dir ) ) { // make directory and its parents
|
2011-12-20 03:52:06 +00:00
|
|
|
$status->fatal( 'directorycreateerror', $params['dir'] );
|
|
|
|
|
} elseif ( !is_writable( $dir ) ) {
|
|
|
|
|
$status->fatal( 'directoryreadonlyerror', $params['dir'] );
|
|
|
|
|
} elseif ( !is_readable( $dir ) ) {
|
|
|
|
|
$status->fatal( 'directorynotreadableerror', $params['dir'] );
|
|
|
|
|
}
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackendStore::doSecureInternal()
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2012-01-12 18:44:00 +00:00
|
|
|
protected function doSecureInternal( $fullCont, $dirRel, array $params ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
$status = Status::newGood();
|
2012-01-12 18:44:00 +00:00
|
|
|
list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
|
|
|
|
|
$contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
|
|
|
|
|
$dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
|
2012-01-04 01:08:33 +00:00
|
|
|
// Seed new directories with a blank index.html, to prevent crawling...
|
|
|
|
|
if ( !empty( $params['noListing'] ) && !file_exists( "{$dir}/index.html" ) ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
wfSuppressWarnings();
|
2012-02-06 05:25:26 +00:00
|
|
|
$bytes = file_put_contents( "{$dir}/index.html", '' );
|
2011-12-20 03:52:06 +00:00
|
|
|
wfRestoreWarnings();
|
2012-02-06 05:25:26 +00:00
|
|
|
if ( !$bytes ) {
|
2012-01-04 01:08:33 +00:00
|
|
|
$status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' );
|
2011-12-20 03:52:06 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-04 01:08:33 +00:00
|
|
|
// Add a .htaccess file to the root of the container...
|
2012-01-09 00:20:28 +00:00
|
|
|
if ( !empty( $params['noAccess'] ) ) {
|
2012-01-12 19:20:58 +00:00
|
|
|
if ( !file_exists( "{$contRoot}/.htaccess" ) ) {
|
2012-01-09 00:20:28 +00:00
|
|
|
wfSuppressWarnings();
|
2012-02-06 05:25:26 +00:00
|
|
|
$bytes = file_put_contents( "{$contRoot}/.htaccess", "Deny from all\n" );
|
2012-01-09 00:20:28 +00:00
|
|
|
wfRestoreWarnings();
|
2012-02-06 05:25:26 +00:00
|
|
|
if ( !$bytes ) {
|
2012-01-12 18:44:00 +00:00
|
|
|
$storeDir = "mwstore://{$this->name}/{$shortCont}";
|
2012-01-12 19:20:58 +00:00
|
|
|
$status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" );
|
2012-01-09 00:20:28 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackendStore::doCleanInternal()
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2012-01-12 18:44:00 +00:00
|
|
|
protected function doCleanInternal( $fullCont, $dirRel, array $params ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
$status = Status::newGood();
|
2012-01-12 18:44:00 +00:00
|
|
|
list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
|
|
|
|
|
$contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
|
|
|
|
|
$dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
|
2011-12-20 03:52:06 +00:00
|
|
|
wfSuppressWarnings();
|
|
|
|
|
if ( is_dir( $dir ) ) {
|
|
|
|
|
rmdir( $dir ); // remove directory if empty
|
|
|
|
|
}
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackendStore::doFileExists()
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2012-01-08 08:40:00 +00:00
|
|
|
protected function doGetFileStat( array $params ) {
|
2012-01-12 18:44:00 +00:00
|
|
|
$source = $this->resolveToFSPath( $params['src'] );
|
2011-12-20 03:52:06 +00:00
|
|
|
if ( $source === null ) {
|
|
|
|
|
return false; // invalid storage path
|
|
|
|
|
}
|
2012-01-08 08:40:00 +00:00
|
|
|
|
2012-01-13 04:32:28 +00:00
|
|
|
$this->trapWarnings();
|
2012-01-12 18:44:00 +00:00
|
|
|
$stat = is_file( $source ) ? stat( $source ) : false; // regular files only
|
2012-01-13 04:32:28 +00:00
|
|
|
$hadError = $this->untrapWarnings();
|
2011-12-20 03:52:06 +00:00
|
|
|
|
2012-01-08 08:40:00 +00:00
|
|
|
if ( $stat ) {
|
|
|
|
|
return array(
|
|
|
|
|
'mtime' => wfTimestamp( TS_MW, $stat['mtime'] ),
|
|
|
|
|
'size' => $stat['size']
|
|
|
|
|
);
|
2012-01-13 04:32:28 +00:00
|
|
|
} elseif ( !$hadError ) {
|
|
|
|
|
return false; // file does not exist
|
2012-01-08 08:40:00 +00:00
|
|
|
} else {
|
2012-01-13 04:32:28 +00:00
|
|
|
return null; // failure
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-20 21:55:15 +00:00
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackendStore::doClearCache()
|
2012-01-20 21:55:15 +00:00
|
|
|
*/
|
|
|
|
|
protected function doClearCache( array $paths = null ) {
|
|
|
|
|
clearstatcache(); // clear the PHP file stat cache
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackendStore::getFileListInternal()
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2012-01-12 18:44:00 +00:00
|
|
|
public function getFileListInternal( $fullCont, $dirRel, array $params ) {
|
|
|
|
|
list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
|
|
|
|
|
$contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
|
|
|
|
|
$dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
|
2011-12-20 03:52:06 +00:00
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$exists = is_dir( $dir );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
if ( !$exists ) {
|
2012-02-08 12:58:27 +00:00
|
|
|
wfDebug( __METHOD__ . "() given directory does not exist: '$dir'\n" );
|
2011-12-20 03:52:06 +00:00
|
|
|
return array(); // nothing under this dir
|
|
|
|
|
}
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$readable = is_readable( $dir );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
if ( !$readable ) {
|
2012-02-08 12:58:27 +00:00
|
|
|
wfDebug( __METHOD__ . "() given directory is unreadable: '$dir'\n" );
|
2011-12-20 03:52:06 +00:00
|
|
|
return null; // bad permissions?
|
|
|
|
|
}
|
2012-01-14 01:52:19 +00:00
|
|
|
return new FSFileBackendFileList( $dir );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackendStore::getLocalReference()
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2012-01-04 01:08:33 +00:00
|
|
|
public function getLocalReference( array $params ) {
|
2012-01-12 18:44:00 +00:00
|
|
|
$source = $this->resolveToFSPath( $params['src'] );
|
2011-12-20 03:52:06 +00:00
|
|
|
if ( $source === null ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return new FSFile( $source );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-29 22:22:28 +00:00
|
|
|
* @see FileBackendStore::getLocalCopy()
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2012-01-04 01:08:33 +00:00
|
|
|
public function getLocalCopy( array $params ) {
|
2012-01-12 18:44:00 +00:00
|
|
|
$source = $this->resolveToFSPath( $params['src'] );
|
2011-12-20 03:52:06 +00:00
|
|
|
if ( $source === null ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-04 01:08:33 +00:00
|
|
|
// Create a new temporary file with the same extension...
|
|
|
|
|
$ext = FileBackend::extensionFromPath( $params['src'] );
|
2011-12-20 03:52:06 +00:00
|
|
|
$tmpFile = TempFSFile::factory( wfBaseName( $source ) . '_', $ext );
|
|
|
|
|
if ( !$tmpFile ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$tmpPath = $tmpFile->getPath();
|
|
|
|
|
|
|
|
|
|
// Copy the source file over the temp file
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$ok = copy( $source, $tmpPath );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
if ( !$ok ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->chmod( $tmpPath );
|
|
|
|
|
|
|
|
|
|
return $tmpFile;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Chmod a file, suppressing the warnings
|
|
|
|
|
*
|
|
|
|
|
* @param $path string Absolute file system path
|
|
|
|
|
* @return bool Success
|
|
|
|
|
*/
|
|
|
|
|
protected function chmod( $path ) {
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$ok = chmod( $path, $this->fileMode );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
|
|
|
|
|
return $ok;
|
|
|
|
|
}
|
2012-01-13 04:32:28 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Suppress E_WARNING errors and track whether any happen
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function trapWarnings() {
|
|
|
|
|
$this->hadWarningErrors[] = false; // push to stack
|
|
|
|
|
set_error_handler( array( $this, 'handleWarning' ), E_WARNING );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Unsuppress E_WARNING errors and return true if any happened
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function untrapWarnings() {
|
|
|
|
|
restore_error_handler(); // restore previous handler
|
|
|
|
|
return array_pop( $this->hadWarningErrors ); // pop from stack
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function handleWarning() {
|
|
|
|
|
$this->hadWarningErrors[count( $this->hadWarningErrors ) - 1] = true;
|
|
|
|
|
return true; // suppress from PHP handler
|
|
|
|
|
}
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wrapper around RecursiveDirectoryIterator that catches
|
|
|
|
|
* exception or does any custom behavoir that we may want.
|
2012-01-13 23:30:46 +00:00
|
|
|
* Do not use this class from places outside FSFileBackend.
|
2011-12-20 03:52:06 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup FileBackend
|
|
|
|
|
*/
|
2012-01-14 01:52:19 +00:00
|
|
|
class FSFileBackendFileList implements Iterator {
|
2011-12-20 03:52:06 +00:00
|
|
|
/** @var RecursiveIteratorIterator */
|
|
|
|
|
protected $iter;
|
2012-01-06 05:15:51 +00:00
|
|
|
protected $suffixStart; // integer
|
2012-01-27 20:12:50 +00:00
|
|
|
protected $pos = 0; // integer
|
2011-12-20 03:52:06 +00:00
|
|
|
|
|
|
|
|
/**
|
2012-01-14 01:52:19 +00:00
|
|
|
* @param $dir string file system directory
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
|
|
|
|
public function __construct( $dir ) {
|
2012-01-12 22:01:02 +00:00
|
|
|
$dir = realpath( $dir ); // normalize
|
|
|
|
|
$this->suffixStart = strlen( $dir ) + 1; // size of "path/to/dir/"
|
2011-12-20 03:52:06 +00:00
|
|
|
try {
|
2012-01-27 20:12:50 +00:00
|
|
|
# Get an iterator that will return leaf nodes (non-directories)
|
2012-01-27 20:35:42 +00:00
|
|
|
if ( MWInit::classExists( 'FilesystemIterator' ) ) { // PHP >= 5.3
|
|
|
|
|
# RecursiveDirectoryIterator extends FilesystemIterator.
|
|
|
|
|
# FilesystemIterator::SKIP_DOTS default is inconsistent in PHP 5.3.x.
|
|
|
|
|
$flags = FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS;
|
|
|
|
|
$this->iter = new RecursiveIteratorIterator(
|
|
|
|
|
new RecursiveDirectoryIterator( $dir, $flags ) );
|
|
|
|
|
} else { // PHP < 5.3
|
|
|
|
|
# RecursiveDirectoryIterator extends DirectoryIterator
|
|
|
|
|
$this->iter = new RecursiveIteratorIterator(
|
|
|
|
|
new RecursiveDirectoryIterator( $dir ) );
|
|
|
|
|
}
|
2011-12-20 03:52:06 +00:00
|
|
|
} catch ( UnexpectedValueException $e ) {
|
|
|
|
|
$this->iter = null; // bad permissions? deleted?
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function current() {
|
2012-01-06 05:15:51 +00:00
|
|
|
// Return only the relative path and normalize slashes to FileBackend-style
|
2012-01-09 13:40:09 +00:00
|
|
|
// Make sure to use the realpath since the suffix is based upon that
|
2012-01-12 18:44:00 +00:00
|
|
|
return str_replace( '\\', '/',
|
|
|
|
|
substr( realpath( $this->iter->current() ), $this->suffixStart ) );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function key() {
|
2012-01-27 20:12:50 +00:00
|
|
|
return $this->pos;
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function next() {
|
|
|
|
|
try {
|
|
|
|
|
$this->iter->next();
|
|
|
|
|
} catch ( UnexpectedValueException $e ) {
|
|
|
|
|
$this->iter = null;
|
|
|
|
|
}
|
2012-01-27 20:12:50 +00:00
|
|
|
++$this->pos;
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function rewind() {
|
2012-01-27 20:12:50 +00:00
|
|
|
$this->pos = 0;
|
2011-12-20 03:52:06 +00:00
|
|
|
try {
|
|
|
|
|
$this->iter->rewind();
|
|
|
|
|
} catch ( UnexpectedValueException $e ) {
|
|
|
|
|
$this->iter = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function valid() {
|
|
|
|
|
return $this->iter && $this->iter->valid();
|
|
|
|
|
}
|
|
|
|
|
}
|