Passed
Pull Request — master (#4799)
by
unknown
07:22
created

getAllFilesWithMd5()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 21
c 1
b 0
f 1
nc 9
nop 4
dl 0
loc 37
rs 8.6506
1
#!/usr/bin/php
2
<?php
3
4
/**
5
 * task_generate_files_reference.php
6
 * ---
7
 * Helper script to generate a `files_reference.txt` file
8
 * listing all project files and their MD5 hashes.
9
 * 
10
 * Output format:
11
 *     <relative/path/to/file> <md5_hash>
12
 * 
13
 * Example:
14
 *     vendor/autoload.php 1bc29b36f623ba82aaf6724fd3b16718
15
 *
16
 * This is used by the enhanced TeamPass file integrity check.
17
 *
18
 * Usage:
19
 *     php scripts/task_generate_files_reference.php
20
 *
21
 * Configuration:
22
 * - Adjust `$excludeDirs` to skip entire directories.
23
 * - Adjust `$excludeFilePrefixes` to skip specific files.
24
 *
25
 * Intended to be run inside the TeamPass project root. 
26
 * It will output `files_reference.txt` in the same base directory.
27
 *
28
 * ---
29
 * @file      task_generate_files_refrence.php
30
 * @author    Gudmundur Mar Kristjansson ([email protected])
31
 * @license   GPL-3.0
32
 * @see       https://www.teampass.net
33
 */
34
35
$baseDir = realpath(__DIR__ . '/..');
36
$outputFile = $baseDir . '/files_reference.txt';
37
38
// Optionally exclude folders or specific file prefixes
39
$excludeDirs = ['upload', 'files', 'install', '_tools', 'random_compat', 'avatars'];
40
$excludeFilePrefixes = ['csrfp.config.php', 'settings.php', 'version-commit.php', 'phpstan.neon'];
41
42
function getAllFilesWithMd5($dir, $baseDir, $excludeDirs, $excludeFilePrefixes) {
0 ignored issues
show
Unused Code introduced by
The parameter $excludeDirs is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

42
function getAllFilesWithMd5($dir, $baseDir, /** @scrutinizer ignore-unused */ $excludeDirs, $excludeFilePrefixes) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $excludeFilePrefixes is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

42
function getAllFilesWithMd5($dir, $baseDir, $excludeDirs, /** @scrutinizer ignore-unused */ $excludeFilePrefixes) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
    $files = [];
44
    $iterator = new RecursiveIteratorIterator(
45
        new RecursiveCallbackFilterIterator(
46
            new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS),
47
            function ($current, $key, $iterator) {
0 ignored issues
show
Unused Code introduced by
The parameter $iterator is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

47
            function ($current, $key, /** @scrutinizer ignore-unused */ $iterator) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Bug introduced by
function(...) { /* ... */ } of type callable is incompatible with the type string expected by parameter $callback of RecursiveCallbackFilterIterator::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
            /** @scrutinizer ignore-type */ function ($current, $key, $iterator) {
Loading history...
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

47
            function ($current, /** @scrutinizer ignore-unused */ $key, $iterator) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
                return $current->getFilename()[0] !== '.'; // skip dotfiles & dotdirs
49
            }
50
        ),
51
        RecursiveIteratorIterator::SELF_FIRST
52
    );
53
54
    foreach ($iterator as $file) {
55
        if ($file->isFile()) {
56
            $relativePath = str_replace($baseDir . DIRECTORY_SEPARATOR, '', $file->getPathname());
57
            $relativePath = str_replace('\\', '/', $relativePath);
58
59
            // Check for excluded dirs
60
            foreach (explode('/', $relativePath) as $part) {
61
                if (in_array($part, $GLOBALS['excludeDirs'], true)) {
62
                    continue 2;
63
                }
64
            }
65
66
            // Check for excluded prefixes
67
            $filename = basename($relativePath);
68
            foreach ($GLOBALS['excludeFilePrefixes'] as $prefix) {
69
                if (strpos($filename, $prefix) === 0) {
70
                    continue 2;
71
                }
72
            }
73
74
            $md5 = md5_file($file->getPathname());
75
            $files[$relativePath] = $md5;
76
        }
77
    }
78
    return $files;
79
}
80
81
$files = getAllFilesWithMd5($baseDir, $baseDir, $excludeDirs, $excludeFilePrefixes);
82
83
$handle = fopen($outputFile, 'w');
84
foreach ($files as $file => $md5) {
85
    fwrite($handle, $file . ' ' . $md5 . PHP_EOL);
86
}
87
fclose($handle);
88
89
echo "[OK] files_reference.txt generated with " . count($files) . " files.\n";
90