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
|
<?php
/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */
// +----------------------------------------------------------------------+
// | 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.id3v1.php |
// | writing module for id3v1 tags |
// | dependencies: module.tag.id3v1.php. |
// +----------------------------------------------------------------------+
//
// $Id: write.id3v1.php,v 1.15 2006/11/20 16:09:33 ah Exp $
class getid3_write_id3v1 extends getid3_handler_write
{
public $title;
public $artist;
public $album;
public $year;
public $genre_id;
public $genre;
public $comment;
public $track;
public function read() {
$engine = new getid3;
$engine->filename = $this->filename;
$engine->fp = fopen($this->filename, 'rb');
$engine->include_module('tag.id3v1');
$tag = new getid3_id3v1($engine);
$tag->Analyze();
if (!isset($engine->info['id3v1'])) {
return;
}
$this->title = $engine->info['id3v1']['title'];
$this->artist = $engine->info['id3v1']['artist'];
$this->album = $engine->info['id3v1']['album'];
$this->year = $engine->info['id3v1']['year'];
$this->genre_id = $engine->info['id3v1']['genre_id'];
$this->genre = $engine->info['id3v1']['genre'];
$this->comment = $engine->info['id3v1']['comment'];
$this->track = $engine->info['id3v1']['track'];
return true;
}
public function write() {
if (!$fp = @fopen($this->filename, 'r+b')) {
throw new getid3_exception('Could not open r+b: ' . $this->filename);
}
// seek to end minus 128 bytes
fseek($fp, -128, SEEK_END);
// overwrite existing ID3v1 tag
if (fread($fp, 3) == 'TAG') {
fseek($fp, -128, SEEK_END);
}
// append new ID3v1 tag
else {
fseek($fp, 0, SEEK_END);
}
fwrite($fp, $this->generate_tag(), 128);
fclose($fp);
clearstatcache();
return true;
}
protected function generate_tag() {
$result = 'TAG';
$result .= str_pad(trim(substr($this->title, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
$result .= str_pad(trim(substr($this->artist, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
$result .= str_pad(trim(substr($this->album, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
$result .= str_pad(trim(substr($this->year, 0, 4)), 4, "\x00", STR_PAD_LEFT);
if (!empty($this->track) && ($this->track > 0) && ($this->track <= 255)) {
$result .= str_pad(trim(substr($this->comment, 0, 28)), 28, "\x00", STR_PAD_RIGHT);
$result .= "\x00";
$result .= chr($this->track);
}
else {
$result .= str_pad(trim(substr($comment, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
}
// both genre and genre_id set
if ($this->genre && $this->genre_id) {
if ($this->genre != getid3_id3v1::LookupGenreName($this->genre_id)) {
throw new getid3_exception('Genre and genre_id does not match. Unset one and the other will be determined automatically.');
}
}
// only genre set
elseif ($this->genre) {
$this->genre_id = getid3_id3v1::LookupGenreID($this->genre);
}
// only genre_id set
else {
if ($this->genre_id < 0 || $this->genre_id > 147) {
$this->genre_id = 255; // 'unknown' genre
}
$this->genre = getid3_id3v1::LookupGenreName($this->genre_id);
}
$result .= chr(intval($this->genre_id));
return $result;
}
public function remove() {
if (!$fp = @fopen($this->filename, 'r+b')) {
throw new getid3_exception('Could not open r+b: ' . $filename);
}
fseek($fp, -128, SEEK_END);
if (fread($fp, 3) == 'TAG') {
ftruncate($fp, filesize($this->filename) - 128);
fclose($fp);
clearstatcache();
}
// success when removing non-existant tag
return true;
}
}
?>
|