PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/chamilo/chamilo/
PHP | 1785 lines | 1408 code | 218 blank | 159 comment | 411 complexity | 0509f104cfa1896cfa61831ca37dfb01 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT

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

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

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