X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Ffilebackend%2FFSFileBackend.php;h=993e2bac49420e04301374a7a5bdcb3d60b21135;hb=34a4bcbd48bf7af458eb110cc383c5bbb6989a31;hp=52c466bfbdf6441de5e4731a88657693ea2c6236;hpb=1180505b80a1cbfb77695ac7a177c1f0026e4b71;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/filebackend/FSFileBackend.php b/includes/filebackend/FSFileBackend.php index 52c466bfbd..993e2bac49 100644 --- a/includes/filebackend/FSFileBackend.php +++ b/includes/filebackend/FSFileBackend.php @@ -39,14 +39,22 @@ * @since 1.19 */ class FSFileBackend extends FileBackendStore { - 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 - protected $fileOwner; // string; required OS username to own files - protected $currentUser; // string; OS username running this script - - /** @var Array */ + /** @var string Directory holding the container directories */ + protected $basePath; + + /** @var array Map of container names to root paths for custom container paths */ + protected $containerPaths = array(); + + /** @var int File permission mode */ + protected $fileMode; + + /** @var string Required OS username to own files */ + protected $fileOwner; + + /** @var string OS username running this script */ + protected $currentUser; + + /** @var array */ protected $hadWarningErrors = array(); /** @@ -90,6 +98,7 @@ class FSFileBackend extends FileBackendStore { return $relStoragePath; } } + return null; } @@ -115,8 +124,8 @@ class FSFileBackend extends FileBackendStore { * 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 + * @param string $shortCont + * @param string $fullCont * @return string|null */ protected function containerFSRoot( $shortCont, $fullCont ) { @@ -125,6 +134,7 @@ class FSFileBackend extends FileBackendStore { } elseif ( isset( $this->basePath ) ) { return "{$this->basePath}/{$fullCont}"; } + return null; // no container base path defined } @@ -144,6 +154,7 @@ class FSFileBackend extends FileBackendStore { if ( $relPath != '' ) { $fsPath .= "/{$relPath}"; } + return $fsPath; } @@ -174,6 +185,7 @@ class FSFileBackend extends FileBackendStore { $dest = $this->resolveToFSPath( $params['dst'] ); if ( $dest === null ) { $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); + return $status; } @@ -181,6 +193,7 @@ class FSFileBackend extends FileBackendStore { $tempFile = TempFSFile::factory( 'create_', 'tmp' ); if ( !$tempFile ) { $status->fatal( 'backend-fail-create', $params['dst'] ); + return $status; } $this->trapWarnings(); @@ -188,6 +201,7 @@ class FSFileBackend extends FileBackendStore { $this->untrapWarnings(); if ( $bytes === false ) { $status->fatal( 'backend-fail-create', $params['dst'] ); + return $status; } $cmd = implode( ' ', array( @@ -203,6 +217,7 @@ class FSFileBackend extends FileBackendStore { $this->untrapWarnings(); if ( $bytes === false ) { $status->fatal( 'backend-fail-create', $params['dst'] ); + return $status; } $this->chmod( $dest ); @@ -227,6 +242,7 @@ class FSFileBackend extends FileBackendStore { $dest = $this->resolveToFSPath( $params['dst'] ); if ( $dest === null ) { $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); + return $status; } @@ -248,6 +264,7 @@ class FSFileBackend extends FileBackendStore { trigger_error( __METHOD__ . ": copy() failed but returned true." ); } $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] ); + return $status; } $this->chmod( $dest ); @@ -272,12 +289,14 @@ class FSFileBackend extends FileBackendStore { $source = $this->resolveToFSPath( $params['src'] ); if ( $source === null ) { $status->fatal( 'backend-fail-invalidpath', $params['src'] ); + return $status; } $dest = $this->resolveToFSPath( $params['dst'] ); if ( $dest === null ) { $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); + return $status; } @@ -285,6 +304,7 @@ class FSFileBackend extends FileBackendStore { if ( empty( $params['ignoreMissingSource'] ) ) { $status->fatal( 'backend-fail-copy', $params['src'] ); } + return $status; // do nothing; either OK or bad status } @@ -308,6 +328,7 @@ class FSFileBackend extends FileBackendStore { trigger_error( __METHOD__ . ": copy() failed but returned true." ); } $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] ); + return $status; } $this->chmod( $dest ); @@ -332,12 +353,14 @@ class FSFileBackend extends FileBackendStore { $source = $this->resolveToFSPath( $params['src'] ); if ( $source === null ) { $status->fatal( 'backend-fail-invalidpath', $params['src'] ); + return $status; } $dest = $this->resolveToFSPath( $params['dst'] ); if ( $dest === null ) { $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); + return $status; } @@ -345,6 +368,7 @@ class FSFileBackend extends FileBackendStore { if ( empty( $params['ignoreMissingSource'] ) ) { $status->fatal( 'backend-fail-move', $params['src'] ); } + return $status; // do nothing; either OK or bad status } @@ -362,6 +386,7 @@ class FSFileBackend extends FileBackendStore { clearstatcache(); // file no longer at source if ( !$ok ) { $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] ); + return $status; } } @@ -385,6 +410,7 @@ class FSFileBackend extends FileBackendStore { $source = $this->resolveToFSPath( $params['src'] ); if ( $source === null ) { $status->fatal( 'backend-fail-invalidpath', $params['src'] ); + return $status; } @@ -392,6 +418,7 @@ class FSFileBackend extends FileBackendStore { if ( empty( $params['ignoreMissingSource'] ) ) { $status->fatal( 'backend-fail-delete', $params['src'] ); } + return $status; // do nothing; either OK or bad status } @@ -407,6 +434,7 @@ class FSFileBackend extends FileBackendStore { $this->untrapWarnings(); if ( !$ok ) { $status->fatal( 'backend-fail-delete', $params['src'] ); + return $status; } } @@ -424,6 +452,12 @@ class FSFileBackend extends FileBackendStore { } } + /** + * @param string $fullCont + * @param $dirRel + * @param array $params + * @return Status + */ protected function doPrepareInternal( $fullCont, $dirRel, array $params ) { $status = Status::newGood(); list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); @@ -444,6 +478,7 @@ class FSFileBackend extends FileBackendStore { if ( is_dir( $dir ) && !$existed ) { $status->merge( $this->doSecureInternal( $fullCont, $dirRel, $params ) ); } + return $status; } @@ -471,6 +506,7 @@ class FSFileBackend extends FileBackendStore { $status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" ); } } + return $status; } @@ -498,6 +534,7 @@ class FSFileBackend extends FileBackendStore { } $this->untrapWarnings(); } + return $status; } @@ -511,6 +548,7 @@ class FSFileBackend extends FileBackendStore { rmdir( $dir ); // remove directory if empty } $this->untrapWarnings(); + return $status; } @@ -557,7 +595,10 @@ class FSFileBackend extends FileBackendStore { /** * @see FileBackendStore::getDirectoryListInternal() - * @return Array|null + * @param string $fullCont + * @param string $dirRel + * @param array $params + * @return array|null */ public function getDirectoryListInternal( $fullCont, $dirRel, array $params ) { list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); @@ -566,17 +607,23 @@ class FSFileBackend extends FileBackendStore { $exists = is_dir( $dir ); if ( !$exists ) { wfDebug( __METHOD__ . "() given directory does not exist: '$dir'\n" ); + return array(); // nothing under this dir } elseif ( !is_readable( $dir ) ) { wfDebug( __METHOD__ . "() given directory is unreadable: '$dir'\n" ); + return null; // bad permissions? } + return new FSFileBackendDirList( $dir, $params ); } /** * @see FileBackendStore::getFileListInternal() - * @return Array|FSFileBackendFileList|null + * @param string $fullCont + * @param string $dirRel + * @param array $params + * @return array|FSFileBackendFileList|null */ public function getFileListInternal( $fullCont, $dirRel, array $params ) { list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); @@ -585,11 +632,14 @@ class FSFileBackend extends FileBackendStore { $exists = is_dir( $dir ); if ( !$exists ) { wfDebug( __METHOD__ . "() given directory does not exist: '$dir'\n" ); + return array(); // nothing under this dir } elseif ( !is_readable( $dir ) ) { wfDebug( __METHOD__ . "() given directory is unreadable: '$dir'\n" ); + return null; // bad permissions? } + return new FSFileBackendFileList( $dir, $params ); } @@ -718,8 +768,6 @@ class FSFileBackend extends FileBackendStore { /** * Listen for E_WARNING errors and track whether any happen - * - * @return void */ protected function trapWarnings() { $this->hadWarningErrors[] = false; // push to stack @@ -737,14 +785,15 @@ class FSFileBackend extends FileBackendStore { } /** - * @param $errno integer - * @param $errstr string + * @param int $errno + * @param string $errstr * @return bool * @access private */ public function handleWarning( $errno, $errstr ) { wfDebugLog( 'FSFileBackend', $errstr ); // more detailed error logging $this->hadWarningErrors[count( $this->hadWarningErrors ) - 1] = true; + return true; // suppress from PHP handler } } @@ -757,13 +806,15 @@ class FSFileOpHandle extends FileBackendStoreOpHandle { public $chmodPath; // string; file to chmod /** - * @param $backend - * @param $params array - * @param $call - * @param $cmd - * @param $chmodPath null + * @param FSFileBackend $backend + * @param array $params + * @param string $call + * @param string $cmd + * @param int|null $chmodPath */ - public function __construct( $backend, array $params, $call, $cmd, $chmodPath = null ) { + public function __construct( + FSFileBackend $backend, array $params, $call, $cmd, $chmodPath = null + ) { $this->backend = $backend; $this->params = $params; $this->call = $call; @@ -782,14 +833,19 @@ class FSFileOpHandle extends FileBackendStoreOpHandle { abstract class FSFileBackendList implements Iterator { /** @var Iterator */ protected $iter; - protected $suffixStart; // integer - protected $pos = 0; // integer - /** @var Array */ + + /** @var int */ + protected $suffixStart; + + /** @var int */ + protected $pos = 0; + + /** @var array */ protected $params = array(); /** * @param string $dir file system directory - * @param $params array + * @param array $params */ public function __construct( $dir, array $params ) { $path = realpath( $dir ); // normalize @@ -821,6 +877,7 @@ abstract class FSFileBackendList implements Iterator { # RecursiveDirectoryIterator extends FilesystemIterator. # FilesystemIterator::SKIP_DOTS default is inconsistent in PHP 5.3.x. $flags = FilesystemIterator::CURRENT_AS_SELF | FilesystemIterator::SKIP_DOTS; + return new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir, $flags ), RecursiveIteratorIterator::CHILD_FIRST // include dirs @@ -830,7 +887,7 @@ abstract class FSFileBackendList implements Iterator { /** * @see Iterator::key() - * @return integer + * @return int */ public function key() { return $this->pos; @@ -846,29 +903,29 @@ abstract class FSFileBackendList implements Iterator { /** * @see Iterator::next() - * @return void + * @throws FileBackendError */ public function next() { try { $this->iter->next(); $this->filterViaNext(); - } catch ( UnexpectedValueException $e ) { - $this->iter = null; + } catch ( UnexpectedValueException $e ) { // bad permissions? deleted? + throw new FileBackendError( "File iterator gave UnexpectedValueException." ); } ++$this->pos; } /** * @see Iterator::rewind() - * @return void + * @throws FileBackendError */ public function rewind() { $this->pos = 0; try { $this->iter->rewind(); $this->filterViaNext(); - } catch ( UnexpectedValueException $e ) { - $this->iter = null; + } catch ( UnexpectedValueException $e ) { // bad permissions? deleted? + throw new FileBackendError( "File iterator gave UnexpectedValueException." ); } } @@ -883,13 +940,14 @@ abstract class FSFileBackendList implements Iterator { /** * Filter out items by advancing to the next ones */ - protected function filterViaNext() {} + protected function filterViaNext() { + } /** * Return only the relative path and normalize slashes to FileBackend-style. * Uses the "real path" since the suffix is based upon that. * - * @param $path string + * @param string $dir * @return string */ protected function getRelPath( $dir ) { @@ -897,6 +955,7 @@ abstract class FSFileBackendList implements Iterator { if ( $path === false ) { $path = $dir; } + return strtr( substr( $path, $this->suffixStart ), '\\', '/' ); } }