2
/////////////////////////////////////////////////////////////////
3
/// getID3() by James Heinrich <info@getid3.org> //
4
// available at http://getid3.sourceforge.net //
5
// or http://www.getid3.org //
6
/////////////////////////////////////////////////////////////////
8
// Please see readme.txt for more information //
10
/////////////////////////////////////////////////////////////////
13
define('GETID3_VERSION', '1.7.5-200512251515');
14
define('GETID3_FREAD_BUFFER_SIZE', 16384); // read buffer size in bytes
21
var $encoding = 'ISO-8859-1'; // CASE SENSITIVE! - i.e. (must be supported by iconv())
22
// Examples: ISO-8859-1 UTF-8 UTF-16 UTF-16BE
24
var $encoding_id3v1 = 'ISO-8859-1'; // Should always be 'ISO-8859-1', but some tags may be written
25
// in other encodings such as 'EUC-CN'
27
// public: Optional tag checks - disable for speed.
28
var $option_tag_id3v1 = true; // Read and process ID3v1 tags
29
var $option_tag_id3v2 = true; // Read and process ID3v2 tags
30
var $option_tag_lyrics3 = true; // Read and process Lyrics3 tags
31
var $option_tag_apetag = true; // Read and process APE tags
32
var $option_tags_process = true; // Copy tags to root key 'tags' and encode to $this->encoding
33
var $option_tags_html = true; // Copy tags to root key 'tags_html' properly translated from various encodings to HTML entities
35
// public: Optional tag/comment calucations
36
var $option_extra_info = true; // Calculate additional info such as bitrate, channelmode etc
38
// public: Optional calculations
39
var $option_md5_data = false; // Get MD5 sum of data part - slow
40
var $option_md5_data_source = false; // Use MD5 of source file if availble - only FLAC and OptimFROG
41
var $option_sha1_data = false; // Get SHA1 sum of data part - slow
42
var $option_max_2gb_check = true; // Check whether file is larger than 2 Gb and thus not supported by PHP
48
// public: constructor
52
$this->startup_error = '';
53
$this->startup_warning = '';
55
// Check for PHP version >= 4.1.0
56
if (phpversion() < '4.1.0') {
57
$this->startup_error .= 'getID3() requires PHP v4.1.0 or higher - you are running v'.phpversion();
61
$memory_limit = ini_get('memory_limit');
62
if (eregi('([0-9]+)M', $memory_limit, $matches)) {
63
// could be stored as "16M" rather than 16777216 for example
64
$memory_limit = $matches[1] * 1048576;
66
if ($memory_limit <= 0) {
67
// memory limits probably disabled
68
} elseif ($memory_limit <= 3145728) {
69
$this->startup_error .= 'PHP has less than 3MB available memory and will very likely run out. Increase memory_limit in php.ini';
70
} elseif ($memory_limit <= 12582912) {
71
$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';
74
// Check safe_mode off
75
if ((bool) ini_get('safe_mode')) {
76
$this->warning('WARNING: Safe mode is on, shorten support disabled, md5data/sha1data for ogg vorbis disabled, ogg vorbos/flac tag writing disabled.');
80
// define a constant rather than looking up every time it is needed
81
if (!defined('GETID3_OS_ISWINDOWS')) {
82
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
83
define('GETID3_OS_ISWINDOWS', true);
85
define('GETID3_OS_ISWINDOWS', false);
89
// Get base path of getID3() - ONCE
90
if (!defined('GETID3_INCLUDEPATH')) {
91
define('GETID3_OS_DIRSLASH', (GETID3_OS_ISWINDOWS ? '\\' : '/'));
93
foreach (get_included_files() as $key => $val) {
94
if (basename($val) == 'getid3.inc') {
95
define('GETID3_INCLUDEPATH', dirname($val).GETID3_OS_DIRSLASH);
101
// Load support library
102
if (!include_once(GETID3_INCLUDEPATH.'getid3.lib.inc')) {
103
$this->startup_error .= 'getid3.lib.inc is missing or corrupt';
107
// Needed for Windows only:
108
// Define locations of helper applications for Shorten, VorbisComment, MetaFLAC
109
// as well as other helper functions such as head, tail, md5sum, etc
110
// IMPORTANT: This path cannot have spaces in it. If neccesary, use the 8dot3 equivalent
111
// ie for "C:/Program Files/Apache/" put "C:/PROGRA~1/APACHE/"
112
// IMPORTANT: This path must include the trailing slash
113
if (GETID3_OS_ISWINDOWS && !defined('GETID3_HELPERAPPSDIR')) {
114
$helperappsdir = GETID3_INCLUDEPATH.'..'.GETID3_OS_DIRSLASH.'helperapps'; // must not have any space in this path
116
if (!is_dir($helperappsdir)) {
117
$this->startup_error .= '"'.$helperappsdir.'" cannot be defined as GETID3_HELPERAPPSDIR because it does not exist';
118
} elseif (strpos(realpath($helperappsdir), ' ') !== false) {
119
$DirPieces = explode(GETID3_OS_DIRSLASH, realpath($helperappsdir));
120
$DirPieces8 = $DirPieces;
122
$CLIdir = $DirPieces[0].' && cd \\';
123
for ($i = 1; $i < count($DirPieces); $i++) {
124
if (strpos($DirPieces[$i], ' ') === false) {
125
$CLIdir .= ' && cd '.$DirPieces[$i];
128
system($CLIdir.' && dir /ad /x');
129
$subdirsraw = explode("\n", ob_get_contents());
131
foreach ($subdirsraw as $line) {
132
if (eregi('^[0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2} [AP]M <DIR> ([^ ]{8}) '.preg_quote($DirPieces[$i]).'$', trim($line), $matches)) {
133
$CLIdir .= ' && cd '.$matches[1];
137
$DirPieces8[$i] = $matches[1];
140
$helperappsdir = implode(GETID3_OS_DIRSLASH, $DirPieces8);
143
define('GETID3_HELPERAPPSDIR', realpath($helperappsdir).GETID3_OS_DIRSLASH);
151
// public: analyze file - replaces GetAllFileInfo() and GetTagOnly()
152
function analyze($filename) {
154
if (!empty($this->startup_error)) {
155
return $this->error($this->startup_error);
157
if (!empty($this->startup_warning)) {
158
$this->warning($this->startup_warning);
161
// init result array and set parameters
162
$this->info = array();
163
$this->info['GETID3_VERSION'] = GETID3_VERSION;
165
// Check encoding/iconv support
166
if (!function_exists('iconv') && !in_array($this->encoding, array('ISO-8859-1', 'UTF-8', 'UTF-16LE', 'UTF-16BE', 'UTF-16'))) {
167
$errormessage = 'iconv() support is needed for encodings other than ISO-8859-1, UTF-8, UTF-16LE, UTF16-BE, UTF-16. ';
168
if (GETID3_OS_ISWINDOWS) {
169
$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';
171
$errormessage .= 'PHP is not compiled with iconv() support. Please recompile with the --with-iconv switch';
173
return $this->error($errormessage);
176
// Disable magic_quotes_runtime, if neccesary
177
$old_magic_quotes_runtime = get_magic_quotes_runtime(); // store current setting of magic_quotes_runtime
178
if ($old_magic_quotes_runtime) {
179
set_magic_quotes_runtime(0); // turn off magic_quotes_runtime
180
if (get_magic_quotes_runtime()) {
181
return $this->error('Could not disable magic_quotes_runtime - getID3() cannot work properly with this setting enabled');
185
// remote files not supported
186
if (preg_match('/^(ht|f)tp:\/\//', $filename)) {
187
return $this->error('Remote files are not supported in this version of getID3() - please copy the file locally first');
191
if (!$fp = @fopen($filename, 'rb')) {
192
return $this->error('Could not open file "'.$filename.'"');
196
$this->info['filesize'] = filesize($filename);
198
// option_max_2gb_check
199
if ($this->option_max_2gb_check) {
200
// PHP doesn't support integers larger than 31-bit (~2GB)
201
// filesize() simply returns (filesize % (pow(2, 32)), no matter the actual filesize
202
// ftell() returns 0 if seeking to the end is beyond the range of unsigned integer
203
fseek($fp, 0, SEEK_END);
204
if ((($this->info['filesize'] != 0) && (ftell($fp) == 0)) ||
205
($this->info['filesize'] < 0) ||
207
unset($this->info['filesize']);
209
return $this->error('File is most likely larger than 2GB and is not supported by PHP');
213
// set more parameters
214
$this->info['avdataoffset'] = 0;
215
$this->info['avdataend'] = $this->info['filesize'];
216
$this->info['fileformat'] = ''; // filled in later
217
$this->info['audio']['dataformat'] = ''; // filled in later, unset if not used
218
$this->info['video']['dataformat'] = ''; // filled in later, unset if not used
219
$this->info['tags'] = array(); // filled in later, unset if not used
220
$this->info['error'] = array(); // filled in later, unset if not used
221
$this->info['warning'] = array(); // filled in later, unset if not used
222
$this->info['comments'] = array(); // filled in later, unset if not used
223
$this->info['encoding'] = $this->encoding; // required by id3v2 and iso modules - can be unset at the end if desired
225
// set redundant parameters - might be needed in some include file
226
$this->info['filename'] = basename($filename);
227
$this->info['filepath'] = str_replace('\\', '/', realpath(dirname($filename)));
228
$this->info['filenamepath'] = $this->info['filepath'].'/'.$this->info['filename'];
231
// handle ID3v2 tag - done first - already at beginning of file
232
// ID3v2 detection (even if not parsing) is always done otherwise fileformat is much harder to detect
233
if ($this->option_tag_id3v2) {
235
$GETID3_ERRORARRAY = &$this->info['warning'];
236
if (getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.id3v2.inc', __FILE__, false)) {
237
$tag = new getid3_id3v2($fp, $this->info);
242
fseek($fp, 0, SEEK_SET);
243
$header = fread($fp, 10);
244
if (substr($header, 0, 3) == 'ID3') {
245
$this->info['id3v2']['header'] = true;
246
$this->info['id3v2']['majorversion'] = ord($header{3});
247
$this->info['id3v2']['minorversion'] = ord($header{4});
248
$this->info['id3v2']['headerlength'] = getid3_lib::BigEndian2Int(substr($header, 6, 4), 1) + 10; // length of ID3v2 tag in 10-byte header doesn't include 10-byte header length
250
$this->info['id3v2']['tag_offset_start'] = 0;
251
$this->info['id3v2']['tag_offset_end'] = $this->info['id3v2']['tag_offset_start'] + $this->info['id3v2']['headerlength'];
252
$this->info['avdataoffset'] = $this->info['id3v2']['tag_offset_end'];
259
if ($this->option_tag_id3v1) {
260
if (!@include_once(GETID3_INCLUDEPATH.'module.tag.id3v1.inc')) {
261
return $this->error('module.tag.id3v1.inc is missing - you may disable option_tag_id3v1.');
263
$tag = new getid3_id3v1($fp, $this->info);
267
if ($this->option_tag_apetag) {
268
if (!@include_once(GETID3_INCLUDEPATH.'module.tag.apetag.inc')) {
269
return $this->error('module.tag.apetag.inc is missing - you may disable option_tag_apetag.');
271
$tag = new getid3_apetag($fp, $this->info);
274
// handle lyrics3 tag
275
if ($this->option_tag_lyrics3) {
276
if (!@include_once(GETID3_INCLUDEPATH.'module.tag.lyrics3.inc')) {
277
return $this->error('module.tag.lyrics3.inc is missing - you may disable option_tag_lyrics3.');
279
$tag = new getid3_lyrics3($fp, $this->info);
282
// read 32 kb file data
283
fseek($fp, $this->info['avdataoffset'], SEEK_SET);
284
$formattest = fread($fp, 32774);
287
$determined_format = $this->GetFileFormat($formattest, $filename);
289
// unable to determine file format
290
if (!$determined_format) {
292
return $this->error('unable to determine file format');
295
// check for illegal ID3 tags
296
if (isset($determined_format['fail_id3']) && (in_array('id3v1', $this->info['tags']) || in_array('id3v2', $this->info['tags']))) {
297
if ($determined_format['fail_id3'] === 'ERROR') {
299
return $this->error('ID3 tags not allowed on this file type.');
300
} elseif ($determined_format['fail_id3'] === 'WARNING') {
301
$this->info['warning'][] = 'ID3 tags not allowed on this file type.';
305
// check for illegal APE tags
306
if (isset($determined_format['fail_ape']) && in_array('ape', $this->info['tags'])) {
307
if ($determined_format['fail_ape'] === 'ERROR') {
309
return $this->error('APE tags not allowed on this file type.');
310
} elseif ($determined_format['fail_ape'] === 'WARNING') {
311
$this->info['warning'][] = 'APE tags not allowed on this file type.';
316
$this->info['mime_type'] = $determined_format['mime_type'];
318
// supported format signature pattern detected, but module deleted
319
if (!file_exists(GETID3_INCLUDEPATH.$determined_format['include'])) {
321
return $this->error('Format not supported, module, '.$determined_format['include'].', was removed.');
324
// module requires iconv support
325
if (!function_exists('iconv') &&
326
isset($determined_format['iconv_req']) &&
327
$determined_format['iconv_req']) {
328
return $this->error('iconv support is required for this module ('.$determined_format['include'].').');
332
include_once(GETID3_INCLUDEPATH.$determined_format['include']);
334
// instantiate module class
335
$class_name = 'getid3_'.$determined_format['module'];
336
if (!class_exists($class_name)) {
337
return $this->error('Format not supported, module, '.$determined_format['include'].', is corrupt.');
339
if (isset($determined_format['option'])) {
340
$class = new $class_name($fp, $this->info, $determined_format['option']);
342
$class = new $class_name($fp, $this->info);
348
// process all tags - copy to 'tags' and convert charsets
349
if ($this->option_tags_process) {
350
$this->HandleAllTags();
353
// perform more calculations
354
if ($this->option_extra_info) {
355
$this->ChannelsBitratePlaytimeCalculations();
356
$this->CalculateCompressionRatioVideo();
357
$this->CalculateCompressionRatioAudio();
358
$this->CalculateReplayGain();
359
$this->ProcessAudioStreams();
362
// get the MD5 sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags
363
if ($this->option_md5_data) {
364
// do not cald md5_data if md5_data_source is present - set by flac only - future MPC/SV8 too
365
if (!$this->option_md5_data_source || empty($this->info['md5_data_source'])) {
366
$this->getHashdata('md5');
370
// get the SHA1 sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags
371
if ($this->option_sha1_data) {
372
$this->getHashdata('sha1');
375
// remove undesired keys
378
// restore magic_quotes_runtime setting
379
set_magic_quotes_runtime($old_magic_quotes_runtime);
386
// private: error handling
387
function error($message) {
391
$this->info['error'][] = $message;
396
// private: warning handling
397
function warning($message) {
398
$this->info['warning'][] = $message;
406
// remove possible empty keys
407
$AVpossibleEmptyKeys = array('dataformat', 'bits_per_sample', 'encoder_options', 'streams');
408
foreach ($AVpossibleEmptyKeys as $key) {
409
if (empty($this->info['audio'][$key]) && isset($this->info['audio'][$key])) {
410
unset($this->info['audio'][$key]);
412
if (empty($this->info['video'][$key]) && isset($this->info['video'][$key])) {
413
unset($this->info['video'][$key]);
417
// remove empty root keys
418
if (!empty($this->info)) {
419
foreach ($this->info as $key => $value) {
420
if (empty($this->info[$key]) && ($this->info[$key] !== 0) && ($this->info[$key] !== '0')) {
421
unset($this->info[$key]);
426
// remove meaningless entries from unknown-format files
427
if (empty($this->info['fileformat'])) {
428
if (isset($this->info['avdataoffset'])) {
429
unset($this->info['avdataoffset']);
431
if (isset($this->info['avdataend'])) {
432
unset($this->info['avdataend']);
438
// return array containing information about all supported formats
439
function GetFileFormatArray() {
440
static $format_info = array();
441
if (empty($format_info)) {
442
$format_info = array(
446
// AC-3 - audio - Dolby AC-3 / Dolby Digital
448
'pattern' => '^\x0B\x77',
451
'mime_type' => 'audio/ac3',
454
// AAC - audio - Advanced Audio Coding (AAC) - ADIF format
456
'pattern' => '^ADIF',
460
'mime_type' => 'application/octet-stream',
461
'fail_ape' => 'WARNING',
465
// AAC - audio - Advanced Audio Coding (AAC) - ADTS format (very similar to MP3)
467
'pattern' => '^\xFF[\xF0-\xF1\xF8-\xF9]',
471
'mime_type' => 'application/octet-stream',
472
'fail_ape' => 'WARNING',
476
// AU - audio - NeXT/Sun AUdio (AU)
478
'pattern' => '^\.snd',
481
'mime_type' => 'audio/basic',
484
// AVR - audio - Audio Visual Research
486
'pattern' => '^2BIT',
489
'mime_type' => 'application/octet-stream',
492
// BONK - audio - Bonk v0.9+
494
'pattern' => '^\x00(BONK|INFO|META| ID3)',
497
'mime_type' => 'audio/xmms-bonk',
500
// FLAC - audio - Free Lossless Audio Codec
502
'pattern' => '^fLaC',
505
'mime_type' => 'audio/x-flac',
508
// LA - audio - Lossless Audio (LA)
510
'pattern' => '^LA0[2-4]',
513
'mime_type' => 'application/octet-stream',
516
// LPAC - audio - Lossless Predictive Audio Compression (LPAC)
518
'pattern' => '^LPAC',
521
'mime_type' => 'application/octet-stream',
524
// MIDI - audio - MIDI (Musical Instrument Digital Interface)
526
'pattern' => '^MThd',
529
'mime_type' => 'audio/midi',
532
// MAC - audio - Monkey's Audio Compressor
534
'pattern' => '^MAC ',
536
'module' => 'monkey',
537
'mime_type' => 'application/octet-stream',
540
// MOD - audio - MODule (assorted sub-formats)
542
'pattern' => '^.{1080}(M.K.|[5-9]CHN|[1-3][0-9]CH)',
546
'mime_type' => 'audio/mod',
549
// MOD - audio - MODule (Impulse Tracker)
551
'pattern' => '^IMPM',
555
'mime_type' => 'audio/it',
558
// MOD - audio - MODule (eXtended Module, various sub-formats)
560
'pattern' => '^Extended Module',
564
'mime_type' => 'audio/xm',
567
// MOD - audio - MODule (ScreamTracker)
569
'pattern' => '^.{44}SCRM',
573
'mime_type' => 'audio/s3m',
576
// MPC - audio - Musepack / MPEGplus
578
'pattern' => '^(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])',
581
'mime_type' => 'application/octet-stream',
584
// MP3 - audio - MPEG-audio Layer 3 (very similar to AAC-ADTS)
586
'pattern' => '^\xFF[\xE2-\xE7\xF2-\xF7\xFA-\xFF][\x00-\xEB]',
589
'mime_type' => 'audio/mpeg',
592
// OFR - audio - OptimFROG
594
'pattern' => '^(\*RIFF|OFR)',
596
'module' => 'optimfrog',
597
'mime_type' => 'application/octet-stream',
600
// RKAU - audio - RKive AUdio compressor
605
'mime_type' => 'application/octet-stream',
608
// SHN - audio - Shorten
610
'pattern' => '^ajkg',
612
'module' => 'shorten',
613
'mime_type' => 'audio/xmms-shn',
614
'fail_id3' => 'ERROR',
615
'fail_ape' => 'ERROR',
618
// TTA - audio - TTA Lossless Audio Compressor (http://tta.corecodec.org)
620
'pattern' => '^TTA', // could also be '^TTA(\x01|\x02|\x03|2|1)'
623
'mime_type' => 'application/octet-stream',
626
// VOC - audio - Creative Voice (VOC)
628
'pattern' => '^Creative Voice File',
631
'mime_type' => 'audio/voc',
634
// VQF - audio - transform-domain weighted interleave Vector Quantization Format (VQF)
636
'pattern' => '^TWIN',
639
'mime_type' => 'application/octet-stream',
642
// WV - audio - WavPack (v4.0+)
644
'pattern' => '^wvpk',
646
'module' => 'wavpack',
647
'mime_type' => 'application/octet-stream',
651
// Audio-Video formats
653
// ASF - audio/video - Advanced Streaming Format, Windows Media Video, Windows Media Audio
655
'pattern' => '^\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C',
656
'group' => 'audio-video',
658
'mime_type' => 'video/x-ms-asf',
659
'iconv_req' => false,
662
// BINK - audio/video - Bink / Smacker
664
'pattern' => '^(BIK|SMK)',
665
'group' => 'audio-video',
667
'mime_type' => 'application/octet-stream',
670
// FLV - audio/video - FLash Video
672
'pattern' => '^FLV\x01',
673
'group' => 'audio-video',
675
'mime_type' => 'video/x-flv',
678
// MKAV - audio/video - Mastroka
680
'pattern' => '^\x1A\x45\xDF\xA3',
681
'group' => 'audio-video',
682
'module' => 'matroska',
683
'mime_type' => 'application/octet-stream',
686
// MPEG - audio/video - MPEG (Moving Pictures Experts Group)
688
'pattern' => '^\x00\x00\x01(\xBA|\xB3)',
689
'group' => 'audio-video',
691
'mime_type' => 'video/mpeg',
694
// NSV - audio/video - Nullsoft Streaming Video (NSV)
696
'pattern' => '^NSV[sf]',
697
'group' => 'audio-video',
699
'mime_type' => 'application/octet-stream',
702
// Ogg - audio/video - Ogg (Ogg-Vorbis, Ogg-FLAC, Speex, Ogg-Theora(*), Ogg-Tarkin(*))
704
'pattern' => '^OggS',
707
'mime_type' => 'application/ogg',
708
'fail_id3' => 'WARNING',
709
'fail_ape' => 'WARNING',
712
// QT - audio/video - Quicktime
713
'quicktime' => array(
714
'pattern' => '^.{4}(cmov|free|ftyp|mdat|moov|pnot|skip|wide)',
715
'group' => 'audio-video',
716
'module' => 'quicktime',
717
'mime_type' => 'video/quicktime',
720
// 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)
722
'pattern' => '^(RIFF|SDSS|FORM)',
723
'group' => 'audio-video',
725
'mime_type' => 'audio/x-wave',
726
'fail_ape' => 'WARNING',
729
// Real - audio/video - RealAudio, RealVideo
731
'pattern' => '^(\.RMF|.ra)',
732
'group' => 'audio-video',
734
'mime_type' => 'audio/x-realaudio',
737
// SWF - audio/video - ShockWave Flash
739
'pattern' => '^(F|C)WS',
740
'group' => 'audio-video',
742
'mime_type' => 'application/x-shockwave-flash',
746
// Still-Image formats
748
// BMP - still image - Bitmap (Windows, OS/2; uncompressed, RLE8, RLE4)
751
'group' => 'graphic',
753
'mime_type' => 'image/bmp',
754
'fail_id3' => 'ERROR',
755
'fail_ape' => 'ERROR',
758
// GIF - still image - Graphics Interchange Format
761
'group' => 'graphic',
763
'mime_type' => 'image/gif',
764
'fail_id3' => 'ERROR',
765
'fail_ape' => 'ERROR',
768
// JPEG - still image - Joint Photographic Experts Group (JPEG)
770
'pattern' => '^\xFF\xD8\xFF',
771
'group' => 'graphic',
773
'mime_type' => 'image/jpeg',
774
'fail_id3' => 'ERROR',
775
'fail_ape' => 'ERROR',
778
// PCD - still image - Kodak Photo CD
780
'pattern' => '^.{2048}PCD_IPI\x00',
781
'group' => 'graphic',
783
'mime_type' => 'image/x-photo-cd',
784
'fail_id3' => 'ERROR',
785
'fail_ape' => 'ERROR',
789
// PNG - still image - Portable Network Graphics (PNG)
791
'pattern' => '^\x89\x50\x4E\x47\x0D\x0A\x1A\x0A',
792
'group' => 'graphic',
794
'mime_type' => 'image/png',
795
'fail_id3' => 'ERROR',
796
'fail_ape' => 'ERROR',
800
// TIFF - still image - Tagged Information File Format (TIFF)
802
'pattern' => '^(II\x2A\x00|MM\x00\x2A)',
803
'group' => 'graphic',
805
'mime_type' => 'image/tiff',
806
'fail_id3' => 'ERROR',
807
'fail_ape' => 'ERROR',
813
// ISO - data - International Standards Organization (ISO) CD-ROM Image
815
'pattern' => '^.{32769}CD001',
818
'mime_type' => 'application/octet-stream',
819
'fail_id3' => 'ERROR',
820
'fail_ape' => 'ERROR',
821
'iconv_req' => false,
824
// RAR - data - RAR compressed data
826
'pattern' => '^Rar\!',
827
'group' => 'archive',
829
'mime_type' => 'application/octet-stream',
830
'fail_id3' => 'ERROR',
831
'fail_ape' => 'ERROR',
834
// SZIP - audio - SZIP compressed data
836
'pattern' => '^SZ\x0A\x04',
837
'group' => 'archive',
839
'mime_type' => 'application/octet-stream',
840
'fail_id3' => 'ERROR',
841
'fail_ape' => 'ERROR',
844
// TAR - data - TAR compressed data
846
'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}',
847
'group' => 'archive',
849
'mime_type' => 'application/x-tar',
850
'fail_id3' => 'ERROR',
851
'fail_ape' => 'ERROR',
854
// GZIP - data - GZIP compressed data
856
'pattern' => '^\x1F\x8B\x08',
857
'group' => 'archive',
859
'mime_type' => 'application/x-gzip',
860
'fail_id3' => 'ERROR',
861
'fail_ape' => 'ERROR',
864
// ZIP - data - ZIP compressed data
866
'pattern' => '^PK\x03\x04',
867
'group' => 'archive',
869
'mime_type' => 'application/zip',
870
'fail_id3' => 'ERROR',
871
'fail_ape' => 'ERROR',
881
function GetFileFormat(&$filedata, $filename='') {
882
// this function will determine the format of a file based on usually
883
// the first 2-4 bytes of the file (8 bytes for PNG, 16 bytes for JPG,
884
// and in the case of ISO CD image, 6 bytes offset 32kb from the start
887
// Identify file format - loop through $format_info and detect with reg expr
888
foreach ($this->GetFileFormatArray() as $format_name => $info) {
889
// Using preg_match() instead of ereg() - much faster
890
// The /s switch on preg_match() forces preg_match() NOT to treat
891
// newline (0x0A) characters as special chars but do a binary match
892
if (preg_match('/'.$info['pattern'].'/s', $filedata)) {
893
$info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.inc';
899
if (preg_match('/\.mp[123a]$/i', $filename)) {
901
// Too many mp3 encoders on the market put gabage in front of mpeg files
902
// use assume format on these if format detection failed
903
$GetFileFormatArray = $this->GetFileFormatArray();
904
$info = $GetFileFormatArray['mp3'];
905
$info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.inc';
908
//} elseif (preg_match('/\.tar$/i', $filename)) {
910
// // TAR files don't have any useful header to work from
911
// // TAR - data - TAR compressed data
913
// 'pattern' => '^.{512}',
914
// 'group' => 'archive',
915
// 'module' => 'tar',
916
// 'mime_type' => 'application/octet-stream',
917
// 'fail_id3' => 'ERROR',
918
// 'fail_ape' => 'ERROR',
920
// $info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.inc';
929
// converts array to $encoding charset from $this->encoding
930
function CharConvert(&$array, $encoding) {
932
// identical encoding - end here
933
if ($encoding == $this->encoding) {
938
foreach ($array as $key => $value) {
941
if (is_array($value)) {
942
$this->CharConvert($array[$key], $encoding);
946
elseif (is_string($value)) {
947
$array[$key] = trim(getid3_lib::iconv_fallback($encoding, $this->encoding, $value));
953
function HandleAllTags() {
955
// key name => array (tag name, character encoding)
959
'asf' => array('asf' , 'UTF-16LE'),
960
'midi' => array('midi' , 'ISO-8859-1'),
961
'nsv' => array('nsv' , 'ISO-8859-1'),
962
'ogg' => array('vorbiscomment' , 'UTF-8'),
963
'png' => array('png' , 'UTF-8'),
964
'tiff' => array('tiff' , 'ISO-8859-1'),
965
'quicktime' => array('quicktime' , 'ISO-8859-1'),
966
'real' => array('real' , 'ISO-8859-1'),
967
'vqf' => array('vqf' , 'ISO-8859-1'),
968
'zip' => array('zip' , 'ISO-8859-1'),
969
'riff' => array('riff' , 'ISO-8859-1'),
970
'lyrics3' => array('lyrics3' , 'ISO-8859-1'),
971
'id3v1' => array('id3v1' , $this->encoding_id3v1),
972
'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
973
'ape' => array('ape' , 'UTF-8')
977
// loop thru comments array
978
foreach ($tags as $comment_name => $tagname_encoding_array) {
979
list($tag_name, $encoding) = $tagname_encoding_array;
981
// fill in default encoding type if not already present
982
if (isset($this->info[$comment_name]) && !isset($this->info[$comment_name]['encoding'])) {
983
$this->info[$comment_name]['encoding'] = $encoding;
986
// copy comments if key name set
987
if (!empty($this->info[$comment_name]['comments'])) {
989
foreach ($this->info[$comment_name]['comments'] as $tag_key => $valuearray) {
990
foreach ($valuearray as $key => $value) {
991
if (strlen(trim($value)) > 0) {
992
$this->info['tags'][trim($tag_name)][trim($tag_key)][] = $value; // do not trim!! Unicode characters will get mangled if trailing nulls are removed!
997
if (!isset($this->info['tags'][$tag_name])) {
998
// comments are set but contain nothing but empty strings, so skip
1002
if ($this->option_tags_html) {
1003
foreach ($this->info['tags'][$tag_name] as $tag_key => $valuearray) {
1004
foreach ($valuearray as $key => $value) {
1005
if (is_string($value)) {
1006
//$this->info['tags_html'][$tag_name][$tag_key][$key] = getid3_lib::MultiByteCharString2HTML($value, $encoding);
1007
$this->info['tags_html'][$tag_name][$tag_key][$key] = str_replace('�', '', getid3_lib::MultiByteCharString2HTML($value, $encoding));
1009
$this->info['tags_html'][$tag_name][$tag_key][$key] = $value;
1015
$this->CharConvert($this->info['tags'][$tag_name], $encoding); // only copy gets converted!
1023
function getHashdata($algorithm) {
1024
switch ($algorithm) {
1030
return $this->error('bad algorithm "'.$algorithm.'" in getHashdata()');
1034
if ((@$this->info['fileformat'] == 'ogg') && (@$this->info['audio']['dataformat'] == 'vorbis')) {
1036
// We cannot get an identical md5_data value for Ogg files where the comments
1037
// span more than 1 Ogg page (compared to the same audio data with smaller
1038
// comments) using the normal getID3() method of MD5'ing the data between the
1039
// end of the comments and the end of the file (minus any trailing tags),
1040
// because the page sequence numbers of the pages that the audio data is on
1041
// do not match. Under normal circumstances, where comments are smaller than
1042
// the nominal 4-8kB page size, then this is not a problem, but if there are
1043
// very large comments, the only way around it is to strip off the comment
1044
// tags with vorbiscomment and MD5 that file.
1045
// This procedure must be applied to ALL Ogg files, not just the ones with
1046
// comments larger than 1 page, because the below method simply MD5's the
1047
// whole file with the comments stripped, not just the portion after the
1048
// comments block (which is the standard getID3() method.
1050
// The above-mentioned problem of comments spanning multiple pages and changing
1051
// page sequence numbers likely happens for OggSpeex and OggFLAC as well, but
1052
// currently vorbiscomment only works on OggVorbis files.
1054
if ((bool) ini_get('safe_mode')) {
1056
$this->info['warning'][] = 'Failed making system call to vorbiscomment.exe - '.$algorithm.'_data is incorrect - error returned: PHP running in Safe Mode (backtick operator not available)';
1057
$this->info[$algorithm.'_data'] = false;
1061
// Prevent user from aborting script
1062
$old_abort = ignore_user_abort(true);
1064
// Create empty file
1065
$empty = tempnam('*', 'getID3');
1069
// Use vorbiscomment to make temp file without comments
1070
$temp = tempnam('*', 'getID3');
1071
$file = $this->info['filenamepath'];
1073
if (GETID3_OS_ISWINDOWS) {
1075
if (file_exists(GETID3_HELPERAPPSDIR.'vorbiscomment.exe')) {
1077
$commandline = '"'.GETID3_HELPERAPPSDIR.'vorbiscomment.exe" -w -c "'.$empty.'" "'.$file.'" "'.$temp.'"';
1078
$VorbisCommentError = `$commandline`;
1082
$VorbisCommentError = 'vorbiscomment.exe not found in '.GETID3_HELPERAPPSDIR;
1088
$commandline = 'vorbiscomment -w -c "'.$empty.'" "'.$file.'" "'.$temp.'" 2>&1';
1089
$VorbisCommentError = `$commandline`;
1093
if (!empty($VorbisCommentError)) {
1095
$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;
1096
$this->info[$algorithm.'_data'] = false;
1100
// Get hash of newly created file
1101
switch ($algorithm) {
1103
$this->info[$algorithm.'_data'] = getid3_lib::md5_file($temp);
1107
$this->info[$algorithm.'_data'] = getid3_lib::sha1_file($temp);
1116
// Reset abort setting
1117
ignore_user_abort($old_abort);
1123
if (!empty($this->info['avdataoffset']) || (isset($this->info['avdataend']) && ($this->info['avdataend'] < $this->info['filesize']))) {
1125
// get hash from part of file
1126
$this->info[$algorithm.'_data'] = getid3_lib::hash_data($this->info['filenamepath'], $this->info['avdataoffset'], $this->info['avdataend'], $algorithm);
1130
// get hash from whole file
1131
switch ($algorithm) {
1133
$this->info[$algorithm.'_data'] = getid3_lib::md5_file($this->info['filenamepath']);
1137
$this->info[$algorithm.'_data'] = getid3_lib::sha1_file($this->info['filenamepath']);
1147
function ChannelsBitratePlaytimeCalculations() {
1149
// set channelmode on audio
1150
if (isset($this->info['audio']['channels'])) {
1151
if (@$this->info['audio']['channels'] == '1') {
1152
$this->info['audio']['channelmode'] = 'mono';
1153
} elseif (@$this->info['audio']['channels'] == '2') {
1154
$this->info['audio']['channelmode'] = 'stereo';
1158
// Calculate combined bitrate - audio + video
1159
$CombinedBitrate = 0;
1160
$CombinedBitrate += (isset($this->info['audio']['bitrate']) ? $this->info['audio']['bitrate'] : 0);
1161
$CombinedBitrate += (isset($this->info['video']['bitrate']) ? $this->info['video']['bitrate'] : 0);
1162
if (($CombinedBitrate > 0) && empty($this->info['bitrate'])) {
1163
$this->info['bitrate'] = $CombinedBitrate;
1165
//if ((isset($this->info['video']) && !isset($this->info['video']['bitrate'])) || (isset($this->info['audio']) && !isset($this->info['audio']['bitrate']))) {
1166
// // for example, VBR MPEG video files cannot determine video bitrate:
1167
// // should not set overall bitrate and playtime from audio bitrate only
1168
// unset($this->info['bitrate']);
1171
if (!isset($this->info['playtime_seconds']) && !empty($this->info['bitrate'])) {
1172
$this->info['playtime_seconds'] = (($this->info['avdataend'] - $this->info['avdataoffset']) * 8) / $this->info['bitrate'];
1175
// Set playtime string
1176
if (!empty($this->info['playtime_seconds']) && empty($this->info['playtime_string'])) {
1177
$this->info['playtime_string'] = getid3_lib::PlaytimeString($this->info['playtime_seconds']);
1182
function CalculateCompressionRatioVideo() {
1183
if (empty($this->info['video'])) {
1186
if (empty($this->info['video']['resolution_x']) || empty($this->info['video']['resolution_y'])) {
1189
if (empty($this->info['video']['bits_per_sample'])) {
1193
switch ($this->info['video']['dataformat']) {
1201
$PlaytimeSeconds = 1;
1202
$BitrateCompressed = $this->info['filesize'] * 8;
1206
if (!empty($this->info['video']['frame_rate'])) {
1207
$FrameRate = $this->info['video']['frame_rate'];
1211
if (!empty($this->info['playtime_seconds'])) {
1212
$PlaytimeSeconds = $this->info['playtime_seconds'];
1216
if (!empty($this->info['video']['bitrate'])) {
1217
$BitrateCompressed = $this->info['video']['bitrate'];
1223
$BitrateUncompressed = $this->info['video']['resolution_x'] * $this->info['video']['resolution_y'] * $this->info['video']['bits_per_sample'] * $FrameRate;
1225
$this->info['video']['compression_ratio'] = $BitrateCompressed / $BitrateUncompressed;
1230
function CalculateCompressionRatioAudio() {
1231
if (empty($this->info['audio']['bitrate']) || empty($this->info['audio']['channels']) || empty($this->info['audio']['sample_rate'])) {
1234
$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));
1236
if (!empty($this->info['audio']['streams'])) {
1237
foreach ($this->info['audio']['streams'] as $streamnumber => $streamdata) {
1238
if (!empty($streamdata['bitrate']) && !empty($streamdata['channels']) && !empty($streamdata['sample_rate'])) {
1239
$this->info['audio']['streams'][$streamnumber]['compression_ratio'] = $streamdata['bitrate'] / ($streamdata['channels'] * $streamdata['sample_rate'] * (!empty($streamdata['bits_per_sample']) ? $streamdata['bits_per_sample'] : 16));
1247
function CalculateReplayGain() {
1248
if (isset($this->info['replay_gain'])) {
1249
$this->info['replay_gain']['reference_volume'] = 89;
1250
if (isset($this->info['replay_gain']['track']['adjustment'])) {
1251
$this->info['replay_gain']['track']['volume'] = $this->info['replay_gain']['reference_volume'] - $this->info['replay_gain']['track']['adjustment'];
1253
if (isset($this->info['replay_gain']['album']['adjustment'])) {
1254
$this->info['replay_gain']['album']['volume'] = $this->info['replay_gain']['reference_volume'] - $this->info['replay_gain']['album']['adjustment'];
1257
if (isset($this->info['replay_gain']['track']['peak'])) {
1258
$this->info['replay_gain']['track']['max_noclip_gain'] = 0 - getid3_lib::RGADamplitude2dB($this->info['replay_gain']['track']['peak']);
1260
if (isset($this->info['replay_gain']['album']['peak'])) {
1261
$this->info['replay_gain']['album']['max_noclip_gain'] = 0 - getid3_lib::RGADamplitude2dB($this->info['replay_gain']['album']['peak']);
1267
function ProcessAudioStreams() {
1268
if (!empty($this->info['audio']['bitrate']) || !empty($this->info['audio']['channels']) || !empty($this->info['audio']['sample_rate'])) {
1269
if (!isset($this->info['audio']['streams'])) {
1270
foreach ($this->info['audio'] as $key => $value) {
1271
if ($key != 'streams') {
1272
$this->info['audio']['streams'][0][$key] = $value;