[3318] | 1 | <?php |
---|
[3544] | 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 | // write.apetag.php // |
---|
| 11 | // module for writing APE tags // |
---|
| 12 | // dependencies: module.tag.apetag.php // |
---|
| 13 | // /// |
---|
| 14 | ///////////////////////////////////////////////////////////////// |
---|
[3318] | 15 | |
---|
| 16 | |
---|
[3544] | 17 | getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.apetag.php', __FILE__, true); |
---|
| 18 | |
---|
| 19 | class getid3_write_apetag |
---|
[3318] | 20 | { |
---|
| 21 | |
---|
[3544] | 22 | var $filename; |
---|
| 23 | var $tag_data; |
---|
| 24 | var $always_preserve_replaygain = true; // ReplayGain / MP3gain tags will be copied from old tag even if not passed in data |
---|
| 25 | var $warnings = array(); // any non-critical errors will be stored here |
---|
| 26 | var $errors = array(); // any critical errors will be stored here |
---|
[3318] | 27 | |
---|
[3544] | 28 | function getid3_write_apetag() { |
---|
| 29 | return true; |
---|
| 30 | } |
---|
[3318] | 31 | |
---|
[3544] | 32 | function WriteAPEtag() { |
---|
| 33 | // NOTE: All data passed to this function must be UTF-8 format |
---|
[3318] | 34 | |
---|
[3544] | 35 | $getID3 = new getID3; |
---|
| 36 | $ThisFileInfo = $getID3->analyze($this->filename); |
---|
[3318] | 37 | |
---|
[3544] | 38 | if (isset($ThisFileInfo['ape']['tag_offset_start']) && isset($ThisFileInfo['lyrics3']['tag_offset_end'])) { |
---|
| 39 | if ($ThisFileInfo['ape']['tag_offset_start'] >= $ThisFileInfo['lyrics3']['tag_offset_end']) { |
---|
| 40 | // Current APE tag between Lyrics3 and ID3v1/EOF |
---|
| 41 | // This break Lyrics3 functionality |
---|
| 42 | if (!$this->DeleteAPEtag()) { |
---|
| 43 | return false; |
---|
| 44 | } |
---|
| 45 | $ThisFileInfo = $getID3->analyze($this->filename); |
---|
| 46 | } |
---|
| 47 | } |
---|
[3318] | 48 | |
---|
[3544] | 49 | if ($this->always_preserve_replaygain) { |
---|
| 50 | $ReplayGainTagsToPreserve = array('mp3gain_minmax', 'mp3gain_album_minmax', 'mp3gain_undo', 'replaygain_track_peak', 'replaygain_track_gain', 'replaygain_album_peak', 'replaygain_album_gain'); |
---|
| 51 | foreach ($ReplayGainTagsToPreserve as $rg_key) { |
---|
| 52 | if (isset($ThisFileInfo['ape']['items'][strtolower($rg_key)]['data'][0]) && !isset($this->tag_data[strtoupper($rg_key)][0])) { |
---|
| 53 | $this->tag_data[strtoupper($rg_key)][0] = $ThisFileInfo['ape']['items'][strtolower($rg_key)]['data'][0]; |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | } |
---|
[3318] | 57 | |
---|
[3544] | 58 | if ($APEtag = $this->GenerateAPEtag()) { |
---|
| 59 | if ($fp = @fopen($this->filename, 'a+b')) { |
---|
| 60 | $oldignoreuserabort = ignore_user_abort(true); |
---|
| 61 | flock($fp, LOCK_EX); |
---|
[3318] | 62 | |
---|
[3544] | 63 | $PostAPEdataOffset = $ThisFileInfo['avdataend']; |
---|
| 64 | if (isset($ThisFileInfo['ape']['tag_offset_end'])) { |
---|
| 65 | $PostAPEdataOffset = max($PostAPEdataOffset, $ThisFileInfo['ape']['tag_offset_end']); |
---|
| 66 | } |
---|
| 67 | if (isset($ThisFileInfo['lyrics3']['tag_offset_start'])) { |
---|
| 68 | $PostAPEdataOffset = max($PostAPEdataOffset, $ThisFileInfo['lyrics3']['tag_offset_start']); |
---|
| 69 | } |
---|
| 70 | fseek($fp, $PostAPEdataOffset, SEEK_SET); |
---|
| 71 | $PostAPEdata = ''; |
---|
| 72 | if ($ThisFileInfo['filesize'] > $PostAPEdataOffset) { |
---|
| 73 | $PostAPEdata = fread($fp, $ThisFileInfo['filesize'] - $PostAPEdataOffset); |
---|
| 74 | } |
---|
[3318] | 75 | |
---|
[3544] | 76 | fseek($fp, $PostAPEdataOffset, SEEK_SET); |
---|
| 77 | if (isset($ThisFileInfo['ape']['tag_offset_start'])) { |
---|
| 78 | fseek($fp, $ThisFileInfo['ape']['tag_offset_start'], SEEK_SET); |
---|
| 79 | } |
---|
| 80 | ftruncate($fp, ftell($fp)); |
---|
| 81 | fwrite($fp, $APEtag, strlen($APEtag)); |
---|
| 82 | if (!empty($PostAPEdata)) { |
---|
| 83 | fwrite($fp, $PostAPEdata, strlen($PostAPEdata)); |
---|
| 84 | } |
---|
| 85 | flock($fp, LOCK_UN); |
---|
| 86 | fclose($fp); |
---|
| 87 | ignore_user_abort($oldignoreuserabort); |
---|
| 88 | return true; |
---|
[3318] | 89 | |
---|
[3544] | 90 | } |
---|
| 91 | return false; |
---|
| 92 | } |
---|
| 93 | return false; |
---|
| 94 | } |
---|
[3318] | 95 | |
---|
[3544] | 96 | function DeleteAPEtag() { |
---|
| 97 | $getID3 = new getID3; |
---|
| 98 | $ThisFileInfo = $getID3->analyze($this->filename); |
---|
| 99 | if (isset($ThisFileInfo['ape']['tag_offset_start']) && isset($ThisFileInfo['ape']['tag_offset_end'])) { |
---|
| 100 | if ($fp = @fopen($this->filename, 'a+b')) { |
---|
[3318] | 101 | |
---|
[3544] | 102 | flock($fp, LOCK_EX); |
---|
| 103 | $oldignoreuserabort = ignore_user_abort(true); |
---|
[3318] | 104 | |
---|
[3544] | 105 | fseek($fp, $ThisFileInfo['ape']['tag_offset_end'], SEEK_SET); |
---|
| 106 | $DataAfterAPE = ''; |
---|
| 107 | if ($ThisFileInfo['filesize'] > $ThisFileInfo['ape']['tag_offset_end']) { |
---|
| 108 | $DataAfterAPE = fread($fp, $ThisFileInfo['filesize'] - $ThisFileInfo['ape']['tag_offset_end']); |
---|
| 109 | } |
---|
[3318] | 110 | |
---|
[3544] | 111 | ftruncate($fp, $ThisFileInfo['ape']['tag_offset_start']); |
---|
| 112 | fseek($fp, $ThisFileInfo['ape']['tag_offset_start'], SEEK_SET); |
---|
[3318] | 113 | |
---|
[3544] | 114 | if (!empty($DataAfterAPE)) { |
---|
| 115 | fwrite($fp, $DataAfterAPE, strlen($DataAfterAPE)); |
---|
| 116 | } |
---|
[3318] | 117 | |
---|
[3544] | 118 | flock($fp, LOCK_UN); |
---|
| 119 | fclose($fp); |
---|
| 120 | ignore_user_abort($oldignoreuserabort); |
---|
[3318] | 121 | |
---|
[3544] | 122 | return true; |
---|
[3318] | 123 | |
---|
[3544] | 124 | } |
---|
| 125 | return false; |
---|
| 126 | } |
---|
| 127 | return true; |
---|
| 128 | } |
---|
[3318] | 129 | |
---|
| 130 | |
---|
[3544] | 131 | function GenerateAPEtag() { |
---|
| 132 | // NOTE: All data passed to this function must be UTF-8 format |
---|
[3318] | 133 | |
---|
[3544] | 134 | $items = array(); |
---|
| 135 | if (!is_array($this->tag_data)) { |
---|
| 136 | return false; |
---|
| 137 | } |
---|
| 138 | foreach ($this->tag_data as $key => $arrayofvalues) { |
---|
| 139 | if (!is_array($arrayofvalues)) { |
---|
| 140 | return false; |
---|
| 141 | } |
---|
[3318] | 142 | |
---|
[3544] | 143 | $valuestring = ''; |
---|
| 144 | foreach ($arrayofvalues as $value) { |
---|
| 145 | $valuestring .= str_replace("\x00", '', $value)."\x00"; |
---|
| 146 | } |
---|
| 147 | $valuestring = rtrim($valuestring, "\x00"); |
---|
[3318] | 148 | |
---|
[3544] | 149 | // Length of the assigned value in bytes |
---|
| 150 | $tagitem = getid3_lib::LittleEndian2String(strlen($valuestring), 4); |
---|
[3318] | 151 | |
---|
[3544] | 152 | //$tagitem .= $this->GenerateAPEtagFlags(true, true, false, 0, false); |
---|
| 153 | $tagitem .= "\x00\x00\x00\x00"; |
---|
[3318] | 154 | |
---|
[3544] | 155 | $tagitem .= $this->CleanAPEtagItemKey($key)."\x00"; |
---|
| 156 | $tagitem .= $valuestring; |
---|
[3318] | 157 | |
---|
[3544] | 158 | $items[] = $tagitem; |
---|
[3318] | 159 | |
---|
[3544] | 160 | } |
---|
[3318] | 161 | |
---|
[3544] | 162 | return $this->GenerateAPEtagHeaderFooter($items, true).implode('', $items).$this->GenerateAPEtagHeaderFooter($items, false); |
---|
| 163 | } |
---|
[3318] | 164 | |
---|
[3544] | 165 | function GenerateAPEtagHeaderFooter(&$items, $isheader=false) { |
---|
| 166 | $tagdatalength = 0; |
---|
| 167 | foreach ($items as $itemdata) { |
---|
| 168 | $tagdatalength += strlen($itemdata); |
---|
| 169 | } |
---|
[3318] | 170 | |
---|
[3544] | 171 | $APEheader = 'APETAGEX'; |
---|
| 172 | $APEheader .= getid3_lib::LittleEndian2String(2000, 4); |
---|
| 173 | $APEheader .= getid3_lib::LittleEndian2String(32 + $tagdatalength, 4); |
---|
| 174 | $APEheader .= getid3_lib::LittleEndian2String(count($items), 4); |
---|
| 175 | $APEheader .= $this->GenerateAPEtagFlags(true, true, $isheader, 0, false); |
---|
| 176 | $APEheader .= str_repeat("\x00", 8); |
---|
[3318] | 177 | |
---|
[3544] | 178 | return $APEheader; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | function GenerateAPEtagFlags($header=true, $footer=true, $isheader=false, $encodingid=0, $readonly=false) { |
---|
| 182 | $APEtagFlags = array_fill(0, 4, 0); |
---|
| 183 | if ($header) { |
---|
| 184 | $APEtagFlags[0] |= 0x80; // Tag contains a header |
---|
| 185 | } |
---|
| 186 | if (!$footer) { |
---|
| 187 | $APEtagFlags[0] |= 0x40; // Tag contains no footer |
---|
| 188 | } |
---|
| 189 | if ($isheader) { |
---|
| 190 | $APEtagFlags[0] |= 0x20; // This is the header, not the footer |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | // 0: Item contains text information coded in UTF-8 |
---|
| 194 | // 1: Item contains binary information °) |
---|
| 195 | // 2: Item is a locator of external stored information °°) |
---|
| 196 | // 3: reserved |
---|
| 197 | $APEtagFlags[3] |= ($encodingid << 1); |
---|
| 198 | |
---|
| 199 | if ($readonly) { |
---|
| 200 | $APEtagFlags[3] |= 0x01; // Tag or Item is Read Only |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | return chr($APEtagFlags[3]).chr($APEtagFlags[2]).chr($APEtagFlags[1]).chr($APEtagFlags[0]); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | function CleanAPEtagItemKey($itemkey) { |
---|
| 207 | $itemkey = eregi_replace("[^\x20-\x7E]", '', $itemkey); |
---|
| 208 | |
---|
| 209 | // http://www.personal.uni-jena.de/~pfk/mpp/sv8/apekey.html |
---|
| 210 | switch (strtoupper($itemkey)) { |
---|
| 211 | case 'EAN/UPC': |
---|
| 212 | case 'ISBN': |
---|
| 213 | case 'LC': |
---|
| 214 | case 'ISRC': |
---|
| 215 | $itemkey = strtoupper($itemkey); |
---|
| 216 | break; |
---|
| 217 | |
---|
| 218 | default: |
---|
| 219 | $itemkey = ucwords($itemkey); |
---|
| 220 | break; |
---|
| 221 | } |
---|
| 222 | return $itemkey; |
---|
| 223 | |
---|
| 224 | } |
---|
| 225 | |
---|
[3318] | 226 | } |
---|
| 227 | |
---|
| 228 | ?> |
---|