49 $this->version = (int) $file_object->
getVersion();
51 $this->absolute_path = $this->file_object->getDirectory($this->version) .
"/" . $this->file_object->getFileName();
63 $fs_relative_path_existing = $this->fs->has($this->relative_path);
65 $native_absolute_path_exists = file_exists($this->absolute_path);
69 !$fs_relative_path_existing
70 || !$fs_valid_relative_path_existing
71 || !$native_absolute_path_exists
72 || !$native_valid_absolute_path_existing
86 $DIC->logger()->root()->debug(
"FilePathSanitizer: " .
$message);
95 $versions = count($this->file_object->getVersions());
96 if ($versions === 1) {
97 $version_one = $this->file_object->getDirectory(1) .
"/" . $this->file_object->getFileName();
99 $version_two = $this->file_object->getDirectory(2) .
"/" . $this->file_object->getFileName();
101 if (!$this->fs->has($relative_version_one) && $this->fs->has($relative_version_two)) {
109 $dirname = dirname($this->relative_path);
110 if (!$this->fs->has($dirname)) {
111 $this->
log(
"FAILED: Sanitizing File Path: {$this->file_object->getFile()}. Message: Directory not found");
116 $first_file = reset($this->fs->listContents($dirname));
118 $this->
log(
"FAILED AGAIN and AGAIN: Sanitizing File Path: {$this->file_object->getFile()}. Message: {$e->getMessage()}");
126 if (!$this->fs->has($valid_filename)) {
127 $this->fs->rename($first_file->getPath(), $valid_filename);
130 $this->
log(
"Sanitized File Path: {$valid_filename}");
136 $this->
log(
"FAILED: Sanitizing File Path: {$this->file_object->getFile()}. Message: {$e->getMessage()}. Will try using native PHP");
140 $scandir = scandir(dirname($this->absolute_path));
141 if (isset($scandir[2])) {
142 $first_file = $scandir[2];
143 if (is_file($first_file)) {
145 if (rename($first_file, $valid_filename)) {
147 $this->
log(
"Sanitized File Path: {$valid_filename}");
150 throw new Exception(
"is not a file: " . $first_file);
153 throw new Exception(
"no File found in " . dirname($this->absolute_path));
156 $this->
log(
"FAILED AGAIN: Sanitizing File Path: {$this->file_object->getFile()}. Message: {$e->getMessage()}");
160 if ($item->isDot()) {
163 if ($item->isFile()) {
165 if (rename($item->getPathname(), $valid_filename)) {
167 $this->
log(
"Sanitized File Path: {$valid_filename}");
173 $this->
log(
"FAILED AGAIN and AGAIN: Sanitizing File Path: {$this->file_object->getFile()}. Message: {$e->getMessage()}");
196 $valid_filename = $first_file;
198 while (preg_match(
'#\p{C}+|^\./#u', $valid_filename)) {
199 $valid_filename = preg_replace(
'#\p{C}+|^\./#u',
'', $valid_filename);
202 $valid_filename = preg_replace(
'/[\x00-\x1F\x7F-\xFF]/',
'', $valid_filename);
211 return $valid_filename;
220 $sanitized_filename = basename($valid_filename);
221 $this->file_object->setFileName($sanitized_filename);
222 $this->file_object->update();
static createRelativePath(string $absolute_path)
Creates a relative path from an absolute path which starts with a valid storage location.
__construct(ilObjFile $file_object)
FilePathSanitizer constructor.
Class DirectoryNotFoundException.
catch(Exception $e) $message
static deriveFilesystemFrom(string $absolute_path)
Tries to fetch the filesystem responsible for the absolute path.
saveNewNameForFileObject($valid_filename)
Class FlySystemFileAccessTest.
static getValidFilename($a_filename)
Get valid filename.
santitizeFilename($first_file)