PluginProbe ʕ •ᴥ•ʔ
Backup Migration / 1.3.5
Backup Migration v1.3.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
509 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 $files[] = $fileInfo->getFilename() . ',' . $fileInfo->getSize();
324
325 } else {
326
327 $dirName = $fileInfo->getFilename();
328 if (BMI_FileScanner::equalFolder($dirName, $ignored_folders)) {
329 Logger::debug('Dodging folder ' . $dirName);
330 continue;
331 }
332
333 $files[] = [];
334 $index = sizeof($files) - 1;
335 $newBase = $path . DIRECTORY_SEPARATOR . $dirName;
336 if (in_array($newBase, $ignored_paths)) {
337 Logger::debug('Dodging path ' . $newBase);
338 continue;
339 }
340
341 $files[$index] = BMI_FileScanner::scanDirectoryNameOnlyAndIgnoreFBC($newBase, $ignored_folders, $ignored_paths);
342 array_unshift($files[$index], $dirName);
343
344 }
345
346 }
347
348 return $files;
349
350 }
351
352 public static function getSizeOfFileList($fileList) {
353
354 $size = 0;
355 for ($i = 0; $i < sizeOf($fileList); $i++) {
356
357 if ($i == 0) continue;
358 if (!is_array($fileList[$i])) {
359
360 $size += intval($fileList[$i]);
361
362 } else {
363
364 $size += BMI_FileScanner::getSizeOfFileList($fileList[$i]);
365
366 }
367
368 }
369 return $size;
370
371 }
372
373 public static function getFileFullPaths($base, $fileList) {
374
375 $paths = []; $merge = [];
376 for ($i = 0; $i < sizeOf($fileList); $i++) {
377
378 if ($i == 0) {
379
380 $base = $base . DIRECTORY_SEPARATOR . $fileList[$i];
381 continue;
382
383 }
384
385 if (!is_array($fileList[$i])) {
386
387 $paths[] = $base . DIRECTORY_SEPARATOR . $fileList[$i];
388
389 } else {
390
391 $merge[] = BMI_FileScanner::getFileFullPaths($base, $fileList[$i]);
392
393 }
394
395 }
396
397 $paths = array_merge($paths, ...$merge);
398 return $paths;
399
400 }
401
402 public static function scanFiles($path, $bm) {
403
404 // Get TOP Dir name
405 $z = explode(DIRECTORY_SEPARATOR, $path);
406 $z = $z[sizeof($z) - 1];
407
408 // Scan Directory
409 $x = BMI_FileScanner::scanDirectorySizeOnly($path, $bm);
410
411 // Push TOP Lever (root) Directory
412 array_unshift($x, $z);
413
414 // Calculate size
415 $y = BMI_FileScanner::getSizeOfFileList($x);
416
417 // Return size in Bytes
418 return $y;
419
420 }
421
422 public static function scanFilesGetNamesWithIgnore($path, $ignored = []) {
423
424 // Require Zipper
425 require_once BMI_INCLUDES . '/zipper/zipping.php';
426
427 // Get TOP Dir name
428 $z = explode(DIRECTORY_SEPARATOR, $path);
429 $z = $z[sizeof($z) - 1];
430
431 // Scan Directory
432 $x = BMI_FileScanner::scanDirectoryNameOnlyAndIgnore($path, $ignored);
433
434 // Push TOP Lever (root) Directory
435 array_unshift($x, $z);
436
437 // Parse Output to Array of Full Paths
438 $path = substr($path, 0, -(strlen($z) + 1));
439 $y = BMI_FileScanner::getFileFullPaths($path, $x);
440
441 // Return array of Full Paths
442 return $y;
443
444 }
445
446 public static function scanFilesGetNamesWithIgnoreFBC($path, $ignored = [], $ignored_paths = []) {
447
448 // Require Zipper
449 require_once BMI_INCLUDES . '/zipper/zipping.php';
450
451 // Exclude paths which won't exist in current $path
452 $max = sizeof($ignored_paths);
453 for ($i = 0; $i < $max; ++$i) {
454
455 if (strpos($ignored_paths[$i], $path) === false) {
456 array_splice($ignored_paths, $i, 1);
457 $max--; $i--;
458 }
459
460 }
461
462 // Get TOP Dir name
463 $z = explode(DIRECTORY_SEPARATOR, $path);
464 $z = $z[sizeof($z) - 1];
465
466 // Scan Directory
467 $x = BMI_FileScanner::scanDirectoryNameOnlyAndIgnoreFBC($path, $ignored, $ignored_paths);
468
469 // Push TOP Lever (root) Directory
470 array_unshift($x, $z);
471
472 // Parse Output to Array of Full Paths
473 $path = substr($path, 0, -(strlen($z) + 1));
474 $y = BMI_FileScanner::getFileFullPaths($path, $x);
475
476 // Return array of Full Paths
477 return $y;
478
479 }
480
481 public static function scanFilesWithIgnore($path, $ignored, $bm) {
482
483 // Get TOP Dir name
484 $z = explode(DIRECTORY_SEPARATOR, $path);
485 $z = $z[sizeof($z) - 1];
486
487 // Scan Directory
488 $x = BMI_FileScanner::scanDirectorySizeOnlyAndIgnore($path, $ignored, $bm);
489
490 // Push TOP Lever (root) Directory
491 array_unshift($x, $z);
492
493 // Calculate size
494 $y = BMI_FileScanner::getSizeOfFileList($x);
495
496 // Return size in Bytes
497 return $y;
498
499 }
500
501 public static function fileTooLarge($size, $max) {
502
503 if ($size > $max) return true;
504 else return false;
505
506 }
507
508 }
509