PageRenderTime 65ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/sys/plugins/id3/getid3/getid3.lib.php

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