[3318] | 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.lyrics3.php | |
---|
| 18 | // | writing module for lyrics3 2.00 tags | |
---|
| 19 | // | dependencies: module.tag.lyrics3.php. | |
---|
| 20 | // | dependencies: module.tag.id3v1.php | |
---|
| 21 | // +----------------------------------------------------------------------+ |
---|
| 22 | // |
---|
| 23 | // $Id: write.lyrics3.php 3318 2009-05-20 21:54:10Z vdigital $ |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | class getid3_write_lyrics3 extends getid3_handler_write |
---|
| 28 | { |
---|
| 29 | public $synched; |
---|
| 30 | public $random_inhibited; |
---|
| 31 | |
---|
| 32 | public $lyrics; |
---|
| 33 | public $comment; |
---|
| 34 | public $author; |
---|
| 35 | public $title; |
---|
| 36 | public $artist; |
---|
| 37 | public $album; |
---|
| 38 | public $images; |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | public function read() { |
---|
| 42 | |
---|
| 43 | $engine = new getid3; |
---|
| 44 | $engine->filename = $this->filename; |
---|
| 45 | $engine->fp = fopen($this->filename, 'rb'); |
---|
| 46 | $engine->include_module('tag.lyrics3'); |
---|
| 47 | |
---|
| 48 | $tag = new getid3_lyrics3($engine); |
---|
| 49 | $tag->Analyze(); |
---|
| 50 | |
---|
| 51 | if (!isset($engine->info['lyrics3']['tag_offset_start'])) { |
---|
| 52 | return; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | $this->lyrics = @$engine->info['lyrics3']['raw']['LYR']; |
---|
| 56 | $this->comment = @$engine->info['lyrics3']['raw']['INF']; |
---|
| 57 | $this->author = @$engine->info['lyrics3']['raw']['AUT']; |
---|
| 58 | $this->title = @$engine->info['lyrics3']['raw']['ETT']; |
---|
| 59 | $this->artist = @$engine->info['lyrics3']['raw']['EAR']; |
---|
| 60 | $this->album = @$engine->info['lyrics3']['raw']['EAL']; |
---|
| 61 | $this->images = @$engine->info['lyrics3']['raw']['IMG']; |
---|
| 62 | |
---|
| 63 | return true; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | public function write() { |
---|
| 68 | |
---|
| 69 | // remove existing apetag |
---|
| 70 | $this->remove(); |
---|
| 71 | |
---|
| 72 | $engine = new getid3; |
---|
| 73 | $engine->filename = $this->filename; |
---|
| 74 | $engine->fp = fopen($this->filename, 'rb'); |
---|
| 75 | $engine->include_module('tag.id3v1'); |
---|
| 76 | |
---|
| 77 | $tag = new getid3_id3v1($engine); |
---|
| 78 | $tag->Analyze(); |
---|
| 79 | |
---|
| 80 | $apetag = $this->generate_tag(); |
---|
| 81 | |
---|
| 82 | if (!$fp = @fopen($this->filename, 'a+b')) { |
---|
| 83 | throw new getid3_exception('Could not open a+b: ' . $this->filename); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | // init: audio ends at eof |
---|
| 87 | $post_audio_offset = filesize($this->filename); |
---|
| 88 | |
---|
| 89 | // id3v1 tag present |
---|
| 90 | if (@$engine->info['id3v1']['tag_offset_start']) { |
---|
| 91 | |
---|
| 92 | // audio ends before id3v1 tag |
---|
| 93 | $post_audio_offset = $engine->info['id3v1']['tag_offset_start']; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | // seek to end of audio data |
---|
| 97 | fseek($fp, $post_audio_offset, SEEK_SET); |
---|
| 98 | |
---|
| 99 | // save data after audio data |
---|
| 100 | $post_audio_data = ''; |
---|
| 101 | if (filesize($this->filename) > $post_audio_offset) { |
---|
| 102 | $post_audio_data = fread($fp, filesize($this->filename) - $post_audio_offset); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | // truncate file before start of new apetag |
---|
| 106 | fseek($fp, $post_audio_offset, SEEK_SET); |
---|
| 107 | ftruncate($fp, ftell($fp)); |
---|
| 108 | |
---|
| 109 | // write new apetag |
---|
| 110 | fwrite($fp, $apetag, strlen($apetag)); |
---|
| 111 | |
---|
| 112 | // rewrite data after audio |
---|
| 113 | if (!empty($post_audio_data)) { |
---|
| 114 | fwrite($fp, $post_audio_data, strlen($post_audio_data)); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | fclose($fp); |
---|
| 118 | clearstatcache(); |
---|
| 119 | |
---|
| 120 | return true; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | protected function generate_tag() { |
---|
| 125 | |
---|
| 126 | // define fields |
---|
| 127 | static $fields = array ( |
---|
| 128 | 'lyrics' => 'LYR', |
---|
| 129 | 'comment' => 'INF', |
---|
| 130 | 'author' => 'AUT', |
---|
| 131 | 'title' => 'ETT', |
---|
| 132 | 'artist' => 'EAR', |
---|
| 133 | 'album' => 'EAL', |
---|
| 134 | 'images' => 'IMG' |
---|
| 135 | ); |
---|
| 136 | |
---|
| 137 | // loop thru fields and add to frames |
---|
| 138 | $frames = ''; |
---|
| 139 | foreach ($fields as $field => $frame_name) { |
---|
| 140 | |
---|
| 141 | // field set? |
---|
| 142 | if ($this->$field) { |
---|
| 143 | $frames .= $frame_name . str_pad(strlen($this->$field), 5, '0', STR_PAD_LEFT) . $this->$field; |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | if (!$frames) { |
---|
| 148 | throw new getid3_exception('Cannot write empty tag, use remove() instead.'); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | // header |
---|
| 152 | $result = 'LYRICSBEGIN'; |
---|
| 153 | |
---|
| 154 | // indicator frame |
---|
| 155 | $result .= 'IND00003' . ($this->lyrics ? '1' : '0') . ($this->synched ? '1' : '0') . ($this->random_inibited ? '1' : '0'); |
---|
| 156 | |
---|
| 157 | // other frames |
---|
| 158 | $result .= $frames; |
---|
| 159 | |
---|
| 160 | // footer |
---|
| 161 | $result .= str_pad(strlen($result), 6, '0', STR_PAD_LEFT); |
---|
| 162 | $result .= 'LYRICS200'; |
---|
| 163 | |
---|
| 164 | return $result; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | public function remove() { |
---|
| 169 | |
---|
| 170 | $engine = new getid3; |
---|
| 171 | $engine->filename = $this->filename; |
---|
| 172 | $engine->fp = fopen($this->filename, 'rb'); |
---|
| 173 | $engine->include_module('tag.lyrics3'); |
---|
| 174 | |
---|
| 175 | $tag = new getid3_lyrics3($engine); |
---|
| 176 | $tag->Analyze(); |
---|
| 177 | |
---|
| 178 | if (isset($engine->info['lyrics3']['tag_offset_start']) && isset($engine->info['lyrics3']['tag_offset_end'])) { |
---|
| 179 | |
---|
| 180 | if (!$fp = @fopen($this->filename, 'a+b')) { |
---|
| 181 | throw new getid3_exception('Could not open a+b: ' . $this->filename); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | // get data after tag |
---|
| 185 | fseek($fp, $engine->info['lyrics3']['tag_offset_end'], SEEK_SET); |
---|
| 186 | $data_after_lyrics3 = ''; |
---|
| 187 | if (filesize($this->filename) > $engine->info['lyrics3']['tag_offset_end']) { |
---|
| 188 | $data_after_lyrics3 = fread($fp, filesize($this->filename) - $engine->info['lyrics3']['tag_offset_end']); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | // truncate file before start of tag and seek to end |
---|
| 192 | ftruncate($fp, $engine->info['lyrics3']['tag_offset_start']); |
---|
| 193 | |
---|
| 194 | // rewrite data after tag |
---|
| 195 | if (!empty($data_after_lyrics3)) { |
---|
| 196 | fseek($fp, $engine->info['lyrics3']['tag_offset_start'], SEEK_SET); |
---|
| 197 | fwrite($fp, $data_after_lyrics3, strlen($data_after_lyrics3)); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | fclose($fp); |
---|
| 201 | clearstatcache(); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | // success when removing non-existant tag |
---|
| 205 | return true; |
---|
| 206 | } |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | ?> |
---|