PageRenderTime 65ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

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

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