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