PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_jce/editor/tiny_mce/plugins/mediamanager/classes/getid3/getid3.lib.php

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