Annotation of /trunk/lib/getid3/module.tag.apetag.php
Parent Directory
|
Revision Log
Revision 2 - (view) (download)
| 1 : | andphe | 2 | <?php |
| 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 : | ///////////////////////////////////////////////////////////////// | ||
| 9 : | // // | ||
| 10 : | // module.tag.apetag.php // | ||
| 11 : | // module for analyzing APE tags // | ||
| 12 : | // dependencies: NONE // | ||
| 13 : | // /// | ||
| 14 : | ///////////////////////////////////////////////////////////////// | ||
| 15 : | // MOS Intruder Alerts | ||
| 16 : | defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' ); | ||
| 17 : | |||
| 18 : | class getid3_apetag | ||
| 19 : | { | ||
| 20 : | |||
| 21 : | function getid3_apetag(&$fd, &$ThisFileInfo, $overrideendoffset=0) { | ||
| 22 : | $id3v1tagsize = 128; | ||
| 23 : | $apetagheadersize = 32; | ||
| 24 : | $lyrics3tagsize = 10; | ||
| 25 : | |||
| 26 : | if ($overrideendoffset == 0) { | ||
| 27 : | |||
| 28 : | fseek($fd, 0 - $id3v1tagsize - $apetagheadersize - $lyrics3tagsize, SEEK_END); | ||
| 29 : | $APEfooterID3v1 = fread($fd, $id3v1tagsize + $apetagheadersize + $lyrics3tagsize); | ||
| 30 : | |||
| 31 : | //if (preg_match('/APETAGEX.{24}TAG.{125}$/i', $APEfooterID3v1)) { | ||
| 32 : | if (substr($APEfooterID3v1, strlen($APEfooterID3v1) - $id3v1tagsize - $apetagheadersize, 8) == 'APETAGEX') { | ||
| 33 : | |||
| 34 : | // APE tag found before ID3v1 | ||
| 35 : | $ThisFileInfo['ape']['tag_offset_end'] = $ThisFileInfo['filesize'] - $id3v1tagsize; | ||
| 36 : | |||
| 37 : | //} elseif (preg_match('/APETAGEX.{24}$/i', $APEfooterID3v1)) { | ||
| 38 : | } elseif (substr($APEfooterID3v1, strlen($APEfooterID3v1) - $apetagheadersize, 8) == 'APETAGEX') { | ||
| 39 : | |||
| 40 : | // APE tag found, no ID3v1 | ||
| 41 : | $ThisFileInfo['ape']['tag_offset_end'] = $ThisFileInfo['filesize']; | ||
| 42 : | |||
| 43 : | } | ||
| 44 : | |||
| 45 : | } else { | ||
| 46 : | |||
| 47 : | fseek($fd, $overrideendoffset - $apetagheadersize, SEEK_SET); | ||
| 48 : | if (fread($fd, 8) == 'APETAGEX') { | ||
| 49 : | $ThisFileInfo['ape']['tag_offset_end'] = $overrideendoffset; | ||
| 50 : | } | ||
| 51 : | |||
| 52 : | } | ||
| 53 : | if (!isset($ThisFileInfo['ape']['tag_offset_end'])) { | ||
| 54 : | |||
| 55 : | // APE tag not found | ||
| 56 : | unset($ThisFileInfo['ape']); | ||
| 57 : | return false; | ||
| 58 : | |||
| 59 : | } | ||
| 60 : | |||
| 61 : | // shortcut | ||
| 62 : | $thisfile_ape = &$ThisFileInfo['ape']; | ||
| 63 : | |||
| 64 : | fseek($fd, $thisfile_ape['tag_offset_end'] - $apetagheadersize, SEEK_SET); | ||
| 65 : | $APEfooterData = fread($fd, 32); | ||
| 66 : | if (!($thisfile_ape['footer'] = $this->parseAPEheaderFooter($APEfooterData))) { | ||
| 67 : | $ThisFileInfo['error'][] = 'Error parsing APE footer at offset '.$thisfile_ape['tag_offset_end']; | ||
| 68 : | return false; | ||
| 69 : | } | ||
| 70 : | |||
| 71 : | if (isset($thisfile_ape['footer']['flags']['header']) && $thisfile_ape['footer']['flags']['header']) { | ||
| 72 : | fseek($fd, $thisfile_ape['tag_offset_end'] - $thisfile_ape['footer']['raw']['tagsize'] - $apetagheadersize, SEEK_SET); | ||
| 73 : | $thisfile_ape['tag_offset_start'] = ftell($fd); | ||
| 74 : | $APEtagData = fread($fd, $thisfile_ape['footer']['raw']['tagsize'] + $apetagheadersize); | ||
| 75 : | } else { | ||
| 76 : | $thisfile_ape['tag_offset_start'] = $thisfile_ape['tag_offset_end'] - $thisfile_ape['footer']['raw']['tagsize']; | ||
| 77 : | fseek($fd, $thisfile_ape['tag_offset_start'], SEEK_SET); | ||
| 78 : | $APEtagData = fread($fd, $thisfile_ape['footer']['raw']['tagsize']); | ||
| 79 : | } | ||
| 80 : | $ThisFileInfo['avdataend'] = $thisfile_ape['tag_offset_start']; | ||
| 81 : | |||
| 82 : | if (isset($ThisFileInfo['id3v1']['tag_offset_start']) && ($ThisFileInfo['id3v1']['tag_offset_start'] < $thisfile_ape['tag_offset_end'])) { | ||
| 83 : | $ThisFileInfo['warning'][] = 'ID3v1 tag information ignored since it appears to be a false synch in APEtag data'; | ||
| 84 : | unset($ThisFileInfo['id3v1']); | ||
| 85 : | foreach ($ThisFileInfo['warning'] as $key => $value) { | ||
| 86 : | if ($value == 'Some ID3v1 fields do not use NULL characters for padding') { | ||
| 87 : | unset($ThisFileInfo['warning'][$key]); | ||
| 88 : | sort($ThisFileInfo['warning']); | ||
| 89 : | break; | ||
| 90 : | } | ||
| 91 : | } | ||
| 92 : | } | ||
| 93 : | |||
| 94 : | $offset = 0; | ||
| 95 : | if (isset($thisfile_ape['footer']['flags']['header']) && $thisfile_ape['footer']['flags']['header']) { | ||
| 96 : | if ($thisfile_ape['header'] = $this->parseAPEheaderFooter(substr($APEtagData, 0, $apetagheadersize))) { | ||
| 97 : | $offset += $apetagheadersize; | ||
| 98 : | } else { | ||
| 99 : | $ThisFileInfo['error'][] = 'Error parsing APE header at offset '.$thisfile_ape['tag_offset_start']; | ||
| 100 : | return false; | ||
| 101 : | } | ||
| 102 : | } | ||
| 103 : | |||
| 104 : | // shortcut | ||
| 105 : | $ThisFileInfo['replay_gain'] = array(); | ||
| 106 : | $thisfile_replaygain = &$ThisFileInfo['replay_gain']; | ||
| 107 : | |||
| 108 : | for ($i = 0; $i < $thisfile_ape['footer']['raw']['tag_items']; $i++) { | ||
| 109 : | $value_size = getid3_lib::LittleEndian2Int(substr($APEtagData, $offset, 4)); | ||
| 110 : | $offset += 4; | ||
| 111 : | $item_flags = getid3_lib::LittleEndian2Int(substr($APEtagData, $offset, 4)); | ||
| 112 : | $offset += 4; | ||
| 113 : | if (strstr(substr($APEtagData, $offset), "\x00") === false) { | ||
| 114 : | $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 : | return false; | ||
| 116 : | } | ||
| 117 : | $ItemKeyLength = strpos($APEtagData, "\x00", $offset) - $offset; | ||
| 118 : | $item_key = strtolower(substr($APEtagData, $offset, $ItemKeyLength)); | ||
| 119 : | |||
| 120 : | // shortcut | ||
| 121 : | $thisfile_ape['items'][$item_key] = array(); | ||
| 122 : | $thisfile_ape_items_current = &$thisfile_ape['items'][$item_key]; | ||
| 123 : | |||
| 124 : | $offset += ($ItemKeyLength + 1); // skip 0x00 terminator | ||
| 125 : | $thisfile_ape_items_current['data'] = substr($APEtagData, $offset, $value_size); | ||
| 126 : | $offset += $value_size; | ||
| 127 : | |||
| 128 : | $thisfile_ape_items_current['flags'] = $this->parseAPEtagFlags($item_flags); | ||
| 129 : | switch ($thisfile_ape_items_current['flags']['item_contents_raw']) { | ||
| 130 : | case 0: // UTF-8 | ||
| 131 : | case 3: // Locator (URL, filename, etc), UTF-8 encoded | ||
| 132 : | $thisfile_ape_items_current['data'] = explode("\x00", trim($thisfile_ape_items_current['data'])); | ||
| 133 : | break; | ||
| 134 : | |||
| 135 : | default: // binary data | ||
| 136 : | break; | ||
| 137 : | } | ||
| 138 : | |||
| 139 : | switch (strtolower($item_key)) { | ||
| 140 : | case 'replaygain_track_gain': | ||
| 141 : | $thisfile_replaygain['track']['adjustment'] = (float) str_replace(',', '.', $thisfile_ape_items_current['data'][0]); // float casting will see "0,95" as zero! | ||
| 142 : | $thisfile_replaygain['track']['originator'] = 'unspecified'; | ||
| 143 : | break; | ||
| 144 : | |||
| 145 : | case 'replaygain_track_peak': | ||
| 146 : | $thisfile_replaygain['track']['peak'] = (float) str_replace(',', '.', $thisfile_ape_items_current['data'][0]); // float casting will see "0,95" as zero! | ||
| 147 : | $thisfile_replaygain['track']['originator'] = 'unspecified'; | ||
| 148 : | if ($thisfile_replaygain['track']['peak'] <= 0) { | ||
| 149 : | $ThisFileInfo['warning'][] = 'ReplayGain Track peak from APEtag appears invalid: '.$thisfile_replaygain['track']['peak'].' (original value = "'.$thisfile_ape_items_current['data'][0].'")'; | ||
| 150 : | } | ||
| 151 : | break; | ||
| 152 : | |||
| 153 : | case 'replaygain_album_gain': | ||
| 154 : | $thisfile_replaygain['album']['adjustment'] = (float) str_replace(',', '.', $thisfile_ape_items_current['data'][0]); // float casting will see "0,95" as zero! | ||
| 155 : | $thisfile_replaygain['album']['originator'] = 'unspecified'; | ||
| 156 : | break; | ||
| 157 : | |||
| 158 : | case 'replaygain_album_peak': | ||
| 159 : | $thisfile_replaygain['album']['peak'] = (float) str_replace(',', '.', $thisfile_ape_items_current['data'][0]); // float casting will see "0,95" as zero! | ||
| 160 : | $thisfile_replaygain['album']['originator'] = 'unspecified'; | ||
| 161 : | if ($thisfile_replaygain['album']['peak'] <= 0) { | ||
| 162 : | $ThisFileInfo['warning'][] = 'ReplayGain Album peak from APEtag appears invalid: '.$thisfile_replaygain['album']['peak'].' (original value = "'.$thisfile_ape_items_current['data'][0].'")'; | ||
| 163 : | } | ||
| 164 : | break; | ||
| 165 : | |||
| 166 : | case 'mp3gain_undo': | ||
| 167 : | list($mp3gain_undo_left, $mp3gain_undo_right, $mp3gain_undo_wrap) = explode(',', $thisfile_ape_items_current['data'][0]); | ||
| 168 : | $thisfile_replaygain['mp3gain']['undo_left'] = intval($mp3gain_undo_left); | ||
| 169 : | $thisfile_replaygain['mp3gain']['undo_right'] = intval($mp3gain_undo_right); | ||
| 170 : | $thisfile_replaygain['mp3gain']['undo_wrap'] = (($mp3gain_undo_wrap == 'Y') ? true : false); | ||
| 171 : | break; | ||
| 172 : | |||
| 173 : | case 'mp3gain_minmax': | ||
| 174 : | list($mp3gain_globalgain_min, $mp3gain_globalgain_max) = explode(',', $thisfile_ape_items_current['data'][0]); | ||
| 175 : | $thisfile_replaygain['mp3gain']['globalgain_track_min'] = intval($mp3gain_globalgain_min); | ||
| 176 : | $thisfile_replaygain['mp3gain']['globalgain_track_max'] = intval($mp3gain_globalgain_max); | ||
| 177 : | break; | ||
| 178 : | |||
| 179 : | case 'mp3gain_album_minmax': | ||
| 180 : | list($mp3gain_globalgain_album_min, $mp3gain_globalgain_album_max) = explode(',', $thisfile_ape_items_current['data'][0]); | ||
| 181 : | $thisfile_replaygain['mp3gain']['globalgain_album_min'] = intval($mp3gain_globalgain_album_min); | ||
| 182 : | $thisfile_replaygain['mp3gain']['globalgain_album_max'] = intval($mp3gain_globalgain_album_max); | ||
| 183 : | break; | ||
| 184 : | |||
| 185 : | case 'tracknumber': | ||
| 186 : | foreach ($thisfile_ape_items_current['data'] as $comment) { | ||
| 187 : | $thisfile_ape['comments']['track'][] = $comment; | ||
| 188 : | } | ||
| 189 : | break; | ||
| 190 : | |||
| 191 : | default: | ||
| 192 : | foreach ($thisfile_ape_items_current['data'] as $comment) { | ||
| 193 : | $thisfile_ape['comments'][strtolower($item_key)][] = $comment; | ||
| 194 : | } | ||
| 195 : | break; | ||
| 196 : | } | ||
| 197 : | |||
| 198 : | } | ||
| 199 : | if (empty($thisfile_replaygain)) { | ||
| 200 : | unset($ThisFileInfo['replay_gain']); | ||
| 201 : | } | ||
| 202 : | |||
| 203 : | return true; | ||
| 204 : | } | ||
| 205 : | |||
| 206 : | function parseAPEheaderFooter($APEheaderFooterData) { | ||
| 207 : | // http://www.uni-jena.de/~pfk/mpp/sv8/apeheader.html | ||
| 208 : | |||
| 209 : | // shortcut | ||
| 210 : | $headerfooterinfo['raw'] = array(); | ||
| 211 : | $headerfooterinfo_raw = &$headerfooterinfo['raw']; | ||
| 212 : | |||
| 213 : | $headerfooterinfo_raw['footer_tag'] = substr($APEheaderFooterData, 0, 8); | ||
| 214 : | if ($headerfooterinfo_raw['footer_tag'] != 'APETAGEX') { | ||
| 215 : | return false; | ||
| 216 : | } | ||
| 217 : | $headerfooterinfo_raw['version'] = getid3_lib::LittleEndian2Int(substr($APEheaderFooterData, 8, 4)); | ||
| 218 : | $headerfooterinfo_raw['tagsize'] = getid3_lib::LittleEndian2Int(substr($APEheaderFooterData, 12, 4)); | ||
| 219 : | $headerfooterinfo_raw['tag_items'] = getid3_lib::LittleEndian2Int(substr($APEheaderFooterData, 16, 4)); | ||
| 220 : | $headerfooterinfo_raw['global_flags'] = getid3_lib::LittleEndian2Int(substr($APEheaderFooterData, 20, 4)); | ||
| 221 : | $headerfooterinfo_raw['reserved'] = substr($APEheaderFooterData, 24, 8); | ||
| 222 : | |||
| 223 : | $headerfooterinfo['tag_version'] = $headerfooterinfo_raw['version'] / 1000; | ||
| 224 : | if ($headerfooterinfo['tag_version'] >= 2) { | ||
| 225 : | $headerfooterinfo['flags'] = $this->parseAPEtagFlags($headerfooterinfo_raw['global_flags']); | ||
| 226 : | } | ||
| 227 : | return $headerfooterinfo; | ||
| 228 : | } | ||
| 229 : | |||
| 230 : | function parseAPEtagFlags($rawflagint) { | ||
| 231 : | // "Note: APE Tags 1.0 do not use any of the APE Tag flags. | ||
| 232 : | // All are set to zero on creation and ignored on reading." | ||
| 233 : | // http://www.uni-jena.de/~pfk/mpp/sv8/apetagflags.html | ||
| 234 : | $flags['header'] = (bool) ($rawflagint & 0x80000000); | ||
| 235 : | $flags['footer'] = (bool) ($rawflagint & 0x40000000); | ||
| 236 : | $flags['this_is_header'] = (bool) ($rawflagint & 0x20000000); | ||
| 237 : | $flags['item_contents_raw'] = ($rawflagint & 0x00000006) >> 1; | ||
| 238 : | $flags['read_only'] = (bool) ($rawflagint & 0x00000001); | ||
| 239 : | |||
| 240 : | $flags['item_contents'] = $this->APEcontentTypeFlagLookup($flags['item_contents_raw']); | ||
| 241 : | |||
| 242 : | return $flags; | ||
| 243 : | } | ||
| 244 : | |||
| 245 : | function APEcontentTypeFlagLookup($contenttypeid) { | ||
| 246 : | static $APEcontentTypeFlagLookup = array( | ||
| 247 : | 0 => 'utf-8', | ||
| 248 : | 1 => 'binary', | ||
| 249 : | 2 => 'external', | ||
| 250 : | 3 => 'reserved' | ||
| 251 : | ); | ||
| 252 : | return (isset($APEcontentTypeFlagLookup[$contenttypeid]) ? $APEcontentTypeFlagLookup[$contenttypeid] : 'invalid'); | ||
| 253 : | } | ||
| 254 : | |||
| 255 : | function APEtagItemIsUTF8Lookup($itemkey) { | ||
| 256 : | static $APEtagItemIsUTF8Lookup = array( | ||
| 257 : | 'title', | ||
| 258 : | 'subtitle', | ||
| 259 : | 'artist', | ||
| 260 : | 'album', | ||
| 261 : | 'debut album', | ||
| 262 : | 'publisher', | ||
| 263 : | 'conductor', | ||
| 264 : | 'track', | ||
| 265 : | 'composer', | ||
| 266 : | 'comment', | ||
| 267 : | 'copyright', | ||
| 268 : | 'publicationright', | ||
| 269 : | 'file', | ||
| 270 : | 'year', | ||
| 271 : | 'record date', | ||
| 272 : | 'record location', | ||
| 273 : | 'genre', | ||
| 274 : | 'media', | ||
| 275 : | 'related', | ||
| 276 : | 'isrc', | ||
| 277 : | 'abstract', | ||
| 278 : | 'language', | ||
| 279 : | 'bibliography' | ||
| 280 : | ); | ||
| 281 : | return in_array(strtolower($itemkey), $APEtagItemIsUTF8Lookup); | ||
| 282 : | } | ||
| 283 : | |||
| 284 : | } | ||
| 285 : | |||
| 286 : | ?> |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |
Web Hosting provided by Network Redux.

