[3318] | 1 | <?php |
---|
[3544] | 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.id3v1.php // |
---|
| 11 | // module for writing ID3v1 tags // |
---|
| 12 | // dependencies: module.tag.id3v1.php // |
---|
| 13 | // /// |
---|
| 14 | ///////////////////////////////////////////////////////////////// |
---|
[3318] | 15 | |
---|
[3544] | 16 | getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.id3v1.php', __FILE__, true); |
---|
[3318] | 17 | |
---|
[3544] | 18 | class getid3_write_id3v1 |
---|
[3318] | 19 | { |
---|
[3544] | 20 | var $filename; |
---|
| 21 | var $tag_data; |
---|
| 22 | var $warnings = array(); // any non-critical errors will be stored here |
---|
| 23 | var $errors = array(); // any critical errors will be stored here |
---|
[3318] | 24 | |
---|
[3544] | 25 | function getid3_write_id3v1() { |
---|
| 26 | return true; |
---|
| 27 | } |
---|
[3318] | 28 | |
---|
[3544] | 29 | function WriteID3v1() { |
---|
| 30 | if ((filesize($this->filename) >= (pow(2, 31) - 128)) || (filesize($this->filename) < 0)) { |
---|
| 31 | $this->errors[] = 'Unable to write ID3v1 because file is larger than 2GB'; |
---|
| 32 | return false; |
---|
| 33 | } |
---|
[3318] | 34 | |
---|
[3544] | 35 | // File MUST be writeable - CHMOD(646) at least |
---|
| 36 | if (is_writeable($this->filename)) { |
---|
| 37 | if ($fp_source = @fopen($this->filename, 'r+b')) { |
---|
[3318] | 38 | |
---|
[3544] | 39 | fseek($fp_source, -128, SEEK_END); |
---|
| 40 | if (fread($fp_source, 3) == 'TAG') { |
---|
| 41 | fseek($fp_source, -128, SEEK_END); // overwrite existing ID3v1 tag |
---|
| 42 | } else { |
---|
| 43 | fseek($fp_source, 0, SEEK_END); // append new ID3v1 tag |
---|
| 44 | } |
---|
| 45 | $this->tag_data['track'] = (isset($this->tag_data['track']) ? $this->tag_data['track'] : (isset($this->tag_data['track_number']) ? $this->tag_data['track_number'] : (isset($this->tag_data['tracknumber']) ? $this->tag_data['tracknumber'] : ''))); |
---|
[3318] | 46 | |
---|
[3544] | 47 | $new_id3v1_tag_data = getid3_id3v1::GenerateID3v1Tag( |
---|
| 48 | @$this->tag_data['title'], |
---|
| 49 | @$this->tag_data['artist'], |
---|
| 50 | @$this->tag_data['album'], |
---|
| 51 | @$this->tag_data['year'], |
---|
| 52 | @$this->tag_data['genreid'], |
---|
| 53 | @$this->tag_data['comment'], |
---|
| 54 | @$this->tag_data['track']); |
---|
| 55 | fwrite($fp_source, $new_id3v1_tag_data, 128); |
---|
| 56 | fclose($fp_source); |
---|
| 57 | return true; |
---|
[3318] | 58 | |
---|
[3544] | 59 | } else { |
---|
| 60 | $this->errors[] = 'Could not open '.$this->filename.' mode "r+b"'; |
---|
| 61 | return false; |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | $this->errors[] = 'File is not writeable: '.$this->filename; |
---|
| 65 | return false; |
---|
| 66 | } |
---|
[3318] | 67 | |
---|
[3544] | 68 | function FixID3v1Padding() { |
---|
| 69 | // ID3v1 data is supposed to be padded with NULL characters, but some taggers incorrectly use spaces |
---|
| 70 | // This function rewrites the ID3v1 tag with correct padding |
---|
| 71 | |
---|
| 72 | // Initialize getID3 engine |
---|
| 73 | $getID3 = new getID3; |
---|
| 74 | $ThisFileInfo = $getID3->analyze($this->filename); |
---|
| 75 | if ($ThisFileInfo['filesize'] >= (pow(2, 31) - 128)) { |
---|
| 76 | // cannot write tags on files > 2GB |
---|
| 77 | return false; |
---|
| 78 | } |
---|
| 79 | if (isset($ThisFileInfo['tags']['id3v1'])) { |
---|
| 80 | foreach ($ThisFileInfo['tags']['id3v1'] as $key => $value) { |
---|
| 81 | $id3v1data[$key] = implode(',', $value); |
---|
| 82 | } |
---|
| 83 | $this->tag_data = $id3v1data; |
---|
| 84 | return $this->WriteID3v1(); |
---|
| 85 | } |
---|
| 86 | return false; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | function RemoveID3v1() { |
---|
| 90 | if ($ThisFileInfo['filesize'] >= pow(2, 31)) { |
---|
| 91 | $this->errors[] = 'Unable to write ID3v1 because file is larger than 2GB'; |
---|
| 92 | return false; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | // File MUST be writeable - CHMOD(646) at least |
---|
| 96 | if (is_writeable($this->filename)) { |
---|
| 97 | if ($fp_source = @fopen($this->filename, 'r+b')) { |
---|
| 98 | |
---|
| 99 | fseek($fp_source, -128, SEEK_END); |
---|
| 100 | if (fread($fp_source, 3) == 'TAG') { |
---|
| 101 | ftruncate($fp_source, filesize($this->filename) - 128); |
---|
| 102 | } else { |
---|
| 103 | // no ID3v1 tag to begin with - do nothing |
---|
| 104 | } |
---|
| 105 | fclose($fp_source); |
---|
| 106 | return true; |
---|
| 107 | |
---|
| 108 | } else { |
---|
| 109 | $this->errors[] = 'Could not open '.$this->filename.' mode "r+b"'; |
---|
| 110 | } |
---|
| 111 | } else { |
---|
| 112 | $this->errors[] = $this->filename.' is not writeable'; |
---|
| 113 | } |
---|
| 114 | return false; |
---|
| 115 | } |
---|
| 116 | |
---|
[3318] | 117 | } |
---|
| 118 | |
---|
| 119 | ?> |
---|