2 /////////////////////////////////////////////////////////////////
3 /// getID3() by James Heinrich <info@getid3.org> //
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
6 // also https://github.com/JamesHeinrich/getID3 //
7 /////////////////////////////////////////////////////////////////
8 // See readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
11 // write.metaflac.php //
12 // module for writing metaflac tags //
13 // dependencies: /helperapps/metaflac.exe //
15 /////////////////////////////////////////////////////////////////
18 class getid3_write_metaflac
23 public $warnings = array(); // any non-critical errors will be stored here
24 public $errors = array(); // any critical errors will be stored here
26 public function __construct() {
30 public function WriteMetaFLAC() {
32 if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
33 $this->errors
[] = 'PHP running in Safe Mode (backtick operator not available) - cannot call metaflac, tags not written';
37 // Create file with new comments
38 $tempcommentsfilename = tempnam(GETID3_TEMP_DIR
, 'getID3');
39 if (is_writable($tempcommentsfilename) && is_file($tempcommentsfilename) && ($fpcomments = fopen($tempcommentsfilename, 'wb'))) {
40 foreach ($this->tag_data
as $key => $value) {
41 foreach ($value as $commentdata) {
42 fwrite($fpcomments, $this->CleanmetaflacName($key).'='.$commentdata."\n");
48 $this->errors
[] = 'failed to open temporary tags file, tags not written - fopen("'.$tempcommentsfilename.'", "wb")';
52 $oldignoreuserabort = ignore_user_abort(true);
53 if (GETID3_OS_ISWINDOWS
) {
55 if (file_exists(GETID3_HELPERAPPSDIR
.'metaflac.exe')) {
56 //$commandline = '"'.GETID3_HELPERAPPSDIR.'metaflac.exe" --no-utf8-convert --remove-all-tags --import-tags-from="'.$tempcommentsfilename.'" "'.str_replace('/', '\\', $this->filename).'"';
57 // metaflac works fine if you copy-paste the above commandline into a command prompt,
58 // but refuses to work with `backtick` if there are "doublequotes" present around BOTH
59 // the metaflac pathname and the target filename. For whatever reason...??
60 // The solution is simply ensure that the metaflac pathname has no spaces,
61 // and therefore does not need to be quoted
63 // On top of that, if error messages are not always captured properly under Windows
64 // To at least see if there was a problem, compare file modification timestamps before and after writing
66 $timestampbeforewriting = filemtime($this->filename
);
68 $commandline = GETID3_HELPERAPPSDIR
.'metaflac.exe --no-utf8-convert --remove-all-tags --import-tags-from='.escapeshellarg($tempcommentsfilename).' '.escapeshellarg($this->filename
).' 2>&1';
69 $metaflacError = `
$commandline`
;
71 if (empty($metaflacError)) {
73 if ($timestampbeforewriting == filemtime($this->filename
)) {
74 $metaflacError = 'File modification timestamp has not changed - it looks like the tags were not written';
78 $metaflacError = 'metaflac.exe not found in '.GETID3_HELPERAPPSDIR
;
83 // It's simpler on *nix
84 $commandline = 'metaflac --no-utf8-convert --remove-all-tags --import-tags-from='.escapeshellarg($tempcommentsfilename).' '.escapeshellarg($this->filename
).' 2>&1';
85 $metaflacError = `
$commandline`
;
89 // Remove temporary comments file
90 unlink($tempcommentsfilename);
91 ignore_user_abort($oldignoreuserabort);
93 if (!empty($metaflacError)) {
95 $this->errors
[] = 'System call to metaflac failed with this message returned: '."\n\n".$metaflacError;
104 public function DeleteMetaFLAC() {
106 if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
107 $this->errors
[] = 'PHP running in Safe Mode (backtick operator not available) - cannot call metaflac, tags not deleted';
111 $oldignoreuserabort = ignore_user_abort(true);
112 if (GETID3_OS_ISWINDOWS
) {
114 if (file_exists(GETID3_HELPERAPPSDIR
.'metaflac.exe')) {
115 // To at least see if there was a problem, compare file modification timestamps before and after writing
117 $timestampbeforewriting = filemtime($this->filename
);
119 $commandline = GETID3_HELPERAPPSDIR
.'metaflac.exe --remove-all-tags "'.$this->filename
.'" 2>&1';
120 $metaflacError = `
$commandline`
;
122 if (empty($metaflacError)) {
124 if ($timestampbeforewriting == filemtime($this->filename
)) {
125 $metaflacError = 'File modification timestamp has not changed - it looks like the tags were not deleted';
129 $metaflacError = 'metaflac.exe not found in '.GETID3_HELPERAPPSDIR
;
134 // It's simpler on *nix
135 $commandline = 'metaflac --remove-all-tags "'.$this->filename
.'" 2>&1';
136 $metaflacError = `
$commandline`
;
140 ignore_user_abort($oldignoreuserabort);
142 if (!empty($metaflacError)) {
143 $this->errors
[] = 'System call to metaflac failed with this message returned: '."\n\n".$metaflacError;
150 public function CleanmetaflacName($originalcommentname) {
151 // A case-insensitive field name that may consist of ASCII 0x20 through 0x7D, 0x3D ('=') excluded.
152 // ASCII 0x41 through 0x5A inclusive (A-Z) is to be considered equivalent to ASCII 0x61 through
153 // 0x7A inclusive (a-z).
155 // replace invalid chars with a space, return uppercase text
156 // Thanks Chris Bolt <chris-getid3Øbolt*cx> for improving this function
157 // note: *reg_replace() replaces nulls with empty string (not space)
158 return strtoupper(preg_replace('#[^ -<>-}]#', ' ', str_replace("\x00", ' ', $originalcommentname)));