2
/////////////////////////////////////////////////////////////////
3
/// getID3() by James Heinrich <info@getid3.org> //
4
// available at http://getid3.sourceforge.net //
5
// or http://www.getid3.org //
6
/////////////////////////////////////////////////////////////////
7
// See readme.txt for more details //
8
/////////////////////////////////////////////////////////////////
10
// module.tag.apetag.php //
11
// module for analyzing APE tags //
12
// dependencies: NONE //
14
/////////////////////////////////////////////////////////////////
16
class getid3_apetag extends getid3_handler
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;
21
public function Analyze() {
22
$info = &$this->getid3->info;
24
if (!getid3_lib::intValueSupported($info['filesize'])) {
25
$info['warning'][] = 'Unable to check for APEtags because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB';
30
$apetagheadersize = 32;
33
if ($this->overrideendoffset == 0) {
35
fseek($this->getid3->fp, 0 - $id3v1tagsize - $apetagheadersize - $lyrics3tagsize, SEEK_END);
36
$APEfooterID3v1 = fread($this->getid3->fp, $id3v1tagsize + $apetagheadersize + $lyrics3tagsize);
38
//if (preg_match('/APETAGEX.{24}TAG.{125}$/i', $APEfooterID3v1)) {
39
if (substr($APEfooterID3v1, strlen($APEfooterID3v1) - $id3v1tagsize - $apetagheadersize, 8) == 'APETAGEX') {
41
// APE tag found before ID3v1
42
$info['ape']['tag_offset_end'] = $info['filesize'] - $id3v1tagsize;
44
//} elseif (preg_match('/APETAGEX.{24}$/i', $APEfooterID3v1)) {
45
} elseif (substr($APEfooterID3v1, strlen($APEfooterID3v1) - $apetagheadersize, 8) == 'APETAGEX') {
47
// APE tag found, no ID3v1
48
$info['ape']['tag_offset_end'] = $info['filesize'];
54
fseek($this->getid3->fp, $this->overrideendoffset - $apetagheadersize, SEEK_SET);
55
if (fread($this->getid3->fp, 8) == 'APETAGEX') {
56
$info['ape']['tag_offset_end'] = $this->overrideendoffset;
60
if (!isset($info['ape']['tag_offset_end'])) {
69
$thisfile_ape = &$info['ape'];
71
fseek($this->getid3->fp, $thisfile_ape['tag_offset_end'] - $apetagheadersize, SEEK_SET);
72
$APEfooterData = fread($this->getid3->fp, 32);
73
if (!($thisfile_ape['footer'] = $this->parseAPEheaderFooter($APEfooterData))) {
74
$info['error'][] = 'Error parsing APE footer at offset '.$thisfile_ape['tag_offset_end'];
78
if (isset($thisfile_ape['footer']['flags']['header']) && $thisfile_ape['footer']['flags']['header']) {
79
fseek($this->getid3->fp, $thisfile_ape['tag_offset_end'] - $thisfile_ape['footer']['raw']['tagsize'] - $apetagheadersize, SEEK_SET);
80
$thisfile_ape['tag_offset_start'] = ftell($this->getid3->fp);
81
$APEtagData = fread($this->getid3->fp, $thisfile_ape['footer']['raw']['tagsize'] + $apetagheadersize);
83
$thisfile_ape['tag_offset_start'] = $thisfile_ape['tag_offset_end'] - $thisfile_ape['footer']['raw']['tagsize'];
84
fseek($this->getid3->fp, $thisfile_ape['tag_offset_start'], SEEK_SET);
85
$APEtagData = fread($this->getid3->fp, $thisfile_ape['footer']['raw']['tagsize']);
87
$info['avdataend'] = $thisfile_ape['tag_offset_start'];
89
if (isset($info['id3v1']['tag_offset_start']) && ($info['id3v1']['tag_offset_start'] < $thisfile_ape['tag_offset_end'])) {
90
$info['warning'][] = 'ID3v1 tag information ignored since it appears to be a false synch in APEtag data';
91
unset($info['id3v1']);
92
foreach ($info['warning'] as $key => $value) {
93
if ($value == 'Some ID3v1 fields do not use NULL characters for padding') {
94
unset($info['warning'][$key]);
95
sort($info['warning']);
102
if (isset($thisfile_ape['footer']['flags']['header']) && $thisfile_ape['footer']['flags']['header']) {
103
if ($thisfile_ape['header'] = $this->parseAPEheaderFooter(substr($APEtagData, 0, $apetagheadersize))) {
104
$offset += $apetagheadersize;
106
$info['error'][] = 'Error parsing APE header at offset '.$thisfile_ape['tag_offset_start'];
112
$info['replay_gain'] = array();
113
$thisfile_replaygain = &$info['replay_gain'];
115
for ($i = 0; $i < $thisfile_ape['footer']['raw']['tag_items']; $i++) {
116
$value_size = getid3_lib::LittleEndian2Int(substr($APEtagData, $offset, 4));
118
$item_flags = getid3_lib::LittleEndian2Int(substr($APEtagData, $offset, 4));
120
if (strstr(substr($APEtagData, $offset), "\x00") === false) {
121
$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);
124
$ItemKeyLength = strpos($APEtagData, "\x00", $offset) - $offset;
125
$item_key = strtolower(substr($APEtagData, $offset, $ItemKeyLength));
128
$thisfile_ape['items'][$item_key] = array();
129
$thisfile_ape_items_current = &$thisfile_ape['items'][$item_key];
131
$thisfile_ape_items_current['offset'] = $thisfile_ape['tag_offset_start'] + $offset;
133
$offset += ($ItemKeyLength + 1); // skip 0x00 terminator
134
$thisfile_ape_items_current['data'] = substr($APEtagData, $offset, $value_size);
135
$offset += $value_size;
137
$thisfile_ape_items_current['flags'] = $this->parseAPEtagFlags($item_flags);
138
switch ($thisfile_ape_items_current['flags']['item_contents_raw']) {
140
case 3: // Locator (URL, filename, etc), UTF-8 encoded
141
$thisfile_ape_items_current['data'] = explode("\x00", trim($thisfile_ape_items_current['data']));
144
default: // binary data
148
switch (strtolower($item_key)) {
149
case 'replaygain_track_gain':
150
$thisfile_replaygain['track']['adjustment'] = (float) str_replace(',', '.', $thisfile_ape_items_current['data'][0]); // float casting will see "0,95" as zero!
151
$thisfile_replaygain['track']['originator'] = 'unspecified';
154
case 'replaygain_track_peak':
155
$thisfile_replaygain['track']['peak'] = (float) str_replace(',', '.', $thisfile_ape_items_current['data'][0]); // float casting will see "0,95" as zero!
156
$thisfile_replaygain['track']['originator'] = 'unspecified';
157
if ($thisfile_replaygain['track']['peak'] <= 0) {
158
$info['warning'][] = 'ReplayGain Track peak from APEtag appears invalid: '.$thisfile_replaygain['track']['peak'].' (original value = "'.$thisfile_ape_items_current['data'][0].'")';
162
case 'replaygain_album_gain':
163
$thisfile_replaygain['album']['adjustment'] = (float) str_replace(',', '.', $thisfile_ape_items_current['data'][0]); // float casting will see "0,95" as zero!
164
$thisfile_replaygain['album']['originator'] = 'unspecified';
167
case 'replaygain_album_peak':
168
$thisfile_replaygain['album']['peak'] = (float) str_replace(',', '.', $thisfile_ape_items_current['data'][0]); // float casting will see "0,95" as zero!
169
$thisfile_replaygain['album']['originator'] = 'unspecified';
170
if ($thisfile_replaygain['album']['peak'] <= 0) {
171
$info['warning'][] = 'ReplayGain Album peak from APEtag appears invalid: '.$thisfile_replaygain['album']['peak'].' (original value = "'.$thisfile_ape_items_current['data'][0].'")';
176
list($mp3gain_undo_left, $mp3gain_undo_right, $mp3gain_undo_wrap) = explode(',', $thisfile_ape_items_current['data'][0]);
177
$thisfile_replaygain['mp3gain']['undo_left'] = intval($mp3gain_undo_left);
178
$thisfile_replaygain['mp3gain']['undo_right'] = intval($mp3gain_undo_right);
179
$thisfile_replaygain['mp3gain']['undo_wrap'] = (($mp3gain_undo_wrap == 'Y') ? true : false);
182
case 'mp3gain_minmax':
183
list($mp3gain_globalgain_min, $mp3gain_globalgain_max) = explode(',', $thisfile_ape_items_current['data'][0]);
184
$thisfile_replaygain['mp3gain']['globalgain_track_min'] = intval($mp3gain_globalgain_min);
185
$thisfile_replaygain['mp3gain']['globalgain_track_max'] = intval($mp3gain_globalgain_max);
188
case 'mp3gain_album_minmax':
189
list($mp3gain_globalgain_album_min, $mp3gain_globalgain_album_max) = explode(',', $thisfile_ape_items_current['data'][0]);
190
$thisfile_replaygain['mp3gain']['globalgain_album_min'] = intval($mp3gain_globalgain_album_min);
191
$thisfile_replaygain['mp3gain']['globalgain_album_max'] = intval($mp3gain_globalgain_album_max);
195
if (is_array($thisfile_ape_items_current['data'])) {
196
foreach ($thisfile_ape_items_current['data'] as $comment) {
197
$thisfile_ape['comments']['track'][] = $comment;
202
case 'cover art (artist)':
203
case 'cover art (back)':
204
case 'cover art (band logo)':
205
case 'cover art (band)':
206
case 'cover art (colored fish)':
207
case 'cover art (composer)':
208
case 'cover art (conductor)':
209
case 'cover art (front)':
210
case 'cover art (icon)':
211
case 'cover art (illustration)':
212
case 'cover art (lead)':
213
case 'cover art (leaflet)':
214
case 'cover art (lyricist)':
215
case 'cover art (media)':
216
case 'cover art (movie scene)':
217
case 'cover art (other icon)':
218
case 'cover art (other)':
219
case 'cover art (performance)':
220
case 'cover art (publisher logo)':
221
case 'cover art (recording)':
222
case 'cover art (studio)':
223
// list of possible cover arts from http://taglib-sharp.sourcearchive.com/documentation/2.0.3.0-2/Ape_2Tag_8cs-source.html
224
list($thisfile_ape_items_current['filename'], $thisfile_ape_items_current['data']) = explode("\x00", $thisfile_ape_items_current['data'], 2);
225
$thisfile_ape_items_current['data_offset'] = $thisfile_ape_items_current['offset'] + strlen($thisfile_ape_items_current['filename']."\x00");
226
$thisfile_ape_items_current['data_length'] = strlen($thisfile_ape_items_current['data']);
228
$thisfile_ape_items_current['image_mime'] = '';
229
$imageinfo = array();
230
$imagechunkcheck = getid3_lib::GetDataImageSize($thisfile_ape_items_current['data'], $imageinfo);
231
$thisfile_ape_items_current['image_mime'] = image_type_to_mime_type($imagechunkcheck[2]);
234
if ($this->inline_attachments === false) {
236
unset($thisfile_ape_items_current['data']);
239
if ($this->inline_attachments === true) {
241
} elseif (is_int($this->inline_attachments)) {
242
if ($this->inline_attachments < $thisfile_ape_items_current['data_length']) {
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']);
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']);
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']);
263
$info['warning'][] = 'attachment at '.$thisfile_ape_items_current['offset'].' cannot be saved to "'.$destination_filename.'" (not writable)';
265
$thisfile_ape_items_current['data_filename'] = $destination_filename;
266
unset($thisfile_ape_items_current['data']);
268
if (!isset($info['ape']['comments']['picture'])) {
269
$info['ape']['comments']['picture'] = array();
271
$info['ape']['comments']['picture'][] = array('data'=>$thisfile_ape_items_current['data'], 'image_mime'=>$thisfile_ape_items_current['image_mime']);
277
if (is_array($thisfile_ape_items_current['data'])) {
278
foreach ($thisfile_ape_items_current['data'] as $comment) {
279
$thisfile_ape['comments'][strtolower($item_key)][] = $comment;
286
if (empty($thisfile_replaygain)) {
287
unset($info['replay_gain']);
292
public function parseAPEheaderFooter($APEheaderFooterData) {
293
// http://www.uni-jena.de/~pfk/mpp/sv8/apeheader.html
296
$headerfooterinfo['raw'] = array();
297
$headerfooterinfo_raw = &$headerfooterinfo['raw'];
299
$headerfooterinfo_raw['footer_tag'] = substr($APEheaderFooterData, 0, 8);
300
if ($headerfooterinfo_raw['footer_tag'] != 'APETAGEX') {
303
$headerfooterinfo_raw['version'] = getid3_lib::LittleEndian2Int(substr($APEheaderFooterData, 8, 4));
304
$headerfooterinfo_raw['tagsize'] = getid3_lib::LittleEndian2Int(substr($APEheaderFooterData, 12, 4));
305
$headerfooterinfo_raw['tag_items'] = getid3_lib::LittleEndian2Int(substr($APEheaderFooterData, 16, 4));
306
$headerfooterinfo_raw['global_flags'] = getid3_lib::LittleEndian2Int(substr($APEheaderFooterData, 20, 4));
307
$headerfooterinfo_raw['reserved'] = substr($APEheaderFooterData, 24, 8);
309
$headerfooterinfo['tag_version'] = $headerfooterinfo_raw['version'] / 1000;
310
if ($headerfooterinfo['tag_version'] >= 2) {
311
$headerfooterinfo['flags'] = $this->parseAPEtagFlags($headerfooterinfo_raw['global_flags']);
313
return $headerfooterinfo;
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://www.uni-jena.de/~pfk/mpp/sv8/apetagflags.html
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);
326
$flags['item_contents'] = $this->APEcontentTypeFlagLookup($flags['item_contents_raw']);
331
public function APEcontentTypeFlagLookup($contenttypeid) {
332
static $APEcontentTypeFlagLookup = array(
338
return (isset($APEcontentTypeFlagLookup[$contenttypeid]) ? $APEcontentTypeFlagLookup[$contenttypeid] : 'invalid');
341
public function APEtagItemIsUTF8Lookup($itemkey) {
342
static $APEtagItemIsUTF8Lookup = array(
367
return in_array(strtolower($itemkey), $APEtagItemIsUTF8Lookup);