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 $info['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;
64 // ID3v1 data is supposed to be padded with NULL characters, but some taggers pad with spaces
65 $GoodFormatID3v1tag = $this->GenerateID3v1Tag(
66 $ParsedID3v1['title'],
67 $ParsedID3v1['artist'],
68 $ParsedID3v1['album'],
70 (isset($ParsedID3v1['genre']) ? $this->LookupGenreID($ParsedID3v1['genre']) : false),
71 $ParsedID3v1['comment'],
72 (!empty($ParsedID3v1['track']) ? $ParsedID3v1['track'] : ''));
73 $ParsedID3v1['padding_valid'] = true;
74 if ($id3v1tag !== $GoodFormatID3v1tag) {
75 $ParsedID3v1['padding_valid'] = false;
76 $info['warning'][] = 'Some ID3v1 fields do not use NULL characters for padding';
79 $ParsedID3v1['tag_offset_end'] = $info['filesize'];
80 $ParsedID3v1['tag_offset_start'] = $ParsedID3v1['tag_offset_end'] - 128;
82 $info['id3v1'] = $ParsedID3v1;
85 if (substr($preid3v1, 0, 3) == 'TAG') {
86 // The way iTunes handles tags is, well, brain-damaged.
87 // It completely ignores v1 if ID3v2 is present.
88 // This goes as far as adding a new v1 tag *even if there already is one*
90 // A suspected double-ID3v1 tag has been detected, but it could be that
91 // the "TAG" identifier is a legitimate part of an APE or Lyrics3 tag
92 if (substr($preid3v1, 96, 8) == 'APETAGEX') {
93 // an APE tag footer was found before the last ID3v1, assume false "TAG" synch
94 } elseif (substr($preid3v1, 119, 6) == 'LYRICS') {
95 // a Lyrics3 tag footer was found before the last ID3v1, assume false "TAG" synch
97 // APE and Lyrics3 footers not found - assume double ID3v1
98 $info['warning'][] = 'Duplicate ID3v1 tag detected - this has been known to happen with iTunes';
99 $info['avdataend'] -= 128;
106 public static function cutfield($str) {
107 return trim(substr($str, 0, strcspn($str, "\x00")));
110 public static function ArrayOfGenres($allowSCMPXextended=false) {
111 static $GenreLookup = array(
145 33 => 'Instrumental',
158 46 => 'Instrumental Pop',
159 47 => 'Instrumental Rock',
163 51 => 'Techno-Industrial',
168 56 => 'Southern Rock',
173 61 => 'Christian Rap',
176 64 => 'Native American',
194 82 => 'National Folk',
204 92 => 'Progressive Rock',
205 93 => 'Psychedelic Rock',
206 94 => 'Symphonic Rock',
210 98 => 'Easy Listening',
216 104 => 'Chamber Music',
221 109 => 'Porn Groove',
229 117 => 'Power Ballad',
230 118 => 'Rhythmic Soul',
239 127 => 'Drum & Bass',
248 136 => 'Christian Gangsta Rap',
249 137 => 'Heavy Metal',
250 138 => 'Black Metal',
252 140 => 'Contemporary Christian',
253 141 => 'Christian Rock',
256 144 => 'Thrash Metal',
267 static $GenreLookupSCMPX = array();
268 if ($allowSCMPXextended && empty($GenreLookupSCMPX)) {
269 $GenreLookupSCMPX = $GenreLookup;
270 // http://www.geocities.co.jp/SiliconValley-Oakland/3664/alittle.html#GenreExtended
271 // Extended ID3v1 genres invented by SCMPX
272 // Note that 255 "Japanese Anime" conflicts with standard "Unknown"
273 $GenreLookupSCMPX[240] = 'Sacred';
274 $GenreLookupSCMPX[241] = 'Northern Europe';
275 $GenreLookupSCMPX[242] = 'Irish & Scottish';
276 $GenreLookupSCMPX[243] = 'Scotland';
277 $GenreLookupSCMPX[244] = 'Ethnic Europe';
278 $GenreLookupSCMPX[245] = 'Enka';
279 $GenreLookupSCMPX[246] = 'Children\'s Song';
280 $GenreLookupSCMPX[247] = 'Japanese Sky';
281 $GenreLookupSCMPX[248] = 'Japanese Heavy Rock';
282 $GenreLookupSCMPX[249] = 'Japanese Doom Rock';
283 $GenreLookupSCMPX[250] = 'Japanese J-POP';
284 $GenreLookupSCMPX[251] = 'Japanese Seiyu';
285 $GenreLookupSCMPX[252] = 'Japanese Ambient Techno';
286 $GenreLookupSCMPX[253] = 'Japanese Moemoe';
287 $GenreLookupSCMPX[254] = 'Japanese Tokusatsu';
288 //$GenreLookupSCMPX[255] = 'Japanese Anime';
291 return ($allowSCMPXextended ? $GenreLookupSCMPX : $GenreLookup);
294 public static function LookupGenreName($genreid, $allowSCMPXextended=true) {
300 if (!is_numeric($genreid)) {
303 $genreid = intval($genreid); // to handle 3 or '3' or '03'
306 $GenreLookup = self::ArrayOfGenres($allowSCMPXextended);
307 return (isset($GenreLookup[$genreid]) ? $GenreLookup[$genreid] : false);
310 public static function LookupGenreID($genre, $allowSCMPXextended=false) {
311 $GenreLookup = self::ArrayOfGenres($allowSCMPXextended);
312 $LowerCaseNoSpaceSearchTerm = strtolower(str_replace(' ', '', $genre));
313 foreach ($GenreLookup as $key => $value) {
314 if (strtolower(str_replace(' ', '', $value)) == $LowerCaseNoSpaceSearchTerm) {
321 public static function StandardiseID3v1GenreName($OriginalGenre) {
322 if (($GenreID = self::LookupGenreID($OriginalGenre)) !== false) {
323 return self::LookupGenreName($GenreID);
325 return $OriginalGenre;
328 public static function GenerateID3v1Tag($title, $artist, $album, $year, $genreid, $comment, $track='') {
330 $ID3v1Tag .= str_pad(trim(substr($title, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
331 $ID3v1Tag .= str_pad(trim(substr($artist, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
332 $ID3v1Tag .= str_pad(trim(substr($album, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
333 $ID3v1Tag .= str_pad(trim(substr($year, 0, 4)), 4, "\x00", STR_PAD_LEFT);
334 if (!empty($track) && ($track > 0) && ($track <= 255)) {
335 $ID3v1Tag .= str_pad(trim(substr($comment, 0, 28)), 28, "\x00", STR_PAD_RIGHT);
337 if (gettype($track) == 'string') {
338 $track = (int) $track;
340 $ID3v1Tag .= chr($track);
342 $ID3v1Tag .= str_pad(trim(substr($comment, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
344 if (($genreid < 0) || ($genreid > 147)) {
345 $genreid = 255; // 'unknown' genre
347 switch (gettype($genreid)) {
350 $ID3v1Tag .= chr(intval($genreid));
353 $ID3v1Tag .= chr(255); // 'unknown' genre