module.tag.lyrics3.php 11 KB

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