module.tag.lyrics3.php 11 KB

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