PageRenderTime 68ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

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

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