PageRenderTime 35ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Vendor/getid3/module.tag.id3v1.php

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