PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/public/mp3/wimpy/getid3/getid3.lib.php

https://github.com/noonchild/rsa
PHP | 1323 lines | 1028 code | 173 blank | 122 comment | 247 complexity | 3a262316fae2cdc4afd536abc311f474 MD5 | raw file
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at http://getid3.sourceforge.net //
  5. // or http://www.getid3.org //
  6. /////////////////////////////////////////////////////////////////
  7. // //
  8. // getid3.lib.php - part of getID3() //
  9. // See readme.txt for more details //
  10. // ///
  11. /////////////////////////////////////////////////////////////////
  12. class getid3_lib
  13. {
  14. function PrintHexBytes($string, $hex=true, $spaces=true, $htmlsafe=true) {
  15. $returnstring = '';
  16. for ($i = 0; $i < strlen($string); $i++) {
  17. if ($hex) {
  18. $returnstring .= str_pad(dechex(ord($string{$i})), 2, '0', STR_PAD_LEFT);
  19. } else {
  20. $returnstring .= ' '.(ereg("[\x20-\x7E]", $string{$i}) ? $string{$i} : '¤');
  21. }
  22. if ($spaces) {
  23. $returnstring .= ' ';
  24. }
  25. }
  26. if ($htmlsafe) {
  27. $returnstring = htmlentities($returnstring);
  28. }
  29. return $returnstring;
  30. }
  31. function SafeStripSlashes($text) {
  32. if (get_magic_quotes_gpc()) {
  33. return stripslashes($text);
  34. }
  35. return $text;
  36. }
  37. function trunc($floatnumber) {
  38. // truncates a floating-point number at the decimal point
  39. // returns int (if possible, otherwise float)
  40. if ($floatnumber >= 1) {
  41. $truncatednumber = floor($floatnumber);
  42. } elseif ($floatnumber <= -1) {
  43. $truncatednumber = ceil($floatnumber);
  44. } else {
  45. $truncatednumber = 0;
  46. }
  47. if ($truncatednumber <= 1073741824) { // 2^30
  48. $truncatednumber = (int) $truncatednumber;
  49. }
  50. return $truncatednumber;
  51. }
  52. function CastAsInt($floatnum) {
  53. // convert to float if not already
  54. $floatnum = (float) $floatnum;
  55. // convert a float to type int, only if possible
  56. if (getid3_lib::trunc($floatnum) == $floatnum) {
  57. // it's not floating point
  58. if ($floatnum <= 1073741824) { // 2^30
  59. // it's within int range
  60. $floatnum = (int) $floatnum;
  61. }
  62. }
  63. return $floatnum;
  64. }
  65. function DecimalBinary2Float($binarynumerator) {
  66. $numerator = getid3_lib::Bin2Dec($binarynumerator);
  67. $denominator = getid3_lib::Bin2Dec('1'.str_repeat('0', strlen($binarynumerator)));
  68. return ($numerator / $denominator);
  69. }
  70. function NormalizeBinaryPoint($binarypointnumber, $maxbits=52) {
  71. // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
  72. if (strpos($binarypointnumber, '.') === false) {
  73. $binarypointnumber = '0.'.$binarypointnumber;
  74. } elseif ($binarypointnumber{0} == '.') {
  75. $binarypointnumber = '0'.$binarypointnumber;
  76. }
  77. $exponent = 0;
  78. while (($binarypointnumber{0} != '1') || (substr($binarypointnumber, 1, 1) != '.')) {
  79. if (substr($binarypointnumber, 1, 1) == '.') {
  80. $exponent--;
  81. $binarypointnumber = substr($binarypointnumber, 2, 1).'.'.substr($binarypointnumber, 3);
  82. } else {
  83. $pointpos = strpos($binarypointnumber, '.');
  84. $exponent += ($pointpos - 1);
  85. $binarypointnumber = str_replace('.', '', $binarypointnumber);
  86. $binarypointnumber = $binarypointnumber{0}.'.'.substr($binarypointnumber, 1);
  87. }
  88. }
  89. $binarypointnumber = str_pad(substr($binarypointnumber, 0, $maxbits + 2), $maxbits + 2, '0', STR_PAD_RIGHT);
  90. return array('normalized'=>$binarypointnumber, 'exponent'=>(int) $exponent);
  91. }
  92. function Float2BinaryDecimal($floatvalue) {
  93. // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
  94. $maxbits = 128; // to how many bits of precision should the calculations be taken?
  95. $intpart = getid3_lib::trunc($floatvalue);
  96. $floatpart = abs($floatvalue - $intpart);
  97. $pointbitstring = '';
  98. while (($floatpart != 0) && (strlen($pointbitstring) < $maxbits)) {
  99. $floatpart *= 2;
  100. $pointbitstring .= (string) getid3_lib::trunc($floatpart);
  101. $floatpart -= getid3_lib::trunc($floatpart);
  102. }
  103. $binarypointnumber = decbin($intpart).'.'.$pointbitstring;
  104. return $binarypointnumber;
  105. }
  106. function Float2String($floatvalue, $bits) {
  107. // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee-expl.html
  108. switch ($bits) {
  109. case 32:
  110. $exponentbits = 8;
  111. $fractionbits = 23;
  112. break;
  113. case 64:
  114. $exponentbits = 11;
  115. $fractionbits = 52;
  116. break;
  117. default:
  118. return false;
  119. break;
  120. }
  121. if ($floatvalue >= 0) {
  122. $signbit = '0';
  123. } else {
  124. $signbit = '1';
  125. }
  126. $normalizedbinary = getid3_lib::NormalizeBinaryPoint(getid3_lib::Float2BinaryDecimal($floatvalue), $fractionbits);
  127. $biasedexponent = pow(2, $exponentbits - 1) - 1 + $normalizedbinary['exponent']; // (127 or 1023) +/- exponent
  128. $exponentbitstring = str_pad(decbin($biasedexponent), $exponentbits, '0', STR_PAD_LEFT);
  129. $fractionbitstring = str_pad(substr($normalizedbinary['normalized'], 2), $fractionbits, '0', STR_PAD_RIGHT);
  130. return getid3_lib::BigEndian2String(getid3_lib::Bin2Dec($signbit.$exponentbitstring.$fractionbitstring), $bits % 8, false);
  131. }
  132. function LittleEndian2Float($byteword) {
  133. return getid3_lib::BigEndian2Float(strrev($byteword));
  134. }
  135. function BigEndian2Float($byteword) {
  136. // ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic
  137. // http://www.psc.edu/general/software/packages/ieee/ieee.html
  138. // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html
  139. $bitword = getid3_lib::BigEndian2Bin($byteword);
  140. $signbit = $bitword{0};
  141. switch (strlen($byteword) * 8) {
  142. case 32:
  143. $exponentbits = 8;
  144. $fractionbits = 23;
  145. break;
  146. case 64:
  147. $exponentbits = 11;
  148. $fractionbits = 52;
  149. break;
  150. case 80:
  151. // 80-bit Apple SANE format
  152. // http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/
  153. $exponentstring = substr($bitword, 1, 15);
  154. $isnormalized = intval($bitword{16});
  155. $fractionstring = substr($bitword, 17, 63);
  156. $exponent = pow(2, getid3_lib::Bin2Dec($exponentstring) - 16383);
  157. $fraction = $isnormalized + getid3_lib::DecimalBinary2Float($fractionstring);
  158. $floatvalue = $exponent * $fraction;
  159. if ($signbit == '1') {
  160. $floatvalue *= -1;
  161. }
  162. return $floatvalue;
  163. break;
  164. default:
  165. return false;
  166. break;
  167. }
  168. $exponentstring = substr($bitword, 1, $exponentbits);
  169. $fractionstring = substr($bitword, $exponentbits + 1, $fractionbits);
  170. $exponent = getid3_lib::Bin2Dec($exponentstring);
  171. $fraction = getid3_lib::Bin2Dec($fractionstring);
  172. if (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction != 0)) {
  173. // Not a Number
  174. $floatvalue = false;
  175. } elseif (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction == 0)) {
  176. if ($signbit == '1') {
  177. $floatvalue = '-infinity';
  178. } else {
  179. $floatvalue = '+infinity';
  180. }
  181. } elseif (($exponent == 0) && ($fraction == 0)) {
  182. if ($signbit == '1') {
  183. $floatvalue = -0;
  184. } else {
  185. $floatvalue = 0;
  186. }
  187. $floatvalue = ($signbit ? 0 : -0);
  188. } elseif (($exponent == 0) && ($fraction != 0)) {
  189. // These are 'unnormalized' values
  190. $floatvalue = pow(2, (-1 * (pow(2, $exponentbits - 1) - 2))) * getid3_lib::DecimalBinary2Float($fractionstring);
  191. if ($signbit == '1') {
  192. $floatvalue *= -1;
  193. }
  194. } elseif ($exponent != 0) {
  195. $floatvalue = pow(2, ($exponent - (pow(2, $exponentbits - 1) - 1))) * (1 + getid3_lib::DecimalBinary2Float($fractionstring));
  196. if ($signbit == '1') {
  197. $floatvalue *= -1;
  198. }
  199. }
  200. return (float) $floatvalue;
  201. }
  202. function BigEndian2Int($byteword, $synchsafe=false, $signed=false) {
  203. $intvalue = 0;
  204. $bytewordlen = strlen($byteword);
  205. for ($i = 0; $i < $bytewordlen; $i++) {
  206. if ($synchsafe) { // disregard MSB, effectively 7-bit bytes
  207. $intvalue = $intvalue | (ord($byteword{$i}) & 0x7F) << (($bytewordlen - 1 - $i) * 7);
  208. } else {
  209. $intvalue += ord($byteword{$i}) * pow(256, ($bytewordlen - 1 - $i));
  210. }
  211. }
  212. if ($signed && !$synchsafe) {
  213. // synchsafe ints are not allowed to be signed
  214. switch ($bytewordlen) {
  215. case 1:
  216. case 2:
  217. case 3:
  218. case 4:
  219. $signmaskbit = 0x80 << (8 * ($bytewordlen - 1));
  220. if ($intvalue & $signmaskbit) {
  221. $intvalue = 0 - ($intvalue & ($signmaskbit - 1));
  222. }
  223. break;
  224. default:
  225. die('ERROR: Cannot have signed integers larger than 32-bits in getid3_lib::BigEndian2Int()');
  226. break;
  227. }
  228. }
  229. return getid3_lib::CastAsInt($intvalue);
  230. }
  231. function LittleEndian2Int($byteword, $signed=false) {
  232. return getid3_lib::BigEndian2Int(strrev($byteword), false, $signed);
  233. }
  234. function BigEndian2Bin($byteword) {
  235. $binvalue = '';
  236. $bytewordlen = strlen($byteword);
  237. for ($i = 0; $i < $bytewordlen; $i++) {
  238. $binvalue .= str_pad(decbin(ord($byteword{$i})), 8, '0', STR_PAD_LEFT);
  239. }
  240. return $binvalue;
  241. }
  242. function BigEndian2String($number, $minbytes=1, $synchsafe=false, $signed=false) {
  243. if ($number < 0) {
  244. return false;
  245. }
  246. $maskbyte = (($synchsafe || $signed) ? 0x7F : 0xFF);
  247. $intstring = '';
  248. if ($signed) {
  249. if ($minbytes > 4) {
  250. die('ERROR: Cannot have signed integers larger than 32-bits in getid3_lib::BigEndian2String()');
  251. }
  252. $number = $number & (0x80 << (8 * ($minbytes - 1)));
  253. }
  254. while ($number != 0) {
  255. $quotient = ($number / ($maskbyte + 1));
  256. $intstring = chr(ceil(($quotient - floor($quotient)) * $maskbyte)).$intstring;
  257. $number = floor($quotient);
  258. }
  259. return str_pad($intstring, $minbytes, "\x00", STR_PAD_LEFT);
  260. }
  261. function Dec2Bin($number) {
  262. while ($number >= 256) {
  263. $bytes[] = (($number / 256) - (floor($number / 256))) * 256;
  264. $number = floor($number / 256);
  265. }
  266. $bytes[] = $number;
  267. $binstring = '';
  268. for ($i = 0; $i < count($bytes); $i++) {
  269. $binstring = (($i == count($bytes) - 1) ? decbin($bytes[$i]) : str_pad(decbin($bytes[$i]), 8, '0', STR_PAD_LEFT)).$binstring;
  270. }
  271. return $binstring;
  272. }
  273. function Bin2Dec($binstring, $signed=false) {
  274. $signmult = 1;
  275. if ($signed) {
  276. if ($binstring{0} == '1') {
  277. $signmult = -1;
  278. }
  279. $binstring = substr($binstring, 1);
  280. }
  281. $decvalue = 0;
  282. for ($i = 0; $i < strlen($binstring); $i++) {
  283. $decvalue += ((int) substr($binstring, strlen($binstring) - $i - 1, 1)) * pow(2, $i);
  284. }
  285. return getid3_lib::CastAsInt($decvalue * $signmult);
  286. }
  287. function Bin2String($binstring) {
  288. // return 'hi' for input of '0110100001101001'
  289. $string = '';
  290. $binstringreversed = strrev($binstring);
  291. for ($i = 0; $i < strlen($binstringreversed); $i += 8) {
  292. $string = chr(getid3_lib::Bin2Dec(strrev(substr($binstringreversed, $i, 8)))).$string;
  293. }
  294. return $string;
  295. }
  296. function LittleEndian2String($number, $minbytes=1, $synchsafe=false) {
  297. $intstring = '';
  298. while ($number > 0) {
  299. if ($synchsafe) {
  300. $intstring = $intstring.chr($number & 127);
  301. $number >>= 7;
  302. } else {
  303. $intstring = $intstring.chr($number & 255);
  304. $number >>= 8;
  305. }
  306. }
  307. return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
  308. }
  309. function array_merge_clobber($array1, $array2) {
  310. // written by kcØhireability*com
  311. // taken from http://www.php.net/manual/en/function.array-merge-recursive.php
  312. if (!is_array($array1) || !is_array($array2)) {
  313. return false;
  314. }
  315. $newarray = $array1;
  316. foreach ($array2 as $key => $val) {
  317. if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
  318. $newarray[$key] = getid3_lib::array_merge_clobber($newarray[$key], $val);
  319. } else {
  320. $newarray[$key] = $val;
  321. }
  322. }
  323. return $newarray;
  324. }
  325. function array_merge_noclobber($array1, $array2) {
  326. if (!is_array($array1) || !is_array($array2)) {
  327. return false;
  328. }
  329. $newarray = $array1;
  330. foreach ($array2 as $key => $val) {
  331. if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
  332. $newarray[$key] = getid3_lib::array_merge_noclobber($newarray[$key], $val);
  333. } elseif (!isset($newarray[$key])) {
  334. $newarray[$key] = $val;
  335. }
  336. }
  337. return $newarray;
  338. }
  339. function fileextension($filename, $numextensions=1) {
  340. if (strstr($filename, '.')) {
  341. $reversedfilename = strrev($filename);
  342. $offset = 0;
  343. for ($i = 0; $i < $numextensions; $i++) {
  344. $offset = strpos($reversedfilename, '.', $offset + 1);
  345. if ($offset === false) {
  346. return '';
  347. }
  348. }
  349. return strrev(substr($reversedfilename, 0, $offset));
  350. }
  351. return '';
  352. }
  353. function PlaytimeString($playtimeseconds) {
  354. $contentseconds = round((($playtimeseconds / 60) - floor($playtimeseconds / 60)) * 60);
  355. $contentminutes = floor($playtimeseconds / 60);
  356. if ($contentseconds >= 60) {
  357. $contentseconds -= 60;
  358. $contentminutes++;
  359. }
  360. return intval($contentminutes).':'.str_pad($contentseconds, 2, 0, STR_PAD_LEFT);
  361. }
  362. function image_type_to_mime_type($imagetypeid) {
  363. // only available in PHP v4.3.0+
  364. static $image_type_to_mime_type = array();
  365. if (empty($image_type_to_mime_type)) {
  366. $image_type_to_mime_type[1] = 'image/gif'; // GIF
  367. $image_type_to_mime_type[2] = 'image/jpeg'; // JPEG
  368. $image_type_to_mime_type[3] = 'image/png'; // PNG
  369. $image_type_to_mime_type[4] = 'application/x-shockwave-flash'; // Flash
  370. $image_type_to_mime_type[5] = 'image/psd'; // PSD
  371. $image_type_to_mime_type[6] = 'image/bmp'; // BMP
  372. $image_type_to_mime_type[7] = 'image/tiff'; // TIFF: little-endian (Intel)
  373. $image_type_to_mime_type[8] = 'image/tiff'; // TIFF: big-endian (Motorola)
  374. //$image_type_to_mime_type[9] = 'image/jpc'; // JPC
  375. //$image_type_to_mime_type[10] = 'image/jp2'; // JPC
  376. //$image_type_to_mime_type[11] = 'image/jpx'; // JPC
  377. //$image_type_to_mime_type[12] = 'image/jb2'; // JPC
  378. $image_type_to_mime_type[13] = 'application/x-shockwave-flash'; // Shockwave
  379. $image_type_to_mime_type[14] = 'image/iff'; // IFF
  380. }
  381. return (isset($image_type_to_mime_type[$imagetypeid]) ? $image_type_to_mime_type[$imagetypeid] : 'application/octet-stream');
  382. }
  383. function DateMac2Unix($macdate) {
  384. // Macintosh timestamp: seconds since 00:00h January 1, 1904
  385. // UNIX timestamp: seconds since 00:00h January 1, 1970
  386. return getid3_lib::CastAsInt($macdate - 2082844800);
  387. }
  388. function FixedPoint8_8($rawdata) {
  389. return getid3_lib::BigEndian2Int(substr($rawdata, 0, 1)) + (float) (getid3_lib::BigEndian2Int(substr($rawdata, 1, 1)) / pow(2, 8));
  390. }
  391. function FixedPoint16_16($rawdata) {
  392. return getid3_lib::BigEndian2Int(substr($rawdata, 0, 2)) + (float) (getid3_lib::BigEndian2Int(substr($rawdata, 2, 2)) / pow(2, 16));
  393. }
  394. function FixedPoint2_30($rawdata) {
  395. $binarystring = getid3_lib::BigEndian2Bin($rawdata);
  396. return getid3_lib::Bin2Dec(substr($binarystring, 0, 2)) + (float) (getid3_lib::Bin2Dec(substr($binarystring, 2, 30)) / 1073741824);
  397. }
  398. function CreateDeepArray($ArrayPath, $Separator, $Value) {
  399. // assigns $Value to a nested array path:
  400. // $foo = getid3_lib::CreateDeepArray('/path/to/my', '/', 'file.txt')
  401. // is the same as:
  402. // $foo = array('path'=>array('to'=>'array('my'=>array('file.txt'))));
  403. // or
  404. // $foo['path']['to']['my'] = 'file.txt';
  405. while ($ArrayPath && ($ArrayPath{0} == $Separator)) {
  406. $ArrayPath = substr($ArrayPath, 1);
  407. }
  408. if (($pos = strpos($ArrayPath, $Separator)) !== false) {
  409. $ReturnedArray[substr($ArrayPath, 0, $pos)] = getid3_lib::CreateDeepArray(substr($ArrayPath, $pos + 1), $Separator, $Value);
  410. } else {
  411. $ReturnedArray[$ArrayPath] = $Value;
  412. }
  413. return $ReturnedArray;
  414. }
  415. function array_max($arraydata, $returnkey=false) {
  416. $maxvalue = false;
  417. $maxkey = false;
  418. foreach ($arraydata as $key => $value) {
  419. if (!is_array($value)) {
  420. if ($value > $maxvalue) {
  421. $maxvalue = $value;
  422. $maxkey = $key;
  423. }
  424. }
  425. }
  426. return ($returnkey ? $maxkey : $maxvalue);
  427. }
  428. function array_min($arraydata, $returnkey=false) {
  429. $minvalue = false;
  430. $minkey = false;
  431. foreach ($arraydata as $key => $value) {
  432. if (!is_array($value)) {
  433. if ($value > $minvalue) {
  434. $minvalue = $value;
  435. $minkey = $key;
  436. }
  437. }
  438. }
  439. return ($returnkey ? $minkey : $minvalue);
  440. }
  441. function md5_file($file) {
  442. // md5_file() exists in PHP 4.2.0+.
  443. if (function_exists('md5_file')) {
  444. return md5_file($file);
  445. }
  446. if (GETID3_OS_ISWINDOWS) {
  447. $RequiredFiles = array('cygwin1.dll', 'md5sum.exe');
  448. foreach ($RequiredFiles as $required_file) {
  449. if (!is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
  450. die(implode(' and ', $RequiredFiles).' are required in '.GETID3_HELPERAPPSDIR.' for getid3_lib::md5_file() to function under Windows in PHP < v4.2.0');
  451. }
  452. }
  453. $commandline = GETID3_HELPERAPPSDIR.'md5sum.exe "'.str_replace('/', DIRECTORY_SEPARATOR, $file).'"';
  454. if (ereg("^[\\]?([0-9a-f]{32})", strtolower(`$commandline`), $r)) {
  455. return $r[1];
  456. }
  457. } else {
  458. // The following works under UNIX only
  459. $file = str_replace('`', '\\`', $file);
  460. if (ereg("^([0-9a-f]{32})[ \t\n\r]", `md5sum "$file"`, $r)) {
  461. return $r[1];
  462. }
  463. }
  464. return false;
  465. }
  466. function sha1_file($file) {
  467. // sha1_file() exists in PHP 4.3.0+.
  468. if (function_exists('sha1_file')) {
  469. return sha1_file($file);
  470. }
  471. $file = str_replace('`', '\\`', $file);
  472. if (GETID3_OS_ISWINDOWS) {
  473. $RequiredFiles = array('cygwin1.dll', 'sha1sum.exe');
  474. foreach ($RequiredFiles as $required_file) {
  475. if (!is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
  476. die(implode(' and ', $RequiredFiles).' are required in '.GETID3_HELPERAPPSDIR.' for getid3_lib::sha1_file() to function under Windows in PHP < v4.3.0');
  477. }
  478. }
  479. $commandline = GETID3_HELPERAPPSDIR.'sha1sum.exe "'.str_replace('/', DIRECTORY_SEPARATOR, $file).'"';
  480. if (ereg("^sha1=([0-9a-f]{40})", strtolower(`$commandline`), $r)) {
  481. return $r[1];
  482. }
  483. } else {
  484. $commandline = 'sha1sum '.escapeshellarg($file).'';
  485. if (ereg("^([0-9a-f]{40})[ \t\n\r]", strtolower(`$commandline`), $r)) {
  486. return $r[1];
  487. }
  488. }
  489. return false;
  490. }
  491. // Allan Hansen <ahØartemis*dk>
  492. // getid3_lib::md5_data() - returns md5sum for a file from startuing position to absolute end position
  493. function hash_data($file, $offset, $end, $algorithm) {
  494. switch ($algorithm) {
  495. case 'md5':
  496. $hash_function = 'md5_file';
  497. $unix_call = 'md5sum';
  498. $windows_call = 'md5sum.exe';
  499. $hash_length = 32;
  500. break;
  501. case 'sha1':
  502. $hash_function = 'sha1_file';
  503. $unix_call = 'sha1sum';
  504. $windows_call = 'sha1sum.exe';
  505. $hash_length = 40;
  506. break;
  507. default:
  508. die('Invalid algorithm ('.$algorithm.') in getid3_lib::hash_data()');
  509. break;
  510. }
  511. $size = $end - $offset;
  512. while (true) {
  513. if (GETID3_OS_ISWINDOWS) {
  514. // It seems that sha1sum.exe for Windows only works on physical files, does not accept piped data
  515. // Fall back to create-temp-file method:
  516. if ($algorithm == 'sha1') {
  517. break;
  518. }
  519. $RequiredFiles = array('cygwin1.dll', 'head.exe', 'tail.exe', $windows_call);
  520. foreach ($RequiredFiles as $required_file) {
  521. if (!is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
  522. // helper apps not available - fall back to old method
  523. break;
  524. }
  525. }
  526. $commandline = GETID3_HELPERAPPSDIR.'head.exe -c '.$end.' "'.escapeshellarg(str_replace('/', DIRECTORY_SEPARATOR, $file)).'" | ';
  527. $commandline .= GETID3_HELPERAPPSDIR.'tail.exe -c '.$size.' | ';
  528. $commandline .= GETID3_HELPERAPPSDIR.$windows_call;
  529. } else {
  530. $commandline = 'head -c'.$end.' '.escapeshellarg($file).' | ';
  531. $commandline .= 'tail -c'.$size.' | ';
  532. $commandline .= $unix_call;
  533. }
  534. if ((bool) ini_get('safe_mode')) {
  535. $ThisFileInfo['warning'][] = 'PHP running in Safe Mode - backtick operator not available, using slower non-system-call '.$algorithm.' algorithm';
  536. break;
  537. }
  538. return substr(`$commandline`, 0, $hash_length);
  539. }
  540. // try to create a temporary file in the system temp directory - invalid dirname should force to system temp dir
  541. if (($data_filename = tempnam('*', 'getID3')) === false) {
  542. // can't find anywhere to create a temp file, just die
  543. return false;
  544. }
  545. // Init
  546. $result = false;
  547. // copy parts of file
  548. if ($fp = @fopen($file, 'rb')) {
  549. if ($fp_data = @fopen($data_filename, 'wb')) {
  550. fseek($fp, $offset, SEEK_SET);
  551. $byteslefttowrite = $end - $offset;
  552. while (($byteslefttowrite > 0) && ($buffer = fread($fp, GETID3_FREAD_BUFFER_SIZE))) {
  553. $byteswritten = fwrite($fp_data, $buffer, $byteslefttowrite);
  554. $byteslefttowrite -= $byteswritten;
  555. }
  556. fclose($fp_data);
  557. $result = getid3_lib::$hash_function($data_filename);
  558. }
  559. fclose($fp);
  560. }
  561. unlink($data_filename);
  562. return $result;
  563. }
  564. function iconv_fallback_int_utf8($charval) {
  565. if ($charval < 128) {
  566. // 0bbbbbbb
  567. $newcharstring = chr($charval);
  568. } elseif ($charval < 2048) {
  569. // 110bbbbb 10bbbbbb
  570. $newcharstring = chr(($charval >> 6) | 0xC0);
  571. $newcharstring .= chr(($charval & 0x3F) | 0x80);
  572. } elseif ($charval < 65536) {
  573. // 1110bbbb 10bbbbbb 10bbbbbb
  574. $newcharstring = chr(($charval >> 12) | 0xE0);
  575. $newcharstring .= chr(($charval >> 6) | 0xC0);
  576. $newcharstring .= chr(($charval & 0x3F) | 0x80);
  577. } else {
  578. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  579. $newcharstring = chr(($charval >> 18) | 0xF0);
  580. $newcharstring .= chr(($charval >> 12) | 0xC0);
  581. $newcharstring .= chr(($charval >> 6) | 0xC0);
  582. $newcharstring .= chr(($charval & 0x3F) | 0x80);
  583. }
  584. return $newcharstring;
  585. }
  586. // ISO-8859-1 => UTF-8
  587. function iconv_fallback_iso88591_utf8($string, $bom=false) {
  588. if (function_exists('utf8_encode')) {
  589. return utf8_encode($string);
  590. }
  591. // utf8_encode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
  592. $newcharstring = '';
  593. if ($bom) {
  594. $newcharstring .= "\xEF\xBB\xBF";
  595. }
  596. for ($i = 0; $i < strlen($string); $i++) {
  597. $charval = ord($string{$i});
  598. $newcharstring .= getid3_lib::iconv_fallback_int_utf8($charval);
  599. }
  600. return $newcharstring;
  601. }
  602. // ISO-8859-1 => UTF-16BE
  603. function iconv_fallback_iso88591_utf16be($string, $bom=false) {
  604. $newcharstring = '';
  605. if ($bom) {
  606. $newcharstring .= "\xFE\xFF";
  607. }
  608. for ($i = 0; $i < strlen($string); $i++) {
  609. $newcharstring .= "\x00".$string{$i};
  610. }
  611. return $newcharstring;
  612. }
  613. // ISO-8859-1 => UTF-16LE
  614. function iconv_fallback_iso88591_utf16le($string, $bom=false) {
  615. $newcharstring = '';
  616. if ($bom) {
  617. $newcharstring .= "\xFF\xFE";
  618. }
  619. for ($i = 0; $i < strlen($string); $i++) {
  620. $newcharstring .= $string{$i}."\x00";
  621. }
  622. return $newcharstring;
  623. }
  624. // ISO-8859-1 => UTF-16LE (BOM)
  625. function iconv_fallback_iso88591_utf16($string) {
  626. return getid3_lib::iconv_fallback_iso88591_utf16le($string, true);
  627. }
  628. // UTF-8 => ISO-8859-1
  629. function iconv_fallback_utf8_iso88591($string) {
  630. if (function_exists('utf8_decode')) {
  631. return utf8_decode($string);
  632. }
  633. // utf8_decode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
  634. $newcharstring = '';
  635. $offset = 0;
  636. $stringlength = strlen($string);
  637. while ($offset < $stringlength) {
  638. if ((ord($string{$offset}) | 0x07) == 0xF7) {
  639. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  640. $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
  641. ((ord($string{($offset + 1)}) & 0x3F) << 12) &
  642. ((ord($string{($offset + 2)}) & 0x3F) << 6) &
  643. (ord($string{($offset + 3)}) & 0x3F);
  644. $offset += 4;
  645. } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
  646. // 1110bbbb 10bbbbbb 10bbbbbb
  647. $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
  648. ((ord($string{($offset + 1)}) & 0x3F) << 6) &
  649. (ord($string{($offset + 2)}) & 0x3F);
  650. $offset += 3;
  651. } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
  652. // 110bbbbb 10bbbbbb
  653. $charval = ((ord($string{($offset + 0)}) & 0x1F) << 6) &
  654. (ord($string{($offset + 1)}) & 0x3F);
  655. $offset += 2;
  656. } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
  657. // 0bbbbbbb
  658. $charval = ord($string{$offset});
  659. $offset += 1;
  660. } else {
  661. // error? throw some kind of warning here?
  662. $charval = false;
  663. $offset += 1;
  664. }
  665. if ($charval !== false) {
  666. $newcharstring .= (($charval < 256) ? chr($charval) : '?');
  667. }
  668. }
  669. return $newcharstring;
  670. }
  671. // UTF-8 => UTF-16BE
  672. function iconv_fallback_utf8_utf16be($string, $bom=false) {
  673. $newcharstring = '';
  674. if ($bom) {
  675. $newcharstring .= "\xFE\xFF";
  676. }
  677. $offset = 0;
  678. $stringlength = strlen($string);
  679. while ($offset < $stringlength) {
  680. if ((ord($string{$offset}) | 0x07) == 0xF7) {
  681. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  682. $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
  683. ((ord($string{($offset + 1)}) & 0x3F) << 12) &
  684. ((ord($string{($offset + 2)}) & 0x3F) << 6) &
  685. (ord($string{($offset + 3)}) & 0x3F);
  686. $offset += 4;
  687. } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
  688. // 1110bbbb 10bbbbbb 10bbbbbb
  689. $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
  690. ((ord($string{($offset + 1)}) & 0x3F) << 6) &
  691. (ord($string{($offset + 2)}) & 0x3F);
  692. $offset += 3;
  693. } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
  694. // 110bbbbb 10bbbbbb
  695. $charval = ((ord($string{($offset + 0)}) & 0x1F) << 6) &
  696. (ord($string{($offset + 1)}) & 0x3F);
  697. $offset += 2;
  698. } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
  699. // 0bbbbbbb
  700. $charval = ord($string{$offset});
  701. $offset += 1;
  702. } else {
  703. // error? throw some kind of warning here?
  704. $charval = false;
  705. $offset += 1;
  706. }
  707. if ($charval !== false) {
  708. $newcharstring .= (($charval < 65536) ? getid3_lib::BigEndian2String($charval, 2) : "\x00".'?');
  709. }
  710. }
  711. return $newcharstring;
  712. }
  713. // UTF-8 => UTF-16LE
  714. function iconv_fallback_utf8_utf16le($string, $bom=false) {
  715. $newcharstring = '';
  716. if ($bom) {
  717. $newcharstring .= "\xFF\xFE";
  718. }
  719. $offset = 0;
  720. $stringlength = strlen($string);
  721. while ($offset < $stringlength) {
  722. if ((ord($string{$offset}) | 0x07) == 0xF7) {
  723. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  724. $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
  725. ((ord($string{($offset + 1)}) & 0x3F) << 12) &
  726. ((ord($string{($offset + 2)}) & 0x3F) << 6) &
  727. (ord($string{($offset + 3)}) & 0x3F);
  728. $offset += 4;
  729. } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
  730. // 1110bbbb 10bbbbbb 10bbbbbb
  731. $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
  732. ((ord($string{($offset + 1)}) & 0x3F) << 6) &
  733. (ord($string{($offset + 2)}) & 0x3F);
  734. $offset += 3;
  735. } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
  736. // 110bbbbb 10bbbbbb
  737. $charval = ((ord($string{($offset + 0)}) & 0x1F) << 6) &
  738. (ord($string{($offset + 1)}) & 0x3F);
  739. $offset += 2;
  740. } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
  741. // 0bbbbbbb
  742. $charval = ord($string{$offset});
  743. $offset += 1;
  744. } else {
  745. // error? maybe throw some warning here?
  746. $charval = false;
  747. $offset += 1;
  748. }
  749. if ($charval !== false) {
  750. $newcharstring .= (($charval < 65536) ? getid3_lib::LittleEndian2String($charval, 2) : '?'."\x00");
  751. }
  752. }
  753. return $newcharstring;
  754. }
  755. // UTF-8 => UTF-16LE (BOM)
  756. function iconv_fallback_utf8_utf16($string) {
  757. return getid3_lib::iconv_fallback_utf8_utf16le($string, true);
  758. }
  759. // UTF-16BE => UTF-8
  760. function iconv_fallback_utf16be_utf8($string) {
  761. if (substr($string, 0, 2) == "\xFE\xFF") {
  762. // strip BOM
  763. $string = substr($string, 2);
  764. }
  765. $newcharstring = '';
  766. for ($i = 0; $i < strlen($string); $i += 2) {
  767. $charval = getid3_lib::BigEndian2Int(substr($string, $i, 2));
  768. $newcharstring .= getid3_lib::iconv_fallback_int_utf8($charval);
  769. }
  770. return $newcharstring;
  771. }
  772. // UTF-16LE => UTF-8
  773. function iconv_fallback_utf16le_utf8($string) {
  774. if (substr($string, 0, 2) == "\xFF\xFE") {
  775. // strip BOM
  776. $string = substr($string, 2);
  777. }
  778. $newcharstring = '';
  779. for ($i = 0; $i < strlen($string); $i += 2) {
  780. $charval = getid3_lib::LittleEndian2Int(substr($string, $i, 2));
  781. $newcharstring .= getid3_lib::iconv_fallback_int_utf8($charval);
  782. }
  783. return $newcharstring;
  784. }
  785. // UTF-16BE => ISO-8859-1
  786. function iconv_fallback_utf16be_iso88591($string) {
  787. if (substr($string, 0, 2) == "\xFE\xFF") {
  788. // strip BOM
  789. $string = substr($string, 2);
  790. }
  791. $newcharstring = '';
  792. for ($i = 0; $i < strlen($string); $i += 2) {
  793. $charval = getid3_lib::BigEndian2Int(substr($string, $i, 2));
  794. $newcharstring .= (($charval < 256) ? chr($charval) : '?');
  795. }
  796. return $newcharstring;
  797. }
  798. // UTF-16LE => ISO-8859-1
  799. function iconv_fallback_utf16le_iso88591($string) {
  800. if (substr($string, 0, 2) == "\xFF\xFE") {
  801. // strip BOM
  802. $string = substr($string, 2);
  803. }
  804. $newcharstring = '';
  805. for ($i = 0; $i < strlen($string); $i += 2) {
  806. $charval = getid3_lib::LittleEndian2Int(substr($string, $i, 2));
  807. $newcharstring .= (($charval < 256) ? chr($charval) : '?');
  808. }
  809. return $newcharstring;
  810. }
  811. // UTF-16 (BOM) => ISO-8859-1
  812. function iconv_fallback_utf16_iso88591($string) {
  813. $bom = substr($string, 0, 2);
  814. if ($bom == "\xFE\xFF") {
  815. return getid3_lib::iconv_fallback_utf16be_iso88591(substr($string, 2));
  816. } elseif ($bom == "\xFF\xFE") {
  817. return getid3_lib::iconv_fallback_utf16le_iso88591(substr($string, 2));
  818. }
  819. return $string;
  820. }
  821. // UTF-16 (BOM) => UTF-8
  822. function iconv_fallback_utf16_utf8($string) {
  823. $bom = substr($string, 0, 2);
  824. if ($bom == "\xFE\xFF") {
  825. return getid3_lib::iconv_fallback_utf16be_utf8(substr($string, 2));
  826. } elseif ($bom == "\xFF\xFE") {
  827. return getid3_lib::iconv_fallback_utf16le_utf8(substr($string, 2));
  828. }
  829. return $string;
  830. }
  831. function iconv_fallback($in_charset, $out_charset, $string) {
  832. if ($in_charset == $out_charset) {
  833. return $string;
  834. }
  835. static $iconv_broken_or_unavailable = array();
  836. if (is_null(@$iconv_broken_or_unavailable[$in_charset.'_'.$out_charset])) {
  837. $GETID3_ICONV_TEST_STRING = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ';
  838. // Check iconv()
  839. if (function_exists('iconv')) {
  840. if (@iconv($in_charset, 'ISO-8859-1', @iconv('ISO-8859-1', $in_charset, $GETID3_ICONV_TEST_STRING)) == $GETID3_ICONV_TEST_STRING) {
  841. if (@iconv($out_charset, 'ISO-8859-1', @iconv('ISO-8859-1', $out_charset, $GETID3_ICONV_TEST_STRING)) == $GETID3_ICONV_TEST_STRING) {
  842. // everything works, use iconv()
  843. $iconv_broken_or_unavailable[$in_charset.'_'.$out_charset] = false;
  844. } else {
  845. // iconv() available, but broken. Use getID3()'s iconv_fallback() conversions instead
  846. // known issue in PHP v4.1.x
  847. $iconv_broken_or_unavailable[$in_charset.'_'.$out_charset] = true;
  848. }
  849. } else {
  850. // iconv() available, but broken. Use getID3()'s iconv_fallback() conversions instead
  851. // known issue in PHP v4.1.x
  852. $iconv_broken_or_unavailable[$in_charset.'_'.$out_charset] = true;
  853. }
  854. } else {
  855. // iconv() unavailable, use getID3()'s iconv_fallback() conversions
  856. $iconv_broken_or_unavailable[$in_charset.'_'.$out_charset] = true;
  857. }
  858. }
  859. if ($iconv_broken_or_unavailable[$in_charset.'_'.$out_charset]) {
  860. static $ConversionFunctionList = array();
  861. if (empty($ConversionFunctionList)) {
  862. $ConversionFunctionList['ISO-8859-1']['UTF-8'] = 'iconv_fallback_iso88591_utf8';
  863. $ConversionFunctionList['ISO-8859-1']['UTF-16'] = 'iconv_fallback_iso88591_utf16';
  864. $ConversionFunctionList['ISO-8859-1']['UTF-16BE'] = 'iconv_fallback_iso88591_utf16be';
  865. $ConversionFunctionList['ISO-8859-1']['UTF-16LE'] = 'iconv_fallback_iso88591_utf16le';
  866. $ConversionFunctionList['UTF-8']['ISO-8859-1'] = 'iconv_fallback_utf8_iso88591';
  867. $ConversionFunctionList['UTF-8']['UTF-16'] = 'iconv_fallback_utf8_utf16';
  868. $ConversionFunctionList['UTF-8']['UTF-16BE'] = 'iconv_fallback_utf8_utf16be';
  869. $ConversionFunctionList['UTF-8']['UTF-16LE'] = 'iconv_fallback_utf8_utf16le';
  870. $ConversionFunctionList['UTF-16']['ISO-8859-1'] = 'iconv_fallback_utf16_iso88591';
  871. $ConversionFunctionList['UTF-16']['UTF-8'] = 'iconv_fallback_utf16_utf8';
  872. $ConversionFunctionList['UTF-16LE']['ISO-8859-1'] = 'iconv_fallback_utf16le_iso88591';
  873. $ConversionFunctionList['UTF-16LE']['UTF-8'] = 'iconv_fallback_utf16le_utf8';
  874. $ConversionFunctionList['UTF-16BE']['ISO-8859-1'] = 'iconv_fallback_utf16be_iso88591';
  875. $ConversionFunctionList['UTF-16BE']['UTF-8'] = 'iconv_fallback_utf16be_utf8';
  876. }
  877. if (isset($ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)])) {
  878. $ConversionFunction = $ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)];
  879. return getid3_lib::$ConversionFunction($string);
  880. }
  881. die('PHP does not have iconv() support - cannot convert from '.$in_charset.' to '.$out_charset);
  882. }
  883. if ($converted_string = @iconv($in_charset, $out_charset.'//TRANSLIT', $string)) {
  884. switch ($out_charset) {
  885. case 'ISO-8859-1':
  886. $converted_string = rtrim($converted_string, "\x00");
  887. break;
  888. }
  889. return $converted_string;
  890. }
  891. // iconv() may sometimes fail with "illegal character in input string" error message
  892. // and return an empty string, but returning the unconverted string is more useful
  893. return $string;
  894. }
  895. function MultiByteCharString2HTML($string, $charset='ISO-8859-1') {
  896. $HTMLstring = '';
  897. switch ($charset) {
  898. case 'ISO-8859-1':
  899. case 'ISO8859-1':
  900. case 'ISO-8859-15':
  901. case 'ISO8859-15':
  902. case 'cp866':
  903. case 'ibm866':
  904. case '866':
  905. case 'cp1251':
  906. case 'Windows-1251':
  907. case 'win-1251':
  908. case '1251':
  909. case 'cp1252':
  910. case 'Windows-1252':
  911. case '1252':
  912. case 'KOI8-R':
  913. case 'koi8-ru':
  914. case 'koi8r':
  915. case 'BIG5':
  916. case '950':
  917. case 'GB2312':
  918. case '936':
  919. case 'BIG5-HKSCS':
  920. case 'Shift_JIS':
  921. case 'SJIS':
  922. case '932':
  923. case 'EUC-JP':
  924. case 'EUCJP':
  925. $HTMLstring = htmlentities($string, ENT_COMPAT, $charset);
  926. break;
  927. case 'UTF-8':
  928. $strlen = strlen($string);
  929. for ($i = 0; $i < $strlen; $i++) {
  930. $char_ord_val = ord($string{$i});
  931. $charval = 0;
  932. if ($char_ord_val < 0x80) {
  933. $charval = $char_ord_val;
  934. } elseif ((($char_ord_val & 0xF0) >> 4) == 0x0F) {
  935. $charval = (($char_ord_val & 0x07) << 18);
  936. $charval += ((ord($string{++$i}) & 0x3F) << 12);
  937. $charval += ((ord($string{++$i}) & 0x3F) << 6);
  938. $charval += (ord($string{++$i}) & 0x3F);
  939. } elseif ((($char_ord_val & 0xE0) >> 5) == 0x07) {
  940. $charval = (($char_ord_val & 0x0F) << 12);
  941. $charval += ((ord($string{++$i}) & 0x3F) << 6);
  942. $charval += (ord($string{++$i}) & 0x3F);
  943. } elseif ((($char_ord_val & 0xC0) >> 6) == 0x03) {
  944. $charval = (($char_ord_val & 0x1F) << 6);
  945. $charval += (ord($string{++$i}) & 0x3F);
  946. }
  947. if (($charval >= 32) && ($charval <= 127)) {
  948. $HTMLstring .= chr($charval);
  949. } else {
  950. $HTMLstring .= '&#'.$charval.';';
  951. }
  952. }
  953. break;
  954. case 'UTF-16LE':
  955. for ($i = 0; $i < strlen($string); $i += 2) {
  956. $charval = getid3_lib::LittleEndian2Int(substr($string, $i, 2));
  957. if (($charval >= 32) && ($charval <= 127)) {
  958. $HTMLstring .= chr($charval);
  959. } else {
  960. $HTMLstring .= '&#'.$charval.';';
  961. }
  962. }
  963. break;
  964. case 'UTF-16BE':
  965. for ($i = 0; $i < strlen($string); $i += 2) {
  966. $charval = getid3_lib::BigEndian2Int(substr($string, $i, 2));
  967. if (($charval >= 32) && ($charval <= 127)) {
  968. $HTMLstring .= chr($charval);
  969. } else {
  970. $HTMLstring .= '&#'.$charval.';';
  971. }
  972. }
  973. break;
  974. default:
  975. $HTMLstring = 'ERROR: Character set "'.$charset.'" not supported in MultiByteCharString2HTML()';
  976. break;
  977. }
  978. return $HTMLstring;
  979. }
  980. function RGADnameLookup($namecode) {
  981. static $RGADname = array();
  982. if (empty($RGADname)) {
  983. $RGADname[0] = 'not set';
  984. $RGADname[1] = 'Track Gain Adjustment';
  985. $RGADname[2] = 'Album Gain Adjustment';
  986. }
  987. return (isset($RGADname[$namecode]) ? $RGADname[$namecode] : '');
  988. }
  989. function RGADoriginatorLookup($originatorcode) {
  990. static $RGADoriginator = array();
  991. if (empty($RGADoriginator)) {
  992. $RGADoriginator[0] = 'unspecified';
  993. $RGADoriginator[1] = 'pre-set by artist/producer/mastering engineer';
  994. $RGADoriginator[2] = 'set by user';
  995. $RGADoriginator[3] = 'determined automatically';
  996. }
  997. return (isset($RGADoriginator[$originatorcode]) ? $RGADoriginator[$originatorcode] : '');
  998. }
  999. function RGADadjustmentLookup($rawadjustment, $signbit) {
  1000. $adjustment = $rawadjustment / 10;
  1001. if ($signbit == 1) {
  1002. $adjustment *= -1;
  1003. }
  1004. return (float) $adjustment;
  1005. }
  1006. function RGADgainString($namecode, $originatorcode, $replaygain) {
  1007. if ($replaygain < 0) {
  1008. $signbit = '1';
  1009. } else {
  1010. $signbit = '0';
  1011. }
  1012. $storedreplaygain = intval(round($replaygain * 10));
  1013. $gainstring = str_pad(decbin($namecode), 3, '0', STR_PAD_LEFT);
  1014. $gainstring .= str_pad(decbin($originatorcode), 3, '0', STR_PAD_LEFT);
  1015. $gainstring .= $signbit;
  1016. $gainstring .= str_pad(decbin($storedreplaygain), 9, '0', STR_PAD_LEFT);
  1017. return $gainstring;
  1018. }
  1019. function RGADamplitude2dB($amplitude) {
  1020. return 20 * log10($amplitude);
  1021. }
  1022. function GetDataImageSize($imgData) {
  1023. $GetDataImageSize = false;
  1024. if ($tempfilename = tempnam('*', 'getID3')) {
  1025. if ($tmp = @fopen($tempfilename, 'wb')) {
  1026. fwrite($tmp, $imgData);
  1027. fclose($tmp);
  1028. $GetDataImageSize = @GetImageSize($tempfilename);
  1029. }
  1030. unlink($tempfilename);
  1031. }
  1032. return $GetDataImageSize;
  1033. }
  1034. function ImageTypesLookup($imagetypeid) {
  1035. static $ImageTypesLookup = array();
  1036. if (empty($ImageTypesLookup)) {
  1037. $ImageTypesLookup[1] = 'gif';
  1038. $ImageTypesLookup[2] = 'jpeg';
  1039. $ImageTypesLookup[3] = 'png';
  1040. $ImageTypesLookup[4] = 'swf';
  1041. $ImageTypesLookup[5] = 'psd';
  1042. $ImageTypesLookup[6] = 'bmp';
  1043. $ImageTypesLookup[7] = 'tiff (little-endian)';
  1044. $ImageTypesLookup[8] = 'tiff (big-endian)';
  1045. $ImageTypesLookup[9] = 'jpc';
  1046. $ImageTypesLookup[10] = 'jp2';
  1047. $ImageTypesLookup[11] = 'jpx';
  1048. $ImageTypesLookup[12] = 'jb2';
  1049. $ImageTypesLookup[13] = 'swc';
  1050. $ImageTypesLookup[14] = 'iff';
  1051. }
  1052. return (isset($ImageTypesLookup[$imagetypeid]) ? $ImageTypesLookup[$imagetypeid] : '');
  1053. }
  1054. function CopyTagsToComments(&$ThisFileInfo) {
  1055. // Copy all entries from ['tags'] into common ['comments']
  1056. if (!empty($ThisFileInfo['tags'])) {
  1057. foreach ($ThisFileInfo['tags'] as $tagtype => $tagarray) {
  1058. foreach ($tagarray as $tagname => $tagdata) {
  1059. foreach ($tagdata as $key => $value) {
  1060. if (!empty($value)) {
  1061. if (empty($ThisFileInfo['comments'][$tagname])) {
  1062. // fall through and append value
  1063. } elseif ($tagtype == 'id3v1') {
  1064. $newvaluelength = strlen(trim($value));
  1065. foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
  1066. $oldvaluelength = strlen(trim($existingvalue));
  1067. if (($newvaluelength <= $oldvaluelength) && (substr($existingvalue, 0, $newvaluelength) == trim($value))) {
  1068. // new value is identical but shorter-than (or equal-length to) one already in comments - skip
  1069. break 2;
  1070. }
  1071. }
  1072. } else {
  1073. $newvaluelength = strlen(trim($value));
  1074. foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
  1075. $oldvaluelength = strlen(trim($existingvalue));
  1076. if (($newvaluelength > $oldvaluelength) && (substr(trim($value), 0, strlen($existingvalue)) == $existingvalue)) {
  1077. $ThisFileInfo['comments'][$tagname][$existingkey] = trim($value);
  1078. break 2;
  1079. }
  1080. }
  1081. }
  1082. if (empty($ThisFileInfo['comments'][$tagname]) || !in_array(trim($value), $ThisFileInfo['comments'][$tagname])) {
  1083. $ThisFileInfo['comments'][$tagname][] = trim($value);
  1084. }
  1085. }
  1086. }
  1087. }
  1088. }
  1089. // Copy to ['comments_html']
  1090. foreach ($ThisFileInfo['comments'] as $field => $values) {
  1091. foreach ($values as $index => $value) {
  1092. $ThisFileInfo['comments_html'][$field][$index] = str_replace('&#0;', '', getid3_lib::MultiByteCharString2HTML($value, $ThisFileInfo['encoding']));
  1093. }
  1094. }
  1095. }
  1096. }
  1097. function EmbeddedLookup($key, $begin, $end, $file, $name) {
  1098. // Cached
  1099. static $cache;
  1100. if (isset($cache[$file][$name])) {
  1101. return @$cache[$file][$name][$key];
  1102. }
  1103. // Init
  1104. $keylength = strlen($key);
  1105. $line_count = $end - $begin - 7;
  1106. // Open php file
  1107. $fp = fopen($file, 'r');
  1108. // Discard $begin lines
  1109. for ($i = 0; $i < ($begin + 3); $i++) {
  1110. fgets($fp, 1024);
  1111. }
  1112. // Loop thru line
  1113. while (0 < $line_count--) {
  1114. // Read line
  1115. $line = ltrim(fgets($fp, 1024), "\t ");
  1116. // METHOD A: only cache the matching key - less memory but slower on next lookup of not-previously-looked-up key
  1117. //$keycheck = substr($line, 0, $keylength);
  1118. //if ($key == $keycheck) {
  1119. // $cache[$file][$name][$keycheck] = substr($line, $keylength + 1);
  1120. // break;
  1121. //}
  1122. // METHOD B: cache all keys in this lookup - more memory but faster on next lookup of not-previously-looked-up key
  1123. //$cache[$file][$name][substr($line, 0, $keylength)] = trim(substr($line, $keylength + 1));
  1124. @list($ThisKey, $ThisValue) = explode("\t", $line, 2);
  1125. $cache[$file][$name][$ThisKey] = trim($ThisValue);
  1126. }
  1127. // Close and return
  1128. fclose($fp);
  1129. return @$cache[$file][$name][$key];
  1130. }
  1131. function IncludeDependency($filename, $sourcefile, $DieOnFailure=false) {
  1132. global $GETID3_ERRORARRAY;
  1133. if (file_exists($filename)) {
  1134. if (@include_once($filename)) {
  1135. return true;
  1136. } else {
  1137. $diemessage = basename($sourcefile).' depends on '.$filename.', which has errors';
  1138. }
  1139. } else {
  1140. $diemessage = basename($sourcefile).' depends on '.$filename.', which is missing';
  1141. }
  1142. if ($DieOnFailure) {
  1143. die($diemessage);
  1144. } else {
  1145. $GETID3_ERRORARRAY[] = $diemessage;
  1146. }
  1147. return false;
  1148. }
  1149. }
  1150. ?>