23 $info = &$this->getid3->info;
26 $this->
warning(
'Unable to check for APEtags because file is larger than '.round(PHP_INT_MAX / 1073741824).
'GB');
31 $apetagheadersize = 32;
34 if ($this->overrideendoffset == 0) {
36 $this->
fseek(0 - $id3v1tagsize - $apetagheadersize - $lyrics3tagsize, SEEK_END);
37 $APEfooterID3v1 = $this->
fread($id3v1tagsize + $apetagheadersize + $lyrics3tagsize);
40 if (substr($APEfooterID3v1, strlen($APEfooterID3v1) - $id3v1tagsize - $apetagheadersize, 8) ==
'APETAGEX') {
43 $info[
'ape'][
'tag_offset_end'] =
$info[
'filesize'] - $id3v1tagsize;
46 } elseif (substr($APEfooterID3v1, strlen($APEfooterID3v1) - $apetagheadersize, 8) ==
'APETAGEX') {
49 $info[
'ape'][
'tag_offset_end'] =
$info[
'filesize'];
55 $this->
fseek($this->overrideendoffset - $apetagheadersize);
56 if ($this->
fread(8) ==
'APETAGEX') {
61 if (!isset(
$info[
'ape'][
'tag_offset_end'])) {
70 $thisfile_ape = &
$info[
'ape'];
72 $this->
fseek($thisfile_ape[
'tag_offset_end'] - $apetagheadersize);
73 $APEfooterData = $this->
fread(32);
75 $this->
error(
'Error parsing APE footer at offset '.$thisfile_ape[
'tag_offset_end']);
79 if (isset($thisfile_ape[
'footer'][
'flags'][
'header']) && $thisfile_ape[
'footer'][
'flags'][
'header']) {
80 $this->
fseek($thisfile_ape[
'tag_offset_end'] - $thisfile_ape[
'footer'][
'raw'][
'tagsize'] - $apetagheadersize);
81 $thisfile_ape[
'tag_offset_start'] = $this->
ftell();
82 $APEtagData = $this->
fread($thisfile_ape[
'footer'][
'raw'][
'tagsize'] + $apetagheadersize);
84 $thisfile_ape[
'tag_offset_start'] = $thisfile_ape[
'tag_offset_end'] - $thisfile_ape[
'footer'][
'raw'][
'tagsize'];
85 $this->
fseek($thisfile_ape[
'tag_offset_start']);
86 $APEtagData = $this->
fread($thisfile_ape[
'footer'][
'raw'][
'tagsize']);
88 $info[
'avdataend'] = $thisfile_ape[
'tag_offset_start'];
90 if (isset(
$info[
'id3v1'][
'tag_offset_start']) && (
$info[
'id3v1'][
'tag_offset_start'] < $thisfile_ape[
'tag_offset_end'])) {
91 $this->
warning(
'ID3v1 tag information ignored since it appears to be a false synch in APEtag data');
92 unset(
$info[
'id3v1']);
93 foreach (
$info[
'warning'] as
$key => $value) {
94 if ($value ==
'Some ID3v1 fields do not use NULL characters for padding') {
96 sort(
$info[
'warning']);
103 if (isset($thisfile_ape[
'footer'][
'flags'][
'header']) && $thisfile_ape[
'footer'][
'flags'][
'header']) {
104 if ($thisfile_ape[
'header'] = $this->
parseAPEheaderFooter(substr($APEtagData, 0, $apetagheadersize))) {
105 $offset += $apetagheadersize;
107 $this->
error(
'Error parsing APE header at offset '.$thisfile_ape[
'tag_offset_start']);
114 $thisfile_replaygain = &
$info[
'replay_gain'];
116 for (
$i = 0;
$i < $thisfile_ape[
'footer'][
'raw'][
'tag_items'];
$i++) {
121 if (strstr(substr($APEtagData, $offset),
"\x00") ===
false) {
122 $this->
error(
'Cannot find null-byte (0x00) separator between ItemKey #'.
$i.
' and value. ItemKey starts '.$offset.
' bytes into the APE tag, at file offset '.($thisfile_ape[
'tag_offset_start'] + $offset));
125 $ItemKeyLength = strpos($APEtagData,
"\x00", $offset) - $offset;
126 $item_key = strtolower(substr($APEtagData, $offset, $ItemKeyLength));
129 $thisfile_ape[
'items'][$item_key] =
array();
130 $thisfile_ape_items_current = &$thisfile_ape[
'items'][$item_key];
132 $thisfile_ape_items_current[
'offset'] = $thisfile_ape[
'tag_offset_start'] + $offset;
134 $offset += ($ItemKeyLength + 1);
135 $thisfile_ape_items_current[
'data'] = substr($APEtagData, $offset, $value_size);
136 $offset += $value_size;
138 $thisfile_ape_items_current[
'flags'] = $this->
parseAPEtagFlags($item_flags);
139 switch ($thisfile_ape_items_current[
'flags'][
'item_contents_raw']) {
142 $thisfile_ape_items_current[
'data'] = explode(
"\x00", $thisfile_ape_items_current[
'data']);
150 switch (strtolower($item_key)) {
152 case 'replaygain_track_gain':
153 if (preg_match(
'#^[\\-\\+][0-9\\.,]{8}$#', $thisfile_ape_items_current[
'data'][0])) {
154 $thisfile_replaygain[
'track'][
'adjustment'] = (float) str_replace(
',',
'.', $thisfile_ape_items_current[
'data'][0]);
155 $thisfile_replaygain[
'track'][
'originator'] =
'unspecified';
157 $this->
warning(
'MP3gainTrackGain value in APEtag appears invalid: "'.$thisfile_ape_items_current[
'data'][0].
'"');
161 case 'replaygain_track_peak':
162 if (preg_match(
'#^[0-9\\.,]{8}$#', $thisfile_ape_items_current[
'data'][0])) {
163 $thisfile_replaygain[
'track'][
'peak'] = (float) str_replace(
',',
'.', $thisfile_ape_items_current[
'data'][0]);
164 $thisfile_replaygain[
'track'][
'originator'] =
'unspecified';
165 if ($thisfile_replaygain[
'track'][
'peak'] <= 0) {
166 $this->
warning(
'ReplayGain Track peak from APEtag appears invalid: '.$thisfile_replaygain[
'track'][
'peak'].
' (original value = "'.$thisfile_ape_items_current[
'data'][0].
'")');
169 $this->
warning(
'MP3gainTrackPeak value in APEtag appears invalid: "'.$thisfile_ape_items_current[
'data'][0].
'"');
173 case 'replaygain_album_gain':
174 if (preg_match(
'#^[\\-\\+][0-9\\.,]{8}$#', $thisfile_ape_items_current[
'data'][0])) {
175 $thisfile_replaygain[
'album'][
'adjustment'] = (float) str_replace(
',',
'.', $thisfile_ape_items_current[
'data'][0]);
176 $thisfile_replaygain[
'album'][
'originator'] =
'unspecified';
178 $this->
warning(
'MP3gainAlbumGain value in APEtag appears invalid: "'.$thisfile_ape_items_current[
'data'][0].
'"');
182 case 'replaygain_album_peak':
183 if (preg_match(
'#^[0-9\\.,]{8}$#', $thisfile_ape_items_current[
'data'][0])) {
184 $thisfile_replaygain[
'album'][
'peak'] = (float) str_replace(
',',
'.', $thisfile_ape_items_current[
'data'][0]);
185 $thisfile_replaygain[
'album'][
'originator'] =
'unspecified';
186 if ($thisfile_replaygain[
'album'][
'peak'] <= 0) {
187 $this->
warning(
'ReplayGain Album peak from APEtag appears invalid: '.$thisfile_replaygain[
'album'][
'peak'].
' (original value = "'.$thisfile_ape_items_current[
'data'][0].
'")');
190 $this->
warning(
'MP3gainAlbumPeak value in APEtag appears invalid: "'.$thisfile_ape_items_current[
'data'][0].
'"');
195 if (preg_match(
'#^[\\-\\+][0-9]{3},[\\-\\+][0-9]{3},[NW]$#', $thisfile_ape_items_current[
'data'][0])) {
196 list($mp3gain_undo_left, $mp3gain_undo_right, $mp3gain_undo_wrap) = explode(
',', $thisfile_ape_items_current[
'data'][0]);
197 $thisfile_replaygain[
'mp3gain'][
'undo_left'] = intval($mp3gain_undo_left);
198 $thisfile_replaygain[
'mp3gain'][
'undo_right'] = intval($mp3gain_undo_right);
199 $thisfile_replaygain[
'mp3gain'][
'undo_wrap'] = (($mp3gain_undo_wrap ==
'Y') ?
true :
false);
201 $this->
warning(
'MP3gainUndo value in APEtag appears invalid: "'.$thisfile_ape_items_current[
'data'][0].
'"');
205 case 'mp3gain_minmax':
206 if (preg_match(
'#^[0-9]{3},[0-9]{3}$#', $thisfile_ape_items_current[
'data'][0])) {
207 list($mp3gain_globalgain_min, $mp3gain_globalgain_max) = explode(
',', $thisfile_ape_items_current[
'data'][0]);
208 $thisfile_replaygain[
'mp3gain'][
'globalgain_track_min'] = intval($mp3gain_globalgain_min);
209 $thisfile_replaygain[
'mp3gain'][
'globalgain_track_max'] = intval($mp3gain_globalgain_max);
211 $this->
warning(
'MP3gainMinMax value in APEtag appears invalid: "'.$thisfile_ape_items_current[
'data'][0].
'"');
215 case 'mp3gain_album_minmax':
216 if (preg_match(
'#^[0-9]{3},[0-9]{3}$#', $thisfile_ape_items_current[
'data'][0])) {
217 list($mp3gain_globalgain_album_min, $mp3gain_globalgain_album_max) = explode(
',', $thisfile_ape_items_current[
'data'][0]);
218 $thisfile_replaygain[
'mp3gain'][
'globalgain_album_min'] = intval($mp3gain_globalgain_album_min);
219 $thisfile_replaygain[
'mp3gain'][
'globalgain_album_max'] = intval($mp3gain_globalgain_album_max);
221 $this->
warning(
'MP3gainAlbumMinMax value in APEtag appears invalid: "'.$thisfile_ape_items_current[
'data'][0].
'"');
226 if (is_array($thisfile_ape_items_current[
'data'])) {
227 foreach ($thisfile_ape_items_current[
'data'] as
$comment) {
228 $thisfile_ape[
'comments'][
'track'][] =
$comment;
233 case 'cover art (artist)':
234 case 'cover art (back)':
235 case 'cover art (band logo)':
236 case 'cover art (band)':
237 case 'cover art (colored fish)':
238 case 'cover art (composer)':
239 case 'cover art (conductor)':
240 case 'cover art (front)':
241 case 'cover art (icon)':
242 case 'cover art (illustration)':
243 case 'cover art (lead)':
244 case 'cover art (leaflet)':
245 case 'cover art (lyricist)':
246 case 'cover art (media)':
247 case 'cover art (movie scene)':
248 case 'cover art (other icon)':
249 case 'cover art (other)':
250 case 'cover art (performance)':
251 case 'cover art (publisher logo)':
252 case 'cover art (recording)':
253 case 'cover art (studio)':
255 if (is_array($thisfile_ape_items_current[
'data'])) {
256 $this->
warning(
'APEtag "'.$item_key.
'" should be flagged as Binary data, but was incorrectly flagged as UTF-8');
257 $thisfile_ape_items_current[
'data'] = implode(
"\x00", $thisfile_ape_items_current[
'data']);
259 list($thisfile_ape_items_current[
'filename'], $thisfile_ape_items_current[
'data']) = explode(
"\x00", $thisfile_ape_items_current[
'data'], 2);
260 $thisfile_ape_items_current[
'data_offset'] = $thisfile_ape_items_current[
'offset'] + strlen($thisfile_ape_items_current[
'filename'].
"\x00");
261 $thisfile_ape_items_current[
'data_length'] = strlen($thisfile_ape_items_current[
'data']);
264 $thisfile_ape_items_current[
'image_mime'] =
'';
265 $imageinfo =
array();
267 if (($imagechunkcheck ===
false) || !isset($imagechunkcheck[2])) {
268 $this->
warning(
'APEtag "'.$item_key.
'" contains invalid image data');
271 $thisfile_ape_items_current[
'image_mime'] = image_type_to_mime_type($imagechunkcheck[2]);
273 if ($this->inline_attachments ===
false) {
275 unset($thisfile_ape_items_current[
'data']);
278 if ($this->inline_attachments ===
true) {
280 } elseif (is_int($this->inline_attachments)) {
281 if ($this->inline_attachments < $thisfile_ape_items_current[
'data_length']) {
283 $this->
warning(
'attachment at '.$thisfile_ape_items_current[
'offset'].
' is too large to process inline ('.number_format($thisfile_ape_items_current[
'data_length']).
' bytes)');
284 unset($thisfile_ape_items_current[
'data']);
287 } elseif (is_string($this->inline_attachments)) {
288 $this->inline_attachments = rtrim(str_replace(
array(
'/',
'\\'), DIRECTORY_SEPARATOR, $this->inline_attachments), DIRECTORY_SEPARATOR);
289 if (!is_dir($this->inline_attachments) || !is_writable($this->inline_attachments)) {
291 $this->
warning(
'attachment at '.$thisfile_ape_items_current[
'offset'].
' cannot be saved to "'.$this->inline_attachments.
'" (not writable)');
292 unset($thisfile_ape_items_current[
'data']);
297 if (is_string($this->inline_attachments)) {
298 $destination_filename = $this->inline_attachments.DIRECTORY_SEPARATOR.md5(
$info[
'filenamepath']).
'_'.$thisfile_ape_items_current[
'data_offset'];
299 if (!file_exists($destination_filename) || is_writable($destination_filename)) {
300 file_put_contents($destination_filename, $thisfile_ape_items_current[
'data']);
302 $this->
warning(
'attachment at '.$thisfile_ape_items_current[
'offset'].
' cannot be saved to "'.$destination_filename.
'" (not writable)');
304 $thisfile_ape_items_current[
'data_filename'] = $destination_filename;
305 unset($thisfile_ape_items_current[
'data']);
307 if (!isset(
$info[
'ape'][
'comments'][
'picture'])) {
308 $info[
'ape'][
'comments'][
'picture'] =
array();
310 $comments_picture_data =
array();
311 foreach (
array(
'data',
'image_mime',
'image_width',
'image_height',
'imagetype',
'picturetype',
'description',
'datalength') as $picture_key) {
312 if (isset($thisfile_ape_items_current[$picture_key])) {
313 $comments_picture_data[$picture_key] = $thisfile_ape_items_current[$picture_key];
316 $info[
'ape'][
'comments'][
'picture'][] = $comments_picture_data;
317 unset($comments_picture_data);
323 if (is_array($thisfile_ape_items_current[
'data'])) {
324 foreach ($thisfile_ape_items_current[
'data'] as
$comment) {
325 $thisfile_ape[
'comments'][strtolower($item_key)][] =
$comment;
332 if (empty($thisfile_replaygain)) {
333 unset(
$info[
'replay_gain']);
static intValueSupported($num)
static LittleEndian2Int($byteword, $signed=false)
static GetDataImageSize($imgData, &$imageinfo=array())
Create styles array
The data for the language used.
parseAPEtagFlags($rawflagint)
parseAPEheaderFooter($APEheaderFooterData)
fseek($bytes, $whence=SEEK_SET)