2 /////////////////////////////////////////////////////////////////
3 /// getID3() by James Heinrich <info@getid3.org> //
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
6 // also https://github.com/JamesHeinrich/getID3 //
7 /////////////////////////////////////////////////////////////////
8 // See readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
11 // module.tag.id3v1.php //
12 // module for analyzing ID3v1 tags //
13 // dependencies: NONE //
15 /////////////////////////////////////////////////////////////////
18 class getid3_id3v1
extends getid3_handler
21 public function Analyze() {
22 $info = &$this->getid3
->info
;
24 if (!getid3_lib
::intValueSupported($info['filesize'])) {
25 $this->warning('Unable to check for ID3v1 because file is larger than '.round(PHP_INT_MAX
/ 1073741824).'GB');
29 $this->fseek(-256, SEEK_END
);
30 $preid3v1 = $this->fread(128);
31 $id3v1tag = $this->fread(128);
33 if (substr($id3v1tag, 0, 3) == 'TAG') {
35 $info['avdataend'] = $info['filesize'] - 128;
37 $ParsedID3v1['title'] = $this->cutfield(substr($id3v1tag, 3, 30));
38 $ParsedID3v1['artist'] = $this->cutfield(substr($id3v1tag, 33, 30));
39 $ParsedID3v1['album'] = $this->cutfield(substr($id3v1tag, 63, 30));
40 $ParsedID3v1['year'] = $this->cutfield(substr($id3v1tag, 93, 4));
41 $ParsedID3v1['comment'] = substr($id3v1tag, 97, 30); // can't remove nulls yet, track detection depends on them
42 $ParsedID3v1['genreid'] = ord(substr($id3v1tag, 127, 1));
44 // If second-last byte of comment field is null and last byte of comment field is non-null
45 // then this is ID3v1.1 and the comment field is 28 bytes long and the 30th byte is the track number
46 if (($id3v1tag{125} === "\x00") && ($id3v1tag{126} !== "\x00")) {
47 $ParsedID3v1['track'] = ord(substr($ParsedID3v1['comment'], 29, 1));
48 $ParsedID3v1['comment'] = substr($ParsedID3v1['comment'], 0, 28);
50 $ParsedID3v1['comment'] = $this->cutfield($ParsedID3v1['comment']);
52 $ParsedID3v1['genre'] = $this->LookupGenreName($ParsedID3v1['genreid']);
53 if (!empty($ParsedID3v1['genre'])) {
54 unset($ParsedID3v1['genreid']);
56 if (isset($ParsedID3v1['genre']) && (empty($ParsedID3v1['genre']) ||
($ParsedID3v1['genre'] == 'Unknown'))) {
57 unset($ParsedID3v1['genre']);
60 foreach ($ParsedID3v1 as $key => $value) {
61 $ParsedID3v1['comments'][$key][0] = $value;
63 // ID3v1 encoding detection hack START
64 // ID3v1 is defined as always using ISO-8859-1 encoding, but it is not uncommon to find files tagged with ID3v1 using Windows-1251 or other character sets
65 // Since ID3v1 has no concept of character sets there is no certain way to know we have the correct non-ISO-8859-1 character set, but we can guess
66 $ID3v1encoding = 'ISO-8859-1';
67 foreach ($ParsedID3v1['comments'] as $tag_key => $valuearray) {
68 foreach ($valuearray as $key => $value) {
69 if (preg_match('#^[\\x00-\\x40\\xA8\\B8\\x80-\\xFF]+$#', $value)) {
70 foreach (array('Windows-1251', 'KOI8-R') as $id3v1_bad_encoding) {
71 if (function_exists('mb_convert_encoding') && @mb_convert_encoding
($value, $id3v1_bad_encoding, $id3v1_bad_encoding) === $value) {
72 $ID3v1encoding = $id3v1_bad_encoding;
74 } elseif (function_exists('iconv') && @iconv
($id3v1_bad_encoding, $id3v1_bad_encoding, $value) === $value) {
75 $ID3v1encoding = $id3v1_bad_encoding;
82 // ID3v1 encoding detection hack END
84 // ID3v1 data is supposed to be padded with NULL characters, but some taggers pad with spaces
85 $GoodFormatID3v1tag = $this->GenerateID3v1Tag(
86 $ParsedID3v1['title'],
87 $ParsedID3v1['artist'],
88 $ParsedID3v1['album'],
90 (isset($ParsedID3v1['genre']) ?
$this->LookupGenreID($ParsedID3v1['genre']) : false),
91 $ParsedID3v1['comment'],
92 (!empty($ParsedID3v1['track']) ?
$ParsedID3v1['track'] : ''));
93 $ParsedID3v1['padding_valid'] = true;
94 if ($id3v1tag !== $GoodFormatID3v1tag) {
95 $ParsedID3v1['padding_valid'] = false;
96 $this->warning('Some ID3v1 fields do not use NULL characters for padding');
99 $ParsedID3v1['tag_offset_end'] = $info['filesize'];
100 $ParsedID3v1['tag_offset_start'] = $ParsedID3v1['tag_offset_end'] - 128;
102 $info['id3v1'] = $ParsedID3v1;
103 $info['id3v1']['encoding'] = $ID3v1encoding;
106 if (substr($preid3v1, 0, 3) == 'TAG') {
107 // The way iTunes handles tags is, well, brain-damaged.
108 // It completely ignores v1 if ID3v2 is present.
109 // This goes as far as adding a new v1 tag *even if there already is one*
111 // A suspected double-ID3v1 tag has been detected, but it could be that
112 // the "TAG" identifier is a legitimate part of an APE or Lyrics3 tag
113 if (substr($preid3v1, 96, 8) == 'APETAGEX') {
114 // an APE tag footer was found before the last ID3v1, assume false "TAG" synch
115 } elseif (substr($preid3v1, 119, 6) == 'LYRICS') {
116 // a Lyrics3 tag footer was found before the last ID3v1, assume false "TAG" synch
118 // APE and Lyrics3 footers not found - assume double ID3v1
119 $this->warning('Duplicate ID3v1 tag detected - this has been known to happen with iTunes');
120 $info['avdataend'] -= 128;
127 public static function cutfield($str) {
128 return trim(substr($str, 0, strcspn($str, "\x00")));
131 public static function ArrayOfGenres($allowSCMPXextended=false) {
132 static $GenreLookup = array(
166 33 => 'Instrumental',
179 46 => 'Instrumental Pop',
180 47 => 'Instrumental Rock',
184 51 => 'Techno-Industrial',
189 56 => 'Southern Rock',
194 61 => 'Christian Rap',
197 64 => 'Native American',
215 82 => 'National Folk',
225 92 => 'Progressive Rock',
226 93 => 'Psychedelic Rock',
227 94 => 'Symphonic Rock',
231 98 => 'Easy Listening',
237 104 => 'Chamber Music',
242 109 => 'Porn Groove',
250 117 => 'Power Ballad',
251 118 => 'Rhythmic Soul',
260 127 => 'Drum & Bass',
269 136 => 'Christian Gangsta Rap',
270 137 => 'Heavy Metal',
271 138 => 'Black Metal',
273 140 => 'Contemporary Christian',
274 141 => 'Christian Rock',
277 144 => 'Thrash Metal',
288 static $GenreLookupSCMPX = array();
289 if ($allowSCMPXextended && empty($GenreLookupSCMPX)) {
290 $GenreLookupSCMPX = $GenreLookup;
291 // http://www.geocities.co.jp/SiliconValley-Oakland/3664/alittle.html#GenreExtended
292 // Extended ID3v1 genres invented by SCMPX
293 // Note that 255 "Japanese Anime" conflicts with standard "Unknown"
294 $GenreLookupSCMPX[240] = 'Sacred';
295 $GenreLookupSCMPX[241] = 'Northern Europe';
296 $GenreLookupSCMPX[242] = 'Irish & Scottish';
297 $GenreLookupSCMPX[243] = 'Scotland';
298 $GenreLookupSCMPX[244] = 'Ethnic Europe';
299 $GenreLookupSCMPX[245] = 'Enka';
300 $GenreLookupSCMPX[246] = 'Children\'s Song';
301 $GenreLookupSCMPX[247] = 'Japanese Sky';
302 $GenreLookupSCMPX[248] = 'Japanese Heavy Rock';
303 $GenreLookupSCMPX[249] = 'Japanese Doom Rock';
304 $GenreLookupSCMPX[250] = 'Japanese J-POP';
305 $GenreLookupSCMPX[251] = 'Japanese Seiyu';
306 $GenreLookupSCMPX[252] = 'Japanese Ambient Techno';
307 $GenreLookupSCMPX[253] = 'Japanese Moemoe';
308 $GenreLookupSCMPX[254] = 'Japanese Tokusatsu';
309 //$GenreLookupSCMPX[255] = 'Japanese Anime';
312 return ($allowSCMPXextended ?
$GenreLookupSCMPX : $GenreLookup);
315 public static function LookupGenreName($genreid, $allowSCMPXextended=true) {
321 if (!is_numeric($genreid)) {
324 $genreid = intval($genreid); // to handle 3 or '3' or '03'
327 $GenreLookup = self
::ArrayOfGenres($allowSCMPXextended);
328 return (isset($GenreLookup[$genreid]) ?
$GenreLookup[$genreid] : false);
331 public static function LookupGenreID($genre, $allowSCMPXextended=false) {
332 $GenreLookup = self
::ArrayOfGenres($allowSCMPXextended);
333 $LowerCaseNoSpaceSearchTerm = strtolower(str_replace(' ', '', $genre));
334 foreach ($GenreLookup as $key => $value) {
335 if (strtolower(str_replace(' ', '', $value)) == $LowerCaseNoSpaceSearchTerm) {
342 public static function StandardiseID3v1GenreName($OriginalGenre) {
343 if (($GenreID = self
::LookupGenreID($OriginalGenre)) !== false) {
344 return self
::LookupGenreName($GenreID);
346 return $OriginalGenre;
349 public static function GenerateID3v1Tag($title, $artist, $album, $year, $genreid, $comment, $track='') {
351 $ID3v1Tag .= str_pad(trim(substr($title, 0, 30)), 30, "\x00", STR_PAD_RIGHT
);
352 $ID3v1Tag .= str_pad(trim(substr($artist, 0, 30)), 30, "\x00", STR_PAD_RIGHT
);
353 $ID3v1Tag .= str_pad(trim(substr($album, 0, 30)), 30, "\x00", STR_PAD_RIGHT
);
354 $ID3v1Tag .= str_pad(trim(substr($year, 0, 4)), 4, "\x00", STR_PAD_LEFT
);
355 if (!empty($track) && ($track > 0) && ($track <= 255)) {
356 $ID3v1Tag .= str_pad(trim(substr($comment, 0, 28)), 28, "\x00", STR_PAD_RIGHT
);
358 if (gettype($track) == 'string') {
359 $track = (int) $track;
361 $ID3v1Tag .= chr($track);
363 $ID3v1Tag .= str_pad(trim(substr($comment, 0, 30)), 30, "\x00", STR_PAD_RIGHT
);
365 if (($genreid < 0) ||
($genreid > 147)) {
366 $genreid = 255; // 'unknown' genre
368 switch (gettype($genreid)) {
371 $ID3v1Tag .= chr(intval($genreid));
374 $ID3v1Tag .= chr(255); // 'unknown' genre