| // | Allan Hansen | // +----------------------------------------------------------------------+ // | write.id3v1.php | // | writing module for id3v1 tags | // | dependencies: module.tag.id3v1.php. | // +----------------------------------------------------------------------+ // // $Id: write.id3v1.php 3318 2009-05-20 21:54:10Z vdigital $ 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; } } ?>