|
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) { |
|
|
|
|
|
|
43
|
|
|
$files = []; |
|
44
|
|
|
$iterator = new RecursiveIteratorIterator( |
|
45
|
|
|
new RecursiveCallbackFilterIterator( |
|
46
|
|
|
new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS), |
|
47
|
|
|
function ($current, $key, $iterator) { |
|
|
|
|
|
|
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
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.