21 $apetagheadersize = 32;
24 if ($overrideendoffset == 0) {
26 fseek($fd, 0 - $id3v1tagsize - $apetagheadersize - $lyrics3tagsize, SEEK_END);
27 $APEfooterID3v1 = fread($fd, $id3v1tagsize + $apetagheadersize + $lyrics3tagsize);
30 if (substr($APEfooterID3v1, strlen($APEfooterID3v1) - $id3v1tagsize - $apetagheadersize, 8) ==
'APETAGEX') {
33 $ThisFileInfo[
'ape'][
'tag_offset_end'] = $ThisFileInfo[
'filesize'] - $id3v1tagsize;
36 } elseif (substr($APEfooterID3v1, strlen($APEfooterID3v1) - $apetagheadersize, 8) ==
'APETAGEX') {
39 $ThisFileInfo[
'ape'][
'tag_offset_end'] = $ThisFileInfo[
'filesize'];
45 fseek($fd, $overrideendoffset - $apetagheadersize, SEEK_SET);
46 if (fread($fd, 8) ==
'APETAGEX') {
47 $ThisFileInfo[
'ape'][
'tag_offset_end'] = $overrideendoffset;
51 if (!isset($ThisFileInfo[
'ape'][
'tag_offset_end'])) {
54 unset($ThisFileInfo[
'ape']);
60 $thisfile_ape = &$ThisFileInfo[
'ape'];
62 fseek($fd, $thisfile_ape[
'tag_offset_end'] - $apetagheadersize, SEEK_SET);
63 $APEfooterData = fread($fd, 32);
65 $ThisFileInfo[
'error'][] =
'Error parsing APE footer at offset '.$thisfile_ape[
'tag_offset_end'];
69 if (isset($thisfile_ape[
'footer'][
'flags'][
'header']) && $thisfile_ape[
'footer'][
'flags'][
'header']) {
70 fseek($fd, $thisfile_ape[
'tag_offset_end'] - $thisfile_ape[
'footer'][
'raw'][
'tagsize'] - $apetagheadersize, SEEK_SET);
71 $thisfile_ape[
'tag_offset_start'] = ftell($fd);
72 $APEtagData = fread($fd, $thisfile_ape[
'footer'][
'raw'][
'tagsize'] + $apetagheadersize);
74 $thisfile_ape[
'tag_offset_start'] = $thisfile_ape[
'tag_offset_end'] - $thisfile_ape[
'footer'][
'raw'][
'tagsize'];
75 fseek($fd, $thisfile_ape[
'tag_offset_start'], SEEK_SET);
76 $APEtagData = fread($fd, $thisfile_ape[
'footer'][
'raw'][
'tagsize']);
78 $ThisFileInfo[
'avdataend'] = $thisfile_ape[
'tag_offset_start'];
80 if (isset($ThisFileInfo[
'id3v1'][
'tag_offset_start']) && ($ThisFileInfo[
'id3v1'][
'tag_offset_start'] < $thisfile_ape[
'tag_offset_end'])) {
81 $ThisFileInfo[
'warning'][] =
'ID3v1 tag information ignored since it appears to be a false synch in APEtag data';
82 unset($ThisFileInfo[
'id3v1']);
83 foreach ($ThisFileInfo[
'warning'] as $key => $value) {
84 if ($value ==
'Some ID3v1 fields do not use NULL characters for padding') {
85 unset($ThisFileInfo[
'warning'][$key]);
86 sort($ThisFileInfo[
'warning']);
93 if (isset($thisfile_ape[
'footer'][
'flags'][
'header']) && $thisfile_ape[
'footer'][
'flags'][
'header']) {
94 if ($thisfile_ape[
'header'] = $this->
parseAPEheaderFooter(substr($APEtagData, 0, $apetagheadersize))) {
95 $offset += $apetagheadersize;
97 $ThisFileInfo[
'error'][] =
'Error parsing APE header at offset '.$thisfile_ape[
'tag_offset_start'];
103 $ThisFileInfo[
'replay_gain'] = array();
104 $thisfile_replaygain = &$ThisFileInfo[
'replay_gain'];
106 for ($i = 0; $i < $thisfile_ape[
'footer'][
'raw'][
'tag_items']; $i++) {
111 if (strstr(substr($APEtagData, $offset),
"\x00") ===
false) {
112 $ThisFileInfo[
'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);
115 $ItemKeyLength = strpos($APEtagData,
"\x00", $offset) - $offset;
116 $item_key = strtolower(substr($APEtagData, $offset, $ItemKeyLength));
119 $thisfile_ape[
'items'][$item_key] = array();
120 $thisfile_ape_items_current = &$thisfile_ape[
'items'][$item_key];
122 $offset += ($ItemKeyLength + 1);
123 $thisfile_ape_items_current[
'data'] = substr($APEtagData, $offset, $value_size);
124 $offset += $value_size;
126 $thisfile_ape_items_current[
'flags'] = $this->
parseAPEtagFlags($item_flags);
127 switch ($thisfile_ape_items_current[
'flags'][
'item_contents_raw']) {
130 $thisfile_ape_items_current[
'data'] = explode(
"\x00", trim($thisfile_ape_items_current[
'data']));
137 switch (strtolower($item_key)) {
138 case 'replaygain_track_gain':
139 $thisfile_replaygain[
'track'][
'adjustment'] = (float) str_replace(
',',
'.', $thisfile_ape_items_current[
'data'][0]);
140 $thisfile_replaygain[
'track'][
'originator'] =
'unspecified';
143 case 'replaygain_track_peak':
144 $thisfile_replaygain[
'track'][
'peak'] = (float) str_replace(
',',
'.', $thisfile_ape_items_current[
'data'][0]);
145 $thisfile_replaygain[
'track'][
'originator'] =
'unspecified';
146 if ($thisfile_replaygain[
'track'][
'peak'] <= 0) {
147 $ThisFileInfo[
'warning'][] =
'ReplayGain Track peak from APEtag appears invalid: '.$thisfile_replaygain[
'track'][
'peak'].
' (original value = "'.$thisfile_ape_items_current[
'data'][0].
'")';
151 case 'replaygain_album_gain':
152 $thisfile_replaygain[
'album'][
'adjustment'] = (float) str_replace(
',',
'.', $thisfile_ape_items_current[
'data'][0]);
153 $thisfile_replaygain[
'album'][
'originator'] =
'unspecified';
156 case 'replaygain_album_peak':
157 $thisfile_replaygain[
'album'][
'peak'] = (float) str_replace(
',',
'.', $thisfile_ape_items_current[
'data'][0]);
158 $thisfile_replaygain[
'album'][
'originator'] =
'unspecified';
159 if ($thisfile_replaygain[
'album'][
'peak'] <= 0) {
160 $ThisFileInfo[
'warning'][] =
'ReplayGain Album peak from APEtag appears invalid: '.$thisfile_replaygain[
'album'][
'peak'].
' (original value = "'.$thisfile_ape_items_current[
'data'][0].
'")';
165 list($mp3gain_undo_left, $mp3gain_undo_right, $mp3gain_undo_wrap) = explode(
',', $thisfile_ape_items_current[
'data'][0]);
166 $thisfile_replaygain[
'mp3gain'][
'undo_left'] = intval($mp3gain_undo_left);
167 $thisfile_replaygain[
'mp3gain'][
'undo_right'] = intval($mp3gain_undo_right);
168 $thisfile_replaygain[
'mp3gain'][
'undo_wrap'] = (($mp3gain_undo_wrap ==
'Y') ?
true :
false);
171 case 'mp3gain_minmax':
172 list($mp3gain_globalgain_min, $mp3gain_globalgain_max) = explode(
',', $thisfile_ape_items_current[
'data'][0]);
173 $thisfile_replaygain[
'mp3gain'][
'globalgain_track_min'] = intval($mp3gain_globalgain_min);
174 $thisfile_replaygain[
'mp3gain'][
'globalgain_track_max'] = intval($mp3gain_globalgain_max);
177 case 'mp3gain_album_minmax':
178 list($mp3gain_globalgain_album_min, $mp3gain_globalgain_album_max) = explode(
',', $thisfile_ape_items_current[
'data'][0]);
179 $thisfile_replaygain[
'mp3gain'][
'globalgain_album_min'] = intval($mp3gain_globalgain_album_min);
180 $thisfile_replaygain[
'mp3gain'][
'globalgain_album_max'] = intval($mp3gain_globalgain_album_max);
184 foreach ($thisfile_ape_items_current[
'data'] as
$comment) {
185 $thisfile_ape[
'comments'][
'track'][] =
$comment;
190 foreach ($thisfile_ape_items_current[
'data'] as $comment) {
191 $thisfile_ape[
'comments'][strtolower($item_key)][] =
$comment;
197 if (empty($thisfile_replaygain)) {
198 unset($ThisFileInfo[
'replay_gain']);
LittleEndian2Int($byteword, $signed=false)
parseAPEtagFlags($rawflagint)
parseAPEheaderFooter($APEheaderFooterData)