PageRenderTime 73ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/includes/js/mootools-filemanager/Assets/Connector/Assets/getid3/module.tag.lyrics3.php

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