Subversion Repositories yt_downloader

Rev

Rev 14 | Details | Compare with Previous | Last modification | View Log | RSS feed

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