module.tag.lyrics3.php 11 KB

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