PageRenderTime 70ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/common/libraries/plugin/getid3/demos/demo.mp3header.php

https://bitbucket.org/renaatdemuynck/chamilo
PHP | 3672 lines | 2833 code | 481 blank | 358 comment | 583 complexity | 53daf93fbe080b7c0222b46fdd8b90ac MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. if (! function_exists('PrintHexBytes'))
  3. {
  4. function PrintHexBytes($string)
  5. {
  6. $returnstring = '';
  7. for($i = 0; $i < strlen($string); $i ++)
  8. {
  9. $returnstring .= str_pad(dechex(ord(substr($string, $i, 1))), 2, '0', STR_PAD_LEFT) . ' ';
  10. }
  11. return $returnstring;
  12. }
  13. }
  14. if (! function_exists('PrintTextBytes'))
  15. {
  16. function PrintTextBytes($string)
  17. {
  18. $returnstring = '';
  19. for($i = 0; $i < strlen($string); $i ++)
  20. {
  21. if (ord(substr($string, $i, 1)) <= 31)
  22. {
  23. $returnstring .= ' ';
  24. }
  25. else
  26. {
  27. $returnstring .= ' ' . substr($string, $i, 1) . ' ';
  28. }
  29. }
  30. return $returnstring;
  31. }
  32. }
  33. if (! function_exists('FixDBFields'))
  34. {
  35. function FixDBFields($text)
  36. {
  37. return mysql_escape_string($text);
  38. }
  39. }
  40. if (! function_exists('FixTextFields'))
  41. {
  42. function FixTextFields($text)
  43. {
  44. $text = SafeStripSlashes($text);
  45. $text = htmlentities($text, ENT_QUOTES);
  46. return $text;
  47. }
  48. }
  49. if (! function_exists('SafeStripSlashes'))
  50. {
  51. function SafeStripSlashes($text)
  52. {
  53. if (get_magic_quotes_gpc())
  54. {
  55. return stripslashes($text);
  56. }
  57. return $text;
  58. }
  59. }
  60. if (! function_exists('table_var_dump'))
  61. {
  62. function table_var_dump($variable)
  63. {
  64. $returnstring = '';
  65. switch (gettype($variable))
  66. {
  67. case 'array' :
  68. $returnstring .= '<TABLE BORDER="1" CELLSPACING="0" CELLPADDING="2">';
  69. foreach ($variable as $key => $value)
  70. {
  71. $returnstring .= '<TR><TD VALIGN="TOP"><B>' . str_replace(chr(0), ' ', $key) . '</B></TD>';
  72. $returnstring .= '<TD VALIGN="TOP">' . gettype($value);
  73. if (is_array($value))
  74. {
  75. $returnstring .= '&nbsp;(' . count($value) . ')';
  76. }
  77. elseif (is_string($value))
  78. {
  79. $returnstring .= '&nbsp;(' . strlen($value) . ')';
  80. }
  81. if (($key == 'data') && isset($variable['image_mime']) && isset($variable['dataoffset']))
  82. {
  83. require_once (GETID3_INCLUDEPATH . 'getid3.getimagesize.php');
  84. $imageinfo = array();
  85. $imagechunkcheck = GetDataImageSize($value, $imageinfo);
  86. $DumpedImageSRC = (! empty($_REQUEST['filename']) ? $_REQUEST['filename'] : '.getid3') . '.' . $variable['dataoffset'] . '.' . ImageTypesLookup($imagechunkcheck[2]);
  87. if ($tempimagefile = fopen($DumpedImageSRC, 'wb'))
  88. {
  89. fwrite($tempimagefile, $value);
  90. fclose($tempimagefile);
  91. }
  92. $returnstring .= '</TD><TD><IMG SRC="' . $DumpedImageSRC . '" WIDTH="' . $imagechunkcheck[0] . '" HEIGHT="' . $imagechunkcheck[1] . '"></TD></TR>';
  93. }
  94. else
  95. {
  96. $returnstring .= '</TD><TD>' . table_var_dump($value) . '</TD></TR>';
  97. }
  98. }
  99. $returnstring .= '</TABLE>';
  100. break;
  101. case 'boolean' :
  102. $returnstring .= ($variable ? 'TRUE' : 'FALSE');
  103. break;
  104. case 'integer' :
  105. case 'double' :
  106. case 'float' :
  107. $returnstring .= $variable;
  108. break;
  109. case 'object' :
  110. case 'null' :
  111. $returnstring .= string_var_dump($variable);
  112. break;
  113. case 'string' :
  114. $variable = str_replace(chr(0), ' ', $variable);
  115. $varlen = strlen($variable);
  116. for($i = 0; $i < $varlen; $i ++)
  117. {
  118. if (ereg('[' . chr(0x0A) . chr(0x0D) . ' -;0-9A-Za-z]', $variable{$i}))
  119. {
  120. $returnstring .= $variable{$i};
  121. }
  122. else
  123. {
  124. $returnstring .= '&#' . str_pad(ord($variable{$i}), 3, '0', STR_PAD_LEFT) . ';';
  125. }
  126. }
  127. $returnstring = nl2br($returnstring);
  128. break;
  129. default :
  130. require_once (GETID3_INCLUDEPATH . 'getid3.getimagesize.php');
  131. $imageinfo = array();
  132. $imagechunkcheck = GetDataImageSize(substr($variable, 0, FREAD_BUFFER_SIZE), $imageinfo);
  133. if (($imagechunkcheck[2] >= 1) && ($imagechunkcheck[2] <= 3))
  134. {
  135. $returnstring .= '<TABLE BORDER="1" CELLSPACING="0" CELLPADDING="2">';
  136. $returnstring .= '<TR><TD><B>type</B></TD><TD>' . ImageTypesLookup($imagechunkcheck[2]) . '</TD></TR>';
  137. $returnstring .= '<TR><TD><B>width</B></TD><TD>' . number_format($imagechunkcheck[0]) . ' px</TD></TR>';
  138. $returnstring .= '<TR><TD><B>height</B></TD><TD>' . number_format($imagechunkcheck[1]) . ' px</TD></TR>';
  139. $returnstring .= '<TR><TD><B>size</B></TD><TD>' . number_format(strlen($variable)) . ' bytes</TD></TR></TABLE>';
  140. }
  141. else
  142. {
  143. $returnstring .= nl2br(htmlspecialchars(str_replace(chr(0), ' ', $variable)));
  144. }
  145. break;
  146. }
  147. return $returnstring;
  148. }
  149. }
  150. if (! function_exists('string_var_dump'))
  151. {
  152. function string_var_dump($variable)
  153. {
  154. ob_start();
  155. var_dump($variable);
  156. $dumpedvariable = ob_get_contents();
  157. ob_end_clean();
  158. return $dumpedvariable;
  159. }
  160. }
  161. if (! function_exists('fileextension'))
  162. {
  163. function fileextension($filename, $numextensions = 1)
  164. {
  165. if (strstr($filename, '.'))
  166. {
  167. $reversedfilename = strrev($filename);
  168. $offset = 0;
  169. for($i = 0; $i < $numextensions; $i ++)
  170. {
  171. $offset = strpos($reversedfilename, '.', $offset + 1);
  172. if ($offset === false)
  173. {
  174. return '';
  175. }
  176. }
  177. return strrev(substr($reversedfilename, 0, $offset));
  178. }
  179. return '';
  180. }
  181. }
  182. if (! function_exists('RemoveAccents'))
  183. {
  184. function RemoveAccents($string)
  185. {
  186. // return strtr($string, '���������������������������������������������������������������������', 'SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyy');
  187. // Revised version by marksteward@hotmail.com
  188. return strtr(strtr($string, '������������������������������������������������������������', 'SZszYAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy'), array(
  189. '�' => 'TH', '�' => 'th', '�' => 'DH', '�' => 'dh', '�' => 'ss', '�' => 'OE', '�' => 'oe', '�' => 'AE',
  190. '�' => 'ae', '�' => 'u'));
  191. }
  192. }
  193. if (! function_exists('MoreNaturalSort'))
  194. {
  195. function MoreNaturalSort($ar1, $ar2)
  196. {
  197. if ($ar1 === $ar2)
  198. {
  199. return 0;
  200. }
  201. $len1 = strlen($ar1);
  202. $len2 = strlen($ar2);
  203. $shortest = min($len1, $len2);
  204. if (substr($ar1, 0, $shortest) === substr($ar2, 0, $shortest))
  205. {
  206. // the shorter argument is the beginning of the longer one, like "str" and "string"
  207. if ($len1 < $len2)
  208. {
  209. return - 1;
  210. }
  211. elseif ($len1 > $len2)
  212. {
  213. return 1;
  214. }
  215. return 0;
  216. }
  217. $ar1 = RemoveAccents(strtolower(trim($ar1)));
  218. $ar2 = RemoveAccents(strtolower(trim($ar2)));
  219. $translatearray = array('\'' => '', '"' => '', '_' => ' ', '(' => '', ')' => '', '-' => ' ', ' ' => ' ',
  220. '.' => '', ',' => '');
  221. foreach ($translatearray as $key => $val)
  222. {
  223. $ar1 = str_replace($key, $val, $ar1);
  224. $ar2 = str_replace($key, $val, $ar2);
  225. }
  226. if ($ar1 < $ar2)
  227. {
  228. return - 1;
  229. }
  230. elseif ($ar1 > $ar2)
  231. {
  232. return 1;
  233. }
  234. return 0;
  235. }
  236. }
  237. if (! function_exists('trunc'))
  238. {
  239. function trunc($floatnumber)
  240. {
  241. // truncates a floating-point number at the decimal point
  242. // returns int (if possible, otherwise float)
  243. if ($floatnumber >= 1)
  244. {
  245. $truncatednumber = floor($floatnumber);
  246. }
  247. elseif ($floatnumber <= - 1)
  248. {
  249. $truncatednumber = ceil($floatnumber);
  250. }
  251. else
  252. {
  253. $truncatednumber = 0;
  254. }
  255. if ($truncatednumber <= pow(2, 30))
  256. {
  257. $truncatednumber = (int) $truncatednumber;
  258. }
  259. return $truncatednumber;
  260. }
  261. }
  262. if (! function_exists('CastAsInt'))
  263. {
  264. function CastAsInt($floatnum)
  265. {
  266. // convert to float if not already
  267. $floatnum = (float) $floatnum;
  268. // convert a float to type int, only if possible
  269. if (trunc($floatnum) == $floatnum)
  270. {
  271. // it's not floating point
  272. if ($floatnum <= pow(2, 30))
  273. {
  274. // it's within int range
  275. $floatnum = (int) $floatnum;
  276. }
  277. }
  278. return $floatnum;
  279. }
  280. }
  281. if (! function_exists('getmicrotime'))
  282. {
  283. function getmicrotime()
  284. {
  285. list($usec, $sec) = explode(' ', microtime());
  286. return ((float) $usec + (float) $sec);
  287. }
  288. }
  289. if (! function_exists('DecimalBinary2Float'))
  290. {
  291. function DecimalBinary2Float($binarynumerator)
  292. {
  293. $numerator = Bin2Dec($binarynumerator);
  294. $denominator = Bin2Dec(str_repeat('1', strlen($binarynumerator)));
  295. return ($numerator / $denominator);
  296. }
  297. }
  298. if (! function_exists('NormalizeBinaryPoint'))
  299. {
  300. function NormalizeBinaryPoint($binarypointnumber, $maxbits = 52)
  301. {
  302. // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
  303. if (strpos($binarypointnumber, '.') === false)
  304. {
  305. $binarypointnumber = '0.' . $binarypointnumber;
  306. }
  307. elseif ($binarypointnumber{0} == '.')
  308. {
  309. $binarypointnumber = '0' . $binarypointnumber;
  310. }
  311. $exponent = 0;
  312. while (($binarypointnumber{0} != '1') || (substr($binarypointnumber, 1, 1) != '.'))
  313. {
  314. if (substr($binarypointnumber, 1, 1) == '.')
  315. {
  316. $exponent --;
  317. $binarypointnumber = substr($binarypointnumber, 2, 1) . '.' . substr($binarypointnumber, 3);
  318. }
  319. else
  320. {
  321. $pointpos = strpos($binarypointnumber, '.');
  322. $exponent += ($pointpos - 1);
  323. $binarypointnumber = str_replace('.', '', $binarypointnumber);
  324. $binarypointnumber = $binarypointnumber{0} . '.' . substr($binarypointnumber, 1);
  325. }
  326. }
  327. $binarypointnumber = str_pad(substr($binarypointnumber, 0, $maxbits + 2), $maxbits + 2, '0', STR_PAD_RIGHT);
  328. return array('normalized' => $binarypointnumber, 'exponent' => (int) $exponent);
  329. }
  330. }
  331. if (! function_exists('Float2BinaryDecimal'))
  332. {
  333. function Float2BinaryDecimal($floatvalue)
  334. {
  335. // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
  336. $maxbits = 128; // to how many bits of precision should the calculations be taken?
  337. $intpart = trunc($floatvalue);
  338. $floatpart = abs($floatvalue - $intpart);
  339. $pointbitstring = '';
  340. while (($floatpart != 0) && (strlen($pointbitstring) < $maxbits))
  341. {
  342. $floatpart *= 2;
  343. $pointbitstring .= (string) trunc($floatpart);
  344. $floatpart -= trunc($floatpart);
  345. }
  346. $binarypointnumber = decbin($intpart) . '.' . $pointbitstring;
  347. return $binarypointnumber;
  348. }
  349. }
  350. if (! function_exists('Float2String'))
  351. {
  352. function Float2String($floatvalue, $bits)
  353. {
  354. // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee-expl.html
  355. switch ($bits)
  356. {
  357. case 32 :
  358. $exponentbits = 8;
  359. $fractionbits = 23;
  360. break;
  361. case 64 :
  362. $exponentbits = 11;
  363. $fractionbits = 52;
  364. break;
  365. default :
  366. return false;
  367. break;
  368. }
  369. if ($floatvalue >= 0)
  370. {
  371. $signbit = '0';
  372. }
  373. else
  374. {
  375. $signbit = '1';
  376. }
  377. $normalizedbinary = NormalizeBinaryPoint(Float2BinaryDecimal($floatvalue), $fractionbits);
  378. $biasedexponent = pow(2, $exponentbits - 1) - 1 + $normalizedbinary['exponent']; // (127 or 1023) +/- exponent
  379. $exponentbitstring = str_pad(decbin($biasedexponent), $exponentbits, '0', STR_PAD_LEFT);
  380. $fractionbitstring = str_pad(substr($normalizedbinary['normalized'], 2), $fractionbits, '0', STR_PAD_RIGHT);
  381. return BigEndian2String(Bin2Dec($signbit . $exponentbitstring . $fractionbitstring), $bits % 8, false);
  382. }
  383. }
  384. if (! function_exists('LittleEndian2Float'))
  385. {
  386. function LittleEndian2Float($byteword)
  387. {
  388. return BigEndian2Float(strrev($byteword));
  389. }
  390. }
  391. if (! function_exists('BigEndian2Float'))
  392. {
  393. function BigEndian2Float($byteword)
  394. {
  395. // ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic
  396. // http://www.psc.edu/general/software/packages/ieee/ieee.html
  397. // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html
  398. $bitword = BigEndian2Bin($byteword);
  399. $signbit = $bitword{0};
  400. switch (strlen($byteword) * 8)
  401. {
  402. case 32 :
  403. $exponentbits = 8;
  404. $fractionbits = 23;
  405. break;
  406. case 64 :
  407. $exponentbits = 11;
  408. $fractionbits = 52;
  409. break;
  410. case 80 :
  411. $exponentbits = 16;
  412. $fractionbits = 64;
  413. break;
  414. default :
  415. return false;
  416. break;
  417. }
  418. $exponentstring = substr($bitword, 1, $exponentbits - 1);
  419. $fractionstring = substr($bitword, $exponentbits, $fractionbits);
  420. $exponent = Bin2Dec($exponentstring);
  421. $fraction = Bin2Dec($fractionstring);
  422. if (($exponentbits == 16) && ($fractionbits == 64))
  423. {
  424. // 80-bit
  425. // As used in Apple AIFF for sample_rate
  426. // A bit of a hack, but it works ;)
  427. return pow(2, ($exponent - 16382)) * DecimalBinary2Float($fractionstring);
  428. }
  429. if (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction != 0))
  430. {
  431. // Not a Number
  432. $floatvalue = false;
  433. }
  434. elseif (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction == 0))
  435. {
  436. if ($signbit == '1')
  437. {
  438. $floatvalue = '-infinity';
  439. }
  440. else
  441. {
  442. $floatvalue = '+infinity';
  443. }
  444. }
  445. elseif (($exponent == 0) && ($fraction == 0))
  446. {
  447. if ($signbit == '1')
  448. {
  449. $floatvalue = - 0;
  450. }
  451. else
  452. {
  453. $floatvalue = 0;
  454. }
  455. $floatvalue = ($signbit ? 0 : - 0);
  456. }
  457. elseif (($exponent == 0) && ($fraction != 0))
  458. {
  459. // These are 'unnormalized' values
  460. $floatvalue = pow(2, (- 1 * (pow(2, $exponentbits - 1) - 2))) * DecimalBinary2Float($fractionstring);
  461. if ($signbit == '1')
  462. {
  463. $floatvalue *= - 1;
  464. }
  465. }
  466. elseif ($exponent != 0)
  467. {
  468. $floatvalue = pow(2, ($exponent - (pow(2, $exponentbits - 1) - 1))) * (1 + DecimalBinary2Float($fractionstring));
  469. if ($signbit == '1')
  470. {
  471. $floatvalue *= - 1;
  472. }
  473. }
  474. return (float) $floatvalue;
  475. }
  476. }
  477. if (! function_exists('BigEndian2Int'))
  478. {
  479. function BigEndian2Int($byteword, $synchsafe = false, $signed = false)
  480. {
  481. $intvalue = 0;
  482. $bytewordlen = strlen($byteword);
  483. for($i = 0; $i < $bytewordlen; $i ++)
  484. {
  485. if ($synchsafe)
  486. { // disregard MSB, effectively 7-bit bytes
  487. $intvalue = $intvalue | (ord($byteword{$i}) & 0x7F) << (($bytewordlen - 1 - $i) * 7);
  488. }
  489. else
  490. {
  491. $intvalue += ord($byteword{$i}) * pow(256, ($bytewordlen - 1 - $i));
  492. }
  493. }
  494. if ($signed && ! $synchsafe)
  495. {
  496. // synchsafe ints are not allowed to be signed
  497. switch ($bytewordlen)
  498. {
  499. case 1 :
  500. case 2 :
  501. case 3 :
  502. case 4 :
  503. $signmaskbit = 0x80 << (8 * ($bytewordlen - 1));
  504. if ($intvalue & $signmaskbit)
  505. {
  506. $intvalue = 0 - ($intvalue & ($signmaskbit - 1));
  507. }
  508. break;
  509. default :
  510. die('ERROR: Cannot have signed integers larger than 32-bits in BigEndian2Int()');
  511. break;
  512. }
  513. }
  514. return CastAsInt($intvalue);
  515. }
  516. }
  517. if (! function_exists('LittleEndian2Int'))
  518. {
  519. function LittleEndian2Int($byteword, $signed = false)
  520. {
  521. return BigEndian2Int(strrev($byteword), false, $signed);
  522. }
  523. }
  524. if (! function_exists('BigEndian2Bin'))
  525. {
  526. function BigEndian2Bin($byteword)
  527. {
  528. $binvalue = '';
  529. $bytewordlen = strlen($byteword);
  530. for($i = 0; $i < $bytewordlen; $i ++)
  531. {
  532. $binvalue .= str_pad(decbin(ord($byteword{$i})), 8, '0', STR_PAD_LEFT);
  533. }
  534. return $binvalue;
  535. }
  536. }
  537. if (! function_exists('BigEndian2String'))
  538. {
  539. function BigEndian2String($number, $minbytes = 1, $synchsafe = false, $signed = false)
  540. {
  541. if ($number < 0)
  542. {
  543. return false;
  544. }
  545. $maskbyte = (($synchsafe || $signed) ? 0x7F : 0xFF);
  546. $intstring = '';
  547. if ($signed)
  548. {
  549. if ($minbytes > 4)
  550. {
  551. die('ERROR: Cannot have signed integers larger than 32-bits in BigEndian2String()');
  552. }
  553. $number = $number & (0x80 << (8 * ($minbytes - 1)));
  554. }
  555. while ($number != 0)
  556. {
  557. $quotient = ($number / ($maskbyte + 1));
  558. $intstring = chr(ceil(($quotient - floor($quotient)) * $maskbyte)) . $intstring;
  559. $number = floor($quotient);
  560. }
  561. return str_pad($intstring, $minbytes, chr(0), STR_PAD_LEFT);
  562. }
  563. }
  564. if (! function_exists('Dec2Bin'))
  565. {
  566. function Dec2Bin($number)
  567. {
  568. while ($number >= 256)
  569. {
  570. $bytes[] = (($number / 256) - (floor($number / 256))) * 256;
  571. $number = floor($number / 256);
  572. }
  573. $bytes[] = $number;
  574. $binstring = '';
  575. for($i = 0; $i < count($bytes); $i ++)
  576. {
  577. $binstring = (($i == count($bytes) - 1) ? decbin($bytes[$i]) : str_pad(decbin($bytes[$i]), 8, '0', STR_PAD_LEFT)) . $binstring;
  578. }
  579. return $binstring;
  580. }
  581. }
  582. if (! function_exists('Bin2Dec'))
  583. {
  584. function Bin2Dec($binstring)
  585. {
  586. $decvalue = 0;
  587. for($i = 0; $i < strlen($binstring); $i ++)
  588. {
  589. $decvalue += ((int) substr($binstring, strlen($binstring) - $i - 1, 1)) * pow(2, $i);
  590. }
  591. return CastAsInt($decvalue);
  592. }
  593. }
  594. if (! function_exists('Bin2String'))
  595. {
  596. function Bin2String($binstring)
  597. {
  598. // return 'hi' for input of '0110100001101001'
  599. $string = '';
  600. $binstringreversed = strrev($binstring);
  601. for($i = 0; $i < strlen($binstringreversed); $i += 8)
  602. {
  603. $string = chr(Bin2Dec(strrev(substr($binstringreversed, $i, 8)))) . $string;
  604. }
  605. return $string;
  606. }
  607. }
  608. if (! function_exists('LittleEndian2String'))
  609. {
  610. function LittleEndian2String($number, $minbytes = 1, $synchsafe = false)
  611. {
  612. $intstring = '';
  613. while ($number > 0)
  614. {
  615. if ($synchsafe)
  616. {
  617. $intstring = $intstring . chr($number & 127);
  618. $number >>= 7;
  619. }
  620. else
  621. {
  622. $intstring = $intstring . chr($number & 255);
  623. $number >>= 8;
  624. }
  625. }
  626. return str_pad($intstring, $minbytes, chr(0), STR_PAD_RIGHT);
  627. }
  628. }
  629. if (! function_exists('Bool2IntString'))
  630. {
  631. function Bool2IntString($intvalue)
  632. {
  633. return ($intvalue ? '1' : '0');
  634. }
  635. }
  636. if (! function_exists('IntString2Bool'))
  637. {
  638. function IntString2Bool($char)
  639. {
  640. if ($char == '1')
  641. {
  642. return true;
  643. }
  644. elseif ($char == '0')
  645. {
  646. return false;
  647. }
  648. return null;
  649. }
  650. }
  651. if (! function_exists('InverseBoolean'))
  652. {
  653. function InverseBoolean($value)
  654. {
  655. return ($value ? false : true);
  656. }
  657. }
  658. if (! function_exists('DeUnSynchronise'))
  659. {
  660. function DeUnSynchronise($data)
  661. {
  662. return str_replace(chr(0xFF) . chr(0x00), chr(0xFF), $data);
  663. }
  664. }
  665. if (! function_exists('Unsynchronise'))
  666. {
  667. function Unsynchronise($data)
  668. {
  669. // Whenever a false synchronisation is found within the tag, one zeroed
  670. // byte is inserted after the first false synchronisation byte. The
  671. // format of a correct sync that should be altered by ID3 encoders is as
  672. // follows:
  673. // %11111111 111xxxxx
  674. // And should be replaced with:
  675. // %11111111 00000000 111xxxxx
  676. // This has the side effect that all $FF 00 combinations have to be
  677. // altered, so they won't be affected by the decoding process. Therefore
  678. // all the $FF 00 combinations have to be replaced with the $FF 00 00
  679. // combination during the unsynchronisation.
  680. $data = str_replace(chr(0xFF) . chr(0x00), chr(0xFF) . chr(0x00) . chr(0x00), $data);
  681. $unsyncheddata = '';
  682. for($i = 0; $i < strlen($data); $i ++)
  683. {
  684. $thischar = $data{$i};
  685. $unsyncheddata .= $thischar;
  686. if ($thischar == chr(255))
  687. {
  688. $nextchar = ord(substr($data, $i + 1, 1));
  689. if (($nextchar | 0xE0) == 0xE0)
  690. {
  691. // previous byte = 11111111, this byte = 111?????
  692. $unsyncheddata .= chr(0);
  693. }
  694. }
  695. }
  696. return $unsyncheddata;
  697. }
  698. }
  699. if (! function_exists('is_hash'))
  700. {
  701. function is_hash($var)
  702. {
  703. // written by dev-null@christophe.vg
  704. // taken from http://www.php.net/manual/en/function.array-merge-recursive.php
  705. if (is_array($var))
  706. {
  707. $keys = array_keys($var);
  708. $all_num = true;
  709. for($i = 0; $i < count($keys); $i ++)
  710. {
  711. if (is_string($keys[$i]))
  712. {
  713. return true;
  714. }
  715. }
  716. }
  717. return false;
  718. }
  719. }
  720. if (! function_exists('array_join_merge'))
  721. {
  722. function array_join_merge($arr1, $arr2)
  723. {
  724. // written by dev-null@christophe.vg
  725. // taken from http://www.php.net/manual/en/function.array-merge-recursive.php
  726. if (is_array($arr1) && is_array($arr2))
  727. {
  728. // the same -> merge
  729. $new_array = array();
  730. if (is_hash($arr1) && is_hash($arr2))
  731. {
  732. // hashes -> merge based on keys
  733. $keys = array_merge(array_keys($arr1), array_keys($arr2));
  734. foreach ($keys as $key)
  735. {
  736. $new_array[$key] = array_join_merge(@$arr1[$key], @$arr2[$key]);
  737. }
  738. }
  739. else
  740. {
  741. // two real arrays -> merge
  742. $new_array = array_reverse(array_unique(array_reverse(array_merge($arr1, $arr2))));
  743. }
  744. return $new_array;
  745. }
  746. else
  747. {
  748. // not the same ... take new one if defined, else the old one stays
  749. return $arr2 ? $arr2 : $arr1;
  750. }
  751. }
  752. }
  753. if (! function_exists('array_merge_clobber'))
  754. {
  755. function array_merge_clobber($array1, $array2)
  756. {
  757. // written by kc@hireability.com
  758. // taken from http://www.php.net/manual/en/function.array-merge-recursive.php
  759. if (! is_array($array1) || ! is_array($array2))
  760. {
  761. return false;
  762. }
  763. $newarray = $array1;
  764. foreach ($array2 as $key => $val)
  765. {
  766. if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key]))
  767. {
  768. $newarray[$key] = array_merge_clobber($newarray[$key], $val);
  769. }
  770. else
  771. {
  772. $newarray[$key] = $val;
  773. }
  774. }
  775. return $newarray;
  776. }
  777. }
  778. if (! function_exists('array_merge_noclobber'))
  779. {
  780. function array_merge_noclobber($array1, $array2)
  781. {
  782. if (! is_array($array1) || ! is_array($array2))
  783. {
  784. return false;
  785. }
  786. $newarray = $array1;
  787. foreach ($array2 as $key => $val)
  788. {
  789. if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key]))
  790. {
  791. $newarray[$key] = array_merge_noclobber($newarray[$key], $val);
  792. }
  793. elseif (! isset($newarray[$key]))
  794. {
  795. $newarray[$key] = $val;
  796. }
  797. }
  798. return $newarray;
  799. }
  800. }
  801. if (! function_exists('RoughTranslateUnicodeToASCII'))
  802. {
  803. function RoughTranslateUnicodeToASCII($rawdata, $frame_textencoding)
  804. {
  805. // rough translation of data for application that can't handle Unicode data
  806. $tempstring = '';
  807. switch ($frame_textencoding)
  808. {
  809. case 0 : // ISO-8859-1. Terminated with $00.
  810. $asciidata = $rawdata;
  811. break;
  812. case 1 : // UTF-16 encoded Unicode with BOM. Terminated with $00 00.
  813. $asciidata = $rawdata;
  814. if (substr($asciidata, 0, 2) == chr(0xFF) . chr(0xFE))
  815. {
  816. // remove BOM, only if present (it should be, but...)
  817. $asciidata = substr($asciidata, 2);
  818. }
  819. if (substr($asciidata, strlen($asciidata) - 2, 2) == chr(0) . chr(0))
  820. {
  821. $asciidata = substr($asciidata, 0, strlen($asciidata) - 2); // remove terminator, only if present (it should be, but...)
  822. }
  823. for($i = 0; $i < strlen($asciidata); $i += 2)
  824. {
  825. if ((ord($asciidata{$i}) <= 0x7F) || (ord($asciidata{$i}) >= 0xA0))
  826. {
  827. $tempstring .= $asciidata{$i};
  828. }
  829. else
  830. {
  831. $tempstring .= '?';
  832. }
  833. }
  834. $asciidata = $tempstring;
  835. break;
  836. case 2 : // UTF-16BE encoded Unicode without BOM. Terminated with $00 00.
  837. $asciidata = $rawdata;
  838. if (substr($asciidata, strlen($asciidata) - 2, 2) == chr(0) . chr(0))
  839. {
  840. $asciidata = substr($asciidata, 0, strlen($asciidata) - 2); // remove terminator, only if present (it should be, but...)
  841. }
  842. for($i = 0; $i < strlen($asciidata); $i += 2)
  843. {
  844. if ((ord($asciidata{$i}) <= 0x7F) || (ord($asciidata{$i}) >= 0xA0))
  845. {
  846. $tempstring .= $asciidata{$i};
  847. }
  848. else
  849. {
  850. $tempstring .= '?';
  851. }
  852. }
  853. $asciidata = $tempstring;
  854. break;
  855. case 3 : // UTF-8 encoded Unicode. Terminated with $00.
  856. $asciidata = utf8_decode($rawdata);
  857. break;
  858. case 255 : // Unicode, Big-Endian. Terminated with $00 00.
  859. $asciidata = $rawdata;
  860. if (substr($asciidata, strlen($asciidata) - 2, 2) == chr(0) . chr(0))
  861. {
  862. $asciidata = substr($asciidata, 0, strlen($asciidata) - 2); // remove terminator, only if present (it should be, but...)
  863. }
  864. for($i = 0; ($i + 1) < strlen($asciidata); $i += 2)
  865. {
  866. if ((ord($asciidata{($i + 1)}) <= 0x7F) || (ord($asciidata{($i + 1)}) >= 0xA0))
  867. {
  868. $tempstring .= $asciidata{($i + 1)};
  869. }
  870. else
  871. {
  872. $tempstring .= '?';
  873. }
  874. }
  875. $asciidata = $tempstring;
  876. break;
  877. default :
  878. // shouldn't happen, but in case $frame_textencoding is not 1 <= $frame_textencoding <= 4
  879. // just pass the data through unchanged.
  880. $asciidata = $rawdata;
  881. break;
  882. }
  883. if (substr($asciidata, strlen($asciidata) - 1, 1) == chr(0))
  884. {
  885. // remove null terminator, if present
  886. $asciidata = NoNullString($asciidata);
  887. }
  888. return $asciidata;
  889. // return str_replace(chr(0), '', $asciidata); // just in case any nulls slipped through
  890. }
  891. }
  892. if (! function_exists('PlaytimeString'))
  893. {
  894. function PlaytimeString($playtimeseconds)
  895. {
  896. $contentseconds = round((($playtimeseconds / 60) - floor($playtimeseconds / 60)) * 60);
  897. $contentminutes = floor($playtimeseconds / 60);
  898. if ($contentseconds >= 60)
  899. {
  900. $contentseconds -= 60;
  901. $contentminutes ++;
  902. }
  903. return number_format($contentminutes) . ':' . str_pad($contentseconds, 2, 0, STR_PAD_LEFT);
  904. }
  905. }
  906. if (! function_exists('CloseMatch'))
  907. {
  908. function CloseMatch($value1, $value2, $tolerance)
  909. {
  910. return (abs($value1 - $value2) <= $tolerance);
  911. }
  912. }
  913. if (! function_exists('ID3v1matchesID3v2'))
  914. {
  915. function ID3v1matchesID3v2($id3v1, $id3v2)
  916. {
  917. $requiredindices = array('title', 'artist', 'album', 'year', 'genre', 'comment');
  918. foreach ($requiredindices as $requiredindex)
  919. {
  920. if (! isset($id3v1["$requiredindex"]))
  921. {
  922. $id3v1["$requiredindex"] = '';
  923. }
  924. if (! isset($id3v2["$requiredindex"]))
  925. {
  926. $id3v2["$requiredindex"] = '';
  927. }
  928. }
  929. if (trim($id3v1['title']) != trim(substr($id3v2['title'], 0, 30)))
  930. {
  931. return false;
  932. }
  933. if (trim($id3v1['artist']) != trim(substr($id3v2['artist'], 0, 30)))
  934. {
  935. return false;
  936. }
  937. if (trim($id3v1['album']) != trim(substr($id3v2['album'], 0, 30)))
  938. {
  939. return false;
  940. }
  941. if (trim($id3v1['year']) != trim(substr($id3v2['year'], 0, 4)))
  942. {
  943. return false;
  944. }
  945. if (trim($id3v1['genre']) != trim($id3v2['genre']))
  946. {
  947. return false;
  948. }
  949. if (isset($id3v1['track']))
  950. {
  951. if (! isset($id3v1['track']) || (trim($id3v1['track']) != trim($id3v2['track'])))
  952. {
  953. return false;
  954. }
  955. if (trim($id3v1['comment']) != trim(substr($id3v2['comment'], 0, 28)))
  956. {
  957. return false;
  958. }
  959. }
  960. else
  961. {
  962. if (trim($id3v1['comment']) != trim(substr($id3v2['comment'], 0, 30)))
  963. {
  964. return false;
  965. }
  966. }
  967. return true;
  968. }
  969. }
  970. if (! function_exists('FILETIMEtoUNIXtime'))
  971. {
  972. function FILETIMEtoUNIXtime($FILETIME, $round = true)
  973. {
  974. // FILETIME is a 64-bit unsigned integer representing
  975. // the number of 100-nanosecond intervals since January 1, 1601
  976. // UNIX timestamp is number of seconds since January 1, 1970
  977. // 116444736000000000 = 10000000 * 60 * 60 * 24 * 365 * 369 + 89 leap days
  978. if ($round)
  979. {
  980. return round(($FILETIME - 116444736000000000) / 10000000);
  981. }
  982. return ($FILETIME - 116444736000000000) / 10000000;
  983. }
  984. }
  985. if (! function_exists('GUIDtoBytestring'))
  986. {
  987. function GUIDtoBytestring($GUIDstring)
  988. {
  989. // Microsoft defines these 16-byte (128-bit) GUIDs in the strangest way:
  990. // first 4 bytes are in little-endian order
  991. // next 2 bytes are appended in little-endian order
  992. // next 2 bytes are appended in little-endian order
  993. // next 2 bytes are appended in big-endian order
  994. // next 6 bytes are appended in big-endian order
  995. // AaBbCcDd-EeFf-GgHh-IiJj-KkLlMmNnOoPp is stored as this 16-byte string:
  996. // $Dd $Cc $Bb $Aa $Ff $Ee $Hh $Gg $Ii $Jj $Kk $Ll $Mm $Nn $Oo $Pp
  997. $hexbytecharstring = chr(hexdec(substr($GUIDstring, 6, 2)));
  998. $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 4, 2)));
  999. $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 2, 2)));
  1000. $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 0, 2)));
  1001. $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 11, 2)));
  1002. $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 9, 2)));
  1003. $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 16, 2)));
  1004. $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 14, 2)));
  1005. $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 19, 2)));
  1006. $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 21, 2)));
  1007. $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 24, 2)));
  1008. $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 26, 2)));
  1009. $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 28, 2)));
  1010. $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 30, 2)));
  1011. $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 32, 2)));
  1012. $hexbytecharstring .= chr(hexdec(substr($GUIDstring, 34, 2)));
  1013. return $hexbytecharstring;
  1014. }
  1015. }
  1016. if (! function_exists('BytestringToGUID'))
  1017. {
  1018. function BytestringToGUID($Bytestring)
  1019. {
  1020. $GUIDstring = str_pad(dechex(ord($Bytestring{3})), 2, '0', STR_PAD_LEFT);
  1021. $GUIDstring .= str_pad(dechex(ord($Bytestring{2})), 2, '0', STR_PAD_LEFT);
  1022. $GUIDstring .= str_pad(dechex(ord($Bytestring{1})), 2, '0', STR_PAD_LEFT);
  1023. $GUIDstring .= str_pad(dechex(ord($Bytestring{0})), 2, '0', STR_PAD_LEFT);
  1024. $GUIDstring .= '-';
  1025. $GUIDstring .= str_pad(dechex(ord($Bytestring{5})), 2, '0', STR_PAD_LEFT);
  1026. $GUIDstring .= str_pad(dechex(ord($Bytestring{4})), 2, '0', STR_PAD_LEFT);
  1027. $GUIDstring .= '-';
  1028. $GUIDstring .= str_pad(dechex(ord($Bytestring{7})), 2, '0', STR_PAD_LEFT);
  1029. $GUIDstring .= str_pad(dechex(ord($Bytestring{6})), 2, '0', STR_PAD_LEFT);
  1030. $GUIDstring .= '-';
  1031. $GUIDstring .= str_pad(dechex(ord($Bytestring{8})), 2, '0', STR_PAD_LEFT);
  1032. $GUIDstring .= str_pad(dechex(ord($Bytestring{9})), 2, '0', STR_PAD_LEFT);
  1033. $GUIDstring .= '-';
  1034. $GUIDstring .= str_pad(dechex(ord($Bytestring{10})), 2, '0', STR_PAD_LEFT);
  1035. $GUIDstring .= str_pad(dechex(ord($Bytestring{11})), 2, '0', STR_PAD_LEFT);
  1036. $GUIDstring .= str_pad(dechex(ord($Bytestring{12})), 2, '0', STR_PAD_LEFT);
  1037. $GUIDstring .= str_pad(dechex(ord($Bytestring{13})), 2, '0', STR_PAD_LEFT);
  1038. $GUIDstring .= str_pad(dechex(ord($Bytestring{14})), 2, '0', STR_PAD_LEFT);
  1039. $GUIDstring .= str_pad(dechex(ord($Bytestring{15})), 2, '0', STR_PAD_LEFT);
  1040. return strtoupper($GUIDstring);
  1041. }
  1042. }
  1043. if (! function_exists('BitrateColor'))
  1044. {
  1045. function BitrateColor($bitrate)
  1046. {
  1047. $bitrate /= 3; // scale from 1-768kbps to 1-256kbps
  1048. $bitrate --; // scale from 1-256kbps to 0-255kbps
  1049. $bitrate = max($bitrate, 0);
  1050. $bitrate = min($bitrate, 255);
  1051. //$bitrate = max($bitrate, 32);
  1052. //$bitrate = min($bitrate, 143);
  1053. //$bitrate = ($bitrate * 2) - 32;
  1054. $Rcomponent = max(255 - ($bitrate * 2), 0);
  1055. $Gcomponent = max(($bitrate * 2) - 255, 0);
  1056. if ($bitrate > 127)
  1057. {
  1058. $Bcomponent = max((255 - $bitrate) * 2, 0);
  1059. }
  1060. else
  1061. {
  1062. $Bcomponent = max($bitrate * 2, 0);
  1063. }
  1064. return str_pad(dechex($Rcomponent), 2, '0', STR_PAD_LEFT) . str_pad(dechex($Gcomponent), 2, '0', STR_PAD_LEFT) . str_pad(dechex($Bcomponent), 2, '0', STR_PAD_LEFT);
  1065. }
  1066. }
  1067. if (! function_exists('BitrateText'))
  1068. {
  1069. function BitrateText($bitrate)
  1070. {
  1071. return '<SPAN STYLE="color: #' . BitrateColor($bitrate) . '">' . round($bitrate) . ' kbps</SPAN>';
  1072. }
  1073. }
  1074. if (! function_exists('image_type_to_mime_type'))
  1075. {
  1076. function image_type_to_mime_type($imagetypeid)
  1077. {
  1078. // only available in PHP v4.3.0+
  1079. static $image_type_to_mime_type = array();
  1080. if (empty($image_type_to_mime_type))
  1081. {
  1082. $image_type_to_mime_type[1] = 'image/gif'; // GIF
  1083. $image_type_to_mime_type[2] = 'image/jpeg'; // JPEG
  1084. $image_type_to_mime_type[3] = 'image/png'; // PNG
  1085. $image_type_to_mime_type[4] = 'application/x-shockwave-flash'; // Flash
  1086. $image_type_to_mime_type[5] = 'image/psd'; // PSD
  1087. $image_type_to_mime_type[6] = 'image/bmp'; // BMP
  1088. $image_type_to_mime_type[7] = 'image/tiff'; // TIFF: little-endian (Intel)
  1089. $image_type_to_mime_type[8] = 'image/tiff'; // TIFF: big-endian (Motorola)
  1090. //$image_type_to_mime_type[9] = 'image/jpc'; // JPC
  1091. //$image_type_to_mime_type[10] = 'image/jp2'; // JPC
  1092. //$image_type_to_mime_type[11] = 'image/jpx'; // JPC
  1093. //$image_type_to_mime_type[12] = 'image/jb2'; // JPC
  1094. $image_type_to_mime_type[13] = 'application/x-shockwave-flash'; // Shockwave
  1095. $image_type_to_mime_type[14] = 'image/iff'; // IFF
  1096. }
  1097. return (isset($image_type_to_mime_type[$imagetypeid]) ? $image_type_to_mime_type[$imagetypeid] : 'application/octet-stream');
  1098. }
  1099. }
  1100. if (! function_exists('utf8_decode'))
  1101. {
  1102. // PHP has this function built-in if it's configured with the --with-xml option
  1103. // This version of the function is only provided in case XML isn't installed
  1104. function utf8_decode($utf8text)
  1105. {
  1106. // http://www.php.net/manual/en/function.utf8-encode.php
  1107. // bytes bits representation
  1108. // 1 7 0bbbbbbb
  1109. // 2 11 110bbbbb 10bbbbbb
  1110. // 3 16 1110bbbb 10bbbbbb 10bbbbbb
  1111. // 4 21 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
  1112. $utf8length = strlen($utf8text);
  1113. $decodedtext = '';
  1114. for($i = 0; $i < $utf8length; $i ++)
  1115. {
  1116. if ((ord($utf8text{$i}) & 0x80) == 0)
  1117. {
  1118. $decodedtext .= $utf8text{$i};
  1119. }
  1120. elseif ((ord($utf8text{$i}) & 0xF0) == 0xF0)
  1121. {
  1122. $decodedtext .= '?';
  1123. $i += 3;
  1124. }
  1125. elseif ((ord($utf8text{$i}) & 0xE0) == 0xE0)
  1126. {
  1127. $decodedtext .= '?';
  1128. $i += 2;
  1129. }
  1130. elseif ((ord($utf8text{$i}) & 0xC0) == 0xC0)
  1131. {
  1132. // 2 11 110bbbbb 10bbbbbb
  1133. $decodedchar = Bin2Dec(substr(Dec2Bin(ord($utf8text{$i})), 3, 5) . substr(Dec2Bin(ord($utf8text{($i + 1)})), 2, 6));
  1134. if ($decodedchar <= 255)
  1135. {
  1136. $decodedtext .= chr($decodedchar);
  1137. }
  1138. else
  1139. {
  1140. $decodedtext .= '?';
  1141. }
  1142. $i += 1;
  1143. }
  1144. }
  1145. return $decodedtext;
  1146. }
  1147. }
  1148. if (! function_exists('DateMac2Unix'))
  1149. {
  1150. function DateMac2Unix($macdate)
  1151. {
  1152. // Macintosh timestamp: seconds since 00:00h January 1, 1904
  1153. // UNIX timestamp: seconds since 00:00h January 1, 1970
  1154. return CastAsInt($macdate - 2082844800);
  1155. }
  1156. }
  1157. if (! function_exists('FixedPoint8_8'))
  1158. {
  1159. function FixedPoint8_8($rawdata)
  1160. {
  1161. return BigEndian2Int(substr($rawdata, 0, 1)) + (float) (BigEndian2Int(substr($rawdata, 1, 1)) / pow(2, 8));
  1162. }
  1163. }
  1164. if (! function_exists('FixedPoint16_16'))
  1165. {
  1166. function FixedPoint16_16($rawdata)
  1167. {
  1168. return BigEndian2Int(substr($rawdata, 0, 2)) + (float) (BigEndian2Int(substr($rawdata, 2, 2)) / pow(2, 16));
  1169. }
  1170. }
  1171. if (! function_exists('FixedPoint2_30'))
  1172. {
  1173. function FixedPoint2_30($rawdata)
  1174. {
  1175. $binarystring = BigEndian2Bin($rawdata);
  1176. return Bin2Dec(substr($binarystring, 0, 2)) + (float) (Bin2Dec(substr($binarystring, 2, 30)) / pow(2, 30));
  1177. }
  1178. }
  1179. if (! function_exists('Pascal2String'))
  1180. {
  1181. function Pascal2String($pascalstring)
  1182. {
  1183. // Pascal strings have 1 byte at the beginning saying how many chars are in the string
  1184. return substr($pascalstring, 1);
  1185. }
  1186. }
  1187. if (! function_exists('NoNullString'))
  1188. {
  1189. function NoNullString($nullterminatedstring)
  1190. {
  1191. // remove the single null terminator on null terminated strings
  1192. if (substr($nullterminatedstring, strlen($nullterminatedstring) - 1, 1) === chr(0))
  1193. {
  1194. return substr($nullterminatedstring, 0, strlen($nullterminatedstring) - 1);
  1195. }
  1196. return $nullterminatedstring;
  1197. }
  1198. }
  1199. if (! function_exists('FileSizeNiceDisplay'))
  1200. {
  1201. function FileSizeNiceDisplay($filesize, $precision = 2)
  1202. {
  1203. if ($filesize < 1000)
  1204. {
  1205. $sizeunit = 'bytes';
  1206. $precision = 0;
  1207. }
  1208. else
  1209. {
  1210. $filesize /= 1024;
  1211. $sizeunit = 'kB';
  1212. }
  1213. if ($filesize >= 1000)
  1214. {
  1215. $filesize /= 1024;
  1216. $sizeunit = 'MB';
  1217. }
  1218. if ($filesize >= 1000)
  1219. {
  1220. $filesize /= 1024;
  1221. $sizeunit = 'GB';
  1222. }
  1223. return number_format($filesize, $precision) . ' ' . $sizeunit;
  1224. }
  1225. }
  1226. if (! function_exists('DOStime2UNIXtime'))
  1227. {
  1228. function DOStime2UNIXtime($DOSdate, $DOStime)
  1229. {
  1230. // wFatDate
  1231. // Specifies the MS-DOS date. The date is a packed 16-bit value with the following format:
  1232. // Bits Contents
  1233. // 0-4 Day of the month (1-31)
  1234. // 5-8 Month (1 = January, 2 = February, and so on)
  1235. // 9-15 Year offset from 1980 (add 1980 to get actual year)
  1236. $UNIXday = ($DOSdate & 0x001F);
  1237. $UNIXmonth = (($DOSdate & 0x01E0) >> 5);
  1238. $UNIXyear = (($DOSdate & 0xFE00) >> 9) + 1980;
  1239. // wFatTime
  1240. // Specifies the MS-DOS time. The time is a packed 16-bit value with the following format:
  1241. // Bits Contents
  1242. // 0-4 Second divided by 2
  1243. // 5-10 Minute (0-59)
  1244. // 11-15 Hour (0-23 on a 24-hour clock)
  1245. $UNIXsecond = ($DOStime & 0x001F) * 2;
  1246. $UNIXminute = (($DOStime & 0x07E0) >> 5);
  1247. $UNIXhour = (($DOStime & 0xF800) >> 11);
  1248. return mktime($UNIXhour, $UNIXminute, $UNIXsecond, $UNIXmonth, $UNIXday, $UNIXyear);
  1249. }
  1250. }
  1251. if (! function_exists('CreateDeepArray'))
  1252. {
  1253. function CreateDeepArray($ArrayPath, $Separator, $Value)
  1254. {
  1255. // assigns $Value to a nested array path:
  1256. // $foo = CreateDeepArray('/path/to/my', '/', 'file.txt')
  1257. // is the same as:
  1258. // $foo = array('path'=>array('to'=>'array('my'=>array('file.txt'))));
  1259. // or
  1260. // $foo['path']['to']['my'] = 'file.txt';
  1261. while ($ArrayPath{0} == $Separator)
  1262. {
  1263. $ArrayPath = substr($ArrayPath, 1);
  1264. }
  1265. if (($pos = strpos($ArrayPath, $Separator)) !== false)
  1266. {
  1267. $ReturnedArray[substr($ArrayPath, 0, $pos)] = CreateDeepArray(substr($ArrayPath, $pos + 1), $Separator, $Value);
  1268. }
  1269. else
  1270. {
  1271. $ReturnedArray["$ArrayPath"] = $Value;
  1272. }
  1273. return $ReturnedArray;
  1274. }
  1275. }
  1276. if (! function_exists('md5_file'))
  1277. {
  1278. // Allan Hansen <ah@artemis.dk>
  1279. // md5_file() exists in PHP 4.2.0.
  1280. // The following works under UNIX only, but dies on windows
  1281. function md5_file($file)
  1282. {
  1283. if (substr(php_uname(), 0, 7) == 'Windows')
  1284. {
  1285. die('PHP 4.2.0 or newer required for md5_file()');
  1286. }
  1287. $file = str_replace('`', '\\`', $file);
  1288. if (ereg("^([0-9a-f]{32})[ \t\n\r]", `md5sum "$file"`, $r))
  1289. {
  1290. return $r[1];
  1291. }
  1292. return false;
  1293. }
  1294. }
  1295. if (! function_exists('md5_data'))
  1296. {
  1297. // Allan Hansen <ah@artemis.dk>
  1298. // md5_data() - returns md5sum for a file from startuing position to absolute end position
  1299. function md5_data($file, $offset, $end, $invertsign = false)
  1300. {
  1301. // first try and create a temporary file in the same directory as the file being scanned
  1302. if (($dataMD5filename = tempnam(dirname($file), eregi_replace('[^[:alnum:]]', '', basename($file)))) === false)
  1303. {
  1304. // if that fails, create a temporary file in the system temp directory
  1305. if (($dataMD5filename = tempnam('/tmp', 'getID3')) === false)
  1306. {
  1307. // if that fails, create a temporary file in the current directory
  1308. if (($dataMD5filename = tempnam('.', eregi_replace('[^[:alnum:]]', '', basename($file)))) === false)
  1309. {
  1310. // can't find anywhere to create a temp file, just die
  1311. return false;
  1312. }
  1313. }
  1314. }
  1315. $md5 = false;
  1316. set_time_limit(max(filesize($file) / 1000000, 30));
  1317. // copy parts of file
  1318. if ($fp = @fopen($file, 'rb'))
  1319. {
  1320. if ($MD5fp = @fopen($dataMD5filename, 'wb'))
  1321. {
  1322. if ($invertsign)
  1323. {
  1324. // Load conversion lookup strings for 8-bit unsigned->signed conversion below
  1325. $from = '';
  1326. $to = '';
  1327. for($i = 0; $i < 128; $i ++)
  1328. {
  1329. $from .= chr($i);
  1330. $to .= chr($i + 128);
  1331. }
  1332. for($i = 128; $i < 256; $i ++)
  1333. {
  1334. $from .= chr($i);
  1335. $to .= chr($i - 128);
  1336. }
  1337. }
  1338. fseek($fp, $offset, SEEK_SET);
  1339. $byteslefttowrite = $end - $offset;
  1340. while (($byteslefttowrite > 0) && ($buffer = fread($fp, FREAD_BUFFER_SIZE)))
  1341. {
  1342. if ($invertsign)
  1343. {
  1344. // Possibly FLAC-specific (?)
  1345. // FLAC calculates the MD5sum of the source data of 8-bit files
  1346. // not on the actual byte values in the source file, but of those
  1347. // values converted from unsigned to signed, or more specifcally,
  1348. // with the MSB inverted. ex: 01 -> 81; F5 -> 75; etc
  1349. // Therefore, 8-bit WAV data has to be converted before getting the
  1350. // md5_data value so as to match the FLAC value
  1351. //…

Large files files are truncated, but you can click here to view the full file