PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/getid3/getid3.lib.php

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