getid3.lib.php 40 KB

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