Subversion Repositories yt_downloader

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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