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

/blog/wp-content/plugins/podpress/getid3/getid3.lib.php

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