1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the TYPO3 CMS project. |
7
|
|
|
* |
8
|
|
|
* It is free software; you can redistribute it and/or modify it under |
9
|
|
|
* the terms of the GNU General Public License, either version 2 |
10
|
|
|
* of the License, or any later version. |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please read the |
13
|
|
|
* LICENSE.txt file that was distributed with this source code. |
14
|
|
|
* |
15
|
|
|
* The TYPO3 project - inspiring people to share! |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace TYPO3\CMS\Core\Resource; |
19
|
|
|
|
20
|
|
|
use LogicException; |
21
|
|
|
use TYPO3\CMS\Core\Core\Environment; |
22
|
|
|
use TYPO3\CMS\Core\Utility\PathUtility; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Model representing an absolute or relative path in the local file system |
26
|
|
|
* @internal |
27
|
|
|
*/ |
28
|
|
|
class LocalPath |
29
|
|
|
{ |
30
|
|
|
public const TYPE_ABSOLUTE = 1; |
31
|
|
|
public const TYPE_RELATIVE = 2; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $raw; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string|null |
40
|
|
|
*/ |
41
|
|
|
protected $relative; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $absolute; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
protected $type; |
52
|
|
|
|
53
|
|
|
public function __construct(string $value, int $type) |
54
|
|
|
{ |
55
|
|
|
if ($type !== self::TYPE_ABSOLUTE && $type !== self::TYPE_RELATIVE) { |
56
|
|
|
throw new LogicException(sprintf('Unexpected type "%d"', $type), 1625826491); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// @todo `../` is erased here, check again if this is a valid scenario |
60
|
|
|
// value and absolute have leading and trailing slash, e.g. '/some/path/' |
61
|
|
|
$value = '/' . trim(PathUtility::getCanonicalPath($value), '/'); |
62
|
|
|
$value .= $value !== '/' ? '/' : ''; |
63
|
|
|
$this->raw = $value; |
64
|
|
|
$this->type = $type; |
65
|
|
|
|
66
|
|
|
$publicPath = Environment::getPublicPath(); |
67
|
|
|
if ($type === self::TYPE_RELATIVE) { |
68
|
|
|
$this->relative = $value; |
69
|
|
|
$this->absolute = PathUtility::getCanonicalPath($publicPath . $value) . '/'; |
70
|
|
|
} elseif ($type === self::TYPE_ABSOLUTE) { |
71
|
|
|
$this->absolute = $value; |
72
|
|
|
$this->relative = strpos($value, $publicPath) === 0 |
73
|
|
|
? substr($value, strlen($publicPath)) |
74
|
|
|
: null; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string normalize path as provided |
80
|
|
|
*/ |
81
|
|
|
public function getRaw(): string |
82
|
|
|
{ |
83
|
|
|
return $this->raw; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string|null (calculated) relative path to public path - `null` if outside public path |
88
|
|
|
*/ |
89
|
|
|
public function getRelative(): ?string |
90
|
|
|
{ |
91
|
|
|
return $this->relative; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string (calculated) absolute path |
96
|
|
|
*/ |
97
|
|
|
public function getAbsolute(): string |
98
|
|
|
{ |
99
|
|
|
return $this->absolute; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function isAbsolute(): bool |
103
|
|
|
{ |
104
|
|
|
return $this->type === self::TYPE_ABSOLUTE; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function isRelative(): bool |
108
|
|
|
{ |
109
|
|
|
return $this->type === self::TYPE_RELATIVE; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|