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