-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
File.php
909 lines (863 loc) · 25.3 KB
/
File.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
<?php
/**
* Origin filesystem driver
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Filesystem\Driver;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem\DriverInterface;
/**
* Class File
* @package Magento\Framework\Filesystem\Driver
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class File implements DriverInterface
{
/**
* @var string
*/
protected $scheme = '';
/**
* Returns last warning message string
*
* @return string
*/
protected function getWarningMessage()
{
$warning = error_get_last();
if ($warning && $warning['type'] == E_WARNING) {
return 'Warning!' . $warning['message'];
}
return null;
}
/**
* Is file or directory exist in file system
*
* @param string $path
* @return bool
* @throws FileSystemException
*/
public function isExists($path)
{
clearstatcache();
$result = @file_exists($this->getScheme() . $path);
if ($result === null) {
throw new FileSystemException(
new \Magento\Framework\Phrase('Error occurred during execution %1', [$this->getWarningMessage()])
);
}
return $result;
}
/**
* Gathers the statistics of the given path
*
* @param string $path
* @return array
* @throws FileSystemException
*/
public function stat($path)
{
clearstatcache();
$result = @stat($this->getScheme() . $path);
if (!$result) {
throw new FileSystemException(
new \Magento\Framework\Phrase('Cannot gather stats! %1', [$this->getWarningMessage()])
);
}
return $result;
}
/**
* Check permissions for reading file or directory
*
* @param string $path
* @return bool
* @throws FileSystemException
*/
public function isReadable($path)
{
clearstatcache();
$result = @is_readable($this->getScheme() . $path);
if ($result === null) {
throw new FileSystemException(
new \Magento\Framework\Phrase('Error occurred during execution %1', [$this->getWarningMessage()])
);
}
return $result;
}
/**
* Tells whether the filename is a regular file
*
* @param string $path
* @return bool
* @throws FileSystemException
*/
public function isFile($path)
{
clearstatcache();
$result = @is_file($this->getScheme() . $path);
if ($result === null) {
throw new FileSystemException(
new \Magento\Framework\Phrase('Error occurred during execution %1', [$this->getWarningMessage()])
);
}
return $result;
}
/**
* Tells whether the filename is a regular directory
*
* @param string $path
* @return bool
* @throws FileSystemException
*/
public function isDirectory($path)
{
clearstatcache();
$result = @is_dir($this->getScheme() . $path);
if ($result === null) {
throw new FileSystemException(
new \Magento\Framework\Phrase('Error occurred during execution %1', [$this->getWarningMessage()])
);
}
return $result;
}
/**
* Retrieve file contents from given path
*
* @param string $path
* @param string|null $flag
* @param resource|null $context
* @return string
* @throws FileSystemException
*/
public function fileGetContents($path, $flag = null, $context = null)
{
clearstatcache();
$result = @file_get_contents($this->getScheme() . $path, $flag, $context);
if (false === $result) {
throw new FileSystemException(
new \Magento\Framework\Phrase(
'Cannot read contents from file "%1" %2',
[$path, $this->getWarningMessage()]
)
);
}
return $result;
}
/**
* Check if given path is writable
*
* @param string $path
* @return bool
* @throws FileSystemException
*/
public function isWritable($path)
{
clearstatcache();
$result = @is_writable($this->getScheme() . $path);
if ($result === null) {
throw new FileSystemException(
new \Magento\Framework\Phrase('Error occurred during execution %1', [$this->getWarningMessage()])
);
}
return $result;
}
/**
* Returns parent directory's path
*
* @param string $path
* @return string
*/
public function getParentDirectory($path)
{
return dirname($this->getScheme() . $path);
}
/**
* Create directory
*
* @param string $path
* @param int $permissions
* @return bool
* @throws FileSystemException
*/
public function createDirectory($path, $permissions)
{
$result = @mkdir($this->getScheme() . $path, $permissions, true);
if (!$result) {
throw new FileSystemException(
new \Magento\Framework\Phrase(
'Directory "%1" cannot be created %2',
[$path, $this->getWarningMessage()]
)
);
}
return $result;
}
/**
* Read directory
*
* @param string $path
* @return string[]
* @throws FileSystemException
*/
public function readDirectory($path)
{
try {
$flags = \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS;
$iterator = new \FilesystemIterator($path, $flags);
$result = [];
/** @var \FilesystemIterator $file */
foreach ($iterator as $file) {
$result[] = $file->getPathname();
}
sort($result);
return $result;
} catch (\Exception $e) {
throw new FileSystemException(new \Magento\Framework\Phrase($e->getMessage()), $e);
}
}
/**
* Search paths by given regex
*
* @param string $pattern
* @param string $path
* @return string[]
* @throws FileSystemException
*/
public function search($pattern, $path)
{
clearstatcache();
$globPattern = rtrim($path, '/') . '/' . ltrim($pattern, '/');
$result = @glob($globPattern, GLOB_BRACE);
return is_array($result) ? $result : [];
}
/**
* Renames a file or directory
*
* @param string $oldPath
* @param string $newPath
* @param DriverInterface|null $targetDriver
* @return bool
* @throws FileSystemException
*/
public function rename($oldPath, $newPath, DriverInterface $targetDriver = null)
{
$result = false;
$targetDriver = $targetDriver ?: $this;
if (get_class($targetDriver) == get_class($this)) {
$result = @rename($this->getScheme() . $oldPath, $newPath);
} else {
$content = $this->fileGetContents($oldPath);
if (false !== $targetDriver->filePutContents($newPath, $content)) {
$result = $this->deleteFile($newPath);
}
}
if (!$result) {
throw new FileSystemException(
new \Magento\Framework\Phrase(
'The "%1" path cannot be renamed into "%2" %3',
[$oldPath, $newPath, $this->getWarningMessage()]
)
);
}
return $result;
}
/**
* Copy source into destination
*
* @param string $source
* @param string $destination
* @param DriverInterface|null $targetDriver
* @return bool
* @throws FileSystemException
*/
public function copy($source, $destination, DriverInterface $targetDriver = null)
{
$targetDriver = $targetDriver ?: $this;
if (get_class($targetDriver) == get_class($this)) {
$result = @copy($this->getScheme() . $source, $destination);
} else {
$content = $this->fileGetContents($source);
$result = $targetDriver->filePutContents($destination, $content);
}
if (!$result) {
throw new FileSystemException(
new \Magento\Framework\Phrase(
'The file or directory "%1" cannot be copied to "%2" %3',
[
$source,
$destination,
$this->getWarningMessage()
]
)
);
}
return $result;
}
/**
* Create symlink on source and place it into destination
*
* @param string $source
* @param string $destination
* @param DriverInterface|null $targetDriver
* @return bool
* @throws FileSystemException
*/
public function symlink($source, $destination, DriverInterface $targetDriver = null)
{
$result = false;
if (get_class($targetDriver) == get_class($this)) {
$result = @symlink($this->getScheme() . $source, $destination);
}
if (!$result) {
throw new FileSystemException(
new \Magento\Framework\Phrase(
'Cannot create a symlink for "%1" and place it to "%2" %3',
[
$source,
$destination,
$this->getWarningMessage()
]
)
);
}
return $result;
}
/**
* Delete file
*
* @param string $path
* @return bool
* @throws FileSystemException
*/
public function deleteFile($path)
{
$result = @unlink($this->getScheme() . $path);
if (!$result) {
throw new FileSystemException(
new \Magento\Framework\Phrase('The file "%1" cannot be deleted %2', [$path, $this->getWarningMessage()])
);
}
return $result;
}
/**
* Recursive delete directory
*
* @param string $path
* @return bool
* @throws FileSystemException
*/
public function deleteDirectory($path)
{
$flags = \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS;
$iterator = new \FilesystemIterator($path, $flags);
/** @var \FilesystemIterator $entity */
foreach ($iterator as $entity) {
if ($entity->isDir()) {
$this->deleteDirectory($entity->getPathname());
} else {
$this->deleteFile($entity->getPathname());
}
}
$result = @rmdir($this->getScheme() . $path);
if (!$result) {
throw new FileSystemException(
new \Magento\Framework\Phrase(
'The directory "%1" cannot be deleted %2',
[$path, $this->getWarningMessage()]
)
);
}
return $result;
}
/**
* Change permissions of given path
*
* @param string $path
* @param int $permissions
* @return bool
* @throws FileSystemException
*/
public function changePermissions($path, $permissions)
{
$result = @chmod($this->getScheme() . $path, $permissions);
if (!$result) {
throw new FileSystemException(
new \Magento\Framework\Phrase(
'Cannot change permissions for path "%1" %2',
[$path, $this->getWarningMessage()]
)
);
}
return $result;
}
/**
* Recursively change permissions of given path
*
* @param string $path
* @param int $dirPermissions
* @param int $filePermissions
* @return bool
* @throws FileSystemException
*/
public function changePermissionsRecursively($path, $dirPermissions, $filePermissions)
{
$result = true;
if ($this->isFile($path)) {
$result = @chmod($path, $filePermissions);
} else {
$result = @chmod($path, $dirPermissions);
}
if (!$result) {
throw new FileSystemException(
new \Magento\Framework\Phrase(
'Cannot change permissions for path "%1" %2',
[$path, $this->getWarningMessage()]
)
);
}
$flags = \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS;
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($path, $flags),
\RecursiveIteratorIterator::CHILD_FIRST
);
/** @var \FilesystemIterator $entity */
foreach ($iterator as $entity) {
if ($entity->isDir()) {
$result = @chmod($entity->getPathname(), $dirPermissions);
} else {
$result = @chmod($entity->getPathname(), $filePermissions);
}
if (!$result) {
throw new FileSystemException(
new \Magento\Framework\Phrase(
'Cannot change permissions for path "%1" %2',
[$path, $this->getWarningMessage()]
)
);
}
}
return $result;
}
/**
* Sets access and modification time of file.
*
* @param string $path
* @param int|null $modificationTime
* @return bool
* @throws FileSystemException
*/
public function touch($path, $modificationTime = null)
{
if (!$modificationTime) {
$result = @touch($this->getScheme() . $path);
} else {
$result = @touch($this->getScheme() . $path, $modificationTime);
}
if (!$result) {
throw new FileSystemException(
new \Magento\Framework\Phrase(
'The file or directory "%1" cannot be touched %2',
[$path, $this->getWarningMessage()]
)
);
}
return $result;
}
/**
* Write contents to file in given path
*
* @param string $path
* @param string $content
* @param string|null $mode
* @return int The number of bytes that were written.
* @throws FileSystemException
*/
public function filePutContents($path, $content, $mode = null)
{
$result = @file_put_contents($this->getScheme() . $path, $content, $mode);
if (!$result) {
throw new FileSystemException(
new \Magento\Framework\Phrase(
'The specified "%1" file could not be written %2',
[$path, $this->getWarningMessage()]
)
);
}
return $result;
}
/**
* Open file
*
* @param string $path
* @param string $mode
* @return resource file
* @throws FileSystemException
*/
public function fileOpen($path, $mode)
{
$result = @fopen($this->getScheme() . $path, $mode);
if (!$result) {
throw new FileSystemException(
new \Magento\Framework\Phrase('File "%1" cannot be opened %2', [$path, $this->getWarningMessage()])
);
}
return $result;
}
/**
* Reads the line content from file pointer (with specified number of bytes from the current position).
*
* @param resource $resource
* @param int $length
* @param string $ending [optional]
* @return string
* @throws FileSystemException
*/
public function fileReadLine($resource, $length, $ending = null)
{
$result = @stream_get_line($resource, $length, $ending);
if (false === $result) {
throw new FileSystemException(
new \Magento\Framework\Phrase('File cannot be read %1', [$this->getWarningMessage()])
);
}
return $result;
}
/**
* Reads the specified number of bytes from the current position.
*
* @param resource $resource
* @param int $length
* @return string
* @throws FileSystemException
*/
public function fileRead($resource, $length)
{
$result = @fread($resource, $length);
if ($result === false) {
throw new FileSystemException(
new \Magento\Framework\Phrase('File cannot be read %1', [$this->getWarningMessage()])
);
}
return $result;
}
/**
* Reads one CSV row from the file
*
* @param resource $resource
* @param int $length [optional]
* @param string $delimiter [optional]
* @param string $enclosure [optional]
* @param string $escape [optional]
* @return array|bool|null
* @throws FileSystemException
*/
public function fileGetCsv($resource, $length = 0, $delimiter = ',', $enclosure = '"', $escape = '\\')
{
$result = @fgetcsv($resource, $length, $delimiter, $enclosure, $escape);
if ($result === null) {
throw new FileSystemException(
new \Magento\Framework\Phrase('Wrong CSV handle %1', [$this->getWarningMessage()])
);
}
return $result;
}
/**
* Returns position of read/write pointer
*
* @param resource $resource
* @return int
* @throws FileSystemException
*/
public function fileTell($resource)
{
$result = @ftell($resource);
if ($result === null) {
throw new FileSystemException(
new \Magento\Framework\Phrase('Error occurred during execution %1', [$this->getWarningMessage()])
);
}
return $result;
}
/**
* Seeks to the specified offset
*
* @param resource $resource
* @param int $offset
* @param int $whence
* @return int
* @throws FileSystemException
*/
public function fileSeek($resource, $offset, $whence = SEEK_SET)
{
$result = @fseek($resource, $offset, $whence);
if ($result === -1) {
throw new FileSystemException(
new \Magento\Framework\Phrase(
'Error occurred during execution of fileSeek %1',
[$this->getWarningMessage()]
)
);
}
return $result;
}
/**
* Returns true if pointer at the end of file or in case of exception
*
* @param resource $resource
* @return boolean
*/
public function endOfFile($resource)
{
return feof($resource);
}
/**
* Close file
*
* @param resource $resource
* @return boolean
* @throws FileSystemException
*/
public function fileClose($resource)
{
$result = @fclose($resource);
if (!$result) {
throw new FileSystemException(
new \Magento\Framework\Phrase(
'Error occurred during execution of fileClose %1',
[$this->getWarningMessage()]
)
);
}
return $result;
}
/**
* Writes data to file
*
* @param resource $resource
* @param string $data
* @return int
* @throws FileSystemException
*/
public function fileWrite($resource, $data)
{
$result = @fwrite($resource, $data);
if (false === $result) {
throw new FileSystemException(
new \Magento\Framework\Phrase(
'Error occurred during execution of fileWrite %1',
[$this->getWarningMessage()]
)
);
}
return $result;
}
/**
* Writes one CSV row to the file.
*
* @param resource $resource
* @param array $data
* @param string $delimiter
* @param string $enclosure
* @return int
* @throws FileSystemException
*/
public function filePutCsv($resource, array $data, $delimiter = ',', $enclosure = '"')
{
/**
* Security enhancement for CSV data processing by Excel-like applications.
* @see https://bugzilla.mozilla.org/show_bug.cgi?id=1054702
*
* @var $value string|\Magento\Framework\Phrase
*/
foreach ($data as $key => $value) {
if (!is_string($value)) {
$value = (string)$value;
}
if (isset($value[0]) && in_array($value[0], ['=', '+', '-'])) {
$data[$key] = ' ' . $value;
}
}
$result = @fputcsv($resource, $data, $delimiter, $enclosure);
if (!$result) {
throw new FileSystemException(
new \Magento\Framework\Phrase(
'Error occurred during execution of filePutCsv %1',
[$this->getWarningMessage()]
)
);
}
return $result;
}
/**
* Flushes the output
*
* @param resource $resource
* @return bool
* @throws FileSystemException
*/
public function fileFlush($resource)
{
$result = @fflush($resource);
if (!$result) {
throw new FileSystemException(
new \Magento\Framework\Phrase(
'Error occurred during execution of fileFlush %1',
[$this->getWarningMessage()]
)
);
}
return $result;
}
/**
* Lock file in selected mode
*
* @param resource $resource
* @param int $lockMode
* @return bool
* @throws FileSystemException
*/
public function fileLock($resource, $lockMode = LOCK_EX)
{
$result = @flock($resource, $lockMode);
if (!$result) {
throw new FileSystemException(
new \Magento\Framework\Phrase(
'Error occurred during execution of fileLock %1',
[$this->getWarningMessage()]
)
);
}
return $result;
}
/**
* Unlock file
*
* @param resource $resource
* @return bool
* @throws FileSystemException
*/
public function fileUnlock($resource)
{
$result = @flock($resource, LOCK_UN);
if (!$result) {
throw new FileSystemException(
new \Magento\Framework\Phrase(
'Error occurred during execution of fileUnlock %1',
[$this->getWarningMessage()]
)
);
}
return $result;
}
/**
* @param string $basePath
* @param string $path
* @param string|null $scheme
* @return string
*/
public function getAbsolutePath($basePath, $path, $scheme = null)
{
return $this->getScheme($scheme) . $basePath . ltrim($this->fixSeparator($path), '/');
}
/**
* Retrieves relative path
*
* @param string $basePath
* @param string $path
* @return string
*/
public function getRelativePath($basePath, $path = null)
{
$path = $this->fixSeparator($path);
if (strpos($path, $basePath) === 0 || $basePath == $path . '/') {
$result = substr($path, strlen($basePath));
} else {
$result = $path;
}
return $result;
}
/**
* Fixes path separator
* Utility method.
*
* @param string $path
* @return string
*/
protected function fixSeparator($path)
{
return str_replace('\\', '/', $path);
}
/**
* Return path with scheme
*
* @param null|string $scheme
* @return string
*/
protected function getScheme($scheme = null)
{
return $scheme ? $scheme . '://' : '';
}
/**
* Read directory recursively
*
* @param string $path
* @return string[]
* @throws FileSystemException
*/
public function readDirectoryRecursively($path = null)
{
$result = [];
$flags = \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS;
try {
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($path, $flags),
\RecursiveIteratorIterator::CHILD_FIRST
);
/** @var \FilesystemIterator $file */
foreach ($iterator as $file) {
$result[] = $file->getPathname();
}
} catch (\Exception $e) {
throw new FileSystemException(new \Magento\Framework\Phrase($e->getMessage()), $e);
}
return $result;
}
/**
* Get real path
*
* @param string $path
*
* @return string|bool
*/
public function getRealPath($path)
{
return realpath($path);
}
/**
* Return correct path for link
*
* @param string $path
* @return mixed
*/
public function getRealPathSafety($path)
{
if (strpos($path, DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR) === false) {
return $path;
}
$pathParts = explode(DIRECTORY_SEPARATOR, $path);
$realPath = [];
foreach ($pathParts as $pathPart) {
if ($pathPart == '.') {
continue;
}
if ($pathPart == '..') {
array_pop($realPath);
continue;
}
$realPath[] = $pathPart;
}
return implode(DIRECTORY_SEPARATOR, $realPath);
}
}