3 /////////////////////////////////////////////////////////////////
4 /// getID3() by James Heinrich <info@getid3.org> //
5 // available at https://github.com/JamesHeinrich/getID3 //
6 // or https://www.getid3.org //
7 // or http://getid3.sourceforge.net //
8 // see readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
11 // write.apetag.php //
12 // module for writing APE tags //
13 // dependencies: module.tag.apetag.php //
15 /////////////////////////////////////////////////////////////////
17 if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
20 getid3_lib
::IncludeDependency(GETID3_INCLUDEPATH
.'module.tag.apetag.php', __FILE__
, true);
22 class getid3_write_apetag
35 * ReplayGain / MP3gain tags will be copied from old tag even if not passed in data.
39 public $always_preserve_replaygain = true;
42 * Any non-critical errors will be stored here.
46 public $warnings = array();
49 * Any critical errors will be stored here.
53 public $errors = array();
55 public function __construct() {
61 public function WriteAPEtag() {
62 // NOTE: All data passed to this function must be UTF-8 format
65 $ThisFileInfo = $getID3->analyze($this->filename
);
67 if (isset($ThisFileInfo['ape']['tag_offset_start']) && isset($ThisFileInfo['lyrics3']['tag_offset_end'])) {
68 if ($ThisFileInfo['ape']['tag_offset_start'] >= $ThisFileInfo['lyrics3']['tag_offset_end']) {
69 // Current APE tag between Lyrics3 and ID3v1/EOF
70 // This break Lyrics3 functionality
71 if (!$this->DeleteAPEtag()) {
74 $ThisFileInfo = $getID3->analyze($this->filename
);
78 if ($this->always_preserve_replaygain
) {
79 $ReplayGainTagsToPreserve = array('mp3gain_minmax', 'mp3gain_album_minmax', 'mp3gain_undo', 'replaygain_track_peak', 'replaygain_track_gain', 'replaygain_album_peak', 'replaygain_album_gain');
80 foreach ($ReplayGainTagsToPreserve as $rg_key) {
81 if (isset($ThisFileInfo['ape']['items'][strtolower($rg_key)]['data'][0]) && !isset($this->tag_data
[strtoupper($rg_key)][0])) {
82 $this->tag_data
[strtoupper($rg_key)][0] = $ThisFileInfo['ape']['items'][strtolower($rg_key)]['data'][0];
87 if ($APEtag = $this->GenerateAPEtag()) {
88 if (getID3
::is_writable($this->filename
) && is_file($this->filename
) && ($fp = fopen($this->filename
, 'a+b'))) {
89 $oldignoreuserabort = ignore_user_abort(true);
92 $PostAPEdataOffset = $ThisFileInfo['avdataend'];
93 if (isset($ThisFileInfo['ape']['tag_offset_end'])) {
94 $PostAPEdataOffset = max($PostAPEdataOffset, $ThisFileInfo['ape']['tag_offset_end']);
96 if (isset($ThisFileInfo['lyrics3']['tag_offset_start'])) {
97 $PostAPEdataOffset = max($PostAPEdataOffset, $ThisFileInfo['lyrics3']['tag_offset_start']);
99 fseek($fp, $PostAPEdataOffset);
101 if ($ThisFileInfo['filesize'] > $PostAPEdataOffset) {
102 $PostAPEdata = fread($fp, $ThisFileInfo['filesize'] - $PostAPEdataOffset);
105 fseek($fp, $PostAPEdataOffset);
106 if (isset($ThisFileInfo['ape']['tag_offset_start'])) {
107 fseek($fp, $ThisFileInfo['ape']['tag_offset_start']);
109 ftruncate($fp, ftell($fp));
110 fwrite($fp, $APEtag, strlen($APEtag));
111 if (!empty($PostAPEdata)) {
112 fwrite($fp, $PostAPEdata, strlen($PostAPEdata));
116 ignore_user_abort($oldignoreuserabort);
126 public function DeleteAPEtag() {
127 $getID3 = new getID3
;
128 $ThisFileInfo = $getID3->analyze($this->filename
);
129 if (isset($ThisFileInfo['ape']['tag_offset_start']) && isset($ThisFileInfo['ape']['tag_offset_end'])) {
130 if (getID3
::is_writable($this->filename
) && is_file($this->filename
) && ($fp = fopen($this->filename
, 'a+b'))) {
133 $oldignoreuserabort = ignore_user_abort(true);
135 fseek($fp, $ThisFileInfo['ape']['tag_offset_end']);
137 if ($ThisFileInfo['filesize'] > $ThisFileInfo['ape']['tag_offset_end']) {
138 $DataAfterAPE = fread($fp, $ThisFileInfo['filesize'] - $ThisFileInfo['ape']['tag_offset_end']);
141 ftruncate($fp, $ThisFileInfo['ape']['tag_offset_start']);
142 fseek($fp, $ThisFileInfo['ape']['tag_offset_start']);
144 if (!empty($DataAfterAPE)) {
145 fwrite($fp, $DataAfterAPE, strlen($DataAfterAPE));
150 ignore_user_abort($oldignoreuserabort);
160 * @return string|false
162 public function GenerateAPEtag() {
163 // NOTE: All data passed to this function must be UTF-8 format
166 if (!is_array($this->tag_data
)) {
169 foreach ($this->tag_data
as $key => $arrayofvalues) {
170 if (!is_array($arrayofvalues)) {
175 foreach ($arrayofvalues as $value) {
176 $valuestring .= str_replace("\x00", '', $value)."\x00";
178 $valuestring = rtrim($valuestring, "\x00");
180 // Length of the assigned value in bytes
181 $tagitem = getid3_lib
::LittleEndian2String(strlen($valuestring), 4);
183 //$tagitem .= $this->GenerateAPEtagFlags(true, true, false, 0, false);
184 $tagitem .= "\x00\x00\x00\x00";
186 $tagitem .= $this->CleanAPEtagItemKey($key)."\x00";
187 $tagitem .= $valuestring;
193 return $this->GenerateAPEtagHeaderFooter($items, true).implode('', $items).$this->GenerateAPEtagHeaderFooter($items, false);
197 * @param array $items
198 * @param bool $isheader
202 public function GenerateAPEtagHeaderFooter(&$items, $isheader=false) {
204 foreach ($items as $itemdata) {
205 $tagdatalength +
= strlen($itemdata);
208 $APEheader = 'APETAGEX';
209 $APEheader .= getid3_lib
::LittleEndian2String(2000, 4);
210 $APEheader .= getid3_lib
::LittleEndian2String(32 +
$tagdatalength, 4);
211 $APEheader .= getid3_lib
::LittleEndian2String(count($items), 4);
212 $APEheader .= $this->GenerateAPEtagFlags(true, true, $isheader, 0, false);
213 $APEheader .= str_repeat("\x00", 8);
219 * @param bool $header
220 * @param bool $footer
221 * @param bool $isheader
222 * @param int $encodingid
223 * @param bool $readonly
227 public function GenerateAPEtagFlags($header=true, $footer=true, $isheader=false, $encodingid=0, $readonly=false) {
228 $APEtagFlags = array_fill(0, 4, 0);
230 $APEtagFlags[0] |
= 0x80; // Tag contains a header
233 $APEtagFlags[0] |
= 0x40; // Tag contains no footer
236 $APEtagFlags[0] |
= 0x20; // This is the header, not the footer
239 // 0: Item contains text information coded in UTF-8
240 // 1: Item contains binary information °)
241 // 2: Item is a locator of external stored information °°)
243 $APEtagFlags[3] |
= ($encodingid << 1);
246 $APEtagFlags[3] |
= 0x01; // Tag or Item is Read Only
249 return chr($APEtagFlags[3]).chr($APEtagFlags[2]).chr($APEtagFlags[1]).chr($APEtagFlags[0]);
253 * @param string $itemkey
257 public function CleanAPEtagItemKey($itemkey) {
258 $itemkey = preg_replace("#[^\x20-\x7E]#i", '', $itemkey);
260 // http://www.personal.uni-jena.de/~pfk/mpp/sv8/apekey.html
261 switch (strtoupper($itemkey)) {
266 $itemkey = strtoupper($itemkey);
270 $itemkey = ucwords($itemkey);