summaryrefslogtreecommitdiffstats
path: root/modules/getid3/write.apetag.php
diff options
context:
space:
mode:
Diffstat (limited to 'modules/getid3/write.apetag.php')
-rw-r--r--modules/getid3/write.apetag.php90
1 files changed, 45 insertions, 45 deletions
diff --git a/modules/getid3/write.apetag.php b/modules/getid3/write.apetag.php
index e154e814..32ba33b2 100644
--- a/modules/getid3/write.apetag.php
+++ b/modules/getid3/write.apetag.php
@@ -27,73 +27,73 @@
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'];
}
@@ -110,24 +110,24 @@ class getid3_write_apetag extends getid3_handler_write
// 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
+
+ // 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');
@@ -137,7 +137,7 @@ class getid3_write_apetag extends getid3_handler_write
$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);
}
@@ -148,7 +148,7 @@ class getid3_write_apetag extends getid3_handler_write
$data_after_ape = fread($fp, filesize($this->filename) - $engine->info['ape']['tag_offset_end']);
}
- // truncate file before start of apetag
+ // truncate file before start of apetag
ftruncate($fp, $engine->info['ape']['tag_offset_start']);
// rewrite data after apetag
@@ -156,36 +156,36 @@ class getid3_write_apetag extends getid3_handler_write
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
+
+ // 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.
+ // 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);
@@ -199,7 +199,7 @@ class getid3_write_apetag extends getid3_handler_write
// 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;
@@ -207,10 +207,10 @@ class getid3_write_apetag extends getid3_handler_write
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);
@@ -225,25 +225,25 @@ class getid3_write_apetag extends getid3_handler_write
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;
+ $flags[0] |= 0x80;
}
-
+
// Tag contains no footer
if (!$footer) {
- $flags[0] |= 0x40;
+ $flags[0] |= 0x40;
}
-
+
// This is the header, not the footer
if ($is_header) {
- $flags[0] |= 0x20;
+ $flags[0] |= 0x20;
}
// 0: Item contains text information coded in UTF-8
@@ -254,12 +254,12 @@ class getid3_write_apetag extends getid3_handler_write
// Tag or Item is Read Only
if ($read_only) {
- $flags[3] |= 0x01;
+ $flags[3] |= 0x01;
}
return chr($flags[3]).chr($flags[2]).chr($flags[1]).chr($flags[0]);
}
-
+
}
?> \ No newline at end of file