1 <?php
2
3 /**
4 * Utility class to generate absolute paths.
5 *
6 * @author gharlan
7 *
8 * @package redaxo\core
9 */
10 class rex_path
11 {
12 protected static $pathprovider;
13
14 /**
15 * Initializes the class.
16 *
17 * @param mixed $pathprovider A path provider
18 */
19 public static function init($pathprovider)
20 {
21 self::$pathprovider = $pathprovider;
22 }
23
24 /**
25 * Returns the base/root path.
26 *
27 * @param string $file File
28 *
29 * @return string
30 */
31 public static function base($file = '')
32 {
33 return self::$pathprovider->base($file);
34 }
35
36 /**
37 * Returns the path to the frontend (the document root).
38 *
39 * @param string $file File
40 *
41 * @return string
42 */
43 public static function frontend($file = '')
44 {
45 return self::$pathprovider->frontend($file);
46 }
47
48 /**
49 * Returns the path to the frontend-controller (index.php from frontend).
50 *
51 * @return string
52 */
53 public static function frontendController()
54 {
55 return self::$pathprovider->frontendController();
56 }
57
58 /**
59 * Returns the path to the backend (folder where the backend controller is placed).
60 *
61 * @param string $file File
62 *
63 * @return string
64 */
65 public static function backend($file = '')
66 {
67 return self::$pathprovider->backend($file);
68 }
69
70 /**
71 * Returns the path to the backend-controller (index.php from backend).
72 *
73 * @return string
74 */
75 public static function backendController()
76 {
77 return self::$pathprovider->backendController();
78 }
79
80 /**
81 * Returns the path to the media-folder.
82 *
83 * @param string $file File
84 *
85 * @return string
86 */
87 public static function media($file = '')
88 {
89 return self::$pathprovider->media($file);
90 }
91
92 /**
93 * Returns the path to the assets folder.
94 *
95 * @param string $file File
96 *
97 * @return string
98 */
99 public static function assets($file = '')
100 {
101 return self::$pathprovider->assets($file);
102 }
103
104 /**
105 * Returns the path to the assets folder of the core, which contains all assets required by the core to work properly.
106 *
107 * @param string $file File
108 *
109 * @return string
110 */
111 public static function coreAssets($file = '')
112 {
113 return self::$pathprovider->coreAssets($file);
114 }
115
116 /**
117 * Returns the path to the public assets folder of the given addon.
118 *
119 * @param string $addon Addon
120 * @param string $file File
121 *
122 * @return string
123 *
124 * @see assets()
125 */
126 public static function addonAssets($addon, $file = '')
127 {
128 return self::$pathprovider->addonAssets($addon, $file);
129 }
130
131 /**
132 * Returns the path to the public assets folder of the given plugin of the given addon.
133 *
134 * @param string $addon Addon
135 * @param string $plugin Plugin
136 * @param string $file File
137 *
138 * @return string
139 *
140 * @see assets()
141 */
142 public static function pluginAssets($addon, $plugin, $file = '')
143 {
144 return self::$pathprovider->pluginAssets($addon, $plugin, $file);
145 }
146
147 /**
148 * Returns the path to the bin folder.
149 *
150 * @param string $file File
151 *
152 * @return string
153 */
154 public static function bin($file = '')
155 {
156 return self::$pathprovider->bin($file);
157 }
158
159 /**
160 * Returns the path to the data folder.
161 *
162 * @param string $file File
163 *
164 * @return string
165 */
166 public static function data($file = '')
167 {
168 return self::$pathprovider->data($file);
169 }
170
171 /**
172 * Returns the path to the data folder of the core.
173 *
174 * @param string $file File
175 *
176 * @return string
177 */
178 public static function coreData($file = '')
179 {
180 return self::$pathprovider->coreData($file);
181 }
182
183 /**
184 * Returns the path to the data folder of the given addon.
185 *
186 * @param string $addon Addon
187 * @param string $file File
188 *
189 * @return string
190 */
191 public static function addonData($addon, $file = '')
192 {
193 return self::$pathprovider->addonData($addon, $file);
194 }
195
196 /**
197 * Returns the path to the data folder of the given plugin of the given addon.
198 *
199 * @param string $addon Addon
200 * @param string $plugin Plugin
201 * @param string $file File
202 *
203 * @return string
204 */
205 public static function pluginData($addon, $plugin, $file = '')
206 {
207 return self::$pathprovider->pluginData($addon, $plugin, $file);
208 }
209
210 /**
211 * Returns the path to the cache folder.
212 *
213 * @param string $file File
214 *
215 * @return string
216 */
217 public static function cache($file = '')
218 {
219 return self::$pathprovider->cache($file);
220 }
221
222 /**
223 * Returns the path to the cache folder of the core.
224 *
225 * @param string $file File
226 *
227 * @return string
228 */
229 public static function coreCache($file = '')
230 {
231 return self::$pathprovider->coreCache($file);
232 }
233
234 /**
235 * Returns the path to the cache folder of the given addon.
236 *
237 * @param string $addon Addon
238 * @param string $file File
239 *
240 * @return string
241 */
242 public static function addonCache($addon, $file = '')
243 {
244 return self::$pathprovider->addonCache($addon, $file);
245 }
246
247 /**
248 * Returns the path to the cache folder of the given plugin.
249 *
250 * @param string $addon Addon
251 * @param string $plugin Plugin
252 * @param string $file File
253 *
254 * @return string
255 */
256 public static function pluginCache($addon, $plugin, $file = '')
257 {
258 return self::$pathprovider->pluginCache($addon, $plugin, $file);
259 }
260
261 /**
262 * Returns the path to the src folder.
263 *
264 * @param string $file File
265 *
266 * @return string
267 */
268 public static function src($file = '')
269 {
270 return self::$pathprovider->src($file);
271 }
272
273 /**
274 * Returns the path to the actual core.
275 *
276 * @param string $file File
277 *
278 * @return string
279 */
280 public static function core($file = '')
281 {
282 return self::$pathprovider->core($file);
283 }
284
285 /**
286 * Returns the base path to the folder of the given addon.
287 *
288 * @param string $addon Addon
289 * @param string $file File
290 *
291 * @return string
292 */
293 public static function addon($addon, $file = '')
294 {
295 return self::$pathprovider->addon($addon, $file);
296 }
297
298 /**
299 * Returns the base path to the folder of the plugin of the given addon.
300 *
301 * @param string $addon Addon
302 * @param string $plugin Plugin
303 * @param string $file File
304 *
305 * @return string
306 */
307 public static function plugin($addon, $plugin, $file = '')
308 {
309 return self::$pathprovider->plugin($addon, $plugin, $file);
310 }
311
312 /**
313 * Converts a relative path to an absolute.
314 *
315 * @param string $relPath The relative path
316 *
317 * @return string Absolute path
318 */
319 public static function absolute($relPath)
320 {
321 $stack = [];
322
323 // pfadtrenner vereinheitlichen
324 $relPath = str_replace('\\', '/', $relPath);
325 foreach (explode('/', $relPath) as $dir) {
326 // Aktuelles Verzeichnis, oder Ordner ohne Namen
327 if ($dir == '.' || $dir == '') {
328 continue;
329 }
330
331 // Zum Parent
332 if ($dir == '..') {
333 array_pop($stack);
334 }
335 // Normaler Ordner
336 else {
337 array_push($stack, $dir);
338 }
339 }
340
341 return implode(DIRECTORY_SEPARATOR, $stack);
342 }
343
344 /**
345 * Converts an absolute path to a relative one.
346 *
347 * If the path is outside of the base path, the absolute path will be kept.
348 *
349 * @param string $absPath
350 * @param null|string $basePath Defaults to `rex_path::base()`
351 *
352 * @return string
353 */
354 public static function relative($absPath, $basePath = null)
355 {
356 if (null === $basePath) {
357 $basePath = self::base();
358 }
359
360 $basePath = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $basePath);
361 $basePath = rtrim($basePath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
362
363 $baseLength = strlen($basePath);
364
365 $absPath = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $absPath);
366
367 if (substr($absPath, 0, $baseLength) !== $basePath) {
368 return $absPath;
369 }
370
371 return substr($absPath, $baseLength);
372 }
373
374 /**
375 * Returns the basename (filename) of the path independent of directory separator (/ or \).
376 *
377 * This method should be used to secure incoming GET/POST parameters containing a filename.
378 *
379 * @param string $path
380 *
381 * @return string
382 */
383 public static function basename($path)
384 {
385 $path = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path);
386
387 return basename($path);
388 }
389 }
390