2
/////////////////////////////////////////////////////////////////
3
/// getID3() by James Heinrich <info@getid3.org> //
4
// available at http://getid3.sourceforge.net //
5
// or http://www.getid3.org //
6
/////////////////////////////////////////////////////////////////
7
// See readme.txt for more details //
8
/////////////////////////////////////////////////////////////////
10
// module.tag.id3v1.php //
11
// module for analyzing ID3v1 tags //
12
// dependencies: NONE //
14
/////////////////////////////////////////////////////////////////
17
class getid3_id3v1 extends getid3_handler
20
public function Analyze() {
21
$info = &$this->getid3->info;
23
if (!getid3_lib::intValueSupported($info['filesize'])) {
24
$info['warning'][] = 'Unable to check for ID3v1 because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB';
28
fseek($this->getid3->fp, -256, SEEK_END);
29
$preid3v1 = fread($this->getid3->fp, 128);
30
$id3v1tag = fread($this->getid3->fp, 128);
32
if (substr($id3v1tag, 0, 3) == 'TAG') {
34
$info['avdataend'] = $info['filesize'] - 128;
36
$ParsedID3v1['title'] = $this->cutfield(substr($id3v1tag, 3, 30));
37
$ParsedID3v1['artist'] = $this->cutfield(substr($id3v1tag, 33, 30));
38
$ParsedID3v1['album'] = $this->cutfield(substr($id3v1tag, 63, 30));
39
$ParsedID3v1['year'] = $this->cutfield(substr($id3v1tag, 93, 4));
40
$ParsedID3v1['comment'] = substr($id3v1tag, 97, 30); // can't remove nulls yet, track detection depends on them
41
$ParsedID3v1['genreid'] = ord(substr($id3v1tag, 127, 1));
43
// If second-last byte of comment field is null and last byte of comment field is non-null
44
// then this is ID3v1.1 and the comment field is 28 bytes long and the 30th byte is the track number
45
if (($id3v1tag{125} === "\x00") && ($id3v1tag{126} !== "\x00")) {
46
$ParsedID3v1['track'] = ord(substr($ParsedID3v1['comment'], 29, 1));
47
$ParsedID3v1['comment'] = substr($ParsedID3v1['comment'], 0, 28);
49
$ParsedID3v1['comment'] = $this->cutfield($ParsedID3v1['comment']);
51
$ParsedID3v1['genre'] = $this->LookupGenreName($ParsedID3v1['genreid']);
52
if (!empty($ParsedID3v1['genre'])) {
53
unset($ParsedID3v1['genreid']);
55
if (isset($ParsedID3v1['genre']) && (empty($ParsedID3v1['genre']) || ($ParsedID3v1['genre'] == 'Unknown'))) {
56
unset($ParsedID3v1['genre']);
59
foreach ($ParsedID3v1 as $key => $value) {
60
$ParsedID3v1['comments'][$key][0] = $value;
63
// ID3v1 data is supposed to be padded with NULL characters, but some taggers pad with spaces
64
$GoodFormatID3v1tag = $this->GenerateID3v1Tag(
65
$ParsedID3v1['title'],
66
$ParsedID3v1['artist'],
67
$ParsedID3v1['album'],
69
(isset($ParsedID3v1['genre']) ? $this->LookupGenreID($ParsedID3v1['genre']) : false),
70
$ParsedID3v1['comment'],
71
(!empty($ParsedID3v1['track']) ? $ParsedID3v1['track'] : ''));
72
$ParsedID3v1['padding_valid'] = true;
73
if ($id3v1tag !== $GoodFormatID3v1tag) {
74
$ParsedID3v1['padding_valid'] = false;
75
$info['warning'][] = 'Some ID3v1 fields do not use NULL characters for padding';
78
$ParsedID3v1['tag_offset_end'] = $info['filesize'];
79
$ParsedID3v1['tag_offset_start'] = $ParsedID3v1['tag_offset_end'] - 128;
81
$info['id3v1'] = $ParsedID3v1;
84
if (substr($preid3v1, 0, 3) == 'TAG') {
85
// The way iTunes handles tags is, well, brain-damaged.
86
// It completely ignores v1 if ID3v2 is present.
87
// This goes as far as adding a new v1 tag *even if there already is one*
89
// A suspected double-ID3v1 tag has been detected, but it could be that
90
// the "TAG" identifier is a legitimate part of an APE or Lyrics3 tag
91
if (substr($preid3v1, 96, 8) == 'APETAGEX') {
92
// an APE tag footer was found before the last ID3v1, assume false "TAG" synch
93
} elseif (substr($preid3v1, 119, 6) == 'LYRICS') {
94
// a Lyrics3 tag footer was found before the last ID3v1, assume false "TAG" synch
96
// APE and Lyrics3 footers not found - assume double ID3v1
97
$info['warning'][] = 'Duplicate ID3v1 tag detected - this has been known to happen with iTunes';
98
$info['avdataend'] -= 128;
105
public static function cutfield($str) {
106
return trim(substr($str, 0, strcspn($str, "\x00")));
109
public static function ArrayOfGenres($allowSCMPXextended=false) {
110
static $GenreLookup = array(
144
33 => 'Instrumental',
157
46 => 'Instrumental Pop',
158
47 => 'Instrumental Rock',
162
51 => 'Techno-Industrial',
167
56 => 'Southern Rock',
172
61 => 'Christian Rap',
175
64 => 'Native American',
193
82 => 'National Folk',
203
92 => 'Progressive Rock',
204
93 => 'Psychedelic Rock',
205
94 => 'Symphonic Rock',
209
98 => 'Easy Listening',
215
104 => 'Chamber Music',
220
109 => 'Porn Groove',
228
117 => 'Power Ballad',
229
118 => 'Rhythmic Soul',
238
127 => 'Drum & Bass',
247
136 => 'Christian Gangsta Rap',
248
137 => 'Heavy Metal',
249
138 => 'Black Metal',
251
140 => 'Contemporary Christian',
252
141 => 'Christian Rock',
255
144 => 'Thrash Metal',
266
static $GenreLookupSCMPX = array();
267
if ($allowSCMPXextended && empty($GenreLookupSCMPX)) {
268
$GenreLookupSCMPX = $GenreLookup;
269
// http://www.geocities.co.jp/SiliconValley-Oakland/3664/alittle.html#GenreExtended
270
// Extended ID3v1 genres invented by SCMPX
271
// Note that 255 "Japanese Anime" conflicts with standard "Unknown"
272
$GenreLookupSCMPX[240] = 'Sacred';
273
$GenreLookupSCMPX[241] = 'Northern Europe';
274
$GenreLookupSCMPX[242] = 'Irish & Scottish';
275
$GenreLookupSCMPX[243] = 'Scotland';
276
$GenreLookupSCMPX[244] = 'Ethnic Europe';
277
$GenreLookupSCMPX[245] = 'Enka';
278
$GenreLookupSCMPX[246] = 'Children\'s Song';
279
$GenreLookupSCMPX[247] = 'Japanese Sky';
280
$GenreLookupSCMPX[248] = 'Japanese Heavy Rock';
281
$GenreLookupSCMPX[249] = 'Japanese Doom Rock';
282
$GenreLookupSCMPX[250] = 'Japanese J-POP';
283
$GenreLookupSCMPX[251] = 'Japanese Seiyu';
284
$GenreLookupSCMPX[252] = 'Japanese Ambient Techno';
285
$GenreLookupSCMPX[253] = 'Japanese Moemoe';
286
$GenreLookupSCMPX[254] = 'Japanese Tokusatsu';
287
//$GenreLookupSCMPX[255] = 'Japanese Anime';
290
return ($allowSCMPXextended ? $GenreLookupSCMPX : $GenreLookup);
293
public static function LookupGenreName($genreid, $allowSCMPXextended=true) {
299
if (!is_numeric($genreid)) {
302
$genreid = intval($genreid); // to handle 3 or '3' or '03'
305
$GenreLookup = self::ArrayOfGenres($allowSCMPXextended);
306
return (isset($GenreLookup[$genreid]) ? $GenreLookup[$genreid] : false);
309
public static function LookupGenreID($genre, $allowSCMPXextended=false) {
310
$GenreLookup = self::ArrayOfGenres($allowSCMPXextended);
311
$LowerCaseNoSpaceSearchTerm = strtolower(str_replace(' ', '', $genre));
312
foreach ($GenreLookup as $key => $value) {
313
if (strtolower(str_replace(' ', '', $value)) == $LowerCaseNoSpaceSearchTerm) {
320
public static function StandardiseID3v1GenreName($OriginalGenre) {
321
if (($GenreID = self::LookupGenreID($OriginalGenre)) !== false) {
322
return self::LookupGenreName($GenreID);
324
return $OriginalGenre;
327
public static function GenerateID3v1Tag($title, $artist, $album, $year, $genreid, $comment, $track='') {
329
$ID3v1Tag .= str_pad(trim(substr($title, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
330
$ID3v1Tag .= str_pad(trim(substr($artist, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
331
$ID3v1Tag .= str_pad(trim(substr($album, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
332
$ID3v1Tag .= str_pad(trim(substr($year, 0, 4)), 4, "\x00", STR_PAD_LEFT);
333
if (!empty($track) && ($track > 0) && ($track <= 255)) {
334
$ID3v1Tag .= str_pad(trim(substr($comment, 0, 28)), 28, "\x00", STR_PAD_RIGHT);
336
if (gettype($track) == 'string') {
337
$track = (int) $track;
339
$ID3v1Tag .= chr($track);
341
$ID3v1Tag .= str_pad(trim(substr($comment, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
343
if (($genreid < 0) || ($genreid > 147)) {
344
$genreid = 255; // 'unknown' genre
346
switch (gettype($genreid)) {
349
$ID3v1Tag .= chr(intval($genreid));
352
$ID3v1Tag .= chr(255); // 'unknown' genre