PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/wp-includes/ID3/module.tag.id3v1.php

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