module.tag.lyrics3.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at https://github.com/JamesHeinrich/getID3 //
  5. // or https://www.getid3.org //
  6. // or http://getid3.sourceforge.net //
  7. // see readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. /// //
  10. // module.tag.lyrics3.php //
  11. // module for analyzing Lyrics3 tags //
  12. // dependencies: module.tag.apetag.php (optional) //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. class getid3_lyrics3 extends getid3_handler
  16. {
  17. /**
  18. * @return bool
  19. */
  20. public function Analyze() {
  21. $info = &$this->getid3->info;
  22. // http://www.volweb.cz/str/tags.htm
  23. if (!getid3_lib::intValueSupported($info['filesize'])) {
  24. $this->warning('Unable to check for Lyrics3 because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB');
  25. return false;
  26. }
  27. $this->fseek((0 - 128 - 9 - 6), SEEK_END); // end - ID3v1 - "LYRICSEND" - [Lyrics3size]
  28. $lyrics3_id3v1 = $this->fread(128 + 9 + 6);
  29. $lyrics3lsz = substr($lyrics3_id3v1, 0, 6); // Lyrics3size
  30. $lyrics3end = substr($lyrics3_id3v1, 6, 9); // LYRICSEND or LYRICS200
  31. $id3v1tag = substr($lyrics3_id3v1, 15, 128); // ID3v1
  32. if ($lyrics3end == 'LYRICSEND') {
  33. // Lyrics3v1, ID3v1, no APE
  34. $lyrics3size = 5100;
  35. $lyrics3offset = $info['filesize'] - 128 - $lyrics3size;
  36. $lyrics3version = 1;
  37. } elseif ($lyrics3end == 'LYRICS200') {
  38. // Lyrics3v2, ID3v1, no APE
  39. // LSZ = lyrics + 'LYRICSBEGIN'; add 6-byte size field; add 'LYRICS200'
  40. $lyrics3size = $lyrics3lsz + 6 + strlen('LYRICS200');
  41. $lyrics3offset = $info['filesize'] - 128 - $lyrics3size;
  42. $lyrics3version = 2;
  43. } elseif (substr(strrev($lyrics3_id3v1), 0, 9) == strrev('LYRICSEND')) {
  44. // Lyrics3v1, no ID3v1, no APE
  45. $lyrics3size = 5100;
  46. $lyrics3offset = $info['filesize'] - $lyrics3size;
  47. $lyrics3version = 1;
  48. $lyrics3offset = $info['filesize'] - $lyrics3size;
  49. } elseif (substr(strrev($lyrics3_id3v1), 0, 9) == strrev('LYRICS200')) {
  50. // Lyrics3v2, no ID3v1, no APE
  51. $lyrics3size = (int) strrev(substr(strrev($lyrics3_id3v1), 9, 6)) + 6 + strlen('LYRICS200'); // LSZ = lyrics + 'LYRICSBEGIN'; add 6-byte size field; add 'LYRICS200'
  52. $lyrics3offset = $info['filesize'] - $lyrics3size;
  53. $lyrics3version = 2;
  54. } else {
  55. if (isset($info['ape']['tag_offset_start']) && ($info['ape']['tag_offset_start'] > 15)) {
  56. $this->fseek($info['ape']['tag_offset_start'] - 15);
  57. $lyrics3lsz = $this->fread(6);
  58. $lyrics3end = $this->fread(9);
  59. if ($lyrics3end == 'LYRICSEND') {
  60. // Lyrics3v1, APE, maybe ID3v1
  61. $lyrics3size = 5100;
  62. $lyrics3offset = $info['ape']['tag_offset_start'] - $lyrics3size;
  63. $info['avdataend'] = $lyrics3offset;
  64. $lyrics3version = 1;
  65. $this->warning('APE tag located after Lyrics3, will probably break Lyrics3 compatability');
  66. } elseif ($lyrics3end == 'LYRICS200') {
  67. // Lyrics3v2, APE, maybe ID3v1
  68. $lyrics3size = $lyrics3lsz + 6 + strlen('LYRICS200'); // LSZ = lyrics + 'LYRICSBEGIN'; add 6-byte size field; add 'LYRICS200'
  69. $lyrics3offset = $info['ape']['tag_offset_start'] - $lyrics3size;
  70. $lyrics3version = 2;
  71. $this->warning('APE tag located after Lyrics3, will probably break Lyrics3 compatability');
  72. }
  73. }
  74. }
  75. if (isset($lyrics3offset) && isset($lyrics3version) && isset($lyrics3size)) {
  76. $info['avdataend'] = $lyrics3offset;
  77. $this->getLyrics3Data($lyrics3offset, $lyrics3version, $lyrics3size);
  78. if (!isset($info['ape'])) {
  79. if (isset($info['lyrics3']['tag_offset_start'])) {
  80. $GETID3_ERRORARRAY = &$info['warning'];
  81. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.apetag.php', __FILE__, true);
  82. $getid3_temp = new getID3();
  83. $getid3_temp->openfile($this->getid3->filename);
  84. $getid3_apetag = new getid3_apetag($getid3_temp);
  85. $getid3_apetag->overrideendoffset = $info['lyrics3']['tag_offset_start'];
  86. $getid3_apetag->Analyze();
  87. if (!empty($getid3_temp->info['ape'])) {
  88. $info['ape'] = $getid3_temp->info['ape'];
  89. }
  90. if (!empty($getid3_temp->info['replay_gain'])) {
  91. $info['replay_gain'] = $getid3_temp->info['replay_gain'];
  92. }
  93. unset($getid3_temp, $getid3_apetag);
  94. } else {
  95. $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)');
  96. }
  97. }
  98. }
  99. return true;
  100. }
  101. /**
  102. * @param int $endoffset
  103. * @param int $version
  104. * @param int $length
  105. *
  106. * @return bool
  107. */
  108. public function getLyrics3Data($endoffset, $version, $length) {
  109. // http://www.volweb.cz/str/tags.htm
  110. $info = &$this->getid3->info;
  111. if (!getid3_lib::intValueSupported($endoffset)) {
  112. $this->warning('Unable to check for Lyrics3 because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB');
  113. return false;
  114. }
  115. $this->fseek($endoffset);
  116. if ($length <= 0) {
  117. return false;
  118. }
  119. $rawdata = $this->fread($length);
  120. $ParsedLyrics3 = array();
  121. $ParsedLyrics3['raw']['lyrics3version'] = $version;
  122. $ParsedLyrics3['raw']['lyrics3tagsize'] = $length;
  123. $ParsedLyrics3['tag_offset_start'] = $endoffset;
  124. $ParsedLyrics3['tag_offset_end'] = $endoffset + $length - 1;
  125. if (substr($rawdata, 0, 11) != 'LYRICSBEGIN') {
  126. if (strpos($rawdata, 'LYRICSBEGIN') !== false) {
  127. $this->warning('"LYRICSBEGIN" expected at '.$endoffset.' but actually found at '.($endoffset + strpos($rawdata, 'LYRICSBEGIN')).' - this is invalid for Lyrics3 v'.$version);
  128. $info['avdataend'] = $endoffset + strpos($rawdata, 'LYRICSBEGIN');
  129. $rawdata = substr($rawdata, strpos($rawdata, 'LYRICSBEGIN'));
  130. $length = strlen($rawdata);
  131. $ParsedLyrics3['tag_offset_start'] = $info['avdataend'];
  132. $ParsedLyrics3['raw']['lyrics3tagsize'] = $length;
  133. } else {
  134. $this->error('"LYRICSBEGIN" expected at '.$endoffset.' but found "'.substr($rawdata, 0, 11).'" instead');
  135. return false;
  136. }
  137. }
  138. switch ($version) {
  139. case 1:
  140. if (substr($rawdata, strlen($rawdata) - 9, 9) == 'LYRICSEND') {
  141. $ParsedLyrics3['raw']['LYR'] = trim(substr($rawdata, 11, strlen($rawdata) - 11 - 9));
  142. $this->Lyrics3LyricsTimestampParse($ParsedLyrics3);
  143. } else {
  144. $this->error('"LYRICSEND" expected at '.($this->ftell() - 11 + $length - 9).' but found "'.substr($rawdata, strlen($rawdata) - 9, 9).'" instead');
  145. return false;
  146. }
  147. break;
  148. case 2:
  149. if (substr($rawdata, strlen($rawdata) - 9, 9) == 'LYRICS200') {
  150. $ParsedLyrics3['raw']['unparsed'] = substr($rawdata, 11, strlen($rawdata) - 11 - 9 - 6); // LYRICSBEGIN + LYRICS200 + LSZ
  151. $rawdata = $ParsedLyrics3['raw']['unparsed'];
  152. while (strlen($rawdata) > 0) {
  153. $fieldname = substr($rawdata, 0, 3);
  154. $fieldsize = (int) substr($rawdata, 3, 5);
  155. $ParsedLyrics3['raw'][$fieldname] = substr($rawdata, 8, $fieldsize);
  156. $rawdata = substr($rawdata, 3 + 5 + $fieldsize);
  157. }
  158. if (isset($ParsedLyrics3['raw']['IND'])) {
  159. $i = 0;
  160. $flagnames = array('lyrics', 'timestamps', 'inhibitrandom');
  161. foreach ($flagnames as $flagname) {
  162. if (strlen($ParsedLyrics3['raw']['IND']) > $i++) {
  163. $ParsedLyrics3['flags'][$flagname] = $this->IntString2Bool(substr($ParsedLyrics3['raw']['IND'], $i, 1 - 1));
  164. }
  165. }
  166. }
  167. $fieldnametranslation = array('ETT'=>'title', 'EAR'=>'artist', 'EAL'=>'album', 'INF'=>'comment', 'AUT'=>'author');
  168. foreach ($fieldnametranslation as $key => $value) {
  169. if (isset($ParsedLyrics3['raw'][$key])) {
  170. $ParsedLyrics3['comments'][$value][] = trim($ParsedLyrics3['raw'][$key]);
  171. }
  172. }
  173. if (isset($ParsedLyrics3['raw']['IMG'])) {
  174. $imagestrings = explode("\r\n", $ParsedLyrics3['raw']['IMG']);
  175. foreach ($imagestrings as $key => $imagestring) {
  176. if (strpos($imagestring, '||') !== false) {
  177. $imagearray = explode('||', $imagestring);
  178. $ParsedLyrics3['images'][$key]['filename'] = (isset($imagearray[0]) ? $imagearray[0] : '');
  179. $ParsedLyrics3['images'][$key]['description'] = (isset($imagearray[1]) ? $imagearray[1] : '');
  180. $ParsedLyrics3['images'][$key]['timestamp'] = $this->Lyrics3Timestamp2Seconds(isset($imagearray[2]) ? $imagearray[2] : '');
  181. }
  182. }
  183. }
  184. if (isset($ParsedLyrics3['raw']['LYR'])) {
  185. $this->Lyrics3LyricsTimestampParse($ParsedLyrics3);
  186. }
  187. } else {
  188. $this->error('"LYRICS200" expected at '.($this->ftell() - 11 + $length - 9).' but found "'.substr($rawdata, strlen($rawdata) - 9, 9).'" instead');
  189. return false;
  190. }
  191. break;
  192. default:
  193. $this->error('Cannot process Lyrics3 version '.$version.' (only v1 and v2)');
  194. return false;
  195. break;
  196. }
  197. if (isset($info['id3v1']['tag_offset_start']) && ($info['id3v1']['tag_offset_start'] <= $ParsedLyrics3['tag_offset_end'])) {
  198. $this->warning('ID3v1 tag information ignored since it appears to be a false synch in Lyrics3 tag data');
  199. unset($info['id3v1']);
  200. foreach ($info['warning'] as $key => $value) {
  201. if ($value == 'Some ID3v1 fields do not use NULL characters for padding') {
  202. unset($info['warning'][$key]);
  203. sort($info['warning']);
  204. break;
  205. }
  206. }
  207. }
  208. $info['lyrics3'] = $ParsedLyrics3;
  209. return true;
  210. }
  211. /**
  212. * @param string $rawtimestamp
  213. *
  214. * @return int|false
  215. */
  216. public function Lyrics3Timestamp2Seconds($rawtimestamp) {
  217. if (preg_match('#^\\[([0-9]{2}):([0-9]{2})\\]$#', $rawtimestamp, $regs)) {
  218. return (int) (($regs[1] * 60) + $regs[2]);
  219. }
  220. return false;
  221. }
  222. /**
  223. * @param array $Lyrics3data
  224. *
  225. * @return bool
  226. */
  227. public function Lyrics3LyricsTimestampParse(&$Lyrics3data) {
  228. $lyricsarray = explode("\r\n", $Lyrics3data['raw']['LYR']);
  229. $notimestamplyricsarray = array();
  230. foreach ($lyricsarray as $key => $lyricline) {
  231. $regs = array();
  232. unset($thislinetimestamps);
  233. while (preg_match('#^(\\[[0-9]{2}:[0-9]{2}\\])#', $lyricline, $regs)) {
  234. $thislinetimestamps[] = $this->Lyrics3Timestamp2Seconds($regs[0]);
  235. $lyricline = str_replace($regs[0], '', $lyricline);
  236. }
  237. $notimestamplyricsarray[$key] = $lyricline;
  238. if (isset($thislinetimestamps) && is_array($thislinetimestamps)) {
  239. sort($thislinetimestamps);
  240. foreach ($thislinetimestamps as $timestampkey => $timestamp) {
  241. if (isset($Lyrics3data['synchedlyrics'][$timestamp])) {
  242. // timestamps only have a 1-second resolution, it's possible that multiple lines
  243. // could have the same timestamp, if so, append
  244. $Lyrics3data['synchedlyrics'][$timestamp] .= "\r\n".$lyricline;
  245. } else {
  246. $Lyrics3data['synchedlyrics'][$timestamp] = $lyricline;
  247. }
  248. }
  249. }
  250. }
  251. $Lyrics3data['unsynchedlyrics'] = implode("\r\n", $notimestamplyricsarray);
  252. if (isset($Lyrics3data['synchedlyrics']) && is_array($Lyrics3data['synchedlyrics'])) {
  253. ksort($Lyrics3data['synchedlyrics']);
  254. }
  255. return true;
  256. }
  257. /**
  258. * @param string $char
  259. *
  260. * @return bool|null
  261. */
  262. public function IntString2Bool($char) {
  263. if ($char == '1') {
  264. return true;
  265. } elseif ($char == '0') {
  266. return false;
  267. }
  268. return null;
  269. }
  270. }