ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
FilesystemWhitelistDecorator.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
31 
41 {
42  public function __construct(private Filesystem $filesystem, private FilenameSanitizer $sanitizer)
43  {
44  }
45 
49  public function hasDir(string $path): bool
50  {
51  return $this->filesystem->hasDir($path);
52  }
53 
57  public function listContents(string $path = '', bool $recursive = false): array
58  {
59  return $this->filesystem->listContents($path, $recursive);
60  }
61 
65  public function createDir(string $path, string $visibility = Visibility::PUBLIC_ACCESS): void
66  {
67  $this->filesystem->createDir($path, $visibility);
68  }
69 
73  public function copyDir(string $source, string $destination): void
74  {
75  $this->ensureDirectoryExistence($source);
76  $this->ensureEmptyDirectory($destination);
77 
78  $contentList = $this->listContents($source, true);
79 
80  //foreach file and dir
81  foreach ($contentList as $content) {
82  //ignore the directories and only copy the files
83  if ($content->isFile()) {
84  //create destination path
85  $position = strpos($content->getPath(), $source);
86  if ($position !== false) {
87  $destinationFilePath = substr_replace(
88  $content->getPath(),
89  $destination,
90  $position,
91  strlen($source)
92  );
93  $this->copy($content->getPath(), $destinationFilePath);
94  }
95  }
96  }
97  }
98 
102  public function deleteDir(string $path): void
103  {
104  $this->filesystem->deleteDir($path);
105  }
106 
110  public function read(string $path): string
111  {
112  return $this->filesystem->read($path);
113  }
114 
118  public function has(string $path): bool
119  {
120  return $this->filesystem->has($path);
121  }
122 
126  public function getMimeType(string $path): string
127  {
128  return $this->filesystem->getMimeType($path);
129  }
130 
134  public function getTimestamp(string $path): \DateTimeImmutable
135  {
136  return $this->filesystem->getTimestamp($path);
137  }
138 
142  public function getSize(string $path, int $unit): DataSize
143  {
144  return $this->filesystem->getSize(
145  $path,
146  $unit
147  );
148  }
149 
153  public function setVisibility(string $path, string $visibility): bool
154  {
155  return $this->filesystem->setVisibility(
156  $this->sanitizer->sanitize($path),
157  $visibility
158  );
159  }
160 
164  public function getVisibility(string $path): string
165  {
166  return $this->filesystem->getVisibility($path);
167  }
168 
172  public function readStream(string $path): FileStream
173  {
174  return $this->filesystem->readStream($path);
175  }
176 
180  public function writeStream(string $path, FileStream $stream): void
181  {
182  $this->filesystem->writeStream($this->sanitizer->sanitize($path), $stream);
183  }
184 
188  public function putStream(string $path, FileStream $stream): void
189  {
190  $this->filesystem->putStream($this->sanitizer->sanitize($path), $stream);
191  }
192 
196  public function updateStream(string $path, FileStream $stream): void
197  {
198  $this->filesystem->updateStream($this->sanitizer->sanitize($path), $stream);
199  }
200 
204  public function write(string $path, string $content): void
205  {
206  $this->filesystem->write($this->sanitizer->sanitize($path), $content);
207  }
208 
212  public function update(string $path, string $new_content): void
213  {
214  $this->filesystem->update($this->sanitizer->sanitize($path), $new_content);
215  }
216 
220  public function put(string $path, string $content): void
221  {
222  $this->filesystem->put($this->sanitizer->sanitize($path), $content);
223  }
224 
228  public function delete(string $path): void
229  {
230  $this->filesystem->delete($path);
231  }
232 
236  public function readAndDelete(string $path): string
237  {
238  return $this->filesystem->readAndDelete($path);
239  }
240 
244  public function rename(string $path, string $new_path): void
245  {
246  $this->filesystem->rename(
247  $path,
248  $this->sanitizer->sanitize($new_path)
249  );
250  }
251 
255  public function copy(string $path, string $copy_path): void
256  {
257  $this->filesystem->copy(
258  $path,
259  $this->sanitizer->sanitize($copy_path)
260  );
261  }
262 
270  private function ensureEmptyDirectory(string $path): void
271  {
272  //check if destination dir is empty
273  try {
274  $destinationContent = $this->listContents($path, true);
275  if ($destinationContent !== []) {
276  throw new IOException("Destination \"$path\" is not empty can not copy files.");
277  }
278  } catch (DirectoryNotFoundException) {
279  //nothing needs to be done the destination was not found
280  }
281  }
282 
291  private function ensureDirectoryExistence(string $path): void
292  {
293  if (!$this->hasDir($path)) {
294  throw new DirectoryNotFoundException("Directory \"$path\" not found.");
295  }
296  }
297 
301  public function finder(): Finder
302  {
303  return $this->filesystem->finder();
304  }
305 }
createDir(string $path, string $visibility=Visibility::PUBLIC_ACCESS)
__construct(private Filesystem $filesystem, private FilenameSanitizer $sanitizer)
Indicates general problems with the input or output operations.
Definition: IOException.php:27
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class provides the data size with additional information to remove the work to calculate the siz...
Definition: DataSize.php:30
ensureEmptyDirectory(string $path)
Ensures that the given path does not exist or is empty.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
The filesystem white list decorator rewrites forbidden file endings and delegates the rest of the ope...
Indicates that the directory is missing or not found.
$path
Definition: ltiservices.php:29
The filename sanitizer verifies and fixes file name endings.
ensureDirectoryExistence(string $path)
Checks if the directory exists.
const PUBLIC_ACCESS
Public file visibility.
Definition: Visibility.php:34
The base interface for all filesystem streams.
Definition: FileStream.php:31