ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
ILIAS\File\Sanitation\FilePathSanitizer Class Reference

Class FilePathSanitizer. More...

+ Collaboration diagram for ILIAS\File\Sanitation\FilePathSanitizer:

Public Member Functions

 __construct (ilObjFile $file_object)
 FilePathSanitizer constructor. More...
 
 needsSanitation ()
 
 sanitizeIfNeeded ()
 

Private Member Functions

 log ( $message)
 
 santitizeFilename ($first_file)
 
 saveNewNameForFileObject ($valid_filename)
 

Private Attributes

 $file_object
 
 $relative_path
 
 $fs
 
 $absolute_path
 
 $version = 1
 

Detailed Description

Constructor & Destructor Documentation

◆ __construct()

ILIAS\File\Sanitation\FilePathSanitizer::__construct ( ilObjFile  $file_object)

FilePathSanitizer constructor.

Parameters
ilObjFile$file_object

Definition at line 47 of file FilePathSanitizer.php.

References ILIAS\File\Sanitation\FilePathSanitizer\$file_object, ILIAS\Filesystem\Util\LegacyPathHelper\createRelativePath(), ILIAS\Filesystem\Util\LegacyPathHelper\deriveFilesystemFrom(), and ilObjFile\getVersion().

48  {
49  $this->version = (int) $file_object->getVersion();
50  $this->file_object = $file_object;
51  $this->absolute_path = $this->file_object->getDirectory($this->version) . "/" . $this->file_object->getFileName();
52  $this->relative_path = LegacyPathHelper::createRelativePath($this->absolute_path);
53  $this->fs = LegacyPathHelper::deriveFilesystemFrom($this->absolute_path);
54  }
static createRelativePath(string $absolute_path)
Creates a relative path from an absolute path which starts with a valid storage location.
static deriveFilesystemFrom(string $absolute_path)
Tries to fetch the filesystem responsible for the absolute path.
+ Here is the call graph for this function:

Member Function Documentation

◆ log()

ILIAS\File\Sanitation\FilePathSanitizer::log (   $message)
private
Parameters
string$message

Definition at line 83 of file FilePathSanitizer.php.

References $DIC, and $message.

Referenced by ILIAS\File\Sanitation\FilePathSanitizer\sanitizeIfNeeded().

84  {
85  global $DIC;
86  $DIC->logger()->root()->debug("FilePathSanitizer: " . $message);
87  }
$DIC
Definition: xapitoken.php:46
$message
Definition: xapiexit.php:14
+ Here is the caller graph for this function:

◆ needsSanitation()

ILIAS\File\Sanitation\FilePathSanitizer::needsSanitation ( )
Returns
bool

Definition at line 60 of file FilePathSanitizer.php.

References Vendor\Package\$e, and ilFileUtils\getValidFilename().

Referenced by ILIAS\File\Sanitation\FilePathSanitizer\sanitizeIfNeeded().

61  {
62  try {
63  $fs_relative_path_existing = $this->fs->has($this->relative_path);
64  $fs_valid_relative_path_existing = $this->fs->has(ilFileUtils::getValidFilename($this->relative_path));
65  $native_absolute_path_exists = file_exists($this->absolute_path);
66  $native_valid_absolute_path_existing = file_exists(ilFileUtils::getValidFilename($this->absolute_path));
67 
68  return (
69  !$fs_relative_path_existing
70  || !$fs_valid_relative_path_existing
71  || !$native_absolute_path_exists
72  || !$native_valid_absolute_path_existing
73  );
74  } catch (Exception $e) {
75  return false;
76  }
77  }
static getValidFilename($a_filename)
Get valid filename.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sanitizeIfNeeded()

ILIAS\File\Sanitation\FilePathSanitizer::sanitizeIfNeeded ( )
Returns
bool

Definition at line 93 of file FilePathSanitizer.php.

References Vendor\Package\$e, ILIAS\File\Sanitation\FilePathSanitizer\log(), ILIAS\File\Sanitation\FilePathSanitizer\needsSanitation(), ILIAS\File\Sanitation\FilePathSanitizer\santitizeFilename(), and ILIAS\File\Sanitation\FilePathSanitizer\saveNewNameForFileObject().

94  {
95  if ($this->needsSanitation()) {
96  // First Try: using FileSystemService
97  $dirname = dirname($this->relative_path);
98  if (!$this->fs->has($dirname)) {
99  $this->log("FAILED: Sanitizing File Path: {$this->file_object->getFile()}. Message: Directory not found");
100 
101  return false;
102  }
103  try {
104  $first_file = reset($this->fs->listContents($dirname));
105  } catch (DirectoryNotFoundException $e) {
106  $this->log("FAILED AGAIN and AGAIN: Sanitizing File Path: {$this->file_object->getFile()}. Message: {$e->getMessage()}");
107 
108  return false;
109  }
110  if ($first_file instanceof \ILIAS\Filesystem\DTO\Metadata) {
111  try {
112  $valid_filename = $this->santitizeFilename($first_file->getPath());
113  // rename file in filesystem
114  if (!$this->fs->has($valid_filename)) {
115  $this->fs->rename($first_file->getPath(), $valid_filename);
116  // rename file object
117 
118  $this->log("Sanitized File Path: {$valid_filename}");
119  }
120  $this->saveNewNameForFileObject($valid_filename);
121 
122  return true;
123  } catch (Exception $e) {
124  $this->log("FAILED: Sanitizing File Path: {$this->file_object->getFile()}. Message: {$e->getMessage()}. Will try using native PHP");
125 
126  try {
127  // Second try: use native php
128  $scandir = scandir(dirname($this->absolute_path));
129  if (isset($scandir[2])) {
130  $first_file = $scandir[2];
131  if (is_file($first_file)) {
132  $valid_filename = $this->santitizeFilename($first_file);
133  if (rename($first_file, $valid_filename)) {
134  $this->saveNewNameForFileObject($valid_filename);
135  $this->log("Sanitized File Path: {$valid_filename}");
136  }
137  } else {
138  throw new Exception("is not a file: " . $first_file);
139  }
140  } else {
141  throw new Exception("no File found in " . dirname($this->absolute_path));
142  }
143  } catch (Exception $e) {
144  $this->log("FAILED AGAIN: Sanitizing File Path: {$this->file_object->getFile()}. Message: {$e->getMessage()}");
145 
146  try {
147  foreach (new DirectoryIterator(dirname($this->absolute_path)) as $item) {
148  if ($item->isDot()) {
149  continue;
150  }
151  if ($item->isFile()) {
152  $valid_filename = $this->santitizeFilename($item->getPathname());
153  if (rename($item->getPathname(), $valid_filename)) {
154  $this->saveNewNameForFileObject($valid_filename);
155  $this->log("Sanitized File Path: {$valid_filename}");
156  }
157  break;
158  }
159  }
160  } catch (Exception $e) {
161  $this->log("FAILED AGAIN and AGAIN: Sanitizing File Path: {$this->file_object->getFile()}. Message: {$e->getMessage()}");
162  }
163  }
164 
165  return false;
166  }
167  }
168 
169  return false;
170  }
171 
172  return true;
173  }
Class ChatMainBarProvider .
Class FlySystemFileAccessTest.
+ Here is the call graph for this function:

◆ santitizeFilename()

ILIAS\File\Sanitation\FilePathSanitizer::santitizeFilename (   $first_file)
private
Parameters
$first_file
Returns
string|string[]|null
Exceptions

Definition at line 182 of file FilePathSanitizer.php.

References ilFileUtils\getValidFilename().

Referenced by ILIAS\File\Sanitation\FilePathSanitizer\sanitizeIfNeeded().

183  {
184  $valid_filename = $first_file;
185 
186  while (preg_match('#\p{C}+|^\./#u', $valid_filename)) {
187  $valid_filename = preg_replace('#\p{C}+|^\./#u', '', $valid_filename);
188  }
189 
190  $valid_filename = preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $valid_filename); // removes all non printable characters (ASCII 7 Bit)
191 
192  // $valid_filename = \League\Flysystem\Util::normalizeRelativePath($valid_filename);
193  // $valid_filename = preg_replace('/[\x00-\x1F\x7F-\xA0\xAD]/u', '', $valid_filename);
194  // $valid_filename = iconv(mb_detect_encoding($valid_filename, mb_detect_order(), true), "UTF-8", $valid_filename);
195  // $valid_filename = utf8_encode($valid_filename);
196 
197  $valid_filename = ilFileUtils::getValidFilename($valid_filename);
198 
199  return $valid_filename;
200  }
static getValidFilename($a_filename)
Get valid filename.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ saveNewNameForFileObject()

ILIAS\File\Sanitation\FilePathSanitizer::saveNewNameForFileObject (   $valid_filename)
private
Parameters
$valid_filename

Definition at line 206 of file FilePathSanitizer.php.

Referenced by ILIAS\File\Sanitation\FilePathSanitizer\sanitizeIfNeeded().

207  {
208  $sanitized_filename = basename($valid_filename);
209  $this->file_object->setFileName($sanitized_filename);
210  $this->file_object->update();
211  }
+ Here is the caller graph for this function:

Field Documentation

◆ $absolute_path

ILIAS\File\Sanitation\FilePathSanitizer::$absolute_path
private

Definition at line 35 of file FilePathSanitizer.php.

◆ $file_object

ILIAS\File\Sanitation\FilePathSanitizer::$file_object
private

◆ $fs

ILIAS\File\Sanitation\FilePathSanitizer::$fs
private

Definition at line 31 of file FilePathSanitizer.php.

◆ $relative_path

ILIAS\File\Sanitation\FilePathSanitizer::$relative_path
private

Definition at line 27 of file FilePathSanitizer.php.

◆ $version

ILIAS\File\Sanitation\FilePathSanitizer::$version = 1
private

Definition at line 39 of file FilePathSanitizer.php.


The documentation for this class was generated from the following file: