45 $this->absolute_path = $this->file_object->getDirectory($this->file_object->getVersion()) .
"/" . $this->file_object->getFileName();
57 $fs_relative_path_existing = $this->fs->has($this->relative_path);
59 $native_absolute_path_exists = file_exists($this->absolute_path);
63 !$fs_relative_path_existing
64 || !$fs_valid_relative_path_existing
65 || !$native_absolute_path_exists
66 || !$native_valid_absolute_path_existing
80 $DIC->logger()->root()->debug(
"FilePathSanitizer: " .
$message);
92 $dirname = dirname($this->relative_path);
93 if (!$this->fs->has($dirname)) {
94 $this->
log(
"FAILED: Sanitizing File Path: {$this->file_object->getFile()}. Message: Directory not found");
98 $first_file = reset($this->fs->listContents($dirname));
103 if (!$this->fs->has($valid_filename)) {
104 $this->fs->rename($first_file->getPath(), $valid_filename);
107 $this->
log(
"Sanitized File Path: {$valid_filename}");
113 $this->
log(
"FAILED: Sanitizing File Path: {$this->file_object->getFile()}. Message: {$e->getMessage()}. Will try using native PHP");
117 $scandir = scandir(dirname($this->absolute_path));
118 if (isset($scandir[2])) {
119 $first_file = $scandir[2];
120 if (is_file($first_file)) {
122 if (rename($first_file, $valid_filename)) {
124 $this->
log(
"Sanitized File Path: {$valid_filename}");
127 throw new Exception(
"is not a file: " . $first_file);
130 throw new Exception(
"no File found in " . dirname($this->absolute_path));
133 $this->
log(
"FAILED AGAIN: Sanitizing File Path: {$this->file_object->getFile()}. Message: {$e->getMessage()}");
137 if ($item->isDot()) {
140 if ($item->isFile()) {
142 if (rename($item->getPathname(), $valid_filename)) {
144 $this->
log(
"Sanitized File Path: {$valid_filename}");
150 $this->
log(
"FAILED AGAIN and AGAIN: Sanitizing File Path: {$this->file_object->getFile()}. Message: {$e->getMessage()}");
173 $valid_filename = $first_file;
175 while (preg_match(
'#\p{C}+|^\./#u', $valid_filename)) {
176 $valid_filename = preg_replace(
'#\p{C}+|^\./#u',
'', $valid_filename);
179 $valid_filename = preg_replace(
'/[\x00-\x1F\x7F-\xFF]/',
'', $valid_filename);
188 return $valid_filename;
197 $sanitized_filename = basename($valid_filename);
198 $this->file_object->setFileName($sanitized_filename);
199 $this->file_object->update();
__construct(ilObjFile $file_object)
FilePathSanitizer constructor.
static createRelativePath($absolute_path)
Creates a relative path from an absolute path which starts with a valid storage location.
catch(Exception $e) $message
static deriveFilesystemFrom($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)