PageRenderTime 54ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/common/libraries/plugin/osflvplayer/flash/getid3.lib.php

https://bitbucket.org/renaatdemuynck/chamilo
PHP | 1590 lines | 1330 code | 138 blank | 122 comment | 219 complexity | f2cc47f19ab1e1d58122018d706baf39 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT, GPL-2.0

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file