-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathDriver.php
849 lines (741 loc) · 22.2 KB
/
Driver.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
<?php
namespace Barryvdh\elFinderFlysystemDriver;
use elFinderVolumeDriver;
use Intervention\Image\Encoders\AutoEncoder;
use Intervention\Image\ImageManager;
use League\Flysystem\Cached\CachedAdapter;
use League\Flysystem\Cached\CacheInterface;
use League\Flysystem\DirectoryAttributes;
use League\Flysystem\FileAttributes;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemOperator;
use League\Flysystem\StorageAttributes;
use League\Flysystem\UnableToCopyFile;
use League\Flysystem\UnableToCreateDirectory;
use League\Flysystem\UnableToDeleteDirectory;
use League\Flysystem\UnableToDeleteFile;
use League\Flysystem\UnableToMoveFile;
use League\Flysystem\UnableToWriteFile;
use League\Flysystem\Util;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\WhitespacePathNormalizer;
use League\Glide\Urls\UrlBuilderFactory;
/**
* elFinder driver for Flysytem (https://github.com/thephpleague/flysystem)
*
* @author Barry vd. Heuvel
* */
class Driver extends elFinderVolumeDriver
{
/**
* Driver id
* Must be started from letter and contains [a-z0-9]
* Used as part of volume id
*
* @var string
**/
protected $driverId = 'fls';
/** @var FilesystemOperator $fs */
protected $fs;
/** @var UrlBuilder $urlBuilder */
protected $urlBuilder = null;
/** @var ImageManager $imageManager */
protected $imageManager = null;
/** @var StorageAttributes $attributes */
protected $attributeCache = [];
/**
* Constructor
* Extend options with required fields
*
**/
public function __construct()
{
$opts = array(
'filesystem' => null,
'URLCallback' => null,
'glideURL' => null,
'glideKey' => null,
'imageManager' => null,
'cache' => 'session', // 'session', 'memory' or false
'checkSubfolders' => false, // Disable for performance
);
$this->options = array_merge($this->options, $opts);
}
protected function clearcache()
{
parent::clearcache();
// clear cached attributes
$this->attributeCache = [];
}
public function mount(array $opts)
{
// If path is not set, use the root
if (!isset($opts['path']) || $opts['path'] === '') {
$opts['path'] = '/';
}
return parent::mount($opts);
}
/**
* Return the icon
*
* @return string
*/
protected function getIcon()
{
$icon = 'volume_icon_ftp.png';
$parentUrl = defined('ELFINDER_IMG_PARENT_URL') ? (rtrim(ELFINDER_IMG_PARENT_URL, '/') . '/') : '';
return $parentUrl . 'img/' . $icon;
}
/**
* Prepare driver before mount volume.
* Return true if volume is ready.
*
* @return bool
**/
protected function init()
{
$this->fs = $this->options['filesystem'];
if (!($this->fs instanceof FilesystemOperator)) {
return $this->setError('A FilesystemOperator instance is required');
}
$this->options['icon'] = $this->options['icon'] ?: (empty($this->options['rootCssClass'])? $this->getIcon() : '');
$this->root = $this->options['path'];
if ($this->options['glideURL']) {
$this->urlBuilder = UrlBuilderFactory::create($this->options['glideURL'], $this->options['glideKey']);
}
if ($this->options['imageManager']) {
$this->imageManager = $this->options['imageManager'];
} else {
$this->imageManager = ImageManager::gd();
}
// enable command archive
$this->options['useRemoteArchive'] = true;
return true;
}
/**
* Return parent directory path
*
* @param string $path file path
* @return string
**/
protected function _dirname($path)
{
$dirname = dirname($path);
return $dirname === '.' ? '/' : $dirname;
}
/**
* Return normalized path
*
* @param string $path path
* @return string
**/
protected function _normpath($path)
{
return $path;
}
/**
* Check if the directory exists in the parent directory. Needed because not all drives handle directories correctly.
*
* @param string $path path
* @return boolean
**/
protected function _dirExists($path)
{
$dir = $this->_dirname($path);
$basename = basename($path);
/** @var StorageAttributes $meta */
foreach ($this->listContents($dir) as $attribute) {
if ($attribute->isDir() && $this->_basename($attribute->path()) == $basename) {
return true;
}
}
return false;
}
/**
* Return stat for given path.
* Stat contains following fields:
* - (int) size file size in b. required
* - (int) ts file modification time in unix time. required
* - (string) mime mimetype. required for folders, others - optionally
* - (bool) read read permissions. required
* - (bool) write write permissions. required
* - (bool) locked is object locked. optionally
* - (bool) hidden is object hidden. optionally
* - (string) alias for symlinks - link target path relative to root path. optionally
* - (string) target for symlinks - link target path. optionally
*
* If file does not exists - returns empty array or false.
*
* @param string $path file path
* @return array|false
**/
protected function _stat($path)
{
$stat = array(
'size' => 0,
'ts' => time(),
'read' => true,
'write' => true,
'locked' => false,
'hidden' => false,
'mime' => 'directory',
);
// If root, just return from above
if ($this->root == $path) {
$stat['name'] = $this->root;
return $stat;
}
if (isset($this->attributeCache[$path])) {
/** @var StorageAttributes $attributes */
$attributes = $this->attributeCache[$path];
$meta = [
'mimetype' => $attributes->type(),
'extension' => null,
'size' => null,
'timestamp' => $attributes->lastModified(),
'type' => $attributes->isFile() ? 'file' : 'dir',
];
if ($attributes instanceof FileAttributes) {
$meta['mimetype'] = $attributes->mimeType();
$meta['size'] = $attributes->fileSize();
}
} else {
// If not exists, return empty
if (!$this->fs->has($path)) {
// Check if the parent doesn't have this path
if ($this->_dirExists($path)) {
return $stat;
}
// Neither a file or directory exist, return empty
return array();
}
try {
$meta = [
'mimetype' => null,
'extension' => null,
'size' => null,
'type' => $this->fs->fileExists($path) ? 'file' : 'dir',
];
if ($meta['type'] === 'file') {
$meta['mimetype'] = $this->fs->mimeType($path);
$meta['timestamp'] = $this->fs->lastModified($path);
$meta['size'] = $this->fs->fileSize($path);
}
} catch (\Exception $e) {
return array();
}
}
if(false === $meta) {
return $stat;
}
// Set item filename.extension to `name` if exists
if (isset($meta['filename']) && isset($meta['extension'])) {
$stat['name'] = $meta['filename'];
if ($meta['extension'] !== '') {
$stat['name'] .= '.' . $meta['extension'];
}
}
// Get timestamp/size if available
if (isset($meta['timestamp'])) {
$stat['ts'] = $meta['timestamp'];
}
if (isset($meta['size'])) {
$stat['size'] = $meta['size'];
}
// Check if file, if so, check mimetype when available
if ($meta['type'] == 'file') {
if(isset($meta['mimetype'])) {
$stat['mime'] = $meta['mimetype'];
} else {
$stat['mime'] = null;
}
$imgMimes = ['image/jpeg', 'image/png', 'image/gif'];
if ($this->urlBuilder && in_array($stat['mime'], $imgMimes)) {
$stat['url'] = $this->urlBuilder->getUrl($path, [
'ts' => $stat['ts']
]);
$stat['tmb'] = $this->urlBuilder->getUrl($path, [
'ts' => $stat['ts'],
'w' => $this->tmbSize,
'h' => $this->tmbSize,
'fit' => $this->options['tmbCrop'] ? 'crop' : 'contain',
]);
}
}
if ($this->options['URLCallback'] && is_callable($this->options['URLCallback'])) {
$stat['url'] = $this->options['URLCallback']($path);
}
return $stat;
}
/**
* @param $path
* @return array|StorageAttributes[]
* @throws \League\Flysystem\FilesystemException
*/
protected function listContents($path): array
{
$contents = $this->fs->listContents($path)->toArray();
/** @var StorageAttributes $item */
foreach ($contents as $item) {
$this->attributeCache[$item['path']] = $item;
}
return $contents;
}
/***************** file stat ********************/
/**
* Return true if path is dir and has at least one childs directory
*
* @param string $path dir path
* @return bool
**/
protected function _subdirs($path)
{
$contents = array_filter($this->listContents($path), function (StorageAttributes $item) {
return $item->isDir();
});
return !empty($contents);
}
/**
* Return object width and height
* Usually used for images, but can be realize for video etc...
*
* @param string $path file path
* @param string $mime file mime type
* @return string
**/
protected function _dimensions($path, $mime)
{
$ret = false;
if ($imgsize = $this->getImageSize($path, $mime)) {
$ret = $imgsize['dimensions'];
}
return $ret;
}
/******************** file/dir content *********************/
/**
* Return files list in directory
*
* @param string $path dir path
* @return array
**/
protected function _scandir($path)
{
$paths = array();
foreach ($this->listContents($path, false) as $object) {
if ($object) {
$paths[] = $object['path'];
}
}
return $paths;
}
/**
* Open file and return file pointer
*
* @param string $path file path
* @param string $mode
* @return resource|false
**/
protected function _fopen($path, $mode = "rb")
{
return $this->fs->readStream($path);
}
/**
* Close opened file
*
* @param resource $fp file pointer
* @param string $path file path
* @return bool
**/
protected function _fclose($fp, $path = '')
{
return @fclose($fp);
}
/******************** file/dir manipulations *************************/
/**
* Create dir and return created dir path or false on failed
*
* @param string $path parent dir path
* @param string $name new directory name
* @return string|bool
**/
protected function _mkdir($path, $name)
{
$path = $this->_joinPath($path, $name);
try {
$this->fs->createDirectory($path);
} catch (UnableToCreateDirectory $e) {
return false;
}
return $path;
}
/**
* Create file and return it's path or false on failed
*
* @param string $path parent dir path
* @param string $name new file name
* @return string|bool
**/
protected function _mkfile($path, $name)
{
$path = $this->_joinPath($path, $name);
try {
$this->fs->write($path, '');
} catch (UnableToWriteFile $e) {
return false;
}
return $path;
}
/**
* Copy file into another file
*
* @param string $source source file path
* @param string $target target directory path
* @param string $name new file name
* @return string|bool
**/
protected function _copy($source, $target, $name)
{
$path = $this->_joinPath($target, $name);
try {
$this->fs->copy($source, $path);
} catch (UnableToCopyFile $e) {
return false;
}
return $path;
}
/**
* Move file into another parent dir.
* Return new file path or false.
*
* @param string $source source file path
* @param string $target target dir path
* @param string $name file name
* @return string|bool
**/
protected function _move($source, $target, $name)
{
$path = $this->_joinPath($target, $name);
try {
$this->fs->move($source, $path);
} catch (UnableToMoveFile $e) {
return false;
}
return $path;
}
/**
* Remove file
*
* @param string $path file path
* @return bool
**/
protected function _unlink($path)
{
try {
$this->fs->delete($path);
} catch (UnableToDeleteFile $e) {
return false;
}
return true;
}
/**
* Remove dir
*
* @param string $path dir path
* @return bool
**/
protected function _rmdir($path)
{
try {
$this->fs->deleteDirectory($path);
} catch (UnableToDeleteDirectory $e) {
return false;
}
return true;
}
/**
* Create new file and write into it from file pointer.
* Return new file path or false on error.
*
* @param resource $fp file pointer
* @param string $dir target dir path
* @param string $name file name
* @param array $stat file stat (required by some virtual fs)
* @return bool|string
**/
protected function _save($fp, $dir, $name, $stat)
{
$path = $this->_joinPath($dir, $name);
$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
$config = [];
if (isset(self::$mimetypes[$ext])) {
$config['mimetype'] = self::$mimetypes[$ext];
}
if (isset($this->options['visibility'])) {
$config['visibility'] = $this->options['visibility'];
}
try {
$this->fs->writeStream($path, $fp, $config);
} catch (UnableToWriteFile $e) {
return false;
}
return $path;
}
/**
* Get file contents
*
* @param string $path file path
* @return string|false
**/
protected function _getContents($path)
{
return $this->fs->read($path);
}
/**
* Write a string to a file
*
* @param string $path file path
* @param string $content new file content
* @return bool
**/
protected function _filePutContents($path, $content)
{
try {
$this->fs->write($path, $content);
} catch (UnableToWriteFile $e) {
return false;
}
return true;
}
/*********************** paths/urls *************************/
/**
* Return file name
*
* @param string $path file path
* @return string
* @author Dmitry (dio) Levashov
**/
protected function _basename($path)
{
return basename($path);
}
/**
* Join dir name and file name and return full path
*
* @param string $dir
* @param string $name
* @return string
* @author Dmitry (dio) Levashov
**/
protected function _joinPath($dir, $name)
{
return (new WhitespacePathNormalizer())->normalizePath($dir . $this->separator . $name);
}
/**
* Return file path related to root dir
*
* @param string $path file path
* @return string
**/
protected function _relpath($path)
{
return $path;
}
/**
* Convert path related to root dir into real path
*
* @param string $path file path
* @return string
**/
protected function _abspath($path)
{
return $path;
}
/**
* Return fake path started from root dir
*
* @param string $path file path
* @return string
**/
protected function _path($path)
{
return $this->rootName . $this->separator . $path;
}
/**
* Return true if $path is children of $parent
*
* @param string $path path to check
* @param string $parent parent path
* @return bool
* @author Dmitry (dio) Levashov
**/
protected function _inpath($path, $parent)
{
return $path == $parent || strpos($path, $parent . '/') === 0;
}
/**
* Create symlink
*
* @param string $source file to link to
* @param string $targetDir folder to create link in
* @param string $name symlink name
* @return bool
**/
protected function _symlink($source, $targetDir, $name)
{
return false;
}
/**
* Extract files from archive
*
* @param string $path file path
* @param array $arc archiver options
* @return bool
**/
protected function _extract($path, $arc)
{
return false;
}
/**
* Create archive and return its path
*
* @param string $dir target dir
* @param array $files files names list
* @param string $name archive name
* @param array $arc archiver options
* @return string|bool
**/
protected function _archive($dir, $files, $name, $arc)
{
return false;
}
/**
* Detect available archivers
*
* @return void
**/
protected function _checkArchivers()
{
return;
}
/**
* chmod implementation
*
* @return bool
**/
protected function _chmod($path, $mode)
{
return false;
}
/**
* Resize image
*
* @param string $hash image file
* @param int $width new width
* @param int $height new height
* @param int $x X start poistion for crop
* @param int $y Y start poistion for crop
* @param string $mode action how to mainpulate image
* @param string $bg background color
* @param int $degree rotete degree
* @param int $jpgQuality JEPG quality (1-100)
* @return array|false
* @author Dmitry (dio) Levashov
* @author Alexey Sukhotin
* @author nao-pon
* @author Troex Nevelin
**/
public function resize($hash, $width, $height, $x, $y, $mode = 'resize', $bg = '', $degree = 0, $jpgQuality = null)
{
if ($this->commandDisabled('resize')) {
return $this->setError(elFinder::ERROR_PERM_DENIED);
}
if (($file = $this->file($hash)) == false) {
return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
}
if (!$file['write'] || !$file['read']) {
return $this->setError(elFinder::ERROR_PERM_DENIED);
}
$path = $this->decode($hash);
if (!$this->canResize($path, $file)) {
return $this->setError(elFinder::ERROR_UNSUPPORT_TYPE);
}
if (!$image = $this->imageManager->read($this->_getContents($path))) {
return false;
}
switch ($mode) {
case 'propresize':
$image->scale($width, $height);
break;
case 'crop':
$image->crop($width, $height, $x, $y);
break;
case 'fitsquare':
$image->cover($width, $height, 'center');
break;
case 'rotate':
$image->rotate($degree);
break;
default:
$image->resize($width, $height);
break;
}
if ($jpgQuality) {
$result = $image->encode(new AutoEncoder(quality: $jpgQuality))->toString();
} else {
$result = $image->encode()->toString();
}
if ($result && $this->_filePutContents($path, $result)) {
$this->rmTmb($file);
$this->clearstatcache();
$stat = $this->stat($path);
$stat['width'] = $image->width();
$stat['height'] = $image->height();
return $stat;
}
return false;
}
public function getImageSize($path, $mime = '')
{
$size = false;
if ($mime === '' || strtolower(substr($mime, 0, 5)) === 'image') {
if ($data = $this->_getContents($path)) {
if ($size = @getimagesizefromstring($data)) {
$size['dimensions'] = $size[0] . 'x' . $size[1];
}
}
}
return $size;
}
/**
* Return content URL
*
* @param string $hash file hash
* @param array $options options
* @return string
**/
public function getContentUrl($hash, $options = array())
{
if (! empty($options['onetime']) && $this->options['onetimeUrl']) {
// use parent method to make onetime URL
return parent::getContentUrl($hash, $options);
}
if (!empty($options['temporary'])) {
// try make temporary file
$url = parent::getContentUrl($hash, $options);
if ($url) {
return $url;
}
}
if (($file = $this->file($hash)) == false || !isset($file['url']) || !$file['url'] || $file['url'] == 1) {
if ($file && !empty($file['url']) && !empty($options['temporary'])) {
return parent::getContentUrl($hash, $options);
}
$path = $this->decode($hash);
if ($this->options['URLCallback'] && is_callable($this->options['URLCallback'])) {
return $this->options['URLCallback']($path);
}
return parent::getContentUrl($hash, $options);
}
return $file['url'];
}
}