PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/getid3/module.tag.id3v1.php

https://bitbucket.org/gregwiley/sermon-manager-for-wordpress
PHP | 362 lines | 303 code | 31 blank | 28 comment | 33 complexity | 373f8a6a4a10b8e26ab596ade3baaeae 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.id3v1.php //
  11. // module for analyzing ID3v1 tags //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. class getid3_id3v1 extends getid3_handler
  16. {
  17. function Analyze() {
  18. $info = &$this->getid3->info;
  19. if (!getid3_lib::intValueSupported($info['filesize'])) {
  20. $info['warning'][] = 'Unable to check for ID3v1 because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB';
  21. return false;
  22. }
  23. fseek($this->getid3->fp, -256, SEEK_END);
  24. $preid3v1 = fread($this->getid3->fp, 128);
  25. $id3v1tag = fread($this->getid3->fp, 128);
  26. if (substr($id3v1tag, 0, 3) == 'TAG') {
  27. $info['avdataend'] = $info['filesize'] - 128;
  28. $ParsedID3v1['title'] = $this->cutfield(substr($id3v1tag, 3, 30));
  29. $ParsedID3v1['artist'] = $this->cutfield(substr($id3v1tag, 33, 30));
  30. $ParsedID3v1['album'] = $this->cutfield(substr($id3v1tag, 63, 30));
  31. $ParsedID3v1['year'] = $this->cutfield(substr($id3v1tag, 93, 4));
  32. $ParsedID3v1['comment'] = substr($id3v1tag, 97, 30); // can't remove nulls yet, track detection depends on them
  33. $ParsedID3v1['genreid'] = ord(substr($id3v1tag, 127, 1));
  34. // If second-last byte of comment field is null and last byte of comment field is non-null
  35. // then this is ID3v1.1 and the comment field is 28 bytes long and the 30th byte is the track number
  36. if (($id3v1tag{125} === "\x00") && ($id3v1tag{126} !== "\x00")) {
  37. $ParsedID3v1['track'] = ord(substr($ParsedID3v1['comment'], 29, 1));
  38. $ParsedID3v1['comment'] = substr($ParsedID3v1['comment'], 0, 28);
  39. }
  40. $ParsedID3v1['comment'] = $this->cutfield($ParsedID3v1['comment']);
  41. $ParsedID3v1['genre'] = $this->LookupGenreName($ParsedID3v1['genreid']);
  42. if (!empty($ParsedID3v1['genre'])) {
  43. unset($ParsedID3v1['genreid']);
  44. }
  45. if (isset($ParsedID3v1['genre']) && (empty($ParsedID3v1['genre']) || ($ParsedID3v1['genre'] == 'Unknown'))) {
  46. unset($ParsedID3v1['genre']);
  47. }
  48. foreach ($ParsedID3v1 as $key => $value) {
  49. $ParsedID3v1['comments'][$key][0] = $value;
  50. }
  51. // ID3v1 data is supposed to be padded with NULL characters, but some taggers pad with spaces
  52. $GoodFormatID3v1tag = $this->GenerateID3v1Tag(
  53. $ParsedID3v1['title'],
  54. $ParsedID3v1['artist'],
  55. $ParsedID3v1['album'],
  56. $ParsedID3v1['year'],
  57. (isset($ParsedID3v1['genre']) ? $this->LookupGenreID($ParsedID3v1['genre']) : false),
  58. $ParsedID3v1['comment'],
  59. (!empty($ParsedID3v1['track']) ? $ParsedID3v1['track'] : ''));
  60. $ParsedID3v1['padding_valid'] = true;
  61. if ($id3v1tag !== $GoodFormatID3v1tag) {
  62. $ParsedID3v1['padding_valid'] = false;
  63. $info['warning'][] = 'Some ID3v1 fields do not use NULL characters for padding';
  64. }
  65. $ParsedID3v1['tag_offset_end'] = $info['filesize'];
  66. $ParsedID3v1['tag_offset_start'] = $ParsedID3v1['tag_offset_end'] - 128;
  67. $info['id3v1'] = $ParsedID3v1;
  68. }
  69. if (substr($preid3v1, 0, 3) == 'TAG') {
  70. // The way iTunes handles tags is, well, brain-damaged.
  71. // It completely ignores v1 if ID3v2 is present.
  72. // This goes as far as adding a new v1 tag *even if there already is one*
  73. // A suspected double-ID3v1 tag has been detected, but it could be that
  74. // the "TAG" identifier is a legitimate part of an APE or Lyrics3 tag
  75. if (substr($preid3v1, 96, 8) == 'APETAGEX') {
  76. // an APE tag footer was found before the last ID3v1, assume false "TAG" synch
  77. } elseif (substr($preid3v1, 119, 6) == 'LYRICS') {
  78. // a Lyrics3 tag footer was found before the last ID3v1, assume false "TAG" synch
  79. } else {
  80. // APE and Lyrics3 footers not found - assume double ID3v1
  81. $info['warning'][] = 'Duplicate ID3v1 tag detected - this has been known to happen with iTunes';
  82. $info['avdataend'] -= 128;
  83. }
  84. }
  85. return true;
  86. }
  87. static function cutfield($str) {
  88. return trim(substr($str, 0, strcspn($str, "\x00")));
  89. }
  90. static function ArrayOfGenres($allowSCMPXextended=false) {
  91. static $GenreLookup = array(
  92. 0 => 'Blues',
  93. 1 => 'Classic Rock',
  94. 2 => 'Country',
  95. 3 => 'Dance',
  96. 4 => 'Disco',
  97. 5 => 'Funk',
  98. 6 => 'Grunge',
  99. 7 => 'Hip-Hop',
  100. 8 => 'Jazz',
  101. 9 => 'Metal',
  102. 10 => 'New Age',
  103. 11 => 'Oldies',
  104. 12 => 'Other',
  105. 13 => 'Pop',
  106. 14 => 'R&B',
  107. 15 => 'Rap',
  108. 16 => 'Reggae',
  109. 17 => 'Rock',
  110. 18 => 'Techno',
  111. 19 => 'Industrial',
  112. 20 => 'Alternative',
  113. 21 => 'Ska',
  114. 22 => 'Death Metal',
  115. 23 => 'Pranks',
  116. 24 => 'Soundtrack',
  117. 25 => 'Euro-Techno',
  118. 26 => 'Ambient',
  119. 27 => 'Trip-Hop',
  120. 28 => 'Vocal',
  121. 29 => 'Jazz+Funk',
  122. 30 => 'Fusion',
  123. 31 => 'Trance',
  124. 32 => 'Classical',
  125. 33 => 'Instrumental',
  126. 34 => 'Acid',
  127. 35 => 'House',
  128. 36 => 'Game',
  129. 37 => 'Sound Clip',
  130. 38 => 'Gospel',
  131. 39 => 'Noise',
  132. 40 => 'Alt. Rock',
  133. 41 => 'Bass',
  134. 42 => 'Soul',
  135. 43 => 'Punk',
  136. 44 => 'Space',
  137. 45 => 'Meditative',
  138. 46 => 'Instrumental Pop',
  139. 47 => 'Instrumental Rock',
  140. 48 => 'Ethnic',
  141. 49 => 'Gothic',
  142. 50 => 'Darkwave',
  143. 51 => 'Techno-Industrial',
  144. 52 => 'Electronic',
  145. 53 => 'Pop-Folk',
  146. 54 => 'Eurodance',
  147. 55 => 'Dream',
  148. 56 => 'Southern Rock',
  149. 57 => 'Comedy',
  150. 58 => 'Cult',
  151. 59 => 'Gangsta Rap',
  152. 60 => 'Top 40',
  153. 61 => 'Christian Rap',
  154. 62 => 'Pop/Funk',
  155. 63 => 'Jungle',
  156. 64 => 'Native American',
  157. 65 => 'Cabaret',
  158. 66 => 'New Wave',
  159. 67 => 'Psychedelic',
  160. 68 => 'Rave',
  161. 69 => 'Showtunes',
  162. 70 => 'Trailer',
  163. 71 => 'Lo-Fi',
  164. 72 => 'Tribal',
  165. 73 => 'Acid Punk',
  166. 74 => 'Acid Jazz',
  167. 75 => 'Polka',
  168. 76 => 'Retro',
  169. 77 => 'Musical',
  170. 78 => 'Rock & Roll',
  171. 79 => 'Hard Rock',
  172. 80 => 'Folk',
  173. 81 => 'Folk/Rock',
  174. 82 => 'National Folk',
  175. 83 => 'Swing',
  176. 84 => 'Fast-Fusion',
  177. 85 => 'Bebob',
  178. 86 => 'Latin',
  179. 87 => 'Revival',
  180. 88 => 'Celtic',
  181. 89 => 'Bluegrass',
  182. 90 => 'Avantgarde',
  183. 91 => 'Gothic Rock',
  184. 92 => 'Progressive Rock',
  185. 93 => 'Psychedelic Rock',
  186. 94 => 'Symphonic Rock',
  187. 95 => 'Slow Rock',
  188. 96 => 'Big Band',
  189. 97 => 'Chorus',
  190. 98 => 'Easy Listening',
  191. 99 => 'Acoustic',
  192. 100 => 'Humour',
  193. 101 => 'Speech',
  194. 102 => 'Chanson',
  195. 103 => 'Opera',
  196. 104 => 'Chamber Music',
  197. 105 => 'Sonata',
  198. 106 => 'Symphony',
  199. 107 => 'Booty Bass',
  200. 108 => 'Primus',
  201. 109 => 'Porn Groove',
  202. 110 => 'Satire',
  203. 111 => 'Slow Jam',
  204. 112 => 'Club',
  205. 113 => 'Tango',
  206. 114 => 'Samba',
  207. 115 => 'Folklore',
  208. 116 => 'Ballad',
  209. 117 => 'Power Ballad',
  210. 118 => 'Rhythmic Soul',
  211. 119 => 'Freestyle',
  212. 120 => 'Duet',
  213. 121 => 'Punk Rock',
  214. 122 => 'Drum Solo',
  215. 123 => 'A Cappella',
  216. 124 => 'Euro-House',
  217. 125 => 'Dance Hall',
  218. 126 => 'Goa',
  219. 127 => 'Drum & Bass',
  220. 128 => 'Club-House',
  221. 129 => 'Hardcore',
  222. 130 => 'Terror',
  223. 131 => 'Indie',
  224. 132 => 'BritPop',
  225. 133 => 'Negerpunk',
  226. 134 => 'Polsk Punk',
  227. 135 => 'Beat',
  228. 136 => 'Christian Gangsta Rap',
  229. 137 => 'Heavy Metal',
  230. 138 => 'Black Metal',
  231. 139 => 'Crossover',
  232. 140 => 'Contemporary Christian',
  233. 141 => 'Christian Rock',
  234. 142 => 'Merengue',
  235. 143 => 'Salsa',
  236. 144 => 'Thrash Metal',
  237. 145 => 'Anime',
  238. 146 => 'JPop',
  239. 147 => 'Synthpop',
  240. 255 => 'Unknown',
  241. 'CR' => 'Cover',
  242. 'RX' => 'Remix'
  243. );
  244. static $GenreLookupSCMPX = array();
  245. if ($allowSCMPXextended && empty($GenreLookupSCMPX)) {
  246. $GenreLookupSCMPX = $GenreLookup;
  247. // http://www.geocities.co.jp/SiliconValley-Oakland/3664/alittle.html#GenreExtended
  248. // Extended ID3v1 genres invented by SCMPX
  249. // Note that 255 "Japanese Anime" conflicts with standard "Unknown"
  250. $GenreLookupSCMPX[240] = 'Sacred';
  251. $GenreLookupSCMPX[241] = 'Northern Europe';
  252. $GenreLookupSCMPX[242] = 'Irish & Scottish';
  253. $GenreLookupSCMPX[243] = 'Scotland';
  254. $GenreLookupSCMPX[244] = 'Ethnic Europe';
  255. $GenreLookupSCMPX[245] = 'Enka';
  256. $GenreLookupSCMPX[246] = 'Children\'s Song';
  257. $GenreLookupSCMPX[247] = 'Japanese Sky';
  258. $GenreLookupSCMPX[248] = 'Japanese Heavy Rock';
  259. $GenreLookupSCMPX[249] = 'Japanese Doom Rock';
  260. $GenreLookupSCMPX[250] = 'Japanese J-POP';
  261. $GenreLookupSCMPX[251] = 'Japanese Seiyu';
  262. $GenreLookupSCMPX[252] = 'Japanese Ambient Techno';
  263. $GenreLookupSCMPX[253] = 'Japanese Moemoe';
  264. $GenreLookupSCMPX[254] = 'Japanese Tokusatsu';
  265. //$GenreLookupSCMPX[255] = 'Japanese Anime';
  266. }
  267. return ($allowSCMPXextended ? $GenreLookupSCMPX : $GenreLookup);
  268. }
  269. static function LookupGenreName($genreid, $allowSCMPXextended=true) {
  270. switch ($genreid) {
  271. case 'RX':
  272. case 'CR':
  273. break;
  274. default:
  275. if (!is_numeric($genreid)) {
  276. return false;
  277. }
  278. $genreid = intval($genreid); // to handle 3 or '3' or '03'
  279. break;
  280. }
  281. $GenreLookup = getid3_id3v1::ArrayOfGenres($allowSCMPXextended);
  282. return (isset($GenreLookup[$genreid]) ? $GenreLookup[$genreid] : false);
  283. }
  284. static function LookupGenreID($genre, $allowSCMPXextended=false) {
  285. $GenreLookup = getid3_id3v1::ArrayOfGenres($allowSCMPXextended);
  286. $LowerCaseNoSpaceSearchTerm = strtolower(str_replace(' ', '', $genre));
  287. foreach ($GenreLookup as $key => $value) {
  288. if (strtolower(str_replace(' ', '', $value)) == $LowerCaseNoSpaceSearchTerm) {
  289. return $key;
  290. }
  291. }
  292. return false;
  293. }
  294. static function StandardiseID3v1GenreName($OriginalGenre) {
  295. if (($GenreID = getid3_id3v1::LookupGenreID($OriginalGenre)) !== false) {
  296. return getid3_id3v1::LookupGenreName($GenreID);
  297. }
  298. return $OriginalGenre;
  299. }
  300. static function GenerateID3v1Tag($title, $artist, $album, $year, $genreid, $comment, $track='') {
  301. $ID3v1Tag = 'TAG';
  302. $ID3v1Tag .= str_pad(trim(substr($title, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
  303. $ID3v1Tag .= str_pad(trim(substr($artist, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
  304. $ID3v1Tag .= str_pad(trim(substr($album, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
  305. $ID3v1Tag .= str_pad(trim(substr($year, 0, 4)), 4, "\x00", STR_PAD_LEFT);
  306. if (!empty($track) && ($track > 0) && ($track <= 255)) {
  307. $ID3v1Tag .= str_pad(trim(substr($comment, 0, 28)), 28, "\x00", STR_PAD_RIGHT);
  308. $ID3v1Tag .= "\x00";
  309. if (gettype($track) == 'string') {
  310. $track = (int) $track;
  311. }
  312. $ID3v1Tag .= chr($track);
  313. } else {
  314. $ID3v1Tag .= str_pad(trim(substr($comment, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
  315. }
  316. if (($genreid < 0) || ($genreid > 147)) {
  317. $genreid = 255; // 'unknown' genre
  318. }
  319. switch (gettype($genreid)) {
  320. case 'string':
  321. case 'integer':
  322. $ID3v1Tag .= chr(intval($genreid));
  323. break;
  324. default:
  325. $ID3v1Tag .= chr(255); // 'unknown' genre
  326. break;
  327. }
  328. return $ID3v1Tag;
  329. }
  330. }
  331. ?>