PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/getid3/getid3/getid3.lib.php

https://github.com/cazwizzle/moodle-mod_pcast
PHP | 1262 lines | 1002 code | 149 blank | 111 comment | 246 complexity | d68bba6e0c48e795c80f9faeabcc62cd 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. 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);
  235. } else {
  236. $intvalue += ord($byteword{$i}) * pow(256, ($bytewordlen - 1 - $i));
  237. }
  238. }
  239. if ($signed && !$synchsafe) {
  240. // synchsafe ints are not allowed to be signed
  241. if ($bytewordlen <= PHP_INT_SIZE) {
  242. $signMaskBit = 0x80 << (8 * ($bytewordlen - 1));
  243. if ($intvalue & $signMaskBit) {
  244. $intvalue = 0 - ($intvalue & ($signMaskBit - 1));
  245. }
  246. } else {
  247. throw new Exception('ERROR: Cannot have signed integers larger than '.(8 * PHP_INT_SIZE).'-bits ('.strlen($byteword).') in getid3_lib::BigEndian2Int()');
  248. break;
  249. }
  250. }
  251. return getid3_lib::CastAsInt($intvalue);
  252. }
  253. static function LittleEndian2Int($byteword, $signed=false) {
  254. return getid3_lib::BigEndian2Int(strrev($byteword), false, $signed);
  255. }
  256. static function BigEndian2Bin($byteword) {
  257. $binvalue = '';
  258. $bytewordlen = strlen($byteword);
  259. for ($i = 0; $i < $bytewordlen; $i++) {
  260. $binvalue .= str_pad(decbin(ord($byteword{$i})), 8, '0', STR_PAD_LEFT);
  261. }
  262. return $binvalue;
  263. }
  264. static function BigEndian2String($number, $minbytes=1, $synchsafe=false, $signed=false) {
  265. if ($number < 0) {
  266. throw new Exception('ERROR: getid3_lib::BigEndian2String() does not support negative numbers');
  267. }
  268. $maskbyte = (($synchsafe || $signed) ? 0x7F : 0xFF);
  269. $intstring = '';
  270. if ($signed) {
  271. if ($minbytes > PHP_INT_SIZE) {
  272. throw new Exception('ERROR: Cannot have signed integers larger than '.(8 * PHP_INT_SIZE).'-bits in getid3_lib::BigEndian2String()');
  273. }
  274. $number = $number & (0x80 << (8 * ($minbytes - 1)));
  275. }
  276. while ($number != 0) {
  277. $quotient = ($number / ($maskbyte + 1));
  278. $intstring = chr(ceil(($quotient - floor($quotient)) * $maskbyte)).$intstring;
  279. $number = floor($quotient);
  280. }
  281. return str_pad($intstring, $minbytes, "\x00", STR_PAD_LEFT);
  282. }
  283. static function Dec2Bin($number) {
  284. while ($number >= 256) {
  285. $bytes[] = (($number / 256) - (floor($number / 256))) * 256;
  286. $number = floor($number / 256);
  287. }
  288. $bytes[] = $number;
  289. $binstring = '';
  290. for ($i = 0; $i < count($bytes); $i++) {
  291. $binstring = (($i == count($bytes) - 1) ? decbin($bytes[$i]) : str_pad(decbin($bytes[$i]), 8, '0', STR_PAD_LEFT)).$binstring;
  292. }
  293. return $binstring;
  294. }
  295. static function Bin2Dec($binstring, $signed=false) {
  296. $signmult = 1;
  297. if ($signed) {
  298. if ($binstring{0} == '1') {
  299. $signmult = -1;
  300. }
  301. $binstring = substr($binstring, 1);
  302. }
  303. $decvalue = 0;
  304. for ($i = 0; $i < strlen($binstring); $i++) {
  305. $decvalue += ((int) substr($binstring, strlen($binstring) - $i - 1, 1)) * pow(2, $i);
  306. }
  307. return getid3_lib::CastAsInt($decvalue * $signmult);
  308. }
  309. static function Bin2String($binstring) {
  310. // return 'hi' for input of '0110100001101001'
  311. $string = '';
  312. $binstringreversed = strrev($binstring);
  313. for ($i = 0; $i < strlen($binstringreversed); $i += 8) {
  314. $string = chr(getid3_lib::Bin2Dec(strrev(substr($binstringreversed, $i, 8)))).$string;
  315. }
  316. return $string;
  317. }
  318. static function LittleEndian2String($number, $minbytes=1, $synchsafe=false) {
  319. $intstring = '';
  320. while ($number > 0) {
  321. if ($synchsafe) {
  322. $intstring = $intstring.chr($number & 127);
  323. $number >>= 7;
  324. } else {
  325. $intstring = $intstring.chr($number & 255);
  326. $number >>= 8;
  327. }
  328. }
  329. return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
  330. }
  331. static function array_merge_clobber($array1, $array2) {
  332. // written by kcล˜hireability*com
  333. // taken from http://www.php.net/manual/en/function.array-merge-recursive.php
  334. if (!is_array($array1) || !is_array($array2)) {
  335. return false;
  336. }
  337. $newarray = $array1;
  338. foreach ($array2 as $key => $val) {
  339. if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
  340. $newarray[$key] = getid3_lib::array_merge_clobber($newarray[$key], $val);
  341. } else {
  342. $newarray[$key] = $val;
  343. }
  344. }
  345. return $newarray;
  346. }
  347. static function array_merge_noclobber($array1, $array2) {
  348. if (!is_array($array1) || !is_array($array2)) {
  349. return false;
  350. }
  351. $newarray = $array1;
  352. foreach ($array2 as $key => $val) {
  353. if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
  354. $newarray[$key] = getid3_lib::array_merge_noclobber($newarray[$key], $val);
  355. } elseif (!isset($newarray[$key])) {
  356. $newarray[$key] = $val;
  357. }
  358. }
  359. return $newarray;
  360. }
  361. static function fileextension($filename, $numextensions=1) {
  362. if (strstr($filename, '.')) {
  363. $reversedfilename = strrev($filename);
  364. $offset = 0;
  365. for ($i = 0; $i < $numextensions; $i++) {
  366. $offset = strpos($reversedfilename, '.', $offset + 1);
  367. if ($offset === false) {
  368. return '';
  369. }
  370. }
  371. return strrev(substr($reversedfilename, 0, $offset));
  372. }
  373. return '';
  374. }
  375. static function PlaytimeString($playtimeseconds) {
  376. $sign = (($playtimeseconds < 0) ? '-' : '');
  377. $playtimeseconds = abs($playtimeseconds);
  378. $contentseconds = round((($playtimeseconds / 60) - floor($playtimeseconds / 60)) * 60);
  379. $contentminutes = floor($playtimeseconds / 60);
  380. if ($contentseconds >= 60) {
  381. $contentseconds -= 60;
  382. $contentminutes++;
  383. }
  384. return $sign.intval($contentminutes).':'.str_pad($contentseconds, 2, 0, STR_PAD_LEFT);
  385. }
  386. static function DateMac2Unix($macdate) {
  387. // Macintosh timestamp: seconds since 00:00h January 1, 1904
  388. // UNIX timestamp: seconds since 00:00h January 1, 1970
  389. return getid3_lib::CastAsInt($macdate - 2082844800);
  390. }
  391. static function FixedPoint8_8($rawdata) {
  392. return getid3_lib::BigEndian2Int(substr($rawdata, 0, 1)) + (float) (getid3_lib::BigEndian2Int(substr($rawdata, 1, 1)) / pow(2, 8));
  393. }
  394. static function FixedPoint16_16($rawdata) {
  395. return getid3_lib::BigEndian2Int(substr($rawdata, 0, 2)) + (float) (getid3_lib::BigEndian2Int(substr($rawdata, 2, 2)) / pow(2, 16));
  396. }
  397. static function FixedPoint2_30($rawdata) {
  398. $binarystring = getid3_lib::BigEndian2Bin($rawdata);
  399. return getid3_lib::Bin2Dec(substr($binarystring, 0, 2)) + (float) (getid3_lib::Bin2Dec(substr($binarystring, 2, 30)) / pow(2, 30));
  400. }
  401. static function CreateDeepArray($ArrayPath, $Separator, $Value) {
  402. // assigns $Value to a nested array path:
  403. // $foo = getid3_lib::CreateDeepArray('/path/to/my', '/', 'file.txt')
  404. // is the same as:
  405. // $foo = array('path'=>array('to'=>'array('my'=>array('file.txt'))));
  406. // or
  407. // $foo['path']['to']['my'] = 'file.txt';
  408. while ($ArrayPath && ($ArrayPath{0} == $Separator)) {
  409. $ArrayPath = substr($ArrayPath, 1);
  410. }
  411. if (($pos = strpos($ArrayPath, $Separator)) !== false) {
  412. $ReturnedArray[substr($ArrayPath, 0, $pos)] = getid3_lib::CreateDeepArray(substr($ArrayPath, $pos + 1), $Separator, $Value);
  413. } else {
  414. $ReturnedArray[$ArrayPath] = $Value;
  415. }
  416. return $ReturnedArray;
  417. }
  418. static function array_max($arraydata, $returnkey=false) {
  419. $maxvalue = false;
  420. $maxkey = false;
  421. foreach ($arraydata as $key => $value) {
  422. if (!is_array($value)) {
  423. if ($value > $maxvalue) {
  424. $maxvalue = $value;
  425. $maxkey = $key;
  426. }
  427. }
  428. }
  429. return ($returnkey ? $maxkey : $maxvalue);
  430. }
  431. static function array_min($arraydata, $returnkey=false) {
  432. $minvalue = false;
  433. $minkey = false;
  434. foreach ($arraydata as $key => $value) {
  435. if (!is_array($value)) {
  436. if ($value > $minvalue) {
  437. $minvalue = $value;
  438. $minkey = $key;
  439. }
  440. }
  441. }
  442. return ($returnkey ? $minkey : $minvalue);
  443. }
  444. // Allan Hansen <ahล˜artemis*dk>
  445. // getid3_lib::md5_data() - returns md5sum for a file from startuing position to absolute end position
  446. static function hash_data($file, $offset, $end, $algorithm) {
  447. if (!getid3_lib::intValueSupported($end)) {
  448. return false;
  449. }
  450. switch ($algorithm) {
  451. case 'md5':
  452. $hash_function = 'md5_file';
  453. $unix_call = 'md5sum';
  454. $windows_call = 'md5sum.exe';
  455. $hash_length = 32;
  456. break;
  457. case 'sha1':
  458. $hash_function = 'sha1_file';
  459. $unix_call = 'sha1sum';
  460. $windows_call = 'sha1sum.exe';
  461. $hash_length = 40;
  462. break;
  463. default:
  464. throw new Exception('Invalid algorithm ('.$algorithm.') in getid3_lib::hash_data()');
  465. break;
  466. }
  467. $size = $end - $offset;
  468. while (true) {
  469. if (GETID3_OS_ISWINDOWS) {
  470. // It seems that sha1sum.exe for Windows only works on physical files, does not accept piped data
  471. // Fall back to create-temp-file method:
  472. if ($algorithm == 'sha1') {
  473. break;
  474. }
  475. $RequiredFiles = array('cygwin1.dll', 'head.exe', 'tail.exe', $windows_call);
  476. foreach ($RequiredFiles as $required_file) {
  477. if (!is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
  478. // helper apps not available - fall back to old method
  479. break;
  480. }
  481. }
  482. $commandline = GETID3_HELPERAPPSDIR.'head.exe -c '.$end.' "'.escapeshellarg(str_replace('/', DIRECTORY_SEPARATOR, $file)).'" | ';
  483. $commandline .= GETID3_HELPERAPPSDIR.'tail.exe -c '.$size.' | ';
  484. $commandline .= GETID3_HELPERAPPSDIR.$windows_call;
  485. } else {
  486. $commandline = 'head -c'.$end.' '.escapeshellarg($file).' | ';
  487. $commandline .= 'tail -c'.$size.' | ';
  488. $commandline .= $unix_call;
  489. }
  490. if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
  491. $ThisFileInfo['warning'][] = 'PHP running in Safe Mode - backtick operator not available, using slower non-system-call '.$algorithm.' algorithm';
  492. break;
  493. }
  494. return substr(`$commandline`, 0, $hash_length);
  495. }
  496. // try to create a temporary file in the system temp directory - invalid dirname should force to system temp dir
  497. if (($data_filename = tempnam(GETID3_TEMP_DIR, 'getID3')) === false) {
  498. // can't find anywhere to create a temp file, just die
  499. return false;
  500. }
  501. // Init
  502. $result = false;
  503. // copy parts of file
  504. ob_start();
  505. if ($fp = fopen($file, 'rb')) {
  506. ob_end_clean();
  507. ob_start();
  508. if ($fp_data = fopen($data_filename, 'wb')) {
  509. fseek($fp, $offset, SEEK_SET);
  510. $byteslefttowrite = $end - $offset;
  511. while (($byteslefttowrite > 0) && ($buffer = fread($fp, GETID3_FREAD_BUFFER_SIZE))) {
  512. $byteswritten = fwrite($fp_data, $buffer, $byteslefttowrite);
  513. $byteslefttowrite -= $byteswritten;
  514. }
  515. fclose($fp_data);
  516. $result = $hash_function($data_filename);
  517. } else {
  518. $errormessage = ob_get_contents();
  519. ob_end_clean();
  520. }
  521. fclose($fp);
  522. } else {
  523. $errormessage = ob_get_contents();
  524. ob_end_clean();
  525. }
  526. unlink($data_filename);
  527. return $result;
  528. }
  529. static function iconv_fallback_int_utf8($charval) {
  530. if ($charval < 128) {
  531. // 0bbbbbbb
  532. $newcharstring = chr($charval);
  533. } elseif ($charval < 2048) {
  534. // 110bbbbb 10bbbbbb
  535. $newcharstring = chr(($charval >> 6) | 0xC0);
  536. $newcharstring .= chr(($charval & 0x3F) | 0x80);
  537. } elseif ($charval < 65536) {
  538. // 1110bbbb 10bbbbbb 10bbbbbb
  539. $newcharstring = chr(($charval >> 12) | 0xE0);
  540. $newcharstring .= chr(($charval >> 6) | 0xC0);
  541. $newcharstring .= chr(($charval & 0x3F) | 0x80);
  542. } else {
  543. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  544. $newcharstring = chr(($charval >> 18) | 0xF0);
  545. $newcharstring .= chr(($charval >> 12) | 0xC0);
  546. $newcharstring .= chr(($charval >> 6) | 0xC0);
  547. $newcharstring .= chr(($charval & 0x3F) | 0x80);
  548. }
  549. return $newcharstring;
  550. }
  551. // ISO-8859-1 => UTF-8
  552. static function iconv_fallback_iso88591_utf8($string, $bom=false) {
  553. if (function_exists('utf8_encode')) {
  554. return utf8_encode($string);
  555. }
  556. // utf8_encode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
  557. $newcharstring = '';
  558. if ($bom) {
  559. $newcharstring .= "\xEF\xBB\xBF";
  560. }
  561. for ($i = 0; $i < strlen($string); $i++) {
  562. $charval = ord($string{$i});
  563. $newcharstring .= getid3_lib::iconv_fallback_int_utf8($charval);
  564. }
  565. return $newcharstring;
  566. }
  567. // ISO-8859-1 => UTF-16BE
  568. static function iconv_fallback_iso88591_utf16be($string, $bom=false) {
  569. $newcharstring = '';
  570. if ($bom) {
  571. $newcharstring .= "\xFE\xFF";
  572. }
  573. for ($i = 0; $i < strlen($string); $i++) {
  574. $newcharstring .= "\x00".$string{$i};
  575. }
  576. return $newcharstring;
  577. }
  578. // ISO-8859-1 => UTF-16LE
  579. static function iconv_fallback_iso88591_utf16le($string, $bom=false) {
  580. $newcharstring = '';
  581. if ($bom) {
  582. $newcharstring .= "\xFF\xFE";
  583. }
  584. for ($i = 0; $i < strlen($string); $i++) {
  585. $newcharstring .= $string{$i}."\x00";
  586. }
  587. return $newcharstring;
  588. }
  589. // ISO-8859-1 => UTF-16LE (BOM)
  590. static function iconv_fallback_iso88591_utf16($string) {
  591. return getid3_lib::iconv_fallback_iso88591_utf16le($string, true);
  592. }
  593. // UTF-8 => ISO-8859-1
  594. static function iconv_fallback_utf8_iso88591($string) {
  595. if (function_exists('utf8_decode')) {
  596. return utf8_decode($string);
  597. }
  598. // utf8_decode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
  599. $newcharstring = '';
  600. $offset = 0;
  601. $stringlength = strlen($string);
  602. while ($offset < $stringlength) {
  603. if ((ord($string{$offset}) | 0x07) == 0xF7) {
  604. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  605. $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
  606. ((ord($string{($offset + 1)}) & 0x3F) << 12) &
  607. ((ord($string{($offset + 2)}) & 0x3F) << 6) &
  608. (ord($string{($offset + 3)}) & 0x3F);
  609. $offset += 4;
  610. } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
  611. // 1110bbbb 10bbbbbb 10bbbbbb
  612. $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
  613. ((ord($string{($offset + 1)}) & 0x3F) << 6) &
  614. (ord($string{($offset + 2)}) & 0x3F);
  615. $offset += 3;
  616. } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
  617. // 110bbbbb 10bbbbbb
  618. $charval = ((ord($string{($offset + 0)}) & 0x1F) << 6) &
  619. (ord($string{($offset + 1)}) & 0x3F);
  620. $offset += 2;
  621. } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
  622. // 0bbbbbbb
  623. $charval = ord($string{$offset});
  624. $offset += 1;
  625. } else {
  626. // error? throw some kind of warning here?
  627. $charval = false;
  628. $offset += 1;
  629. }
  630. if ($charval !== false) {
  631. $newcharstring .= (($charval < 256) ? chr($charval) : '?');
  632. }
  633. }
  634. return $newcharstring;
  635. }
  636. // UTF-8 => UTF-16BE
  637. static function iconv_fallback_utf8_utf16be($string, $bom=false) {
  638. $newcharstring = '';
  639. if ($bom) {
  640. $newcharstring .= "\xFE\xFF";
  641. }
  642. $offset = 0;
  643. $stringlength = strlen($string);
  644. while ($offset < $stringlength) {
  645. if ((ord($string{$offset}) | 0x07) == 0xF7) {
  646. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  647. $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
  648. ((ord($string{($offset + 1)}) & 0x3F) << 12) &
  649. ((ord($string{($offset + 2)}) & 0x3F) << 6) &
  650. (ord($string{($offset + 3)}) & 0x3F);
  651. $offset += 4;
  652. } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
  653. // 1110bbbb 10bbbbbb 10bbbbbb
  654. $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
  655. ((ord($string{($offset + 1)}) & 0x3F) << 6) &
  656. (ord($string{($offset + 2)}) & 0x3F);
  657. $offset += 3;
  658. } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
  659. // 110bbbbb 10bbbbbb
  660. $charval = ((ord($string{($offset + 0)}) & 0x1F) << 6) &
  661. (ord($string{($offset + 1)}) & 0x3F);
  662. $offset += 2;
  663. } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
  664. // 0bbbbbbb
  665. $charval = ord($string{$offset});
  666. $offset += 1;
  667. } else {
  668. // error? throw some kind of warning here?
  669. $charval = false;
  670. $offset += 1;
  671. }
  672. if ($charval !== false) {
  673. $newcharstring .= (($charval < 65536) ? getid3_lib::BigEndian2String($charval, 2) : "\x00".'?');
  674. }
  675. }
  676. return $newcharstring;
  677. }
  678. // UTF-8 => UTF-16LE
  679. static function iconv_fallback_utf8_utf16le($string, $bom=false) {
  680. $newcharstring = '';
  681. if ($bom) {
  682. $newcharstring .= "\xFF\xFE";
  683. }
  684. $offset = 0;
  685. $stringlength = strlen($string);
  686. while ($offset < $stringlength) {
  687. if ((ord($string{$offset}) | 0x07) == 0xF7) {
  688. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  689. $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
  690. ((ord($string{($offset + 1)}) & 0x3F) << 12) &
  691. ((ord($string{($offset + 2)}) & 0x3F) << 6) &
  692. (ord($string{($offset + 3)}) & 0x3F);
  693. $offset += 4;
  694. } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
  695. // 1110bbbb 10bbbbbb 10bbbbbb
  696. $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
  697. ((ord($string{($offset + 1)}) & 0x3F) << 6) &
  698. (ord($string{($offset + 2)}) & 0x3F);
  699. $offset += 3;
  700. } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
  701. // 110bbbbb 10bbbbbb
  702. $charval = ((ord($string{($offset + 0)}) & 0x1F) << 6) &
  703. (ord($string{($offset + 1)}) & 0x3F);
  704. $offset += 2;
  705. } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
  706. // 0bbbbbbb
  707. $charval = ord($string{$offset});
  708. $offset += 1;
  709. } else {
  710. // error? maybe throw some warning here?
  711. $charval = false;
  712. $offset += 1;
  713. }
  714. if ($charval !== false) {
  715. $newcharstring .= (($charval < 65536) ? getid3_lib::LittleEndian2String($charval, 2) : '?'."\x00");
  716. }
  717. }
  718. return $newcharstring;
  719. }
  720. // UTF-8 => UTF-16LE (BOM)
  721. static function iconv_fallback_utf8_utf16($string) {
  722. return getid3_lib::iconv_fallback_utf8_utf16le($string, true);
  723. }
  724. // UTF-16BE => UTF-8
  725. static function iconv_fallback_utf16be_utf8($string) {
  726. if (substr($string, 0, 2) == "\xFE\xFF") {
  727. // strip BOM
  728. $string = substr($string, 2);
  729. }
  730. $newcharstring = '';
  731. for ($i = 0; $i < strlen($string); $i += 2) {
  732. $charval = getid3_lib::BigEndian2Int(substr($string, $i, 2));
  733. $newcharstring .= getid3_lib::iconv_fallback_int_utf8($charval);
  734. }
  735. return $newcharstring;
  736. }
  737. // UTF-16LE => UTF-8
  738. static function iconv_fallback_utf16le_utf8($string) {
  739. if (substr($string, 0, 2) == "\xFF\xFE") {
  740. // strip BOM
  741. $string = substr($string, 2);
  742. }
  743. $newcharstring = '';
  744. for ($i = 0; $i < strlen($string); $i += 2) {
  745. $charval = getid3_lib::LittleEndian2Int(substr($string, $i, 2));
  746. $newcharstring .= getid3_lib::iconv_fallback_int_utf8($charval);
  747. }
  748. return $newcharstring;
  749. }
  750. // UTF-16BE => ISO-8859-1
  751. static function iconv_fallback_utf16be_iso88591($string) {
  752. if (substr($string, 0, 2) == "\xFE\xFF") {
  753. // strip BOM
  754. $string = substr($string, 2);
  755. }
  756. $newcharstring = '';
  757. for ($i = 0; $i < strlen($string); $i += 2) {
  758. $charval = getid3_lib::BigEndian2Int(substr($string, $i, 2));
  759. $newcharstring .= (($charval < 256) ? chr($charval) : '?');
  760. }
  761. return $newcharstring;
  762. }
  763. // UTF-16LE => ISO-8859-1
  764. static function iconv_fallback_utf16le_iso88591($string) {
  765. if (substr($string, 0, 2) == "\xFF\xFE") {
  766. // strip BOM
  767. $string = substr($string, 2);
  768. }
  769. $newcharstring = '';
  770. for ($i = 0; $i < strlen($string); $i += 2) {
  771. $charval = getid3_lib::LittleEndian2Int(substr($string, $i, 2));
  772. $newcharstring .= (($charval < 256) ? chr($charval) : '?');
  773. }
  774. return $newcharstring;
  775. }
  776. // UTF-16 (BOM) => ISO-8859-1
  777. static function iconv_fallback_utf16_iso88591($string) {
  778. $bom = substr($string, 0, 2);
  779. if ($bom == "\xFE\xFF") {
  780. return getid3_lib::iconv_fallback_utf16be_iso88591(substr($string, 2));
  781. } elseif ($bom == "\xFF\xFE") {
  782. return getid3_lib::iconv_fallback_utf16le_iso88591(substr($string, 2));
  783. }
  784. return $string;
  785. }
  786. // UTF-16 (BOM) => UTF-8
  787. static function iconv_fallback_utf16_utf8($string) {
  788. $bom = substr($string, 0, 2);
  789. if ($bom == "\xFE\xFF") {
  790. return getid3_lib::iconv_fallback_utf16be_utf8(substr($string, 2));
  791. } elseif ($bom == "\xFF\xFE") {
  792. return getid3_lib::iconv_fallback_utf16le_utf8(substr($string, 2));
  793. }
  794. return $string;
  795. }
  796. static function iconv_fallback($in_charset, $out_charset, $string) {
  797. if ($in_charset == $out_charset) {
  798. return $string;
  799. }
  800. // iconv() availble
  801. if (function_exists('iconv')) {
  802. ob_start();
  803. if ($converted_string = iconv($in_charset, $out_charset.'//TRANSLIT', $string)) {
  804. ob_end_clean();
  805. switch ($out_charset) {
  806. case 'ISO-8859-1':
  807. $converted_string = rtrim($converted_string, "\x00");
  808. break;
  809. }
  810. return $converted_string;
  811. } else {
  812. $errormessage = ob_get_contents();
  813. ob_end_clean();
  814. }
  815. // iconv() may sometimes fail with "illegal character in input string" error message
  816. // and return an empty string, but returning the unconverted string is more useful
  817. return $string;
  818. }
  819. // iconv() not available
  820. static $ConversionFunctionList = array();
  821. if (empty($ConversionFunctionList)) {
  822. $ConversionFunctionList['ISO-8859-1']['UTF-8'] = 'iconv_fallback_iso88591_utf8';
  823. $ConversionFunctionList['ISO-8859-1']['UTF-16'] = 'iconv_fallback_iso88591_utf16';
  824. $ConversionFunctionList['ISO-8859-1']['UTF-16BE'] = 'iconv_fallback_iso88591_utf16be';
  825. $ConversionFunctionList['ISO-8859-1']['UTF-16LE'] = 'iconv_fallback_iso88591_utf16le';
  826. $ConversionFunctionList['UTF-8']['ISO-8859-1'] = 'iconv_fallback_utf8_iso88591';
  827. $ConversionFunctionList['UTF-8']['UTF-16'] = 'iconv_fallback_utf8_utf16';
  828. $ConversionFunctionList['UTF-8']['UTF-16BE'] = 'iconv_fallback_utf8_utf16be';
  829. $ConversionFunctionList['UTF-8']['UTF-16LE'] = 'iconv_fallback_utf8_utf16le';
  830. $ConversionFunctionList['UTF-16']['ISO-8859-1'] = 'iconv_fallback_utf16_iso88591';
  831. $ConversionFunctionList['UTF-16']['UTF-8'] = 'iconv_fallback_utf16_utf8';
  832. $ConversionFunctionList['UTF-16LE']['ISO-8859-1'] = 'iconv_fallback_utf16le_iso88591';
  833. $ConversionFunctionList['UTF-16LE']['UTF-8'] = 'iconv_fallback_utf16le_utf8';
  834. $ConversionFunctionList['UTF-16BE']['ISO-8859-1'] = 'iconv_fallback_utf16be_iso88591';
  835. $ConversionFunctionList['UTF-16BE']['UTF-8'] = 'iconv_fallback_utf16be_utf8';
  836. }
  837. if (isset($ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)])) {
  838. $ConversionFunction = $ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)];
  839. return getid3_lib::$ConversionFunction($string);
  840. }
  841. throw new Exception('PHP does not have iconv() support - cannot convert from '.$in_charset.' to '.$out_charset);
  842. }
  843. static function MultiByteCharString2HTML($string, $charset='ISO-8859-1') {
  844. $HTMLstring = '';
  845. switch ($charset) {
  846. case '1251':
  847. case '1252':
  848. case '866':
  849. case '932':
  850. case '936':
  851. case '950':
  852. case 'BIG5':
  853. case 'BIG5-HKSCS':
  854. case 'cp1251':
  855. case 'cp1252':
  856. case 'cp866':
  857. case 'EUC-JP':
  858. case 'EUCJP':
  859. case 'GB2312':
  860. case 'ibm866':
  861. case 'ISO-8859-1':
  862. case 'ISO-8859-15':
  863. case 'ISO8859-1':
  864. case 'ISO8859-15':
  865. case 'KOI8-R':
  866. case 'koi8-ru':
  867. case 'koi8r':
  868. case 'Shift_JIS':
  869. case 'SJIS':
  870. case 'win-1251':
  871. case 'Windows-1251':
  872. case 'Windows-1252':
  873. $HTMLstring = htmlentities($string, ENT_COMPAT, $charset);
  874. break;
  875. case 'UTF-8':
  876. $strlen = strlen($string);
  877. for ($i = 0; $i < $strlen; $i++) {
  878. $char_ord_val = ord($string{$i});
  879. $charval = 0;
  880. if ($char_ord_val < 0x80) {
  881. $charval = $char_ord_val;
  882. } elseif ((($char_ord_val & 0xF0) >> 4) == 0x0F && $i+3 < $strlen) {
  883. $charval = (($char_ord_val & 0x07) << 18);
  884. $charval += ((ord($string{++$i}) & 0x3F) << 12);
  885. $charval += ((ord($string{++$i}) & 0x3F) << 6);
  886. $charval += (ord($string{++$i}) & 0x3F);
  887. } elseif ((($char_ord_val & 0xE0) >> 5) == 0x07 && $i+2 < $strlen) {
  888. $charval = (($char_ord_val & 0x0F) << 12);
  889. $charval += ((ord($string{++$i}) & 0x3F) << 6);
  890. $charval += (ord($string{++$i}) & 0x3F);
  891. } elseif ((($char_ord_val & 0xC0) >> 6) == 0x03 && $i+1 < $strlen) {
  892. $charval = (($char_ord_val & 0x1F) << 6);
  893. $charval += (ord($string{++$i}) & 0x3F);
  894. }
  895. if (($charval >= 32) && ($charval <= 127)) {
  896. $HTMLstring .= htmlentities(chr($charval));
  897. } else {
  898. $HTMLstring .= '&#'.$charval.';';
  899. }
  900. }
  901. break;
  902. case 'UTF-16LE':
  903. for ($i = 0; $i < strlen($string); $i += 2) {
  904. $charval = getid3_lib::LittleEndian2Int(substr($string, $i, 2));
  905. if (($charval >= 32) && ($charval <= 127)) {
  906. $HTMLstring .= chr($charval);
  907. } else {
  908. $HTMLstring .= '&#'.$charval.';';
  909. }
  910. }
  911. break;
  912. case 'UTF-16BE':
  913. for ($i = 0; $i < strlen($string); $i += 2) {
  914. $charval = getid3_lib::BigEndian2Int(substr($string, $i, 2));
  915. if (($charval >= 32) && ($charval <= 127)) {
  916. $HTMLstring .= chr($charval);
  917. } else {
  918. $HTMLstring .= '&#'.$charval.';';
  919. }
  920. }
  921. break;
  922. default:
  923. $HTMLstring = 'ERROR: Character set "'.$charset.'" not supported in MultiByteCharString2HTML()';
  924. break;
  925. }
  926. return $HTMLstring;
  927. }
  928. static function RGADnameLookup($namecode) {
  929. static $RGADname = array();
  930. if (empty($RGADname)) {
  931. $RGADname[0] = 'not set';
  932. $RGADname[1] = 'Track Gain Adjustment';
  933. $RGADname[2] = 'Album Gain Adjustment';
  934. }
  935. return (isset($RGADname[$namecode]) ? $RGADname[$namecode] : '');
  936. }
  937. static function RGADoriginatorLookup($originatorcode) {
  938. static $RGADoriginator = array();
  939. if (empty($RGADoriginator)) {
  940. $RGADoriginator[0] = 'unspecified';
  941. $RGADoriginator[1] = 'pre-set by artist/producer/mastering engineer';
  942. $RGADoriginator[2] = 'set by user';
  943. $RGADoriginator[3] = 'determined automatically';
  944. }
  945. return (isset($RGADoriginator[$originatorcode]) ? $RGADoriginator[$originatorcode] : '');
  946. }
  947. static function RGADadjustmentLookup($rawadjustment, $signbit) {
  948. $adjustment = $rawadjustment / 10;
  949. if ($signbit == 1) {
  950. $adjustment *= -1;
  951. }
  952. return (float) $adjustment;
  953. }
  954. static function RGADgainString($namecode, $originatorcode, $replaygain) {
  955. if ($replaygain < 0) {
  956. $signbit = '1';
  957. } else {
  958. $signbit = '0';
  959. }
  960. $storedreplaygain = intval(round($replaygain * 10));
  961. $gainstring = str_pad(decbin($namecode), 3, '0', STR_PAD_LEFT);
  962. $gainstring .= str_pad(decbin($originatorcode), 3, '0', STR_PAD_LEFT);
  963. $gainstring .= $signbit;
  964. $gainstring .= str_pad(decbin($storedreplaygain), 9, '0', STR_PAD_LEFT);
  965. return $gainstring;
  966. }
  967. static function RGADamplitude2dB($amplitude) {
  968. return 20 * log10($amplitude);
  969. }
  970. static function GetDataImageSize($imgData, &$imageinfo) {
  971. $GetDataImageSize = false;
  972. if ($tempfilename = tempnam(GETID3_TEMP_DIR, 'getID3')) {
  973. ob_start();
  974. if ($tmp = fopen($tempfilename, 'wb')) {
  975. ob_end_clean();
  976. fwrite($tmp, $imgData);
  977. fclose($tmp);
  978. ob_start();
  979. $GetDataImageSize = GetImageSize($tempfilename, $imageinfo);
  980. $errormessage = ob_get_contents();
  981. ob_end_clean();
  982. } else {
  983. $errormessage = ob_get_contents();
  984. ob_end_clean();
  985. }
  986. unlink($tempfilename);
  987. }
  988. return $GetDataImageSize;
  989. }
  990. static function ImageTypesLookup($imagetypeid) {
  991. static $ImageTypesLookup = array();
  992. if (empty($ImageTypesLookup)) {
  993. $ImageTypesLookup[1] = 'gif';
  994. $ImageTypesLookup[2] = 'jpeg';
  995. $ImageTypesLookup[3] = 'png';
  996. $ImageTypesLookup[4] = 'swf';
  997. $ImageTypesLookup[5] = 'psd';
  998. $ImageTypesLookup[6] = 'bmp';
  999. $ImageTypesLookup[7] = 'tiff (little-endian)';
  1000. $ImageTypesLookup[8] = 'tiff (big-endian)';
  1001. $ImageTypesLookup[9] = 'jpc';
  1002. $ImageTypesLookup[10] = 'jp2';
  1003. $ImageTypesLookup[11] = 'jpx';
  1004. $ImageTypesLookup[12] = 'jb2';
  1005. $ImageTypesLookup[13] = 'swc';
  1006. $ImageTypesLookup[14] = 'iff';
  1007. }
  1008. return (isset($ImageTypesLookup[$imagetypeid]) ? $ImageTypesLookup[$imagetypeid] : '');
  1009. }
  1010. static function CopyTagsToComments(&$ThisFileInfo) {
  1011. // Copy all entries from ['tags'] into common ['comments']
  1012. if (!empty($ThisFileInfo['tags'])) {
  1013. foreach ($ThisFileInfo['tags'] as $tagtype => $tagarray) {
  1014. foreach ($tagarray as $tagname => $tagdata) {
  1015. foreach ($tagdata as $key => $value) {
  1016. if (!empty($value)) {
  1017. if (empty($ThisFileInfo['comments'][$tagname])) {
  1018. // fall through and append value
  1019. } elseif ($tagtype == 'id3v1') {
  1020. $newvaluelength = strlen(trim($value));
  1021. foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
  1022. $oldvaluelength = strlen(trim($existingvalue));
  1023. if (($newvaluelength <= $oldvaluelength) && (substr($existingvalue, 0, $newvaluelength) == trim($value))) {
  1024. // new value is identical but shorter-than (or equal-length to) one already in comments - skip
  1025. break 2;
  1026. }
  1027. }
  1028. } elseif (!is_array($value)) {
  1029. $newvaluelength = strlen(trim($value));
  1030. foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
  1031. $oldvaluelength = strlen(trim($existingvalue));
  1032. if (($newvaluelength > $oldvaluelength) && (substr(trim($value), 0, strlen($existingvalue)) == $existingvalue)) {
  1033. $ThisFileInfo['comments'][$tagname][$existingkey] = trim($value);
  1034. break 2;
  1035. }
  1036. }
  1037. }
  1038. if (is_array($value) || empty($ThisFileInfo['comments'][$tagname]) || !in_array(trim($value), $ThisFileInfo['comments'][$tagname])) {
  1039. $value = (is_string($value) ? trim($value) : $value);
  1040. $ThisFileInfo['comments'][$tagname][] = $value;
  1041. }
  1042. }
  1043. }
  1044. }
  1045. }
  1046. // Copy to ['comments_html']
  1047. foreach ($ThisFileInfo['comments'] as $field => $values) {
  1048. foreach ($values as $index => $value) {
  1049. if (is_array($value)) {
  1050. $ThisFileInfo['comments_html'][$field][$index] = $value;
  1051. } else {
  1052. $ThisFileInfo['comments_html'][$field][$index] = str_replace('&#0;', '', getid3_lib::MultiByteCharString2HTML($value, $ThisFileInfo['encoding']));
  1053. }
  1054. }
  1055. }
  1056. }
  1057. }
  1058. static function EmbeddedLookup($key, $begin, $end, $file, $name) {
  1059. // Cached
  1060. static $cache;
  1061. if (isset($cache[$file][$name])) {
  1062. return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : '');
  1063. }
  1064. // Init
  1065. $keylength = strlen($key);
  1066. $line_count = $end - $begin - 7;
  1067. // Open php file
  1068. $fp = fopen($file, 'r');
  1069. // Discard $begin lines
  1070. for ($i = 0; $i < ($begin + 3); $i++) {
  1071. fgets($fp, 1024);
  1072. }
  1073. // Loop thru line
  1074. while (0 < $line_count--) {
  1075. // Read line
  1076. $line = ltrim(fgets($fp, 1024), "\t ");
  1077. // METHOD A: only cache the matching key - less memory but slower on next lookup of not-previously-looked-up key
  1078. //$keycheck = substr($line, 0, $keylength);
  1079. //if ($key == $keycheck) {
  1080. // $cache[$file][$name][$keycheck] = substr($line, $keylength + 1);
  1081. // break;
  1082. //}
  1083. // METHOD B: cache all keys in this lookup - more memory but faster on next lookup of not-previously-looked-up key
  1084. //$cache[$file][$name][substr($line, 0, $keylength)] = trim(substr($line, $keylength + 1));
  1085. $explodedLine = explode("\t", $line, 2);
  1086. $ThisKey = (isset($explodedLine[0]) ? $explodedLine[0] : '');
  1087. $ThisValue = (isset($explodedLine[1]) ? $explodedLine[1] : '');
  1088. $cache[$file][$name][$ThisKey] = trim($ThisValue);
  1089. }
  1090. // Close and return
  1091. fclose($fp);
  1092. return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : '');
  1093. }
  1094. static function IncludeDependency($filename, $sourcefile, $DieOnFailure=false) {
  1095. global $GETID3_ERRORARRAY;
  1096. if (file_exists($filename)) {
  1097. if (include_once($filename)) {
  1098. return true;
  1099. } else {
  1100. $diemessage = basename($sourcefile).' depends on '.$filename.', which has errors';
  1101. }
  1102. } else {
  1103. $diemessage = basename($sourcefile).' depends on '.$filename.', which is missing';
  1104. }
  1105. if ($DieOnFailure) {
  1106. throw new Exception($diemessage);
  1107. } else {
  1108. $GETID3_ERRORARRAY[] = $diemessage;
  1109. }
  1110. return false;
  1111. }
  1112. }
  1113. ?>