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

/wp-includes/ID3/getid3.lib.php

http://github.com/wordpress/wordpress
PHP | 1786 lines | 1125 code | 121 blank | 540 comment | 294 complexity | 8dbfeaa0bfc2a3bd355f1eccd68a4b35 MD5 | raw file
Possible License(s): 0BSD

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 https://github.com/JamesHeinrich/getID3 //
  5. // or https://www.getid3.org //
  6. // or http://getid3.sourceforge.net //
  7. // //
  8. // getid3.lib.php - part of getID3() //
  9. // see readme.txt for more details //
  10. // ///
  11. /////////////////////////////////////////////////////////////////
  12. class getid3_lib
  13. {
  14. /**
  15. * @param string $string
  16. * @param bool $hex
  17. * @param bool $spaces
  18. * @param string|bool $htmlencoding
  19. *
  20. * @return string
  21. */
  22. public static function PrintHexBytes($string, $hex=true, $spaces=true, $htmlencoding='UTF-8') {
  23. $returnstring = '';
  24. for ($i = 0; $i < strlen($string); $i++) {
  25. if ($hex) {
  26. $returnstring .= str_pad(dechex(ord($string[$i])), 2, '0', STR_PAD_LEFT);
  27. } else {
  28. $returnstring .= ' '.(preg_match("#[\x20-\x7E]#", $string[$i]) ? $string[$i] : '¤');
  29. }
  30. if ($spaces) {
  31. $returnstring .= ' ';
  32. }
  33. }
  34. if (!empty($htmlencoding)) {
  35. if ($htmlencoding === true) {
  36. $htmlencoding = 'UTF-8'; // prior to getID3 v1.9.0 the function's 4th parameter was boolean
  37. }
  38. $returnstring = htmlentities($returnstring, ENT_QUOTES, $htmlencoding);
  39. }
  40. return $returnstring;
  41. }
  42. /**
  43. * Truncates a floating-point number at the decimal point.
  44. *
  45. * @param float $floatnumber
  46. *
  47. * @return float|int returns int (if possible, otherwise float)
  48. */
  49. public static function trunc($floatnumber) {
  50. if ($floatnumber >= 1) {
  51. $truncatednumber = floor($floatnumber);
  52. } elseif ($floatnumber <= -1) {
  53. $truncatednumber = ceil($floatnumber);
  54. } else {
  55. $truncatednumber = 0;
  56. }
  57. if (self::intValueSupported($truncatednumber)) {
  58. $truncatednumber = (int) $truncatednumber;
  59. }
  60. return $truncatednumber;
  61. }
  62. /**
  63. * @param int|null $variable
  64. * @param int $increment
  65. *
  66. * @return bool
  67. */
  68. public static function safe_inc(&$variable, $increment=1) {
  69. if (isset($variable)) {
  70. $variable += $increment;
  71. } else {
  72. $variable = $increment;
  73. }
  74. return true;
  75. }
  76. /**
  77. * @param int|float $floatnum
  78. *
  79. * @return int|float
  80. */
  81. public static function CastAsInt($floatnum) {
  82. // convert to float if not already
  83. $floatnum = (float) $floatnum;
  84. // convert a float to type int, only if possible
  85. if (self::trunc($floatnum) == $floatnum) {
  86. // it's not floating point
  87. if (self::intValueSupported($floatnum)) {
  88. // it's within int range
  89. $floatnum = (int) $floatnum;
  90. }
  91. }
  92. return $floatnum;
  93. }
  94. /**
  95. * @param int $num
  96. *
  97. * @return bool
  98. */
  99. public static function intValueSupported($num) {
  100. // check if integers are 64-bit
  101. static $hasINT64 = null;
  102. if ($hasINT64 === null) { // 10x faster than is_null()
  103. $hasINT64 = is_int(pow(2, 31)); // 32-bit int are limited to (2^31)-1
  104. if (!$hasINT64 && !defined('PHP_INT_MIN')) {
  105. define('PHP_INT_MIN', ~PHP_INT_MAX);
  106. }
  107. }
  108. // if integers are 64-bit - no other check required
  109. if ($hasINT64 || (($num <= PHP_INT_MAX) && ($num >= PHP_INT_MIN))) { // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
  110. return true;
  111. }
  112. return false;
  113. }
  114. /**
  115. * @param string $fraction
  116. *
  117. * @return float
  118. */
  119. public static function DecimalizeFraction($fraction) {
  120. list($numerator, $denominator) = explode('/', $fraction);
  121. return $numerator / ($denominator ? $denominator : 1);
  122. }
  123. /**
  124. * @param string $binarynumerator
  125. *
  126. * @return float
  127. */
  128. public static function DecimalBinary2Float($binarynumerator) {
  129. $numerator = self::Bin2Dec($binarynumerator);
  130. $denominator = self::Bin2Dec('1'.str_repeat('0', strlen($binarynumerator)));
  131. return ($numerator / $denominator);
  132. }
  133. /**
  134. * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
  135. *
  136. * @param string $binarypointnumber
  137. * @param int $maxbits
  138. *
  139. * @return array
  140. */
  141. public static function NormalizeBinaryPoint($binarypointnumber, $maxbits=52) {
  142. if (strpos($binarypointnumber, '.') === false) {
  143. $binarypointnumber = '0.'.$binarypointnumber;
  144. } elseif ($binarypointnumber[0] == '.') {
  145. $binarypointnumber = '0'.$binarypointnumber;
  146. }
  147. $exponent = 0;
  148. while (($binarypointnumber[0] != '1') || (substr($binarypointnumber, 1, 1) != '.')) {
  149. if (substr($binarypointnumber, 1, 1) == '.') {
  150. $exponent--;
  151. $binarypointnumber = substr($binarypointnumber, 2, 1).'.'.substr($binarypointnumber, 3);
  152. } else {
  153. $pointpos = strpos($binarypointnumber, '.');
  154. $exponent += ($pointpos - 1);
  155. $binarypointnumber = str_replace('.', '', $binarypointnumber);
  156. $binarypointnumber = $binarypointnumber[0].'.'.substr($binarypointnumber, 1);
  157. }
  158. }
  159. $binarypointnumber = str_pad(substr($binarypointnumber, 0, $maxbits + 2), $maxbits + 2, '0', STR_PAD_RIGHT);
  160. return array('normalized'=>$binarypointnumber, 'exponent'=>(int) $exponent);
  161. }
  162. /**
  163. * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
  164. *
  165. * @param float $floatvalue
  166. *
  167. * @return string
  168. */
  169. public static function Float2BinaryDecimal($floatvalue) {
  170. $maxbits = 128; // to how many bits of precision should the calculations be taken?
  171. $intpart = self::trunc($floatvalue);
  172. $floatpart = abs($floatvalue - $intpart);
  173. $pointbitstring = '';
  174. while (($floatpart != 0) && (strlen($pointbitstring) < $maxbits)) {
  175. $floatpart *= 2;
  176. $pointbitstring .= (string) self::trunc($floatpart);
  177. $floatpart -= self::trunc($floatpart);
  178. }
  179. $binarypointnumber = decbin($intpart).'.'.$pointbitstring;
  180. return $binarypointnumber;
  181. }
  182. /**
  183. * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee-expl.html
  184. *
  185. * @param float $floatvalue
  186. * @param int $bits
  187. *
  188. * @return string|false
  189. */
  190. public static function Float2String($floatvalue, $bits) {
  191. $exponentbits = 0;
  192. $fractionbits = 0;
  193. switch ($bits) {
  194. case 32:
  195. $exponentbits = 8;
  196. $fractionbits = 23;
  197. break;
  198. case 64:
  199. $exponentbits = 11;
  200. $fractionbits = 52;
  201. break;
  202. default:
  203. return false;
  204. }
  205. if ($floatvalue >= 0) {
  206. $signbit = '0';
  207. } else {
  208. $signbit = '1';
  209. }
  210. $normalizedbinary = self::NormalizeBinaryPoint(self::Float2BinaryDecimal($floatvalue), $fractionbits);
  211. $biasedexponent = pow(2, $exponentbits - 1) - 1 + $normalizedbinary['exponent']; // (127 or 1023) +/- exponent
  212. $exponentbitstring = str_pad(decbin($biasedexponent), $exponentbits, '0', STR_PAD_LEFT);
  213. $fractionbitstring = str_pad(substr($normalizedbinary['normalized'], 2), $fractionbits, '0', STR_PAD_RIGHT);
  214. return self::BigEndian2String(self::Bin2Dec($signbit.$exponentbitstring.$fractionbitstring), $bits % 8, false);
  215. }
  216. /**
  217. * @param string $byteword
  218. *
  219. * @return float|false
  220. */
  221. public static function LittleEndian2Float($byteword) {
  222. return self::BigEndian2Float(strrev($byteword));
  223. }
  224. /**
  225. * ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic
  226. *
  227. * @link http://www.psc.edu/general/software/packages/ieee/ieee.html
  228. * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html
  229. *
  230. * @param string $byteword
  231. *
  232. * @return float|false
  233. */
  234. public static function BigEndian2Float($byteword) {
  235. $bitword = self::BigEndian2Bin($byteword);
  236. if (!$bitword) {
  237. return 0;
  238. }
  239. $signbit = $bitword[0];
  240. $floatvalue = 0;
  241. $exponentbits = 0;
  242. $fractionbits = 0;
  243. switch (strlen($byteword) * 8) {
  244. case 32:
  245. $exponentbits = 8;
  246. $fractionbits = 23;
  247. break;
  248. case 64:
  249. $exponentbits = 11;
  250. $fractionbits = 52;
  251. break;
  252. case 80:
  253. // 80-bit Apple SANE format
  254. // http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/
  255. $exponentstring = substr($bitword, 1, 15);
  256. $isnormalized = intval($bitword[16]);
  257. $fractionstring = substr($bitword, 17, 63);
  258. $exponent = pow(2, self::Bin2Dec($exponentstring) - 16383);
  259. $fraction = $isnormalized + self::DecimalBinary2Float($fractionstring);
  260. $floatvalue = $exponent * $fraction;
  261. if ($signbit == '1') {
  262. $floatvalue *= -1;
  263. }
  264. return $floatvalue;
  265. default:
  266. return false;
  267. }
  268. $exponentstring = substr($bitword, 1, $exponentbits);
  269. $fractionstring = substr($bitword, $exponentbits + 1, $fractionbits);
  270. $exponent = self::Bin2Dec($exponentstring);
  271. $fraction = self::Bin2Dec($fractionstring);
  272. if (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction != 0)) {
  273. // Not a Number
  274. $floatvalue = false;
  275. } elseif (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction == 0)) {
  276. if ($signbit == '1') {
  277. $floatvalue = '-infinity';
  278. } else {
  279. $floatvalue = '+infinity';
  280. }
  281. } elseif (($exponent == 0) && ($fraction == 0)) {
  282. if ($signbit == '1') {
  283. $floatvalue = -0;
  284. } else {
  285. $floatvalue = 0;
  286. }
  287. $floatvalue = ($signbit ? 0 : -0);
  288. } elseif (($exponent == 0) && ($fraction != 0)) {
  289. // These are 'unnormalized' values
  290. $floatvalue = pow(2, (-1 * (pow(2, $exponentbits - 1) - 2))) * self::DecimalBinary2Float($fractionstring);
  291. if ($signbit == '1') {
  292. $floatvalue *= -1;
  293. }
  294. } elseif ($exponent != 0) {
  295. $floatvalue = pow(2, ($exponent - (pow(2, $exponentbits - 1) - 1))) * (1 + self::DecimalBinary2Float($fractionstring));
  296. if ($signbit == '1') {
  297. $floatvalue *= -1;
  298. }
  299. }
  300. return (float) $floatvalue;
  301. }
  302. /**
  303. * @param string $byteword
  304. * @param bool $synchsafe
  305. * @param bool $signed
  306. *
  307. * @return int|float|false
  308. * @throws Exception
  309. */
  310. public static function BigEndian2Int($byteword, $synchsafe=false, $signed=false) {
  311. $intvalue = 0;
  312. $bytewordlen = strlen($byteword);
  313. if ($bytewordlen == 0) {
  314. return false;
  315. }
  316. for ($i = 0; $i < $bytewordlen; $i++) {
  317. if ($synchsafe) { // disregard MSB, effectively 7-bit bytes
  318. //$intvalue = $intvalue | (ord($byteword{$i}) & 0x7F) << (($bytewordlen - 1 - $i) * 7); // faster, but runs into problems past 2^31 on 32-bit systems
  319. $intvalue += (ord($byteword[$i]) & 0x7F) * pow(2, ($bytewordlen - 1 - $i) * 7);
  320. } else {
  321. $intvalue += ord($byteword[$i]) * pow(256, ($bytewordlen - 1 - $i));
  322. }
  323. }
  324. if ($signed && !$synchsafe) {
  325. // synchsafe ints are not allowed to be signed
  326. if ($bytewordlen <= PHP_INT_SIZE) {
  327. $signMaskBit = 0x80 << (8 * ($bytewordlen - 1));
  328. if ($intvalue & $signMaskBit) {
  329. $intvalue = 0 - ($intvalue & ($signMaskBit - 1));
  330. }
  331. } else {
  332. throw new Exception('ERROR: Cannot have signed integers larger than '.(8 * PHP_INT_SIZE).'-bits ('.strlen($byteword).') in self::BigEndian2Int()');
  333. }
  334. }
  335. return self::CastAsInt($intvalue);
  336. }
  337. /**
  338. * @param string $byteword
  339. * @param bool $signed
  340. *
  341. * @return int|float|false
  342. */
  343. public static function LittleEndian2Int($byteword, $signed=false) {
  344. return self::BigEndian2Int(strrev($byteword), false, $signed);
  345. }
  346. /**
  347. * @param string $byteword
  348. *
  349. * @return string
  350. */
  351. public static function LittleEndian2Bin($byteword) {
  352. return self::BigEndian2Bin(strrev($byteword));
  353. }
  354. /**
  355. * @param string $byteword
  356. *
  357. * @return string
  358. */
  359. public static function BigEndian2Bin($byteword) {
  360. $binvalue = '';
  361. $bytewordlen = strlen($byteword);
  362. for ($i = 0; $i < $bytewordlen; $i++) {
  363. $binvalue .= str_pad(decbin(ord($byteword[$i])), 8, '0', STR_PAD_LEFT);
  364. }
  365. return $binvalue;
  366. }
  367. /**
  368. * @param int $number
  369. * @param int $minbytes
  370. * @param bool $synchsafe
  371. * @param bool $signed
  372. *
  373. * @return string
  374. * @throws Exception
  375. */
  376. public static function BigEndian2String($number, $minbytes=1, $synchsafe=false, $signed=false) {
  377. if ($number < 0) {
  378. throw new Exception('ERROR: self::BigEndian2String() does not support negative numbers');
  379. }
  380. $maskbyte = (($synchsafe || $signed) ? 0x7F : 0xFF);
  381. $intstring = '';
  382. if ($signed) {
  383. if ($minbytes > PHP_INT_SIZE) {
  384. throw new Exception('ERROR: Cannot have signed integers larger than '.(8 * PHP_INT_SIZE).'-bits in self::BigEndian2String()');
  385. }
  386. $number = $number & (0x80 << (8 * ($minbytes - 1)));
  387. }
  388. while ($number != 0) {
  389. $quotient = ($number / ($maskbyte + 1));
  390. $intstring = chr(ceil(($quotient - floor($quotient)) * $maskbyte)).$intstring;
  391. $number = floor($quotient);
  392. }
  393. return str_pad($intstring, $minbytes, "\x00", STR_PAD_LEFT);
  394. }
  395. /**
  396. * @param int $number
  397. *
  398. * @return string
  399. */
  400. public static function Dec2Bin($number) {
  401. while ($number >= 256) {
  402. $bytes[] = (($number / 256) - (floor($number / 256))) * 256;
  403. $number = floor($number / 256);
  404. }
  405. $bytes[] = $number;
  406. $binstring = '';
  407. for ($i = 0; $i < count($bytes); $i++) {
  408. $binstring = (($i == count($bytes) - 1) ? decbin($bytes[$i]) : str_pad(decbin($bytes[$i]), 8, '0', STR_PAD_LEFT)).$binstring;
  409. }
  410. return $binstring;
  411. }
  412. /**
  413. * @param string $binstring
  414. * @param bool $signed
  415. *
  416. * @return int|float
  417. */
  418. public static function Bin2Dec($binstring, $signed=false) {
  419. $signmult = 1;
  420. if ($signed) {
  421. if ($binstring[0] == '1') {
  422. $signmult = -1;
  423. }
  424. $binstring = substr($binstring, 1);
  425. }
  426. $decvalue = 0;
  427. for ($i = 0; $i < strlen($binstring); $i++) {
  428. $decvalue += ((int) substr($binstring, strlen($binstring) - $i - 1, 1)) * pow(2, $i);
  429. }
  430. return self::CastAsInt($decvalue * $signmult);
  431. }
  432. /**
  433. * @param string $binstring
  434. *
  435. * @return string
  436. */
  437. public static function Bin2String($binstring) {
  438. // return 'hi' for input of '0110100001101001'
  439. $string = '';
  440. $binstringreversed = strrev($binstring);
  441. for ($i = 0; $i < strlen($binstringreversed); $i += 8) {
  442. $string = chr(self::Bin2Dec(strrev(substr($binstringreversed, $i, 8)))).$string;
  443. }
  444. return $string;
  445. }
  446. /**
  447. * @param int $number
  448. * @param int $minbytes
  449. * @param bool $synchsafe
  450. *
  451. * @return string
  452. */
  453. public static function LittleEndian2String($number, $minbytes=1, $synchsafe=false) {
  454. $intstring = '';
  455. while ($number > 0) {
  456. if ($synchsafe) {
  457. $intstring = $intstring.chr($number & 127);
  458. $number >>= 7;
  459. } else {
  460. $intstring = $intstring.chr($number & 255);
  461. $number >>= 8;
  462. }
  463. }
  464. return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
  465. }
  466. /**
  467. * @param mixed $array1
  468. * @param mixed $array2
  469. *
  470. * @return array|false
  471. */
  472. public static function array_merge_clobber($array1, $array2) {
  473. // written by kcØhireability*com
  474. // taken from http://www.php.net/manual/en/function.array-merge-recursive.php
  475. if (!is_array($array1) || !is_array($array2)) {
  476. return false;
  477. }
  478. $newarray = $array1;
  479. foreach ($array2 as $key => $val) {
  480. if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
  481. $newarray[$key] = self::array_merge_clobber($newarray[$key], $val);
  482. } else {
  483. $newarray[$key] = $val;
  484. }
  485. }
  486. return $newarray;
  487. }
  488. /**
  489. * @param mixed $array1
  490. * @param mixed $array2
  491. *
  492. * @return array|false
  493. */
  494. public static function array_merge_noclobber($array1, $array2) {
  495. if (!is_array($array1) || !is_array($array2)) {
  496. return false;
  497. }
  498. $newarray = $array1;
  499. foreach ($array2 as $key => $val) {
  500. if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
  501. $newarray[$key] = self::array_merge_noclobber($newarray[$key], $val);
  502. } elseif (!isset($newarray[$key])) {
  503. $newarray[$key] = $val;
  504. }
  505. }
  506. return $newarray;
  507. }
  508. /**
  509. * @param mixed $array1
  510. * @param mixed $array2
  511. *
  512. * @return array|false|null
  513. */
  514. public static function flipped_array_merge_noclobber($array1, $array2) {
  515. if (!is_array($array1) || !is_array($array2)) {
  516. return false;
  517. }
  518. # naturally, this only works non-recursively
  519. $newarray = array_flip($array1);
  520. foreach (array_flip($array2) as $key => $val) {
  521. if (!isset($newarray[$key])) {
  522. $newarray[$key] = count($newarray);
  523. }
  524. }
  525. return array_flip($newarray);
  526. }
  527. /**
  528. * @param array $theArray
  529. *
  530. * @return bool
  531. */
  532. public static function ksort_recursive(&$theArray) {
  533. ksort($theArray);
  534. foreach ($theArray as $key => $value) {
  535. if (is_array($value)) {
  536. self::ksort_recursive($theArray[$key]);
  537. }
  538. }
  539. return true;
  540. }
  541. /**
  542. * @param string $filename
  543. * @param int $numextensions
  544. *
  545. * @return string
  546. */
  547. public static function fileextension($filename, $numextensions=1) {
  548. if (strstr($filename, '.')) {
  549. $reversedfilename = strrev($filename);
  550. $offset = 0;
  551. for ($i = 0; $i < $numextensions; $i++) {
  552. $offset = strpos($reversedfilename, '.', $offset + 1);
  553. if ($offset === false) {
  554. return '';
  555. }
  556. }
  557. return strrev(substr($reversedfilename, 0, $offset));
  558. }
  559. return '';
  560. }
  561. /**
  562. * @param int $seconds
  563. *
  564. * @return string
  565. */
  566. public static function PlaytimeString($seconds) {
  567. $sign = (($seconds < 0) ? '-' : '');
  568. $seconds = round(abs($seconds));
  569. $H = (int) floor( $seconds / 3600);
  570. $M = (int) floor(($seconds - (3600 * $H) ) / 60);
  571. $S = (int) round( $seconds - (3600 * $H) - (60 * $M) );
  572. return $sign.($H ? $H.':' : '').($H ? str_pad($M, 2, '0', STR_PAD_LEFT) : intval($M)).':'.str_pad($S, 2, 0, STR_PAD_LEFT);
  573. }
  574. /**
  575. * @param int $macdate
  576. *
  577. * @return int|float
  578. */
  579. public static function DateMac2Unix($macdate) {
  580. // Macintosh timestamp: seconds since 00:00h January 1, 1904
  581. // UNIX timestamp: seconds since 00:00h January 1, 1970
  582. return self::CastAsInt($macdate - 2082844800);
  583. }
  584. /**
  585. * @param string $rawdata
  586. *
  587. * @return float
  588. */
  589. public static function FixedPoint8_8($rawdata) {
  590. return self::BigEndian2Int(substr($rawdata, 0, 1)) + (float) (self::BigEndian2Int(substr($rawdata, 1, 1)) / pow(2, 8));
  591. }
  592. /**
  593. * @param string $rawdata
  594. *
  595. * @return float
  596. */
  597. public static function FixedPoint16_16($rawdata) {
  598. return self::BigEndian2Int(substr($rawdata, 0, 2)) + (float) (self::BigEndian2Int(substr($rawdata, 2, 2)) / pow(2, 16));
  599. }
  600. /**
  601. * @param string $rawdata
  602. *
  603. * @return float
  604. */
  605. public static function FixedPoint2_30($rawdata) {
  606. $binarystring = self::BigEndian2Bin($rawdata);
  607. return self::Bin2Dec(substr($binarystring, 0, 2)) + (float) (self::Bin2Dec(substr($binarystring, 2, 30)) / pow(2, 30));
  608. }
  609. /**
  610. * @param string $ArrayPath
  611. * @param string $Separator
  612. * @param mixed $Value
  613. *
  614. * @return array
  615. */
  616. public static function CreateDeepArray($ArrayPath, $Separator, $Value) {
  617. // assigns $Value to a nested array path:
  618. // $foo = self::CreateDeepArray('/path/to/my', '/', 'file.txt')
  619. // is the same as:
  620. // $foo = array('path'=>array('to'=>'array('my'=>array('file.txt'))));
  621. // or
  622. // $foo['path']['to']['my'] = 'file.txt';
  623. $ArrayPath = ltrim($ArrayPath, $Separator);
  624. if (($pos = strpos($ArrayPath, $Separator)) !== false) {
  625. $ReturnedArray[substr($ArrayPath, 0, $pos)] = self::CreateDeepArray(substr($ArrayPath, $pos + 1), $Separator, $Value);
  626. } else {
  627. $ReturnedArray[$ArrayPath] = $Value;
  628. }
  629. return $ReturnedArray;
  630. }
  631. /**
  632. * @param array $arraydata
  633. * @param bool $returnkey
  634. *
  635. * @return int|false
  636. */
  637. public static function array_max($arraydata, $returnkey=false) {
  638. $maxvalue = false;
  639. $maxkey = false;
  640. foreach ($arraydata as $key => $value) {
  641. if (!is_array($value)) {
  642. if ($value > $maxvalue) {
  643. $maxvalue = $value;
  644. $maxkey = $key;
  645. }
  646. }
  647. }
  648. return ($returnkey ? $maxkey : $maxvalue);
  649. }
  650. /**
  651. * @param array $arraydata
  652. * @param bool $returnkey
  653. *
  654. * @return int|false
  655. */
  656. public static function array_min($arraydata, $returnkey=false) {
  657. $minvalue = false;
  658. $minkey = false;
  659. foreach ($arraydata as $key => $value) {
  660. if (!is_array($value)) {
  661. if ($value > $minvalue) {
  662. $minvalue = $value;
  663. $minkey = $key;
  664. }
  665. }
  666. }
  667. return ($returnkey ? $minkey : $minvalue);
  668. }
  669. /**
  670. * @param string $XMLstring
  671. *
  672. * @return array|false
  673. */
  674. public static function XML2array($XMLstring) {
  675. if (function_exists('simplexml_load_string') && function_exists('libxml_disable_entity_loader')) {
  676. // http://websec.io/2012/08/27/Preventing-XEE-in-PHP.html
  677. // https://core.trac.wordpress.org/changeset/29378
  678. $loader = libxml_disable_entity_loader(true);
  679. $XMLobject = simplexml_load_string($XMLstring, 'SimpleXMLElement', LIBXML_NOENT);
  680. $return = self::SimpleXMLelement2array($XMLobject);
  681. libxml_disable_entity_loader($loader);
  682. return $return;
  683. }
  684. return false;
  685. }
  686. /**
  687. * @param SimpleXMLElement|array|mixed $XMLobject
  688. *
  689. * @return mixed
  690. */
  691. public static function SimpleXMLelement2array($XMLobject) {
  692. if (!is_object($XMLobject) && !is_array($XMLobject)) {
  693. return $XMLobject;
  694. }
  695. $XMLarray = $XMLobject instanceof SimpleXMLElement ? get_object_vars($XMLobject) : $XMLobject;
  696. foreach ($XMLarray as $key => $value) {
  697. $XMLarray[$key] = self::SimpleXMLelement2array($value);
  698. }
  699. return $XMLarray;
  700. }
  701. /**
  702. * Returns checksum for a file from starting position to absolute end position.
  703. *
  704. * @param string $file
  705. * @param int $offset
  706. * @param int $end
  707. * @param string $algorithm
  708. *
  709. * @return string|false
  710. * @throws getid3_exception
  711. */
  712. public static function hash_data($file, $offset, $end, $algorithm) {
  713. if (!self::intValueSupported($end)) {
  714. return false;
  715. }
  716. if (!in_array($algorithm, array('md5', 'sha1'))) {
  717. throw new getid3_exception('Invalid algorithm ('.$algorithm.') in self::hash_data()');
  718. }
  719. $size = $end - $offset;
  720. $fp = fopen($file, 'rb');
  721. fseek($fp, $offset);
  722. $ctx = hash_init($algorithm);
  723. while ($size > 0) {
  724. $buffer = fread($fp, min($size, getID3::FREAD_BUFFER_SIZE));
  725. hash_update($ctx, $buffer);
  726. $size -= getID3::FREAD_BUFFER_SIZE;
  727. }
  728. $hash = hash_final($ctx);
  729. fclose($fp);
  730. return $hash;
  731. }
  732. /**
  733. * @param string $filename_source
  734. * @param string $filename_dest
  735. * @param int $offset
  736. * @param int $length
  737. *
  738. * @return bool
  739. * @throws Exception
  740. *
  741. * @deprecated Unused, may be removed in future versions of getID3
  742. */
  743. public static function CopyFileParts($filename_source, $filename_dest, $offset, $length) {
  744. if (!self::intValueSupported($offset + $length)) {
  745. throw new Exception('cannot copy file portion, it extends beyond the '.round(PHP_INT_MAX / 1073741824).'GB limit');
  746. }
  747. if (is_readable($filename_source) && is_file($filename_source) && ($fp_src = fopen($filename_source, 'rb'))) {
  748. if (($fp_dest = fopen($filename_dest, 'wb'))) {
  749. if (fseek($fp_src, $offset) == 0) {
  750. $byteslefttowrite = $length;
  751. while (($byteslefttowrite > 0) && ($buffer = fread($fp_src, min($byteslefttowrite, getID3::FREAD_BUFFER_SIZE)))) {
  752. $byteswritten = fwrite($fp_dest, $buffer, $byteslefttowrite);
  753. $byteslefttowrite -= $byteswritten;
  754. }
  755. fclose($fp_dest);
  756. return true;
  757. } else {
  758. fclose($fp_src);
  759. throw new Exception('failed to seek to offset '.$offset.' in '.$filename_source);
  760. }
  761. } else {
  762. throw new Exception('failed to create file for writing '.$filename_dest);
  763. }
  764. } else {
  765. throw new Exception('failed to open file for reading '.$filename_source);
  766. }
  767. }
  768. /**
  769. * @param int $charval
  770. *
  771. * @return string
  772. */
  773. public static function iconv_fallback_int_utf8($charval) {
  774. if ($charval < 128) {
  775. // 0bbbbbbb
  776. $newcharstring = chr($charval);
  777. } elseif ($charval < 2048) {
  778. // 110bbbbb 10bbbbbb
  779. $newcharstring = chr(($charval >> 6) | 0xC0);
  780. $newcharstring .= chr(($charval & 0x3F) | 0x80);
  781. } elseif ($charval < 65536) {
  782. // 1110bbbb 10bbbbbb 10bbbbbb
  783. $newcharstring = chr(($charval >> 12) | 0xE0);
  784. $newcharstring .= chr(($charval >> 6) | 0xC0);
  785. $newcharstring .= chr(($charval & 0x3F) | 0x80);
  786. } else {
  787. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  788. $newcharstring = chr(($charval >> 18) | 0xF0);
  789. $newcharstring .= chr(($charval >> 12) | 0xC0);
  790. $newcharstring .= chr(($charval >> 6) | 0xC0);
  791. $newcharstring .= chr(($charval & 0x3F) | 0x80);
  792. }
  793. return $newcharstring;
  794. }
  795. /**
  796. * ISO-8859-1 => UTF-8
  797. *
  798. * @param string $string
  799. * @param bool $bom
  800. *
  801. * @return string
  802. */
  803. public static function iconv_fallback_iso88591_utf8($string, $bom=false) {
  804. if (function_exists('utf8_encode')) {
  805. return utf8_encode($string);
  806. }
  807. // utf8_encode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
  808. $newcharstring = '';
  809. if ($bom) {
  810. $newcharstring .= "\xEF\xBB\xBF";
  811. }
  812. for ($i = 0; $i < strlen($string); $i++) {
  813. $charval = ord($string[$i]);
  814. $newcharstring .= self::iconv_fallback_int_utf8($charval);
  815. }
  816. return $newcharstring;
  817. }
  818. /**
  819. * ISO-8859-1 => UTF-16BE
  820. *
  821. * @param string $string
  822. * @param bool $bom
  823. *
  824. * @return string
  825. */
  826. public static function iconv_fallback_iso88591_utf16be($string, $bom=false) {
  827. $newcharstring = '';
  828. if ($bom) {
  829. $newcharstring .= "\xFE\xFF";
  830. }
  831. for ($i = 0; $i < strlen($string); $i++) {
  832. $newcharstring .= "\x00".$string[$i];
  833. }
  834. return $newcharstring;
  835. }
  836. /**
  837. * ISO-8859-1 => UTF-16LE
  838. *
  839. * @param string $string
  840. * @param bool $bom
  841. *
  842. * @return string
  843. */
  844. public static function iconv_fallback_iso88591_utf16le($string, $bom=false) {
  845. $newcharstring = '';
  846. if ($bom) {
  847. $newcharstring .= "\xFF\xFE";
  848. }
  849. for ($i = 0; $i < strlen($string); $i++) {
  850. $newcharstring .= $string[$i]."\x00";
  851. }
  852. return $newcharstring;
  853. }
  854. /**
  855. * ISO-8859-1 => UTF-16LE (BOM)
  856. *
  857. * @param string $string
  858. *
  859. * @return string
  860. */
  861. public static function iconv_fallback_iso88591_utf16($string) {
  862. return self::iconv_fallback_iso88591_utf16le($string, true);
  863. }
  864. /**
  865. * UTF-8 => ISO-8859-1
  866. *
  867. * @param string $string
  868. *
  869. * @return string
  870. */
  871. public static function iconv_fallback_utf8_iso88591($string) {
  872. if (function_exists('utf8_decode')) {
  873. return utf8_decode($string);
  874. }
  875. // utf8_decode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
  876. $newcharstring = '';
  877. $offset = 0;
  878. $stringlength = strlen($string);
  879. while ($offset < $stringlength) {
  880. if ((ord($string[$offset]) | 0x07) == 0xF7) {
  881. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  882. $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) &
  883. ((ord($string[($offset + 1)]) & 0x3F) << 12) &
  884. ((ord($string[($offset + 2)]) & 0x3F) << 6) &
  885. (ord($string[($offset + 3)]) & 0x3F);
  886. $offset += 4;
  887. } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) {
  888. // 1110bbbb 10bbbbbb 10bbbbbb
  889. $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) &
  890. ((ord($string[($offset + 1)]) & 0x3F) << 6) &
  891. (ord($string[($offset + 2)]) & 0x3F);
  892. $offset += 3;
  893. } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) {
  894. // 110bbbbb 10bbbbbb
  895. $charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) &
  896. (ord($string[($offset + 1)]) & 0x3F);
  897. $offset += 2;
  898. } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) {
  899. // 0bbbbbbb
  900. $charval = ord($string[$offset]);
  901. $offset += 1;
  902. } else {
  903. // error? throw some kind of warning here?
  904. $charval = false;
  905. $offset += 1;
  906. }
  907. if ($charval !== false) {
  908. $newcharstring .= (($charval < 256) ? chr($charval) : '?');
  909. }
  910. }
  911. return $newcharstring;
  912. }
  913. /**
  914. * UTF-8 => UTF-16BE
  915. *
  916. * @param string $string
  917. * @param bool $bom
  918. *
  919. * @return string
  920. */
  921. public static function iconv_fallback_utf8_utf16be($string, $bom=false) {
  922. $newcharstring = '';
  923. if ($bom) {
  924. $newcharstring .= "\xFE\xFF";
  925. }
  926. $offset = 0;
  927. $stringlength = strlen($string);
  928. while ($offset < $stringlength) {
  929. if ((ord($string[$offset]) | 0x07) == 0xF7) {
  930. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  931. $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) &
  932. ((ord($string[($offset + 1)]) & 0x3F) << 12) &
  933. ((ord($string[($offset + 2)]) & 0x3F) << 6) &
  934. (ord($string[($offset + 3)]) & 0x3F);
  935. $offset += 4;
  936. } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) {
  937. // 1110bbbb 10bbbbbb 10bbbbbb
  938. $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) &
  939. ((ord($string[($offset + 1)]) & 0x3F) << 6) &
  940. (ord($string[($offset + 2)]) & 0x3F);
  941. $offset += 3;
  942. } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) {
  943. // 110bbbbb 10bbbbbb
  944. $charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) &
  945. (ord($string[($offset + 1)]) & 0x3F);
  946. $offset += 2;
  947. } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) {
  948. // 0bbbbbbb
  949. $charval = ord($string[$offset]);
  950. $offset += 1;
  951. } else {
  952. // error? throw some kind of warning here?
  953. $charval = false;
  954. $offset += 1;
  955. }
  956. if ($charval !== false) {
  957. $newcharstring .= (($charval < 65536) ? self::BigEndian2String($charval, 2) : "\x00".'?');
  958. }
  959. }
  960. return $newcharstring;
  961. }
  962. /**
  963. * UTF-8 => UTF-16LE
  964. *
  965. * @param string $string
  966. * @param bool $bom
  967. *
  968. * @return string
  969. */
  970. public static function iconv_fallback_utf8_utf16le($string, $bom=false) {
  971. $newcharstring = '';
  972. if ($bom) {
  973. $newcharstring .= "\xFF\xFE";
  974. }
  975. $offset = 0;
  976. $stringlength = strlen($string);
  977. while ($offset < $stringlength) {
  978. if ((ord($string[$offset]) | 0x07) == 0xF7) {
  979. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  980. $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) &
  981. ((ord($string[($offset + 1)]) & 0x3F) << 12) &
  982. ((ord($string[($offset + 2)]) & 0x3F) << 6) &
  983. (ord($string[($offset + 3)]) & 0x3F);
  984. $offset += 4;
  985. } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) {
  986. // 1110bbbb 10bbbbbb 10bbbbbb
  987. $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) &
  988. ((ord($string[($offset + 1)]) & 0x3F) << 6) &
  989. (ord($string[($offset + 2)]) & 0x3F);
  990. $offset += 3;
  991. } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) {
  992. // 110bbbbb 10bbbbbb
  993. $charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) &
  994. (ord($string[($offset + 1)]) & 0x3F);
  995. $offset += 2;
  996. } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) {
  997. // 0bbbbbbb
  998. $charval = ord($string[$offset]);
  999. $offset += 1;
  1000. } else {
  1001. // error? maybe throw some warning here?
  1002. $charval = false;
  1003. $offset += 1;
  1004. }
  1005. if ($charval !== false) {
  1006. $newcharstring .= (($charval < 65536) ? self::LittleEndian2String($charval, 2) : '?'."\x00");
  1007. }
  1008. }
  1009. return $newcharstring;
  1010. }
  1011. /**
  1012. * UTF-8 => UTF-16LE (BOM)
  1013. *
  1014. * @param string $string
  1015. *
  1016. * @return string
  1017. */
  1018. public static function iconv_fallback_utf8_utf16($string) {
  1019. return self::iconv_fallback_utf8_utf16le($string, true);
  1020. }
  1021. /**
  1022. * UTF-16BE => UTF-8
  1023. *
  1024. * @param string $string
  1025. *
  1026. * @return string
  1027. */
  1028. public static function iconv_fallback_utf16be_utf8($string) {
  1029. if (substr($string, 0, 2) == "\xFE\xFF") {
  1030. // strip BOM
  1031. $string = substr($string, 2);
  1032. }
  1033. $newcharstring = '';
  1034. for ($i = 0; $i < strlen($string); $i += 2) {
  1035. $charval = self::BigEndian2Int(substr($string, $i, 2));
  1036. $newcharstring .= self::iconv_fallback_int_utf8($charval);
  1037. }
  1038. return $newcharstring;
  1039. }
  1040. /**
  1041. * UTF-16LE => UTF-8
  1042. *
  1043. * @param string $string
  1044. *
  1045. * @return string
  1046. */
  1047. public static function iconv_fallback_utf16le_utf8($string) {
  1048. if (substr($string, 0, 2) == "\xFF\xFE") {
  1049. // strip BOM
  1050. $string = substr($string, 2);
  1051. }
  1052. $newcharstring = '';
  1053. for ($i = 0; $i < strlen($string); $i += 2) {
  1054. $charval = self::LittleEndian2Int(substr($string, $i, 2));
  1055. $newcharstring .= self::iconv_fallback_int_utf8($charval);
  1056. }
  1057. return $newcharstring;
  1058. }
  1059. /**
  1060. * UTF-16BE => ISO-8859-1
  1061. *
  1062. * @param string $string
  1063. *
  1064. * @return string
  1065. */
  1066. public static function iconv_fallback_utf16be_iso88591($string) {
  1067. if (substr($string, 0, 2) == "\xFE\xFF") {
  1068. // strip BOM
  1069. $string = substr($string, 2);
  1070. }
  1071. $newcharstring = '';
  1072. for ($i = 0; $i < strlen($string); $i += 2) {
  1073. $charval = self::BigEndian2Int(substr($string, $i, 2));
  1074. $newcharstring .= (($charval < 256) ? chr($charval) : '?');
  1075. }
  1076. return $newcharstring;
  1077. }
  1078. /**
  1079. * UTF-16LE => ISO-8859-1
  1080. *
  1081. * @param string $string
  1082. *
  1083. * @return string
  1084. */
  1085. public static function iconv_fallback_utf16le_iso88591($string) {
  1086. if (substr($string, 0, 2) == "\xFF\xFE") {
  1087. // strip BOM
  1088. $string = substr($string, 2);
  1089. }
  1090. $newcharstring = '';
  1091. for ($i = 0; $i < strlen($string); $i += 2) {
  1092. $charval = self::LittleEndian2Int(substr($string, $i, 2));
  1093. $newcharstring .= (($charval < 256) ? chr($charval) : '?');
  1094. }
  1095. return $newcharstring;
  1096. }
  1097. /**
  1098. * UTF-16 (BOM) => ISO-8859-1
  1099. *
  1100. * @param string $string
  1101. *
  1102. * @return string
  1103. */
  1104. public static function iconv_fallback_utf16_iso88591($string) {
  1105. $bom = substr($string, 0, 2);
  1106. if ($bom == "\xFE\xFF") {
  1107. return self::iconv_fallback_utf16be_iso88591(substr($string, 2));
  1108. } elseif ($bom == "\xFF\xFE") {
  1109. return self::iconv_fallback_utf16le_iso88591(substr($string, 2));
  1110. }
  1111. return $string;
  1112. }
  1113. /**
  1114. * UTF-16 (BOM) => UTF-8
  1115. *
  1116. * @param string $string
  1117. *
  1118. * @return string
  1119. */
  1120. public static function iconv_fallback_utf16_utf8($string) {
  1121. $bom = substr($string, 0, 2);
  1122. if ($bom == "\xFE\xFF") {
  1123. return self::iconv_fallback_utf16be_utf8(substr($string, 2));
  1124. } elseif ($bom == "\xFF\xFE") {
  1125. return self::iconv_fallback_utf16le_utf8(substr($string, 2));
  1126. }
  1127. return $string;
  1128. }
  1129. /**
  1130. * @param string $in_charset
  1131. * @param string $out_charset
  1132. * @param string $string
  1133. *
  1134. * @return string
  1135. * @throws Exception
  1136. */
  1137. public static function iconv_fallback($in_charset, $out_charset, $string) {
  1138. if ($in_charset == $out_charset) {
  1139. return $string;
  1140. }
  1141. // mb_convert_encoding() available
  1142. if (function_exists('mb_convert_encoding')) {
  1143. if ((strtoupper($in_charset) == 'UTF-16') && (substr($string, 0, 2) != "\xFE\xFF") && (substr($string, 0, 2) != "\xFF\xFE")) {
  1144. // if BOM missing, mb_convert_encoding will mishandle the conversion, assume UTF-16BE and prepend appropriate BOM
  1145. $string = "\xFF\xFE".$string;
  1146. }
  1147. if ((strtoupper($in_charset) == 'UTF-16') && (strtoupper($out_charset) == 'UTF-8')) {
  1148. if (($string == "\xFF\xFE") || ($string == "\xFE\xFF")) {
  1149. // if string consists of only BOM, mb_convert_encoding will return the BOM unmodified
  1150. return '';
  1151. }
  1152. }
  1153. if ($converted_string = @mb_convert_encoding($string, $out_charset, $in_charset)) {
  1154. switch ($out_charset) {
  1155. case 'ISO-8859-1':
  1156. $converted_string = rtrim($converted_string, "\x00");
  1157. break;
  1158. }
  1159. return $converted_string;
  1160. }
  1161. return $string;
  1162. // iconv() available
  1163. } elseif (function_exists('iconv')) {
  1164. if ($converted_string = @iconv($in_charset, $out_charset.'//TRANSLIT', $string)) {
  1165. switch ($out_charset) {
  1166. case 'ISO-8859-1':
  1167. $converted_string = rtrim($converted_string, "\x00");
  1168. break;
  1169. }
  1170. return $converted_string;
  1171. }
  1172. // iconv() may sometimes fail with "illegal character in input string" error message
  1173. // and return an empty string, but returning the unconverted string is more useful
  1174. return $string;
  1175. }
  1176. // neither mb_convert_encoding or iconv() is available
  1177. static $ConversionFunctionList = array();
  1178. if (empty($ConversionFunctionList)) {
  1179. $ConversionFunctionList['ISO-8859-1']['UTF-8'] = 'iconv_fallback_iso88591_utf8';
  1180. $ConversionFunctionList['ISO-8859-1']['UTF-16'] = 'iconv_fallback_iso88591_utf16';
  1181. $ConversionFunctionList['ISO-8859-1']['UTF-16BE'] = 'iconv_fallback_iso88591_utf16be';
  1182. $ConversionFunctionList['ISO-8859-1']['UTF-16LE'] = 'iconv_fallback_iso88591_utf16le';
  1183. $ConversionFunctionList['UTF-8']['ISO-8859-1'] = 'iconv_fallback_utf8_iso88591';
  1184. $ConversionFunctionList['UTF-8']['UTF-16'] = 'iconv_fallback_utf8_utf16';
  1185. $ConversionFunctionList['UTF-8']['UTF-16BE'] = 'iconv_fallback_utf8_utf16be';
  1186. $ConversionFunctionList['UTF-8']['UTF-16LE'] = 'iconv_fallback_utf8_utf16le';
  1187. $ConversionFunctionList['UTF-16']['ISO-8859-1'] = 'iconv_fallback_utf16_iso88591';
  1188. $ConversionFunctionList['UTF-16']['UTF-8'] = 'iconv_fallback_utf16_utf8';
  1189. $ConversionFunctionList['UTF-16LE']['ISO-8859-1'] = 'iconv_fallback_utf16le_iso88591';
  1190. $ConversionFunctionList['UTF-16LE']['UTF-8'] = 'iconv_fallback_utf16le_utf8';
  1191. $ConversionFunctionList['UTF-16BE']['ISO-8859-1'] = 'iconv_fallback_utf16be_iso88591';
  1192. $ConversionFunctionList['UTF-16BE']['UTF-8'] = 'iconv_fallback_utf16be_utf8';
  1193. }
  1194. if (isset($ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)])) {
  1195. $ConversionFunction = $ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)];
  1196. return self::$ConversionFunction($string);
  1197. }
  1198. throw new Exception('PHP does not has mb_convert_encoding() or iconv() support - cannot convert from '.$in_charset.' to '.$out_charset);
  1199. }
  1200. /**
  1201. * @param mixed $data
  1202. * @param string $charset
  1203. *
  1204. * @return mixed
  1205. */
  1206. public static function recursiveMultiByteCharString2HTML($data, $charset='ISO-8859-1') {
  1207. if (is_string($data)) {
  1208. return self::MultiByteCharString2HTML($data, $charset);
  1209. } elseif (is_array($data)) {
  1210. $return_data = array();
  1211. foreach ($data as $key => $value) {
  1212. $return_data[$key] = self::recursiveMultiByteCharString2HTML($value, $charset);
  1213. }
  1214. return $return_data;
  1215. }
  1216. // integer, float, objects, resources, etc
  1217. return $data;
  1218. }
  1219. /**
  1220. * @param string|int|float $string
  1221. * @param string $charset
  1222. *
  1223. * @return string
  1224. */
  1225. public static function MultiByteCharString2HTML($string, $charset='ISO-8859-1') {
  1226. $string = (string) $string; // in case trying to pass a numeric (float, int) string, would otherwise return an empty string
  1227. $HTMLstring = '';
  1228. switch (strtolower($charset)) {
  1229. case '1251':
  1230. case '1252':
  1231. case '866':
  1232. case '932':
  1233. case '936':
  1234. case '950':
  1235. case 'big5':
  1236. case 'big5-hkscs':
  1237. case 'cp1251':
  1238. case 'cp1252':
  1239. case 'cp866':
  1240. case 'euc-jp':
  1241. case 'eucjp':
  1242. case 'gb2312':
  1243. case 'ibm866':
  1244. case 'iso-8859-1':
  1245. case 'iso-8859-15':
  1246. case 'iso8859-1':
  1247. case 'iso8859-15':
  1248. case 'koi8-r':
  1249. case 'koi8-ru':
  1250. case 'koi8r':
  1251. case 'shift_jis':
  1252. case 'sjis':
  1253. case 'win-1251':
  1254. case 'windows-1251':
  1255. case 'windows-1252':
  1256. $HTMLstring = htmlentities($string, ENT_COMPAT, $charset);
  1257. break;
  1258. case 'utf-8':
  1259. $strlen = strlen($string);
  1260. for ($i = 0; $i < $strlen; $i++) {
  1261. $char_ord_val = ord($string[$i]);
  1262. $charval = 0;
  1263. if ($char_ord_val < 0x80) {
  1264. $charval = $char_ord_val;
  1265. } elseif ((($char_ord_val & 0xF0) >> 4) == 0x0F && $i+3 < $strlen) {
  1266. $charval = (($char_ord_val & 0x07) << 18);
  1267. $charval += ((ord($string[++$i]) & 0x3F) << 12);
  1268. $charval += ((ord($string[++$i]) & 0x3F) << 6);
  1269. $charval += (ord($string[++$i]) & 0x3F);
  1270. } elseif ((($char_ord_val & 0xE0) >> 5) == 0x07 && $i+2 < $strlen) {
  1271. $charval = (($char_ord_val & 0x0F) << 12);
  1272. $charval += ((ord($string[++$i]) & 0x3F) << 6);
  1273. $charval += (ord($string[++$i]) & 0x3F);
  1274. } elseif ((($char_ord_val & 0xC0) >> 6) == 0x03 && $i+1 < $strlen) {
  1275. $charval = (($char_ord_val & 0x1F) << 6);
  1276. $charval += (ord($string[++$i]) & 0x3F);
  1277. }
  1278. if (($charval >= 32) && ($charval <= 127)) {
  1279. $HTMLstring .= htmlentities(chr($charval));
  1280. } else {
  1281. $HTMLstring .= '&#'.$charval.';';
  1282. }
  1283. }
  1284. break;
  1285. case 'utf-16le':
  1286. for ($i = 0; $i < strlen($string); $i += 2) {
  1287. $charval = self::LittleEndian2Int(substr($string, $i, 2));
  1288. if (($charval >= 32) && ($charval <= 127)) {
  1289. $HTMLstring .= chr($charval);
  1290. } else {
  1291. $HTMLstring .= '&#'.$charval.';';
  1292. }
  1293. }
  1294. break;
  1295. case 'utf-16be':
  1296. for ($i = 0; $i < strlen($string); $i += 2) {
  1297. $charval = self::BigEndian2Int(substr($string, $i, 2));
  1298. if (($charval >= 32) && ($charval <= 127)) {
  1299. $HTMLstring .= chr($charval);
  1300. } else {
  1301. $HTMLstring .= '&#'.$charval.';';
  1302. }
  1303. }
  1304. break;
  1305. default:
  1306. $HTMLstring = 'ERROR: Character set "'.$charset.'" not supported in MultiByteCharString2HTML()';
  1307. break;
  1308. }
  1309. return $HTMLstring;
  1310. }
  1311. /**
  1312. * @param int $namecode
  1313. *
  1314. * @return string
  1315. */
  1316. public static function RGADnameLookup($namecode) {
  1317. static $RGADname = array();
  1318. if (empty($RGADname)) {
  1319. $RGADname[0] = 'not set';
  1320. $RGADname[1] = 'Track Gain Adjustment';
  1321. $RGADname[2] = 'Album Gain Adjustment';
  1322. }
  1323. return (isset($RGADname[$namecode]) ? $RGADname[$namecode] : '');
  1324. }
  1325. /**
  1326. * @param int $originatorcode
  1327. *
  1328. * @return string
  1329. */
  1330. public static function RGADoriginatorLookup($originatorcode) {
  1331. static $RGADoriginator = array();
  1332. if (empty($RGADoriginator)) {
  1333. $RGADoriginator[0] = 'unspecified';
  1334. $RGADoriginator[1] = 'pre-set by artist/producer/mastering engineer';
  1335. $RGADoriginator[2] = 'set by user';
  1336. $RGADoriginator[3] = 'determined automatically';
  1337. }
  1338. return (isset($RGADoriginator[$originatorcode]) ? $RGADoriginator[$originatorcode] : '');
  1339. }
  1340. /**
  1341. * @param int $rawadjustment
  1342. * @param int $signbit
  1343. *
  1344. * @return float
  1345. */
  1346. public static function RGADadjustmentLookup($rawadjustment, $signbit) {
  1347. $adjustment = (float) $rawadjustment / 10;
  1348. if ($signbit == 1) {
  1349. $adjustment *= -1;
  1350. }
  1351. return $adjustment;
  1352. }
  1353. /**
  1354. * @param int $namecode
  1355. * @param int $originatorcode
  1356. * @param int $replaygain
  1357. *
  1358. * @return string
  1359. */
  1360. public static function RGADgainString($namecode, $originatorcode, $replaygain) {
  1361. if ($replaygain < 0) {
  1362. $signbit = '1';
  1363. } else {
  1364. $signbit = '0';
  1365. }
  1366. $storedreplaygain = intval(round($replaygain * 10));
  1367. $gainstring = str_pad(decbin($namecode), 3, '0', STR_PAD_LEFT);
  1368. $gainstring .= str_pad(decbin($originatorcode), 3, '0', STR_PAD_LEFT);
  1369. $gainstring .= $signbit;
  1370. $gainstring .= str_pad(decbin($storedreplaygain), 9, '0', STR_PAD_LEFT);
  1371. return $gainstring;
  1372. }
  1373. /**
  1374. * @param float $amplitude
  1375. *
  1376. * @return float
  1377. */
  1378. public static function RGADamplitude2dB($amplitude) {
  1379. return 20 * log10($amplitude);
  1380. }
  1381. /**
  1382. * @param string $imgData
  1383. * @param array $imageinfo
  1384. *
  1385. * @return array|false
  1386. */
  1387. public static function GetDataImageSize($imgData, &$imageinfo=array()) {
  1388. if (PHP_VERSION_ID >= 50400) {
  1389. $GetDataImageSize = @getimagesizefromstring($imgData, $imageinfo);
  1390. if ($GetDataImageSize === false || !isset($GetDataImageSize[0], $GetDataImageSize[1])) {
  1391. return false;
  1392. }
  1393. $GetDataImageSize['height'] = $GetDataImageSize[0];
  1394. $GetDataImageSize['width'] = $GetDataImageSize[1];
  1395. return $GetDataImageSize;
  1396. }
  1397. static $tempdir = '';
  1398. if (empty($tempdir)) {
  1399. if (function_exists('sys_get_temp_dir')) {
  1400. $tempdir = sys_get_temp_dir(); // https://github.com/JamesHeinrich/getID3/issues/52
  1401. }
  1402. // yes this is ugly, feel free to suggest a better way
  1403. if (include_once(dirname(__FILE__).'/getid3.php')) {
  1404. $getid3_temp = new getID3();
  1405. if ($getid3_temp_tempdir = $getid3_temp->tempdir) {
  1406. $tempdir = $getid3_temp_tempdir;
  1407. }
  1408. unset($getid3_temp, $getid3_temp_tempdir);
  1409. }
  1410. }
  1411. $GetDataImageSize = false;
  1412. if ($tempfilename = tempnam($tempdir, 'gI3')) {
  1413. if (is_writable($tempfilename) && is_file($tempfilename) && ($tmp = fopen($tempfilename, 'wb'))) {
  1414. fwrite($tmp, $imgData);
  1415. fclose($tmp);
  1416. $GetDataImageSize = @getimagesize($tempfilename, $imageinfo);
  1417. if (($GetDataImageSize === false) || !isset($GetDataImageSize[0]) || !isset($GetDataImageSize[1])) {
  1418. return false;
  1419. }
  1420. $GetDataImageSize['height'] = $GetDataImageSize[0];
  1421. $GetDataImageSize['width'] = $GetDataImageSize[1];
  1422. }
  1423. unlink($tempfilename);
  1424. }
  1425. return $GetDataImageSize;
  1426. }
  1427. /**
  1428. * @param string $mime_type
  1429. *
  1430. * @return string
  1431. */
  1432. public static function ImageExtFromMime($mime_type) {
  1433. // temporary way, works OK for now, but should be reworked in the future
  1434. return str_replace(array('image/', 'x-', 'jpeg'), array('', '', 'jpg'), $mime_type);
  1435. }
  1436. /**
  1437. * @param array $ThisFileInfo
  1438. *
  1439. * @return bool
  1440. */
  1441. public static function CopyTagsToComments(&$ThisFileInfo) {
  1442. // Copy all entries from ['tags'] into common ['comments']
  1443. if (!empty($ThisFileInfo['tags'])) {
  1444. foreach ($ThisFileInfo['tags'] as $tagtype => $tagarray) {
  1445. foreach ($tagarray as $tagname => $tagdata) {
  1446. foreach ($tagdata as $key => $value) {
  1447. if (!empty($value)) {
  1448. if (empty($ThisFileInfo['comments'][$tagname])) {
  1449. // fall through and append value
  1450. } elseif ($tagtype == 'id3v1') {
  1451. $newvaluelength = strlen(trim($value));
  1452. foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
  1453. $oldvaluelength = strlen(trim($existingvalue));
  1454. if (($newvaluelength <= $oldvaluelength) && (substr($existingvalue, 0, $newvaluelength) == trim($value))) {
  1455. // new value is identical but shorter-than (or equal-length to) one already in comments - skip
  1456. break 2;
  1457. }
  1458. }
  1459. } elseif (!is_array($value)) {
  1460. $newvaluelength = strlen(trim($value));
  1461. foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
  1462. $oldvaluelength = strlen(trim($existingvalue));
  1463. if ((strlen($existingvalue) > 10) && ($newvaluelength > $oldvaluelength) && (substr(trim($value), 0, strlen($existingvalue)) == $existingvalue)) {
  1464. $ThisFileInfo['comments'][$tagname][$existingkey] = trim($value);
  1465. //break 2;
  1466. break;
  1467. }
  1468. }
  1469. }
  1470. if (is_array($value) || empty($ThisFileInfo['comments'][$tagname]) || !in_array(trim($value), $ThisFileInfo['comments'][$tagname])) {
  1471. $value = (is_string($value) ? trim($value) : $value);
  1472. if (!is_int($key) && !ctype_digit($key)) {
  1473. $ThisFileInfo['comments'][$tagname][$key] = $value;
  1474. } else {
  1475. if (!isset($ThisFileInfo['comments'][$tagname])) {
  1476. $ThisFileInfo['comments'][$tagname] = array($value);
  1477. } else {
  1478. $ThisFileInfo['comments'][$tagname][] = $value;
  1479. }
  1480. }
  1481. }
  1482. }
  1483. }
  1484. }
  1485. }
  1486. // attempt to standardize spelling of returned keys
  1487. $StandardizeFieldNames = array(
  1488. 'tracknumber' => 'track_number',
  1489. 'track' => 'track_number',
  1490. );
  1491. foreach ($StandardizeFieldNames as $badkey => $goodkey) {
  1492. if (array_key_exists($badkey, $ThisFileInfo['comments']) && !array_key_exists($goodkey, $ThisFileInfo['comments'])) {
  1493. $ThisFileInfo['comments'][$goodkey] = $ThisFileInfo['comments'][$badkey];
  1494. unset($ThisFileInfo['comments'][$badkey]);
  1495. }
  1496. }
  1497. // Copy to ['comments_html']
  1498. if (!empty($ThisFileInfo['comments'])) {
  1499. foreach ($ThisFileInfo['comments'] as $field => $values) {
  1500. if ($field == 'picture') {
  1501. // pictures can take up a lot of space, and we don't need multiple copies of them
  1502. // let there be a single copy in [comments][picture], and not elsewhere
  1503. continue;
  1504. }
  1505. foreach ($values as $index => $value) {
  1506. if (is_array($value)) {
  1507. $ThisFileInfo['comments_html'][$field][$index] = $value;
  1508. } else {
  1509. $ThisFileInfo['comments_html'][$field][$index] = str_replace('&#0;', '', self::MultiByteCharString2HTML($value, $ThisFileInfo['encoding']));
  1510. }
  1511. }
  1512. }
  1513. }
  1514. }
  1515. return true;
  1516. }
  1517. /**
  1518. * @param string $key
  1519. * @param int $begin
  1520. * @param int $end
  1521. * @param string $file
  1522. * @param string $name
  1523. *
  1524. * @return string
  1525. */
  1526. public static function EmbeddedLookup($key, $begin, $end, $file, $name) {
  1527. // Cached
  1528. static $cache;
  1529. if (isset($cache[$file][$name])) {
  1530. return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : '');
  1531. }
  1532. // Init
  1533. $keylength = strlen($key);
  1534. $line_count = $end - $begin - 7;
  1535. // Open php file
  1536. $fp = fopen($file, 'r');
  1537. // Discard $begin lines
  1538. for ($i = 0; $i < ($begin + 3); $i++) {
  1539. fgets($fp, 1024);
  1540. }
  1541. // Loop thru line
  1542. while (0 < $line_count--) {
  1543. // Read line
  1544. $line = ltrim(fgets($fp, 1024), "\t ");
  1545. // METHOD A: only cache the matching key - less memory but slower on next lookup of not-previously-looked-up key
  1546. //$keycheck = substr($line, 0, $keylength);
  1547. //if ($key == $keycheck) {
  1548. // $cache[$file][$name][$keycheck] = substr($line, $keylength + 1);
  1549. // break;
  1550. //}
  1551. // METHOD B: cache all keys in this lookup - more memory but faster on next lookup of not-previously-looked-up key
  1552. //$cache[$file][$name][substr($line, 0, $keylength)] = trim(substr($line, $keylength + 1));
  1553. $explodedLine = explode("\t", $line, 2);
  1554. $ThisKey = (isset($explodedLine[0]) ? $explodedLine[0] : '');
  1555. $ThisValue = (isset($explodedLine[1]) ? $explodedLine[1] : '');
  1556. $cache[$file][$name][$ThisKey] = trim($ThisValue);
  1557. }
  1558. // Close and return
  1559. fclose($fp);
  1560. return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : '');
  1561. }
  1562. /**
  1563. * @param string $filename
  1564. * @param string $sourcefile
  1565. * @param bool $DieOnFailure
  1566. *
  1567. * @return bool
  1568. * @throws Exception
  1569. */
  1570. public static function IncludeDependency($filename, $sourcefile, $DieOnFailure=false) {
  1571. global $GETID3_ERRORARRAY;
  1572. if (file_exists($filename)) {
  1573. if (include_once($filename)) {
  1574. return true;
  1575. } else {
  1576. $diemessage = basename($sourcefile).' depends on '.$filename.', which has errors';
  1577. }
  1578. } else {
  1579. $diemessage = basename($sourcefile).' depends on '.$filename.', which is missing';
  1580. }
  1581. if ($DieOnFailure) {
  1582. throw new Exception($diemessage);
  1583. } else {
  1584. $GETID3_ERRORARRAY[] = $diemessage;
  1585. }
  1586. return false;
  1587. }
  1588. /**
  1589. * @param string $string
  1590. *
  1591. * @return string
  1592. */
  1593. public static function trimNullByte($string) {
  1594. return trim($string, "\x00");
  1595. }
  1596. /**
  1597. * @param string $path
  1598. *
  1599. * @return float|bool
  1600. */
  1601. public static function getFileSizeSyscall($path) {
  1602. $filesize = false;
  1603. if (GETID3_OS_ISWINDOWS) {
  1604. if (class_exists('COM')) { // From PHP 5.3.15 and 5.4.5, COM and DOTNET is no longer built into the php core.you have to add COM support in php.ini:
  1605. $filesystem = new COM('Scripting.FileSystemObject');
  1606. $file = $filesystem->GetFile($path);
  1607. $filesize = $file->Size();
  1608. unset($filesystem, $file);
  1609. } else {
  1610. $commandli

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