author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 13:49:49 +0100 | |
changeset 16 | a86126ab1dd4 |
parent 7 | cf61fcea0001 |
child 19 | 3d72ae0968f4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
16 | 2 |
|
0 | 3 |
///////////////////////////////////////////////////////////////// |
4 |
/// getID3() by James Heinrich <info@getid3.org> // |
|
16 | 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 // |
|
0 | 9 |
///////////////////////////////////////////////////////////////// |
10 |
/// // |
|
11 |
// module.tag.lyrics3.php // |
|
12 |
// module for analyzing Lyrics3 tags // |
|
13 |
// dependencies: module.tag.apetag.php (optional) // |
|
14 |
// /// |
|
15 |
///////////////////////////////////////////////////////////////// |
|
16 |
||
16 | 17 |
if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers |
18 |
exit; |
|
19 |
} |
|
0 | 20 |
class getid3_lyrics3 extends getid3_handler |
21 |
{ |
|
16 | 22 |
/** |
23 |
* @return bool |
|
24 |
*/ |
|
0 | 25 |
public function Analyze() { |
26 |
$info = &$this->getid3->info; |
|
27 |
||
28 |
// http://www.volweb.cz/str/tags.htm |
|
29 |
||
30 |
if (!getid3_lib::intValueSupported($info['filesize'])) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
31 |
$this->warning('Unable to check for Lyrics3 because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB'); |
0 | 32 |
return false; |
33 |
} |
|
34 |
||
5 | 35 |
$this->fseek((0 - 128 - 9 - 6), SEEK_END); // end - ID3v1 - "LYRICSEND" - [Lyrics3size] |
36 |
$lyrics3_id3v1 = $this->fread(128 + 9 + 6); |
|
16 | 37 |
$lyrics3lsz = (int) substr($lyrics3_id3v1, 0, 6); // Lyrics3size |
0 | 38 |
$lyrics3end = substr($lyrics3_id3v1, 6, 9); // LYRICSEND or LYRICS200 |
39 |
$id3v1tag = substr($lyrics3_id3v1, 15, 128); // ID3v1 |
|
40 |
||
41 |
if ($lyrics3end == 'LYRICSEND') { |
|
42 |
// Lyrics3v1, ID3v1, no APE |
|
43 |
||
44 |
$lyrics3size = 5100; |
|
45 |
$lyrics3offset = $info['filesize'] - 128 - $lyrics3size; |
|
46 |
$lyrics3version = 1; |
|
47 |
||
48 |
} elseif ($lyrics3end == 'LYRICS200') { |
|
49 |
// Lyrics3v2, ID3v1, no APE |
|
50 |
||
51 |
// LSZ = lyrics + 'LYRICSBEGIN'; add 6-byte size field; add 'LYRICS200' |
|
52 |
$lyrics3size = $lyrics3lsz + 6 + strlen('LYRICS200'); |
|
53 |
$lyrics3offset = $info['filesize'] - 128 - $lyrics3size; |
|
54 |
$lyrics3version = 2; |
|
55 |
||
56 |
} elseif (substr(strrev($lyrics3_id3v1), 0, 9) == strrev('LYRICSEND')) { |
|
57 |
// Lyrics3v1, no ID3v1, no APE |
|
58 |
||
59 |
$lyrics3size = 5100; |
|
60 |
$lyrics3offset = $info['filesize'] - $lyrics3size; |
|
61 |
$lyrics3version = 1; |
|
62 |
$lyrics3offset = $info['filesize'] - $lyrics3size; |
|
63 |
||
64 |
} elseif (substr(strrev($lyrics3_id3v1), 0, 9) == strrev('LYRICS200')) { |
|
65 |
||
66 |
// Lyrics3v2, no ID3v1, no APE |
|
67 |
||
16 | 68 |
$lyrics3size = (int) strrev(substr(strrev($lyrics3_id3v1), 9, 6)) + 6 + strlen('LYRICS200'); // LSZ = lyrics + 'LYRICSBEGIN'; add 6-byte size field; add 'LYRICS200' |
0 | 69 |
$lyrics3offset = $info['filesize'] - $lyrics3size; |
70 |
$lyrics3version = 2; |
|
71 |
||
72 |
} else { |
|
73 |
||
74 |
if (isset($info['ape']['tag_offset_start']) && ($info['ape']['tag_offset_start'] > 15)) { |
|
75 |
||
5 | 76 |
$this->fseek($info['ape']['tag_offset_start'] - 15); |
77 |
$lyrics3lsz = $this->fread(6); |
|
78 |
$lyrics3end = $this->fread(9); |
|
0 | 79 |
|
80 |
if ($lyrics3end == 'LYRICSEND') { |
|
81 |
// Lyrics3v1, APE, maybe ID3v1 |
|
82 |
||
83 |
$lyrics3size = 5100; |
|
84 |
$lyrics3offset = $info['ape']['tag_offset_start'] - $lyrics3size; |
|
85 |
$info['avdataend'] = $lyrics3offset; |
|
86 |
$lyrics3version = 1; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
87 |
$this->warning('APE tag located after Lyrics3, will probably break Lyrics3 compatability'); |
0 | 88 |
|
89 |
} elseif ($lyrics3end == 'LYRICS200') { |
|
90 |
// Lyrics3v2, APE, maybe ID3v1 |
|
91 |
||
92 |
$lyrics3size = $lyrics3lsz + 6 + strlen('LYRICS200'); // LSZ = lyrics + 'LYRICSBEGIN'; add 6-byte size field; add 'LYRICS200' |
|
93 |
$lyrics3offset = $info['ape']['tag_offset_start'] - $lyrics3size; |
|
94 |
$lyrics3version = 2; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
95 |
$this->warning('APE tag located after Lyrics3, will probably break Lyrics3 compatability'); |
0 | 96 |
|
97 |
} |
|
98 |
||
99 |
} |
|
100 |
||
101 |
} |
|
102 |
||
16 | 103 |
if (isset($lyrics3offset) && isset($lyrics3version) && isset($lyrics3size)) { |
0 | 104 |
$info['avdataend'] = $lyrics3offset; |
105 |
$this->getLyrics3Data($lyrics3offset, $lyrics3version, $lyrics3size); |
|
106 |
||
107 |
if (!isset($info['ape'])) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
108 |
if (isset($info['lyrics3']['tag_offset_start'])) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
109 |
$GETID3_ERRORARRAY = &$info['warning']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
110 |
getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.apetag.php', __FILE__, true); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
111 |
$getid3_temp = new getID3(); |
16 | 112 |
$getid3_temp->openfile($this->getid3->filename, $this->getid3->info['filesize'], $this->getid3->fp); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
113 |
$getid3_apetag = new getid3_apetag($getid3_temp); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
114 |
$getid3_apetag->overrideendoffset = $info['lyrics3']['tag_offset_start']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
115 |
$getid3_apetag->Analyze(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
116 |
if (!empty($getid3_temp->info['ape'])) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
117 |
$info['ape'] = $getid3_temp->info['ape']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
118 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
119 |
if (!empty($getid3_temp->info['replay_gain'])) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
120 |
$info['replay_gain'] = $getid3_temp->info['replay_gain']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
121 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
122 |
unset($getid3_temp, $getid3_apetag); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
123 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
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)'); |
0 | 125 |
} |
126 |
} |
|
127 |
||
128 |
} |
|
129 |
||
130 |
return true; |
|
131 |
} |
|
132 |
||
16 | 133 |
/** |
134 |
* @param int $endoffset |
|
135 |
* @param int $version |
|
136 |
* @param int $length |
|
137 |
* |
|
138 |
* @return bool |
|
139 |
*/ |
|
0 | 140 |
public function getLyrics3Data($endoffset, $version, $length) { |
141 |
// http://www.volweb.cz/str/tags.htm |
|
142 |
||
143 |
$info = &$this->getid3->info; |
|
144 |
||
145 |
if (!getid3_lib::intValueSupported($endoffset)) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
146 |
$this->warning('Unable to check for Lyrics3 because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB'); |
0 | 147 |
return false; |
148 |
} |
|
149 |
||
5 | 150 |
$this->fseek($endoffset); |
0 | 151 |
if ($length <= 0) { |
152 |
return false; |
|
153 |
} |
|
5 | 154 |
$rawdata = $this->fread($length); |
0 | 155 |
|
16 | 156 |
$ParsedLyrics3 = array(); |
157 |
||
0 | 158 |
$ParsedLyrics3['raw']['lyrics3version'] = $version; |
159 |
$ParsedLyrics3['raw']['lyrics3tagsize'] = $length; |
|
160 |
$ParsedLyrics3['tag_offset_start'] = $endoffset; |
|
161 |
$ParsedLyrics3['tag_offset_end'] = $endoffset + $length - 1; |
|
162 |
||
163 |
if (substr($rawdata, 0, 11) != 'LYRICSBEGIN') { |
|
164 |
if (strpos($rawdata, 'LYRICSBEGIN') !== false) { |
|
165 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
166 |
$this->warning('"LYRICSBEGIN" expected at '.$endoffset.' but actually found at '.($endoffset + strpos($rawdata, 'LYRICSBEGIN')).' - this is invalid for Lyrics3 v'.$version); |
0 | 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; |
|
172 |
||
173 |
} else { |
|
174 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
175 |
$this->error('"LYRICSBEGIN" expected at '.$endoffset.' but found "'.substr($rawdata, 0, 11).'" instead'); |
0 | 176 |
return false; |
177 |
||
178 |
} |
|
179 |
||
180 |
} |
|
181 |
||
182 |
switch ($version) { |
|
183 |
||
184 |
case 1: |
|
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); |
|
188 |
} else { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
189 |
$this->error('"LYRICSEND" expected at '.($this->ftell() - 11 + $length - 9).' but found "'.substr($rawdata, strlen($rawdata) - 9, 9).'" instead'); |
0 | 190 |
return false; |
191 |
} |
|
192 |
break; |
|
193 |
||
194 |
case 2: |
|
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); |
|
203 |
} |
|
204 |
||
205 |
if (isset($ParsedLyrics3['raw']['IND'])) { |
|
206 |
$i = 0; |
|
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)); |
|
211 |
} |
|
212 |
} |
|
213 |
} |
|
214 |
||
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]); |
|
219 |
} |
|
220 |
} |
|
221 |
||
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] : ''); |
|
230 |
} |
|
231 |
} |
|
232 |
} |
|
233 |
if (isset($ParsedLyrics3['raw']['LYR'])) { |
|
234 |
$this->Lyrics3LyricsTimestampParse($ParsedLyrics3); |
|
235 |
} |
|
236 |
} else { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
237 |
$this->error('"LYRICS200" expected at '.($this->ftell() - 11 + $length - 9).' but found "'.substr($rawdata, strlen($rawdata) - 9, 9).'" instead'); |
0 | 238 |
return false; |
239 |
} |
|
240 |
break; |
|
241 |
||
242 |
default: |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
243 |
$this->error('Cannot process Lyrics3 version '.$version.' (only v1 and v2)'); |
0 | 244 |
return false; |
245 |
} |
|
246 |
||
247 |
||
248 |
if (isset($info['id3v1']['tag_offset_start']) && ($info['id3v1']['tag_offset_start'] <= $ParsedLyrics3['tag_offset_end'])) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
249 |
$this->warning('ID3v1 tag information ignored since it appears to be a false synch in Lyrics3 tag data'); |
0 | 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']); |
|
255 |
break; |
|
256 |
} |
|
257 |
} |
|
258 |
} |
|
259 |
||
260 |
$info['lyrics3'] = $ParsedLyrics3; |
|
261 |
||
262 |
return true; |
|
263 |
} |
|
264 |
||
16 | 265 |
/** |
266 |
* @param string $rawtimestamp |
|
267 |
* |
|
268 |
* @return int|false |
|
269 |
*/ |
|
0 | 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]); |
|
273 |
} |
|
274 |
return false; |
|
275 |
} |
|
276 |
||
16 | 277 |
/** |
278 |
* @param array $Lyrics3data |
|
279 |
* |
|
280 |
* @return bool |
|
281 |
*/ |
|
0 | 282 |
public function Lyrics3LyricsTimestampParse(&$Lyrics3data) { |
283 |
$lyricsarray = explode("\r\n", $Lyrics3data['raw']['LYR']); |
|
16 | 284 |
$notimestamplyricsarray = array(); |
0 | 285 |
foreach ($lyricsarray as $key => $lyricline) { |
286 |
$regs = array(); |
|
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); |
|
291 |
} |
|
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; |
|
300 |
} else { |
|
301 |
$Lyrics3data['synchedlyrics'][$timestamp] = $lyricline; |
|
302 |
} |
|
303 |
} |
|
304 |
} |
|
305 |
} |
|
306 |
$Lyrics3data['unsynchedlyrics'] = implode("\r\n", $notimestamplyricsarray); |
|
307 |
if (isset($Lyrics3data['synchedlyrics']) && is_array($Lyrics3data['synchedlyrics'])) { |
|
308 |
ksort($Lyrics3data['synchedlyrics']); |
|
309 |
} |
|
310 |
return true; |
|
311 |
} |
|
312 |
||
16 | 313 |
/** |
314 |
* @param string $char |
|
315 |
* |
|
316 |
* @return bool|null |
|
317 |
*/ |
|
0 | 318 |
public function IntString2Bool($char) { |
319 |
if ($char == '1') { |
|
320 |
return true; |
|
321 |
} elseif ($char == '0') { |
|
322 |
return false; |
|
323 |
} |
|
324 |
return null; |
|
325 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
} |