/
Local.php
447 lines (386 loc) · 12.1 KB
/
Local.php
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\fs;
use Craft;
use craft\base\Fs;
use craft\base\LocalFsInterface;
use craft\errors\FsException;
use craft\errors\FsObjectNotFoundException;
use craft\helpers\App;
use craft\helpers\FileHelper;
use craft\helpers\Path;
use craft\helpers\StringHelper;
use craft\models\FsListing;
use DirectoryIterator;
use FilesystemIterator;
use Generator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use yii\validators\InlineValidator;
/**
* Local represents a local filesystem.
*
* @property-read mixed $settingsHtml
* @property-read string $rootPath
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 4.0.0
*/
class Local extends Fs implements LocalFsInterface
{
public const VISIBILITY_FILE = 'file';
public const VISIBILITY_DIR = 'dir';
/**
* @var int[][] Visibility map
*/
protected array $visibilityMap = [
self::VISIBILITY_FILE => [
self::VISIBILITY_DEFAULT => 0644,
self::VISIBILITY_PUBLIC => 0644,
self::VISIBILITY_HIDDEN => 0600,
],
self::VISIBILITY_DIR => [
self::VISIBILITY_DEFAULT => 0775,
self::VISIBILITY_PUBLIC => 0775,
self::VISIBILITY_HIDDEN => 0700,
],
];
/**
* @inheritdoc
*/
public static function displayName(): string
{
return Craft::t('app', 'Local Folder');
}
/**
* @var string|null Path to the root of this sources local folder.
*/
public ?string $path = null;
/**
* @inheritdoc
*/
public function __construct($config = [])
{
// Config normalization
if (isset($config['path'])) {
$config['path'] = rtrim(str_replace('\\', '/', $config['path']), '/');
if ($config['path'] === '') {
unset($config['path']);
}
}
parent::__construct($config);
}
/**
* @inheritdoc
*/
public function init(): void
{
parent::init();
$generalConfig = Craft::$app->getConfig()->getGeneral();
if ($generalConfig->defaultFileMode) {
$this->visibilityMap[self::VISIBILITY_FILE][self::VISIBILITY_DEFAULT] = $generalConfig->defaultFileMode;
}
if ($generalConfig->defaultFileMode) {
$this->visibilityMap[self::VISIBILITY_DIR][self::VISIBILITY_DEFAULT] = $generalConfig->defaultDirMode;
}
}
/**
* @inheritdoc
*/
public function attributeLabels(): array
{
return array_merge(parent::attributeLabels(), [
'path' => Craft::t('app', 'Base Path'),
]);
}
/**
* @inheritdoc
*/
protected function defineRules(): array
{
$rules = parent::defineRules();
$rules[] = [['path'], 'required'];
$rules[] = [['path'], 'validatePath'];
return $rules;
}
/**
* @param string $attribute
* @param array|null $params
* @param InlineValidator $validator
* @return void
* @since 4.4.6
*/
public function validatePath(string $attribute, ?array $params, InlineValidator $validator): void
{
// Make sure it’s not within any of the system directories
$path = FileHelper::absolutePath($this->getRootPath(), '/');
$systemDirs = Craft::$app->getPath()->getSystemPaths();
foreach ($systemDirs as $dir) {
$dir = FileHelper::absolutePath($dir, '/');
if (str_starts_with("$path/", "$dir/")) {
$validator->addError($this, $attribute, Craft::t('app', 'Local filesystems cannot be located within system directories.'));
break;
}
if (str_starts_with("$dir/", "$path/")) {
$validator->addError($this, $attribute, Craft::t('app', 'Local filesystems cannot be located above system directories.'));
break;
}
}
}
/**
* @inheritdoc
*/
public function getSettingsHtml(): ?string
{
return Craft::$app->getView()->renderTemplate('_components/fs/Local/settings.twig',
[
'volume' => $this,
]);
}
/**
* @inheritdoc
*/
public function afterSave(bool $isNew): void
{
// If the folder doesn't exist yet, create it with a .gitignore file
$path = $this->getRootPath();
if (!is_dir($path)) {
FileHelper::createDirectory($path);
FileHelper::writeGitignoreFile($path);
}
parent::afterSave($isNew);
}
/**
* @inheritdoc
*/
public function getRootPath(): string
{
$path = FileHelper::normalizePath(App::parseEnv($this->path));
// Pass it through realpath() in case the path is symlinked
return realpath($path) ?: $path;
}
/**
* @inheritdoc
*/
public function getFileList(string $directory = '', bool $recursive = true): Generator
{
$targetDir = $this->prefixPath($directory);
try {
$iterator = $recursive ? $this->getRecursiveIterator($targetDir) : new DirectoryIterator($targetDir);
} catch (\UnexpectedValueException $e) {
Craft::$app->getErrorHandler()->logException($e);
return;
}
/** @var DirectoryIterator $listing */
foreach ($iterator as $listing) {
if ($listing->isDir() && $listing->isDot()) {
continue;
}
$filePath = FileHelper::normalizePath(StringHelper::removeLeft($listing->getRealPath(), $this->prefixPath()), '/');
$dirname = pathinfo($filePath, PATHINFO_DIRNAME);
$basename = $listing->getFilename();
yield new FsListing([
'dirname' => $dirname,
'basename' => $basename,
'type' => $listing->isDir() ? 'dir' : 'file',
'dateModified' => filemtime($listing->getRealPath()),
'fileSize' => !$listing->isDir() ? filesize($listing->getRealPath()) : null,
]);
}
}
/**
* @inheritdoc
*/
public function getFileSize(string $uri): int
{
$targetPath = $this->prefixPath($uri);
clearstatcache();
$fileSize = is_file($targetPath) ? filesize($targetPath) : false;
if ($fileSize === false) {
throw new FsException("Unable to get file size for “{$uri}”");
}
return $fileSize;
}
/**
* @inheritdoc
*/
public function getDateModified(string $uri): int
{
$targetPath = $this->prefixPath($uri);
clearstatcache();
$dateModified = filemtime($targetPath);
if ($dateModified === false) {
throw new FsException("Unable to get date modified for “{$uri}”");
}
return $dateModified;
}
/**
* @inheritdoc
*/
public function writeFileFromStream(string $path, $stream, array $config = []): void
{
$this->createDirectory(pathinfo($path, PATHINFO_DIRNAME));
$fullPath = $this->prefixPath($path);
$targetStream = @fopen($fullPath, 'w+b');
if (!$targetStream || !@stream_copy_to_stream($stream, $targetStream)) {
throw new FsException("Unable to copy stream to `$fullPath`");
}
fclose($targetStream);
$visibility = $this->resolveVisibility(self::VISIBILITY_FILE, $config);
if ($visibility) {
@chmod($fullPath, $visibility);
}
}
/**
* @inheritdoc
*/
public function read(string $path): string
{
$stream = $this->getFileStream($path);
$contents = stream_get_contents($stream);
fclose($stream);
return $contents;
}
/**
* @inheritdoc
*/
public function write(string $path, string $contents, array $config = []): void
{
$stream = tmpfile();
fwrite($stream, $contents);
rewind($stream);
$this->writeFileFromStream($path, $stream, $config);
}
/**
* @inheritdoc
*/
public function fileExists(string $path): bool
{
try {
return file_exists($this->prefixPath($path));
} catch (FsException $exception) {
Craft::$app->getErrorHandler()->logException($exception);
}
return false;
}
/**
* @inheritdoc
*/
public function deleteFile(string $path): void
{
if (!$this->fileExists($path)) {
return;
}
if (!unlink($this->prefixPath($path))) {
Craft::warning("Tried to delete `$path`, but could not.");
}
}
/**
* @inheritdoc
*/
public function renameFile(string $path, string $newPath): void
{
$this->createDirectory(pathinfo($newPath, PATHINFO_DIRNAME));
@rename($this->prefixPath($path), $this->prefixPath($newPath));
}
/**
* @inheritdoc
*/
public function copyFile(string $path, string $newPath): void
{
$this->createDirectory(pathinfo($newPath, PATHINFO_DIRNAME));
@copy($this->prefixPath($path), $this->prefixPath($newPath));
}
/**
* @inheritdoc
*/
public function getFileStream(string $uriPath)
{
$path = $this->prefixPath($uriPath);
$file = @fopen($path, 'rb');
if (!$file) {
throw new FsObjectNotFoundException("Unable to open $path.");
}
return $file;
}
/**
* @inheritdoc
*/
public function directoryExists(string $path): bool
{
return is_dir($this->prefixPath($path));
}
/**
* @inheritdoc
*/
public function createDirectory(string $path, array $config = []): void
{
$dirPath = StringHelper::removeRight($this->prefixPath($path), '.');
FileHelper::createDirectory($dirPath, $this->resolveVisibility(self::VISIBILITY_DIR, $config), true);
}
/**
* @inheritdoc
*/
public function deleteDirectory(string $path): void
{
FileHelper::removeDirectory($this->prefixPath($path));
}
/**
* @inheritdoc
*/
public function renameDirectory(string $path, string $newName): void
{
if (!is_dir($this->prefixPath($path))) {
throw new FsObjectNotFoundException('No folder exists at path: ' . $path);
}
$components = explode("/", $this->prefixPath($path));
array_pop($components);
$components[] = $newName;
$newPath = implode("/", $components);
@rename($this->prefixPath($path), $newPath);
}
/**
* Create the recursive iterator for traversing file system.
*
* @param string $targetDir
* @return RecursiveIteratorIterator
*/
protected function getRecursiveIterator(string $targetDir): RecursiveIteratorIterator
{
$directoryMode = FilesystemIterator::FOLLOW_SYMLINKS | FilesystemIterator::SKIP_DOTS | FilesystemIterator::CURRENT_AS_SELF;
$recursiveIteratorMode = RecursiveIteratorIterator::SELF_FIRST;
$recursiveDirectoryIterator = new RecursiveDirectoryIterator($targetDir, $directoryMode);
return new RecursiveIteratorIterator($recursiveDirectoryIterator, $recursiveIteratorMode);
}
/**
* Prefix the path with the root path.
*
* @param string $path
* @return string
* @throws FsException if path is not contained.
*/
protected function prefixPath(string $path = ''): string
{
if (!Path::ensurePathIsContained($path)) {
throw new FsException("The path `$path` is not contained.");
}
return $this->getRootPath() . DIRECTORY_SEPARATOR . $path;
}
/**
* Resolve visibility by a config array and type.
*
* @param string $type
* @param array $config
* @return int
*/
protected function resolveVisibility(string $type, array $config = []): int
{
if (empty($config[self::CONFIG_VISIBILITY])) {
return $this->visibilityMap[$type][self::VISIBILITY_DEFAULT];
}
return $this->visibilityMap[$type][$config[self::CONFIG_VISIBILITY]];
}
}