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 /////////////////////////////////////////////////////////////////
9 // Please see readme.txt for more information //
11 /////////////////////////////////////////////////////////////////
13 // define a constant rather than looking up every time it is needed
14 if (!defined('GETID3_OS_ISWINDOWS')) {
15 define('GETID3_OS_ISWINDOWS', (stripos(PHP_OS
, 'WIN') === 0));
17 // Get base path of getID3() - ONCE
18 if (!defined('GETID3_INCLUDEPATH')) {
19 define('GETID3_INCLUDEPATH', dirname(__FILE__
).DIRECTORY_SEPARATOR
);
21 // Workaround Bug #39923 (https://bugs.php.net/bug.php?id=39923)
22 if (!defined('IMG_JPG') && defined('IMAGETYPE_JPEG')) {
23 define('IMG_JPG', IMAGETYPE_JPEG
);
25 if (!defined('ENT_SUBSTITUTE')) { // PHP5.3 adds ENT_IGNORE, PHP5.4 adds ENT_SUBSTITUTE
26 define('ENT_SUBSTITUTE', (defined('ENT_IGNORE') ? ENT_IGNORE
: 8));
29 // attempt to define temp dir as something flexible but reliable
30 $temp_dir = ini_get('upload_tmp_dir');
31 if ($temp_dir && (!is_dir($temp_dir) ||
!is_readable($temp_dir))) {
34 if (!$temp_dir && function_exists('sys_get_temp_dir')) { // sys_get_temp_dir added in PHP v5.2.1
35 // sys_get_temp_dir() may give inaccessible temp dir, e.g. with open_basedir on virtual hosts
36 $temp_dir = sys_get_temp_dir();
38 $temp_dir = @realpath
($temp_dir); // see https://github.com/JamesHeinrich/getID3/pull/10
39 $open_basedir = ini_get('open_basedir');
41 // e.g. "/var/www/vhosts/getid3.org/httpdocs/:/tmp/"
42 $temp_dir = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR
, $temp_dir);
43 $open_basedir = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR
, $open_basedir);
44 if (substr($temp_dir, -1, 1) != DIRECTORY_SEPARATOR
) {
45 $temp_dir .= DIRECTORY_SEPARATOR
;
47 $found_valid_tempdir = false;
48 $open_basedirs = explode(PATH_SEPARATOR
, $open_basedir);
49 foreach ($open_basedirs as $basedir) {
50 if (substr($basedir, -1, 1) != DIRECTORY_SEPARATOR
) {
51 $basedir .= DIRECTORY_SEPARATOR
;
53 if (preg_match('#^'.preg_quote($basedir).'#', $temp_dir)) {
54 $found_valid_tempdir = true;
58 if (!$found_valid_tempdir) {
61 unset($open_basedirs, $found_valid_tempdir, $basedir);
64 $temp_dir = '*'; // invalid directory name should force tempnam() to use system default temp dir
66 // $temp_dir = '/something/else/'; // feel free to override temp dir here if it works better for your system
67 if (!defined('GETID3_TEMP_DIR')) {
68 define('GETID3_TEMP_DIR', $temp_dir);
70 unset($open_basedir, $temp_dir);
78 public $encoding = 'UTF-8'; // CASE SENSITIVE! - i.e. (must be supported by iconv()). Examples: ISO-8859-1 UTF-8 UTF-16 UTF-16BE
79 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'
81 // public: Optional tag checks - disable for speed.
82 public $option_tag_id3v1 = true; // Read and process ID3v1 tags
83 public $option_tag_id3v2 = true; // Read and process ID3v2 tags
84 public $option_tag_lyrics3 = true; // Read and process Lyrics3 tags
85 public $option_tag_apetag = true; // Read and process APE tags
86 public $option_tags_process = true; // Copy tags to root key 'tags' and encode to $this->encoding
87 public $option_tags_html = true; // Copy tags to root key 'tags_html' properly translated from various encodings to HTML entities
89 // public: Optional tag/comment calucations
90 public $option_extra_info = true; // Calculate additional info such as bitrate, channelmode etc
92 // public: Optional handling of embedded attachments (e.g. images)
93 public $option_save_attachments = true; // defaults to true (ATTACHMENTS_INLINE) for backward compatibility
95 // public: Optional calculations
96 public $option_md5_data = false; // Get MD5 sum of data part - slow
97 public $option_md5_data_source = false; // Use MD5 of source file if availble - only FLAC and OptimFROG
98 public $option_sha1_data = false; // Get SHA1 sum of data part - slow
99 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)
101 // public: Read buffer size in bytes
102 public $option_fread_buffer_size = 32768;
105 public $filename; // Filename of file being analysed.
106 public $fp; // Filepointer to file being analysed.
107 public $info; // Result array.
108 public $tempdir = GETID3_TEMP_DIR
;
109 public $memory_limit = 0;
111 // Protected variables
112 protected $startup_error = '';
113 protected $startup_warning = '';
115 const VERSION
= '1.9.14-201703261440';
116 const FREAD_BUFFER_SIZE
= 32768;
118 const ATTACHMENTS_NONE
= false;
119 const ATTACHMENTS_INLINE
= true;
121 // public: constructor
122 public function __construct() {
124 // Check for PHP version
125 $required_php_version = '5.3.0';
126 if (version_compare(PHP_VERSION
, $required_php_version, '<')) {
127 $this->startup_error
.= 'getID3() requires PHP v'.$required_php_version.' or higher - you are running v'.PHP_VERSION
."\n";
132 $this->memory_limit
= ini_get('memory_limit');
133 if (preg_match('#([0-9]+)M#i', $this->memory_limit
, $matches)) {
134 // could be stored as "16M" rather than 16777216 for example
135 $this->memory_limit
= $matches[1] * 1048576;
136 } elseif (preg_match('#([0-9]+)G#i', $this->memory_limit
, $matches)) { // The 'G' modifier is available since PHP 5.1.0
137 // could be stored as "2G" rather than 2147483648 for example
138 $this->memory_limit
= $matches[1] * 1073741824;
140 if ($this->memory_limit
<= 0) {
141 // memory limits probably disabled
142 } elseif ($this->memory_limit
<= 4194304) {
143 $this->startup_error
.= 'PHP has less than 4MB available memory and will very likely run out. Increase memory_limit in php.ini'."\n";
144 } elseif ($this->memory_limit
<= 12582912) {
145 $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'."\n";
148 // Check safe_mode off
149 if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
150 $this->warning('WARNING: Safe mode is on, shorten support disabled, md5data/sha1data for ogg vorbis disabled, ogg vorbos/flac tag writing disabled.');
153 if (($mbstring_func_overload = ini_get('mbstring.func_overload')) && ($mbstring_func_overload & 0x02)) {
154 // http://php.net/manual/en/mbstring.overload.php
155 // "mbstring.func_overload in php.ini is a positive value that represents a combination of bitmasks specifying the categories of functions to be overloaded. It should be set to 1 to overload the mail() function. 2 for string functions, 4 for regular expression functions"
156 // getID3 cannot run when string functions are overloaded. It doesn't matter if mail() or ereg* functions are overloaded since getID3 does not use those.
157 $this->startup_error
.= 'WARNING: php.ini contains "mbstring.func_overload = '.ini_get('mbstring.func_overload').'", getID3 cannot run with this setting (bitmask 2 (string functions) cannot be set). Recommended to disable entirely.'."\n";
160 // Check for magic_quotes_runtime
161 if (function_exists('get_magic_quotes_runtime')) {
162 if (get_magic_quotes_runtime()) {
163 $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).'."\n";
167 // Check for magic_quotes_gpc
168 if (function_exists('magic_quotes_gpc')) {
169 if (get_magic_quotes_gpc()) {
170 $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).'."\n";
174 // Load support library
175 if (!include_once(GETID3_INCLUDEPATH
.'getid3.lib.php')) {
176 $this->startup_error
.= 'getid3.lib.php is missing or corrupt'."\n";
179 if ($this->option_max_2gb_check
=== null) {
180 $this->option_max_2gb_check
= (PHP_INT_MAX
<= 2147483647);
184 // Needed for Windows only:
185 // Define locations of helper applications for Shorten, VorbisComment, MetaFLAC
186 // as well as other helper functions such as head, tail, md5sum, etc
187 // This path cannot contain spaces, but the below code will attempt to get the
188 // 8.3-equivalent path automatically
189 // IMPORTANT: This path must include the trailing slash
190 if (GETID3_OS_ISWINDOWS
&& !defined('GETID3_HELPERAPPSDIR')) {
192 $helperappsdir = GETID3_INCLUDEPATH
.'..'.DIRECTORY_SEPARATOR
.'helperapps'; // must not have any space in this path
194 if (!is_dir($helperappsdir)) {
195 $this->startup_warning
.= '"'.$helperappsdir.'" cannot be defined as GETID3_HELPERAPPSDIR because it does not exist'."\n";
196 } elseif (strpos(realpath($helperappsdir), ' ') !== false) {
197 $DirPieces = explode(DIRECTORY_SEPARATOR
, realpath($helperappsdir));
198 $path_so_far = array();
199 foreach ($DirPieces as $key => $value) {
200 if (strpos($value, ' ') !== false) {
201 if (!empty($path_so_far)) {
202 $commandline = 'dir /x '.escapeshellarg(implode(DIRECTORY_SEPARATOR
, $path_so_far));
203 $dir_listing = `
$commandline`
;
204 $lines = explode("\n", $dir_listing);
205 foreach ($lines as $line) {
207 if (preg_match('#^([0-9/]{10}) +([0-9:]{4,5}( [AP]M)?) +(<DIR>|[0-9,]+) +([^ ]{0,11}) +(.+)$#', $line, $matches)) {
208 list($dummy, $date, $time, $ampm, $filesize, $shortname, $filename) = $matches;
209 if ((strtoupper($filesize) == '<DIR>') && (strtolower($filename) == strtolower($value))) {
215 $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.'."\n";
218 $path_so_far[] = $value;
220 $helperappsdir = implode(DIRECTORY_SEPARATOR
, $path_so_far);
222 define('GETID3_HELPERAPPSDIR', $helperappsdir.DIRECTORY_SEPARATOR
);
225 if (!empty($this->startup_error
)) {
226 echo $this->startup_error
;
227 throw new getid3_exception($this->startup_error
);
233 public function version() {
234 return self
::VERSION
;
237 public function fread_buffer_size() {
238 return $this->option_fread_buffer_size
;
243 public function setOption($optArray) {
244 if (!is_array($optArray) ||
empty($optArray)) {
247 foreach ($optArray as $opt => $val) {
248 if (isset($this->$opt) === false) {
257 public function openfile($filename, $filesize=null) {
259 if (!empty($this->startup_error
)) {
260 throw new getid3_exception($this->startup_error
);
262 if (!empty($this->startup_warning
)) {
263 foreach (explode("\n", $this->startup_warning
) as $startup_warning) {
264 $this->warning($startup_warning);
268 // init result array and set parameters
269 $this->filename
= $filename;
270 $this->info
= array();
271 $this->info
['GETID3_VERSION'] = $this->version();
272 $this->info
['php_memory_limit'] = (($this->memory_limit
> 0) ?
$this->memory_limit
: false);
274 // remote files not supported
275 if (preg_match('#^(ht|f)tp://#', $filename)) {
276 throw new getid3_exception('Remote files are not supported - please copy the file locally first');
279 $filename = str_replace('/', DIRECTORY_SEPARATOR
, $filename);
280 $filename = preg_replace('#(.+)'.preg_quote(DIRECTORY_SEPARATOR
).'{2,}#U', '\1'.DIRECTORY_SEPARATOR
, $filename);
283 //if (is_readable($filename) && is_file($filename) && ($this->fp = fopen($filename, 'rb'))) { // see http://www.getid3.org/phpBB3/viewtopic.php?t=1720
284 if ((is_readable($filename) ||
file_exists($filename)) && is_file($filename) && ($this->fp
= fopen($filename, 'rb'))) {
287 $errormessagelist = array();
288 if (!is_readable($filename)) {
289 $errormessagelist[] = '!is_readable';
291 if (!is_file($filename)) {
292 $errormessagelist[] = '!is_file';
294 if (!file_exists($filename)) {
295 $errormessagelist[] = '!file_exists';
297 if (empty($errormessagelist)) {
298 $errormessagelist[] = 'fopen failed';
300 throw new getid3_exception('Could not open "'.$filename.'" ('.implode('; ', $errormessagelist).')');
303 $this->info
['filesize'] = (!is_null($filesize) ?
$filesize : filesize($filename));
304 // set redundant parameters - might be needed in some include file
305 // filenames / filepaths in getID3 are always expressed with forward slashes (unix-style) for both Windows and other to try and minimize confusion
306 $filename = str_replace('\\', '/', $filename);
307 $this->info
['filepath'] = str_replace('\\', '/', realpath(dirname($filename)));
308 $this->info
['filename'] = getid3_lib
::mb_basename($filename);
309 $this->info
['filenamepath'] = $this->info
['filepath'].'/'.$this->info
['filename'];
311 // set more parameters
312 $this->info
['avdataoffset'] = 0;
313 $this->info
['avdataend'] = $this->info
['filesize'];
314 $this->info
['fileformat'] = ''; // filled in later
315 $this->info
['audio']['dataformat'] = ''; // filled in later, unset if not used
316 $this->info
['video']['dataformat'] = ''; // filled in later, unset if not used
317 $this->info
['tags'] = array(); // filled in later, unset if not used
318 $this->info
['error'] = array(); // filled in later, unset if not used
319 $this->info
['warning'] = array(); // filled in later, unset if not used
320 $this->info
['comments'] = array(); // filled in later, unset if not used
321 $this->info
['encoding'] = $this->encoding
; // required by id3v2 and iso modules - can be unset at the end if desired
323 // option_max_2gb_check
324 if ($this->option_max_2gb_check
) {
325 // PHP (32-bit all, and 64-bit Windows) doesn't support integers larger than 2^31 (~2GB)
326 // filesize() simply returns (filesize % (pow(2, 32)), no matter the actual filesize
327 // ftell() returns 0 if seeking to the end is beyond the range of unsigned integer
328 $fseek = fseek($this->fp
, 0, SEEK_END
);
329 if (($fseek < 0) ||
(($this->info
['filesize'] != 0) && (ftell($this->fp
) == 0)) ||
330 ($this->info
['filesize'] < 0) ||
331 (ftell($this->fp
) < 0)) {
332 $real_filesize = getid3_lib
::getFileSizeSyscall($this->info
['filenamepath']);
334 if ($real_filesize === false) {
335 unset($this->info
['filesize']);
337 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.');
338 } elseif (getid3_lib
::intValueSupported($real_filesize)) {
339 unset($this->info
['filesize']);
341 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');
343 $this->info
['filesize'] = $real_filesize;
344 $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.');
350 } catch (Exception
$e) {
351 $this->error($e->getMessage());
356 // public: analyze file
357 public function analyze($filename, $filesize=null, $original_filename='') {
359 if (!$this->openfile($filename, $filesize)) {
364 foreach (array('id3v2'=>'id3v2', 'id3v1'=>'id3v1', 'apetag'=>'ape', 'lyrics3'=>'lyrics3') as $tag_name => $tag_key) {
365 $option_tag = 'option_tag_'.$tag_name;
366 if ($this->$option_tag) {
367 $this->include_module('tag.'.$tag_name);
369 $tag_class = 'getid3_'.$tag_name;
370 $tag = new $tag_class($this);
373 catch (getid3_exception
$e) {
378 if (isset($this->info
['id3v2']['tag_offset_start'])) {
379 $this->info
['avdataoffset'] = max($this->info
['avdataoffset'], $this->info
['id3v2']['tag_offset_end']);
381 foreach (array('id3v1'=>'id3v1', 'apetag'=>'ape', 'lyrics3'=>'lyrics3') as $tag_name => $tag_key) {
382 if (isset($this->info
[$tag_key]['tag_offset_start'])) {
383 $this->info
['avdataend'] = min($this->info
['avdataend'], $this->info
[$tag_key]['tag_offset_start']);
387 // ID3v2 detection (NOT parsing), even if ($this->option_tag_id3v2 == false) done to make fileformat easier
388 if (!$this->option_tag_id3v2
) {
390 $header = fread($this->fp
, 10);
391 if ((substr($header, 0, 3) == 'ID3') && (strlen($header) == 10)) {
392 $this->info
['id3v2']['header'] = true;
393 $this->info
['id3v2']['majorversion'] = ord($header{3});
394 $this->info
['id3v2']['minorversion'] = ord($header{4});
395 $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
399 // read 32 kb file data
400 fseek($this->fp
, $this->info
['avdataoffset']);
401 $formattest = fread($this->fp
, 32774);
404 $determined_format = $this->GetFileFormat($formattest, ($original_filename ?
$original_filename : $filename));
406 // unable to determine file format
407 if (!$determined_format) {
409 return $this->error('unable to determine file format');
412 // check for illegal ID3 tags
413 if (isset($determined_format['fail_id3']) && (in_array('id3v1', $this->info
['tags']) ||
in_array('id3v2', $this->info
['tags']))) {
414 if ($determined_format['fail_id3'] === 'ERROR') {
416 return $this->error('ID3 tags not allowed on this file type.');
417 } elseif ($determined_format['fail_id3'] === 'WARNING') {
418 $this->warning('ID3 tags not allowed on this file type.');
422 // check for illegal APE tags
423 if (isset($determined_format['fail_ape']) && in_array('ape', $this->info
['tags'])) {
424 if ($determined_format['fail_ape'] === 'ERROR') {
426 return $this->error('APE tags not allowed on this file type.');
427 } elseif ($determined_format['fail_ape'] === 'WARNING') {
428 $this->warning('APE tags not allowed on this file type.');
433 $this->info
['mime_type'] = $determined_format['mime_type'];
435 // supported format signature pattern detected, but module deleted
436 if (!file_exists(GETID3_INCLUDEPATH
.$determined_format['include'])) {
438 return $this->error('Format not supported, module "'.$determined_format['include'].'" was removed.');
441 // module requires mb_convert_encoding/iconv support
442 // Check encoding/iconv support
443 if (!empty($determined_format['iconv_req']) && !function_exists('mb_convert_encoding') && !function_exists('iconv') && !in_array($this->encoding
, array('ISO-8859-1', 'UTF-8', 'UTF-16LE', 'UTF-16BE', 'UTF-16'))) {
444 $errormessage = 'mb_convert_encoding() or 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. ';
445 if (GETID3_OS_ISWINDOWS
) {
446 $errormessage .= 'PHP does not have mb_convert_encoding() or iconv() support. Please enable php_mbstring.dll / php_iconv.dll in php.ini, and copy php_mbstring.dll / iconv.dll from c:/php/dlls to c:/windows/system32';
448 $errormessage .= 'PHP is not compiled with mb_convert_encoding() or iconv() support. Please recompile with the --enable-mbstring / --with-iconv switch';
450 return $this->error($errormessage);
454 include_once(GETID3_INCLUDEPATH
.$determined_format['include']);
456 // instantiate module class
457 $class_name = 'getid3_'.$determined_format['module'];
458 if (!class_exists($class_name)) {
459 return $this->error('Format not supported, module "'.$determined_format['include'].'" is corrupt.');
461 $class = new $class_name($this);
468 // process all tags - copy to 'tags' and convert charsets
469 if ($this->option_tags_process
) {
470 $this->HandleAllTags();
473 // perform more calculations
474 if ($this->option_extra_info
) {
475 $this->ChannelsBitratePlaytimeCalculations();
476 $this->CalculateCompressionRatioVideo();
477 $this->CalculateCompressionRatioAudio();
478 $this->CalculateReplayGain();
479 $this->ProcessAudioStreams();
482 // get the MD5 sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags
483 if ($this->option_md5_data
) {
484 // do not calc md5_data if md5_data_source is present - set by flac only - future MPC/SV8 too
485 if (!$this->option_md5_data_source ||
empty($this->info
['md5_data_source'])) {
486 $this->getHashdata('md5');
490 // get the SHA1 sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags
491 if ($this->option_sha1_data
) {
492 $this->getHashdata('sha1');
495 // remove undesired keys
498 } catch (Exception
$e) {
499 $this->error('Caught exception: '.$e->getMessage());
507 // private: error handling
508 public function error($message) {
510 if (!isset($this->info
['error'])) {
511 $this->info
['error'] = array();
513 $this->info
['error'][] = $message;
518 // private: warning handling
519 public function warning($message) {
520 $this->info
['warning'][] = $message;
526 private function CleanUp() {
528 // remove possible empty keys
529 $AVpossibleEmptyKeys = array('dataformat', 'bits_per_sample', 'encoder_options', 'streams', 'bitrate');
530 foreach ($AVpossibleEmptyKeys as $dummy => $key) {
531 if (empty($this->info
['audio'][$key]) && isset($this->info
['audio'][$key])) {
532 unset($this->info
['audio'][$key]);
534 if (empty($this->info
['video'][$key]) && isset($this->info
['video'][$key])) {
535 unset($this->info
['video'][$key]);
539 // remove empty root keys
540 if (!empty($this->info
)) {
541 foreach ($this->info
as $key => $value) {
542 if (empty($this->info
[$key]) && ($this->info
[$key] !== 0) && ($this->info
[$key] !== '0')) {
543 unset($this->info
[$key]);
548 // remove meaningless entries from unknown-format files
549 if (empty($this->info
['fileformat'])) {
550 if (isset($this->info
['avdataoffset'])) {
551 unset($this->info
['avdataoffset']);
553 if (isset($this->info
['avdataend'])) {
554 unset($this->info
['avdataend']);
558 // remove possible duplicated identical entries
559 if (!empty($this->info
['error'])) {
560 $this->info
['error'] = array_values(array_unique($this->info
['error']));
562 if (!empty($this->info
['warning'])) {
563 $this->info
['warning'] = array_values(array_unique($this->info
['warning']));
566 // remove "global variable" type keys
567 unset($this->info
['php_memory_limit']);
573 // return array containing information about all supported formats
574 public function GetFileFormatArray() {
575 static $format_info = array();
576 if (empty($format_info)) {
577 $format_info = array(
581 // AC-3 - audio - Dolby AC-3 / Dolby Digital
583 'pattern' => '^\\x0B\\x77',
586 'mime_type' => 'audio/ac3',
589 // AAC - audio - Advanced Audio Coding (AAC) - ADIF format
591 'pattern' => '^ADIF',
594 'mime_type' => 'application/octet-stream',
595 'fail_ape' => 'WARNING',
599 // AA - audio - Audible Audiobook
601 'pattern' => '^.{4}\\x57\\x90\\x75\\x36',
604 'mime_type' => 'audio/audible',
607 // AAC - audio - Advanced Audio Coding (AAC) - ADTS format (very similar to MP3)
609 'pattern' => '^\\xFF[\\xF0-\\xF1\\xF8-\\xF9]',
612 'mime_type' => 'application/octet-stream',
613 'fail_ape' => 'WARNING',
617 // AU - audio - NeXT/Sun AUdio (AU)
619 'pattern' => '^\\.snd',
622 'mime_type' => 'audio/basic',
625 // AMR - audio - Adaptive Multi Rate
627 'pattern' => '^\\x23\\x21AMR\\x0A', // #!AMR[0A]
630 'mime_type' => 'audio/amr',
633 // AVR - audio - Audio Visual Research
635 'pattern' => '^2BIT',
638 'mime_type' => 'application/octet-stream',
641 // BONK - audio - Bonk v0.9+
643 'pattern' => '^\\x00(BONK|INFO|META| ID3)',
646 'mime_type' => 'audio/xmms-bonk',
649 // DSF - audio - Direct Stream Digital (DSD) Storage Facility files (DSF) - https://en.wikipedia.org/wiki/Direct_Stream_Digital
651 'pattern' => '^DSD ', // including trailing space: 44 53 44 20
654 'mime_type' => 'audio/dsd',
657 // DSS - audio - Digital Speech Standard
659 'pattern' => '^[\\x02-\\x06]ds[s2]',
662 'mime_type' => 'application/octet-stream',
665 // DTS - audio - Dolby Theatre System
667 'pattern' => '^\\x7F\\xFE\\x80\\x01',
670 'mime_type' => 'audio/dts',
673 // FLAC - audio - Free Lossless Audio Codec
675 'pattern' => '^fLaC',
678 'mime_type' => 'audio/x-flac',
681 // LA - audio - Lossless Audio (LA)
683 'pattern' => '^LA0[2-4]',
686 'mime_type' => 'application/octet-stream',
689 // LPAC - audio - Lossless Predictive Audio Compression (LPAC)
691 'pattern' => '^LPAC',
694 'mime_type' => 'application/octet-stream',
697 // MIDI - audio - MIDI (Musical Instrument Digital Interface)
699 'pattern' => '^MThd',
702 'mime_type' => 'audio/midi',
705 // MAC - audio - Monkey's Audio Compressor
707 'pattern' => '^MAC ',
709 'module' => 'monkey',
710 'mime_type' => 'application/octet-stream',
713 // has been known to produce false matches in random files (e.g. JPEGs), leave out until more precise matching available
714 // // MOD - audio - MODule (assorted sub-formats)
716 // 'pattern' => '^.{1080}(M\\.K\\.|M!K!|FLT4|FLT8|[5-9]CHN|[1-3][0-9]CH)',
717 // 'group' => 'audio',
718 // 'module' => 'mod',
719 // 'option' => 'mod',
720 // 'mime_type' => 'audio/mod',
723 // MOD - audio - MODule (Impulse Tracker)
725 'pattern' => '^IMPM',
729 'mime_type' => 'audio/it',
732 // MOD - audio - MODule (eXtended Module, various sub-formats)
734 'pattern' => '^Extended Module',
738 'mime_type' => 'audio/xm',
741 // MOD - audio - MODule (ScreamTracker)
743 'pattern' => '^.{44}SCRM',
747 'mime_type' => 'audio/s3m',
750 // MPC - audio - Musepack / MPEGplus
752 'pattern' => '^(MPCK|MP\\+|[\\x00\\x01\\x10\\x11\\x40\\x41\\x50\\x51\\x80\\x81\\x90\\x91\\xC0\\xC1\\xD0\\xD1][\\x20-\\x37][\\x00\\x20\\x40\\x60\\x80\\xA0\\xC0\\xE0])',
755 'mime_type' => 'audio/x-musepack',
758 // MP3 - audio - MPEG-audio Layer 3 (very similar to AAC-ADTS)
760 '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]',
763 'mime_type' => 'audio/mpeg',
766 // OFR - audio - OptimFROG
768 'pattern' => '^(\\*RIFF|OFR)',
770 'module' => 'optimfrog',
771 'mime_type' => 'application/octet-stream',
774 // RKAU - audio - RKive AUdio compressor
779 'mime_type' => 'application/octet-stream',
782 // SHN - audio - Shorten
784 'pattern' => '^ajkg',
786 'module' => 'shorten',
787 'mime_type' => 'audio/xmms-shn',
788 'fail_id3' => 'ERROR',
789 'fail_ape' => 'ERROR',
792 // TTA - audio - TTA Lossless Audio Compressor (http://tta.corecodec.org)
794 'pattern' => '^TTA', // could also be '^TTA(\\x01|\\x02|\\x03|2|1)'
797 'mime_type' => 'application/octet-stream',
800 // VOC - audio - Creative Voice (VOC)
802 'pattern' => '^Creative Voice File',
805 'mime_type' => 'audio/voc',
808 // VQF - audio - transform-domain weighted interleave Vector Quantization Format (VQF)
810 'pattern' => '^TWIN',
813 'mime_type' => 'application/octet-stream',
816 // WV - audio - WavPack (v4.0+)
818 'pattern' => '^wvpk',
820 'module' => 'wavpack',
821 'mime_type' => 'application/octet-stream',
825 // Audio-Video formats
827 // ASF - audio/video - Advanced Streaming Format, Windows Media Video, Windows Media Audio
829 'pattern' => '^\\x30\\x26\\xB2\\x75\\x8E\\x66\\xCF\\x11\\xA6\\xD9\\x00\\xAA\\x00\\x62\\xCE\\x6C',
830 'group' => 'audio-video',
832 'mime_type' => 'video/x-ms-asf',
833 'iconv_req' => false,
836 // BINK - audio/video - Bink / Smacker
838 'pattern' => '^(BIK|SMK)',
839 'group' => 'audio-video',
841 'mime_type' => 'application/octet-stream',
844 // FLV - audio/video - FLash Video
846 'pattern' => '^FLV[\\x01]',
847 'group' => 'audio-video',
849 'mime_type' => 'video/x-flv',
852 // MKAV - audio/video - Mastroka
854 'pattern' => '^\\x1A\\x45\\xDF\\xA3',
855 'group' => 'audio-video',
856 'module' => 'matroska',
857 'mime_type' => 'video/x-matroska', // may also be audio/x-matroska
860 // MPEG - audio/video - MPEG (Moving Pictures Experts Group)
862 'pattern' => '^\\x00\\x00\\x01[\\xB3\\xBA]',
863 'group' => 'audio-video',
865 'mime_type' => 'video/mpeg',
868 // NSV - audio/video - Nullsoft Streaming Video (NSV)
870 'pattern' => '^NSV[sf]',
871 'group' => 'audio-video',
873 'mime_type' => 'application/octet-stream',
876 // Ogg - audio/video - Ogg (Ogg-Vorbis, Ogg-FLAC, Speex, Ogg-Theora(*), Ogg-Tarkin(*))
878 'pattern' => '^OggS',
881 'mime_type' => 'application/ogg',
882 'fail_id3' => 'WARNING',
883 'fail_ape' => 'WARNING',
886 // QT - audio/video - Quicktime
887 'quicktime' => array(
888 'pattern' => '^.{4}(cmov|free|ftyp|mdat|moov|pnot|skip|wide)',
889 'group' => 'audio-video',
890 'module' => 'quicktime',
891 'mime_type' => 'video/quicktime',
894 // 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)
896 'pattern' => '^(RIFF|SDSS|FORM)',
897 'group' => 'audio-video',
899 'mime_type' => 'audio/x-wav',
900 'fail_ape' => 'WARNING',
903 // Real - audio/video - RealAudio, RealVideo
905 'pattern' => '^\\.(RMF|ra)',
906 'group' => 'audio-video',
908 'mime_type' => 'audio/x-realaudio',
911 // SWF - audio/video - ShockWave Flash
913 'pattern' => '^(F|C)WS',
914 'group' => 'audio-video',
916 'mime_type' => 'application/x-shockwave-flash',
919 // TS - audio/video - MPEG-2 Transport Stream
921 'pattern' => '^(\\x47.{187}){10,}', // packets are 188 bytes long and start with 0x47 "G". Check for at least 10 packets matching this pattern
922 'group' => 'audio-video',
924 'mime_type' => 'video/MP2T',
928 // Still-Image formats
930 // BMP - still image - Bitmap (Windows, OS/2; uncompressed, RLE8, RLE4)
933 'group' => 'graphic',
935 'mime_type' => 'image/bmp',
936 'fail_id3' => 'ERROR',
937 'fail_ape' => 'ERROR',
940 // GIF - still image - Graphics Interchange Format
943 'group' => 'graphic',
945 'mime_type' => 'image/gif',
946 'fail_id3' => 'ERROR',
947 'fail_ape' => 'ERROR',
950 // JPEG - still image - Joint Photographic Experts Group (JPEG)
952 'pattern' => '^\\xFF\\xD8\\xFF',
953 'group' => 'graphic',
955 'mime_type' => 'image/jpeg',
956 'fail_id3' => 'ERROR',
957 'fail_ape' => 'ERROR',
960 // PCD - still image - Kodak Photo CD
962 'pattern' => '^.{2048}PCD_IPI\\x00',
963 'group' => 'graphic',
965 'mime_type' => 'image/x-photo-cd',
966 'fail_id3' => 'ERROR',
967 'fail_ape' => 'ERROR',
971 // PNG - still image - Portable Network Graphics (PNG)
973 'pattern' => '^\\x89\\x50\\x4E\\x47\\x0D\\x0A\\x1A\\x0A',
974 'group' => 'graphic',
976 'mime_type' => 'image/png',
977 'fail_id3' => 'ERROR',
978 'fail_ape' => 'ERROR',
982 // SVG - still image - Scalable Vector Graphics (SVG)
984 'pattern' => '(<!DOCTYPE svg PUBLIC |xmlns="http://www\\.w3\\.org/2000/svg")',
985 'group' => 'graphic',
987 'mime_type' => 'image/svg+xml',
988 'fail_id3' => 'ERROR',
989 'fail_ape' => 'ERROR',
993 // TIFF - still image - Tagged Information File Format (TIFF)
995 'pattern' => '^(II\\x2A\\x00|MM\\x00\\x2A)',
996 'group' => 'graphic',
998 'mime_type' => 'image/tiff',
999 'fail_id3' => 'ERROR',
1000 'fail_ape' => 'ERROR',
1004 // EFAX - still image - eFax (TIFF derivative)
1006 'pattern' => '^\\xDC\\xFE',
1007 'group' => 'graphic',
1009 'mime_type' => 'image/efax',
1010 'fail_id3' => 'ERROR',
1011 'fail_ape' => 'ERROR',
1017 // ISO - data - International Standards Organization (ISO) CD-ROM Image
1019 'pattern' => '^.{32769}CD001',
1022 'mime_type' => 'application/octet-stream',
1023 'fail_id3' => 'ERROR',
1024 'fail_ape' => 'ERROR',
1025 'iconv_req' => false,
1028 // RAR - data - RAR compressed data
1030 'pattern' => '^Rar\\!',
1031 'group' => 'archive',
1033 'mime_type' => 'application/octet-stream',
1034 'fail_id3' => 'ERROR',
1035 'fail_ape' => 'ERROR',
1038 // SZIP - audio/data - SZIP compressed data
1040 'pattern' => '^SZ\\x0A\\x04',
1041 'group' => 'archive',
1043 'mime_type' => 'application/octet-stream',
1044 'fail_id3' => 'ERROR',
1045 'fail_ape' => 'ERROR',
1048 // TAR - data - TAR compressed data
1050 '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}',
1051 'group' => 'archive',
1053 'mime_type' => 'application/x-tar',
1054 'fail_id3' => 'ERROR',
1055 'fail_ape' => 'ERROR',
1058 // GZIP - data - GZIP compressed data
1060 'pattern' => '^\\x1F\\x8B\\x08',
1061 'group' => 'archive',
1063 'mime_type' => 'application/x-gzip',
1064 'fail_id3' => 'ERROR',
1065 'fail_ape' => 'ERROR',
1068 // ZIP - data - ZIP compressed data
1070 'pattern' => '^PK\\x03\\x04',
1071 'group' => 'archive',
1073 'mime_type' => 'application/zip',
1074 'fail_id3' => 'ERROR',
1075 'fail_ape' => 'ERROR',
1079 // Misc other formats
1081 // PAR2 - data - Parity Volume Set Specification 2.0
1083 'pattern' => '^PAR2\\x00PKT',
1086 'mime_type' => 'application/octet-stream',
1087 'fail_id3' => 'ERROR',
1088 'fail_ape' => 'ERROR',
1091 // PDF - data - Portable Document Format
1093 'pattern' => '^\\x25PDF',
1096 'mime_type' => 'application/pdf',
1097 'fail_id3' => 'ERROR',
1098 'fail_ape' => 'ERROR',
1101 // MSOFFICE - data - ZIP compressed data
1102 'msoffice' => array(
1103 'pattern' => '^\\xD0\\xCF\\x11\\xE0\\xA1\\xB1\\x1A\\xE1', // D0CF11E == DOCFILE == Microsoft Office Document
1105 'module' => 'msoffice',
1106 'mime_type' => 'application/octet-stream',
1107 'fail_id3' => 'ERROR',
1108 'fail_ape' => 'ERROR',
1111 // CUE - data - CUEsheet (index to single-file disc images)
1113 'pattern' => '', // empty pattern means cannot be automatically detected, will fall through all other formats and match based on filename and very basic file contents
1116 'mime_type' => 'application/octet-stream',
1122 return $format_info;
1127 public function GetFileFormat(&$filedata, $filename='') {
1128 // this function will determine the format of a file based on usually
1129 // the first 2-4 bytes of the file (8 bytes for PNG, 16 bytes for JPG,
1130 // and in the case of ISO CD image, 6 bytes offset 32kb from the start
1133 // Identify file format - loop through $format_info and detect with reg expr
1134 foreach ($this->GetFileFormatArray() as $format_name => $info) {
1135 // The /s switch on preg_match() forces preg_match() NOT to treat
1136 // newline (0x0A) characters as special chars but do a binary match
1137 if (!empty($info['pattern']) && preg_match('#'.$info['pattern'].'#s', $filedata)) {
1138 $info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php';
1144 if (preg_match('#\\.mp[123a]$#i', $filename)) {
1145 // Too many mp3 encoders on the market put gabage in front of mpeg files
1146 // use assume format on these if format detection failed
1147 $GetFileFormatArray = $this->GetFileFormatArray();
1148 $info = $GetFileFormatArray['mp3'];
1149 $info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php';
1151 } elseif (preg_match('#\\.cue$#i', $filename) && preg_match('#FILE "[^"]+" (BINARY|MOTOROLA|AIFF|WAVE|MP3)#', $filedata)) {
1152 // there's not really a useful consistent "magic" at the beginning of .cue files to identify them
1153 // so until I think of something better, just go by filename if all other format checks fail
1154 // and verify there's at least one instance of "TRACK xx AUDIO" in the file
1155 $GetFileFormatArray = $this->GetFileFormatArray();
1156 $info = $GetFileFormatArray['cue'];
1157 $info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php';
1165 // converts array to $encoding charset from $this->encoding
1166 public function CharConvert(&$array, $encoding) {
1168 // identical encoding - end here
1169 if ($encoding == $this->encoding
) {
1174 foreach ($array as $key => $value) {
1177 if (is_array($value)) {
1178 $this->CharConvert($array[$key], $encoding);
1182 elseif (is_string($value)) {
1183 $array[$key] = trim(getid3_lib
::iconv_fallback($encoding, $this->encoding
, $value));
1189 public function HandleAllTags() {
1191 // key name => array (tag name, character encoding)
1195 'asf' => array('asf' , 'UTF-16LE'),
1196 'midi' => array('midi' , 'ISO-8859-1'),
1197 'nsv' => array('nsv' , 'ISO-8859-1'),
1198 'ogg' => array('vorbiscomment' , 'UTF-8'),
1199 'png' => array('png' , 'UTF-8'),
1200 'tiff' => array('tiff' , 'ISO-8859-1'),
1201 'quicktime' => array('quicktime' , 'UTF-8'),
1202 'real' => array('real' , 'ISO-8859-1'),
1203 'vqf' => array('vqf' , 'ISO-8859-1'),
1204 'zip' => array('zip' , 'ISO-8859-1'),
1205 'riff' => array('riff' , 'ISO-8859-1'),
1206 'lyrics3' => array('lyrics3' , 'ISO-8859-1'),
1207 'id3v1' => array('id3v1' , $this->encoding_id3v1
),
1208 '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
1209 'ape' => array('ape' , 'UTF-8'),
1210 'cue' => array('cue' , 'ISO-8859-1'),
1211 'matroska' => array('matroska' , 'UTF-8'),
1212 'flac' => array('vorbiscomment' , 'UTF-8'),
1213 'divxtag' => array('divx' , 'ISO-8859-1'),
1214 'iptc' => array('iptc' , 'ISO-8859-1'),
1218 // loop through comments array
1219 foreach ($tags as $comment_name => $tagname_encoding_array) {
1220 list($tag_name, $encoding) = $tagname_encoding_array;
1222 // fill in default encoding type if not already present
1223 if (isset($this->info
[$comment_name]) && !isset($this->info
[$comment_name]['encoding'])) {
1224 $this->info
[$comment_name]['encoding'] = $encoding;
1227 // copy comments if key name set
1228 if (!empty($this->info
[$comment_name]['comments'])) {
1229 foreach ($this->info
[$comment_name]['comments'] as $tag_key => $valuearray) {
1230 foreach ($valuearray as $key => $value) {
1231 if (is_string($value)) {
1232 $value = trim($value, " \r\n\t"); // do not trim nulls from $value!! Unicode characters will get mangled if trailing nulls are removed!
1235 if (!is_numeric($key)) {
1236 $this->info
['tags'][trim($tag_name)][trim($tag_key)][$key] = $value;
1238 $this->info
['tags'][trim($tag_name)][trim($tag_key)][] = $value;
1242 if ($tag_key == 'picture') {
1243 unset($this->info
[$comment_name]['comments'][$tag_key]);
1247 if (!isset($this->info
['tags'][$tag_name])) {
1248 // comments are set but contain nothing but empty strings, so skip
1252 $this->CharConvert($this->info
['tags'][$tag_name], $this->info
[$comment_name]['encoding']); // only copy gets converted!
1254 if ($this->option_tags_html
) {
1255 foreach ($this->info
['tags'][$tag_name] as $tag_key => $valuearray) {
1256 $this->info
['tags_html'][$tag_name][$tag_key] = getid3_lib
::recursiveMultiByteCharString2HTML($valuearray, $this->info
[$comment_name]['encoding']);
1264 // pictures can take up a lot of space, and we don't need multiple copies of them
1265 // let there be a single copy in [comments][picture], and not elsewhere
1266 if (!empty($this->info
['tags'])) {
1267 $unset_keys = array('tags', 'tags_html');
1268 foreach ($this->info
['tags'] as $tagtype => $tagarray) {
1269 foreach ($tagarray as $tagname => $tagdata) {
1270 if ($tagname == 'picture') {
1271 foreach ($tagdata as $key => $tagarray) {
1272 $this->info
['comments']['picture'][] = $tagarray;
1273 if (isset($tagarray['data']) && isset($tagarray['image_mime'])) {
1274 if (isset($this->info
['tags'][$tagtype][$tagname][$key])) {
1275 unset($this->info
['tags'][$tagtype][$tagname][$key]);
1277 if (isset($this->info
['tags_html'][$tagtype][$tagname][$key])) {
1278 unset($this->info
['tags_html'][$tagtype][$tagname][$key]);
1284 foreach ($unset_keys as $unset_key) {
1285 // remove possible empty keys from (e.g. [tags][id3v2][picture])
1286 if (empty($this->info
[$unset_key][$tagtype]['picture'])) {
1287 unset($this->info
[$unset_key][$tagtype]['picture']);
1289 if (empty($this->info
[$unset_key][$tagtype])) {
1290 unset($this->info
[$unset_key][$tagtype]);
1292 if (empty($this->info
[$unset_key])) {
1293 unset($this->info
[$unset_key]);
1296 // remove duplicate copy of picture data from (e.g. [id3v2][comments][picture])
1297 if (isset($this->info
[$tagtype]['comments']['picture'])) {
1298 unset($this->info
[$tagtype]['comments']['picture']);
1300 if (empty($this->info
[$tagtype]['comments'])) {
1301 unset($this->info
[$tagtype]['comments']);
1303 if (empty($this->info
[$tagtype])) {
1304 unset($this->info
[$tagtype]);
1311 public function getHashdata($algorithm) {
1312 switch ($algorithm) {
1318 return $this->error('bad algorithm "'.$algorithm.'" in getHashdata()');
1322 if (!empty($this->info
['fileformat']) && !empty($this->info
['dataformat']) && ($this->info
['fileformat'] == 'ogg') && ($this->info
['audio']['dataformat'] == 'vorbis')) {
1324 // We cannot get an identical md5_data value for Ogg files where the comments
1325 // span more than 1 Ogg page (compared to the same audio data with smaller
1326 // comments) using the normal getID3() method of MD5'ing the data between the
1327 // end of the comments and the end of the file (minus any trailing tags),
1328 // because the page sequence numbers of the pages that the audio data is on
1329 // do not match. Under normal circumstances, where comments are smaller than
1330 // the nominal 4-8kB page size, then this is not a problem, but if there are
1331 // very large comments, the only way around it is to strip off the comment
1332 // tags with vorbiscomment and MD5 that file.
1333 // This procedure must be applied to ALL Ogg files, not just the ones with
1334 // comments larger than 1 page, because the below method simply MD5's the
1335 // whole file with the comments stripped, not just the portion after the
1336 // comments block (which is the standard getID3() method.
1338 // The above-mentioned problem of comments spanning multiple pages and changing
1339 // page sequence numbers likely happens for OggSpeex and OggFLAC as well, but
1340 // currently vorbiscomment only works on OggVorbis files.
1342 if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
1344 $this->warning('Failed making system call to vorbiscomment.exe - '.$algorithm.'_data is incorrect - error returned: PHP running in Safe Mode (backtick operator not available)');
1345 $this->info
[$algorithm.'_data'] = false;
1349 // Prevent user from aborting script
1350 $old_abort = ignore_user_abort(true);
1352 // Create empty file
1353 $empty = tempnam(GETID3_TEMP_DIR
, 'getID3');
1356 // Use vorbiscomment to make temp file without comments
1357 $temp = tempnam(GETID3_TEMP_DIR
, 'getID3');
1358 $file = $this->info
['filenamepath'];
1360 if (GETID3_OS_ISWINDOWS
) {
1362 if (file_exists(GETID3_HELPERAPPSDIR
.'vorbiscomment.exe')) {
1364 $commandline = '"'.GETID3_HELPERAPPSDIR
.'vorbiscomment.exe" -w -c "'.$empty.'" "'.$file.'" "'.$temp.'"';
1365 $VorbisCommentError = `
$commandline`
;
1369 $VorbisCommentError = 'vorbiscomment.exe not found in '.GETID3_HELPERAPPSDIR
;
1375 $commandline = 'vorbiscomment -w -c "'.$empty.'" "'.$file.'" "'.$temp.'" 2>&1';
1376 $commandline = 'vorbiscomment -w -c '.escapeshellarg($empty).' '.escapeshellarg($file).' '.escapeshellarg($temp).' 2>&1';
1377 $VorbisCommentError = `
$commandline`
;
1381 if (!empty($VorbisCommentError)) {
1383 $this->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);
1384 $this->info
[$algorithm.'_data'] = false;
1388 // Get hash of newly created file
1389 switch ($algorithm) {
1391 $this->info
[$algorithm.'_data'] = md5_file($temp);
1395 $this->info
[$algorithm.'_data'] = sha1_file($temp);
1404 // Reset abort setting
1405 ignore_user_abort($old_abort);
1411 if (!empty($this->info
['avdataoffset']) ||
(isset($this->info
['avdataend']) && ($this->info
['avdataend'] < $this->info
['filesize']))) {
1413 // get hash from part of file
1414 $this->info
[$algorithm.'_data'] = getid3_lib
::hash_data($this->info
['filenamepath'], $this->info
['avdataoffset'], $this->info
['avdataend'], $algorithm);
1418 // get hash from whole file
1419 switch ($algorithm) {
1421 $this->info
[$algorithm.'_data'] = md5_file($this->info
['filenamepath']);
1425 $this->info
[$algorithm.'_data'] = sha1_file($this->info
['filenamepath']);
1435 public function ChannelsBitratePlaytimeCalculations() {
1437 // set channelmode on audio
1438 if (!empty($this->info
['audio']['channelmode']) ||
!isset($this->info
['audio']['channels'])) {
1440 } elseif ($this->info
['audio']['channels'] == 1) {
1441 $this->info
['audio']['channelmode'] = 'mono';
1442 } elseif ($this->info
['audio']['channels'] == 2) {
1443 $this->info
['audio']['channelmode'] = 'stereo';
1446 // Calculate combined bitrate - audio + video
1447 $CombinedBitrate = 0;
1448 $CombinedBitrate +
= (isset($this->info
['audio']['bitrate']) ?
$this->info
['audio']['bitrate'] : 0);
1449 $CombinedBitrate +
= (isset($this->info
['video']['bitrate']) ?
$this->info
['video']['bitrate'] : 0);
1450 if (($CombinedBitrate > 0) && empty($this->info
['bitrate'])) {
1451 $this->info
['bitrate'] = $CombinedBitrate;
1453 //if ((isset($this->info['video']) && !isset($this->info['video']['bitrate'])) || (isset($this->info['audio']) && !isset($this->info['audio']['bitrate']))) {
1454 // // for example, VBR MPEG video files cannot determine video bitrate:
1455 // // should not set overall bitrate and playtime from audio bitrate only
1456 // unset($this->info['bitrate']);
1459 // video bitrate undetermined, but calculable
1460 if (isset($this->info
['video']['dataformat']) && $this->info
['video']['dataformat'] && (!isset($this->info
['video']['bitrate']) ||
($this->info
['video']['bitrate'] == 0))) {
1461 // if video bitrate not set
1462 if (isset($this->info
['audio']['bitrate']) && ($this->info
['audio']['bitrate'] > 0) && ($this->info
['audio']['bitrate'] == $this->info
['bitrate'])) {
1463 // AND if audio bitrate is set to same as overall bitrate
1464 if (isset($this->info
['playtime_seconds']) && ($this->info
['playtime_seconds'] > 0)) {
1465 // AND if playtime is set
1466 if (isset($this->info
['avdataend']) && isset($this->info
['avdataoffset'])) {
1467 // AND if AV data offset start/end is known
1468 // THEN we can calculate the video bitrate
1469 $this->info
['bitrate'] = round((($this->info
['avdataend'] - $this->info
['avdataoffset']) * 8) / $this->info
['playtime_seconds']);
1470 $this->info
['video']['bitrate'] = $this->info
['bitrate'] - $this->info
['audio']['bitrate'];
1476 if ((!isset($this->info
['playtime_seconds']) ||
($this->info
['playtime_seconds'] <= 0)) && !empty($this->info
['bitrate'])) {
1477 $this->info
['playtime_seconds'] = (($this->info
['avdataend'] - $this->info
['avdataoffset']) * 8) / $this->info
['bitrate'];
1480 if (!isset($this->info
['bitrate']) && !empty($this->info
['playtime_seconds'])) {
1481 $this->info
['bitrate'] = (($this->info
['avdataend'] - $this->info
['avdataoffset']) * 8) / $this->info
['playtime_seconds'];
1483 if (isset($this->info
['bitrate']) && empty($this->info
['audio']['bitrate']) && empty($this->info
['video']['bitrate'])) {
1484 if (isset($this->info
['audio']['dataformat']) && empty($this->info
['video']['resolution_x'])) {
1486 $this->info
['audio']['bitrate'] = $this->info
['bitrate'];
1487 } elseif (isset($this->info
['video']['resolution_x']) && empty($this->info
['audio']['dataformat'])) {
1489 $this->info
['video']['bitrate'] = $this->info
['bitrate'];
1493 // Set playtime string
1494 if (!empty($this->info
['playtime_seconds']) && empty($this->info
['playtime_string'])) {
1495 $this->info
['playtime_string'] = getid3_lib
::PlaytimeString($this->info
['playtime_seconds']);
1500 public function CalculateCompressionRatioVideo() {
1501 if (empty($this->info
['video'])) {
1504 if (empty($this->info
['video']['resolution_x']) ||
empty($this->info
['video']['resolution_y'])) {
1507 if (empty($this->info
['video']['bits_per_sample'])) {
1511 switch ($this->info
['video']['dataformat']) {
1519 $PlaytimeSeconds = 1;
1520 $BitrateCompressed = $this->info
['filesize'] * 8;
1524 if (!empty($this->info
['video']['frame_rate'])) {
1525 $FrameRate = $this->info
['video']['frame_rate'];
1529 if (!empty($this->info
['playtime_seconds'])) {
1530 $PlaytimeSeconds = $this->info
['playtime_seconds'];
1534 if (!empty($this->info
['video']['bitrate'])) {
1535 $BitrateCompressed = $this->info
['video']['bitrate'];
1541 $BitrateUncompressed = $this->info
['video']['resolution_x'] * $this->info
['video']['resolution_y'] * $this->info
['video']['bits_per_sample'] * $FrameRate;
1543 $this->info
['video']['compression_ratio'] = $BitrateCompressed / $BitrateUncompressed;
1548 public function CalculateCompressionRatioAudio() {
1549 if (empty($this->info
['audio']['bitrate']) ||
empty($this->info
['audio']['channels']) ||
empty($this->info
['audio']['sample_rate']) ||
!is_numeric($this->info
['audio']['sample_rate'])) {
1552 $this->info
['audio']['compression_ratio'] = $this->info
['audio']['bitrate'] / ($this->info
['audio']['channels'] * $this->info
['audio']['sample_rate'] * (!empty($this->info
['audio']['bits_per_sample']) ?
$this->info
['audio']['bits_per_sample'] : 16));
1554 if (!empty($this->info
['audio']['streams'])) {
1555 foreach ($this->info
['audio']['streams'] as $streamnumber => $streamdata) {
1556 if (!empty($streamdata['bitrate']) && !empty($streamdata['channels']) && !empty($streamdata['sample_rate'])) {
1557 $this->info
['audio']['streams'][$streamnumber]['compression_ratio'] = $streamdata['bitrate'] / ($streamdata['channels'] * $streamdata['sample_rate'] * (!empty($streamdata['bits_per_sample']) ?
$streamdata['bits_per_sample'] : 16));
1565 public function CalculateReplayGain() {
1566 if (isset($this->info
['replay_gain'])) {
1567 if (!isset($this->info
['replay_gain']['reference_volume'])) {
1568 $this->info
['replay_gain']['reference_volume'] = (double) 89.0;
1570 if (isset($this->info
['replay_gain']['track']['adjustment'])) {
1571 $this->info
['replay_gain']['track']['volume'] = $this->info
['replay_gain']['reference_volume'] - $this->info
['replay_gain']['track']['adjustment'];
1573 if (isset($this->info
['replay_gain']['album']['adjustment'])) {
1574 $this->info
['replay_gain']['album']['volume'] = $this->info
['replay_gain']['reference_volume'] - $this->info
['replay_gain']['album']['adjustment'];
1577 if (isset($this->info
['replay_gain']['track']['peak'])) {
1578 $this->info
['replay_gain']['track']['max_noclip_gain'] = 0 - getid3_lib
::RGADamplitude2dB($this->info
['replay_gain']['track']['peak']);
1580 if (isset($this->info
['replay_gain']['album']['peak'])) {
1581 $this->info
['replay_gain']['album']['max_noclip_gain'] = 0 - getid3_lib
::RGADamplitude2dB($this->info
['replay_gain']['album']['peak']);
1587 public function ProcessAudioStreams() {
1588 if (!empty($this->info
['audio']['bitrate']) ||
!empty($this->info
['audio']['channels']) ||
!empty($this->info
['audio']['sample_rate'])) {
1589 if (!isset($this->info
['audio']['streams'])) {
1590 foreach ($this->info
['audio'] as $key => $value) {
1591 if ($key != 'streams') {
1592 $this->info
['audio']['streams'][0][$key] = $value;
1600 public function getid3_tempnam() {
1601 return tempnam($this->tempdir
, 'gI3');
1604 public function include_module($name) {
1605 //if (!file_exists($this->include_path.'module.'.$name.'.php')) {
1606 if (!file_exists(GETID3_INCLUDEPATH
.'module.'.$name.'.php')) {
1607 throw new getid3_exception('Required module.'.$name.'.php is missing.');
1609 include_once(GETID3_INCLUDEPATH
.'module.'.$name.'.php');
1616 abstract class getid3_handler
{
1621 protected $getid3; // pointer
1623 protected $data_string_flag = false; // analyzing filepointer or string
1624 protected $data_string = ''; // string to analyze
1625 protected $data_string_position = 0; // seek position in string
1626 protected $data_string_length = 0; // string length
1628 private $dependency_to = null;
1631 public function __construct(getID3
$getid3, $call_module=null) {
1632 $this->getid3
= $getid3;
1635 $this->dependency_to
= str_replace('getid3_', '', $call_module);
1640 // Analyze from file pointer
1641 abstract public function Analyze();
1644 // Analyze from string instead
1645 public function AnalyzeString($string) {
1646 // Enter string mode
1647 $this->setStringMode($string);
1650 $saved_avdataoffset = $this->getid3
->info
['avdataoffset'];
1651 $saved_avdataend = $this->getid3
->info
['avdataend'];
1652 $saved_filesize = (isset($this->getid3
->info
['filesize']) ?
$this->getid3
->info
['filesize'] : null); // may be not set if called as dependency without openfile() call
1655 $this->getid3
->info
['avdataoffset'] = 0;
1656 $this->getid3
->info
['avdataend'] = $this->getid3
->info
['filesize'] = $this->data_string_length
;
1661 // Restore some info
1662 $this->getid3
->info
['avdataoffset'] = $saved_avdataoffset;
1663 $this->getid3
->info
['avdataend'] = $saved_avdataend;
1664 $this->getid3
->info
['filesize'] = $saved_filesize;
1667 $this->data_string_flag
= false;
1670 public function setStringMode($string) {
1671 $this->data_string_flag
= true;
1672 $this->data_string
= $string;
1673 $this->data_string_length
= strlen($string);
1676 protected function ftell() {
1677 if ($this->data_string_flag
) {
1678 return $this->data_string_position
;
1680 return ftell($this->getid3
->fp
);
1683 protected function fread($bytes) {
1684 if ($this->data_string_flag
) {
1685 $this->data_string_position +
= $bytes;
1686 return substr($this->data_string
, $this->data_string_position
- $bytes, $bytes);
1688 $pos = $this->ftell() +
$bytes;
1689 if (!getid3_lib
::intValueSupported($pos)) {
1690 throw new getid3_exception('cannot fread('.$bytes.' from '.$this->ftell().') because beyond PHP filesystem limit', 10);
1693 //return fread($this->getid3->fp, $bytes);
1695 * http://www.getid3.org/phpBB3/viewtopic.php?t=1930
1696 * "I found out that the root cause for the problem was how getID3 uses the PHP system function fread().
1697 * It seems to assume that fread() would always return as many bytes as were requested.
1698 * However, according the PHP manual (http://php.net/manual/en/function.fread.php), this is the case only with regular local files, but not e.g. with Linux pipes.
1699 * The call may return only part of the requested data and a new call is needed to get more."
1703 $part = fread($this->getid3
->fp
, $bytes);
1704 $partLength = strlen($part);
1705 $bytes -= $partLength;
1707 } while (($bytes > 0) && ($partLength > 0));
1711 protected function fseek($bytes, $whence=SEEK_SET
) {
1712 if ($this->data_string_flag
) {
1715 $this->data_string_position
= $bytes;
1719 $this->data_string_position +
= $bytes;
1723 $this->data_string_position
= $this->data_string_length +
$bytes;
1729 if ($whence == SEEK_CUR
) {
1730 $pos = $this->ftell() +
$bytes;
1731 } elseif ($whence == SEEK_END
) {
1732 $pos = $this->getid3
->info
['filesize'] +
$bytes;
1734 if (!getid3_lib
::intValueSupported($pos)) {
1735 throw new getid3_exception('cannot fseek('.$pos.') because beyond PHP filesystem limit', 10);
1738 return fseek($this->getid3
->fp
, $bytes, $whence);
1741 protected function feof() {
1742 if ($this->data_string_flag
) {
1743 return $this->data_string_position
>= $this->data_string_length
;
1745 return feof($this->getid3
->fp
);
1748 final protected function isDependencyFor($module) {
1749 return $this->dependency_to
== $module;
1752 protected function error($text) {
1753 $this->getid3
->info
['error'][] = $text;
1758 protected function warning($text) {
1759 return $this->getid3
->warning($text);
1762 protected function notice($text) {
1763 // does nothing for now
1766 public function saveAttachment($name, $offset, $length, $image_mime=null) {
1769 // do not extract at all
1770 if ($this->getid3
->option_save_attachments
=== getID3
::ATTACHMENTS_NONE
) {
1772 $attachment = null; // do not set any
1774 // extract to return array
1775 } elseif ($this->getid3
->option_save_attachments
=== getID3
::ATTACHMENTS_INLINE
) {
1777 $this->fseek($offset);
1778 $attachment = $this->fread($length); // get whole data in one pass, till it is anyway stored in memory
1779 if ($attachment === false ||
strlen($attachment) != $length) {
1780 throw new Exception('failed to read attachment data');
1783 // assume directory path is given
1786 // set up destination path
1787 $dir = rtrim(str_replace(array('/', '\\'), DIRECTORY_SEPARATOR
, $this->getid3
->option_save_attachments
), DIRECTORY_SEPARATOR
);
1788 if (!is_dir($dir) ||
!is_writable($dir)) { // check supplied directory
1789 throw new Exception('supplied path ('.$dir.') does not exist, or is not writable');
1791 $dest = $dir.DIRECTORY_SEPARATOR
.$name.($image_mime ?
'.'.getid3_lib
::ImageExtFromMime($image_mime) : '');
1794 if (($fp_dest = fopen($dest, 'wb')) == false) {
1795 throw new Exception('failed to create file '.$dest);
1799 $this->fseek($offset);
1800 $buffersize = ($this->data_string_flag ?
$length : $this->getid3
->fread_buffer_size());
1801 $bytesleft = $length;
1802 while ($bytesleft > 0) {
1803 if (($buffer = $this->fread(min($buffersize, $bytesleft))) === false ||
($byteswritten = fwrite($fp_dest, $buffer)) === false ||
($byteswritten === 0)) {
1804 throw new Exception($buffer === false ?
'not enough data to read' : 'failed to write to destination file, may be not enough disk space');
1806 $bytesleft -= $byteswritten;
1810 $attachment = $dest;
1814 } catch (Exception
$e) {
1816 // close and remove dest file if created
1817 if (isset($fp_dest) && is_resource($fp_dest)) {
1822 // do not set any is case of error
1824 $this->warning('Failed to extract attachment '.$name.': '.$e->getMessage());
1828 // seek to the end of attachment
1829 $this->fseek($offset +
$length);
1837 class getid3_exception
extends Exception