ILIAS  release_8 Revision v8.23
LegacyPathHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 
26 
37 final class LegacyPathHelper
38 {
39  use FilesystemsAware;
40 
41  public static function deriveLocationFrom(string $absolute_path): int
42  {
43  [
44  $web,
45  $webRelativeWithLeadingDot,
46  $webRelativeWithoutLeadingDot,
47  $storage,
48  $customizing,
49  $customizingRelativeWithLeadingDot,
50  $libs,
51  $libsRelativeWithLeadingDot,
52  $temp,
53  $nodeModules,
54  $nodeModulesWithLeadingDot
55  ] = self::listPaths();
56 
57  switch (true) {
58  case self::checkPossiblePath($temp, $absolute_path):
59  return Location::TEMPORARY;
60  case self::checkPossiblePath($web, $absolute_path):
61  case self::checkPossiblePath($webRelativeWithLeadingDot, $absolute_path):
62  case self::checkPossiblePath($webRelativeWithoutLeadingDot, $absolute_path):
63  return Location::WEB;
64  case self::checkPossiblePath($storage, $absolute_path):
65  return Location::STORAGE;
66  case self::checkPossiblePath($customizing, $absolute_path):
67  case self::checkPossiblePath($customizingRelativeWithLeadingDot, $absolute_path):
68  return Location::CUSTOMIZING;
69  default:
70  throw new \InvalidArgumentException("Invalid path supplied. Path must start with the web, storage, temp, customizing or libs storage location. Path given: '{$absolute_path}'");
71  }
72  }
73 
87  public static function deriveFilesystemFrom(string $absolute_path): Filesystem
88  {
89  [
90  $web,
91  $webRelativeWithLeadingDot,
92  $webRelativeWithoutLeadingDot,
93  $storage,
94  $customizing,
95  $customizingRelativeWithLeadingDot,
96  $libs,
97  $libsRelativeWithLeadingDot,
98  $temp,
99  $nodeModules,
100  $nodeModulesWithLeadingDot
101  ] = self::listPaths();
102 
103  switch (true) {
104  case self::checkPossiblePath($temp, $absolute_path):
105  return self::filesystems()->temp();
106  case self::checkPossiblePath($web, $absolute_path):
107  return self::filesystems()->web();
108  case self::checkPossiblePath($webRelativeWithLeadingDot, $absolute_path):
109  return self::filesystems()->web();
110  case self::checkPossiblePath($webRelativeWithoutLeadingDot, $absolute_path):
111  return self::filesystems()->web();
112  case self::checkPossiblePath($storage, $absolute_path):
113  return self::filesystems()->storage();
114  case self::checkPossiblePath($customizing, $absolute_path):
115  return self::filesystems()->customizing();
116  case self::checkPossiblePath($customizingRelativeWithLeadingDot, $absolute_path):
117  return self::filesystems()->customizing();
118  case self::checkPossiblePath($libs, $absolute_path):
119  return self::filesystems()->libs();
120  case self::checkPossiblePath($libsRelativeWithLeadingDot, $absolute_path):
121  return self::filesystems()->libs();
122  case self::checkPossiblePath($nodeModules, $absolute_path):
123  return self::filesystems()->nodeModules();
124  case self::checkPossiblePath($nodeModulesWithLeadingDot, $absolute_path):
125  return self::filesystems()->nodeModules();
126  default:
127  throw new \InvalidArgumentException("Invalid path supplied. Path must start with the web, storage, temp, customizing or libs storage location. Path given: '{$absolute_path}'");
128  }
129  }
130 
131 
144  public static function createRelativePath(string $absolute_path): string
145  {
146  [
147  $web,
148  $webRelativeWithLeadingDot,
149  $webRelativeWithoutLeadingDot,
150  $storage,
151  $customizing,
152  $customizingRelativeWithLeadingDot,
153  $libs,
154  $libsRelativeWithLeadingDot,
155  $temp,
156  $nodeModules,
157  $nodeModulesWithLeadingDot
158  ] = self::listPaths();
159 
160  switch (true) {
161  // web without ./
162  case self::checkPossiblePath($webRelativeWithoutLeadingDot, $absolute_path):
163  return self::resolveRelativePath($webRelativeWithoutLeadingDot, $absolute_path);
164  // web with ./
165  case self::checkPossiblePath($webRelativeWithLeadingDot, $absolute_path):
166  return self::resolveRelativePath($webRelativeWithLeadingDot, $absolute_path);
167  // web/
168  case self::checkPossiblePath($web, $absolute_path):
169  return self::resolveRelativePath($web, $absolute_path);
170  // temp/
171  case self::checkPossiblePath($temp, $absolute_path):
172  return self::resolveRelativePath($temp, $absolute_path);
173  // iliasdata/
174  case self::checkPossiblePath($storage, $absolute_path):
175  return self::resolveRelativePath($storage, $absolute_path);
176  // Customizing/
177  case self::checkPossiblePath($customizing, $absolute_path):
178  return self::resolveRelativePath($customizing, $absolute_path);
179  // ./Customizing/
180  case self::checkPossiblePath($customizingRelativeWithLeadingDot, $absolute_path):
181  return self::resolveRelativePath($customizingRelativeWithLeadingDot, $absolute_path);
182  // libs/
183  case self::checkPossiblePath($libs, $absolute_path):
184  // ./libs
185  case self::checkPossiblePath($libsRelativeWithLeadingDot, $absolute_path):
186  return self::resolveRelativePath($libsRelativeWithLeadingDot, $absolute_path);
187  // node_modules/
188  case self::checkPossiblePath($nodeModules, $absolute_path):
189  // ./node_modules
190  case self::checkPossiblePath($nodeModulesWithLeadingDot, $absolute_path):
191  return self::resolveRelativePath($nodeModulesWithLeadingDot, $absolute_path);
192  default:
193  throw new \InvalidArgumentException("Invalid path supplied. Path must start with the web, storage, temp, customizing or libs storage location. Path given: '{$absolute_path}'");
194  }
195  }
196 
197 
198  private static function resolveRelativePath(string $possible_path, string $absolute_path): string
199  {
200  $real_possible_path = realpath($possible_path);
201 
202  switch (true) {
203  case $possible_path === $absolute_path:
204  case $real_possible_path === $absolute_path:
205  return "";
206  case strpos($absolute_path, $possible_path) === 0:
207  return substr(
208  $absolute_path,
209  strlen($possible_path) + 1
210  ); //also remove the trailing slash
211  case strpos($absolute_path, $real_possible_path) === 0:
212  return substr(
213  $absolute_path,
214  strlen($real_possible_path) + 1
215  ); //also remove the trailing slash
216  default:
217  throw new \InvalidArgumentException("Invalid path supplied. Path must start with the web, storage, temp, customizing or libs storage location. Path given: '{$absolute_path}'");
218  }
219  }
220 
221 
228  private static function checkPossiblePath(string $possible_path, string $absolute_path): bool
229  {
230  $real_possible_path = realpath($possible_path);
231 
232  switch (true) {
233  case $possible_path === $absolute_path:
234  return true;
235  case $real_possible_path === $absolute_path:
236  return true;
237  case is_string($possible_path) && strpos($absolute_path, $possible_path) === 0:
238  return true;
239  case is_string($real_possible_path) && strpos($absolute_path, $real_possible_path) === 0:
240  return true;
241  default:
242  return false;
243  }
244  }
245 
246 
250  private static function listPaths(): array
251  {
252  $web = CLIENT_WEB_DIR;
253  $webRelativeWithLeadingDot = './' . ILIAS_WEB_DIR . '/' . CLIENT_ID;
254  $webRelativeWithoutLeadingDot = ILIAS_WEB_DIR . '/' . CLIENT_ID;
255  $storage = CLIENT_DATA_DIR;
256  $customizing = ILIAS_ABSOLUTE_PATH . '/Customizing';
257  $customizingRelativeWithLeadingDot = './Customizing';
258  $libs = ILIAS_ABSOLUTE_PATH . '/libs';
259  $libsRelativeWithLeadingDot = "./libs";
260  $temp = CLIENT_DATA_DIR . "/temp";
261  $nodeModules = ILIAS_ABSOLUTE_PATH . '/node_modules';
262  $nodeModulesWithLeadingDot = './node_modules';
263 
264  return array($web,
265  $webRelativeWithLeadingDot,
266  $webRelativeWithoutLeadingDot,
267  $storage,
268  $customizing,
269  $customizingRelativeWithLeadingDot,
270  $libs,
271  $libsRelativeWithLeadingDot,
272  $temp,
273  $nodeModules,
274  $nodeModulesWithLeadingDot
275  );
276  }
277 }
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)
const CLIENT_DATA_DIR
Definition: constants.php:46
static checkPossiblePath(string $possible_path, string $absolute_path)
static filesystems()
Returns the loaded filesystems.
const CLIENT_ID
Definition: constants.php:41
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static deriveLocationFrom(string $absolute_path)
const CLIENT_WEB_DIR
Definition: constants.php:47
static deriveFilesystemFrom(string $absolute_path)
Tries to fetch the filesystem responsible for the absolute path.
Class FlySystemFileAccessTest disabled disabled disabled.
const ILIAS_WEB_DIR
Definition: constants.php:45