PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/MediaGallery-master/include/lib/getid3/getid3.lib.php

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