PageRenderTime 44ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/common/libraries/plugin/getid3/getid3.php

https://bitbucket.org/renaatdemuynck/chamilo
PHP | 1334 lines | 908 code | 218 blank | 208 comment | 163 complexity | 8ce079475cf4d67b503396fb72243a8a 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. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at http://getid3.sourceforge.net //
  5. // or http://www.getid3.org //
  6. /////////////////////////////////////////////////////////////////
  7. // //
  8. // Please see readme.txt for more information //
  9. // ///
  10. /////////////////////////////////////////////////////////////////
  11. // Defines
  12. define('GETID3_VERSION', '1.7.9-20090308');
  13. define('GETID3_FREAD_BUFFER_SIZE', 16384); // read buffer size in bytes
  14. class getID3
  15. {
  16. // public: Settings
  17. var $encoding = 'ISO-8859-1'; // CASE SENSITIVE! - i.e. (must be supported by iconv())
  18. // Examples: ISO-8859-1 UTF-8 UTF-16 UTF-16BE
  19. var $encoding_id3v1 = 'ISO-8859-1'; // Should always be 'ISO-8859-1', but some tags may be written in other encodings such as 'EUC-CN'
  20. var $tempdir = '*'; // default '*' should use system temp dir
  21. // public: Optional tag checks - disable for speed.
  22. var $option_tag_id3v1 = true; // Read and process ID3v1 tags
  23. var $option_tag_id3v2 = true; // Read and process ID3v2 tags
  24. var $option_tag_lyrics3 = true; // Read and process Lyrics3 tags
  25. var $option_tag_apetag = true; // Read and process APE tags
  26. var $option_tags_process = true; // Copy tags to root key 'tags' and encode to $this->encoding
  27. var $option_tags_html = true; // Copy tags to root key 'tags_html' properly translated from various encodings to HTML entities
  28. // public: Optional tag/comment calucations
  29. var $option_extra_info = true; // Calculate additional info such as bitrate, channelmode etc
  30. // public: Optional calculations
  31. var $option_md5_data = false; // Get MD5 sum of data part - slow
  32. var $option_md5_data_source = false; // Use MD5 of source file if availble - only FLAC and OptimFROG
  33. var $option_sha1_data = false; // Get SHA1 sum of data part - slow
  34. var $option_max_2gb_check = true; // Check whether file is larger than 2 Gb and thus not supported by PHP
  35. // private
  36. var $filename;
  37. // public: constructor
  38. function __construct()
  39. {
  40. $this->startup_error = '';
  41. $this->startup_warning = '';
  42. // Check for PHP version >= 4.2.0
  43. if (phpversion() < '4.2.0')
  44. {
  45. $this->startup_error .= 'getID3() requires PHP v4.2.0 or higher - you are running v' . phpversion();
  46. }
  47. // Check memory
  48. $memory_limit = ini_get('memory_limit');
  49. if (eregi('([0-9]+)M', $memory_limit, $matches))
  50. {
  51. // could be stored as "16M" rather than 16777216 for example
  52. $memory_limit = $matches[1] * 1048576;
  53. }
  54. if ($memory_limit <= 0)
  55. {
  56. // memory limits probably disabled
  57. }
  58. elseif ($memory_limit <= 3145728)
  59. {
  60. $this->startup_error .= 'PHP has less than 3MB available memory and will very likely run out. Increase memory_limit in php.ini';
  61. }
  62. elseif ($memory_limit <= 12582912)
  63. {
  64. $this->startup_warning .= 'PHP has less than 12MB available memory and might run out if all modules are loaded. Increase memory_limit in php.ini';
  65. }
  66. // Check safe_mode off
  67. if ((bool) ini_get('safe_mode'))
  68. {
  69. $this->warning('WARNING: Safe mode is on, shorten support disabled, md5data/sha1data for ogg vorbis disabled, ogg vorbos/flac tag writing disabled.');
  70. }
  71. // define a constant rather than looking up every time it is needed
  72. if (! defined('GETID3_OS_ISWINDOWS'))
  73. {
  74. if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
  75. {
  76. define('GETID3_OS_ISWINDOWS', true);
  77. }
  78. else
  79. {
  80. define('GETID3_OS_ISWINDOWS', false);
  81. }
  82. }
  83. // Get base path of getID3() - ONCE
  84. if (! defined('GETID3_INCLUDEPATH'))
  85. {
  86. foreach (get_included_files() as $key => $val)
  87. {
  88. if (basename($val) == 'getid3.php')
  89. {
  90. define('GETID3_INCLUDEPATH', dirname($val) . DIRECTORY_SEPARATOR);
  91. break;
  92. }
  93. }
  94. }
  95. // Load support library
  96. if (! include_once (GETID3_INCLUDEPATH . 'getid3.lib.php'))
  97. {
  98. $this->startup_error .= 'getid3.lib.php is missing or corrupt';
  99. }
  100. // Needed for Windows only:
  101. // Define locations of helper applications for Shorten, VorbisComment, MetaFLAC
  102. // as well as other helper functions such as head, tail, md5sum, etc
  103. // IMPORTANT: This path cannot have spaces in it. If neccesary, use the 8dot3 equivalent
  104. // ie for "C:/Program Files/Apache/" put "C:/PROGRA~1/APACHE/"
  105. // IMPORTANT: This path must include the trailing slash
  106. if (GETID3_OS_ISWINDOWS && ! defined('GETID3_HELPERAPPSDIR'))
  107. {
  108. $helperappsdir = GETID3_INCLUDEPATH . DIRECTORY_SEPARATOR . 'helperapps'; // must not have any space in this path
  109. if (! is_dir($helperappsdir))
  110. {
  111. $this->startup_error .= '"' . $helperappsdir . '" cannot be defined as GETID3_HELPERAPPSDIR because it does not exist';
  112. }
  113. elseif (strpos(realpath($helperappsdir), ' ') !== false)
  114. {
  115. $DirPieces = explode(DIRECTORY_SEPARATOR, realpath($helperappsdir));
  116. foreach ($DirPieces as $key => $value)
  117. {
  118. if ((strpos($value, '.') !== false) && (strpos($value, ' ') === false))
  119. {
  120. if (strpos($value, '.') > 8)
  121. {
  122. $value = substr($value, 0, 6) . '~1';
  123. }
  124. }
  125. elseif ((strpos($value, ' ') !== false) || strlen($value) > 8)
  126. {
  127. $value = substr($value, 0, 6) . '~1';
  128. }
  129. $DirPieces[$key] = strtoupper($value);
  130. }
  131. $this->startup_error .= 'GETID3_HELPERAPPSDIR must not have any spaces in it - use 8dot3 naming convention if neccesary (on this server that would be something like "' . implode(DIRECTORY_SEPARATOR, $DirPieces) . '" - NOTE: this may or may not be the actual 8.3 equivalent of "' . $helperappsdir . '", please double-check). You can run "dir /x" from the commandline to see the correct 8.3-style names.';
  132. }
  133. define('GETID3_HELPERAPPSDIR', realpath($helperappsdir) . DIRECTORY_SEPARATOR);
  134. }
  135. }
  136. // public: setOption
  137. function setOption($optArray)
  138. {
  139. if (! is_array($optArray) || empty($optArray))
  140. {
  141. return false;
  142. }
  143. foreach ($optArray as $opt => $val)
  144. {
  145. //if (isset($this, $opt) === false) {
  146. if (isset($this->$opt) === false)
  147. {
  148. continue;
  149. }
  150. $this->$opt = $val;
  151. }
  152. return true;
  153. }
  154. // public: analyze file - replaces GetAllFileInfo() and GetTagOnly()
  155. function analyze($filename)
  156. {
  157. if (! empty($this->startup_error))
  158. {
  159. return $this->error($this->startup_error);
  160. }
  161. if (! empty($this->startup_warning))
  162. {
  163. $this->warning($this->startup_warning);
  164. }
  165. // init result array and set parameters
  166. $this->info = array();
  167. $this->info['GETID3_VERSION'] = GETID3_VERSION;
  168. // Check encoding/iconv support
  169. if (! function_exists('iconv') && ! in_array($this->encoding, array('ISO-8859-1', 'UTF-8', 'UTF-16LE',
  170. 'UTF-16BE', 'UTF-16')))
  171. {
  172. $errormessage = 'iconv() support is needed for encodings other than ISO-8859-1, UTF-8, UTF-16LE, UTF16-BE, UTF-16. ';
  173. if (GETID3_OS_ISWINDOWS)
  174. {
  175. $errormessage .= 'PHP does not have iconv() support. Please enable php_iconv.dll in php.ini, and copy iconv.dll from c:/php/dlls to c:/windows/system32';
  176. }
  177. else
  178. {
  179. $errormessage .= 'PHP is not compiled with iconv() support. Please recompile with the --with-iconv switch';
  180. }
  181. return $this->error($errormessage);
  182. }
  183. // Disable magic_quotes_runtime, if neccesary
  184. $old_magic_quotes_runtime = get_magic_quotes_runtime(); // store current setting of magic_quotes_runtime
  185. if ($old_magic_quotes_runtime)
  186. {
  187. set_magic_quotes_runtime(0); // turn off magic_quotes_runtime
  188. if (get_magic_quotes_runtime())
  189. {
  190. return $this->error('Could not disable magic_quotes_runtime - getID3() cannot work properly with this setting enabled');
  191. }
  192. }
  193. // remote files not supported
  194. if (preg_match('/^(ht|f)tp:\/\//', $filename))
  195. {
  196. return $this->error('Remote files are not supported in this version of getID3() - please copy the file locally first');
  197. }
  198. $filename = str_replace('/', DIRECTORY_SEPARATOR, $filename);
  199. $filename = preg_replace('#' . preg_quote(DIRECTORY_SEPARATOR) . '{2,}#', DIRECTORY_SEPARATOR, $filename);
  200. // open local file
  201. if (file_exists($filename) && ($fp = @fopen($filename, 'rb')))
  202. {
  203. // great
  204. }
  205. else
  206. {
  207. return $this->error('Could not open file "' . $filename . '"');
  208. }
  209. // set parameters
  210. $this->info['filesize'] = filesize($filename);
  211. // option_max_2gb_check
  212. if ($this->option_max_2gb_check)
  213. {
  214. // PHP doesn't support integers larger than 31-bit (~2GB)
  215. // filesize() simply returns (filesize % (pow(2, 32)), no matter the actual filesize
  216. // ftell() returns 0 if seeking to the end is beyond the range of unsigned integer
  217. fseek($fp, 0, SEEK_END);
  218. if ((($this->info['filesize'] != 0) && (ftell($fp) == 0)) || ($this->info['filesize'] < 0) || (ftell($fp) < 0))
  219. {
  220. $real_filesize = false;
  221. if (GETID3_OS_ISWINDOWS)
  222. {
  223. $commandline = 'dir /-C "' . str_replace('/', DIRECTORY_SEPARATOR, $filename) . '"';
  224. $dir_output = `$commandline`;
  225. if (eregi('1 File\(s\)[ ]+([0-9]+) bytes', $dir_output, $matches))
  226. {
  227. $real_filesize = (float) $matches[1];
  228. }
  229. }
  230. else
  231. {
  232. $commandline = 'ls -o -g -G --time-style=long-iso ' . escapeshellarg($filename);
  233. $dir_output = `$commandline`;
  234. if (eregi('([0-9]+) ([0-9]{4}-[0-9]{2}\-[0-9]{2} [0-9]{2}:[0-9]{2}) ' . preg_quote($filename) . '$', $dir_output, $matches))
  235. {
  236. $real_filesize = (float) $matches[1];
  237. }
  238. }
  239. if ($real_filesize === false)
  240. {
  241. unset($this->info['filesize']);
  242. fclose($fp);
  243. return $this->error('File is most likely larger than 2GB and is not supported by PHP');
  244. }
  245. elseif ($real_filesize < pow(2, 31))
  246. {
  247. unset($this->info['filesize']);
  248. fclose($fp);
  249. return $this->error('PHP seems to think the file is larger than 2GB, but filesystem reports it as ' . number_format($real_filesize, 3) . 'GB, please report to info@getid3.org');
  250. }
  251. $this->info['filesize'] = $real_filesize;
  252. $this->error('File is larger than 2GB (filesystem reports it as ' . number_format($real_filesize, 3) . 'GB) and is not properly supported by PHP.');
  253. }
  254. }
  255. // set more parameters
  256. $this->info['avdataoffset'] = 0;
  257. $this->info['avdataend'] = $this->info['filesize'];
  258. $this->info['fileformat'] = ''; // filled in later
  259. $this->info['audio']['dataformat'] = ''; // filled in later, unset if not used
  260. $this->info['video']['dataformat'] = ''; // filled in later, unset if not used
  261. $this->info['tags'] = array(); // filled in later, unset if not used
  262. $this->info['error'] = array(); // filled in later, unset if not used
  263. $this->info['warning'] = array(); // filled in later, unset if not used
  264. $this->info['comments'] = array(); // filled in later, unset if not used
  265. $this->info['encoding'] = $this->encoding; // required by id3v2 and iso modules - can be unset at the end if desired
  266. // set redundant parameters - might be needed in some include file
  267. $this->info['filename'] = basename($filename);
  268. $this->info['filepath'] = str_replace('\\', '/', realpath(dirname($filename)));
  269. $this->info['filenamepath'] = $this->info['filepath'] . '/' . $this->info['filename'];
  270. // handle ID3v2 tag - done first - already at beginning of file
  271. // ID3v2 detection (even if not parsing) is always done otherwise fileformat is much harder to detect
  272. if ($this->option_tag_id3v2)
  273. {
  274. $GETID3_ERRORARRAY = &$this->info['warning'];
  275. if (getid3_lib :: IncludeDependency(GETID3_INCLUDEPATH . 'module.tag.id3v2.php', __FILE__, false))
  276. {
  277. $tag = new getid3_id3v2($fp, $this->info);
  278. unset($tag);
  279. }
  280. }
  281. else
  282. {
  283. fseek($fp, 0, SEEK_SET);
  284. $header = fread($fp, 10);
  285. if (substr($header, 0, 3) == 'ID3' && strlen($header) == 10)
  286. {
  287. $this->info['id3v2']['header'] = true;
  288. $this->info['id3v2']['majorversion'] = ord($header{3});
  289. $this->info['id3v2']['minorversion'] = ord($header{4});
  290. $this->info['id3v2']['headerlength'] = getid3_lib :: BigEndian2Int(substr($header, 6, 4), 1) + 10; // length of ID3v2 tag in 10-byte header doesn't include 10-byte header length
  291. $this->info['id3v2']['tag_offset_start'] = 0;
  292. $this->info['id3v2']['tag_offset_end'] = $this->info['id3v2']['tag_offset_start'] + $this->info['id3v2']['headerlength'];
  293. $this->info['avdataoffset'] = $this->info['id3v2']['tag_offset_end'];
  294. }
  295. }
  296. // handle ID3v1 tag
  297. if ($this->option_tag_id3v1)
  298. {
  299. if (! @include_once (GETID3_INCLUDEPATH . 'module.tag.id3v1.php'))
  300. {
  301. return $this->error('module.tag.id3v1.php is missing - you may disable option_tag_id3v1.');
  302. }
  303. $tag = new getid3_id3v1($fp, $this->info);
  304. unset($tag);
  305. }
  306. // handle APE tag
  307. if ($this->option_tag_apetag)
  308. {
  309. if (! @include_once (GETID3_INCLUDEPATH . 'module.tag.apetag.php'))
  310. {
  311. return $this->error('module.tag.apetag.php is missing - you may disable option_tag_apetag.');
  312. }
  313. $tag = new getid3_apetag($fp, $this->info);
  314. unset($tag);
  315. }
  316. // handle lyrics3 tag
  317. if ($this->option_tag_lyrics3)
  318. {
  319. if (! @include_once (GETID3_INCLUDEPATH . 'module.tag.lyrics3.php'))
  320. {
  321. return $this->error('module.tag.lyrics3.php is missing - you may disable option_tag_lyrics3.');
  322. }
  323. $tag = new getid3_lyrics3($fp, $this->info);
  324. unset($tag);
  325. }
  326. // read 32 kb file data
  327. fseek($fp, $this->info['avdataoffset'], SEEK_SET);
  328. $formattest = fread($fp, 32774);
  329. // determine format
  330. $determined_format = $this->GetFileFormat($formattest, $filename);
  331. // unable to determine file format
  332. if (! $determined_format)
  333. {
  334. fclose($fp);
  335. return $this->error('unable to determine file format');
  336. }
  337. // check for illegal ID3 tags
  338. if (isset($determined_format['fail_id3']) && (in_array('id3v1', $this->info['tags']) || in_array('id3v2', $this->info['tags'])))
  339. {
  340. if ($determined_format['fail_id3'] === 'ERROR')
  341. {
  342. fclose($fp);
  343. return $this->error('ID3 tags not allowed on this file type.');
  344. }
  345. elseif ($determined_format['fail_id3'] === 'WARNING')
  346. {
  347. $this->info['warning'][] = 'ID3 tags not allowed on this file type.';
  348. }
  349. }
  350. // check for illegal APE tags
  351. if (isset($determined_format['fail_ape']) && in_array('ape', $this->info['tags']))
  352. {
  353. if ($determined_format['fail_ape'] === 'ERROR')
  354. {
  355. fclose($fp);
  356. return $this->error('APE tags not allowed on this file type.');
  357. }
  358. elseif ($determined_format['fail_ape'] === 'WARNING')
  359. {
  360. $this->info['warning'][] = 'APE tags not allowed on this file type.';
  361. }
  362. }
  363. // set mime type
  364. $this->info['mime_type'] = $determined_format['mime_type'];
  365. // supported format signature pattern detected, but module deleted
  366. if (! file_exists(GETID3_INCLUDEPATH . $determined_format['include']))
  367. {
  368. fclose($fp);
  369. return $this->error('Format not supported, module "' . $determined_format['include'] . '" was removed.');
  370. }
  371. // module requires iconv support
  372. if (! function_exists('iconv') && @$determined_format['iconv_req'])
  373. {
  374. return $this->error('iconv support is required for this module (' . $determined_format['include'] . ').');
  375. }
  376. // include module
  377. include_once (GETID3_INCLUDEPATH . $determined_format['include']);
  378. // instantiate module class
  379. $class_name = 'getid3_' . $determined_format['module'];
  380. if (! class_exists($class_name))
  381. {
  382. return $this->error('Format not supported, module "' . $determined_format['include'] . '" is corrupt.');
  383. }
  384. if (isset($determined_format['option']))
  385. {
  386. $class = new $class_name($fp, $this->info, $determined_format['option']);
  387. }
  388. else
  389. {
  390. $class = new $class_name($fp, $this->info);
  391. }
  392. unset($class);
  393. // close file
  394. fclose($fp);
  395. // process all tags - copy to 'tags' and convert charsets
  396. if ($this->option_tags_process)
  397. {
  398. $this->HandleAllTags();
  399. }
  400. // perform more calculations
  401. if ($this->option_extra_info)
  402. {
  403. $this->ChannelsBitratePlaytimeCalculations();
  404. $this->CalculateCompressionRatioVideo();
  405. $this->CalculateCompressionRatioAudio();
  406. $this->CalculateReplayGain();
  407. $this->ProcessAudioStreams();
  408. }
  409. // get the MD5 sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags
  410. if ($this->option_md5_data)
  411. {
  412. // do not cald md5_data if md5_data_source is present - set by flac only - future MPC/SV8 too
  413. if (! $this->option_md5_data_source || empty($this->info['md5_data_source']))
  414. {
  415. $this->getHashdata('md5');
  416. }
  417. }
  418. // get the SHA1 sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags
  419. if ($this->option_sha1_data)
  420. {
  421. $this->getHashdata('sha1');
  422. }
  423. // remove undesired keys
  424. $this->CleanUp();
  425. // restore magic_quotes_runtime setting
  426. set_magic_quotes_runtime($old_magic_quotes_runtime);
  427. // return info array
  428. return $this->info;
  429. }
  430. // private: error handling
  431. function error($message)
  432. {
  433. $this->CleanUp();
  434. $this->info['error'][] = $message;
  435. return $this->info;
  436. }
  437. // private: warning handling
  438. function warning($message)
  439. {
  440. $this->info['warning'][] = $message;
  441. return true;
  442. }
  443. // private: CleanUp
  444. function CleanUp()
  445. {
  446. // remove possible empty keys
  447. $AVpossibleEmptyKeys = array('dataformat', 'bits_per_sample', 'encoder_options', 'streams', 'bitrate');
  448. foreach ($AVpossibleEmptyKeys as $dummy => $key)
  449. {
  450. if (empty($this->info['audio'][$key]) && isset($this->info['audio'][$key]))
  451. {
  452. unset($this->info['audio'][$key]);
  453. }
  454. if (empty($this->info['video'][$key]) && isset($this->info['video'][$key]))
  455. {
  456. unset($this->info['video'][$key]);
  457. }
  458. }
  459. // remove empty root keys
  460. if (! empty($this->info))
  461. {
  462. foreach ($this->info as $key => $value)
  463. {
  464. if (empty($this->info[$key]) && ($this->info[$key] !== 0) && ($this->info[$key] !== '0'))
  465. {
  466. unset($this->info[$key]);
  467. }
  468. }
  469. }
  470. // remove meaningless entries from unknown-format files
  471. if (empty($this->info['fileformat']))
  472. {
  473. if (isset($this->info['avdataoffset']))
  474. {
  475. unset($this->info['avdataoffset']);
  476. }
  477. if (isset($this->info['avdataend']))
  478. {
  479. unset($this->info['avdataend']);
  480. }
  481. }
  482. }
  483. // return array containing information about all supported formats
  484. function GetFileFormatArray()
  485. {
  486. static $format_info = array();
  487. if (empty($format_info))
  488. {
  489. $format_info = array(
  490. // Audio formats
  491. // AC-3 - audio - Dolby AC-3 / Dolby Digital
  492. 'ac3' => array('pattern' => '^\x0B\x77', 'group' => 'audio',
  493. 'module' => 'ac3', 'mime_type' => 'audio/ac3'),
  494. // AAC - audio - Advanced Audio Coding (AAC) - ADIF format
  495. 'adif' => array('pattern' => '^ADIF', 'group' => 'audio', 'module' => 'aac',
  496. 'option' => 'adif', 'mime_type' => 'application/octet-stream', 'fail_ape' => 'WARNING'),
  497. // AAC - audio - Advanced Audio Coding (AAC) - ADTS format (very similar to MP3)
  498. 'adts' => array('pattern' => '^\xFF[\xF0-\xF1\xF8-\xF9]',
  499. 'group' => 'audio', 'module' => 'aac', 'option' => 'adts',
  500. 'mime_type' => 'application/octet-stream', 'fail_ape' => 'WARNING'),
  501. // AU - audio - NeXT/Sun AUdio (AU)
  502. 'au' => array('pattern' => '^\.snd', 'group' => 'audio', 'module' => 'au',
  503. 'mime_type' => 'audio/basic'),
  504. // AVR - audio - Audio Visual Research
  505. 'avr' => array('pattern' => '^2BIT', 'group' => 'audio', 'module' => 'avr',
  506. 'mime_type' => 'application/octet-stream'),
  507. // BONK - audio - Bonk v0.9+
  508. 'bonk' => array('pattern' => '^\x00(BONK|INFO|META| ID3)',
  509. 'group' => 'audio', 'module' => 'bonk', 'mime_type' => 'audio/xmms-bonk'),
  510. // DSS - audio - Digital Speech Standard
  511. 'dss' => array('pattern' => '^[\x02]dss', 'group' => 'audio',
  512. 'module' => 'dss', 'mime_type' => 'application/octet-stream'),
  513. // DTS - audio - Dolby Theatre System
  514. 'dts' => array('pattern' => '^\x7F\xFE\x80\x01', 'group' => 'audio',
  515. 'module' => 'dts', 'mime_type' => 'audio/dts'),
  516. // FLAC - audio - Free Lossless Audio Codec
  517. 'flac' => array('pattern' => '^fLaC', 'group' => 'audio', 'module' => 'flac',
  518. 'mime_type' => 'audio/x-flac'),
  519. // LA - audio - Lossless Audio (LA)
  520. 'la' => array('pattern' => '^LA0[2-4]', 'group' => 'audio', 'module' => 'la',
  521. 'mime_type' => 'application/octet-stream'),
  522. // LPAC - audio - Lossless Predictive Audio Compression (LPAC)
  523. 'lpac' => array('pattern' => '^LPAC', 'group' => 'audio', 'module' => 'lpac',
  524. 'mime_type' => 'application/octet-stream'),
  525. // MIDI - audio - MIDI (Musical Instrument Digital Interface)
  526. 'midi' => array('pattern' => '^MThd', 'group' => 'audio', 'module' => 'midi',
  527. 'mime_type' => 'audio/midi'),
  528. // MAC - audio - Monkey's Audio Compressor
  529. 'mac' => array('pattern' => '^MAC ', 'group' => 'audio',
  530. 'module' => 'monkey', 'mime_type' => 'application/octet-stream'),
  531. // has been known to produce false matches in random files (e.g. JPEGs), leave out until more precise matching available
  532. // // MOD - audio - MODule (assorted sub-formats)
  533. // 'mod' => array(
  534. // 'pattern' => '^.{1080}(M\\.K\\.|M!K!|FLT4|FLT8|[5-9]CHN|[1-3][0-9]CH)',
  535. // 'group' => 'audio',
  536. // 'module' => 'mod',
  537. // 'option' => 'mod',
  538. // 'mime_type' => 'audio/mod',
  539. // ),
  540. // MOD - audio - MODule (Impulse Tracker)
  541. 'it' => array('pattern' => '^IMPM', 'group' => 'audio', 'module' => 'mod',
  542. 'option' => 'it', 'mime_type' => 'audio/it'),
  543. // MOD - audio - MODule (eXtended Module, various sub-formats)
  544. 'xm' => array('pattern' => '^Extended Module', 'group' => 'audio',
  545. 'module' => 'mod', 'option' => 'xm', 'mime_type' => 'audio/xm'),
  546. // MOD - audio - MODule (ScreamTracker)
  547. 's3m' => array('pattern' => '^.{44}SCRM', 'group' => 'audio',
  548. 'module' => 'mod', 'option' => 's3m', 'mime_type' => 'audio/s3m'),
  549. // MPC - audio - Musepack / MPEGplus
  550. 'mpc' => array(
  551. 'pattern' => '^(MPCK|MP\+|[\x00\x01\x10\x11\x40\x41\x50\x51\x80\x81\x90\x91\xC0\xC1\xD0\xD1][\x20-37][\x00\x20\x40\x60\x80\xA0\xC0\xE0])',
  552. 'group' => 'audio', 'module' => 'mpc', 'mime_type' => 'audio/x-musepack'),
  553. // MP3 - audio - MPEG-audio Layer 3 (very similar to AAC-ADTS)
  554. 'mp3' => array('pattern' => '^\xFF[\xE2-\xE7\xF2-\xF7\xFA-\xFF][\x00-\xEB]',
  555. 'group' => 'audio', 'module' => 'mp3', 'mime_type' => 'audio/mpeg'),
  556. // OFR - audio - OptimFROG
  557. 'ofr' => array('pattern' => '^(\*RIFF|OFR)', 'group' => 'audio',
  558. 'module' => 'optimfrog', 'mime_type' => 'application/octet-stream'),
  559. // RKAU - audio - RKive AUdio compressor
  560. 'rkau' => array('pattern' => '^RKA', 'group' => 'audio', 'module' => 'rkau',
  561. 'mime_type' => 'application/octet-stream'),
  562. // SHN - audio - Shorten
  563. 'shn' => array('pattern' => '^ajkg', 'group' => 'audio',
  564. 'module' => 'shorten', 'mime_type' => 'audio/xmms-shn', 'fail_id3' => 'ERROR',
  565. 'fail_ape' => 'ERROR'),
  566. // TTA - audio - TTA Lossless Audio Compressor (http://tta.corecodec.org)
  567. 'tta' => array('pattern' => '^TTA', // could also be '^TTA(\x01|\x02|\x03|2|1)'
  568. 'group' => 'audio', 'module' => 'tta',
  569. 'mime_type' => 'application/octet-stream'),
  570. // VOC - audio - Creative Voice (VOC)
  571. 'voc' => array('pattern' => '^Creative Voice File', 'group' => 'audio',
  572. 'module' => 'voc', 'mime_type' => 'audio/voc'),
  573. // VQF - audio - transform-domain weighted interleave Vector Quantization Format (VQF)
  574. 'vqf' => array('pattern' => '^TWIN', 'group' => 'audio', 'module' => 'vqf',
  575. 'mime_type' => 'application/octet-stream'),
  576. // WV - audio - WavPack (v4.0+)
  577. 'wv' => array('pattern' => '^wvpk', 'group' => 'audio',
  578. 'module' => 'wavpack', 'mime_type' => 'application/octet-stream'),
  579. // Audio-Video formats
  580. // ASF - audio/video - Advanced Streaming Format, Windows Media Video, Windows Media Audio
  581. 'asf' => array(
  582. 'pattern' => '^\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C',
  583. 'group' => 'audio-video', 'module' => 'asf', 'mime_type' => 'video/x-ms-asf',
  584. 'iconv_req' => false),
  585. // BINK - audio/video - Bink / Smacker
  586. 'bink' => array('pattern' => '^(BIK|SMK)', 'group' => 'audio-video',
  587. 'module' => 'bink', 'mime_type' => 'application/octet-stream'),
  588. // FLV - audio/video - FLash Video
  589. 'flv' => array('pattern' => '^FLV\x01', 'group' => 'audio-video',
  590. 'module' => 'flv', 'mime_type' => 'video/x-flv'),
  591. // MKAV - audio/video - Mastroka
  592. 'matroska' => array('pattern' => '^\x1A\x45\xDF\xA3',
  593. 'group' => 'audio-video', 'module' => 'matroska', 'mime_type' => 'video/x-matroska')// may also be audio/x-matroska
  594. ,
  595. // MPEG - audio/video - MPEG (Moving Pictures Experts Group)
  596. 'mpeg' => array('pattern' => '^\x00\x00\x01(\xBA|\xB3)',
  597. 'group' => 'audio-video', 'module' => 'mpeg', 'mime_type' => 'video/mpeg'),
  598. // NSV - audio/video - Nullsoft Streaming Video (NSV)
  599. 'nsv' => array('pattern' => '^NSV[sf]', 'group' => 'audio-video',
  600. 'module' => 'nsv', 'mime_type' => 'application/octet-stream'),
  601. // Ogg - audio/video - Ogg (Ogg-Vorbis, Ogg-FLAC, Speex, Ogg-Theora(*), Ogg-Tarkin(*))
  602. 'ogg' => array('pattern' => '^OggS', 'group' => 'audio', 'module' => 'ogg',
  603. 'mime_type' => 'application/ogg', 'fail_id3' => 'WARNING', 'fail_ape' => 'WARNING'),
  604. // QT - audio/video - Quicktime
  605. 'quicktime' => array(
  606. 'pattern' => '^.{4}(cmov|free|ftyp|mdat|moov|pnot|skip|wide)', 'group' => 'audio-video',
  607. 'module' => 'quicktime', 'mime_type' => 'video/quicktime'),
  608. // RIFF - audio/video - Resource Interchange File Format (RIFF) / WAV / AVI / CD-audio / SDSS = renamed variant used by SmartSound QuickTracks (www.smartsound.com) / FORM = Audio Interchange File Format (AIFF)
  609. 'riff' => array('pattern' => '^(RIFF|SDSS|FORM)', 'group' => 'audio-video',
  610. 'module' => 'riff', 'mime_type' => 'audio/x-wave', 'fail_ape' => 'WARNING'),
  611. // Real - audio/video - RealAudio, RealVideo
  612. 'real' => array('pattern' => '^(\\.RMF|\\.ra)', 'group' => 'audio-video',
  613. 'module' => 'real', 'mime_type' => 'audio/x-realaudio'),
  614. // SWF - audio/video - ShockWave Flash
  615. 'swf' => array('pattern' => '^(F|C)WS', 'group' => 'audio-video',
  616. 'module' => 'swf', 'mime_type' => 'application/x-shockwave-flash'),
  617. // Still-Image formats
  618. // BMP - still image - Bitmap (Windows, OS/2; uncompressed, RLE8, RLE4)
  619. 'bmp' => array('pattern' => '^BM', 'group' => 'graphic', 'module' => 'bmp',
  620. 'mime_type' => 'image/bmp', 'fail_id3' => 'ERROR', 'fail_ape' => 'ERROR'),
  621. // GIF - still image - Graphics Interchange Format
  622. 'gif' => array('pattern' => '^GIF', 'group' => 'graphic', 'module' => 'gif',
  623. 'mime_type' => 'image/gif', 'fail_id3' => 'ERROR', 'fail_ape' => 'ERROR'),
  624. // JPEG - still image - Joint Photographic Experts Group (JPEG)
  625. 'jpg' => array('pattern' => '^\xFF\xD8\xFF', 'group' => 'graphic',
  626. 'module' => 'jpg', 'mime_type' => 'image/jpeg', 'fail_id3' => 'ERROR',
  627. 'fail_ape' => 'ERROR'),
  628. // PCD - still image - Kodak Photo CD
  629. 'pcd' => array('pattern' => '^.{2048}PCD_IPI\x00', 'group' => 'graphic',
  630. 'module' => 'pcd', 'mime_type' => 'image/x-photo-cd', 'fail_id3' => 'ERROR',
  631. 'fail_ape' => 'ERROR'),
  632. // PNG - still image - Portable Network Graphics (PNG)
  633. 'png' => array('pattern' => '^\x89\x50\x4E\x47\x0D\x0A\x1A\x0A',
  634. 'group' => 'graphic', 'module' => 'png', 'mime_type' => 'image/png', 'fail_id3' => 'ERROR',
  635. 'fail_ape' => 'ERROR'),
  636. // SVG - still image - Scalable Vector Graphics (SVG)
  637. 'svg' => array('pattern' => '<!DOCTYPE svg PUBLIC ', 'group' => 'graphic',
  638. 'module' => 'svg', 'mime_type' => 'image/svg+xml', 'fail_id3' => 'ERROR',
  639. 'fail_ape' => 'ERROR'),
  640. // TIFF - still image - Tagged Information File Format (TIFF)
  641. 'tiff' => array('pattern' => '^(II\x2A\x00|MM\x00\x2A)',
  642. 'group' => 'graphic', 'module' => 'tiff', 'mime_type' => 'image/tiff',
  643. 'fail_id3' => 'ERROR', 'fail_ape' => 'ERROR'),
  644. // Data formats
  645. // ISO - data - International Standards Organization (ISO) CD-ROM Image
  646. 'iso' => array('pattern' => '^.{32769}CD001', 'group' => 'misc',
  647. 'module' => 'iso', 'mime_type' => 'application/octet-stream', 'fail_id3' => 'ERROR',
  648. 'fail_ape' => 'ERROR', 'iconv_req' => false),
  649. // RAR - data - RAR compressed data
  650. 'rar' => array('pattern' => '^Rar\!', 'group' => 'archive',
  651. 'module' => 'rar', 'mime_type' => 'application/octet-stream', 'fail_id3' => 'ERROR',
  652. 'fail_ape' => 'ERROR'),
  653. // SZIP - audio/data - SZIP compressed data
  654. 'szip' => array('pattern' => '^SZ\x0A\x04', 'group' => 'archive',
  655. 'module' => 'szip', 'mime_type' => 'application/octet-stream', 'fail_id3' => 'ERROR',
  656. 'fail_ape' => 'ERROR'),
  657. // TAR - data - TAR compressed data
  658. 'tar' => array(
  659. 'pattern' => '^.{100}[0-9\x20]{7}\x00[0-9\x20]{7}\x00[0-9\x20]{7}\x00[0-9\x20\x00]{12}[0-9\x20\x00]{12}',
  660. 'group' => 'archive', 'module' => 'tar', 'mime_type' => 'application/x-tar',
  661. 'fail_id3' => 'ERROR', 'fail_ape' => 'ERROR'),
  662. // GZIP - data - GZIP compressed data
  663. 'gz' => array('pattern' => '^\x1F\x8B\x08', 'group' => 'archive',
  664. 'module' => 'gzip', 'mime_type' => 'application/x-gzip', 'fail_id3' => 'ERROR',
  665. 'fail_ape' => 'ERROR'),
  666. // ZIP - data - ZIP compressed data
  667. 'zip' => array('pattern' => '^PK\x03\x04', 'group' => 'archive',
  668. 'module' => 'zip', 'mime_type' => 'application/zip', 'fail_id3' => 'ERROR',
  669. 'fail_ape' => 'ERROR'),
  670. // Misc other formats
  671. // PAR2 - data - Parity Volume Set Specification 2.0
  672. 'par2' => array('pattern' => '^PAR2\x00PKT', 'group' => 'misc',
  673. 'module' => 'par2', 'mime_type' => 'application/octet-stream', 'fail_id3' => 'ERROR',
  674. 'fail_ape' => 'ERROR'),
  675. // PDF - data - Portable Document Format
  676. 'pdf' => array('pattern' => '^\x25PDF', 'group' => 'misc', 'module' => 'pdf',
  677. 'mime_type' => 'application/pdf', 'fail_id3' => 'ERROR', 'fail_ape' => 'ERROR'),
  678. // MSOFFICE - data - ZIP compressed data
  679. 'msoffice' => array('pattern' => '^\xD0\xCF\x11\xE0', // D0CF11E == DOCFILE == Microsoft Office Document
  680. 'group' => 'misc',
  681. 'module' => 'msoffice', 'mime_type' => 'application/octet-stream', 'fail_id3' => 'ERROR',
  682. 'fail_ape' => 'ERROR'));
  683. }
  684. return $format_info;
  685. }
  686. function GetFileFormat(&$filedata, $filename = '')
  687. {
  688. // this function will determine the format of a file based on usually
  689. // the first 2-4 bytes of the file (8 bytes for PNG, 16 bytes for JPG,
  690. // and in the case of ISO CD image, 6 bytes offset 32kb from the start
  691. // of the file).
  692. // Identify file format - loop through $format_info and detect with reg expr
  693. foreach ($this->GetFileFormatArray() as $format_name => $info)
  694. {
  695. // Using preg_match() instead of ereg() - much faster
  696. // The /s switch on preg_match() forces preg_match() NOT to treat
  697. // newline (0x0A) characters as special chars but do a binary match
  698. if (preg_match('/' . $info['pattern'] . '/s', $filedata))
  699. {
  700. $info['include'] = 'module.' . $info['group'] . '.' . $info['module'] . '.php';
  701. return $info;
  702. }
  703. }
  704. if (preg_match('/\.mp[123a]$/i', $filename))
  705. {
  706. // Too many mp3 encoders on the market put gabage in front of mpeg files
  707. // use assume format on these if format detection failed
  708. $GetFileFormatArray = $this->GetFileFormatArray();
  709. $info = $GetFileFormatArray['mp3'];
  710. $info['include'] = 'module.' . $info['group'] . '.' . $info['module'] . '.php';
  711. return $info;
  712. }
  713. return false;
  714. }
  715. // converts array to $encoding charset from $this->encoding
  716. function CharConvert(&$array, $encoding)
  717. {
  718. // identical encoding - end here
  719. if ($encoding == $this->encoding)
  720. {
  721. return;
  722. }
  723. // loop thru array
  724. foreach ($array as $key => $value)
  725. {
  726. // go recursive
  727. if (is_array($value))
  728. {
  729. $this->CharConvert($array[$key], $encoding);
  730. }
  731. // convert string
  732. elseif (is_string($value))
  733. {
  734. $array[$key] = trim(getid3_lib :: iconv_fallback($encoding, $this->encoding, $value));
  735. }
  736. }
  737. }
  738. function HandleAllTags()
  739. {
  740. // key name => array (tag name, character encoding)
  741. static $tags;
  742. if (empty($tags))
  743. {
  744. $tags = array('asf' => array('asf', 'UTF-16LE'), 'midi' => array('midi', 'ISO-8859-1'),
  745. 'nsv' => array('nsv', 'ISO-8859-1'), 'ogg' => array('vorbiscomment', 'UTF-8'),
  746. 'png' => array('png', 'UTF-8'), 'tiff' => array('tiff', 'ISO-8859-1'),
  747. 'quicktime' => array('quicktime', 'ISO-8859-1'),
  748. 'real' => array('real', 'ISO-8859-1'), 'vqf' => array('vqf', 'ISO-8859-1'),
  749. 'zip' => array('zip', 'ISO-8859-1'), 'riff' => array('riff', 'ISO-8859-1'),
  750. 'lyrics3' => array('lyrics3', 'ISO-8859-1'),
  751. 'id3v1' => array('id3v1', $this->encoding_id3v1), 'id3v2' => array('id3v2', 'UTF-8'), // not according to the specs (every frame can have a different encoding), but getID3() force-converts all encodings to UTF-8
  752. 'ape' => array('ape', 'UTF-8'));
  753. }
  754. // loop thru comments array
  755. foreach ($tags as $comment_name => $tagname_encoding_array)
  756. {
  757. list($tag_name, $encoding) = $tagname_encoding_array;
  758. // fill in default encoding type if not already present
  759. if (isset($this->info[$comment_name]) && ! isset($this->info[$comment_name]['encoding']))
  760. {
  761. $this->info[$comment_name]['encoding'] = $encoding;
  762. }
  763. // copy comments if key name set
  764. if (! empty($this->info[$comment_name]['comments']))
  765. {
  766. foreach ($this->info[$comment_name]['comments'] as $tag_key => $valuearray)
  767. {
  768. foreach ($valuearray as $key => $value)
  769. {
  770. if (strlen(trim($value)) > 0)
  771. {
  772. $this->info['tags'][trim($tag_name)][trim($tag_key)][] = $value; // do not trim!! Unicode characters will get mangled if trailing nulls are removed!
  773. }
  774. }
  775. }
  776. if (! isset($this->info['tags'][$tag_name]))
  777. {
  778. // comments are set but contain nothing but empty strings, so skip
  779. continue;
  780. }
  781. if ($this->option_tags_html)
  782. {
  783. foreach ($this->info['tags'][$tag_name] as $tag_key => $valuearray)
  784. {
  785. foreach ($valuearray as $key => $value)
  786. {
  787. if (is_string($value))
  788. {
  789. //$this->info['tags_html'][$tag_name][$tag_key][$key] = getid3_lib::MultiByteCharString2HTML($value, $encoding);
  790. $this->info['tags_html'][$tag_name][$tag_key][$key] = str_replace('&#0;', '', getid3_lib :: MultiByteCharString2HTML($value, $encoding));
  791. }
  792. else
  793. {
  794. $this->info['tags_html'][$tag_name][$tag_key][$key] = $value;
  795. }
  796. }
  797. }
  798. }
  799. $this->CharConvert($this->info['tags'][$tag_name], $encoding); // only copy gets converted!
  800. }
  801. }
  802. return true;
  803. }
  804. function getHashdata($algorithm)
  805. {
  806. switch ($algorithm)
  807. {
  808. case 'md5' :
  809. case 'sha1' :
  810. break;
  811. default :
  812. return $this->error('bad algorithm "' . $algorithm . '" in getHashdata()');
  813. break;
  814. }
  815. if ((@$this->info['fileformat'] == 'ogg') && (@$this->info['audio']['dataformat'] == 'vorbis'))
  816. {
  817. // We cannot get an identical md5_data value for Ogg files where the comments
  818. // span more than 1 Ogg page (compared to the same audio data with smaller
  819. // comments) using the normal getID3() method of MD5'ing the data between the
  820. // end of the comments and the end of the file (minus any trailing tags),
  821. // because the page sequence numbers of the pages that the audio data is on
  822. // do not match. Under normal circumstances, where comments are smaller than
  823. // the nominal 4-8kB page size, then this is not a problem, but if there are
  824. // very large comments, the only way around it is to strip off the comment
  825. // tags with vorbiscomment and MD5 that file.
  826. // This procedure must be applied to ALL Ogg files, not just the ones with
  827. // comments larger than 1 page, because the below method simply MD5's the
  828. // whole file with the comments stripped, not just the portion after the
  829. // comments block (which is the standard getID3() method.
  830. // The above-mentioned problem of comments spanning multiple pages and changing
  831. // page sequence numbers likely happens for OggSpeex and OggFLAC as well, but
  832. // currently vorbiscomment only works on OggVorbis files.
  833. if ((bool) ini_get('safe_mode'))
  834. {
  835. $this->info['warning'][] = 'Failed making system call to vorbiscomment.exe - ' . $algorithm . '_data is incorrect - error returned: PHP running in Safe Mode (backtick operator not available)';
  836. $this->info[$algorithm . '_data'] = false;
  837. }
  838. else
  839. {
  840. // Prevent user from aborting script
  841. $old_abort = ignore_user_abort(true);
  842. // Create empty file
  843. $empty = tempnam('*', 'getID3');
  844. touch($empty);
  845. // Use vorbiscomment to make temp file without comments
  846. $temp = tempnam('*', 'getID3');
  847. $file = $this->info['filenamepath'];
  848. if (GETID3_OS_ISWINDOWS)
  849. {
  850. if (file_exists(GETID3_HELPERAPPSDIR . 'vorbiscomment.exe'))
  851. {
  852. $commandline = '"' . GETID3_HELPERAPPSDIR . 'vorbiscomment.exe" -w -c "' . $empty . '" "' . $file . '" "' . $temp . '"';
  853. $VorbisCommentError = `$commandline`;
  854. }
  855. else
  856. {
  857. $VorbisCommentError = 'vorbiscomment.exe not found in ' . GETID3_HELPERAPPSDIR;
  858. }
  859. }
  860. else
  861. {
  862. $commandline = 'vorbiscomment -w -c "' . $empty . '" "' . $file . '" "' . $temp . '" 2>&1';
  863. $commandline = 'vorbiscomment -w -c ' . escapeshellarg($empty) . ' ' . escapeshellarg($file) . ' ' . escapeshellarg($temp) . ' 2>&1';
  864. $

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