ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Id3v1.php
Go to the documentation of this file.
1 <?php
2 
3 namespace GetId3\Write;
4 
8 
11 // available at http://getid3.sourceforge.net //
12 // or http://www.getid3.org //
14 // See readme.txt for more details //
16 // //
17 // write.id3v1.php //
18 // module for writing ID3v1 tags //
19 // dependencies: module.tag.id3v1.php //
20 // ///
22 
31 class Id3v1
32 {
33  public $filename;
34  public $filesize;
35  public $tag_data;
40  public $warnings = array(); // any non-critical errors will be stored here
45  public $errors = array(); // any critical errors will be stored here
46 
51  public function __construct()
52  {
53  return true;
54  }
55 
60  public function WriteID3v1()
61  {
62  // File MUST be writeable - CHMOD(646) at least
63  if (!empty($this->filename) && is_readable($this->filename) && is_writable($this->filename) && is_file($this->filename)) {
64  $this->setRealFileSize();
65  if (($this->filesize <= 0) || !Helper::intValueSupported($this->filesize)) {
66  $this->errors[] = 'Unable to WriteID3v1('.$this->filename.') because filesize ('.$this->filesize.') is larger than '.round(PHP_INT_MAX / 1073741824).'GB';
67 
68  return false;
69  }
70  if ($fp_source = fopen($this->filename, 'r+b')) {
71  fseek($fp_source, -128, SEEK_END);
72  if (fread($fp_source, 3) == 'TAG') {
73  fseek($fp_source, -128, SEEK_END); // overwrite existing ID3v1 tag
74  } else {
75  fseek($fp_source, 0, SEEK_END); // append new ID3v1 tag
76  }
77  $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'] : '')));
78 
79  $new_id3v1_tag_data = Tag\Id3v1::GenerateID3v1Tag(
80  (isset($this->tag_data['title'] ) ? $this->tag_data['title'] : ''),
81  (isset($this->tag_data['artist'] ) ? $this->tag_data['artist'] : ''),
82  (isset($this->tag_data['album'] ) ? $this->tag_data['album'] : ''),
83  (isset($this->tag_data['year'] ) ? $this->tag_data['year'] : ''),
84  (isset($this->tag_data['genreid']) ? $this->tag_data['genreid'] : ''),
85  (isset($this->tag_data['comment']) ? $this->tag_data['comment'] : ''),
86  (isset($this->tag_data['track'] ) ? $this->tag_data['track'] : ''));
87  fwrite($fp_source, $new_id3v1_tag_data, 128);
88  fclose($fp_source);
89 
90  return true;
91 
92  } else {
93  $this->errors[] = 'Could not fopen('.$this->filename.', "r+b")';
94 
95  return false;
96  }
97  }
98  $this->errors[] = 'File is not writeable: '.$this->filename;
99 
100  return false;
101  }
102 
107  public function FixID3v1Padding()
108  {
109  // ID3v1 data is supposed to be padded with NULL characters, but some taggers incorrectly use spaces
110  // This function rewrites the ID3v1 tag with correct padding
111 
112  // Initialize GetId3 engine
113  $getID3 = new GetId3Core();
114  $getID3->option_tag_id3v2 = false;
115  $getID3->option_tag_apetag = false;
116  $getID3->option_tags_html = false;
117  $getID3->option_extra_info = false;
118  $getID3->option_tag_id3v1 = true;
119  $ThisFileInfo = $getID3->analyze($this->filename);
120  if (isset($ThisFileInfo['tags']['id3v1'])) {
121  foreach ($ThisFileInfo['tags']['id3v1'] as $key => $value) {
122  $id3v1data[$key] = implode(',', $value);
123  }
124  $this->tag_data = $id3v1data;
125 
126  return $this->WriteID3v1();
127  }
128 
129  return false;
130  }
131 
132  public function RemoveID3v1()
133  {
134  // File MUST be writeable - CHMOD(646) at least
135  if (!empty($this->filename) && is_readable($this->filename) && is_writable($this->filename) && is_file($this->filename)) {
136  $this->setRealFileSize();
137  if (($this->filesize <= 0) || !Helper::intValueSupported($this->filesize)) {
138  $this->errors[] = 'Unable to RemoveID3v1('.$this->filename.') because filesize ('.$this->filesize.') is larger than '.round(PHP_INT_MAX / 1073741824).'GB';
139 
140  return false;
141  }
142  if ($fp_source = fopen($this->filename, 'r+b')) {
143 
144  fseek($fp_source, -128, SEEK_END);
145  if (fread($fp_source, 3) == 'TAG') {
146  ftruncate($fp_source, $this->filesize - 128);
147  } else {
148  // no ID3v1 tag to begin with - do nothing
149  }
150  fclose($fp_source);
151 
152  return true;
153 
154  } else {
155  $this->errors[] = 'Could not fopen('.$this->filename.', "r+b")';
156  }
157  } else {
158  $this->errors[] = $this->filename.' is not writeable';
159  }
160 
161  return false;
162  }
163 
168  public function setRealFileSize()
169  {
170  if (PHP_INT_MAX > 2147483647) {
171  $this->filesize = filesize($this->filename);
172 
173  return true;
174  }
175  // 32-bit PHP will not return correct values for filesize() if file is >=2GB
176  // but GetId3->analyze() has workarounds to get actual filesize
177  $getID3 = new GetId3Core();
178  $getID3->option_tag_id3v1 = false;
179  $getID3->option_tag_id3v2 = false;
180  $getID3->option_tag_apetag = false;
181  $getID3->option_tags_html = false;
182  $getID3->option_extra_info = false;
183  $ThisFileInfo = $getID3->analyze($this->filename);
184  $this->filesize = $ThisFileInfo['filesize'];
185 
186  return true;
187  }
188 
189 }
GetId3() by James Heinrich info@getid3.org //.
Definition: Id3v1.php:29
fseek($bytes, $whence=SEEK_SET)
GetId3() by James Heinrich info@getid3.org //.
Definition: GetId3Core.php:25
Create styles array
The data for the language used.
$errors
static intValueSupported($num)
null $hasINT64
Definition: Helper.php:130