~ubuntu-branches/ubuntu/hardy/gallery2/hardy-security

« back to all changes in this revision

Viewing changes to modules/getid3/lib/getid3/getid3.inc

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2006-04-16 16:42:35 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060416164235-8uy0u4bfjdxpge2o
Tags: 2.1.1-1
* New upstream release (Closes: #362936)
  + Bugfixes for Postgres7 (Closes: #359000, #362152)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/////////////////////////////////////////////////////////////////
 
3
/// getID3() by James Heinrich <info@getid3.org>               //
 
4
//  available at http://getid3.sourceforge.net                 //
 
5
//            or http://www.getid3.org                         //
 
6
/////////////////////////////////////////////////////////////////
 
7
//                                                             //
 
8
// Please see readme.txt for more information                  //
 
9
//                                                            ///
 
10
/////////////////////////////////////////////////////////////////
 
11
 
 
12
// Defines
 
13
define('GETID3_VERSION', '1.7.5-200512251515');
 
14
define('GETID3_FREAD_BUFFER_SIZE', 16384); // read buffer size in bytes
 
15
 
 
16
 
 
17
 
 
18
class getID3
 
19
{
 
20
        // public: Settings
 
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
 
23
 
 
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'
 
26
 
 
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
 
34
 
 
35
        // public: Optional tag/comment calucations
 
36
        var $option_extra_info        = true;             // Calculate additional info such as bitrate, channelmode etc
 
37
 
 
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
 
43
 
 
44
        // private
 
45
        var $filename;
 
46
 
 
47
 
 
48
        // public: constructor
 
49
        function getID3()
 
50
        {
 
51
 
 
52
                $this->startup_error   = '';
 
53
                $this->startup_warning = '';
 
54
 
 
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();
 
58
                }
 
59
 
 
60
                // Check memory
 
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;
 
65
                }
 
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';
 
72
                }
 
73
 
 
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.');
 
77
                }
 
78
 
 
79
 
 
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);
 
84
                        } else {
 
85
                                define('GETID3_OS_ISWINDOWS', false);
 
86
                        }
 
87
                }
 
88
 
 
89
                // Get base path of getID3() - ONCE
 
90
                if (!defined('GETID3_INCLUDEPATH')) {
 
91
                        define('GETID3_OS_DIRSLASH', (GETID3_OS_ISWINDOWS ? '\\' : '/'));
 
92
 
 
93
                        foreach (get_included_files() as $key => $val) {
 
94
                                if (basename($val) == 'getid3.inc') {
 
95
                                        define('GETID3_INCLUDEPATH', dirname($val).GETID3_OS_DIRSLASH);
 
96
                                        break;
 
97
                                }
 
98
                        }
 
99
                }
 
100
 
 
101
                // Load support library
 
102
                if (!include_once(GETID3_INCLUDEPATH.'getid3.lib.inc')) {
 
103
                        $this->startup_error .= 'getid3.lib.inc is missing or corrupt';
 
104
                }
 
105
 
 
106
 
 
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
 
115
 
 
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;
 
121
                                
 
122
                                $CLIdir = $DirPieces[0].' && cd \\';
 
123
                                for ($i = 1; $i < count($DirPieces); $i++) {
 
124
                                  if (strpos($DirPieces[$i], ' ') === false) {
 
125
                                    $CLIdir .= ' && cd '.$DirPieces[$i];
 
126
                                  } else {
 
127
                                    ob_start();
 
128
                                    system($CLIdir.' && dir /ad /x');
 
129
                                    $subdirsraw = explode("\n", ob_get_contents());
 
130
                                    ob_end_clean();
 
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];
 
134
                                        break;
 
135
                                      }
 
136
                                    }
 
137
                                    $DirPieces8[$i] = $matches[1];
 
138
                                  }
 
139
                                }
 
140
                                $helperappsdir = implode(GETID3_OS_DIRSLASH, $DirPieces8);
 
141
 
 
142
                        }
 
143
                        define('GETID3_HELPERAPPSDIR', realpath($helperappsdir).GETID3_OS_DIRSLASH);
 
144
 
 
145
                }
 
146
 
 
147
        }
 
148
 
 
149
 
 
150
 
 
151
        // public: analyze file - replaces GetAllFileInfo() and GetTagOnly()
 
152
        function analyze($filename) {
 
153
 
 
154
                if (!empty($this->startup_error)) {
 
155
                        return $this->error($this->startup_error);
 
156
                }
 
157
                if (!empty($this->startup_warning)) {
 
158
                        $this->warning($this->startup_warning);
 
159
                }
 
160
 
 
161
                // init result array and set parameters
 
162
                $this->info = array();
 
163
                $this->info['GETID3_VERSION'] = GETID3_VERSION;
 
164
 
 
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';
 
170
                        } else {
 
171
                                $errormessage .= 'PHP is not compiled with iconv() support. Please recompile with the --with-iconv switch';
 
172
                        }
 
173
                return $this->error($errormessage);
 
174
                }
 
175
 
 
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');
 
182
                        }
 
183
                }
 
184
 
 
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');
 
188
                }
 
189
 
 
190
                // open local file
 
191
                if (!$fp = @fopen($filename, 'rb')) {
 
192
                        return $this->error('Could not open file "'.$filename.'"');
 
193
                }
 
194
 
 
195
                // set parameters
 
196
                $this->info['filesize'] = filesize($filename);
 
197
 
 
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) ||
 
206
                                (ftell($fp) < 0)) {
 
207
                                        unset($this->info['filesize']);
 
208
                                        fclose($fp);
 
209
                                        return $this->error('File is most likely larger than 2GB and is not supported by PHP');
 
210
                        }
 
211
                }
 
212
 
 
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
 
224
 
 
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'];
 
229
 
 
230
 
 
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) {
 
234
 
 
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);
 
238
                        }
 
239
 
 
240
                } else {
 
241
 
 
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
 
249
 
 
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'];
 
253
                        }
 
254
 
 
255
                }
 
256
 
 
257
 
 
258
                // handle ID3v1 tag
 
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.');
 
262
                        }
 
263
                        $tag = new getid3_id3v1($fp, $this->info);
 
264
                }
 
265
 
 
266
                // handle APE tag
 
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.');
 
270
                        }
 
271
                        $tag = new getid3_apetag($fp, $this->info);
 
272
                }
 
273
 
 
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.');
 
278
                        }
 
279
                        $tag = new getid3_lyrics3($fp, $this->info);
 
280
                }
 
281
 
 
282
                // read 32 kb file data
 
283
                fseek($fp, $this->info['avdataoffset'], SEEK_SET);
 
284
                $formattest = fread($fp, 32774);
 
285
 
 
286
                // determine format
 
287
                $determined_format = $this->GetFileFormat($formattest, $filename);
 
288
 
 
289
                // unable to determine file format
 
290
                if (!$determined_format) {
 
291
                        fclose($fp);
 
292
                        return $this->error('unable to determine file format');
 
293
                }
 
294
 
 
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') {
 
298
                                fclose($fp);
 
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.';
 
302
                        }
 
303
                }
 
304
 
 
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') {
 
308
                                fclose($fp);
 
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.';
 
312
                        }
 
313
                }
 
314
 
 
315
                // set mime type
 
316
                $this->info['mime_type'] = $determined_format['mime_type'];
 
317
 
 
318
                // supported format signature pattern detected, but module deleted
 
319
                if (!file_exists(GETID3_INCLUDEPATH.$determined_format['include'])) {
 
320
                        fclose($fp);
 
321
                        return $this->error('Format not supported, module, '.$determined_format['include'].', was removed.');
 
322
                }
 
323
 
 
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'].').');
 
329
                }
 
330
 
 
331
                // include module
 
332
                include_once(GETID3_INCLUDEPATH.$determined_format['include']);
 
333
 
 
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.');
 
338
                }
 
339
                if (isset($determined_format['option'])) {
 
340
                        $class = new $class_name($fp, $this->info, $determined_format['option']);
 
341
                } else {
 
342
                        $class = new $class_name($fp, $this->info);
 
343
                }
 
344
 
 
345
                // close file
 
346
                fclose($fp);
 
347
 
 
348
                // process all tags - copy to 'tags' and convert charsets
 
349
                if ($this->option_tags_process) {
 
350
                        $this->HandleAllTags();
 
351
                }
 
352
 
 
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();
 
360
                }
 
361
 
 
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');
 
367
                        }
 
368
                }
 
369
 
 
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');
 
373
                }
 
374
 
 
375
                // remove undesired keys
 
376
                $this->CleanUp();
 
377
 
 
378
                // restore magic_quotes_runtime setting
 
379
                set_magic_quotes_runtime($old_magic_quotes_runtime);
 
380
 
 
381
                // return info array
 
382
                return $this->info;
 
383
        }
 
384
 
 
385
 
 
386
        // private: error handling
 
387
        function error($message) {
 
388
 
 
389
                $this->CleanUp();
 
390
 
 
391
                $this->info['error'][] = $message;
 
392
                return $this->info;
 
393
        }
 
394
 
 
395
 
 
396
        // private: warning handling
 
397
        function warning($message) {
 
398
                $this->info['warning'][] = $message;
 
399
                return true;
 
400
        }
 
401
 
 
402
 
 
403
        // private: CleanUp
 
404
        function CleanUp() {
 
405
 
 
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]);
 
411
                        }
 
412
                        if (empty($this->info['video'][$key]) && isset($this->info['video'][$key])) {
 
413
                                unset($this->info['video'][$key]);
 
414
                        }
 
415
                }
 
416
 
 
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]);
 
422
                                }
 
423
                        }
 
424
                }
 
425
 
 
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']);
 
430
                        }
 
431
                        if (isset($this->info['avdataend'])) {
 
432
                                unset($this->info['avdataend']);
 
433
                        }
 
434
                }
 
435
        }
 
436
 
 
437
 
 
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(
 
443
 
 
444
                                // Audio formats
 
445
 
 
446
                                // AC-3   - audio      - Dolby AC-3 / Dolby Digital
 
447
                                'ac3'  => array(
 
448
                                                        'pattern'   => '^\x0B\x77',
 
449
                                                        'group'     => 'audio',
 
450
                                                        'module'    => 'ac3',
 
451
                                                        'mime_type' => 'audio/ac3',
 
452
                                                ),
 
453
 
 
454
                                // AAC  - audio       - Advanced Audio Coding (AAC) - ADIF format
 
455
                                'adif' => array(
 
456
                                                        'pattern'   => '^ADIF',
 
457
                                                        'group'     => 'audio',
 
458
                                                        'module'    => 'aac',
 
459
                                                        'option'    => 'adif',
 
460
                                                        'mime_type' => 'application/octet-stream',
 
461
                                                        'fail_ape'  => 'WARNING',
 
462
                                                ),
 
463
 
 
464
 
 
465
                                // AAC  - audio       - Advanced Audio Coding (AAC) - ADTS format (very similar to MP3)
 
466
                                'adts' => array(
 
467
                                                        'pattern'   => '^\xFF[\xF0-\xF1\xF8-\xF9]',
 
468
                                                        'group'     => 'audio',
 
469
                                                        'module'    => 'aac',
 
470
                                                        'option'    => 'adts',
 
471
                                                        'mime_type' => 'application/octet-stream',
 
472
                                                        'fail_ape'  => 'WARNING',
 
473
                                                ),
 
474
 
 
475
 
 
476
                                // AU   - audio       - NeXT/Sun AUdio (AU)
 
477
                                'au'   => array(
 
478
                                                        'pattern'   => '^\.snd',
 
479
                                                        'group'     => 'audio',
 
480
                                                        'module'    => 'au',
 
481
                                                        'mime_type' => 'audio/basic',
 
482
                                                ),
 
483
 
 
484
                                // AVR  - audio       - Audio Visual Research
 
485
                                'avr'  => array(
 
486
                                                        'pattern'   => '^2BIT',
 
487
                                                        'group'     => 'audio',
 
488
                                                        'module'    => 'avr',
 
489
                                                        'mime_type' => 'application/octet-stream',
 
490
                                                ),
 
491
 
 
492
                                // BONK - audio       - Bonk v0.9+
 
493
                                'bonk' => array(
 
494
                                                        'pattern'   => '^\x00(BONK|INFO|META| ID3)',
 
495
                                                        'group'     => 'audio',
 
496
                                                        'module'    => 'bonk',
 
497
                                                        'mime_type' => 'audio/xmms-bonk',
 
498
                                                ),
 
499
 
 
500
                                // FLAC - audio       - Free Lossless Audio Codec
 
501
                                'flac' => array(
 
502
                                                        'pattern'   => '^fLaC',
 
503
                                                        'group'     => 'audio',
 
504
                                                        'module'    => 'flac',
 
505
                                                        'mime_type' => 'audio/x-flac',
 
506
                                                ),
 
507
 
 
508
                                // LA   - audio       - Lossless Audio (LA)
 
509
                                'la'   => array(
 
510
                                                        'pattern'   => '^LA0[2-4]',
 
511
                                                        'group'     => 'audio',
 
512
                                                        'module'    => 'la',
 
513
                                                        'mime_type' => 'application/octet-stream',
 
514
                                                ),
 
515
 
 
516
                                // LPAC - audio       - Lossless Predictive Audio Compression (LPAC)
 
517
                                'lpac' => array(
 
518
                                                        'pattern'   => '^LPAC',
 
519
                                                        'group'     => 'audio',
 
520
                                                        'module'    => 'lpac',
 
521
                                                        'mime_type' => 'application/octet-stream',
 
522
                                                ),
 
523
 
 
524
                                // MIDI - audio       - MIDI (Musical Instrument Digital Interface)
 
525
                                'midi' => array(
 
526
                                                        'pattern'   => '^MThd',
 
527
                                                        'group'     => 'audio',
 
528
                                                        'module'    => 'midi',
 
529
                                                        'mime_type' => 'audio/midi',
 
530
                                                ),
 
531
 
 
532
                                // MAC  - audio       - Monkey's Audio Compressor
 
533
                                'mac'  => array(
 
534
                                                        'pattern'   => '^MAC ',
 
535
                                                        'group'     => 'audio',
 
536
                                                        'module'    => 'monkey',
 
537
                                                        'mime_type' => 'application/octet-stream',
 
538
                                                ),
 
539
 
 
540
                                // MOD  - audio       - MODule (assorted sub-formats)
 
541
                                'mod'  => array(
 
542
                                                        'pattern'   => '^.{1080}(M.K.|[5-9]CHN|[1-3][0-9]CH)',
 
543
                                                        'group'     => 'audio',
 
544
                                                        'module'    => 'mod',
 
545
                                                        'option'    => 'mod',
 
546
                                                        'mime_type' => 'audio/mod',
 
547
                                                ),
 
548
 
 
549
                                // MOD  - audio       - MODule (Impulse Tracker)
 
550
                                'it'   => array(
 
551
                                                        'pattern'   => '^IMPM',
 
552
                                                        'group'     => 'audio',
 
553
                                                        'module'    => 'mod',
 
554
                                                        'option'    => 'it',
 
555
                                                        'mime_type' => 'audio/it',
 
556
                                                ),
 
557
 
 
558
                                // MOD  - audio       - MODule (eXtended Module, various sub-formats)
 
559
                                'xm'   => array(
 
560
                                                        'pattern'   => '^Extended Module',
 
561
                                                        'group'     => 'audio',
 
562
                                                        'module'    => 'mod',
 
563
                                                        'option'    => 'xm',
 
564
                                                        'mime_type' => 'audio/xm',
 
565
                                                ),
 
566
 
 
567
                                // MOD  - audio       - MODule (ScreamTracker)
 
568
                                's3m'  => array(
 
569
                                                        'pattern'   => '^.{44}SCRM',
 
570
                                                        'group'     => 'audio',
 
571
                                                        'module'    => 'mod',
 
572
                                                        'option'    => 's3m',
 
573
                                                        'mime_type' => 'audio/s3m',
 
574
                                                ),
 
575
 
 
576
                                // MPC  - audio       - Musepack / MPEGplus
 
577
                                'mpc'  => array(
 
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])',
 
579
                                                        'group'     => 'audio',
 
580
                                                        'module'    => 'mpc',
 
581
                                                        'mime_type' => 'application/octet-stream',
 
582
                                                ),
 
583
 
 
584
                                // MP3  - audio       - MPEG-audio Layer 3 (very similar to AAC-ADTS)
 
585
                                'mp3'  => array(
 
586
                                                        'pattern'   => '^\xFF[\xE2-\xE7\xF2-\xF7\xFA-\xFF][\x00-\xEB]',
 
587
                                                        'group'     => 'audio',
 
588
                                                        'module'    => 'mp3',
 
589
                                                        'mime_type' => 'audio/mpeg',
 
590
                                                ),
 
591
 
 
592
                                // OFR  - audio       - OptimFROG
 
593
                                'ofr'  => array(
 
594
                                                        'pattern'   => '^(\*RIFF|OFR)',
 
595
                                                        'group'     => 'audio',
 
596
                                                        'module'    => 'optimfrog',
 
597
                                                        'mime_type' => 'application/octet-stream',
 
598
                                                ),
 
599
 
 
600
                                // RKAU - audio       - RKive AUdio compressor
 
601
                                'rkau' => array(
 
602
                                                        'pattern'   => '^RKA',
 
603
                                                        'group'     => 'audio',
 
604
                                                        'module'    => 'rkau',
 
605
                                                        'mime_type' => 'application/octet-stream',
 
606
                                                ),
 
607
 
 
608
                                // SHN  - audio       - Shorten
 
609
                                'shn'  => array(
 
610
                                                        'pattern'   => '^ajkg',
 
611
                                                        'group'     => 'audio',
 
612
                                                        'module'    => 'shorten',
 
613
                                                        'mime_type' => 'audio/xmms-shn',
 
614
                                                        'fail_id3'  => 'ERROR',
 
615
                                                        'fail_ape'  => 'ERROR',
 
616
                                                ),
 
617
 
 
618
                                // TTA  - audio       - TTA Lossless Audio Compressor (http://tta.corecodec.org)
 
619
                                'tta'  => array(
 
620
                                                        'pattern'   => '^TTA',  // could also be '^TTA(\x01|\x02|\x03|2|1)'
 
621
                                                        'group'     => 'audio',
 
622
                                                        'module'    => 'tta',
 
623
                                                        'mime_type' => 'application/octet-stream',
 
624
                                                ),
 
625
 
 
626
                                // VOC  - audio       - Creative Voice (VOC)
 
627
                                'voc'  => array(
 
628
                                                        'pattern'   => '^Creative Voice File',
 
629
                                                        'group'     => 'audio',
 
630
                                                        'module'    => 'voc',
 
631
                                                        'mime_type' => 'audio/voc',
 
632
                                                ),
 
633
 
 
634
                                // VQF  - audio       - transform-domain weighted interleave Vector Quantization Format (VQF)
 
635
                                'vqf'  => array(
 
636
                                                        'pattern'   => '^TWIN',
 
637
                                                        'group'     => 'audio',
 
638
                                                        'module'    => 'vqf',
 
639
                                                        'mime_type' => 'application/octet-stream',
 
640
                                                ),
 
641
 
 
642
                                // WV  - audio        - WavPack (v4.0+)
 
643
                                'wv'   => array(
 
644
                                                        'pattern'   => '^wvpk',
 
645
                                                        'group'     => 'audio',
 
646
                                                        'module'    => 'wavpack',
 
647
                                                        'mime_type' => 'application/octet-stream',
 
648
                                                ),
 
649
 
 
650
 
 
651
                                // Audio-Video formats
 
652
 
 
653
                                // ASF  - audio/video - Advanced Streaming Format, Windows Media Video, Windows Media Audio
 
654
                                'asf'  => array(
 
655
                                                        'pattern'   => '^\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C',
 
656
                                                        'group'     => 'audio-video',
 
657
                                                        'module'    => 'asf',
 
658
                                                        'mime_type' => 'video/x-ms-asf',
 
659
                                                        'iconv_req' => false,
 
660
                                                ),
 
661
 
 
662
                                // BINK - audio/video - Bink / Smacker
 
663
                                'bink' => array(
 
664
                                                        'pattern'   => '^(BIK|SMK)',
 
665
                                                        'group'     => 'audio-video',
 
666
                                                        'module'    => 'bink',
 
667
                                                        'mime_type' => 'application/octet-stream',
 
668
                                                ),
 
669
 
 
670
                                // FLV  - audio/video - FLash Video
 
671
                                'flv' => array(
 
672
                                                        'pattern'   => '^FLV\x01',
 
673
                                                        'group'     => 'audio-video',
 
674
                                                        'module'    => 'flv',
 
675
                                                        'mime_type' => 'video/x-flv',
 
676
                                                ),
 
677
 
 
678
                                // MKAV - audio/video - Mastroka
 
679
                                'matroska' => array(
 
680
                                                        'pattern'   => '^\x1A\x45\xDF\xA3',
 
681
                                                        'group'     => 'audio-video',
 
682
                                                        'module'    => 'matroska',
 
683
                                                        'mime_type' => 'application/octet-stream',
 
684
                                                ),
 
685
 
 
686
                                // MPEG - audio/video - MPEG (Moving Pictures Experts Group)
 
687
                                'mpeg' => array(
 
688
                                                        'pattern'   => '^\x00\x00\x01(\xBA|\xB3)',
 
689
                                                        'group'     => 'audio-video',
 
690
                                                        'module'    => 'mpeg',
 
691
                                                        'mime_type' => 'video/mpeg',
 
692
                                                ),
 
693
 
 
694
                                // NSV  - audio/video - Nullsoft Streaming Video (NSV)
 
695
                                'nsv'  => array(
 
696
                                                        'pattern'   => '^NSV[sf]',
 
697
                                                        'group'     => 'audio-video',
 
698
                                                        'module'    => 'nsv',
 
699
                                                        'mime_type' => 'application/octet-stream',
 
700
                                                ),
 
701
 
 
702
                                // Ogg  - audio/video - Ogg (Ogg-Vorbis, Ogg-FLAC, Speex, Ogg-Theora(*), Ogg-Tarkin(*))
 
703
                                'ogg'  => array(
 
704
                                                        'pattern'   => '^OggS',
 
705
                                                        'group'     => 'audio',
 
706
                                                        'module'    => 'ogg',
 
707
                                                        'mime_type' => 'application/ogg',
 
708
                                                        'fail_id3'  => 'WARNING',
 
709
                                                        'fail_ape'  => 'WARNING',
 
710
                                                ),
 
711
 
 
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',
 
718
                                                ),
 
719
 
 
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)
 
721
                                'riff' => array(
 
722
                                                        'pattern'   => '^(RIFF|SDSS|FORM)',
 
723
                                                        'group'     => 'audio-video',
 
724
                                                        'module'    => 'riff',
 
725
                                                        'mime_type' => 'audio/x-wave',
 
726
                                                        'fail_ape'  => 'WARNING',
 
727
                                                ),
 
728
 
 
729
                                // Real - audio/video - RealAudio, RealVideo
 
730
                                'real' => array(
 
731
                                                        'pattern'   => '^(\.RMF|.ra)',
 
732
                                                        'group'     => 'audio-video',
 
733
                                                        'module'    => 'real',
 
734
                                                        'mime_type' => 'audio/x-realaudio',
 
735
                                                ),
 
736
 
 
737
                                // SWF - audio/video - ShockWave Flash
 
738
                                'swf' => array(
 
739
                                                        'pattern'   => '^(F|C)WS',
 
740
                                                        'group'     => 'audio-video',
 
741
                                                        'module'    => 'swf',
 
742
                                                        'mime_type' => 'application/x-shockwave-flash',
 
743
                                                ),
 
744
 
 
745
 
 
746
                                // Still-Image formats
 
747
 
 
748
                                // BMP  - still image - Bitmap (Windows, OS/2; uncompressed, RLE8, RLE4)
 
749
                                'bmp'  => array(
 
750
                                                        'pattern'   => '^BM',
 
751
                                                        'group'     => 'graphic',
 
752
                                                        'module'    => 'bmp',
 
753
                                                        'mime_type' => 'image/bmp',
 
754
                                                        'fail_id3'  => 'ERROR',
 
755
                                                        'fail_ape'  => 'ERROR',
 
756
                                                ),
 
757
 
 
758
                                // GIF  - still image - Graphics Interchange Format
 
759
                                'gif'  => array(
 
760
                                                        'pattern'   => '^GIF',
 
761
                                                        'group'     => 'graphic',
 
762
                                                        'module'    => 'gif',
 
763
                                                        'mime_type' => 'image/gif',
 
764
                                                        'fail_id3'  => 'ERROR',
 
765
                                                        'fail_ape'  => 'ERROR',
 
766
                                                ),
 
767
 
 
768
                                // JPEG - still image - Joint Photographic Experts Group (JPEG)
 
769
                                'jpg'  => array(
 
770
                                                        'pattern'   => '^\xFF\xD8\xFF',
 
771
                                                        'group'     => 'graphic',
 
772
                                                        'module'    => 'jpg',
 
773
                                                        'mime_type' => 'image/jpeg',
 
774
                                                        'fail_id3'  => 'ERROR',
 
775
                                                        'fail_ape'  => 'ERROR',
 
776
                                                ),
 
777
 
 
778
                                // PCD  - still image - Kodak Photo CD
 
779
                                'pcd'  => array(
 
780
                                                        'pattern'   => '^.{2048}PCD_IPI\x00',
 
781
                                                        'group'     => 'graphic',
 
782
                                                        'module'    => 'pcd',
 
783
                                                        'mime_type' => 'image/x-photo-cd',
 
784
                                                        'fail_id3'  => 'ERROR',
 
785
                                                        'fail_ape'  => 'ERROR',
 
786
                                                ),
 
787
 
 
788
 
 
789
                                // PNG  - still image - Portable Network Graphics (PNG)
 
790
                                'png'  => array(
 
791
                                                        'pattern'   => '^\x89\x50\x4E\x47\x0D\x0A\x1A\x0A',
 
792
                                                        'group'     => 'graphic',
 
793
                                                        'module'    => 'png',
 
794
                                                        'mime_type' => 'image/png',
 
795
                                                        'fail_id3'  => 'ERROR',
 
796
                                                        'fail_ape'  => 'ERROR',
 
797
                                                ),
 
798
 
 
799
 
 
800
                                // TIFF  - still image - Tagged Information File Format (TIFF)
 
801
                                'tiff' => array(
 
802
                                                        'pattern'   => '^(II\x2A\x00|MM\x00\x2A)',
 
803
                                                        'group'     => 'graphic',
 
804
                                                        'module'    => 'tiff',
 
805
                                                        'mime_type' => 'image/tiff',
 
806
                                                        'fail_id3'  => 'ERROR',
 
807
                                                        'fail_ape'  => 'ERROR',
 
808
                                                ),
 
809
 
 
810
 
 
811
                                // Data formats
 
812
 
 
813
                                // ISO  - data        - International Standards Organization (ISO) CD-ROM Image
 
814
                                'iso'  => array(
 
815
                                                        'pattern'   => '^.{32769}CD001',
 
816
                                                        'group'     => 'misc',
 
817
                                                        'module'    => 'iso',
 
818
                                                        'mime_type' => 'application/octet-stream',
 
819
                                                        'fail_id3'  => 'ERROR',
 
820
                                                        'fail_ape'  => 'ERROR',
 
821
                                                        'iconv_req' => false,
 
822
                                                ),
 
823
 
 
824
                                // RAR  - data        - RAR compressed data
 
825
                                'rar'  => array(
 
826
                                                        'pattern'   => '^Rar\!',
 
827
                                                        'group'     => 'archive',
 
828
                                                        'module'    => 'rar',
 
829
                                                        'mime_type' => 'application/octet-stream',
 
830
                                                        'fail_id3'  => 'ERROR',
 
831
                                                        'fail_ape'  => 'ERROR',
 
832
                                                ),
 
833
 
 
834
                                // SZIP - audio       - SZIP compressed data
 
835
                                'szip' => array(
 
836
                                                        'pattern'   => '^SZ\x0A\x04',
 
837
                                                        'group'     => 'archive',
 
838
                                                        'module'    => 'szip',
 
839
                                                        'mime_type' => 'application/octet-stream',
 
840
                                                        'fail_id3'  => 'ERROR',
 
841
                                                        'fail_ape'  => 'ERROR',
 
842
                                                ),
 
843
 
 
844
                                // TAR  - data        - TAR compressed data
 
845
                                'tar'  => array(
 
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',
 
848
                                                        'module'    => 'tar',
 
849
                                                        'mime_type' => 'application/x-tar',
 
850
                                                        'fail_id3'  => 'ERROR',
 
851
                                                        'fail_ape'  => 'ERROR',
 
852
                                                ),
 
853
 
 
854
                                // GZIP  - data        - GZIP compressed data
 
855
                                'gz'  => array(
 
856
                                                        'pattern'   => '^\x1F\x8B\x08',
 
857
                                                        'group'     => 'archive',
 
858
                                                        'module'    => 'gzip',
 
859
                                                        'mime_type' => 'application/x-gzip',
 
860
                                                        'fail_id3'  => 'ERROR',
 
861
                                                        'fail_ape'  => 'ERROR',
 
862
                                                ),
 
863
 
 
864
                                // ZIP  - data        - ZIP compressed data
 
865
                                'zip'  => array(
 
866
                                                        'pattern'   => '^PK\x03\x04',
 
867
                                                        'group'     => 'archive',
 
868
                                                        'module'    => 'zip',
 
869
                                                        'mime_type' => 'application/zip',
 
870
                                                        'fail_id3'  => 'ERROR',
 
871
                                                        'fail_ape'  => 'ERROR',
 
872
                                                  )
 
873
                        );
 
874
                }
 
875
 
 
876
                return $format_info;
 
877
        }
 
878
 
 
879
 
 
880
 
 
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
 
885
                // of the file).
 
886
 
 
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';
 
894
                                return $info;
 
895
                        }
 
896
                }
 
897
 
 
898
 
 
899
                if (preg_match('/\.mp[123a]$/i', $filename)) {
 
900
 
 
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';
 
906
                        return $info;
 
907
 
 
908
                //} elseif (preg_match('/\.tar$/i', $filename)) {
 
909
        //
 
910
                //      // TAR files don't have any useful header to work from
 
911
                //      // TAR  - data        - TAR compressed data
 
912
                //      $info = array(
 
913
                //              'pattern'   => '^.{512}',
 
914
                //              'group'     => 'archive',
 
915
                //              'module'    => 'tar',
 
916
                //              'mime_type' => 'application/octet-stream',
 
917
                //              'fail_id3'  => 'ERROR',
 
918
                //              'fail_ape'  => 'ERROR',
 
919
                //      );
 
920
                //      $info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.inc';
 
921
                //      return $info;
 
922
 
 
923
                }
 
924
 
 
925
                return false;
 
926
        }
 
927
 
 
928
 
 
929
        // converts array to $encoding charset from $this->encoding
 
930
        function CharConvert(&$array, $encoding) {
 
931
 
 
932
                // identical encoding - end here
 
933
                if ($encoding == $this->encoding) {
 
934
                        return;
 
935
                }
 
936
 
 
937
                // loop thru array
 
938
                foreach ($array as $key => $value) {
 
939
 
 
940
                        // go recursive
 
941
                        if (is_array($value)) {
 
942
                                $this->CharConvert($array[$key], $encoding);
 
943
                        }
 
944
 
 
945
                        // convert string
 
946
                        elseif (is_string($value)) {
 
947
                                $array[$key] = trim(getid3_lib::iconv_fallback($encoding, $this->encoding, $value));
 
948
                        }
 
949
                }
 
950
        }
 
951
 
 
952
 
 
953
        function HandleAllTags() {
 
954
 
 
955
                // key name => array (tag name, character encoding)
 
956
                static $tags;
 
957
                if (empty($tags)) {
 
958
                        $tags = array(
 
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')
 
974
                        );
 
975
                }
 
976
 
 
977
                // loop thru comments array
 
978
                foreach ($tags as $comment_name => $tagname_encoding_array) {
 
979
                        list($tag_name, $encoding) = $tagname_encoding_array;
 
980
 
 
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;
 
984
                        }
 
985
 
 
986
                        // copy comments if key name set
 
987
                        if (!empty($this->info[$comment_name]['comments'])) {
 
988
 
 
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!
 
993
                                                }
 
994
                                        }
 
995
                                }
 
996
 
 
997
                                if (!isset($this->info['tags'][$tag_name])) {
 
998
                                        // comments are set but contain nothing but empty strings, so skip
 
999
                                        continue;
 
1000
                                }
 
1001
 
 
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('&#0;', '', getid3_lib::MultiByteCharString2HTML($value, $encoding));
 
1008
                                                        } else {
 
1009
                                                                $this->info['tags_html'][$tag_name][$tag_key][$key] = $value;
 
1010
                                                        }
 
1011
                                                }
 
1012
                                        }
 
1013
                                }
 
1014
 
 
1015
                                $this->CharConvert($this->info['tags'][$tag_name], $encoding);           // only copy gets converted!
 
1016
                        }
 
1017
 
 
1018
                }
 
1019
                return true;
 
1020
        }
 
1021
 
 
1022
 
 
1023
        function getHashdata($algorithm) {
 
1024
                switch ($algorithm) {
 
1025
                        case 'md5':
 
1026
                        case 'sha1':
 
1027
                                break;
 
1028
 
 
1029
                        default:
 
1030
                                return $this->error('bad algorithm "'.$algorithm.'" in getHashdata()');
 
1031
                                break;
 
1032
                }
 
1033
 
 
1034
                if ((@$this->info['fileformat'] == 'ogg') && (@$this->info['audio']['dataformat'] == 'vorbis')) {
 
1035
 
 
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.
 
1049
 
 
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.
 
1053
 
 
1054
                        if ((bool) ini_get('safe_mode')) {
 
1055
 
 
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;
 
1058
 
 
1059
                        } else {
 
1060
 
 
1061
                                // Prevent user from aborting script
 
1062
                                $old_abort = ignore_user_abort(true);
 
1063
 
 
1064
                                // Create empty file
 
1065
                                $empty = tempnam('*', 'getID3');
 
1066
                                touch($empty);
 
1067
 
 
1068
 
 
1069
                                // Use vorbiscomment to make temp file without comments
 
1070
                                $temp = tempnam('*', 'getID3');
 
1071
                                $file = $this->info['filenamepath'];
 
1072
 
 
1073
                                if (GETID3_OS_ISWINDOWS) {
 
1074
 
 
1075
                                        if (file_exists(GETID3_HELPERAPPSDIR.'vorbiscomment.exe')) {
 
1076
 
 
1077
                                                $commandline = '"'.GETID3_HELPERAPPSDIR.'vorbiscomment.exe" -w -c "'.$empty.'" "'.$file.'" "'.$temp.'"';
 
1078
                                                $VorbisCommentError = `$commandline`;
 
1079
 
 
1080
                                        } else {
 
1081
 
 
1082
                                                $VorbisCommentError = 'vorbiscomment.exe not found in '.GETID3_HELPERAPPSDIR;
 
1083
 
 
1084
                                        }
 
1085
 
 
1086
                                } else {
 
1087
 
 
1088
                                        $commandline = 'vorbiscomment -w -c "'.$empty.'" "'.$file.'" "'.$temp.'" 2>&1';
 
1089
                                        $VorbisCommentError = `$commandline`;
 
1090
 
 
1091
                                }
 
1092
 
 
1093
                                if (!empty($VorbisCommentError)) {
 
1094
 
 
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;
 
1097
 
 
1098
                                } else {
 
1099
 
 
1100
                                        // Get hash of newly created file
 
1101
                                        switch ($algorithm) {
 
1102
                                                case 'md5':
 
1103
                                                        $this->info[$algorithm.'_data'] = getid3_lib::md5_file($temp);
 
1104
                                                        break;
 
1105
 
 
1106
                                                case 'sha1':
 
1107
                                                        $this->info[$algorithm.'_data'] = getid3_lib::sha1_file($temp);
 
1108
                                                        break;
 
1109
                                        }
 
1110
                                }
 
1111
 
 
1112
                                // Clean up
 
1113
                                unlink($empty);
 
1114
                                unlink($temp);
 
1115
 
 
1116
                                // Reset abort setting
 
1117
                                ignore_user_abort($old_abort);
 
1118
 
 
1119
                        }
 
1120
 
 
1121
                } else {
 
1122
 
 
1123
                        if (!empty($this->info['avdataoffset']) || (isset($this->info['avdataend']) && ($this->info['avdataend'] < $this->info['filesize']))) {
 
1124
 
 
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);
 
1127
 
 
1128
                        } else {
 
1129
 
 
1130
                                // get hash from whole file
 
1131
                                switch ($algorithm) {
 
1132
                                        case 'md5':
 
1133
                                                $this->info[$algorithm.'_data'] = getid3_lib::md5_file($this->info['filenamepath']);
 
1134
                                                break;
 
1135
 
 
1136
                                        case 'sha1':
 
1137
                                                $this->info[$algorithm.'_data'] = getid3_lib::sha1_file($this->info['filenamepath']);
 
1138
                                                break;
 
1139
                                }
 
1140
                        }
 
1141
 
 
1142
                }
 
1143
                return true;
 
1144
        }
 
1145
 
 
1146
 
 
1147
        function ChannelsBitratePlaytimeCalculations() {
 
1148
 
 
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';
 
1155
                        }
 
1156
                }
 
1157
 
 
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;
 
1164
                }
 
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']);
 
1169
                //}
 
1170
 
 
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'];
 
1173
                }
 
1174
 
 
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']);
 
1178
                }
 
1179
        }
 
1180
 
 
1181
 
 
1182
        function CalculateCompressionRatioVideo() {
 
1183
                if (empty($this->info['video'])) {
 
1184
                        return false;
 
1185
                }
 
1186
                if (empty($this->info['video']['resolution_x']) || empty($this->info['video']['resolution_y'])) {
 
1187
                        return false;
 
1188
                }
 
1189
                if (empty($this->info['video']['bits_per_sample'])) {
 
1190
                        return false;
 
1191
                }
 
1192
 
 
1193
                switch ($this->info['video']['dataformat']) {
 
1194
                        case 'bmp':
 
1195
                        case 'gif':
 
1196
                        case 'jpeg':
 
1197
                        case 'jpg':
 
1198
                        case 'png':
 
1199
                        case 'tiff':
 
1200
                                $FrameRate = 1;
 
1201
                                $PlaytimeSeconds = 1;
 
1202
                                $BitrateCompressed = $this->info['filesize'] * 8;
 
1203
                                break;
 
1204
 
 
1205
                        default:
 
1206
                                if (!empty($this->info['video']['frame_rate'])) {
 
1207
                                        $FrameRate = $this->info['video']['frame_rate'];
 
1208
                                } else {
 
1209
                                        return false;
 
1210
                                }
 
1211
                                if (!empty($this->info['playtime_seconds'])) {
 
1212
                                        $PlaytimeSeconds = $this->info['playtime_seconds'];
 
1213
                                } else {
 
1214
                                        return false;
 
1215
                                }
 
1216
                                if (!empty($this->info['video']['bitrate'])) {
 
1217
                                        $BitrateCompressed = $this->info['video']['bitrate'];
 
1218
                                } else {
 
1219
                                        return false;
 
1220
                                }
 
1221
                                break;
 
1222
                }
 
1223
                $BitrateUncompressed = $this->info['video']['resolution_x'] * $this->info['video']['resolution_y'] * $this->info['video']['bits_per_sample'] * $FrameRate;
 
1224
 
 
1225
                $this->info['video']['compression_ratio'] = $BitrateCompressed / $BitrateUncompressed;
 
1226
                return true;
 
1227
        }
 
1228
 
 
1229
 
 
1230
        function CalculateCompressionRatioAudio() {
 
1231
                if (empty($this->info['audio']['bitrate']) || empty($this->info['audio']['channels']) || empty($this->info['audio']['sample_rate'])) {
 
1232
                        return false;
 
1233
                }
 
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));
 
1235
 
 
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));
 
1240
                                }
 
1241
                        }
 
1242
                }
 
1243
                return true;
 
1244
        }
 
1245
 
 
1246
 
 
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'];
 
1252
                        }
 
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'];
 
1255
                        }
 
1256
 
 
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']);
 
1259
                        }
 
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']);
 
1262
                        }
 
1263
                }
 
1264
                return true;
 
1265
        }
 
1266
 
 
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;
 
1273
                                        }
 
1274
                                }
 
1275
                        }
 
1276
                }
 
1277
                return true;
 
1278
        }
 
1279
 
 
1280
}
 
1281
 
 
1282
?>