1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
<?php
// +----------------------------------------------------------------------+
// | PHP version 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 2002-2006 James Heinrich, Allan Hansen |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2 of the GPL license, |
// | that is bundled with this package in the file license.txt and is |
// | available through the world-wide-web at the following url: |
// | http://www.gnu.org/copyleft/gpl.html |
// +----------------------------------------------------------------------+
// | getID3() - http://getid3.sourceforge.net or http://www.getid3.org |
// +----------------------------------------------------------------------+
// | Authors: James Heinrich <info�getid3*org> |
// | Allan Hansen <ah�artemis*dk> |
// +----------------------------------------------------------------------+
// | write.apetag.php |
// | writing module for ape tags |
// | dependencies: module.tag.apetag.php |
// | dependencies: module.tag.id3v1.php |
// | dependencies: module.tag.lyrics3.php |
// +----------------------------------------------------------------------+
//
// $Id: write.apetag.php,v 1.9 2006/11/20 16:13:31 ah Exp $
class getid3_write_apetag extends getid3_handler_write
{
public $comments;
public function read() {
$engine = new getid3;
$engine->filename = $this->filename;
$engine->fp = fopen($this->filename, 'rb');
$engine->include_module('tag.apetag');
$tag = new getid3_apetag($engine);
$tag->Analyze();
if (!isset($engine->info['ape']['comments'])) {
return;
}
$this->comments = $engine->info['ape']['comments'];
// convert single element arrays to string
foreach ($this->comments as $key => $value) {
if (sizeof($value) == 1) {
$this->comments[$key] = $value[0];
}
}
return true;
}
public function write() {
// remove existing apetag
$this->remove();
$engine = new getid3;
$engine->filename = $this->filename;
$engine->fp = fopen($this->filename, 'rb');
$engine->include_module('tag.id3v1');
$engine->include_module('tag.lyrics3');
$tag = new getid3_id3v1($engine);
$tag->Analyze();
$tag = new getid3_lyrics3($engine);
$tag->Analyze();
$apetag = $this->generate_tag();
if (!$fp = @fopen($this->filename, 'a+b')) {
throw new getid3_exception('Could not open a+b: ' . $this->filename);
}
// init: audio ends at eof
$post_audio_offset = filesize($this->filename);
// lyrics3 tag present
if (@$engine->info['lyrics3']['tag_offset_start']) {
// audio ends before lyrics3 tag
$post_audio_offset = @$engine->info['lyrics3']['tag_offset_start'];
}
// id3v1 tag present
elseif (@$engine->info['id3v1']['tag_offset_start']) {
// audio ends before id3v1 tag
$post_audio_offset = $engine->info['id3v1']['tag_offset_start'];
}
// seek to end of audio data
fseek($fp, $post_audio_offset, SEEK_SET);
// save data after audio data
$post_audio_data = '';
if (filesize($this->filename) > $post_audio_offset) {
$post_audio_data = fread($fp, filesize($this->filename) - $post_audio_offset);
}
// truncate file before start of new apetag
fseek($fp, $post_audio_offset, SEEK_SET);
ftruncate($fp, ftell($fp));
// write new apetag
fwrite($fp, $apetag, strlen($apetag));
// rewrite data after audio
if (!empty($post_audio_data)) {
fwrite($fp, $post_audio_data, strlen($post_audio_data));
}
fclose($fp);
clearstatcache();
return true;
}
public function remove() {
$engine = new getid3;
$engine->filename = $this->filename;
$engine->fp = fopen($this->filename, 'rb');
$engine->include_module('tag.apetag');
$tag = new getid3_apetag($engine);
$tag->Analyze();
if (isset($engine->info['ape']['tag_offset_start']) && isset($engine->info['ape']['tag_offset_end'])) {
if (!$fp = @fopen($this->filename, 'a+b')) {
throw new getid3_exception('Could not open a+b: ' . $this->filename);
}
// get data after apetag
if (filesize($this->filename) > $engine->info['ape']['tag_offset_end']) {
fseek($fp, $engine->info['ape']['tag_offset_end'], SEEK_SET);
$data_after_ape = fread($fp, filesize($this->filename) - $engine->info['ape']['tag_offset_end']);
}
// truncate file before start of apetag
ftruncate($fp, $engine->info['ape']['tag_offset_start']);
// rewrite data after apetag
if (isset($data_after_ape)) {
fseek($fp, $engine->info['ape']['tag_offset_start'], SEEK_SET);
fwrite($fp, $data_after_ape, strlen($data_after_ape));
}
fclose($fp);
clearstatcache();
}
// success when removing non-existant tag
return true;
}
protected function generate_tag() {
// NOTE: All data passed to this function must be UTF-8 format
$items = array();
if (!is_array($this->comments)) {
throw new getid3_exception('Cannot write empty tag, use remove() instead.');
}
foreach ($this->comments as $key => $values) {
// http://www.personal.uni-jena.de/~pfk/mpp/sv8/apekey.html
// A case-insensitive vobiscomment field name that may consist of ASCII 0x20 through 0x7E.
// ASCII 0x41 through 0x5A inclusive (A-Z) is to be considered equivalent to ASCII 0x61 through 0x7A inclusive (a-z).
if (preg_match("/[^\x20-\x7E]/", $key)) {
throw new getid3_exception('Field name "' . $key . '" contains invalid character(s).');
}
$key = strtolower($key);
// convert single value comment to array
if (!is_array($values)) {
$values = array ($values);
}
$value_array = array ();
foreach ($values as $value) {
$value_array[] = str_replace("\x00", '', $value);
}
$value_string = implode("\x00", $value_array);
// length of the assigned value in bytes
$tag_item = getid3_lib::LittleEndian2String(strlen($value_string), 4);
$tag_item .= "\x00\x00\x00\x00" . $key . "\x00" . $value_string;
$items[] = $tag_item;
}
return $this->generate_header_footer($items, true) . implode('', $items) . $this->generate_header_footer($items, false);
}
protected function generate_header_footer(&$items, $is_header=false) {
$comments_length = 0;
foreach ($items as $item_data) {
$comments_length += strlen($item_data);
}
$header = 'APETAGEX';
$header .= getid3_lib::LittleEndian2String(2000, 4);
$header .= getid3_lib::LittleEndian2String(32 + $comments_length, 4);
$header .= getid3_lib::LittleEndian2String(count($items), 4);
$header .= $this->generate_flags(true, true, $is_header, 0, false);
$header .= str_repeat("\x00", 8);
return $header;
}
protected function generate_flags($header=true, $footer=true, $is_header=false, $encoding_id=0, $read_only=false) {
$flags = array_fill(0, 4, 0);
// Tag contains a header
if ($header) {
$flags[0] |= 0x80;
}
// Tag contains no footer
if (!$footer) {
$flags[0] |= 0x40;
}
// This is the header, not the footer
if ($is_header) {
$flags[0] |= 0x20;
}
// 0: Item contains text information coded in UTF-8
// 1: Item contains binary information �)
// 2: Item is a locator of external stored information ��)
// 3: reserved
$flags[3] |= ($encoding_id << 1);
// Tag or Item is Read Only
if ($read_only) {
$flags[3] |= 0x01;
}
return chr($flags[3]).chr($flags[2]).chr($flags[1]).chr($flags[0]);
}
}
?>
|