-
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathStorage.php
More file actions
259 lines (214 loc) · 8.25 KB
/
Storage.php
File metadata and controls
259 lines (214 loc) · 8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
declare(strict_types=1);
namespace Saloon\Helpers;
use InvalidArgumentException;
use Saloon\Exceptions\DirectoryNotFoundException;
use Saloon\Exceptions\UnableToCreateFileException;
use Saloon\Exceptions\UnableToCreateDirectoryException;
/**
* @internal
*/
class Storage
{
/**
* The base directory to access the files.
*/
protected string $baseDirectory;
/**
* Constructor
*
* @throws \Saloon\Exceptions\DirectoryNotFoundException
* @throws \Saloon\Exceptions\UnableToCreateDirectoryException
*/
public function __construct(string $baseDirectory, bool $createMissingBaseDirectory = false)
{
if (! is_dir($baseDirectory)) {
$createMissingBaseDirectory ? $this->createDirectory($baseDirectory) : throw new DirectoryNotFoundException($baseDirectory);
}
$this->baseDirectory = $baseDirectory;
}
/**
* Get the base directory
*/
public function getBaseDirectory(): string
{
return $this->baseDirectory;
}
/**
* Combine the base directory with a path.
*/
protected function buildPath(string $path): string
{
$trimRules = DIRECTORY_SEPARATOR . ' ';
return mb_rtrim($this->baseDirectory, $trimRules) . DIRECTORY_SEPARATOR . mb_ltrim($path, $trimRules);
}
/**
* Normalize a path by resolving . and .. segments (no filesystem access).
*/
protected function normalizePath(string $path): string
{
$leadingSlash = $path !== '' && $path[0] === DIRECTORY_SEPARATOR;
$leadingDrive = mb_strlen($path) >= 2 && $path[1] === ':';
$segments = [];
foreach (preg_split('#[/\\\\]+#', $path, -1, PREG_SPLIT_NO_EMPTY) ?: [] as $segment) {
if ($segment === '.') {
continue;
}
if ($segment === '..') {
array_pop($segments);
continue;
}
$segments[] = $segment;
}
$result = implode(DIRECTORY_SEPARATOR, $segments);
if ($leadingSlash && $result !== '') {
$result = DIRECTORY_SEPARATOR . $result;
}
if ($leadingDrive && $result !== '' && ! preg_match('#^[a-zA-Z]:#', $result)) {
$result = $path[0] . ':' . $result;
}
return $result;
}
/**
* Ensure the resolved path is under the base directory to prevent path traversal.
*
* @throws InvalidArgumentException
*/
protected function ensurePathUnderBase(string $fullPath): void
{
$baseReal = realpath($this->baseDirectory);
if ($baseReal === false) {
throw new InvalidArgumentException('Unable to determine the realpath of the base directory.');
}
if (str_contains($fullPath, '~')) {
throw new InvalidArgumentException('Path must remain inside the storage base directory.');
}
$baseTrimmed = mb_rtrim($this->baseDirectory, DIRECTORY_SEPARATOR . ' ');
$baseNorm = $this->normalizePath($baseTrimmed);
$fullNorm = $this->normalizePath($fullPath);
$baseWithSep = $baseNorm . DIRECTORY_SEPARATOR;
if ($baseTrimmed !== '' && $fullNorm !== $baseNorm && ! str_starts_with($fullNorm, $baseWithSep)) {
throw new InvalidArgumentException('Path must remain inside the storage base directory.');
}
$pathSuffix = $baseTrimmed === '' ? $fullPath : ($fullNorm === $baseNorm ? '' : mb_substr($fullNorm, mb_strlen($baseWithSep)));
$normalizedAbsolute = $this->normalizePath($baseReal . DIRECTORY_SEPARATOR . $pathSuffix);
$baseWithSeparator = $baseReal . DIRECTORY_SEPARATOR;
if ($normalizedAbsolute !== $baseReal && ! str_starts_with($normalizedAbsolute, $baseWithSeparator)) {
throw new InvalidArgumentException('Path must remain inside the storage base directory.');
}
}
/**
* Normalize a path by resolving . and .. segments (no filesystem access).
*/
protected function normalizePath(string $path): string
{
$leadingSlash = $path !== '' && $path[0] === DIRECTORY_SEPARATOR;
$leadingDrive = mb_strlen($path) >= 2 && $path[1] === ':';
$segments = [];
foreach (preg_split('#[/\\\\]+#', $path, -1, PREG_SPLIT_NO_EMPTY) ?: [] as $segment) {
if ($segment === '.') {
continue;
}
if ($segment === '..') {
array_pop($segments);
continue;
}
$segments[] = $segment;
}
$result = implode(DIRECTORY_SEPARATOR, $segments);
if ($leadingSlash && $result !== '') {
$result = DIRECTORY_SEPARATOR . $result;
}
if ($leadingDrive && $result !== '' && ! preg_match('#^[a-zA-Z]:#', $result)) {
$result = $path[0] . ':' . $result;
}
return $result;
}
/**
* Ensure the resolved path is under the base directory to prevent path traversal.
*
* @throws InvalidArgumentException
*/
protected function ensurePathUnderBase(string $fullPath): void
{
$baseReal = realpath($this->baseDirectory);
if ($baseReal === false) {
throw new InvalidArgumentException('Unable to determine the realpath of the base directory.');
}
if (str_contains($fullPath, '~')) {
throw new InvalidArgumentException('Path must remain inside the storage base directory.');
}
$baseTrimmed = rtrim($this->baseDirectory, DIRECTORY_SEPARATOR . ' ');
$baseNorm = $this->normalizePath($baseTrimmed);
$fullNorm = $this->normalizePath($fullPath);
$baseWithSep = $baseNorm . DIRECTORY_SEPARATOR;
if ($baseTrimmed !== '' && $fullNorm !== $baseNorm && ! str_starts_with($fullNorm, $baseWithSep)) {
throw new InvalidArgumentException('Path must remain inside the storage base directory.');
}
$pathSuffix = $baseTrimmed === '' ? $fullPath : ($fullNorm === $baseNorm ? '' : mb_substr($fullNorm, mb_strlen($baseWithSep)));
$normalizedAbsolute = $this->normalizePath($baseReal . DIRECTORY_SEPARATOR . $pathSuffix);
$baseWithSeparator = $baseReal . DIRECTORY_SEPARATOR;
if ($normalizedAbsolute !== $baseReal && ! str_starts_with($normalizedAbsolute, $baseWithSeparator)) {
throw new InvalidArgumentException('Path must remain inside the storage base directory.');
}
}
/**
* Check if the file exists
*/
public function exists(string $path): bool
{
$fullPath = $this->buildPath($path);
$this->ensurePathUnderBase($fullPath);
return file_exists($fullPath);
}
/**
* Check if the file is missing
*/
public function missing(string $path): bool
{
return ! $this->exists($path);
}
/**
* Retrieve an item from storage
*/
public function get(string $path): bool|string
{
$fullPath = $this->buildPath($path);
$this->ensurePathUnderBase($fullPath);
return file_get_contents($fullPath);
}
/**
* Put an item in storage
*
* @return $this
* @throws \Saloon\Exceptions\UnableToCreateDirectoryException
* @throws \Saloon\Exceptions\UnableToCreateFileException
*/
public function put(string $path, string $contents): static
{
$fullPath = $this->buildPath($path);
$this->ensurePathUnderBase($fullPath);
$directoryWithoutFilename = implode(DIRECTORY_SEPARATOR, explode(DIRECTORY_SEPARATOR, $fullPath, -1));
if (empty($directoryWithoutFilename) === false && is_dir($directoryWithoutFilename) === false) {
$this->createDirectory($directoryWithoutFilename);
}
$createdFile = file_put_contents($fullPath, $contents);
if ($createdFile === false) {
throw new UnableToCreateFileException($fullPath);
}
return $this;
}
/**
* Create a directory
*
* @throws \Saloon\Exceptions\UnableToCreateDirectoryException
*/
public function createDirectory(string $directory): bool
{
$createdDirectory = mkdir($directory, 0777, true);
if ($createdDirectory === false && is_dir($directory) === false) {
throw new UnableToCreateDirectoryException($directory);
}
return true;
}
}