PluginProbe ʕ •ᴥ•ʔ
Backup Migration / 1.3.0
Backup Migration v1.3.0
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
511 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 ($fileInfo->isDot()) continue;
93 if ($fileInfo->isLink()) continue;
94 if (!$fileInfo->isDir()) {
95
96 try {
97 $files[] = $fileInfo->getFilename() . DIRECTORY_SEPARATOR . $fileInfo->getSize();
98 } catch (\Exception $e) {
99 Logger::debug('Failed to check file: ' . $fileInfo->getFilename());
100 }
101
102 } else {
103
104 $files[] = [];
105 $index = sizeof($files) - 1;
106 $dirName = $fileInfo->getFilename();
107 $newBase = $path . DIRECTORY_SEPARATOR . $dirName;
108 $files[$index] = BMI_FileScanner::scanDirectory($newBase);
109 array_unshift($files[$index], $dirName);
110
111 }
112
113 }
114
115 return $files;
116
117 }
118
119 public static function scanDirectorySizeOnly($path, $bm) {
120
121 $files = [];
122 if (!is_readable($path) || is_link($path)) return $files;
123 foreach (new \DirectoryIterator($path) as $fileInfo) {
124
125 if ($fileInfo->isDot()) continue;
126 if ($fileInfo->isLink()) continue;
127 if (!$fileInfo->isDir()) {
128
129 try {
130 $files[] = $fileInfo->getSize();
131 } catch (\Exception $e) {
132 Logger::debug('Failed to check file: ' . $fileInfo->getFilename());
133 }
134
135 } else {
136
137 $files[] = [];
138 $index = sizeof($files) - 1;
139 $dirName = $fileInfo->getFilename();
140 $newBase = $path . DIRECTORY_SEPARATOR . $dirName;
141 if ($newBase == $bm) continue;
142 $files[$index] = BMI_FileScanner::scanDirectorySizeOnly($newBase, $bm);
143 array_unshift($files[$index], $dirName);
144
145 }
146
147 }
148
149 return $files;
150
151 }
152
153 public static function scanDirectorySizeOnlyAndIgnore($path, $ignored = [], $bm = '') {
154
155 $files = [];
156 if (!is_readable($path) || is_link($path)) return $files;
157 foreach (new \DirectoryIterator($path) as $fileInfo) {
158
159 if ($fileInfo->isDot()) continue;
160 if ($fileInfo->isLink()) continue;
161 if (!$fileInfo->isDir()) {
162
163 try {
164 $files[] = $fileInfo->getSize();
165 } catch (\Exception $e) {
166 Logger::debug('Failed to check file: ' . $fileInfo->getFilename());
167 }
168
169 } else {
170
171 $dirPath = $fileInfo->getPath();
172 $dirName = $fileInfo->getFilename();
173 if (in_array($dirName, $ignored) || in_array($dirPath, $ignored)) {
174 Logger::debug('Dodging ' . $dirName);
175 continue;
176 }
177
178 $files[] = [];
179 $index = sizeof($files) - 1;
180 $newBase = $path . DIRECTORY_SEPARATOR . $dirName;
181 if ($newBase == $bm) continue;
182 $files[$index] = BMI_FileScanner::scanDirectorySizeOnlyAndIgnoreDirOnly($newBase, $ignored, $bm);
183 array_unshift($files[$index], $dirName);
184
185 }
186
187 }
188
189 return $files;
190
191 }
192
193 public static function scanDirectorySizeOnlyAndIgnoreDirOnly($path, $ignored = [], $bm = '') {
194
195 $files = [];
196 try {
197
198 if (!is_readable($path) || is_link($path)) return $files;
199 foreach (new \DirectoryIterator($path) as $fileInfo) {
200
201 if ($fileInfo->isDot()) continue;
202 if ($fileInfo->isLink()) continue;
203 if (!$fileInfo->isDir()) {
204
205 try {
206 $files[] = $fileInfo->getSize();
207 } catch (\Exception $e) {
208 Logger::debug('Failed to check file: ' . $fileInfo->getFilename());
209 }
210
211 } else {
212
213 $dirName = $fileInfo->getFilename();
214 $dirPath = $fileInfo->getPath();
215 if (in_array($dirPath, $ignored)) {
216 Logger::debug('Dodging ' . $dirName);
217 continue;
218 }
219
220 $files[] = [];
221 $index = sizeof($files) - 1;
222 $newBase = $path . DIRECTORY_SEPARATOR . $dirName;
223 if ($newBase == $bm) continue;
224 $files[$index] = BMI_FileScanner::scanDirectorySizeOnlyAndIgnoreDirOnly($newBase, $ignored, $bm);
225 array_unshift($files[$index], $dirName);
226
227 }
228
229 }
230
231 } catch (\Exception $e) {
232 Logger::log($e->getMessage());
233 return $files;
234 } catch (\Throwable $e) {
235 Logger::log($e->getMessage());
236 return $files;
237 }
238
239 return $files;
240
241 }
242
243 public static function scanDirectoryNameOnly($path) {
244
245 $files = [];
246
247 if (!is_readable($path) || is_link($path)) return $files;
248 foreach (new \DirectoryIterator($path) as $fileInfo) {
249
250 if ($fileInfo->isDot()) continue;
251 if ($fileInfo->isLink()) continue;
252 if (!$fileInfo->isDir()) {
253
254 if (!$fileInfo->isLink()) {
255 $files[] = $fileInfo->getFilename();
256 }
257
258 } else {
259
260 $files[] = [];
261 $index = sizeof($files) - 1;
262 $dirName = $fileInfo->getFilename();
263 $newBase = $path . DIRECTORY_SEPARATOR . $dirName;
264 $files[$index] = BMI_FileScanner::scanDirectoryNameOnly($newBase);
265 array_unshift($files[$index], $dirName);
266
267 }
268
269 }
270
271 return $files;
272
273 }
274
275 public static function scanDirectoryNameOnlyAndIgnore($path, $ignored = []) {
276
277 $files = [];
278
279 if (!is_readable($path) || is_link($path)) return $files;
280 foreach (new \DirectoryIterator($path) as $fileInfo) {
281
282 if ($fileInfo->isDot()) continue;
283 if ($fileInfo->isLink()) continue;
284 if (!$fileInfo->isDir()) {
285
286 if (!$fileInfo->isLink()) {
287 $files[] = $fileInfo->getFilename();
288 }
289
290 } else {
291
292 $dirName = $fileInfo->getFilename();
293 if (in_array($dirName, $ignored)) {
294 Logger::debug('Dodging ' . $dirName);
295 continue;
296 }
297
298 $files[] = [];
299 $index = sizeof($files) - 1;
300 $newBase = $path . DIRECTORY_SEPARATOR . $dirName;
301 $files[$index] = BMI_FileScanner::scanDirectoryNameOnlyAndIgnore($newBase, $ignored);
302 array_unshift($files[$index], $dirName);
303
304 }
305
306 }
307
308 return $files;
309
310 }
311
312 public static function scanDirectoryNameOnlyAndIgnoreFBC($path, $ignored_folders = [], $ignored_paths = []) {
313
314 $files = [];
315
316 if (!is_readable($path) || is_link($path)) return $files;
317 foreach (new \DirectoryIterator($path) as $fileInfo) {
318
319 if ($fileInfo->isDot()) continue;
320 if ($fileInfo->isLink()) continue;
321 if (!$fileInfo->isDir()) {
322
323 if (!$fileInfo->isLink()) {
324 $files[] = $fileInfo->getFilename() . ',' . $fileInfo->getSize();
325 }
326
327 } else {
328
329 $dirName = $fileInfo->getFilename();
330 if (BMI_FileScanner::equalFolder($dirName, $ignored_folders)) {
331 Logger::debug('Dodging folder ' . $dirName);
332 continue;
333 }
334
335 $files[] = [];
336 $index = sizeof($files) - 1;
337 $newBase = $path . DIRECTORY_SEPARATOR . $dirName;
338 if (in_array($newBase, $ignored_paths)) {
339 Logger::debug('Dodging path ' . $newBase);
340 continue;
341 }
342
343 $files[$index] = BMI_FileScanner::scanDirectoryNameOnlyAndIgnoreFBC($newBase, $ignored_folders, $ignored_paths);
344 array_unshift($files[$index], $dirName);
345
346 }
347
348 }
349
350 return $files;
351
352 }
353
354 public static function getSizeOfFileList($fileList) {
355
356 $size = 0;
357 for ($i = 0; $i < sizeOf($fileList); $i++) {
358
359 if ($i == 0) continue;
360 if (!is_array($fileList[$i])) {
361
362 $size += intval($fileList[$i]);
363
364 } else {
365
366 $size += BMI_FileScanner::getSizeOfFileList($fileList[$i]);
367
368 }
369
370 }
371 return $size;
372
373 }
374
375 public static function getFileFullPaths($base, $fileList) {
376
377 $paths = []; $merge = [];
378 for ($i = 0; $i < sizeOf($fileList); $i++) {
379
380 if ($i == 0) {
381
382 $base = $base . DIRECTORY_SEPARATOR . $fileList[$i];
383 continue;
384
385 }
386
387 if (!is_array($fileList[$i])) {
388
389 $paths[] = $base . DIRECTORY_SEPARATOR . $fileList[$i];
390
391 } else {
392
393 $merge[] = BMI_FileScanner::getFileFullPaths($base, $fileList[$i]);
394
395 }
396
397 }
398
399 $paths = array_merge($paths, ...$merge);
400 return $paths;
401
402 }
403
404 public static function scanFiles($path, $bm) {
405
406 // Get TOP Dir name
407 $z = explode(DIRECTORY_SEPARATOR, $path);
408 $z = $z[sizeof($z) - 1];
409
410 // Scan Directory
411 $x = BMI_FileScanner::scanDirectorySizeOnly($path, $bm);
412
413 // Push TOP Lever (root) Directory
414 array_unshift($x, $z);
415
416 // Calculate size
417 $y = BMI_FileScanner::getSizeOfFileList($x);
418
419 // Return size in Bytes
420 return $y;
421
422 }
423
424 public static function scanFilesGetNamesWithIgnore($path, $ignored = []) {
425
426 // Require Zipper
427 require_once BMI_INCLUDES . '/zipper/zipping.php';
428
429 // Get TOP Dir name
430 $z = explode(DIRECTORY_SEPARATOR, $path);
431 $z = $z[sizeof($z) - 1];
432
433 // Scan Directory
434 $x = BMI_FileScanner::scanDirectoryNameOnlyAndIgnore($path, $ignored);
435
436 // Push TOP Lever (root) Directory
437 array_unshift($x, $z);
438
439 // Parse Output to Array of Full Paths
440 $path = substr($path, 0, -(strlen($z) + 1));
441 $y = BMI_FileScanner::getFileFullPaths($path, $x);
442
443 // Return array of Full Paths
444 return $y;
445
446 }
447
448 public static function scanFilesGetNamesWithIgnoreFBC($path, $ignored = [], $ignored_paths = []) {
449
450 // Require Zipper
451 require_once BMI_INCLUDES . '/zipper/zipping.php';
452
453 // Exclude paths which won't exist in current $path
454 $max = sizeof($ignored_paths);
455 for ($i = 0; $i < $max; ++$i) {
456
457 if (strpos($ignored_paths[$i], $path) === false) {
458 array_splice($ignored_paths, $i, 1);
459 $max--; $i--;
460 }
461
462 }
463
464 // Get TOP Dir name
465 $z = explode(DIRECTORY_SEPARATOR, $path);
466 $z = $z[sizeof($z) - 1];
467
468 // Scan Directory
469 $x = BMI_FileScanner::scanDirectoryNameOnlyAndIgnoreFBC($path, $ignored, $ignored_paths);
470
471 // Push TOP Lever (root) Directory
472 array_unshift($x, $z);
473
474 // Parse Output to Array of Full Paths
475 $path = substr($path, 0, -(strlen($z) + 1));
476 $y = BMI_FileScanner::getFileFullPaths($path, $x);
477
478 // Return array of Full Paths
479 return $y;
480
481 }
482
483 public static function scanFilesWithIgnore($path, $ignored, $bm) {
484
485 // Get TOP Dir name
486 $z = explode(DIRECTORY_SEPARATOR, $path);
487 $z = $z[sizeof($z) - 1];
488
489 // Scan Directory
490 $x = BMI_FileScanner::scanDirectorySizeOnlyAndIgnore($path, $ignored, $bm);
491
492 // Push TOP Lever (root) Directory
493 array_unshift($x, $z);
494
495 // Calculate size
496 $y = BMI_FileScanner::getSizeOfFileList($x);
497
498 // Return size in Bytes
499 return $y;
500
501 }
502
503 public static function fileTooLarge($size, $max) {
504
505 if ($size > $max) return true;
506 else return false;
507
508 }
509
510 }
511