ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
SuffixDefinitions.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
26 final class SuffixDefinitions
27 {
28  public const SEC = ".sec";
29  protected array $white_list = [];
30 
34  public function __construct(array $white_list, protected array $black_list)
35  {
36  $this->white_list[] = '';
37  $this->white_list = array_unique($white_list);
38  }
39 
43  public function getWhiteList(): array
44  {
45  return $this->white_list;
46  }
47 
51  public function getBlackList(): array
52  {
53  return $this->black_list;
54  }
55 
59  public function getValidFileName(string $filename): string
60  {
61  if ($this->hasValidFileName($filename)) {
62  return $filename;
63  }
64  $pi = pathinfo($filename);
65  // if extension is not in white list, remove all "." and add ".sec" extension
66  $basename = str_replace(".", "", $pi["basename"]);
67  if (trim($basename) == "") {
68  throw new \RuntimeException("Invalid upload filename.");
69  }
70  $basename .= self::SEC;
71  if ($pi["dirname"] != "" && ($pi["dirname"] != "." || substr($filename, 0, 2) == "./")) {
72  return $pi["dirname"] . "/" . $basename;
73  }
74  return $basename;
75  }
76 
80  public function hasValidFileName(string $filename): bool
81  {
82  $pi = pathinfo($filename);
83 
84  return in_array(strtolower($pi["extension"]), $this->white_list)
85  && !in_array(strtolower($pi["extension"]), $this->black_list);
86  }
87 }
__construct(array $white_list, protected array $black_list)
$filename
Definition: buildRTE.php:78