getid3_lib

The WordPress Core getid3 lib class.

Defined (1)

The class is defined in the following location(s).

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