Rev 4 | Rev 14 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4 | Rev 5 | ||
---|---|---|---|
Line 16... | Line 16... | ||
16 | See the License for the specific language governing permissions and |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
17 | limitations under the License. |
18 | */ |
18 | */ |
19 | 19 | ||
20 | // TODO: make use of STDERR and return different exit codes |
20 | // TODO: make use of STDERR and return different exit codes |
- | 21 | // TODO: On Windows file systems, accept file names case insensitively |
|
- | 22 | ||
- | 23 | function utf8_normalize($str) { |
|
- | 24 | // This helps to handle decomposite Unicode endpoints (E.g. German Umlauts have different representations) |
|
- | 25 | // Requires php-intl |
|
- | 26 | if (!class_exists('Normalizer')) return $str; |
|
- | 27 | return Normalizer::normalize($str); |
|
- | 28 | } |
|
- | 29 | ||
- | 30 | function convertToUTF8($str) { |
|
- | 31 | $enc = mb_detect_encoding($str); |
|
- | 32 | if ($enc && $enc != 'UTF-8') { |
|
- | 33 | return iconv($enc, 'UTF-8', $str); |
|
- | 34 | } else { |
|
- | 35 | return $str; |
|
- | 36 | } |
|
- | 37 | } |
|
21 | 38 | ||
22 | function testmd5($file) { |
39 | function testmd5($file) { |
23 | // TODO: warn if an entry is multiple times (with different checksums) in a single file |
40 | // TODO: warn if an entry is multiple times (with different checksums) in a single file |
24 | if (!file_exists($file)) { |
41 | if (!file_exists($file)) { |
25 | echo "ERROR: File $file does not exist.\n"; |
42 | echo "ERROR: File $file does not exist.\n"; |
Line 34... | Line 51... | ||
34 | $tmp = 0; |
51 | $tmp = 0; |
35 | $line = str_replace("\xEF\xBB\xBF",'',$line,$tmp); |
52 | $line = str_replace("\xEF\xBB\xBF",'',$line,$tmp); |
36 | if ($tmp > 0) $force_utf8 = true; |
53 | if ($tmp > 0) $force_utf8 = true; |
37 | $is_first_line = false; |
54 | $is_first_line = false; |
38 | } |
55 | } |
39 | $is_ansi = strstr(utf8_decode($line),'?') !== false; // Attention: This assumes that '?' is not part of the line! |
56 | if (!$force_utf8) $line = convertToUTF8($line); |
- | 57 | ||
40 | if (!$force_utf8 && $is_ansi) $line = utf8_encode($line); |
58 | if (substr(trim($line),0,1) == ';') continue; |
41 | 59 | ||
42 | $line = trim($line); |
60 | $line = trim($line); |
43 | if ($line == '') continue; |
61 | if ($line == '') continue; |
44 | $line = str_replace('*', ' ', $line); |
62 | $line = str_replace('*', ' ', $line); |
45 | $line = str_replace("\t", ' ', $line); |
63 | $line = str_replace("\t", ' ', $line); |
Line 47... | Line 65... | ||
47 | $origname = dirname($file) . '/' . trim($origname); |
65 | $origname = dirname($file) . '/' . trim($origname); |
48 | $checksum = trim($checksum); |
66 | $checksum = trim($checksum); |
49 | if (!file_exists($origname)) { |
67 | if (!file_exists($origname)) { |
50 | echo "WARNING: File vanished : $origname\n"; |
68 | echo "WARNING: File vanished : $origname\n"; |
51 | } else { |
69 | } else { |
- | 70 | if (is_file($origname)) { |
|
52 | $checksum2 = md5_file($origname); |
71 | $checksum2 = md5_file($origname); |
53 | if (strtolower($checksum) != strtolower($checksum2)) { |
72 | if (strtolower($checksum) != strtolower($checksum2)) { |
54 | echo "CHECKSUM FAIL: $origname (expected $checksum, but is $checksum2)\n"; |
73 | echo "CHECKSUM FAIL: $origname (expected $checksum, but is $checksum2)\n"; |
55 | } else { |
74 | } else { |
56 | global $show_verbose; |
75 | global $show_verbose; |
57 | if ($show_verbose) echo "OK: $origname\n"; |
76 | if ($show_verbose) echo "OK: $origname\n"; |
58 | } |
77 | } |
- | 78 | } else { |
|
- | 79 | // For some reason, some files on a NTFS volume are "FIFO" pipe files?! |
|
- | 80 | echo "Warning: $origname is not a regular file!\n"; |
|
- | 81 | } |
|
- | 82 | } |
|
- | 83 | ||
- | 84 | $origname = utf8_normalize(basename($origname)); |
|
- | 85 | $files_checked[] = dirname($file) . '/' . $origname; |
|
- | 86 | } |
|
- | 87 | ||
- | 88 | // Now check if files have vanished! |
|
- | 89 | $directory = dirname($file); |
|
- | 90 | $sd = @scandir($directory); |
|
- | 91 | if ($sd === false) { |
|
- | 92 | echo "Error: Cannot scan directory $directory\n"; |
|
- | 93 | } else { |
|
- | 94 | foreach ($sd as $file) { |
|
- | 95 | if ($file === '.') continue; |
|
- | 96 | if ($file === '..') continue; |
|
- | 97 | if (strtolower($file) === 'thumbs.db') continue; |
|
- | 98 | if (strtolower(substr($file, -4)) === '.md5') continue; |
|
- | 99 | if (strtolower(substr($file, -4)) === '.sfv') continue; |
|
- | 100 | $fullpath = $directory . '/' . $file; |
|
- | 101 | if (!is_dir($fullpath)) { |
|
- | 102 | $fullpath = utf8_normalize($fullpath); |
|
- | 103 | if (!in_array($fullpath,$files_checked)) { |
|
- | 104 | echo "Warning: File not in SFV checksum file: $fullpath\n"; |
|
- | 105 | } |
|
- | 106 | } |
|
59 | } |
107 | } |
60 | // TODO: Also warn about extra files which are not indexed |
- | |
61 | } |
108 | } |
62 | } |
109 | } |
63 | 110 | ||
64 | function _rec($directory) { |
111 | function _rec($directory) { |
65 | $directory = rtrim($directory, '/\\'); |
112 | $directory = rtrim($directory, '/\\'); |