module.tag.apetag.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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.apetag.php //
  12. // module for analyzing APE tags //
  13. // dependencies: NONE //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. class getid3_apetag extends getid3_handler
  17. {
  18. public $inline_attachments = true; // true: return full data for all attachments; false: return no data for all attachments; integer: return data for attachments <= than this; string: save as file to this directory
  19. public $overrideendoffset = 0;
  20. public function Analyze() {
  21. $info = &$this->getid3->info;
  22. if (!getid3_lib::intValueSupported($info['filesize'])) {
  23. $info['warning'][] = 'Unable to check for APEtags because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB';
  24. return false;
  25. }
  26. $id3v1tagsize = 128;
  27. $apetagheadersize = 32;
  28. $lyrics3tagsize = 10;
  29. if ($this->overrideendoffset == 0) {
  30. $this->fseek(0 - $id3v1tagsize - $apetagheadersize - $lyrics3tagsize, SEEK_END);
  31. $APEfooterID3v1 = $this->fread($id3v1tagsize + $apetagheadersize + $lyrics3tagsize);
  32. //if (preg_match('/APETAGEX.{24}TAG.{125}$/i', $APEfooterID3v1)) {
  33. if (substr($APEfooterID3v1, strlen($APEfooterID3v1) - $id3v1tagsize - $apetagheadersize, 8) == 'APETAGEX') {
  34. // APE tag found before ID3v1
  35. $info['ape']['tag_offset_end'] = $info['filesize'] - $id3v1tagsize;
  36. //} elseif (preg_match('/APETAGEX.{24}$/i', $APEfooterID3v1)) {
  37. } elseif (substr($APEfooterID3v1, strlen($APEfooterID3v1) - $apetagheadersize, 8) == 'APETAGEX') {
  38. // APE tag found, no ID3v1
  39. $info['ape']['tag_offset_end'] = $info['filesize'];
  40. }
  41. } else {
  42. $this->fseek($this->overrideendoffset - $apetagheadersize);
  43. if ($this->fread(8) == 'APETAGEX') {
  44. $info['ape']['tag_offset_end'] = $this->overrideendoffset;
  45. }
  46. }
  47. if (!isset($info['ape']['tag_offset_end'])) {
  48. // APE tag not found
  49. unset($info['ape']);
  50. return false;
  51. }
  52. // shortcut
  53. $thisfile_ape = &$info['ape'];
  54. $this->fseek($thisfile_ape['tag_offset_end'] - $apetagheadersize);
  55. $APEfooterData = $this->fread(32);
  56. if (!($thisfile_ape['footer'] = $this->parseAPEheaderFooter($APEfooterData))) {
  57. $info['error'][] = 'Error parsing APE footer at offset '.$thisfile_ape['tag_offset_end'];
  58. return false;
  59. }
  60. if (isset($thisfile_ape['footer']['flags']['header']) && $thisfile_ape['footer']['flags']['header']) {
  61. $this->fseek($thisfile_ape['tag_offset_end'] - $thisfile_ape['footer']['raw']['tagsize'] - $apetagheadersize);
  62. $thisfile_ape['tag_offset_start'] = $this->ftell();
  63. $APEtagData = $this->fread($thisfile_ape['footer']['raw']['tagsize'] + $apetagheadersize);
  64. } else {
  65. $thisfile_ape['tag_offset_start'] = $thisfile_ape['tag_offset_end'] - $thisfile_ape['footer']['raw']['tagsize'];
  66. $this->fseek($thisfile_ape['tag_offset_start']);
  67. $APEtagData = $this->fread($thisfile_ape['footer']['raw']['tagsize']);
  68. }
  69. $info['avdataend'] = $thisfile_ape['tag_offset_start'];
  70. if (isset($info['id3v1']['tag_offset_start']) && ($info['id3v1']['tag_offset_start'] < $thisfile_ape['tag_offset_end'])) {
  71. $info['warning'][] = 'ID3v1 tag information ignored since it appears to be a false synch in APEtag data';
  72. unset($info['id3v1']);
  73. foreach ($info['warning'] as $key => $value) {
  74. if ($value == 'Some ID3v1 fields do not use NULL characters for padding') {
  75. unset($info['warning'][$key]);
  76. sort($info['warning']);
  77. break;
  78. }
  79. }
  80. }
  81. $offset = 0;
  82. if (isset($thisfile_ape['footer']['flags']['header']) && $thisfile_ape['footer']['flags']['header']) {
  83. if ($thisfile_ape['header'] = $this->parseAPEheaderFooter(substr($APEtagData, 0, $apetagheadersize))) {
  84. $offset += $apetagheadersize;
  85. } else {
  86. $info['error'][] = 'Error parsing APE header at offset '.$thisfile_ape['tag_offset_start'];
  87. return false;
  88. }
  89. }
  90. // shortcut
  91. $info['replay_gain'] = array();
  92. $thisfile_replaygain = &$info['replay_gain'];
  93. for ($i = 0; $i < $thisfile_ape['footer']['raw']['tag_items']; $i++) {
  94. $value_size = getid3_lib::LittleEndian2Int(substr($APEtagData, $offset, 4));
  95. $offset += 4;
  96. $item_flags = getid3_lib::LittleEndian2Int(substr($APEtagData, $offset, 4));
  97. $offset += 4;
  98. if (strstr(substr($APEtagData, $offset), "\x00") === false) {
  99. $info['error'][] = 'Cannot find null-byte (0x00) seperator between ItemKey #'.$i.' and value. ItemKey starts '.$offset.' bytes into the APE tag, at file offset '.($thisfile_ape['tag_offset_start'] + $offset);
  100. return false;
  101. }
  102. $ItemKeyLength = strpos($APEtagData, "\x00", $offset) - $offset;
  103. $item_key = strtolower(substr($APEtagData, $offset, $ItemKeyLength));
  104. // shortcut
  105. $thisfile_ape['items'][$item_key] = array();
  106. $thisfile_ape_items_current = &$thisfile_ape['items'][$item_key];
  107. $thisfile_ape_items_current['offset'] = $thisfile_ape['tag_offset_start'] + $offset;
  108. $offset += ($ItemKeyLength + 1); // skip 0x00 terminator
  109. $thisfile_ape_items_current['data'] = substr($APEtagData, $offset, $value_size);
  110. $offset += $value_size;
  111. $thisfile_ape_items_current['flags'] = $this->parseAPEtagFlags($item_flags);
  112. switch ($thisfile_ape_items_current['flags']['item_contents_raw']) {
  113. case 0: // UTF-8
  114. case 2: // Locator (URL, filename, etc), UTF-8 encoded
  115. $thisfile_ape_items_current['data'] = explode("\x00", $thisfile_ape_items_current['data']);
  116. break;
  117. case 1: // binary data
  118. default:
  119. break;
  120. }
  121. switch (strtolower($item_key)) {
  122. // http://wiki.hydrogenaud.io/index.php?title=ReplayGain#MP3Gain
  123. case 'replaygain_track_gain':
  124. if (preg_match('#^[\\-\\+][0-9\\.,]{8}$#', $thisfile_ape_items_current['data'][0])) {
  125. $thisfile_replaygain['track']['adjustment'] = (float) str_replace(',', '.', $thisfile_ape_items_current['data'][0]); // float casting will see "0,95" as zero!
  126. $thisfile_replaygain['track']['originator'] = 'unspecified';
  127. } else {
  128. $info['warning'][] = 'MP3gainTrackGain value in APEtag appears invalid: "'.$thisfile_ape_items_current['data'][0].'"';
  129. }
  130. break;
  131. case 'replaygain_track_peak':
  132. if (preg_match('#^[0-9\\.,]{8}$#', $thisfile_ape_items_current['data'][0])) {
  133. $thisfile_replaygain['track']['peak'] = (float) str_replace(',', '.', $thisfile_ape_items_current['data'][0]); // float casting will see "0,95" as zero!
  134. $thisfile_replaygain['track']['originator'] = 'unspecified';
  135. if ($thisfile_replaygain['track']['peak'] <= 0) {
  136. $info['warning'][] = 'ReplayGain Track peak from APEtag appears invalid: '.$thisfile_replaygain['track']['peak'].' (original value = "'.$thisfile_ape_items_current['data'][0].'")';
  137. }
  138. } else {
  139. $info['warning'][] = 'MP3gainTrackPeak value in APEtag appears invalid: "'.$thisfile_ape_items_current['data'][0].'"';
  140. }
  141. break;
  142. case 'replaygain_album_gain':
  143. if (preg_match('#^[\\-\\+][0-9\\.,]{8}$#', $thisfile_ape_items_current['data'][0])) {
  144. $thisfile_replaygain['album']['adjustment'] = (float) str_replace(',', '.', $thisfile_ape_items_current['data'][0]); // float casting will see "0,95" as zero!
  145. $thisfile_replaygain['album']['originator'] = 'unspecified';
  146. } else {
  147. $info['warning'][] = 'MP3gainAlbumGain value in APEtag appears invalid: "'.$thisfile_ape_items_current['data'][0].'"';
  148. }
  149. break;
  150. case 'replaygain_album_peak':
  151. if (preg_match('#^[0-9\\.,]{8}$#', $thisfile_ape_items_current['data'][0])) {
  152. $thisfile_replaygain['album']['peak'] = (float) str_replace(',', '.', $thisfile_ape_items_current['data'][0]); // float casting will see "0,95" as zero!
  153. $thisfile_replaygain['album']['originator'] = 'unspecified';
  154. if ($thisfile_replaygain['album']['peak'] <= 0) {
  155. $info['warning'][] = 'ReplayGain Album peak from APEtag appears invalid: '.$thisfile_replaygain['album']['peak'].' (original value = "'.$thisfile_ape_items_current['data'][0].'")';
  156. }
  157. } else {
  158. $info['warning'][] = 'MP3gainAlbumPeak value in APEtag appears invalid: "'.$thisfile_ape_items_current['data'][0].'"';
  159. }
  160. break;
  161. case 'mp3gain_undo':
  162. if (preg_match('#^[\\-\\+][0-9]{3},[\\-\\+][0-9]{3},[NW]$#', $thisfile_ape_items_current['data'][0])) {
  163. list($mp3gain_undo_left, $mp3gain_undo_right, $mp3gain_undo_wrap) = explode(',', $thisfile_ape_items_current['data'][0]);
  164. $thisfile_replaygain['mp3gain']['undo_left'] = intval($mp3gain_undo_left);
  165. $thisfile_replaygain['mp3gain']['undo_right'] = intval($mp3gain_undo_right);
  166. $thisfile_replaygain['mp3gain']['undo_wrap'] = (($mp3gain_undo_wrap == 'Y') ? true : false);
  167. } else {
  168. $info['warning'][] = 'MP3gainUndo value in APEtag appears invalid: "'.$thisfile_ape_items_current['data'][0].'"';
  169. }
  170. break;
  171. case 'mp3gain_minmax':
  172. if (preg_match('#^[0-9]{3},[0-9]{3}$#', $thisfile_ape_items_current['data'][0])) {
  173. list($mp3gain_globalgain_min, $mp3gain_globalgain_max) = explode(',', $thisfile_ape_items_current['data'][0]);
  174. $thisfile_replaygain['mp3gain']['globalgain_track_min'] = intval($mp3gain_globalgain_min);
  175. $thisfile_replaygain['mp3gain']['globalgain_track_max'] = intval($mp3gain_globalgain_max);
  176. } else {
  177. $info['warning'][] = 'MP3gainMinMax value in APEtag appears invalid: "'.$thisfile_ape_items_current['data'][0].'"';
  178. }
  179. break;
  180. case 'mp3gain_album_minmax':
  181. if (preg_match('#^[0-9]{3},[0-9]{3}$#', $thisfile_ape_items_current['data'][0])) {
  182. list($mp3gain_globalgain_album_min, $mp3gain_globalgain_album_max) = explode(',', $thisfile_ape_items_current['data'][0]);
  183. $thisfile_replaygain['mp3gain']['globalgain_album_min'] = intval($mp3gain_globalgain_album_min);
  184. $thisfile_replaygain['mp3gain']['globalgain_album_max'] = intval($mp3gain_globalgain_album_max);
  185. } else {
  186. $info['warning'][] = 'MP3gainAlbumMinMax value in APEtag appears invalid: "'.$thisfile_ape_items_current['data'][0].'"';
  187. }
  188. break;
  189. case 'tracknumber':
  190. if (is_array($thisfile_ape_items_current['data'])) {
  191. foreach ($thisfile_ape_items_current['data'] as $comment) {
  192. $thisfile_ape['comments']['track'][] = $comment;
  193. }
  194. }
  195. break;
  196. case 'cover art (artist)':
  197. case 'cover art (back)':
  198. case 'cover art (band logo)':
  199. case 'cover art (band)':
  200. case 'cover art (colored fish)':
  201. case 'cover art (composer)':
  202. case 'cover art (conductor)':
  203. case 'cover art (front)':
  204. case 'cover art (icon)':
  205. case 'cover art (illustration)':
  206. case 'cover art (lead)':
  207. case 'cover art (leaflet)':
  208. case 'cover art (lyricist)':
  209. case 'cover art (media)':
  210. case 'cover art (movie scene)':
  211. case 'cover art (other icon)':
  212. case 'cover art (other)':
  213. case 'cover art (performance)':
  214. case 'cover art (publisher logo)':
  215. case 'cover art (recording)':
  216. case 'cover art (studio)':
  217. // list of possible cover arts from http://taglib-sharp.sourcearchive.com/documentation/2.0.3.0-2/Ape_2Tag_8cs-source.html
  218. if (is_array($thisfile_ape_items_current['data'])) {
  219. $info['warning'][] = 'APEtag "'.$item_key.'" should be flagged as Binary data, but was incorrectly flagged as UTF-8';
  220. $thisfile_ape_items_current['data'] = implode("\x00", $thisfile_ape_items_current['data']);
  221. }
  222. list($thisfile_ape_items_current['filename'], $thisfile_ape_items_current['data']) = explode("\x00", $thisfile_ape_items_current['data'], 2);
  223. $thisfile_ape_items_current['data_offset'] = $thisfile_ape_items_current['offset'] + strlen($thisfile_ape_items_current['filename']."\x00");
  224. $thisfile_ape_items_current['data_length'] = strlen($thisfile_ape_items_current['data']);
  225. do {
  226. $thisfile_ape_items_current['image_mime'] = '';
  227. $imageinfo = array();
  228. $imagechunkcheck = getid3_lib::GetDataImageSize($thisfile_ape_items_current['data'], $imageinfo);
  229. if (($imagechunkcheck === false) || !isset($imagechunkcheck[2])) {
  230. $info['warning'][] = 'APEtag "'.$item_key.'" contains invalid image data';
  231. break;
  232. }
  233. $thisfile_ape_items_current['image_mime'] = image_type_to_mime_type($imagechunkcheck[2]);
  234. if ($this->inline_attachments === false) {
  235. // skip entirely
  236. unset($thisfile_ape_items_current['data']);
  237. break;
  238. }
  239. if ($this->inline_attachments === true) {
  240. // great
  241. } elseif (is_int($this->inline_attachments)) {
  242. if ($this->inline_attachments < $thisfile_ape_items_current['data_length']) {
  243. // too big, skip
  244. $info['warning'][] = 'attachment at '.$thisfile_ape_items_current['offset'].' is too large to process inline ('.number_format($thisfile_ape_items_current['data_length']).' bytes)';
  245. unset($thisfile_ape_items_current['data']);
  246. break;
  247. }
  248. } elseif (is_string($this->inline_attachments)) {
  249. $this->inline_attachments = rtrim(str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $this->inline_attachments), DIRECTORY_SEPARATOR);
  250. if (!is_dir($this->inline_attachments) || !is_writable($this->inline_attachments)) {
  251. // cannot write, skip
  252. $info['warning'][] = 'attachment at '.$thisfile_ape_items_current['offset'].' cannot be saved to "'.$this->inline_attachments.'" (not writable)';
  253. unset($thisfile_ape_items_current['data']);
  254. break;
  255. }
  256. }
  257. // if we get this far, must be OK
  258. if (is_string($this->inline_attachments)) {
  259. $destination_filename = $this->inline_attachments.DIRECTORY_SEPARATOR.md5($info['filenamepath']).'_'.$thisfile_ape_items_current['data_offset'];
  260. if (!file_exists($destination_filename) || is_writable($destination_filename)) {
  261. file_put_contents($destination_filename, $thisfile_ape_items_current['data']);
  262. } else {
  263. $info['warning'][] = 'attachment at '.$thisfile_ape_items_current['offset'].' cannot be saved to "'.$destination_filename.'" (not writable)';
  264. }
  265. $thisfile_ape_items_current['data_filename'] = $destination_filename;
  266. unset($thisfile_ape_items_current['data']);
  267. } else {
  268. if (!isset($info['ape']['comments']['picture'])) {
  269. $info['ape']['comments']['picture'] = array();
  270. }
  271. $comments_picture_data = array();
  272. foreach (array('data', 'image_mime', 'image_width', 'image_height', 'imagetype', 'picturetype', 'description', 'datalength') as $picture_key) {
  273. if (isset($thisfile_ape_items_current[$picture_key])) {
  274. $comments_picture_data[$picture_key] = $thisfile_ape_items_current[$picture_key];
  275. }
  276. }
  277. $info['ape']['comments']['picture'][] = $comments_picture_data;
  278. unset($comments_picture_data);
  279. }
  280. } while (false);
  281. break;
  282. default:
  283. if (is_array($thisfile_ape_items_current['data'])) {
  284. foreach ($thisfile_ape_items_current['data'] as $comment) {
  285. $thisfile_ape['comments'][strtolower($item_key)][] = $comment;
  286. }
  287. }
  288. break;
  289. }
  290. }
  291. if (empty($thisfile_replaygain)) {
  292. unset($info['replay_gain']);
  293. }
  294. return true;
  295. }
  296. public function parseAPEheaderFooter($APEheaderFooterData) {
  297. // http://www.uni-jena.de/~pfk/mpp/sv8/apeheader.html
  298. // shortcut
  299. $headerfooterinfo['raw'] = array();
  300. $headerfooterinfo_raw = &$headerfooterinfo['raw'];
  301. $headerfooterinfo_raw['footer_tag'] = substr($APEheaderFooterData, 0, 8);
  302. if ($headerfooterinfo_raw['footer_tag'] != 'APETAGEX') {
  303. return false;
  304. }
  305. $headerfooterinfo_raw['version'] = getid3_lib::LittleEndian2Int(substr($APEheaderFooterData, 8, 4));
  306. $headerfooterinfo_raw['tagsize'] = getid3_lib::LittleEndian2Int(substr($APEheaderFooterData, 12, 4));
  307. $headerfooterinfo_raw['tag_items'] = getid3_lib::LittleEndian2Int(substr($APEheaderFooterData, 16, 4));
  308. $headerfooterinfo_raw['global_flags'] = getid3_lib::LittleEndian2Int(substr($APEheaderFooterData, 20, 4));
  309. $headerfooterinfo_raw['reserved'] = substr($APEheaderFooterData, 24, 8);
  310. $headerfooterinfo['tag_version'] = $headerfooterinfo_raw['version'] / 1000;
  311. if ($headerfooterinfo['tag_version'] >= 2) {
  312. $headerfooterinfo['flags'] = $this->parseAPEtagFlags($headerfooterinfo_raw['global_flags']);
  313. }
  314. return $headerfooterinfo;
  315. }
  316. public function parseAPEtagFlags($rawflagint) {
  317. // "Note: APE Tags 1.0 do not use any of the APE Tag flags.
  318. // All are set to zero on creation and ignored on reading."
  319. // http://wiki.hydrogenaud.io/index.php?title=Ape_Tags_Flags
  320. $flags['header'] = (bool) ($rawflagint & 0x80000000);
  321. $flags['footer'] = (bool) ($rawflagint & 0x40000000);
  322. $flags['this_is_header'] = (bool) ($rawflagint & 0x20000000);
  323. $flags['item_contents_raw'] = ($rawflagint & 0x00000006) >> 1;
  324. $flags['read_only'] = (bool) ($rawflagint & 0x00000001);
  325. $flags['item_contents'] = $this->APEcontentTypeFlagLookup($flags['item_contents_raw']);
  326. return $flags;
  327. }
  328. public function APEcontentTypeFlagLookup($contenttypeid) {
  329. static $APEcontentTypeFlagLookup = array(
  330. 0 => 'utf-8',
  331. 1 => 'binary',
  332. 2 => 'external',
  333. 3 => 'reserved'
  334. );
  335. return (isset($APEcontentTypeFlagLookup[$contenttypeid]) ? $APEcontentTypeFlagLookup[$contenttypeid] : 'invalid');
  336. }
  337. public function APEtagItemIsUTF8Lookup($itemkey) {
  338. static $APEtagItemIsUTF8Lookup = array(
  339. 'title',
  340. 'subtitle',
  341. 'artist',
  342. 'album',
  343. 'debut album',
  344. 'publisher',
  345. 'conductor',
  346. 'track',
  347. 'composer',
  348. 'comment',
  349. 'copyright',
  350. 'publicationright',
  351. 'file',
  352. 'year',
  353. 'record date',
  354. 'record location',
  355. 'genre',
  356. 'media',
  357. 'related',
  358. 'isrc',
  359. 'abstract',
  360. 'language',
  361. 'bibliography'
  362. );
  363. return in_array(strtolower($itemkey), $APEtagItemIsUTF8Lookup);
  364. }
  365. }