- Timestamp:
- Jul 7, 2009, 10:27:37 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/charlies_content/getid3/getid3/write.lyrics3.php
r3318 r3544 1 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$ 2 ///////////////////////////////////////////////////////////////// 3 /// getID3() by James Heinrich <info@getid3.org> // 4 // available at http://getid3.sourceforge.net // 5 // or http://www.getid3.org // 6 ///////////////////////////////////////////////////////////////// 7 // See readme.txt for more details // 8 ///////////////////////////////////////////////////////////////// 9 // // 10 // write.lyrics3.php // 11 // module for writing Lyrics3 tags // 12 // dependencies: module.tag.lyrics3.php // 13 // /// 14 ///////////////////////////////////////////////////////////////// 15 16 17 class getid3_write_lyrics3 18 { 19 var $filename; 20 var $tag_data; 21 //var $lyrics3_version = 2; // 1 or 2 22 var $warnings = array(); // any non-critical errors will be stored here 23 var $errors = array(); // any critical errors will be stored here 24 25 function getid3_write_lyrics3() { 26 return true; 27 } 28 29 function WriteLyrics3() { 30 $this->errors[] = 'WriteLyrics3() not yet functional - cannot write Lyrics3'; 31 return false; 32 } 33 34 function DeleteLyrics3() { 35 // Initialize getID3 engine 36 $getID3 = new getID3; 37 $ThisFileInfo = $getID3->analyze($this->filename); 38 if (isset($ThisFileInfo['lyrics3']['tag_offset_start']) && isset($ThisFileInfo['lyrics3']['tag_offset_end'])) { 39 if ($fp = @fopen($this->filename, 'a+b')) { 40 41 flock($fp, LOCK_EX); 42 $oldignoreuserabort = ignore_user_abort(true); 43 44 fseek($fp, $ThisFileInfo['lyrics3']['tag_offset_end'], SEEK_SET); 45 $DataAfterLyrics3 = ''; 46 if ($ThisFileInfo['filesize'] > $ThisFileInfo['lyrics3']['tag_offset_end']) { 47 $DataAfterLyrics3 = fread($fp, $ThisFileInfo['filesize'] - $ThisFileInfo['lyrics3']['tag_offset_end']); 48 } 49 50 ftruncate($fp, $ThisFileInfo['lyrics3']['tag_offset_start']); 51 52 if (!empty($DataAfterLyrics3)) { 53 fseek($fp, $ThisFileInfo['lyrics3']['tag_offset_start'], SEEK_SET); 54 fwrite($fp, $DataAfterLyrics3, strlen($DataAfterLyrics3)); 55 } 56 57 flock($fp, LOCK_UN); 58 fclose($fp); 59 ignore_user_abort($oldignoreuserabort); 60 61 return true; 62 63 } else { 64 65 $this->errors[] = 'Cannot open "'.$this->filename.'" in "a+b" mode'; 66 return false; 67 68 } 69 } 70 // no Lyrics3 present 71 return true; 72 } 24 73 25 74 26 75 27 class getid3_write_lyrics3 extends getid3_handler_write28 {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 apetag70 $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 eof87 $post_audio_offset = filesize($this->filename);88 89 // id3v1 tag present90 if (@$engine->info['id3v1']['tag_offset_start']) {91 92 // audio ends before id3v1 tag93 $post_audio_offset = $engine->info['id3v1']['tag_offset_start'];94 }95 96 // seek to end of audio data97 fseek($fp, $post_audio_offset, SEEK_SET);98 99 // save data after audio data100 $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 apetag106 fseek($fp, $post_audio_offset, SEEK_SET);107 ftruncate($fp, ftell($fp));108 109 // write new apetag110 fwrite($fp, $apetag, strlen($apetag));111 112 // rewrite data after audio113 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 fields127 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 frames138 $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 // header152 $result = 'LYRICSBEGIN';153 154 // indicator frame155 $result .= 'IND00003' . ($this->lyrics ? '1' : '0') . ($this->synched ? '1' : '0') . ($this->random_inibited ? '1' : '0');156 157 // other frames158 $result .= $frames;159 160 // footer161 $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 tag185 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 end192 ftruncate($fp, $engine->info['lyrics3']['tag_offset_start']);193 194 // rewrite data after tag195 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 tag205 return true;206 }207 76 } 208 77
Note: See TracChangeset
for help on using the changeset viewer.