ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Metaflac.php
Go to the documentation of this file.
1 <?php
2 
3 namespace GetId3\Write;
4 
6 
9 // available at http://getid3.sourceforge.net //
10 // or http://www.getid3.org //
12 // See readme.txt for more details //
14 // //
15 // write.metaflac.php //
16 // module for writing metaflac tags //
17 // dependencies: /helperapps/metaflac.exe //
18 // ///
20 
29 class Metaflac
30 {
31 
32  public $filename;
33  public $tag_data;
38  public $warnings = array(); // any non-critical errors will be stored here
43  public $errors = array(); // any critical errors will be stored here
44 
49  public function __construct()
50  {
51  return true;
52  }
53 
58  public function WriteMetaFLAC()
59  {
60  if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
61  $this->errors[] = 'PHP running in Safe Mode (backtick operator not available) - cannot call metaflac, tags not written';
62 
63  return false;
64  }
65 
66  // Create file with new comments
67  $tempcommentsfilename = tempnam(GetId3Core::getTempDir(), 'getID3');
68  if (is_writable($tempcommentsfilename) && is_file($tempcommentsfilename) && ($fpcomments = fopen($tempcommentsfilename, 'wb'))) {
69  foreach ($this->tag_data as $key => $value) {
70  foreach ($value as $commentdata) {
71  fwrite($fpcomments, $this->CleanmetaflacName($key).'='.$commentdata."\n");
72  }
73  }
74  fclose($fpcomments);
75 
76  } else {
77  $this->errors[] = 'failed to open temporary tags file, tags not written - fopen("'.$tempcommentsfilename.'", "wb")';
78 
79  return false;
80  }
81 
82  $oldignoreuserabort = ignore_user_abort(true);
84 
85  if (file_exists(GetId3Core::getHelperAppsDir().'metaflac.exe')) {
86  //$commandline = '"'.GetId3Core::getHelperAppsDir().'metaflac.exe" --no-utf8-convert --remove-all-tags --import-tags-from="'.$tempcommentsfilename.'" "'.str_replace('/', '\\', $this->filename).'"';
87  // metaflac works fine if you copy-paste the above commandline into a command prompt,
88  // but refuses to work with `backtick` if there are "doublequotes" present around BOTH
89  // the metaflac pathname and the target filename. For whatever reason...??
90  // The solution is simply ensure that the metaflac pathname has no spaces,
91  // and therefore does not need to be quoted
92 
93  // On top of that, if error messages are not always captured properly under Windows
94  // To at least see if there was a problem, compare file modification timestamps before and after writing
95  clearstatcache();
96  $timestampbeforewriting = filemtime($this->filename);
97 
98  $commandline = GetId3Core::getHelperAppsDir().'metaflac.exe --no-utf8-convert --remove-all-tags --import-tags-from='.escapeshellarg($tempcommentsfilename).' '.escapeshellarg($this->filename).' 2>&1';
99  $metaflacError = `$commandline`;
100 
101  if (empty($metaflacError)) {
102  clearstatcache();
103  if ($timestampbeforewriting == filemtime($this->filename)) {
104  $metaflacError = 'File modification timestamp has not changed - it looks like the tags were not written';
105  }
106  }
107  } else {
108  $metaflacError = 'metaflac.exe not found in '.GetId3Core::getHelperAppsDir();
109  }
110 
111  } else {
112 
113  // It's simpler on *nix
114  $commandline = 'metaflac --no-utf8-convert --remove-all-tags --import-tags-from='.escapeshellarg($tempcommentsfilename).' '.escapeshellarg($this->filename).' 2>&1';
115  $metaflacError = `$commandline`;
116 
117  }
118 
119  // Remove temporary comments file
120  unlink($tempcommentsfilename);
121  ignore_user_abort($oldignoreuserabort);
122 
123  if (!empty($metaflacError)) {
124 
125  $this->errors[] = 'System call to metaflac failed with this message returned: '."\n\n".$metaflacError;
126 
127  return false;
128 
129  }
130 
131  return true;
132  }
133 
138  public function DeleteMetaFLAC()
139  {
140  if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
141  $this->errors[] = 'PHP running in Safe Mode (backtick operator not available) - cannot call metaflac, tags not deleted';
142 
143  return false;
144  }
145 
146  $oldignoreuserabort = ignore_user_abort(true);
148 
149  if (file_exists(GetId3Core::getHelperAppsDir().'metaflac.exe')) {
150  // To at least see if there was a problem, compare file modification timestamps before and after writing
151  clearstatcache();
152  $timestampbeforewriting = filemtime($this->filename);
153 
154  $commandline = GetId3Core::getHelperAppsDir().'metaflac.exe --remove-all-tags "'.$this->filename.'" 2>&1';
155  $metaflacError = `$commandline`;
156 
157  if (empty($metaflacError)) {
158  clearstatcache();
159  if ($timestampbeforewriting == filemtime($this->filename)) {
160  $metaflacError = 'File modification timestamp has not changed - it looks like the tags were not deleted';
161  }
162  }
163  } else {
164  $metaflacError = 'metaflac.exe not found in '.GetId3Core::getHelperAppsDir();
165  }
166 
167  } else {
168 
169  // It's simpler on *nix
170  $commandline = 'metaflac --remove-all-tags "'.$this->filename.'" 2>&1';
171  $metaflacError = `$commandline`;
172 
173  }
174 
175  ignore_user_abort($oldignoreuserabort);
176 
177  if (!empty($metaflacError)) {
178  $this->errors[] = 'System call to metaflac failed with this message returned: '."\n\n".$metaflacError;
179 
180  return false;
181  }
182 
183  return true;
184  }
185 
191  public function CleanmetaflacName($originalcommentname)
192  {
193  // A case-insensitive field name that may consist of ASCII 0x20 through 0x7D, 0x3D ('=') excluded.
194  // ASCII 0x41 through 0x5A inclusive (A-Z) is to be considered equivalent to ASCII 0x61 through
195  // 0x7A inclusive (a-z).
196 
197  // replace invalid chars with a space, return uppercase text
198  // Thanks Chris Bolt <chris-getid3Øbolt*cx> for improving this function
199  // note: *reg_replace() replaces nulls with empty string (not space)
200  return strtoupper(preg_replace('#[^ -<>-}]#', ' ', str_replace("\x00", ' ', $originalcommentname)));
201 
202  }
203 
204 }
GetId3() by James Heinrich info@getid3.org //.
Definition: Metaflac.php:29
static environmentIsWindows()
Create styles array
The data for the language used.
CleanmetaflacName($originalcommentname)
Definition: Metaflac.php:191
static getHelperAppsDir()
Definition: GetId3Core.php:186