module.tag.id3v1.php 13 KB

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