PageRenderTime 74ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/getid3/getid3.php

https://bitbucket.org/holyfield/wpgetid
PHP | 1744 lines | 1270 code | 244 blank | 230 comment | 228 complexity | 64dd687b1821a258d9d1e6f8d9acd860 MD5 | raw file

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

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