3 /////////////////////////////////////////////////////////////////
4 /// getID3() by James Heinrich <info@getid3.org> //
5 // available at https://github.com/JamesHeinrich/getID3 //
6 // or https://www.getid3.org //
7 // or http://getid3.sourceforge.net //
8 // see readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
11 // module.tag.id3v1.php //
12 // module for analyzing ID3v1 tags //
13 // dependencies: NONE //
15 /////////////////////////////////////////////////////////////////
17 if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
21 class getid3_id3v1
extends getid3_handler
26 public function Analyze() {
27 $info = &$this->getid3
->info
;
29 if (!getid3_lib
::intValueSupported($info['filesize'])) {
30 $this->warning('Unable to check for ID3v1 because file is larger than '.round(PHP_INT_MAX
/ 1073741824).'GB');
34 $this->fseek(-256, SEEK_END
);
35 $preid3v1 = $this->fread(128);
36 $id3v1tag = $this->fread(128);
38 if (substr($id3v1tag, 0, 3) == 'TAG') {
40 $info['avdataend'] = $info['filesize'] - 128;
42 $ParsedID3v1['title'] = $this->cutfield(substr($id3v1tag, 3, 30));
43 $ParsedID3v1['artist'] = $this->cutfield(substr($id3v1tag, 33, 30));
44 $ParsedID3v1['album'] = $this->cutfield(substr($id3v1tag, 63, 30));
45 $ParsedID3v1['year'] = $this->cutfield(substr($id3v1tag, 93, 4));
46 $ParsedID3v1['comment'] = substr($id3v1tag, 97, 30); // can't remove nulls yet, track detection depends on them
47 $ParsedID3v1['genreid'] = ord(substr($id3v1tag, 127, 1));
49 // If second-last byte of comment field is null and last byte of comment field is non-null
50 // then this is ID3v1.1 and the comment field is 28 bytes long and the 30th byte is the track number
51 if (($id3v1tag[125] === "\x00") && ($id3v1tag[126] !== "\x00")) {
52 $ParsedID3v1['track_number'] = ord(substr($ParsedID3v1['comment'], 29, 1));
53 $ParsedID3v1['comment'] = substr($ParsedID3v1['comment'], 0, 28);
55 $ParsedID3v1['comment'] = $this->cutfield($ParsedID3v1['comment']);
57 $ParsedID3v1['genre'] = $this->LookupGenreName($ParsedID3v1['genreid']);
58 if (!empty($ParsedID3v1['genre'])) {
59 unset($ParsedID3v1['genreid']);
61 if (isset($ParsedID3v1['genre']) && (empty($ParsedID3v1['genre']) ||
($ParsedID3v1['genre'] == 'Unknown'))) {
62 unset($ParsedID3v1['genre']);
65 foreach ($ParsedID3v1 as $key => $value) {
66 $ParsedID3v1['comments'][$key][0] = $value;
68 $ID3v1encoding = $this->getid3
->encoding_id3v1
;
69 if ($this->getid3
->encoding_id3v1_autodetect
) {
70 // ID3v1 encoding detection hack START
71 // 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
72 // 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
73 foreach ($ParsedID3v1['comments'] as $tag_key => $valuearray) {
74 foreach ($valuearray as $key => $value) {
75 if (preg_match('#^[\\x00-\\x40\\x80-\\xFF]+$#', $value) && !ctype_digit((string) $value)) { // check for strings with only characters above chr(128) and punctuation/numbers, but not just numeric strings (e.g. track numbers or years)
76 foreach (array('Windows-1251', 'KOI8-R') as $id3v1_bad_encoding) {
77 if (function_exists('mb_convert_encoding') && @mb_convert_encoding
($value, $id3v1_bad_encoding, $id3v1_bad_encoding) === $value) {
78 $ID3v1encoding = $id3v1_bad_encoding;
79 $this->warning('ID3v1 detected as '.$id3v1_bad_encoding.' text encoding in '.$tag_key);
81 } elseif (function_exists('iconv') && @iconv
($id3v1_bad_encoding, $id3v1_bad_encoding, $value) === $value) {
82 $ID3v1encoding = $id3v1_bad_encoding;
83 $this->warning('ID3v1 detected as '.$id3v1_bad_encoding.' text encoding in '.$tag_key);
90 // ID3v1 encoding detection hack END
93 // ID3v1 data is supposed to be padded with NULL characters, but some taggers pad with spaces
94 $GoodFormatID3v1tag = $this->GenerateID3v1Tag(
95 $ParsedID3v1['title'],
96 $ParsedID3v1['artist'],
97 $ParsedID3v1['album'],
99 (isset($ParsedID3v1['genre']) ?
$this->LookupGenreID($ParsedID3v1['genre']) : false),
100 $ParsedID3v1['comment'],
101 (!empty($ParsedID3v1['track_number']) ?
$ParsedID3v1['track_number'] : ''));
102 $ParsedID3v1['padding_valid'] = true;
103 if ($id3v1tag !== $GoodFormatID3v1tag) {
104 $ParsedID3v1['padding_valid'] = false;
105 $this->warning('Some ID3v1 fields do not use NULL characters for padding');
108 $ParsedID3v1['tag_offset_end'] = $info['filesize'];
109 $ParsedID3v1['tag_offset_start'] = $ParsedID3v1['tag_offset_end'] - 128;
111 $info['id3v1'] = $ParsedID3v1;
112 $info['id3v1']['encoding'] = $ID3v1encoding;
115 if (substr($preid3v1, 0, 3) == 'TAG') {
116 // The way iTunes handles tags is, well, brain-damaged.
117 // It completely ignores v1 if ID3v2 is present.
118 // This goes as far as adding a new v1 tag *even if there already is one*
120 // A suspected double-ID3v1 tag has been detected, but it could be that
121 // the "TAG" identifier is a legitimate part of an APE or Lyrics3 tag
122 if (substr($preid3v1, 96, 8) == 'APETAGEX') {
123 // an APE tag footer was found before the last ID3v1, assume false "TAG" synch
124 } elseif (substr($preid3v1, 119, 6) == 'LYRICS') {
125 // a Lyrics3 tag footer was found before the last ID3v1, assume false "TAG" synch
127 // APE and Lyrics3 footers not found - assume double ID3v1
128 $this->warning('Duplicate ID3v1 tag detected - this has been known to happen with iTunes');
129 $info['avdataend'] -= 128;
141 public static function cutfield($str) {
142 return trim(substr($str, 0, strcspn($str, "\x00")));
146 * @param bool $allowSCMPXextended
150 public static function ArrayOfGenres($allowSCMPXextended=false) {
151 static $GenreLookup = array(
185 33 => 'Instrumental',
198 46 => 'Instrumental Pop',
199 47 => 'Instrumental Rock',
203 51 => 'Techno-Industrial',
208 56 => 'Southern Rock',
213 61 => 'Christian Rap',
216 64 => 'Native American',
234 82 => 'National Folk',
244 92 => 'Progressive Rock',
245 93 => 'Psychedelic Rock',
246 94 => 'Symphonic Rock',
250 98 => 'Easy Listening',
256 104 => 'Chamber Music',
261 109 => 'Porn Groove',
269 117 => 'Power Ballad',
270 118 => 'Rhythmic Soul',
279 127 => 'Drum & Bass',
288 136 => 'Christian Gangsta Rap',
289 137 => 'Heavy Metal',
290 138 => 'Black Metal',
292 140 => 'Contemporary Christian',
293 141 => 'Christian Rock',
296 144 => 'Thrash Metal',
307 static $GenreLookupSCMPX = array();
308 if ($allowSCMPXextended && empty($GenreLookupSCMPX)) {
309 $GenreLookupSCMPX = $GenreLookup;
310 // http://www.geocities.co.jp/SiliconValley-Oakland/3664/alittle.html#GenreExtended
311 // Extended ID3v1 genres invented by SCMPX
312 // Note that 255 "Japanese Anime" conflicts with standard "Unknown"
313 $GenreLookupSCMPX[240] = 'Sacred';
314 $GenreLookupSCMPX[241] = 'Northern Europe';
315 $GenreLookupSCMPX[242] = 'Irish & Scottish';
316 $GenreLookupSCMPX[243] = 'Scotland';
317 $GenreLookupSCMPX[244] = 'Ethnic Europe';
318 $GenreLookupSCMPX[245] = 'Enka';
319 $GenreLookupSCMPX[246] = 'Children\'s Song';
320 $GenreLookupSCMPX[247] = 'Japanese Sky';
321 $GenreLookupSCMPX[248] = 'Japanese Heavy Rock';
322 $GenreLookupSCMPX[249] = 'Japanese Doom Rock';
323 $GenreLookupSCMPX[250] = 'Japanese J-POP';
324 $GenreLookupSCMPX[251] = 'Japanese Seiyu';
325 $GenreLookupSCMPX[252] = 'Japanese Ambient Techno';
326 $GenreLookupSCMPX[253] = 'Japanese Moemoe';
327 $GenreLookupSCMPX[254] = 'Japanese Tokusatsu';
328 //$GenreLookupSCMPX[255] = 'Japanese Anime';
331 return ($allowSCMPXextended ?
$GenreLookupSCMPX : $GenreLookup);
335 * @param string $genreid
336 * @param bool $allowSCMPXextended
338 * @return string|false
340 public static function LookupGenreName($genreid, $allowSCMPXextended=true) {
346 if (!is_numeric($genreid)) {
349 $genreid = intval($genreid); // to handle 3 or '3' or '03'
352 $GenreLookup = self
::ArrayOfGenres($allowSCMPXextended);
353 return (isset($GenreLookup[$genreid]) ?
$GenreLookup[$genreid] : false);
357 * @param string $genre
358 * @param bool $allowSCMPXextended
360 * @return string|false
362 public static function LookupGenreID($genre, $allowSCMPXextended=false) {
363 $GenreLookup = self
::ArrayOfGenres($allowSCMPXextended);
364 $LowerCaseNoSpaceSearchTerm = strtolower(str_replace(' ', '', $genre));
365 foreach ($GenreLookup as $key => $value) {
366 if (strtolower(str_replace(' ', '', $value)) == $LowerCaseNoSpaceSearchTerm) {
374 * @param string $OriginalGenre
376 * @return string|false
378 public static function StandardiseID3v1GenreName($OriginalGenre) {
379 if (($GenreID = self
::LookupGenreID($OriginalGenre)) !== false) {
380 return self
::LookupGenreName($GenreID);
382 return $OriginalGenre;
386 * @param string $title
387 * @param string $artist
388 * @param string $album
389 * @param string $year
390 * @param int $genreid
391 * @param string $comment
392 * @param int|string $track
396 public static function GenerateID3v1Tag($title, $artist, $album, $year, $genreid, $comment, $track='') {
398 $ID3v1Tag .= str_pad(trim(substr($title, 0, 30)), 30, "\x00", STR_PAD_RIGHT
);
399 $ID3v1Tag .= str_pad(trim(substr($artist, 0, 30)), 30, "\x00", STR_PAD_RIGHT
);
400 $ID3v1Tag .= str_pad(trim(substr($album, 0, 30)), 30, "\x00", STR_PAD_RIGHT
);
401 $ID3v1Tag .= str_pad(trim(substr($year, 0, 4)), 4, "\x00", STR_PAD_LEFT
);
402 if (!empty($track) && ($track > 0) && ($track <= 255)) {
403 $ID3v1Tag .= str_pad(trim(substr($comment, 0, 28)), 28, "\x00", STR_PAD_RIGHT
);
405 if (gettype($track) == 'string') {
406 $track = (int) $track;
408 $ID3v1Tag .= chr($track);
410 $ID3v1Tag .= str_pad(trim(substr($comment, 0, 30)), 30, "\x00", STR_PAD_RIGHT
);
412 if (($genreid < 0) ||
($genreid > 147)) {
413 $genreid = 255; // 'unknown' genre
415 switch (gettype($genreid)) {
418 $ID3v1Tag .= chr(intval($genreid));
421 $ID3v1Tag .= chr(255); // 'unknown' genre