ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
LegacyPathHelper.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types=1);
3 
5 
8 
19 final class LegacyPathHelper
20 {
21  use FilesystemsAware;
22 
23 
37  public static function deriveFilesystemFrom(string $absolute_path) : Filesystem
38  {
39  list(
40  $web, $webRelativeWithLeadingDot, $webRelativeWithoutLeadingDot, $storage, $customizing, $customizingRelativeWithLeadingDot, $libs, $libsRelativeWithLeadingDot, $temp
41  )
42  = self::listPaths();
43 
44  switch (true) {
45  case self::checkPossiblePath($temp, $absolute_path):
46  return self::filesystems()->temp();
47  case self::checkPossiblePath($web, $absolute_path):
48  return self::filesystems()->web();
49  case self::checkPossiblePath($webRelativeWithLeadingDot, $absolute_path):
50  return self::filesystems()->web();
51  case self::checkPossiblePath($webRelativeWithoutLeadingDot, $absolute_path):
52  return self::filesystems()->web();
53  case self::checkPossiblePath($storage, $absolute_path):
54  return self::filesystems()->storage();
55  case self::checkPossiblePath($customizing, $absolute_path):
56  return self::filesystems()->customizing();
57  case self::checkPossiblePath($customizingRelativeWithLeadingDot, $absolute_path):
58  return self::filesystems()->customizing();
59  case self::checkPossiblePath($libs, $absolute_path):
60  return self::filesystems()->libs();
61  case self::checkPossiblePath($libsRelativeWithLeadingDot, $absolute_path):
62  return self::filesystems()->libs();
63  default:
64  throw new \InvalidArgumentException("Invalid path supplied. Path must start with the web, storage, temp, customizing or libs storage location. Path given: '{$absolute_path}'");
65  }
66  }
67 
68 
81  public static function createRelativePath(string $absolute_path) : string
82  {
83  list(
84  $web, $webRelativeWithLeadingDot, $webRelativeWithoutLeadingDot, $storage, $customizing, $customizingRelativeWithLeadingDot, $libs, $libsRelativeWithLeadingDot, $temp
85  )
86  = self::listPaths();
87 
88  switch (true) {
89  // web without ./
90  case self::checkPossiblePath($webRelativeWithoutLeadingDot, $absolute_path):
91  return self::resolveRelativePath($webRelativeWithoutLeadingDot, $absolute_path);
92  // web with ./
93  case self::checkPossiblePath($webRelativeWithLeadingDot, $absolute_path):
94  return self::resolveRelativePath($webRelativeWithLeadingDot, $absolute_path);
95  // web/
96  case self::checkPossiblePath($web, $absolute_path):
97  return self::resolveRelativePath($web, $absolute_path);
98  // temp/
99  case self::checkPossiblePath($temp, $absolute_path):
100  return self::resolveRelativePath($temp, $absolute_path);
101  // iliasdata/
102  case self::checkPossiblePath($storage, $absolute_path):
103  return self::resolveRelativePath($storage, $absolute_path);
104  // Customizing/
105  case self::checkPossiblePath($customizing, $absolute_path):
106  return self::resolveRelativePath($customizing, $absolute_path);
107  // ./Customizing/
108  case self::checkPossiblePath($customizingRelativeWithLeadingDot, $absolute_path):
109  return self::resolveRelativePath($customizingRelativeWithLeadingDot, $absolute_path);
110  // libs/
111  case self::checkPossiblePath($libs, $absolute_path):
112  // ./libs
113  case self::checkPossiblePath($libsRelativeWithLeadingDot, $absolute_path):
114  return self::resolveRelativePath($libsRelativeWithLeadingDot, $absolute_path);
115  default:
116  throw new \InvalidArgumentException("Invalid path supplied. Path must start with the web, storage, temp, customizing or libs storage location. Path given: '{$absolute_path}'");
117  }
118  }
119 
120 
121  private static function resolveRelativePath(string $possible_path, string $absolute_path) : string
122  {
123  $real_possible_path = realpath($possible_path);
124 
125  switch (true) {
126  case $possible_path === $absolute_path:
127  case $real_possible_path === $absolute_path:
128  return "";
129  case strpos($absolute_path, $possible_path) === 0:
130  return substr($absolute_path, strlen($possible_path) + 1); //also remove the trailing slash
131  case strpos($absolute_path, $real_possible_path) === 0:
132  return substr($absolute_path, strlen($real_possible_path) + 1); //also remove the trailing slash
133  default:
134  throw new \InvalidArgumentException("Invalid path supplied. Path must start with the web, storage, temp, customizing or libs storage location. Path given: '{$absolute_path}'");
135  }
136  }
137 
138 
145  private static function checkPossiblePath(string $possible_path, string $absolute_path) : bool
146  {
147  $real_possible_path = realpath($possible_path);
148 
149  switch (true) {
150  case $possible_path === $absolute_path:
151  return true;
152  case $real_possible_path === $absolute_path:
153  return true;
154  case strpos($absolute_path, $possible_path) === 0:
155  return true;
156  case strpos($absolute_path, $real_possible_path) === 0:
157  return true;
158  default:
159  return false;
160  }
161  }
162 
163 
167  private static function listPaths() : array
168  {
169  $web = CLIENT_WEB_DIR;
170  $webRelativeWithLeadingDot = './' . ILIAS_WEB_DIR . '/' . CLIENT_ID;
171  $webRelativeWithoutLeadingDot = ILIAS_WEB_DIR . '/' . CLIENT_ID;
172  $storage = CLIENT_DATA_DIR;
173  $customizing = ILIAS_ABSOLUTE_PATH . '/Customizing';
174  $customizingRelativeWithLeadingDot = './Customizing';
175  $libs = ILIAS_ABSOLUTE_PATH . '/libs';
176  $libsRelativeWithLeadingDot = "./libs";
177  $temp = CLIENT_DATA_DIR . "/temp";
178 
179  return array($web, $webRelativeWithLeadingDot, $webRelativeWithoutLeadingDot, $storage, $customizing, $customizingRelativeWithLeadingDot, $libs, $libsRelativeWithLeadingDot, $temp);
180  }
181 }
static createRelativePath(string $absolute_path)
Creates a relative path from an absolute path which starts with a valid storage location.
trait FilesystemsAware
Trait FilesystemsAware.
static resolveRelativePath(string $possible_path, string $absolute_path)
static checkPossiblePath(string $possible_path, string $absolute_path)
static filesystems()
Returns the loaded filesystems.
static deriveFilesystemFrom(string $absolute_path)
Tries to fetch the filesystem responsible for the absolute path.
Class FlySystemFileAccessTest.