Rev 14 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 14 | Rev 15 | ||
---|---|---|---|
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | // ViaThinkSoft YouTube Downloader Util 2.3 |
3 | // ViaThinkSoft YouTube Downloader Util 2.3 |
4 | // Revision: 2022-02-06 |
4 | // Revision: 2022-02-07 |
5 | // Author: Daniel Marschall <www.daniel-marschall.de> |
5 | // Author: Daniel Marschall <www.daniel-marschall.de> |
6 | // Licensed under the terms of the Apache 2.0 License |
6 | // Licensed under the terms of the Apache 2.0 License |
7 | 7 | ||
8 | function cs_get_checksumfilename($file, $type='sfv') { |
8 | function cs_get_checksumfilename($file, $type='sfv') { |
9 | $dir = dirname($file); |
9 | $dir = dirname($file); |
10 | $files = preg_grep('/\.'.preg_quote($type,'/').'$/i', glob($dir.'/*')); |
10 | $files = preg_grep('/\.'.preg_quote($type,'/').'$/i', glob($dir.'/*')); |
11 | 11 | ||
12 | if (count($files) > 0) { |
12 | if (count($files) > 0) { |
13 | $cs_file = array_pop($files); |
13 | $cs_file = array_pop($files); |
14 | } else { |
14 | } else { |
15 | $cs_file = $dir.'/'.basename(dirname($file)).'.'.$type; |
15 | $cs_file = $dir.'/'.basename(dirname($file)).'.'.$type; |
16 | } |
16 | } |
17 | return $cs_file; |
17 | return $cs_file; |
18 | } |
18 | } |
19 | 19 | ||
20 | function cs_add_automatically($file, $type='sfv') { |
20 | function cs_add_automatically($file, $type='sfv') { |
- | 21 | $type = strtolower($type); |
|
21 | $cs_file = cs_get_checksumfilename($file, $type); |
22 | $cs_file = cs_get_checksumfilename($file, $type); |
22 | // echo "Checksum file: $cs_file\n"; |
23 | // echo "Checksum file: $cs_file\n"; |
23 | if (strtolower($type) == 'sfv') { |
24 | if ($type == 'sfv') { |
24 | if (file_exists($cs_file)) { |
25 | if (file_exists($cs_file)) { |
25 | $files = sfv_get_files($cs_file); |
26 | $files = sfv_get_files($cs_file); |
26 | if (in_arrayi(basename($file), $files)) return true; |
27 | if (in_arrayi(basename($file), $files)) return true; |
27 | } else { |
28 | } else { |
28 | file_put_contents($cs_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM |
29 | file_put_contents($cs_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM |
29 | $files = array(); |
30 | $files = array(); |
30 | } |
31 | } |
31 | $hash = crc32_file($file); |
32 | $hash = crc32_file($file); |
32 | if ($hash === false) { |
33 | if ($hash === false) { |
33 | fwrite(STDERR, "Cannot calculate hash of file '$file'\n"); |
34 | fwrite(STDERR, "Cannot calculate hash of file '$file'\n"); |
34 | return false; |
35 | return false; |
35 | } |
36 | } |
36 | $crc32 = strtoupper($hash); |
37 | $crc32 = strtoupper($hash); |
37 | file_put_contents($cs_file, basename($file)." $crc32\r\n", FILE_APPEND); |
38 | file_put_contents($cs_file, basename($file)." $crc32\r\n", FILE_APPEND); |
38 | return true; |
39 | return true; |
39 | } |
40 | } |
40 | else if (strtolower($type) == 'md5') { |
41 | else if ($type == 'md5') { |
41 | if (file_exists($cs_file)) { |
42 | if (file_exists($cs_file)) { |
42 | $files = md5_get_files($cs_file); |
43 | $files = md5_get_files($cs_file); |
43 | if (in_arrayi(basename($file), $files)) return true; |
44 | if (in_arrayi(basename($file), $files)) return true; |
44 | } else { |
45 | } else { |
45 | file_put_contents($cs_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM |
46 | file_put_contents($cs_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM |
46 | $files = array(); |
47 | $files = array(); |
47 | } |
48 | } |
48 | $hash = md5_file($file); |
49 | $hash = md5_file($file); |
49 | if ($hash === false) { |
50 | if ($hash === false) { |
50 | fwrite(STDERR, "Cannot calculate hash of file '$file'\n"); |
51 | fwrite(STDERR, "Cannot calculate hash of file '$file'\n"); |
51 | return false; |
52 | return false; |
52 | } |
53 | } |
53 | $md5 = strtolower($hash); |
54 | $md5 = strtolower($hash); |
54 | file_put_contents($cs_file, "$md5 *".basename($file)."\r\n", FILE_APPEND); |
55 | file_put_contents($cs_file, "$md5 *".basename($file)."\r\n", FILE_APPEND); |
55 | return true; |
56 | return true; |
56 | } |
57 | } |
57 | else if (strtolower($type) == 'none') { |
58 | else if ($type == 'none') { |
58 | return true; |
59 | return true; |
59 | } |
60 | } |
60 | else { |
61 | else { |
61 | fwrite(STDERR, "Unknown checksum type '$type'. Must be SFV, MD5 or None\n"); |
62 | fwrite(STDERR, "Unknown checksum type '$type'. Must be SFV, MD5 or None\n"); |
62 | return false; |
63 | return false; |
63 | } |
64 | } |
64 | } |
65 | } |
65 | 66 | ||
66 | function md5_get_files($filename) { |
67 | function md5_get_files($filename) { |
67 | // Source: https://github.com/danielmarschall/checksum-tools/blob/master/PHP/md5_generate.php |
68 | // Source: https://github.com/danielmarschall/checksum-tools/blob/master/PHP/md5_generate.php |
68 | $out = array(); |
69 | $out = array(); |
69 | $lines = file($filename); |
70 | $lines = file($filename); |
70 | foreach ($lines as $line) { |
71 | foreach ($lines as $line) { |
71 | $line = str_replace("\xEF\xBB\xBF",'',$line); |
72 | $line = str_replace("\xEF\xBB\xBF",'',$line); |
72 | $line = trim($line); |
73 | $line = trim($line); |
73 | if ($line == '') continue; |
74 | if ($line == '') continue; |
74 | $line = str_replace('*', ' ', $line); |
75 | $line = str_replace('*', ' ', $line); |
75 | $line = str_replace("\t", ' ', $line); |
76 | $line = str_replace("\t", ' ', $line); |
76 | list($checksum, $origname) = explode(' ', $line, 2); |
77 | list($checksum, $origname) = explode(' ', $line, 2); |
77 | $origname = dirname($filename) . '/' . trim($origname); |
78 | $origname = dirname($filename) . '/' . trim($origname); |
78 | $checksum = trim($checksum); |
79 | $checksum = trim($checksum); |
79 | $out[] = $origname; |
80 | $out[] = $origname; |
80 | } |
81 | } |
81 | 82 | ||
82 | return $out; |
83 | return $out; |
83 | } |
84 | } |
84 | 85 | ||
85 | function sfv_get_files($filename) { |
86 | function sfv_get_files($filename) { |
86 | // Source: https://github.com/danielmarschall/checksum-tools/blob/master/PHP/sfv_generate.php |
87 | // Source: https://github.com/danielmarschall/checksum-tools/blob/master/PHP/sfv_generate.php |
87 | $out = array(); |
88 | $out = array(); |
88 | $lines = file($filename); |
89 | $lines = file($filename); |
89 | foreach ($lines as $line) { |
90 | foreach ($lines as $line) { |
90 | $line = rtrim($line); |
91 | $line = rtrim($line); |
91 | if ($line == '') continue; |
92 | if ($line == '') continue; |
92 | if (substr($line,0,1) == ';') continue; |
93 | if (substr($line,0,1) == ';') continue; |
93 | $out[] = substr($line, 0, strrpos($line, ' ')); |
94 | $out[] = substr($line, 0, strrpos($line, ' ')); |
94 | 95 | ||
95 | } |
96 | } |
96 | return $out; |
97 | return $out; |
97 | } |
98 | } |
98 | 99 | ||
99 | function swapEndianness($hex) { |
100 | function swapEndianness($hex) { |
100 | // Source: https://github.com/danielmarschall/checksum-tools/blob/master/PHP/sfv_generate.php |
101 | // Source: https://github.com/danielmarschall/checksum-tools/blob/master/PHP/sfv_generate.php |
101 | return implode('', array_reverse(str_split($hex, 2))); |
102 | return implode('', array_reverse(str_split($hex, 2))); |
102 | } |
103 | } |
103 | 104 | ||
104 | function crc32_file($filename, $rawOutput = false) { |
105 | function crc32_file($filename, $rawOutput = false) { |
105 | // Source: https://github.com/danielmarschall/checksum-tools/blob/master/PHP/sfv_generate.php |
106 | // Source: https://github.com/danielmarschall/checksum-tools/blob/master/PHP/sfv_generate.php |
106 | $hash = hash_file('crc32b', $filename, true); |
107 | $hash = hash_file('crc32b', $filename, true); |
107 | if ($hash === false) return false; |
108 | if ($hash === false) return false; |
108 | $out = bin2hex($hash); |
109 | $out = bin2hex($hash); |
109 | if (hash('crc32b', 'TEST') == 'b893eaee') { |
110 | if (hash('crc32b', 'TEST') == 'b893eaee') { |
110 | // hash_file() in PHP 5.2 has the wrong Endianess! |
111 | // hash_file() in PHP 5.2 has the wrong Endianess! |
111 | // https://bugs.php.net/bug.php?id=47467 |
112 | // https://bugs.php.net/bug.php?id=47467 |
112 | $out = swapEndianness($out); |
113 | $out = swapEndianness($out); |
113 | } |
114 | } |
114 | return $out; |
115 | return $out; |
115 | } |
116 | } |
116 | 117 | ||
117 | function in_arrayi($needle, $haystack) { |
118 | function in_arrayi($needle, $haystack) { |
118 | // Source: https://www.php.net/manual/en/function.in-array.php#89256 |
119 | // Source: https://www.php.net/manual/en/function.in-array.php#89256 |
119 | return in_array(strtolower($needle), array_map('strtolower', $haystack)); |
120 | return in_array(strtolower($needle), array_map('strtolower', $haystack)); |
120 | } |
121 | } |
121 | 122 |