PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/ID3/getid3.php

https://bitbucket.org/acipriani/madeinapulia.com
PHP | 1796 lines | 1275 code | 270 blank | 251 comment | 241 complexity | 22f0a40a51c7512e0bc5c1f962b26fe2 MD5 | raw file
Possible License(s): GPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-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. // also https://github.com/JamesHeinrich/getID3 //
  7. /////////////////////////////////////////////////////////////////
  8. // //
  9. // Please see readme.txt for more information //
  10. // ///
  11. /////////////////////////////////////////////////////////////////
  12. // define a constant rather than looking up every time it is needed
  13. if (!defined('GETID3_OS_ISWINDOWS')) {
  14. define('GETID3_OS_ISWINDOWS', (stripos(PHP_OS, 'WIN') === 0));
  15. }
  16. // Get base path of getID3() - ONCE
  17. if (!defined('GETID3_INCLUDEPATH')) {
  18. define('GETID3_INCLUDEPATH', dirname(__FILE__).DIRECTORY_SEPARATOR);
  19. }
  20. // Workaround Bug #39923 (https://bugs.php.net/bug.php?id=39923)
  21. if (!defined('IMG_JPG') && defined('IMAGETYPE_JPEG')) {
  22. define('IMG_JPG', IMAGETYPE_JPEG);
  23. }
  24. // attempt to define temp dir as something flexible but reliable
  25. $temp_dir = ini_get('upload_tmp_dir');
  26. if ($temp_dir && (!is_dir($temp_dir) || !is_readable($temp_dir))) {
  27. $temp_dir = '';
  28. }
  29. if (!$temp_dir) {
  30. // sys_get_temp_dir() may give inaccessible temp dir, e.g. with open_basedir on virtual hosts
  31. $temp_dir = sys_get_temp_dir();
  32. }
  33. $temp_dir = @realpath($temp_dir); // see https://github.com/JamesHeinrich/getID3/pull/10
  34. $open_basedir = ini_get('open_basedir');
  35. if ($open_basedir) {
  36. // e.g. "/var/www/vhosts/getid3.org/httpdocs/:/tmp/"
  37. $temp_dir = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $temp_dir);
  38. $open_basedir = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $open_basedir);
  39. if (substr($temp_dir, -1, 1) != DIRECTORY_SEPARATOR) {
  40. $temp_dir .= DIRECTORY_SEPARATOR;
  41. }
  42. $found_valid_tempdir = false;
  43. $open_basedirs = explode(PATH_SEPARATOR, $open_basedir);
  44. foreach ($open_basedirs as $basedir) {
  45. if (substr($basedir, -1, 1) != DIRECTORY_SEPARATOR) {
  46. $basedir .= DIRECTORY_SEPARATOR;
  47. }
  48. if (preg_match('#^'.preg_quote($basedir).'#', $temp_dir)) {
  49. $found_valid_tempdir = true;
  50. break;
  51. }
  52. }
  53. if (!$found_valid_tempdir) {
  54. $temp_dir = '';
  55. }
  56. unset($open_basedirs, $found_valid_tempdir, $basedir);
  57. }
  58. if (!$temp_dir) {
  59. $temp_dir = '*'; // invalid directory name should force tempnam() to use system default temp dir
  60. }
  61. // $temp_dir = '/something/else/'; // feel free to override temp dir here if it works better for your system
  62. if (!defined('GETID3_TEMP_DIR')) {
  63. define('GETID3_TEMP_DIR', $temp_dir);
  64. }
  65. unset($open_basedir, $temp_dir);
  66. // End: Defines
  67. class getID3
  68. {
  69. // public: Settings
  70. public $encoding = 'UTF-8'; // CASE SENSITIVE! - i.e. (must be supported by iconv()). Examples: ISO-8859-1 UTF-8 UTF-16 UTF-16BE
  71. public $encoding_id3v1 = 'ISO-8859-1'; // Should always be 'ISO-8859-1', but some tags may be written in other encodings such as 'EUC-CN' or 'CP1252'
  72. // public: Optional tag checks - disable for speed.
  73. public $option_tag_id3v1 = true; // Read and process ID3v1 tags
  74. public $option_tag_id3v2 = true; // Read and process ID3v2 tags
  75. public $option_tag_lyrics3 = true; // Read and process Lyrics3 tags
  76. public $option_tag_apetag = true; // Read and process APE tags
  77. public $option_tags_process = true; // Copy tags to root key 'tags' and encode to $this->encoding
  78. public $option_tags_html = true; // Copy tags to root key 'tags_html' properly translated from various encodings to HTML entities
  79. // public: Optional tag/comment calucations
  80. public $option_extra_info = true; // Calculate additional info such as bitrate, channelmode etc
  81. // public: Optional handling of embedded attachments (e.g. images)
  82. public $option_save_attachments = true; // defaults to true (ATTACHMENTS_INLINE) for backward compatibility
  83. // public: Optional calculations
  84. public $option_md5_data = false; // Get MD5 sum of data part - slow
  85. public $option_md5_data_source = false; // Use MD5 of source file if availble - only FLAC and OptimFROG
  86. public $option_sha1_data = false; // Get SHA1 sum of data part - slow
  87. public $option_max_2gb_check = null; // Check whether file is larger than 2GB and thus not supported by 32-bit PHP (null: auto-detect based on PHP_INT_MAX)
  88. // public: Read buffer size in bytes
  89. public $option_fread_buffer_size = 32768;
  90. // Public variables
  91. public $filename; // Filename of file being analysed.
  92. public $fp; // Filepointer to file being analysed.
  93. public $info; // Result array.
  94. public $tempdir = GETID3_TEMP_DIR;
  95. public $memory_limit = 0;
  96. // Protected variables
  97. protected $startup_error = '';
  98. protected $startup_warning = '';
  99. const VERSION = '1.9.8-20140511';
  100. const FREAD_BUFFER_SIZE = 32768;
  101. const ATTACHMENTS_NONE = false;
  102. const ATTACHMENTS_INLINE = true;
  103. // public: constructor
  104. public function __construct() {
  105. // Check memory
  106. $this->memory_limit = ini_get('memory_limit');
  107. if (preg_match('#([0-9]+)M#i', $this->memory_limit, $matches)) {
  108. // could be stored as "16M" rather than 16777216 for example
  109. $this->memory_limit = $matches[1] * 1048576;
  110. } elseif (preg_match('#([0-9]+)G#i', $this->memory_limit, $matches)) { // The 'G' modifier is available since PHP 5.1.0
  111. // could be stored as "2G" rather than 2147483648 for example
  112. $this->memory_limit = $matches[1] * 1073741824;
  113. }
  114. if ($this->memory_limit <= 0) {
  115. // memory limits probably disabled
  116. } elseif ($this->memory_limit <= 4194304) {
  117. $this->startup_error .= 'PHP has less than 4MB available memory and will very likely run out. Increase memory_limit in php.ini';
  118. } elseif ($this->memory_limit <= 12582912) {
  119. $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';
  120. }
  121. // Check safe_mode off
  122. if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
  123. $this->warning('WARNING: Safe mode is on, shorten support disabled, md5data/sha1data for ogg vorbis disabled, ogg vorbos/flac tag writing disabled.');
  124. }
  125. if (intval(ini_get('mbstring.func_overload')) > 0) {
  126. $this->warning('WARNING: php.ini contains "mbstring.func_overload = '.ini_get('mbstring.func_overload').'", this may break things.');
  127. }
  128. // Check for magic_quotes_runtime
  129. if (function_exists('get_magic_quotes_runtime')) {
  130. if (get_magic_quotes_runtime()) {
  131. return $this->startup_error('magic_quotes_runtime must be disabled before running getID3(). Surround getid3 block by set_magic_quotes_runtime(0) and set_magic_quotes_runtime(1).');
  132. }
  133. }
  134. // Check for magic_quotes_gpc
  135. if (function_exists('magic_quotes_gpc')) {
  136. if (get_magic_quotes_gpc()) {
  137. return $this->startup_error('magic_quotes_gpc must be disabled before running getID3(). Surround getid3 block by set_magic_quotes_gpc(0) and set_magic_quotes_gpc(1).');
  138. }
  139. }
  140. // Load support library
  141. if (!include_once(GETID3_INCLUDEPATH.'getid3.lib.php')) {
  142. $this->startup_error .= 'getid3.lib.php is missing or corrupt';
  143. }
  144. if ($this->option_max_2gb_check === null) {
  145. $this->option_max_2gb_check = (PHP_INT_MAX <= 2147483647);
  146. }
  147. // Needed for Windows only:
  148. // Define locations of helper applications for Shorten, VorbisComment, MetaFLAC
  149. // as well as other helper functions such as head, tail, md5sum, etc
  150. // This path cannot contain spaces, but the below code will attempt to get the
  151. // 8.3-equivalent path automatically
  152. // IMPORTANT: This path must include the trailing slash
  153. if (GETID3_OS_ISWINDOWS && !defined('GETID3_HELPERAPPSDIR')) {
  154. $helperappsdir = GETID3_INCLUDEPATH.'..'.DIRECTORY_SEPARATOR.'helperapps'; // must not have any space in this path
  155. if (!is_dir($helperappsdir)) {
  156. $this->startup_warning .= '"'.$helperappsdir.'" cannot be defined as GETID3_HELPERAPPSDIR because it does not exist';
  157. } elseif (strpos(realpath($helperappsdir), ' ') !== false) {
  158. $DirPieces = explode(DIRECTORY_SEPARATOR, realpath($helperappsdir));
  159. $path_so_far = array();
  160. foreach ($DirPieces as $key => $value) {
  161. if (strpos($value, ' ') !== false) {
  162. if (!empty($path_so_far)) {
  163. $commandline = 'dir /x '.escapeshellarg(implode(DIRECTORY_SEPARATOR, $path_so_far));
  164. $dir_listing = `$commandline`;
  165. $lines = explode("\n", $dir_listing);
  166. foreach ($lines as $line) {
  167. $line = trim($line);
  168. if (preg_match('#^([0-9/]{10}) +([0-9:]{4,5}( [AP]M)?) +(<DIR>|[0-9,]+) +([^ ]{0,11}) +(.+)$#', $line, $matches)) {
  169. list($dummy, $date, $time, $ampm, $filesize, $shortname, $filename) = $matches;
  170. if ((strtoupper($filesize) == '<DIR>') && (strtolower($filename) == strtolower($value))) {
  171. $value = $shortname;
  172. }
  173. }
  174. }
  175. } else {
  176. $this->startup_warning .= 'GETID3_HELPERAPPSDIR must not have any spaces in it - use 8dot3 naming convention if neccesary. You can run "dir /x" from the commandline to see the correct 8.3-style names.';
  177. }
  178. }
  179. $path_so_far[] = $value;
  180. }
  181. $helperappsdir = implode(DIRECTORY_SEPARATOR, $path_so_far);
  182. }
  183. define('GETID3_HELPERAPPSDIR', $helperappsdir.DIRECTORY_SEPARATOR);
  184. }
  185. return true;
  186. }
  187. public function version() {
  188. return self::VERSION;
  189. }
  190. public function fread_buffer_size() {
  191. return $this->option_fread_buffer_size;
  192. }
  193. // public: setOption
  194. public function setOption($optArray) {
  195. if (!is_array($optArray) || empty($optArray)) {
  196. return false;
  197. }
  198. foreach ($optArray as $opt => $val) {
  199. if (isset($this->$opt) === false) {
  200. continue;
  201. }
  202. $this->$opt = $val;
  203. }
  204. return true;
  205. }
  206. public function openfile($filename) {
  207. try {
  208. if (!empty($this->startup_error)) {
  209. throw new getid3_exception($this->startup_error);
  210. }
  211. if (!empty($this->startup_warning)) {
  212. $this->warning($this->startup_warning);
  213. }
  214. // init result array and set parameters
  215. $this->filename = $filename;
  216. $this->info = array();
  217. $this->info['GETID3_VERSION'] = $this->version();
  218. $this->info['php_memory_limit'] = $this->memory_limit;
  219. // remote files not supported
  220. if (preg_match('/^(ht|f)tp:\/\//', $filename)) {
  221. throw new getid3_exception('Remote files are not supported - please copy the file locally first');
  222. }
  223. $filename = str_replace('/', DIRECTORY_SEPARATOR, $filename);
  224. $filename = preg_replace('#(.+)'.preg_quote(DIRECTORY_SEPARATOR).'{2,}#U', '\1'.DIRECTORY_SEPARATOR, $filename);
  225. // open local file
  226. //if (is_readable($filename) && is_file($filename) && ($this->fp = fopen($filename, 'rb'))) { // see http://www.getid3.org/phpBB3/viewtopic.php?t=1720
  227. if ((is_readable($filename) || file_exists($filename)) && is_file($filename) && ($this->fp = fopen($filename, 'rb'))) {
  228. // great
  229. } else {
  230. $errormessagelist = array();
  231. if (!is_readable($filename)) {
  232. $errormessagelist[] = '!is_readable';
  233. }
  234. if (!is_file($filename)) {
  235. $errormessagelist[] = '!is_file';
  236. }
  237. if (!file_exists($filename)) {
  238. $errormessagelist[] = '!file_exists';
  239. }
  240. if (empty($errormessagelist)) {
  241. $errormessagelist[] = 'fopen failed';
  242. }
  243. throw new getid3_exception('Could not open "'.$filename.'" ('.implode('; ', $errormessagelist).')');
  244. }
  245. $this->info['filesize'] = filesize($filename);
  246. // set redundant parameters - might be needed in some include file
  247. // filenames / filepaths in getID3 are always expressed with forward slashes (unix-style) for both Windows and other to try and minimize confusion
  248. $filename = str_replace('\\', '/', $filename);
  249. $this->info['filepath'] = str_replace('\\', '/', realpath(dirname($filename)));
  250. $this->info['filename'] = getid3_lib::mb_basename($filename);
  251. $this->info['filenamepath'] = $this->info['filepath'].'/'.$this->info['filename'];
  252. // option_max_2gb_check
  253. if ($this->option_max_2gb_check) {
  254. // PHP (32-bit all, and 64-bit Windows) doesn't support integers larger than 2^31 (~2GB)
  255. // filesize() simply returns (filesize % (pow(2, 32)), no matter the actual filesize
  256. // ftell() returns 0 if seeking to the end is beyond the range of unsigned integer
  257. $fseek = fseek($this->fp, 0, SEEK_END);
  258. if (($fseek < 0) || (($this->info['filesize'] != 0) && (ftell($this->fp) == 0)) ||
  259. ($this->info['filesize'] < 0) ||
  260. (ftell($this->fp) < 0)) {
  261. $real_filesize = getid3_lib::getFileSizeSyscall($this->info['filenamepath']);
  262. if ($real_filesize === false) {
  263. unset($this->info['filesize']);
  264. fclose($this->fp);
  265. throw new getid3_exception('Unable to determine actual filesize. File is most likely larger than '.round(PHP_INT_MAX / 1073741824).'GB and is not supported by PHP.');
  266. } elseif (getid3_lib::intValueSupported($real_filesize)) {
  267. unset($this->info['filesize']);
  268. fclose($this->fp);
  269. throw new getid3_exception('PHP seems to think the file is larger than '.round(PHP_INT_MAX / 1073741824).'GB, but filesystem reports it as '.number_format($real_filesize, 3).'GB, please report to info@getid3.org');
  270. }
  271. $this->info['filesize'] = $real_filesize;
  272. $this->warning('File is larger than '.round(PHP_INT_MAX / 1073741824).'GB (filesystem reports it as '.number_format($real_filesize, 3).'GB) and is not properly supported by PHP.');
  273. }
  274. }
  275. // set more parameters
  276. $this->info['avdataoffset'] = 0;
  277. $this->info['avdataend'] = $this->info['filesize'];
  278. $this->info['fileformat'] = ''; // filled in later
  279. $this->info['audio']['dataformat'] = ''; // filled in later, unset if not used
  280. $this->info['video']['dataformat'] = ''; // filled in later, unset if not used
  281. $this->info['tags'] = array(); // filled in later, unset if not used
  282. $this->info['error'] = array(); // filled in later, unset if not used
  283. $this->info['warning'] = array(); // filled in later, unset if not used
  284. $this->info['comments'] = array(); // filled in later, unset if not used
  285. $this->info['encoding'] = $this->encoding; // required by id3v2 and iso modules - can be unset at the end if desired
  286. return true;
  287. } catch (Exception $e) {
  288. $this->error($e->getMessage());
  289. }
  290. return false;
  291. }
  292. // public: analyze file
  293. public function analyze($filename) {
  294. try {
  295. if (!$this->openfile($filename)) {
  296. return $this->info;
  297. }
  298. // Handle tags
  299. foreach (array('id3v2'=>'id3v2', 'id3v1'=>'id3v1', 'apetag'=>'ape', 'lyrics3'=>'lyrics3') as $tag_name => $tag_key) {
  300. $option_tag = 'option_tag_'.$tag_name;
  301. if ($this->$option_tag) {
  302. $this->include_module('tag.'.$tag_name);
  303. try {
  304. $tag_class = 'getid3_'.$tag_name;
  305. $tag = new $tag_class($this);
  306. $tag->Analyze();
  307. }
  308. catch (getid3_exception $e) {
  309. throw $e;
  310. }
  311. }
  312. }
  313. if (isset($this->info['id3v2']['tag_offset_start'])) {
  314. $this->info['avdataoffset'] = max($this->info['avdataoffset'], $this->info['id3v2']['tag_offset_end']);
  315. }
  316. foreach (array('id3v1'=>'id3v1', 'apetag'=>'ape', 'lyrics3'=>'lyrics3') as $tag_name => $tag_key) {
  317. if (isset($this->info[$tag_key]['tag_offset_start'])) {
  318. $this->info['avdataend'] = min($this->info['avdataend'], $this->info[$tag_key]['tag_offset_start']);
  319. }
  320. }
  321. // ID3v2 detection (NOT parsing), even if ($this->option_tag_id3v2 == false) done to make fileformat easier
  322. if (!$this->option_tag_id3v2) {
  323. fseek($this->fp, 0);
  324. $header = fread($this->fp, 10);
  325. if ((substr($header, 0, 3) == 'ID3') && (strlen($header) == 10)) {
  326. $this->info['id3v2']['header'] = true;
  327. $this->info['id3v2']['majorversion'] = ord($header{3});
  328. $this->info['id3v2']['minorversion'] = ord($header{4});
  329. $this->info['avdataoffset'] += getid3_lib::BigEndian2Int(substr($header, 6, 4), 1) + 10; // length of ID3v2 tag in 10-byte header doesn't include 10-byte header length
  330. }
  331. }
  332. // read 32 kb file data
  333. fseek($this->fp, $this->info['avdataoffset']);
  334. $formattest = fread($this->fp, 32774);
  335. // determine format
  336. $determined_format = $this->GetFileFormat($formattest, $filename);
  337. // unable to determine file format
  338. if (!$determined_format) {
  339. fclose($this->fp);
  340. return $this->error('unable to determine file format');
  341. }
  342. // check for illegal ID3 tags
  343. if (isset($determined_format['fail_id3']) && (in_array('id3v1', $this->info['tags']) || in_array('id3v2', $this->info['tags']))) {
  344. if ($determined_format['fail_id3'] === 'ERROR') {
  345. fclose($this->fp);
  346. return $this->error('ID3 tags not allowed on this file type.');
  347. } elseif ($determined_format['fail_id3'] === 'WARNING') {
  348. $this->warning('ID3 tags not allowed on this file type.');
  349. }
  350. }
  351. // check for illegal APE tags
  352. if (isset($determined_format['fail_ape']) && in_array('ape', $this->info['tags'])) {
  353. if ($determined_format['fail_ape'] === 'ERROR') {
  354. fclose($this->fp);
  355. return $this->error('APE tags not allowed on this file type.');
  356. } elseif ($determined_format['fail_ape'] === 'WARNING') {
  357. $this->warning('APE tags not allowed on this file type.');
  358. }
  359. }
  360. // set mime type
  361. $this->info['mime_type'] = $determined_format['mime_type'];
  362. // supported format signature pattern detected, but module deleted
  363. if (!file_exists(GETID3_INCLUDEPATH.$determined_format['include'])) {
  364. fclose($this->fp);
  365. return $this->error('Format not supported, module "'.$determined_format['include'].'" was removed.');
  366. }
  367. // module requires iconv support
  368. // Check encoding/iconv support
  369. if (!empty($determined_format['iconv_req']) && !function_exists('iconv') && !in_array($this->encoding, array('ISO-8859-1', 'UTF-8', 'UTF-16LE', 'UTF-16BE', 'UTF-16'))) {
  370. $errormessage = 'iconv() support is required for this module ('.$determined_format['include'].') for encodings other than ISO-8859-1, UTF-8, UTF-16LE, UTF16-BE, UTF-16. ';
  371. if (GETID3_OS_ISWINDOWS) {
  372. $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';
  373. } else {
  374. $errormessage .= 'PHP is not compiled with iconv() support. Please recompile with the --with-iconv switch';
  375. }
  376. return $this->error($errormessage);
  377. }
  378. // include module
  379. include_once(GETID3_INCLUDEPATH.$determined_format['include']);
  380. // instantiate module class
  381. $class_name = 'getid3_'.$determined_format['module'];
  382. if (!class_exists($class_name)) {
  383. return $this->error('Format not supported, module "'.$determined_format['include'].'" is corrupt.');
  384. }
  385. $class = new $class_name($this);
  386. $class->Analyze();
  387. unset($class);
  388. // close file
  389. fclose($this->fp);
  390. // process all tags - copy to 'tags' and convert charsets
  391. if ($this->option_tags_process) {
  392. $this->HandleAllTags();
  393. }
  394. // perform more calculations
  395. if ($this->option_extra_info) {
  396. $this->ChannelsBitratePlaytimeCalculations();
  397. $this->CalculateCompressionRatioVideo();
  398. $this->CalculateCompressionRatioAudio();
  399. $this->CalculateReplayGain();
  400. $this->ProcessAudioStreams();
  401. }
  402. // get the MD5 sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags
  403. if ($this->option_md5_data) {
  404. // do not calc md5_data if md5_data_source is present - set by flac only - future MPC/SV8 too
  405. if (!$this->option_md5_data_source || empty($this->info['md5_data_source'])) {
  406. $this->getHashdata('md5');
  407. }
  408. }
  409. // get the SHA1 sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags
  410. if ($this->option_sha1_data) {
  411. $this->getHashdata('sha1');
  412. }
  413. // remove undesired keys
  414. $this->CleanUp();
  415. } catch (Exception $e) {
  416. $this->error('Caught exception: '.$e->getMessage());
  417. }
  418. // return info array
  419. return $this->info;
  420. }
  421. // private: error handling
  422. public function error($message) {
  423. $this->CleanUp();
  424. if (!isset($this->info['error'])) {
  425. $this->info['error'] = array();
  426. }
  427. $this->info['error'][] = $message;
  428. return $this->info;
  429. }
  430. // private: warning handling
  431. public function warning($message) {
  432. $this->info['warning'][] = $message;
  433. return true;
  434. }
  435. // private: CleanUp
  436. private function CleanUp() {
  437. // remove possible empty keys
  438. $AVpossibleEmptyKeys = array('dataformat', 'bits_per_sample', 'encoder_options', 'streams', 'bitrate');
  439. foreach ($AVpossibleEmptyKeys as $dummy => $key) {
  440. if (empty($this->info['audio'][$key]) && isset($this->info['audio'][$key])) {
  441. unset($this->info['audio'][$key]);
  442. }
  443. if (empty($this->info['video'][$key]) && isset($this->info['video'][$key])) {
  444. unset($this->info['video'][$key]);
  445. }
  446. }
  447. // remove empty root keys
  448. if (!empty($this->info)) {
  449. foreach ($this->info as $key => $value) {
  450. if (empty($this->info[$key]) && ($this->info[$key] !== 0) && ($this->info[$key] !== '0')) {
  451. unset($this->info[$key]);
  452. }
  453. }
  454. }
  455. // remove meaningless entries from unknown-format files
  456. if (empty($this->info['fileformat'])) {
  457. if (isset($this->info['avdataoffset'])) {
  458. unset($this->info['avdataoffset']);
  459. }
  460. if (isset($this->info['avdataend'])) {
  461. unset($this->info['avdataend']);
  462. }
  463. }
  464. // remove possible duplicated identical entries
  465. if (!empty($this->info['error'])) {
  466. $this->info['error'] = array_values(array_unique($this->info['error']));
  467. }
  468. if (!empty($this->info['warning'])) {
  469. $this->info['warning'] = array_values(array_unique($this->info['warning']));
  470. }
  471. // remove "global variable" type keys
  472. unset($this->info['php_memory_limit']);
  473. return true;
  474. }
  475. // return array containing information about all supported formats
  476. public function GetFileFormatArray() {
  477. static $format_info = array();
  478. if (empty($format_info)) {
  479. $format_info = array(
  480. // Audio formats
  481. // AC-3 - audio - Dolby AC-3 / Dolby Digital
  482. 'ac3' => array(
  483. 'pattern' => '^\x0B\x77',
  484. 'group' => 'audio',
  485. 'module' => 'ac3',
  486. 'mime_type' => 'audio/ac3',
  487. ),
  488. // AAC - audio - Advanced Audio Coding (AAC) - ADIF format
  489. 'adif' => array(
  490. 'pattern' => '^ADIF',
  491. 'group' => 'audio',
  492. 'module' => 'aac',
  493. 'mime_type' => 'application/octet-stream',
  494. 'fail_ape' => 'WARNING',
  495. ),
  496. /*
  497. // AA - audio - Audible Audiobook
  498. 'aa' => array(
  499. 'pattern' => '^.{4}\x57\x90\x75\x36',
  500. 'group' => 'audio',
  501. 'module' => 'aa',
  502. 'mime_type' => 'audio/audible',
  503. ),
  504. */
  505. // AAC - audio - Advanced Audio Coding (AAC) - ADTS format (very similar to MP3)
  506. 'adts' => array(
  507. 'pattern' => '^\xFF[\xF0-\xF1\xF8-\xF9]',
  508. 'group' => 'audio',
  509. 'module' => 'aac',
  510. 'mime_type' => 'application/octet-stream',
  511. 'fail_ape' => 'WARNING',
  512. ),
  513. // AU - audio - NeXT/Sun AUdio (AU)
  514. 'au' => array(
  515. 'pattern' => '^\.snd',
  516. 'group' => 'audio',
  517. 'module' => 'au',
  518. 'mime_type' => 'audio/basic',
  519. ),
  520. // AMR - audio - Adaptive Multi Rate
  521. 'amr' => array(
  522. 'pattern' => '^\x23\x21AMR\x0A', // #!AMR[0A]
  523. 'group' => 'audio',
  524. 'module' => 'amr',
  525. 'mime_type' => 'audio/amr',
  526. ),
  527. // AVR - audio - Audio Visual Research
  528. 'avr' => array(
  529. 'pattern' => '^2BIT',
  530. 'group' => 'audio',
  531. 'module' => 'avr',
  532. 'mime_type' => 'application/octet-stream',
  533. ),
  534. // BONK - audio - Bonk v0.9+
  535. 'bonk' => array(
  536. 'pattern' => '^\x00(BONK|INFO|META| ID3)',
  537. 'group' => 'audio',
  538. 'module' => 'bonk',
  539. 'mime_type' => 'audio/xmms-bonk',
  540. ),
  541. // DSS - audio - Digital Speech Standard
  542. 'dss' => array(
  543. 'pattern' => '^[\x02-\x03]ds[s2]',
  544. 'group' => 'audio',
  545. 'module' => 'dss',
  546. 'mime_type' => 'application/octet-stream',
  547. ),
  548. // DTS - audio - Dolby Theatre System
  549. 'dts' => array(
  550. 'pattern' => '^\x7F\xFE\x80\x01',
  551. 'group' => 'audio',
  552. 'module' => 'dts',
  553. 'mime_type' => 'audio/dts',
  554. ),
  555. // FLAC - audio - Free Lossless Audio Codec
  556. 'flac' => array(
  557. 'pattern' => '^fLaC',
  558. 'group' => 'audio',
  559. 'module' => 'flac',
  560. 'mime_type' => 'audio/x-flac',
  561. ),
  562. // LA - audio - Lossless Audio (LA)
  563. 'la' => array(
  564. 'pattern' => '^LA0[2-4]',
  565. 'group' => 'audio',
  566. 'module' => 'la',
  567. 'mime_type' => 'application/octet-stream',
  568. ),
  569. // LPAC - audio - Lossless Predictive Audio Compression (LPAC)
  570. 'lpac' => array(
  571. 'pattern' => '^LPAC',
  572. 'group' => 'audio',
  573. 'module' => 'lpac',
  574. 'mime_type' => 'application/octet-stream',
  575. ),
  576. // MIDI - audio - MIDI (Musical Instrument Digital Interface)
  577. 'midi' => array(
  578. 'pattern' => '^MThd',
  579. 'group' => 'audio',
  580. 'module' => 'midi',
  581. 'mime_type' => 'audio/midi',
  582. ),
  583. // MAC - audio - Monkey's Audio Compressor
  584. 'mac' => array(
  585. 'pattern' => '^MAC ',
  586. 'group' => 'audio',
  587. 'module' => 'monkey',
  588. 'mime_type' => 'application/octet-stream',
  589. ),
  590. // has been known to produce false matches in random files (e.g. JPEGs), leave out until more precise matching available
  591. // // MOD - audio - MODule (assorted sub-formats)
  592. // 'mod' => array(
  593. // 'pattern' => '^.{1080}(M\\.K\\.|M!K!|FLT4|FLT8|[5-9]CHN|[1-3][0-9]CH)',
  594. // 'group' => 'audio',
  595. // 'module' => 'mod',
  596. // 'option' => 'mod',
  597. // 'mime_type' => 'audio/mod',
  598. // ),
  599. // MOD - audio - MODule (Impulse Tracker)
  600. 'it' => array(
  601. 'pattern' => '^IMPM',
  602. 'group' => 'audio',
  603. 'module' => 'mod',
  604. //'option' => 'it',
  605. 'mime_type' => 'audio/it',
  606. ),
  607. // MOD - audio - MODule (eXtended Module, various sub-formats)
  608. 'xm' => array(
  609. 'pattern' => '^Extended Module',
  610. 'group' => 'audio',
  611. 'module' => 'mod',
  612. //'option' => 'xm',
  613. 'mime_type' => 'audio/xm',
  614. ),
  615. // MOD - audio - MODule (ScreamTracker)
  616. 's3m' => array(
  617. 'pattern' => '^.{44}SCRM',
  618. 'group' => 'audio',
  619. 'module' => 'mod',
  620. //'option' => 's3m',
  621. 'mime_type' => 'audio/s3m',
  622. ),
  623. // MPC - audio - Musepack / MPEGplus
  624. 'mpc' => array(
  625. '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])',
  626. 'group' => 'audio',
  627. 'module' => 'mpc',
  628. 'mime_type' => 'audio/x-musepack',
  629. ),
  630. // MP3 - audio - MPEG-audio Layer 3 (very similar to AAC-ADTS)
  631. 'mp3' => array(
  632. 'pattern' => '^\xFF[\xE2-\xE7\xF2-\xF7\xFA-\xFF][\x00-\x0B\x10-\x1B\x20-\x2B\x30-\x3B\x40-\x4B\x50-\x5B\x60-\x6B\x70-\x7B\x80-\x8B\x90-\x9B\xA0-\xAB\xB0-\xBB\xC0-\xCB\xD0-\xDB\xE0-\xEB\xF0-\xFB]',
  633. 'group' => 'audio',
  634. 'module' => 'mp3',
  635. 'mime_type' => 'audio/mpeg',
  636. ),
  637. // OFR - audio - OptimFROG
  638. 'ofr' => array(
  639. 'pattern' => '^(\*RIFF|OFR)',
  640. 'group' => 'audio',
  641. 'module' => 'optimfrog',
  642. 'mime_type' => 'application/octet-stream',
  643. ),
  644. // RKAU - audio - RKive AUdio compressor
  645. 'rkau' => array(
  646. 'pattern' => '^RKA',
  647. 'group' => 'audio',
  648. 'module' => 'rkau',
  649. 'mime_type' => 'application/octet-stream',
  650. ),
  651. // SHN - audio - Shorten
  652. 'shn' => array(
  653. 'pattern' => '^ajkg',
  654. 'group' => 'audio',
  655. 'module' => 'shorten',
  656. 'mime_type' => 'audio/xmms-shn',
  657. 'fail_id3' => 'ERROR',
  658. 'fail_ape' => 'ERROR',
  659. ),
  660. // TTA - audio - TTA Lossless Audio Compressor (http://tta.corecodec.org)
  661. 'tta' => array(
  662. 'pattern' => '^TTA', // could also be '^TTA(\x01|\x02|\x03|2|1)'
  663. 'group' => 'audio',
  664. 'module' => 'tta',
  665. 'mime_type' => 'application/octet-stream',
  666. ),
  667. // VOC - audio - Creative Voice (VOC)
  668. 'voc' => array(
  669. 'pattern' => '^Creative Voice File',
  670. 'group' => 'audio',
  671. 'module' => 'voc',
  672. 'mime_type' => 'audio/voc',
  673. ),
  674. // VQF - audio - transform-domain weighted interleave Vector Quantization Format (VQF)
  675. 'vqf' => array(
  676. 'pattern' => '^TWIN',
  677. 'group' => 'audio',
  678. 'module' => 'vqf',
  679. 'mime_type' => 'application/octet-stream',
  680. ),
  681. // WV - audio - WavPack (v4.0+)
  682. 'wv' => array(
  683. 'pattern' => '^wvpk',
  684. 'group' => 'audio',
  685. 'module' => 'wavpack',
  686. 'mime_type' => 'application/octet-stream',
  687. ),
  688. // Audio-Video formats
  689. // ASF - audio/video - Advanced Streaming Format, Windows Media Video, Windows Media Audio
  690. 'asf' => array(
  691. 'pattern' => '^\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C',
  692. 'group' => 'audio-video',
  693. 'module' => 'asf',
  694. 'mime_type' => 'video/x-ms-asf',
  695. 'iconv_req' => false,
  696. ),
  697. // BINK - audio/video - Bink / Smacker
  698. 'bink' => array(
  699. 'pattern' => '^(BIK|SMK)',
  700. 'group' => 'audio-video',
  701. 'module' => 'bink',
  702. 'mime_type' => 'application/octet-stream',
  703. ),
  704. // FLV - audio/video - FLash Video
  705. 'flv' => array(
  706. 'pattern' => '^FLV\x01',
  707. 'group' => 'audio-video',
  708. 'module' => 'flv',
  709. 'mime_type' => 'video/x-flv',
  710. ),
  711. // MKAV - audio/video - Mastroka
  712. 'matroska' => array(
  713. 'pattern' => '^\x1A\x45\xDF\xA3',
  714. 'group' => 'audio-video',
  715. 'module' => 'matroska',
  716. 'mime_type' => 'video/x-matroska', // may also be audio/x-matroska
  717. ),
  718. // MPEG - audio/video - MPEG (Moving Pictures Experts Group)
  719. 'mpeg' => array(
  720. 'pattern' => '^\x00\x00\x01(\xBA|\xB3)',
  721. 'group' => 'audio-video',
  722. 'module' => 'mpeg',
  723. 'mime_type' => 'video/mpeg',
  724. ),
  725. // NSV - audio/video - Nullsoft Streaming Video (NSV)
  726. 'nsv' => array(
  727. 'pattern' => '^NSV[sf]',
  728. 'group' => 'audio-video',
  729. 'module' => 'nsv',
  730. 'mime_type' => 'application/octet-stream',
  731. ),
  732. // Ogg - audio/video - Ogg (Ogg-Vorbis, Ogg-FLAC, Speex, Ogg-Theora(*), Ogg-Tarkin(*))
  733. 'ogg' => array(
  734. 'pattern' => '^OggS',
  735. 'group' => 'audio',
  736. 'module' => 'ogg',
  737. 'mime_type' => 'application/ogg',
  738. 'fail_id3' => 'WARNING',
  739. 'fail_ape' => 'WARNING',
  740. ),
  741. // QT - audio/video - Quicktime
  742. 'quicktime' => array(
  743. 'pattern' => '^.{4}(cmov|free|ftyp|mdat|moov|pnot|skip|wide)',
  744. 'group' => 'audio-video',
  745. 'module' => 'quicktime',
  746. 'mime_type' => 'video/quicktime',
  747. ),
  748. // 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)
  749. 'riff' => array(
  750. 'pattern' => '^(RIFF|SDSS|FORM)',
  751. 'group' => 'audio-video',
  752. 'module' => 'riff',
  753. 'mime_type' => 'audio/x-wave',
  754. 'fail_ape' => 'WARNING',
  755. ),
  756. // Real - audio/video - RealAudio, RealVideo
  757. 'real' => array(
  758. 'pattern' => '^(\\.RMF|\\.ra)',
  759. 'group' => 'audio-video',
  760. 'module' => 'real',
  761. 'mime_type' => 'audio/x-realaudio',
  762. ),
  763. // SWF - audio/video - ShockWave Flash
  764. 'swf' => array(
  765. 'pattern' => '^(F|C)WS',
  766. 'group' => 'audio-video',
  767. 'module' => 'swf',
  768. 'mime_type' => 'application/x-shockwave-flash',
  769. ),
  770. // TS - audio/video - MPEG-2 Transport Stream
  771. 'ts' => array(
  772. 'pattern' => '^(\x47.{187}){10,}', // packets are 188 bytes long and start with 0x47 "G". Check for at least 10 packets matching this pattern
  773. 'group' => 'audio-video',
  774. 'module' => 'ts',
  775. 'mime_type' => 'video/MP2T',
  776. ),
  777. // Still-Image formats
  778. // BMP - still image - Bitmap (Windows, OS/2; uncompressed, RLE8, RLE4)
  779. 'bmp' => array(
  780. 'pattern' => '^BM',
  781. 'group' => 'graphic',
  782. 'module' => 'bmp',
  783. 'mime_type' => 'image/bmp',
  784. 'fail_id3' => 'ERROR',
  785. 'fail_ape' => 'ERROR',
  786. ),
  787. // GIF - still image - Graphics Interchange Format
  788. 'gif' => array(
  789. 'pattern' => '^GIF',
  790. 'group' => 'graphic',
  791. 'module' => 'gif',
  792. 'mime_type' => 'image/gif',
  793. 'fail_id3' => 'ERROR',
  794. 'fail_ape' => 'ERROR',
  795. ),
  796. // JPEG - still image - Joint Photographic Experts Group (JPEG)
  797. 'jpg' => array(
  798. 'pattern' => '^\xFF\xD8\xFF',
  799. 'group' => 'graphic',
  800. 'module' => 'jpg',
  801. 'mime_type' => 'image/jpeg',
  802. 'fail_id3' => 'ERROR',
  803. 'fail_ape' => 'ERROR',
  804. ),
  805. // PCD - still image - Kodak Photo CD
  806. 'pcd' => array(
  807. 'pattern' => '^.{2048}PCD_IPI\x00',
  808. 'group' => 'graphic',
  809. 'module' => 'pcd',
  810. 'mime_type' => 'image/x-photo-cd',
  811. 'fail_id3' => 'ERROR',
  812. 'fail_ape' => 'ERROR',
  813. ),
  814. // PNG - still image - Portable Network Graphics (PNG)
  815. 'png' => array(
  816. 'pattern' => '^\x89\x50\x4E\x47\x0D\x0A\x1A\x0A',
  817. 'group' => 'graphic',
  818. 'module' => 'png',
  819. 'mime_type' => 'image/png',
  820. 'fail_id3' => 'ERROR',
  821. 'fail_ape' => 'ERROR',
  822. ),
  823. // SVG - still image - Scalable Vector Graphics (SVG)
  824. 'svg' => array(
  825. 'pattern' => '(<!DOCTYPE svg PUBLIC |xmlns="http:\/\/www\.w3\.org\/2000\/svg")',
  826. 'group' => 'graphic',
  827. 'module' => 'svg',
  828. 'mime_type' => 'image/svg+xml',
  829. 'fail_id3' => 'ERROR',
  830. 'fail_ape' => 'ERROR',
  831. ),
  832. // TIFF - still image - Tagged Information File Format (TIFF)
  833. 'tiff' => array(
  834. 'pattern' => '^(II\x2A\x00|MM\x00\x2A)',
  835. 'group' => 'graphic',
  836. 'module' => 'tiff',
  837. 'mime_type' => 'image/tiff',
  838. 'fail_id3' => 'ERROR',
  839. 'fail_ape' => 'ERROR',
  840. ),
  841. // EFAX - still image - eFax (TIFF derivative)
  842. 'efax' => array(
  843. 'pattern' => '^\xDC\xFE',
  844. 'group' => 'graphic',
  845. 'module' => 'efax',
  846. 'mime_type' => 'image/efax',
  847. 'fail_id3' => 'ERROR',
  848. 'fail_ape' => 'ERROR',
  849. ),
  850. // Data formats
  851. // ISO - data - International Standards Organization (ISO) CD-ROM Image
  852. 'iso' => array(
  853. 'pattern' => '^.{32769}CD001',
  854. 'group' => 'misc',
  855. 'module' => 'iso',
  856. 'mime_type' => 'application/octet-stream',
  857. 'fail_id3' => 'ERROR',
  858. 'fail_ape' => 'ERROR',
  859. 'iconv_req' => false,
  860. ),
  861. // RAR - data - RAR compressed data
  862. 'rar' => array(
  863. 'pattern' => '^Rar\!',
  864. 'group' => 'archive',
  865. 'module' => 'rar',
  866. 'mime_type' => 'application/octet-stream',
  867. 'fail_id3' => 'ERROR',
  868. 'fail_ape' => 'ERROR',
  869. ),
  870. // SZIP - audio/data - SZIP compressed data
  871. 'szip' => array(
  872. 'pattern' => '^SZ\x0A\x04',
  873. 'group' => 'archive',
  874. 'module' => 'szip',
  875. 'mime_type' => 'application/octet-stream',
  876. 'fail_id3' => 'ERROR',
  877. 'fail_ape' => 'ERROR',
  878. ),
  879. // TAR - data - TAR compressed data
  880. 'tar' => array(
  881. '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}',
  882. 'group' => 'archive',
  883. 'module' => 'tar',
  884. 'mime_type' => 'application/x-tar',
  885. 'fail_id3' => 'ERROR',
  886. 'fail_ape' => 'ERROR',
  887. ),
  888. // GZIP - data - GZIP compressed data
  889. 'gz' => array(
  890. 'pattern' => '^\x1F\x8B\x08',
  891. 'group' => 'archive',
  892. 'module' => 'gzip',
  893. 'mime_type' => 'application/x-gzip',
  894. 'fail_id3' => 'ERROR',
  895. 'fail_ape' => 'ERROR',
  896. ),
  897. // ZIP - data - ZIP compressed data
  898. 'zip' => array(
  899. 'pattern' => '^PK\x03\x04',
  900. 'group' => 'archive',
  901. 'module' => 'zip',
  902. 'mime_type' => 'application/zip',
  903. 'fail_id3' => 'ERROR',
  904. 'fail_ape' => 'ERROR',
  905. ),
  906. // Misc other formats
  907. // PAR2 - data - Parity Volume Set Specification 2.0
  908. 'par2' => array (
  909. 'pattern' => '^PAR2\x00PKT',
  910. 'group' => 'misc',
  911. 'module' => 'par2',
  912. 'mime_type' => 'application/octet-stream',
  913. 'fail_id3' => 'ERROR',
  914. 'fail_ape' => 'ERROR',
  915. ),
  916. // PDF - data - Portable Document Format
  917. 'pdf' => array(
  918. 'pattern' => '^\x25PDF',
  919. 'group' => 'misc',
  920. 'module' => 'pdf',
  921. 'mime_type' => 'application/pdf',
  922. 'fail_id3' => 'ERROR',
  923. 'fail_ape' => 'ERROR',
  924. ),
  925. // MSOFFICE - data - ZIP compressed data
  926. 'msoffice' => array(
  927. 'pattern' => '^\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1', // D0CF11E == DOCFILE == Microsoft Office Document
  928. 'group' => 'misc',
  929. 'module' => 'msoffice',
  930. 'mime_type' => 'application/octet-stream',
  931. 'fail_id3' => 'ERROR',
  932. 'fail_ape' => 'ERROR',
  933. ),
  934. // CUE - data - CUEsheet (index to single-file disc images)
  935. 'cue' => array(
  936. 'pattern' => '', // empty pattern means cannot be automatically detected, will fall through all other formats and match based on filename and very basic file contents
  937. 'group' => 'misc',
  938. 'module' => 'cue',
  939. 'mime_type' => 'application/octet-stream',
  940. ),
  941. );
  942. }
  943. return $format_info;
  944. }
  945. public function GetFileFormat(&$filedata, $filename='') {
  946. // this function will determine the format of a file based on usually
  947. // the first 2-4 bytes of the file (8 bytes for PNG, 16 bytes for JPG,
  948. // and in the case of ISO CD image, 6 bytes offset 32kb from the start
  949. // of the file).
  950. // Identify file format - loop through $format_info and detect with reg expr
  951. foreach ($this->GetFileFormatArray() as $format_name => $info) {
  952. // The /s switch on preg_match() forces preg_match() NOT to treat
  953. // newline (0x0A) characters as special chars but do a binary match
  954. if (!empty($info['pattern']) && preg_match('#'.$info['pattern'].'#s', $filedata)) {
  955. $info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php';
  956. return $info;
  957. }
  958. }
  959. if (preg_match('#\.mp[123a]$#i', $filename)) {
  960. // Too many mp3 encoders on the market put gabage in front of mpeg files
  961. // use assume format on these if format detection failed
  962. $GetFileFormatArray = $this->GetFileFormatArray();
  963. $info = $GetFileFormatArray['mp3'];
  964. $info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php';
  965. return $info;
  966. } elseif (preg_match('/\.cue$/i', $filename) && preg_match('#FILE "[^"]+" (BINARY|MOTOROLA|AIFF|WAVE|MP3)#', $filedata)) {
  967. // there's not really a useful consistent "magic" at the beginning of .cue files to identify them
  968. // so until I think of something better, just go by filename if all other format checks fail
  969. // and verify there's at least one instance of "TRACK xx AUDIO" in the file
  970. $GetFileFormatArray = $this->GetFileFormatArray();
  971. $info = $GetFileFormatArray['cue'];
  972. $info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php';
  973. return $info;
  974. }
  975. return false;
  976. }
  977. // converts array to $encoding charset from $this->encoding
  978. public function CharConvert(&$array, $encoding) {
  979. // identical encoding - end here
  980. if ($encoding == $this->encoding) {
  981. return;
  982. }
  983. // loop thru array
  984. foreach ($array as $key => $value) {
  985. // go recursive
  986. if (is_array($value)) {
  987. $this->CharConvert($array[$key], $encoding);
  988. }
  989. // convert string
  990. elseif (is_string($value)) {
  991. $array[$key] = trim(getid3_lib::iconv_fallback($encoding, $this->encoding, $value));
  992. }
  993. }
  994. }
  995. public function HandleAllTags() {
  996. // key name => array (tag name, character encoding)
  997. static $tags;
  998. if (empty($tags)) {
  999. $tags = array(
  1000. 'asf' => array('asf' , 'UTF-16LE'),
  1001. 'midi' => array('midi' , 'ISO-8859-1'),
  1002. 'nsv' => array('nsv' , 'ISO-8859-1'),
  1003. 'ogg' => array('vorbiscomment' , 'UTF-8'),
  1004. 'png' => array('png' , 'UTF-8'),
  1005. 'tiff' => array('tiff' , 'ISO-8859-1'),
  1006. 'quicktime' => array('quicktime' , 'UTF-8'),
  1007. 'real' => array('real' , 'ISO-8859-1'),
  1008. 'vqf' => array('vqf' , 'ISO-8859-1'),
  1009. 'zip' => array('zip' , 'ISO-8859-1'),
  1010. 'riff' => array('riff' , 'ISO-8859-1'),
  1011. 'lyrics3' => array('lyrics3' , 'ISO-8859-1'),
  1012. 'id3v1' => array('id3v1' , $this->encoding_id3v1),
  1013. '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
  1014. 'ape' => array('ape' , 'UTF-8'),
  1015. 'cue' => array('cue' , 'ISO-8859-1'),
  1016. 'matroska' => array('matroska' , 'UTF-8'),
  1017. 'flac' => array('vorbiscomment' , 'UTF-8'),
  1018. 'divxtag' => array('divx' , 'ISO-8859-1'),
  1019. 'iptc' => array('iptc' , 'ISO-8859-1'),
  1020. );
  1021. }
  1022. // loop through comments array
  1023. foreach ($tags as $comment_name => $tagname_encoding_array) {
  1024. list($tag_name, $encoding) = $tagname_encoding_array;
  1025. // fill in default encoding type if not already present
  1026. if (isset($this->info[$comment_name]) && !isset($this->info[$comment_name]['encoding'])) {
  1027. $this->info[$comment_name]['encoding'] = $encoding;
  1028. }
  1029. // copy comments if key name set
  1030. if (!empty($this->info[$comment_name]['comments'])) {
  1031. foreach ($this->info[$comment_name]['comments'] as $tag_key => $valuearray) {
  1032. foreach ($valuearray as $key => $value) {
  1033. if (is_string($value)) {
  1034. $value = trim($value, " \r\n\t"); // do not trim nulls from $value!! Unicode characters will get mangled if trailing nulls are removed!
  1035. }
  1036. if ($value) {
  1037. if (!is_numeric($key)) {
  1038. $this->info['tags'][trim($tag_name)][trim($tag_key)][$key] = $value;
  1039. } else {
  1040. $this->info['tags'][trim($tag_name)][trim($tag_key)][] = $value;
  1041. }
  1042. }
  1043. }
  1044. if ($tag_key == 'picture') {
  1045. unset($this->info[$comment_name]['comments'][$tag_key]);
  1046. }
  1047. }
  1048. if (!isset($this->info['tags'][$tag_name])) {
  1049. // comments are set but contain nothing but empty strings, so skip
  1050. continue;
  1051. }
  1052. if ($this->option_tags_html) {
  1053. foreach ($this->info['tags'][$tag_name] as $tag_key => $valuearray) {
  1054. $this->info['tags_html'][$tag_name][$tag_key] = getid3_lib::recursiveMultiByteCharString2HTML($valuearray, $encoding);
  1055. }
  1056. }
  1057. $this->CharConvert($this->info['tags'][$tag_name], $encoding); // only copy gets converted!
  1058. }
  1059. }
  1060. // pictures can take up a lot of space, and we don't need multiple copies of them
  1061. // let there be a single copy in [comments][picture], and not elsewhere
  1062. if (!empty($this->info['tags'])) {
  1063. $unset_keys = array('tags', 'tags_html');
  1064. foreach ($this->info['tags'] as $tagtype => $tagarray) {
  1065. foreach ($tagarray as $tagname => $tagdata) {
  1066. if ($tagname == 'picture') {
  1067. foreach ($tagdata as $key => $tagarray) {
  1068. $this->info['comments']['picture'][] = $tagarray;
  1069. if (isset($tagarray['data']) && isset($tagarray['image_mime'])) {
  1070. if (isset($this->info['tags'][$tagtype][$tagname][$key])) {
  1071. unset($this->info['tags'][$tagtype][$tagname][$key]);
  1072. }
  1073. if (isset($this->info['tags_html'][$tagtype][$tagname][$key])) {
  1074. unset($this->info['tags_html'][$tagtype][$tagname][$key]);
  1075. }
  1076. }
  1077. }
  1078. }
  1079. }
  1080. foreach ($unset_keys as $unset_key) {
  1081. // remove possible empty keys from (e.g. [tags][id3v2][picture])
  1082. if (empty($this->info[$unset_key][$tagtype]['picture'])) {
  1083. unset($this->info[$unset_key][$tagtype]['picture']);
  1084. }
  1085. if (empty($this->info[$unset_key][$tagtype])) {
  1086. unset($this->info[$unset_key][$tagtype]);
  1087. }
  1088. if (empty($this->info[$unset_key])) {
  1089. unset($this->info[$unset_key]);
  1090. }
  1091. }
  1092. // remove duplicate copy of picture data from (e.g. [id3v2][comments][picture])
  1093. if (isset($this->info[$tagtype]['comments']['picture'])) {
  1094. unset($this->info[$tagtype]['comments']['picture']);
  1095. }
  1096. if (empty($this->info[$tagtype]['comments'])) {
  1097. unset($this->info[$tagtype]['comments']);
  1098. }
  1099. if (empty($this->info[$tagtype])) {
  1100. unset($this->info[$tagtype]);
  1101. }
  1102. }
  1103. }
  1104. return true;
  1105. }
  1106. public function getHashdata($algorithm) {
  1107. switch ($algorithm) {
  1108. case 'md5':
  1109. case 'sha1':
  1110. break;
  1111. default:
  1112. return $this->error('bad algorithm "'.$algorithm.'" in getHashdata()');
  1113. break;
  1114. }
  1115. if (!empty($this->info['fileformat']) && !empty($this->info['dataformat']) && ($this->info['fileformat'] == 'ogg') && ($this->info['audio']['dataformat'] == 'vorbis')) {
  1116. // We cannot get an identical md5_data value for Ogg files where the comments
  1117. // span more than 1 Ogg page (compared to the same audio data with smaller
  1118. // comments) using the normal getID3() method of MD5'ing the data between the
  1119. // end of the comments and the end of the file (minus any trailing tags),
  1120. // because the page sequence numbers of the pages that the audio data is on
  1121. // do not match. Under normal circumstances, where comments are smaller than
  1122. // the nominal 4-8kB page size, then this is not a problem, but if there are
  1123. // very large comments, the only way around it is to strip off the comment
  1124. // tags with vorbiscomment and MD5 that file.
  1125. // This procedure must be applied to ALL Ogg files, not just the ones with
  1126. // comments larger than 1 page, because the below method simply MD5's the
  1127. // whole file with the comments stripped, not just the portion after the
  1128. // comments block (which is the standard getID3() method.
  1129. // The above-mentioned problem of comments spanning multiple pages and changing
  1130. // page sequence numbers likely happens for OggSpeex and OggFLAC as well, but
  1131. // currently vorbiscomment only works on OggVorbis files.
  1132. if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
  1133. $this->warning('Failed making system call to vorbiscomment.exe - '.$algorithm.'_data is incorrect - error returned: PHP running in Safe Mode (backtick operator not available)');
  1134. $this->info[$algorithm.'_data'] = false;
  1135. } else {
  1136. // Prevent user from aborting script
  1137. $old_abort = ignore_user_abort(true);
  1138. // Create empty file
  1139. $empty = tempnam(GETID3_TEMP_DIR, 'getID3');
  1140. touch($empty);
  1141. // Use vorbiscomment to make temp file without comments
  1142. $temp = tempnam(GETID3_TEMP_DIR, 'getID3');
  1143. $file = $this->info['filenamepath'];
  1144. if (GETID3_OS_ISWINDOWS) {
  1145. if (file_exists(GETID3_HELPERAPPSDIR.'vorbiscomment.exe')) {
  1146. $commandline = '"'.GETID3_HELPERAPPSDIR.'vorbiscomment.exe" -w -c "'.$empty.'" "'.$file.'" "'.$temp.'"';
  1147. $VorbisCommentError = `$commandline`;
  1148. } else {
  1149. $VorbisCommentError = 'vorbiscomment.exe not found in '.GETID3_HELPERAPPSDIR;
  1150. }
  1151. } else {
  1152. $commandline = 'vorbiscomment -w -c "'.$empty.'" "'.$file.'" "'.$temp.'" 2>&1';
  1153. $commandline = 'vorbiscomment -w -c '.escapeshellarg($empty).' '.escapeshellarg($file).' '.escapeshellarg($temp).' 2>&1';
  1154. $VorbisCommentError = `$commandline`;
  1155. }
  1156. if (!empty($VorbisCommentError)) {
  1157. $this->info['warning'][] = 'Failed making system call to vorbiscomment(.exe) - '.$algorithm.'_data will be incorrect. If vorbiscomment is unavailable, please download from http://www.vorbis.com/download.psp and put in the getID3() directory. Error returned: '.$VorbisCommentError;
  1158. $this->info[$algorithm.'_data'] = false;
  1159. } else {
  1160. // Get hash of newly created file
  1161. switch ($algorithm) {
  1162. case 'md5':
  1163. $this->info[$algorithm.'_data'] = md5_file($temp);
  1164. break;
  1165. case 'sha1':
  1166. $this->info[$algorithm.'_data'] = sha1_file($temp);
  1167. break;
  1168. }
  1169. }
  1170. // Clean up
  1171. unlink($empty);
  1172. unlink($temp);
  1173. // Reset abort setting
  1174. ignore_user_abort($old_abort);
  1175. }
  1176. } else {
  1177. if (!empty($this->info['avdataoffset']) || (isset($this->info['avdataend']) && ($this->info['avdataend'] < $this->info['filesize']))) {
  1178. // get hash from part of file
  1179. $this->info[$algorithm.'_data'] = getid3_lib::hash_data($this->info['filenamepath'], $this->info['avdataoffset'], $this->info['avdataend'], $algorithm);
  1180. } else {
  1181. // get hash from whole file
  1182. switch ($algorithm) {
  1183. case 'md5':
  1184. $this->info[$algorithm.'_data'] = md5_file($this->info['filenamepath']);
  1185. break;
  1186. case 'sha1':
  1187. $this->info[$algorithm.'_data'] = sha1_file($this->info['filenamepath']);
  1188. break;
  1189. }
  1190. }
  1191. }
  1192. return true;
  1193. }
  1194. public function ChannelsBitratePlaytimeCalculations() {
  1195. // set channelmode on audio
  1196. if (!e

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