#!/usr/bin/php
<?php
/*
Copyright 2020 Daniel Marschall, ViaThinkSoft
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// TODO: make use of STDERR and return different exit codes
// TODO: On Windows file systems, accept file names case insensitively
function utf8_normalize($str) {
// This helps to handle decomposite Unicode endpoints (E.g. German Umlauts have different representations)
// Requires php-intl
return Normalizer::normalize($str);
}
function convertToUTF8($str) {
if ($enc && $enc != 'UTF-8') {
return iconv($enc, 'UTF-8', $str);
} else {
return $str;
}
}
function testmd5($file) {
// TODO: warn if an entry is multiple times (with different checksums) in a single file
echo "ERROR: File $file does not exist.\n";
return;
}
$is_first_line = true;
$force_utf8 = false;
foreach ($lines as $line) {
if ($is_first_line) {
$tmp = 0;
if ($tmp > 0) $force_utf8 = true;
$is_first_line = false;
}
if (!$force_utf8) $line = convertToUTF8($line);
if ($line == '') continue;
$checksum = trim($checksum);
echo "WARNING: File vanished : $origname\n";
} else {
echo "CHECKSUM FAIL: $origname (expected $checksum, but is $checksum2)\n";
} else {
global $show_verbose;
if ($show_verbose) echo "OK: $origname\n";
}
} else {
// For some reason, some files on a NTFS volume are "FIFO" pipe files?!
echo "Warning: $origname is not a regular file!\n";
}
}
$origname = utf8_normalize
(basename($origname));
$files_checked[] = dirname($file) . '/' . $origname;
}
// Now check if files have vanished!
if ($sd === false) {
echo "Error: Cannot scan directory $directory\n";
} else {
foreach ($sd as $file) {
if ($file === '.') continue;
if ($file === '..') continue;
$fullpath = $directory . '/' . $file;
$fullpath = utf8_normalize($fullpath);
if (!in_array($fullpath,$files_checked)) {
echo "Warning: File not in SFV checksum file: $fullpath\n";
}
}
}
}
}
function _rec($directory) {
$directory = rtrim($directory, '/\\');
exit("Invalid directory path $directory\n");
}
if ($dont_add_files = count(glob("$directory/*.md5")) == 0) {
global $show_verbose;
if ($show_verbose) echo "Directory $directory has no MD5 file. Skipping.\n";
} else {
global $show_verbose;
if ($show_verbose) echo "Check directory $directory\n";
$md5files = glob($directory.'/*.md5');
foreach ($md5files as $md5file) {
testmd5($md5file);
}
}
if ($sd === false) {
echo "Error: Cannot scan directory $directory\n";
return;
}
foreach ($sd as $file) {
if ($file !== '.' && $file !== '..') {
$file = $directory . '/' . $file;
_rec($file);
}
}
}
}
# ---
$show_verbose = false;
$dir = '';
for ($i=1; $i<$argc; $i++) {
if ($argv[$i] == '-v') {
$show_verbose = true;
} else {
$dir = $argv[$i];
}
}
echo "Syntax: $argv[0] [-v] <directory>\n";
}
echo "Directory not found\n";
}
_rec($dir);
if ($show_verbose) echo "Done.\n";