PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/jessehall/hudson_alpha
PHP | 1342 lines | 1071 code | 153 blank | 118 comment | 264 complexity | 73f2602010116203397b414bd194da56 MD5 | raw file
Possible License(s): GPL-2.0
  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' ) && function_exists( 'libxml_disable_entity_loader' ) ) {
  454. $loader = libxml_disable_entity_loader( true );
  455. $XMLobject = simplexml_load_string( $XMLstring, 'SimpleXMLElement', LIBXML_NOENT );
  456. $return = self::SimpleXMLelement2array( $XMLobject );
  457. libxml_disable_entity_loader( $loader );
  458. return $return;
  459. }
  460. return false;
  461. }
  462. public static function SimpleXMLelement2array($XMLobject) {
  463. if (!is_object($XMLobject) && !is_array($XMLobject)) {
  464. return $XMLobject;
  465. }
  466. $XMLarray = (is_object($XMLobject) ? get_object_vars($XMLobject) : $XMLobject);
  467. foreach ($XMLarray as $key => $value) {
  468. $XMLarray[$key] = self::SimpleXMLelement2array($value);
  469. }
  470. return $XMLarray;
  471. }
  472. // Allan Hansen <ahร˜artemis*dk>
  473. // self::md5_data() - returns md5sum for a file from startuing position to absolute end position
  474. public static function hash_data($file, $offset, $end, $algorithm) {
  475. static $tempdir = '';
  476. if (!self::intValueSupported($end)) {
  477. return false;
  478. }
  479. switch ($algorithm) {
  480. case 'md5':
  481. $hash_function = 'md5_file';
  482. $unix_call = 'md5sum';
  483. $windows_call = 'md5sum.exe';
  484. $hash_length = 32;
  485. break;
  486. case 'sha1':
  487. $hash_function = 'sha1_file';
  488. $unix_call = 'sha1sum';
  489. $windows_call = 'sha1sum.exe';
  490. $hash_length = 40;
  491. break;
  492. default:
  493. throw new Exception('Invalid algorithm ('.$algorithm.') in self::hash_data()');
  494. break;
  495. }
  496. $size = $end - $offset;
  497. while (true) {
  498. if (GETID3_OS_ISWINDOWS) {
  499. // It seems that sha1sum.exe for Windows only works on physical files, does not accept piped data
  500. // Fall back to create-temp-file method:
  501. if ($algorithm == 'sha1') {
  502. break;
  503. }
  504. $RequiredFiles = array('cygwin1.dll', 'head.exe', 'tail.exe', $windows_call);
  505. foreach ($RequiredFiles as $required_file) {
  506. if (!is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
  507. // helper apps not available - fall back to old method
  508. break 2;
  509. }
  510. }
  511. $commandline = GETID3_HELPERAPPSDIR.'head.exe -c '.$end.' '.escapeshellarg(str_replace('/', DIRECTORY_SEPARATOR, $file)).' | ';
  512. $commandline .= GETID3_HELPERAPPSDIR.'tail.exe -c '.$size.' | ';
  513. $commandline .= GETID3_HELPERAPPSDIR.$windows_call;
  514. } else {
  515. $commandline = 'head -c'.$end.' '.escapeshellarg($file).' | ';
  516. $commandline .= 'tail -c'.$size.' | ';
  517. $commandline .= $unix_call;
  518. }
  519. if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
  520. //throw new Exception('PHP running in Safe Mode - backtick operator not available, using slower non-system-call '.$algorithm.' algorithm');
  521. break;
  522. }
  523. return substr(`$commandline`, 0, $hash_length);
  524. }
  525. if (empty($tempdir)) {
  526. // yes this is ugly, feel free to suggest a better way
  527. require_once(dirname(__FILE__).'/getid3.php');
  528. $getid3_temp = new getID3();
  529. $tempdir = $getid3_temp->tempdir;
  530. unset($getid3_temp);
  531. }
  532. // try to create a temporary file in the system temp directory - invalid dirname should force to system temp dir
  533. if (($data_filename = tempnam($tempdir, 'gI3')) === false) {
  534. // can't find anywhere to create a temp file, just fail
  535. return false;
  536. }
  537. // Init
  538. $result = false;
  539. // copy parts of file
  540. try {
  541. self::CopyFileParts($file, $data_filename, $offset, $end - $offset);
  542. $result = $hash_function($data_filename);
  543. } catch (Exception $e) {
  544. throw new Exception('self::CopyFileParts() failed in getid_lib::hash_data(): '.$e->getMessage());
  545. }
  546. unlink($data_filename);
  547. return $result;
  548. }
  549. public static function CopyFileParts($filename_source, $filename_dest, $offset, $length) {
  550. if (!self::intValueSupported($offset + $length)) {
  551. throw new Exception('cannot copy file portion, it extends beyond the '.round(PHP_INT_MAX / 1073741824).'GB limit');
  552. }
  553. if (is_readable($filename_source) && is_file($filename_source) && ($fp_src = fopen($filename_source, 'rb'))) {
  554. if (($fp_dest = fopen($filename_dest, 'wb'))) {
  555. if (fseek($fp_src, $offset, SEEK_SET) == 0) {
  556. $byteslefttowrite = $length;
  557. while (($byteslefttowrite > 0) && ($buffer = fread($fp_src, min($byteslefttowrite, getID3::FREAD_BUFFER_SIZE)))) {
  558. $byteswritten = fwrite($fp_dest, $buffer, $byteslefttowrite);
  559. $byteslefttowrite -= $byteswritten;
  560. }
  561. return true;
  562. } else {
  563. throw new Exception('failed to seek to offset '.$offset.' in '.$filename_source);
  564. }
  565. fclose($fp_dest);
  566. } else {
  567. throw new Exception('failed to create file for writing '.$filename_dest);
  568. }
  569. fclose($fp_src);
  570. } else {
  571. throw new Exception('failed to open file for reading '.$filename_source);
  572. }
  573. return false;
  574. }
  575. public static function iconv_fallback_int_utf8($charval) {
  576. if ($charval < 128) {
  577. // 0bbbbbbb
  578. $newcharstring = chr($charval);
  579. } elseif ($charval < 2048) {
  580. // 110bbbbb 10bbbbbb
  581. $newcharstring = chr(($charval >> 6) | 0xC0);
  582. $newcharstring .= chr(($charval & 0x3F) | 0x80);
  583. } elseif ($charval < 65536) {
  584. // 1110bbbb 10bbbbbb 10bbbbbb
  585. $newcharstring = chr(($charval >> 12) | 0xE0);
  586. $newcharstring .= chr(($charval >> 6) | 0xC0);
  587. $newcharstring .= chr(($charval & 0x3F) | 0x80);
  588. } else {
  589. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  590. $newcharstring = chr(($charval >> 18) | 0xF0);
  591. $newcharstring .= chr(($charval >> 12) | 0xC0);
  592. $newcharstring .= chr(($charval >> 6) | 0xC0);
  593. $newcharstring .= chr(($charval & 0x3F) | 0x80);
  594. }
  595. return $newcharstring;
  596. }
  597. // ISO-8859-1 => UTF-8
  598. public static function iconv_fallback_iso88591_utf8($string, $bom=false) {
  599. if (function_exists('utf8_encode')) {
  600. return utf8_encode($string);
  601. }
  602. // utf8_encode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
  603. $newcharstring = '';
  604. if ($bom) {
  605. $newcharstring .= "\xEF\xBB\xBF";
  606. }
  607. for ($i = 0; $i < strlen($string); $i++) {
  608. $charval = ord($string{$i});
  609. $newcharstring .= self::iconv_fallback_int_utf8($charval);
  610. }
  611. return $newcharstring;
  612. }
  613. // ISO-8859-1 => UTF-16BE
  614. public static function iconv_fallback_iso88591_utf16be($string, $bom=false) {
  615. $newcharstring = '';
  616. if ($bom) {
  617. $newcharstring .= "\xFE\xFF";
  618. }
  619. for ($i = 0; $i < strlen($string); $i++) {
  620. $newcharstring .= "\x00".$string{$i};
  621. }
  622. return $newcharstring;
  623. }
  624. // ISO-8859-1 => UTF-16LE
  625. public static function iconv_fallback_iso88591_utf16le($string, $bom=false) {
  626. $newcharstring = '';
  627. if ($bom) {
  628. $newcharstring .= "\xFF\xFE";
  629. }
  630. for ($i = 0; $i < strlen($string); $i++) {
  631. $newcharstring .= $string{$i}."\x00";
  632. }
  633. return $newcharstring;
  634. }
  635. // ISO-8859-1 => UTF-16LE (BOM)
  636. public static function iconv_fallback_iso88591_utf16($string) {
  637. return self::iconv_fallback_iso88591_utf16le($string, true);
  638. }
  639. // UTF-8 => ISO-8859-1
  640. public static function iconv_fallback_utf8_iso88591($string) {
  641. if (function_exists('utf8_decode')) {
  642. return utf8_decode($string);
  643. }
  644. // utf8_decode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
  645. $newcharstring = '';
  646. $offset = 0;
  647. $stringlength = strlen($string);
  648. while ($offset < $stringlength) {
  649. if ((ord($string{$offset}) | 0x07) == 0xF7) {
  650. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  651. $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
  652. ((ord($string{($offset + 1)}) & 0x3F) << 12) &
  653. ((ord($string{($offset + 2)}) & 0x3F) << 6) &
  654. (ord($string{($offset + 3)}) & 0x3F);
  655. $offset += 4;
  656. } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
  657. // 1110bbbb 10bbbbbb 10bbbbbb
  658. $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
  659. ((ord($string{($offset + 1)}) & 0x3F) << 6) &
  660. (ord($string{($offset + 2)}) & 0x3F);
  661. $offset += 3;
  662. } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
  663. // 110bbbbb 10bbbbbb
  664. $charval = ((ord($string{($offset + 0)}) & 0x1F) << 6) &
  665. (ord($string{($offset + 1)}) & 0x3F);
  666. $offset += 2;
  667. } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
  668. // 0bbbbbbb
  669. $charval = ord($string{$offset});
  670. $offset += 1;
  671. } else {
  672. // error? throw some kind of warning here?
  673. $charval = false;
  674. $offset += 1;
  675. }
  676. if ($charval !== false) {
  677. $newcharstring .= (($charval < 256) ? chr($charval) : '?');
  678. }
  679. }
  680. return $newcharstring;
  681. }
  682. // UTF-8 => UTF-16BE
  683. public static function iconv_fallback_utf8_utf16be($string, $bom=false) {
  684. $newcharstring = '';
  685. if ($bom) {
  686. $newcharstring .= "\xFE\xFF";
  687. }
  688. $offset = 0;
  689. $stringlength = strlen($string);
  690. while ($offset < $stringlength) {
  691. if ((ord($string{$offset}) | 0x07) == 0xF7) {
  692. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  693. $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
  694. ((ord($string{($offset + 1)}) & 0x3F) << 12) &
  695. ((ord($string{($offset + 2)}) & 0x3F) << 6) &
  696. (ord($string{($offset + 3)}) & 0x3F);
  697. $offset += 4;
  698. } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
  699. // 1110bbbb 10bbbbbb 10bbbbbb
  700. $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
  701. ((ord($string{($offset + 1)}) & 0x3F) << 6) &
  702. (ord($string{($offset + 2)}) & 0x3F);
  703. $offset += 3;
  704. } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
  705. // 110bbbbb 10bbbbbb
  706. $charval = ((ord($string{($offset + 0)}) & 0x1F) << 6) &
  707. (ord($string{($offset + 1)}) & 0x3F);
  708. $offset += 2;
  709. } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
  710. // 0bbbbbbb
  711. $charval = ord($string{$offset});
  712. $offset += 1;
  713. } else {
  714. // error? throw some kind of warning here?
  715. $charval = false;
  716. $offset += 1;
  717. }
  718. if ($charval !== false) {
  719. $newcharstring .= (($charval < 65536) ? self::BigEndian2String($charval, 2) : "\x00".'?');
  720. }
  721. }
  722. return $newcharstring;
  723. }
  724. // UTF-8 => UTF-16LE
  725. public static function iconv_fallback_utf8_utf16le($string, $bom=false) {
  726. $newcharstring = '';
  727. if ($bom) {
  728. $newcharstring .= "\xFF\xFE";
  729. }
  730. $offset = 0;
  731. $stringlength = strlen($string);
  732. while ($offset < $stringlength) {
  733. if ((ord($string{$offset}) | 0x07) == 0xF7) {
  734. // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  735. $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
  736. ((ord($string{($offset + 1)}) & 0x3F) << 12) &
  737. ((ord($string{($offset + 2)}) & 0x3F) << 6) &
  738. (ord($string{($offset + 3)}) & 0x3F);
  739. $offset += 4;
  740. } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
  741. // 1110bbbb 10bbbbbb 10bbbbbb
  742. $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
  743. ((ord($string{($offset + 1)}) & 0x3F) << 6) &
  744. (ord($string{($offset + 2)}) & 0x3F);
  745. $offset += 3;
  746. } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
  747. // 110bbbbb 10bbbbbb
  748. $charval = ((ord($string{($offset + 0)}) & 0x1F) << 6) &
  749. (ord($string{($offset + 1)}) & 0x3F);
  750. $offset += 2;
  751. } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
  752. // 0bbbbbbb
  753. $charval = ord($string{$offset});
  754. $offset += 1;
  755. } else {
  756. // error? maybe throw some warning here?
  757. $charval = false;
  758. $offset += 1;
  759. }
  760. if ($charval !== false) {
  761. $newcharstring .= (($charval < 65536) ? self::LittleEndian2String($charval, 2) : '?'."\x00");
  762. }
  763. }
  764. return $newcharstring;
  765. }
  766. // UTF-8 => UTF-16LE (BOM)
  767. public static function iconv_fallback_utf8_utf16($string) {
  768. return self::iconv_fallback_utf8_utf16le($string, true);
  769. }
  770. // UTF-16BE => UTF-8
  771. public static function iconv_fallback_utf16be_utf8($string) {
  772. if (substr($string, 0, 2) == "\xFE\xFF") {
  773. // strip BOM
  774. $string = substr($string, 2);
  775. }
  776. $newcharstring = '';
  777. for ($i = 0; $i < strlen($string); $i += 2) {
  778. $charval = self::BigEndian2Int(substr($string, $i, 2));
  779. $newcharstring .= self::iconv_fallback_int_utf8($charval);
  780. }
  781. return $newcharstring;
  782. }
  783. // UTF-16LE => UTF-8
  784. public static function iconv_fallback_utf16le_utf8($string) {
  785. if (substr($string, 0, 2) == "\xFF\xFE") {
  786. // strip BOM
  787. $string = substr($string, 2);
  788. }
  789. $newcharstring = '';
  790. for ($i = 0; $i < strlen($string); $i += 2) {
  791. $charval = self::LittleEndian2Int(substr($string, $i, 2));
  792. $newcharstring .= self::iconv_fallback_int_utf8($charval);
  793. }
  794. return $newcharstring;
  795. }
  796. // UTF-16BE => ISO-8859-1
  797. public static function iconv_fallback_utf16be_iso88591($string) {
  798. if (substr($string, 0, 2) == "\xFE\xFF") {
  799. // strip BOM
  800. $string = substr($string, 2);
  801. }
  802. $newcharstring = '';
  803. for ($i = 0; $i < strlen($string); $i += 2) {
  804. $charval = self::BigEndian2Int(substr($string, $i, 2));
  805. $newcharstring .= (($charval < 256) ? chr($charval) : '?');
  806. }
  807. return $newcharstring;
  808. }
  809. // UTF-16LE => ISO-8859-1
  810. public static function iconv_fallback_utf16le_iso88591($string) {
  811. if (substr($string, 0, 2) == "\xFF\xFE") {
  812. // strip BOM
  813. $string = substr($string, 2);
  814. }
  815. $newcharstring = '';
  816. for ($i = 0; $i < strlen($string); $i += 2) {
  817. $charval = self::LittleEndian2Int(substr($string, $i, 2));
  818. $newcharstring .= (($charval < 256) ? chr($charval) : '?');
  819. }
  820. return $newcharstring;
  821. }
  822. // UTF-16 (BOM) => ISO-8859-1
  823. public static function iconv_fallback_utf16_iso88591($string) {
  824. $bom = substr($string, 0, 2);
  825. if ($bom == "\xFE\xFF") {
  826. return self::iconv_fallback_utf16be_iso88591(substr($string, 2));
  827. } elseif ($bom == "\xFF\xFE") {
  828. return self::iconv_fallback_utf16le_iso88591(substr($string, 2));
  829. }
  830. return $string;
  831. }
  832. // UTF-16 (BOM) => UTF-8
  833. public static function iconv_fallback_utf16_utf8($string) {
  834. $bom = substr($string, 0, 2);
  835. if ($bom == "\xFE\xFF") {
  836. return self::iconv_fallback_utf16be_utf8(substr($string, 2));
  837. } elseif ($bom == "\xFF\xFE") {
  838. return self::iconv_fallback_utf16le_utf8(substr($string, 2));
  839. }
  840. return $string;
  841. }
  842. public static function iconv_fallback($in_charset, $out_charset, $string) {
  843. if ($in_charset == $out_charset) {
  844. return $string;
  845. }
  846. // iconv() availble
  847. if (function_exists('iconv')) {
  848. if ($converted_string = @iconv($in_charset, $out_charset.'//TRANSLIT', $string)) {
  849. switch ($out_charset) {
  850. case 'ISO-8859-1':
  851. $converted_string = rtrim($converted_string, "\x00");
  852. break;
  853. }
  854. return $converted_string;
  855. }
  856. // iconv() may sometimes fail with "illegal character in input string" error message
  857. // and return an empty string, but returning the unconverted string is more useful
  858. return $string;
  859. }
  860. // iconv() not available
  861. static $ConversionFunctionList = array();
  862. if (empty($ConversionFunctionList)) {
  863. $ConversionFunctionList['ISO-8859-1']['UTF-8'] = 'iconv_fallback_iso88591_utf8';
  864. $ConversionFunctionList['ISO-8859-1']['UTF-16'] = 'iconv_fallback_iso88591_utf16';
  865. $ConversionFunctionList['ISO-8859-1']['UTF-16BE'] = 'iconv_fallback_iso88591_utf16be';
  866. $ConversionFunctionList['ISO-8859-1']['UTF-16LE'] = 'iconv_fallback_iso88591_utf16le';
  867. $ConversionFunctionList['UTF-8']['ISO-8859-1'] = 'iconv_fallback_utf8_iso88591';
  868. $ConversionFunctionList['UTF-8']['UTF-16'] = 'iconv_fallback_utf8_utf16';
  869. $ConversionFunctionList['UTF-8']['UTF-16BE'] = 'iconv_fallback_utf8_utf16be';
  870. $ConversionFunctionList['UTF-8']['UTF-16LE'] = 'iconv_fallback_utf8_utf16le';
  871. $ConversionFunctionList['UTF-16']['ISO-8859-1'] = 'iconv_fallback_utf16_iso88591';
  872. $ConversionFunctionList['UTF-16']['UTF-8'] = 'iconv_fallback_utf16_utf8';
  873. $ConversionFunctionList['UTF-16LE']['ISO-8859-1'] = 'iconv_fallback_utf16le_iso88591';
  874. $ConversionFunctionList['UTF-16LE']['UTF-8'] = 'iconv_fallback_utf16le_utf8';
  875. $ConversionFunctionList['UTF-16BE']['ISO-8859-1'] = 'iconv_fallback_utf16be_iso88591';
  876. $ConversionFunctionList['UTF-16BE']['UTF-8'] = 'iconv_fallback_utf16be_utf8';
  877. }
  878. if (isset($ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)])) {
  879. $ConversionFunction = $ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)];
  880. return self::$ConversionFunction($string);
  881. }
  882. throw new Exception('PHP does not have iconv() support - cannot convert from '.$in_charset.' to '.$out_charset);
  883. }
  884. public static function MultiByteCharString2HTML($string, $charset='ISO-8859-1') {
  885. $string = (string) $string; // in case trying to pass a numeric (float, int) string, would otherwise return an empty string
  886. $HTMLstring = '';
  887. switch ($charset) {
  888. case '1251':
  889. case '1252':
  890. case '866':
  891. case '932':
  892. case '936':
  893. case '950':
  894. case 'BIG5':
  895. case 'BIG5-HKSCS':
  896. case 'cp1251':
  897. case 'cp1252':
  898. case 'cp866':
  899. case 'EUC-JP':
  900. case 'EUCJP':
  901. case 'GB2312':
  902. case 'ibm866':
  903. case 'ISO-8859-1':
  904. case 'ISO-8859-15':
  905. case 'ISO8859-1':
  906. case 'ISO8859-15':
  907. case 'KOI8-R':
  908. case 'koi8-ru':
  909. case 'koi8r':
  910. case 'Shift_JIS':
  911. case 'SJIS':
  912. case 'win-1251':
  913. case 'Windows-1251':
  914. case 'Windows-1252':
  915. $HTMLstring = htmlentities($string, ENT_COMPAT, $charset);
  916. break;
  917. case 'UTF-8':
  918. $strlen = strlen($string);
  919. for ($i = 0; $i < $strlen; $i++) {
  920. $char_ord_val = ord($string{$i});
  921. $charval = 0;
  922. if ($char_ord_val < 0x80) {
  923. $charval = $char_ord_val;
  924. } elseif ((($char_ord_val & 0xF0) >> 4) == 0x0F && $i+3 < $strlen) {
  925. $charval = (($char_ord_val & 0x07) << 18);
  926. $charval += ((ord($string{++$i}) & 0x3F) << 12);
  927. $charval += ((ord($string{++$i}) & 0x3F) << 6);
  928. $charval += (ord($string{++$i}) & 0x3F);
  929. } elseif ((($char_ord_val & 0xE0) >> 5) == 0x07 && $i+2 < $strlen) {
  930. $charval = (($char_ord_val & 0x0F) << 12);
  931. $charval += ((ord($string{++$i}) & 0x3F) << 6);
  932. $charval += (ord($string{++$i}) & 0x3F);
  933. } elseif ((($char_ord_val & 0xC0) >> 6) == 0x03 && $i+1 < $strlen) {
  934. $charval = (($char_ord_val & 0x1F) << 6);
  935. $charval += (ord($string{++$i}) & 0x3F);
  936. }
  937. if (($charval >= 32) && ($charval <= 127)) {
  938. $HTMLstring .= htmlentities(chr($charval));
  939. } else {
  940. $HTMLstring .= '&#'.$charval.';';
  941. }
  942. }
  943. break;
  944. case 'UTF-16LE':
  945. for ($i = 0; $i < strlen($string); $i += 2) {
  946. $charval = self::LittleEndian2Int(substr($string, $i, 2));
  947. if (($charval >= 32) && ($charval <= 127)) {
  948. $HTMLstring .= chr($charval);
  949. } else {
  950. $HTMLstring .= '&#'.$charval.';';
  951. }
  952. }
  953. break;
  954. case 'UTF-16BE':
  955. for ($i = 0; $i < strlen($string); $i += 2) {
  956. $charval = self::BigEndian2Int(substr($string, $i, 2));
  957. if (($charval >= 32) && ($charval <= 127)) {
  958. $HTMLstring .= chr($charval);
  959. } else {
  960. $HTMLstring .= '&#'.$charval.';';
  961. }
  962. }
  963. break;
  964. default:
  965. $HTMLstring = 'ERROR: Character set "'.$charset.'" not supported in MultiByteCharString2HTML()';
  966. break;
  967. }
  968. return $HTMLstring;
  969. }
  970. public static function RGADnameLookup($namecode) {
  971. static $RGADname = array();
  972. if (empty($RGADname)) {
  973. $RGADname[0] = 'not set';
  974. $RGADname[1] = 'Track Gain Adjustment';
  975. $RGADname[2] = 'Album Gain Adjustment';
  976. }
  977. return (isset($RGADname[$namecode]) ? $RGADname[$namecode] : '');
  978. }
  979. public static function RGADoriginatorLookup($originatorcode) {
  980. static $RGADoriginator = array();
  981. if (empty($RGADoriginator)) {
  982. $RGADoriginator[0] = 'unspecified';
  983. $RGADoriginator[1] = 'pre-set by artist/producer/mastering engineer';
  984. $RGADoriginator[2] = 'set by user';
  985. $RGADoriginator[3] = 'determined automatically';
  986. }
  987. return (isset($RGADoriginator[$originatorcode]) ? $RGADoriginator[$originatorcode] : '');
  988. }
  989. public static function RGADadjustmentLookup($rawadjustment, $signbit) {
  990. $adjustment = $rawadjustment / 10;
  991. if ($signbit == 1) {
  992. $adjustment *= -1;
  993. }
  994. return (float) $adjustment;
  995. }
  996. public static function RGADgainString($namecode, $originatorcode, $replaygain) {
  997. if ($replaygain < 0) {
  998. $signbit = '1';
  999. } else {
  1000. $signbit = '0';
  1001. }
  1002. $storedreplaygain = intval(round($replaygain * 10));
  1003. $gainstring = str_pad(decbin($namecode), 3, '0', STR_PAD_LEFT);
  1004. $gainstring .= str_pad(decbin($originatorcode), 3, '0', STR_PAD_LEFT);
  1005. $gainstring .= $signbit;
  1006. $gainstring .= str_pad(decbin($storedreplaygain), 9, '0', STR_PAD_LEFT);
  1007. return $gainstring;
  1008. }
  1009. public static function RGADamplitude2dB($amplitude) {
  1010. return 20 * log10($amplitude);
  1011. }
  1012. public static function GetDataImageSize($imgData, &$imageinfo=array()) {
  1013. static $tempdir = '';
  1014. if (empty($tempdir)) {
  1015. // yes this is ugly, feel free to suggest a better way
  1016. require_once(dirname(__FILE__).'/getid3.php');
  1017. $getid3_temp = new getID3();
  1018. $tempdir = $getid3_temp->tempdir;
  1019. unset($getid3_temp);
  1020. }
  1021. $GetDataImageSize = false;
  1022. if ($tempfilename = tempnam($tempdir, 'gI3')) {
  1023. if (is_writable($tempfilename) && is_file($tempfilename) && ($tmp = fopen($tempfilename, 'wb'))) {
  1024. fwrite($tmp, $imgData);
  1025. fclose($tmp);
  1026. $GetDataImageSize = @getimagesize($tempfilename, $imageinfo);
  1027. }
  1028. unlink($tempfilename);
  1029. }
  1030. return $GetDataImageSize;
  1031. }
  1032. public static function ImageExtFromMime($mime_type) {
  1033. // temporary way, works OK for now, but should be reworked in the future
  1034. return str_replace(array('image/', 'x-', 'jpeg'), array('', '', 'jpg'), $mime_type);
  1035. }
  1036. public static function ImageTypesLookup($imagetypeid) {
  1037. static $ImageTypesLookup = array();
  1038. if (empty($ImageTypesLookup)) {
  1039. $ImageTypesLookup[1] = 'gif';
  1040. $ImageTypesLookup[2] = 'jpeg';
  1041. $ImageTypesLookup[3] = 'png';
  1042. $ImageTypesLookup[4] = 'swf';
  1043. $ImageTypesLookup[5] = 'psd';
  1044. $ImageTypesLookup[6] = 'bmp';
  1045. $ImageTypesLookup[7] = 'tiff (little-endian)';
  1046. $ImageTypesLookup[8] = 'tiff (big-endian)';
  1047. $ImageTypesLookup[9] = 'jpc';
  1048. $ImageTypesLookup[10] = 'jp2';
  1049. $ImageTypesLookup[11] = 'jpx';
  1050. $ImageTypesLookup[12] = 'jb2';
  1051. $ImageTypesLookup[13] = 'swc';
  1052. $ImageTypesLookup[14] = 'iff';
  1053. }
  1054. return (isset($ImageTypesLookup[$imagetypeid]) ? $ImageTypesLookup[$imagetypeid] : '');
  1055. }
  1056. public static function CopyTagsToComments(&$ThisFileInfo) {
  1057. // Copy all entries from ['tags'] into common ['comments']
  1058. if (!empty($ThisFileInfo['tags'])) {
  1059. foreach ($ThisFileInfo['tags'] as $tagtype => $tagarray) {
  1060. foreach ($tagarray as $tagname => $tagdata) {
  1061. foreach ($tagdata as $key => $value) {
  1062. if (!empty($value)) {
  1063. if (empty($ThisFileInfo['comments'][$tagname])) {
  1064. // fall through and append value
  1065. } elseif ($tagtype == 'id3v1') {
  1066. $newvaluelength = strlen(trim($value));
  1067. foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
  1068. $oldvaluelength = strlen(trim($existingvalue));
  1069. if (($newvaluelength <= $oldvaluelength) && (substr($existingvalue, 0, $newvaluelength) == trim($value))) {
  1070. // new value is identical but shorter-than (or equal-length to) one already in comments - skip
  1071. break 2;
  1072. }
  1073. }
  1074. } elseif (!is_array($value)) {
  1075. $newvaluelength = strlen(trim($value));
  1076. foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
  1077. $oldvaluelength = strlen(trim($existingvalue));
  1078. if (($newvaluelength > $oldvaluelength) && (substr(trim($value), 0, strlen($existingvalue)) == $existingvalue)) {
  1079. $ThisFileInfo['comments'][$tagname][$existingkey] = trim($value);
  1080. break 2;
  1081. }
  1082. }
  1083. }
  1084. if (is_array($value) || empty($ThisFileInfo['comments'][$tagname]) || !in_array(trim($value), $ThisFileInfo['comments'][$tagname])) {
  1085. $value = (is_string($value) ? trim($value) : $value);
  1086. $ThisFileInfo['comments'][$tagname][] = $value;
  1087. }
  1088. }
  1089. }
  1090. }
  1091. }
  1092. // Copy to ['comments_html']
  1093. foreach ($ThisFileInfo['comments'] as $field => $values) {
  1094. if ($field == 'picture') {
  1095. // pictures can take up a lot of space, and we don't need multiple copies of them
  1096. // let there be a single copy in [comments][picture], and not elsewhere
  1097. continue;
  1098. }
  1099. foreach ($values as $index => $value) {
  1100. if (is_array($value)) {
  1101. $ThisFileInfo['comments_html'][$field][$index] = $value;
  1102. } else {
  1103. $ThisFileInfo['comments_html'][$field][$index] = str_replace('&#0;', '', self::MultiByteCharString2HTML($value, $ThisFileInfo['encoding']));
  1104. }
  1105. }
  1106. }
  1107. }
  1108. return true;
  1109. }
  1110. public static function EmbeddedLookup($key, $begin, $end, $file, $name) {
  1111. // Cached
  1112. static $cache;
  1113. if (isset($cache[$file][$name])) {
  1114. return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : '');
  1115. }
  1116. // Init
  1117. $keylength = strlen($key);
  1118. $line_count = $end - $begin - 7;
  1119. // Open php file
  1120. $fp = fopen($file, 'r');
  1121. // Discard $begin lines
  1122. for ($i = 0; $i < ($begin + 3); $i++) {
  1123. fgets($fp, 1024);
  1124. }
  1125. // Loop thru line
  1126. while (0 < $line_count--) {
  1127. // Read line
  1128. $line = ltrim(fgets($fp, 1024), "\t ");
  1129. // METHOD A: only cache the matching key - less memory but slower on next lookup of not-previously-looked-up key
  1130. //$keycheck = substr($line, 0, $keylength);
  1131. //if ($key == $keycheck) {
  1132. // $cache[$file][$name][$keycheck] = substr($line, $keylength + 1);
  1133. // break;
  1134. //}
  1135. // METHOD B: cache all keys in this lookup - more memory but faster on next lookup of not-previously-looked-up key
  1136. //$cache[$file][$name][substr($line, 0, $keylength)] = trim(substr($line, $keylength + 1));
  1137. $explodedLine = explode("\t", $line, 2);
  1138. $ThisKey = (isset($explodedLine[0]) ? $explodedLine[0] : '');
  1139. $ThisValue = (isset($explodedLine[1]) ? $explodedLine[1] : '');
  1140. $cache[$file][$name][$ThisKey] = trim($ThisValue);
  1141. }
  1142. // Close and return
  1143. fclose($fp);
  1144. return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : '');
  1145. }
  1146. public static function IncludeDependency($filename, $sourcefile, $DieOnFailure=false) {
  1147. global $GETID3_ERRORARRAY;
  1148. if (file_exists($filename)) {
  1149. if (include_once($filename)) {
  1150. return true;
  1151. } else {
  1152. $diemessage = basename($sourcefile).' depends on '.$filename.', which has errors';
  1153. }
  1154. } else {
  1155. $diemessage = basename($sourcefile).' depends on '.$filename.', which is missing';
  1156. }
  1157. if ($DieOnFailure) {
  1158. throw new Exception($diemessage);
  1159. } else {
  1160. $GETID3_ERRORARRAY[] = $diemessage;
  1161. }
  1162. return false;
  1163. }
  1164. public static function trimNullByte($string) {
  1165. return trim($string, "\x00");
  1166. }
  1167. public static function getFileSizeSyscall($path) {
  1168. $filesize = false;
  1169. if (GETID3_OS_ISWINDOWS) {
  1170. 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:
  1171. $filesystem = new COM('Scripting.FileSystemObject');
  1172. $file = $filesystem->GetFile($path);
  1173. $filesize = $file->Size();
  1174. unset($filesystem, $file);
  1175. } else {
  1176. $commandline = 'for %I in ('.escapeshellarg($path).') do @echo %~zI';
  1177. }
  1178. } else {
  1179. $commandline = 'ls -l '.escapeshellarg($path).' | awk \'{print $5}\'';
  1180. }
  1181. if (isset($commandline)) {
  1182. $output = trim(`$commandline`);
  1183. if (ctype_digit($output)) {
  1184. $filesize = (float) $output;
  1185. }
  1186. }
  1187. return $filesize;
  1188. }
  1189. }