summaryrefslogtreecommitdiffstats
path: root/modules/getid3/write.lyrics3.php
blob: eab0d8d672b4c239553f662f259c335a0be834bf (plain)
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
<?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.lyrics3.php                                                    |
// | writing module for lyrics3 2.00 tags                                 |
// | dependencies: module.tag.lyrics3.php.                                |
// | dependencies: module.tag.id3v1.php                                   |
// +----------------------------------------------------------------------+
//
// $Id: write.lyrics3.php,v 1.5 2006/11/20 16:13:39 ah Exp $



class getid3_write_lyrics3 extends getid3_handler_write
{
    public $synched;
    public $random_inhibited;
    
    public $lyrics;
    public $comment;
    public $author;
    public $title;
    public $artist;
    public $album;
    public $images;


    public function read() {
        
        $engine = new getid3;
        $engine->filename = $this->filename;
        $engine->fp = fopen($this->filename, 'rb');
        $engine->include_module('tag.lyrics3');

        $tag = new getid3_lyrics3($engine);
        $tag->Analyze();
        
        if (!isset($engine->info['lyrics3']['tag_offset_start'])) {
            return;
        }
        
        $this->lyrics  = @$engine->info['lyrics3']['raw']['LYR'];
        $this->comment = @$engine->info['lyrics3']['raw']['INF'];
        $this->author  = @$engine->info['lyrics3']['raw']['AUT'];
        $this->title   = @$engine->info['lyrics3']['raw']['ETT'];
        $this->artist  = @$engine->info['lyrics3']['raw']['EAR'];
        $this->album   = @$engine->info['lyrics3']['raw']['EAL'];
        $this->images  = @$engine->info['lyrics3']['raw']['IMG'];
        
        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');
        
        $tag = new getid3_id3v1($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);
        
        // id3v1 tag present
        if (@$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;
    }

    
    protected function generate_tag() {
        
        // define fields
        static $fields = array (
            'lyrics'  => 'LYR', 
            'comment' => 'INF',
            'author'  => 'AUT',
            'title'   => 'ETT',
            'artist'  => 'EAR',
            'album'   => 'EAL',
            'images'  => 'IMG'
        );
        
        // loop thru fields and add to frames
        $frames = '';
        foreach ($fields as $field => $frame_name) {
        
            // field set?
            if ($this->$field) {
                $frames .= $frame_name . str_pad(strlen($this->$field), 5, '0', STR_PAD_LEFT) . $this->$field;
            }
        }
        
        if (!$frames) {
            throw new getid3_exception('Cannot write empty tag, use remove() instead.');
        }

        // header        
        $result  = 'LYRICSBEGIN';
        
        // indicator frame
        $result .= 'IND00003' . ($this->lyrics ? '1' : '0') . ($this->synched ? '1' : '0') . ($this->random_inibited ? '1' : '0'); 
        
        // other frames
        $result .= $frames;
                
        // footer
        $result .= str_pad(strlen($result), 6, '0', STR_PAD_LEFT);
        $result .= 'LYRICS200';
        
        return $result;
    }

    
    public function remove() {
        
        $engine = new getid3;
        $engine->filename = $this->filename;
        $engine->fp = fopen($this->filename, 'rb');
        $engine->include_module('tag.lyrics3');

        $tag = new getid3_lyrics3($engine);
        $tag->Analyze();
        
        if (isset($engine->info['lyrics3']['tag_offset_start']) && isset($engine->info['lyrics3']['tag_offset_end'])) {
            
            if (!$fp = @fopen($this->filename, 'a+b')) {
                throw new getid3_exception('Could not open a+b: ' . $this->filename);
            }

            // get data after tag
            fseek($fp, $engine->info['lyrics3']['tag_offset_end'], SEEK_SET);
            $data_after_lyrics3 = '';
            if (filesize($this->filename) > $engine->info['lyrics3']['tag_offset_end']) {
                $data_after_lyrics3 = fread($fp, filesize($this->filename) - $engine->info['lyrics3']['tag_offset_end']);
            }

            // truncate file before start of tag and seek to end
            ftruncate($fp, $engine->info['lyrics3']['tag_offset_start']);

            // rewrite data after tag
            if (!empty($data_after_lyrics3)) {
                fseek($fp, $engine->info['lyrics3']['tag_offset_start'], SEEK_SET);
                fwrite($fp, $data_after_lyrics3, strlen($data_after_lyrics3));
            }

            fclose($fp);
            clearstatcache();
        }
        
        // success when removing non-existant tag 
        return true;
    }
}

?>