1 | <?php |
---|
2 | // +----------------------------------------------------------------------+ |
---|
3 | // | PHP version 5 | |
---|
4 | // +----------------------------------------------------------------------+ |
---|
5 | // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen | |
---|
6 | // +----------------------------------------------------------------------+ |
---|
7 | // | This source file is subject to version 2 of the GPL license, | |
---|
8 | // | that is bundled with this package in the file license.txt and is | |
---|
9 | // | available through the world-wide-web at the following url: | |
---|
10 | // | http://www.gnu.org/copyleft/gpl.html | |
---|
11 | // +----------------------------------------------------------------------+ |
---|
12 | // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org | |
---|
13 | // +----------------------------------------------------------------------+ |
---|
14 | // | Authors: James Heinrich <infoØgetid3*org> | |
---|
15 | // | Allan Hansen <ahØartemis*dk> | |
---|
16 | // +----------------------------------------------------------------------+ |
---|
17 | // | write.id3v1.php | |
---|
18 | // | writing module for id3v1 tags | |
---|
19 | // | dependencies: module.tag.id3v1.php. | |
---|
20 | // +----------------------------------------------------------------------+ |
---|
21 | // |
---|
22 | // $Id: write.id3v1.php 3318 2009-05-20 21:54:10Z vdigital $ |
---|
23 | |
---|
24 | |
---|
25 | |
---|
26 | class getid3_write_id3v1 extends getid3_handler_write |
---|
27 | { |
---|
28 | public $title; |
---|
29 | public $artist; |
---|
30 | public $album; |
---|
31 | public $year; |
---|
32 | public $genre_id; |
---|
33 | public $genre; |
---|
34 | public $comment; |
---|
35 | public $track; |
---|
36 | |
---|
37 | |
---|
38 | public function read() { |
---|
39 | |
---|
40 | $engine = new getid3; |
---|
41 | $engine->filename = $this->filename; |
---|
42 | $engine->fp = fopen($this->filename, 'rb'); |
---|
43 | $engine->include_module('tag.id3v1'); |
---|
44 | |
---|
45 | $tag = new getid3_id3v1($engine); |
---|
46 | $tag->Analyze(); |
---|
47 | |
---|
48 | if (!isset($engine->info['id3v1'])) { |
---|
49 | return; |
---|
50 | } |
---|
51 | |
---|
52 | $this->title = $engine->info['id3v1']['title']; |
---|
53 | $this->artist = $engine->info['id3v1']['artist']; |
---|
54 | $this->album = $engine->info['id3v1']['album']; |
---|
55 | $this->year = $engine->info['id3v1']['year']; |
---|
56 | $this->genre_id = $engine->info['id3v1']['genre_id']; |
---|
57 | $this->genre = $engine->info['id3v1']['genre']; |
---|
58 | $this->comment = $engine->info['id3v1']['comment']; |
---|
59 | $this->track = $engine->info['id3v1']['track']; |
---|
60 | |
---|
61 | return true; |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | public function write() { |
---|
66 | |
---|
67 | if (!$fp = @fopen($this->filename, 'r+b')) { |
---|
68 | throw new getid3_exception('Could not open r+b: ' . $this->filename); |
---|
69 | } |
---|
70 | |
---|
71 | // seek to end minus 128 bytes |
---|
72 | fseek($fp, -128, SEEK_END); |
---|
73 | |
---|
74 | // overwrite existing ID3v1 tag |
---|
75 | if (fread($fp, 3) == 'TAG') { |
---|
76 | fseek($fp, -128, SEEK_END); |
---|
77 | } |
---|
78 | |
---|
79 | // append new ID3v1 tag |
---|
80 | else { |
---|
81 | fseek($fp, 0, SEEK_END); |
---|
82 | } |
---|
83 | |
---|
84 | fwrite($fp, $this->generate_tag(), 128); |
---|
85 | |
---|
86 | fclose($fp); |
---|
87 | clearstatcache(); |
---|
88 | |
---|
89 | return true; |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | protected function generate_tag() { |
---|
94 | |
---|
95 | $result = 'TAG'; |
---|
96 | $result .= str_pad(trim(substr($this->title, 0, 30)), 30, "\x00", STR_PAD_RIGHT); |
---|
97 | $result .= str_pad(trim(substr($this->artist, 0, 30)), 30, "\x00", STR_PAD_RIGHT); |
---|
98 | $result .= str_pad(trim(substr($this->album, 0, 30)), 30, "\x00", STR_PAD_RIGHT); |
---|
99 | $result .= str_pad(trim(substr($this->year, 0, 4)), 4, "\x00", STR_PAD_LEFT); |
---|
100 | |
---|
101 | if (!empty($this->track) && ($this->track > 0) && ($this->track <= 255)) { |
---|
102 | |
---|
103 | $result .= str_pad(trim(substr($this->comment, 0, 28)), 28, "\x00", STR_PAD_RIGHT); |
---|
104 | $result .= "\x00"; |
---|
105 | $result .= chr($this->track); |
---|
106 | } |
---|
107 | else { |
---|
108 | $result .= str_pad(trim(substr($comment, 0, 30)), 30, "\x00", STR_PAD_RIGHT); |
---|
109 | } |
---|
110 | |
---|
111 | // both genre and genre_id set |
---|
112 | if ($this->genre && $this->genre_id) { |
---|
113 | if ($this->genre != getid3_id3v1::LookupGenreName($this->genre_id)) { |
---|
114 | throw new getid3_exception('Genre and genre_id does not match. Unset one and the other will be determined automatically.'); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | // only genre set |
---|
119 | elseif ($this->genre) { |
---|
120 | $this->genre_id = getid3_id3v1::LookupGenreID($this->genre); |
---|
121 | } |
---|
122 | |
---|
123 | // only genre_id set |
---|
124 | else { |
---|
125 | if ($this->genre_id < 0 || $this->genre_id > 147) { |
---|
126 | $this->genre_id = 255; // 'unknown' genre |
---|
127 | } |
---|
128 | $this->genre = getid3_id3v1::LookupGenreName($this->genre_id); |
---|
129 | } |
---|
130 | |
---|
131 | $result .= chr(intval($this->genre_id)); |
---|
132 | |
---|
133 | return $result; |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | public function remove() { |
---|
138 | |
---|
139 | if (!$fp = @fopen($this->filename, 'r+b')) { |
---|
140 | throw new getid3_exception('Could not open r+b: ' . $filename); |
---|
141 | } |
---|
142 | |
---|
143 | fseek($fp, -128, SEEK_END); |
---|
144 | if (fread($fp, 3) == 'TAG') { |
---|
145 | ftruncate($fp, filesize($this->filename) - 128); |
---|
146 | fclose($fp); |
---|
147 | clearstatcache(); |
---|
148 | } |
---|
149 | |
---|
150 | // success when removing non-existant tag |
---|
151 | return true; |
---|
152 | } |
---|
153 | |
---|
154 | } |
---|
155 | |
---|
156 | ?> |
---|