PluginProbe ʕ •ᴥ•ʔ
Backup Migration / 1.4.5
Backup Migration v1.4.5
2.1.7 2.1.6 2.1.5.2 trunk 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.6.1 1.4.7 1.4.8 1.4.9 1.4.9.1 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.5.1
backup-backup / includes / scanner / files.php
backup-backup / includes / scanner Last commit date
backups.php 2 years ago files.php 2 years ago
files.php
524 lines
1 <?php
2
3 // Namespace
4 namespace BMI\Plugin\Scanner;
5
6 // Use
7 use BMI\Plugin\BMI_Logger AS Logger;
8 use BMI\Plugin\Zipper\BMI_Zipper AS Zipper;
9
10 // Exit on direct access
11 if (!defined('ABSPATH')) exit;
12
13 /**
14 * Main File Scanner Logic
15 */
16 class BMI_FileScanner {
17
18 public static function equalEnd($name, $nsiz, $rsiz, $rule) {
19
20 if (substr($name, -$rsiz) == $rule) return true;
21
22 }
23
24 public static function equalStart($name, $nsiz, $rsiz, $rule) {
25
26 if (substr($name, 0, $rsiz) == $rule) return true;
27
28 }
29
30 public static function equalAnywhere($name, $rule) {
31
32 if (strpos($name, $rule) !== false) return true;
33
34 }
35
36 public static function equalFolder($name, $ignores) {
37
38 $s = strlen($name);
39 for ($i = 0; $i < sizeof($ignores); ++$i) {
40
41 if ($ignores[$i]['z'] > $s) continue;
42 if ($ignores[$i]['w'] == '1') {
43
44 if (BMI_FileScanner::equalAnywhere($name, $ignores[$i]['s'])) return true;
45 else continue;
46
47 } elseif ($ignores[$i]['w'] == '2') {
48
49 if (BMI_FileScanner::equalStart($name, $s, $ignores[$i]['z'], $ignores[$i]['s'])) return true;
50 else continue;
51
52 } elseif ($ignores[$i]['w'] == '3') {
53
54 if (BMI_FileScanner::equalEnd($name, $s, $ignores[$i]['z'], $ignores[$i]['s'])) return true;
55 else continue;
56
57 }
58
59 }
60
61 return false;
62
63 }
64
65 public static function equalFolderByPath($abs, $path, $ignores) {
66
67 $alen = strlen($abs);
68 $path = substr($path, $alen + 1);
69 $paths = explode(DIRECTORY_SEPARATOR, $path);
70
71 for ($i=0; $i < sizeof($paths); ++$i) {
72
73 $c = $paths[$i];
74 if (BMI_FileScanner::equalFolder($c, $ignores)) {
75
76 return true;
77
78 }
79
80 }
81
82 return false;
83
84 }
85
86 public static function scanDirectory($path) {
87
88 $files = [];
89 if (!is_readable($path) || is_link($path)) return $files;
90 foreach (new \DirectoryIterator($path) as $fileInfo) {
91
92 if (!is_readable($fileInfo->getPathname())) continue;
93 if ($fileInfo->isDot()) continue;
94 if ($fileInfo->isLink()) continue;
95 if (!$fileInfo->isDir()) {
96
97 try {
98 $files[] = $fileInfo->getFilename() . DIRECTORY_SEPARATOR . $fileInfo->getSize();
99 } catch (\Exception $e) {
100 Logger::debug('Failed to check file: ' . $fileInfo->getFilename());
101 } catch (\Throwable $e) {
102 Logger::debug('Failed to check file: ' . $fileInfo->getFilename());
103 }
104
105 } else {
106
107 $files[] = [];
108 $index = sizeof($files) - 1;
109 $dirName = $fileInfo->getFilename();
110 $newBase = $path . DIRECTORY_SEPARATOR . $dirName;
111 $files[$index] = BMI_FileScanner::scanDirectory($newBase);
112 array_unshift($files[$index], $dirName);
113
114 }
115
116 }
117
118 return $files;
119
120 }
121
122 public static function scanDirectorySizeOnly($path, $bm) {
123
124 $files = [];
125 if (!is_readable($path) || is_link($path)) return $files;
126 foreach (new \DirectoryIterator($path) as $fileInfo) {
127
128 if (!is_readable($fileInfo->getPathname())) continue;
129 if ($fileInfo->isDot()) continue;
130 if ($fileInfo->isLink()) continue;
131 if (!$fileInfo->isDir()) {
132
133 try {
134 $files[] = $fileInfo->getSize();
135 } catch (\Exception $e) {
136 Logger::debug('Failed to check file: ' . $fileInfo->getFilename());
137 } catch (\Throwable $e) {
138 Logger::debug('Failed to check file: ' . $fileInfo->getFilename());
139 }
140
141 } else {
142
143 $files[] = [];
144 $index = sizeof($files) - 1;
145 $dirName = $fileInfo->getFilename();
146 $newBase = $path . DIRECTORY_SEPARATOR . $dirName;
147 if ($newBase == $bm) continue;
148 $files[$index] = BMI_FileScanner::scanDirectorySizeOnly($newBase, $bm);
149 array_unshift($files[$index], $dirName);
150
151 }
152
153 }
154
155 return $files;
156
157 }
158
159 public static function scanDirectorySizeOnlyAndIgnore($path, $ignored = [], $bm = '') {
160
161 $files = [];
162 if (!is_readable($path) || is_link($path)) return $files;
163 foreach (new \DirectoryIterator($path) as $fileInfo) {
164
165 if (!is_readable($fileInfo->getPathname())) continue;
166 if ($fileInfo->isDot()) continue;
167 if ($fileInfo->isLink()) continue;
168 if (!$fileInfo->isDir()) {
169
170 try {
171 $files[] = $fileInfo->getSize();
172 } catch (\Exception $e) {
173 Logger::debug('Failed to check file: ' . $fileInfo->getFilename());
174 } catch (\Throwable $e) {
175 Logger::debug('Failed to check file: ' . $fileInfo->getFilename());
176 }
177
178 } else {
179
180 $dirPath = $fileInfo->getPath();
181 $dirName = $fileInfo->getFilename();
182 if (in_array($dirName, $ignored) || in_array($dirPath, $ignored)) {
183 Logger::debug('Dodging ' . $dirName);
184 continue;
185 }
186
187 $files[] = [];
188 $index = sizeof($files) - 1;
189 $newBase = $path . DIRECTORY_SEPARATOR . $dirName;
190 if ($newBase == $bm) continue;
191 $files[$index] = BMI_FileScanner::scanDirectorySizeOnlyAndIgnoreDirOnly($newBase, $ignored, $bm);
192 array_unshift($files[$index], $dirName);
193
194 }
195
196 }
197
198 return $files;
199
200 }
201
202 public static function scanDirectorySizeOnlyAndIgnoreDirOnly($path, $ignored = [], $bm = '') {
203
204 $files = [];
205 try {
206
207 if (!is_readable($path) || is_link($path)) return $files;
208 foreach (new \DirectoryIterator($path) as $fileInfo) {
209
210 if (!is_readable($fileInfo->getPathname())) continue;
211 if ($fileInfo->isDot()) continue;
212 if ($fileInfo->isLink()) continue;
213 if (!$fileInfo->isDir()) {
214
215 try {
216 $files[] = $fileInfo->getSize();
217 } catch (\Exception $e) {
218 Logger::debug('Failed to check file: ' . $fileInfo->getFilename());
219 } catch (\Throwable $e) {
220 Logger::debug('Failed to check file: ' . $fileInfo->getFilename());
221 }
222
223 } else {
224
225 $dirName = $fileInfo->getFilename();
226 $dirPath = $fileInfo->getPath();
227 if (in_array($dirPath, $ignored)) {
228 Logger::debug('Dodging ' . $dirName);
229 continue;
230 }
231
232 $files[] = [];
233 $index = sizeof($files) - 1;
234 $newBase = $path . DIRECTORY_SEPARATOR . $dirName;
235 if ($newBase == $bm) continue;
236 $files[$index] = BMI_FileScanner::scanDirectorySizeOnlyAndIgnoreDirOnly($newBase, $ignored, $bm);
237 array_unshift($files[$index], $dirName);
238
239 }
240
241 }
242
243 } catch (\Exception $e) {
244 Logger::log($e->getMessage());
245 return $files;
246 } catch (\Throwable $e) {
247 Logger::log($e->getMessage());
248 return $files;
249 }
250
251 return $files;
252
253 }
254
255 public static function scanDirectoryNameOnly($path) {
256
257 $files = [];
258
259 if (!is_readable($path) || is_link($path)) return $files;
260 foreach (new \DirectoryIterator($path) as $fileInfo) {
261
262 if (!is_readable($fileInfo->getPathname())) continue;
263 if ($fileInfo->isDot()) continue;
264 if ($fileInfo->isLink()) continue;
265 if (!$fileInfo->isDir()) {
266
267 if (!$fileInfo->isLink()) {
268 $files[] = $fileInfo->getFilename();
269 }
270
271 } else {
272
273 $files[] = [];
274 $index = sizeof($files) - 1;
275 $dirName = $fileInfo->getFilename();
276 $newBase = $path . DIRECTORY_SEPARATOR . $dirName;
277 $files[$index] = BMI_FileScanner::scanDirectoryNameOnly($newBase);
278 array_unshift($files[$index], $dirName);
279
280 }
281
282 }
283
284 return $files;
285
286 }
287
288 public static function scanDirectoryNameOnlyAndIgnore($path, $ignored = []) {
289
290 $files = [];
291
292 if (!is_readable($path) || is_link($path)) return $files;
293 foreach (new \DirectoryIterator($path) as $fileInfo) {
294
295 if (!is_readable($fileInfo->getPathname())) continue;
296 if ($fileInfo->isDot()) continue;
297 if ($fileInfo->isLink()) continue;
298 if (!$fileInfo->isDir()) {
299
300 if (!$fileInfo->isLink()) {
301 $files[] = $fileInfo->getFilename();
302 }
303
304 } else {
305
306 $dirName = $fileInfo->getFilename();
307 if (in_array($dirName, $ignored)) {
308 Logger::debug('Dodging ' . $dirName);
309 continue;
310 }
311
312 $files[] = [];
313 $index = sizeof($files) - 1;
314 $newBase = $path . DIRECTORY_SEPARATOR . $dirName;
315 $files[$index] = BMI_FileScanner::scanDirectoryNameOnlyAndIgnore($newBase, $ignored);
316 array_unshift($files[$index], $dirName);
317
318 }
319
320 }
321
322 return $files;
323
324 }
325
326 public static function scanDirectoryNameOnlyAndIgnoreFBC($path, $ignored_folders = [], $ignored_paths = []) {
327
328 $files = [];
329
330 if (!is_readable($path) || is_link($path)) return $files;
331 foreach (new \DirectoryIterator($path) as $fileInfo) {
332
333 if (!is_readable($fileInfo->getPathname())) continue;
334 if ($fileInfo->isDot()) continue;
335 if ($fileInfo->isLink()) continue;
336 if (!$fileInfo->isDir()) {
337
338 $files[] = $fileInfo->getFilename() . ',' . $fileInfo->getSize();
339
340 } else {
341
342 $dirName = $fileInfo->getFilename();
343 if (BMI_FileScanner::equalFolder($dirName, $ignored_folders)) {
344 Logger::debug('Dodging folder ' . $dirName);
345 continue;
346 }
347
348 $files[] = [];
349 $index = sizeof($files) - 1;
350 $newBase = $path . DIRECTORY_SEPARATOR . $dirName;
351 if (in_array($newBase, $ignored_paths)) {
352 Logger::debug('Dodging path ' . $newBase);
353 continue;
354 }
355
356 $files[$index] = BMI_FileScanner::scanDirectoryNameOnlyAndIgnoreFBC($newBase, $ignored_folders, $ignored_paths);
357 array_unshift($files[$index], $dirName);
358
359 }
360
361 }
362
363 return $files;
364
365 }
366
367 public static function getSizeOfFileList($fileList) {
368
369 $size = 0;
370 for ($i = 0; $i < sizeOf($fileList); $i++) {
371
372 if ($i == 0) continue;
373 if (!is_array($fileList[$i])) {
374
375 $size += intval($fileList[$i]);
376
377 } else {
378
379 $size += BMI_FileScanner::getSizeOfFileList($fileList[$i]);
380
381 }
382
383 }
384 return $size;
385
386 }
387
388 public static function getFileFullPaths($base, $fileList) {
389
390 $paths = []; $merge = [];
391 for ($i = 0; $i < sizeOf($fileList); $i++) {
392
393 if ($i == 0) {
394
395 $base = $base . DIRECTORY_SEPARATOR . $fileList[$i];
396 continue;
397
398 }
399
400 if (!is_array($fileList[$i])) {
401
402 $paths[] = $base . DIRECTORY_SEPARATOR . $fileList[$i];
403
404 } else {
405
406 $merge[] = BMI_FileScanner::getFileFullPaths($base, $fileList[$i]);
407
408 }
409
410 }
411
412 $paths = array_merge($paths, ...$merge);
413 return $paths;
414
415 }
416
417 public static function scanFiles($path, $bm) {
418
419 // Get TOP Dir name
420 $z = explode(DIRECTORY_SEPARATOR, $path);
421 $z = $z[sizeof($z) - 1];
422
423 // Scan Directory
424 $x = BMI_FileScanner::scanDirectorySizeOnly($path, $bm);
425
426 // Push TOP Lever (root) Directory
427 array_unshift($x, $z);
428
429 // Calculate size
430 $y = BMI_FileScanner::getSizeOfFileList($x);
431
432 // Return size in Bytes
433 return $y;
434
435 }
436
437 public static function scanFilesGetNamesWithIgnore($path, $ignored = []) {
438
439 // Require Zipper
440 require_once BMI_INCLUDES . '/zipper/zipping.php';
441
442 // Get TOP Dir name
443 $z = explode(DIRECTORY_SEPARATOR, $path);
444 $z = $z[sizeof($z) - 1];
445
446 // Scan Directory
447 $x = BMI_FileScanner::scanDirectoryNameOnlyAndIgnore($path, $ignored);
448
449 // Push TOP Lever (root) Directory
450 array_unshift($x, $z);
451
452 // Parse Output to Array of Full Paths
453 $path = substr($path, 0, -(strlen($z) + 1));
454 $y = BMI_FileScanner::getFileFullPaths($path, $x);
455
456 // Return array of Full Paths
457 return $y;
458
459 }
460
461 public static function scanFilesGetNamesWithIgnoreFBC($path, $ignored = [], $ignored_paths = []) {
462
463 // Require Zipper
464 require_once BMI_INCLUDES . '/zipper/zipping.php';
465
466 // Exclude paths which won't exist in current $path
467 $max = sizeof($ignored_paths);
468 for ($i = 0; $i < $max; ++$i) {
469
470 if (strpos($ignored_paths[$i], $path) === false) {
471 array_splice($ignored_paths, $i, 1);
472 $max--; $i--;
473 }
474
475 }
476
477 // Get TOP Dir name
478 $z = explode(DIRECTORY_SEPARATOR, $path);
479 $z = $z[sizeof($z) - 1];
480
481 // Scan Directory
482 $x = BMI_FileScanner::scanDirectoryNameOnlyAndIgnoreFBC($path, $ignored, $ignored_paths);
483
484 // Push TOP Lever (root) Directory
485 array_unshift($x, $z);
486
487 // Parse Output to Array of Full Paths
488 $path = substr($path, 0, -(strlen($z) + 1));
489 $y = BMI_FileScanner::getFileFullPaths($path, $x);
490
491 // Return array of Full Paths
492 return $y;
493
494 }
495
496 public static function scanFilesWithIgnore($path, $ignored, $bm) {
497
498 // Get TOP Dir name
499 $z = explode(DIRECTORY_SEPARATOR, $path);
500 $z = $z[sizeof($z) - 1];
501
502 // Scan Directory
503 $x = BMI_FileScanner::scanDirectorySizeOnlyAndIgnore($path, $ignored, $bm);
504
505 // Push TOP Lever (root) Directory
506 array_unshift($x, $z);
507
508 // Calculate size
509 $y = BMI_FileScanner::getSizeOfFileList($x);
510
511 // Return size in Bytes
512 return $y;
513
514 }
515
516 public static function fileTooLarge($size, $max) {
517
518 if ($size > $max) return true;
519 else return false;
520
521 }
522
523 }
524