module.tag.id3v1.php 14 KB

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