getid3.lib.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808
  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))) {
  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 (($maxvalue === false) || ($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 (($minvalue === false) || ($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. if (PHP_VERSION_ID < 80000) {
  677. // http://websec.io/2012/08/27/Preventing-XEE-in-PHP.html
  678. // https://core.trac.wordpress.org/changeset/29378
  679. // This function has been deprecated in PHP 8.0 because in libxml 2.9.0, external entity loading is
  680. // disabled by default, so this function is no longer needed to protect against XXE attacks.
  681. $loader = libxml_disable_entity_loader(true);
  682. }
  683. $XMLobject = simplexml_load_string($XMLstring, 'SimpleXMLElement', LIBXML_NOENT);
  684. $return = self::SimpleXMLelement2array($XMLobject);
  685. if (PHP_VERSION_ID < 80000 && isset($loader)) {
  686. libxml_disable_entity_loader($loader);
  687. }
  688. return $return;
  689. }
  690. return false;
  691. }
  692. /**
  693. * @param SimpleXMLElement|array|mixed $XMLobject
  694. *
  695. * @return mixed
  696. */
  697. public static function SimpleXMLelement2array($XMLobject) {
  698. if (!is_object($XMLobject) && !is_array($XMLobject)) {
  699. return $XMLobject;
  700. }
  701. $XMLarray = $XMLobject instanceof SimpleXMLElement ? get_object_vars($XMLobject) : $XMLobject;
  702. foreach ($XMLarray as $key => $value) {
  703. $XMLarray[$key] = self::SimpleXMLelement2array($value);
  704. }
  705. return $XMLarray;
  706. }
  707. /**
  708. * Returns checksum for a file from starting position to absolute end position.
  709. *
  710. * @param string $file
  711. * @param int $offset
  712. * @param int $end
  713. * @param string $algorithm
  714. *
  715. * @return string|false
  716. * @throws getid3_exception
  717. */
  718. public static function hash_data($file, $offset, $end, $algorithm) {
  719. if (!self::intValueSupported($end)) {
  720. return false;
  721. }
  722. if (!in_array($algorithm, array('md5', 'sha1'))) {
  723. throw new getid3_exception('Invalid algorithm ('.$algorithm.') in self::hash_data()');
  724. }
  725. $size = $end - $offset;
  726. $fp = fopen($file, 'rb');
  727. fseek($fp, $offset);
  728. $ctx = hash_init($algorithm);
  729. while ($size > 0) {
  730. $buffer = fread($fp, min($size, getID3::FREAD_BUFFER_SIZE));
  731. hash_update($ctx, $buffer);
  732. $size -= getID3::FREAD_BUFFER_SIZE;
  733. }
  734. $hash = hash_final($ctx);
  735. fclose($fp);
  736. return $hash;
  737. }
  738. /**
  739. * @param string $filename_source
  740. * @param string $filename_dest
  741. * @param int $offset
  742. * @param int $length
  743. *
  744. * @return bool
  745. * @throws Exception
  746. *
  747. * @deprecated Unused, may be removed in future versions of getID3
  748. */
  749. public static function CopyFileParts($filename_source, $filename_dest, $offset, $length) {
  750. if (!self::intValueSupported($offset + $length)) {
  751. throw new Exception('cannot copy file portion, it extends beyond the '.round(PHP_INT_MAX / 1073741824).'GB limit');
  752. }
  753. if (is_readable($filename_source) && is_file($filename_source) && ($fp_src = fopen($filename_source, 'rb'))) {
  754. if (($fp_dest = fopen($filename_dest, 'wb'))) {
  755. if (fseek($fp_src, $offset) == 0) {
  756. $byteslefttowrite = $length;
  757. while (($byteslefttowrite > 0) && ($buffer = fread($fp_src, min($byteslefttowrite, getID3::FREAD_BUFFER_SIZE)))) {
  758. $byteswritten = fwrite($fp_dest, $buffer, $byteslefttowrite);
  759. $byteslefttowrite -= $byteswritten;
  760. }
  761. fclose($fp_dest);
  762. return true;
  763. } else {
  764. fclose($fp_src);
  765. throw new Exception('failed to seek to offset '.$offset.' in '.$filename_source);
  766. }
  767. } else {
  768. throw new Exception('failed to create file for writing '.$filename_dest);
  769. }
  770. } else {
  771. throw new Exception('failed to open file for reading '.$filename_source);
  772. }
  773. }
  774. /**
  775. * @param int $charval
  776. *
  777. * @return string
  778. */
  779. public static function iconv_fallback_int_utf8($charval) {
  780. if ($charval < 128) {
  781. // 0bbbbbbb
  782. $newcharstring = chr($charval);
  783. } elseif ($charval < 2048) {
  784. // 110bbbbb 10bbbbbb
  785. $newcharstring = chr(($charval >> 6) | 0xC0);
  786. $newcharstring .= chr(($charval & 0x3F) | 0x80);
  787. } elseif ($charval < 65536) {
  788. // 1110bbbb 10bbbbbb 10bbbbbb
  789. $newcharstring = chr(($charval >> 12) | 0xE0);
  790. $newcharstring .= chr(($charval >> 6) | 0xC0);
  791. $newcharstring .= chr(($charval & 0x3F) | 0x80);
  792. } else {
  793. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  794. $newcharstring = chr(($charval >> 18) | 0xF0);
  795. $newcharstring .= chr(($charval >> 12) | 0xC0);
  796. $newcharstring .= chr(($charval >> 6) | 0xC0);
  797. $newcharstring .= chr(($charval & 0x3F) | 0x80);
  798. }
  799. return $newcharstring;
  800. }
  801. /**
  802. * ISO-8859-1 => UTF-8
  803. *
  804. * @param string $string
  805. * @param bool $bom
  806. *
  807. * @return string
  808. */
  809. public static function iconv_fallback_iso88591_utf8($string, $bom=false) {
  810. if (function_exists('utf8_encode')) {
  811. return utf8_encode($string);
  812. }
  813. // utf8_encode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
  814. $newcharstring = '';
  815. if ($bom) {
  816. $newcharstring .= "\xEF\xBB\xBF";
  817. }
  818. for ($i = 0; $i < strlen($string); $i++) {
  819. $charval = ord($string[$i]);
  820. $newcharstring .= self::iconv_fallback_int_utf8($charval);
  821. }
  822. return $newcharstring;
  823. }
  824. /**
  825. * ISO-8859-1 => UTF-16BE
  826. *
  827. * @param string $string
  828. * @param bool $bom
  829. *
  830. * @return string
  831. */
  832. public static function iconv_fallback_iso88591_utf16be($string, $bom=false) {
  833. $newcharstring = '';
  834. if ($bom) {
  835. $newcharstring .= "\xFE\xFF";
  836. }
  837. for ($i = 0; $i < strlen($string); $i++) {
  838. $newcharstring .= "\x00".$string[$i];
  839. }
  840. return $newcharstring;
  841. }
  842. /**
  843. * ISO-8859-1 => UTF-16LE
  844. *
  845. * @param string $string
  846. * @param bool $bom
  847. *
  848. * @return string
  849. */
  850. public static function iconv_fallback_iso88591_utf16le($string, $bom=false) {
  851. $newcharstring = '';
  852. if ($bom) {
  853. $newcharstring .= "\xFF\xFE";
  854. }
  855. for ($i = 0; $i < strlen($string); $i++) {
  856. $newcharstring .= $string[$i]."\x00";
  857. }
  858. return $newcharstring;
  859. }
  860. /**
  861. * ISO-8859-1 => UTF-16LE (BOM)
  862. *
  863. * @param string $string
  864. *
  865. * @return string
  866. */
  867. public static function iconv_fallback_iso88591_utf16($string) {
  868. return self::iconv_fallback_iso88591_utf16le($string, true);
  869. }
  870. /**
  871. * UTF-8 => ISO-8859-1
  872. *
  873. * @param string $string
  874. *
  875. * @return string
  876. */
  877. public static function iconv_fallback_utf8_iso88591($string) {
  878. if (function_exists('utf8_decode')) {
  879. return utf8_decode($string);
  880. }
  881. // utf8_decode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
  882. $newcharstring = '';
  883. $offset = 0;
  884. $stringlength = strlen($string);
  885. while ($offset < $stringlength) {
  886. if ((ord($string[$offset]) | 0x07) == 0xF7) {
  887. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  888. $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) &
  889. ((ord($string[($offset + 1)]) & 0x3F) << 12) &
  890. ((ord($string[($offset + 2)]) & 0x3F) << 6) &
  891. (ord($string[($offset + 3)]) & 0x3F);
  892. $offset += 4;
  893. } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) {
  894. // 1110bbbb 10bbbbbb 10bbbbbb
  895. $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) &
  896. ((ord($string[($offset + 1)]) & 0x3F) << 6) &
  897. (ord($string[($offset + 2)]) & 0x3F);
  898. $offset += 3;
  899. } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) {
  900. // 110bbbbb 10bbbbbb
  901. $charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) &
  902. (ord($string[($offset + 1)]) & 0x3F);
  903. $offset += 2;
  904. } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) {
  905. // 0bbbbbbb
  906. $charval = ord($string[$offset]);
  907. $offset += 1;
  908. } else {
  909. // error? throw some kind of warning here?
  910. $charval = false;
  911. $offset += 1;
  912. }
  913. if ($charval !== false) {
  914. $newcharstring .= (($charval < 256) ? chr($charval) : '?');
  915. }
  916. }
  917. return $newcharstring;
  918. }
  919. /**
  920. * UTF-8 => UTF-16BE
  921. *
  922. * @param string $string
  923. * @param bool $bom
  924. *
  925. * @return string
  926. */
  927. public static function iconv_fallback_utf8_utf16be($string, $bom=false) {
  928. $newcharstring = '';
  929. if ($bom) {
  930. $newcharstring .= "\xFE\xFF";
  931. }
  932. $offset = 0;
  933. $stringlength = strlen($string);
  934. while ($offset < $stringlength) {
  935. if ((ord($string[$offset]) | 0x07) == 0xF7) {
  936. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  937. $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) &
  938. ((ord($string[($offset + 1)]) & 0x3F) << 12) &
  939. ((ord($string[($offset + 2)]) & 0x3F) << 6) &
  940. (ord($string[($offset + 3)]) & 0x3F);
  941. $offset += 4;
  942. } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) {
  943. // 1110bbbb 10bbbbbb 10bbbbbb
  944. $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) &
  945. ((ord($string[($offset + 1)]) & 0x3F) << 6) &
  946. (ord($string[($offset + 2)]) & 0x3F);
  947. $offset += 3;
  948. } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) {
  949. // 110bbbbb 10bbbbbb
  950. $charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) &
  951. (ord($string[($offset + 1)]) & 0x3F);
  952. $offset += 2;
  953. } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) {
  954. // 0bbbbbbb
  955. $charval = ord($string[$offset]);
  956. $offset += 1;
  957. } else {
  958. // error? throw some kind of warning here?
  959. $charval = false;
  960. $offset += 1;
  961. }
  962. if ($charval !== false) {
  963. $newcharstring .= (($charval < 65536) ? self::BigEndian2String($charval, 2) : "\x00".'?');
  964. }
  965. }
  966. return $newcharstring;
  967. }
  968. /**
  969. * UTF-8 => UTF-16LE
  970. *
  971. * @param string $string
  972. * @param bool $bom
  973. *
  974. * @return string
  975. */
  976. public static function iconv_fallback_utf8_utf16le($string, $bom=false) {
  977. $newcharstring = '';
  978. if ($bom) {
  979. $newcharstring .= "\xFF\xFE";
  980. }
  981. $offset = 0;
  982. $stringlength = strlen($string);
  983. while ($offset < $stringlength) {
  984. if ((ord($string[$offset]) | 0x07) == 0xF7) {
  985. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  986. $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) &
  987. ((ord($string[($offset + 1)]) & 0x3F) << 12) &
  988. ((ord($string[($offset + 2)]) & 0x3F) << 6) &
  989. (ord($string[($offset + 3)]) & 0x3F);
  990. $offset += 4;
  991. } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) {
  992. // 1110bbbb 10bbbbbb 10bbbbbb
  993. $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) &
  994. ((ord($string[($offset + 1)]) & 0x3F) << 6) &
  995. (ord($string[($offset + 2)]) & 0x3F);
  996. $offset += 3;
  997. } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) {
  998. // 110bbbbb 10bbbbbb
  999. $charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) &
  1000. (ord($string[($offset + 1)]) & 0x3F);
  1001. $offset += 2;
  1002. } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) {
  1003. // 0bbbbbbb
  1004. $charval = ord($string[$offset]);
  1005. $offset += 1;
  1006. } else {
  1007. // error? maybe throw some warning here?
  1008. $charval = false;
  1009. $offset += 1;
  1010. }
  1011. if ($charval !== false) {
  1012. $newcharstring .= (($charval < 65536) ? self::LittleEndian2String($charval, 2) : '?'."\x00");
  1013. }
  1014. }
  1015. return $newcharstring;
  1016. }
  1017. /**
  1018. * UTF-8 => UTF-16LE (BOM)
  1019. *
  1020. * @param string $string
  1021. *
  1022. * @return string
  1023. */
  1024. public static function iconv_fallback_utf8_utf16($string) {
  1025. return self::iconv_fallback_utf8_utf16le($string, true);
  1026. }
  1027. /**
  1028. * UTF-16BE => UTF-8
  1029. *
  1030. * @param string $string
  1031. *
  1032. * @return string
  1033. */
  1034. public static function iconv_fallback_utf16be_utf8($string) {
  1035. if (substr($string, 0, 2) == "\xFE\xFF") {
  1036. // strip BOM
  1037. $string = substr($string, 2);
  1038. }
  1039. $newcharstring = '';
  1040. for ($i = 0; $i < strlen($string); $i += 2) {
  1041. $charval = self::BigEndian2Int(substr($string, $i, 2));
  1042. $newcharstring .= self::iconv_fallback_int_utf8($charval);
  1043. }
  1044. return $newcharstring;
  1045. }
  1046. /**
  1047. * UTF-16LE => UTF-8
  1048. *
  1049. * @param string $string
  1050. *
  1051. * @return string
  1052. */
  1053. public static function iconv_fallback_utf16le_utf8($string) {
  1054. if (substr($string, 0, 2) == "\xFF\xFE") {
  1055. // strip BOM
  1056. $string = substr($string, 2);
  1057. }
  1058. $newcharstring = '';
  1059. for ($i = 0; $i < strlen($string); $i += 2) {
  1060. $charval = self::LittleEndian2Int(substr($string, $i, 2));
  1061. $newcharstring .= self::iconv_fallback_int_utf8($charval);
  1062. }
  1063. return $newcharstring;
  1064. }
  1065. /**
  1066. * UTF-16BE => ISO-8859-1
  1067. *
  1068. * @param string $string
  1069. *
  1070. * @return string
  1071. */
  1072. public static function iconv_fallback_utf16be_iso88591($string) {
  1073. if (substr($string, 0, 2) == "\xFE\xFF") {
  1074. // strip BOM
  1075. $string = substr($string, 2);
  1076. }
  1077. $newcharstring = '';
  1078. for ($i = 0; $i < strlen($string); $i += 2) {
  1079. $charval = self::BigEndian2Int(substr($string, $i, 2));
  1080. $newcharstring .= (($charval < 256) ? chr($charval) : '?');
  1081. }
  1082. return $newcharstring;
  1083. }
  1084. /**
  1085. * UTF-16LE => ISO-8859-1
  1086. *
  1087. * @param string $string
  1088. *
  1089. * @return string
  1090. */
  1091. public static function iconv_fallback_utf16le_iso88591($string) {
  1092. if (substr($string, 0, 2) == "\xFF\xFE") {
  1093. // strip BOM
  1094. $string = substr($string, 2);
  1095. }
  1096. $newcharstring = '';
  1097. for ($i = 0; $i < strlen($string); $i += 2) {
  1098. $charval = self::LittleEndian2Int(substr($string, $i, 2));
  1099. $newcharstring .= (($charval < 256) ? chr($charval) : '?');
  1100. }
  1101. return $newcharstring;
  1102. }
  1103. /**
  1104. * UTF-16 (BOM) => ISO-8859-1
  1105. *
  1106. * @param string $string
  1107. *
  1108. * @return string
  1109. */
  1110. public static function iconv_fallback_utf16_iso88591($string) {
  1111. $bom = substr($string, 0, 2);
  1112. if ($bom == "\xFE\xFF") {
  1113. return self::iconv_fallback_utf16be_iso88591(substr($string, 2));
  1114. } elseif ($bom == "\xFF\xFE") {
  1115. return self::iconv_fallback_utf16le_iso88591(substr($string, 2));
  1116. }
  1117. return $string;
  1118. }
  1119. /**
  1120. * UTF-16 (BOM) => UTF-8
  1121. *
  1122. * @param string $string
  1123. *
  1124. * @return string
  1125. */
  1126. public static function iconv_fallback_utf16_utf8($string) {
  1127. $bom = substr($string, 0, 2);
  1128. if ($bom == "\xFE\xFF") {
  1129. return self::iconv_fallback_utf16be_utf8(substr($string, 2));
  1130. } elseif ($bom == "\xFF\xFE") {
  1131. return self::iconv_fallback_utf16le_utf8(substr($string, 2));
  1132. }
  1133. return $string;
  1134. }
  1135. /**
  1136. * @param string $in_charset
  1137. * @param string $out_charset
  1138. * @param string $string
  1139. *
  1140. * @return string
  1141. * @throws Exception
  1142. */
  1143. public static function iconv_fallback($in_charset, $out_charset, $string) {
  1144. if ($in_charset == $out_charset) {
  1145. return $string;
  1146. }
  1147. // mb_convert_encoding() available
  1148. if (function_exists('mb_convert_encoding')) {
  1149. if ((strtoupper($in_charset) == 'UTF-16') && (substr($string, 0, 2) != "\xFE\xFF") && (substr($string, 0, 2) != "\xFF\xFE")) {
  1150. // if BOM missing, mb_convert_encoding will mishandle the conversion, assume UTF-16BE and prepend appropriate BOM
  1151. $string = "\xFF\xFE".$string;
  1152. }
  1153. if ((strtoupper($in_charset) == 'UTF-16') && (strtoupper($out_charset) == 'UTF-8')) {
  1154. if (($string == "\xFF\xFE") || ($string == "\xFE\xFF")) {
  1155. // if string consists of only BOM, mb_convert_encoding will return the BOM unmodified
  1156. return '';
  1157. }
  1158. }
  1159. if ($converted_string = @mb_convert_encoding($string, $out_charset, $in_charset)) {
  1160. switch ($out_charset) {
  1161. case 'ISO-8859-1':
  1162. $converted_string = rtrim($converted_string, "\x00");
  1163. break;
  1164. }
  1165. return $converted_string;
  1166. }
  1167. return $string;
  1168. // iconv() available
  1169. } elseif (function_exists('iconv')) {
  1170. if ($converted_string = @iconv($in_charset, $out_charset.'//TRANSLIT', $string)) {
  1171. switch ($out_charset) {
  1172. case 'ISO-8859-1':
  1173. $converted_string = rtrim($converted_string, "\x00");
  1174. break;
  1175. }
  1176. return $converted_string;
  1177. }
  1178. // iconv() may sometimes fail with "illegal character in input string" error message
  1179. // and return an empty string, but returning the unconverted string is more useful
  1180. return $string;
  1181. }
  1182. // neither mb_convert_encoding or iconv() is available
  1183. static $ConversionFunctionList = array();
  1184. if (empty($ConversionFunctionList)) {
  1185. $ConversionFunctionList['ISO-8859-1']['UTF-8'] = 'iconv_fallback_iso88591_utf8';
  1186. $ConversionFunctionList['ISO-8859-1']['UTF-16'] = 'iconv_fallback_iso88591_utf16';
  1187. $ConversionFunctionList['ISO-8859-1']['UTF-16BE'] = 'iconv_fallback_iso88591_utf16be';
  1188. $ConversionFunctionList['ISO-8859-1']['UTF-16LE'] = 'iconv_fallback_iso88591_utf16le';
  1189. $ConversionFunctionList['UTF-8']['ISO-8859-1'] = 'iconv_fallback_utf8_iso88591';
  1190. $ConversionFunctionList['UTF-8']['UTF-16'] = 'iconv_fallback_utf8_utf16';
  1191. $ConversionFunctionList['UTF-8']['UTF-16BE'] = 'iconv_fallback_utf8_utf16be';
  1192. $ConversionFunctionList['UTF-8']['UTF-16LE'] = 'iconv_fallback_utf8_utf16le';
  1193. $ConversionFunctionList['UTF-16']['ISO-8859-1'] = 'iconv_fallback_utf16_iso88591';
  1194. $ConversionFunctionList['UTF-16']['UTF-8'] = 'iconv_fallback_utf16_utf8';
  1195. $ConversionFunctionList['UTF-16LE']['ISO-8859-1'] = 'iconv_fallback_utf16le_iso88591';
  1196. $ConversionFunctionList['UTF-16LE']['UTF-8'] = 'iconv_fallback_utf16le_utf8';
  1197. $ConversionFunctionList['UTF-16BE']['ISO-8859-1'] = 'iconv_fallback_utf16be_iso88591';
  1198. $ConversionFunctionList['UTF-16BE']['UTF-8'] = 'iconv_fallback_utf16be_utf8';
  1199. }
  1200. if (isset($ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)])) {
  1201. $ConversionFunction = $ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)];
  1202. return self::$ConversionFunction($string);
  1203. }
  1204. throw new Exception('PHP does not has mb_convert_encoding() or iconv() support - cannot convert from '.$in_charset.' to '.$out_charset);
  1205. }
  1206. /**
  1207. * @param mixed $data
  1208. * @param string $charset
  1209. *
  1210. * @return mixed
  1211. */
  1212. public static function recursiveMultiByteCharString2HTML($data, $charset='ISO-8859-1') {
  1213. if (is_string($data)) {
  1214. return self::MultiByteCharString2HTML($data, $charset);
  1215. } elseif (is_array($data)) {
  1216. $return_data = array();
  1217. foreach ($data as $key => $value) {
  1218. $return_data[$key] = self::recursiveMultiByteCharString2HTML($value, $charset);
  1219. }
  1220. return $return_data;
  1221. }
  1222. // integer, float, objects, resources, etc
  1223. return $data;
  1224. }
  1225. /**
  1226. * @param string|int|float $string
  1227. * @param string $charset
  1228. *
  1229. * @return string
  1230. */
  1231. public static function MultiByteCharString2HTML($string, $charset='ISO-8859-1') {
  1232. $string = (string) $string; // in case trying to pass a numeric (float, int) string, would otherwise return an empty string
  1233. $HTMLstring = '';
  1234. switch (strtolower($charset)) {
  1235. case '1251':
  1236. case '1252':
  1237. case '866':
  1238. case '932':
  1239. case '936':
  1240. case '950':
  1241. case 'big5':
  1242. case 'big5-hkscs':
  1243. case 'cp1251':
  1244. case 'cp1252':
  1245. case 'cp866':
  1246. case 'euc-jp':
  1247. case 'eucjp':
  1248. case 'gb2312':
  1249. case 'ibm866':
  1250. case 'iso-8859-1':
  1251. case 'iso-8859-15':
  1252. case 'iso8859-1':
  1253. case 'iso8859-15':
  1254. case 'koi8-r':
  1255. case 'koi8-ru':
  1256. case 'koi8r':
  1257. case 'shift_jis':
  1258. case 'sjis':
  1259. case 'win-1251':
  1260. case 'windows-1251':
  1261. case 'windows-1252':
  1262. $HTMLstring = htmlentities($string, ENT_COMPAT, $charset);
  1263. break;
  1264. case 'utf-8':
  1265. $strlen = strlen($string);
  1266. for ($i = 0; $i < $strlen; $i++) {
  1267. $char_ord_val = ord($string[$i]);
  1268. $charval = 0;
  1269. if ($char_ord_val < 0x80) {
  1270. $charval = $char_ord_val;
  1271. } elseif ((($char_ord_val & 0xF0) >> 4) == 0x0F && $i+3 < $strlen) {
  1272. $charval = (($char_ord_val & 0x07) << 18);
  1273. $charval += ((ord($string[++$i]) & 0x3F) << 12);
  1274. $charval += ((ord($string[++$i]) & 0x3F) << 6);
  1275. $charval += (ord($string[++$i]) & 0x3F);
  1276. } elseif ((($char_ord_val & 0xE0) >> 5) == 0x07 && $i+2 < $strlen) {
  1277. $charval = (($char_ord_val & 0x0F) << 12);
  1278. $charval += ((ord($string[++$i]) & 0x3F) << 6);
  1279. $charval += (ord($string[++$i]) & 0x3F);
  1280. } elseif ((($char_ord_val & 0xC0) >> 6) == 0x03 && $i+1 < $strlen) {
  1281. $charval = (($char_ord_val & 0x1F) << 6);
  1282. $charval += (ord($string[++$i]) & 0x3F);
  1283. }
  1284. if (($charval >= 32) && ($charval <= 127)) {
  1285. $HTMLstring .= htmlentities(chr($charval));
  1286. } else {
  1287. $HTMLstring .= '&#'.$charval.';';
  1288. }
  1289. }
  1290. break;
  1291. case 'utf-16le':
  1292. for ($i = 0; $i < strlen($string); $i += 2) {
  1293. $charval = self::LittleEndian2Int(substr($string, $i, 2));
  1294. if (($charval >= 32) && ($charval <= 127)) {
  1295. $HTMLstring .= chr($charval);
  1296. } else {
  1297. $HTMLstring .= '&#'.$charval.';';
  1298. }
  1299. }
  1300. break;
  1301. case 'utf-16be':
  1302. for ($i = 0; $i < strlen($string); $i += 2) {
  1303. $charval = self::BigEndian2Int(substr($string, $i, 2));
  1304. if (($charval >= 32) && ($charval <= 127)) {
  1305. $HTMLstring .= chr($charval);
  1306. } else {
  1307. $HTMLstring .= '&#'.$charval.';';
  1308. }
  1309. }
  1310. break;
  1311. default:
  1312. $HTMLstring = 'ERROR: Character set "'.$charset.'" not supported in MultiByteCharString2HTML()';
  1313. break;
  1314. }
  1315. return $HTMLstring;
  1316. }
  1317. /**
  1318. * @param int $namecode
  1319. *
  1320. * @return string
  1321. */
  1322. public static function RGADnameLookup($namecode) {
  1323. static $RGADname = array();
  1324. if (empty($RGADname)) {
  1325. $RGADname[0] = 'not set';
  1326. $RGADname[1] = 'Track Gain Adjustment';
  1327. $RGADname[2] = 'Album Gain Adjustment';
  1328. }
  1329. return (isset($RGADname[$namecode]) ? $RGADname[$namecode] : '');
  1330. }
  1331. /**
  1332. * @param int $originatorcode
  1333. *
  1334. * @return string
  1335. */
  1336. public static function RGADoriginatorLookup($originatorcode) {
  1337. static $RGADoriginator = array();
  1338. if (empty($RGADoriginator)) {
  1339. $RGADoriginator[0] = 'unspecified';
  1340. $RGADoriginator[1] = 'pre-set by artist/producer/mastering engineer';
  1341. $RGADoriginator[2] = 'set by user';
  1342. $RGADoriginator[3] = 'determined automatically';
  1343. }
  1344. return (isset($RGADoriginator[$originatorcode]) ? $RGADoriginator[$originatorcode] : '');
  1345. }
  1346. /**
  1347. * @param int $rawadjustment
  1348. * @param int $signbit
  1349. *
  1350. * @return float
  1351. */
  1352. public static function RGADadjustmentLookup($rawadjustment, $signbit) {
  1353. $adjustment = (float) $rawadjustment / 10;
  1354. if ($signbit == 1) {
  1355. $adjustment *= -1;
  1356. }
  1357. return $adjustment;
  1358. }
  1359. /**
  1360. * @param int $namecode
  1361. * @param int $originatorcode
  1362. * @param int $replaygain
  1363. *
  1364. * @return string
  1365. */
  1366. public static function RGADgainString($namecode, $originatorcode, $replaygain) {
  1367. if ($replaygain < 0) {
  1368. $signbit = '1';
  1369. } else {
  1370. $signbit = '0';
  1371. }
  1372. $storedreplaygain = intval(round($replaygain * 10));
  1373. $gainstring = str_pad(decbin($namecode), 3, '0', STR_PAD_LEFT);
  1374. $gainstring .= str_pad(decbin($originatorcode), 3, '0', STR_PAD_LEFT);
  1375. $gainstring .= $signbit;
  1376. $gainstring .= str_pad(decbin($storedreplaygain), 9, '0', STR_PAD_LEFT);
  1377. return $gainstring;
  1378. }
  1379. /**
  1380. * @param float $amplitude
  1381. *
  1382. * @return float
  1383. */
  1384. public static function RGADamplitude2dB($amplitude) {
  1385. return 20 * log10($amplitude);
  1386. }
  1387. /**
  1388. * @param string $imgData
  1389. * @param array $imageinfo
  1390. *
  1391. * @return array|false
  1392. */
  1393. public static function GetDataImageSize($imgData, &$imageinfo=array()) {
  1394. if (PHP_VERSION_ID >= 50400) {
  1395. $GetDataImageSize = @getimagesizefromstring($imgData, $imageinfo);
  1396. if ($GetDataImageSize === false || !isset($GetDataImageSize[0], $GetDataImageSize[1])) {
  1397. return false;
  1398. }
  1399. $GetDataImageSize['height'] = $GetDataImageSize[0];
  1400. $GetDataImageSize['width'] = $GetDataImageSize[1];
  1401. return $GetDataImageSize;
  1402. }
  1403. static $tempdir = '';
  1404. if (empty($tempdir)) {
  1405. if (function_exists('sys_get_temp_dir')) {
  1406. $tempdir = sys_get_temp_dir(); // https://github.com/JamesHeinrich/getID3/issues/52
  1407. }
  1408. // yes this is ugly, feel free to suggest a better way
  1409. if (include_once(dirname(__FILE__).'/getid3.php')) {
  1410. $getid3_temp = new getID3();
  1411. if ($getid3_temp_tempdir = $getid3_temp->tempdir) {
  1412. $tempdir = $getid3_temp_tempdir;
  1413. }
  1414. unset($getid3_temp, $getid3_temp_tempdir);
  1415. }
  1416. }
  1417. $GetDataImageSize = false;
  1418. if ($tempfilename = tempnam($tempdir, 'gI3')) {
  1419. if (is_writable($tempfilename) && is_file($tempfilename) && ($tmp = fopen($tempfilename, 'wb'))) {
  1420. fwrite($tmp, $imgData);
  1421. fclose($tmp);
  1422. $GetDataImageSize = @getimagesize($tempfilename, $imageinfo);
  1423. if (($GetDataImageSize === false) || !isset($GetDataImageSize[0]) || !isset($GetDataImageSize[1])) {
  1424. return false;
  1425. }
  1426. $GetDataImageSize['height'] = $GetDataImageSize[0];
  1427. $GetDataImageSize['width'] = $GetDataImageSize[1];
  1428. }
  1429. unlink($tempfilename);
  1430. }
  1431. return $GetDataImageSize;
  1432. }
  1433. /**
  1434. * @param string $mime_type
  1435. *
  1436. * @return string
  1437. */
  1438. public static function ImageExtFromMime($mime_type) {
  1439. // temporary way, works OK for now, but should be reworked in the future
  1440. return str_replace(array('image/', 'x-', 'jpeg'), array('', '', 'jpg'), $mime_type);
  1441. }
  1442. /**
  1443. * @param array $ThisFileInfo
  1444. * @param bool $option_tags_html default true (just as in the main getID3 class)
  1445. *
  1446. * @return bool
  1447. */
  1448. public static function CopyTagsToComments(&$ThisFileInfo, $option_tags_html=true) {
  1449. // Copy all entries from ['tags'] into common ['comments']
  1450. if (!empty($ThisFileInfo['tags'])) {
  1451. if (isset($ThisFileInfo['tags']['id3v1'])) {
  1452. // bubble ID3v1 to the end, if present to aid in detecting bad ID3v1 encodings
  1453. $ID3v1 = $ThisFileInfo['tags']['id3v1'];
  1454. unset($ThisFileInfo['tags']['id3v1']);
  1455. $ThisFileInfo['tags']['id3v1'] = $ID3v1;
  1456. unset($ID3v1);
  1457. }
  1458. foreach ($ThisFileInfo['tags'] as $tagtype => $tagarray) {
  1459. foreach ($tagarray as $tagname => $tagdata) {
  1460. foreach ($tagdata as $key => $value) {
  1461. if (!empty($value)) {
  1462. if (empty($ThisFileInfo['comments'][$tagname])) {
  1463. // fall through and append value
  1464. } elseif ($tagtype == 'id3v1') {
  1465. $newvaluelength = strlen(trim($value));
  1466. foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
  1467. $oldvaluelength = strlen(trim($existingvalue));
  1468. if (($newvaluelength <= $oldvaluelength) && (substr($existingvalue, 0, $newvaluelength) == trim($value))) {
  1469. // new value is identical but shorter-than (or equal-length to) one already in comments - skip
  1470. break 2;
  1471. }
  1472. if (function_exists('mb_convert_encoding')) {
  1473. if (trim($value) == trim(substr(mb_convert_encoding($existingvalue, $ThisFileInfo['id3v1']['encoding'], $ThisFileInfo['encoding']), 0, 30))) {
  1474. // value stored in ID3v1 appears to be probably the multibyte value transliterated (badly) into ISO-8859-1 in ID3v1.
  1475. // As an example, Foobar2000 will do this if you tag a file with Chinese or Arabic or Cyrillic or something that doesn't fit into ISO-8859-1 the ID3v1 will consist of mostly "?" characters, one per multibyte unrepresentable character
  1476. break 2;
  1477. }
  1478. }
  1479. }
  1480. } elseif (!is_array($value)) {
  1481. $newvaluelength = strlen(trim($value));
  1482. foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
  1483. $oldvaluelength = strlen(trim($existingvalue));
  1484. if ((strlen($existingvalue) > 10) && ($newvaluelength > $oldvaluelength) && (substr(trim($value), 0, strlen($existingvalue)) == $existingvalue)) {
  1485. $ThisFileInfo['comments'][$tagname][$existingkey] = trim($value);
  1486. break;
  1487. }
  1488. }
  1489. }
  1490. if (is_array($value) || empty($ThisFileInfo['comments'][$tagname]) || !in_array(trim($value), $ThisFileInfo['comments'][$tagname])) {
  1491. $value = (is_string($value) ? trim($value) : $value);
  1492. if (!is_int($key) && !ctype_digit($key)) {
  1493. $ThisFileInfo['comments'][$tagname][$key] = $value;
  1494. } else {
  1495. if (!isset($ThisFileInfo['comments'][$tagname])) {
  1496. $ThisFileInfo['comments'][$tagname] = array($value);
  1497. } else {
  1498. $ThisFileInfo['comments'][$tagname][] = $value;
  1499. }
  1500. }
  1501. }
  1502. }
  1503. }
  1504. }
  1505. }
  1506. // attempt to standardize spelling of returned keys
  1507. $StandardizeFieldNames = array(
  1508. 'tracknumber' => 'track_number',
  1509. 'track' => 'track_number',
  1510. );
  1511. foreach ($StandardizeFieldNames as $badkey => $goodkey) {
  1512. if (array_key_exists($badkey, $ThisFileInfo['comments']) && !array_key_exists($goodkey, $ThisFileInfo['comments'])) {
  1513. $ThisFileInfo['comments'][$goodkey] = $ThisFileInfo['comments'][$badkey];
  1514. unset($ThisFileInfo['comments'][$badkey]);
  1515. }
  1516. }
  1517. if ($option_tags_html) {
  1518. // Copy ['comments'] to ['comments_html']
  1519. if (!empty($ThisFileInfo['comments'])) {
  1520. foreach ($ThisFileInfo['comments'] as $field => $values) {
  1521. if ($field == 'picture') {
  1522. // pictures can take up a lot of space, and we don't need multiple copies of them
  1523. // let there be a single copy in [comments][picture], and not elsewhere
  1524. continue;
  1525. }
  1526. foreach ($values as $index => $value) {
  1527. if (is_array($value)) {
  1528. $ThisFileInfo['comments_html'][$field][$index] = $value;
  1529. } else {
  1530. $ThisFileInfo['comments_html'][$field][$index] = str_replace('&#0;', '', self::MultiByteCharString2HTML($value, $ThisFileInfo['encoding']));
  1531. }
  1532. }
  1533. }
  1534. }
  1535. }
  1536. }
  1537. return true;
  1538. }
  1539. /**
  1540. * @param string $key
  1541. * @param int $begin
  1542. * @param int $end
  1543. * @param string $file
  1544. * @param string $name
  1545. *
  1546. * @return string
  1547. */
  1548. public static function EmbeddedLookup($key, $begin, $end, $file, $name) {
  1549. // Cached
  1550. static $cache;
  1551. if (isset($cache[$file][$name])) {
  1552. return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : '');
  1553. }
  1554. // Init
  1555. $keylength = strlen($key);
  1556. $line_count = $end - $begin - 7;
  1557. // Open php file
  1558. $fp = fopen($file, 'r');
  1559. // Discard $begin lines
  1560. for ($i = 0; $i < ($begin + 3); $i++) {
  1561. fgets($fp, 1024);
  1562. }
  1563. // Loop thru line
  1564. while (0 < $line_count--) {
  1565. // Read line
  1566. $line = ltrim(fgets($fp, 1024), "\t ");
  1567. // METHOD A: only cache the matching key - less memory but slower on next lookup of not-previously-looked-up key
  1568. //$keycheck = substr($line, 0, $keylength);
  1569. //if ($key == $keycheck) {
  1570. // $cache[$file][$name][$keycheck] = substr($line, $keylength + 1);
  1571. // break;
  1572. //}
  1573. // METHOD B: cache all keys in this lookup - more memory but faster on next lookup of not-previously-looked-up key
  1574. //$cache[$file][$name][substr($line, 0, $keylength)] = trim(substr($line, $keylength + 1));
  1575. $explodedLine = explode("\t", $line, 2);
  1576. $ThisKey = (isset($explodedLine[0]) ? $explodedLine[0] : '');
  1577. $ThisValue = (isset($explodedLine[1]) ? $explodedLine[1] : '');
  1578. $cache[$file][$name][$ThisKey] = trim($ThisValue);
  1579. }
  1580. // Close and return
  1581. fclose($fp);
  1582. return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : '');
  1583. }
  1584. /**
  1585. * @param string $filename
  1586. * @param string $sourcefile
  1587. * @param bool $DieOnFailure
  1588. *
  1589. * @return bool
  1590. * @throws Exception
  1591. */
  1592. public static function IncludeDependency($filename, $sourcefile, $DieOnFailure=false) {
  1593. global $GETID3_ERRORARRAY;
  1594. if (file_exists($filename)) {
  1595. if (include_once($filename)) {
  1596. return true;
  1597. } else {
  1598. $diemessage = basename($sourcefile).' depends on '.$filename.', which has errors';
  1599. }
  1600. } else {
  1601. $diemessage = basename($sourcefile).' depends on '.$filename.', which is missing';
  1602. }
  1603. if ($DieOnFailure) {
  1604. throw new Exception($diemessage);
  1605. } else {
  1606. $GETID3_ERRORARRAY[] = $diemessage;
  1607. }
  1608. return false;
  1609. }
  1610. /**
  1611. * @param string $string
  1612. *
  1613. * @return string
  1614. */
  1615. public static function trimNullByte($string) {
  1616. return trim($string, "\x00");
  1617. }
  1618. /**
  1619. * @param string $path
  1620. *
  1621. * @return float|bool
  1622. */
  1623. public static function getFileSizeSyscall($path) {
  1624. $filesize = false;
  1625. if (GETID3_OS_ISWINDOWS) {
  1626. 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:
  1627. $filesystem = new COM('Scripting.FileSystemObject');
  1628. $file = $filesystem->GetFile($path);
  1629. $filesize = $file->Size();
  1630. unset($filesystem, $file);
  1631. } else {
  1632. $commandline = 'for %I in ('.escapeshellarg($path).') do @echo %~zI';
  1633. }
  1634. } else {
  1635. $commandline = 'ls -l '.escapeshellarg($path).' | awk \'{print $5}\'';
  1636. }
  1637. if (isset($commandline)) {
  1638. $output = trim(`$commandline`);
  1639. if (ctype_digit($output)) {
  1640. $filesize = (float) $output;
  1641. }
  1642. }
  1643. return $filesize;
  1644. }
  1645. /**
  1646. * @param string $filename
  1647. *
  1648. * @return string|false
  1649. */
  1650. public static function truepath($filename) {
  1651. // 2017-11-08: this could use some improvement, patches welcome
  1652. if (preg_match('#^(\\\\\\\\|//)[a-z0-9]#i', $filename, $matches)) {
  1653. // PHP's built-in realpath function does not work on UNC Windows shares
  1654. $goodpath = array();
  1655. foreach (explode('/', str_replace('\\', '/', $filename)) as $part) {
  1656. if ($part == '.') {
  1657. continue;
  1658. }
  1659. if ($part == '..') {
  1660. if (count($goodpath)) {
  1661. array_pop($goodpath);
  1662. } else {
  1663. // cannot step above this level, already at top level
  1664. return false;
  1665. }
  1666. } else {
  1667. $goodpath[] = $part;
  1668. }
  1669. }
  1670. return implode(DIRECTORY_SEPARATOR, $goodpath);
  1671. }
  1672. return realpath($filename);
  1673. }
  1674. /**
  1675. * Workaround for Bug #37268 (https://bugs.php.net/bug.php?id=37268)
  1676. *
  1677. * @param string $path A path.
  1678. * @param string $suffix If the name component ends in suffix this will also be cut off.
  1679. *
  1680. * @return string
  1681. */
  1682. public static function mb_basename($path, $suffix = null) {
  1683. $splited = preg_split('#/#', rtrim($path, '/ '));
  1684. return substr(basename('X'.$splited[count($splited) - 1], $suffix), 1);
  1685. }
  1686. }