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.lyrics3.php //
12 // module for analyzing Lyrics3 tags //
13 // dependencies: module.tag.apetag.php (optional) //
15 /////////////////////////////////////////////////////////////////
17 if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
20 class getid3_lyrics3
extends getid3_handler
25 public function Analyze() {
26 $info = &$this->getid3
->info
;
28 // http://www.volweb.cz/str/tags.htm
30 if (!getid3_lib
::intValueSupported($info['filesize'])) {
31 $this->warning('Unable to check for Lyrics3 because file is larger than '.round(PHP_INT_MAX
/ 1073741824).'GB');
35 $this->fseek((0 - 128 - 9 - 6), SEEK_END
); // end - ID3v1 - "LYRICSEND" - [Lyrics3size]
36 $lyrics3_id3v1 = $this->fread(128 +
9 +
6);
37 $lyrics3lsz = (int) substr($lyrics3_id3v1, 0, 6); // Lyrics3size
38 $lyrics3end = substr($lyrics3_id3v1, 6, 9); // LYRICSEND or LYRICS200
39 $id3v1tag = substr($lyrics3_id3v1, 15, 128); // ID3v1
41 if ($lyrics3end == 'LYRICSEND') {
42 // Lyrics3v1, ID3v1, no APE
45 $lyrics3offset = $info['filesize'] - 128 - $lyrics3size;
48 } elseif ($lyrics3end == 'LYRICS200') {
49 // Lyrics3v2, ID3v1, no APE
51 // LSZ = lyrics + 'LYRICSBEGIN'; add 6-byte size field; add 'LYRICS200'
52 $lyrics3size = $lyrics3lsz +
6 +
strlen('LYRICS200');
53 $lyrics3offset = $info['filesize'] - 128 - $lyrics3size;
56 } elseif (substr(strrev($lyrics3_id3v1), 0, 9) == strrev('LYRICSEND')) {
57 // Lyrics3v1, no ID3v1, no APE
60 $lyrics3offset = $info['filesize'] - $lyrics3size;
62 $lyrics3offset = $info['filesize'] - $lyrics3size;
64 } elseif (substr(strrev($lyrics3_id3v1), 0, 9) == strrev('LYRICS200')) {
66 // Lyrics3v2, no ID3v1, no APE
68 $lyrics3size = (int) strrev(substr(strrev($lyrics3_id3v1), 9, 6)) +
6 +
strlen('LYRICS200'); // LSZ = lyrics + 'LYRICSBEGIN'; add 6-byte size field; add 'LYRICS200'
69 $lyrics3offset = $info['filesize'] - $lyrics3size;
74 if (isset($info['ape']['tag_offset_start']) && ($info['ape']['tag_offset_start'] > 15)) {
76 $this->fseek($info['ape']['tag_offset_start'] - 15);
77 $lyrics3lsz = $this->fread(6);
78 $lyrics3end = $this->fread(9);
80 if ($lyrics3end == 'LYRICSEND') {
81 // Lyrics3v1, APE, maybe ID3v1
84 $lyrics3offset = $info['ape']['tag_offset_start'] - $lyrics3size;
85 $info['avdataend'] = $lyrics3offset;
87 $this->warning('APE tag located after Lyrics3, will probably break Lyrics3 compatability');
89 } elseif ($lyrics3end == 'LYRICS200') {
90 // Lyrics3v2, APE, maybe ID3v1
92 $lyrics3size = $lyrics3lsz +
6 +
strlen('LYRICS200'); // LSZ = lyrics + 'LYRICSBEGIN'; add 6-byte size field; add 'LYRICS200'
93 $lyrics3offset = $info['ape']['tag_offset_start'] - $lyrics3size;
95 $this->warning('APE tag located after Lyrics3, will probably break Lyrics3 compatability');
103 if (isset($lyrics3offset) && isset($lyrics3version) && isset($lyrics3size)) {
104 $info['avdataend'] = $lyrics3offset;
105 $this->getLyrics3Data($lyrics3offset, $lyrics3version, $lyrics3size);
107 if (!isset($info['ape'])) {
108 if (isset($info['lyrics3']['tag_offset_start'])) {
109 $GETID3_ERRORARRAY = &$info['warning'];
110 getid3_lib
::IncludeDependency(GETID3_INCLUDEPATH
.'module.tag.apetag.php', __FILE__
, true);
111 $getid3_temp = new getID3();
112 $getid3_temp->openfile($this->getid3
->filename
, $this->getid3
->info
['filesize'], $this->getid3
->fp
);
113 $getid3_apetag = new getid3_apetag($getid3_temp);
114 $getid3_apetag->overrideendoffset
= $info['lyrics3']['tag_offset_start'];
115 $getid3_apetag->Analyze();
116 if (!empty($getid3_temp->info
['ape'])) {
117 $info['ape'] = $getid3_temp->info
['ape'];
119 if (!empty($getid3_temp->info
['replay_gain'])) {
120 $info['replay_gain'] = $getid3_temp->info
['replay_gain'];
122 unset($getid3_temp, $getid3_apetag);
124 $this->warning('Lyrics3 and APE tags appear to have become entangled (most likely due to updating the APE tags with a non-Lyrics3-aware tagger)');
134 * @param int $endoffset
135 * @param int $version
140 public function getLyrics3Data($endoffset, $version, $length) {
141 // http://www.volweb.cz/str/tags.htm
143 $info = &$this->getid3
->info
;
145 if (!getid3_lib
::intValueSupported($endoffset)) {
146 $this->warning('Unable to check for Lyrics3 because file is larger than '.round(PHP_INT_MAX
/ 1073741824).'GB');
150 $this->fseek($endoffset);
154 $rawdata = $this->fread($length);
156 $ParsedLyrics3 = array();
158 $ParsedLyrics3['raw']['lyrics3version'] = $version;
159 $ParsedLyrics3['raw']['lyrics3tagsize'] = $length;
160 $ParsedLyrics3['tag_offset_start'] = $endoffset;
161 $ParsedLyrics3['tag_offset_end'] = $endoffset +
$length - 1;
163 if (substr($rawdata, 0, 11) != 'LYRICSBEGIN') {
164 if (strpos($rawdata, 'LYRICSBEGIN') !== false) {
166 $this->warning('"LYRICSBEGIN" expected at '.$endoffset.' but actually found at '.($endoffset +
strpos($rawdata, 'LYRICSBEGIN')).' - this is invalid for Lyrics3 v'.$version);
167 $info['avdataend'] = $endoffset +
strpos($rawdata, 'LYRICSBEGIN');
168 $rawdata = substr($rawdata, strpos($rawdata, 'LYRICSBEGIN'));
169 $length = strlen($rawdata);
170 $ParsedLyrics3['tag_offset_start'] = $info['avdataend'];
171 $ParsedLyrics3['raw']['lyrics3tagsize'] = $length;
175 $this->error('"LYRICSBEGIN" expected at '.$endoffset.' but found "'.substr($rawdata, 0, 11).'" instead');
185 if (substr($rawdata, strlen($rawdata) - 9, 9) == 'LYRICSEND') {
186 $ParsedLyrics3['raw']['LYR'] = trim(substr($rawdata, 11, strlen($rawdata) - 11 - 9));
187 $this->Lyrics3LyricsTimestampParse($ParsedLyrics3);
189 $this->error('"LYRICSEND" expected at '.($this->ftell() - 11 +
$length - 9).' but found "'.substr($rawdata, strlen($rawdata) - 9, 9).'" instead');
195 if (substr($rawdata, strlen($rawdata) - 9, 9) == 'LYRICS200') {
196 $ParsedLyrics3['raw']['unparsed'] = substr($rawdata, 11, strlen($rawdata) - 11 - 9 - 6); // LYRICSBEGIN + LYRICS200 + LSZ
197 $rawdata = $ParsedLyrics3['raw']['unparsed'];
198 while (strlen($rawdata) > 0) {
199 $fieldname = substr($rawdata, 0, 3);
200 $fieldsize = (int) substr($rawdata, 3, 5);
201 $ParsedLyrics3['raw'][$fieldname] = substr($rawdata, 8, $fieldsize);
202 $rawdata = substr($rawdata, 3 +
5 +
$fieldsize);
205 if (isset($ParsedLyrics3['raw']['IND'])) {
207 $flagnames = array('lyrics', 'timestamps', 'inhibitrandom');
208 foreach ($flagnames as $flagname) {
209 if (strlen($ParsedLyrics3['raw']['IND']) > $i++
) {
210 $ParsedLyrics3['flags'][$flagname] = $this->IntString2Bool(substr($ParsedLyrics3['raw']['IND'], $i, 1 - 1));
215 $fieldnametranslation = array('ETT'=>'title', 'EAR'=>'artist', 'EAL'=>'album', 'INF'=>'comment', 'AUT'=>'author');
216 foreach ($fieldnametranslation as $key => $value) {
217 if (isset($ParsedLyrics3['raw'][$key])) {
218 $ParsedLyrics3['comments'][$value][] = trim($ParsedLyrics3['raw'][$key]);
222 if (isset($ParsedLyrics3['raw']['IMG'])) {
223 $imagestrings = explode("\r\n", $ParsedLyrics3['raw']['IMG']);
224 foreach ($imagestrings as $key => $imagestring) {
225 if (strpos($imagestring, '||') !== false) {
226 $imagearray = explode('||', $imagestring);
227 $ParsedLyrics3['images'][$key]['filename'] = (isset($imagearray[0]) ?
$imagearray[0] : '');
228 $ParsedLyrics3['images'][$key]['description'] = (isset($imagearray[1]) ?
$imagearray[1] : '');
229 $ParsedLyrics3['images'][$key]['timestamp'] = $this->Lyrics3Timestamp2Seconds(isset($imagearray[2]) ?
$imagearray[2] : '');
233 if (isset($ParsedLyrics3['raw']['LYR'])) {
234 $this->Lyrics3LyricsTimestampParse($ParsedLyrics3);
237 $this->error('"LYRICS200" expected at '.($this->ftell() - 11 +
$length - 9).' but found "'.substr($rawdata, strlen($rawdata) - 9, 9).'" instead');
243 $this->error('Cannot process Lyrics3 version '.$version.' (only v1 and v2)');
248 if (isset($info['id3v1']['tag_offset_start']) && ($info['id3v1']['tag_offset_start'] <= $ParsedLyrics3['tag_offset_end'])) {
249 $this->warning('ID3v1 tag information ignored since it appears to be a false synch in Lyrics3 tag data');
250 unset($info['id3v1']);
251 foreach ($info['warning'] as $key => $value) {
252 if ($value == 'Some ID3v1 fields do not use NULL characters for padding') {
253 unset($info['warning'][$key]);
254 sort($info['warning']);
260 $info['lyrics3'] = $ParsedLyrics3;
266 * @param string $rawtimestamp
270 public function Lyrics3Timestamp2Seconds($rawtimestamp) {
271 if (preg_match('#^\\[([0-9]{2}):([0-9]{2})\\]$#', $rawtimestamp, $regs)) {
272 return (int) (($regs[1] * 60) +
$regs[2]);
278 * @param array $Lyrics3data
282 public function Lyrics3LyricsTimestampParse(&$Lyrics3data) {
283 $lyricsarray = explode("\r\n", $Lyrics3data['raw']['LYR']);
284 $notimestamplyricsarray = array();
285 foreach ($lyricsarray as $key => $lyricline) {
287 unset($thislinetimestamps);
288 while (preg_match('#^(\\[[0-9]{2}:[0-9]{2}\\])#', $lyricline, $regs)) {
289 $thislinetimestamps[] = $this->Lyrics3Timestamp2Seconds($regs[0]);
290 $lyricline = str_replace($regs[0], '', $lyricline);
292 $notimestamplyricsarray[$key] = $lyricline;
293 if (isset($thislinetimestamps) && is_array($thislinetimestamps)) {
294 sort($thislinetimestamps);
295 foreach ($thislinetimestamps as $timestampkey => $timestamp) {
296 if (isset($Lyrics3data['synchedlyrics'][$timestamp])) {
297 // timestamps only have a 1-second resolution, it's possible that multiple lines
298 // could have the same timestamp, if so, append
299 $Lyrics3data['synchedlyrics'][$timestamp] .= "\r\n".$lyricline;
301 $Lyrics3data['synchedlyrics'][$timestamp] = $lyricline;
306 $Lyrics3data['unsynchedlyrics'] = implode("\r\n", $notimestamplyricsarray);
307 if (isset($Lyrics3data['synchedlyrics']) && is_array($Lyrics3data['synchedlyrics'])) {
308 ksort($Lyrics3data['synchedlyrics']);
314 * @param string $char
318 public function IntString2Bool($char) {
321 } elseif ($char == '0') {