-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathElfinderConnectorAction.php
1032 lines (892 loc) · 28.9 KB
/
ElfinderConnectorAction.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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Charcoal\Admin\Action;
use InvalidArgumentException;
use RuntimeException;
use UnexpectedValueException;
// From PSR-7
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
// From Pimple
use Pimple\Container;
// From elFinder
use elFinder;
use elFinderConnector;
use elFinderVolumeDriver;
// From 'charcoal-config'
use Charcoal\Config\ConfigInterface;
// From 'charcoal-factory'
use Charcoal\Factory\FactoryInterface;
// From 'charcoal-property'
use Charcoal\Property\PropertyInterface;
// From 'charcoal-app'
use Charcoal\App\CallableResolverAwareTrait;
// From 'charcoal-admin'
use Charcoal\Admin\AdminAction;
use Charcoal\Admin\Template\ElfinderTemplate;
/**
* Action: Setup elFinder Connector
*/
class ElfinderConnectorAction extends AdminAction
{
use CallableResolverAwareTrait;
/**
* The default relative path (from filesystem's root) to the storage directory.
*
* @const string
*/
const DEFAULT_STORAGE_PATH = 'uploads';
/**
* The base path for the Charcoal installation.
*
* @var string|null
*/
protected $basePath;
/**
* The path to the public / web directory.
*
* @var string|null
*/
protected $publicPath;
/**
* Store the collection of filesystem adapters.
*
* @var \League\Flysystem\FilesystemInterface[]
*/
protected $filesystems;
/**
* Store the filesystem configset.
*
* @var \Charcoal\App\Config\FilesystemConfig
*/
protected $filesystemConfig;
/**
* Store the elFinder configuration from the admin / app configset.
*
* @var ConfigInterface
*/
protected $elfinderConfig;
/**
* Store the compiled elFinder configuration settings.
*
* @var array
* - {@link https://github.com/Studio-42/elFinder/wiki/Connector-configuration-options}
*/
protected $elfinderOptions;
/**
* Store the elFinder connector instance.
*
* @var \elFinderConnector
*/
protected $elfinderConnector;
/**
* Store the current property instance for the current class.
*
* @var PropertyInterface
*/
protected $formProperty;
/**
* The related object type.
*
* @var string
*/
private $objType;
/**
* The related object ID.
*
* @var string
*/
private $objId;
/**
* The related property identifier.
*
* @var string
*/
private $propertyIdent;
/**
* Sets the action data from a PSR Request object.
*
* @param RequestInterface $request A PSR-7 compatible Request instance.
* @return self
*/
protected function setDataFromRequest(RequestInterface $request)
{
$keys = $this->validDataFromRequest();
$data = $request->getParams($keys);
if (isset($data['obj_type'])) {
$this->objType = $data['obj_type'];
}
if (isset($data['obj_id'])) {
$this->objId = $data['obj_id'];
}
if (isset($data['property'])) {
$this->propertyIdent = $data['property'];
}
return $this;
}
/**
* Retrieve the list of parameters to extract from the HTTP request.
*
* @return string[]
*/
protected function validDataFromRequest()
{
return [
'obj_type',
'obj_id',
'property',
];
}
/**
* @param RequestInterface $request A PSR-7 compatible Request instance.
* @param ResponseInterface $response A PSR-7 compatible Response instance.
* @return ResponseInterface
*/
public function run(RequestInterface $request, ResponseInterface $response)
{
unset($request);
$this->connector = $this->setupElfinder();
return $response;
}
/**
* Setup the elFinder connector.
*
* @param array|null $extraOptions Additional settings to pass to elFinder.
* @return elFinderConnector
*/
public function setupElfinder(array $extraOptions = [])
{
if (!defined('ELFINDER_IMG_PARENT_URL')) {
// Ensure images injected by elFinder are relative to its assets directory
define('ELFINDER_IMG_PARENT_URL', (string)$this->baseUrl(ElfinderTemplate::ELFINDER_ASSETS_REL_PATH));
}
$options = $this->buildConnectorOptions($extraOptions);
// Run elFinder
$connector = new elFinderConnector(new elFinder($options));
$connector->run();
return $connector;
}
/**
* Retrieve the elFinder Connector options.
*
* @return array
*/
public function getConnectorOptions()
{
if ($this->elfinderOptions === null) {
$this->elfinderOptions = $this->buildConnectorOptions();
}
return $this->elfinderOptions;
}
/**
* Build and retrieve the elFinder Connector options.
*
* @link https://github.com/Studio-42/elFinder/wiki/Connector-configuration-options
* Documentation for connector options.
* @example https://gist.github.com/mcaskill/5944478b1894a5bf1349bfa699387cd4
* The Connector can be customized by defining a "elfinder" structure in
* your application's admin configuration.
*
* @param array $extraOptions Additional settings to pass to elFinder.
* @return array
*/
public function buildConnectorOptions(array $extraOptions = [])
{
$options = [
'debug' => false,
'roots' => $this->getCurrentRoots(),
];
$adminOptions = $this->getAdminConnectorOptions();
$adminOptions = $this->parseAdminOptionsForConnectorBuild($adminOptions);
$extraOptions = $this->parseExtraOptionsForConnectorBuild($extraOptions);
$options = $this->mergeConnectorOptions($options, $adminOptions);
$options = $this->mergeConnectorOptions($options, $extraOptions);
if (isset($options['bind'])) {
$options['bind'] = $this->resolveCallbacksForBindOption($options['bind']);
}
$this->elfinderOptions = $options;
return $options;
}
/**
* Merge the elFinder Connector options.
*
* @param array $options1 The settings in which data is replaced.
* @param array $options2 The settings from which data is extracted.
* @return array The merged settings.
*/
protected function mergeConnectorOptions(array $options1, array $options2)
{
return array_replace_recursive($options1, $options2);
}
/**
* Parse the admin options for the elFinder Connector.
*
* @param array $options The admin settings to parse.
* @return array The parsed settings.
*/
protected function parseAdminOptionsForConnectorBuild(array $options)
{
// Root settings are already merged when retrieving available roots.
unset($options['roots']);
return $options;
}
/**
* Parse the extra options for the elFinder Connector.
*
* @param array $options The extra settings to parse.
* @return array The parsed settings.
*/
protected function parseExtraOptionsForConnectorBuild(array $options)
{
// Resolve callbacks on extra options
if (isset($options['roots'])) {
$options['roots'] = $this->resolveCallbacksForRoots($options['roots']);
}
return $options;
}
/**
* Retrieve the admin's elFinder Connector options.
*
* Path: `config.admin.elfinder.connector`
*
* @return array
*/
public function getAdminConnectorOptions()
{
$config = $this->elfinderConfig('connector');
if (!is_array($config)) {
return [];
}
return $config;
}
/**
* Retrieve the default elFinder Connector options.
*
* @return array
*/
protected function getDefaultElfinderRootSettings()
{
return [
'driver' => 'LocalFileSystem',
'i18nFolderName' => true,
'jpgQuality' => 80,
'tmbSize' => 200,
'tmbCrop' => true,
'tmbBgColor' => 'transparent',
'uploadDeny' => $this->defaultUploadDeny(),
'uploadAllow' => $this->defaultUploadAllow(),
'uploadOrder' => [ 'deny', 'allow' ],
'accessControl' => [ $this, 'checkAccess' ],
'duplicateSuffix' => '_%s_',
];
}
/**
* Retrieve the default Flysystem / elFinder options.
*
* @return array
*/
protected function getDefaultFlysystemRootSettings()
{
return [
'driver' => 'Flysystem',
'rootCssClass' => 'elfinder-navbar-root-local',
'filesystem' => null,
'cache' => false,
'URL' => (string)$this->baseUrl(self::DEFAULT_STORAGE_PATH),
'path' => self::DEFAULT_STORAGE_PATH,
];
}
/**
* Retrieve the default Flysystem / elFinder options.
*
* @param string $ident The disk identifier.
* @return array
*/
protected function resolveFallbackRootSettings($ident)
{
$fsConfig = $this->getFilesystemConfig($ident);
$uploadPath = $this->defaultUploadPath();
if (isset($fsConfig['base_url'])) {
$baseUrl = rtrim($fsConfig['base_url'], '/').'/';
} else {
$baseUrl = $this->baseUrl();
}
return [
'URL' => $baseUrl.'/'.$uploadPath,
'path' => $uploadPath,
'tmbURL' => (string)$this->baseUrl($uploadPath.'/.tmb'),
'tmbPath' => $uploadPath.'/.tmb',
];
}
/**
* Retrieve the elFinder root options for the given file system.
*
* Merges `config.filesystem.connections` with
* {@see self::getDefaultDiskSettings() default root settings}.
*
* @param string $ident The disk identifier.
* @return array|null Returns an elFinder root structure or NULL.
*/
public function getNamedRoot($ident)
{
if ($this->hasFilesystem($ident) === false) {
return null;
}
$filesystem = $this->getFilesystem($ident);
$fsConfig = $this->getFilesystemConfig($ident);
$elfConfig = $this->getFilesystemAdminConfig($ident);
$immutableSettings = [
'filesystem' => $this->getFilesystem($ident),
'volumeName' => $this->getFilesystemName($ident),
];
$root = array_replace_recursive(
$this->getDefaultElfinderRootSettings(),
$this->getDefaultFlysystemRootSettings(),
$elfConfig
);
$root = array_replace_recursive(
$this->resolveFallbackRootSettings($ident),
$root,
$immutableSettings
);
return $this->resolveCallbacksForRoot($root);
}
/**
* Retrieve only the public elFinder root volumes.
*
* @return array
*/
public function getPublicRoots()
{
$roots = [];
foreach ($this->filesystems->keys() as $ident) {
if ($this->isFilesystemPublic($ident)) {
$disk = $this->getNamedRoot($ident);
if ($disk !== null) {
$roots[$ident] = $disk;
}
}
}
return $roots;
}
/**
* Retrieve all elFinder root volumes.
*
* @return array
*/
public function getAllRoots()
{
$roots = [];
foreach ($this->filesystems->keys() as $ident) {
$disk = $this->getNamedRoot($ident);
if ($disk !== null) {
$roots[$ident] = $disk;
}
}
return $roots;
}
/**
* Retrieve only the current context's elFinder root volumes.
*
* @return array Returns all public root volumes
* or a subset if the context has a related form property.
*/
public function getCurrentRoots()
{
$formProperty = $this->formProperty();
$targetFilesystem = $formProperty ? $formProperty['filesystem'] : null;
if ($this->hasFilesystem($targetFilesystem)) {
$disk = $this->getNamedRoot($targetFilesystem);
$startPath = $formProperty['uploadPath'];
$isPublic = $formProperty['publicAccess'];
$acceptedMimetypes = $formProperty['acceptedMimetypes'];
$basePath = $isPublic ? $this->publicPath : $this->basePath;
if (!file_exists($basePath.$startPath)) {
mkdir($basePath.$startPath, 0755, true);
}
if ($startPath) {
$disk['startPath'] = $startPath;
}
if ($acceptedMimetypes) {
$disk['uploadAllow'] = array_merge(
isset($disk['uploadAllow'])
? $disk['uploadAllow']
: [],
$acceptedMimetypes
);
}
return [ $disk ];
}
return $this->getPublicRoots();
}
/**
* Resolve callables in a collection of elFinder's root volumes.
*
* @param array $roots One or many roots with possible unresolved callables.
* @return array Returns the root(s) with resolved callables.
*/
protected function resolveCallbacksForRoots(array $roots)
{
foreach ($roots as $i => $root) {
$roots[$i] = $this->resolveCallbacksForRoot($root);
}
return $roots;
}
/**
* Resolve callables in one elFinder root volume.
*
* @param array $root A root structure with possible unresolved callables.
* @return array Returns the root with resolved callables.
*/
protected function resolveCallbacksForRoot(array $root)
{
if (isset($root['accessControl'])) {
$callable = $root['accessControl'];
if (!is_callable($callable) && is_string($callable)) {
$root['accessControl'] = $this->resolveCallable($callable);
}
}
return $root;
}
/**
* Resolve callables in elFinder's "bind" option.
*
* @param array $toResolve One or many pairs of callbacks.
* @return array Returns the parsed event listeners.
*/
protected function resolveCallbacksForBindOption(array $toResolve)
{
$resolved = $toResolve;
foreach ($toResolve as $actions => $callables) {
foreach ($callables as $i => $callable) {
if (!is_callable($callable) && is_string($callable)) {
if (0 === strpos($callable, 'Plugin.')) {
continue;
}
$resolved[$actions][$i] = $this->resolveCallable($callable);
}
}
}
return $resolved;
}
/**
* Attempts to localize directory names in results on all commands.
*
* Expected to be used as a callback on elFinder's connector `bind` setting.
*
* @todo Move this method to a dedicated plugin class.
*
* @param string $cmd The command name.
* @param mixed $result The command result.
* @param mixed $args The command arguments from the client.
* @param object $elfinder The elFinder instance.
* @param object $volume The current volume instance.
* @return void|boolean|array
*/
public function translateDirectoriesOnAnyCommand($cmd, &$result, $args, $elfinder, $volume)
{
// To please PHPCS
unset($cmd, $args, $volume);
if (isset($result['cwd'])) {
$result['cwd'] = $this->translateDirectoryStat($result['cwd'], $elfinder);
}
$groupings = [ 'files', 'added', 'removed', 'changed' ];
foreach ($groupings as $grouping) {
if (isset($result[$grouping]) && is_array($result[$grouping])) {
foreach ($result[$grouping] as $i => $stat) {
if (isset($stat['mime']) && $stat['mime'] === 'directory') {
$result[$grouping][$i] = $this->translateDirectoryStat($stat, $elfinder);
}
}
}
}
}
/**
* Attempts to localize the directory name.
*
* @param array $stat The directory reference.
* @param object $elfinder The elFinder instance or volume instance.
* @throws UnexpectedValueException If the related volume is not found.
* @return array
*/
protected function translateDirectoryStat(array $stat, object $elfinder): array
{
if (isset($stat['hash'], $stat['isroot']) && $stat['isroot']) {
$stat['i18'] = $this->getVolumeNameFromHash($stat['hash'], $elfinder);
}
return $stat;
}
/**
* Attempts to retrieve the volume name by its directory hash.
*
* The "volumeName" option is defined by {@see self::getNamedRoot()}
* and its value is resolved by {@see self::translateVolumeName()}.
*
* @param string $hash The directory hash.
* @param object $elfinder The elFinder instance or volume instance.
* @throws InvalidArgumentException If the elFinder client or volume is not provided.
* @throws UnexpectedValueException If the related volume is not found.
* @return ?string
*/
protected function getVolumeNameFromHash(string $hash, object $elfinder): ?string
{
if ($elfinder instanceof elFinder) {
$volume = $elfinder->getVolume($hash);
if (!$volume) {
throw new UnexpectedValueException(
sprintf('Could not find volume for path [%s]', $hash)
);
}
} elseif ($elfinder instanceof elFinderVolumeDriver) {
$volume = $elfinder;
} else {
throw new InvalidArgumentException(
'Expected an instance of elFinder or elFinderVolumeDriver'
);
}
return $volume->getOption('volumeName');
}
/**
* Attempts to retrieve the filesystem name.
*
* @param string $ident The filesystem identifier.
* @return ?string
*/
protected function getFilesystemName(string $ident): ?string
{
$fsConfig = $this->getFilesystemConfig($ident);
if (isset($fsConfig['label'])) {
$name = $this->translator()->translate($fsConfig['label']);
if ($name) {
return $name;
}
}
return $this->translateFilesystemName($ident);
}
/**
* Attempts to localize the filesystem identifier.
*
* @param string $ident The filesystem identifier.
* @return ?string
*/
protected function translateFilesystemName(string $ident): ?string
{
$key = 'filesystem.volume.'.$ident;
$name = $this->translator()->trans($key);
if ($key !== $name) {
return $name;
}
return null;
}
/**
* Sanitizes a file name on `upload.presave`.
*
* Based on {@see \elFinderPluginSanitizer::onUpLoadPreSave()}.
*
* Expected to be used as a callback on elFinder's connector `bind` setting.
*
* @todo Move this method to a dedicated plugin class.
*
* @param string $path The target path.
* @param string $name The target name.
* @param string $src The temporary file name.
* @param object $elfinder The elFinder instance.
* @param object $volume The current volume instance.
* @return void|boolean|array
*/
public function sanitizeOnUploadPreSave(&$path, &$name, $src, $elfinder, $volume)
{
// To please PHPCS
unset($path, $src, $elfinder, $volume);
if (isset($this->elfinderOptions['plugin']['Sanitizer'])) {
$options = $this->elfinderOptions['plugin']['Sanitizer'];
if (isset($options['enable']) && $options['enable']) {
$name = $this->sanitizeFileName($name, $options);
return true;
}
}
return false;
}
/**
* Sanitize a file name.
*
* Trims whitespace and squeezes delimeter.
*
* @param string $name The file name to sanitize.
* @param array $options The elFinder Sanitizer plugin options.
* @return string The sanitized filename.
*/
protected function sanitizeFileName(string $name, array $options): string
{
if (is_array($options['replace'])) {
$mask = implode($options['replace']);
} else {
$mask = $options['replace'];
}
$ext = '.'.pathinfo($name, PATHINFO_EXTENSION);
// Strip leading and trailing dashes or underscores
$name = trim(str_replace($ext, '', $name), $mask);
// Squeeze multiple delimiters and whitespace with a single separator
$name = preg_replace(
'!['.preg_quote($mask, '!').'\.\s]{2,}!',
$options['replace'],
$name
);
// Reassemble the file name
$name .= $ext;
return $name;
}
/**
* Control file access.
*
* This method will disable accessing files/folders starting from '.' (dot)
*
* @param string $attr Attribute name ("read", "write", "locked", "hidden").
* @param string $path Absolute file path.
* @param string $data Value of volume option `accessControlData`.
* @param \elFinderVolumeDriver $volume ElFinder volume driver object.
* @param boolean|null $isDir Whether the path is a directory
* (TRUE: directory, FALSE: file, NULL: unknown).
* @param string $relPath File path relative to the volume root directory
* started with directory separator.
* @return boolean|null TRUE to allow, FALSE to deny, NULL to let elFinder decide.
**/
public function checkAccess($attr, $path, $data, elFinderVolumeDriver $volume, $isDir, $relPath)
{
unset($data, $volume, $isDir);
$basename = basename($path);
/**
* If file/folder begins with '.' (dot) but without volume root,
* set to FALSE if attributes are "read" or "write",
* set to TRUE if attributes are other ("locked" + "hidden"),
* set to NULL to let elFinder decide itself.
*/
return ($basename[0] === '.' && strlen($relPath) !== 1)
? !($attr === 'read' || $attr === 'write')
: null;
}
/**
* Retrieve the current object type from the GET parameters.
*
* @return string|null
*/
public function objType()
{
return $this->objType;
}
/**
* Retrieve the current object ID from the GET parameters.
*
* @return string|null
*/
public function objId()
{
return $this->objId;
}
/**
* Retrieve the current object's property identifier from the GET parameters.
*
* @return string|null
*/
public function propertyIdent()
{
return $this->propertyIdent;
}
/**
* Retrieve the current property.
*
* @return PropertyInterface|boolean A Form Property instance
* or FALSE if a property can not be resolved.
*/
public function formProperty()
{
if ($this->formProperty === null) {
$this->formProperty = false;
$objType = $this->objType();
$propertyIdent = $this->propertyIdent();
if ($objType && $propertyIdent) {
$model = $this->modelFactory()->get($objType);
if ($model->hasProperty($propertyIdent)) {
$this->formProperty = $model->property($propertyIdent);
}
}
}
return $this->formProperty;
}
/**
* Retrieve the default root path.
*
* @return string
*/
public function defaultUploadPath()
{
return self::DEFAULT_STORAGE_PATH;
}
/**
* Allow upload for a subset MIME types.
*
* @return array
*/
protected function defaultUploadAllow()
{
// By default, all images, PDF, and plain-text files are allowed.
return [
'image',
'application/pdf',
'text/plain',
];
}
/**
* Deny upload for all MIME types.
*
* @return array
*/
protected function defaultUploadDeny()
{
// By default, all files are rejected.
return [
'all',
];
}
/**
* Default attributes for files and directories.
*
* @return array
*/
protected function attributesForHiddenFiles()
{
return [
// Block access to all hidden files and directories (anything starting with ".")
'pattern' => '!(?:^|/)\..+$!',
'read' => false,
'write' => false,
'hidden' => true,
'locked' => false,
];
}
/**
* Inject dependencies from a DI Container.
*
* @param Container $container A dependencies container instance.
* @return void
*/
public function setDependencies(Container $container)
{
parent::setDependencies($container);
$this->basePath = $container['config']['base_path'];
$this->publicPath = $container['config']['public_path'];
$this->setElfinderConfig($container['elfinder/config']);
$this->setCallableResolver($container['callableResolver']);
/** @see \Charcoal\App\ServiceProvide\FilesystemServiceProvider */
$this->filesystemConfig = $container['filesystem/config'];
$this->filesystems = $container['filesystems'];
}
/**
* Get the named filesystem object.
*
* @param string $ident The filesystem identifier.
* @return \League\Flysystem\FilesystemInterface|null Returns the filesystem instance
* or NULL if not found.
*/
protected function getFilesystem($ident)
{
if (isset($this->filesystems[$ident])) {
return $this->filesystems[$ident];
}
return null;
}
/**
* Determine if the named filesystem object exists.
*
* @param string $ident The filesystem identifier.
* @return boolean TRUE if the filesystem instance exists, otherwise FALSE.
*/
protected function hasFilesystem($ident)
{
return ($this->getFilesystem($ident) !== null);
}
/**
* Get the given filesystem's storage configset.
*
* @param string $ident The filesystem identifier.
* @return array|null Returns the filesystem configset
* or NULL if the filesystem is not found.
*/
protected function getFilesystemConfig($ident)
{
if ($this->hasFilesystem($ident) === false) {
return null;
}
if (isset($this->filesystemConfig['connections'][$ident])) {
return $this->filesystemConfig['connections'][$ident];
}
return [];
}
/**
* Determine if the named filesystem is public (from its configset).
*
* @param string $ident The filesystem identifier.
* @return boolean TRUE if the filesystem is public, otherwise FALSE.
*/
protected function isFilesystemPublic($ident)
{
if ($this->hasFilesystem($ident) === false) {
return false;
}
$config = $this->getFilesystemConfig($ident);
if (isset($config['public']) && $config['public'] === false) {
return false;
}
return true;
}
/**
* Get the given filesystem's admin configset.
*
* @param string $ident The filesystem identifier.
* @return array|null Returns the filesystem configset
* or NULL if the filesystem is not found.
*/
protected function getFilesystemAdminConfig($ident)
{
if ($this->hasFilesystem($ident) === false) {
return null;
}
$elfConfig = $this->getAdminConnectorOptions();
if (isset($elfConfig['roots'][$ident])) {
return $elfConfig['roots'][$ident];
}
return [];
}
/**
* Set the elFinder's configset.
*