123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
<?php
class getid3_write_metaflac
{
public $filename;
public $tag_data;
public $warnings = array();
public $errors = array();
private $pictures = array();
public function __construct() {
}
public function WriteMetaFLAC() {
if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
$this->errors[] = 'PHP running in Safe Mode (backtick operator not available) - cannot call metaflac, tags not written';
return false;
}
$tempfilenames = array();
if (!empty($this->tag_data['ATTACHED_PICTURE'])) {
foreach ($this->tag_data['ATTACHED_PICTURE'] as $key => $picturedetails) {
$temppicturefilename = tempnam(GETID3_TEMP_DIR, 'getID3');
$tempfilenames[] = $temppicturefilename;
if (getID3::is_writable($temppicturefilename) && is_file($temppicturefilename) && ($fpcomments = fopen($temppicturefilename, 'wb'))) {
fwrite($fpcomments, $picturedetails['data']);
fclose($fpcomments);
$picture_typeid = (!empty($picturedetails['picturetypeid']) ? $this->ID3v2toFLACpictureTypes($picturedetails['picturetypeid']) : 3);
$picture_mimetype = (!empty($picturedetails['mime']) ? $picturedetails['mime'] : '');
$picture_width_height_depth = '';
$this->pictures[] = $picture_typeid.'|'.$picture_mimetype.'|'.preg_replace('#[^\x20-\x7B\x7D-\x7F]#', '', (string) $picturedetails['description']).'|'.$picture_width_height_depth.'|'.$temppicturefilename;
} else {
$this->errors[] = 'failed to open temporary tags file, tags not written - fopen("'.$temppicturefilename.'", "wb")';
return false;
}
}
unset($this->tag_data['ATTACHED_PICTURE']);
}
$tempcommentsfilename = tempnam(GETID3_TEMP_DIR, 'getID3');
$tempfilenames[] = $tempcommentsfilename;
if (getID3::is_writable($tempcommentsfilename) && is_file($tempcommentsfilename) && ($fpcomments = fopen($tempcommentsfilename, 'wb'))) {
foreach ($this->tag_data as $key => $value) {
foreach ($value as $commentdata) {
fwrite($fpcomments, $this->CleanmetaflacName($key).'='.$commentdata."\n");
}
}
fclose($fpcomments);
} else {
$this->errors[] = 'failed to open temporary tags file, tags not written - fopen("'.$tempcommentsfilename.'", "wb")';
return false;
}
$oldignoreuserabort = ignore_user_abort(true);
if (GETID3_OS_ISWINDOWS) {
if (file_exists(GETID3_HELPERAPPSDIR.'metaflac.exe')) {
clearstatcache(true, $this->filename);
$timestampbeforewriting = filemtime($this->filename);
$commandline = GETID3_HELPERAPPSDIR.'metaflac.exe --no-utf8-convert --remove-all-tags --import-tags-from='.escapeshellarg($tempcommentsfilename);
foreach ($this->pictures as $picturecommand) {
$commandline .= ' --import-picture-from='.escapeshellarg($picturecommand);
}
$commandline .= ' '.escapeshellarg($this->filename).' 2>&1';
$metaflacError = `$commandline`;
if (empty($metaflacError)) {
clearstatcache(true, $this->filename);
if ($timestampbeforewriting == filemtime($this->filename)) {
$metaflacError = 'File modification timestamp has not changed - it looks like the tags were not written';
}
}
} else {
$metaflacError = 'metaflac.exe not found in '.GETID3_HELPERAPPSDIR;
}
} else {
$commandline = 'metaflac --no-utf8-convert --remove-all-tags --import-tags-from='.escapeshellarg($tempcommentsfilename);
foreach ($this->pictures as $picturecommand) {
$commandline .= ' --import-picture-from='.escapeshellarg($picturecommand);
}
$commandline .= ' '.escapeshellarg($this->filename).' 2>&1';
$metaflacError = `$commandline`;
}
foreach ($tempfilenames as $tempfilename) {
unlink($tempfilename);
}
ignore_user_abort($oldignoreuserabort);
if (!empty($metaflacError)) {
$this->errors[] = 'System call to metaflac failed with this message returned: '."\n\n".$metaflacError;
return false;
}
return true;
}
public function DeleteMetaFLAC() {
if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
$this->errors[] = 'PHP running in Safe Mode (backtick operator not available) - cannot call metaflac, tags not deleted';
return false;
}
$oldignoreuserabort = ignore_user_abort(true);
if (GETID3_OS_ISWINDOWS) {
if (file_exists(GETID3_HELPERAPPSDIR.'metaflac.exe')) {
clearstatcache(true, $this->filename);
$timestampbeforewriting = filemtime($this->filename);
$commandline = GETID3_HELPERAPPSDIR.'metaflac.exe --remove-all-tags "'.$this->filename.'" 2>&1';
$metaflacError = `$commandline`;
if (empty($metaflacError)) {
clearstatcache(true, $this->filename);
if ($timestampbeforewriting == filemtime($this->filename)) {
$metaflacError = 'File modification timestamp has not changed - it looks like the tags were not deleted';
}
}
} else {
$metaflacError = 'metaflac.exe not found in '.GETID3_HELPERAPPSDIR;
}
} else {
$commandline = 'metaflac --remove-all-tags "'.$this->filename.'" 2>&1';
$metaflacError = `$commandline`;
}
ignore_user_abort($oldignoreuserabort);
if (!empty($metaflacError)) {
$this->errors[] = 'System call to metaflac failed with this message returned: '."\n\n".$metaflacError;
return false;
}
return true;
}
public function ID3v2toFLACpictureTypes($id3v2_picture_typeid) {
return (($id3v2_picture_typeid <= 0x14) ? $id3v2_picture_typeid : 3);
}
public function CleanmetaflacName($originalcommentname) {
return strtoupper(preg_replace('#[^ -<>-}]#', ' ', str_replace("\x00", ' ', $originalcommentname)));
}
}