PrivilegedFileManager
in package
implements
IFileManager
Privileged file manager
Table of Contents
Interfaces
- IFileManager
- File manager interface
Constants
- WRITE_SCRIPT = <<<'SH' set -e target=$(realpath -m -- "$1") mkdir -p -- "$(dirname -- "$target")" tmp=$(mktemp -- "$target.XXXXXX") trap 'rm -f -- "$tmp"' EXIT if [ -e "$target" ]; then cp --attributes-only --preserve=mode,ownership,xattr -- "$target" "$tmp" elif [ -z "$2" ]; then chmod -- "$(printf '%o' $((0666 & ~$(umask))))" "$tmp" fi cat > "$tmp" if [ -n "$2" ]; then chmod -- "$2" "$tmp" fi mv -f -- "$tmp" "$target" trap - EXIT SH
- Shell script to write the file atomically, arguments: target path, optional mode (octal)
Properties
- $commandExecutor : CommandExecutor
- $directory : string
Methods
- __construct() : mixed
- Constructor
- chmod() : void
- Changes the permissions of a file or directory
- chown() : void
- Changes the owner of a file or directory
- copy() : void
- Copies file to a destination
- createDirectory() : void
- Creates a directory (including missing parent directories)
- createSymLink() : void
- Creates the symbolic link
- delete() : void
- Deletes the file
- exists() : bool
- Checks if the file exists
- getBasePath() : string
- Returns Base directory path
- isSymLink() : bool
- Checks if the file is symbolic link
- listDirectories() : array<string|int, string>
- Returns list of subdirectories in directory
- listFiles() : array<int, string>
- Returns list of files in directory
- read() : string
- Reads the file
- readIni() : array<string, mixed>
- Reads an INI file and decodes contents to array
- readJson() : mixed
- Reads a JSON file and decode contents to array
- readNeon() : mixed
- Reads a NEON file and decodes contents to array
- readStream() : resource
- Opens the file for reading as a stream
- write() : void
- Writes into the file
- writeIni() : void
- Encodes content from array into INI and writes to file
- writeJson() : void
- Encodes content from array into JSON and writes to file
- writeNeon() : void
- Encodes content from array into NEON and writes to file
- writeStream() : void
- Writes a stream into the file
- buildPath() : string
- Returns path to subdirectory or file, if the path contains spaces, returned string is surrounded in quotation marks
- writeAtomic() : void
- Writes the content into the file atomically
Constants
WRITE_SCRIPT
Shell script to write the file atomically, arguments: target path, optional mode (octal)
private
mixed
WRITE_SCRIPT
= <<<'SH'
set -e
target=$(realpath -m -- "$1")
mkdir -p -- "$(dirname -- "$target")"
tmp=$(mktemp -- "$target.XXXXXX")
trap 'rm -f -- "$tmp"' EXIT
if [ -e "$target" ]; then
cp --attributes-only --preserve=mode,ownership,xattr -- "$target" "$tmp"
elif [ -z "$2" ]; then
chmod -- "$(printf '%o' $((0666 & ~$(umask))))" "$tmp"
fi
cat > "$tmp"
if [ -n "$2" ]; then
chmod -- "$2" "$tmp"
fi
mv -f -- "$tmp" "$target"
trap - EXIT
SH
The content from the standard input is written into a temporary file in the same directory, which is renamed to the target afterwards. Symbolic links are resolved, so the target of the link is replaced. The mode, owner, ACL and extended attributes of the existing file are preserved, unless the mode is specified. The temporary file is created with mode 0600 by mktemp, so the content is never exposed with wider permissions.
Properties
$commandExecutor read-only
private
CommandExecutor
$commandExecutor
$directory read-only
private
string
$directory
Directory path
Methods
__construct()
Constructor
public
__construct(string $directory, CommandExecutor $commandExecutor) : mixed
Parameters
- $directory : string
-
Directory path
- $commandExecutor : CommandExecutor
-
Command manager
chmod()
Changes the permissions of a file or directory
public
chmod(string|null $path, int $mode[, bool $recursive = false ]) : void
Parameters
- $path : string|null
-
Path to the file or directory
- $mode : int
-
File mode
- $recursive : bool = false
-
Indicates whether the mode should be changed recursively
chown()
Changes the owner of a file or directory
public
chown(string|null $path, string $user[, string|null $group = null ][, bool $recursive = false ]) : void
Parameters
- $path : string|null
-
Path to the file or directory
- $user : string
-
User name
- $group : string|null = null
-
Group name
- $recursive : bool = false
-
Indicates whether the owner should be changed recursively
copy()
Copies file to a destination
public
copy(string $destination, string $fileName) : void
Parameters
- $destination : string
-
Destination path
- $fileName : string
-
Source file path
createDirectory()
Creates a directory (including missing parent directories)
public
createDirectory(string $directory[, int|null $mode = null ]) : void
Parameters
- $directory : string
-
Relative path to the directory
- $mode : int|null = null
-
Directory mode (permissions) applied to the directory, null to keep the default
createSymLink()
Creates the symbolic link
public
createSymLink(string $target, string $link) : void
Parameters
- $target : string
-
Target of the symbolic link
- $link : string
-
Symbolic link name
delete()
Deletes the file
public
delete(string $fileName) : void
Parameters
- $fileName : string
-
File name
exists()
Checks if the file exists
public
exists(string $fileName) : bool
Parameters
- $fileName : string
-
File name
Return values
bool —Is file exists?
getBasePath()
Returns Base directory path
public
getBasePath() : string
Return values
string —Base directory path
isSymLink()
Checks if the file is symbolic link
public
isSymLink(string $fileName) : bool
Parameters
- $fileName : string
-
File name
Return values
bool —Is symbolic link?
listDirectories()
Returns list of subdirectories in directory
public
listDirectories([string|null $subdirectory = null ]) : array<string|int, string>
Parameters
- $subdirectory : string|null = null
-
Relative path to subdirectory
Return values
array<string|int, string> —List of directories
listFiles()
Returns list of files in directory
public
listFiles([string|null $subdirectory = null ][, bool $includeSymlinks = false ]) : array<int, string>
Parameters
- $subdirectory : string|null = null
-
Relative path to subdirectory
- $includeSymlinks : bool = false
-
Include symlinks in list
Return values
array<int, string> —List of files
read()
Reads the file
public
read(string $fileName) : string
Parameters
- $fileName : string
-
File name
Return values
string —File content
readIni()
Reads an INI file and decodes contents to array
public
readIni(string $fileName) : array<string, mixed>
Parameters
- $fileName : string
-
File name
Return values
array<string, mixed> —Decoded INI file
readJson()
Reads a JSON file and decode contents to array
public
readJson(string $fileName[, bool $forceArray = true ]) : mixed
Parameters
- $fileName : string
-
File name
- $forceArray : bool = true
-
Force object to array conversion
Return values
mixed —Decoded JSON data
readNeon()
Reads a NEON file and decodes contents to array
public
readNeon(string $fileName) : mixed
Parameters
- $fileName : string
-
File name
Return values
mixed —Decoded NEON file
readStream()
Opens the file for reading as a stream
public
readStream(string $fileName) : resource
Parameters
- $fileName : string
-
File name
Return values
resource —Readable stream, the caller is responsible for closing it
write()
Writes into the file
public
write(string $fileName, mixed $content[, int|null $mode = null ]) : void
Parameters
- $fileName : string
-
File name
- $content : mixed
-
File content
- $mode : int|null = null
-
File mode (permissions), null to keep the default
writeIni()
Encodes content from array into INI and writes to file
public
writeIni(string $fileName, array<string|int, mixed> $content[, int|null $mode = null ]) : void
Parameters
- $fileName : string
-
File name
- $content : array<string|int, mixed>
-
Array to encode
- $mode : int|null = null
-
File mode (permissions), null to keep the default
writeJson()
Encodes content from array into JSON and writes to file
public
writeJson(string $fileName, mixed $content[, int|null $mode = null ]) : void
Parameters
- $fileName : string
-
File name
- $content : mixed
-
JSON data to encode
- $mode : int|null = null
-
File mode (permissions), null to keep the default
writeNeon()
Encodes content from array into NEON and writes to file
public
writeNeon(string $fileName, mixed $content[, int|null $mode = null ]) : void
Parameters
- $fileName : string
-
File name
- $content : mixed
-
NEON data to encode
- $mode : int|null = null
-
File mode (permissions), null to keep the default
writeStream()
Writes a stream into the file
public
writeStream(string $fileName, mixed $stream[, int|null $mode = null ]) : void
Parameters
- $fileName : string
-
File name
- $stream : mixed
-
Readable stream with the file content, it is not closed
- $mode : int|null = null
-
File mode (permissions), null to keep the default
buildPath()
Returns path to subdirectory or file, if the path contains spaces, returned string is surrounded in quotation marks
private
buildPath(string|null $name) : string
Parameters
- $name : string|null
-
Name of subdirectory or file
Return values
string —Path to subdirectory or file
writeAtomic()
Writes the content into the file atomically
private
writeAtomic(string $fileName, resource|string|Traversable<string|int, mixed> $content, int|null $mode, int $timeout) : void
Parameters
- $fileName : string
-
File name
- $content : resource|string|Traversable<string|int, mixed>
-
File content
- $mode : int|null
-
File mode (permissions), null to preserve the mode of the existing file
- $timeout : int
-
Command's timeout