PHP Class Piwik\Filesystem

ファイルを表示 Open project: piwik/piwik Class Usage Examples

Public Methods

Method Description
checkIfFileSystemIsNFS ( ) : boolean Checks if the filesystem Piwik stores sessions in is NFS or not. This check is done in order to avoid using file based sessions on NFS system, since on such a filesystem file locking can make file based sessions incredibly slow.
clearPhpCaches ( )
copy ( string $source, string $dest, boolean $excludePhp = false ) : true Copies a file from $source to $dest.
copyRecursive ( string $source, string $target, boolean $excludePhp = false ) Copies the contents of a directory recursively from $source to $target.
deleteAllCacheOnUpdate ( $pluginName = false ) Called on Core install, update, plugin enable/disable Will clear all cache that could be affected by the change in configuration being made
deleteFileIfExists ( string $pathToFile ) : boolean Deletes the given file if it exists.
directoryDiff ( $source, $target ) : string[] Computes the difference of directories. Compares $target against $source and returns a relative path to all files and directories in $target that are not present in $source.
getFileSize ( string $pathToFile, string $unit = 'B' ) : float | null Get the size of a file in the specified unit.
getPathToPiwikRoot ( ) : string ending WITHOUT slash
globr ( string $sDir, string $sPattern, integer $nFlags = null ) : array Recursively find pathnames that match a pattern.
isValidFilename ( string $filename ) : boolean Returns true if the string is a valid filename File names that start with a-Z or 0-9 and contain a-Z, 0-9, underscore(_), dash(-), and dot(.) will be accepted.
mkdir ( string $path ) Attempts to create a new directory. All errors are silenced.
realpath ( string $path ) : string Get canonicalized absolute path See http://php.net/realpath
remove ( string $file, boolean $silenceErrors = false ) Remove a file.
sortFilesDescByPathLength ( string[] $files ) : string[] Sort all given paths/filenames by its path length. Long path names will be listed first. This method can be useful if you have for instance a bunch of files/directories to delete. By sorting them by lengh you can make sure to delete all files within the folders before deleting the actual folder.
unlinkRecursive ( string $dir, boolean $deleteRootToo, Closure $beforeUnlink = null ) Recursively deletes a directory.
unlinkTargetFilesNotPresentInSource ( string $source, string $target ) Removes all files and directories that are present in the target directory but are not in the source directory.

Private Methods

Method Description
createIndexFilesToPreventDirectoryListing ( $path ) in tmp/ (sub-)folder(s) we create empty index.htm|php files
getChmodForPath ( $path ) : integer
hasPHPExtension ( $file )
havePhpFilesSameContent ( $file1, $file2 )
isPathWithinTmpFolder ( $path ) : boolean
tryToCopyFileAndVerifyItWasCopied ( $source, $dest )

Method Details

checkIfFileSystemIsNFS() public static method

Note: In order to figure this out, we try to run the 'df' program. If the 'exec' or 'shell_exec' functions are not available, we can't do the check.
public static checkIfFileSystemIsNFS ( ) : boolean
return boolean True if on an NFS filesystem, false if otherwise or if we can't use shell_exec or exec.

clearPhpCaches() public static method

public static clearPhpCaches ( )

copy() public static method

Copies a file from $source to $dest.
public static copy ( string $source, string $dest, boolean $excludePhp = false ) : true
$source string A path to a file, eg. './tmp/latest/index.php'. The file must exist.
$dest string A path to a file, eg. './index.php'. The file does not have to exist.
$excludePhp boolean Whether to avoid copying files if the file is related to PHP (includes .php, .tpl, .twig files).
return true

copyRecursive() public static method

Copies the contents of a directory recursively from $source to $target.
public static copyRecursive ( string $source, string $target, boolean $excludePhp = false )
$source string A directory or file to copy, eg. './tmp/latest'.
$target string A directory to copy to, eg. '.'.
$excludePhp boolean Whether to avoid copying files if the file is related to PHP (includes .php, .tpl, .twig files).

deleteAllCacheOnUpdate() public static method

Called on Core install, update, plugin enable/disable Will clear all cache that could be affected by the change in configuration being made
public static deleteAllCacheOnUpdate ( $pluginName = false )

deleteFileIfExists() public static method

Deletes the given file if it exists.
public static deleteFileIfExists ( string $pathToFile ) : boolean
$pathToFile string
return boolean true in case of success or if file does not exist, false otherwise. It might fail in case the file is not writeable.

directoryDiff() public static method

Computes the difference of directories. Compares $target against $source and returns a relative path to all files and directories in $target that are not present in $source.
public static directoryDiff ( $source, $target ) : string[]
$source
$target
return string[]

getFileSize() public static method

Get the size of a file in the specified unit.
public static getFileSize ( string $pathToFile, string $unit = 'B' ) : float | null
$pathToFile string
$unit string eg 'B' for Byte, 'KB', 'MB', 'GB', 'TB'.
return float | null Returns null if file does not exist or the size of the file in the specified unit

getPathToPiwikRoot() public static method

ending WITHOUT slash
public static getPathToPiwikRoot ( ) : string
return string

globr() public static method

See {@link http://php.net/manual/en/function.glob.php glob} for more info.
public static globr ( string $sDir, string $sPattern, integer $nFlags = null ) : array
$sDir string directory The directory to glob in.
$sPattern string pattern The pattern to match paths against.
$nFlags integer `glob()` . See {@link http://php.net/manual/en/function.glob.php glob()}.
return array The list of paths that match the pattern.

isValidFilename() public static method

File names beginning with anything but a-Z or 0-9 will be rejected (including .htaccess for example). File names containing anything other than above mentioned will also be rejected (file names with spaces won't be accepted).
public static isValidFilename ( string $filename ) : boolean
$filename string
return boolean

mkdir() public static method

_Note: This function does **not** create directories recursively._
public static mkdir ( string $path )
$path string The path of the directory to create.

realpath() public static method

Get canonicalized absolute path See http://php.net/realpath
public static realpath ( string $path ) : string
$path string
return string canonicalized absolute path

remove() public static method

Remove a file.
public static remove ( string $file, boolean $silenceErrors = false )
$file string
$silenceErrors boolean If true, no exception will be thrown in case removing fails.

sortFilesDescByPathLength() public static method

Sort all given paths/filenames by its path length. Long path names will be listed first. This method can be useful if you have for instance a bunch of files/directories to delete. By sorting them by lengh you can make sure to delete all files within the folders before deleting the actual folder.
public static sortFilesDescByPathLength ( string[] $files ) : string[]
$files string[]
return string[]

unlinkRecursive() public static method

Recursively deletes a directory.
public static unlinkRecursive ( string $dir, boolean $deleteRootToo, Closure $beforeUnlink = null )
$dir string Path of the directory to delete.
$deleteRootToo boolean If true, `$dir` is deleted, otherwise just its contents.
$beforeUnlink Closure An optional closure to execute on a file path before unlinking.

unlinkTargetFilesNotPresentInSource() public static method

Removes all files and directories that are present in the target directory but are not in the source directory.
public static unlinkTargetFilesNotPresentInSource ( string $source, string $target )
$source string Path to the source directory
$target string Path to the target