3 /////////////////////////////////////////////////////////////////
4 /// getID3() by James Heinrich <info@getid3.org> //
5 // available at https://github.com/JamesHeinrich/getID3 //
6 // or https://www.getid3.org //
7 // or http://getid3.sourceforge.net //
8 // see readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
12 // module for writing ID3v1 tags //
13 // dependencies: module.tag.id3v1.php //
15 /////////////////////////////////////////////////////////////////
17 getid3_lib
::IncludeDependency(GETID3_INCLUDEPATH
.'module.tag.id3v1.php', __FILE__
, true);
19 class getid3_write_id3v1
37 * Any non-critical errors will be stored here.
41 public $warnings = array();
44 * Any critical errors will be stored here.
48 public $errors = array();
50 public function __construct() {
56 public function WriteID3v1() {
57 // File MUST be writeable - CHMOD(646) at least
58 if (!empty($this->filename
) && is_readable($this->filename
) && getID3
::is_writable($this->filename
) && is_file($this->filename
)) {
59 $this->setRealFileSize();
60 if (($this->filesize
<= 0) ||
!getid3_lib
::intValueSupported($this->filesize
)) {
61 $this->errors
[] = 'Unable to WriteID3v1('.$this->filename
.') because filesize ('.$this->filesize
.') is larger than '.round(PHP_INT_MAX
/ 1073741824).'GB';
64 if ($fp_source = fopen($this->filename
, 'r+b')) {
65 fseek($fp_source, -128, SEEK_END
);
66 if (fread($fp_source, 3) == 'TAG') {
67 fseek($fp_source, -128, SEEK_END
); // overwrite existing ID3v1 tag
69 fseek($fp_source, 0, SEEK_END
); // append new ID3v1 tag
71 $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'] : '')));
73 $new_id3v1_tag_data = getid3_id3v1
::GenerateID3v1Tag(
74 (isset($this->tag_data
['title'] ) ?
$this->tag_data
['title'] : ''),
75 (isset($this->tag_data
['artist'] ) ?
$this->tag_data
['artist'] : ''),
76 (isset($this->tag_data
['album'] ) ?
$this->tag_data
['album'] : ''),
77 (isset($this->tag_data
['year'] ) ?
$this->tag_data
['year'] : ''),
78 (isset($this->tag_data
['genreid']) ?
$this->tag_data
['genreid'] : ''),
79 (isset($this->tag_data
['comment']) ?
$this->tag_data
['comment'] : ''),
80 (isset($this->tag_data
['track'] ) ?
$this->tag_data
['track'] : ''));
81 fwrite($fp_source, $new_id3v1_tag_data, 128);
86 $this->errors
[] = 'Could not fopen('.$this->filename
.', "r+b")';
90 $this->errors
[] = 'File is not writeable: '.$this->filename
;
97 public function FixID3v1Padding() {
98 // ID3v1 data is supposed to be padded with NULL characters, but some taggers incorrectly use spaces
99 // This function rewrites the ID3v1 tag with correct padding
101 // Initialize getID3 engine
102 $getID3 = new getID3
;
103 $getID3->option_tag_id3v2
= false;
104 $getID3->option_tag_apetag
= false;
105 $getID3->option_tags_html
= false;
106 $getID3->option_extra_info
= false;
107 $getID3->option_tag_id3v1
= true;
108 $ThisFileInfo = $getID3->analyze($this->filename
);
109 if (isset($ThisFileInfo['tags']['id3v1'])) {
110 $id3v1data = array();
111 foreach ($ThisFileInfo['tags']['id3v1'] as $key => $value) {
112 $id3v1data[$key] = implode(',', $value);
114 $this->tag_data
= $id3v1data;
115 return $this->WriteID3v1();
123 public function RemoveID3v1() {
124 // File MUST be writeable - CHMOD(646) at least
125 if (!empty($this->filename
) && is_readable($this->filename
) && getID3
::is_writable($this->filename
) && is_file($this->filename
)) {
126 $this->setRealFileSize();
127 if (($this->filesize
<= 0) ||
!getid3_lib
::intValueSupported($this->filesize
)) {
128 $this->errors
[] = 'Unable to RemoveID3v1('.$this->filename
.') because filesize ('.$this->filesize
.') is larger than '.round(PHP_INT_MAX
/ 1073741824).'GB';
131 if ($fp_source = fopen($this->filename
, 'r+b')) {
133 fseek($fp_source, -128, SEEK_END
);
134 if (fread($fp_source, 3) == 'TAG') {
135 ftruncate($fp_source, $this->filesize
- 128);
137 // no ID3v1 tag to begin with - do nothing
143 $this->errors
[] = 'Could not fopen('.$this->filename
.', "r+b")';
146 $this->errors
[] = $this->filename
.' is not writeable';
154 public function setRealFileSize() {
155 if (PHP_INT_MAX
> 2147483647) {
156 $this->filesize
= filesize($this->filename
);
159 // 32-bit PHP will not return correct values for filesize() if file is >=2GB
160 // but getID3->analyze() has workarounds to get actual filesize
161 $getID3 = new getID3
;
162 $getID3->option_tag_id3v1
= false;
163 $getID3->option_tag_id3v2
= false;
164 $getID3->option_tag_apetag
= false;
165 $getID3->option_tags_html
= false;
166 $getID3->option_extra_info
= false;
167 $ThisFileInfo = $getID3->analyze($this->filename
);
168 $this->filesize
= $ThisFileInfo['filesize'];