-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataManager.php
1105 lines (1052 loc) · 35 KB
/
DataManager.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
/*
GitHub: https://github.com/matheusjohannaraujo/data_manager
Country: Brasil
State: Pernambuco
Developer: Matheus Johann Araujo
Date: 2022-04-06
*/
namespace Lib;
error_reporting(E_ALL ^ E_WARNING);
use ZipArchive;
/**
* **DataManager**
*
* @author Matheus Johann Araujo
* @package Lib
*/
class DataManager
{
// Attributes and methods below to be used in the class instance
/**
*
* @var int $stream
*/
public $stream = -1;
/**
*
* @var int $size
*/
public $size = 0;
/**
*
* @var int $start
*/
public $start = 0;
/**
*
* @var int $bitrate
*/
public $bitrate = 1;
/**
*
* @var array $fp
*/
public $fp = [];
/**
*
* @param string|array $value [optional, default = null]
* @return $this
*/
public function __construct($value = null)
{
if (is_string($value)) {
$this->fopen($value);
} else if (is_array($value)) {
for ($i = 0, $j = count($value); $i < $j; $i++) {
$this->fopen($value[$i]);
}
}
}
/**
*
* @param string|resource $value
* @param string $mode [optional, default = "rb"]
* @return void
*/
public function fopen($value, string $mode = "rb") :void
{
if (is_string($value)) {
$value = self::path($value);
if (self::exist($value) == "FILE" || self::exist($value) == "FP") {
$fp = fopen($value, $mode);
$size = self::size($fp, false);
$this->size += $size;
$this->fp[] = [
"fp" => $fp,
"size" => $size,
"data" => stream_get_meta_data($fp),
];
$mega = 1000000;// 1Mb
if (count($this->fp) > 1) {
if ($this->bitrate > $size) {
$this->bitrate = $size;
}
} else {
if ($size > $mega) {
$this->bitrate = $mega;
} else {
$this->bitrate = $size;
}
}
} else if (self::exist($value) == "FOLDER") {
$folderScan = self::folderScan($value, true);
foreach ($folderScan as $path) {
$this->fopen($path, $mode);
}
}
} else if (gettype($value) == "resource") {
$size = self::size($value, false);
$this->fp[] = [
"fp" => $value,
"size" => $size,
"data" => stream_get_meta_data($value),
];
}
}
/**
*
* @return bool
*/
public function fmemory() :bool
{
if (($fpMem = fopen("php://memory", "w+") ?? false) && count($this->fp) > 0) {
rewind($fpMem);
$this->size = 0;
foreach ($this->fp as $key => $value) {
while (!feof($value["fp"])) {
$str = fread($value["fp"], 1024);
fwrite($fpMem, $str);
$this->size += strlen($str);
}
fclose($value["fp"]);
unset($this->fp[$key]);
}
$this->fp = [];
rewind($fpMem);
$this->fp[] = [
"fp" => $fpMem,
"size" => $this->size,
"path" => stream_get_meta_data($fpMem)["uri"],
];
return true;
}
return false;
}
/**
*
* @return bool
*/
public function feof() :bool
{
if ($this->stream == -1) {
$this->fseek(0);
}
$bool = feof($this->fp[$this->stream]["fp"]);
if ($bool && $this->start < $this->size) {
$bool = false;
}
return $bool;
}
/**
*
* @param int $index
* @return void
*/
public function fseek(int $index) :void
{
$this->start = $index;
$this->setStream($index);
if ($this->stream > -1) {
fseek($this->fp[$this->stream]["fp"], $index);
}
}
/**
*
* @return string
*/
public function fgets() :string
{
$str = "";
$start = $this->start;
$this->setStream($start);
if ($this->stream > -1) {
$str = fgets($this->fp[$this->stream]["fp"]);
$this->start += strlen($str);
if ($str) {
if (!preg_match("/\n/", $str)) {
if (!$this->feof()) {
$str .= $this->fgets();
}
}
}
}
return $str;
}
/**
*
* @param int $buffer
* @return string
*/
public function fread(int $buffer) :string
{
$str = "";
$start = $this->start;
$this->setStream($start);
if ($this->stream > -1) {
$str = fread($this->fp[$this->stream]["fp"], $buffer);
$len = strlen($str);
$this->start += $len;
$buffer = $buffer - $len;
if ($this->start < $this->size && $buffer > 0) {
$str .= $this->fread($buffer);
}
}
return $str;
}
/**
*
* @param int &$start [reference]
* @return void
*/
private function setStream(&$start) :void
{
$size = 0;
$i = 0;
if ($this->start < $this->size) {
$this->stream = -1;
foreach ($this->fp as $key => $value) {
$size += $value["size"];
if ($start < $size) {
$this->stream = $key;
if ($i >= 1) {
$x = $size - $value["size"];
$start = $start - $x;
}
break;
}
$i++;
}
} else {
if ($this->fp[$this->stream]["fp"] ?? false) {
fseek($this->fp[$this->stream]["fp"], -1);
}
}
}
/**
*
* @return void
*/
public function fclose()
{
foreach ($this->fp as $key => $value) {
fclose($value["fp"]);
unset($this->fp[$key]);
}
$this->fp = [];
}
// Statistical methods below
/**
*
* @param string|resource $path
* @return string|null
*/
public static function exist($path) :?string
{
try {
if (gettype($path) == "resource") {
return "FP";
}
if (file_exists($path) && is_file($path)) {
return "FILE";
}
if (file_exists($path) && is_dir($path)) {
return "FOLDER";
}
if (($fp = fopen($path, "rb") ?? false)) {
fclose($fp);
return "FP";
}
} catch (\Throwable $th) {}
return null;
}
/**
*
* @param string $path
* @param string $newPath
* @return bool
*/
public static function rename(string $path, string $newPath) :bool
{
if (self::exist($path)) {
return rename($path, $newPath);
}
return false;
}
/**
*
* @param string $path
* @return bool
*/
public static function delete(string $path) :bool
{
if (self::exist($path) == "FILE") {
return unlink($path);
} else if (self::exist($path) == "FOLDER") {
$scanSuperficie = self::folderScan($path, true, false);
for ($i = count($scanSuperficie) - 1, $j = 0; $i >= $j; $i--) {
if (self::exist($scanSuperficie[$i]) == "FILE") {
unlink($scanSuperficie[$i]);
} else if (self::exist($scanSuperficie[$i]) == "FOLDER") {
$scanProfundo = self::folderScan($scanSuperficie[$i], true, true);
for ($k = count($scanProfundo) - 1, $l = 0; $k >= $l; $k--) {
if (self::exist($scanProfundo[$k]) == "FILE") {
unlink($scanProfundo[$k]);
} else if (self::exist($scanProfundo[$k]) == "FOLDER") {
rmdir($scanProfundo[$k]);
}
}
rmdir($scanSuperficie[$i]);
}
}
return rmdir($path);
}
return false;
}
/**
*
* @param string $path
* @param int $permit [optional, default = -8910]
* @return string|null
*/
public static function perm(string $path, int $permit = -8910) :?string
{
if (self::exist($path)) {
if ($permit != -8910) {
chmod($path, $permit);
}
return substr(sprintf('%o', fileperms($path)), -4);
}
return null;
}
/**
*
* @param string $path
* @param string $newPath
* @return bool
*/
public static function copy(string $path, string $newPath) :bool
{
if (self::exist($path) == "FILE") {
if (self::exist($newPath) == "FOLDER") {
$newPath = self::path($newPath) . pathinfo($path)['basename'];
}
return copy($path, $newPath);
} else if (self::exist($path) == "FOLDER") {
$result = true;
$path = self::path($path);
$newPath = self::path($newPath);
if (!self::exist($newPath)) {
$result = $result && self::folderCreate($newPath);
}
$array = self::folderScan($path, true, true);
foreach ($array as $key => $value) {
if (self::exist($value) == "FOLDER") {
$result = $result && self::folderCreate(str_replace($path, $newPath, $value));
} else if (self::exist($value) == "FILE") {
$result = $result && copy($value, str_replace($path, $newPath, $value));
}
}
return $result;
}
return false;
}
/**
*
* @param string $path
* @param string $newPath
* @return bool
*/
public static function move(string $path, string $newPath) :bool
{
if (mb_strtolower($path) === mb_strtolower($newPath)) {
return self::rename($path, $newPath);
} else if (self::copy($path, $newPath)) {
return self::delete($path);
}
return false;
}
/**
*
* @param string $path
* @param bool $created_at [optional, default = true]
* @return string|null
*/
public static function time(string $path, bool $created_at = true) :?string
{
if (self::exist($path)) {
return date("Y-m-d H:i:s", $created_at ? filectime($path) : filemtime($path));
}
return null;
}
/**
*
* @param resource $fp
* @return int
*/
private static function fpsize($fp) :int
{
$data = stream_get_meta_data($fp);
// var_export($data);
$bytes = 0;
if ($data["seekable"] === true) {
fseek($fp, 0);
while (!feof($fp)) {
$bytes += strlen(fgets($fp));
}
fseek($fp, 0);
}
return $bytes;
}
/**
*
* @param string|int|resource $path
* @param bool $convert [optional, default = true]
* @param bool $precision [optional, default = false]
* @return string|int
*/
public static function size($path, bool $convert = true, bool $precision = false)
{
$bytes = 0;
if (self::exist($path) == "FOLDER") {
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS));
foreach ($iterator as $file) {
if (self::exist($file) == "FILE") {
$x = self::fileSize($file, $precision);
if ($x < 0) {
$x *= -1;
}
$bytes += $x;
}
}
} else if (self::exist($path) == "FILE") {
$bytes = self::fileSize($path, $precision);
if ($bytes < 0) {
$bytes *= -1;
}
} else if (is_numeric($path)) {
$bytes = $path;
if ($bytes < 0) {
$bytes *= -1;
}
} else if (gettype($path) == "resource") {
$bytes = self::fpsize($path);
} else if (self::exist($path) == "FP") {
if (($fp = fopen($path, "rb") ?? false)) {
$bytes = self::fpsize($fp);
fclose($fp);
}
}
if ($convert) {
if ($bytes >= 1073741824) {
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
} else if ($bytes >= 1048576) {
$bytes = number_format($bytes / 1048576, 2) . ' MB';
} else if ($bytes >= 1024) {
$bytes = number_format($bytes / 1024, 2) . ' KB';
} else if ($bytes > 1) {
$bytes = $bytes . ' bytes';
} else if ($bytes == 1) {
$bytes = $bytes . ' byte';
} else {
$bytes = '0 bytes';
}
}
return $bytes;
}
/**
*
* @param string $path
* @return string
*/
public static function path(string $path) :string
{
$path = str_replace(["\\", "/"], "/", $path);
if (self::exist($path) == "FOLDER") {
if (substr($path, -1) != "/" && substr($path, -1) != "\\") {
$path .= "/";
}
}
return $path;
}
/**
*
* @param string $path
* @param string $value [optional, default = ""]
* @param bool $append [optional, default = false]
* @return bool
*/
public static function fileCreate(string $path, string $value = "", bool $append = false) :bool
{
if (self::exist($path) === null) {
return (file_put_contents($path, $value, ($append ? FILE_APPEND : false)) >= 0);
}
return false;
}
/**
*
* @param string $path
* @param string $value
* @param string $mode [optional, default = "w"]
* @return bool
*/
public static function fileWrite(string $path, string $value, string $mode = "w") :bool
{
if (self::exist($path) != "FOLDER" && $mode == "w" || $mode == "w+") {
return ($handle = fopen($path, $mode)) && (fwrite($handle, $value) >= 0) && fclose($handle);
}
return false;
}
private static $vetFileAppend = [];
/**
*
* @param string $path
* @param string $value
* @param bool $close [optional, default = true]
* @param string $mode [optional, default = "a"]
* @return bool
*/
public static function fileAppend(string $path, string $value, bool $close = true, string $mode = "a") :bool
{
if (self::exist($path) != "FOLDER" && ($mode == "a" || $mode == "a+")) {
$faName = md5($path);
if (!(self::$vetFileAppend[$faName] ?? false)) {
self::$vetFileAppend[$faName] = fopen($path, $mode);
}
fwrite(self::$vetFileAppend[$faName], $value);
if ($close) {
return self::fileAppendClose($path);
}
return true;
}
return false;
}
/**
*
* @param string $path
* @return bool
*/
public static function fileAppendClose(string $path) :bool
{
$faName = md5($path);
$handle = self::$vetFileAppend[$faName] ?? false;
if ($handle) {
if (fclose($handle)) {
unset(self::$vetFileAppend[$faName]);
return true;
}
}
return false;
}
/**
*
* @param string $path
* @param int $type [optional, default = 1, possible values = 1...4]
* @param int $length [optional, default = 1024]
* @param string $mode [optional, default = "rb"]
* @return string|array|Generator|null
*/
public static function fileRead(string $path, int $type = 1, int $length = 1024, string $mode = "rb")
{
$gen = self::fileReadGenerator($path, $type, $length, $mode);
if ($type === 1 || $type === 2) {
return $gen->current();
}
return $gen;
}
/**
*
* @param string $path
* @param int $type [optional, default = 1, possible values = 1...4]
* @param int $length [optional, default = 1024]
* @param string $mode [optional, default = "rb"]
* @return Generator
*
* Using:
* ------ https://www.php.net/manual/pt_BR/class.generator.php
* ------ https://pt.stackoverflow.com/questions/108082/qual-a-diferen%C3%A7a-entre-yield-e-return-no-php
*/
private static function fileReadGenerator(string $path, int $type = 1, int $length = 1024, string $mode = "rb") :\Generator
{
if (self::exist($path) == "FILE" || self::exist($path) == "FP") {
switch ($type) {
case 1:
yield file_get_contents($path);
case 2:
yield file($path);
case 3:
case 4:
if ($mode == "rb" || $mode == "r" || $mode == "r+") {
$handle = fopen($path, $mode);
if ($type === 3) {
while ($value = fgets($handle)) {
yield $value;
unset($value);
}
} else if ($type === 4) {
while (!feof($handle)) {
yield fread($handle, $length);
}
}
fclose($handle);
}
break;
}
}
}
/**
*
* @param string $path
* @param bool $precision
* @return int
*/
private static function fileSize(string $path, bool $precision) :int
{
if (self::exist($path) == "FILE") {
$size = filesize($path);
if (!$precision) {
return $size;
}
if (!($file = fopen($path, 'rb'))) {
return false;
}
if ($size >= 0) { // Check if it really is a small file (< 2 GB)
if (fseek($file, 0, SEEK_END) === 0) { // It really is a small file
fclose($file);
return $size;
}
}
// Quickly jump the first 2 GB with fseek. After that fseek is not working on 32 bit php (it uses int internally)
$size = PHP_INT_MAX - 1;
if (fseek($file, PHP_INT_MAX - 1) !== 0) {
fclose($file);
return 0;
}
}
$read = "";
$length = 8192;
while (!feof($file)) { // Read the file until end
$read = fread($file, $length);
$size = bcadd($size, $length);
}
$size = bcsub($size, $length);
$size = bcadd($size, strlen($read));
fclose($file);
return $size;
}
/**
*
* @param string $path
* @param bool $del [optional, default = false]
* @return string|null
*/
public static function fileEncodeBase64(string $path, bool $del = false) :?string
{
if (self::exist($path) == "FILE") {
$newName = "enc_" . uniqid() . "." . pathinfo($path)["extension"];
$gen = self::fileRead($path, 3);
foreach ($gen as $value) {
self::fileAppend($newName, base64_encode($value) . "\r\n", false);
}
self::fileAppendClose($newName);
if (self::exist($newName) == "FILE" && $del) {
if (self::delete($path)) {
if (rename($newName, $path)) {
$newName = $path;
}
}
}
return $newName;
}
return null;
}
/**
*
* @param string $path
* @param bool $del [optional, default = false]
* @return string|null
*/
public static function fileDecodeBase64(string $path, bool $del = false) :?string
{
if (self::exist($path) == "FILE") {
$newName = "dec_" . uniqid() . "." . pathinfo($path)["extension"];
$gen = self::fileRead($path, 3);
foreach ($gen as $value) {
self::fileAppend($newName, base64_decode($value), false);
}
self::fileAppendClose($newName);
if (self::exist($newName) == "FILE" && $del) {
if (self::delete($path)) {
if (rename($newName, $path)) {
$newName = $path;
}
}
}
return $newName;
}
return null;
}
/**
*
* @param string $path
* @param float $buffer [optional, default = 1]
* @return string|null
*/
public static function fileSplit(string $path, float $buffer = 1) :?string
{
if (self::exist($path) == "FILE") {
$pathinfo = pathinfo($path);
$store = self::path($pathinfo["dirname"] . "/split_" . $pathinfo["basename"] . "/");
if (self::exist($store) == "FOLDER") {
self::delete($store);
}
if (self::folderCreate($store)) {
$buffer = 1024 * 1024 * $buffer;
$parts = self::size($path, false) / $buffer;
$handle = fopen($path, 'rb');
for ($i = 0; $i < $parts; $i++) {
$partPath = $store . "part$i";
self::fileCreate($partPath, fread($handle, $buffer));
}
fclose($handle);
return $store;
}
}
return null;
}
/**
*
* @param string $path
* @return string|null
*/
public static function fileJoin(string $path) :?string
{
if (self::exist($path) == "FOLDER") {
$pathinfo = pathinfo($path);
$dirname = self::path($pathinfo["dirname"] . "/" . $pathinfo["basename"] . "/../");
$files = self::folderScan($path, true);
$newName = $dirname . str_replace("split_", "", $pathinfo["basename"]);
if (self::fileWrite($newName, "")) {
$newName = realpath($newName);
for ($i = 0, $j = count($files); $i < $j; $i++) {
$gen = self::fileRead($files[$i], 3);
foreach ($gen as $value) {
self::fileAppend($newName, $value, false);
}
}
self::fileAppendClose($newName);
return $newName;
}
}
return null;
}
/**
*
* @param string $path
* @return string|null
*/
public static function fileMd5(string $path) :?string
{
return (self::exist($path) == "FILE") ? md5_file($path) : null;
}
/**
*
* @param string $path
* @param int $permit [optional, default = 0777]
* @return bool
*/
public static function folderCreate(string $path, int $permit = 0777, bool $recur = true) :bool
{
if (!self::exist($path)) {
return mkdir($path, $permit, $recur);
}
return false;
}
/**
*
* @param string $path
* @param bool $arrayClean [optional, default = false]
* @param bool $recursive [optional, default = false]
* @param string $typeList [optional, default = "FILE FOLDER", options = "FILE FOLDER | FILE | FOLDER"]
* @return array|null
*/
public static function folderScan(string $path, bool $arrayClean = false, bool $recursive = false, string $typeList = "FILE FOLDER") :?array
{
$result = [];
$path = realpath($path);
if (self::exist($path) != "FOLDER") {
return null;
}
if (!$recursive) {
$files = scandir($path);
foreach($files as $value){
$file = self::path(realpath($path . "/" . $value));
if (self::exist($file) == "FOLDER" && (basename($value) == ".." || $path == realpath($file))) {
continue;
}
$result[] = $file;
}
} else {
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
foreach ($iterator as $file) {
if ($file->isDir()) {
if (basename($file) == ".." || $path == realpath($file)) {
continue;
}
if (basename($file) == ".") {
$file = substr($file, 0, -1);
}
}
$result[] = self::path($file);
}
}
if ($typeList !== "FILE FOLDER") {
sort($result, SORT_NATURAL);
foreach ($result as $key => $value) {
if (self::exist($value) == "FOLDER" && $typeList == "FILE") {
unset($result[$key]);
}
if (self::exist($value) == "FILE" && $typeList == "FOLDER") {
unset($result[$key]);
}
}
}
sort($result, SORT_NATURAL);
if (!$arrayClean) {
foreach ($result as $key => $value) {
if (self::exist($value) == "FOLDER") {
$result[$key] = [
"src" => self::path(pathinfo($value)["dirname"] . "/"),
"name" => pathinfo($value)["basename"],
"path" => self::path($value),
"type" => "FOLDER",
"perm" => self::perm($value),
"size" => self::size($value),
"time" => self::time($value),
];
} else if (self::exist($value) == "FILE") {
$result[$key] = [
"src" => self::path(pathinfo($value)["dirname"] . "/"),
"name" => pathinfo($value)["basename"],
"path" => self::path($value),
"type" => "FILE",
"perm" => self::perm($value),
"size" => self::size($value),
"time" => self::time($value),
];
}
}
}
return $result;
}
/**
*
* @param string $path
* @return string|null
*/
public static function folderMd5(string $path) :?string
{
$array = self::folderScan($path, true, true);
foreach ($array as $key => $value) {
if (self::exist($value) == "FILE") {
$value = md5(md5($value) . self::fileMd5($value));
$array[$key] = $value;
} else if (self::exist($value) == "FOLDER") {
$array[$key] = md5($value);
}
}
if (is_array($array) && count($array) > 0) {
return md5(implode('', $array));
}
return null;
}
/**
*
* @param string $path
* @param string|array $array
* @param string $passZip [optional, default = ""]
* @return int
*/
public static function zipCreate(string $path, $array, string $passZip = "") :int
{
$return = 0;
$zip = new ZipArchive();
if (is_string($array)) {
$array = [$array];
}
if ($zip->open($path, ZIPARCHIVE::CREATE) && is_array($array)) {
$passStatus = false;
if ($passZip != "") {
$passStatus = $zip->setPassword($passZip);
}
$fANON = function ($zip, $path, $fANON, $passStatus, $dir = "") {
if (is_dir($path)) {
$name = pathinfo($path)["basename"] . "/";
$array = glob($path . "/" . "*");
foreach (glob($path . "/" . ".*") as $value) {
$basename = pathinfo($value)["basename"];
if ($basename != "." && $basename != ".." && (self::exist($value) == "FILE" || self::exist($value) == "FOLDER")) {
$array[] = $value;
}
}
sort($array, SORT_NATURAL);
foreach ($array as $key => $value) {
$zipValue = $dir . $name;
if (file_exists($value) && is_file($value)) {
//echo "FILE: ", $value, " = ", $zipValue . pathinfo($value)["basename"], "<br>";
$zip->addFile($value, $zipValue . pathinfo($value)["basename"]);
if ($passStatus) {
$zip->setEncryptionName($zipValue . pathinfo($value)["basename"], ZipArchive::EM_AES_256);
}
} else if (file_exists($value) && is_dir($value)) {
//var_dump($value);
//echo "DIR: ", $value, " = ", $zipValue . pathinfo($value)["basename"], "<br>";
$fANON($zip, $value, $fANON, $passStatus, $zipValue);
}
}
}
};
foreach ($array as $value) {
if (is_string($value)) {
if (file_exists($value) && is_file($value)) { // FILE
$zip->addFile($value, pathinfo($value)['basename']);
if ($passStatus) {
$zip->setEncryptionName($value, ZipArchive::EM_AES_256);
}
} else if (file_exists($value) && is_dir($value)) { // FOLDER
$fANON($zip, $value, $fANON, $passStatus);
}
} else if (is_array($value)) {
if (!file_exists($value[1]) && !is_file($value[1])) { // FILE AND STRING
$zip->addFromString($value[0], $value[1]);
if ($passStatus) {
$zip->setEncryptionName($value[0], ZipArchive::EM_AES_256);
}
}
}
}
$return = $zip->numFiles;
$zip->close();
}
return $return;
}
/**
*
* @param string $pathZip
* @param string $pathExtract
* @param string $passZip [optional, default = ""]
* @return bool
*/
public static function zipExtract(string $pathZip, string $pathExtract, string $passZip = "") :bool